2 * tramp-ia64.c: JIT trampoline code for ia64
5 * Zoltan Varga (vargaz@gmail.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/marshal.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/mono-debug-debugger.h>
17 #include <mono/arch/ia64/ia64-codegen.h>
20 #include "mini-ia64.h"
22 #define GP_SCRATCH_REG 31
23 #define GP_SCRATCH_REG2 30
26 * mono_arch_get_unbox_trampoline:
28 * @addr: pointer to native code for @m
30 * when value type methods are called through the vtable we need to unbox the
31 * this argument. This method returns a pointer to a trampoline which does
32 * unboxing before calling the method
35 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
38 gpointer func_addr, func_gp;
39 Ia64CodegenState code;
42 MonoDomain *domain = mono_domain_get ();
44 /* FIXME: Optimize this */
46 if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
49 func_addr = ((gpointer*)addr) [0];
50 func_gp = ((gpointer*)addr) [1];
52 buf = mono_domain_code_reserve (domain, 256);
54 /* Since the this reg is a stacked register, its a bit hard to access it */
55 ia64_codegen_init (code, buf);
56 ia64_alloc (code, 40, 8, 1, 0, 0);
57 ia64_adds_imm (code, 32 + this_reg, sizeof (MonoObject), 32 + this_reg);
58 ia64_mov_to_ar_i (code, IA64_PFS, 40);
59 ia64_movl (code, GP_SCRATCH_REG, func_addr);
60 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
61 ia64_br_cond_reg (code, IA64_B6);
62 ia64_codegen_close (code);
64 g_assert (code.buf - buf < 256);
66 mono_arch_flush_icache (buf, code.buf - buf);
69 desc = g_malloc0 (sizeof (gpointer) * 2);
77 mono_arch_patch_callsite (guint8 *method_start, guint8 *code, guint8 *addr)
79 guint8 *callsite_begin;
80 guint64 *callsite = (guint64*)(gpointer)(code - 16);
82 guint64 ins, instructions [3];
85 gpointer func = ((gpointer*)(gpointer)addr)[0];
87 while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
88 (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
90 callsite_begin = (guint8*)callsite;
92 next_bundle = callsite + 2;
93 ins = ia64_bundle_ins1 (next_bundle);
94 if (ia64_ins_opcode (ins) == 5) {
95 /* ld8_inc_imm -> indirect call through a function pointer */
96 g_assert (ia64_ins_r1 (ins) == GP_SCRATCH_REG2);
97 g_assert (ia64_ins_r3 (ins) == GP_SCRATCH_REG);
101 /* Patch the code generated by emit_call */
103 instructions [0] = ia64_bundle_ins1 (callsite);
104 instructions [1] = ia64_bundle_ins2 (callsite);
105 instructions [2] = ia64_bundle_ins3 (callsite);
107 ia64_codegen_init (gen, (guint8*)buf);
108 ia64_movl (gen, GP_SCRATCH_REG, func);
109 instructions [1] = gen.instructions [0];
110 instructions [2] = gen.instructions [1];
112 ia64_codegen_init (gen, (guint8*)buf);
113 ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
114 ia64_codegen_close (gen);
116 /* This might not be safe, but not all itanium processors support st16 */
117 callsite [0] = buf [0];
118 callsite [1] = buf [1];
120 mono_arch_flush_icache (callsite_begin, code - callsite_begin);
124 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
126 g_assert_not_reached ();
130 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
132 guint8 *callsite_begin;
133 guint64 *callsite = (guint64*)(gpointer)(code - 16);
134 guint64 instructions [3];
136 Ia64CodegenState gen;
138 while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
139 (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
141 callsite_begin = (guint8*)callsite;
143 /* Replace the code generated by emit_call with a sets of nops */
145 /* The first bundle might have other instructions in it */
146 instructions [0] = ia64_bundle_ins1 (callsite);
147 instructions [1] = IA64_NOP_X;
148 instructions [2] = IA64_NOP_X;
150 ia64_codegen_init (gen, (guint8*)buf);
151 ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
152 ia64_codegen_close (gen);
154 /* This might not be safe, but not all itanium processors support st16 */
155 callsite [0] = buf [0];
156 callsite [1] = buf [1];
160 /* The other bundles can be full replaced with nops */
162 ia64_codegen_init (gen, (guint8*)buf);
163 ia64_emit_bundle_template (&gen, IA64_TEMPLATE_MII, IA64_NOP_M, IA64_NOP_I, IA64_NOP_I);
164 ia64_codegen_close (gen);
166 while ((guint8*)callsite < code) {
167 callsite [0] = buf [0];
168 callsite [1] = buf [1];
172 mono_arch_flush_icache (callsite_begin, code - callsite_begin);
176 mono_arch_nullify_plt_entry (guint8 *code, mgreg_t *regs)
178 g_assert_not_reached ();
182 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
185 int i, offset, saved_regs_offset, saved_fpregs_offset, last_offset, framesize;
186 int in0, local0, out0, l0, l1, l2, l3, l4, l5, l6, l7, l8, o0, o1, o2, o3;
188 Ia64CodegenState code;
190 unw_dyn_region_info_t *r_pro;
197 * Since jump trampolines are not patched, this trampoline is executed every
198 * time a call is made to a jump trampoline. So we try to keep things faster
201 if (tramp_type == MONO_TRAMPOLINE_JUMP)
206 buf = mono_global_codeman_reserve (2048);
208 ia64_codegen_init (code, buf);
210 /* Stacked Registers */
219 l5 = 45; /* saved ar.pfs */
222 l8 = 48; /* saved sp */
223 o0 = out0 + 0; /* regs */
224 o1 = out0 + 1; /* code */
225 o2 = out0 + 2; /* arg */
226 o3 = out0 + 3; /* tramp */
228 framesize = (128 * 8) + 1024;
229 framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
232 * Allocate a new register+memory stack frame.
233 * 8 input registers (the max used by the ABI)
235 * 4 output (number of parameters passed to trampoline)
237 ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + l5);
238 ia64_alloc (code, l5, local0 - in0, out0 - local0, 4, 0);
239 ia64_unw_save_reg (code, UNW_IA64_SP, UNW_IA64_GR + l8);
240 ia64_mov (code, l8, IA64_SP);
241 ia64_adds_imm (code, IA64_SP, (-framesize), IA64_SP);
243 offset = 16; /* scratch area */
245 /* Save the argument received from the specific trampoline */
246 ia64_mov (code, l6, GP_SCRATCH_REG);
248 /* Save the calling address */
249 ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 7);
250 ia64_mov_from_br (code, l7, IA64_B0);
252 /* Create unwind info for the prolog */
253 ia64_begin_bundle (code);
254 r_pro = mono_ia64_create_unwind_region (&code);
257 /* Not needed for jump trampolines */
258 if (tramp_type != MONO_TRAMPOLINE_JUMP) {
259 saved_regs_offset = offset;
262 * Only the registers which are needed for computing vtable slots need
266 for (i = 0; i < 64; ++i)
267 if ((1 << i) & MONO_ARCH_CALLEE_REGS) {
268 if (last_offset != i * 8)
269 ia64_adds_imm (code, l1, saved_regs_offset + (i * 8), IA64_SP);
270 ia64_st8_spill_inc_imm_hint (code, l1, i, 8, 0);
271 last_offset = (i + 1) * 8;
275 /* Save fp registers */
276 saved_fpregs_offset = offset;
278 ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
279 for (i = 0; i < 8; ++i)
280 ia64_stfd_inc_imm_hint (code, l1, i + 8, 8, 0);
282 g_assert (offset < framesize);
284 /* Arg1 is the pointer to the saved registers */
285 ia64_adds_imm (code, o0, saved_regs_offset, IA64_SP);
287 /* Arg2 is the address of the calling code */
289 ia64_mov (code, o1, l7);
291 ia64_mov (code, o1, 0);
293 /* Arg3 is the method/vtable ptr */
294 ia64_mov (code, o2, l6);
296 /* Arg4 is the trampoline address */
298 ia64_mov (code, o3, 0);
300 tramp = (guint8*)mono_get_trampoline_func (tramp_type);
302 /* Call the trampoline using an indirect call */
303 ia64_movl (code, l0, tramp);
304 ia64_ld8_inc_imm (code, l1, l0, 8);
305 ia64_mov_to_br (code, IA64_B6, l1);
306 ia64_ld8 (code, IA64_GP, l0);
307 ia64_br_call_reg (code, 0, IA64_B6);
309 /* Check for thread interruption */
310 /* This is not perf critical code so no need to check the interrupt flag */
311 ia64_mov (code, l2, IA64_R8);
313 tramp = (guint8*)mono_thread_force_interruption_checkpoint;
314 ia64_movl (code, l0, tramp);
315 ia64_ld8_inc_imm (code, l1, l0, 8);
316 ia64_mov_to_br (code, IA64_B6, l1);
317 ia64_ld8 (code, IA64_GP, l0);
318 ia64_br_call_reg (code, 0, IA64_B6);
320 ia64_mov (code, IA64_R8, l2);
322 /* Restore fp regs */
323 ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
324 for (i = 0; i < 8; ++i)
325 ia64_ldfd_inc_imm (code, i + 8, l1, 8);
327 /* FIXME: Handle NATs in fp regs / scratch regs */
329 if (tramp_type != MONO_TRAMPOLINE_CLASS_INIT) {
330 /* Load method address from function descriptor */
331 ia64_ld8 (code, l0, IA64_R8);
332 ia64_mov_to_br (code, IA64_B6, l0);
335 /* Clean up register/memory stack frame */
336 ia64_adds_imm (code, IA64_SP, framesize, IA64_SP);
337 ia64_mov_to_ar_i (code, IA64_PFS, l5);
339 if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
340 ia64_mov_ret_to_br (code, IA64_B0, l7);
341 ia64_br_ret_reg (code, IA64_B0);
344 /* Call the compiled method */
345 ia64_mov_to_br (code, IA64_B0, l7);
346 ia64_br_cond_reg (code, IA64_B6);
349 ia64_codegen_close (code);
351 g_assert ((code.buf - buf) <= 2048);
353 /* FIXME: emit unwind info for epilog */
354 di = g_malloc0 (sizeof (unw_dyn_info_t));
355 di->start_ip = (unw_word_t) buf;
356 di->end_ip = (unw_word_t) code.buf;
358 di->format = UNW_INFO_FORMAT_DYNAMIC;
359 di->u.pi.name_ptr = (unw_word_t)"ia64_generic_trampoline";
360 di->u.pi.regions = r_pro;
362 _U_dyn_register (di);
364 mono_arch_flush_icache (buf, code.buf - buf);
369 #define TRAMPOLINE_SIZE 128
372 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
376 Ia64CodegenState code;
378 tramp = mono_get_trampoline_code (tramp_type);
380 buf = mono_domain_code_reserve (domain, TRAMPOLINE_SIZE);
382 /* FIXME: Optimize this */
384 ia64_codegen_init (code, buf);
386 ia64_movl (code, GP_SCRATCH_REG, arg1);
388 ia64_begin_bundle (code);
389 disp = (tramp - code.buf) >> 4;
390 if (ia64_is_imm21 (disp)) {
391 ia64_br_cond (code, disp);
394 ia64_movl (code, GP_SCRATCH_REG2, tramp);
395 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
396 ia64_br_cond_reg (code, IA64_B6);
399 ia64_codegen_close (code);
401 g_assert (code.buf - buf <= TRAMPOLINE_SIZE);
403 mono_arch_flush_icache (buf, code.buf - buf);
406 *code_len = code.buf - buf;
412 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
418 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
420 /* FIXME: implement! */
421 g_assert_not_reached ();