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);
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)->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);
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)->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->klass == mono_defaults.boolean_class) {
197 printf ("[BOOLEAN:%p:%d]", o, *((guint8 *)o + sizeof (MonoObject)));
198 } else if (o->klass == mono_defaults.int32_class) {
199 printf ("[INT32:%p:%d]", o, *((gint32 *)((gpointer)o + sizeof (MonoObject))));
201 printf ("[%s.%s:%p]", o->klass->name_space, o->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, guint8 *code, MonoMethod *m)
354 /* go to the start of the call instruction */
356 g_assert (*code == 0xff);
360 g_assert ((ab >> 6) == 2);
362 /* extract the register number containing the address */
366 /* extract the displacement */
367 disp = *((gint32*)code);
389 g_assert_not_reached ();
394 if (m->klass->valuetype) {
395 return *((gpointer *)o) = get_unbox_trampoline (m);
397 return *((gpointer *)o) = arch_compile_method (m);
401 * arch_create_jit_trampoline:
402 * @method: pointer to the method info
404 * Creates a trampoline function for virtual methods. If the created
405 * code is called it first starts JIT compilation of method,
406 * and then calls the newly created method. I also replaces the
407 * corresponding vtable entry (see x86_magic_trampoline).
409 * Returns: a pointer to the newly created code
412 arch_create_jit_trampoline (MonoMethod *method)
415 static guint8 *vc = NULL;
421 vc = buf = g_malloc (24);
423 /* push the return address onto the stack */
424 x86_push_membase (buf, X86_ESP, 4);
426 /* save all register values */
427 x86_push_reg (buf, X86_EBX);
428 x86_push_reg (buf, X86_EDI);
429 x86_push_reg (buf, X86_ESI);
430 x86_push_reg (buf, X86_EDX);
431 x86_push_reg (buf, X86_ECX);
432 x86_push_reg (buf, X86_EAX);
434 x86_call_code (buf, x86_magic_trampoline);
435 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
437 /* call the compiled method */
438 x86_jump_reg (buf, X86_EAX);
440 g_assert ((buf - vc) <= 24);
443 code = buf = g_malloc (16);
444 x86_push_imm (buf, method);
445 x86_jump_code (buf, vc);
446 g_assert ((buf - code) <= 16);
452 * arch_create_simple_jit_trampoline:
453 * @method: pointer to the method info
455 * Creates a trampoline function for method. If the created
456 * code is called it first starts JIT compilation of method,
457 * and then calls the newly created method. I also replaces the
458 * address in method->addr with the result of the JIT
459 * compilation step (in arch_compile_method).
461 * Returns: a pointer to the newly created code
464 arch_create_simple_jit_trampoline (MonoMethod *method)
471 /* we never free the allocated code buffer */
472 code = buf = g_malloc (16);
473 x86_push_imm (buf, method);
474 x86_call_code (buf, arch_compile_method);
475 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
476 /* jump to the compiled method */
477 x86_jump_reg (buf, X86_EAX);
478 g_assert ((buf - code) < 16);
484 mono_label_cfg (MonoFlowGraph *cfg)
488 for (i = 0; i < cfg->block_count; i++) {
489 GPtrArray *forest = cfg->bblocks [i].forest;
490 const int top = forest->len;
492 for (j = 0; j < top; j++) {
493 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
496 mbstate = mono_burg_label (t1, cfg);
499 if (mono_debug_handle)
501 g_warning ("tree does not match");
502 mono_print_ctree (t1); printf ("\n\n");
504 mono_print_forest (forest);
505 g_assert_not_reached ();
512 tree_preallocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
515 case MB_TERM_CALL_I4:
516 case MB_TERM_CALL_I8:
517 case MB_TERM_CALL_R8:
518 // case MB_TERM_CALL_VOID :
519 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
520 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
521 tree->reg3 = mono_regset_alloc_reg (rs, X86_ECX, tree->exclude_mask);
528 case MB_NTERM_lreg: {
533 tree->exclude_mask |= (1 << X86_ECX);
534 tree->left->exclude_mask |= (1 << X86_ECX);
537 case MB_TERM_MUL_OVF:
538 case MB_TERM_MUL_OVF_UN:
543 tree->reg1 = mono_regset_alloc_reg (rs, X86_EAX, tree->exclude_mask);
544 tree->reg2 = mono_regset_alloc_reg (rs, X86_EDX, tree->exclude_mask);
545 if (goal == MB_NTERM_reg) {
546 tree->left->exclude_mask |= (1 << X86_EDX);
547 tree->right->exclude_mask |= (1 << X86_EDX) | (1 << X86_EAX);
561 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
564 int ern = mono_burg_rule (tree->state, goal);
565 guint16 *nts = mono_burg_nts [ern];
568 mono_burg_kids (tree, ern, kids);
570 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
572 if (nts [0] && kids [0] == tree) {
574 tree_allocate_regs (kids [0], nts [0], rs);
578 for (i = 0; nts [i]; i++)
579 tree_preallocate_regs (kids [i], nts [i], rs);
581 for (i = 0; nts [i]; i++)
582 tree_allocate_regs (kids [i], nts [i], rs);
584 for (i = 0; nts [i]; i++) {
585 mono_regset_free_reg (rs, kids [i]->reg1);
586 mono_regset_free_reg (rs, kids [i]->reg2);
587 mono_regset_free_reg (rs, kids [i]->reg3);
592 if (tree->reg1 < 0) {
593 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
594 g_assert (tree->reg1 != -1);
599 if (tree->reg1 < 0) {
600 tree->reg1 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
601 g_assert (tree->reg1 != -1);
603 if (tree->reg2 < 0) {
604 tree->reg2 = mono_regset_alloc_reg (rs, -1, tree->exclude_mask);
605 g_assert (tree->reg2 != -1);
610 /* fixme: allocate floating point registers */
614 if (tree->op == MB_TERM_ADD) {
615 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
616 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
621 if (tree->op == MB_TERM_ADD) {
622 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
627 if (tree->op == MB_TERM_SHL ||
628 tree->op == MB_TERM_MUL) {
629 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
637 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
638 tree->emit = mono_burg_func [ern];
642 arch_allocate_regs (MonoFlowGraph *cfg)
646 for (i = 0; i < cfg->block_count; i++) {
647 GPtrArray *forest = cfg->bblocks [i].forest;
648 const int top = forest->len;
650 for (j = 0; j < top; j++) {
651 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
652 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
653 tree_allocate_regs (t1, 1, cfg->rs);
654 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
655 g_assert (cfg->rs->free_mask == 0xffffffff);
661 tree_emit (int goal, MonoFlowGraph *cfg, MBTree *tree)
664 int i, ern = mono_burg_rule (tree->state, goal);
665 guint16 *nts = mono_burg_nts [ern];
669 mono_burg_kids (tree, ern, kids);
671 for (i = 0; nts [i]; i++)
672 tree_emit (nts [i], cfg, kids [i]);
674 tree->addr = offset = cfg->code - cfg->start;
676 // we assume an instruction uses a maximum of 128 bytes
677 if ((cfg->code_size - offset) <= 128) {
678 int add = MIN ((cfg->code_size * 2), 1024);
680 cfg->code_size += add;
681 cfg->start = g_realloc (cfg->start, cfg->code_size);
682 g_assert (cfg->start);
683 cfg->code = cfg->start + offset;
686 if ((emit = mono_burg_func [ern]))
689 g_assert ((cfg->code - cfg->start) < cfg->code_size);
693 mono_emit_cfg (MonoFlowGraph *cfg)
697 for (i = 0; i < cfg->block_count; i++) {
698 MonoBBlock *bb = &cfg->bblocks [i];
699 GPtrArray *forest = bb->forest;
700 const int top = forest->len;
702 bb->addr = cfg->code - cfg->start;
704 for (j = 0; j < top; j++) {
705 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
707 tree_emit (1, cfg, t1);
711 cfg->epilog = cfg->code - cfg->start;
715 mono_compute_branches (MonoFlowGraph *cfg)
722 for (j = 0; j < cfg->block_count; j++) {
723 MonoBBlock *bb = &cfg->bblocks [j];
724 GPtrArray *forest = bb->forest;
725 const int top = forest->len;
727 for (i = 0; i < top; i++) {
728 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
732 if (t1->op == MB_TERM_SWITCH) {
733 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
734 guint32 *rt = (guint32 *)t1->data.p;
736 int m = *((guint32 *)t1->data.p) + 1;
739 for (j = 1; j <= m; j++)
740 rt [j] = (int)(jt [j]->addr + cfg->start);
743 /* emit the jump instruction again to update addresses */
744 cfg->code = cfg->start + t1->addr;
745 ((MBEmitFunc)t1->emit) (t1, cfg);
755 match_debug_method (MonoMethod* method)
757 GList *tmp = mono_debug_methods;
759 for (; tmp; tmp = tmp->next) {
760 if (strcmp (method->name, tmp->data) == 0) {
768 * arch_compile_method:
769 * @method: pointer to the method info
771 * JIT compilation of a single method. This method also writes the result
772 * back to method->addr, an thus overwrites the trampoline function.
774 * Returns: a pointer to the newly created code.
777 arch_compile_method (MonoMethod *method)
780 MonoMemPool *mp = mono_mempool_new ();
782 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
783 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
785 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
786 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
787 method->klass->name, method->name);
790 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
791 MonoClassField *field;
792 const char *name = method->name;
793 static guint target_offset = 0;
794 static guint method_offset = 0;
796 gboolean delegate = FALSE;
798 if (method->klass->parent &&
799 method->klass->parent->parent == mono_defaults.delegate_class)
802 if (!target_offset) {
803 mono_class_init (mono_defaults.delegate_class);
805 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
806 target_offset = field->offset;
807 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
808 method_offset = field->offset;
811 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
812 method->addr = code = g_malloc (32);
813 x86_push_reg (code, X86_EBP);
814 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
816 /* load the this pointer */
817 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
818 /* load m_target arg */
819 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
821 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
822 /* load method_ptr arg */
823 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
824 /* store method_ptr */
825 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
830 g_assert ((code - (guint8*)method->addr) < 32);
832 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
833 MonoMethodSignature *csig = method->signature;
834 int i, target, this_pos = 4;
837 method->addr = g_malloc (64);
839 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
840 g_assert (!csig->ret->byref);
844 for (i = 0; i < 2; i ++) {
846 /* load the this pointer */
847 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
849 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
850 /* check if zero (static method call without this pointer) */
851 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
852 x86_branch32 (code, X86_CC_EQ, target, TRUE);
855 /* virtual delegate methods: we have to replace the this pointer
856 * withe the actual target */
857 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
858 /* jump to method_ptr() */
859 x86_jump_membase (code, X86_EAX, method_offset);
861 /* static delegate methods: we have to remove the this pointer
862 * from the activation frame */
863 target = code - source;
865 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 4, 4);
866 x86_mov_membase_reg (code, X86_ESP, 8, X86_EDX, 4);
868 x86_mov_reg_membase (code, X86_EDX, X86_ESP, 0, 4);
869 x86_mov_membase_reg (code, X86_ESP, 4, X86_EDX, 4);
870 x86_alu_reg_imm (code, X86_ADD, X86_ESP, 4);
872 /* jump to method_ptr() */
873 x86_jump_membase (code, X86_EAX, method_offset);
876 g_assert ((code - (guint8*)method->addr) < 64);
879 if (mono_debug_handle)
881 g_error ("Don't know how to exec runtime method %s.%s::%s",
882 method->klass->name_space, method->klass->name, method->name);
886 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
887 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
889 cfg = mono_cfg_new (method, mp);
891 mono_analyze_flow (cfg);
895 mono_analyze_stack (cfg);
899 cfg->rs = mono_regset_new (X86_NREG);
900 mono_regset_reserve_reg (cfg->rs, X86_ESP);
901 mono_regset_reserve_reg (cfg->rs, X86_EBP);
903 cfg->code_size = 256;
904 cfg->start = cfg->code = g_malloc (cfg->code_size);
906 if (match_debug_method (method))
907 x86_breakpoint (cfg->code);
909 if (mono_jit_dump_forest) {
911 printf ("FOREST %s.%s:%s\n", method->klass->name_space,
912 method->klass->name, method->name);
913 for (i = 0; i < cfg->block_count; i++) {
914 printf ("BLOCK %d:\n", i);
915 mono_print_forest (cfg->bblocks [i].forest);
919 mono_label_cfg (cfg);
923 arch_allocate_regs (cfg);
925 /* align to 8 byte boundary */
926 cfg->locals_size += 7;
927 cfg->locals_size &= ~7;
929 arch_emit_prologue (cfg);
931 arch_emit_epilogue (cfg);
933 method->addr = cfg->start;
935 mono_compute_branches (cfg);
937 if (mono_jit_dump_asm) {
938 char *id = g_strdup_printf ("%s.%s_%s", method->klass->name_space,
939 method->klass->name, method->name);
940 mono_disassemble_code (cfg->start, cfg->code - cfg->start, id);
943 if (mono_debug_handle)
944 mono_debug_add_method (mono_debug_handle, cfg);
946 ji->code_size = cfg->code - cfg->start;
947 ji->used_regs = cfg->rs->used_mask;
949 ji->code_start = method->addr;
950 mono_jit_info_table_add (mono_jit_info_table, ji);
952 if (header->num_clauses) {
953 int i, start_block, end_block;
955 ji->num_clauses = header->num_clauses;
956 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
958 for (i = 0; i < header->num_clauses; i++) {
959 MonoExceptionClause *ec = &header->clauses [i];
960 MonoJitExceptionInfo *ei = &ji->clauses [i];
962 ei->flags = ec->flags;
963 ei->token_or_filter = ec->token_or_filter;
965 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
966 start_block = cfg->bcinfo [ec->try_offset].block_id;
967 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
968 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
970 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
971 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
973 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
974 start_block = cfg->bcinfo [ec->handler_offset].block_id;
975 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
977 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
981 mono_regset_free (cfg->rs);
985 mono_mempool_destroy (mp);
989 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
990 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
991 method->klass->name, method->name, method, method->addr);
999 * arch_get_restore_context:
1001 * Returns a pointer to a method which restores a previously saved sigcontext.
1004 arch_get_restore_context ()
1006 static guint8 *start = NULL;
1012 /* restore_contect (struct sigcontext *ctx) */
1013 /* we do not restore X86_EAX, X86_EDX */
1015 start = code = malloc (1024);
1018 x86_mov_reg_membase (code, X86_EAX, X86_ESP, 4, 4);
1020 /* get return address, stored in EDX */
1021 x86_mov_reg_membase (code, X86_EDX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, eip), 4);
1024 x86_mov_reg_membase (code, X86_EBX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebx), 4);
1026 x86_mov_reg_membase (code, X86_EDI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, edi), 4);
1028 x86_mov_reg_membase (code, X86_ESI, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esi), 4);
1030 x86_mov_reg_membase (code, X86_ESP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
1032 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1033 /* restore ECX. the exception object is passed here to the catch handler */
1034 x86_mov_reg_membase (code, X86_ECX, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ecx), 4);
1036 /* jump to the saved IP */
1037 x86_jump_reg (code, X86_EDX);
1043 * arch_get_call_finally:
1045 * Returns a pointer to a method which calls a finally handler.
1048 arch_get_call_finally ()
1050 static guint8 *start = NULL;
1056 /* call_finally (struct sigcontext *ctx, unsigned long eip) */
1057 start = code = malloc (1024);
1059 x86_push_reg (code, X86_EBP);
1060 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
1061 x86_push_reg (code, X86_EBX);
1062 x86_push_reg (code, X86_EDI);
1063 x86_push_reg (code, X86_ESI);
1066 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
1068 x86_mov_reg_membase (code, X86_ECX, X86_EBP, 12, 4);
1070 x86_push_reg (code, X86_EBP);
1072 x86_mov_reg_membase (code, X86_EBP, X86_EAX, G_STRUCT_OFFSET (struct sigcontext, ebp), 4);
1073 /* call the handler */
1074 x86_call_reg (code, X86_ECX);
1076 x86_pop_reg (code, X86_EBP);
1077 /* restore saved regs */
1078 x86_pop_reg (code, X86_ESI);
1079 x86_pop_reg (code, X86_EDI);
1080 x86_pop_reg (code, X86_EBX);
1088 * arch_handle_exception:
1089 * @ctx: saved processor state
1093 arch_handle_exception (struct sigcontext *ctx, gpointer obj)
1096 gpointer ip = (gpointer)ctx->eip;
1097 static void (*restore_context) (struct sigcontext *);
1098 static void (*call_finally) (struct sigcontext *, unsigned long);
1100 g_assert (ctx != NULL);
1101 g_assert (obj != NULL);
1103 ji = mono_jit_info_table_find (mono_jit_info_table, ip);
1105 if (!restore_context)
1106 restore_context = arch_get_restore_context ();
1109 call_finally = arch_get_call_finally ();
1111 if (ji) { /* we are inside managed code */
1112 MonoMethod *m = ji->method;
1116 if (ji->num_clauses) {
1119 g_assert (ji->clauses);
1121 for (i = 0; i < ji->num_clauses; i++) {
1122 MonoJitExceptionInfo *ei = &ji->clauses [i];
1124 if (ei->try_start <= ip && ip <= (ei->try_end)) {
1126 if (ei->flags == 0 && mono_object_isinst (obj,
1127 mono_class_get (m->klass->image, ei->token_or_filter))) {
1129 ctx->eip = (unsigned long)ei->handler_start;
1130 ctx->ecx = (unsigned long)obj;
1131 restore_context (ctx);
1132 g_assert_not_reached ();
1137 /* no handler found - we need to call all finally handlers */
1138 for (i = 0; i < ji->num_clauses; i++) {
1139 MonoJitExceptionInfo *ei = &ji->clauses [i];
1141 if (ei->try_start <= ip && ip < (ei->try_end) &&
1142 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
1143 call_finally (ctx, (unsigned long)ei->handler_start);
1148 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1149 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1152 if (!strcmp (strace, "TODO: implement stack traces"))
1153 strace = g_strdup ("");
1155 tmp = g_strdup_printf ("%sin %s.%s:%s ()\n", strace, m->klass->name_space,
1156 m->klass->name, m->name);
1160 ((MonoException*)obj)->stack_trace = mono_string_new (tmp);
1164 /* continue unwinding */
1166 /* restore caller saved registers */
1167 if (ji->used_regs & X86_ESI_MASK) {
1168 ctx->esi = *((int *)ctx->ebp + offset);
1171 if (ji->used_regs & X86_EDI_MASK) {
1172 ctx->edi = *((int *)ctx->ebp + offset);
1175 if (ji->used_regs & X86_EBX_MASK) {
1176 ctx->ebx = *((int *)ctx->ebp + offset);
1179 ctx->esp = ctx->ebp;
1180 ctx->eip = *((int *)ctx->ebp + 1);
1181 ctx->ebp = *((int *)ctx->ebp);
1183 if (next_bp < (unsigned)mono_end_of_stack)
1184 arch_handle_exception (ctx, obj);
1186 mono_jit_abort (obj);
1189 gpointer *lmf_addr = TlsGetValue (lmf_thread_id);
1193 g_assert (lmf_addr);
1194 lmf = *((MonoLMF **)lmf_addr);
1197 mono_jit_abort (obj);
1201 *lmf_addr = lmf->previous_lmf;
1203 ctx->esi = lmf->esi;
1204 ctx->edi = lmf->edi;
1205 ctx->ebx = lmf->ebx;
1206 ctx->ebp = lmf->ebp;
1207 ctx->eip = lmf->eip;
1208 ctx->esp = (unsigned long)lmf;
1210 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
1211 char *strace = mono_string_to_utf8 (((MonoException*)obj)->stack_trace);
1214 if (!strcmp (strace, "TODO: implement stack traces"))
1215 strace = g_strdup ("");
1217 tmp = g_strdup_printf ("%sin (unmanaged) %s.%s:%s ()\n", strace, m->klass->name_space,
1218 m->klass->name, m->name);
1222 ((MonoException*)obj)->stack_trace = mono_string_new (tmp);
1226 if (ctx->eip < (unsigned)mono_end_of_stack)
1227 arch_handle_exception (ctx, obj);
1229 mono_jit_abort (obj);
1232 g_assert_not_reached ();
1236 throw_exception (unsigned long eax, unsigned long ecx, unsigned long edx, unsigned long ebx,
1237 unsigned long esi, unsigned long edi, unsigned long ebp, MonoObject *exc,
1238 unsigned long eip, unsigned long esp)
1240 struct sigcontext ctx;
1252 arch_handle_exception (&ctx, exc);
1254 g_assert_not_reached ();
1258 arch_get_throw_exception (void)
1260 static guint8 *start = NULL;
1266 code = start = g_malloc (1024);
1268 x86_push_reg (code, X86_ESP);
1269 x86_push_membase (code, X86_ESP, 4); /* IP */
1270 x86_push_membase (code, X86_ESP, 12); /* exception */
1271 x86_push_reg (code, X86_EBP);
1272 x86_push_reg (code, X86_EDI);
1273 x86_push_reg (code, X86_ESI);
1274 x86_push_reg (code, X86_EBX);
1275 x86_push_reg (code, X86_EDX);
1276 x86_push_reg (code, X86_ECX);
1277 x86_push_reg (code, X86_EAX);
1278 x86_call_code (code, throw_exception);
1279 /* we should never reach this breakpoint */
1280 x86_breakpoint (code);