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 printf ("this:%p[%s.%s], ", o, class->name_space, class->name);
54 ebp += sizeof (gpointer);
57 for (i = 0; i < method->signature->param_count; ++i) {
58 MonoType *type = method->signature->params [i];
60 size = mono_type_size (type, &align);
63 case MONO_TYPE_BOOLEAN:
73 printf ("%d, ", *((int *)(ebp)));
75 case MONO_TYPE_STRING:
78 case MONO_TYPE_OBJECT:
81 case MONO_TYPE_SZARRAY:
82 printf ("%p, ", *((gpointer *)(ebp)));
85 printf ("%lld, ", *((gint64 *)(ebp)));
88 printf ("%f, ", *((double *)(ebp)));
90 case MONO_TYPE_VALUETYPE:
92 for (j = 0; j < size; j++)
93 printf ("%02x,", *((guint8*)ebp +j));
101 ebp = (gpointer)((unsigned)ebp & ~(3));
108 leave_method (MonoMethod *method, int edx, int eax, double test)
112 switch (method->signature->ret->type) {
114 printf ("LEAVE: %s.%s::%s\n", method->klass->name_space,
115 method->klass->name, method->name);
117 case MONO_TYPE_BOOLEAN:
127 printf ("LEAVE: %s.%s::%s EAX=%d\n", method->klass->name_space,
128 method->klass->name, method->name, eax);
130 case MONO_TYPE_STRING:
132 case MONO_TYPE_CLASS:
133 case MONO_TYPE_OBJECT:
134 case MONO_TYPE_FNPTR:
135 case MONO_TYPE_ARRAY:
136 case MONO_TYPE_SZARRAY:
137 printf ("LEAVE: %s.%s::%s EAX=%p\n", method->klass->name_space,
138 method->klass->name, method->name, (gpointer)eax);
141 *((gint32 *)&l) = eax;
142 *((gint32 *)&l + 1) = edx;
143 printf ("LEAVE: %s.%s::%s EAX/EDX=%lld\n", method->klass->name_space,
144 method->klass->name, method->name, l);
147 printf ("LEAVE: %s.%s::%s FP=%f\n", method->klass->name_space,
148 method->klass->name, method->name, test);
151 printf ("LEAVE: %s.%s::%s (unknown return type)\n", method->klass->name_space,
152 method->klass->name, method->name);
157 * arch_emit_prologue:
158 * @cfg: pointer to status information
160 * Emits the function prolog.
163 arch_emit_prologue (MonoFlowGraph *cfg)
165 x86_push_reg (cfg->code, X86_EBP);
166 x86_mov_reg_reg (cfg->code, X86_EBP, X86_ESP, 4);
168 if (cfg->locals_size)
169 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, cfg->locals_size);
171 if (mono_regset_reg_used (cfg->rs, X86_EBX))
172 x86_push_reg (cfg->code, X86_EBX);
174 if (mono_regset_reg_used (cfg->rs, X86_EDI))
175 x86_push_reg (cfg->code, X86_EDI);
177 if (mono_regset_reg_used (cfg->rs, X86_ESI))
178 x86_push_reg (cfg->code, X86_ESI);
180 if (mono_jit_trace_calls) {
181 x86_push_reg (cfg->code, X86_EBP);
182 x86_push_imm (cfg->code, cfg->method);
183 x86_call_code (cfg->code, enter_method);
184 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
189 * arch_emit_epilogue:
190 * @cfg: pointer to status information
192 * Emits the function epilog.
195 arch_emit_epilogue (MonoFlowGraph *cfg)
197 if (mono_jit_trace_calls) {
198 x86_fld_reg (cfg->code, 0);
199 x86_alu_reg_imm (cfg->code, X86_SUB, X86_ESP, 8);
200 x86_fst_membase (cfg->code, X86_ESP, 0, TRUE, TRUE);
201 x86_push_reg (cfg->code, X86_EAX);
202 x86_push_reg (cfg->code, X86_EDX);
203 x86_push_imm (cfg->code, cfg->method);
204 x86_call_code (cfg->code, leave_method);
205 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 4);
206 x86_pop_reg (cfg->code, X86_EDX);
207 x86_pop_reg (cfg->code, X86_EAX);
208 x86_alu_reg_imm (cfg->code, X86_ADD, X86_ESP, 8);
211 if (mono_regset_reg_used (cfg->rs, X86_EDI))
212 x86_pop_reg (cfg->code, X86_EDI);
214 if (mono_regset_reg_used (cfg->rs, X86_ESI))
215 x86_pop_reg (cfg->code, X86_ESI);
217 if (mono_regset_reg_used (cfg->rs, X86_EBX))
218 x86_pop_reg (cfg->code, X86_EBX);
220 x86_leave (cfg->code);
225 * x86_magic_trampoline:
226 * @eax: saved x86 register
227 * @ecx: saved x86 register
228 * @edx: saved x86 register
229 * @esi: saved x86 register
230 * @edi: saved x86 register
231 * @ebx: saved x86 register
232 * @code: pointer into caller code
233 * @method: the method to translate
235 * This method is called by the trampoline functions for virtual
236 * methods. It inspects the caller code to find the address of the
237 * vtable slot, then calls the JIT compiler and writes the address
238 * of the compiled method back to the vtable. All virtual methods
239 * are called with: x86_call_membase (inst, basereg, disp). We always
240 * use 32 bit displacement to ensure that the length of the call
241 * instruction is 6 bytes. We need to get the value of the basereg
242 * and the constant displacement.
245 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
246 int ebx, guint8 *code, MonoMethod *m)
252 /* go to the start of the call instruction */
254 g_assert (*code == 0xff);
258 g_assert ((ab >> 6) == 2);
260 /* extract the register number containing the address */
264 /* extract the displacement */
265 disp = *((gint32*)code);
287 g_assert_not_reached ();
292 return *((gpointer *)o) = arch_compile_method (m);
296 * arch_create_jit_trampoline:
297 * @method: pointer to the method info
299 * Creates a trampoline function for virtual methods. If the created
300 * code is called it first starts JIT compilation of method,
301 * and then calls the newly created method. I also replaces the
302 * corresponding vtable entry (see x86_magic_trampoline).
304 * Returns: a pointer to the newly created code
307 arch_create_jit_trampoline (MonoMethod *method)
310 static guint8 *vc = NULL;
316 vc = buf = g_malloc (24);
318 /* push the return address onto the stack */
319 x86_push_membase (buf, X86_ESP, 4);
321 /* save all register values */
322 x86_push_reg (buf, X86_EBX);
323 x86_push_reg (buf, X86_EDI);
324 x86_push_reg (buf, X86_ESI);
325 x86_push_reg (buf, X86_EDX);
326 x86_push_reg (buf, X86_ECX);
327 x86_push_reg (buf, X86_EAX);
329 x86_call_code (buf, x86_magic_trampoline);
330 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
332 /* call the compiled method */
333 x86_jump_reg (buf, X86_EAX);
335 g_assert ((buf - vc) <= 24);
338 code = buf = g_malloc (16);
339 x86_push_imm (buf, method);
340 x86_jump_code (buf, vc);
341 g_assert ((buf - code) <= 16);
347 * arch_create_simple_jit_trampoline:
348 * @method: pointer to the method info
350 * Creates a trampoline function for method. If the created
351 * code is called it first starts JIT compilation of method,
352 * and then calls the newly created method. I also replaces the
353 * address in method->addr with the result of the JIT
354 * compilation step (in arch_compile_method).
356 * Returns: a pointer to the newly created code
359 arch_create_simple_jit_trampoline (MonoMethod *method)
366 /* we never free the allocated code buffer */
367 code = buf = g_malloc (16);
368 x86_push_imm (buf, method);
369 x86_call_code (buf, arch_compile_method);
370 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
371 /* jump to the compiled method */
372 x86_jump_reg (buf, X86_EAX);
373 g_assert ((buf - code) < 16);
379 mono_label_cfg (MonoFlowGraph *cfg)
383 for (i = 0; i < cfg->block_count; i++) {
384 GPtrArray *forest = cfg->bblocks [i].forest;
385 const int top = forest->len;
387 for (j = 0; j < top; j++) {
388 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
391 mbstate = mono_burg_label (t1, cfg);
393 g_warning ("tree does not match");
394 mono_print_ctree (t1); printf ("\n\n");
396 mono_print_forest (forest);
397 g_assert_not_reached ();
404 tree_allocate_regs (MBTree *tree, int goal, MonoRegSet *rs)
407 int ern = mono_burg_rule (tree->state, goal);
408 guint16 *nts = mono_burg_nts [ern];
411 mono_burg_kids (tree, ern, kids);
415 case MB_NTERM_lreg: {
420 tree->exclude_mask |= (1 << X86_ECX);
421 tree->left->exclude_mask |= (1 << X86_ECX);
423 case MB_TERM_CALL_I4:
424 tree->reg1 = X86_EAX;
426 case MB_TERM_CALL_I8:
427 tree->reg1 = X86_EAX;
428 tree->reg2 = X86_EDX;
434 tree->reg1 = X86_EAX;
435 tree->reg2 = X86_EDX;
436 if (goal == MB_NTERM_reg) {
437 tree->left->exclude_mask |= (1 << X86_EDX);
438 tree->right->exclude_mask |= (1 << X86_EDX);
449 //printf ("RALLOC START %d %p %d\n", tree->op, rs->free_mask, goal);
451 if (nts [0] && kids [0] == tree) {
453 tree_allocate_regs (kids [0], nts [0], rs);
457 for (i = 0; nts [i]; i++)
458 tree_allocate_regs (kids [i], nts [i], rs);
460 for (i = 0; nts [i]; i++) {
461 mono_regset_free_reg (rs, kids [i]->reg1);
462 mono_regset_free_reg (rs, kids [i]->reg2);
468 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1) {
469 g_warning ("register allocation failed %d 0x%08x 0x%08x\n", tree->reg1, rs->free_mask, tree->exclude_mask);
470 g_assert_not_reached ();
476 mono_regset_alloc_reg (rs, tree->reg1, tree->exclude_mask)) == -1 ||
478 mono_regset_alloc_reg (rs, tree->reg2, tree->exclude_mask)) == -1) {
479 g_warning ("register allocation failed\n");
480 g_assert_not_reached ();
485 /* fixme: allocate floating point registers */
489 if (tree->op == MB_TERM_ADD) {
490 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
491 tree->reg2 = mono_regset_alloc_reg (rs, tree->right->reg1, tree->exclude_mask);
493 if (tree->op == MB_TERM_CALL_I4) {
494 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
499 if (tree->op == MB_TERM_ADD) {
500 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
505 if (tree->op == MB_TERM_SHL ||
506 tree->op == MB_TERM_MUL) {
507 tree->reg1 = mono_regset_alloc_reg (rs, tree->left->reg1, tree->exclude_mask);
515 //printf ("RALLOC END %d %p\n", tree->op, rs->free_mask);
516 tree->emit = mono_burg_func [ern];
520 arch_allocate_regs (MonoFlowGraph *cfg)
524 for (i = 0; i < cfg->block_count; i++) {
525 GPtrArray *forest = cfg->bblocks [i].forest;
526 const int top = forest->len;
528 for (j = 0; j < top; j++) {
529 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
530 //printf ("AREGSTART %d:%d %p\n", i, j, cfg->rs->free_mask);
531 tree_allocate_regs (t1, 1, cfg->rs);
532 //printf ("AREGENDT %d:%d %p\n", i, j, cfg->rs->free_mask);
533 g_assert (cfg->rs->free_mask == 0xffffffff);
539 tree_emit (int goal, MonoFlowGraph *s, MBTree *tree)
542 int i, ern = mono_burg_rule (tree->state, goal);
543 guint16 *nts = mono_burg_nts [ern];
546 mono_burg_kids (tree, ern, kids);
548 for (i = 0; nts [i]; i++)
549 tree_emit (nts [i], s, kids [i]);
551 tree->addr = s->code - s->start;
553 if ((emit = mono_burg_func [ern]))
558 mono_emit_cfg (MonoFlowGraph *cfg)
562 for (i = 0; i < cfg->block_count; i++) {
563 MonoBBlock *bb = &cfg->bblocks [i];
564 GPtrArray *forest = bb->forest;
565 const int top = forest->len;
567 bb->addr = cfg->code - cfg->start;
569 for (j = 0; j < top; j++) {
570 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, j);
571 tree_emit (1, cfg, t1);
575 cfg->epilog = cfg->code - cfg->start;
579 mono_compute_branches (MonoFlowGraph *cfg)
586 for (j = 0; j < cfg->block_count; j++) {
587 MonoBBlock *bb = &cfg->bblocks [j];
588 GPtrArray *forest = bb->forest;
589 const int top = forest->len;
591 for (i = 0; i < top; i++) {
592 MBTree *t1 = (MBTree *) g_ptr_array_index (forest, i);
596 if (t1->op == MB_TERM_SWITCH) {
597 MonoBBlock **jt = (MonoBBlock **)t1->data.p;
598 guint32 *rt = (guint32 *)t1->data.p;
600 int m = *((guint32 *)t1->data.p) + 1;
603 for (j = 1; j <= m; j++)
604 rt [j] = (int)(jt [j]->addr + cfg->start);
607 /* emit the jump instruction again to update addresses */
608 cfg->code = cfg->start + t1->addr;
609 ((MBEmitFunc)t1->emit) (t1, cfg);
619 * arch_compile_method:
620 * @method: pointer to the method info
622 * JIT compilation of a single method. This method also writes the result
623 * back to method->addr, an thus overwrites the trampoline function.
625 * Returns: a pointer to the newly created code.
628 arch_compile_method (MonoMethod *method)
631 MonoMemPool *mp = mono_mempool_new ();
633 g_assert (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL));
634 g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL));
636 if (mono_jit_trace_calls) {
637 printf ("Start JIT compilation of %s.%s:%s\n", method->klass->name_space,
638 method->klass->name, method->name);
641 cfg = mono_cfg_new (method, mp);
643 mono_analyze_flow (cfg);
645 mono_analyze_stack (cfg);
648 cfg->rs = mono_regset_new (X86_NREG);
649 mono_regset_reserve_reg (cfg->rs, X86_ESP);
650 mono_regset_reserve_reg (cfg->rs, X86_EBP);
652 // fixme: remove limitation to 4096 bytes
653 method->addr = cfg->start = cfg->code = g_malloc (4096);
655 if (mono_jit_dump_forest) {
657 for (i = 0; i < cfg->block_count; i++) {
658 printf ("BLOCK %d:\n", i);
659 mono_print_forest (cfg->bblocks [i].forest);
663 mono_label_cfg (cfg);
665 arch_allocate_regs (cfg);
667 arch_emit_prologue (cfg);
671 arch_emit_epilogue (cfg);
673 mono_compute_branches (cfg);
675 if (mono_jit_dump_asm)
676 mono_disassemble_code (cfg->start, cfg->code - cfg->start);
678 mono_regset_free (cfg->rs);
682 mono_mempool_destroy (mp);
684 if (mono_jit_trace_calls) {
685 printf ("END JIT compilation of %s.%s:%s %p %p\n", method->klass->name_space,
686 method->klass->name, method->name, method, method->addr);