2002-02-18 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mono / jit / emit-x86.c
1 /*
2  * emit-x86.c: Support functions for emitting x86 code
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Miguel de Icaza (miguel@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/loader.h>
16 #include <mono/metadata/cil-coff.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/class.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/arch/x86/x86-codegen.h>
21
22 #include "jit.h"
23 #include "codegen.h"
24 #include "debug.h"
25
26 static void
27 enter_method (MonoMethod *method, gpointer ebp)
28 {
29         int i, j;
30         MonoClass *class;
31         MonoObject *o;
32
33         printf ("ENTER: %s.%s::%s\n(", method->klass->name_space,
34                 method->klass->name, method->name);
35
36         
37         if (((int)ebp & 3) != 0) {
38                 g_error ("unaligned stack detected (%p)", ebp);
39         }
40
41         ebp += 8;
42
43         if (ISSTRUCT (method->signature->ret)) {
44                 int size, align;
45                 
46                 g_assert (!method->signature->ret->byref);
47
48                 size = mono_type_stack_size (method->signature->ret, &align);
49
50                 printf ("VALUERET:%p, ", *((gpointer *)ebp));
51                 ebp += sizeof (gpointer);
52         }
53
54         if (method->signature->hasthis) {
55                 if (method->klass->valuetype) {
56                         printf ("value:%p, ", *((gpointer *)ebp));
57                 } else {
58                         o = *((MonoObject **)ebp);
59
60                         g_assert (o);
61
62                         class = o->vtable->klass;
63
64                         if (class == mono_defaults.string_class) {
65                                 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
66                         } else {
67                                 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
68                         }
69                 }
70                 ebp += sizeof (gpointer);
71         }
72
73         for (i = 0; i < method->signature->param_count; ++i) {
74                 MonoType *type = method->signature->params [i];
75                 int size, align;
76                 size = mono_type_stack_size (type, &align);
77
78                 if (type->byref) {
79                         printf ("[BYREF:%p], ", *((gpointer *)ebp)); 
80                 } else switch (type->type) {
81                         
82                 case MONO_TYPE_BOOLEAN:
83                 case MONO_TYPE_CHAR:
84                 case MONO_TYPE_I1:
85                 case MONO_TYPE_U1:
86                 case MONO_TYPE_I2:
87                 case MONO_TYPE_U2:
88                 case MONO_TYPE_I4:
89                 case MONO_TYPE_U4:
90                 case MONO_TYPE_I:
91                 case MONO_TYPE_U:
92                         printf ("%d, ", *((int *)(ebp)));
93                         break;
94                 case MONO_TYPE_STRING: {
95                         MonoString *s = *((MonoString **)ebp);
96                         if (s) {
97                                 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
98                                 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
99                         } else 
100                                 printf ("[STRING:null], ");
101                         break;
102                 }
103                 case MONO_TYPE_CLASS:
104                 case MONO_TYPE_OBJECT: {
105                         o = *((MonoObject **)ebp);
106                         if (o) {
107                                 class = o->vtable->klass;
108                     
109                                 if (class == mono_defaults.string_class) {
110                                         printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
111                                 } else if (class == mono_defaults.int32_class) {
112                                         printf ("[INT32:%p:%d], ", o, *(gint32 *)((gpointer)o + sizeof (MonoObject)));
113                                 } else
114                                         printf ("[%s.%s:%p], ", class->name_space, class->name, o);
115                         } else {
116                                 printf ("%p, ", *((gpointer *)(ebp)));                          
117                         }
118                         break;
119                 }
120                 case MONO_TYPE_PTR:
121                 case MONO_TYPE_FNPTR:
122                 case MONO_TYPE_ARRAY:
123                 case MONO_TYPE_SZARRAY:
124                         printf ("%p, ", *((gpointer *)(ebp)));
125                         break;
126                 case MONO_TYPE_I8:
127                         printf ("%lld, ", *((gint64 *)(ebp)));
128                         break;
129                 case MONO_TYPE_R4:
130                         printf ("%f, ", *((float *)(ebp)));
131                         break;
132                 case MONO_TYPE_R8:
133                         printf ("%f, ", *((double *)(ebp)));
134                         break;
135                 case MONO_TYPE_VALUETYPE: 
136                         printf ("[");
137                         for (j = 0; j < size; j++)
138                                 printf ("%02x,", *((guint8*)ebp +j));
139                         printf ("], ");
140                         break;
141                 default:
142                         printf ("XX, ");
143                 }
144
145                 g_assert (align == 4);
146                 ebp += size + 3;
147                 ebp = (gpointer)((unsigned)ebp & ~(3));
148         }
149
150         printf (")\n");
151 }
152
153 static void
154 leave_method (MonoMethod *method, int edx, int eax, double test)
155 {
156         gint64 l;
157
158         printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
159                 method->klass->name, method->name);
160
161         switch (method->signature->ret->type) {
162         case MONO_TYPE_VOID:
163                 break;
164         case MONO_TYPE_BOOLEAN:
165                 if (eax)
166                         printf ("TRUE:%d", eax);
167                 else 
168                         printf ("FALSE");
169                         
170                 break;
171         case MONO_TYPE_CHAR:
172         case MONO_TYPE_I1:
173         case MONO_TYPE_U1:
174         case MONO_TYPE_I2:
175         case MONO_TYPE_U2:
176         case MONO_TYPE_I4:
177         case MONO_TYPE_U4:
178         case MONO_TYPE_I:
179         case MONO_TYPE_U:
180                 printf ("EAX=%d", eax);
181                 break;
182         case MONO_TYPE_STRING: {
183                 MonoString *s = (MonoString *)eax;
184
185                 if (s) {
186                         g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
187                         printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
188                 } else 
189                         printf ("[STRING:null], ");
190                 break;
191         }
192         case MONO_TYPE_OBJECT: {
193                 MonoObject *o = (MonoObject *)eax;
194
195                 if (o) {
196                         if (o->vtable->klass == mono_defaults.boolean_class) {
197                                 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));            
198                         } else if  (o->vtable->klass == mono_defaults.int32_class) {
199                                 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));  
200                         } else
201                                 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
202                 } else
203                         printf ("[OBJECT:%p]", o);
204                
205                 break;
206         }
207         case MONO_TYPE_CLASS:
208         case MONO_TYPE_PTR:
209         case MONO_TYPE_FNPTR:
210         case MONO_TYPE_ARRAY:
211         case MONO_TYPE_SZARRAY:
212                 printf ("EAX=%p", (gpointer)eax);
213                 break;
214         case MONO_TYPE_I8:
215                 *((gint32 *)&l) = eax;
216                 *((gint32 *)&l + 1) = edx;
217                 printf ("EAX/EDX=%lld", l);
218                 break;
219         case MONO_TYPE_R8:
220                 printf ("FP=%f\n", test);
221                 break;
222         default:
223                 printf ("(unknown return type)");
224         }
225
226         printf ("\n");
227 }
228
229 /**
230  * arch_emit_prologue:
231  * @cfg: pointer to status information
232  *
233  * Emits the function prolog.
234  */
235 static void
236 arch_emit_prologue (MonoFlowGraph *cfg)
237 {
238         x86_push_reg (cfg->code, X86_EBP);
239         x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
240
241         if (cfg->locals_size)
242                 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
243
244         if (mono_regset_reg_used (cfg->rs, X86_EBX)) 
245                 x86_push_reg (cfg->code, X86_EBX);
246
247         if (mono_regset_reg_used (cfg->rs, X86_EDI)) 
248                 x86_push_reg (cfg->code, X86_EDI);
249
250         if (mono_regset_reg_used (cfg->rs, X86_ESI))
251                 x86_push_reg (cfg->code, X86_ESI);
252
253         if (mono_jit_trace_calls) {
254                 x86_push_reg (cfg->code, X86_EBP);
255                 x86_push_imm (cfg->code, cfg->method);
256                 x86_mov_reg_imm (cfg->code, X86_EAX, enter_method);
257                 x86_call_reg (cfg->code, X86_EAX);
258                 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
259         }
260 }
261
262 /**
263  * arch_emit_epilogue:
264  * @cfg: pointer to status information
265  *
266  * Emits the function epilog.
267  */
268 static void
269 arch_emit_epilogue (MonoFlowGraph *cfg)
270 {
271         if (mono_jit_trace_calls) {
272                 x86_fld_reg (cfg->code, 0);
273                 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
274                 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
275                 x86_push_reg (cfg->code, X86_EAX);
276                 x86_push_reg (cfg->code, X86_EDX);
277                 x86_push_imm (cfg->code, cfg->method);
278                 x86_mov_reg_imm (cfg->code, X86_EAX, leave_method);
279                 x86_call_reg (cfg->code, X86_EAX);
280                 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
281                 x86_pop_reg (cfg->code, X86_EDX);
282                 x86_pop_reg (cfg->code, X86_EAX);
283                 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
284         }
285
286         if (mono_regset_reg_used (cfg->rs, X86_ESI))
287                 x86_pop_reg (cfg->code, X86_ESI);
288
289         if (mono_regset_reg_used (cfg->rs, X86_EDI))
290                 x86_pop_reg (cfg->code, X86_EDI);
291
292         if (mono_regset_reg_used (cfg->rs, X86_EBX))
293                 x86_pop_reg (cfg->code, X86_EBX);
294
295         x86_leave (cfg->code);
296         x86_ret (cfg->code);
297 }
298
299 /*
300  * get_unbox_trampoline:
301  * @m: method pointer
302  *
303  * when value type methods are called through the vtable we need to unbox the
304  * this argument. This method returns a pointer to a trampoline which does
305  * unboxing before calling the method
306  */
307 static gpointer
308 get_unbox_trampoline (MonoMethod *m)
309 {
310         gpointer p = arch_compile_method (m);
311         guint8 *code, *start;
312         int this_pos = 4;
313
314         if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
315                 this_pos = 8;
316             
317         start = code = g_malloc (16);
318
319         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
320         x86_jump_code (code, p);
321         g_assert ((code - start) < 16);
322
323         return start;
324 }
325
326 /**
327  * x86_magic_trampoline:
328  * @eax: saved x86 register 
329  * @ecx: saved x86 register 
330  * @edx: saved x86 register 
331  * @esi: saved x86 register 
332  * @edi: saved x86 register 
333  * @ebx: saved x86 register
334  * @code: pointer into caller code
335  * @method: the method to translate
336  *
337  * This method is called by the trampoline functions for virtual
338  * methods. It inspects the caller code to find the address of the
339  * vtable slot, then calls the JIT compiler and writes the address
340  * of the compiled method back to the vtable. All virtual methods 
341  * are called with: x86_call_membase (inst, basereg, disp). We always
342  * use 32 bit displacement to ensure that the length of the call 
343  * instruction is 6 bytes. We need to get the value of the basereg 
344  * and the constant displacement.
345  */
346 static gpointer
347 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
348                       int ebx, const guint8 *code, MonoMethod *m)
349 {
350         guint8 reg;
351         gint32 disp;
352         gpointer o;
353
354         /* go to the start of the call instruction
355          *
356          * address_byte = (m << 6) | (o << 3) | reg
357          * call opcode: 0xff address_byte displacement
358          * 0xff m=1,o=2 imm8
359          * 0xff m=2,o=2 imm32
360          */
361         code -= 6;
362         if ((code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
363                 reg = code [4] & 0x07;
364                 disp = (signed char)code [5];
365         } else {
366                 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
367                         reg = code [1] & 0x07;
368                         disp = *((gint32*)(code + 2));
369                 } else if ((code [1] == 0xe8)) {
370                         gpointer addr = arch_compile_method (m);
371                         g_assert (addr);
372                         *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5; 
373                         return addr;
374                 } else {
375                         printf ("%x %x %x %x\n", code [0], code [1], code [2], code [3]);
376                         g_assert_not_reached ();
377                 }
378         }
379
380         switch (reg) {
381         case X86_EAX:
382                 o = (gpointer)eax;
383                 break;
384         case X86_EDX:
385                 o = (gpointer)edx;
386                 break;
387         case X86_ECX:
388                 o = (gpointer)ecx;
389                 break;
390         case X86_ESI:
391                 o = (gpointer)esi;
392                 break;
393         case X86_EDI:
394                 o = (gpointer)edi;
395                 break;
396         case X86_EBX:
397                 o = (gpointer)ebx;
398                 break;
399         default:
400                 g_assert_not_reached ();
401         }
402
403         o += disp;
404
405         if (m->klass->valuetype) {
406                 return *((gpointer *)o) = get_unbox_trampoline (m);
407         } else
408                 return *((gpointer *)o) = arch_compile_method (m);
409 }
410
411 /**
412  * arch_create_jit_trampoline:
413  * @method: pointer to the method info
414  *
415  * Creates a trampoline function for virtual methods. If the created
416  * code is called it first starts JIT compilation of method,
417  * and then calls the newly created method. I also replaces the
418  * corresponding vtable entry (see x86_magic_trampoline).
419  * 
420  * Returns: a pointer to the newly created code 
421  */
422 gpointer
423 arch_create_jit_trampoline (MonoMethod *method)
424 {
425         guint8 *code, *buf;
426         static guint8 *vc = NULL;
427
428         if (method->addr)
429                 return method->addr;
430
431         if (!vc) {
432                 vc = buf = g_malloc (24);
433
434                 /* push the return address onto the stack */
435                 x86_push_membase (buf, X86_ESP, 4);
436
437                 /* save all register values */
438                 x86_push_reg (buf, X86_EBX);
439                 x86_push_reg (buf, X86_EDI);
440                 x86_push_reg (buf, X86_ESI);
441                 x86_push_reg (buf, X86_EDX);
442                 x86_push_reg (buf, X86_ECX);
443                 x86_push_reg (buf, X86_EAX);
444
445                 x86_call_code (buf, x86_magic_trampoline);
446                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
447
448                 /* call the compiled method */
449                 x86_jump_reg (buf, X86_EAX);
450
451                 g_assert ((buf - vc) <= 24);
452         }
453
454         code = buf = g_malloc (16);
455         x86_push_imm (buf, method);
456         x86_jump_code (buf, vc);
457         g_assert ((buf - code) <= 16);
458
459         return code;
460 }
461
462 static void
463 mono_label_cfg (MonoFlowGraph *cfg)
464 {
465         int i, j;
466         
467         for (i = 0; i < cfg->block_count; i++) {
468                 GPtrArray *forest = cfg->bblocks [i].forest;
469                 int top;
470
471                 if (!cfg->bblocks [i].reached) /* unreachable code */
472                         continue;
473                 
474                 top = forest->len;
475
476                 for (j = 0; j < top; j++) {
477                         MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
478                         MBState *mbstate;
479
480                         mbstate =  mono_burg_label (t1, cfg);
481                         if (!mbstate) {
482                                 cfg->invalid = 1;
483                                 if (mono_debug_handle)
484                                         return;
485                                 g_warning ("tree does not match");
486                                 mono_print_ctree (t1); printf ("\n\n");
487
488                                 mono_print_forest (forest);
489                                 g_assert_not_reached ();
490                         }
491                 }
492         }
493 }
494
495 static void
496 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs) 
497 {
498         switch (tree->op) {
499         case MB_TERM_CALL_I4:
500         case MB_TERM_CALL_I8:
501         case MB_TERM_CALL_R8:
502 //      case MB_TERM_CALL_VOID :
503                 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
504                 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
505                 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
506                 return;
507         default: break;
508         }
509
510         switch (goal) {
511         case MB_NTERM_reg:
512         case MB_NTERM_lreg: {
513                 switch (tree->op) {
514                 case MB_TERM_SHL:
515                 case MB_TERM_SHR:
516                 case MB_TERM_SHR_UN:
517                         tree->exclude_mask |= (1 << X86_ECX);
518                         tree->left->exclude_mask |= (1 << X86_ECX);
519                         break;
520                 case MB_TERM_MUL:
521                 case MB_TERM_MUL_OVF:
522                 case MB_TERM_MUL_OVF_UN:
523                 case MB_TERM_DIV:
524                 case MB_TERM_DIV_UN:
525                 case MB_TERM_REM:
526                 case MB_TERM_REM_UN:
527                         tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
528                         tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
529                         if (goal == MB_NTERM_reg) {
530                                 tree->left->exclude_mask |= (1 << X86_EDX);
531                                 tree->right->exclude_mask |= (1 << X86_EDX) | (1 << X86_EAX);
532                         }
533                         break;
534                 default:
535                         break;
536                 }
537                 break;
538         }
539         default:
540                 break;
541         }
542 }
543
544 static void
545 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs) 
546 {
547         MBTree *kids[10];
548         int ern = mono_burg_rule (tree->state, goal);
549         const guint16 *nts = mono_burg_nts [ern];
550         int i;
551         
552         mono_burg_kids (tree, ern, kids);
553
554         //printf ("RALLOC START %d %p %d\n",  tree->op, rs->free_mask, goal);
555
556         if (nts [0] && kids [0] == tree) {
557                 /* chain rule */
558                 tree_allocate_regs (kids [0], nts [0], rs);
559                 return;
560         }
561
562         for (i = 0; nts [i]; i++)
563                 tree_preallocate_regs (kids [i], nts [i], rs);
564
565         for (i = 0; nts [i]; i++)
566                 tree_allocate_regs (kids [i], nts [i], rs);
567
568         for (i = 0; nts [i]; i++) {
569                 mono_regset_free_reg (rs, kids [i]->reg1);
570                 mono_regset_free_reg (rs, kids [i]->reg2);
571                 mono_regset_free_reg (rs, kids [i]->reg3);
572         }
573
574         switch (goal) {
575         case MB_NTERM_reg:
576                 if (tree->reg1 < 0) { 
577                         tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
578                         g_assert (tree->reg1 != -1);
579                 }
580                 break;
581
582         case MB_NTERM_lreg:
583                 if (tree->reg1 < 0) { 
584                         tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
585                         g_assert (tree->reg1 != -1);
586                 }
587                 if (tree->reg2 < 0) { 
588                         tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
589                         g_assert (tree->reg2 != -1);
590                 }
591                 break;
592
593         case MB_NTERM_freg:
594                 /* fixme: allocate floating point registers */
595                 break;
596       
597         case MB_NTERM_addr:
598                 if (tree->op == MB_TERM_ADD) {
599                         tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
600                         tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
601                 }
602                 break;
603                 
604         case MB_NTERM_base:
605                 if (tree->op == MB_TERM_ADD) {
606                         tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
607                 }
608                 break;
609                
610         case MB_NTERM_index:
611                 if (tree->op == MB_TERM_SHL ||
612                     tree->op == MB_TERM_MUL) {
613                         tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
614                 }
615                 break;
616                
617         default:
618                 /* do nothing */
619         }
620
621         //printf ("RALLOC END %d %p\n",  tree->op, rs->free_mask);
622         tree->emit = mono_burg_func [ern];
623 }
624
625 static void
626 arch_allocate_regs (MonoFlowGraph *cfg)
627 {
628         int i, j;
629         
630         for (i = 0; i < cfg->block_count; i++) {
631                 GPtrArray *forest = cfg->bblocks [i].forest;
632                 int top;
633
634                 if (!cfg->bblocks [i].reached) /* unreachable code */
635                         continue;
636
637                 top = forest->len;
638
639                 for (j = 0; j < top; j++) {
640                         MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
641                         //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
642                         tree_allocate_regs (t1, 1, cfg->rs);
643                         //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
644                         g_assert (cfg->rs->free_mask == 0xffffffff);
645                 }
646         }
647 }
648
649 static void
650 tree_emit (int goal, MonoFlowGraph *cfg, MBTree *tree) 
651 {
652         MBTree *kids[10];
653         int i, ern = mono_burg_rule (tree->state, goal);
654         const guint16 *nts = mono_burg_nts [ern];
655         MBEmitFunc emit;
656         int offset;
657
658         mono_burg_kids (tree, ern, kids);
659
660         for (i = 0; nts [i]; i++) 
661                 tree_emit (nts [i], cfg, kids [i]);
662
663         tree->addr = offset = cfg->code - cfg->start;
664
665         // we assume an instruction uses a maximum of 128 bytes
666         if ((cfg->code_size - offset) <= 128) {
667                 int add = MIN ((cfg->code_size * 2), 1024);
668
669                 cfg->code_size += add;
670                 cfg->start = g_realloc (cfg->start, cfg->code_size);
671                 g_assert (cfg->start);
672                 cfg->code = cfg->start + offset;
673         }
674
675         if ((emit = mono_burg_func [ern]))
676                 emit (tree, cfg);
677
678         g_assert ((cfg->code - cfg->start) < cfg->code_size);
679 }
680
681 static void
682 mono_emit_cfg (MonoFlowGraph *cfg)
683 {
684         int i, j;
685
686         for (i = 0; i < cfg->block_count; i++) {
687                 MonoBBlock *bb = &cfg->bblocks [i];
688                 GPtrArray *forest = bb->forest;
689                 int top;
690
691                 if (!bb->reached) /* unreachable code */
692                         continue;
693                 
694                 top = forest->len;
695
696                 bb->addr = cfg->code - cfg->start;
697           
698                 for (j = 0; j < top; j++) {
699                         MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
700                         
701                         tree_emit (1, cfg, t1);
702                 }
703         }
704                 
705         cfg->epilog = cfg->code - cfg->start;
706 }
707
708 static void
709 mono_compute_branches (MonoFlowGraph *cfg)
710 {
711         guint8 *end;
712         int i, j;
713
714         end = cfg->code;
715
716         for (j = 0; j < cfg->block_count; j++) {
717                 MonoBBlock *bb = &cfg->bblocks [j];
718                 GPtrArray *forest = bb->forest;
719                 int top;
720                 
721                 if (!bb->reached) /* unreachable code */
722                         continue;
723
724                 top = forest->len;
725         
726                 for (i = 0; i < top; i++) {
727                         MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
728
729                         if (t1->is_jump) {
730
731                                 if (t1->op == MB_TERM_SWITCH) {
732                                         MonoBBlock **jt = (MonoBBlock **)t1->data.p;
733                                         guint32 *rt = (guint32 *)t1->data.p;
734
735                                         int m = *((guint32 *)t1->data.p) + 1;
736                                         int j;
737                                         
738                                         for (j = 1; j <= m; j++)
739                                                 rt [j] = (int)(jt [j]->addr + cfg->start);
740                                 }
741
742                                 /* emit the jump instruction again to update addresses */
743                                 cfg->code = cfg->start + t1->addr;
744                                 ((MBEmitFunc)t1->emit) (t1, cfg);
745
746                         }
747                 }
748         }
749
750         cfg->code = end;
751 }
752
753 static int
754 match_debug_method (MonoMethod* method)
755 {
756         GList *tmp = mono_debug_methods;
757
758         for (; tmp; tmp = tmp->next) {
759                 if (strcmp (method->name, tmp->data) == 0) {
760                         return 1;
761                 }
762         }
763         return 0;
764 }
765
766 /**
767  * arch_compile_method:
768  * @method: pointer to the method info
769  *
770  * JIT compilation of a single method. 
771  *
772  * Returns: a pointer to the newly created code.
773  */
774 gpointer
775 arch_compile_method (MonoMethod *method)
776 {
777         MonoDomain *domain = mono_domain_get ();
778         MonoFlowGraph *cfg;
779         MonoMemPool *mp = mono_mempool_new ();
780         guint8 *addr;
781
782         g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
783         g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
784
785         if ((addr = g_hash_table_lookup (domain->jit_code_hash, method)))
786                 return addr;
787
788         if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
789                 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
790                         method->klass->name, method->name);
791         }
792
793         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
794                 MonoClassField *field;
795                 const char *name = method->name;
796                 static guint target_offset = 0;
797                 static guint method_offset = 0;
798                 guint8 *code;
799                 gboolean delegate = FALSE;
800
801                 if (method->klass->parent && 
802                     method->klass->parent->parent == mono_defaults.delegate_class)
803                         delegate = TRUE;
804                                 
805                 if (!target_offset) {
806                         mono_class_init (mono_defaults.delegate_class);
807
808                         field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
809                         target_offset = field->offset;
810                         field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
811                         method_offset = field->offset;
812                 }
813                 
814                 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
815                         addr = code = g_malloc (32);
816                         x86_push_reg (code, X86_EBP);
817                         x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
818                         
819                         /* load the this pointer */
820                         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4); 
821                         /* load m_target arg */
822                         x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
823                         /* store mtarget */
824                         x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4); 
825                         /* load method_ptr arg */
826                         x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
827                         /* store method_ptr */
828                         x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4); 
829
830                         x86_leave (code);
831                         x86_ret (code);
832
833                         g_assert ((code - (guint8*)addr) < 32);
834
835                 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
836                         MonoMethodSignature *csig = method->signature;
837                         int i, arg_size, target, this_pos = 4;
838                         guint8 *source;
839                         
840                         if (csig->ret->type == MONO_TYPE_VALUETYPE) {
841                                 g_assert (!csig->ret->byref);
842                                 this_pos = 8;
843                         }
844
845                         arg_size = 0;
846                         if (csig->param_count) {
847                                 int align;
848                                 
849                                 for (i = 0; i < csig->param_count; ++i) {
850                                         arg_size += mono_type_stack_size (csig->params [i], &align);
851                                         g_assert (align == 4);
852                                 }
853                         }
854
855                         addr = g_malloc (64 + arg_size);
856
857                         for (i = 0; i < 2; i ++) {
858                                 int j;
859
860                                 code = addr;
861                                 /* load the this pointer */
862                                 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
863                                 /* load mtarget */
864                                 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4); 
865                                 /* check if zero (static method call without this pointer) */
866                                 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
867                                 x86_branch32 (code, X86_CC_EQ, target, TRUE); 
868                                 source = code;
869
870                                 /* virtual delegate methods: we have to replace the this pointer 
871                                  * withe the actual target */
872                                 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4); 
873                                 /* jump to method_ptr() */
874                                 x86_jump_membase (code, X86_EAX, method_offset);
875
876                                 /* static delegate methods: we have to remove the this pointer 
877                                  * from the activation frame - I do this do creating a new 
878                                  * stack frame an copy all arguments except the this pointer */
879
880                                 target = code - source;
881                                 g_assert ((arg_size & 3) == 0);
882                                 for (j = 0; j < (arg_size>>2); j++) {
883                                         x86_push_membase (code, X86_ESP, (arg_size + this_pos));
884                                 }
885
886                                 if (this_pos == 8) 
887                                         x86_push_membase (code, X86_ESP, (arg_size + 4));
888                                 
889                                 x86_call_membase (code, X86_EAX, method_offset);
890                                 if (this_pos == 8) 
891                                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
892                                 else
893                                         x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size);
894                                 
895                                 x86_ret (code);
896
897                         }
898
899                         g_assert ((code - (guint8*)addr) < (64 + arg_size));
900
901                 } else {
902                         if (mono_debug_handle)
903                                 return NULL;
904                         g_error ("Don't know how to exec runtime method %s.%s::%s", 
905                                  method->klass->name_space, method->klass->name, method->name);
906                 }
907         
908         } else {
909                 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
910                 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
911                 
912                 cfg = mono_cfg_new (method, mp);
913
914                 mono_analyze_flow (cfg);
915                 if (cfg->invalid)
916                         return NULL;
917
918                 mono_analyze_stack (cfg);
919                 if (cfg->invalid)
920                         return NULL;
921         
922                 cfg->rs = mono_regset_new (X86_NREG);
923                 mono_regset_reserve_reg (cfg->rs, X86_ESP);
924                 mono_regset_reserve_reg (cfg->rs, X86_EBP);
925
926                 cfg->code_size = 256000;
927                 cfg->start = cfg->code = g_malloc (cfg->code_size);
928
929                 if (match_debug_method (method))
930                         x86_breakpoint (cfg->code);
931
932                 if (mono_jit_dump_forest) {
933                         int i;
934                         printf ("FOREST %s.%s:%s\n", method->klass->name_space,
935                                 method->klass->name, method->name);
936                         for (i = 0; i < cfg->block_count; i++) {
937                                 printf ("BLOCK %d:\n", i);
938                                 mono_print_forest (cfg->bblocks [i].forest);
939                         }
940                 }
941         
942                 mono_label_cfg (cfg);
943                 if (cfg->invalid)
944                         return NULL;
945
946                 arch_allocate_regs (cfg);
947
948                 /* align to 8 byte boundary */
949                 cfg->locals_size += 7;
950                 cfg->locals_size &= ~7;
951
952                 arch_emit_prologue (cfg);
953                 mono_emit_cfg (cfg);
954                 arch_emit_epilogue (cfg);               
955
956                 addr = cfg->start;
957
958                 mono_compute_branches (cfg);
959                 
960                 if (mono_jit_dump_asm) {
961                         char *id = g_strdup_printf ("%s.%s_%s", method->klass->name_space,
962                                                     method->klass->name, method->name);
963                         mono_disassemble_code (cfg->start, cfg->code - cfg->start, id);
964                         g_free (id);
965                 }
966                 if (mono_debug_handle)
967                         mono_debug_add_method (mono_debug_handle, cfg);
968
969                 ji->code_size = cfg->code - cfg->start;
970                 ji->used_regs = cfg->rs->used_mask;
971                 ji->method = method;
972                 ji->code_start = addr;
973                 mono_jit_info_table_add (mono_jit_info_table, ji);
974
975                 if (header->num_clauses) {
976                         int i, start_block, end_block;
977
978                         ji->num_clauses = header->num_clauses;
979                         ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
980
981                         for (i = 0; i < header->num_clauses; i++) {
982                                 MonoExceptionClause *ec = &header->clauses [i];
983                                 MonoJitExceptionInfo *ei = &ji->clauses [i];
984                         
985                                 ei->flags = ec->flags;
986                                 ei->token_or_filter = ec->token_or_filter;
987
988                                 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
989                                 start_block = cfg->bcinfo [ec->try_offset].block_id;
990                                 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
991                                 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
992                                 
993                                 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
994                                 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
995                                 
996                                 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
997                                 start_block = cfg->bcinfo [ec->handler_offset].block_id;
998                                 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;       
999                                 
1000                                 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
1001                         }
1002                 }
1003                 
1004                 mono_regset_free (cfg->rs);
1005
1006                 mono_cfg_free (cfg);
1007
1008                 mono_mempool_destroy (mp);
1009
1010         }
1011
1012         if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
1013                 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
1014                         method->klass->name, method->name, method, addr);
1015         }
1016
1017         g_hash_table_insert (domain->jit_code_hash, method, addr);
1018
1019         return addr;
1020 }
1021
1022 /*
1023  * arch_get_restore_context:
1024  *
1025  * Returns a pointer to a method which restores a previously saved sigcontext.
1026  */
1027 static gpointer
1028 arch_get_restore_context ()
1029 {
1030         static guint8 *start = NULL;
1031         guint8 *code;
1032
1033         if (start)
1034                 return start;
1035
1036         /* restore_contect (struct sigcontext *ctx) */
1037         /* we do not restore X86_EAX, X86_EDX */
1038
1039         start = code = malloc (1024);
1040         
1041         /* load ctx */
1042         x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
1043
1044         /* get return address, stored in EDX */
1045         x86_mov_reg_membase (code, X86_EDX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, eip), 4);
1046
1047         /* restore EBX */
1048         x86_mov_reg_membase (code, X86_EBX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
1049         /* restore EDI */
1050         x86_mov_reg_membase (code, X86_EDI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, edi), 4);
1051         /* restore ESI */
1052         x86_mov_reg_membase (code, X86_ESI, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, esi), 4);
1053         /* restore ESP */
1054         x86_mov_reg_membase (code, X86_ESP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, esp), 4);
1055         /* restore EBP */
1056         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1057         /* restore ECX. the exception object is passed here to the catch handler */
1058         x86_mov_reg_membase (code, X86_ECX, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
1059
1060         /* jump to the saved IP */
1061         x86_jump_reg (code, X86_EDX);
1062
1063         return start;
1064 }
1065
1066 /*
1067  * arch_get_call_finally:
1068  *
1069  * Returns a pointer to a method which calls a finally handler.
1070  */
1071 static gpointer
1072 arch_get_call_finally ()
1073 {
1074         static guint8 start [28];
1075         static int inited = 0;
1076         guint8 *code;
1077
1078         if (inited)
1079                 return start;
1080
1081         inited = 1;
1082         /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1083         code = start;
1084
1085         x86_push_reg (code, X86_EBP);
1086         x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1087         x86_push_reg (code, X86_EBX);
1088         x86_push_reg (code, X86_EDI);
1089         x86_push_reg (code, X86_ESI);
1090
1091         /* load ctx */
1092         x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1093         /* load eip */
1094         x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1095         /* save EBP */
1096         x86_push_reg (code, X86_EBP);
1097         /* set new EBP */
1098         x86_mov_reg_membase (code, X86_EBP, X86_EAX,  G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1099         /* call the handler */
1100         x86_call_reg (code, X86_ECX);
1101         /* restore EBP */
1102         x86_pop_reg (code, X86_EBP);
1103         /* restore saved regs */
1104         x86_pop_reg (code, X86_ESI);
1105         x86_pop_reg (code, X86_EDI);
1106         x86_pop_reg (code, X86_EBX);
1107         x86_leave (code);
1108         x86_ret (code);
1109
1110         g_assert ((code - start) < 28);
1111         return start;
1112 }
1113
1114 /**
1115  * arch_handle_exception:
1116  * @ctx: saved processor state
1117  * @obj:
1118  */
1119 void
1120 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1121 {
1122         MonoDomain *domain = mono_domain_get ();
1123         MonoJitInfo *ji;
1124         gpointer ip = (gpointer)ctx->eip;
1125         static void (*restore_context) (struct sigcontext *);
1126         static void (*call_finally) (struct sigcontext *, unsigned long);
1127        
1128         g_assert (ctx != NULL);
1129         g_assert (obj != NULL);
1130
1131         ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1132
1133         if (!restore_context)
1134                 restore_context = arch_get_restore_context ();
1135         
1136         if (!call_finally)
1137                 call_finally = arch_get_call_finally ();
1138
1139         if (ji) { /* we are inside managed code */
1140                 MonoMethod *m = ji->method;
1141                 unsigned next_bp;
1142                 int offset = 2;
1143
1144                 if (ji->num_clauses) {
1145                         int i;
1146
1147                         g_assert (ji->clauses);
1148                         
1149                         for (i = 0; i < ji->num_clauses; i++) {
1150                                 MonoJitExceptionInfo *ei = &ji->clauses [i];
1151
1152                                 if (ei->try_start <= ip && ip <= (ei->try_end)) { 
1153                                         /* catch block */
1154                                         if (ei->flags == 0 && mono_object_isinst (obj, 
1155                                                 mono_class_get (m->klass->image, ei->token_or_filter))) {
1156                                         
1157                                                 ctx->eip = (unsigned long)ei->handler_start;
1158                                                 ctx->ecx = (unsigned long)obj;
1159                                                 restore_context (ctx);
1160                                                 g_assert_not_reached ();
1161                                         }
1162                                 }
1163                         }
1164
1165                         /* no handler found - we need to call all finally handlers */
1166                         for (i = 0; i < ji->num_clauses; i++) {
1167                                 MonoJitExceptionInfo *ei = &ji->clauses [i];
1168
1169                                 if (ei->try_start <= ip && ip < (ei->try_end) &&
1170                                     (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1171                                         call_finally (ctx, (unsigned long)ei->handler_start);
1172                                 }
1173                         }
1174                 }
1175
1176                 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1177                         char  *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1178                         char  *tmp;
1179
1180                         if (!strcmp (strace, "TODO: implement stack traces")){
1181                                 g_free (strace);
1182                                 strace = g_strdup ("");
1183                         }
1184
1185                         tmp = g_strdup_printf ("%sin %s.%s:%s ()\n", strace, m->klass->name_space,  
1186                                                m->klass->name, m->name);
1187
1188                         g_free (strace);
1189
1190                         ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1191                         g_free (tmp);
1192                 }
1193
1194                 /* continue unwinding */
1195
1196                 /* restore caller saved registers */
1197                 if (ji->used_regs & X86_ESI_MASK) {
1198                         ctx->esi = *((int *)ctx->ebp + offset);
1199                         offset++;
1200                 }
1201                 if (ji->used_regs & X86_EDI_MASK) {
1202                         ctx->edi = *((int *)ctx->ebp + offset);
1203                         offset++;
1204                 }
1205                 if (ji->used_regs & X86_EBX_MASK) {
1206                         ctx->ebx = *((int *)ctx->ebp + offset);
1207                 }
1208
1209                 ctx->esp = ctx->ebp;
1210                 ctx->eip = *((int *)ctx->ebp + 1);
1211                 ctx->ebp = *((int *)ctx->ebp);
1212                 
1213                 if (next_bp < (unsigned)mono_end_of_stack)
1214                         arch_handle_exception (ctx, obj);
1215                 else
1216                         mono_jit_abort (obj);
1217
1218         } else {
1219                 gpointer *lmf_addr = TlsGetValue (lmf_thread_id);
1220                 MonoLMF *lmf;
1221                 MonoMethod *m;
1222
1223                 g_assert (lmf_addr);
1224                 lmf = *((MonoLMF **)lmf_addr);
1225
1226                 if (!lmf)
1227                         mono_jit_abort (obj);
1228
1229                 m = lmf->method;
1230
1231                 *lmf_addr = lmf->previous_lmf;
1232
1233                 ctx->esi = lmf->esi;
1234                 ctx->edi = lmf->edi;
1235                 ctx->ebx = lmf->ebx;
1236                 ctx->ebp = lmf->ebp;
1237                 ctx->eip = lmf->eip;
1238                 ctx->esp = (unsigned long)lmf;
1239
1240                 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1241                         char  *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1242                         char  *tmp;
1243
1244                         if (!strcmp (strace, "TODO: implement stack traces"))
1245                                 strace = g_strdup ("");
1246
1247                         tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,  
1248                                                m->klass->name, m->name);
1249
1250                         g_free (strace);
1251
1252                         ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1253                         g_free (tmp);
1254                 }
1255
1256                 if (ctx->eip < (unsigned)mono_end_of_stack)
1257                         arch_handle_exception (ctx, obj);
1258                 else
1259                         mono_jit_abort (obj);
1260         }
1261
1262         g_assert_not_reached ();
1263 }
1264
1265 static void
1266 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
1267                  unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
1268                  unsigned long eip,  unsigned long esp)
1269 {
1270         struct sigcontext ctx;
1271
1272         ctx.esp = esp;
1273         ctx.eip = eip;
1274         ctx.ebp = ebp;
1275         ctx.edi = edi;
1276         ctx.esi = esi;
1277         ctx.ebx = ebx;
1278         ctx.edx = edx;
1279         ctx.ecx = ecx;
1280         ctx.eax = eax;
1281         
1282         arch_handle_exception (&ctx, exc);
1283
1284         g_assert_not_reached ();
1285 }
1286
1287 /**
1288  * arch_get_throw_exception:
1289  *
1290  * Returns a function pointer which can be used to raise 
1291  * exceptions. The returned function has the following 
1292  * signature: void (*func) (MonoException *exc); 
1293  * For example to raise an arithmetic exception you can use:
1294  *
1295  * x86_push_imm (code, mono_get_exception_arithmetic ()); 
1296  * x86_call_code (code, arch_get_throw_exception ()); 
1297  *
1298  */
1299 gpointer 
1300 arch_get_throw_exception (void)
1301 {
1302         static guint8 start [24];
1303         static int inited = 0;
1304         guint8 *code;
1305
1306         if (inited)
1307                 return start;
1308
1309         inited = 1;
1310         code = start;
1311
1312         x86_push_reg (code, X86_ESP);
1313         x86_push_membase (code, X86_ESP, 4); /* IP */
1314         x86_push_membase (code, X86_ESP, 12); /* exception */
1315         x86_push_reg (code, X86_EBP);
1316         x86_push_reg (code, X86_EDI);
1317         x86_push_reg (code, X86_ESI);
1318         x86_push_reg (code, X86_EBX);
1319         x86_push_reg (code, X86_EDX);
1320         x86_push_reg (code, X86_ECX);
1321         x86_push_reg (code, X86_EAX);
1322         x86_call_code (code, throw_exception);
1323         /* we should never reach this breakpoint */
1324         x86_breakpoint (code);
1325
1326         g_assert ((code - start) < 24);
1327         return start;
1328 }
1329
1330 /**
1331  * arch_get_throw_exception_by_name:
1332  *
1333  * Returns a function pointer which can be used to raise 
1334  * corlib exceptions. The returned function has the following 
1335  * signature: void (*func) (char *exc_name); 
1336  * For example to raise an arithmetic exception you can use:
1337  *
1338  * x86_push_imm (code, "ArithmeticException"); 
1339  * x86_call_code (code, arch_get_throw_exception ()); 
1340  *
1341  */
1342 gpointer 
1343 arch_get_throw_exception_by_name ()
1344 {
1345         static guint8 start [32];
1346         static int inited = 0;
1347         guint8 *code;
1348
1349         if (inited)
1350                 return start;
1351
1352         inited = 1;
1353         code = start;
1354
1355         /* fixme: we do not save EAX, EDX, ECD - unsure if we need that */
1356
1357         x86_push_membase (code, X86_ESP, 4); /* exception name */
1358         x86_push_imm (code, "System");
1359         x86_push_imm (code, mono_defaults.exception_class->image);
1360         x86_call_code (code, mono_exception_from_name);
1361         x86_alu_reg_imm (code, X86_ADD, X86_ESP, 12);
1362         /* save the newly create object (overwrite exception name)*/
1363         x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
1364         x86_jump_code (code, arch_get_throw_exception ());
1365
1366         g_assert ((code - start) < 32);
1367
1368         return start;
1369 }       
1370
1371
1372
1373