* Dietmar Maurer (dietmar@ximian.com)
* Paolo Molaro (lupus@ximian.com)
* Carlos Valiente <yo@virutass.net>
+ * Andreas Faerber <andreas.faerber@web.de>
*
* (C) 2001 Ximian, Inc.
+ * (C) 2007-2008 Andreas Faerber
*/
#include <config.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/tabledefs.h>
#include <mono/arch/ppc/ppc-codegen.h>
-#include <mono/metadata/mono-debug-debugger.h>
#include "mini.h"
#include "mini-ppc.h"
-typedef enum {
- MONO_TRAMPOLINE_GENERIC,
- MONO_TRAMPOLINE_JUMP,
- MONO_TRAMPOLINE_CLASS_INIT
-} MonoTrampolineType;
-
-/* adapt to mini later... */
-#define mono_jit_share_code (1)
-
/*
- * Address of the x86 trampoline code. This is used by the debugger to check
- * whether a method is a trampoline.
+ * Return the instruction to jump from code to target, 0 if not
+ * reachable with a single instruction
*/
-guint8 *mono_generic_trampoline_code = NULL;
+static guint32
+branch_for_target_reachable (guint8 *branch, guint8 *target)
+{
+ gint diff = target - branch;
+ g_assert ((diff & 3) == 0);
+ if (diff >= 0) {
+ if (diff <= 33554431)
+ return (18 << 26) | (diff);
+ } else {
+ /* diff between 0 and -33554432 */
+ if (diff >= -33554432)
+ return (18 << 26) | (diff & ~0xfc000000);
+ }
+ return 0;
+}
/*
* get_unbox_trampoline:
+ * @gsctx: the generic sharing context
* @m: method pointer
* @addr: pointer to native code for @m
*
* this argument. This method returns a pointer to a trampoline which does
* unboxing before calling the method
*/
-static gpointer
-get_unbox_trampoline (MonoMethod *m, gpointer addr)
+gpointer
+mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
{
guint8 *code, *start;
int this_pos = 3;
+ guint32 short_branch;
+ MonoDomain *domain = mono_domain_get ();
+ int size = MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE;
+
+ addr = mono_get_addr_from_ftnptr (addr);
- if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
+ if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
this_pos = 4;
- start = code = g_malloc (20);
+ mono_domain_lock (domain);
+ start = code = mono_code_manager_reserve (domain->code_mp, size);
+ code = mono_ppc_create_pre_code_ftnptr (code);
+ short_branch = branch_for_target_reachable (code + 4, addr);
+ if (short_branch)
+ mono_code_manager_commit (domain->code_mp, code, size, 8);
+ mono_domain_unlock (domain);
- ppc_load (code, ppc_r0, addr);
- ppc_mtctr (code, ppc_r0);
- ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
- ppc_bcctr (code, 20, 0);
+ if (short_branch) {
+ ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
+ ppc_emit32 (code, short_branch);
+ } else {
+ ppc_load (code, ppc_r0, addr);
+ ppc_mtctr (code, ppc_r0);
+ ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
+ ppc_bcctr (code, 20, 0);
+ }
mono_arch_flush_icache (start, code - start);
- g_assert ((code - start) <= 20);
+ g_assert ((code - start) <= size);
/*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
g_print ("unbox code is at %p for method at %p\n", start, addr);*/
return start;
}
-/* Stack size for trampoline function
- * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
- * + MonoLMF + 14 fp regs + 13 gregs + alignment
- * #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * (sizeof (gulong)))
- * STACK would be 444 for 32 bit darwin
- */
-#define STACK (448)
-
-/* Method-specific trampoline code fragment size */
-#define METHOD_TRAMPOLINE_SIZE 64
-
-/* Jump-specific trampoline code fragment size */
-#define JUMP_TRAMPOLINE_SIZE 64
-
-/**
- * ppc_magic_trampoline:
- * @code: pointer into caller code
- * @method: the method to translate
- * @sp: stack pointer
- *
- * This method is called by the function 'arch_create_jit_trampoline', which in
- * turn is called by the trampoline functions for virtual methods.
- * After having called the JIT compiler to compile the method, it inspects the
- * caller code to find the address of the method-specific part of the
- * trampoline vtable slot for this method, updates it with a fragment that calls
- * the newly compiled code and returns this address of the compiled code to
- * 'arch_create_jit_trampoline'
- */
-static gpointer
-ppc_magic_trampoline (MonoMethod *method, guint32 *code, char *sp)
+void
+mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
{
- char *o = NULL;
- gpointer addr;
- MonoJitInfo *ji, *target_ji;
- int reg, offset = 0;
+ guint32 *code = (guint32*)code_ptr;
- addr = mono_compile_method(method);
- /*g_print ("method code at %p for %s:%s\n", addr, method->klass->name, method->name);*/
- g_assert(addr);
+ addr = mono_get_addr_from_ftnptr (addr);
- if (!code){
- return addr;
- }
-
- /* We can't trampoline across domains */
- ji = mono_jit_info_table_find (mono_domain_get (), code);
- target_ji = mono_jit_info_table_find (mono_domain_get (), addr);
- if (!mono_method_same_domain (ji, target_ji))
- return addr;
-
- /* Locate the address of the method-specific trampoline. The call using
- the vtable slot that took the processing flow to 'arch_create_jit_trampoline'
- looks something like this:
-
- mtlr rA ; Move rA (a register containing the
- ; target address) to LR
- blrl ; Call function at LR
-
- PowerPC instructions are 32-bit long, which means that a 32-bit target
- address cannot be encoded as an immediate value (because we already
- have spent some bits to encode the branch instruction!). That's why a
- 'b'ranch to the contents of the 'l'ink 'r'egister (with 'l'ink register
- update) is needed, instead of a simpler 'branch immediate'. This
- complicates our purpose here, because 'blrl' overwrites LR, which holds
- the value we're interested in.
-
- Therefore, we need to locate the 'mtlr rA' instruction to know which
- register LR was loaded from, and then retrieve the value from that
- register */
-
/* This is the 'blrl' instruction */
--code;
*/
if (((*code) >> 26) == 18) {
/*g_print ("direct patching\n");*/
- ppc_patch ((char*)code, addr);
- mono_arch_flush_icache ((char*)code, 4);
- return addr;
+ ppc_patch ((guint8*)code, addr);
+ mono_arch_flush_icache ((guint8*)code, 4);
+ return;
}
- /* Sanity check: instruction must be 'blrl' */
- g_assert(*code == 0x4e800021);
-
- /* OK, we're now at the 'blrl' instruction. Now walk backwards
- till we get to a 'mtlr rA' */
- for(; --code;) {
- if((*code & 0x7c0803a6) == 0x7c0803a6) {
- gint16 soff;
- gint reg_offset;
- /* Here we are: we reached the 'mtlr rA'.
- Extract the register from the instruction */
- reg = (*code & 0x03e00000) >> 21;
- --code;
- /* ok, this is a lwz reg, offset (vtreg)
- * it is emitted with:
- * ppc_emit32 (c, (32 << 26) | ((D) << 21) | ((a) << 16) | (guint16)(d))
- */
- soff = (*code & 0xffff);
- offset = soff;
- reg = (*code >> 16) & 0x1f;
- g_assert (reg != ppc_r1);
- /*g_print ("patching reg is %d\n", reg);*/
- if (reg >= 13) {
- /* saved in the MonoLMF structure */
- reg_offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
- reg_offset += (reg - 13) * sizeof (gulong);
- } else {
- /* saved in the stack, see frame diagram below */
- reg_offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong));
- reg_offset += reg * sizeof (gulong);
- }
- /* o contains now the value of register reg */
- o = *((char**) (sp + reg_offset));
- break;
- }
- }
+ /* Sanity check */
+ g_assert (mono_ppc_is_direct_call_sequence (code));
- /* this is not done for non-virtual calls, because in that case
- we won't have an object, but the actual pointer to the
- valuetype as the this argument
- */
- if (method->klass->valuetype && !mono_aot_is_got_entry (code, o))
- addr = get_unbox_trampoline (method, addr);
+ ppc_patch ((guint8*)code, addr);
+}
- o += offset;
- if (mono_aot_is_got_entry (code, o) || mono_domain_owns_vtable_slot (mono_domain_get (), o))
- *((gpointer *)o) = addr;
- return addr;
+void
+mono_arch_patch_plt_entry (guint8 *code, guint8 *addr)
+{
+ g_assert_not_reached ();
}
-static void
-ppc_class_init_trampoline (void *vtable, guint32 *code, char *sp)
+void
+mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
{
- mono_runtime_class_init (vtable);
+ return;
+}
-#if 0
- /* This is the 'bl' instruction */
- --code;
-
- if (((*code) >> 26) == 18) {
- ppc_ori (code, 0, 0, 0); /* nop */
- mono_arch_flush_icache (code, 4);
- return;
- } else {
- g_assert_not_reached ();
- }
-#endif
+void
+mono_arch_nullify_plt_entry (guint8 *code)
+{
+ g_assert_not_reached ();
}
+/* Stack size for trampoline function
+ * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
+ * + MonoLMF + 14 fp regs + 13 gregs + alignment
+ * #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * (sizeof (gulong)))
+ * STACK would be 444 for 32 bit darwin
+ */
+#ifdef __mono_ppc64__
+#define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * sizeof (gulong))
+#else
+#define STACK (448)
+#endif
+
+/* Method-specific trampoline code fragment size */
+#define METHOD_TRAMPOLINE_SIZE 64
+
+/* Jump-specific trampoline code fragment size */
+#define JUMP_TRAMPOLINE_SIZE 64
+
/*
* Stack frame description when the generic trampoline is called.
* caller frame
* linkage area
* -------------------
*/
-static guchar*
-create_trampoline_code (MonoTrampolineType tramp_type)
+guchar*
+mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
{
guint8 *buf, *code = NULL;
- static guint8* generic_jump_trampoline = NULL;
- static guint8 *generic_class_init_trampoline = NULL;
int i, offset;
+ gconstpointer tramp_handler;
+ int size = MONO_PPC_32_64_CASE (512, 688);
+
+ /* Now we'll create in 'buf' the PowerPC trampoline code. This
+ is the trampoline code common to all methods */
+
+ code = buf = mono_global_codeman_reserve (size);
- switch (tramp_type) {
- case MONO_TRAMPOLINE_GENERIC:
- if (mono_generic_trampoline_code)
- return mono_generic_trampoline_code;
- break;
- case MONO_TRAMPOLINE_JUMP:
- if (generic_jump_trampoline)
- return generic_jump_trampoline;
- break;
- case MONO_TRAMPOLINE_CLASS_INIT:
- if (generic_class_init_trampoline)
- return generic_class_init_trampoline;
- break;
+ ppc_store_reg_update (buf, ppc_r1, -STACK, ppc_r1);
+
+ /* start building the MonoLMF on the stack */
+ offset = STACK - sizeof (double) * MONO_SAVED_FREGS;
+ for (i = 14; i < 32; i++) {
+ ppc_stfd (buf, i, offset, ppc_r1);
+ offset += sizeof (double);
}
+ /*
+ * now the integer registers.
+ */
+ offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
+ ppc_store_multiple_regs (buf, ppc_r13, ppc_r1, offset);
- if(!code) {
- /* Now we'll create in 'buf' the PowerPC trampoline code. This
- is the trampoline code common to all methods */
-
- code = buf = g_malloc(512);
-
- ppc_stwu (buf, ppc_r1, -STACK, ppc_r1);
-
- /* start building the MonoLMF on the stack */
- offset = STACK - sizeof (double) * MONO_SAVED_FREGS;
- for (i = 14; i < 32; i++) {
- ppc_stfd (buf, i, offset, ppc_r1);
- offset += sizeof (double);
- }
- /*
- * now the integer registers. r13 is already saved in the trampoline,
- * and at this point contains the method to compile, so we skip it.
- */
- offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs) + sizeof (gulong);
- ppc_stmw (buf, ppc_r14, ppc_r1, offset);
-
- /* Now save the rest of the registers below the MonoLMF struct, first 14
- * fp regs and then the 13 gregs.
- */
- offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
- for (i = 0; i < 14; i++) {
- ppc_stfd (buf, i, offset, ppc_r1);
- offset += sizeof (double);
- }
- offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong));
- for (i = 0; i < 13; i++) {
- ppc_stw (buf, i, offset, ppc_r1);
- offset += sizeof (gulong);
- }
- /* we got here through a jump to the ctr reg, we must save the lr
- * in the parent frame (we do it here to reduce the size of the
- * method-specific trampoline)
- */
- ppc_mflr (buf, ppc_r0);
- ppc_stw (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
-
- /* ok, now we can continue with the MonoLMF setup, mostly untouched
- * from emit_prolog in mini-ppc.c
- */
- ppc_load (buf, ppc_r0, mono_get_lmf_addr);
- ppc_mtlr (buf, ppc_r0);
- ppc_blrl (buf);
- /* we build the MonoLMF structure on the stack - see mini-ppc.h
- * The pointer to the struct is put in ppc_r11.
- */
- ppc_addi (buf, ppc_r11, ppc_sp, STACK - sizeof (MonoLMF));
- ppc_stw (buf, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
- /* new_lmf->previous_lmf = *lmf_addr */
- ppc_lwz (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
- ppc_stw (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
- /* *(lmf_addr) = r11 */
- ppc_stw (buf, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
- /* save method info (it's in r13) */
- ppc_stw (buf, ppc_r13, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
- ppc_stw (buf, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
- /* save the IP (caller ip) */
- if (tramp_type == MONO_TRAMPOLINE_JUMP) {
- ppc_li (buf, ppc_r0, 0);
- } else {
- ppc_lwz (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
- }
- ppc_stw (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
-
- /*
- * Now we're ready to call ppc_magic_trampoline ().
- */
- /* Arg 1: MonoMethod *method. It was put in r13 */
- ppc_mr (buf, ppc_r3, ppc_r13);
-
- /* Arg 2: code (next address to the instruction that called us) */
- if (tramp_type == MONO_TRAMPOLINE_JUMP) {
- ppc_li (buf, ppc_r4, 0);
- } else {
- ppc_lwz (buf, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
- }
-
- /* Arg 3: stack pointer so that the magic trampoline can access the
- * registers we saved above
- */
- ppc_mr (buf, ppc_r5, ppc_r1);
+ /* Now save the rest of the registers below the MonoLMF struct, first 14
+ * fp regs and then the 13 gregs.
+ */
+ offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
+ for (i = 0; i < 14; i++) {
+ ppc_stfd (buf, i, offset, ppc_r1);
+ offset += sizeof (double);
+ }
+#define GREGS_OFFSET (STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong)))
+ offset = GREGS_OFFSET;
+ for (i = 0; i < 13; i++) {
+ ppc_store_reg (buf, i, offset, ppc_r1);
+ offset += sizeof (gulong);
+ }
+ /* we got here through a jump to the ctr reg, we must save the lr
+ * in the parent frame (we do it here to reduce the size of the
+ * method-specific trampoline)
+ */
+ ppc_mflr (buf, ppc_r0);
+ ppc_store_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
+
+ /* ok, now we can continue with the MonoLMF setup, mostly untouched
+ * from emit_prolog in mini-ppc.c
+ */
+ ppc_load_func (buf, ppc_r0, mono_get_lmf_addr);
+ ppc_mtlr (buf, ppc_r0);
+ ppc_blrl (buf);
+ /* we build the MonoLMF structure on the stack - see mini-ppc.h
+ * The pointer to the struct is put in ppc_r11.
+ */
+ ppc_addi (buf, ppc_r11, ppc_sp, STACK - sizeof (MonoLMF));
+ ppc_store_reg (buf, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
+ /* new_lmf->previous_lmf = *lmf_addr */
+ ppc_load_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
+ ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
+ /* *(lmf_addr) = r11 */
+ ppc_store_reg (buf, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
+ /* save method info (it's stored on the stack, so get it first and put it
+ * in r5 as it's the third argument to the function)
+ */
+ if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
+ ppc_load_reg (buf, ppc_r5, GREGS_OFFSET + PPC_FIRST_ARG_REG * sizeof (gpointer), ppc_r1);
+ else
+ ppc_load_reg (buf, ppc_r5, GREGS_OFFSET, ppc_r1);
+ if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
+ ppc_store_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
+ ppc_store_reg (buf, ppc_sp, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
+ /* save the IP (caller ip) */
+ if (tramp_type == MONO_TRAMPOLINE_JUMP) {
+ ppc_li (buf, ppc_r0, 0);
+ } else {
+ ppc_load_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
+ }
+ ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
+
+ /*
+ * Now we're ready to call trampoline (gssize *regs, guint8 *code, gpointer value, guint8 *tramp)
+ * Note that the last argument is unused.
+ */
+ /* Arg 1: a pointer to the registers */
+ ppc_addi (buf, ppc_r3, ppc_r1, GREGS_OFFSET);
- if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
- ppc_lis (buf, ppc_r0, (guint32) ppc_class_init_trampoline >> 16);
- ppc_ori (buf, ppc_r0, ppc_r0, (guint32) ppc_class_init_trampoline & 0xffff);
- } else {
- ppc_lis (buf, ppc_r0, (guint32) ppc_magic_trampoline >> 16);
- ppc_ori (buf, ppc_r0, ppc_r0, (guint32) ppc_magic_trampoline & 0xffff);
- }
- ppc_mtlr (buf, ppc_r0);
- ppc_blrl (buf);
+ /* Arg 2: code (next address to the instruction that called us) */
+ if (tramp_type == MONO_TRAMPOLINE_JUMP)
+ ppc_li (buf, ppc_r4, 0);
+ else
+ ppc_load_reg (buf, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
+
+ /* Arg 3: MonoMethod *method. It was put in r5 already above */
+ /*ppc_mr (buf, ppc_r5, ppc_r5);*/
+
+ tramp_handler = mono_get_trampoline_func (tramp_type);
+ ppc_load_func (buf, ppc_r0, tramp_handler);
+ ppc_mtlr (buf, ppc_r0);
+ ppc_blrl (buf);
- /* OK, code address is now on r3. Move it to the counter reg
- * so it will be ready for the final jump: this is safe since we
- * won't do any more calls.
- */
+ /* OK, code address is now on r3. Move it to the counter reg
+ * so it will be ready for the final jump: this is safe since we
+ * won't do any more calls.
+ */
+ if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
+#ifdef PPC_USES_FUNCTION_DESCRIPTOR
+ if (tramp_type != MONO_TRAMPOLINE_DELEGATE)
+ ppc_load_reg (buf, ppc_r3, 0, ppc_r3);
+#endif
ppc_mtctr (buf, ppc_r3);
+ }
+
+ /*
+ * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
+ * and the rest of the registers, so the method called will see
+ * the same state as before we executed.
+ * The pointer to MonoLMF is in ppc_r11.
+ */
+ ppc_addi (buf, ppc_r11, ppc_r1, STACK - sizeof (MonoLMF));
+ /* r5 = previous_lmf */
+ ppc_load_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
+ /* r6 = lmf_addr */
+ ppc_load_reg (buf, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
+ /* *(lmf_addr) = previous_lmf */
+ ppc_store_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
+ /* restore iregs */
+ ppc_load_multiple_regs (buf, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
+ /* restore fregs */
+ for (i = 14; i < 32; i++)
+ ppc_lfd (buf, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
+
+ /* restore the volatile registers, we skip r1, of course */
+ offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
+ for (i = 0; i < 14; i++) {
+ ppc_lfd (buf, i, offset, ppc_r1);
+ offset += sizeof (double);
+ }
+ offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong));
+ ppc_load_reg (buf, ppc_r0, offset, ppc_r1);
+ offset += 2 * sizeof (gulong);
+ for (i = 2; i < 13; i++) {
+ if (i != 3 || tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH)
+ ppc_load_reg (buf, i, offset, ppc_r1);
+ offset += sizeof (gulong);
+ }
- /*
- * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
- * and the rest of the registers, so the method called will see
- * the same state as before we executed.
- * The pointer to MonoLMF is in ppc_r11.
- */
- ppc_addi (buf, ppc_r11, ppc_r1, STACK - sizeof (MonoLMF));
- /* r5 = previous_lmf */
- ppc_lwz (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
- /* r6 = lmf_addr */
- ppc_lwz (buf, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
- /* *(lmf_addr) = previous_lmf */
- ppc_stw (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
- /* restore iregs: this time include r13 */
- ppc_lmw (buf, ppc_r13, ppc_r11, G_STRUCT_OFFSET(MonoLMF, iregs));
- /* restore fregs */
- for (i = 14; i < 32; i++) {
- ppc_lfd (buf, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
- }
-
- /* restore the volatile registers, we skip r1, of course */
- offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
- for (i = 0; i < 14; i++) {
- ppc_lfd (buf, i, offset, ppc_r1);
- offset += sizeof (double);
- }
- offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (13 * sizeof (gulong));
- ppc_lwz (buf, ppc_r0, offset, ppc_r1);
- offset += 2 * sizeof (gulong);
- for (i = 2; i < 13; i++) {
- ppc_lwz (buf, i, offset, ppc_r1);
- offset += sizeof (gulong);
- }
-
- /* Non-standard function epilogue. Instead of doing a proper
- * return, we just hump to the compiled code.
- */
- /* Restore stack pointer and LR and jump to the code */
- ppc_lwz (buf, ppc_r1, 0, ppc_r1);
- ppc_lwz (buf, ppc_r11, PPC_RET_ADDR_OFFSET, ppc_r1);
- ppc_mtlr (buf, ppc_r11);
+ /* Non-standard function epilogue. Instead of doing a proper
+ * return, we just jump to the compiled code.
+ */
+ /* Restore stack pointer and LR and jump to the code */
+ ppc_load_reg (buf, ppc_r1, 0, ppc_r1);
+ ppc_load_reg (buf, ppc_r11, PPC_RET_ADDR_OFFSET, ppc_r1);
+ ppc_mtlr (buf, ppc_r11);
+ if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
+ ppc_blr (buf);
+ else
ppc_bcctr (buf, 20, 0);
- /* Flush instruction cache, since we've generated code */
- mono_arch_flush_icache (code, buf - code);
+ /* Flush instruction cache, since we've generated code */
+ mono_arch_flush_icache (code, buf - code);
- /* Sanity check */
- g_assert ((buf - code) <= 512);
- }
-
- switch (tramp_type) {
- case MONO_TRAMPOLINE_GENERIC:
- mono_generic_trampoline_code = code;
- break;
- case MONO_TRAMPOLINE_JUMP:
- generic_jump_trampoline = code;
- break;
- case MONO_TRAMPOLINE_CLASS_INIT:
- generic_class_init_trampoline = code;
- break;
- }
+ /* Sanity check */
+ g_assert ((buf - code) <= size);
return code;
}
-static MonoJitInfo*
-create_specific_tramp (MonoMethod *method, guint8* tramp, MonoDomain *domain) {
- guint8 *code, *buf;
- MonoJitInfo *ji;
+#define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
+gpointer
+mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
+{
+ guint8 *code, *buf, *tramp;
+ guint32 short_branch;
+
+ tramp = mono_get_trampoline_code (tramp_type);
mono_domain_lock (domain);
- code = buf = mono_code_manager_reserve (domain->code_mp, 32);
+ code = buf = mono_code_manager_reserve_align (domain->code_mp, TRAMPOLINE_SIZE, 4);
+ short_branch = branch_for_target_reachable (code + MONO_PPC_32_64_CASE (8, 5*4), tramp);
+#ifdef __mono_ppc64__
+ /* FIXME: make shorter if possible */
+#else
+ if (short_branch)
+ mono_code_manager_commit (domain->code_mp, code, TRAMPOLINE_SIZE, 12);
+#endif
mono_domain_unlock (domain);
- /* Save r13 in the place it will have in the on-stack MonoLMF */
- ppc_stw (buf, ppc_r13, -(MONO_SAVED_FREGS * 8 + MONO_SAVED_GREGS * sizeof (gpointer)), ppc_r1);
-
- /* Prepare the jump to the generic trampoline code.*/
- ppc_lis (buf, ppc_r13, (guint32) tramp >> 16);
- ppc_ori (buf, ppc_r13, ppc_r13, (guint32) tramp & 0xffff);
- ppc_mtctr (buf, ppc_r13);
+ if (short_branch) {
+ ppc_load_sequence (buf, ppc_r0, (gulong) arg1);
+ ppc_emit32 (buf, short_branch);
+ } else {
+ /* Prepare the jump to the generic trampoline code.*/
+ ppc_load (buf, ppc_r0, (gulong) tramp);
+ ppc_mtctr (buf, ppc_r0);
- /* And finally put 'method' in r13 and fly! */
- ppc_lis (buf, ppc_r13, (guint32) method >> 16);
- ppc_ori (buf, ppc_r13, ppc_r13, (guint32) method & 0xffff);
- ppc_bcctr (buf, 20, 0);
+ /* And finally put 'arg1' in r0 and fly! */
+ ppc_load (buf, ppc_r0, (gulong) arg1);
+ ppc_bcctr (buf, 20, 0);
+ }
/* Flush instruction cache, since we've generated code */
mono_arch_flush_icache (code, buf - code);
- g_assert ((buf - code) <= 32);
-
- ji = g_new0 (MonoJitInfo, 1);
- ji->method = method;
- ji->code_start = code;
- ji->code_size = buf - code;
+ g_assert ((buf - code) <= TRAMPOLINE_SIZE);
+ if (code_len)
+ *code_len = buf - code;
- mono_jit_stats.method_trampolines++;
-
- return ji;
+ return code;
}
-MonoJitInfo*
-mono_arch_create_jump_trampoline (MonoMethod *method)
+static guint8*
+emit_trampoline_jump (guint8 *code, guint8 *tramp)
{
- guint8 *tramp;
- MonoDomain* domain = mono_domain_get ();
-
- tramp = create_trampoline_code (MONO_TRAMPOLINE_JUMP);
- return create_specific_tramp (method, tramp, domain);
+ guint32 short_branch = branch_for_target_reachable (code, tramp);
+
+ /* FIXME: we can save a few bytes here by committing if the
+ short branch is possible */
+ if (short_branch) {
+ ppc_emit32 (code, short_branch);
+ } else {
+ ppc_load (code, ppc_r0, tramp);
+ ppc_mtctr (code, ppc_r0);
+ ppc_bcctr (code, 20, 0);
+ }
+
+ return code;
}
-/**
- * arch_create_jit_trampoline:
- * @method: pointer to the method info
- *
- * Creates a trampoline function for virtual methods. If the created
- * code is called it first starts JIT compilation of method,
- * and then calls the newly created method. It also replaces the
- * corresponding vtable entry (see ppc_magic_trampoline).
- *
- * A trampoline consists of two parts: a main fragment, shared by all method
- * trampolines, and some code specific to each method, which hard-codes a
- * reference to that method and then calls the main fragment.
- *
- * The main fragment contains a call to 'ppc_magic_trampoline', which performs
- * call to the JIT compiler and substitutes the method-specific fragment with
- * some code that directly calls the JIT-compiled method.
- *
- * Returns: a pointer to the newly created code
- */
gpointer
-mono_arch_create_jit_trampoline (MonoMethod *method)
+mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
{
+#ifdef MONO_ARCH_VTABLE_REG
guint8 *tramp;
- MonoJitInfo *ji;
- MonoDomain* domain = mono_domain_get ();
- gpointer code_start;
+ guint8 *code, *buf;
+ guint8 **rgctx_null_jumps;
+ int tramp_size;
+ int depth, index;
+ int i;
+ gboolean mrgctx;
+
+ mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
+ index = MONO_RGCTX_SLOT_INDEX (slot);
+ if (mrgctx)
+ index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
+ for (depth = 0; ; ++depth) {
+ int size = mono_class_rgctx_get_array_size (depth, mrgctx);
+
+ if (index < size - 1)
+ break;
+ index -= size - 1;
+ }
- tramp = create_trampoline_code (MONO_TRAMPOLINE_GENERIC);
- /* FIXME: should pass the domain down to this function */
- ji = create_specific_tramp (method, tramp, domain);
- code_start = ji->code_start;
- g_free (ji);
+ tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
+ if (mrgctx)
+ tramp_size += 4;
+ else
+ tramp_size += 12;
- return code_start;
-}
+ code = buf = mono_global_codeman_reserve (tramp_size);
-/**
- * mono_arch_create_class_init_trampoline:
- * @vtable: the type to initialize
- *
- * Creates a trampoline function to run a type initializer.
- * If the trampoline is called, it calls mono_runtime_class_init with the
- * given vtable, then patches the caller code so it does not get called any
- * more.
- *
- * Returns: a pointer to the newly created code
- */
-gpointer
-mono_arch_create_class_init_trampoline (MonoVTable *vtable)
-{
- guint8 *code, *buf, *tramp;
+ rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
- tramp = create_trampoline_code (MONO_TRAMPOLINE_CLASS_INIT);
-
- /* This is the method-specific part of the trampoline. Its purpose is
- to provide the generic part with the MonoMethod *method pointer. We'll
- use r11 to keep that value, for instance. However, the generic part of
- the trampoline relies on r11 having the same value it had before coming
- here, so we must save it before. */
- //code = buf = g_malloc(METHOD_TRAMPOLINE_SIZE);
- mono_domain_lock (vtable->domain);
- code = buf = mono_code_manager_reserve (vtable->domain->code_mp, METHOD_TRAMPOLINE_SIZE);
- mono_domain_unlock (vtable->domain);
-
- ppc_mflr (buf, ppc_r4);
- ppc_stw (buf, ppc_r4, PPC_RET_ADDR_OFFSET, ppc_sp);
- ppc_stwu (buf, ppc_sp, -64, ppc_sp);
- ppc_load (buf, ppc_r3, vtable);
- ppc_load (buf, ppc_r5, 0);
-
- ppc_load (buf, ppc_r0, ppc_class_init_trampoline);
- ppc_mtlr (buf, ppc_r0);
- ppc_blrl (buf);
+ if (mrgctx) {
+ /* get mrgctx ptr */
+ ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
+ } else {
+ /* load rgctx ptr from vtable */
+ ppc_load_reg (code, ppc_r4, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), PPC_FIRST_ARG_REG);
+ /* is the rgctx ptr null? */
+ ppc_compare_reg_imm (code, 0, ppc_r4, 0);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [0] = code;
+ ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
+ }
- ppc_lwz (buf, ppc_r0, 64 + PPC_RET_ADDR_OFFSET, ppc_sp);
- ppc_mtlr (buf, ppc_r0);
- ppc_addic (buf, ppc_sp, ppc_sp, 64);
- ppc_blr (buf);
+ for (i = 0; i < depth; ++i) {
+ /* load ptr to next array */
+ if (mrgctx && i == 0)
+ ppc_load_reg (code, ppc_r4, sizeof (MonoMethodRuntimeGenericContext), ppc_r4);
+ else
+ ppc_load_reg (code, ppc_r4, 0, ppc_r4);
+ /* is the ptr null? */
+ ppc_compare_reg_imm (code, 0, ppc_r4, 0);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [i + 1] = code;
+ ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
+ }
- /* Flush instruction cache, since we've generated code */
- mono_arch_flush_icache (code, buf - code);
-
- /* Sanity check */
- g_assert ((buf - code) <= METHOD_TRAMPOLINE_SIZE);
+ /* fetch slot */
+ ppc_load_reg (code, ppc_r4, sizeof (gpointer) * (index + 1), ppc_r4);
+ /* is the slot null? */
+ ppc_compare_reg_imm (code, 0, ppc_r4, 0);
+ /* if yes, jump to actual trampoline */
+ rgctx_null_jumps [depth + 1] = code;
+ ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
+ /* otherwise return r4 */
+ /* FIXME: if we use r3 as the work register we can avoid this copy */
+ ppc_mr (code, ppc_r3, ppc_r4);
+ ppc_blr (code);
- mono_jit_stats.method_trampolines++;
+ for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
+ ppc_patch (rgctx_null_jumps [i], code);
- return code;
+ g_free (rgctx_null_jumps);
+
+ /* move the rgctx pointer to the VTABLE register */
+ ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
+
+ tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
+ MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
+
+ /* jump to the actual trampoline */
+ code = emit_trampoline_jump (code, tramp);
+
+ mono_arch_flush_icache (buf, code - buf);
+
+ g_assert (code - buf <= tramp_size);
+
+ return buf;
+#else
+ g_assert_not_reached ();
+#endif
}
-/*
- * This method is only called when running in the Mono Debugger.
- */
gpointer
-mono_debugger_create_notification_function (gpointer *notification_address)
+mono_arch_create_generic_class_init_trampoline (void)
{
- guint8 *ptr, *buf;
+ guint8 *tramp;
+ guint8 *code, *buf;
+ static int byte_offset = -1;
+ static guint8 bitmask;
+ guint8 *jump;
+ int tramp_size;
- ptr = buf = g_malloc0 (16);
- ppc_break (buf);
- if (notification_address)
- *notification_address = buf;
- ppc_blr (buf);
+ tramp_size = 32;
- return ptr;
-}
+ code = buf = mono_global_codeman_reserve (tramp_size);
+
+ if (byte_offset < 0)
+ mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
+
+ ppc_lbz (code, ppc_r4, byte_offset, PPC_FIRST_ARG_REG);
+ ppc_andid (code, ppc_r4, ppc_r4, bitmask);
+ jump = code;
+ ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
+
+ ppc_blr (code);
+
+ ppc_patch (jump, code);
+ tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
+ mono_get_root_domain (), NULL);
+
+ /* jump to the actual trampoline */
+ code = emit_trampoline_jump (code, tramp);
+
+ mono_arch_flush_icache (buf, code - buf);
+
+ g_assert (code - buf <= tramp_size);
+
+ return buf;
+}