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 (method->signature->ret->type == MONO_TYPE_VALUETYPE) {
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_call_code (cfg->code, enter_method);
247 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
252 * arch_emit_epilogue:
253 * @cfg: pointer to status information
255 * Emits the function epilog.
258 arch_emit_epilogue (MonoFlowGraph *cfg)
260 if (mono_jit_trace_calls) {
261 x86_fld_reg (cfg->code, 0);
262 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
263 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
264 x86_push_reg (cfg->code, X86_EAX);
265 x86_push_reg (cfg->code, X86_EDX);
266 x86_push_imm (cfg->code, cfg->method);
267 x86_call_code (cfg->code, leave_method);
268 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
269 x86_pop_reg (cfg->code, X86_EDX);
270 x86_pop_reg (cfg->code, X86_EAX);
271 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
274 if (mono_regset_reg_used (cfg->rs, X86_ESI))
275 x86_pop_reg (cfg->code, X86_ESI);
277 if (mono_regset_reg_used (cfg->rs, X86_EDI))
278 x86_pop_reg (cfg->code, X86_EDI);
280 if (mono_regset_reg_used (cfg->rs, X86_EBX))
281 x86_pop_reg (cfg->code, X86_EBX);
283 x86_leave (cfg->code);
288 * get_unbox_trampoline:
291 * when value type methods are called through the vtable we need to unbox the
292 * this argument. This method returns a pointer to a trampoline which does
293 * unboxing before calling the method
296 get_unbox_trampoline (MonoMethod *m)
298 gpointer p = arch_compile_method (m);
299 guint8 *code, *start;
302 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
305 start = code = g_malloc (16);
307 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
308 x86_jump_code (code, p);
309 g_assert ((code - start) < 16);
315 * x86_magic_trampoline:
316 * @eax: saved x86 register
317 * @ecx: saved x86 register
318 * @edx: saved x86 register
319 * @esi: saved x86 register
320 * @edi: saved x86 register
321 * @ebx: saved x86 register
322 * @code: pointer into caller code
323 * @method: the method to translate
325 * This method is called by the trampoline functions for virtual
326 * methods. It inspects the caller code to find the address of the
327 * vtable slot, then calls the JIT compiler and writes the address
328 * of the compiled method back to the vtable. All virtual methods
329 * are called with: x86_call_membase (inst, basereg, disp). We always
330 * use 32 bit displacement to ensure that the length of the call
331 * instruction is 6 bytes. We need to get the value of the basereg
332 * and the constant displacement.
335 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
336 int ebx, guint8 *code, MonoMethod *m)
342 /* go to the start of the call instruction */
344 g_assert (*code == 0xff);
348 g_assert ((ab >> 6) == 2);
350 /* extract the register number containing the address */
354 /* extract the displacement */
355 disp = *((gint32*)code);
377 g_assert_not_reached ();
382 if (m->klass->valuetype) {
383 return *((gpointer *)o) = get_unbox_trampoline (m);
385 return *((gpointer *)o) = arch_compile_method (m);
389 * arch_create_jit_trampoline:
390 * @method: pointer to the method info
392 * Creates a trampoline function for virtual methods. If the created
393 * code is called it first starts JIT compilation of method,
394 * and then calls the newly created method. I also replaces the
395 * corresponding vtable entry (see x86_magic_trampoline).
397 * Returns: a pointer to the newly created code
400 arch_create_jit_trampoline (MonoMethod *method)
403 static guint8 *vc = NULL;
409 vc = buf = g_malloc (24);
411 /* push the return address onto the stack */
412 x86_push_membase (buf, X86_ESP, 4);
414 /* save all register values */
415 x86_push_reg (buf, X86_EBX);
416 x86_push_reg (buf, X86_EDI);
417 x86_push_reg (buf, X86_ESI);
418 x86_push_reg (buf, X86_EDX);
419 x86_push_reg (buf, X86_ECX);
420 x86_push_reg (buf, X86_EAX);
422 x86_call_code (buf, x86_magic_trampoline);
423 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
425 /* call the compiled method */
426 x86_jump_reg (buf, X86_EAX);
428 g_assert ((buf - vc) <= 24);
431 code = buf = g_malloc (16);
432 x86_push_imm (buf, method);
433 x86_jump_code (buf, vc);
434 g_assert ((buf - code) <= 16);
440 * arch_create_simple_jit_trampoline:
441 * @method: pointer to the method info
443 * Creates a trampoline function for method. If the created
444 * code is called it first starts JIT compilation of method,
445 * and then calls the newly created method. I also replaces the
446 * address in method->addr with the result of the JIT
447 * compilation step (in arch_compile_method).
449 * Returns: a pointer to the newly created code
452 arch_create_simple_jit_trampoline (MonoMethod *method)
459 /* we never free the allocated code buffer */
460 code = buf = g_malloc (16);
461 x86_push_imm (buf, method);
462 x86_call_code (buf, arch_compile_method);
463 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
464 /* jump to the compiled method */
465 x86_jump_reg (buf, X86_EAX);
466 g_assert ((buf - code) < 16);
472 mono_label_cfg (MonoFlowGraph *cfg)
476 for (i = 0; i < cfg->block_count; i++) {
477 GPtrArray *forest = cfg->bblocks [i].forest;
478 const int top = forest->len;
480 for (j = 0; j < top; j++) {
481 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
484 mbstate = mono_burg_label (t1, cfg);
487 if (mono_debug_handle)
489 g_warning ("tree does not match");
490 mono_print_ctree (t1); printf ("\n\n");
492 mono_print_forest (forest);
493 g_assert_not_reached ();
500 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
503 case MB_TERM_CALL_I4:
504 case MB_TERM_CALL_I8:
505 case MB_TERM_CALL_R8:
506 // case MB_TERM_CALL_VOID :
507 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
508 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
509 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
515 case MB_NTERM_lreg: {
520 tree->exclude_mask |= (1 << X86_ECX);
521 tree->left->exclude_mask |= (1 << X86_ECX);
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);
545 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
548 int ern = mono_burg_rule (tree->state, goal);
549 guint16 *nts = mono_burg_nts [ern];
552 mono_burg_kids (tree, ern, kids);
554 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
556 if (nts [0] && kids [0] == tree) {
558 tree_allocate_regs (kids [0], nts [0], rs);
562 for (i = 0; nts [i]; i++)
563 tree_preallocate_regs (kids [i], nts [i], rs);
565 for (i = 0; nts [i]; i++)
566 tree_allocate_regs (kids [i], nts [i], rs);
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);
576 if (tree->reg1 < 0) {
577 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
578 g_assert (tree->reg1 != -1);
583 if (tree->reg1 < 0) {
584 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
585 g_assert (tree->reg1 != -1);
587 if (tree->reg2 < 0) {
588 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
589 g_assert (tree->reg2 != -1);
594 /* fixme: allocate floating point registers */
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);
605 if (tree->op == MB_TERM_ADD) {
606 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
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);
621 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
622 tree->emit = mono_burg_func [ern];
626 arch_allocate_regs (MonoFlowGraph *cfg)
630 for (i = 0; i < cfg->block_count; i++) {
631 GPtrArray *forest = cfg->bblocks [i].forest;
632 const int top = forest->len;
634 for (j = 0; j < top; j++) {
635 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
636 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
637 tree_allocate_regs (t1, 1, cfg->rs);
638 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
639 g_assert (cfg->rs->free_mask == 0xffffffff);
645 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
648 int i, ern = mono_burg_rule (tree->state, goal);
649 guint16 *nts = mono_burg_nts [ern];
652 mono_burg_kids (tree, ern, kids);
654 for (i = 0; nts [i]; i++)
655 tree_emit (nts [i], s, kids [i]);
657 tree->addr = s->code - s->start;
659 if ((emit = mono_burg_func [ern]))
664 mono_emit_cfg (MonoFlowGraph *cfg)
668 for (i = 0; i < cfg->block_count; i++) {
669 MonoBBlock *bb = &cfg->bblocks [i];
670 GPtrArray *forest = bb->forest;
671 const int top = forest->len;
673 bb->addr = cfg->code - cfg->start;
675 for (j = 0; j < top; j++) {
676 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
677 tree_emit (1, cfg, t1);
681 cfg->epilog = cfg->code - cfg->start;
685 mono_compute_branches (MonoFlowGraph *cfg)
692 for (j = 0; j < cfg->block_count; j++) {
693 MonoBBlock *bb = &cfg->bblocks [j];
694 GPtrArray *forest = bb->forest;
695 const int top = forest->len;
697 for (i = 0; i < top; i++) {
698 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
702 if (t1->op == MB_TERM_SWITCH) {
703 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
704 guint32 *rt = (guint32 *)t1->data.p;
706 int m = *((guint32 *)t1->data.p) + 1;
709 for (j = 1; j <= m; j++)
710 rt [j] = (int)(jt [j]->addr + cfg->start);
713 /* emit the jump instruction again to update addresses */
714 cfg->code = cfg->start + t1->addr;
715 ((MBEmitFunc)t1->emit) (t1, cfg);
725 match_debug_method (MonoMethod* method)
727 GList *tmp = mono_debug_methods;
729 for (; tmp; tmp = tmp->next) {
730 if (strcmp (method->name, tmp->data) == 0) {
738 * arch_compile_method:
739 * @method: pointer to the method info
741 * JIT compilation of a single method. This method also writes the result
742 * back to method->addr, an thus overwrites the trampoline function.
744 * Returns: a pointer to the newly created code.
747 arch_compile_method (MonoMethod *method)
750 MonoMemPool *mp = mono_mempool_new ();
752 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
753 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
755 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
756 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
757 method->klass->name, method->name);
760 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
761 MonoClassField *field;
762 const char *name = method->name;
763 static guint target_offset = 0;
764 static guint method_offset = 0;
766 gboolean delegate = FALSE;
768 if (method->klass->parent &&
769 method->klass->parent->parent == mono_defaults.delegate_class)
772 if (!target_offset) {
773 mono_class_init (mono_defaults.delegate_class);
775 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
776 target_offset = field->offset;
777 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
778 method_offset = field->offset;
781 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
782 method->addr = code = g_malloc (32);
783 x86_push_reg (code, X86_EBP);
784 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
786 /* load the this pointer */
787 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
788 /* load m_target arg */
789 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
791 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
792 /* load method_ptr arg */
793 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
794 /* store method_ptr */
795 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
800 g_assert ((code - (guint8*)method->addr) < 32);
802 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
803 MonoMethodSignature *csig = method->signature;
804 int i, target, this_pos = 4;
807 method->addr = g_malloc (32);
809 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
810 g_assert (!csig->ret->byref);
814 for (i = 0; i < 2; i ++) {
816 /* load the this pointer */
817 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
819 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
820 /* check if zero (static method call without this pointer) */
821 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
822 x86_branch32 (code, X86_CC_EQ, target, TRUE);
825 /* virtual call - we have to replace the this pointer */
826 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
828 /* jump to method_ptr() */
829 target = code - source;
830 x86_jump_membase (code, X86_EAX, method_offset);
833 g_assert ((code - (guint8*)method->addr) < 32);
836 if (mono_debug_handle)
838 g_error ("Don't know how to exec runtime method %s.%s::%s",
839 method->klass->name_space, method->klass->name, method->name);
843 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
844 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
846 cfg = mono_cfg_new (method, mp);
848 mono_analyze_flow (cfg);
852 mono_analyze_stack (cfg);
857 cfg->rs = mono_regset_new (X86_NREG);
858 mono_regset_reserve_reg (cfg->rs, X86_ESP);
859 mono_regset_reserve_reg (cfg->rs, X86_EBP);
861 // fixme: remove limitation to 8192 bytes
862 ji->code_size = 8192*2;
863 method->addr = cfg->start = cfg->code = g_malloc (ji->code_size);
865 if (match_debug_method (method))
866 x86_breakpoint (cfg->code);
868 if (mono_jit_dump_forest) {
870 printf ("FOREST %s.%s:%s\n", method->klass->name_space,
871 method->klass->name, method->name);
872 for (i = 0; i < cfg->block_count; i++) {
873 printf ("BLOCK %d:\n", i);
874 mono_print_forest (cfg->bblocks [i].forest);
878 mono_label_cfg (cfg);
882 arch_allocate_regs (cfg);
884 /* align to 8 byte boundary */
885 cfg->locals_size += 7;
886 cfg->locals_size &= ~7;
888 arch_emit_prologue (cfg);
892 arch_emit_epilogue (cfg);
894 g_assert ((cfg->code - cfg->start) < ji->code_size);
896 mono_compute_branches (cfg);
898 if (mono_jit_dump_asm)
899 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
901 if (mono_debug_handle)
902 mono_debug_add_method (mono_debug_handle, cfg);
904 ji->used_regs = cfg->rs->used_mask;
906 ji->code_start = method->addr;
907 mono_jit_info_table_add (mono_jit_info_table, ji);
909 if (header->num_clauses) {
910 int i, start_block, end_block;
912 ji->num_clauses = header->num_clauses;
913 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
915 for (i = 0; i < header->num_clauses; i++) {
916 MonoExceptionClause *ec = &header->clauses [i];
917 MonoJitExceptionInfo *ei = &ji->clauses [i];
919 ei->flags = ec->flags;
920 ei->token_or_filter = ec->token_or_filter;
922 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
923 start_block = cfg->bcinfo [ec->try_offset].block_id;
924 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
925 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
927 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
928 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
930 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
931 start_block = cfg->bcinfo [ec->handler_offset].block_id;
932 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
934 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
938 mono_regset_free (cfg->rs);
942 mono_mempool_destroy (mp);
946 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
947 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
948 method->klass->name, method->name, method, method->addr);
956 * arch_get_restore_context:
958 * Returns a pointer to a method which restores a previously saved sigcontext.
961 arch_get_restore_context ()
963 static guint8 *start = NULL;
969 /* restore_contect (struct sigcontext *ctx) */
970 /* we do not restore X86_EAX, X86_EDX */
972 start = code = malloc (1024);
975 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
977 /* get return address, stored in EDX */
978 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
981 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
983 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
985 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
987 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
989 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
990 /* restore ECX. the exception object is passed here to the catch handler */
991 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
993 /* jump to the saved IP */
994 x86_jump_reg (code, X86_EDX);
1000 * arch_get_call_finally:
1002 * Returns a pointer to a method which calls a finally handler.
1005 arch_get_call_finally ()
1007 static guint8 *start = NULL;
1013 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1014 start = code = malloc (1024);
1016 x86_push_reg (code, X86_EBP);
1017 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1018 x86_push_reg (code, X86_EBX);
1019 x86_push_reg (code, X86_EDI);
1020 x86_push_reg (code, X86_ESI);
1023 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1025 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1027 x86_push_reg (code, X86_EBP);
1029 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1030 /* call the handler */
1031 x86_call_reg (code, X86_ECX);
1033 x86_pop_reg (code, X86_EBP);
1034 /* restore saved regs */
1035 x86_pop_reg (code, X86_ESI);
1036 x86_pop_reg (code, X86_EDI);
1037 x86_pop_reg (code, X86_EBX);
1045 * arch_handle_exception:
1046 * @ctx: saved processor state
1050 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1053 gpointer ip = (gpointer)ctx->eip;
1054 static void (*restore_context) (struct sigcontext *);
1055 static void (*call_finally) (struct sigcontext *, unsigned long);
1057 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1059 if (!restore_context)
1060 restore_context = arch_get_restore_context ();
1063 call_finally = arch_get_call_finally ();
1065 if (ji) { /* we are inside managed code */
1066 MonoMethod *m = ji->method;
1067 unsigned next_bp, next_ip;
1070 if (ji->num_clauses) {
1073 g_assert (ji->clauses);
1075 for (i = 0; i < ji->num_clauses; i++) {
1076 MonoJitExceptionInfo *ei = &ji->clauses [i];
1078 if (ei->try_start <= ip && ip <= (ei->try_end)) {
1080 if (ei->flags == 0 && mono_object_isinst (obj,
1081 mono_class_get (m->klass->image, ei->token_or_filter))) {
1083 ctx->eip = (unsigned long)ei->handler_start;
1084 ctx->ecx = (unsigned long)obj;
1085 restore_context (ctx);
1086 g_assert_not_reached ();
1091 /* no handler found - we need to call all finally handlers */
1092 for (i = 0; i < ji->num_clauses; i++) {
1093 MonoJitExceptionInfo *ei = &ji->clauses [i];
1095 if (ei->try_start <= ip && ip < (ei->try_end) &&
1096 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1097 call_finally (ctx, (unsigned long)ei->handler_start);
1102 /* continue unwinding */
1104 /* restore caller saved registers */
1105 if (ji->used_regs & X86_ESI_MASK) {
1106 ctx->esi = *((int *)ctx->ebp + offset);
1109 if (ji->used_regs & X86_EDI_MASK) {
1110 ctx->edi = *((int *)ctx->ebp + offset);
1113 if (ji->used_regs & X86_EBX_MASK) {
1114 ctx->ebx = *((int *)ctx->ebp + offset);
1117 ctx->esp = ctx->ebp;
1119 next_bp = *((int *)ctx->ebp);
1120 next_ip = *((int *)ctx->ebp + 1);
1122 if (next_bp < (unsigned)mono_end_of_stack) {
1126 arch_handle_exception (ctx, obj);
1129 mono_jit_abort (obj);
1133 /* fixme: implement exceptions inside unmanaged code */
1134 mono_jit_abort (obj);
1137 g_assert_not_reached ();