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>
26 enter_method (MonoMethod *method, gpointer ebp)
32 printf ("ENTER: %s.%s::%s (", method->klass->name_space,
33 method->klass->name, method->name);
37 if (method->signature->ret->type == MONO_TYPE_VALUETYPE) {
40 if ((size = mono_type_size (method->signature->ret, &align)) > 4 || size == 3) {
41 printf ("VALUERET:%p, ", *((gpointer *)ebp));
42 ebp += sizeof (gpointer);
46 if (method->signature->hasthis) {
47 if (method->klass->valuetype) {
48 printf ("value:%p, ", *((gpointer *)ebp));
50 o = *((MonoObject **)ebp);
52 if (!strcmp (class->name_space, "System") &&
53 !strcmp (class->name, "String")) {
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_EDI))
221 x86_pop_reg (cfg->code, X86_EDI);
223 if (mono_regset_reg_used (cfg->rs, X86_ESI))
224 x86_pop_reg (cfg->code, X86_ESI);
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);
402 g_warning ("tree does not match");
403 mono_print_ctree (t1); printf ("\n\n");
405 mono_print_forest (forest);
406 g_assert_not_reached ();
413 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
416 int ern = mono_burg_rule (tree->state, goal);
417 guint16 *nts = mono_burg_nts [ern];
420 mono_burg_kids (tree, ern, kids);
424 case MB_NTERM_lreg: {
429 tree->exclude_mask |= (1 << X86_ECX);
430 tree->left->exclude_mask |= (1 << X86_ECX);
432 case MB_TERM_CALL_I4:
433 tree->reg1 = X86_EAX;
435 case MB_TERM_CALL_I8:
436 tree->reg1 = X86_EAX;
437 tree->reg2 = X86_EDX;
443 tree->reg1 = X86_EAX;
444 tree->reg2 = X86_EDX;
445 if (goal == MB_NTERM_reg) {
446 tree->left->exclude_mask |= (1 << X86_EDX);
447 tree->right->exclude_mask |= (1 << X86_EDX);
458 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
460 if (nts [0] && kids [0] == tree) {
462 tree_allocate_regs (kids [0], nts [0], rs);
466 for (i = 0; nts [i]; i++)
467 tree_allocate_regs (kids [i], nts [i], rs);
469 for (i = 0; nts [i]; i++) {
470 mono_regset_free_reg (rs, kids [i]->reg1);
471 mono_regset_free_reg (rs, kids [i]->reg2);
477 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1) {
478 g_warning ("register allocation failed %d 0x%08x 0x%08x\n", tree->reg1, rs->free_mask, tree->exclude_mask);
479 g_assert_not_reached ();
485 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1 ||
487 mono_regset_alloc_reg (rs, tree->reg2, tree->exclude_mask)) == -1) {
488 g_warning ("register allocation failed\n");
489 g_assert_not_reached ();
494 /* fixme: allocate floating point registers */
498 if (tree->op == MB_TERM_ADD) {
499 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
500 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
502 //if (tree->op == MB_TERM_CALL_I4) {
503 //tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
508 if (tree->op == MB_TERM_ADD) {
509 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
514 if (tree->op == MB_TERM_SHL ||
515 tree->op == MB_TERM_MUL) {
516 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
524 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
525 tree->emit = mono_burg_func [ern];
529 arch_allocate_regs (MonoFlowGraph *cfg)
533 for (i = 0; i < cfg->block_count; i++) {
534 GPtrArray *forest = cfg->bblocks [i].forest;
535 const int top = forest->len;
537 for (j = 0; j < top; j++) {
538 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
539 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
540 tree_allocate_regs (t1, 1, cfg->rs);
541 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
542 g_assert (cfg->rs->free_mask == 0xffffffff);
548 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
551 int i, ern = mono_burg_rule (tree->state, goal);
552 guint16 *nts = mono_burg_nts [ern];
555 mono_burg_kids (tree, ern, kids);
557 for (i = 0; nts [i]; i++)
558 tree_emit (nts [i], s, kids [i]);
560 tree->addr = s->code - s->start;
562 if ((emit = mono_burg_func [ern]))
567 mono_emit_cfg (MonoFlowGraph *cfg)
571 for (i = 0; i < cfg->block_count; i++) {
572 MonoBBlock *bb = &cfg->bblocks [i];
573 GPtrArray *forest = bb->forest;
574 const int top = forest->len;
576 bb->addr = cfg->code - cfg->start;
578 for (j = 0; j < top; j++) {
579 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
580 tree_emit (1, cfg, t1);
584 cfg->epilog = cfg->code - cfg->start;
588 mono_compute_branches (MonoFlowGraph *cfg)
595 for (j = 0; j < cfg->block_count; j++) {
596 MonoBBlock *bb = &cfg->bblocks [j];
597 GPtrArray *forest = bb->forest;
598 const int top = forest->len;
600 for (i = 0; i < top; i++) {
601 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
605 if (t1->op == MB_TERM_SWITCH) {
606 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
607 guint32 *rt = (guint32 *)t1->data.p;
609 int m = *((guint32 *)t1->data.p) + 1;
612 for (j = 1; j <= m; j++)
613 rt [j] = (int)(jt [j]->addr + cfg->start);
616 /* emit the jump instruction again to update addresses */
617 cfg->code = cfg->start + t1->addr;
618 ((MBEmitFunc)t1->emit) (t1, cfg);
628 * arch_compile_method:
629 * @method: pointer to the method info
631 * JIT compilation of a single method. This method also writes the result
632 * back to method->addr, an thus overwrites the trampoline function.
634 * Returns: a pointer to the newly created code.
637 arch_compile_method (MonoMethod *method)
640 MonoMemPool *mp = mono_mempool_new ();
642 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
643 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
645 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
646 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
647 method->klass->name, method->name);
650 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
651 MonoClassField *field;
652 const char *name = method->name;
653 static guint target_offset = 0;
654 static guint method_offset = 0;
656 gboolean delegate = FALSE;
658 if (method->klass->parent &&
659 method->klass->parent->parent == mono_defaults.delegate_class)
662 if (!target_offset) {
663 mono_jit_init_class (mono_defaults.delegate_class);
665 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
666 target_offset = field->offset;
667 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
668 method_offset = field->offset;
671 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
672 method->addr = code = g_malloc (32);
673 x86_push_reg (code, X86_EBP);
674 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
676 /* load the this pointer */
677 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
678 /* load m_target arg */
679 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
681 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
682 /* load method_ptr arg */
683 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
684 /* store method_ptr */
685 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
690 g_assert ((code - (guint8*)method->addr) < 32);
691 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
692 MonoMethodSignature *csig = method->signature;
693 int i, target, this_pos = 4;
696 method->addr = g_malloc (1024);
698 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
700 if ((size = mono_type_size (csig->ret, &align)) > 4 || size == 3)
704 for (i = 0; i < 2; i ++) {
706 /* load the this pointer */
707 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
709 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
710 /* check if zero (static method call without this pointer) */
711 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
712 x86_branch32 (code, X86_CC_EQ, target, TRUE);
715 /* virtual call - we have to replace the this pointer */
716 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
718 /* jump to method_ptr() */
719 target = code - source;
720 x86_jump_membase (code, X86_EAX, method_offset);
724 g_error ("Don't know how to exec runtime method %s.%s::%s",
725 method->klass->name_space, method->klass->name, method->name);
730 cfg = mono_cfg_new (method, mp);
732 mono_analyze_flow (cfg);
734 mono_analyze_stack (cfg);
737 cfg->rs = mono_regset_new (X86_NREG);
738 mono_regset_reserve_reg (cfg->rs, X86_ESP);
739 mono_regset_reserve_reg (cfg->rs, X86_EBP);
741 // fixme: remove limitation to 8192 bytes
742 method->addr = cfg->start = cfg->code = g_malloc (8192);
744 if (mono_jit_dump_forest) {
746 for (i = 0; i < cfg->block_count; i++) {
747 printf ("BLOCK %d:\n", i);
748 mono_print_forest (cfg->bblocks [i].forest);
752 mono_label_cfg (cfg);
754 arch_allocate_regs (cfg);
756 /* align to 8 byte boundary */
757 cfg->locals_size += 7;
758 cfg->locals_size &= ~7;
760 arch_emit_prologue (cfg);
764 g_assert ((cfg->code - cfg->start) < 8100);
766 arch_emit_epilogue (cfg);
768 mono_compute_branches (cfg);
770 if (mono_jit_dump_asm)
771 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
773 mono_regset_free (cfg->rs);
777 mono_mempool_destroy (mp);
781 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
782 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
783 method->klass->name, method->name, method, method->addr);