* ILParser.jay: Set int64's masquerading as int32 to upper and
[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                         DriverMain driver = new DriverMain (args);
28                         driver.Run ();
29                         return 0;
30                 }
31
32                 private class DriverMain {
33
34                         private ArrayList il_file_list;
35                         private string output_file;
36                         private Target target = Target.Exe;
37                         private bool show_tokens = false;
38                         private bool show_method_def = false;
39                         private bool show_method_ref = false;
40                         private bool show_parser = false;
41                         private bool scan_only = false;
42                         private CodeGen codegen;
43
44                         public DriverMain (string[] args)
45                         {
46                                 il_file_list = new ArrayList ();
47                                 ParseArgs (args);
48                         }
49
50                         public void Run ()
51                         {
52                                 if (il_file_list.Count == 0) {
53                                         Usage ();
54                                         return;
55                                 }
56                                 if (output_file == null)
57                                         output_file = CreateOutputFile ();
58                                 codegen = new CodeGen (output_file, target == Target.Dll, true);
59                                 foreach (string file_path in il_file_list)
60                                         ProcessFile (file_path);
61                                 if (scan_only)
62                                         return;
63
64                                 codegen.Write ();
65                         }
66
67                         private void ProcessFile (string file_path)
68                         {
69                                 if (!File.Exists (file_path)) {
70                                         Console.WriteLine ("File does not exist: {0}",
71                                                 file_path);
72                                         Environment.Exit (2);
73                                 }
74                                 StreamReader reader = File.OpenText (file_path);
75                                 ILTokenizer scanner = new ILTokenizer (reader);
76
77                                 if (show_tokens)
78                                         scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
79                                 //if (show_method_def)
80                                 //        MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
81                                 //if (show_method_ref)
82                                 //       MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
83
84                                 if (scan_only) {
85                                         ILToken tok;
86                                         while ((tok = scanner.NextToken) != ILToken.EOF) {
87                                                 Console.WriteLine (tok);
88                                         }
89                                         return;
90                                 }
91
92                                 ILParser parser = new ILParser (codegen);
93                                 if (show_parser)
94                                         parser.yyparse (new ScannerAdapter (scanner),  new yydebug.yyDebugSimple ());
95                                 else
96                                         parser.yyparse (new ScannerAdapter (scanner),  null);
97                         }
98
99                         public void ShowToken (object sender, NewTokenEventArgs args)
100                         {
101                                 Console.WriteLine ("token: '{0}'", args.Token);
102                         }
103                         /*
104                         public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
105                         {
106                                 Console.WriteLine ("***** Method defined *****");
107                                 Console.WriteLine ("-- signature:   {0}", args.Signature);
108                                 Console.WriteLine ("-- name:        {0}", args.Name);
109                                 Console.WriteLine ("-- return type: {0}", args.ReturnType);
110                                 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
111                                 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
112                                 Console.WriteLine ("-- impl atts:   {0}", args.ImplAttributes);
113                                 Console.WriteLine ("-- call conv:   {0}", args.CallConv);
114                         }
115
116                         public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
117                         {
118                                 Console.WriteLine ("***** Method referenced *****");
119                                 Console.WriteLine ("-- signature:   {0}", args.Signature);
120                                 Console.WriteLine ("-- name:        {0}", args.Name);
121                                 Console.WriteLine ("-- return type: {0}", args.ReturnType);
122                                 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
123                         }
124                         */
125                         private void ParseArgs (string[] args)
126                         {
127                                 string command_arg;
128                                 foreach (string str in args) {
129                                         if ((str[0] != '-') && (str[0] != '/')) {
130                                                 il_file_list.Add (str);
131                                                 continue;
132                                         }
133                                         switch (GetCommand (str, out command_arg)) {
134                                                 case "out":
135                                                         output_file = command_arg;
136                                                         break;
137                                                 case "exe":
138                                                         target = Target.Exe;
139                                                         break;
140                                                 case "dll":
141                                                         target = Target.Dll;
142                                                         break;
143                                                 case "scan_only":
144                                                         scan_only = true;
145                                                         break;
146                                                 case "show_tokens":
147                                                         show_tokens = true;
148                                                         break;
149                                                 case "show_method_def":
150                                                         show_method_def = true;
151                                                         break;
152                                                 case "show_method_ref":
153                                                         show_method_ref = true;
154                                                         break;
155                                                 case "show_parser":
156                                                         show_parser = true;
157                                                         break;
158                                                 case "-about":
159                                                         if (str[0] != '-')
160                                                                 break;
161                                                         About ();
162                                                         break;
163                                                 case "-version":
164                                                         if (str[0] != '-')
165                                                                 break;
166                                                         Version ();
167                                                         break;
168                                         }
169                                 }
170                         }
171
172                         private string GetCommand (string str, out string command_arg)
173                         {
174                                 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
175                                 string command = str.Substring (1,
176                                         end_index == -1 ? str.Length - 1 : end_index - 1);
177
178                                 if (end_index != -1) {
179                                         command_arg = str.Substring (end_index+1);
180                                 } else {
181                                         command_arg = null;
182                                 }
183
184                                 return command.ToLower ();
185                         }
186
187                         /// <summary>
188                         ///   Get the first file name and makes it into an output file name
189                         /// </summary>
190                         private string CreateOutputFile ()
191                         {
192                                 string file_name = (string)il_file_list[0];
193                                 int ext_index = file_name.LastIndexOf ('.');
194
195                                 if (ext_index == -1)
196                                         ext_index = file_name.Length;
197
198                                 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
199                                         target.ToString ().ToLower ());
200                         }
201
202                         private void Usage ()
203                         {
204                                 Console.WriteLine ("Mono ILasm compiler\n" +
205                                         "ilasm [options] source-files\n" +
206                                         "   --about            About the Mono ILasm compiler\n" +
207                                         "   --version          Print the version number of the Mono ILasm compiler\n" +
208                                         "   /out:file_name     Specifies output file.\n" +
209                                         "   /exe               Compile to executable.\n" +
210                                         "   /dll               Compile to library.\n" +
211                                         "Options can be of the form -option or /option\n");
212                         }
213
214                         private void About ()
215                         {
216                                 Console.WriteLine (
217                                         "For more information on Mono, visit the project Web site\n" +
218                                         "   http://www.go-mono.com\n\n");
219                                 Environment.Exit (0);
220                         }
221
222                         private void Version ()
223                         {
224                                 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
225                                 Console.WriteLine ("Mono ILasm compiler version {0}", version);
226                                 Environment.Exit (0);
227                         }
228
229                 }
230         }
231 }
232