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 (class == mono_defaults.string_class) {
53 printf ("this:%p[STRING:%s], ", o, mono_string_to_utf8 ((MonoString *)o));
56 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
58 ebp += sizeof (gpointer);
61 for (i = 0; i < method->signature->param_count; ++i) {
62 MonoType *type = method->signature->params [i];
64 size = mono_type_size (type, &align);
67 case MONO_TYPE_BOOLEAN:
77 printf ("%d, ", *((int *)(ebp)));
79 case MONO_TYPE_STRING:
80 printf ("[STRING:%s], ", mono_string_to_utf8 (*(MonoString **)(ebp)));
84 case MONO_TYPE_OBJECT:
87 case MONO_TYPE_SZARRAY:
88 printf ("%p, ", *((gpointer *)(ebp)));
91 printf ("%lld, ", *((gint64 *)(ebp)));
94 printf ("%f, ", *((double *)(ebp)));
96 case MONO_TYPE_VALUETYPE:
98 for (j = 0; j < size; j++)
99 printf ("%02x,", *((guint8*)ebp +j));
107 ebp = (gpointer)((unsigned)ebp & ~(3));
114 leave_method (MonoMethod *method, int edx, int eax, double test)
118 switch (method->signature->ret->type) {
120 printf ("LEAVE: %s.%s::%s\n", method->klass->name_space,
121 method->klass->name, method->name);
123 case MONO_TYPE_BOOLEAN:
133 printf ("LEAVE: %s.%s::%s EAX=%d\n", method->klass->name_space,
134 method->klass->name, method->name, eax);
136 case MONO_TYPE_STRING:
137 printf ("[STRING:%s], ", mono_string_to_utf8 ((MonoString *)(eax)));
140 case MONO_TYPE_CLASS:
141 case MONO_TYPE_OBJECT:
142 case MONO_TYPE_FNPTR:
143 case MONO_TYPE_ARRAY:
144 case MONO_TYPE_SZARRAY:
145 printf ("LEAVE: %s.%s::%s EAX=%p\n", method->klass->name_space,
146 method->klass->name, method->name, (gpointer)eax);
149 *((gint32 *)&l) = eax;
150 *((gint32 *)&l + 1) = edx;
151 printf ("LEAVE: %s.%s::%s EAX/EDX=%lld\n", method->klass->name_space,
152 method->klass->name, method->name, l);
155 printf ("LEAVE: %s.%s::%s FP=%f\n", method->klass->name_space,
156 method->klass->name, method->name, test);
159 printf ("LEAVE: %s.%s::%s (unknown return type)\n", method->klass->name_space,
160 method->klass->name, method->name);
165 * arch_emit_prologue:
166 * @cfg: pointer to status information
168 * Emits the function prolog.
171 arch_emit_prologue (MonoFlowGraph *cfg)
173 x86_push_reg (cfg->code, X86_EBP);
174 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
176 if (cfg->locals_size)
177 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
179 if (mono_regset_reg_used (cfg->rs, X86_EBX))
180 x86_push_reg (cfg->code, X86_EBX);
182 if (mono_regset_reg_used (cfg->rs, X86_EDI))
183 x86_push_reg (cfg->code, X86_EDI);
185 if (mono_regset_reg_used (cfg->rs, X86_ESI))
186 x86_push_reg (cfg->code, X86_ESI);
188 if (mono_jit_trace_calls) {
189 x86_push_reg (cfg->code, X86_EBP);
190 x86_push_imm (cfg->code, cfg->method);
191 x86_call_code (cfg->code, enter_method);
192 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
197 * arch_emit_epilogue:
198 * @cfg: pointer to status information
200 * Emits the function epilog.
203 arch_emit_epilogue (MonoFlowGraph *cfg)
205 if (mono_jit_trace_calls) {
206 x86_fld_reg (cfg->code, 0);
207 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
208 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
209 x86_push_reg (cfg->code, X86_EAX);
210 x86_push_reg (cfg->code, X86_EDX);
211 x86_push_imm (cfg->code, cfg->method);
212 x86_call_code (cfg->code, leave_method);
213 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
214 x86_pop_reg (cfg->code, X86_EDX);
215 x86_pop_reg (cfg->code, X86_EAX);
216 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
219 if (mono_regset_reg_used (cfg->rs, X86_EDI))
220 x86_pop_reg (cfg->code, X86_EDI);
222 if (mono_regset_reg_used (cfg->rs, X86_ESI))
223 x86_pop_reg (cfg->code, X86_ESI);
225 if (mono_regset_reg_used (cfg->rs, X86_EBX))
226 x86_pop_reg (cfg->code, X86_EBX);
228 x86_leave (cfg->code);
233 * x86_magic_trampoline:
234 * @eax: saved x86 register
235 * @ecx: saved x86 register
236 * @edx: saved x86 register
237 * @esi: saved x86 register
238 * @edi: saved x86 register
239 * @ebx: saved x86 register
240 * @code: pointer into caller code
241 * @method: the method to translate
243 * This method is called by the trampoline functions for virtual
244 * methods. It inspects the caller code to find the address of the
245 * vtable slot, then calls the JIT compiler and writes the address
246 * of the compiled method back to the vtable. All virtual methods
247 * are called with: x86_call_membase (inst, basereg, disp). We always
248 * use 32 bit displacement to ensure that the length of the call
249 * instruction is 6 bytes. We need to get the value of the basereg
250 * and the constant displacement.
253 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
254 int ebx, guint8 *code, MonoMethod *m)
260 /* go to the start of the call instruction */
262 g_assert (*code == 0xff);
266 g_assert ((ab >> 6) == 2);
268 /* extract the register number containing the address */
272 /* extract the displacement */
273 disp = *((gint32*)code);
295 g_assert_not_reached ();
300 return *((gpointer *)o) = arch_compile_method (m);
304 * arch_create_jit_trampoline:
305 * @method: pointer to the method info
307 * Creates a trampoline function for virtual methods. If the created
308 * code is called it first starts JIT compilation of method,
309 * and then calls the newly created method. I also replaces the
310 * corresponding vtable entry (see x86_magic_trampoline).
312 * Returns: a pointer to the newly created code
315 arch_create_jit_trampoline (MonoMethod *method)
318 static guint8 *vc = NULL;
324 vc = buf = g_malloc (24);
326 /* push the return address onto the stack */
327 x86_push_membase (buf, X86_ESP, 4);
329 /* save all register values */
330 x86_push_reg (buf, X86_EBX);
331 x86_push_reg (buf, X86_EDI);
332 x86_push_reg (buf, X86_ESI);
333 x86_push_reg (buf, X86_EDX);
334 x86_push_reg (buf, X86_ECX);
335 x86_push_reg (buf, X86_EAX);
337 x86_call_code (buf, x86_magic_trampoline);
338 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
340 /* call the compiled method */
341 x86_jump_reg (buf, X86_EAX);
343 g_assert ((buf - vc) <= 24);
346 code = buf = g_malloc (16);
347 x86_push_imm (buf, method);
348 x86_jump_code (buf, vc);
349 g_assert ((buf - code) <= 16);
355 * arch_create_simple_jit_trampoline:
356 * @method: pointer to the method info
358 * Creates a trampoline function for method. If the created
359 * code is called it first starts JIT compilation of method,
360 * and then calls the newly created method. I also replaces the
361 * address in method->addr with the result of the JIT
362 * compilation step (in arch_compile_method).
364 * Returns: a pointer to the newly created code
367 arch_create_simple_jit_trampoline (MonoMethod *method)
374 /* we never free the allocated code buffer */
375 code = buf = g_malloc (16);
376 x86_push_imm (buf, method);
377 x86_call_code (buf, arch_compile_method);
378 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
379 /* jump to the compiled method */
380 x86_jump_reg (buf, X86_EAX);
381 g_assert ((buf - code) < 16);
387 mono_label_cfg (MonoFlowGraph *cfg)
391 for (i = 0; i < cfg->block_count; i++) {
392 GPtrArray *forest = cfg->bblocks [i].forest;
393 const int top = forest->len;
395 for (j = 0; j < top; j++) {
396 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
399 mbstate = mono_burg_label (t1, cfg);
401 g_warning ("tree does not match");
402 mono_print_ctree (t1); printf ("\n\n");
404 mono_print_forest (forest);
405 g_assert_not_reached ();
412 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
415 int ern = mono_burg_rule (tree->state, goal);
416 guint16 *nts = mono_burg_nts [ern];
419 mono_burg_kids (tree, ern, kids);
423 case MB_NTERM_lreg: {
428 tree->exclude_mask |= (1 << X86_ECX);
429 tree->left->exclude_mask |= (1 << X86_ECX);
431 case MB_TERM_CALL_I4:
432 tree->reg1 = X86_EAX;
434 case MB_TERM_CALL_I8:
435 tree->reg1 = X86_EAX;
436 tree->reg2 = X86_EDX;
442 tree->reg1 = X86_EAX;
443 tree->reg2 = X86_EDX;
444 if (goal == MB_NTERM_reg) {
445 tree->left->exclude_mask |= (1 << X86_EDX);
446 tree->right->exclude_mask |= (1 << X86_EDX);
457 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
459 if (nts [0] && kids [0] == tree) {
461 tree_allocate_regs (kids [0], nts [0], rs);
465 for (i = 0; nts [i]; i++)
466 tree_allocate_regs (kids [i], nts [i], rs);
468 for (i = 0; nts [i]; i++) {
469 mono_regset_free_reg (rs, kids [i]->reg1);
470 mono_regset_free_reg (rs, kids [i]->reg2);
476 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1) {
477 g_warning ("register allocation failed %d 0x%08x 0x%08x\n", tree->reg1, rs->free_mask, tree->exclude_mask);
478 g_assert_not_reached ();
484 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1 ||
486 mono_regset_alloc_reg (rs, tree->reg2, tree->exclude_mask)) == -1) {
487 g_warning ("register allocation failed\n");
488 g_assert_not_reached ();
493 /* fixme: allocate floating point registers */
497 if (tree->op == MB_TERM_ADD) {
498 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
499 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
501 //if (tree->op == MB_TERM_CALL_I4) {
502 //tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
507 if (tree->op == MB_TERM_ADD) {
508 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
513 if (tree->op == MB_TERM_SHL ||
514 tree->op == MB_TERM_MUL) {
515 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
523 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
524 tree->emit = mono_burg_func [ern];
528 arch_allocate_regs (MonoFlowGraph *cfg)
532 for (i = 0; i < cfg->block_count; i++) {
533 GPtrArray *forest = cfg->bblocks [i].forest;
534 const int top = forest->len;
536 for (j = 0; j < top; j++) {
537 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
538 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
539 tree_allocate_regs (t1, 1, cfg->rs);
540 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
541 g_assert (cfg->rs->free_mask == 0xffffffff);
547 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
550 int i, ern = mono_burg_rule (tree->state, goal);
551 guint16 *nts = mono_burg_nts [ern];
554 mono_burg_kids (tree, ern, kids);
556 for (i = 0; nts [i]; i++)
557 tree_emit (nts [i], s, kids [i]);
559 tree->addr = s->code - s->start;
561 if ((emit = mono_burg_func [ern]))
566 mono_emit_cfg (MonoFlowGraph *cfg)
570 for (i = 0; i < cfg->block_count; i++) {
571 MonoBBlock *bb = &cfg->bblocks [i];
572 GPtrArray *forest = bb->forest;
573 const int top = forest->len;
575 bb->addr = cfg->code - cfg->start;
577 for (j = 0; j < top; j++) {
578 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
579 tree_emit (1, cfg, t1);
583 cfg->epilog = cfg->code - cfg->start;
587 mono_compute_branches (MonoFlowGraph *cfg)
594 for (j = 0; j < cfg->block_count; j++) {
595 MonoBBlock *bb = &cfg->bblocks [j];
596 GPtrArray *forest = bb->forest;
597 const int top = forest->len;
599 for (i = 0; i < top; i++) {
600 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
604 if (t1->op == MB_TERM_SWITCH) {
605 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
606 guint32 *rt = (guint32 *)t1->data.p;
608 int m = *((guint32 *)t1->data.p) + 1;
611 for (j = 1; j <= m; j++)
612 rt [j] = (int)(jt [j]->addr + cfg->start);
615 /* emit the jump instruction again to update addresses */
616 cfg->code = cfg->start + t1->addr;
617 ((MBEmitFunc)t1->emit) (t1, cfg);
627 * arch_compile_method:
628 * @method: pointer to the method info
630 * JIT compilation of a single method. This method also writes the result
631 * back to method->addr, an thus overwrites the trampoline function.
633 * Returns: a pointer to the newly created code.
636 arch_compile_method (MonoMethod *method)
639 MonoMemPool *mp = mono_mempool_new ();
641 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
642 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
644 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
645 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
646 method->klass->name, method->name);
649 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
650 MonoClassField *field;
651 const char *name = method->name;
652 static guint target_offset = 0;
653 static guint method_offset = 0;
655 gboolean delegate = FALSE;
657 if (method->klass->parent &&
658 method->klass->parent->parent == mono_defaults.delegate_class)
661 if (!target_offset) {
662 mono_jit_init_class (mono_defaults.delegate_class);
664 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "m_target");
665 target_offset = field->offset;
666 field = mono_class_get_field_from_name (mono_defaults.delegate_class, "method_ptr");
667 method_offset = field->offset;
670 if (delegate && *name == '.' && (strcmp (name, ".ctor") == 0)) {
671 method->addr = code = g_malloc (32);
672 x86_push_reg (code, X86_EBP);
673 x86_mov_reg_reg (code, X86_EBP, X86_ESP, 4);
675 /* load the this pointer */
676 x86_mov_reg_membase (code, X86_EAX, X86_EBP, 8, 4);
677 /* load m_target arg */
678 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 12, 4);
680 x86_mov_membase_reg (code, X86_EAX, target_offset, X86_EDX, 4);
681 /* load method_ptr arg */
682 x86_mov_reg_membase (code, X86_EDX, X86_EBP, 16, 4);
683 /* store method_ptr */
684 x86_mov_membase_reg (code, X86_EAX, method_offset, X86_EDX, 4);
689 g_assert ((code - (guint8*)method->addr) < 32);
690 } else if (delegate && *name == 'I' && (strcmp (name, "Invoke") == 0)) {
691 MonoMethodSignature *csig = method->signature;
692 int i, target, this_pos = 4;
695 method->addr = g_malloc (1024);
697 if (csig->ret->type == MONO_TYPE_VALUETYPE) {
699 if ((size = mono_type_size (csig->ret, &align)) > 4 || size == 3)
703 for (i = 0; i < 2; i ++) {
705 /* load the this pointer */
706 x86_mov_reg_membase (code, X86_EAX, X86_ESP, this_pos, 4);
708 x86_mov_reg_membase (code, X86_EDX, X86_EAX, target_offset, 4);
709 /* check if zero (static method call without this pointer) */
710 x86_alu_reg_imm (code, X86_CMP, X86_EDX, 0);
711 x86_branch32 (code, X86_CC_EQ, target, TRUE);
714 /* virtual call - we have to replace the this pointer */
715 x86_mov_membase_reg (code, X86_ESP, this_pos, X86_EDX, 4);
717 /* jump to method_ptr() */
718 target = code - source;
719 x86_jump_membase (code, X86_EAX, method_offset);
723 g_error ("Don't know how to exec runtime method %s.%s::%s",
724 method->klass->name_space, method->klass->name, method->name);
729 cfg = mono_cfg_new (method, mp);
731 mono_analyze_flow (cfg);
733 mono_analyze_stack (cfg);
736 cfg->rs = mono_regset_new (X86_NREG);
737 mono_regset_reserve_reg (cfg->rs, X86_ESP);
738 mono_regset_reserve_reg (cfg->rs, X86_EBP);
740 // fixme: remove limitation to 8192 bytes
741 method->addr = cfg->start = cfg->code = g_malloc (8192);
743 if (mono_jit_dump_forest) {
745 for (i = 0; i < cfg->block_count; i++) {
746 printf ("BLOCK %d:\n", i);
747 mono_print_forest (cfg->bblocks [i].forest);
751 mono_label_cfg (cfg);
753 arch_allocate_regs (cfg);
755 /* align to 8 byte boundary */
756 cfg->locals_size += 7;
757 cfg->locals_size &= ~7;
759 arch_emit_prologue (cfg);
763 g_assert ((cfg->code - cfg->start) < 8100);
765 arch_emit_epilogue (cfg);
767 mono_compute_branches (cfg);
769 if (mono_jit_dump_asm)
770 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
772 mono_regset_free (cfg->rs);
776 mono_mempool_destroy (mp);
780 if (mono_jit_trace_calls || mono_jit_dump_asm || mono_jit_dump_forest) {
781 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
782 method->klass->name, method->name, method, method->addr);