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