2 * tramp-sparc.c: JIT trampoline code for Sparc 64
5 * Mark Crichton (crichton@gimp.org)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
14 #include <mono/arch/sparc/sparc-codegen.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/mono-debug-debugger.h>
21 #include "mini-sparc.h"
24 MONO_TRAMPOLINE_GENERIC,
26 MONO_TRAMPOLINE_CLASS_INIT
29 /* adapt to mini later... */
30 #define mono_jit_share_code (1)
33 * Address of the Sparc trampoline code. This is used by the debugger to check
34 * whether a method is a trampoline.
36 guint8 *mono_generic_trampoline_code = NULL;
39 * get_unbox_trampoline:
41 * @addr: pointer to native code for @m
43 * when value type methods are called through the vtable we need to unbox the
44 * this argument. This method returns a pointer to a trampoline which does
45 * unboxing before calling the method
48 get_unbox_trampoline (MonoMethod *m, gpointer addr)
53 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
56 start = code = g_malloc (32);
58 /* This executes in the context of the caller, hence o0 */
59 sparc_add_imm (code, 0, sparc_o0, sizeof (MonoObject), sparc_o0);
60 sparc_set (code, addr, sparc_g1);
61 sparc_jmpl (code, sparc_g1, sparc_g0, sparc_g0);
64 g_assert ((code - start) <= 32);
66 mono_arch_flush_icache (start, code - start);
72 * sparc_magic_trampoline:
73 * @m: the method to translate
74 * @code: the address of the call instruction
76 * This method is called by the trampoline functions for methods. It calls the
77 * JIT compiler to compile the method, then patches the calling instruction so
78 * further calls will bypass the trampoline. For virtual methods, it finds the
79 * address of the vtable slot and updates it.
82 sparc_magic_trampoline (MonoMethod *m, guint32 *code)
86 addr = mono_compile_method (m);
89 /* FIXME: patch calling code and vtable */
92 * Check whenever this is a virtual call, and call an unbox trampoline if
95 if (mono_sparc_is_virtual_call (code)) {
96 if (m->klass->valuetype)
97 addr = get_unbox_trampoline (m, addr);
104 sparc_class_init_trampoline (MonoVTable *vtable, guint32 *code)
106 mono_runtime_class_init (vtable);
108 /* FIXME: patch calling code */
112 create_trampoline_code (MonoTrampolineType tramp_type)
114 guint8 *buf, *code, *tramp_addr;
115 static guint8* generic_jump_trampoline = NULL;
116 static guint8 *generic_class_init_trampoline = NULL;
118 switch (tramp_type) {
119 case MONO_TRAMPOLINE_GENERIC:
120 if (mono_generic_trampoline_code)
121 return mono_generic_trampoline_code;
123 case MONO_TRAMPOLINE_JUMP:
124 if (generic_jump_trampoline)
125 return generic_jump_trampoline;
127 case MONO_TRAMPOLINE_CLASS_INIT:
128 if (generic_class_init_trampoline)
129 return generic_class_init_trampoline;
133 code = buf = g_malloc (256);
135 /* FIXME: save lmf etc */
137 sparc_save_imm (code, sparc_sp, -200, sparc_sp);
139 /* We receive the method address in %r1 */
140 sparc_mov_reg_reg (code, sparc_g1, sparc_o0);
142 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
143 tramp_addr = &sparc_class_init_trampoline;
145 tramp_addr = &sparc_magic_trampoline;
146 sparc_call_simple (code, tramp_addr - code);
147 /* set %o1 to caller address in delay slot */
148 sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
150 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
153 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
155 /* restore previous frame in delay slot */
156 sparc_restore_simple (code);
158 g_assert ((code - buf) <= 256);
160 switch (tramp_type) {
161 case MONO_TRAMPOLINE_GENERIC:
162 mono_generic_trampoline_code = buf;
164 case MONO_TRAMPOLINE_JUMP:
165 generic_jump_trampoline = buf;
167 case MONO_TRAMPOLINE_CLASS_INIT:
168 generic_class_init_trampoline = buf;
172 /* FIXME: flush icache */
177 #define TRAMPOLINE_SIZE (((SPARC_SET_MAX_SIZE >> 2) * 2) + 2)
180 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type)
183 guint32 *code, *buf, *tramp;
185 tramp = create_trampoline_code (tramp_type);
187 code = buf = g_malloc (TRAMPOLINE_SIZE * 4);
189 /* We have to use g5 here because there is no other free register */
190 sparc_set (code, tramp, sparc_g5);
191 sparc_set (code, arg1, sparc_r1);
192 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
195 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
197 ji = g_new0 (MonoJitInfo, 1);
198 ji->code_start = buf;
199 ji->code_size = (code - buf) * 4;
201 mono_jit_stats.method_trampolines++;
203 mono_arch_flush_icache (ji->code_start, ji->code_size);
209 mono_arch_create_jump_trampoline (MonoMethod *method)
211 MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC);
218 * mono_arch_create_jit_trampoline:
219 * @method: pointer to the method info
221 * Creates a trampoline function for virtual methods. If the created
222 * code is called it first starts JIT compilation of method,
223 * and then calls the newly created method. I also replaces the
224 * corresponding vtable entry (see sparc_magic_trampoline).
226 * Returns: a pointer to the newly created code
229 mono_arch_create_jit_trampoline (MonoMethod *method)
233 /* previously created trampoline code */
237 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
238 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
240 ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC);
241 method->info = ji->code_start;
248 * mono_arch_create_class_init_trampoline:
249 * @vtable: the type to initialize
251 * Creates a trampoline function to run a type initializer.
252 * If the trampoline is called, it calls mono_runtime_class_init with the
253 * given vtable, then patches the caller code so it does not get called any
256 * Returns: a pointer to the newly created code
259 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
264 ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT);
265 code = ji->code_start;
272 * This method is only called when running in the Mono Debugger.
275 mono_debugger_create_notification_function (gpointer *notification_address)
279 ptr = buf = g_malloc0 (16);
280 //x86_breakpoint (buf);
281 if (notification_address)
282 *notification_address = buf;