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