2009-06-19 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / tramp-ppc.c
1 /*
2  * tramp-ppc.c: JIT trampoline code for PowerPC
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *   Carlos Valiente <yo@virutass.net>
8  *   Andreas Faerber <andreas.faerber@web.de>
9  *
10  * (C) 2001 Ximian, Inc.
11  * (C) 2007-2008 Andreas Faerber
12  */
13
14 #include <config.h>
15 #include <glib.h>
16
17 #include <mono/metadata/appdomain.h>
18 #include <mono/metadata/marshal.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/arch/ppc/ppc-codegen.h>
21
22 #include "mini.h"
23 #include "mini-ppc.h"
24
25 static guint8* nullified_class_init_trampoline;
26
27 /* Same as mono_create_ftnptr, but doesn't require a domain */
28 static gpointer
29 mono_ppc_create_ftnptr (guint8 *code)
30 {
31 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
32         MonoPPCFunctionDescriptor *ftnptr = mono_global_codeman_reserve (sizeof (MonoPPCFunctionDescriptor));
33
34         ftnptr->code = code;
35         ftnptr->toc = NULL;
36         ftnptr->env = NULL;
37
38         return ftnptr;
39 #else
40         return code;
41 #endif
42 }
43
44 /*
45  * Return the instruction to jump from code to target, 0 if not
46  * reachable with a single instruction
47  */
48 static guint32
49 branch_for_target_reachable (guint8 *branch, guint8 *target)
50 {
51         gint diff = target - branch;
52         g_assert ((diff & 3) == 0);
53         if (diff >= 0) {
54                 if (diff <= 33554431)
55                         return (18 << 26) | (diff);
56         } else {
57                 /* diff between 0 and -33554432 */
58                 if (diff >= -33554432)
59                         return (18 << 26) | (diff & ~0xfc000000);
60         }
61         return 0;
62 }
63
64 /*
65  * get_unbox_trampoline:
66  * @gsctx: the generic sharing context
67  * @m: method pointer
68  * @addr: pointer to native code for @m
69  *
70  * when value type methods are called through the vtable we need to unbox the
71  * this argument. This method returns a pointer to a trampoline which does
72  * unboxing before calling the method
73  */
74 gpointer
75 mono_arch_get_unbox_trampoline (MonoGenericSharingContext *gsctx, MonoMethod *m, gpointer addr)
76 {
77         guint8 *code, *start;
78         int this_pos = 3;
79         guint32 short_branch;
80         MonoDomain *domain = mono_domain_get ();
81         int size = MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE;
82
83         addr = mono_get_addr_from_ftnptr (addr);
84
85         if (MONO_TYPE_ISSTRUCT (mono_method_signature (m)->ret))
86                 this_pos = 4;
87             
88         mono_domain_lock (domain);
89         start = code = mono_domain_code_reserve (domain, size);
90         code = mono_ppc_create_pre_code_ftnptr (code);
91         short_branch = branch_for_target_reachable (code + 4, addr);
92         if (short_branch)
93                 mono_domain_code_commit (domain, code, size, 8);
94         mono_domain_unlock (domain);
95
96         if (short_branch) {
97                 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
98                 ppc_emit32 (code, short_branch);
99         } else {
100                 ppc_load (code, ppc_r0, addr);
101                 ppc_mtctr (code, ppc_r0);
102                 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
103                 ppc_bcctr (code, 20, 0);
104         }
105         mono_arch_flush_icache (start, code - start);
106         g_assert ((code - start) <= size);
107         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
108         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
109
110         return start;
111 }
112
113 /*
114  * mono_arch_get_static_rgctx_trampoline:
115  *
116  *   Create a trampoline which sets RGCTX_REG to MRGCTX, then jumps to ADDR.
117  */
118 gpointer
119 mono_arch_get_static_rgctx_trampoline (MonoMethod *m, MonoMethodRuntimeGenericContext *mrgctx, gpointer addr)
120 {
121         guint8 *code, *start, *p;
122         guint8 imm_buf [128];
123         guint32 short_branch;
124         MonoDomain *domain = mono_domain_get ();
125         int imm_size;
126         int size = MONO_PPC_32_64_CASE (24, (PPC_LOAD_SEQUENCE_LENGTH * 2) + 8) + PPC_FTNPTR_SIZE;
127
128         addr = mono_get_addr_from_ftnptr (addr);
129
130         /* Compute size of code needed to emit mrgctx */
131         p = imm_buf;
132         ppc_load (p, MONO_ARCH_RGCTX_REG, mrgctx);
133         imm_size = p - imm_buf;
134
135         mono_domain_lock (domain);
136         start = code = mono_domain_code_reserve (domain, size);
137         code = mono_ppc_create_pre_code_ftnptr (code);
138         short_branch = branch_for_target_reachable (code + imm_size, addr);
139         if (short_branch)
140                 mono_domain_code_commit (domain, code, size, imm_size + 4);
141         mono_domain_unlock (domain);
142
143         if (short_branch) {
144                 ppc_load (code, MONO_ARCH_RGCTX_REG, mrgctx);
145                 ppc_emit32 (code, short_branch);
146         } else {
147                 ppc_load (code, ppc_r0, addr);
148                 ppc_mtctr (code, ppc_r0);
149                 ppc_load (code, MONO_ARCH_RGCTX_REG, mrgctx);
150                 ppc_bcctr (code, 20, 0);
151         }
152         mono_arch_flush_icache (start, code - start);
153         g_assert ((code - start) <= size);
154
155         return start;
156 }
157
158 void
159 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
160 {
161         guint32 *code = (guint32*)code_ptr;
162
163         addr = mono_get_addr_from_ftnptr (addr);
164
165         /* This is the 'blrl' instruction */
166         --code;
167         
168         /*
169          * Note that methods are called also with the bl opcode.
170          */
171         if (((*code) >> 26) == 18) {
172                 /*g_print ("direct patching\n");*/
173                 ppc_patch ((guint8*)code, addr);
174                 mono_arch_flush_icache ((guint8*)code, 4);
175                 return;
176         }
177         
178         /* Sanity check */
179         g_assert (mono_ppc_is_direct_call_sequence (code));
180
181         ppc_patch ((guint8*)code, addr);
182 }
183
184 void
185 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, gssize *regs, guint8 *addr)
186 {
187         g_assert_not_reached ();
188 }
189
190 void
191 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
192 {
193         mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
194 }
195
196 void
197 mono_arch_nullify_plt_entry (guint8 *code, gssize *regs)
198 {
199         g_assert_not_reached ();
200 }
201
202 /* Stack size for trampoline function 
203  * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
204  * + MonoLMF + 14 fp regs + 13 gregs + alignment
205  * #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * (sizeof (gulong)))
206  * STACK would be 444 for 32 bit darwin
207  */
208 #ifdef __mono_ppc64__
209 #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 13 * sizeof (gulong))
210 #else
211 #define STACK (448)
212 #endif
213
214 /* Method-specific trampoline code fragment size */
215 #define METHOD_TRAMPOLINE_SIZE 64
216
217 /* Jump-specific trampoline code fragment size */
218 #define JUMP_TRAMPOLINE_SIZE   64
219
220 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
221 #define PPC_TOC_REG ppc_r2
222 #else
223 #define PPC_TOC_REG -1
224 #endif
225
226 guchar*
227 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
228 {
229         MonoJumpInfo *ji;
230         guint32 code_size;
231         guchar *code;
232         GSList *unwind_ops, *l;
233
234         code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
235
236         //mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
237
238         for (l = unwind_ops; l; l = l->next)
239                 g_free (l->data);
240         g_slist_free (unwind_ops);
241
242         return code;
243 }
244
245 /*
246  * Stack frame description when the generic trampoline is called.
247  * caller frame
248  * --------------------
249  *  MonoLMF
250  *  -------------------
251  *  Saved FP registers 0-13
252  *  -------------------
253  *  Saved general registers 0-30
254  *  -------------------
255  *  param area for 3 args to ppc_magic_trampoline
256  *  -------------------
257  *  linkage area
258  *  -------------------
259  */
260 guchar*
261 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
262 {
263         guint8 *buf, *code = NULL;
264         int i, offset;
265         gconstpointer tramp_handler;
266         int size = MONO_PPC_32_64_CASE (600, 800);
267
268         /* Now we'll create in 'buf' the PowerPC trampoline code. This
269            is the trampoline code common to all methods  */
270
271         code = buf = mono_global_codeman_reserve (size);
272
273         *ji = NULL;
274         *out_unwind_ops = NULL;
275
276         ppc_store_reg_update (buf, ppc_r1, -STACK, ppc_r1);
277
278         /* start building the MonoLMF on the stack */
279         offset = STACK - sizeof (double) * MONO_SAVED_FREGS;
280         for (i = 14; i < 32; i++) {
281                 ppc_stfd (buf, i, offset, ppc_r1);
282                 offset += sizeof (double);
283         }
284         /* 
285          * now the integer registers.
286          */
287         offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
288         ppc_store_multiple_regs (buf, ppc_r13, offset, ppc_r1);
289
290         /* Now save the rest of the registers below the MonoLMF struct, first 14
291          * fp regs and then the 31 gregs.
292          */
293         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
294         for (i = 0; i < 14; i++) {
295                 ppc_stfd (buf, i, offset, ppc_r1);
296                 offset += sizeof (double);
297         }
298 #define GREGS_OFFSET (STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (gulong)))
299         offset = GREGS_OFFSET;
300         for (i = 0; i < 31; i++) {
301                 ppc_store_reg (buf, i, offset, ppc_r1);
302                 offset += sizeof (gulong);
303         }
304
305         /* we got here through a jump to the ctr reg, we must save the lr
306          * in the parent frame (we do it here to reduce the size of the
307          * method-specific trampoline)
308          */
309         ppc_mflr (buf, ppc_r0);
310         ppc_store_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
311
312         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
313          * from emit_prolog in mini-ppc.c
314          */
315         if (aot) {
316                 buf = mono_arch_emit_load_aotconst (code, buf, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
317 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
318                 ppc_load_reg (buf, ppc_r2, sizeof (gpointer), ppc_r11);
319                 ppc_load_reg (buf, ppc_r11, 0, ppc_r11);
320 #endif
321                 ppc_mtlr (buf, ppc_r11);
322                 ppc_blrl (buf);
323         }  else {
324                 ppc_load_func (buf, ppc_r0, mono_get_lmf_addr);
325                 ppc_mtlr (buf, ppc_r0);
326                 ppc_blrl (buf);
327         }
328         /* we build the MonoLMF structure on the stack - see mini-ppc.h
329          * The pointer to the struct is put in ppc_r11.
330          */
331         ppc_addi (buf, ppc_r11, ppc_sp, STACK - sizeof (MonoLMF));
332         ppc_store_reg (buf, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
333         /* new_lmf->previous_lmf = *lmf_addr */
334         ppc_load_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
335         ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
336         /* *(lmf_addr) = r11 */
337         ppc_store_reg (buf, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
338         /* save method info (it's stored on the stack, so get it first and put it
339          * in r5 as it's the third argument to the function)
340          */
341         if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
342                 ppc_load_reg (buf, ppc_r5, GREGS_OFFSET + PPC_FIRST_ARG_REG * sizeof (gpointer), ppc_r1);
343         else
344                 ppc_load_reg (buf, ppc_r5, GREGS_OFFSET, ppc_r1);
345         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
346                 ppc_store_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
347         /* store the frame pointer of the calling method */
348         ppc_addi (buf, ppc_r0, ppc_sp, STACK);
349         ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
350         /* save the IP (caller ip) */
351         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
352                 ppc_li (buf, ppc_r0, 0);
353         } else {
354                 ppc_load_reg (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
355         }
356         ppc_store_reg (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
357
358         /*
359          * Now we're ready to call trampoline (gssize *regs, guint8 *code, gpointer value, guint8 *tramp)
360          * Note that the last argument is unused.
361          */
362         /* Arg 1: a pointer to the registers */
363         ppc_addi (buf, ppc_r3, ppc_r1, GREGS_OFFSET);
364                 
365         /* Arg 2: code (next address to the instruction that called us) */
366         if (tramp_type == MONO_TRAMPOLINE_JUMP)
367                 ppc_li (buf, ppc_r4, 0);
368         else
369                 ppc_load_reg  (buf, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
370
371         /* Arg 3: MonoMethod *method. It was put in r5 already above */
372         /*ppc_mr  (buf, ppc_r5, ppc_r5);*/
373
374         if (aot) {
375                 buf = mono_arch_emit_load_aotconst (code, buf, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("trampoline_func_%d", tramp_type));
376 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
377                 ppc_load_reg (buf, ppc_r2, sizeof (gpointer), ppc_r11);
378                 ppc_load_reg (buf, ppc_r11, 0, ppc_r11);
379 #endif
380                 ppc_mtlr (buf, ppc_r11);
381                 ppc_blrl (buf);
382         } else {
383                 tramp_handler = mono_get_trampoline_func (tramp_type);
384                 ppc_load_func (buf, ppc_r0, tramp_handler);
385                 ppc_mtlr (buf, ppc_r0);
386                 ppc_blrl (buf);
387         }
388                 
389         /* OK, code address is now on r3. Move it to the counter reg
390          * so it will be ready for the final jump: this is safe since we
391          * won't do any more calls.
392          */
393         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
394 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
395                 ppc_load_reg (buf, ppc_r2, sizeof (gpointer), ppc_r3);
396                 ppc_load_reg (buf, ppc_r3, 0, ppc_r3);
397 #endif
398                 ppc_mtctr (buf, ppc_r3);
399         }
400
401         /*
402          * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
403          * and the rest of the registers, so the method called will see
404          * the same state as before we executed.
405          * The pointer to MonoLMF is in ppc_r11.
406          */
407         ppc_addi (buf, ppc_r11, ppc_r1, STACK - sizeof (MonoLMF));
408         /* r5 = previous_lmf */
409         ppc_load_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
410         /* r6 = lmf_addr */
411         ppc_load_reg (buf, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
412         /* *(lmf_addr) = previous_lmf */
413         ppc_store_reg (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
414         /* restore iregs */
415         ppc_load_multiple_regs (buf, ppc_r13, G_STRUCT_OFFSET(MonoLMF, iregs), ppc_r11);
416         /* restore fregs */
417         for (i = 14; i < 32; i++)
418                 ppc_lfd (buf, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
419
420         /* restore the volatile registers, we skip r1, of course */
421         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
422         for (i = 0; i < 14; i++) {
423                 ppc_lfd (buf, i, offset, ppc_r1);
424                 offset += sizeof (double);
425         }
426         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (gulong));
427         ppc_load_reg (buf, ppc_r0, offset, ppc_r1);
428         offset += 2 * sizeof (gulong);
429         for (i = 2; i < 13; i++) {
430                 if (i != PPC_TOC_REG && (i != 3 || tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
431                         ppc_load_reg (buf, i, offset, ppc_r1);
432                 offset += sizeof (gulong);
433         }
434
435         /* Non-standard function epilogue. Instead of doing a proper
436          * return, we just jump to the compiled code.
437          */
438         /* Restore stack pointer and LR and jump to the code */
439         ppc_load_reg  (buf, ppc_r1,  0, ppc_r1);
440         ppc_load_reg  (buf, ppc_r11, PPC_RET_ADDR_OFFSET, ppc_r1);
441         ppc_mtlr (buf, ppc_r11);
442         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
443                 ppc_blr (buf);
444         else
445                 ppc_bcctr (buf, 20, 0);
446
447         /* Flush instruction cache, since we've generated code */
448         mono_arch_flush_icache (code, buf - code);
449         
450         *code_size = buf - code;
451
452         /* Sanity check */
453         g_assert ((buf - code) <= size);
454
455         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
456                 guint32 code_len;
457
458                 /* Initialize the nullified class init trampoline */
459                 nullified_class_init_trampoline = mono_ppc_create_ftnptr (mono_arch_get_nullified_class_init_trampoline (&code_len));
460         }
461
462         return code;
463 }
464
465 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
466 gpointer
467 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
468 {
469         guint8 *code, *buf, *tramp;
470         guint32 short_branch;
471
472         tramp = mono_get_trampoline_code (tramp_type);
473
474         mono_domain_lock (domain);
475         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
476         short_branch = branch_for_target_reachable (code + MONO_PPC_32_64_CASE (8, 5*4), tramp);
477 #ifdef __mono_ppc64__
478         /* FIXME: make shorter if possible */
479 #else
480         if (short_branch)
481                 mono_domain_code_commit (domain, code, TRAMPOLINE_SIZE, 12);
482 #endif
483         mono_domain_unlock (domain);
484
485         if (short_branch) {
486                 ppc_load_sequence (buf, ppc_r0, (gulong) arg1);
487                 ppc_emit32 (buf, short_branch);
488         } else {
489                 /* Prepare the jump to the generic trampoline code.*/
490                 ppc_load (buf, ppc_r0, (gulong) tramp);
491                 ppc_mtctr (buf, ppc_r0);
492         
493                 /* And finally put 'arg1' in r0 and fly! */
494                 ppc_load (buf, ppc_r0, (gulong) arg1);
495                 ppc_bcctr (buf, 20, 0);
496         }
497         
498         /* Flush instruction cache, since we've generated code */
499         mono_arch_flush_icache (code, buf - code);
500
501         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
502         if (code_len)
503                 *code_len = buf - code;
504
505         return code;
506 }
507
508 static guint8*
509 emit_trampoline_jump (guint8 *code, guint8 *tramp)
510 {
511         guint32 short_branch = branch_for_target_reachable (code, tramp);
512
513         /* FIXME: we can save a few bytes here by committing if the
514            short branch is possible */
515         if (short_branch) {
516                 ppc_emit32 (code, short_branch);
517         } else {
518                 ppc_load (code, ppc_r0, tramp);
519                 ppc_mtctr (code, ppc_r0);
520                 ppc_bcctr (code, 20, 0);
521         }
522
523         return code;
524 }
525
526 gpointer
527 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
528 {
529         guint32 code_size;
530         MonoJumpInfo *ji;
531
532         return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
533 }
534
535 gpointer
536 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
537 {
538 #ifdef MONO_ARCH_VTABLE_REG
539         guint8 *tramp;
540         guint8 *code, *buf;
541         guint8 **rgctx_null_jumps;
542         int tramp_size;
543         int depth, index;
544         int i;
545         gboolean mrgctx;
546
547         *ji = NULL;
548
549         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
550         index = MONO_RGCTX_SLOT_INDEX (slot);
551         if (mrgctx)
552                 index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
553         for (depth = 0; ; ++depth) {
554                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
555
556                 if (index < size - 1)
557                         break;
558                 index -= size - 1;
559         }
560
561         tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
562         if (mrgctx)
563                 tramp_size += 4;
564         else
565                 tramp_size += 12;
566         if (aot)
567                 tramp_size += 32;
568
569         code = buf = mono_global_codeman_reserve (tramp_size);
570
571         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
572
573         if (mrgctx) {
574                 /* get mrgctx ptr */
575                 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
576         } else {
577                 /* load rgctx ptr from vtable */
578                 ppc_load_reg (code, ppc_r4, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), PPC_FIRST_ARG_REG);
579                 /* is the rgctx ptr null? */
580                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
581                 /* if yes, jump to actual trampoline */
582                 rgctx_null_jumps [0] = code;
583                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
584         }
585
586         for (i = 0; i < depth; ++i) {
587                 /* load ptr to next array */
588                 if (mrgctx && i == 0)
589                         ppc_load_reg (code, ppc_r4, sizeof (MonoMethodRuntimeGenericContext), ppc_r4);
590                 else
591                         ppc_load_reg (code, ppc_r4, 0, ppc_r4);
592                 /* is the ptr null? */
593                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
594                 /* if yes, jump to actual trampoline */
595                 rgctx_null_jumps [i + 1] = code;
596                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
597         }
598
599         /* fetch slot */
600         ppc_load_reg (code, ppc_r4, sizeof (gpointer) * (index  + 1), ppc_r4);
601         /* is the slot null? */
602         ppc_compare_reg_imm (code, 0, ppc_r4, 0);
603         /* if yes, jump to actual trampoline */
604         rgctx_null_jumps [depth + 1] = code;
605         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
606         /* otherwise return r4 */
607         /* FIXME: if we use r3 as the work register we can avoid this copy */
608         ppc_mr (code, ppc_r3, ppc_r4);
609         ppc_blr (code);
610
611         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
612                 ppc_patch (rgctx_null_jumps [i], code);
613
614         g_free (rgctx_null_jumps);
615
616         /* move the rgctx pointer to the VTABLE register */
617         ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
618
619         if (aot) {
620                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
621                 /* Branch to the trampoline */
622 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
623                 ppc_load_reg (code, ppc_r11, 0, ppc_r11);
624 #endif
625                 ppc_mtctr (code, ppc_r11);
626                 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
627         } else {
628                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
629                         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
630
631                 /* jump to the actual trampoline */
632                 code = emit_trampoline_jump (code, tramp);
633         }
634
635         mono_arch_flush_icache (buf, code - buf);
636
637         g_assert (code - buf <= tramp_size);
638
639         *code_size = code - buf;
640
641         return buf;
642 #else
643         g_assert_not_reached ();
644 #endif
645 }
646
647 gpointer
648 mono_arch_create_generic_class_init_trampoline (void)
649 {
650         guint32 code_size;
651         MonoJumpInfo *ji;
652
653         return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
654 }
655
656 gpointer
657 mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
658 {
659         guint8 *tramp;
660         guint8 *code, *buf;
661         static int byte_offset = -1;
662         static guint8 bitmask;
663         guint8 *jump;
664         int tramp_size;
665
666         tramp_size = MONO_PPC_32_64_CASE (32, 44);
667         if (aot)
668                 tramp_size += 32;
669
670         code = buf = mono_global_codeman_reserve (tramp_size);
671
672         *ji = NULL;
673
674         if (byte_offset < 0)
675                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
676
677         ppc_lbz (code, ppc_r4, byte_offset, PPC_FIRST_ARG_REG);
678         ppc_andid (code, ppc_r4, ppc_r4, bitmask);
679         jump = code;
680         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
681
682         ppc_blr (code);
683
684         ppc_patch (jump, code);
685
686         if (aot) {
687                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
688                 /* Branch to the trampoline */
689 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
690                 ppc_load_reg (code, ppc_r11, 0, ppc_r11);
691 #endif
692                 ppc_mtctr (code, ppc_r11);
693                 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
694         } else {
695                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
696                         mono_get_root_domain (), NULL);
697
698                 /* jump to the actual trampoline */
699                 code = emit_trampoline_jump (code, tramp);
700         }
701
702         mono_arch_flush_icache (buf, code - buf);
703
704         *code_size = code - buf;
705
706         g_assert (code - buf <= tramp_size);
707
708         return buf;
709 }
710
711 gpointer
712 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
713 {
714         guint8 *code, *buf;
715         guint32 tramp_size = 64;
716
717         code = buf = mono_global_codeman_reserve (tramp_size);
718         ppc_blr (code);
719
720         mono_arch_flush_icache (buf, code - buf);
721
722         *code_len = code - buf;
723
724         g_assert (code - buf <= tramp_size);
725
726         return buf;
727 }