2 * emit-x86.c: Support functions for emitting x86 code
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
8 * (C) 2001 Ximian, Inc.
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>
27 enter_method (MonoMethod *method, gpointer ebp)
33 printf ("ENTER: %s.%s::%s\n(", method->klass->name_space,
34 method->klass->name, method->name);
38 if (ISSTRUCT (method->signature->ret)) {
41 g_assert (!method->signature->ret->byref);
43 size = mono_type_size (method->signature->ret, &align);
45 printf ("VALUERET:%p, ", *((gpointer *)ebp));
46 ebp += sizeof (gpointer);
49 if (method->signature->hasthis) {
50 if (method->klass->valuetype) {
51 printf ("value:%p, ", *((gpointer *)ebp));
53 o = *((MonoObject **)ebp);
59 if (class == mono_defaults.string_class) {
60 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
62 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
65 ebp += sizeof (gpointer);
68 for (i = 0; i < method->signature->param_count; ++i) {
69 MonoType *type = method->signature->params [i];
71 size = mono_type_size (type, &align);
74 case MONO_TYPE_BOOLEAN:
84 printf ("%d, ", *((int *)(ebp)));
86 case MONO_TYPE_STRING: {
87 MonoString *s = *((MonoString **)ebp);
89 g_assert (((MonoObject *)s)->klass == mono_defaults.string_class);
90 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
92 printf ("[STRING:null], ");
96 case MONO_TYPE_OBJECT: {
97 o = *((MonoObject **)ebp);
100 if (class == mono_defaults.string_class) {
101 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
102 } else if (class == mono_defaults.int32_class) {
103 printf ("[INT32:%p:%d], ", o, *(gint32 *)((gpointer)o + sizeof (MonoObject)));
105 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
107 printf ("%p, ", *((gpointer *)(ebp)));
112 case MONO_TYPE_FNPTR:
113 case MONO_TYPE_ARRAY:
114 case MONO_TYPE_SZARRAY:
115 printf ("%p, ", *((gpointer *)(ebp)));
118 printf ("%lld, ", *((gint64 *)(ebp)));
121 printf ("%f, ", *((float *)(ebp)));
124 printf ("%f, ", *((double *)(ebp)));
126 case MONO_TYPE_VALUETYPE:
128 for (j = 0; j < size; j++)
129 printf ("%02x,", *((guint8*)ebp +j));
137 ebp = (gpointer)((unsigned)ebp & ~(3));
144 leave_method (MonoMethod *method, int edx, int eax, double test)
148 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
149 method->klass->name, method->name);
151 switch (method->signature->ret->type) {
154 case MONO_TYPE_BOOLEAN:
156 printf ("TRUE:%d", eax);
170 printf ("EAX=%d", eax);
172 case MONO_TYPE_STRING: {
173 MonoString *s = (MonoString *)eax;
176 g_assert (((MonoObject *)s)->klass == mono_defaults.string_class);
177 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
179 printf ("[STRING:null], ");
182 case MONO_TYPE_OBJECT: {
183 MonoObject *o = (MonoObject *)eax;
185 if (o->klass == mono_defaults.boolean_class) {
186 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
187 } else if (o->klass == mono_defaults.int32_class) {
188 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
191 printf ("[%s.%s:%p]", o->klass->name_space, o->klass->name, o);
193 printf ("[OBJECT:%p]", (gpointer)eax);
197 case MONO_TYPE_CLASS:
199 case MONO_TYPE_FNPTR:
200 case MONO_TYPE_ARRAY:
201 case MONO_TYPE_SZARRAY:
202 printf ("EAX=%p", (gpointer)eax);
205 *((gint32 *)&l) = eax;
206 *((gint32 *)&l + 1) = edx;
207 printf ("EAX/EDX=%lld", l);
210 printf ("FP=%f\n", test);
213 printf ("(unknown return type)");
220 * arch_emit_prologue:
221 * @cfg: pointer to status information
223 * Emits the function prolog.
226 arch_emit_prologue (MonoFlowGraph *cfg)
228 x86_push_reg (cfg->code, X86_EBP);
229 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
231 if (cfg->locals_size)
232 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
234 if (mono_regset_reg_used (cfg->rs, X86_EBX))
235 x86_push_reg (cfg->code, X86_EBX);
237 if (mono_regset_reg_used (cfg->rs, X86_EDI))
238 x86_push_reg (cfg->code, X86_EDI);
240 if (mono_regset_reg_used (cfg->rs, X86_ESI))
241 x86_push_reg (cfg->code, X86_ESI);
243 if (mono_jit_trace_calls) {
244 x86_push_reg (cfg->code, X86_EBP);
245 x86_push_imm (cfg->code, cfg->method);
246 x86_mov_reg_imm (cfg->code, X86_EAX, enter_method);
247 x86_call_reg (cfg->code, X86_EAX);
248 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
253 * arch_emit_epilogue:
254 * @cfg: pointer to status information
256 * Emits the function epilog.
259 arch_emit_epilogue (MonoFlowGraph *cfg)
261 if (mono_jit_trace_calls) {
262 x86_fld_reg (cfg->code, 0);
263 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
264 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
265 x86_push_reg (cfg->code, X86_EAX);
266 x86_push_reg (cfg->code, X86_EDX);
267 x86_push_imm (cfg->code, cfg->method);
268 x86_mov_reg_imm (cfg->code, X86_EAX, leave_method);
269 x86_call_reg (cfg->code, X86_EAX);
270 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
271 x86_pop_reg (cfg->code, X86_EDX);
272 x86_pop_reg (cfg->code, X86_EAX);
273 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
276 if (mono_regset_reg_used (cfg->rs, X86_ESI))
277 x86_pop_reg (cfg->code, X86_ESI);
279 if (mono_regset_reg_used (cfg->rs, X86_EDI))
280 x86_pop_reg (cfg->code, X86_EDI);
282 if (mono_regset_reg_used (cfg->rs, X86_EBX))
283 x86_pop_reg (cfg->code, X86_EBX);
285 x86_leave (cfg->code);
290 * get_unbox_trampoline:
293 * when value type methods are called through the vtable we need to unbox the
294 * this argument. This method returns a pointer to a trampoline which does
295 * unboxing before calling the method
298 get_unbox_trampoline (MonoMethod *m)
300 gpointer p = arch_compile_method (m);
301 guint8 *code, *start;
304 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
307 start = code = g_malloc (16);
309 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
310 x86_jump_code (code, p);
311 g_assert ((code - start) < 16);
317 * x86_magic_trampoline:
318 * @eax: saved x86 register
319 * @ecx: saved x86 register
320 * @edx: saved x86 register
321 * @esi: saved x86 register
322 * @edi: saved x86 register
323 * @ebx: saved x86 register
324 * @code: pointer into caller code
325 * @method: the method to translate
327 * This method is called by the trampoline functions for virtual
328 * methods. It inspects the caller code to find the address of the
329 * vtable slot, then calls the JIT compiler and writes the address
330 * of the compiled method back to the vtable. All virtual methods
331 * are called with: x86_call_membase (inst, basereg, disp). We always
332 * use 32 bit displacement to ensure that the length of the call
333 * instruction is 6 bytes. We need to get the value of the basereg
334 * and the constant displacement.
337 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
338 int ebx, guint8 *code, MonoMethod *m)
344 /* go to the start of the call instruction */
346 g_assert (*code == 0xff);
350 g_assert ((ab >> 6) == 2);
352 /* extract the register number containing the address */
356 /* extract the displacement */
357 disp = *((gint32*)code);
379 g_assert_not_reached ();
384 if (m->klass->valuetype) {
385 return *((gpointer *)o) = get_unbox_trampoline (m);
387 return *((gpointer *)o) = arch_compile_method (m);
391 * arch_create_jit_trampoline:
392 * @method: pointer to the method info
394 * Creates a trampoline function for virtual methods. If the created
395 * code is called it first starts JIT compilation of method,
396 * and then calls the newly created method. I also replaces the
397 * corresponding vtable entry (see x86_magic_trampoline).
399 * Returns: a pointer to the newly created code
402 arch_create_jit_trampoline (MonoMethod *method)
405 static guint8 *vc = NULL;
411 vc = buf = g_malloc (24);
413 /* push the return address onto the stack */
414 x86_push_membase (buf, X86_ESP, 4);
416 /* save all register values */
417 x86_push_reg (buf, X86_EBX);
418 x86_push_reg (buf, X86_EDI);
419 x86_push_reg (buf, X86_ESI);
420 x86_push_reg (buf, X86_EDX);
421 x86_push_reg (buf, X86_ECX);
422 x86_push_reg (buf, X86_EAX);
424 x86_call_code (buf, x86_magic_trampoline);
425 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
427 /* call the compiled method */
428 x86_jump_reg (buf, X86_EAX);
430 g_assert ((buf - vc) <= 24);
433 code = buf = g_malloc (16);
434 x86_push_imm (buf, method);
435 x86_jump_code (buf, vc);
436 g_assert ((buf - code) <= 16);
442 * arch_create_simple_jit_trampoline:
443 * @method: pointer to the method info
445 * Creates a trampoline function for method. If the created
446 * code is called it first starts JIT compilation of method,
447 * and then calls the newly created method. I also replaces the
448 * address in method->addr with the result of the JIT
449 * compilation step (in arch_compile_method).
451 * Returns: a pointer to the newly created code
454 arch_create_simple_jit_trampoline (MonoMethod *method)
461 /* we never free the allocated code buffer */
462 code = buf = g_malloc (16);
463 x86_push_imm (buf, method);
464 x86_call_code (buf, arch_compile_method);
465 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
466 /* jump to the compiled method */
467 x86_jump_reg (buf, X86_EAX);
468 g_assert ((buf - code) < 16);
474 mono_label_cfg (MonoFlowGraph *cfg)
478 for (i = 0; i < cfg->block_count; i++) {
479 GPtrArray *forest = cfg->bblocks [i].forest;
480 const int top = forest->len;
482 for (j = 0; j < top; j++) {
483 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
486 mbstate = mono_burg_label (t1, cfg);
489 if (mono_debug_handle)
491 g_warning ("tree does not match");
492 mono_print_ctree (t1); printf ("\n\n");
494 mono_print_forest (forest);
495 g_assert_not_reached ();
502 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
505 case MB_TERM_CALL_I4:
506 case MB_TERM_CALL_I8:
507 case MB_TERM_CALL_R8:
508 // case MB_TERM_CALL_VOID :
509 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
510 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
511 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
518 case MB_NTERM_lreg: {
523 tree->exclude_mask |= (1 << X86_ECX);
524 tree->left->exclude_mask |= (1 << X86_ECX);
531 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
532 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
533 if (goal == MB_NTERM_reg) {
534 tree->left->exclude_mask |= (1 << X86_EDX);
535 tree->right->exclude_mask |= (1 << X86_EDX) | (1 << X86_EAX);
549 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
552 int ern = mono_burg_rule (tree->state, goal);
553 guint16 *nts = mono_burg_nts [ern];
556 mono_burg_kids (tree, ern, kids);
558 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
560 if (nts [0] && kids [0] == tree) {
562 tree_allocate_regs (kids [0], nts [0], rs);
566 for (i = 0; nts [i]; i++)
567 tree_preallocate_regs (kids [i], nts [i], rs);
569 for (i = 0; nts [i]; i++)
570 tree_allocate_regs (kids [i], nts [i], rs);
572 for (i = 0; nts [i]; i++) {
573 mono_regset_free_reg (rs, kids [i]->reg1);
574 mono_regset_free_reg (rs, kids [i]->reg2);
575 mono_regset_free_reg (rs, kids [i]->reg3);
580 if (tree->reg1 < 0) {
581 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
582 g_assert (tree->reg1 != -1);
587 if (tree->reg1 < 0) {
588 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
589 g_assert (tree->reg1 != -1);
591 if (tree->reg2 < 0) {
592 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
593 g_assert (tree->reg2 != -1);
598 /* fixme: allocate floating point registers */
602 if (tree->op == MB_TERM_ADD) {
603 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
604 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
609 if (tree->op == MB_TERM_ADD) {
610 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
615 if (tree->op == MB_TERM_SHL ||
616 tree->op == MB_TERM_MUL) {
617 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
625 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
626 tree->emit = mono_burg_func [ern];
630 arch_allocate_regs (MonoFlowGraph *cfg)
634 for (i = 0; i < cfg->block_count; i++) {
635 GPtrArray *forest = cfg->bblocks [i].forest;
636 const int top = forest->len;
638 for (j = 0; j < top; j++) {
639 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
640 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
641 tree_allocate_regs (t1, 1, cfg->rs);
642 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
643 g_assert (cfg->rs->free_mask == 0xffffffff);
649 tree_emit (int goal, MonoFlowGraph *cfg, MBTree *tree)
652 int i, ern = mono_burg_rule (tree->state, goal);
653 guint16 *nts = mono_burg_nts [ern];
657 mono_burg_kids (tree, ern, kids);
659 for (i = 0; nts [i]; i++)
660 tree_emit (nts [i], cfg, kids [i]);
662 tree->addr = offset = cfg->code - cfg->start;
664 // we assume an instruction uses a maximum of 128 bytes
665 if ((cfg->code_size - offset) <= 128) {
666 int add = MIN ((cfg->code_size * 2), 1024);
668 cfg->code_size += add;
669 cfg->start = g_realloc (cfg->start, cfg->code_size);
670 g_assert (cfg->start);
671 cfg->code = cfg->start + offset;
674 if ((emit = mono_burg_func [ern]))
677 g_assert ((cfg->code - cfg->start) < cfg->code_size);
681 mono_emit_cfg (MonoFlowGraph *cfg)
685 for (i = 0; i < cfg->block_count; i++) {
686 MonoBBlock *bb = &cfg->bblocks [i];
687 GPtrArray *forest = bb->forest;
688 const int top = forest->len;
690 bb->addr = cfg->code - cfg->start;
692 for (j = 0; j < top; j++) {
693 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
695 tree_emit (1, cfg, t1);
699 cfg->epilog = cfg->code - cfg->start;
703 mono_compute_branches (MonoFlowGraph *cfg)
710 for (j = 0; j < cfg->block_count; j++) {
711 MonoBBlock *bb = &cfg->bblocks [j];
712 GPtrArray *forest = bb->forest;
713 const int top = forest->len;
715 for (i = 0; i < top; i++) {
716 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
720 if (t1->op == MB_TERM_SWITCH) {
721 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
722 guint32 *rt = (guint32 *)t1->data.p;
724 int m = *((guint32 *)t1->data.p) + 1;
727 for (j = 1; j <= m; j++)
728 rt [j] = (int)(jt [j]->addr + cfg->start);
731 /* emit the jump instruction again to update addresses */
732 cfg->code = cfg->start + t1->addr;
733 ((MBEmitFunc)t1->emit) (t1, cfg);
743 match_debug_method (MonoMethod* method)
745 GList *tmp = mono_debug_methods;
747 for (; tmp; tmp = tmp->next) {
748 if (strcmp (method->name, tmp->data) == 0) {
756 * arch_compile_method:
757 * @method: pointer to the method info
759 * JIT compilation of a single method. This method also writes the result
760 * back to method->addr, an thus overwrites the trampoline function.
762 * Returns: a pointer to the newly created code.
765 arch_compile_method (MonoMethod *method)
768 MonoMemPool *mp = mono_mempool_new ();
770 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
771 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
773 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
774 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
775 method->klass->name, method->name);
778 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
779 MonoClassField *field;
780 const char *name = method->name;
781 static guint target_offset = 0;
782 static guint method_offset = 0;
784 gboolean delegate = FALSE;
786 if (method->klass->parent &&
787 method->klass->parent->parent == mono_defaults.delegate_class)
790 if (!target_offset) {
791 mono_class_init (mono_defaults.delegate_class);
793 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
794 target_offset = field->offset;
795 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
796 method_offset = field->offset;
799 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
800 method->addr = code = g_malloc (32);
801 x86_push_reg (code, X86_EBP);
802 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
804 /* load the this pointer */
805 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
806 /* load m_target arg */
807 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
809 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
810 /* load method_ptr arg */
811 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
812 /* store method_ptr */
813 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
818 g_assert ((code - (guint8*)method->addr) < 32);
820 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
821 MonoMethodSignature *csig = method->signature;
822 int i, target, this_pos = 4;
825 method->addr = g_malloc (64);
827 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
828 g_assert (!csig->ret->byref);
832 for (i = 0; i < 2; i ++) {
834 /* load the this pointer */
835 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
837 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
838 /* check if zero (static method call without this pointer) */
839 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
840 x86_branch32 (code, X86_CC_EQ, target, TRUE);
843 /* virtual delegate methods: we have to replace the this pointer
844 * withe the actual target */
845 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
846 /* jump to method_ptr() */
847 x86_jump_membase (code, X86_EAX, method_offset);
849 /* static delegate methods: we have to remove the this pointer
850 * from the activation frame */
851 target = code - source;
853 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
854 x86_mov_membase_reg (code, X86_ESP, 8, X86_EDX, 4);
856 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 0, 4);
857 x86_mov_membase_reg (code, X86_ESP, 4, X86_EDX, 4);
858 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
860 /* jump to method_ptr() */
861 x86_jump_membase (code, X86_EAX, method_offset);
864 g_assert ((code - (guint8*)method->addr) < 64);
867 if (mono_debug_handle)
869 g_error ("Don't know how to exec runtime method %s.%s::%s",
870 method->klass->name_space, method->klass->name, method->name);
874 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
875 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
877 cfg = mono_cfg_new (method, mp);
879 mono_analyze_flow (cfg);
883 mono_analyze_stack (cfg);
887 cfg->rs = mono_regset_new (X86_NREG);
888 mono_regset_reserve_reg (cfg->rs, X86_ESP);
889 mono_regset_reserve_reg (cfg->rs, X86_EBP);
891 cfg->code_size = 256;
892 cfg->start = cfg->code = g_malloc (cfg->code_size);
894 if (match_debug_method (method))
895 x86_breakpoint (cfg->code);
897 if (mono_jit_dump_forest) {
899 printf ("FOREST %s.%s:%s\n", method->klass->name_space,
900 method->klass->name, method->name);
901 for (i = 0; i < cfg->block_count; i++) {
902 printf ("BLOCK %d:\n", i);
903 mono_print_forest (cfg->bblocks [i].forest);
907 mono_label_cfg (cfg);
911 arch_allocate_regs (cfg);
913 /* align to 8 byte boundary */
914 cfg->locals_size += 7;
915 cfg->locals_size &= ~7;
917 arch_emit_prologue (cfg);
919 arch_emit_epilogue (cfg);
921 method->addr = cfg->start;
923 mono_compute_branches (cfg);
925 if (mono_jit_dump_asm) {
926 char *id = g_strdup_printf ("%s_%s__%s", method->klass->name_space,
927 method->klass->name, method->name);
928 mono_disassemble_code (cfg->start, cfg->code - cfg->start, id);
931 if (mono_debug_handle)
932 mono_debug_add_method (mono_debug_handle, cfg);
934 ji->code_size = cfg->code - cfg->start;
935 ji->used_regs = cfg->rs->used_mask;
937 ji->code_start = method->addr;
938 mono_jit_info_table_add (mono_jit_info_table, ji);
940 if (header->num_clauses) {
941 int i, start_block, end_block;
943 ji->num_clauses = header->num_clauses;
944 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
946 for (i = 0; i < header->num_clauses; i++) {
947 MonoExceptionClause *ec = &header->clauses [i];
948 MonoJitExceptionInfo *ei = &ji->clauses [i];
950 ei->flags = ec->flags;
951 ei->token_or_filter = ec->token_or_filter;
953 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
954 start_block = cfg->bcinfo [ec->try_offset].block_id;
955 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
956 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
958 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
959 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
961 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
962 start_block = cfg->bcinfo [ec->handler_offset].block_id;
963 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
965 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
969 mono_regset_free (cfg->rs);
973 mono_mempool_destroy (mp);
977 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
978 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
979 method->klass->name, method->name, method, method->addr);
987 * arch_get_restore_context:
989 * Returns a pointer to a method which restores a previously saved sigcontext.
992 arch_get_restore_context ()
994 static guint8 *start = NULL;
1000 /* restore_contect (struct sigcontext *ctx) */
1001 /* we do not restore X86_EAX, X86_EDX */
1003 start = code = malloc (1024);
1006 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
1008 /* get return address, stored in EDX */
1009 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
1012 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
1014 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
1016 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
1018 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
1020 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1021 /* restore ECX. the exception object is passed here to the catch handler */
1022 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
1024 /* jump to the saved IP */
1025 x86_jump_reg (code, X86_EDX);
1031 * arch_get_call_finally:
1033 * Returns a pointer to a method which calls a finally handler.
1036 arch_get_call_finally ()
1038 static guint8 *start = NULL;
1044 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1045 start = code = malloc (1024);
1047 x86_push_reg (code, X86_EBP);
1048 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1049 x86_push_reg (code, X86_EBX);
1050 x86_push_reg (code, X86_EDI);
1051 x86_push_reg (code, X86_ESI);
1054 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1056 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1058 x86_push_reg (code, X86_EBP);
1060 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1061 /* call the handler */
1062 x86_call_reg (code, X86_ECX);
1064 x86_pop_reg (code, X86_EBP);
1065 /* restore saved regs */
1066 x86_pop_reg (code, X86_ESI);
1067 x86_pop_reg (code, X86_EDI);
1068 x86_pop_reg (code, X86_EBX);
1076 * arch_handle_exception:
1077 * @ctx: saved processor state
1081 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1084 gpointer ip = (gpointer)ctx->eip;
1085 static void (*restore_context) (struct sigcontext *);
1086 static void (*call_finally) (struct sigcontext *, unsigned long);
1088 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1090 if (!restore_context)
1091 restore_context = arch_get_restore_context ();
1094 call_finally = arch_get_call_finally ();
1096 if (ji) { /* we are inside managed code */
1097 MonoMethod *m = ji->method;
1098 unsigned next_bp, next_ip;
1101 if (ji->num_clauses) {
1104 g_assert (ji->clauses);
1106 for (i = 0; i < ji->num_clauses; i++) {
1107 MonoJitExceptionInfo *ei = &ji->clauses [i];
1109 if (ei->try_start <= ip && ip <= (ei->try_end)) {
1111 if (ei->flags == 0 && mono_object_isinst (obj,
1112 mono_class_get (m->klass->image, ei->token_or_filter))) {
1114 ctx->eip = (unsigned long)ei->handler_start;
1115 ctx->ecx = (unsigned long)obj;
1116 restore_context (ctx);
1117 g_assert_not_reached ();
1122 /* no handler found - we need to call all finally handlers */
1123 for (i = 0; i < ji->num_clauses; i++) {
1124 MonoJitExceptionInfo *ei = &ji->clauses [i];
1126 if (ei->try_start <= ip && ip < (ei->try_end) &&
1127 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1128 call_finally (ctx, (unsigned long)ei->handler_start);
1133 /* continue unwinding */
1135 /* restore caller saved registers */
1136 if (ji->used_regs & X86_ESI_MASK) {
1137 ctx->esi = *((int *)ctx->ebp + offset);
1140 if (ji->used_regs & X86_EDI_MASK) {
1141 ctx->edi = *((int *)ctx->ebp + offset);
1144 if (ji->used_regs & X86_EBX_MASK) {
1145 ctx->ebx = *((int *)ctx->ebp + offset);
1148 ctx->esp = ctx->ebp;
1149 ctx->eip = *((int *)ctx->ebp + 1);
1150 ctx->ebp = *((int *)ctx->ebp);
1152 if (next_bp < (unsigned)mono_end_of_stack)
1153 arch_handle_exception (ctx, obj);
1155 mono_jit_abort (obj);
1158 gpointer *lmf_addr = TlsGetValue (lmf_thread_id);
1162 g_assert (lmf_addr);
1163 lmf = *((MonoLMF **)lmf_addr);
1166 mono_jit_abort (obj);
1170 *lmf_addr = lmf->previous_lmf;
1172 ctx->esi = lmf->esi;
1173 ctx->edi = lmf->edi;
1174 ctx->ebx = lmf->ebx;
1175 ctx->ebp = lmf->ebp;
1176 ctx->eip = lmf->eip;
1180 g_warning ("Exception inside unmanaged code. %s.%s::%s %p", m->klass->name_space,
1181 m->klass->name, m->name, lmf->previous_lmf);
1184 if (ctx->eip < (unsigned)mono_end_of_stack)
1185 arch_handle_exception (ctx, obj);
1187 mono_jit_abort (obj);
1190 g_assert_not_reached ();
1194 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
1195 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
1196 unsigned long eip, unsigned long esp)
1198 struct sigcontext ctx;
1210 arch_handle_exception (&ctx, exc);
1212 g_assert_not_reached ();
1216 arch_get_throw_exception (void)
1218 static guint8 *start = NULL;
1224 code = start = g_malloc (1024);
1226 x86_push_reg (code, X86_ESP);
1227 x86_push_membase (code, X86_ESP, 4); /* IP */
1228 x86_push_membase (code, X86_ESP, 12); /* exception */
1229 x86_push_reg (code, X86_EBP);
1230 x86_push_reg (code, X86_EDI);
1231 x86_push_reg (code, X86_ESI);
1232 x86_push_reg (code, X86_EBX);
1233 x86_push_reg (code, X86_EDX);
1234 x86_push_reg (code, X86_ECX);
1235 x86_push_reg (code, X86_EAX);
1236 x86_call_code (code, throw_exception);
1237 /* we should never reach this breakpoint */
1238 x86_breakpoint (code);