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 Console.WriteLine ("***** FAILURE *****");
36 Console.WriteLine ("Operation completed successfully");
40 private class DriverMain {
42 private ArrayList il_file_list;
43 private Report report;
44 private string output_file;
45 private Target target = Target.Exe;
46 private string target_string = "exe";
47 private bool quiet = false;
48 private bool show_tokens = false;
49 private bool show_method_def = false;
50 private bool show_method_ref = false;
51 private bool show_parser = false;
52 private bool scan_only = false;
53 private bool debugging_info = false;
54 private CodeGen codegen;
56 public DriverMain (string[] args)
58 il_file_list = new ArrayList ();
60 report = new Report (quiet);
65 if (il_file_list.Count == 0)
67 if (output_file == null)
68 output_file = CreateOutputFile ();
69 codegen = new CodeGen (output_file, target == Target.Dll, true, debugging_info, report);
70 foreach (string file_path in il_file_list)
71 ProcessFile (file_path);
75 if (report.ErrorCount > 0)
81 private void ProcessFile (string file_path)
83 if (!File.Exists (file_path)) {
84 Console.WriteLine ("File does not exist: {0}",
88 report.AssembleFile (file_path, null,
89 target_string, output_file);
90 StreamReader reader = File.OpenText (file_path);
91 ILTokenizer scanner = new ILTokenizer (reader);
94 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
95 //if (show_method_def)
96 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
97 //if (show_method_ref)
98 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
102 while ((tok = scanner.NextToken) != ILToken.EOF) {
103 Console.WriteLine (tok);
108 ILParser parser = new ILParser (codegen, scanner);
109 codegen.BeginSourceFile (file_path);
112 parser.yyparse (new ScannerAdapter (scanner),
113 new yydebug.yyDebugSimple ());
115 parser.yyparse (new ScannerAdapter (scanner), null);
116 } catch (ILTokenizingException ilte) {
117 report.Error (file_path + "(" + ilte.Location.line + ") : error : " +
118 "syntax error at token '" + ilte.Token + "'.");
120 Console.WriteLine ("Error at: " + scanner.Reader.Location);
123 codegen.EndSourceFile ();
127 public void ShowToken (object sender, NewTokenEventArgs args)
129 Console.WriteLine ("token: '{0}'", args.Token);
132 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
134 Console.WriteLine ("***** Method defined *****");
135 Console.WriteLine ("-- signature: {0}", args.Signature);
136 Console.WriteLine ("-- name: {0}", args.Name);
137 Console.WriteLine ("-- return type: {0}", args.ReturnType);
138 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
139 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
140 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
141 Console.WriteLine ("-- call conv: {0}", args.CallConv);
144 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
146 Console.WriteLine ("***** Method referenced *****");
147 Console.WriteLine ("-- signature: {0}", args.Signature);
148 Console.WriteLine ("-- name: {0}", args.Name);
149 Console.WriteLine ("-- return type: {0}", args.ReturnType);
150 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
153 private void ParseArgs (string[] args)
156 foreach (string str in args) {
157 if ((str[0] != '-') && (str[0] != '/')) {
158 il_file_list.Add (str);
161 switch (GetCommand (str, out command_arg)) {
164 output_file = command_arg;
168 target_string = "exe";
172 target_string = "dll";
181 debugging_info = true;
183 // Stubs to stay commandline compatible with MS
201 case "show_method_def":
202 show_method_def = true;
204 case "show_method_ref":
205 show_method_ref = true;
223 il_file_list.Add (str);
229 private string GetCommand (string str, out string command_arg)
231 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
232 string command = str.Substring (1,
233 end_index == -1 ? str.Length - 1 : end_index - 1);
235 if (end_index != -1) {
236 command_arg = str.Substring (end_index+1);
241 return command.ToLower ();
245 /// Get the first file name and makes it into an output file name
247 private string CreateOutputFile ()
249 string file_name = (string)il_file_list[0];
250 int ext_index = file_name.LastIndexOf ('.');
253 ext_index = file_name.Length;
255 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
259 private void Usage ()
261 Console.WriteLine ("Mono ILasm compiler\n" +
262 "ilasm [options] source-files\n" +
263 " --about About the Mono ILasm compiler\n" +
264 " --version Print the version number of the Mono ILasm compiler\n" +
265 " /output:file_name Specifies output file.\n" +
266 " /exe Compile to executable.\n" +
267 " /dll Compile to library.\n" +
268 " /debug Include debug information.\n" +
269 "Options can be of the form -option or /option\n");
270 Environment.Exit (1);
273 private void About ()
276 "For more information on Mono, visit the project Web site\n" +
277 " http://www.go-mono.com\n\n");
278 Environment.Exit (0);
281 private void Version ()
283 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
284 Console.WriteLine ("Mono ILasm compiler version {0}", version);
285 Environment.Exit (0);