2 * tramp-sparc.c: JIT trampoline code for Sparc
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>
20 #include "mini-sparc.h"
23 * mono_arch_get_unbox_trampoline:
24 * @gsctx: the generic sharing context
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 mono_arch_get_unbox_trampoline (MonoGenericSharingContext
*gsctx
, MonoMethod
*m
, gpointer addr
)
36 int this_pos
= 4, reg
;
38 if (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 mono_arch_patch_callsite (guint8
*method_start
, guint8
*code
, guint8
*addr
)
64 if (sparc_inst_op (*(guint32
*)code
) == 0x1) {
65 sparc_call_simple (code
, (guint8
*)addr
- (guint8
*)code
);
70 mono_arch_patch_plt_entry (guint8
*code
, gpointer
*got
, mgreg_t
*regs
, guint8
*addr
)
72 g_assert_not_reached ();
76 mono_arch_nullify_class_init_trampoline (guint8
*code
, mgreg_t
*regs
)
78 /* Patch calling code */
83 mono_arch_nullify_plt_entry (guint8
*code
, mgreg_t
*regs
)
85 g_assert_not_reached ();
88 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
91 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type
)
93 guint8
*buf
, *code
, *tramp_addr
;
94 guint32 lmf_offset
, regs_offset
, method_reg
, i
;
97 if (tramp_type
== MONO_TRAMPOLINE_JUMP
)
102 code
= buf
= mono_global_codeman_reserve (1024);
104 sparc_save_imm (code
, sparc_sp
, -1608, sparc_sp
);
107 method_reg
= sparc_g4
;
109 method_reg
= sparc_g1
;
112 regs_offset
= MONO_SPARC_STACK_BIAS
+ 1000;
114 /* Save r1 needed by the IMT code */
115 sparc_sti_imm (code
, sparc_g1
, sparc_sp
, regs_offset
+ (sparc_g1
* sizeof (gpointer
)));
118 * sparc_g5 contains the return address, the trampoline argument is stored in the
119 * instruction stream after the call.
121 sparc_ld_imm (code
, sparc_g5
, 8, method_reg
);
124 /* Save fp regs since they are not preserved by calls */
125 for (i
= 0; i
< 16; i
++)
126 sparc_stdf_imm (code
, sparc_f0
+ (i
* 2), sparc_sp
, MONO_SPARC_STACK_BIAS
+ 320 + (i
* 8));
129 /* We receive the method address in %r1, so save it here */
130 sparc_sti_imm (code
, method_reg
, sparc_sp
, MONO_SPARC_STACK_BIAS
+ 200);
132 /* Save lmf since compilation can raise exceptions */
133 lmf_offset
= MONO_SPARC_STACK_BIAS
- sizeof (MonoLMF
);
135 /* Save the data for the parent (managed) frame */
138 sparc_sti_imm (code
, sparc_i7
, sparc_fp
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, ip
));
140 sparc_sti_imm (code
, sparc_fp
, sparc_fp
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, sp
));
142 /* Load previous fp from the saved register window */
144 sparc_ldi_imm (code
, sparc_fp
, MONO_SPARC_STACK_BIAS
+ (sparc_i6
- 16) * sizeof (gpointer
), sparc_o7
);
145 sparc_sti_imm (code
, sparc_o7
, sparc_fp
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, ebp
));
147 sparc_sti_imm (code
, method_reg
, sparc_fp
, lmf_offset
+ G_STRUCT_OFFSET (MonoLMF
, method
));
149 sparc_set (code
, mono_get_lmf_addr
, sparc_o7
);
150 sparc_jmpl (code
, sparc_o7
, sparc_g0
, sparc_o7
);
153 code
= mono_sparc_emit_save_lmf (code
, lmf_offset
);
156 /* Load all registers of the caller into a table inside this frame */
157 /* first the out registers */
158 for (i
= 0; i
< 8; ++i
)
159 sparc_sti_imm (code
, sparc_i0
+ i
, sparc_sp
, regs_offset
+ ((sparc_o0
+ i
) * sizeof (gpointer
)));
160 /* then the in+local registers */
161 for (i
= 0; i
< 16; i
++) {
162 sparc_ldi_imm (code
, sparc_fp
, MONO_SPARC_STACK_BIAS
+ (i
* sizeof (gpointer
)), sparc_o7
);
163 sparc_sti_imm (code
, sparc_o7
, sparc_sp
, regs_offset
+ ((sparc_l0
+ i
) * sizeof (gpointer
)));
167 tramp_addr
= mono_get_trampoline_func (tramp_type
);
168 sparc_ldi_imm (code
, sparc_sp
, MONO_SPARC_STACK_BIAS
+ 200, sparc_o2
);
169 /* pass address of register table as third argument */
170 sparc_add_imm (code
, FALSE
, sparc_sp
, regs_offset
, sparc_o0
);
171 sparc_set (code
, tramp_addr
, sparc_o7
);
172 /* set %o1 to caller address */
174 sparc_mov_reg_reg (code
, sparc_i7
, sparc_o1
);
176 sparc_set (code
, 0, sparc_o1
);
177 sparc_set (code
, 0, sparc_o3
);
178 sparc_jmpl (code
, sparc_o7
, sparc_g0
, sparc_o7
);
182 sparc_sti_imm (code
, sparc_o0
, sparc_sp
, MONO_SPARC_STACK_BIAS
+ 304);
184 /* Check for thread interruption */
185 sparc_set (code
, (guint8
*)mono_thread_force_interruption_checkpoint
, sparc_o7
);
186 sparc_jmpl (code
, sparc_o7
, sparc_g0
, sparc_o7
);
190 code
= mono_sparc_emit_restore_lmf (code
, lmf_offset
);
193 sparc_ldi_imm (code
, sparc_sp
, MONO_SPARC_STACK_BIAS
+ 304, sparc_o0
);
197 for (i
= 0; i
< 16; i
++)
198 sparc_lddf_imm (code
, sparc_sp
, MONO_SPARC_STACK_BIAS
+ 320 + (i
* 8), sparc_f0
+ (i
* 2));
201 if (tramp_type
== MONO_TRAMPOLINE_CLASS_INIT
)
204 sparc_jmpl (code
, sparc_o0
, sparc_g0
, sparc_g0
);
206 /* restore previous frame in delay slot */
207 sparc_restore_simple (code
);
213 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
215 sparc_call_simple (code, 16);
217 sparc_rett_simple (code);
220 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
222 tramp_addr = &sparc_magic_trampoline;
223 sparc_call_simple (code, tramp_addr - code);
225 sparc_rett_simple (code);
230 g_assert ((code
- buf
) <= 512);
232 mono_arch_flush_icache (buf
, code
- buf
);
237 #define TRAMPOLINE_SIZE (SPARC_SET_MAX_SIZE + 3)
240 mono_arch_create_specific_trampoline (gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, guint32
*code_len
)
242 guint32
*code
, *buf
, *tramp
;
244 tramp
= mono_get_trampoline_code (tramp_type
);
246 code
= buf
= mono_domain_code_reserve (domain
, TRAMPOLINE_SIZE
* 4);
248 /* We have to use g5 here because there is no other free register */
249 sparc_set (code
, tramp
, sparc_g5
);
250 sparc_jmpl (code
, sparc_g5
, sparc_g0
, sparc_g5
);
253 g_assert_not_reached ();
255 *code
= (guint32
)arg1
;
259 g_assert ((code
- buf
) <= TRAMPOLINE_SIZE
);
262 *code_len
= (code
- buf
) * 4;
264 mono_jit_stats
.method_trampolines
++;
266 mono_arch_flush_icache ((guint8
*)buf
, (code
- buf
) * 4);
272 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset
)
274 /* FIXME: implement! */
275 g_assert_not_reached ();