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);
34 Console.WriteLine ("***** FAILURE *****");
37 } catch (Exception e) {
38 Console.WriteLine (e);
39 Console.WriteLine ("Error while compiling.");
42 Console.WriteLine ("Operation completed successfully");
46 private class DriverMain {
48 private ArrayList il_file_list;
49 private Report report;
50 private string output_file;
51 private Target target = Target.Exe;
52 private string target_string = "exe";
53 private bool quiet = false;
54 private bool show_tokens = false;
55 private bool show_method_def = false;
56 private bool show_method_ref = false;
57 private bool show_parser = false;
58 private bool scan_only = false;
59 private bool debugging_info = false;
60 private CodeGen codegen;
62 public DriverMain (string[] args)
64 il_file_list = new ArrayList ();
66 report = new Report (quiet);
72 if (il_file_list.Count == 0)
74 if (output_file == null)
75 output_file = CreateOutputFile ();
76 codegen = new CodeGen (output_file, target == Target.Dll, true, debugging_info, report);
77 foreach (string file_path in il_file_list)
78 ProcessFile (file_path);
82 if (report.ErrorCount > 0)
92 private void ProcessFile (string file_path)
94 if (!File.Exists (file_path)) {
95 Console.WriteLine ("File does not exist: {0}",
99 report.AssembleFile (file_path, null,
100 target_string, output_file);
101 StreamReader reader = File.OpenText (file_path);
102 ILTokenizer scanner = new ILTokenizer (reader);
105 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
106 //if (show_method_def)
107 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
108 //if (show_method_ref)
109 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
113 while ((tok = scanner.NextToken) != ILToken.EOF) {
114 Console.WriteLine (tok);
119 ILParser parser = new ILParser (codegen, scanner);
120 codegen.BeginSourceFile (file_path);
123 parser.yyparse (new ScannerAdapter (scanner),
124 new yydebug.yyDebugSimple ());
126 parser.yyparse (new ScannerAdapter (scanner), null);
127 } catch (ILTokenizingException ilte) {
128 report.Error (file_path + "(" + ilte.Location.line + ") : error : " +
129 "syntax error at token '" + ilte.Token + "'.");
131 Console.WriteLine ("Error at: " + scanner.Reader.Location);
134 codegen.EndSourceFile ();
138 public void ShowToken (object sender, NewTokenEventArgs args)
140 Console.WriteLine ("token: '{0}'", args.Token);
143 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
145 Console.WriteLine ("***** Method defined *****");
146 Console.WriteLine ("-- signature: {0}", args.Signature);
147 Console.WriteLine ("-- name: {0}", args.Name);
148 Console.WriteLine ("-- return type: {0}", args.ReturnType);
149 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
150 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
151 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
152 Console.WriteLine ("-- call conv: {0}", args.CallConv);
155 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
157 Console.WriteLine ("***** Method referenced *****");
158 Console.WriteLine ("-- signature: {0}", args.Signature);
159 Console.WriteLine ("-- name: {0}", args.Name);
160 Console.WriteLine ("-- return type: {0}", args.ReturnType);
161 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
164 private void ParseArgs (string[] args)
167 foreach (string str in args) {
168 if ((str[0] != '-') && (str[0] != '/')) {
169 il_file_list.Add (str);
172 switch (GetCommand (str, out command_arg)) {
175 output_file = command_arg;
179 target_string = "exe";
183 target_string = "dll";
192 debugging_info = true;
194 // Stubs to stay commandline compatible with MS
212 case "show_method_def":
213 show_method_def = true;
215 case "show_method_ref":
216 show_method_ref = true;
234 il_file_list.Add (str);
240 private string GetCommand (string str, out string command_arg)
242 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
243 string command = str.Substring (1,
244 end_index == -1 ? str.Length - 1 : end_index - 1);
246 if (end_index != -1) {
247 command_arg = str.Substring (end_index+1);
252 return command.ToLower ();
256 /// Get the first file name and makes it into an output file name
258 private string CreateOutputFile ()
260 string file_name = (string)il_file_list[0];
261 int ext_index = file_name.LastIndexOf ('.');
264 ext_index = file_name.Length;
266 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
270 private void Usage ()
272 Console.WriteLine ("Mono ILasm compiler\n" +
273 "ilasm [options] source-files\n" +
274 " --about About the Mono ILasm compiler\n" +
275 " --version Print the version number of the Mono ILasm compiler\n" +
276 " /output:file_name Specifies output file.\n" +
277 " /exe Compile to executable.\n" +
278 " /dll Compile to library.\n" +
279 " /debug Include debug information.\n" +
280 "Options can be of the form -option or /option\n");
281 Environment.Exit (1);
284 private void About ()
287 "For more information on Mono, visit the project Web site\n" +
288 " http://www.go-mono.com\n\n");
289 Environment.Exit (0);
292 private void Version ()
294 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
295 Console.WriteLine ("Mono ILasm compiler version {0}", version);
296 Environment.Exit (0);