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 if ((size = mono_type_size (method->signature->ret, &align)) > 4 || size == 3) {
42 printf ("VALUERET:%p, ", *((gpointer *)ebp));
43 ebp += sizeof (gpointer);
47 if (method->signature->hasthis) {
48 if (method->klass->valuetype) {
49 printf ("value:%p, ", *((gpointer *)ebp));
51 o = *((MonoObject **)ebp);
53 if (class == mono_defaults.string_class) {
54 printf ("this:%p[STRING:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
57 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
59 ebp += sizeof (gpointer);
62 for (i = 0; i < method->signature->param_count; ++i) {
63 MonoType *type = method->signature->params [i];
65 size = mono_type_size (type, &align);
68 case MONO_TYPE_BOOLEAN:
78 printf ("%d, ", *((int *)(ebp)));
80 case MONO_TYPE_STRING:
81 printf ("[STRING:%s], ", mono_string_to_utf8 (*(MonoString **)(ebp)));
85 case MONO_TYPE_OBJECT:
88 case MONO_TYPE_SZARRAY:
89 printf ("%p, ", *((gpointer *)(ebp)));
92 printf ("%lld, ", *((gint64 *)(ebp)));
95 printf ("%f, ", *((double *)(ebp)));
97 case MONO_TYPE_VALUETYPE:
99 for (j = 0; j < size; j++)
100 printf ("%02x,", *((guint8*)ebp +j));
108 ebp = (gpointer)((unsigned)ebp & ~(3));
115 leave_method (MonoMethod *method, int edx, int eax, double test)
119 switch (method->signature->ret->type) {
121 printf ("LEAVE: %s.%s::%s\n", method->klass->name_space,
122 method->klass->name, method->name);
124 case MONO_TYPE_BOOLEAN:
134 printf ("LEAVE: %s.%s::%s EAX=%d\n", method->klass->name_space,
135 method->klass->name, method->name, eax);
137 case MONO_TYPE_STRING:
138 printf ("LEAVE: %s.%s::%s [STRING:%s], ", method->klass->name_space,
139 method->klass->name, method->name,
140 mono_string_to_utf8 ((MonoString *)(eax)));
142 case MONO_TYPE_OBJECT: {
143 MonoObject *o = (MonoClass *)eax;
145 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
146 method->klass->name, method->name);
148 if (o->klass == mono_defaults.boolean_class) {
149 printf ("[BOOLEAN:%p:%d]\n", o, *((guint8 *)o + sizeof (MonoObject)));
152 printf ("[%s.%s:%p]\n", o->klass->name_space, o->klass->name, o);
154 printf ("EAX=%p\n", (gpointer)eax);
158 case MONO_TYPE_CLASS:
160 case MONO_TYPE_FNPTR:
161 case MONO_TYPE_ARRAY:
162 case MONO_TYPE_SZARRAY:
163 printf ("LEAVE: %s.%s::%s EAX=%p\n", method->klass->name_space,
164 method->klass->name, method->name, (gpointer)eax);
167 *((gint32 *)&l) = eax;
168 *((gint32 *)&l + 1) = edx;
169 printf ("LEAVE: %s.%s::%s EAX/EDX=%lld\n", method->klass->name_space,
170 method->klass->name, method->name, l);
173 printf ("LEAVE: %s.%s::%s FP=%f\n", method->klass->name_space,
174 method->klass->name, method->name, test);
177 printf ("LEAVE: %s.%s::%s (unknown return type)\n", method->klass->name_space,
178 method->klass->name, method->name);
183 * arch_emit_prologue:
184 * @cfg: pointer to status information
186 * Emits the function prolog.
189 arch_emit_prologue (MonoFlowGraph *cfg)
191 x86_push_reg (cfg->code, X86_EBP);
192 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
194 if (cfg->locals_size)
195 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
197 if (mono_regset_reg_used (cfg->rs, X86_EBX))
198 x86_push_reg (cfg->code, X86_EBX);
200 if (mono_regset_reg_used (cfg->rs, X86_EDI))
201 x86_push_reg (cfg->code, X86_EDI);
203 if (mono_regset_reg_used (cfg->rs, X86_ESI))
204 x86_push_reg (cfg->code, X86_ESI);
206 if (mono_jit_trace_calls) {
207 x86_push_reg (cfg->code, X86_EBP);
208 x86_push_imm (cfg->code, cfg->method);
209 x86_call_code (cfg->code, enter_method);
210 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
215 * arch_emit_epilogue:
216 * @cfg: pointer to status information
218 * Emits the function epilog.
221 arch_emit_epilogue (MonoFlowGraph *cfg)
223 if (mono_jit_trace_calls) {
224 x86_fld_reg (cfg->code, 0);
225 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
226 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
227 x86_push_reg (cfg->code, X86_EAX);
228 x86_push_reg (cfg->code, X86_EDX);
229 x86_push_imm (cfg->code, cfg->method);
230 x86_call_code (cfg->code, leave_method);
231 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
232 x86_pop_reg (cfg->code, X86_EDX);
233 x86_pop_reg (cfg->code, X86_EAX);
234 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
237 if (mono_regset_reg_used (cfg->rs, X86_ESI))
238 x86_pop_reg (cfg->code, X86_ESI);
240 if (mono_regset_reg_used (cfg->rs, X86_EDI))
241 x86_pop_reg (cfg->code, X86_EDI);
243 if (mono_regset_reg_used (cfg->rs, X86_EBX))
244 x86_pop_reg (cfg->code, X86_EBX);
246 x86_leave (cfg->code);
251 * x86_magic_trampoline:
252 * @eax: saved x86 register
253 * @ecx: saved x86 register
254 * @edx: saved x86 register
255 * @esi: saved x86 register
256 * @edi: saved x86 register
257 * @ebx: saved x86 register
258 * @code: pointer into caller code
259 * @method: the method to translate
261 * This method is called by the trampoline functions for virtual
262 * methods. It inspects the caller code to find the address of the
263 * vtable slot, then calls the JIT compiler and writes the address
264 * of the compiled method back to the vtable. All virtual methods
265 * are called with: x86_call_membase (inst, basereg, disp). We always
266 * use 32 bit displacement to ensure that the length of the call
267 * instruction is 6 bytes. We need to get the value of the basereg
268 * and the constant displacement.
271 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
272 int ebx, guint8 *code, MonoMethod *m)
278 /* go to the start of the call instruction */
280 g_assert (*code == 0xff);
284 g_assert ((ab >> 6) == 2);
286 /* extract the register number containing the address */
290 /* extract the displacement */
291 disp = *((gint32*)code);
313 g_assert_not_reached ();
318 return *((gpointer *)o) = arch_compile_method (m);
322 * arch_create_jit_trampoline:
323 * @method: pointer to the method info
325 * Creates a trampoline function for virtual methods. If the created
326 * code is called it first starts JIT compilation of method,
327 * and then calls the newly created method. I also replaces the
328 * corresponding vtable entry (see x86_magic_trampoline).
330 * Returns: a pointer to the newly created code
333 arch_create_jit_trampoline (MonoMethod *method)
336 static guint8 *vc = NULL;
342 vc = buf = g_malloc (24);
344 /* push the return address onto the stack */
345 x86_push_membase (buf, X86_ESP, 4);
347 /* save all register values */
348 x86_push_reg (buf, X86_EBX);
349 x86_push_reg (buf, X86_EDI);
350 x86_push_reg (buf, X86_ESI);
351 x86_push_reg (buf, X86_EDX);
352 x86_push_reg (buf, X86_ECX);
353 x86_push_reg (buf, X86_EAX);
355 x86_call_code (buf, x86_magic_trampoline);
356 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
358 /* call the compiled method */
359 x86_jump_reg (buf, X86_EAX);
361 g_assert ((buf - vc) <= 24);
364 code = buf = g_malloc (16);
365 x86_push_imm (buf, method);
366 x86_jump_code (buf, vc);
367 g_assert ((buf - code) <= 16);
373 * arch_create_simple_jit_trampoline:
374 * @method: pointer to the method info
376 * Creates a trampoline function for method. If the created
377 * code is called it first starts JIT compilation of method,
378 * and then calls the newly created method. I also replaces the
379 * address in method->addr with the result of the JIT
380 * compilation step (in arch_compile_method).
382 * Returns: a pointer to the newly created code
385 arch_create_simple_jit_trampoline (MonoMethod *method)
392 /* we never free the allocated code buffer */
393 code = buf = g_malloc (16);
394 x86_push_imm (buf, method);
395 x86_call_code (buf, arch_compile_method);
396 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
397 /* jump to the compiled method */
398 x86_jump_reg (buf, X86_EAX);
399 g_assert ((buf - code) < 16);
405 mono_label_cfg (MonoFlowGraph *cfg)
409 for (i = 0; i < cfg->block_count; i++) {
410 GPtrArray *forest = cfg->bblocks [i].forest;
411 const int top = forest->len;
413 for (j = 0; j < top; j++) {
414 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
417 mbstate = mono_burg_label (t1, cfg);
420 if (mono_debug_handle)
422 g_warning ("tree does not match");
423 mono_print_ctree (t1); printf ("\n\n");
425 mono_print_forest (forest);
426 g_assert_not_reached ();
433 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
436 int ern = mono_burg_rule (tree->state, goal);
437 guint16 *nts = mono_burg_nts [ern];
440 mono_burg_kids (tree, ern, kids);
444 case MB_NTERM_lreg: {
449 tree->exclude_mask |= (1 << X86_ECX);
450 tree->left->exclude_mask |= (1 << X86_ECX);
452 case MB_TERM_CALL_I4:
453 tree->reg1 = X86_EAX;
455 case MB_TERM_CALL_I8:
456 tree->reg1 = X86_EAX;
457 tree->reg2 = X86_EDX;
463 tree->reg1 = X86_EAX;
464 tree->reg2 = X86_EDX;
465 if (goal == MB_NTERM_reg) {
466 tree->left->exclude_mask |= (1 << X86_EDX);
467 tree->right->exclude_mask |= (1 << X86_EDX);
478 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
480 if (nts [0] && kids [0] == tree) {
482 tree_allocate_regs (kids [0], nts [0], rs);
486 for (i = 0; nts [i]; i++)
487 tree_allocate_regs (kids [i], nts [i], rs);
489 for (i = 0; nts [i]; i++) {
490 mono_regset_free_reg (rs, kids [i]->reg1);
491 mono_regset_free_reg (rs, kids [i]->reg2);
497 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1) {
498 g_warning ("register allocation failed %d 0x%08x 0x%08x\n", tree->reg1, rs->free_mask, tree->exclude_mask);
499 g_assert_not_reached ();
505 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1 ||
507 mono_regset_alloc_reg (rs, tree->reg2, tree->exclude_mask)) == -1) {
508 g_warning ("register allocation failed\n");
509 g_assert_not_reached ();
514 /* fixme: allocate floating point registers */
518 if (tree->op == MB_TERM_ADD) {
519 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
520 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
522 //if (tree->op == MB_TERM_CALL_I4) {
523 //tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
528 if (tree->op == MB_TERM_ADD) {
529 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
534 if (tree->op == MB_TERM_SHL ||
535 tree->op == MB_TERM_MUL) {
536 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
544 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
545 tree->emit = mono_burg_func [ern];
549 arch_allocate_regs (MonoFlowGraph *cfg)
553 for (i = 0; i < cfg->block_count; i++) {
554 GPtrArray *forest = cfg->bblocks [i].forest;
555 const int top = forest->len;
557 for (j = 0; j < top; j++) {
558 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
559 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
560 tree_allocate_regs (t1, 1, cfg->rs);
561 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
562 g_assert (cfg->rs->free_mask == 0xffffffff);
568 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
571 int i, ern = mono_burg_rule (tree->state, goal);
572 guint16 *nts = mono_burg_nts [ern];
575 mono_burg_kids (tree, ern, kids);
577 for (i = 0; nts [i]; i++)
578 tree_emit (nts [i], s, kids [i]);
580 tree->addr = s->code - s->start;
582 if ((emit = mono_burg_func [ern]))
587 mono_emit_cfg (MonoFlowGraph *cfg)
591 for (i = 0; i < cfg->block_count; i++) {
592 MonoBBlock *bb = &cfg->bblocks [i];
593 GPtrArray *forest = bb->forest;
594 const int top = forest->len;
596 bb->addr = cfg->code - cfg->start;
598 for (j = 0; j < top; j++) {
599 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
600 tree_emit (1, cfg, t1);
604 cfg->epilog = cfg->code - cfg->start;
608 mono_compute_branches (MonoFlowGraph *cfg)
615 for (j = 0; j < cfg->block_count; j++) {
616 MonoBBlock *bb = &cfg->bblocks [j];
617 GPtrArray *forest = bb->forest;
618 const int top = forest->len;
620 for (i = 0; i < top; i++) {
621 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
625 if (t1->op == MB_TERM_SWITCH) {
626 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
627 guint32 *rt = (guint32 *)t1->data.p;
629 int m = *((guint32 *)t1->data.p) + 1;
632 for (j = 1; j <= m; j++)
633 rt [j] = (int)(jt [j]->addr + cfg->start);
636 /* emit the jump instruction again to update addresses */
637 cfg->code = cfg->start + t1->addr;
638 ((MBEmitFunc)t1->emit) (t1, cfg);
648 match_debug_method (MonoMethod* method)
650 GList *tmp = mono_debug_methods;
652 for (; tmp; tmp = tmp->next) {
653 if (strcmp (method->name, tmp->data) == 0) {
661 * arch_compile_method:
662 * @method: pointer to the method info
664 * JIT compilation of a single method. This method also writes the result
665 * back to method->addr, an thus overwrites the trampoline function.
667 * Returns: a pointer to the newly created code.
670 arch_compile_method (MonoMethod *method)
673 MonoMemPool *mp = mono_mempool_new ();
675 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
676 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
678 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
679 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
680 method->klass->name, method->name);
683 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
684 MonoClassField *field;
685 const char *name = method->name;
686 static guint target_offset = 0;
687 static guint method_offset = 0;
689 gboolean delegate = FALSE;
691 if (method->klass->parent &&
692 method->klass->parent->parent == mono_defaults.delegate_class)
695 if (!target_offset) {
696 mono_jit_init_class (mono_defaults.delegate_class);
698 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
699 target_offset = field->offset;
700 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
701 method_offset = field->offset;
704 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
705 method->addr = code = g_malloc (32);
706 x86_push_reg (code, X86_EBP);
707 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
709 /* load the this pointer */
710 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
711 /* load m_target arg */
712 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
714 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
715 /* load method_ptr arg */
716 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
717 /* store method_ptr */
718 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
723 g_assert ((code - (guint8*)method->addr) < 32);
725 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
726 MonoMethodSignature *csig = method->signature;
727 int i, target, this_pos = 4;
730 method->addr = g_malloc (32);
732 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
734 if ((size = mono_type_size (csig->ret, &align)) > 4 || size == 3)
738 for (i = 0; i < 2; i ++) {
740 /* load the this pointer */
741 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
743 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
744 /* check if zero (static method call without this pointer) */
745 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
746 x86_branch32 (code, X86_CC_EQ, target, TRUE);
749 /* virtual call - we have to replace the this pointer */
750 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
752 /* jump to method_ptr() */
753 target = code - source;
754 x86_jump_membase (code, X86_EAX, method_offset);
757 g_assert ((code - (guint8*)method->addr) < 32);
760 if (mono_debug_handle)
762 g_error ("Don't know how to exec runtime method %s.%s::%s",
763 method->klass->name_space, method->klass->name, method->name);
767 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
768 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
770 cfg = mono_cfg_new (method, mp);
772 mono_analyze_flow (cfg);
776 mono_analyze_stack (cfg);
781 cfg->rs = mono_regset_new (X86_NREG);
782 mono_regset_reserve_reg (cfg->rs, X86_ESP);
783 mono_regset_reserve_reg (cfg->rs, X86_EBP);
785 // fixme: remove limitation to 8192 bytes
786 ji->code_size = 8192;
787 method->addr = cfg->start = cfg->code = g_malloc (ji->code_size);
789 if (match_debug_method (method))
790 x86_breakpoint (cfg->code);
792 if (mono_jit_dump_forest) {
794 for (i = 0; i < cfg->block_count; i++) {
795 printf ("BLOCK %d:\n", i);
796 mono_print_forest (cfg->bblocks [i].forest);
800 mono_label_cfg (cfg);
804 arch_allocate_regs (cfg);
806 /* align to 8 byte boundary */
807 cfg->locals_size += 7;
808 cfg->locals_size &= ~7;
810 arch_emit_prologue (cfg);
814 arch_emit_epilogue (cfg);
816 g_assert ((cfg->code - cfg->start) < ji->code_size);
818 mono_compute_branches (cfg);
820 if (mono_jit_dump_asm)
821 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
823 if (mono_debug_handle)
824 mono_debug_add_method (mono_debug_handle, cfg);
826 ji->used_regs = cfg->rs->used_mask;
828 ji->code_start = method->addr;
829 mono_jit_info_table_add (mono_jit_info_table, ji);
831 if (header->num_clauses) {
832 int i, start_block, end_block;
834 ji->num_clauses = header->num_clauses;
835 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
837 for (i = 0; i < header->num_clauses; i++) {
838 MonoExceptionClause *ec = &header->clauses [i];
839 MonoJitExceptionInfo *ei = &ji->clauses [i];
841 ei->flags = ec->flags;
842 ei->token_or_filter = ec->token_or_filter;
844 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
845 start_block = cfg->bcinfo [ec->try_offset].block_id;
846 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
847 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
849 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
850 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
852 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
853 start_block = cfg->bcinfo [ec->handler_offset].block_id;
854 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
856 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
860 mono_regset_free (cfg->rs);
864 mono_mempool_destroy (mp);
868 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
869 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
870 method->klass->name, method->name, method, method->addr);
877 * arch_get_restore_context:
879 * Returns a pointer to a method which restores a previously saved sigcontext.
882 arch_get_restore_context ()
884 static guint8 *start = NULL;
890 /* restore_contect (struct sigcontext *ctx) */
891 /* we do not restore X86_EAX, X86_EDX */
893 start = code = malloc (1024);
896 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
898 /* get return address, stored in EDX */
899 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
902 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
904 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
906 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
908 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
910 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
911 /* restore ECX. the exception object is passed here to the catch handler */
912 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
914 /* jump to the saved IP */
915 x86_jump_reg (code, X86_EDX);
921 * arch_get_call_finally:
923 * Returns a pointer to a method which calls a finally handler.
926 arch_get_call_finally ()
928 static guint8 *start = NULL;
934 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
935 start = code = malloc (1024);
937 x86_push_reg (code, X86_EBP);
938 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
939 x86_push_reg (code, X86_EBX);
940 x86_push_reg (code, X86_EDI);
941 x86_push_reg (code, X86_ESI);
944 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
946 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
948 x86_push_reg (code, X86_EBP);
950 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
951 /* call the handler */
952 x86_call_reg (code, X86_ECX);
954 x86_pop_reg (code, X86_EBP);
955 /* restore saved regs */
956 x86_pop_reg (code, X86_ESI);
957 x86_pop_reg (code, X86_EDI);
958 x86_pop_reg (code, X86_EBX);
966 * arch_handle_exception:
967 * @ctx: saved processor state
971 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
974 gpointer ip = (gpointer)ctx->eip;
975 static void (*restore_context) (struct sigcontext *);
976 static void (*call_finally) (struct sigcontext *, unsigned long);
978 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
980 if (!restore_context)
981 restore_context = arch_get_restore_context ();
984 call_finally = arch_get_call_finally ();
986 if (ji) { /* we are inside managed code */
987 MonoMethod *m = ji->method;
988 unsigned next_bp, next_ip;
991 if (ji->num_clauses) {
994 g_assert (ji->clauses);
996 for (i = 0; i < ji->num_clauses; i++) {
997 MonoJitExceptionInfo *ei = &ji->clauses [i];
999 if (ei->try_start <= ip && ip < (ei->try_end)) {
1002 if (ei->flags == 0 && mono_object_isinst (obj,
1003 mono_class_get (m->klass->image, ei->token_or_filter))) {
1005 ctx->eip = (unsigned long)ei->handler_start;
1006 ctx->ecx = (unsigned long)obj;
1007 restore_context (ctx);
1008 g_assert_not_reached ();
1013 /* no handler found - we need to call all finally handlers */
1014 for (i = 0; i < ji->num_clauses; i++) {
1015 MonoJitExceptionInfo *ei = &ji->clauses [i];
1017 if (ei->try_start <= ip && ip < (ei->try_end) &&
1018 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1019 call_finally (ctx, (unsigned long)ei->handler_start);
1024 /* continue unwinding */
1026 /* restore caller saved registers */
1027 if (ji->used_regs & X86_ESI_MASK) {
1028 ctx->esi = *((int *)ctx->ebp + offset);
1031 if (ji->used_regs & X86_EDI_MASK) {
1032 ctx->edi = *((int *)ctx->ebp + offset);
1035 if (ji->used_regs & X86_EBX_MASK) {
1036 ctx->ebx = *((int *)ctx->ebp + offset);
1039 ctx->esp = ctx->ebp;
1041 next_bp = *((int *)ctx->ebp);
1042 next_ip = *((int *)ctx->ebp + 1);
1044 if (next_bp < (unsigned)mono_end_of_stack) {
1048 arch_handle_exception (ctx, obj);
1051 mono_jit_abort (obj);
1055 /* fixme: implement exceptions inside unmanaged code */
1056 mono_jit_abort (obj);
1059 g_assert_not_reached ();