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);
32 private class DriverMain {
34 private ArrayList il_file_list;
35 private string output_file;
36 private Target target = Target.Exe;
37 private bool show_tokens = false;
38 private bool show_method_def = false;
39 private bool show_method_ref = false;
40 private bool show_parser = false;
41 private bool scan_only = false;
42 private CodeGen codegen;
44 public DriverMain (string[] args)
46 il_file_list = new ArrayList ();
52 if (il_file_list.Count == 0) {
56 if (output_file == null)
57 output_file = CreateOutputFile ();
58 codegen = new CodeGen (output_file, target == Target.Dll, true);
59 foreach (string file_path in il_file_list)
60 ProcessFile (file_path);
67 private void ProcessFile (string file_path)
69 if (!File.Exists (file_path)) {
70 Console.WriteLine ("File does not exist: {0}",
74 StreamReader reader = File.OpenText (file_path);
75 ILTokenizer scanner = new ILTokenizer (reader);
78 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
79 //if (show_method_def)
80 // MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
81 //if (show_method_ref)
82 // MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
86 while ((tok = scanner.NextToken) != ILToken.EOF) {
87 Console.WriteLine (tok);
92 ILParser parser = new ILParser (codegen);
94 parser.yyparse (new ScannerAdapter (scanner), new yydebug.yyDebugSimple ());
96 parser.yyparse (new ScannerAdapter (scanner), null);
99 public void ShowToken (object sender, NewTokenEventArgs args)
101 Console.WriteLine ("token: '{0}'", args.Token);
104 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
106 Console.WriteLine ("***** Method defined *****");
107 Console.WriteLine ("-- signature: {0}", args.Signature);
108 Console.WriteLine ("-- name: {0}", args.Name);
109 Console.WriteLine ("-- return type: {0}", args.ReturnType);
110 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
111 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
112 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
113 Console.WriteLine ("-- call conv: {0}", args.CallConv);
116 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
118 Console.WriteLine ("***** Method referenced *****");
119 Console.WriteLine ("-- signature: {0}", args.Signature);
120 Console.WriteLine ("-- name: {0}", args.Name);
121 Console.WriteLine ("-- return type: {0}", args.ReturnType);
122 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
125 private void ParseArgs (string[] args)
128 foreach (string str in args) {
129 if ((str[0] != '-') && (str[0] != '/')) {
130 il_file_list.Add (str);
133 switch (GetCommand (str, out command_arg)) {
135 output_file = command_arg;
149 case "show_method_def":
150 show_method_def = true;
152 case "show_method_ref":
153 show_method_ref = true;
172 private string GetCommand (string str, out string command_arg)
174 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
175 string command = str.Substring (1,
176 end_index == -1 ? str.Length - 1 : end_index - 1);
178 if (end_index != -1) {
179 command_arg = str.Substring (end_index+1);
184 return command.ToLower ();
188 /// Get the first file name and makes it into an output file name
190 private string CreateOutputFile ()
192 string file_name = (string)il_file_list[0];
193 int ext_index = file_name.LastIndexOf ('.');
196 ext_index = file_name.Length;
198 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
199 target.ToString ().ToLower ());
202 private void Usage ()
204 Console.WriteLine ("Mono ILasm compiler\n" +
205 "ilasm [options] source-files\n" +
206 " --about About the Mono ILasm compiler\n" +
207 " --version Print the version number of the Mono ILasm compiler\n" +
208 " /out:file_name Specifies output file.\n" +
209 " /exe Compile to executable.\n" +
210 " /dll Compile to library.\n" +
211 "Options can be of the form -option or /option\n");
214 private void About ()
217 "For more information on Mono, visit the project Web site\n" +
218 " http://www.go-mono.com\n\n");
219 Environment.Exit (0);
222 private void Version ()
224 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
225 Console.WriteLine ("Mono ILasm compiler version {0}", version);
226 Environment.Exit (0);