2 * emit-x86.c: Support functions for emitting x86 code
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
8 * (C) 2001 Ximian, Inc.
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/loader.h>
16 #include <mono/metadata/cil-coff.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/class.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/arch/x86/x86-codegen.h>
27 enter_method (MonoMethod *method, gpointer ebp)
33 printf ("ENTER: %s.%s::%s\n(", method->klass->name_space,
34 method->klass->name, method->name);
38 if (ISSTRUCT (method->signature->ret)) {
41 g_assert (!method->signature->ret->byref);
43 size = mono_type_size (method->signature->ret, &align);
45 printf ("VALUERET:%p, ", *((gpointer *)ebp));
46 ebp += sizeof (gpointer);
49 if (method->signature->hasthis) {
50 if (method->klass->valuetype) {
51 printf ("value:%p, ", *((gpointer *)ebp));
53 o = *((MonoObject **)ebp);
59 if (class == mono_defaults.string_class) {
60 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
62 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
65 ebp += sizeof (gpointer);
68 for (i = 0; i < method->signature->param_count; ++i) {
69 MonoType *type = method->signature->params [i];
71 size = mono_type_size (type, &align);
74 printf ("[BYREF:%p], ", *((gpointer *)ebp));
75 } else switch (type->type) {
77 case MONO_TYPE_BOOLEAN:
87 printf ("%d, ", *((int *)(ebp)));
89 case MONO_TYPE_STRING: {
90 MonoString *s = *((MonoString **)ebp);
92 g_assert (((MonoObject *)s)->klass == mono_defaults.string_class);
93 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
95 printf ("[STRING:null], ");
99 case MONO_TYPE_OBJECT: {
100 o = *((MonoObject **)ebp);
103 if (class == mono_defaults.string_class) {
104 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
105 } else if (class == mono_defaults.int32_class) {
106 printf ("[INT32:%p:%d], ", o, *(gint32 *)((gpointer)o + sizeof (MonoObject)));
108 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
110 printf ("%p, ", *((gpointer *)(ebp)));
115 case MONO_TYPE_FNPTR:
116 case MONO_TYPE_ARRAY:
117 case MONO_TYPE_SZARRAY:
118 printf ("%p, ", *((gpointer *)(ebp)));
121 printf ("%lld, ", *((gint64 *)(ebp)));
124 printf ("%f, ", *((float *)(ebp)));
127 printf ("%f, ", *((double *)(ebp)));
129 case MONO_TYPE_VALUETYPE:
131 for (j = 0; j < size; j++)
132 printf ("%02x,", *((guint8*)ebp +j));
140 ebp = (gpointer)((unsigned)ebp & ~(3));
147 leave_method (MonoMethod *method, int edx, int eax, double test)
151 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
152 method->klass->name, method->name);
154 switch (method->signature->ret->type) {
157 case MONO_TYPE_BOOLEAN:
159 printf ("TRUE:%d", eax);
173 printf ("EAX=%d", eax);
175 case MONO_TYPE_STRING: {
176 MonoString *s = (MonoString *)eax;
179 g_assert (((MonoObject *)s)->klass == mono_defaults.string_class);
180 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
182 printf ("[STRING:null], ");
185 case MONO_TYPE_OBJECT: {
186 MonoObject *o = (MonoObject *)eax;
189 if (o->klass == mono_defaults.boolean_class) {
190 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
191 } else if (o->klass == mono_defaults.int32_class) {
192 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
194 printf ("[%s.%s:%p]", o->klass->name_space, o->klass->name, o);
196 printf ("[OBJECT:%p]", o);
200 case MONO_TYPE_CLASS:
202 case MONO_TYPE_FNPTR:
203 case MONO_TYPE_ARRAY:
204 case MONO_TYPE_SZARRAY:
205 printf ("EAX=%p", (gpointer)eax);
208 *((gint32 *)&l) = eax;
209 *((gint32 *)&l + 1) = edx;
210 printf ("EAX/EDX=%lld", l);
213 printf ("FP=%f\n", test);
216 printf ("(unknown return type)");
223 * arch_emit_prologue:
224 * @cfg: pointer to status information
226 * Emits the function prolog.
229 arch_emit_prologue (MonoFlowGraph *cfg)
231 x86_push_reg (cfg->code, X86_EBP);
232 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
234 if (cfg->locals_size)
235 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
237 if (mono_regset_reg_used (cfg->rs, X86_EBX))
238 x86_push_reg (cfg->code, X86_EBX);
240 if (mono_regset_reg_used (cfg->rs, X86_EDI))
241 x86_push_reg (cfg->code, X86_EDI);
243 if (mono_regset_reg_used (cfg->rs, X86_ESI))
244 x86_push_reg (cfg->code, X86_ESI);
246 if (mono_jit_trace_calls) {
247 x86_push_reg (cfg->code, X86_EBP);
248 x86_push_imm (cfg->code, cfg->method);
249 x86_mov_reg_imm (cfg->code, X86_EAX, enter_method);
250 x86_call_reg (cfg->code, X86_EAX);
251 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
256 * arch_emit_epilogue:
257 * @cfg: pointer to status information
259 * Emits the function epilog.
262 arch_emit_epilogue (MonoFlowGraph *cfg)
264 if (mono_jit_trace_calls) {
265 x86_fld_reg (cfg->code, 0);
266 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
267 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
268 x86_push_reg (cfg->code, X86_EAX);
269 x86_push_reg (cfg->code, X86_EDX);
270 x86_push_imm (cfg->code, cfg->method);
271 x86_mov_reg_imm (cfg->code, X86_EAX, leave_method);
272 x86_call_reg (cfg->code, X86_EAX);
273 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
274 x86_pop_reg (cfg->code, X86_EDX);
275 x86_pop_reg (cfg->code, X86_EAX);
276 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
279 if (mono_regset_reg_used (cfg->rs, X86_ESI))
280 x86_pop_reg (cfg->code, X86_ESI);
282 if (mono_regset_reg_used (cfg->rs, X86_EDI))
283 x86_pop_reg (cfg->code, X86_EDI);
285 if (mono_regset_reg_used (cfg->rs, X86_EBX))
286 x86_pop_reg (cfg->code, X86_EBX);
288 x86_leave (cfg->code);
293 * get_unbox_trampoline:
296 * when value type methods are called through the vtable we need to unbox the
297 * this argument. This method returns a pointer to a trampoline which does
298 * unboxing before calling the method
301 get_unbox_trampoline (MonoMethod *m)
303 gpointer p = arch_compile_method (m);
304 guint8 *code, *start;
307 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
310 start = code = g_malloc (16);
312 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
313 x86_jump_code (code, p);
314 g_assert ((code - start) < 16);
320 * x86_magic_trampoline:
321 * @eax: saved x86 register
322 * @ecx: saved x86 register
323 * @edx: saved x86 register
324 * @esi: saved x86 register
325 * @edi: saved x86 register
326 * @ebx: saved x86 register
327 * @code: pointer into caller code
328 * @method: the method to translate
330 * This method is called by the trampoline functions for virtual
331 * methods. It inspects the caller code to find the address of the
332 * vtable slot, then calls the JIT compiler and writes the address
333 * of the compiled method back to the vtable. All virtual methods
334 * are called with: x86_call_membase (inst, basereg, disp). We always
335 * use 32 bit displacement to ensure that the length of the call
336 * instruction is 6 bytes. We need to get the value of the basereg
337 * and the constant displacement.
340 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
341 int ebx, guint8 *code, MonoMethod *m)
347 /* go to the start of the call instruction */
349 g_assert (*code == 0xff);
353 g_assert ((ab >> 6) == 2);
355 /* extract the register number containing the address */
359 /* extract the displacement */
360 disp = *((gint32*)code);
382 g_assert_not_reached ();
387 if (m->klass->valuetype) {
388 return *((gpointer *)o) = get_unbox_trampoline (m);
390 return *((gpointer *)o) = arch_compile_method (m);
394 * arch_create_jit_trampoline:
395 * @method: pointer to the method info
397 * Creates a trampoline function for virtual methods. If the created
398 * code is called it first starts JIT compilation of method,
399 * and then calls the newly created method. I also replaces the
400 * corresponding vtable entry (see x86_magic_trampoline).
402 * Returns: a pointer to the newly created code
405 arch_create_jit_trampoline (MonoMethod *method)
408 static guint8 *vc = NULL;
414 vc = buf = g_malloc (24);
416 /* push the return address onto the stack */
417 x86_push_membase (buf, X86_ESP, 4);
419 /* save all register values */
420 x86_push_reg (buf, X86_EBX);
421 x86_push_reg (buf, X86_EDI);
422 x86_push_reg (buf, X86_ESI);
423 x86_push_reg (buf, X86_EDX);
424 x86_push_reg (buf, X86_ECX);
425 x86_push_reg (buf, X86_EAX);
427 x86_call_code (buf, x86_magic_trampoline);
428 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
430 /* call the compiled method */
431 x86_jump_reg (buf, X86_EAX);
433 g_assert ((buf - vc) <= 24);
436 code = buf = g_malloc (16);
437 x86_push_imm (buf, method);
438 x86_jump_code (buf, vc);
439 g_assert ((buf - code) <= 16);
445 * arch_create_simple_jit_trampoline:
446 * @method: pointer to the method info
448 * Creates a trampoline function for method. If the created
449 * code is called it first starts JIT compilation of method,
450 * and then calls the newly created method. I also replaces the
451 * address in method->addr with the result of the JIT
452 * compilation step (in arch_compile_method).
454 * Returns: a pointer to the newly created code
457 arch_create_simple_jit_trampoline (MonoMethod *method)
464 /* we never free the allocated code buffer */
465 code = buf = g_malloc (16);
466 x86_push_imm (buf, method);
467 x86_call_code (buf, arch_compile_method);
468 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
469 /* jump to the compiled method */
470 x86_jump_reg (buf, X86_EAX);
471 g_assert ((buf - code) < 16);
477 mono_label_cfg (MonoFlowGraph *cfg)
481 for (i = 0; i < cfg->block_count; i++) {
482 GPtrArray *forest = cfg->bblocks [i].forest;
483 const int top = forest->len;
485 for (j = 0; j < top; j++) {
486 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
489 mbstate = mono_burg_label (t1, cfg);
492 if (mono_debug_handle)
494 g_warning ("tree does not match");
495 mono_print_ctree (t1); printf ("\n\n");
497 mono_print_forest (forest);
498 g_assert_not_reached ();
505 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
508 case MB_TERM_CALL_I4:
509 case MB_TERM_CALL_I8:
510 case MB_TERM_CALL_R8:
511 // case MB_TERM_CALL_VOID :
512 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
513 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
514 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
521 case MB_NTERM_lreg: {
526 tree->exclude_mask |= (1 << X86_ECX);
527 tree->left->exclude_mask |= (1 << X86_ECX);
530 case MB_TERM_MUL_OVF:
531 case MB_TERM_MUL_OVF_UN:
536 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
537 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
538 if (goal == MB_NTERM_reg) {
539 tree->left->exclude_mask |= (1 << X86_EDX);
540 tree->right->exclude_mask |= (1 << X86_EDX) | (1 << X86_EAX);
554 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
557 int ern = mono_burg_rule (tree->state, goal);
558 guint16 *nts = mono_burg_nts [ern];
561 mono_burg_kids (tree, ern, kids);
563 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
565 if (nts [0] && kids [0] == tree) {
567 tree_allocate_regs (kids [0], nts [0], rs);
571 for (i = 0; nts [i]; i++)
572 tree_preallocate_regs (kids [i], nts [i], rs);
574 for (i = 0; nts [i]; i++)
575 tree_allocate_regs (kids [i], nts [i], rs);
577 for (i = 0; nts [i]; i++) {
578 mono_regset_free_reg (rs, kids [i]->reg1);
579 mono_regset_free_reg (rs, kids [i]->reg2);
580 mono_regset_free_reg (rs, kids [i]->reg3);
585 if (tree->reg1 < 0) {
586 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
587 g_assert (tree->reg1 != -1);
592 if (tree->reg1 < 0) {
593 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
594 g_assert (tree->reg1 != -1);
596 if (tree->reg2 < 0) {
597 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
598 g_assert (tree->reg2 != -1);
603 /* fixme: allocate floating point registers */
607 if (tree->op == MB_TERM_ADD) {
608 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
609 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
614 if (tree->op == MB_TERM_ADD) {
615 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
620 if (tree->op == MB_TERM_SHL ||
621 tree->op == MB_TERM_MUL) {
622 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
630 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
631 tree->emit = mono_burg_func [ern];
635 arch_allocate_regs (MonoFlowGraph *cfg)
639 for (i = 0; i < cfg->block_count; i++) {
640 GPtrArray *forest = cfg->bblocks [i].forest;
641 const int top = forest->len;
643 for (j = 0; j < top; j++) {
644 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
645 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
646 tree_allocate_regs (t1, 1, cfg->rs);
647 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
648 g_assert (cfg->rs->free_mask == 0xffffffff);
654 tree_emit (int goal, MonoFlowGraph *cfg, MBTree *tree)
657 int i, ern = mono_burg_rule (tree->state, goal);
658 guint16 *nts = mono_burg_nts [ern];
662 mono_burg_kids (tree, ern, kids);
664 for (i = 0; nts [i]; i++)
665 tree_emit (nts [i], cfg, kids [i]);
667 tree->addr = offset = cfg->code - cfg->start;
669 // we assume an instruction uses a maximum of 128 bytes
670 if ((cfg->code_size - offset) <= 128) {
671 int add = MIN ((cfg->code_size * 2), 1024);
673 cfg->code_size += add;
674 cfg->start = g_realloc (cfg->start, cfg->code_size);
675 g_assert (cfg->start);
676 cfg->code = cfg->start + offset;
679 if ((emit = mono_burg_func [ern]))
682 g_assert ((cfg->code - cfg->start) < cfg->code_size);
686 mono_emit_cfg (MonoFlowGraph *cfg)
690 for (i = 0; i < cfg->block_count; i++) {
691 MonoBBlock *bb = &cfg->bblocks [i];
692 GPtrArray *forest = bb->forest;
693 const int top = forest->len;
695 bb->addr = cfg->code - cfg->start;
697 for (j = 0; j < top; j++) {
698 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
700 tree_emit (1, cfg, t1);
704 cfg->epilog = cfg->code - cfg->start;
708 mono_compute_branches (MonoFlowGraph *cfg)
715 for (j = 0; j < cfg->block_count; j++) {
716 MonoBBlock *bb = &cfg->bblocks [j];
717 GPtrArray *forest = bb->forest;
718 const int top = forest->len;
720 for (i = 0; i < top; i++) {
721 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
725 if (t1->op == MB_TERM_SWITCH) {
726 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
727 guint32 *rt = (guint32 *)t1->data.p;
729 int m = *((guint32 *)t1->data.p) + 1;
732 for (j = 1; j <= m; j++)
733 rt [j] = (int)(jt [j]->addr + cfg->start);
736 /* emit the jump instruction again to update addresses */
737 cfg->code = cfg->start + t1->addr;
738 ((MBEmitFunc)t1->emit) (t1, cfg);
748 match_debug_method (MonoMethod* method)
750 GList *tmp = mono_debug_methods;
752 for (; tmp; tmp = tmp->next) {
753 if (strcmp (method->name, tmp->data) == 0) {
761 * arch_compile_method:
762 * @method: pointer to the method info
764 * JIT compilation of a single method. This method also writes the result
765 * back to method->addr, an thus overwrites the trampoline function.
767 * Returns: a pointer to the newly created code.
770 arch_compile_method (MonoMethod *method)
773 MonoMemPool *mp = mono_mempool_new ();
775 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
776 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
778 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
779 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
780 method->klass->name, method->name);
783 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
784 MonoClassField *field;
785 const char *name = method->name;
786 static guint target_offset = 0;
787 static guint method_offset = 0;
789 gboolean delegate = FALSE;
791 if (method->klass->parent &&
792 method->klass->parent->parent == mono_defaults.delegate_class)
795 if (!target_offset) {
796 mono_class_init (mono_defaults.delegate_class);
798 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
799 target_offset = field->offset;
800 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
801 method_offset = field->offset;
804 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
805 method->addr = code = g_malloc (32);
806 x86_push_reg (code, X86_EBP);
807 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
809 /* load the this pointer */
810 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
811 /* load m_target arg */
812 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
814 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
815 /* load method_ptr arg */
816 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
817 /* store method_ptr */
818 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
823 g_assert ((code - (guint8*)method->addr) < 32);
825 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
826 MonoMethodSignature *csig = method->signature;
827 int i, target, this_pos = 4;
830 method->addr = g_malloc (64);
832 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
833 g_assert (!csig->ret->byref);
837 for (i = 0; i < 2; i ++) {
839 /* load the this pointer */
840 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
842 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
843 /* check if zero (static method call without this pointer) */
844 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
845 x86_branch32 (code, X86_CC_EQ, target, TRUE);
848 /* virtual delegate methods: we have to replace the this pointer
849 * withe the actual target */
850 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
851 /* jump to method_ptr() */
852 x86_jump_membase (code, X86_EAX, method_offset);
854 /* static delegate methods: we have to remove the this pointer
855 * from the activation frame */
856 target = code - source;
858 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
859 x86_mov_membase_reg (code, X86_ESP, 8, X86_EDX, 4);
861 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 0, 4);
862 x86_mov_membase_reg (code, X86_ESP, 4, X86_EDX, 4);
863 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
865 /* jump to method_ptr() */
866 x86_jump_membase (code, X86_EAX, method_offset);
869 g_assert ((code - (guint8*)method->addr) < 64);
872 if (mono_debug_handle)
874 g_error ("Don't know how to exec runtime method %s.%s::%s",
875 method->klass->name_space, method->klass->name, method->name);
879 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
880 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
882 cfg = mono_cfg_new (method, mp);
884 mono_analyze_flow (cfg);
888 mono_analyze_stack (cfg);
892 cfg->rs = mono_regset_new (X86_NREG);
893 mono_regset_reserve_reg (cfg->rs, X86_ESP);
894 mono_regset_reserve_reg (cfg->rs, X86_EBP);
896 cfg->code_size = 256;
897 cfg->start = cfg->code = g_malloc (cfg->code_size);
899 if (match_debug_method (method))
900 x86_breakpoint (cfg->code);
902 if (mono_jit_dump_forest) {
904 printf ("FOREST %s.%s:%s\n", method->klass->name_space,
905 method->klass->name, method->name);
906 for (i = 0; i < cfg->block_count; i++) {
907 printf ("BLOCK %d:\n", i);
908 mono_print_forest (cfg->bblocks [i].forest);
912 mono_label_cfg (cfg);
916 arch_allocate_regs (cfg);
918 /* align to 8 byte boundary */
919 cfg->locals_size += 7;
920 cfg->locals_size &= ~7;
922 arch_emit_prologue (cfg);
924 arch_emit_epilogue (cfg);
926 method->addr = cfg->start;
928 mono_compute_branches (cfg);
930 if (mono_jit_dump_asm) {
931 char *id = g_strdup_printf ("%s.%s_%s", method->klass->name_space,
932 method->klass->name, method->name);
933 mono_disassemble_code (cfg->start, cfg->code - cfg->start, id);
936 if (mono_debug_handle)
937 mono_debug_add_method (mono_debug_handle, cfg);
939 ji->code_size = cfg->code - cfg->start;
940 ji->used_regs = cfg->rs->used_mask;
942 ji->code_start = method->addr;
943 mono_jit_info_table_add (mono_jit_info_table, ji);
945 if (header->num_clauses) {
946 int i, start_block, end_block;
948 ji->num_clauses = header->num_clauses;
949 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
951 for (i = 0; i < header->num_clauses; i++) {
952 MonoExceptionClause *ec = &header->clauses [i];
953 MonoJitExceptionInfo *ei = &ji->clauses [i];
955 ei->flags = ec->flags;
956 ei->token_or_filter = ec->token_or_filter;
958 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
959 start_block = cfg->bcinfo [ec->try_offset].block_id;
960 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
961 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
963 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
964 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
966 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
967 start_block = cfg->bcinfo [ec->handler_offset].block_id;
968 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
970 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
974 mono_regset_free (cfg->rs);
978 mono_mempool_destroy (mp);
982 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
983 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
984 method->klass->name, method->name, method, method->addr);
992 * arch_get_restore_context:
994 * Returns a pointer to a method which restores a previously saved sigcontext.
997 arch_get_restore_context ()
999 static guint8 *start = NULL;
1005 /* restore_contect (struct sigcontext *ctx) */
1006 /* we do not restore X86_EAX, X86_EDX */
1008 start = code = malloc (1024);
1011 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
1013 /* get return address, stored in EDX */
1014 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
1017 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
1019 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
1021 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
1023 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
1025 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1026 /* restore ECX. the exception object is passed here to the catch handler */
1027 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
1029 /* jump to the saved IP */
1030 x86_jump_reg (code, X86_EDX);
1036 * arch_get_call_finally:
1038 * Returns a pointer to a method which calls a finally handler.
1041 arch_get_call_finally ()
1043 static guint8 *start = NULL;
1049 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1050 start = code = malloc (1024);
1052 x86_push_reg (code, X86_EBP);
1053 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1054 x86_push_reg (code, X86_EBX);
1055 x86_push_reg (code, X86_EDI);
1056 x86_push_reg (code, X86_ESI);
1059 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1061 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1063 x86_push_reg (code, X86_EBP);
1065 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1066 /* call the handler */
1067 x86_call_reg (code, X86_ECX);
1069 x86_pop_reg (code, X86_EBP);
1070 /* restore saved regs */
1071 x86_pop_reg (code, X86_ESI);
1072 x86_pop_reg (code, X86_EDI);
1073 x86_pop_reg (code, X86_EBX);
1081 * arch_handle_exception:
1082 * @ctx: saved processor state
1086 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1089 gpointer ip = (gpointer)ctx->eip;
1090 static void (*restore_context) (struct sigcontext *);
1091 static void (*call_finally) (struct sigcontext *, unsigned long);
1093 g_assert (ctx != NULL);
1094 g_assert (obj != NULL);
1096 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1098 if (!restore_context)
1099 restore_context = arch_get_restore_context ();
1102 call_finally = arch_get_call_finally ();
1104 if (ji) { /* we are inside managed code */
1105 MonoMethod *m = ji->method;
1109 if (ji->num_clauses) {
1112 g_assert (ji->clauses);
1114 for (i = 0; i < ji->num_clauses; i++) {
1115 MonoJitExceptionInfo *ei = &ji->clauses [i];
1117 if (ei->try_start <= ip && ip <= (ei->try_end)) {
1119 if (ei->flags == 0 && mono_object_isinst (obj,
1120 mono_class_get (m->klass->image, ei->token_or_filter))) {
1122 ctx->eip = (unsigned long)ei->handler_start;
1123 ctx->ecx = (unsigned long)obj;
1124 restore_context (ctx);
1125 g_assert_not_reached ();
1130 /* no handler found - we need to call all finally handlers */
1131 for (i = 0; i < ji->num_clauses; i++) {
1132 MonoJitExceptionInfo *ei = &ji->clauses [i];
1134 if (ei->try_start <= ip && ip < (ei->try_end) &&
1135 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1136 call_finally (ctx, (unsigned long)ei->handler_start);
1141 /* continue unwinding */
1143 /* restore caller saved registers */
1144 if (ji->used_regs & X86_ESI_MASK) {
1145 ctx->esi = *((int *)ctx->ebp + offset);
1148 if (ji->used_regs & X86_EDI_MASK) {
1149 ctx->edi = *((int *)ctx->ebp + offset);
1152 if (ji->used_regs & X86_EBX_MASK) {
1153 ctx->ebx = *((int *)ctx->ebp + offset);
1156 ctx->esp = ctx->ebp;
1157 ctx->eip = *((int *)ctx->ebp + 1);
1158 ctx->ebp = *((int *)ctx->ebp);
1160 if (next_bp < (unsigned)mono_end_of_stack)
1161 arch_handle_exception (ctx, obj);
1163 mono_jit_abort (obj);
1166 gpointer *lmf_addr = TlsGetValue (lmf_thread_id);
1170 g_assert (lmf_addr);
1171 lmf = *((MonoLMF **)lmf_addr);
1174 mono_jit_abort (obj);
1178 *lmf_addr = lmf->previous_lmf;
1180 ctx->esi = lmf->esi;
1181 ctx->edi = lmf->edi;
1182 ctx->ebx = lmf->ebx;
1183 ctx->ebp = lmf->ebp;
1184 ctx->eip = lmf->eip;
1188 g_warning ("Exception inside unmanaged code. %s.%s::%s %p", m->klass->name_space,
1189 m->klass->name, m->name, lmf->previous_lmf);
1192 if (ctx->eip < (unsigned)mono_end_of_stack)
1193 arch_handle_exception (ctx, obj);
1195 mono_jit_abort (obj);
1198 g_assert_not_reached ();
1202 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
1203 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
1204 unsigned long eip, unsigned long esp)
1206 struct sigcontext ctx;
1218 arch_handle_exception (&ctx, exc);
1220 g_assert_not_reached ();
1224 arch_get_throw_exception (void)
1226 static guint8 *start = NULL;
1232 code = start = g_malloc (1024);
1234 x86_push_reg (code, X86_ESP);
1235 x86_push_membase (code, X86_ESP, 4); /* IP */
1236 x86_push_membase (code, X86_ESP, 12); /* exception */
1237 x86_push_reg (code, X86_EBP);
1238 x86_push_reg (code, X86_EDI);
1239 x86_push_reg (code, X86_ESI);
1240 x86_push_reg (code, X86_EBX);
1241 x86_push_reg (code, X86_EDX);
1242 x86_push_reg (code, X86_ECX);
1243 x86_push_reg (code, X86_EAX);
1244 x86_call_code (code, throw_exception);
1245 /* we should never reach this breakpoint */
1246 x86_breakpoint (code);