In mono/metadata:
[mono.git] / mono / mini / tramp-sparc.c
blob0fae9ac9b6aa6f1fe04aaea8f8c4f487956b7c11
1 /*
2 * tramp-sparc.c: JIT trampoline code for Sparc
4 * Authors:
5 * Mark Crichton (crichton@gimp.org)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
9 */
11 #include <config.h>
12 #include <glib.h>
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>
19 #include "mini.h"
20 #include "mini-sparc.h"
23 * mono_arch_get_unbox_trampoline:
24 * @gsctx: the generic sharing context
25 * @m: method pointer
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
32 gpointer
33 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
35 guint8 *code, *start;
36 int this_pos = 4, reg;
38 if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
39 this_pos = 8;
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);
45 #ifdef SPARCV9
46 reg = sparc_g4;
47 #else
48 reg = sparc_g1;
49 #endif
50 sparc_set (code, addr, reg);
51 sparc_jmpl (code, reg, sparc_g0, sparc_g0);
52 sparc_nop (code);
54 g_assert ((code - start) <= 36);
56 mono_arch_flush_icache (start, code - start);
58 return start;
61 void
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);
69 void
70 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
72 g_assert_not_reached ();
75 void
76 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
78 /* Patch calling code */
79 sparc_nop (code);
82 void
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))
90 guchar*
91 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
93 guint8 *buf, *code, *tramp_addr;
94 guint32 lmf_offset, regs_offset, method_reg, i;
95 gboolean has_caller;
97 if (tramp_type == MONO_TRAMPOLINE_JUMP)
98 has_caller = FALSE;
99 else
100 has_caller = TRUE;
102 code = buf = mono_global_codeman_reserve (1024);
104 sparc_save_imm (code, sparc_sp, -1608, sparc_sp);
106 #ifdef SPARCV9
107 method_reg = sparc_g4;
108 #else
109 method_reg = sparc_g1;
110 #endif
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);
123 #ifdef SPARCV9
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));
127 #endif
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 */
137 /* Save ip */
138 sparc_sti_imm (code, sparc_i7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
139 /* Save sp */
140 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
141 /* Save fp */
142 /* Load previous fp from the saved register window */
143 sparc_flushw (code);
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));
146 /* Save method */
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);
151 sparc_nop (code);
153 code = mono_sparc_emit_save_lmf (code, lmf_offset);
155 if (has_caller) {
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 */
173 if (has_caller)
174 sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
175 else
176 sparc_set (code, 0, sparc_o1);
177 sparc_set (code, 0, sparc_o3);
178 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
179 sparc_nop (code);
181 /* Save result */
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);
187 sparc_nop (code);
189 /* Restore lmf */
190 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
192 /* Reload result */
193 sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 304, sparc_o0);
195 #ifdef SPARCV9
196 /* Reload fp regs */
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));
199 #endif
201 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
202 sparc_ret (code);
203 else
204 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
206 /* restore previous frame in delay slot */
207 sparc_restore_simple (code);
211 gpointer addr;
213 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
214 addr = code;
215 sparc_call_simple (code, 16);
216 sparc_nop (code);
217 sparc_rett_simple (code);
218 sparc_nop (code);
220 sparc_save_imm (code, sparc_sp, -608, sparc_sp);
221 sparc_ta (code, 1);
222 tramp_addr = &sparc_magic_trampoline;
223 sparc_call_simple (code, tramp_addr - code);
224 sparc_nop (code);
225 sparc_rett_simple (code);
226 sparc_nop (code);
230 g_assert ((code - buf) <= 512);
232 mono_arch_flush_icache (buf, code - buf);
234 return buf;
237 #define TRAMPOLINE_SIZE (SPARC_SET_MAX_SIZE + 3)
239 gpointer
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);
251 sparc_nop (code);
252 #ifdef SPARCV9
253 g_assert_not_reached ();
254 #else
255 *code = (guint32)arg1;
256 code ++;
257 #endif
259 g_assert ((code - buf) <= TRAMPOLINE_SIZE);
261 if (code_len)
262 *code_len = (code - buf) * 4;
264 mono_jit_stats.method_trampolines++;
266 mono_arch_flush_icache ((guint8*)buf, (code - buf) * 4);
268 return buf;
271 gpointer
272 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 encoded_offset)
274 /* FIXME: implement! */
275 g_assert_not_reached ();
276 return NULL;