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);
104 if (class == mono_defaults.string_class) {
105 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
106 } else if (class == mono_defaults.int32_class) {
107 printf ("[INT32:%p:%d], ", o, *(gint32 *)((gpointer)o + sizeof (MonoObject)));
109 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
111 printf ("%p, ", *((gpointer *)(ebp)));
116 case MONO_TYPE_FNPTR:
117 case MONO_TYPE_ARRAY:
118 case MONO_TYPE_SZARRAY:
119 printf ("%p, ", *((gpointer *)(ebp)));
122 printf ("%lld, ", *((gint64 *)(ebp)));
125 printf ("%f, ", *((float *)(ebp)));
128 printf ("%f, ", *((double *)(ebp)));
130 case MONO_TYPE_VALUETYPE:
132 for (j = 0; j < size; j++)
133 printf ("%02x,", *((guint8*)ebp +j));
141 ebp = (gpointer)((unsigned)ebp & ~(3));
148 leave_method (MonoMethod *method, int edx, int eax, double test)
152 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
153 method->klass->name, method->name);
155 switch (method->signature->ret->type) {
158 case MONO_TYPE_BOOLEAN:
160 printf ("TRUE:%d", eax);
174 printf ("EAX=%d", eax);
176 case MONO_TYPE_STRING: {
177 MonoString *s = (MonoString *)eax;
180 g_assert (((MonoObject *)s)->klass == mono_defaults.string_class);
181 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
183 printf ("[STRING:null], ");
186 case MONO_TYPE_OBJECT: {
187 MonoObject *o = (MonoObject *)eax;
190 if (o->klass == mono_defaults.boolean_class) {
191 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
192 } else if (o->klass == mono_defaults.int32_class) {
193 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
195 printf ("[%s.%s:%p]", o->klass->name_space, o->klass->name, o);
197 printf ("[OBJECT:%p]", o);
201 case MONO_TYPE_CLASS:
203 case MONO_TYPE_FNPTR:
204 case MONO_TYPE_ARRAY:
205 case MONO_TYPE_SZARRAY:
206 printf ("EAX=%p", (gpointer)eax);
209 *((gint32 *)&l) = eax;
210 *((gint32 *)&l + 1) = edx;
211 printf ("EAX/EDX=%lld", l);
214 printf ("FP=%f\n", test);
217 printf ("(unknown return type)");
224 * arch_emit_prologue:
225 * @cfg: pointer to status information
227 * Emits the function prolog.
230 arch_emit_prologue (MonoFlowGraph *cfg)
232 x86_push_reg (cfg->code, X86_EBP);
233 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
235 if (cfg->locals_size)
236 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
238 if (mono_regset_reg_used (cfg->rs, X86_EBX))
239 x86_push_reg (cfg->code, X86_EBX);
241 if (mono_regset_reg_used (cfg->rs, X86_EDI))
242 x86_push_reg (cfg->code, X86_EDI);
244 if (mono_regset_reg_used (cfg->rs, X86_ESI))
245 x86_push_reg (cfg->code, X86_ESI);
247 if (mono_jit_trace_calls) {
248 x86_push_reg (cfg->code, X86_EBP);
249 x86_push_imm (cfg->code, cfg->method);
250 x86_mov_reg_imm (cfg->code, X86_EAX, enter_method);
251 x86_call_reg (cfg->code, X86_EAX);
252 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
257 * arch_emit_epilogue:
258 * @cfg: pointer to status information
260 * Emits the function epilog.
263 arch_emit_epilogue (MonoFlowGraph *cfg)
265 if (mono_jit_trace_calls) {
266 x86_fld_reg (cfg->code, 0);
267 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
268 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
269 x86_push_reg (cfg->code, X86_EAX);
270 x86_push_reg (cfg->code, X86_EDX);
271 x86_push_imm (cfg->code, cfg->method);
272 x86_mov_reg_imm (cfg->code, X86_EAX, leave_method);
273 x86_call_reg (cfg->code, X86_EAX);
274 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
275 x86_pop_reg (cfg->code, X86_EDX);
276 x86_pop_reg (cfg->code, X86_EAX);
277 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
280 if (mono_regset_reg_used (cfg->rs, X86_ESI))
281 x86_pop_reg (cfg->code, X86_ESI);
283 if (mono_regset_reg_used (cfg->rs, X86_EDI))
284 x86_pop_reg (cfg->code, X86_EDI);
286 if (mono_regset_reg_used (cfg->rs, X86_EBX))
287 x86_pop_reg (cfg->code, X86_EBX);
289 x86_leave (cfg->code);
294 * get_unbox_trampoline:
297 * when value type methods are called through the vtable we need to unbox the
298 * this argument. This method returns a pointer to a trampoline which does
299 * unboxing before calling the method
302 get_unbox_trampoline (MonoMethod *m)
304 gpointer p = arch_compile_method (m);
305 guint8 *code, *start;
308 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
311 start = code = g_malloc (16);
313 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
314 x86_jump_code (code, p);
315 g_assert ((code - start) < 16);
321 * x86_magic_trampoline:
322 * @eax: saved x86 register
323 * @ecx: saved x86 register
324 * @edx: saved x86 register
325 * @esi: saved x86 register
326 * @edi: saved x86 register
327 * @ebx: saved x86 register
328 * @code: pointer into caller code
329 * @method: the method to translate
331 * This method is called by the trampoline functions for virtual
332 * methods. It inspects the caller code to find the address of the
333 * vtable slot, then calls the JIT compiler and writes the address
334 * of the compiled method back to the vtable. All virtual methods
335 * are called with: x86_call_membase (inst, basereg, disp). We always
336 * use 32 bit displacement to ensure that the length of the call
337 * instruction is 6 bytes. We need to get the value of the basereg
338 * and the constant displacement.
341 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
342 int ebx, guint8 *code, MonoMethod *m)
348 /* go to the start of the call instruction */
350 g_assert (*code == 0xff);
354 g_assert ((ab >> 6) == 2);
356 /* extract the register number containing the address */
360 /* extract the displacement */
361 disp = *((gint32*)code);
383 g_assert_not_reached ();
388 if (m->klass->valuetype) {
389 return *((gpointer *)o) = get_unbox_trampoline (m);
391 return *((gpointer *)o) = arch_compile_method (m);
395 * arch_create_jit_trampoline:
396 * @method: pointer to the method info
398 * Creates a trampoline function for virtual methods. If the created
399 * code is called it first starts JIT compilation of method,
400 * and then calls the newly created method. I also replaces the
401 * corresponding vtable entry (see x86_magic_trampoline).
403 * Returns: a pointer to the newly created code
406 arch_create_jit_trampoline (MonoMethod *method)
409 static guint8 *vc = NULL;
415 vc = buf = g_malloc (24);
417 /* push the return address onto the stack */
418 x86_push_membase (buf, X86_ESP, 4);
420 /* save all register values */
421 x86_push_reg (buf, X86_EBX);
422 x86_push_reg (buf, X86_EDI);
423 x86_push_reg (buf, X86_ESI);
424 x86_push_reg (buf, X86_EDX);
425 x86_push_reg (buf, X86_ECX);
426 x86_push_reg (buf, X86_EAX);
428 x86_call_code (buf, x86_magic_trampoline);
429 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
431 /* call the compiled method */
432 x86_jump_reg (buf, X86_EAX);
434 g_assert ((buf - vc) <= 24);
437 code = buf = g_malloc (16);
438 x86_push_imm (buf, method);
439 x86_jump_code (buf, vc);
440 g_assert ((buf - code) <= 16);
446 * arch_create_simple_jit_trampoline:
447 * @method: pointer to the method info
449 * Creates a trampoline function for method. If the created
450 * code is called it first starts JIT compilation of method,
451 * and then calls the newly created method. I also replaces the
452 * address in method->addr with the result of the JIT
453 * compilation step (in arch_compile_method).
455 * Returns: a pointer to the newly created code
458 arch_create_simple_jit_trampoline (MonoMethod *method)
465 /* we never free the allocated code buffer */
466 code = buf = g_malloc (16);
467 x86_push_imm (buf, method);
468 x86_call_code (buf, arch_compile_method);
469 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
470 /* jump to the compiled method */
471 x86_jump_reg (buf, X86_EAX);
472 g_assert ((buf - code) < 16);
478 mono_label_cfg (MonoFlowGraph *cfg)
482 for (i = 0; i < cfg->block_count; i++) {
483 GPtrArray *forest = cfg->bblocks [i].forest;
484 const int top = forest->len;
486 for (j = 0; j < top; j++) {
487 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
490 mbstate = mono_burg_label (t1, cfg);
493 if (mono_debug_handle)
495 g_warning ("tree does not match");
496 mono_print_ctree (t1); printf ("\n\n");
498 mono_print_forest (forest);
499 g_assert_not_reached ();
506 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
509 case MB_TERM_CALL_I4:
510 case MB_TERM_CALL_I8:
511 case MB_TERM_CALL_R8:
512 // case MB_TERM_CALL_VOID :
513 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
514 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
515 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
522 case MB_NTERM_lreg: {
527 tree->exclude_mask |= (1 << X86_ECX);
528 tree->left->exclude_mask |= (1 << X86_ECX);
531 case MB_TERM_MUL_OVF:
532 case MB_TERM_MUL_OVF_UN:
537 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
538 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
539 if (goal == MB_NTERM_reg) {
540 tree->left->exclude_mask |= (1 << X86_EDX);
541 tree->right->exclude_mask |= (1 << X86_EDX) | (1 << X86_EAX);
555 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
558 int ern = mono_burg_rule (tree->state, goal);
559 guint16 *nts = mono_burg_nts [ern];
562 mono_burg_kids (tree, ern, kids);
564 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
566 if (nts [0] && kids [0] == tree) {
568 tree_allocate_regs (kids [0], nts [0], rs);
572 for (i = 0; nts [i]; i++)
573 tree_preallocate_regs (kids [i], nts [i], rs);
575 for (i = 0; nts [i]; i++)
576 tree_allocate_regs (kids [i], nts [i], rs);
578 for (i = 0; nts [i]; i++) {
579 mono_regset_free_reg (rs, kids [i]->reg1);
580 mono_regset_free_reg (rs, kids [i]->reg2);
581 mono_regset_free_reg (rs, kids [i]->reg3);
586 if (tree->reg1 < 0) {
587 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
588 g_assert (tree->reg1 != -1);
593 if (tree->reg1 < 0) {
594 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
595 g_assert (tree->reg1 != -1);
597 if (tree->reg2 < 0) {
598 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
599 g_assert (tree->reg2 != -1);
604 /* fixme: allocate floating point registers */
608 if (tree->op == MB_TERM_ADD) {
609 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
610 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
615 if (tree->op == MB_TERM_ADD) {
616 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
621 if (tree->op == MB_TERM_SHL ||
622 tree->op == MB_TERM_MUL) {
623 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
631 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
632 tree->emit = mono_burg_func [ern];
636 arch_allocate_regs (MonoFlowGraph *cfg)
640 for (i = 0; i < cfg->block_count; i++) {
641 GPtrArray *forest = cfg->bblocks [i].forest;
642 const int top = forest->len;
644 for (j = 0; j < top; j++) {
645 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
646 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
647 tree_allocate_regs (t1, 1, cfg->rs);
648 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
649 g_assert (cfg->rs->free_mask == 0xffffffff);
655 tree_emit (int goal, MonoFlowGraph *cfg, MBTree *tree)
658 int i, ern = mono_burg_rule (tree->state, goal);
659 guint16 *nts = mono_burg_nts [ern];
663 mono_burg_kids (tree, ern, kids);
665 for (i = 0; nts [i]; i++)
666 tree_emit (nts [i], cfg, kids [i]);
668 tree->addr = offset = cfg->code - cfg->start;
670 // we assume an instruction uses a maximum of 128 bytes
671 if ((cfg->code_size - offset) <= 128) {
672 int add = MIN ((cfg->code_size * 2), 1024);
674 cfg->code_size += add;
675 cfg->start = g_realloc (cfg->start, cfg->code_size);
676 g_assert (cfg->start);
677 cfg->code = cfg->start + offset;
680 if ((emit = mono_burg_func [ern]))
683 g_assert ((cfg->code - cfg->start) < cfg->code_size);
687 mono_emit_cfg (MonoFlowGraph *cfg)
691 for (i = 0; i < cfg->block_count; i++) {
692 MonoBBlock *bb = &cfg->bblocks [i];
693 GPtrArray *forest = bb->forest;
694 const int top = forest->len;
696 bb->addr = cfg->code - cfg->start;
698 for (j = 0; j < top; j++) {
699 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
701 tree_emit (1, cfg, t1);
705 cfg->epilog = cfg->code - cfg->start;
709 mono_compute_branches (MonoFlowGraph *cfg)
716 for (j = 0; j < cfg->block_count; j++) {
717 MonoBBlock *bb = &cfg->bblocks [j];
718 GPtrArray *forest = bb->forest;
719 const int top = forest->len;
721 for (i = 0; i < top; i++) {
722 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
726 if (t1->op == MB_TERM_SWITCH) {
727 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
728 guint32 *rt = (guint32 *)t1->data.p;
730 int m = *((guint32 *)t1->data.p) + 1;
733 for (j = 1; j <= m; j++)
734 rt [j] = (int)(jt [j]->addr + cfg->start);
737 /* emit the jump instruction again to update addresses */
738 cfg->code = cfg->start + t1->addr;
739 ((MBEmitFunc)t1->emit) (t1, cfg);
749 match_debug_method (MonoMethod* method)
751 GList *tmp = mono_debug_methods;
753 for (; tmp; tmp = tmp->next) {
754 if (strcmp (method->name, tmp->data) == 0) {
762 * arch_compile_method:
763 * @method: pointer to the method info
765 * JIT compilation of a single method. This method also writes the result
766 * back to method->addr, an thus overwrites the trampoline function.
768 * Returns: a pointer to the newly created code.
771 arch_compile_method (MonoMethod *method)
774 MonoMemPool *mp = mono_mempool_new ();
776 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
777 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
779 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
780 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
781 method->klass->name, method->name);
784 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
785 MonoClassField *field;
786 const char *name = method->name;
787 static guint target_offset = 0;
788 static guint method_offset = 0;
790 gboolean delegate = FALSE;
792 if (method->klass->parent &&
793 method->klass->parent->parent == mono_defaults.delegate_class)
796 if (!target_offset) {
797 mono_class_init (mono_defaults.delegate_class);
799 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
800 target_offset = field->offset;
801 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
802 method_offset = field->offset;
805 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
806 method->addr = code = g_malloc (32);
807 x86_push_reg (code, X86_EBP);
808 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
810 /* load the this pointer */
811 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
812 /* load m_target arg */
813 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
815 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
816 /* load method_ptr arg */
817 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
818 /* store method_ptr */
819 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
824 g_assert ((code - (guint8*)method->addr) < 32);
826 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
827 MonoMethodSignature *csig = method->signature;
828 int i, target, this_pos = 4;
831 method->addr = g_malloc (64);
833 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
834 g_assert (!csig->ret->byref);
838 for (i = 0; i < 2; i ++) {
840 /* load the this pointer */
841 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
843 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
844 /* check if zero (static method call without this pointer) */
845 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
846 x86_branch32 (code, X86_CC_EQ, target, TRUE);
849 /* virtual delegate methods: we have to replace the this pointer
850 * withe the actual target */
851 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
852 /* jump to method_ptr() */
853 x86_jump_membase (code, X86_EAX, method_offset);
855 /* static delegate methods: we have to remove the this pointer
856 * from the activation frame */
857 target = code - source;
859 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
860 x86_mov_membase_reg (code, X86_ESP, 8, X86_EDX, 4);
862 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 0, 4);
863 x86_mov_membase_reg (code, X86_ESP, 4, X86_EDX, 4);
864 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
866 /* jump to method_ptr() */
867 x86_jump_membase (code, X86_EAX, method_offset);
870 g_assert ((code - (guint8*)method->addr) < 64);
873 if (mono_debug_handle)
875 g_error ("Don't know how to exec runtime method %s.%s::%s",
876 method->klass->name_space, method->klass->name, method->name);
880 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
881 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
883 cfg = mono_cfg_new (method, mp);
885 mono_analyze_flow (cfg);
889 mono_analyze_stack (cfg);
893 cfg->rs = mono_regset_new (X86_NREG);
894 mono_regset_reserve_reg (cfg->rs, X86_ESP);
895 mono_regset_reserve_reg (cfg->rs, X86_EBP);
897 cfg->code_size = 256;
898 cfg->start = cfg->code = g_malloc (cfg->code_size);
900 if (match_debug_method (method))
901 x86_breakpoint (cfg->code);
903 if (mono_jit_dump_forest) {
905 printf ("FOREST %s.%s:%s\n", method->klass->name_space,
906 method->klass->name, method->name);
907 for (i = 0; i < cfg->block_count; i++) {
908 printf ("BLOCK %d:\n", i);
909 mono_print_forest (cfg->bblocks [i].forest);
913 mono_label_cfg (cfg);
917 arch_allocate_regs (cfg);
919 /* align to 8 byte boundary */
920 cfg->locals_size += 7;
921 cfg->locals_size &= ~7;
923 arch_emit_prologue (cfg);
925 arch_emit_epilogue (cfg);
927 method->addr = cfg->start;
929 mono_compute_branches (cfg);
931 if (mono_jit_dump_asm) {
932 char *id = g_strdup_printf ("%s.%s_%s", method->klass->name_space,
933 method->klass->name, method->name);
934 mono_disassemble_code (cfg->start, cfg->code - cfg->start, id);
937 if (mono_debug_handle)
938 mono_debug_add_method (mono_debug_handle, cfg);
940 ji->code_size = cfg->code - cfg->start;
941 ji->used_regs = cfg->rs->used_mask;
943 ji->code_start = method->addr;
944 mono_jit_info_table_add (mono_jit_info_table, ji);
946 if (header->num_clauses) {
947 int i, start_block, end_block;
949 ji->num_clauses = header->num_clauses;
950 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
952 for (i = 0; i < header->num_clauses; i++) {
953 MonoExceptionClause *ec = &header->clauses [i];
954 MonoJitExceptionInfo *ei = &ji->clauses [i];
956 ei->flags = ec->flags;
957 ei->token_or_filter = ec->token_or_filter;
959 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
960 start_block = cfg->bcinfo [ec->try_offset].block_id;
961 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
962 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
964 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
965 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
967 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
968 start_block = cfg->bcinfo [ec->handler_offset].block_id;
969 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
971 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
975 mono_regset_free (cfg->rs);
979 mono_mempool_destroy (mp);
983 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
984 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
985 method->klass->name, method->name, method, method->addr);
993 * arch_get_restore_context:
995 * Returns a pointer to a method which restores a previously saved sigcontext.
998 arch_get_restore_context ()
1000 static guint8 *start = NULL;
1006 /* restore_contect (struct sigcontext *ctx) */
1007 /* we do not restore X86_EAX, X86_EDX */
1009 start = code = malloc (1024);
1012 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
1014 /* get return address, stored in EDX */
1015 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
1018 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
1020 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
1022 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
1024 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
1026 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1027 /* restore ECX. the exception object is passed here to the catch handler */
1028 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
1030 /* jump to the saved IP */
1031 x86_jump_reg (code, X86_EDX);
1037 * arch_get_call_finally:
1039 * Returns a pointer to a method which calls a finally handler.
1042 arch_get_call_finally ()
1044 static guint8 *start = NULL;
1050 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1051 start = code = malloc (1024);
1053 x86_push_reg (code, X86_EBP);
1054 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1055 x86_push_reg (code, X86_EBX);
1056 x86_push_reg (code, X86_EDI);
1057 x86_push_reg (code, X86_ESI);
1060 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1062 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1064 x86_push_reg (code, X86_EBP);
1066 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1067 /* call the handler */
1068 x86_call_reg (code, X86_ECX);
1070 x86_pop_reg (code, X86_EBP);
1071 /* restore saved regs */
1072 x86_pop_reg (code, X86_ESI);
1073 x86_pop_reg (code, X86_EDI);
1074 x86_pop_reg (code, X86_EBX);
1082 * arch_handle_exception:
1083 * @ctx: saved processor state
1087 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1090 gpointer ip = (gpointer)ctx->eip;
1091 static void (*restore_context) (struct sigcontext *);
1092 static void (*call_finally) (struct sigcontext *, unsigned long);
1094 g_assert (ctx != NULL);
1095 g_assert (obj != NULL);
1097 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1099 if (!restore_context)
1100 restore_context = arch_get_restore_context ();
1103 call_finally = arch_get_call_finally ();
1105 if (ji) { /* we are inside managed code */
1106 MonoMethod *m = ji->method;
1110 if (ji->num_clauses) {
1113 g_assert (ji->clauses);
1115 for (i = 0; i < ji->num_clauses; i++) {
1116 MonoJitExceptionInfo *ei = &ji->clauses [i];
1118 if (ei->try_start <= ip && ip <= (ei->try_end)) {
1120 if (ei->flags == 0 && mono_object_isinst (obj,
1121 mono_class_get (m->klass->image, ei->token_or_filter))) {
1123 ctx->eip = (unsigned long)ei->handler_start;
1124 ctx->ecx = (unsigned long)obj;
1125 restore_context (ctx);
1126 g_assert_not_reached ();
1131 /* no handler found - we need to call all finally handlers */
1132 for (i = 0; i < ji->num_clauses; i++) {
1133 MonoJitExceptionInfo *ei = &ji->clauses [i];
1135 if (ei->try_start <= ip && ip < (ei->try_end) &&
1136 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1137 call_finally (ctx, (unsigned long)ei->handler_start);
1142 /* continue unwinding */
1144 /* restore caller saved registers */
1145 if (ji->used_regs & X86_ESI_MASK) {
1146 ctx->esi = *((int *)ctx->ebp + offset);
1149 if (ji->used_regs & X86_EDI_MASK) {
1150 ctx->edi = *((int *)ctx->ebp + offset);
1153 if (ji->used_regs & X86_EBX_MASK) {
1154 ctx->ebx = *((int *)ctx->ebp + offset);
1157 ctx->esp = ctx->ebp;
1158 ctx->eip = *((int *)ctx->ebp + 1);
1159 ctx->ebp = *((int *)ctx->ebp);
1161 if (next_bp < (unsigned)mono_end_of_stack)
1162 arch_handle_exception (ctx, obj);
1164 mono_jit_abort (obj);
1167 gpointer *lmf_addr = TlsGetValue (lmf_thread_id);
1171 g_assert (lmf_addr);
1172 lmf = *((MonoLMF **)lmf_addr);
1175 mono_jit_abort (obj);
1179 *lmf_addr = lmf->previous_lmf;
1181 ctx->esi = lmf->esi;
1182 ctx->edi = lmf->edi;
1183 ctx->ebx = lmf->ebx;
1184 ctx->ebp = lmf->ebp;
1185 ctx->eip = lmf->eip;
1189 g_warning ("Exception inside unmanaged code. %s.%s::%s %p", m->klass->name_space,
1190 m->klass->name, m->name, lmf->previous_lmf);
1193 if (ctx->eip < (unsigned)mono_end_of_stack)
1194 arch_handle_exception (ctx, obj);
1196 mono_jit_abort (obj);
1199 g_assert_not_reached ();
1203 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
1204 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
1205 unsigned long eip, unsigned long esp)
1207 struct sigcontext ctx;
1219 arch_handle_exception (&ctx, exc);
1221 g_assert_not_reached ();
1225 arch_get_throw_exception (void)
1227 static guint8 *start = NULL;
1233 code = start = g_malloc (1024);
1235 x86_push_reg (code, X86_ESP);
1236 x86_push_membase (code, X86_ESP, 4); /* IP */
1237 x86_push_membase (code, X86_ESP, 12); /* exception */
1238 x86_push_reg (code, X86_EBP);
1239 x86_push_reg (code, X86_EDI);
1240 x86_push_reg (code, X86_ESI);
1241 x86_push_reg (code, X86_EBX);
1242 x86_push_reg (code, X86_EDX);
1243 x86_push_reg (code, X86_ECX);
1244 x86_push_reg (code, X86_EAX);
1245 x86_call_code (code, throw_exception);
1246 /* we should never reach this breakpoint */
1247 x86_breakpoint (code);