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 // Do everything in Invariant
28 System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
30 DriverMain driver = new DriverMain (args);
33 } catch (Exception e) {
34 Console.WriteLine (e);
35 Console.WriteLine ("Error while compiling.");
38 Console.WriteLine ("Compilation succeeded");
42 private class DriverMain {
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;
57 public DriverMain (string[] args)
59 il_file_list = new ArrayList ();
61 report = new Report (quiet);
67 if (il_file_list.Count == 0)
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);
83 private void ProcessFile (string file_path)
85 if (!File.Exists (file_path)) {
86 Console.WriteLine ("File does not exist: {0}",
90 report.AssembleFile (file_path, null,
91 target_string, output_file);
92 StreamReader reader = File.OpenText (file_path);
93 ILTokenizer scanner = new ILTokenizer (reader);
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);
104 while ((tok = scanner.NextToken) != ILToken.EOF) {
105 Console.WriteLine (tok);
110 ILParser parser = new ILParser (codegen, scanner);
113 parser.yyparse (new ScannerAdapter (scanner),
114 new yydebug.yyDebugSimple ());
116 parser.yyparse (new ScannerAdapter (scanner), null);
118 Console.WriteLine ("Error at: " + scanner.Reader.Location);
123 public void ShowToken (object sender, NewTokenEventArgs args)
125 Console.WriteLine ("token: '{0}'", args.Token);
128 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
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);
140 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
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);
149 private void ParseArgs (string[] args)
152 foreach (string str in args) {
153 if ((str[0] != '-') && (str[0] != '/')) {
154 il_file_list.Add (str);
157 switch (GetCommand (str, out command_arg)) {
160 output_file = command_arg;
164 target_string = "exe";
168 target_string = "dll";
173 // Stubs to stay commandline compatible with MS
192 case "show_method_def":
193 show_method_def = true;
195 case "show_method_ref":
196 show_method_ref = true;
214 il_file_list.Add (str);
220 private string GetCommand (string str, out string command_arg)
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);
226 if (end_index != -1) {
227 command_arg = str.Substring (end_index+1);
232 return command.ToLower ();
236 /// Get the first file name and makes it into an output file name
238 private string CreateOutputFile ()
240 string file_name = (string)il_file_list[0];
241 int ext_index = file_name.LastIndexOf ('.');
244 ext_index = file_name.Length;
246 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
250 private void Usage ()
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);
263 private void About ()
266 "For more information on Mono, visit the project Web site\n" +
267 " http://www.go-mono.com\n\n");
268 Environment.Exit (0);
271 private void Version ()
273 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
274 Console.WriteLine ("Mono ILasm compiler version {0}", version);
275 Environment.Exit (0);