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 scan_only = false;
41 private CodeGen codegen;
43 public DriverMain (string[] args)
45 il_file_list = new ArrayList ();
51 if (il_file_list.Count == 0) {
55 if (output_file == null)
56 output_file = CreateOutputFile ();
57 codegen = new CodeGen (output_file, target == Target.Dll, true);
58 foreach (string file_path in il_file_list)
59 ProcessFile (file_path);
62 codegen.ClassTable.CheckForUndefined ();
63 codegen.PEFile.WritePEFile ();
66 private void ProcessFile (string file_path)
68 if (!File.Exists (file_path)) {
69 Console.WriteLine ("File does not exist: {0}",
73 StreamReader reader = File.OpenText (file_path);
74 ILTokenizer scanner = new ILTokenizer (reader);
77 scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
79 MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
81 MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
85 while ((tok = scanner.NextToken) != ILToken.EOF) {
86 Console.WriteLine (tok);
91 ILParser parser = new ILParser (codegen);
92 parser.yyparse (new ScannerAdapter (scanner), null);
95 public void ShowToken (object sender, NewTokenEventArgs args)
97 Console.WriteLine ("token: '{0}'", args.Token);
100 public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
102 Console.WriteLine ("***** Method defined *****");
103 Console.WriteLine ("-- signature: {0}", args.Signature);
104 Console.WriteLine ("-- name: {0}", args.Name);
105 Console.WriteLine ("-- return type: {0}", args.ReturnType);
106 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
107 Console.WriteLine ("-- method atts: {0}", args.MethodAttributes);
108 Console.WriteLine ("-- impl atts: {0}", args.ImplAttributes);
109 Console.WriteLine ("-- call conv: {0}", args.CallConv);
112 public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
114 Console.WriteLine ("***** Method referenced *****");
115 Console.WriteLine ("-- signature: {0}", args.Signature);
116 Console.WriteLine ("-- name: {0}", args.Name);
117 Console.WriteLine ("-- return type: {0}", args.ReturnType);
118 Console.WriteLine ("-- is in table: {0}", args.IsInTable);
121 private void ParseArgs (string[] args)
124 foreach (string str in args) {
125 if ((str[0] != '-') && (str[0] != '/')) {
126 il_file_list.Add (str);
129 switch (GetCommand (str, out command_arg)) {
131 output_file = command_arg;
145 case "show_method_def":
146 show_method_def = true;
148 case "show_method_ref":
149 show_method_ref = true;
165 private string GetCommand (string str, out string command_arg)
167 int end_index = str.IndexOfAny (new char[] {':', '='}, 1);
168 string command = str.Substring (1,
169 end_index == -1 ? str.Length - 1 : end_index - 1);
171 if (end_index != -1) {
172 command_arg = str.Substring (end_index+1);
177 return command.ToLower ();
181 /// Get the first file name and makes it into an output file name
183 private string CreateOutputFile ()
185 string file_name = (string)il_file_list[0];
186 int ext_index = file_name.LastIndexOf ('.');
189 ext_index = file_name.Length;
191 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index),
192 target.ToString ().ToLower ());
195 private void Usage ()
197 Console.WriteLine ("Mono ILasm compiler\n" +
198 "ilasm [options] source-files\n" +
199 " --about About the Mono ILasm compiler\n" +
200 " --version Print the version number of the Mono ILasm compiler\n" +
201 " /out:file_name Specifies output file.\n" +
202 " /exe Compile to executable.\n" +
203 " /dll Compile to library.\n" +
204 "Options can be of the form -option or /option\n");
207 private void About ()
210 "For more information on Mono, visit the project Web site\n" +
211 " http://www.go-mono.com\n\n");
212 Environment.Exit (0);
215 private void Version ()
217 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
218 Console.WriteLine ("Mono ILasm compiler version {0}", version);
219 Environment.Exit (0);