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