2004-06-09 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / ilasm / Driver.cs
1 //
2 // Mono.ILASM.Driver
3 //    Main Command line interface for Mono ILasm Compiler
4 //
5 // Author(s):
6 //  Jackson Harper (Jackson@LatitudeGeo.com)
7 //
8 // (C) 2003 Jackson Harper, All rights reserved
9 //
10
11 using System;
12 using System.IO;
13 using System.Reflection;
14 using System.Collections;
15
16 namespace Mono.ILASM {
17
18         public class Driver {
19
20                 enum Target {
21                         Dll,
22                         Exe
23                 }
24
25                 public static int Main (string[] args)
26                 {
27                         // Do everything in Invariant
28                         System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
29
30                         DriverMain driver = new DriverMain (args);
31                         try {
32                                 driver.Run ();
33                         } catch (Exception e) {
34                                 Console.WriteLine (e);
35                                 Console.WriteLine ("Error while compiling.");
36                                 return 1;
37                         }
38                         Console.WriteLine ("Compilation succeeded");
39                         return 0;
40                 }
41
42                 private class DriverMain {
43
44                         private ArrayList il_file_list;
45                         private Report report;
46                         private string output_file;
47                         private Target target = Target.Exe;
48                         private string target_string = "exe";
49                         private bool quiet = false;
50                         private bool show_tokens = false;
51                         private bool show_method_def = false;
52                         private bool show_method_ref = false;
53                         private bool show_parser = false;
54                         private bool scan_only = false;
55                         private CodeGen codegen;
56
57                         public DriverMain (string[] args)
58                         {
59                                 il_file_list = new ArrayList ();
60                                 ParseArgs (args);
61                                 report = new Report (quiet);
62                         }
63
64                         public void Run ()
65                         {
66                                 try {
67                                         if (il_file_list.Count == 0)
68                                                 Usage ();
69                                         if (output_file == null)
70                                                 output_file = CreateOutputFile ();
71                                         codegen = new CodeGen (output_file, target == Target.Dll, true, report);
72                                         foreach (string file_path in il_file_list)
73                                                 ProcessFile (file_path);
74                                         if (scan_only)
75                                                 return;
76
77                                         codegen.Write ();
78                                 } catch {
79                                         throw;
80                                 }
81                         }
82
83                         private void ProcessFile (string file_path)
84                         {
85                                 if (!File.Exists (file_path)) {
86                                         Console.WriteLine ("File does not exist: {0}",
87                                                 file_path);
88                                         Environment.Exit (2);
89                                 }
90                                 report.AssembleFile (file_path, null,
91                                                 target_string, output_file);
92                                 StreamReader reader = File.OpenText (file_path);
93                                 ILTokenizer scanner = new ILTokenizer (reader);
94
95                                 if (show_tokens)
96                                         scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
97                                 //if (show_method_def)
98                                 //        MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
99                                 //if (show_method_ref)
100                                 //       MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
101
102                                 if (scan_only) {
103                                         ILToken tok;
104                                         while ((tok = scanner.NextToken) != ILToken.EOF) {
105                                                 Console.WriteLine (tok);
106                                         }
107                                         return;
108                                 }
109
110                                 ILParser parser = new ILParser (codegen, scanner);
111                                 try {
112                                         if (show_parser)
113                                                 parser.yyparse (new ScannerAdapter (scanner),
114                                                                 new yydebug.yyDebugSimple ());
115                                         else
116                                                 parser.yyparse (new ScannerAdapter (scanner),  null);
117                                 } catch {
118                                         Console.WriteLine ("Error at: " + scanner.Reader.Location);
119                                         throw;
120                                 }
121                         }
122
123                         public void ShowToken (object sender, NewTokenEventArgs args)
124                         {
125                                 Console.WriteLine ("token: '{0}'", args.Token);
126                         }
127                         /*
128                         public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
129                         {
130                                 Console.WriteLine ("***** Method defined *****");
131                                 Console.WriteLine ("-- signature:   {0}", args.Signature);
132                                 Console.WriteLine ("-- name:        {0}", args.Name);
133                                 Console.WriteLine ("-- return type: {0}", args.ReturnType);
134                                 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
135                                 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
136                                 Console.WriteLine ("-- impl atts:   {0}", args.ImplAttributes);
137                                 Console.WriteLine ("-- call conv:   {0}", args.CallConv);
138                         }
139
140                         public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
141                         {
142                                 Console.WriteLine ("***** Method referenced *****");
143                                 Console.WriteLine ("-- signature:   {0}", args.Signature);
144                                 Console.WriteLine ("-- name:        {0}", args.Name);
145                                 Console.WriteLine ("-- return type: {0}", args.ReturnType);
146                                 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
147                         }
148                         */
149                         private void ParseArgs (string[] args)
150                         {
151                                 string command_arg;
152                                 foreach (string str in args) {
153                                         if ((str[0] != '-') && (str[0] != '/')) {
154                                                 il_file_list.Add (str);
155                                                 continue;
156                                         }
157                                         switch (GetCommand (str, out command_arg)) {
158                                         case "out":
159                                         case "output":
160                                                 output_file = command_arg;
161                                                 break;
162                                         case "exe":
163                                                 target = Target.Exe;
164                                                 target_string = "exe";
165                                                 break;
166                                         case "dll":
167                                                 target = Target.Dll;
168                                                 target_string = "dll";
169                                                 break;
170                                         case "quiet":
171                                                 quiet = true;
172                                                 break;
173                                         // Stubs to stay commandline compatible with MS 
174                                         case "listing":
175                                         case "nologo":
176                                         case "debug":
177                                         case "clock":
178                                         case "error":
179                                         case "subsystem":
180                                         case "flags":
181                                         case "alignment":
182                                         case "base":
183                                         case "key":
184                                         case "resource":
185                                                 break;
186                                         case "scan_only":
187                                                 scan_only = true;
188                                                 break;
189                                         case "show_tokens":
190                                                 show_tokens = true;
191                                                 break;
192                                         case "show_method_def":
193                                                 show_method_def = true;
194                                                 break;
195                                         case "show_method_ref":
196                                                 show_method_ref = true;
197                                                 break;
198                                         case "show_parser":
199                                                 show_parser = true;
200                                                 break;
201                                         case "-about":
202                                                 if (str[0] != '-')
203                                                         break;
204                                                 About ();
205                                                 break;
206                                         case "-version":
207                                                 if (str[0] != '-')
208                                                         break;
209                                                 Version ();
210                                                 break;
211                                         default:
212                                                 if (str [0] == '-')
213                                                         break;
214                                                 il_file_list.Add (str);
215                                                 break;
216                                         }
217                                 }
218                         }
219
220                         private string GetCommand (string str, out string command_arg)
221                         {
222                                 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
223                                 string command = str.Substring (1,
224                                         end_index == -1 ? str.Length - 1 : end_index - 1);
225
226                                 if (end_index != -1) {
227                                         command_arg = str.Substring (end_index+1);
228                                 } else {
229                                         command_arg = null;
230                                 }
231
232                                 return command.ToLower ();
233                         }
234
235                         /// <summary>
236                         ///   Get the first file name and makes it into an output file name
237                         /// </summary>
238                         private string CreateOutputFile ()
239                         {
240                                 string file_name = (string)il_file_list[0];
241                                 int ext_index = file_name.LastIndexOf ('.');
242
243                                 if (ext_index == -1)
244                                         ext_index = file_name.Length;
245
246                                 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
247                                         target_string);
248                         }
249
250                         private void Usage ()
251                         {
252                                 Console.WriteLine ("Mono ILasm compiler\n" +
253                                         "ilasm [options] source-files\n" +
254                                         "   --about            About the Mono ILasm compiler\n" +
255                                         "   --version          Print the version number of the Mono ILasm compiler\n" +
256                                         "   /output:file_name  Specifies output file.\n" +
257                                         "   /exe               Compile to executable.\n" +
258                                         "   /dll               Compile to library.\n" +
259                                         "Options can be of the form -option or /option\n");
260                                 Environment.Exit (1);
261                         }
262
263                         private void About ()
264                         {
265                                 Console.WriteLine (
266                                         "For more information on Mono, visit the project Web site\n" +
267                                         "   http://www.go-mono.com\n\n");
268                                 Environment.Exit (0);
269                         }
270
271                         private void Version ()
272                         {
273                                 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
274                                 Console.WriteLine ("Mono ILasm compiler version {0}", version);
275                                 Environment.Exit (0);
276                         }
277
278                 }
279         }
280 }
281