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);
37 if (((int)ebp & 3) != 0) {
38 g_error ("unaligned stack detected (%p)", ebp);
43 if (ISSTRUCT (method->signature->ret)) {
46 g_assert (!method->signature->ret->byref);
48 size = mono_type_stack_size (method->signature->ret, &align);
50 printf ("VALUERET:%p, ", *((gpointer *)ebp));
51 ebp += sizeof (gpointer);
54 if (method->signature->hasthis) {
55 if (method->klass->valuetype) {
56 printf ("value:%p, ", *((gpointer *)ebp));
58 o = *((MonoObject **)ebp);
62 class = o->vtable->klass;
64 if (class == mono_defaults.string_class) {
65 printf ("this:[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
67 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
70 ebp += sizeof (gpointer);
73 for (i = 0; i < method->signature->param_count; ++i) {
74 MonoType *type = method->signature->params [i];
76 size = mono_type_stack_size (type, &align);
79 printf ("[BYREF:%p], ", *((gpointer *)ebp));
80 } else switch (type->type) {
82 case MONO_TYPE_BOOLEAN:
92 printf ("%d, ", *((int *)(ebp)));
94 case MONO_TYPE_STRING: {
95 MonoString *s = *((MonoString **)ebp);
97 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
98 printf ("[STRING:%p:%s], ", s, mono_string_to_utf8 (s));
100 printf ("[STRING:null], ");
103 case MONO_TYPE_CLASS:
104 case MONO_TYPE_OBJECT: {
105 o = *((MonoObject **)ebp);
107 class = o->vtable->klass;
109 if (class == mono_defaults.string_class) {
110 printf ("[STRING:%p:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
111 } else if (class == mono_defaults.int32_class) {
112 printf ("[INT32:%p:%d], ", o, *(gint32 *)((gpointer)o + sizeof (MonoObject)));
114 printf ("[%s.%s:%p], ", class->name_space, class->name, o);
116 printf ("%p, ", *((gpointer *)(ebp)));
121 case MONO_TYPE_FNPTR:
122 case MONO_TYPE_ARRAY:
123 case MONO_TYPE_SZARRAY:
124 printf ("%p, ", *((gpointer *)(ebp)));
127 printf ("%lld, ", *((gint64 *)(ebp)));
130 printf ("%f, ", *((float *)(ebp)));
133 printf ("%f, ", *((double *)(ebp)));
135 case MONO_TYPE_VALUETYPE:
137 for (j = 0; j < size; j++)
138 printf ("%02x,", *((guint8*)ebp +j));
145 g_assert (align == 4);
147 ebp = (gpointer)((unsigned)ebp & ~(3));
154 leave_method (MonoMethod *method, int edx, int eax, double test)
158 printf ("LEAVE: %s.%s::%s ", method->klass->name_space,
159 method->klass->name, method->name);
161 switch (method->signature->ret->type) {
164 case MONO_TYPE_BOOLEAN:
166 printf ("TRUE:%d", eax);
180 printf ("EAX=%d", eax);
182 case MONO_TYPE_STRING: {
183 MonoString *s = (MonoString *)eax;
186 g_assert (((MonoObject *)s)->vtable->klass == mono_defaults.string_class);
187 printf ("[STRING:%p:%s]", s, mono_string_to_utf8 (s));
189 printf ("[STRING:null], ");
192 case MONO_TYPE_OBJECT: {
193 MonoObject *o = (MonoObject *)eax;
196 if (o->vtable->klass == mono_defaults.boolean_class) {
197 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
198 } else if (o->vtable->klass == mono_defaults.int32_class) {
199 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
201 printf ("[%s.%s:%p]", o->vtable->klass->name_space, o->vtable->klass->name, o);
203 printf ("[OBJECT:%p]", o);
207 case MONO_TYPE_CLASS:
209 case MONO_TYPE_FNPTR:
210 case MONO_TYPE_ARRAY:
211 case MONO_TYPE_SZARRAY:
212 printf ("EAX=%p", (gpointer)eax);
215 *((gint32 *)&l) = eax;
216 *((gint32 *)&l + 1) = edx;
217 printf ("EAX/EDX=%lld", l);
220 printf ("FP=%f\n", test);
223 printf ("(unknown return type)");
230 * arch_emit_prologue:
231 * @cfg: pointer to status information
233 * Emits the function prolog.
236 arch_emit_prologue (MonoFlowGraph *cfg)
238 x86_push_reg (cfg->code, X86_EBP);
239 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
241 if (cfg->locals_size)
242 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
244 if (mono_regset_reg_used (cfg->rs, X86_EBX))
245 x86_push_reg (cfg->code, X86_EBX);
247 if (mono_regset_reg_used (cfg->rs, X86_EDI))
248 x86_push_reg (cfg->code, X86_EDI);
250 if (mono_regset_reg_used (cfg->rs, X86_ESI))
251 x86_push_reg (cfg->code, X86_ESI);
253 if (mono_jit_trace_calls) {
254 x86_push_reg (cfg->code, X86_EBP);
255 x86_push_imm (cfg->code, cfg->method);
256 x86_mov_reg_imm (cfg->code, X86_EAX, enter_method);
257 x86_call_reg (cfg->code, X86_EAX);
258 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
263 * arch_emit_epilogue:
264 * @cfg: pointer to status information
266 * Emits the function epilog.
269 arch_emit_epilogue (MonoFlowGraph *cfg)
271 if (mono_jit_trace_calls) {
272 x86_fld_reg (cfg->code, 0);
273 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
274 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
275 x86_push_reg (cfg->code, X86_EAX);
276 x86_push_reg (cfg->code, X86_EDX);
277 x86_push_imm (cfg->code, cfg->method);
278 x86_mov_reg_imm (cfg->code, X86_EAX, leave_method);
279 x86_call_reg (cfg->code, X86_EAX);
280 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
281 x86_pop_reg (cfg->code, X86_EDX);
282 x86_pop_reg (cfg->code, X86_EAX);
283 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
286 if (mono_regset_reg_used (cfg->rs, X86_ESI))
287 x86_pop_reg (cfg->code, X86_ESI);
289 if (mono_regset_reg_used (cfg->rs, X86_EDI))
290 x86_pop_reg (cfg->code, X86_EDI);
292 if (mono_regset_reg_used (cfg->rs, X86_EBX))
293 x86_pop_reg (cfg->code, X86_EBX);
295 x86_leave (cfg->code);
300 * get_unbox_trampoline:
303 * when value type methods are called through the vtable we need to unbox the
304 * this argument. This method returns a pointer to a trampoline which does
305 * unboxing before calling the method
308 get_unbox_trampoline (MonoMethod *m)
310 gpointer p = arch_compile_method (m);
311 guint8 *code, *start;
314 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
317 start = code = g_malloc (16);
319 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
320 x86_jump_code (code, p);
321 g_assert ((code - start) < 16);
327 * x86_magic_trampoline:
328 * @eax: saved x86 register
329 * @ecx: saved x86 register
330 * @edx: saved x86 register
331 * @esi: saved x86 register
332 * @edi: saved x86 register
333 * @ebx: saved x86 register
334 * @code: pointer into caller code
335 * @method: the method to translate
337 * This method is called by the trampoline functions for virtual
338 * methods. It inspects the caller code to find the address of the
339 * vtable slot, then calls the JIT compiler and writes the address
340 * of the compiled method back to the vtable. All virtual methods
341 * are called with: x86_call_membase (inst, basereg, disp). We always
342 * use 32 bit displacement to ensure that the length of the call
343 * instruction is 6 bytes. We need to get the value of the basereg
344 * and the constant displacement.
347 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
348 int ebx, const guint8 *code, MonoMethod *m)
354 /* go to the start of the call instruction
356 * address_byte = (m << 6) | (o << 3) | reg
357 * call opcode: 0xff address_byte displacement
362 if ((code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
363 reg = code [4] & 0x07;
364 disp = (signed char)code [5];
366 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
367 reg = code [1] & 0x07;
368 disp = *((gint32*)(code + 2));
370 g_assert_not_reached ();
394 g_assert_not_reached ();
399 if (m->klass->valuetype) {
400 return *((gpointer *)o) = get_unbox_trampoline (m);
402 return *((gpointer *)o) = arch_compile_method (m);
406 * arch_create_jit_trampoline:
407 * @method: pointer to the method info
409 * Creates a trampoline function for virtual methods. If the created
410 * code is called it first starts JIT compilation of method,
411 * and then calls the newly created method. I also replaces the
412 * corresponding vtable entry (see x86_magic_trampoline).
414 * Returns: a pointer to the newly created code
417 arch_create_jit_trampoline (MonoMethod *method)
420 static guint8 *vc = NULL;
426 vc = buf = g_malloc (24);
428 /* push the return address onto the stack */
429 x86_push_membase (buf, X86_ESP, 4);
431 /* save all register values */
432 x86_push_reg (buf, X86_EBX);
433 x86_push_reg (buf, X86_EDI);
434 x86_push_reg (buf, X86_ESI);
435 x86_push_reg (buf, X86_EDX);
436 x86_push_reg (buf, X86_ECX);
437 x86_push_reg (buf, X86_EAX);
439 x86_call_code (buf, x86_magic_trampoline);
440 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
442 /* call the compiled method */
443 x86_jump_reg (buf, X86_EAX);
445 g_assert ((buf - vc) <= 24);
448 code = buf = g_malloc (16);
449 x86_push_imm (buf, method);
450 x86_jump_code (buf, vc);
451 g_assert ((buf - code) <= 16);
457 * arch_create_simple_jit_trampoline:
458 * @method: pointer to the method info
460 * Creates a trampoline function for method. If the created
461 * code is called it first starts JIT compilation of method,
462 * and then calls the newly created method. I also replaces the
463 * address in method->addr with the result of the JIT
464 * compilation step (in arch_compile_method).
466 * Returns: a pointer to the newly created code
469 arch_create_simple_jit_trampoline (MonoMethod *method)
476 /* we never free the allocated code buffer */
477 code = buf = g_malloc (16);
478 x86_push_imm (buf, method);
479 x86_call_code (buf, arch_compile_method);
480 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
481 /* jump to the compiled method */
482 x86_jump_reg (buf, X86_EAX);
483 g_assert ((buf - code) < 16);
489 mono_label_cfg (MonoFlowGraph *cfg)
493 for (i = 0; i < cfg->block_count; i++) {
494 GPtrArray *forest = cfg->bblocks [i].forest;
495 const int top = forest->len;
497 for (j = 0; j < top; j++) {
498 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
501 mbstate = mono_burg_label (t1, cfg);
504 if (mono_debug_handle)
506 g_warning ("tree does not match");
507 mono_print_ctree (t1); printf ("\n\n");
509 mono_print_forest (forest);
510 g_assert_not_reached ();
517 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
520 case MB_TERM_CALL_I4:
521 case MB_TERM_CALL_I8:
522 case MB_TERM_CALL_R8:
523 // case MB_TERM_CALL_VOID :
524 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
525 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
526 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
533 case MB_NTERM_lreg: {
538 tree->exclude_mask |= (1 << X86_ECX);
539 tree->left->exclude_mask |= (1 << X86_ECX);
542 case MB_TERM_MUL_OVF:
543 case MB_TERM_MUL_OVF_UN:
548 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
549 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
550 if (goal == MB_NTERM_reg) {
551 tree->left->exclude_mask |= (1 << X86_EDX);
552 tree->right->exclude_mask |= (1 << X86_EDX) | (1 << X86_EAX);
566 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
569 int ern = mono_burg_rule (tree->state, goal);
570 const guint16 *nts = mono_burg_nts [ern];
573 mono_burg_kids (tree, ern, kids);
575 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
577 if (nts [0] && kids [0] == tree) {
579 tree_allocate_regs (kids [0], nts [0], rs);
583 for (i = 0; nts [i]; i++)
584 tree_preallocate_regs (kids [i], nts [i], rs);
586 for (i = 0; nts [i]; i++)
587 tree_allocate_regs (kids [i], nts [i], rs);
589 for (i = 0; nts [i]; i++) {
590 mono_regset_free_reg (rs, kids [i]->reg1);
591 mono_regset_free_reg (rs, kids [i]->reg2);
592 mono_regset_free_reg (rs, kids [i]->reg3);
597 if (tree->reg1 < 0) {
598 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
599 g_assert (tree->reg1 != -1);
604 if (tree->reg1 < 0) {
605 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
606 g_assert (tree->reg1 != -1);
608 if (tree->reg2 < 0) {
609 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
610 g_assert (tree->reg2 != -1);
615 /* fixme: allocate floating point registers */
619 if (tree->op == MB_TERM_ADD) {
620 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
621 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
626 if (tree->op == MB_TERM_ADD) {
627 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
632 if (tree->op == MB_TERM_SHL ||
633 tree->op == MB_TERM_MUL) {
634 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
642 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
643 tree->emit = mono_burg_func [ern];
647 arch_allocate_regs (MonoFlowGraph *cfg)
651 for (i = 0; i < cfg->block_count; i++) {
652 GPtrArray *forest = cfg->bblocks [i].forest;
653 const int top = forest->len;
655 for (j = 0; j < top; j++) {
656 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
657 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
658 tree_allocate_regs (t1, 1, cfg->rs);
659 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
660 g_assert (cfg->rs->free_mask == 0xffffffff);
666 tree_emit (int goal, MonoFlowGraph *cfg, MBTree *tree)
669 int i, ern = mono_burg_rule (tree->state, goal);
670 const guint16 *nts = mono_burg_nts [ern];
674 mono_burg_kids (tree, ern, kids);
676 for (i = 0; nts [i]; i++)
677 tree_emit (nts [i], cfg, kids [i]);
679 tree->addr = offset = cfg->code - cfg->start;
681 // we assume an instruction uses a maximum of 128 bytes
682 if ((cfg->code_size - offset) <= 128) {
683 int add = MIN ((cfg->code_size * 2), 1024);
685 cfg->code_size += add;
686 cfg->start = g_realloc (cfg->start, cfg->code_size);
687 g_assert (cfg->start);
688 cfg->code = cfg->start + offset;
691 if ((emit = mono_burg_func [ern]))
694 g_assert ((cfg->code - cfg->start) < cfg->code_size);
698 mono_emit_cfg (MonoFlowGraph *cfg)
702 for (i = 0; i < cfg->block_count; i++) {
703 MonoBBlock *bb = &cfg->bblocks [i];
704 GPtrArray *forest = bb->forest;
705 const int top = forest->len;
707 bb->addr = cfg->code - cfg->start;
709 for (j = 0; j < top; j++) {
710 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
712 tree_emit (1, cfg, t1);
716 cfg->epilog = cfg->code - cfg->start;
720 mono_compute_branches (MonoFlowGraph *cfg)
727 for (j = 0; j < cfg->block_count; j++) {
728 MonoBBlock *bb = &cfg->bblocks [j];
729 GPtrArray *forest = bb->forest;
730 const int top = forest->len;
732 for (i = 0; i < top; i++) {
733 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
737 if (t1->op == MB_TERM_SWITCH) {
738 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
739 guint32 *rt = (guint32 *)t1->data.p;
741 int m = *((guint32 *)t1->data.p) + 1;
744 for (j = 1; j <= m; j++)
745 rt [j] = (int)(jt [j]->addr + cfg->start);
748 /* emit the jump instruction again to update addresses */
749 cfg->code = cfg->start + t1->addr;
750 ((MBEmitFunc)t1->emit) (t1, cfg);
760 match_debug_method (MonoMethod* method)
762 GList *tmp = mono_debug_methods;
764 for (; tmp; tmp = tmp->next) {
765 if (strcmp (method->name, tmp->data) == 0) {
773 * arch_compile_method:
774 * @method: pointer to the method info
776 * JIT compilation of a single method. This method also writes the result
777 * back to method->addr, an thus overwrites the trampoline function.
779 * Returns: a pointer to the newly created code.
782 arch_compile_method (MonoMethod *method)
785 MonoMemPool *mp = mono_mempool_new ();
787 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
788 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
790 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
791 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
792 method->klass->name, method->name);
795 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
796 MonoClassField *field;
797 const char *name = method->name;
798 static guint target_offset = 0;
799 static guint method_offset = 0;
801 gboolean delegate = FALSE;
803 if (method->klass->parent &&
804 method->klass->parent->parent == mono_defaults.delegate_class)
807 if (!target_offset) {
808 mono_class_init (mono_defaults.delegate_class);
810 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
811 target_offset = field->offset;
812 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
813 method_offset = field->offset;
816 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
817 method->addr = code = g_malloc (32);
818 x86_push_reg (code, X86_EBP);
819 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
821 /* load the this pointer */
822 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
823 /* load m_target arg */
824 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
826 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
827 /* load method_ptr arg */
828 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
829 /* store method_ptr */
830 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
835 g_assert ((code - (guint8*)method->addr) < 32);
837 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
838 MonoMethodSignature *csig = method->signature;
839 int i, arg_size, target, this_pos = 4;
842 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
843 g_assert (!csig->ret->byref);
848 if (csig->param_count) {
851 for (i = 0; i < csig->param_count; ++i) {
852 arg_size += mono_type_stack_size (csig->params [i], &align);
853 g_assert (align == 4);
857 method->addr = g_malloc (64 + arg_size);
859 for (i = 0; i < 2; i ++) {
863 /* load the this pointer */
864 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
866 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
867 /* check if zero (static method call without this pointer) */
868 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
869 x86_branch32 (code, X86_CC_EQ, target, TRUE);
872 /* virtual delegate methods: we have to replace the this pointer
873 * withe the actual target */
874 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
875 /* jump to method_ptr() */
876 x86_jump_membase (code, X86_EAX, method_offset);
878 /* static delegate methods: we have to remove the this pointer
879 * from the activation frame - I do this do creating a new
880 * stack frame an copy all arguments except the this pointer */
882 target = code - source;
883 g_assert ((arg_size & 3) == 0);
884 for (j = 0; j < (arg_size>>2); j++) {
885 x86_push_membase (code, X86_ESP, (arg_size + this_pos));
889 x86_push_membase (code, X86_ESP, (arg_size + 4));
891 x86_call_membase (code, X86_EAX, method_offset);
893 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size + 4);
895 x86_alu_reg_imm (code, X86_ADD, X86_ESP, arg_size);
901 g_assert ((code - (guint8*)method->addr) < (64 + arg_size));
904 if (mono_debug_handle)
906 g_error ("Don't know how to exec runtime method %s.%s::%s",
907 method->klass->name_space, method->klass->name, method->name);
911 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
912 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
914 cfg = mono_cfg_new (method, mp);
916 mono_analyze_flow (cfg);
920 mono_analyze_stack (cfg);
924 cfg->rs = mono_regset_new (X86_NREG);
925 mono_regset_reserve_reg (cfg->rs, X86_ESP);
926 mono_regset_reserve_reg (cfg->rs, X86_EBP);
928 cfg->code_size = 256;
929 cfg->start = cfg->code = g_malloc (cfg->code_size);
931 if (match_debug_method (method))
932 x86_breakpoint (cfg->code);
934 if (mono_jit_dump_forest) {
936 printf ("FOREST %s.%s:%s\n", method->klass->name_space,
937 method->klass->name, method->name);
938 for (i = 0; i < cfg->block_count; i++) {
939 printf ("BLOCK %d:\n", i);
940 mono_print_forest (cfg->bblocks [i].forest);
944 mono_label_cfg (cfg);
948 arch_allocate_regs (cfg);
950 /* align to 8 byte boundary */
951 cfg->locals_size += 7;
952 cfg->locals_size &= ~7;
954 arch_emit_prologue (cfg);
956 arch_emit_epilogue (cfg);
958 method->addr = cfg->start;
960 mono_compute_branches (cfg);
962 if (mono_jit_dump_asm) {
963 char *id = g_strdup_printf ("%s.%s_%s", method->klass->name_space,
964 method->klass->name, method->name);
965 mono_disassemble_code (cfg->start, cfg->code - cfg->start, id);
968 if (mono_debug_handle)
969 mono_debug_add_method (mono_debug_handle, cfg);
971 ji->code_size = cfg->code - cfg->start;
972 ji->used_regs = cfg->rs->used_mask;
974 ji->code_start = method->addr;
975 mono_jit_info_table_add (mono_jit_info_table, ji);
977 if (header->num_clauses) {
978 int i, start_block, end_block;
980 ji->num_clauses = header->num_clauses;
981 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
983 for (i = 0; i < header->num_clauses; i++) {
984 MonoExceptionClause *ec = &header->clauses [i];
985 MonoJitExceptionInfo *ei = &ji->clauses [i];
987 ei->flags = ec->flags;
988 ei->token_or_filter = ec->token_or_filter;
990 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
991 start_block = cfg->bcinfo [ec->try_offset].block_id;
992 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
993 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
995 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
996 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
998 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
999 start_block = cfg->bcinfo [ec->handler_offset].block_id;
1000 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
1002 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
1006 mono_regset_free (cfg->rs);
1008 mono_cfg_free (cfg);
1010 mono_mempool_destroy (mp);
1014 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
1015 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
1016 method->klass->name, method->name, method, method->addr);
1020 return method->addr;
1024 * arch_get_restore_context:
1026 * Returns a pointer to a method which restores a previously saved sigcontext.
1029 arch_get_restore_context ()
1031 static guint8 *start = NULL;
1037 /* restore_contect (struct sigcontext *ctx) */
1038 /* we do not restore X86_EAX, X86_EDX */
1040 start = code = malloc (1024);
1043 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
1045 /* get return address, stored in EDX */
1046 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
1049 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
1051 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
1053 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
1055 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
1057 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1058 /* restore ECX. the exception object is passed here to the catch handler */
1059 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
1061 /* jump to the saved IP */
1062 x86_jump_reg (code, X86_EDX);
1068 * arch_get_call_finally:
1070 * Returns a pointer to a method which calls a finally handler.
1073 arch_get_call_finally ()
1075 static guint8 *start = NULL;
1081 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1082 start = code = malloc (1024);
1084 x86_push_reg (code, X86_EBP);
1085 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1086 x86_push_reg (code, X86_EBX);
1087 x86_push_reg (code, X86_EDI);
1088 x86_push_reg (code, X86_ESI);
1091 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1093 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1095 x86_push_reg (code, X86_EBP);
1097 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1098 /* call the handler */
1099 x86_call_reg (code, X86_ECX);
1101 x86_pop_reg (code, X86_EBP);
1102 /* restore saved regs */
1103 x86_pop_reg (code, X86_ESI);
1104 x86_pop_reg (code, X86_EDI);
1105 x86_pop_reg (code, X86_EBX);
1113 * arch_handle_exception:
1114 * @ctx: saved processor state
1118 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1120 MonoDomain *domain = mono_domain_get ();
1122 gpointer ip = (gpointer)ctx->eip;
1123 static void (*restore_context) (struct sigcontext *);
1124 static void (*call_finally) (struct sigcontext *, unsigned long);
1126 g_assert (ctx != NULL);
1127 g_assert (obj != NULL);
1129 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1131 if (!restore_context)
1132 restore_context = arch_get_restore_context ();
1135 call_finally = arch_get_call_finally ();
1137 if (ji) { /* we are inside managed code */
1138 MonoMethod *m = ji->method;
1142 if (ji->num_clauses) {
1145 g_assert (ji->clauses);
1147 for (i = 0; i < ji->num_clauses; i++) {
1148 MonoJitExceptionInfo *ei = &ji->clauses [i];
1150 if (ei->try_start <= ip && ip <= (ei->try_end)) {
1152 if (ei->flags == 0 && mono_object_isinst (obj,
1153 mono_class_get (m->klass->image, ei->token_or_filter))) {
1155 ctx->eip = (unsigned long)ei->handler_start;
1156 ctx->ecx = (unsigned long)obj;
1157 restore_context (ctx);
1158 g_assert_not_reached ();
1163 /* no handler found - we need to call all finally handlers */
1164 for (i = 0; i < ji->num_clauses; i++) {
1165 MonoJitExceptionInfo *ei = &ji->clauses [i];
1167 if (ei->try_start <= ip && ip < (ei->try_end) &&
1168 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1169 call_finally (ctx, (unsigned long)ei->handler_start);
1174 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1175 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1178 if (!strcmp (strace, "TODO: implement stack traces")){
1180 strace = g_strdup ("");
1183 tmp = g_strdup_printf ("%sin %s.%s:%s ()\n", strace, m->klass->name_space,
1184 m->klass->name, m->name);
1188 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1192 /* continue unwinding */
1194 /* restore caller saved registers */
1195 if (ji->used_regs & X86_ESI_MASK) {
1196 ctx->esi = *((int *)ctx->ebp + offset);
1199 if (ji->used_regs & X86_EDI_MASK) {
1200 ctx->edi = *((int *)ctx->ebp + offset);
1203 if (ji->used_regs & X86_EBX_MASK) {
1204 ctx->ebx = *((int *)ctx->ebp + offset);
1207 ctx->esp = ctx->ebp;
1208 ctx->eip = *((int *)ctx->ebp + 1);
1209 ctx->ebp = *((int *)ctx->ebp);
1211 if (next_bp < (unsigned)mono_end_of_stack)
1212 arch_handle_exception (ctx, obj);
1214 mono_jit_abort (obj);
1217 gpointer *lmf_addr = TlsGetValue (lmf_thread_id);
1221 g_assert (lmf_addr);
1222 lmf = *((MonoLMF **)lmf_addr);
1225 mono_jit_abort (obj);
1229 *lmf_addr = lmf->previous_lmf;
1231 ctx->esi = lmf->esi;
1232 ctx->edi = lmf->edi;
1233 ctx->ebx = lmf->ebx;
1234 ctx->ebp = lmf->ebp;
1235 ctx->eip = lmf->eip;
1236 ctx->esp = (unsigned long)lmf;
1238 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1239 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1242 if (!strcmp (strace, "TODO: implement stack traces"))
1243 strace = g_strdup ("");
1245 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
1246 m->klass->name, m->name);
1250 ((MonoException*)obj)->stack_trace = mono_string_new (domain, tmp);
1254 if (ctx->eip < (unsigned)mono_end_of_stack)
1255 arch_handle_exception (ctx, obj);
1257 mono_jit_abort (obj);
1260 g_assert_not_reached ();
1264 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
1265 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
1266 unsigned long eip, unsigned long esp)
1268 struct sigcontext ctx;
1280 arch_handle_exception (&ctx, exc);
1282 g_assert_not_reached ();
1286 arch_get_throw_exception (void)
1288 static guint8 *start = NULL;
1294 code = start = g_malloc (1024);
1296 x86_push_reg (code, X86_ESP);
1297 x86_push_membase (code, X86_ESP, 4); /* IP */
1298 x86_push_membase (code, X86_ESP, 12); /* exception */
1299 x86_push_reg (code, X86_EBP);
1300 x86_push_reg (code, X86_EDI);
1301 x86_push_reg (code, X86_ESI);
1302 x86_push_reg (code, X86_EBX);
1303 x86_push_reg (code, X86_EDX);
1304 x86_push_reg (code, X86_ECX);
1305 x86_push_reg (code, X86_EAX);
1306 x86_call_code (code, throw_exception);
1307 /* we should never reach this breakpoint */
1308 x86_breakpoint (code);