2003-03-26 Ville Palo <vi64pa@kolumbus.fi>
[mono.git] / mcs / ilasm / Driver.cs
1 //
2 // Mono.ILASM.Driver
3 //    Main Command line interface for Mono ILasm Compiler
4 //
5 // Author(s):
6 //  Jackson Harper (Jackson@LatitudeGeo.com)
7 //
8 // (C) 2003 Jackson Harper, All rights reserved
9 //
10
11 using System;
12 using System.IO;
13 using System.Reflection;
14 using System.Collections;
15
16 namespace Mono.ILASM {
17
18         public class Driver {
19
20                 enum Target {
21                         Dll, 
22                         Exe
23                 }
24
25                 public static int Main (string[] args)
26                 {
27                         DriverMain driver = new DriverMain (args);
28                         driver.Run ();
29                         return 0;
30                 }
31
32                 private class DriverMain {
33                         
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;
42
43                         public DriverMain (string[] args)
44                         {
45                                 il_file_list = new ArrayList ();
46                                 ParseArgs (args);
47                         }
48                         
49                         public void Run () 
50                         {
51                                 if (il_file_list.Count == 0) {
52                                         Usage ();
53                                         return;
54                                 }
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);
60                                 if (scan_only)
61                                         return;
62                                 codegen.ClassTable.CheckForUndefined ();
63                                 codegen.PEFile.WritePEFile ();
64                         }
65
66                         private void ProcessFile (string file_path)
67                         {
68                                 if (!File.Exists (file_path)) {
69                                         Console.WriteLine ("File does not exist: {0}",
70                                                 file_path);
71                                         Environment.Exit (2);
72                                 }
73                                 StreamReader reader = File.OpenText (file_path);
74                                 ILTokenizer scanner = new ILTokenizer (reader);
75
76                                 if (show_tokens)
77                                         scanner.NewTokenEvent += new NewTokenEvent (ShowToken);
78                                 if (show_method_def)
79                                         MethodTable.MethodDefinedEvent += new MethodDefinedEvent (ShowMethodDef);
80                                 if (show_method_ref)
81                                         MethodTable.MethodReferencedEvent += new MethodReferencedEvent (ShowMethodRef);
82
83                                 if (scan_only) {
84                                         ILToken tok;
85                                         while ((tok = scanner.NextToken) != ILToken.EOF) {
86                                                 Console.WriteLine (tok);
87                                         }
88                                         return;
89                                 }
90
91                                 ILParser parser = new ILParser (codegen);
92                                 parser.yyparse (new ScannerAdapter (scanner), null);
93                         }
94
95                         public void ShowToken (object sender, NewTokenEventArgs args)
96                         {
97                                 Console.WriteLine ("token: '{0}'", args.Token);
98                         }
99
100                         public void ShowMethodDef (object sender, MethodDefinedEventArgs args)
101                         {
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);
110                         }
111
112                         public void ShowMethodRef (object sender, MethodReferencedEventArgs args)
113                         {
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);
119                         }
120
121                         private void ParseArgs (string[] args)
122                         {
123                                 string command_arg;
124                                 foreach (string str in args) {
125                                         if ((str[0] != '-') && (str[0] != '/')) {
126                                                 il_file_list.Add (str);
127                                                 continue;
128                                         } 
129                                         switch (GetCommand (str, out command_arg)) {
130                                                 case "out":
131                                                         output_file = command_arg;
132                                                         break;
133                                                 case "exe":
134                                                         target = Target.Exe;
135                                                         break;
136                                                 case "dll":
137                                                         target = Target.Dll;
138                                                         break;
139                                                 case "scan_only":
140                                                         scan_only = true;
141                                                         break;
142                                                 case "show_tokens":
143                                                         show_tokens = true;
144                                                         break;
145                                                 case "show_method_def":
146                                                         show_method_def = true;
147                                                         break;
148                                                 case "show_method_ref":
149                                                         show_method_ref = true;
150                                                         break;
151                                                 case "-about":
152                                                         if (str[0] != '-')
153                                                                 break;
154                                                         About ();
155                                                         break;
156                                                 case "-version":
157                                                         if (str[0] != '-')
158                                                                 break;
159                                                         Version ();
160                                                         break;
161                                         }
162                                 }
163                         }
164                         
165                         private string GetCommand (string str, out string command_arg)
166                         {
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);
170                                 
171                                 if (end_index != -1) {
172                                         command_arg = str.Substring (end_index+1);
173                                 } else {
174                                         command_arg = null;
175                                 }
176
177                                 return command.ToLower ();
178                         }
179                         
180                         /// <summary>
181                         ///   Get the first file name and makes it into an output file name
182                         /// </summary>
183                         private string CreateOutputFile () 
184                         {
185                                 string file_name = (string)il_file_list[0];
186                                 int ext_index = file_name.LastIndexOf ('.');                    
187         
188                                 if (ext_index == -1)
189                                         ext_index = file_name.Length;
190                                 
191                                 return String.Format ("{0}.{1}", file_name.Substring (0, ext_index), 
192                                         target.ToString ().ToLower ());
193                         }
194
195                         private void Usage ()
196                         {
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");
205                         }
206
207                         private void About ()
208                         {       
209                                 Console.WriteLine (
210                                         "For more information on Mono, visit the project Web site\n" +
211                                         "   http://www.go-mono.com\n\n");
212                                 Environment.Exit (0);
213                         }
214                         
215                         private void Version ()
216                         {
217                                 string version = Assembly.GetExecutingAssembly ().GetName ().Version.ToString ();
218                                 Console.WriteLine ("Mono ILasm compiler version {0}", version);
219                                 Environment.Exit (0);
220                         }
221
222                 }
223         }
224 }
225