3 // Main Command line interface for Mono ILasm Compiler
6 // Jackson Harper (Jackson@LatitudeGeo.com)
8 // (C) 2003 Jackson Harper, All rights reserved
13 using System.Reflection;
14 using System.Collections;
16 namespace Mono.ILASM {
25 public static int Main (string[] args)
27 DriverMain driver = new DriverMain (args);
30 } catch (Exception e) {
31 Console.WriteLine (e);
32 Console.WriteLine ("Error while compiling.");
35 Console.WriteLine ("Compilation succeeded");
39 private class DriverMain {
41 private ArrayList il_file_list;
42 private Report report;
43 private string output_file;
44 private Target target = Target.Exe;
45 private string target_string = "exe";
46 private bool quiet = false;
47 private bool show_tokens = false;
48 private bool show_method_def = false;
49 private bool show_method_ref = false;
50 private bool show_parser = false;
51 private bool scan_only = false;
52 private CodeGen codegen;
54 public DriverMain (string[] args)
56 il_file_list = new ArrayList ();
58 report = new Report (quiet);
64 if (il_file_list.Count == 0)
66 if (output_file == null)
67 output_file = CreateOutputFile ();
68 codegen = new CodeGen (output_file, target == Target.Dll, true, report);
69 foreach (string file_path in il_file_list)
70 ProcessFile (file_path);
80 private void ProcessFile (string file_path)
82 if (!File.Exists (file_path)) {
83 Console.WriteLine ("File does not exist: {0}",
87 report.AssembleFile (file_path, null,
88 target_string, output_file);
89 StreamReader reader = File.OpenText (file_path);
90 ILTokenizer scanner = new ILTokenizer (reader);
93 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
94 //if (show_method_def)
95 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
96 //if (show_method_ref)
97 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
101 while ((tok = scanner.NextToken) != ILToken.EOF) {
102 Console.WriteLine (tok);
107 ILParser parser = new ILParser (codegen, scanner);
110 parser.yyparse (new ScannerAdapter (scanner),
111 new yydebug.yyDebugSimple ());
113 parser.yyparse (new ScannerAdapter (scanner), null);
115 Console.WriteLine ("Error at: " + scanner.Reader.Location);
120 public void ShowToken (object sender, NewTokenEventArgs args)
122 Console.WriteLine ("token: '{0}'", args.Token);
125 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
127 Console.WriteLine ("***** Method defined *****");
128 Console.WriteLine ("-- signature: {0}", args.Signature);
129 Console.WriteLine ("-- name: {0}", args.Name);
130 Console.WriteLine ("-- return type: {0}", args.ReturnType);
131 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
132 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
133 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
134 Console.WriteLine ("-- call conv: {0}", args.CallConv);
137 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
139 Console.WriteLine ("***** Method referenced *****");
140 Console.WriteLine ("-- signature: {0}", args.Signature);
141 Console.WriteLine ("-- name: {0}", args.Name);
142 Console.WriteLine ("-- return type: {0}", args.ReturnType);
143 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
146 private void ParseArgs (string[] args)
149 foreach (string str in args) {
150 if ((str[0] != '-') && (str[0] != '/')) {
151 il_file_list.Add (str);
154 switch (GetCommand (str, out command_arg)) {
157 output_file = command_arg;
161 target_string = "exe";
165 target_string = "dll";
170 // Stubs to stay commandline compatible with MS
189 case "show_method_def":
190 show_method_def = true;
192 case "show_method_ref":
193 show_method_ref = true;
211 il_file_list.Add (str);
217 private string GetCommand (string str, out string command_arg)
219 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
220 string command = str.Substring (1,
221 end_index == -1 ? str.Length - 1 : end_index - 1);
223 if (end_index != -1) {
224 command_arg = str.Substring (end_index+1);
229 return command.ToLower ();
233 /// Get the first file name and makes it into an output file name
235 private string CreateOutputFile ()
237 string file_name = (string)il_file_list[0];
238 int ext_index = file_name.LastIndexOf ('.');
241 ext_index = file_name.Length;
243 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
247 private void Usage ()
249 Console.WriteLine ("Mono ILasm compiler\n" +
250 "ilasm [options] source-files\n" +
251 " --about About the Mono ILasm compiler\n" +
252 " --version Print the version number of the Mono ILasm compiler\n" +
253 " /output:file_name Specifies output file.\n" +
254 " /exe Compile to executable.\n" +
255 " /dll Compile to library.\n" +
256 "Options can be of the form -option or /option\n");
257 Environment.Exit (1);
260 private void About ()
263 "For more information on Mono, visit the project Web site\n" +
264 " http://www.go-mono.com\n\n");
265 Environment.Exit (0);
268 private void Version ()
270 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
271 Console.WriteLine ("Mono ILasm compiler version {0}", version);
272 Environment.Exit (0);