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 string output_file;
43 private Target target = Target.Exe;
44 private bool show_tokens = false;
45 private bool show_method_def = false;
46 private bool show_method_ref = false;
47 private bool show_parser = false;
48 private bool scan_only = false;
49 private bool delete_on_error = false;
50 private CodeGen codegen;
52 public DriverMain (string[] args)
54 il_file_list = new ArrayList ();
61 if (il_file_list.Count == 0)
63 if (output_file == null)
64 output_file = CreateOutputFile ();
65 codegen = new CodeGen (output_file, target == Target.Dll, true);
66 foreach (string file_path in il_file_list)
67 ProcessFile (file_path);
73 if (delete_on_error) {
74 if (File.Exists (output_file))
75 File.Delete (output_file);
81 private void ProcessFile (string file_path)
83 if (!File.Exists (file_path)) {
84 Console.WriteLine ("File does not exist: {0}",
88 StreamReader reader = File.OpenText (file_path);
89 ILTokenizer scanner = new ILTokenizer (reader);
92 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
93 //if (show_method_def)
94 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
95 //if (show_method_ref)
96 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
100 while ((tok = scanner.NextToken) != ILToken.EOF) {
101 Console.WriteLine (tok);
106 ILParser parser = new ILParser (codegen);
108 parser.yyparse (new ScannerAdapter (scanner), new yydebug.yyDebugSimple ());
110 parser.yyparse (new ScannerAdapter (scanner), null);
113 public void ShowToken (object sender, NewTokenEventArgs args)
115 Console.WriteLine ("token: '{0}'", args.Token);
118 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
120 Console.WriteLine ("***** Method defined *****");
121 Console.WriteLine ("-- signature: {0}", args.Signature);
122 Console.WriteLine ("-- name: {0}", args.Name);
123 Console.WriteLine ("-- return type: {0}", args.ReturnType);
124 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
125 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
126 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
127 Console.WriteLine ("-- call conv: {0}", args.CallConv);
130 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
132 Console.WriteLine ("***** Method referenced *****");
133 Console.WriteLine ("-- signature: {0}", args.Signature);
134 Console.WriteLine ("-- name: {0}", args.Name);
135 Console.WriteLine ("-- return type: {0}", args.ReturnType);
136 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
139 private void ParseArgs (string[] args)
142 foreach (string str in args) {
143 if ((str[0] != '-') && (str[0] != '/')) {
144 il_file_list.Add (str);
147 switch (GetCommand (str, out command_arg)) {
149 output_file = command_arg;
163 case "show_method_def":
164 show_method_def = true;
166 case "show_method_ref":
167 show_method_ref = true;
172 case "delete_on_error":
174 delete_on_error = true;
190 private string GetCommand (string str, out string command_arg)
192 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
193 string command = str.Substring (1,
194 end_index == -1 ? str.Length - 1 : end_index - 1);
196 if (end_index != -1) {
197 command_arg = str.Substring (end_index+1);
202 return command.ToLower ();
206 /// Get the first file name and makes it into an output file name
208 private string CreateOutputFile ()
210 string file_name = (string)il_file_list[0];
211 int ext_index = file_name.LastIndexOf ('.');
214 ext_index = file_name.Length;
216 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
217 target.ToString ().ToLower ());
220 private void Usage ()
222 Console.WriteLine ("Mono ILasm compiler\n" +
223 "ilasm [options] source-files\n" +
224 " --about About the Mono ILasm compiler\n" +
225 " --version Print the version number of the Mono ILasm compiler\n" +
226 " /out:file_name Specifies output file.\n" +
227 " /exe Compile to executable.\n" +
228 " /dll Compile to library.\n" +
229 "Options can be of the form -option or /option\n");
230 Environment.Exit (1);
233 private void About ()
236 "For more information on Mono, visit the project Web site\n" +
237 " http://www.go-mono.com\n\n");
238 Environment.Exit (0);
241 private void Version ()
243 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
244 Console.WriteLine ("Mono ILasm compiler version {0}", version);
245 Environment.Exit (0);