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 (", 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);
56 if (class == mono_defaults.string_class) {
57 printf ("this:%p[STRING:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
60 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
63 ebp += sizeof (gpointer);
66 for (i = 0; i < method->signature->param_count; ++i) {
67 MonoType *type = method->signature->params [i];
69 size = mono_type_size (type, &align);
72 case MONO_TYPE_BOOLEAN:
82 printf ("%d, ", *((int *)(ebp)));
84 case MONO_TYPE_STRING:
85 printf ("[STRING:%s], ", mono_string_to_utf8 (*(MonoString **)(ebp)));
89 case MONO_TYPE_OBJECT:
92 case MONO_TYPE_SZARRAY:
93 printf ("%p, ", *((gpointer *)(ebp)));
96 printf ("%lld, ", *((gint64 *)(ebp)));
99 printf ("%f, ", *((float *)(ebp)));
102 printf ("%f, ", *((double *)(ebp)));
104 case MONO_TYPE_VALUETYPE:
106 for (j = 0; j < size; j++)
107 printf ("%02x,", *((guint8*)ebp +j));
115 ebp = (gpointer)((unsigned)ebp & ~(3));
122 leave_method (MonoMethod *method, int edx, int eax, double test)
126 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
127 method->klass->name, method->name);
129 switch (method->signature->ret->type) {
132 case MONO_TYPE_BOOLEAN:
134 printf ("TRUE:%d", eax);
148 printf ("EAX=%d", eax);
150 case MONO_TYPE_STRING:
151 printf ("[STRING:%s]", mono_string_to_utf8 ((MonoString *)(eax)));
153 case MONO_TYPE_OBJECT: {
154 MonoObject *o = (MonoObject *)eax;
156 if (o->klass == mono_defaults.boolean_class) {
157 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
158 } else if (o->klass == mono_defaults.int32_class) {
159 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
162 printf ("[%s.%s:%p]", o->klass->name_space, o->klass->name, o);
164 printf ("[OBJECT:%p]", (gpointer)eax);
168 case MONO_TYPE_CLASS:
170 case MONO_TYPE_FNPTR:
171 case MONO_TYPE_ARRAY:
172 case MONO_TYPE_SZARRAY:
173 printf ("EAX=%p", (gpointer)eax);
176 *((gint32 *)&l) = eax;
177 *((gint32 *)&l + 1) = edx;
178 printf ("EAX/EDX=%lld", l);
181 printf ("FP=%f\n", test);
184 printf ("(unknown return type)");
191 * arch_emit_prologue:
192 * @cfg: pointer to status information
194 * Emits the function prolog.
197 arch_emit_prologue (MonoFlowGraph *cfg)
199 x86_push_reg (cfg->code, X86_EBP);
200 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
202 if (cfg->locals_size)
203 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
205 if (mono_regset_reg_used (cfg->rs, X86_EBX))
206 x86_push_reg (cfg->code, X86_EBX);
208 if (mono_regset_reg_used (cfg->rs, X86_EDI))
209 x86_push_reg (cfg->code, X86_EDI);
211 if (mono_regset_reg_used (cfg->rs, X86_ESI))
212 x86_push_reg (cfg->code, X86_ESI);
214 if (mono_jit_trace_calls) {
215 x86_push_reg (cfg->code, X86_EBP);
216 x86_push_imm (cfg->code, cfg->method);
217 x86_call_code (cfg->code, enter_method);
218 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
223 * arch_emit_epilogue:
224 * @cfg: pointer to status information
226 * Emits the function epilog.
229 arch_emit_epilogue (MonoFlowGraph *cfg)
231 if (mono_jit_trace_calls) {
232 x86_fld_reg (cfg->code, 0);
233 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
234 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
235 x86_push_reg (cfg->code, X86_EAX);
236 x86_push_reg (cfg->code, X86_EDX);
237 x86_push_imm (cfg->code, cfg->method);
238 x86_call_code (cfg->code, leave_method);
239 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
240 x86_pop_reg (cfg->code, X86_EDX);
241 x86_pop_reg (cfg->code, X86_EAX);
242 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
245 if (mono_regset_reg_used (cfg->rs, X86_ESI))
246 x86_pop_reg (cfg->code, X86_ESI);
248 if (mono_regset_reg_used (cfg->rs, X86_EDI))
249 x86_pop_reg (cfg->code, X86_EDI);
251 if (mono_regset_reg_used (cfg->rs, X86_EBX))
252 x86_pop_reg (cfg->code, X86_EBX);
254 x86_leave (cfg->code);
259 * x86_magic_trampoline:
260 * @eax: saved x86 register
261 * @ecx: saved x86 register
262 * @edx: saved x86 register
263 * @esi: saved x86 register
264 * @edi: saved x86 register
265 * @ebx: saved x86 register
266 * @code: pointer into caller code
267 * @method: the method to translate
269 * This method is called by the trampoline functions for virtual
270 * methods. It inspects the caller code to find the address of the
271 * vtable slot, then calls the JIT compiler and writes the address
272 * of the compiled method back to the vtable. All virtual methods
273 * are called with: x86_call_membase (inst, basereg, disp). We always
274 * use 32 bit displacement to ensure that the length of the call
275 * instruction is 6 bytes. We need to get the value of the basereg
276 * and the constant displacement.
279 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
280 int ebx, guint8 *code, MonoMethod *m)
286 /* go to the start of the call instruction */
288 g_assert (*code == 0xff);
292 g_assert ((ab >> 6) == 2);
294 /* extract the register number containing the address */
298 /* extract the displacement */
299 disp = *((gint32*)code);
321 g_assert_not_reached ();
326 return *((gpointer *)o) = arch_compile_method (m);
330 * arch_create_jit_trampoline:
331 * @method: pointer to the method info
333 * Creates a trampoline function for virtual methods. If the created
334 * code is called it first starts JIT compilation of method,
335 * and then calls the newly created method. I also replaces the
336 * corresponding vtable entry (see x86_magic_trampoline).
338 * Returns: a pointer to the newly created code
341 arch_create_jit_trampoline (MonoMethod *method)
344 static guint8 *vc = NULL;
350 vc = buf = g_malloc (24);
352 /* push the return address onto the stack */
353 x86_push_membase (buf, X86_ESP, 4);
355 /* save all register values */
356 x86_push_reg (buf, X86_EBX);
357 x86_push_reg (buf, X86_EDI);
358 x86_push_reg (buf, X86_ESI);
359 x86_push_reg (buf, X86_EDX);
360 x86_push_reg (buf, X86_ECX);
361 x86_push_reg (buf, X86_EAX);
363 x86_call_code (buf, x86_magic_trampoline);
364 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
366 /* call the compiled method */
367 x86_jump_reg (buf, X86_EAX);
369 g_assert ((buf - vc) <= 24);
372 code = buf = g_malloc (16);
373 x86_push_imm (buf, method);
374 x86_jump_code (buf, vc);
375 g_assert ((buf - code) <= 16);
381 * arch_create_simple_jit_trampoline:
382 * @method: pointer to the method info
384 * Creates a trampoline function for method. If the created
385 * code is called it first starts JIT compilation of method,
386 * and then calls the newly created method. I also replaces the
387 * address in method->addr with the result of the JIT
388 * compilation step (in arch_compile_method).
390 * Returns: a pointer to the newly created code
393 arch_create_simple_jit_trampoline (MonoMethod *method)
400 /* we never free the allocated code buffer */
401 code = buf = g_malloc (16);
402 x86_push_imm (buf, method);
403 x86_call_code (buf, arch_compile_method);
404 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
405 /* jump to the compiled method */
406 x86_jump_reg (buf, X86_EAX);
407 g_assert ((buf - code) < 16);
413 mono_label_cfg (MonoFlowGraph *cfg)
417 for (i = 0; i < cfg->block_count; i++) {
418 GPtrArray *forest = cfg->bblocks [i].forest;
419 const int top = forest->len;
421 for (j = 0; j < top; j++) {
422 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
425 mbstate = mono_burg_label (t1, cfg);
428 if (mono_debug_handle)
430 g_warning ("tree does not match");
431 mono_print_ctree (t1); printf ("\n\n");
433 mono_print_forest (forest);
434 g_assert_not_reached ();
441 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
444 case MB_TERM_CALL_I4:
445 case MB_TERM_CALL_I8:
446 case MB_TERM_CALL_R8:
447 // case MB_TERM_CALL_VOID :
448 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
449 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
450 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
456 case MB_NTERM_lreg: {
461 tree->exclude_mask |= (1 << X86_ECX);
462 tree->left->exclude_mask |= (1 << X86_ECX);
468 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
469 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
470 if (goal == MB_NTERM_reg) {
471 tree->left->exclude_mask |= (1 << X86_EDX);
472 tree->right->exclude_mask |= (1 << X86_EDX);
486 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
489 int ern = mono_burg_rule (tree->state, goal);
490 guint16 *nts = mono_burg_nts [ern];
493 mono_burg_kids (tree, ern, kids);
495 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
497 if (nts [0] && kids [0] == tree) {
499 tree_allocate_regs (kids [0], nts [0], rs);
503 for (i = 0; nts [i]; i++)
504 tree_preallocate_regs (kids [i], nts [i], rs);
506 for (i = 0; nts [i]; i++)
507 tree_allocate_regs (kids [i], nts [i], rs);
509 for (i = 0; nts [i]; i++) {
510 mono_regset_free_reg (rs, kids [i]->reg1);
511 mono_regset_free_reg (rs, kids [i]->reg2);
512 mono_regset_free_reg (rs, kids [i]->reg3);
517 if (tree->reg1 < 0) {
518 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
519 g_assert (tree->reg1 != -1);
524 if (tree->reg1 < 0) {
525 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
526 g_assert (tree->reg1 != -1);
528 if (tree->reg2 < 0) {
529 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
530 g_assert (tree->reg2 != -1);
535 /* fixme: allocate floating point registers */
539 if (tree->op == MB_TERM_ADD) {
540 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
541 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
546 if (tree->op == MB_TERM_ADD) {
547 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
552 if (tree->op == MB_TERM_SHL ||
553 tree->op == MB_TERM_MUL) {
554 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
562 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
563 tree->emit = mono_burg_func [ern];
567 arch_allocate_regs (MonoFlowGraph *cfg)
571 for (i = 0; i < cfg->block_count; i++) {
572 GPtrArray *forest = cfg->bblocks [i].forest;
573 const int top = forest->len;
575 for (j = 0; j < top; j++) {
576 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
577 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
578 tree_allocate_regs (t1, 1, cfg->rs);
579 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
580 g_assert (cfg->rs->free_mask == 0xffffffff);
586 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
589 int i, ern = mono_burg_rule (tree->state, goal);
590 guint16 *nts = mono_burg_nts [ern];
593 mono_burg_kids (tree, ern, kids);
595 for (i = 0; nts [i]; i++)
596 tree_emit (nts [i], s, kids [i]);
598 tree->addr = s->code - s->start;
600 if ((emit = mono_burg_func [ern]))
605 mono_emit_cfg (MonoFlowGraph *cfg)
609 for (i = 0; i < cfg->block_count; i++) {
610 MonoBBlock *bb = &cfg->bblocks [i];
611 GPtrArray *forest = bb->forest;
612 const int top = forest->len;
614 bb->addr = cfg->code - cfg->start;
616 for (j = 0; j < top; j++) {
617 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
618 tree_emit (1, cfg, t1);
622 cfg->epilog = cfg->code - cfg->start;
626 mono_compute_branches (MonoFlowGraph *cfg)
633 for (j = 0; j < cfg->block_count; j++) {
634 MonoBBlock *bb = &cfg->bblocks [j];
635 GPtrArray *forest = bb->forest;
636 const int top = forest->len;
638 for (i = 0; i < top; i++) {
639 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
643 if (t1->op == MB_TERM_SWITCH) {
644 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
645 guint32 *rt = (guint32 *)t1->data.p;
647 int m = *((guint32 *)t1->data.p) + 1;
650 for (j = 1; j <= m; j++)
651 rt [j] = (int)(jt [j]->addr + cfg->start);
654 /* emit the jump instruction again to update addresses */
655 cfg->code = cfg->start + t1->addr;
656 ((MBEmitFunc)t1->emit) (t1, cfg);
666 match_debug_method (MonoMethod* method)
668 GList *tmp = mono_debug_methods;
670 for (; tmp; tmp = tmp->next) {
671 if (strcmp (method->name, tmp->data) == 0) {
679 * arch_compile_method:
680 * @method: pointer to the method info
682 * JIT compilation of a single method. This method also writes the result
683 * back to method->addr, an thus overwrites the trampoline function.
685 * Returns: a pointer to the newly created code.
688 arch_compile_method (MonoMethod *method)
691 MonoMemPool *mp = mono_mempool_new ();
693 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
694 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
696 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
697 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
698 method->klass->name, method->name);
701 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
702 MonoClassField *field;
703 const char *name = method->name;
704 static guint target_offset = 0;
705 static guint method_offset = 0;
707 gboolean delegate = FALSE;
709 if (method->klass->parent &&
710 method->klass->parent->parent == mono_defaults.delegate_class)
713 if (!target_offset) {
714 mono_class_init (mono_defaults.delegate_class);
716 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
717 target_offset = field->offset;
718 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
719 method_offset = field->offset;
722 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
723 method->addr = code = g_malloc (32);
724 x86_push_reg (code, X86_EBP);
725 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
727 /* load the this pointer */
728 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
729 /* load m_target arg */
730 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
732 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
733 /* load method_ptr arg */
734 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
735 /* store method_ptr */
736 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
741 g_assert ((code - (guint8*)method->addr) < 32);
743 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
744 MonoMethodSignature *csig = method->signature;
745 int i, target, this_pos = 4;
748 method->addr = g_malloc (32);
750 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
751 g_assert (!csig->ret->byref);
755 for (i = 0; i < 2; i ++) {
757 /* load the this pointer */
758 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
760 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
761 /* check if zero (static method call without this pointer) */
762 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
763 x86_branch32 (code, X86_CC_EQ, target, TRUE);
766 /* virtual call - we have to replace the this pointer */
767 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
769 /* jump to method_ptr() */
770 target = code - source;
771 x86_jump_membase (code, X86_EAX, method_offset);
774 g_assert ((code - (guint8*)method->addr) < 32);
777 if (mono_debug_handle)
779 g_error ("Don't know how to exec runtime method %s.%s::%s",
780 method->klass->name_space, method->klass->name, method->name);
784 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
785 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
787 cfg = mono_cfg_new (method, mp);
789 mono_analyze_flow (cfg);
793 mono_analyze_stack (cfg);
798 cfg->rs = mono_regset_new (X86_NREG);
799 mono_regset_reserve_reg (cfg->rs, X86_ESP);
800 mono_regset_reserve_reg (cfg->rs, X86_EBP);
802 // fixme: remove limitation to 8192 bytes
803 ji->code_size = 8192*2;
804 method->addr = cfg->start = cfg->code = g_malloc (ji->code_size);
806 if (match_debug_method (method))
807 x86_breakpoint (cfg->code);
809 if (mono_jit_dump_forest) {
811 for (i = 0; i < cfg->block_count; i++) {
812 printf ("BLOCK %d:\n", i);
813 mono_print_forest (cfg->bblocks [i].forest);
817 mono_label_cfg (cfg);
821 arch_allocate_regs (cfg);
823 /* align to 8 byte boundary */
824 cfg->locals_size += 7;
825 cfg->locals_size &= ~7;
827 arch_emit_prologue (cfg);
831 arch_emit_epilogue (cfg);
833 g_assert ((cfg->code - cfg->start) < ji->code_size);
835 mono_compute_branches (cfg);
837 if (mono_jit_dump_asm)
838 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
840 if (mono_debug_handle)
841 mono_debug_add_method (mono_debug_handle, cfg);
843 ji->used_regs = cfg->rs->used_mask;
845 ji->code_start = method->addr;
846 mono_jit_info_table_add (mono_jit_info_table, ji);
848 if (header->num_clauses) {
849 int i, start_block, end_block;
851 ji->num_clauses = header->num_clauses;
852 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
854 for (i = 0; i < header->num_clauses; i++) {
855 MonoExceptionClause *ec = &header->clauses [i];
856 MonoJitExceptionInfo *ei = &ji->clauses [i];
858 ei->flags = ec->flags;
859 ei->token_or_filter = ec->token_or_filter;
861 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
862 start_block = cfg->bcinfo [ec->try_offset].block_id;
863 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
864 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
866 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
867 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
869 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
870 start_block = cfg->bcinfo [ec->handler_offset].block_id;
871 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
873 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
877 mono_regset_free (cfg->rs);
881 mono_mempool_destroy (mp);
885 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
886 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
887 method->klass->name, method->name, method, method->addr);
894 * arch_get_restore_context:
896 * Returns a pointer to a method which restores a previously saved sigcontext.
899 arch_get_restore_context ()
901 static guint8 *start = NULL;
907 /* restore_contect (struct sigcontext *ctx) */
908 /* we do not restore X86_EAX, X86_EDX */
910 start = code = malloc (1024);
913 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
915 /* get return address, stored in EDX */
916 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
919 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
921 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
923 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
925 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
927 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
928 /* restore ECX. the exception object is passed here to the catch handler */
929 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
931 /* jump to the saved IP */
932 x86_jump_reg (code, X86_EDX);
938 * arch_get_call_finally:
940 * Returns a pointer to a method which calls a finally handler.
943 arch_get_call_finally ()
945 static guint8 *start = NULL;
951 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
952 start = code = malloc (1024);
954 x86_push_reg (code, X86_EBP);
955 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
956 x86_push_reg (code, X86_EBX);
957 x86_push_reg (code, X86_EDI);
958 x86_push_reg (code, X86_ESI);
961 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
963 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
965 x86_push_reg (code, X86_EBP);
967 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
968 /* call the handler */
969 x86_call_reg (code, X86_ECX);
971 x86_pop_reg (code, X86_EBP);
972 /* restore saved regs */
973 x86_pop_reg (code, X86_ESI);
974 x86_pop_reg (code, X86_EDI);
975 x86_pop_reg (code, X86_EBX);
983 * arch_handle_exception:
984 * @ctx: saved processor state
988 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
991 gpointer ip = (gpointer)ctx->eip;
992 static void (*restore_context) (struct sigcontext *);
993 static void (*call_finally) (struct sigcontext *, unsigned long);
995 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
997 if (!restore_context)
998 restore_context = arch_get_restore_context ();
1001 call_finally = arch_get_call_finally ();
1003 if (ji) { /* we are inside managed code */
1004 MonoMethod *m = ji->method;
1005 unsigned next_bp, next_ip;
1008 if (ji->num_clauses) {
1011 g_assert (ji->clauses);
1013 for (i = 0; i < ji->num_clauses; i++) {
1014 MonoJitExceptionInfo *ei = &ji->clauses [i];
1016 if (ei->try_start <= ip && ip < (ei->try_end)) {
1019 if (ei->flags == 0 && mono_object_isinst (obj,
1020 mono_class_get (m->klass->image, ei->token_or_filter))) {
1022 ctx->eip = (unsigned long)ei->handler_start;
1023 ctx->ecx = (unsigned long)obj;
1024 restore_context (ctx);
1025 g_assert_not_reached ();
1030 /* no handler found - we need to call all finally handlers */
1031 for (i = 0; i < ji->num_clauses; i++) {
1032 MonoJitExceptionInfo *ei = &ji->clauses [i];
1034 if (ei->try_start <= ip && ip < (ei->try_end) &&
1035 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1036 call_finally (ctx, (unsigned long)ei->handler_start);
1041 /* continue unwinding */
1043 /* restore caller saved registers */
1044 if (ji->used_regs & X86_ESI_MASK) {
1045 ctx->esi = *((int *)ctx->ebp + offset);
1048 if (ji->used_regs & X86_EDI_MASK) {
1049 ctx->edi = *((int *)ctx->ebp + offset);
1052 if (ji->used_regs & X86_EBX_MASK) {
1053 ctx->ebx = *((int *)ctx->ebp + offset);
1056 ctx->esp = ctx->ebp;
1058 next_bp = *((int *)ctx->ebp);
1059 next_ip = *((int *)ctx->ebp + 1);
1061 if (next_bp < (unsigned)mono_end_of_stack) {
1065 arch_handle_exception (ctx, obj);
1068 mono_jit_abort (obj);
1072 /* fixme: implement exceptions inside unmanaged code */
1073 mono_jit_abort (obj);
1076 g_assert_not_reached ();