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);
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)) {
156 output_file = command_arg;
160 target_string = "exe";
164 target_string = "dll";
169 // Stubs to stay commandline compatible with MS
188 case "show_method_def":
189 show_method_def = true;
191 case "show_method_ref":
192 show_method_ref = true;
211 private string GetCommand (string str, out string command_arg)
213 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
214 string command = str.Substring (1,
215 end_index == -1 ? str.Length - 1 : end_index - 1);
217 if (end_index != -1) {
218 command_arg = str.Substring (end_index+1);
223 return command.ToLower ();
227 /// Get the first file name and makes it into an output file name
229 private string CreateOutputFile ()
231 string file_name = (string)il_file_list[0];
232 int ext_index = file_name.LastIndexOf ('.');
235 ext_index = file_name.Length;
237 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
241 private void Usage ()
243 Console.WriteLine ("Mono ILasm compiler\n" +
244 "ilasm [options] source-files\n" +
245 " --about About the Mono ILasm compiler\n" +
246 " --version Print the version number of the Mono ILasm compiler\n" +
247 " /out:file_name Specifies output file.\n" +
248 " /exe Compile to executable.\n" +
249 " /dll Compile to library.\n" +
250 "Options can be of the form -option or /option\n");
251 Environment.Exit (1);
254 private void About ()
257 "For more information on Mono, visit the project Web site\n" +
258 " http://www.go-mono.com\n\n");
259 Environment.Exit (0);
262 private void Version ()
264 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
265 Console.WriteLine ("Mono ILasm compiler version {0}", version);
266 Environment.Exit (0);