2004-11-15 Cesar Lopez Nataren <cesar@ciencias.unam.mx>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / CodeGenerator.cs
1 //
2 // CodeGenerator.cs
3 //
4 // Author:
5 //      Cesar Lopez Nataren (cesar@ciencias.unam.mx)
6 //
7 // (C) 2003, 2004 Cesar Lopez Nataren
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Threading;
35 using Microsoft.JScript.Vsa;
36 using System.Runtime.CompilerServices;
37
38 namespace Microsoft.JScript {
39
40         internal class EmitContext {
41
42                 internal TypeBuilder type_builder;
43                 internal ILGenerator ig;
44                 internal ModuleBuilder mod_builder;
45
46                 internal Label LoopBegin, LoopEnd;
47
48                 internal EmitContext (TypeBuilder type)
49                 {
50                         type_builder = type;
51
52                         if (type_builder != null) {
53                                 MethodBuilder global_code =  type_builder.DefineMethod (
54                                                                         "Global Code",
55                                                                         MethodAttributes.Public,
56                                                                         typeof (System.Object),
57                                                                         new Type [] {});
58                                 ig = global_code.GetILGenerator ();
59                         }
60                 }
61
62                 internal EmitContext (TypeBuilder type_builder, ModuleBuilder mod_builder, ILGenerator ig)
63                 {
64                         this.type_builder = type_builder;
65                         this.mod_builder = mod_builder;
66                         this.ig = ig;
67                 }
68         }
69
70         public class CodeGenerator {
71
72                 private static string MODULE = "JScript Module";
73
74                 internal static string mod_name;
75                 internal static AppDomain app_domain;
76                 internal static AssemblyName assembly_name;
77                 internal static AssemblyBuilder assembly_builder;
78                 internal static ModuleBuilder module_builder;
79
80                 internal static void Init (string file_name)
81                 {
82                         app_domain = Thread.GetDomain ();
83
84                         assembly_name = new AssemblyName ();
85                         assembly_name.Name =  trim_extension (file_name);
86
87                         mod_name = MODULE;
88
89                         assembly_builder = app_domain.DefineDynamicAssembly (
90                                              assembly_name,
91                                              AssemblyBuilderAccess.RunAndSave);
92
93                         ConstructorInfo ctr_info = typeof (Microsoft.JScript.ReferenceAttribute).GetConstructor (new Type [] { typeof (string) });
94                         // FIXME: find out which is the blob.
95                         byte [] blob  = new byte [] {};
96                         assembly_builder.SetCustomAttribute (ctr_info, blob); 
97
98                         module_builder = assembly_builder.DefineDynamicModule (
99                                                 mod_name,
100                                                 assembly_name.Name + ".exe", 
101                                                 false);
102                 }
103
104                 internal static string trim_extension (string file_name)
105                 {
106                         int index = file_name.LastIndexOf ('.');
107
108                         if (index < 0)
109                                 return file_name;
110                         else
111                                 return file_name.Substring (0, index);
112                 }
113
114                 internal static void Save (string target_name)
115                 {
116                         assembly_builder.Save (target_name);
117                 }
118
119                 internal static void Emit (AST prog)
120                 {
121                         if (prog == null)
122                                 return;
123
124                         TypeBuilder type_builder;
125                         type_builder = module_builder.DefineType ("JScript 0", TypeAttributes.Public);
126
127                         type_builder.SetParent (typeof (GlobalScope));
128                         type_builder.SetCustomAttribute (new CustomAttributeBuilder
129                                                          (typeof (CompilerGlobalScopeAttribute).GetConstructor (new Type [] {}), new object [] {}));
130
131                         EmitContext ec = new EmitContext (type_builder);
132                         ec.mod_builder = module_builder;
133                         ILGenerator global_code = ec.ig;
134
135                         emit_default_script_constructor (ec);
136                         emit_default_init_global_code (global_code);
137                         prog.Emit (ec);
138                         emit_default_end_global_code (global_code);
139                         ec.type_builder.CreateType ();
140
141                         //
142                         // Build the default 'JScript Main' class
143                         //
144                         ec.type_builder = module_builder.DefineType ("JScript Main");
145                         emit_jscript_main (ec.type_builder);
146                         ec.type_builder.CreateType ();
147                 }
148
149                 internal static void emit_default_init_global_code (ILGenerator ig)
150                 {
151                         ig.Emit (OpCodes.Ldarg_0);
152                         ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
153                         ig.Emit (OpCodes.Ldarg_0);
154                         ig.Emit (OpCodes.Call,
155                                 typeof (VsaEngine).GetMethod ("PushScriptObject",
156                                                               new Type [] { typeof (ScriptObject)}));
157                 }
158
159                 internal static void emit_default_end_global_code (ILGenerator ig)
160                 {
161                         ig.Emit (OpCodes.Ldnull);
162                         ig.Emit (OpCodes.Ldarg_0);
163                         ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
164                         ig.Emit (OpCodes.Call, typeof (VsaEngine).GetMethod ("PopScriptObject"));
165                         ig.Emit (OpCodes.Pop);
166                         ig.Emit (OpCodes.Ret);          
167                 }
168
169                 internal static void emit_default_script_constructor (EmitContext ec)
170                 {
171                         ConstructorBuilder cons_builder;
172                         TypeBuilder tb = ec.type_builder;
173                         cons_builder = tb.DefineConstructor (MethodAttributes.Public,
174                                                              CallingConventions.Standard,
175                                                              new Type [] { typeof (GlobalScope) });
176
177                         ILGenerator ig = cons_builder.GetILGenerator ();
178                         ig.Emit (OpCodes.Ldarg_0);
179                         ig.Emit (OpCodes.Ldarg_1);
180                         ig.Emit (OpCodes.Dup);
181                         ig.Emit (OpCodes.Ldfld,
182                                  typeof (ScriptObject).GetField ("engine"));
183                         
184                         ig.Emit (OpCodes.Call, 
185                                  typeof (GlobalScope).GetConstructor (new Type [] {typeof (GlobalScope), 
186                                                                                    typeof (VsaEngine)}));
187                         ig.Emit (OpCodes.Ret);
188                 }
189
190                 internal static void emit_jscript_main (TypeBuilder tb)
191                 {
192                         emit_jscript_main_constructor (tb);
193                         emit_jscript_main_entry_point (tb);
194                 }
195
196                 internal static void emit_jscript_main_constructor (TypeBuilder tb)
197                 {
198                         ConstructorBuilder cons = tb.DefineConstructor (MethodAttributes.Public, 
199                                                                         CallingConventions.Standard,
200                                                                         new Type [] {});
201                         ILGenerator ig = cons.GetILGenerator ();
202                         ig.Emit (OpCodes.Ldarg_0);
203                         ig.Emit (OpCodes.Call, typeof (Object).GetConstructor (new Type [] {}));
204                         ig.Emit (OpCodes.Ret);
205                 }
206
207                 internal static void emit_jscript_main_entry_point (TypeBuilder tb)
208                 {
209                         MethodBuilder method;
210                         method = tb.DefineMethod ("Main", 
211                                                   MethodAttributes.Public | MethodAttributes.Static,
212                                                   typeof (void), new Type [] {typeof (String [])});
213
214                         method.SetCustomAttribute (new CustomAttributeBuilder 
215                                                    (typeof (STAThreadAttribute).GetConstructor (
216                                                                                         new Type [] {}),
217                                                      new object [] {}));
218
219                         ILGenerator ig = method.GetILGenerator ();
220
221                         ig.DeclareLocal (typeof (GlobalScope));
222
223                         ig.Emit (OpCodes.Ldc_I4_1);
224                         ig.Emit (OpCodes.Ldc_I4_1);
225                         ig.Emit (OpCodes.Newarr, typeof (string));
226                         ig.Emit (OpCodes.Dup);
227                         ig.Emit (OpCodes.Ldc_I4_0);
228
229                         ig.Emit (OpCodes.Ldstr,
230                                  "mscorlib, Version=1.0.3300.0, Culture=neutral, Pub" + 
231                                  "licKeyToken=b77a5c561934e089");
232
233                         ig.Emit (OpCodes.Stelem_Ref);
234
235                         ig.Emit (OpCodes.Call,
236                                  typeof (VsaEngine).GetMethod ("CreateEngineAndGetGlobalScope", 
237                                                                new Type [] {typeof (bool), 
238                                                                             typeof (string [])}));      
239                         ig.Emit (OpCodes.Stloc_0);
240                         ig.Emit (OpCodes.Ldloc_0);
241                         
242                         ig.Emit (OpCodes.Newobj,
243                                  assembly_builder.GetType ("JScript 0").GetConstructor (
244                                                                         new Type [] {typeof (GlobalScope)})); 
245                         ig.Emit (OpCodes.Call, 
246                                  assembly_builder.GetType ("JScript 0").GetMethod (
247                                                                            "Global Code", new Type [] {}));
248                         ig.Emit (OpCodes.Pop);
249                         ig.Emit (OpCodes.Ret);
250
251                         assembly_builder.SetEntryPoint (method);
252                 }
253
254                 public static void Run (string file_name, AST prog)
255                 {
256                         CodeGenerator.Init (file_name);
257                         CodeGenerator.Emit (prog);
258                         CodeGenerator.Save (trim_extension (file_name) + ".exe");
259                 }
260
261                 static void emit_default_case (EmitContext ec, AST ast, OpCode op, Label lbl)
262                 {
263                         ast.Emit (ec);
264                         if (need_convert_to_boolean (ast))
265                                 emit_to_boolean (ast, ec.ig, 0);
266                         ec.ig.Emit (op, lbl);
267                 }
268
269                 static void ft_binary_recursion (EmitContext ec, AST ast, Label lbl)
270                 {
271                         ILGenerator ig = ec.ig;
272                         if (ast is Binary) {
273                                 Binary b = ast as Binary;
274                                 switch (b.op) {
275                                 case JSToken.LogicalOr:
276                                         Label ftLb = ig.DefineLabel ();
277                                         fall_false (ec, b.left, ftLb);
278                                         fall_true (ec, b.right, lbl);
279                                         ig.MarkLabel (ftLb);
280                                         break;
281                                 case JSToken.LogicalAnd:
282                                         fall_true (ec, b.left, lbl);
283                                         fall_true (ec, b.right, lbl);
284                                         break;
285
286                                 case JSToken.LessThan:
287                                         ig.Emit (OpCodes.Ldc_I4_0);
288                                         ig.Emit (OpCodes.Conv_R8);
289                                         ig.Emit (OpCodes.Blt, lbl);
290                                         break;
291                                 }
292                         }
293                 }
294
295                 static void ft_emit_equality (EmitContext ec, AST ast, Label lbl)
296                 {
297                         ILGenerator ig = ec.ig;
298                         Equality eq = ast as Equality;
299
300                         switch (eq.op) {
301                         case JSToken.NotEqual:
302                                 ig.Emit (OpCodes.Brtrue, lbl);
303                                 break;
304                         case JSToken.Equal:
305                                 ig.Emit (OpCodes.Brfalse, lbl);
306                                 break;
307                         }
308                 }
309
310                 internal static void fall_true (EmitContext ec, AST ast, Label lbl)
311                 {
312                         ILGenerator ig = ec.ig;
313                         Type type = ast.GetType ();
314
315                         if (type == typeof (Expression)) {  
316                                 Expression exp = ast as Expression;
317                                 exp.Emit (ec);
318                                 AST last_exp = (AST) exp.exprs [exp.exprs.Count - 1];
319
320                                 if (last_exp is Binary)
321                                         ft_binary_recursion (ec, last_exp, lbl);
322                                 else if (last_exp is Equality)
323                                         ft_emit_equality (ec, last_exp, lbl);
324                         } else if (type == typeof (Binary))
325                                 ft_binary_recursion (ec, ast, lbl);
326                         else
327                                 emit_default_case (ec, ast, OpCodes.Brfalse, lbl);
328                 }
329
330                 static void ff_emit_relational (ILGenerator ig, AST ast, Label lbl)
331                 {
332                         Relational r = ast as Relational;
333
334                         switch (r.op) {
335                         case JSToken.LessThan:
336                                 ig.Emit (OpCodes.Ldc_I4_0);
337                                 ig.Emit (OpCodes.Conv_R8);
338                                 ig.Emit (OpCodes.Blt, lbl);
339                                 break;
340                         }
341                 }
342
343                 static void ff_binary_recursion (EmitContext ec, AST ast, Label lbl)
344                 {
345                         ILGenerator ig = ec.ig;
346                         Binary b = ast as Binary;
347
348                         switch (b.op) {
349                         case JSToken.LogicalOr:
350                                 fall_false (ec, b.left, lbl);
351                                 fall_false (ec, b.right, lbl);
352                                 break;
353
354                         case JSToken.LogicalAnd:
355                                 Label ftLb = ig.DefineLabel ();
356                                 fall_true (ec, b.left, ftLb);
357                                 fall_false (ec, b.right, lbl);
358                                 ig.MarkLabel (ftLb);
359                                 break;
360                         }
361                 }
362
363                 internal static void fall_false (EmitContext ec, AST ast, Label lbl)
364                 {
365                         ILGenerator ig = ec.ig;
366                         Type type = ast.GetType ();
367
368                         if (type == typeof (Expression)) {  
369                                 Expression exp = ast as Expression;
370                                 exp.Emit (ec);
371                                 AST last_exp = (AST) exp.exprs [exp.exprs.Count - 1];
372
373                                 if (last_exp is Relational)
374                                         ff_emit_relational (ec.ig, last_exp, lbl);
375                                 else if (last_exp is Binary)
376                                         ff_binary_recursion (ec, last_exp, lbl);
377                         } else if (type == typeof (Binary))
378                                 ff_binary_recursion (ec, ast, lbl);
379                         else 
380                                 emit_default_case (ec, ast, OpCodes.Brtrue, lbl);
381                 }
382
383                 internal static void emit_to_boolean (AST ast, ILGenerator ig, int i)
384                 {
385                         ig.Emit (OpCodes.Ldc_I4, i);
386                         ig.Emit (OpCodes.Call, typeof (Convert).GetMethod ("ToBoolean", 
387                                                                            new Type [] { typeof (object), typeof (Boolean)}));
388                 }
389
390                 internal static bool need_convert_to_boolean (AST ast)
391                 {
392                         if (ast == null)
393                                 return false;
394
395                         if (ast is Identifier)
396                                 return true;
397                         else if (ast is Expression) {
398                                 Expression exp = ast as Expression;
399                                 int n = exp.exprs.Count - 1;
400                                 AST tmp = (AST) exp.exprs [n];
401                                 if (tmp is Equality || tmp is Relational || tmp is BooleanLiteral)
402                                         return false;
403                                 else
404                                         return true;
405                         } else 
406                                 return false;
407                 }
408         }
409 }