2005-12-12 Zoltan Varga <vargaz@gmail.com>
[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 #include <mono/metadata/mono-debug-debugger.h>
18
19 #include "mini.h"
20 #include "mini-ia64.h"
21
22 #define NOT_IMPLEMENTED g_assert_not_reached ()
23
24 #define GP_SCRATCH_REG 31
25 #define GP_SCRATCH_REG2 30
26
27 /*
28  * mono_arch_get_unbox_trampoline:
29  * @m: method pointer
30  * @addr: pointer to native code for @m
31  *
32  * when value type methods are called through the vtable we need to unbox the
33  * this argument. This method returns a pointer to a trampoline which does
34  * unboxing before calling the method
35  */
36 gpointer
37 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
38 {
39         guint8 *buf;
40         gpointer func_addr, func_gp;
41         Ia64CodegenState code;
42         int this_reg = 0;
43         MonoDomain *domain = mono_domain_get ();
44
45         /* FIXME: Optimize this */
46
47         if (!mono_method_signature (m)->ret->byref && MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
48                 this_reg = 1;
49
50         func_addr = ((gpointer*)addr) [0];
51         func_gp = ((gpointer*)addr) [1];
52
53         mono_domain_lock (domain);
54         buf = mono_code_manager_reserve (domain->code_mp, 256);
55         mono_domain_unlock (domain);
56
57         /* Since the this reg is a stacked register, its a bit hard to access it */
58         ia64_codegen_init (code, buf);
59         ia64_alloc (code, 40, 8, 1, 0, 0);
60         ia64_adds_imm (code, 32 + this_reg, sizeof (MonoObject), 32 + this_reg);
61         ia64_mov_to_ar_i (code, IA64_PFS, 40);  
62         ia64_movl (code, GP_SCRATCH_REG, func_addr);
63         ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG);
64         ia64_br_cond_reg (code, IA64_B6);
65         ia64_codegen_close (code);
66
67         g_assert (code.buf - buf < 256);
68
69         /* FIXME: */
70
71         gpointer *desc = g_malloc0 (sizeof (gpointer) * 2);
72         desc [0] = buf;
73         desc [1] = func_gp;
74
75         return desc;
76 }
77
78 void
79 mono_arch_patch_callsite (guint8 *code, guint8 *addr)
80 {
81         guint8 *callsite_begin;
82         guint64 *callsite = (guint64*)(gpointer)(code - 16);
83         guint64 instructions [3];
84         guint64 buf [16];
85         Ia64CodegenState gen;
86         gpointer func = ((gpointer*)(gpointer)addr)[0];
87
88         while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
89                    (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
90                 callsite -= 2;
91         callsite_begin = (guint8*)callsite;
92
93         /* Patch the code generated by emit_call */
94
95         instructions [0] = ia64_bundle_ins1 (callsite);
96         instructions [1] = ia64_bundle_ins2 (callsite);
97         instructions [2] = ia64_bundle_ins3 (callsite);
98
99         ia64_codegen_init (gen, (guint8*)buf);
100         ia64_movl (gen, GP_SCRATCH_REG, func);
101         instructions [1] = gen.instructions [0];
102         instructions [2] = gen.instructions [1];
103
104         ia64_codegen_init (gen, (guint8*)buf);
105         ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
106         ia64_codegen_close (gen);
107
108         /* This might not be safe, but not all itanium processors support st16 */
109         callsite [0] = buf [0];
110         callsite [1] = buf [1];
111
112         mono_arch_flush_icache (callsite_begin, code - callsite_begin);
113 }
114
115 void
116 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
117 {
118         guint8 *callsite_begin;
119         guint64 *callsite = (guint64*)(gpointer)(code - 16);
120         guint64 instructions [3];
121         guint64 buf [16];
122         Ia64CodegenState gen;
123
124         while ((ia64_bundle_template (callsite) != IA64_TEMPLATE_MLX) &&
125                    (ia64_bundle_template (callsite) != IA64_TEMPLATE_MLXS))
126                 callsite -= 2;
127         callsite_begin = (guint8*)callsite;
128
129         /* Replace the code generated by emit_call with a sets of nops */
130
131         /* The first bundle might have other instructions in it */
132         instructions [0] = ia64_bundle_ins1 (callsite);
133         instructions [1] = IA64_NOP_X;
134         instructions [2] = IA64_NOP_X;
135
136         ia64_codegen_init (gen, (guint8*)buf);
137         ia64_emit_bundle_template (&gen, ia64_bundle_template (callsite), instructions [0], instructions [1], instructions [2]);
138         ia64_codegen_close (gen);
139
140         /* This might not be safe, but not all itanium processors support st16 */
141         callsite [0] = buf [0];
142         callsite [1] = buf [1];
143
144         callsite += 2;
145
146         /* The other bundles can be full replaced with nops */
147
148         ia64_codegen_init (gen, (guint8*)buf);
149         ia64_emit_bundle_template (&gen, IA64_TEMPLATE_MII, IA64_NOP_M, IA64_NOP_I, IA64_NOP_I);
150         ia64_codegen_close (gen);
151
152         while ((guint8*)callsite < code) {
153                 callsite [0] = buf [0];
154                 callsite [1] = buf [1];
155                 callsite += 2;
156         }
157
158         mono_arch_flush_icache (callsite_begin, code - callsite_begin);
159 }
160
161 void
162 mono_arch_patch_delegate_trampoline (guint8 *code, guint8 *tramp, gssize *regs, guint8 *addr)
163 {
164         /* 
165          * This is called by the code generated by OP_CALL_REG:
166          * ld8 r30=[r8],8
167          * nop.i 0x0;;
168          * mov.sptk b6=r30
169          * ld8 r1=[r8]
170          * br.call.sptk.few b0=b6
171          */
172
173         /* We patch the function descriptor instead of delegate->method_ptr */
174         //g_assert (((gpointer*)(regs [8] - 8))[0] == tramp);
175         ((gpointer*)(regs [8] - 8))[0] = mono_get_addr_from_ftnptr (addr);
176         ((gpointer*)(regs [8] - 8))[1] = NULL;
177 }
178
179 guchar*
180 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
181 {
182         guint8 *buf, *tramp;
183         int i, offset, saved_regs_offset, saved_fpregs_offset, last_offset, framesize;
184         int in0, local0, out0, l0, l1, l2, l3, l4, l5, l6, l7, l8, o0, o1, o2, o3;
185         gboolean has_caller;
186         Ia64CodegenState code;
187         unw_dyn_info_t *di;
188         unw_dyn_region_info_t *r_pro;
189
190         /* 
191          * Since jump trampolines are not patched, this trampoline is executed every
192          * time a call is made to a jump trampoline. So we try to keep things faster
193          * in that case.
194          */
195         if (tramp_type == MONO_TRAMPOLINE_JUMP)
196                 has_caller = FALSE;
197         else
198                 has_caller = TRUE;
199
200         buf = mono_global_codeman_reserve (2048);
201
202         ia64_codegen_init (code, buf);
203
204         /* FIXME: Save/restore lmf */
205
206         /* Stacked Registers */
207         in0 = 32;
208         local0 = in0 + 8;
209         out0 = local0 + 16;
210         l0 = 40;
211         l1 = 41;
212         l2 = 42;
213         l3 = 43;
214         l4 = 44;
215         l5 = 45; /* saved ar.pfs */
216         l6 = 46; /* arg */
217         l7 = 47; /* code */
218         l8 = 48; /* saved sp */
219         o0 = out0 + 0; /* regs */
220         o1 = out0 + 1; /* code */
221         o2 = out0 + 2; /* arg */
222         o3 = out0 + 3; /* tramp */
223
224         framesize = (128 * 8) + 1024;
225         framesize = (framesize + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~ (MONO_ARCH_FRAME_ALIGNMENT - 1);
226
227         /*
228          * Allocate a new register+memory stack frame.
229          * 8 input registers (the max used by the ABI)
230          * 16 locals
231          * 4 output (number of parameters passed to trampoline)
232          */
233         ia64_unw_save_reg (code, UNW_IA64_AR_PFS, UNW_IA64_GR + l5);
234         ia64_alloc (code, l5, local0 - in0, out0 - local0, 4, 0);
235         ia64_unw_save_reg (code, UNW_IA64_SP, UNW_IA64_GR + l8);
236         ia64_mov (code, l8, IA64_SP);
237         ia64_adds_imm (code, IA64_SP, (-framesize), IA64_SP);
238
239         offset = 16; /* scratch area */
240
241         /* Save the argument received from the specific trampoline */
242         ia64_mov (code, l6, GP_SCRATCH_REG);
243
244         /* Save the calling address */
245         ia64_unw_save_reg (code, UNW_IA64_RP, UNW_IA64_GR + local0 + 7);
246         ia64_mov_from_br (code, l7, IA64_B0);
247
248         /* Create unwind info for the prolog */
249         ia64_begin_bundle (code);
250         r_pro = mono_ia64_create_unwind_region (&code);
251
252         /* Save registers */
253         /* Not needed for jump trampolines */
254         if (tramp_type != MONO_TRAMPOLINE_JUMP) {
255                 saved_regs_offset = offset;
256                 offset += 128 * 8;
257                 /* 
258                  * Only the registers which are needed for computing vtable slots need
259                  * to be saved.
260                  */
261                 last_offset = -1;
262                 for (i = 0; i < 64; ++i)
263                         if ((1 << i) & MONO_ARCH_CALLEE_REGS) {
264                                 if (last_offset != i * 8)
265                                         ia64_adds_imm (code, l1, saved_regs_offset + (i * 8), IA64_SP);
266                                 ia64_st8_spill_inc_imm_hint (code, l1, i, 8, 0);
267                                 last_offset = (i + 1) * 8;
268                         }
269         }
270
271         /* Save fp registers */
272         saved_fpregs_offset = offset;
273         offset += 8 * 8;
274         ia64_adds_imm (code, l1, saved_fpregs_offset, IA64_SP);
275         for (i = 0; i < 8; ++i)
276                 ia64_stfd_inc_imm_hint (code, l1, i + 8, 8, 0);
277
278         g_assert (offset < framesize);
279
280         /* Arg1 is the pointer to the saved registers */
281         ia64_adds_imm (code, o0, saved_regs_offset, IA64_SP);
282
283         /* Arg2 is the address of the calling code */
284         if (has_caller)
285                 ia64_mov (code, o1, l7);
286         else
287                 ia64_mov (code, o1, 0);
288
289         /* Arg3 is the method/vtable ptr */
290         ia64_mov (code, o2, l6);
291
292         /* Arg4 is the trampoline address */
293         /* FIXME: */
294         ia64_mov (code, o3, 0);
295
296         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
297                 tramp = (guint8*)mono_class_init_trampoline;
298         else if (tramp_type == MONO_TRAMPOLINE_AOT)
299                 tramp = (guint8*)mono_aot_trampoline;
300         else if (tramp_type == MONO_TRAMPOLINE_DELEGATE)
301                 tramp = (guint8*)mono_delegate_trampoline;
302         else
303                 tramp = (guint8*)mono_magic_trampoline;
304
305         /* Call the trampoline using an indirect call */
306         ia64_movl (code, l0, tramp);
307         ia64_ld8_inc_imm (code, l1, l0, 8);
308         ia64_mov_to_br (code, IA64_B6, l1);
309         ia64_ld8 (code, IA64_GP, l0);
310         ia64_br_call_reg (code, 0, IA64_B6);
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         mono_domain_lock (domain);
371         buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE);
372         mono_domain_unlock (domain);
373
374         /* FIXME: Optimize this */
375
376         ia64_codegen_init (code, buf);
377
378         ia64_movl (code, GP_SCRATCH_REG, arg1);
379
380         ia64_begin_bundle (code);
381         disp = (tramp - code.buf) >> 4;
382         if (ia64_is_imm21 (disp)) {
383                 ia64_br_cond (code, disp);
384         }
385         else {
386                 ia64_movl (code, GP_SCRATCH_REG2, tramp);
387                 ia64_mov_to_br (code, IA64_B6, GP_SCRATCH_REG2);
388                 ia64_br_cond_reg (code, IA64_B6);
389         }
390
391         ia64_codegen_close (code);
392
393         g_assert (code.buf - buf <= TRAMPOLINE_SIZE);
394
395         mono_arch_flush_icache (buf, code.buf - buf);
396
397         if (code_len)
398                 *code_len = code.buf - buf;
399
400         return buf;
401 }
402
403 void
404 mono_arch_invalidate_method (MonoJitInfo *ji, void *func, gpointer func_arg)
405 {
406         NOT_IMPLEMENTED;
407 }
408
409 gpointer
410 mono_debugger_create_notification_function (gpointer *notification_address)
411 {
412         NOT_IMPLEMENTED;
413
414         return NULL;
415 }