2 * emit-x86.c: Support functions for emitting x86 code
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
8 * (C) 2001 Ximian, Inc.
14 #include <mono/metadata/assembly.h>
15 #include <mono/metadata/loader.h>
16 #include <mono/metadata/cil-coff.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/class.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/arch/x86/x86-codegen.h>
27 enter_method (MonoMethod *method, gpointer ebp)
33 printf ("ENTER: %s.%s::%s (", method->klass->name_space,
34 method->klass->name, method->name);
38 if (method->signature->ret->type == MONO_TYPE_VALUETYPE) {
41 if ((size = mono_type_size (method->signature->ret, &align)) > 4 || size == 3) {
42 printf ("VALUERET:%p, ", *((gpointer *)ebp));
43 ebp += sizeof (gpointer);
47 if (method->signature->hasthis) {
48 if (method->klass->valuetype) {
49 printf ("value:%p, ", *((gpointer *)ebp));
51 o = *((MonoObject **)ebp);
53 if (class == mono_defaults.string_class) {
54 printf ("this:%p[STRING:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
57 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
59 ebp += sizeof (gpointer);
62 for (i = 0; i < method->signature->param_count; ++i) {
63 MonoType *type = method->signature->params [i];
65 size = mono_type_size (type, &align);
68 case MONO_TYPE_BOOLEAN:
78 printf ("%d, ", *((int *)(ebp)));
80 case MONO_TYPE_STRING:
81 printf ("[STRING:%s], ", mono_string_to_utf8 (*(MonoString **)(ebp)));
85 case MONO_TYPE_OBJECT:
88 case MONO_TYPE_SZARRAY:
89 printf ("%p, ", *((gpointer *)(ebp)));
92 printf ("%lld, ", *((gint64 *)(ebp)));
95 printf ("%f, ", *((double *)(ebp)));
97 case MONO_TYPE_VALUETYPE:
99 for (j = 0; j < size; j++)
100 printf ("%02x,", *((guint8*)ebp +j));
108 ebp = (gpointer)((unsigned)ebp & ~(3));
115 leave_method (MonoMethod *method, int edx, int eax, double test)
119 switch (method->signature->ret->type) {
121 printf ("LEAVE: %s.%s::%s\n", method->klass->name_space,
122 method->klass->name, method->name);
124 case MONO_TYPE_BOOLEAN:
134 printf ("LEAVE: %s.%s::%s EAX=%d\n", method->klass->name_space,
135 method->klass->name, method->name, eax);
137 case MONO_TYPE_STRING:
138 printf ("[STRING:%s], ", mono_string_to_utf8 ((MonoString *)(eax)));
141 case MONO_TYPE_CLASS:
142 case MONO_TYPE_OBJECT:
143 case MONO_TYPE_FNPTR:
144 case MONO_TYPE_ARRAY:
145 case MONO_TYPE_SZARRAY:
146 printf ("LEAVE: %s.%s::%s EAX=%p\n", method->klass->name_space,
147 method->klass->name, method->name, (gpointer)eax);
150 *((gint32 *)&l) = eax;
151 *((gint32 *)&l + 1) = edx;
152 printf ("LEAVE: %s.%s::%s EAX/EDX=%lld\n", method->klass->name_space,
153 method->klass->name, method->name, l);
156 printf ("LEAVE: %s.%s::%s FP=%f\n", method->klass->name_space,
157 method->klass->name, method->name, test);
160 printf ("LEAVE: %s.%s::%s (unknown return type)\n", method->klass->name_space,
161 method->klass->name, method->name);
166 * arch_emit_prologue:
167 * @cfg: pointer to status information
169 * Emits the function prolog.
172 arch_emit_prologue (MonoFlowGraph *cfg)
174 x86_push_reg (cfg->code, X86_EBP);
175 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
177 if (cfg->locals_size)
178 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
180 if (mono_regset_reg_used (cfg->rs, X86_EBX))
181 x86_push_reg (cfg->code, X86_EBX);
183 if (mono_regset_reg_used (cfg->rs, X86_EDI))
184 x86_push_reg (cfg->code, X86_EDI);
186 if (mono_regset_reg_used (cfg->rs, X86_ESI))
187 x86_push_reg (cfg->code, X86_ESI);
189 if (mono_jit_trace_calls) {
190 x86_push_reg (cfg->code, X86_EBP);
191 x86_push_imm (cfg->code, cfg->method);
192 x86_call_code (cfg->code, enter_method);
193 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
198 * arch_emit_epilogue:
199 * @cfg: pointer to status information
201 * Emits the function epilog.
204 arch_emit_epilogue (MonoFlowGraph *cfg)
206 if (mono_jit_trace_calls) {
207 x86_fld_reg (cfg->code, 0);
208 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
209 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
210 x86_push_reg (cfg->code, X86_EAX);
211 x86_push_reg (cfg->code, X86_EDX);
212 x86_push_imm (cfg->code, cfg->method);
213 x86_call_code (cfg->code, leave_method);
214 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
215 x86_pop_reg (cfg->code, X86_EDX);
216 x86_pop_reg (cfg->code, X86_EAX);
217 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
220 if (mono_regset_reg_used (cfg->rs, X86_ESI))
221 x86_pop_reg (cfg->code, X86_ESI);
223 if (mono_regset_reg_used (cfg->rs, X86_EDI))
224 x86_pop_reg (cfg->code, X86_EDI);
226 if (mono_regset_reg_used (cfg->rs, X86_EBX))
227 x86_pop_reg (cfg->code, X86_EBX);
229 x86_leave (cfg->code);
234 * x86_magic_trampoline:
235 * @eax: saved x86 register
236 * @ecx: saved x86 register
237 * @edx: saved x86 register
238 * @esi: saved x86 register
239 * @edi: saved x86 register
240 * @ebx: saved x86 register
241 * @code: pointer into caller code
242 * @method: the method to translate
244 * This method is called by the trampoline functions for virtual
245 * methods. It inspects the caller code to find the address of the
246 * vtable slot, then calls the JIT compiler and writes the address
247 * of the compiled method back to the vtable. All virtual methods
248 * are called with: x86_call_membase (inst, basereg, disp). We always
249 * use 32 bit displacement to ensure that the length of the call
250 * instruction is 6 bytes. We need to get the value of the basereg
251 * and the constant displacement.
254 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
255 int ebx, guint8 *code, MonoMethod *m)
261 /* go to the start of the call instruction */
263 g_assert (*code == 0xff);
267 g_assert ((ab >> 6) == 2);
269 /* extract the register number containing the address */
273 /* extract the displacement */
274 disp = *((gint32*)code);
296 g_assert_not_reached ();
301 return *((gpointer *)o) = arch_compile_method (m);
305 * arch_create_jit_trampoline:
306 * @method: pointer to the method info
308 * Creates a trampoline function for virtual methods. If the created
309 * code is called it first starts JIT compilation of method,
310 * and then calls the newly created method. I also replaces the
311 * corresponding vtable entry (see x86_magic_trampoline).
313 * Returns: a pointer to the newly created code
316 arch_create_jit_trampoline (MonoMethod *method)
319 static guint8 *vc = NULL;
325 vc = buf = g_malloc (24);
327 /* push the return address onto the stack */
328 x86_push_membase (buf, X86_ESP, 4);
330 /* save all register values */
331 x86_push_reg (buf, X86_EBX);
332 x86_push_reg (buf, X86_EDI);
333 x86_push_reg (buf, X86_ESI);
334 x86_push_reg (buf, X86_EDX);
335 x86_push_reg (buf, X86_ECX);
336 x86_push_reg (buf, X86_EAX);
338 x86_call_code (buf, x86_magic_trampoline);
339 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
341 /* call the compiled method */
342 x86_jump_reg (buf, X86_EAX);
344 g_assert ((buf - vc) <= 24);
347 code = buf = g_malloc (16);
348 x86_push_imm (buf, method);
349 x86_jump_code (buf, vc);
350 g_assert ((buf - code) <= 16);
356 * arch_create_simple_jit_trampoline:
357 * @method: pointer to the method info
359 * Creates a trampoline function for method. If the created
360 * code is called it first starts JIT compilation of method,
361 * and then calls the newly created method. I also replaces the
362 * address in method->addr with the result of the JIT
363 * compilation step (in arch_compile_method).
365 * Returns: a pointer to the newly created code
368 arch_create_simple_jit_trampoline (MonoMethod *method)
375 /* we never free the allocated code buffer */
376 code = buf = g_malloc (16);
377 x86_push_imm (buf, method);
378 x86_call_code (buf, arch_compile_method);
379 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
380 /* jump to the compiled method */
381 x86_jump_reg (buf, X86_EAX);
382 g_assert ((buf - code) < 16);
388 mono_label_cfg (MonoFlowGraph *cfg)
392 for (i = 0; i < cfg->block_count; i++) {
393 GPtrArray *forest = cfg->bblocks [i].forest;
394 const int top = forest->len;
396 for (j = 0; j < top; j++) {
397 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
400 mbstate = mono_burg_label (t1, cfg);
403 if (mono_debug_handle)
405 g_warning ("tree does not match");
406 mono_print_ctree (t1); printf ("\n\n");
408 mono_print_forest (forest);
409 g_assert_not_reached ();
416 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
419 int ern = mono_burg_rule (tree->state, goal);
420 guint16 *nts = mono_burg_nts [ern];
423 mono_burg_kids (tree, ern, kids);
427 case MB_NTERM_lreg: {
432 tree->exclude_mask |= (1 << X86_ECX);
433 tree->left->exclude_mask |= (1 << X86_ECX);
435 case MB_TERM_CALL_I4:
436 tree->reg1 = X86_EAX;
438 case MB_TERM_CALL_I8:
439 tree->reg1 = X86_EAX;
440 tree->reg2 = X86_EDX;
446 tree->reg1 = X86_EAX;
447 tree->reg2 = X86_EDX;
448 if (goal == MB_NTERM_reg) {
449 tree->left->exclude_mask |= (1 << X86_EDX);
450 tree->right->exclude_mask |= (1 << X86_EDX);
461 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
463 if (nts [0] && kids [0] == tree) {
465 tree_allocate_regs (kids [0], nts [0], rs);
469 for (i = 0; nts [i]; i++)
470 tree_allocate_regs (kids [i], nts [i], rs);
472 for (i = 0; nts [i]; i++) {
473 mono_regset_free_reg (rs, kids [i]->reg1);
474 mono_regset_free_reg (rs, kids [i]->reg2);
480 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1) {
481 g_warning ("register allocation failed %d 0x%08x 0x%08x\n", tree->reg1, rs->free_mask, tree->exclude_mask);
482 g_assert_not_reached ();
488 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1 ||
490 mono_regset_alloc_reg (rs, tree->reg2, tree->exclude_mask)) == -1) {
491 g_warning ("register allocation failed\n");
492 g_assert_not_reached ();
497 /* fixme: allocate floating point registers */
501 if (tree->op == MB_TERM_ADD) {
502 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
503 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
505 //if (tree->op == MB_TERM_CALL_I4) {
506 //tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
511 if (tree->op == MB_TERM_ADD) {
512 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
517 if (tree->op == MB_TERM_SHL ||
518 tree->op == MB_TERM_MUL) {
519 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
527 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
528 tree->emit = mono_burg_func [ern];
532 arch_allocate_regs (MonoFlowGraph *cfg)
536 for (i = 0; i < cfg->block_count; i++) {
537 GPtrArray *forest = cfg->bblocks [i].forest;
538 const int top = forest->len;
540 for (j = 0; j < top; j++) {
541 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
542 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
543 tree_allocate_regs (t1, 1, cfg->rs);
544 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
545 g_assert (cfg->rs->free_mask == 0xffffffff);
551 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
554 int i, ern = mono_burg_rule (tree->state, goal);
555 guint16 *nts = mono_burg_nts [ern];
558 mono_burg_kids (tree, ern, kids);
560 for (i = 0; nts [i]; i++)
561 tree_emit (nts [i], s, kids [i]);
563 tree->addr = s->code - s->start;
565 if ((emit = mono_burg_func [ern]))
570 mono_emit_cfg (MonoFlowGraph *cfg)
574 for (i = 0; i < cfg->block_count; i++) {
575 MonoBBlock *bb = &cfg->bblocks [i];
576 GPtrArray *forest = bb->forest;
577 const int top = forest->len;
579 bb->addr = cfg->code - cfg->start;
581 for (j = 0; j < top; j++) {
582 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
583 tree_emit (1, cfg, t1);
586 if (t1->cli_addr >= 0)
587 printf ("CLIADDR: %IL%05x %p\n", t1->cli_addr, cfg->start + t1->addr);
592 cfg->epilog = cfg->code - cfg->start;
596 mono_compute_branches (MonoFlowGraph *cfg)
603 for (j = 0; j < cfg->block_count; j++) {
604 MonoBBlock *bb = &cfg->bblocks [j];
605 GPtrArray *forest = bb->forest;
606 const int top = forest->len;
608 for (i = 0; i < top; i++) {
609 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
613 if (t1->op == MB_TERM_SWITCH) {
614 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
615 guint32 *rt = (guint32 *)t1->data.p;
617 int m = *((guint32 *)t1->data.p) + 1;
620 for (j = 1; j <= m; j++)
621 rt [j] = (int)(jt [j]->addr + cfg->start);
624 /* emit the jump instruction again to update addresses */
625 cfg->code = cfg->start + t1->addr;
626 ((MBEmitFunc)t1->emit) (t1, cfg);
636 match_debug_method (MonoMethod* method)
638 GList *tmp = mono_debug_methods;
640 for (; tmp; tmp = tmp->next) {
641 if (strcmp (method->name, tmp->data) == 0) {
649 * arch_compile_method:
650 * @method: pointer to the method info
652 * JIT compilation of a single method. This method also writes the result
653 * back to method->addr, an thus overwrites the trampoline function.
655 * Returns: a pointer to the newly created code.
658 arch_compile_method (MonoMethod *method)
661 MonoMemPool *mp = mono_mempool_new ();
663 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
664 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
666 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
667 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
668 method->klass->name, method->name);
671 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
672 MonoClassField *field;
673 const char *name = method->name;
674 static guint target_offset = 0;
675 static guint method_offset = 0;
677 gboolean delegate = FALSE;
679 if (method->klass->parent &&
680 method->klass->parent->parent == mono_defaults.delegate_class)
683 if (!target_offset) {
684 mono_jit_init_class (mono_defaults.delegate_class);
686 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
687 target_offset = field->offset;
688 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
689 method_offset = field->offset;
692 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
693 method->addr = code = g_malloc (32);
694 x86_push_reg (code, X86_EBP);
695 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
697 /* load the this pointer */
698 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
699 /* load m_target arg */
700 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
702 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
703 /* load method_ptr arg */
704 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
705 /* store method_ptr */
706 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
711 g_assert ((code - (guint8*)method->addr) < 32);
713 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
714 MonoMethodSignature *csig = method->signature;
715 int i, target, this_pos = 4;
718 method->addr = g_malloc (32);
720 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
722 if ((size = mono_type_size (csig->ret, &align)) > 4 || size == 3)
726 for (i = 0; i < 2; i ++) {
728 /* load the this pointer */
729 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
731 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
732 /* check if zero (static method call without this pointer) */
733 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
734 x86_branch32 (code, X86_CC_EQ, target, TRUE);
737 /* virtual call - we have to replace the this pointer */
738 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
740 /* jump to method_ptr() */
741 target = code - source;
742 x86_jump_membase (code, X86_EAX, method_offset);
745 g_assert ((code - (guint8*)method->addr) < 32);
748 if (mono_debug_handle)
750 g_error ("Don't know how to exec runtime method %s.%s::%s",
751 method->klass->name_space, method->klass->name, method->name);
755 MonoMethodHeader *header = ((MonoMethodNormal *)method)->header;
756 MonoJitInfo *ji = g_new0 (MonoJitInfo, 1);
758 cfg = mono_cfg_new (method, mp);
760 mono_analyze_flow (cfg);
764 mono_analyze_stack (cfg);
769 cfg->rs = mono_regset_new (X86_NREG);
770 mono_regset_reserve_reg (cfg->rs, X86_ESP);
771 mono_regset_reserve_reg (cfg->rs, X86_EBP);
773 // fixme: remove limitation to 8192 bytes
774 ji->code_size = 8192;
775 method->addr = cfg->start = cfg->code = g_malloc (ji->code_size);
777 if (match_debug_method (method))
778 x86_breakpoint (cfg->code);
780 if (mono_jit_dump_forest) {
782 for (i = 0; i < cfg->block_count; i++) {
783 printf ("BLOCK %d:\n", i);
784 mono_print_forest (cfg->bblocks [i].forest);
788 mono_label_cfg (cfg);
792 arch_allocate_regs (cfg);
794 /* align to 8 byte boundary */
795 cfg->locals_size += 7;
796 cfg->locals_size &= ~7;
798 arch_emit_prologue (cfg);
802 arch_emit_epilogue (cfg);
804 g_assert ((cfg->code - cfg->start) < ji->code_size);
806 mono_compute_branches (cfg);
808 if (mono_jit_dump_asm)
809 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
811 if (mono_debug_handle)
812 mono_debug_add_method (mono_debug_handle, cfg);
814 ji->used_regs = cfg->rs->used_mask;
816 ji->code_start = method->addr;
817 mono_jit_info_table_add (mono_jit_info_table, ji);
819 if (header->num_clauses) {
820 int i, start_block, end_block;
822 ji->num_clauses = header->num_clauses;
823 ji->clauses = g_new0 (MonoJitExceptionInfo, header->num_clauses);
825 for (i = 0; i < header->num_clauses; i++) {
826 MonoExceptionClause *ec = &header->clauses [i];
827 MonoJitExceptionInfo *ei = &ji->clauses [i];
829 ei->flags = ec->flags;
830 ei->token_or_filter = ec->token_or_filter;
832 g_assert (cfg->bcinfo [ec->try_offset].is_block_start);
833 start_block = cfg->bcinfo [ec->try_offset].block_id;
834 end_block = cfg->bcinfo [ec->try_offset + ec->try_len].block_id;
835 g_assert (cfg->bcinfo [ec->try_offset + ec->try_len].is_block_start);
837 ei->try_start = cfg->start + cfg->bblocks [start_block].addr;
838 ei->try_end = cfg->start + cfg->bblocks [end_block].addr;
840 g_assert (cfg->bcinfo [ec->handler_offset].is_block_start);
841 start_block = cfg->bcinfo [ec->handler_offset].block_id;
842 ei->handler_start = cfg->start + cfg->bblocks [start_block].addr;
844 //printf ("TEST %x %x %x\n", ei->try_start, ei->try_end, ei->handler_start);
848 mono_regset_free (cfg->rs);
852 mono_mempool_destroy (mp);
856 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
857 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
858 method->klass->name, method->name, method, method->addr);
864 if (!strcmp (method->klass->name, "String") && !strcmp (method->name, "ToCharArray"))