Merge pull request #1709 from atsushieno/import-codedom-core
[mono.git] / mono / mini / tramp-ia64.c
1 /*
2  * tramp-ia64.c: JIT trampoline code for ia64
3  *
4  * Authors:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
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>
18
19 #include "mini.h"
20 #include "mini-ia64.h"
21
22 #define GP_SCRATCH_REG 31
23 #define GP_SCRATCH_REG2 30
24
25 /*
26  * mono_arch_get_unbox_trampoline:
27  * @m: method pointer
28  * @addr: pointer to native code for @m
29  *
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
33  */
34 gpointer
35 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
36 {
37         guint8 *buf;
38         gpointer func_addr, func_gp;
39         Ia64CodegenState code;
40         int this_reg = 0;
41         gpointer *desc;
42         MonoDomain *domain = mono_domain_get ();
43
44         /* FIXME: Optimize this */
45
46         func_addr = ((gpointer*)addr) [0];
47         func_gp = ((gpointer*)addr) [1];
48
49         buf = mono_domain_code_reserve (domain, 256);
50
51         /* Since the this reg is a stacked register, its a bit hard to access it */
52         ia64_codegen_init (code, buf);
53         ia64_alloc (code, 40, 8, 1, 0, 0);
54         ia64_adds_imm (code, 32 + this_reg, sizeof (MonoObject), 32 + this_reg);
55         ia64_mov_to_ar_i (code, IA64_PFS, 40);  
56         ia64_movl (code, GP_SCRATCH_REG, func_addr);
57         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
58         ia64_br_cond_reg (code, IA64_B6);
59         ia64_codegen_close (code);
60
61         g_assert (code.buf - buf < 256);
62
63         mono_arch_flush_icache (buf, code.buf - buf);
64
65         /* FIXME: */
66         desc = g_malloc0 (sizeof (gpointer) * 2);
67         desc [0] = buf;
68         desc [1] = func_gp;
69
70         return desc;
71 }
72
73 void
74 mono_arch_patch_callsite (guint8 *method_start, guint8 *code, guint8 *addr)
75 {
76         guint8 *callsite_begin;
77         guint64 *callsite = (guint64*)(gpointer)(code - 16);
78         guint64 *next_bundle;
79         guint64 ins, instructions [3];
80         guint64 buf [16];
81         Ia64CodegenState gen;
82         gpointer func = ((gpointer*)(gpointer)addr)[0];
83
84         while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
85                    (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
86                 callsite -= 2;
87         callsite_begin = (guint8*)callsite;
88
89         next_bundle = callsite + 2;
90         ins = ia64_bundle_ins1 (next_bundle);
91         if (ia64_ins_opcode (ins) == 5) {
92                 /* ld8_inc_imm -> indirect call through a function pointer */
93                 g_assert (ia64_ins_r1 (ins) == GP_SCRATCH_REG2);
94                 g_assert (ia64_ins_r3 (ins) == GP_SCRATCH_REG);
95                 return;
96         }
97
98         /* Patch the code generated by emit_call */
99
100         instructions [0] = ia64_bundle_ins1 (callsite);
101         instructions [1] = ia64_bundle_ins2 (callsite);
102         instructions [2] = ia64_bundle_ins3 (callsite);
103
104         ia64_codegen_init (gen, (guint8*)buf);
105         ia64_movl (gen, GP_SCRATCH_REG, func);
106         instructions [1] = gen.instructions [0];
107         instructions [2] = gen.instructions [1];
108
109         ia64_codegen_init (gen, (guint8*)buf);
110         ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
111         ia64_codegen_close (gen);
112
113         /* This might not be safe, but not all itanium processors support st16 */
114         callsite [0] = buf [0];
115         callsite [1] = buf [1];
116
117         mono_arch_flush_icache (callsite_begin, code - callsite_begin);
118 }
119
120 void
121 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
122 {
123         g_assert_not_reached ();
124 }
125
126 void
127 mono_arch_nullify_class_init_trampoline (guint8 *code, mgreg_t *regs)
128 {
129         guint8 *callsite_begin;
130         guint64 *callsite = (guint64*)(gpointer)(code - 16);
131         guint64 instructions [3];
132         guint64 buf [16];
133         Ia64CodegenState gen;
134
135         while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
136                    (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
137                 callsite -= 2;
138         callsite_begin = (guint8*)callsite;
139
140         /* Replace the code generated by emit_call with a sets of nops */
141
142         /* The first bundle might have other instructions in it */
143         instructions [0] = ia64_bundle_ins1 (callsite);
144         instructions [1] = IA64_NOP_X;
145         instructions [2] = IA64_NOP_X;
146
147         ia64_codegen_init (gen, (guint8*)buf);
148         ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
149         ia64_codegen_close (gen);
150
151         /* This might not be safe, but not all itanium processors support st16 */
152         callsite [0] = buf [0];
153         callsite [1] = buf [1];
154
155         callsite += 2;
156
157         /* The other bundles can be full replaced with nops */
158
159         ia64_codegen_init (gen, (guint8*)buf);
160         ia64_emit_bundle_template (&gen, IA64_TEMPLATE_MII, IA64_NOP_M, IA64_NOP_I, IA64_NOP_I);
161         ia64_codegen_close (gen);
162
163         while ((guint8*)callsite < code) {
164                 callsite [0] = buf [0];
165                 callsite [1] = buf [1];
166                 callsite += 2;
167         }
168
169         mono_arch_flush_icache (callsite_begin, code - callsite_begin);
170 }
171
172 guchar*
173 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type, MonoTrampInfo **info, gboolean aot)
174 {
175         guint8 *buf, *tramp;
176         int i, offset, saved_regs_offset, saved_fpregs_offset, last_offset, framesize;
177         int in0, local0, out0, l0, l1, l2, l3, l4, l5, l6, l7, l8, o0, o1, o2, o3;
178         gboolean has_caller;
179         Ia64CodegenState code;
180         unw_dyn_info_t *di;
181         unw_dyn_region_info_t *r_pro;
182
183         g_assert (!aot);
184         *info = NULL;
185
186         /* 
187          * Since jump trampolines are not patched, this trampoline is executed every
188          * time a call is made to a jump trampoline. So we try to keep things faster
189          * in that case.
190          */
191         if (tramp_type == MONO_TRAMPOLINE_JUMP)
192                 has_caller = FALSE;
193         else
194                 has_caller = TRUE;
195
196         buf = mono_global_codeman_reserve (2048);
197
198         ia64_codegen_init (code, buf);
199
200         /* Stacked Registers */
201         in0 = 32;
202         local0 = in0 + 8;
203         out0 = local0 + 16;
204         l0 = 40;
205         l1 = 41;
206         l2 = 42;
207         l3 = 43;
208         l4 = 44;
209         l5 = 45; /* saved ar.pfs */
210         l6 = 46; /* arg */
211         l7 = 47; /* code */
212         l8 = 48; /* saved sp */
213         o0 = out0 + 0; /* regs */
214         o1 = out0 + 1; /* code */
215         o2 = out0 + 2; /* arg */
216         o3 = out0 + 3; /* tramp */
217
218         framesize = (128 * 8) + 1024;
219         framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
220
221         /*
222          * Allocate a new register+memory stack frame.
223          * 8 input registers (the max used by the ABI)
224          * 16 locals
225          * 4 output (number of parameters passed to trampoline)
226          */
227         ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + l5);
228         ia64_alloc (code, l5, local0 - in0, out0 - local0, 4, 0);
229         ia64_unw_save_reg (code, UNW_IA64_SP, UNW_IA64_GR + l8);
230         ia64_mov (code, l8, IA64_SP);
231         ia64_adds_imm (code, IA64_SP, (-framesize), IA64_SP);
232
233         offset = 16; /* scratch area */
234
235         /* Save the argument received from the specific trampoline */
236         ia64_mov (code, l6, GP_SCRATCH_REG);
237
238         /* Save the calling address */
239         ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 7);
240         ia64_mov_from_br (code, l7, IA64_B0);
241
242         /* Create unwind info for the prolog */
243         ia64_begin_bundle (code);
244         r_pro = mono_ia64_create_unwind_region (&code);
245
246         /* Save registers */
247         /* Not needed for jump trampolines */
248         if (tramp_type != MONO_TRAMPOLINE_JUMP) {
249                 saved_regs_offset = offset;
250                 offset += 128 * 8;
251                 /* 
252                  * Only the registers which are needed for computing vtable slots need
253                  * to be saved.
254                  */
255                 last_offset = -1;
256                 for (i = 0; i < 64; ++i)
257                         if ((1 << i) & MONO_ARCH_CALLEE_REGS) {
258                                 if (last_offset != i * 8)
259                                         ia64_adds_imm (code, l1, saved_regs_offset + (i * 8), IA64_SP);
260                                 ia64_st8_spill_inc_imm_hint (code, l1, i, 8, 0);
261                                 last_offset = (i + 1) * 8;
262                         }
263         }
264
265         /* Save fp registers */
266         saved_fpregs_offset = offset;
267         offset += 8 * 8;
268         ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
269         for (i = 0; i < 8; ++i)
270                 ia64_stfd_inc_imm_hint (code, l1, i + 8, 8, 0);
271
272         g_assert (offset < framesize);
273
274         /* Arg1 is the pointer to the saved registers */
275         ia64_adds_imm (code, o0, saved_regs_offset, IA64_SP);
276
277         /* Arg2 is the address of the calling code */
278         if (has_caller)
279                 ia64_mov (code, o1, l7);
280         else
281                 ia64_mov (code, o1, 0);
282
283         /* Arg3 is the method/vtable ptr */
284         ia64_mov (code, o2, l6);
285
286         /* Arg4 is the trampoline address */
287         /* FIXME: */
288         ia64_mov (code, o3, 0);
289
290         tramp = (guint8*)mono_get_trampoline_func (tramp_type);
291
292         /* Call the trampoline using an indirect call */
293         ia64_movl (code, l0, tramp);
294         ia64_ld8_inc_imm (code, l1, l0, 8);
295         ia64_mov_to_br (code, IA64_B6, l1);
296         ia64_ld8 (code, IA64_GP, l0);
297         ia64_br_call_reg (code, 0, IA64_B6);
298
299         /* Check for thread interruption */
300         /* This is not perf critical code so no need to check the interrupt flag */
301         ia64_mov (code, l2, IA64_R8);
302
303         tramp = (guint8*)mono_thread_force_interruption_checkpoint;
304         ia64_movl (code, l0, tramp);
305         ia64_ld8_inc_imm (code, l1, l0, 8);
306         ia64_mov_to_br (code, IA64_B6, l1);
307         ia64_ld8 (code, IA64_GP, l0);
308         ia64_br_call_reg (code, 0, IA64_B6);
309
310         ia64_mov (code, IA64_R8, l2);
311
312         /* Restore fp regs */
313         ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
314         for (i = 0; i < 8; ++i)
315                 ia64_ldfd_inc_imm (code, i + 8, l1, 8);
316
317         /* FIXME: Handle NATs in fp regs / scratch regs */
318
319         if (tramp_type != MONO_TRAMPOLINE_CLASS_INIT) {
320                 /* Load method address from function descriptor */
321                 ia64_ld8 (code, l0, IA64_R8);
322                 ia64_mov_to_br (code, IA64_B6, l0);
323         }
324
325         /* Clean up register/memory stack frame */
326         ia64_adds_imm (code, IA64_SP, framesize, IA64_SP);
327         ia64_mov_to_ar_i (code, IA64_PFS, l5);
328
329         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
330                 ia64_mov_ret_to_br (code, IA64_B0, l7);
331                 ia64_br_ret_reg (code, IA64_B0);
332         }
333         else {
334                 /* Call the compiled method */
335                 ia64_mov_to_br (code, IA64_B0, l7);
336                 ia64_br_cond_reg (code, IA64_B6);
337         }
338
339         ia64_codegen_close (code);
340
341         g_assert ((code.buf - buf) <= 2048);
342
343         /* FIXME: emit unwind info for epilog */
344         di = g_malloc0 (sizeof (unw_dyn_info_t));
345         di->start_ip = (unw_word_t) buf;
346         di->end_ip = (unw_word_t) code.buf;
347         di->gp = 0;
348         di->format = UNW_INFO_FORMAT_DYNAMIC;
349         di->u.pi.name_ptr = (unw_word_t)"ia64_generic_trampoline";
350         di->u.pi.regions = r_pro;
351
352         _U_dyn_register (di);
353
354         mono_arch_flush_icache (buf, code.buf - buf);
355
356         return buf;
357 }
358
359 #define TRAMPOLINE_SIZE 128
360
361 gpointer
362 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
363 {
364         guint8 *buf, *tramp;
365         gint64 disp;
366         Ia64CodegenState code;
367
368         tramp = mono_get_trampoline_code (tramp_type);
369
370         buf = mono_domain_code_reserve (domain, TRAMPOLINE_SIZE);
371
372         /* FIXME: Optimize this */
373
374         ia64_codegen_init (code, buf);
375
376         ia64_movl (code, GP_SCRATCH_REG, arg1);
377
378         ia64_begin_bundle (code);
379         disp = (tramp - code.buf) >> 4;
380         if (ia64_is_imm21 (disp)) {
381                 ia64_br_cond (code, disp);
382         }
383         else {
384                 ia64_movl (code, GP_SCRATCH_REG2, tramp);
385                 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
386                 ia64_br_cond_reg (code, IA64_B6);
387         }
388
389         ia64_codegen_close (code);
390
391         g_assert (code.buf - buf <= TRAMPOLINE_SIZE);
392
393         mono_arch_flush_icache (buf, code.buf - buf);
394
395         if (code_len)
396                 *code_len = code.buf - buf;
397
398         return buf;
399 }
400
401 void
402 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
403 {
404         NOT_IMPLEMENTED;
405 }
406
407 gpointer
408 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
409 {
410         /* FIXME: implement! */
411         g_assert_not_reached ();
412         return NULL;
413 }