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, ", *((float *)(ebp)));
98 printf ("%f, ", *((double *)(ebp)));
100 case MONO_TYPE_VALUETYPE:
102 for (j = 0; j < size; j++)
103 printf ("%02x,", *((guint8*)ebp +j));
111 ebp = (gpointer)((unsigned)ebp & ~(3));
118 leave_method (MonoMethod *method, int edx, int eax, double test)
122 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
123 method->klass->name, method->name);
125 switch (method->signature->ret->type) {
128 case MONO_TYPE_BOOLEAN:
130 printf ("TRUE:%d", eax);
144 printf ("EAX=%d", eax);
146 case MONO_TYPE_STRING:
147 printf ("[STRING:%s]", mono_string_to_utf8 ((MonoString *)(eax)));
149 case MONO_TYPE_OBJECT: {
150 MonoObject *o = (MonoObject *)eax;
152 if (o->klass == mono_defaults.boolean_class) {
153 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
154 } else if (o->klass == mono_defaults.int32_class) {
155 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
158 printf ("[%s.%s:%p]", o->klass->name_space, o->klass->name, o);
160 printf ("[OBJECT:%p]", (gpointer)eax);
164 case MONO_TYPE_CLASS:
166 case MONO_TYPE_FNPTR:
167 case MONO_TYPE_ARRAY:
168 case MONO_TYPE_SZARRAY:
169 printf ("EAX=%p", (gpointer)eax);
172 *((gint32 *)&l) = eax;
173 *((gint32 *)&l + 1) = edx;
174 printf ("EAX/EDX=%lld", l);
177 printf ("FP=%f\n", test);
180 printf ("(unknown return type)");
187 * arch_emit_prologue:
188 * @cfg: pointer to status information
190 * Emits the function prolog.
193 arch_emit_prologue (MonoFlowGraph *cfg)
195 x86_push_reg (cfg->code, X86_EBP);
196 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
198 if (cfg->locals_size)
199 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
201 if (mono_regset_reg_used (cfg->rs, X86_EBX))
202 x86_push_reg (cfg->code, X86_EBX);
204 if (mono_regset_reg_used (cfg->rs, X86_EDI))
205 x86_push_reg (cfg->code, X86_EDI);
207 if (mono_regset_reg_used (cfg->rs, X86_ESI))
208 x86_push_reg (cfg->code, X86_ESI);
210 if (mono_jit_trace_calls) {
211 x86_push_reg (cfg->code, X86_EBP);
212 x86_push_imm (cfg->code, cfg->method);
213 x86_call_code (cfg->code, enter_method);
214 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
219 * arch_emit_epilogue:
220 * @cfg: pointer to status information
222 * Emits the function epilog.
225 arch_emit_epilogue (MonoFlowGraph *cfg)
227 if (mono_jit_trace_calls) {
228 x86_fld_reg (cfg->code, 0);
229 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
230 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
231 x86_push_reg (cfg->code, X86_EAX);
232 x86_push_reg (cfg->code, X86_EDX);
233 x86_push_imm (cfg->code, cfg->method);
234 x86_call_code (cfg->code, leave_method);
235 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
236 x86_pop_reg (cfg->code, X86_EDX);
237 x86_pop_reg (cfg->code, X86_EAX);
238 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
241 if (mono_regset_reg_used (cfg->rs, X86_ESI))
242 x86_pop_reg (cfg->code, X86_ESI);
244 if (mono_regset_reg_used (cfg->rs, X86_EDI))
245 x86_pop_reg (cfg->code, X86_EDI);
247 if (mono_regset_reg_used (cfg->rs, X86_EBX))
248 x86_pop_reg (cfg->code, X86_EBX);
250 x86_leave (cfg->code);
255 * x86_magic_trampoline:
256 * @eax: saved x86 register
257 * @ecx: saved x86 register
258 * @edx: saved x86 register
259 * @esi: saved x86 register
260 * @edi: saved x86 register
261 * @ebx: saved x86 register
262 * @code: pointer into caller code
263 * @method: the method to translate
265 * This method is called by the trampoline functions for virtual
266 * methods. It inspects the caller code to find the address of the
267 * vtable slot, then calls the JIT compiler and writes the address
268 * of the compiled method back to the vtable. All virtual methods
269 * are called with: x86_call_membase (inst, basereg, disp). We always
270 * use 32 bit displacement to ensure that the length of the call
271 * instruction is 6 bytes. We need to get the value of the basereg
272 * and the constant displacement.
275 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
276 int ebx, guint8 *code, MonoMethod *m)
282 /* go to the start of the call instruction */
284 g_assert (*code == 0xff);
288 g_assert ((ab >> 6) == 2);
290 /* extract the register number containing the address */
294 /* extract the displacement */
295 disp = *((gint32*)code);
317 g_assert_not_reached ();
322 return *((gpointer *)o) = arch_compile_method (m);
326 * arch_create_jit_trampoline:
327 * @method: pointer to the method info
329 * Creates a trampoline function for virtual methods. If the created
330 * code is called it first starts JIT compilation of method,
331 * and then calls the newly created method. I also replaces the
332 * corresponding vtable entry (see x86_magic_trampoline).
334 * Returns: a pointer to the newly created code
337 arch_create_jit_trampoline (MonoMethod *method)
340 static guint8 *vc = NULL;
346 vc = buf = g_malloc (24);
348 /* push the return address onto the stack */
349 x86_push_membase (buf, X86_ESP, 4);
351 /* save all register values */
352 x86_push_reg (buf, X86_EBX);
353 x86_push_reg (buf, X86_EDI);
354 x86_push_reg (buf, X86_ESI);
355 x86_push_reg (buf, X86_EDX);
356 x86_push_reg (buf, X86_ECX);
357 x86_push_reg (buf, X86_EAX);
359 x86_call_code (buf, x86_magic_trampoline);
360 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
362 /* call the compiled method */
363 x86_jump_reg (buf, X86_EAX);
365 g_assert ((buf - vc) <= 24);
368 code = buf = g_malloc (16);
369 x86_push_imm (buf, method);
370 x86_jump_code (buf, vc);
371 g_assert ((buf - code) <= 16);
377 * arch_create_simple_jit_trampoline:
378 * @method: pointer to the method info
380 * Creates a trampoline function for method. If the created
381 * code is called it first starts JIT compilation of method,
382 * and then calls the newly created method. I also replaces the
383 * address in method->addr with the result of the JIT
384 * compilation step (in arch_compile_method).
386 * Returns: a pointer to the newly created code
389 arch_create_simple_jit_trampoline (MonoMethod *method)
396 /* we never free the allocated code buffer */
397 code = buf = g_malloc (16);
398 x86_push_imm (buf, method);
399 x86_call_code (buf, arch_compile_method);
400 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
401 /* jump to the compiled method */
402 x86_jump_reg (buf, X86_EAX);
403 g_assert ((buf - code) < 16);
409 mono_label_cfg (MonoFlowGraph *cfg)
413 for (i = 0; i < cfg->block_count; i++) {
414 GPtrArray *forest = cfg->bblocks [i].forest;
415 const int top = forest->len;
417 for (j = 0; j < top; j++) {
418 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
421 mbstate = mono_burg_label (t1, cfg);
424 if (mono_debug_handle)
426 g_warning ("tree does not match");
427 mono_print_ctree (t1); printf ("\n\n");
429 mono_print_forest (forest);
430 g_assert_not_reached ();
437 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
440 case MB_TERM_CALL_I4:
441 case MB_TERM_CALL_I8:
442 case MB_TERM_CALL_R8:
443 // case MB_TERM_CALL_VOID :
444 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
445 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
446 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
452 case MB_NTERM_lreg: {
457 tree->exclude_mask |= (1 << X86_ECX);
458 tree->left->exclude_mask |= (1 << X86_ECX);
464 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
465 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
466 if (goal == MB_NTERM_reg) {
467 tree->left->exclude_mask |= (1 << X86_EDX);
468 tree->right->exclude_mask |= (1 << X86_EDX);
482 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
485 int ern = mono_burg_rule (tree->state, goal);
486 guint16 *nts = mono_burg_nts [ern];
489 mono_burg_kids (tree, ern, kids);
491 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
493 if (nts [0] && kids [0] == tree) {
495 tree_allocate_regs (kids [0], nts [0], rs);
499 for (i = 0; nts [i]; i++)
500 tree_preallocate_regs (kids [i], nts [i], rs);
502 for (i = 0; nts [i]; i++)
503 tree_allocate_regs (kids [i], nts [i], rs);
505 for (i = 0; nts [i]; i++) {
506 mono_regset_free_reg (rs, kids [i]->reg1);
507 mono_regset_free_reg (rs, kids [i]->reg2);
508 mono_regset_free_reg (rs, kids [i]->reg3);
513 if (tree->reg1 < 0) {
514 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
515 g_assert (tree->reg1 != -1);
520 if (tree->reg1 < 0) {
521 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
522 g_assert (tree->reg1 != -1);
524 if (tree->reg2 < 0) {
525 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
526 g_assert (tree->reg2 != -1);
531 /* fixme: allocate floating point registers */
535 if (tree->op == MB_TERM_ADD) {
536 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
537 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
542 if (tree->op == MB_TERM_ADD) {
543 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
548 if (tree->op == MB_TERM_SHL ||
549 tree->op == MB_TERM_MUL) {
550 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
558 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
559 tree->emit = mono_burg_func [ern];
563 arch_allocate_regs (MonoFlowGraph *cfg)
567 for (i = 0; i < cfg->block_count; i++) {
568 GPtrArray *forest = cfg->bblocks [i].forest;
569 const int top = forest->len;
571 for (j = 0; j < top; j++) {
572 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
573 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
574 tree_allocate_regs (t1, 1, cfg->rs);
575 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
576 g_assert (cfg->rs->free_mask == 0xffffffff);
582 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
585 int i, ern = mono_burg_rule (tree->state, goal);
586 guint16 *nts = mono_burg_nts [ern];
589 mono_burg_kids (tree, ern, kids);
591 for (i = 0; nts [i]; i++)
592 tree_emit (nts [i], s, kids [i]);
594 tree->addr = s->code - s->start;
596 if ((emit = mono_burg_func [ern]))
601 mono_emit_cfg (MonoFlowGraph *cfg)
605 for (i = 0; i < cfg->block_count; i++) {
606 MonoBBlock *bb = &cfg->bblocks [i];
607 GPtrArray *forest = bb->forest;
608 const int top = forest->len;
610 bb->addr = cfg->code - cfg->start;
612 for (j = 0; j < top; j++) {
613 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
614 tree_emit (1, cfg, t1);
618 cfg->epilog = cfg->code - cfg->start;
622 mono_compute_branches (MonoFlowGraph *cfg)
629 for (j = 0; j < cfg->block_count; j++) {
630 MonoBBlock *bb = &cfg->bblocks [j];
631 GPtrArray *forest = bb->forest;
632 const int top = forest->len;
634 for (i = 0; i < top; i++) {
635 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
639 if (t1->op == MB_TERM_SWITCH) {
640 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
641 guint32 *rt = (guint32 *)t1->data.p;
643 int m = *((guint32 *)t1->data.p) + 1;
646 for (j = 1; j <= m; j++)
647 rt [j] = (int)(jt [j]->addr + cfg->start);
650 /* emit the jump instruction again to update addresses */
651 cfg->code = cfg->start + t1->addr;
652 ((MBEmitFunc)t1->emit) (t1, cfg);
662 match_debug_method (MonoMethod* method)
664 GList *tmp = mono_debug_methods;
666 for (; tmp; tmp = tmp->next) {
667 if (strcmp (method->name, tmp->data) == 0) {
675 * arch_compile_method:
676 * @method: pointer to the method info
678 * JIT compilation of a single method. This method also writes the result
679 * back to method->addr, an thus overwrites the trampoline function.
681 * Returns: a pointer to the newly created code.
684 arch_compile_method (MonoMethod *method)
687 MonoMemPool *mp = mono_mempool_new ();
689 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
690 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
692 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
693 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
694 method->klass->name, method->name);
697 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
698 MonoClassField *field;
699 const char *name = method->name;
700 static guint target_offset = 0;
701 static guint method_offset = 0;
703 gboolean delegate = FALSE;
705 if (method->klass->parent &&
706 method->klass->parent->parent == mono_defaults.delegate_class)
709 if (!target_offset) {
710 mono_class_init (mono_defaults.delegate_class);
712 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
713 target_offset = field->offset;
714 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
715 method_offset = field->offset;
718 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
719 method->addr = code = g_malloc (32);
720 x86_push_reg (code, X86_EBP);
721 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
723 /* load the this pointer */
724 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
725 /* load m_target arg */
726 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
728 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
729 /* load method_ptr arg */
730 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
731 /* store method_ptr */
732 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
737 g_assert ((code - (guint8*)method->addr) < 32);
739 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
740 MonoMethodSignature *csig = method->signature;
741 int i, target, this_pos = 4;
744 method->addr = g_malloc (32);
746 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
748 if ((size = mono_type_size (csig->ret, &align)) > 4 || size == 3)
752 for (i = 0; i < 2; i ++) {
754 /* load the this pointer */
755 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
757 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
758 /* check if zero (static method call without this pointer) */
759 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
760 x86_branch32 (code, X86_CC_EQ, target, TRUE);
763 /* virtual call - we have to replace the this pointer */
764 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
766 /* jump to method_ptr() */
767 target = code - source;
768 x86_jump_membase (code, X86_EAX, method_offset);
771 g_assert ((code - (guint8*)method->addr) < 32);
774 if (mono_debug_handle)
776 g_error ("Don't know how to exec runtime method %s.%s::%s",
777 method->klass->name_space, method->klass->name, method->name);
781 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
782 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
784 cfg = mono_cfg_new (method, mp);
786 mono_analyze_flow (cfg);
790 mono_analyze_stack (cfg);
795 cfg->rs = mono_regset_new (X86_NREG);
796 mono_regset_reserve_reg (cfg->rs, X86_ESP);
797 mono_regset_reserve_reg (cfg->rs, X86_EBP);
799 // fixme: remove limitation to 8192 bytes
800 ji->code_size = 8192;
801 method->addr = cfg->start = cfg->code = g_malloc (ji->code_size);
803 if (match_debug_method (method))
804 x86_breakpoint (cfg->code);
806 if (mono_jit_dump_forest) {
808 for (i = 0; i < cfg->block_count; i++) {
809 printf ("BLOCK %d:\n", i);
810 mono_print_forest (cfg->bblocks [i].forest);
814 mono_label_cfg (cfg);
818 arch_allocate_regs (cfg);
820 /* align to 8 byte boundary */
821 cfg->locals_size += 7;
822 cfg->locals_size &= ~7;
824 arch_emit_prologue (cfg);
828 arch_emit_epilogue (cfg);
830 g_assert ((cfg->code - cfg->start) < ji->code_size);
832 mono_compute_branches (cfg);
834 if (mono_jit_dump_asm)
835 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
837 if (mono_debug_handle)
838 mono_debug_add_method (mono_debug_handle, cfg);
840 ji->used_regs = cfg->rs->used_mask;
842 ji->code_start = method->addr;
843 mono_jit_info_table_add (mono_jit_info_table, ji);
845 if (header->num_clauses) {
846 int i, start_block, end_block;
848 ji->num_clauses = header->num_clauses;
849 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
851 for (i = 0; i < header->num_clauses; i++) {
852 MonoExceptionClause *ec = &header->clauses [i];
853 MonoJitExceptionInfo *ei = &ji->clauses [i];
855 ei->flags = ec->flags;
856 ei->token_or_filter = ec->token_or_filter;
858 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
859 start_block = cfg->bcinfo [ec->try_offset].block_id;
860 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
861 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
863 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
864 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
866 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
867 start_block = cfg->bcinfo [ec->handler_offset].block_id;
868 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
870 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
874 mono_regset_free (cfg->rs);
878 mono_mempool_destroy (mp);
882 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
883 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
884 method->klass->name, method->name, method, method->addr);
891 * arch_get_restore_context:
893 * Returns a pointer to a method which restores a previously saved sigcontext.
896 arch_get_restore_context ()
898 static guint8 *start = NULL;
904 /* restore_contect (struct sigcontext *ctx) */
905 /* we do not restore X86_EAX, X86_EDX */
907 start = code = malloc (1024);
910 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
912 /* get return address, stored in EDX */
913 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
916 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
918 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
920 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
922 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
924 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
925 /* restore ECX. the exception object is passed here to the catch handler */
926 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
928 /* jump to the saved IP */
929 x86_jump_reg (code, X86_EDX);
935 * arch_get_call_finally:
937 * Returns a pointer to a method which calls a finally handler.
940 arch_get_call_finally ()
942 static guint8 *start = NULL;
948 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
949 start = code = malloc (1024);
951 x86_push_reg (code, X86_EBP);
952 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
953 x86_push_reg (code, X86_EBX);
954 x86_push_reg (code, X86_EDI);
955 x86_push_reg (code, X86_ESI);
958 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
960 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
962 x86_push_reg (code, X86_EBP);
964 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
965 /* call the handler */
966 x86_call_reg (code, X86_ECX);
968 x86_pop_reg (code, X86_EBP);
969 /* restore saved regs */
970 x86_pop_reg (code, X86_ESI);
971 x86_pop_reg (code, X86_EDI);
972 x86_pop_reg (code, X86_EBX);
980 * arch_handle_exception:
981 * @ctx: saved processor state
985 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
988 gpointer ip = (gpointer)ctx->eip;
989 static void (*restore_context) (struct sigcontext *);
990 static void (*call_finally) (struct sigcontext *, unsigned long);
992 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
994 if (!restore_context)
995 restore_context = arch_get_restore_context ();
998 call_finally = arch_get_call_finally ();
1000 if (ji) { /* we are inside managed code */
1001 MonoMethod *m = ji->method;
1002 unsigned next_bp, next_ip;
1005 if (ji->num_clauses) {
1008 g_assert (ji->clauses);
1010 for (i = 0; i < ji->num_clauses; i++) {
1011 MonoJitExceptionInfo *ei = &ji->clauses [i];
1013 if (ei->try_start <= ip && ip < (ei->try_end)) {
1016 if (ei->flags == 0 && mono_object_isinst (obj,
1017 mono_class_get (m->klass->image, ei->token_or_filter))) {
1019 ctx->eip = (unsigned long)ei->handler_start;
1020 ctx->ecx = (unsigned long)obj;
1021 restore_context (ctx);
1022 g_assert_not_reached ();
1027 /* no handler found - we need to call all finally handlers */
1028 for (i = 0; i < ji->num_clauses; i++) {
1029 MonoJitExceptionInfo *ei = &ji->clauses [i];
1031 if (ei->try_start <= ip && ip < (ei->try_end) &&
1032 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1033 call_finally (ctx, (unsigned long)ei->handler_start);
1038 /* continue unwinding */
1040 /* restore caller saved registers */
1041 if (ji->used_regs & X86_ESI_MASK) {
1042 ctx->esi = *((int *)ctx->ebp + offset);
1045 if (ji->used_regs & X86_EDI_MASK) {
1046 ctx->edi = *((int *)ctx->ebp + offset);
1049 if (ji->used_regs & X86_EBX_MASK) {
1050 ctx->ebx = *((int *)ctx->ebp + offset);
1053 ctx->esp = ctx->ebp;
1055 next_bp = *((int *)ctx->ebp);
1056 next_ip = *((int *)ctx->ebp + 1);
1058 if (next_bp < (unsigned)mono_end_of_stack) {
1062 arch_handle_exception (ctx, obj);
1065 mono_jit_abort (obj);
1069 /* fixme: implement exceptions inside unmanaged code */
1070 mono_jit_abort (obj);
1073 g_assert_not_reached ();