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 * get_unbox_trampoline:
26 * @addr: pointer to native code for @m
28 * when value type methods are called through the vtable we need to unbox the
29 * this argument. This method returns a pointer to a trampoline which does
30 * unboxing before calling the method
33 get_unbox_trampoline (MonoMethod *m, gpointer addr)
36 int this_pos = 4, reg;
38 if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
41 start = code = mono_global_codeman_reserve (36);
43 /* This executes in the context of the caller, hence o0 */
44 sparc_add_imm (code, 0, sparc_o0, sizeof (MonoObject), sparc_o0);
50 sparc_set (code, addr, reg);
51 sparc_jmpl (code, reg, sparc_g0, sparc_g0);
54 g_assert ((code - start) <= 36);
56 mono_arch_flush_icache (start, code - start);
62 * sparc_magic_trampoline:
63 * @m: the method to translate
64 * @code: the address of the call instruction
65 * @fp: address of the stack frame for the caller
67 * This method is called by the trampoline functions for methods. It calls the
68 * JIT compiler to compile the method, then patches the calling instruction so
69 * further calls will bypass the trampoline. For virtual methods, it finds the
70 * address of the vtable slot and updates it.
73 sparc_magic_trampoline (MonoMethod *m, guint32 *code, guint32 *fp)
76 gpointer *vtable_slot;
78 addr = mono_compile_method (m);
82 * Check whenever this is a virtual call, and call an unbox trampoline if
85 if (mono_sparc_is_virtual_call (code)) {
86 /* Compute address of vtable slot */
87 vtable_slot = mono_sparc_get_vcall_slot_addr (code, fp);
88 if (m->klass->valuetype && !mono_aot_is_got_entry (code, vtable_slot))
89 addr = get_unbox_trampoline (m, addr);
90 if (mono_aot_is_got_entry (code, vtable_slot) || mono_domain_owns_vtable_slot (mono_domain_get (), vtable_slot))
94 /* Patch calling code */
95 if (sparc_inst_op (*code) == 0x1) {
97 mono_jit_info_table_find (mono_domain_get (), code);
98 MonoJitInfo *target_ji =
99 mono_jit_info_table_find (mono_domain_get (), addr);
101 if (mono_method_same_domain (ji, target_ji)) {
102 sparc_call_simple (code, (guint8*)addr - (guint8*)code);
111 sparc_class_init_trampoline (MonoVTable *vtable, guint32 *code)
113 mono_runtime_class_init (vtable);
115 /* Patch calling code */
119 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
122 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
124 guint8 *buf, *code, *tramp_addr;
125 guint32 lmf_offset, method_reg, i;
127 code = buf = mono_global_codeman_reserve (512);
129 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
132 method_reg = sparc_g4;
134 method_reg = sparc_g1;
138 /* Save fp regs since they are not preserved by calls */
139 for (i = 0; i < 16; i ++)
140 sparc_stdf_imm (code, sparc_f0 + (i * 2), sparc_sp, MONO_SPARC_STACK_BIAS + 320 + (i * 8));
143 /* We receive the method address in %r1, so save it here */
144 sparc_sti_imm (code, method_reg, sparc_sp, MONO_SPARC_STACK_BIAS + 200);
146 /* Save lmf since compilation can raise exceptions */
147 lmf_offset = MONO_SPARC_STACK_BIAS - sizeof (MonoLMF);
149 /* Save the data for the parent (managed) frame */
152 sparc_sti_imm (code, sparc_i7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
154 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
156 /* Load previous fp from the saved register window */
158 sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS + (sparc_i6 - 16) * sizeof (gpointer), sparc_o7);
159 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
161 sparc_sti_imm (code, method_reg, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
163 sparc_set (code, mono_get_lmf_addr, sparc_o7);
164 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
167 code = mono_sparc_emit_save_lmf (code, lmf_offset);
169 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
170 tramp_addr = &sparc_class_init_trampoline;
172 tramp_addr = &sparc_magic_trampoline;
173 sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 200, sparc_o0);
174 /* pass parent frame address as third argument */
175 sparc_mov_reg_reg (code, sparc_fp, sparc_o2);
176 sparc_set (code, tramp_addr, sparc_o7);
177 /* set %o1 to caller address */
178 sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
179 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
183 sparc_sti_imm (code, sparc_o0, sparc_sp, MONO_SPARC_STACK_BIAS + 304);
186 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
189 sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 304, sparc_o0);
193 for (i = 0; i < 16; i ++)
194 sparc_lddf_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 320 + (i * 8), sparc_f0 + (i * 2));
197 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
200 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
202 /* restore previous frame in delay slot */
203 sparc_restore_simple (code);
209 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
211 sparc_call_simple (code, 16);
213 sparc_rett_simple (code);
216 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
218 tramp_addr = &sparc_magic_trampoline;
219 sparc_call_simple (code, tramp_addr - code);
221 sparc_rett_simple (code);
226 g_assert ((code - buf) <= 512);
228 mono_arch_flush_icache (buf, code - buf);
233 #define TRAMPOLINE_SIZE (((SPARC_SET_MAX_SIZE >> 2) * 2) + 2)
236 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain)
239 guint32 *code, *buf, *tramp;
241 tramp = mono_get_trampoline_code (tramp_type);
243 mono_domain_lock (domain);
244 code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE * 4);
245 mono_domain_unlock (domain);
247 /* We have to use g5 here because there is no other free register */
248 sparc_set (code, tramp, sparc_g5);
250 sparc_set (code, arg1, sparc_g4);
252 sparc_set (code, arg1, sparc_g1);
254 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
257 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
259 ji = g_new0 (MonoJitInfo, 1);
260 ji->code_start = buf;
261 ji->code_size = (code - buf) * 4;
263 mono_jit_stats.method_trampolines++;
265 mono_arch_flush_icache (ji->code_start, ji->code_size);
271 mono_arch_create_jump_trampoline (MonoMethod *method)
273 MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get ());
280 * mono_arch_create_jit_trampoline:
281 * @method: pointer to the method info
283 * Creates a trampoline function for virtual methods. If the created
284 * code is called it first starts JIT compilation of method,
285 * and then calls the newly created method. I also replaces the
286 * corresponding vtable entry (see sparc_magic_trampoline).
288 * Returns: a pointer to the newly created code
291 mono_arch_create_jit_trampoline (MonoMethod *method)
296 ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get ());
297 code_start = ji->code_start;
304 * mono_arch_create_class_init_trampoline:
305 * @vtable: the type to initialize
307 * Creates a trampoline function to run a type initializer.
308 * If the trampoline is called, it calls mono_runtime_class_init with the
309 * given vtable, then patches the caller code so it does not get called any
312 * Returns: a pointer to the newly created code
315 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
320 ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain);
321 code = ji->code_start;
328 * This method is only called when running in the Mono Debugger.
331 mono_debugger_create_notification_function (gpointer *notification_address)
335 ptr = buf = mono_global_codeman_reserve (16);
336 if (notification_address)
337 *notification_address = buf;
339 g_assert_not_reached ();