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