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)
51 int this_pos = 4, reg;
53 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
56 start = code = g_malloc (36);
58 /* This executes in the context of the caller, hence o0 */
59 sparc_add_imm (code, 0, sparc_o0, sizeof (MonoObject), sparc_o0);
65 sparc_set (code, addr, reg);
66 sparc_jmpl (code, reg, sparc_g0, sparc_g0);
69 g_assert ((code - start) <= 36);
71 mono_arch_flush_icache (start, code - start);
77 * sparc_magic_trampoline:
78 * @m: the method to translate
79 * @code: the address of the call instruction
80 * @fp: address of the stack frame for the caller
82 * This method is called by the trampoline functions for methods. It calls the
83 * JIT compiler to compile the method, then patches the calling instruction so
84 * further calls will bypass the trampoline. For virtual methods, it finds the
85 * address of the vtable slot and updates it.
88 sparc_magic_trampoline (MonoMethod *m, guint32 *code, guint32 *fp)
91 gpointer *vtable_slot;
93 addr = mono_compile_method (m);
97 * Check whenever this is a virtual call, and call an unbox trampoline if
100 if (mono_sparc_is_virtual_call (code)) {
101 if (m->klass->valuetype)
102 addr = get_unbox_trampoline (m, addr);
104 /* Compute address of vtable slot */
105 vtable_slot = mono_sparc_get_vcall_slot_addr (code, fp);
109 /* Patch calling code */
110 if (sparc_inst_op (*code) == 0x1) {
112 mono_jit_info_table_find (mono_domain_get (), code);
113 MonoJitInfo *target_ji =
114 mono_jit_info_table_find (mono_domain_get (), addr);
116 if (mono_method_same_domain (ji, target_ji)) {
117 sparc_call_simple (code, (guint8*)addr - (guint8*)code);
126 sparc_class_init_trampoline (MonoVTable *vtable, guint32 *code)
128 mono_runtime_class_init (vtable);
130 /* Patch calling code */
134 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
137 create_trampoline_code (MonoTrampolineType tramp_type)
139 guint8 *buf, *code, *tramp_addr;
140 guint32 lmf_offset, method_reg, i;
141 static guint8* generic_jump_trampoline = NULL;
142 static guint8 *generic_class_init_trampoline = NULL;
144 switch (tramp_type) {
145 case MONO_TRAMPOLINE_GENERIC:
146 if (mono_generic_trampoline_code)
147 return mono_generic_trampoline_code;
149 case MONO_TRAMPOLINE_JUMP:
150 if (generic_jump_trampoline)
151 return generic_jump_trampoline;
153 case MONO_TRAMPOLINE_CLASS_INIT:
154 if (generic_class_init_trampoline)
155 return generic_class_init_trampoline;
159 code = buf = g_malloc (512);
161 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
164 method_reg = sparc_g4;
166 method_reg = sparc_g1;
170 /* Save fp regs since they are not preserved by calls */
171 for (i = 0; i < 16; i ++)
172 sparc_stdf_imm (code, sparc_f0 + (i * 2), sparc_sp, MONO_SPARC_STACK_BIAS + 320 + (i * 8));
175 /* We receive the method address in %r1, so save it here */
176 sparc_sti_imm (code, method_reg, sparc_sp, MONO_SPARC_STACK_BIAS + 200);
178 /* Save lmf since compilation can raise exceptions */
179 lmf_offset = MONO_SPARC_STACK_BIAS - sizeof (MonoLMF);
181 /* Save the data for the parent (managed) frame */
184 sparc_sti_imm (code, sparc_i7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
186 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
188 /* Load previous fp from the saved register window */
190 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS + (sparc_i6 - 16) * sizeof (gpointer), sparc_o7);
191 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
193 sparc_sti_imm (code, method_reg, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
195 sparc_set (code, mono_get_lmf_addr, sparc_o7);
196 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
199 code = mono_sparc_emit_save_lmf (code, lmf_offset);
201 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
202 tramp_addr = &sparc_class_init_trampoline;
204 tramp_addr = &sparc_magic_trampoline;
205 sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 200, sparc_o0);
206 /* pass parent frame address as third argument */
207 sparc_mov_reg_reg (code, sparc_fp, sparc_o2);
208 sparc_set (code, tramp_addr, sparc_o7);
209 /* set %o1 to caller address */
210 sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
211 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
215 sparc_sti_imm (code, sparc_o0, sparc_sp, MONO_SPARC_STACK_BIAS + 304);
218 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
221 sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 304, sparc_o0);
225 for (i = 0; i < 16; i ++)
226 sparc_lddf_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 320 + (i * 8), sparc_f0 + (i * 2));
229 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
232 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
234 /* restore previous frame in delay slot */
235 sparc_restore_simple (code);
241 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
243 sparc_call_simple (code, 16);
245 sparc_rett_simple (code);
248 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
250 tramp_addr = &sparc_magic_trampoline;
251 sparc_call_simple (code, tramp_addr - code);
253 sparc_rett_simple (code);
258 g_assert ((code - buf) <= 512);
260 switch (tramp_type) {
261 case MONO_TRAMPOLINE_GENERIC:
262 mono_generic_trampoline_code = buf;
264 case MONO_TRAMPOLINE_JUMP:
265 generic_jump_trampoline = buf;
267 case MONO_TRAMPOLINE_CLASS_INIT:
268 generic_class_init_trampoline = buf;
272 mono_arch_flush_icache (buf, code - buf);
277 #define TRAMPOLINE_SIZE (((SPARC_SET_MAX_SIZE >> 2) * 2) + 2)
280 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain)
283 guint32 *code, *buf, *tramp;
285 tramp = create_trampoline_code (tramp_type);
287 mono_domain_lock (domain);
288 code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE * 4);
289 mono_domain_unlock (domain);
291 /* We have to use g5 here because there is no other free register */
292 sparc_set (code, tramp, sparc_g5);
294 sparc_set (code, arg1, sparc_g4);
296 sparc_set (code, arg1, sparc_g1);
298 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
301 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
303 ji = g_new0 (MonoJitInfo, 1);
304 ji->code_start = buf;
305 ji->code_size = (code - buf) * 4;
307 mono_jit_stats.method_trampolines++;
309 mono_arch_flush_icache (ji->code_start, ji->code_size);
315 mono_arch_create_jump_trampoline (MonoMethod *method)
317 MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get ());
324 * mono_arch_create_jit_trampoline:
325 * @method: pointer to the method info
327 * Creates a trampoline function for virtual methods. If the created
328 * code is called it first starts JIT compilation of method,
329 * and then calls the newly created method. I also replaces the
330 * corresponding vtable entry (see sparc_magic_trampoline).
332 * Returns: a pointer to the newly created code
335 mono_arch_create_jit_trampoline (MonoMethod *method)
340 ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get ());
341 code_start = ji->code_start;
348 * mono_arch_create_class_init_trampoline:
349 * @vtable: the type to initialize
351 * Creates a trampoline function to run a type initializer.
352 * If the trampoline is called, it calls mono_runtime_class_init with the
353 * given vtable, then patches the caller code so it does not get called any
356 * Returns: a pointer to the newly created code
359 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
364 ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain);
365 code = ji->code_start;
372 * This method is only called when running in the Mono Debugger.
375 mono_debugger_create_notification_function (gpointer *notification_address)
379 ptr = buf = g_malloc0 (16);
380 if (notification_address)
381 *notification_address = buf;
383 g_assert_not_reached ();