2009-07-13 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         guint32 ins1, ins2, offset;
188         mgreg_t *r = (mgreg_t*)regs;
189
190         /* Patch the jump table entry used by the plt entry */
191
192         /* Should be a lis+ori */
193         ins1 = ((guint32*)code)[0];
194         g_assert (ins1 >> 26 == 15);
195         ins2 = ((guint32*)code)[1];
196         g_assert (ins2 >> 26 == 24);
197         offset = ((ins1 & 0xffff) << 16) | (ins2 & 0xffff);
198
199         /* Either got or regs is set */
200         if (!got)
201                 got = (gpointer*)r [30];
202         *(guint8**)((guint8*)got + offset) = addr;
203 }
204
205 void
206 mono_arch_nullify_class_init_trampoline (guint8 *code, gssize *regs)
207 {
208         mono_arch_patch_callsite (NULL, code, nullified_class_init_trampoline);
209 }
210
211 void
212 mono_arch_nullify_plt_entry (guint8 *code, gssize *regs)
213 {
214         if (mono_aot_only && !nullified_class_init_trampoline)
215                 nullified_class_init_trampoline = mono_aot_get_named_code ("nullified_class_init_trampoline");
216
217         mono_arch_patch_plt_entry (code, NULL, regs, nullified_class_init_trampoline);
218 }
219
220 /* Stack size for trampoline function 
221  * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
222  * + MonoLMF + 14 fp regs + 31 gregs + alignment
223  */
224 #define STACK (PPC_MINIMAL_STACK_SIZE + 4 * sizeof (gulong) + sizeof (MonoLMF) + 14 * sizeof (double) + 31 * sizeof (gulong))
225
226 /* Method-specific trampoline code fragment size */
227 #define METHOD_TRAMPOLINE_SIZE 64
228
229 /* Jump-specific trampoline code fragment size */
230 #define JUMP_TRAMPOLINE_SIZE   64
231
232 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
233 #define PPC_TOC_REG ppc_r2
234 #else
235 #define PPC_TOC_REG -1
236 #endif
237
238 guchar*
239 mono_arch_create_trampoline_code (MonoTrampolineType tramp_type)
240 {
241         MonoJumpInfo *ji;
242         guint32 code_size;
243         guchar *code;
244         GSList *unwind_ops, *l;
245
246         code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, FALSE);
247
248         //mono_save_trampoline_xdebug_info ("<generic_trampoline>", code, code_size, unwind_ops);
249
250         for (l = unwind_ops; l; l = l->next)
251                 g_free (l->data);
252         g_slist_free (unwind_ops);
253
254         return code;
255 }
256
257 /*
258  * Stack frame description when the generic trampoline is called.
259  * caller frame
260  * --------------------
261  *  MonoLMF
262  *  -------------------
263  *  Saved FP registers 0-13
264  *  -------------------
265  *  Saved general registers 0-30
266  *  -------------------
267  *  param area for 3 args to ppc_magic_trampoline
268  *  -------------------
269  *  linkage area
270  *  -------------------
271  */
272 guchar*
273 mono_arch_create_trampoline_code_full (MonoTrampolineType tramp_type, guint32 *code_size, MonoJumpInfo **ji, GSList **out_unwind_ops, gboolean aot)
274 {
275         guint8 *buf, *code = NULL;
276         int i, offset;
277         gconstpointer tramp_handler;
278         int size = MONO_PPC_32_64_CASE (600, 800);
279
280         /* Now we'll create in 'buf' the PowerPC trampoline code. This
281            is the trampoline code common to all methods  */
282
283         code = buf = mono_global_codeman_reserve (size);
284
285         *ji = NULL;
286         *out_unwind_ops = NULL;
287
288         ppc_stptr_update (buf, ppc_r1, -STACK, ppc_r1);
289
290         /* start building the MonoLMF on the stack */
291         offset = STACK - sizeof (double) * MONO_SAVED_FREGS;
292         for (i = 14; i < 32; i++) {
293                 ppc_stfd (buf, i, offset, ppc_r1);
294                 offset += sizeof (double);
295         }
296         /* 
297          * now the integer registers.
298          */
299         offset = STACK - sizeof (MonoLMF) + G_STRUCT_OFFSET (MonoLMF, iregs);
300         ppc_store_multiple_regs (buf, ppc_r13, offset, ppc_r1);
301
302         /* Now save the rest of the registers below the MonoLMF struct, first 14
303          * fp regs and then the 31 gregs.
304          */
305         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
306         for (i = 0; i < 14; i++) {
307                 ppc_stfd (buf, i, offset, ppc_r1);
308                 offset += sizeof (double);
309         }
310 #define GREGS_OFFSET (STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (gulong)))
311         offset = GREGS_OFFSET;
312         for (i = 0; i < 31; i++) {
313                 ppc_stptr (buf, i, offset, ppc_r1);
314                 offset += sizeof (gulong);
315         }
316
317         /* we got here through a jump to the ctr reg, we must save the lr
318          * in the parent frame (we do it here to reduce the size of the
319          * method-specific trampoline)
320          */
321         ppc_mflr (buf, ppc_r0);
322         ppc_stptr (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
323
324         /* ok, now we can continue with the MonoLMF setup, mostly untouched 
325          * from emit_prolog in mini-ppc.c
326          */
327         if (aot) {
328                 buf = mono_arch_emit_load_aotconst (code, buf, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "mono_get_lmf_addr");
329 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
330                 ppc_ldptr (buf, ppc_r2, sizeof (gpointer), ppc_r11);
331                 ppc_ldptr (buf, ppc_r11, 0, ppc_r11);
332 #endif
333                 ppc_mtlr (buf, ppc_r11);
334                 ppc_blrl (buf);
335         }  else {
336                 ppc_load_func (buf, ppc_r0, mono_get_lmf_addr);
337                 ppc_mtlr (buf, ppc_r0);
338                 ppc_blrl (buf);
339         }
340         /* we build the MonoLMF structure on the stack - see mini-ppc.h
341          * The pointer to the struct is put in ppc_r11.
342          */
343         ppc_addi (buf, ppc_r11, ppc_sp, STACK - sizeof (MonoLMF));
344         ppc_stptr (buf, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
345         /* new_lmf->previous_lmf = *lmf_addr */
346         ppc_ldptr (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
347         ppc_stptr (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
348         /* *(lmf_addr) = r11 */
349         ppc_stptr (buf, ppc_r11, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r3);
350         /* save method info (it's stored on the stack, so get it first and put it
351          * in r5 as it's the third argument to the function)
352          */
353         if (tramp_type == MONO_TRAMPOLINE_GENERIC_CLASS_INIT)
354                 ppc_ldptr (buf, ppc_r5, GREGS_OFFSET + PPC_FIRST_ARG_REG * sizeof (gpointer), ppc_r1);
355         else
356                 ppc_ldptr (buf, ppc_r5, GREGS_OFFSET, ppc_r1);
357         if ((tramp_type == MONO_TRAMPOLINE_JIT) || (tramp_type == MONO_TRAMPOLINE_JUMP))
358                 ppc_stptr (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, method), ppc_r11);
359         /* store the frame pointer of the calling method */
360         ppc_addi (buf, ppc_r0, ppc_sp, STACK);
361         ppc_stptr (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, ebp), ppc_r11);
362         /* save the IP (caller ip) */
363         if (tramp_type == MONO_TRAMPOLINE_JUMP) {
364                 ppc_li (buf, ppc_r0, 0);
365         } else {
366                 ppc_ldptr (buf, ppc_r0, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
367         }
368         ppc_stptr (buf, ppc_r0, G_STRUCT_OFFSET(MonoLMF, eip), ppc_r11);
369
370         /*
371          * Now we're ready to call trampoline (gssize *regs, guint8 *code, gpointer value, guint8 *tramp)
372          * Note that the last argument is unused.
373          */
374         /* Arg 1: a pointer to the registers */
375         ppc_addi (buf, ppc_r3, ppc_r1, GREGS_OFFSET);
376                 
377         /* Arg 2: code (next address to the instruction that called us) */
378         if (tramp_type == MONO_TRAMPOLINE_JUMP)
379                 ppc_li (buf, ppc_r4, 0);
380         else
381                 ppc_ldptr  (buf, ppc_r4, STACK + PPC_RET_ADDR_OFFSET, ppc_r1);
382
383         /* Arg 3: MonoMethod *method. It was put in r5 already above */
384         /*ppc_mr  (buf, ppc_r5, ppc_r5);*/
385
386         if (aot) {
387                 buf = mono_arch_emit_load_aotconst (code, buf, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("trampoline_func_%d", tramp_type));
388 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
389                 ppc_ldptr (buf, ppc_r2, sizeof (gpointer), ppc_r11);
390                 ppc_ldptr (buf, ppc_r11, 0, ppc_r11);
391 #endif
392                 ppc_mtlr (buf, ppc_r11);
393                 ppc_blrl (buf);
394         } else {
395                 tramp_handler = mono_get_trampoline_func (tramp_type);
396                 ppc_load_func (buf, ppc_r0, tramp_handler);
397                 ppc_mtlr (buf, ppc_r0);
398                 ppc_blrl (buf);
399         }
400                 
401         /* OK, code address is now on r3. Move it to the counter reg
402          * so it will be ready for the final jump: this is safe since we
403          * won't do any more calls.
404          */
405         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
406 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
407                 ppc_ldptr (buf, ppc_r2, sizeof (gpointer), ppc_r3);
408                 ppc_ldptr (buf, ppc_r3, 0, ppc_r3);
409 #endif
410                 ppc_mtctr (buf, ppc_r3);
411         }
412
413         /*
414          * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
415          * and the rest of the registers, so the method called will see
416          * the same state as before we executed.
417          * The pointer to MonoLMF is in ppc_r11.
418          */
419         ppc_addi (buf, ppc_r11, ppc_r1, STACK - sizeof (MonoLMF));
420         /* r5 = previous_lmf */
421         ppc_ldptr (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r11);
422         /* r6 = lmf_addr */
423         ppc_ldptr (buf, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r11);
424         /* *(lmf_addr) = previous_lmf */
425         ppc_stptr (buf, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
426         /* restore iregs */
427         ppc_load_multiple_regs (buf, ppc_r13, G_STRUCT_OFFSET(MonoLMF, iregs), ppc_r11);
428         /* restore fregs */
429         for (i = 14; i < 32; i++)
430                 ppc_lfd (buf, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r11);
431
432         /* restore the volatile registers, we skip r1, of course */
433         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
434         for (i = 0; i < 14; i++) {
435                 ppc_lfd (buf, i, offset, ppc_r1);
436                 offset += sizeof (double);
437         }
438         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (gulong));
439         ppc_ldptr (buf, ppc_r0, offset, ppc_r1);
440         offset += 2 * sizeof (gulong);
441         for (i = 2; i < 13; i++) {
442                 if (i != PPC_TOC_REG && (i != 3 || tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
443                         ppc_ldptr (buf, i, offset, ppc_r1);
444                 offset += sizeof (gulong);
445         }
446
447         /* Non-standard function epilogue. Instead of doing a proper
448          * return, we just jump to the compiled code.
449          */
450         /* Restore stack pointer and LR and jump to the code */
451         ppc_ldptr  (buf, ppc_r1,  0, ppc_r1);
452         ppc_ldptr  (buf, ppc_r11, PPC_RET_ADDR_OFFSET, ppc_r1);
453         ppc_mtlr (buf, ppc_r11);
454         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
455                 ppc_blr (buf);
456         else
457                 ppc_bcctr (buf, 20, 0);
458
459         /* Flush instruction cache, since we've generated code */
460         mono_arch_flush_icache (code, buf - code);
461         
462         *code_size = buf - code;
463
464         /* Sanity check */
465         g_assert ((buf - code) <= size);
466
467         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT) {
468                 guint32 code_len;
469
470                 /* Initialize the nullified class init trampoline */
471                 nullified_class_init_trampoline = mono_ppc_create_ftnptr (mono_arch_get_nullified_class_init_trampoline (&code_len));
472         }
473
474         return code;
475 }
476
477 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
478 gpointer
479 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
480 {
481         guint8 *code, *buf, *tramp;
482         guint32 short_branch;
483
484         tramp = mono_get_trampoline_code (tramp_type);
485
486         mono_domain_lock (domain);
487         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
488         short_branch = branch_for_target_reachable (code + MONO_PPC_32_64_CASE (8, 5*4), tramp);
489 #ifdef __mono_ppc64__
490         /* FIXME: make shorter if possible */
491 #else
492         if (short_branch)
493                 mono_domain_code_commit (domain, code, TRAMPOLINE_SIZE, 12);
494 #endif
495         mono_domain_unlock (domain);
496
497         if (short_branch) {
498                 ppc_load_sequence (buf, ppc_r0, (gulong) arg1);
499                 ppc_emit32 (buf, short_branch);
500         } else {
501                 /* Prepare the jump to the generic trampoline code.*/
502                 ppc_load (buf, ppc_r0, (gulong) tramp);
503                 ppc_mtctr (buf, ppc_r0);
504         
505                 /* And finally put 'arg1' in r0 and fly! */
506                 ppc_load (buf, ppc_r0, (gulong) arg1);
507                 ppc_bcctr (buf, 20, 0);
508         }
509         
510         /* Flush instruction cache, since we've generated code */
511         mono_arch_flush_icache (code, buf - code);
512
513         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
514         if (code_len)
515                 *code_len = buf - code;
516
517         return code;
518 }
519
520 static guint8*
521 emit_trampoline_jump (guint8 *code, guint8 *tramp)
522 {
523         guint32 short_branch = branch_for_target_reachable (code, tramp);
524
525         /* FIXME: we can save a few bytes here by committing if the
526            short branch is possible */
527         if (short_branch) {
528                 ppc_emit32 (code, short_branch);
529         } else {
530                 ppc_load (code, ppc_r0, tramp);
531                 ppc_mtctr (code, ppc_r0);
532                 ppc_bcctr (code, 20, 0);
533         }
534
535         return code;
536 }
537
538 gpointer
539 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot)
540 {
541         guint32 code_size;
542         MonoJumpInfo *ji;
543
544         return mono_arch_create_rgctx_lazy_fetch_trampoline_full (slot, &code_size, &ji, FALSE);
545 }
546
547 gpointer
548 mono_arch_create_rgctx_lazy_fetch_trampoline_full (guint32 slot, guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
549 {
550 #ifdef MONO_ARCH_VTABLE_REG
551         guint8 *tramp;
552         guint8 *code, *buf;
553         guint8 **rgctx_null_jumps;
554         int tramp_size;
555         int depth, index;
556         int i;
557         gboolean mrgctx;
558
559         *ji = NULL;
560
561         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
562         index = MONO_RGCTX_SLOT_INDEX (slot);
563         if (mrgctx)
564                 index += sizeof (MonoMethodRuntimeGenericContext) / sizeof (gpointer);
565         for (depth = 0; ; ++depth) {
566                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
567
568                 if (index < size - 1)
569                         break;
570                 index -= size - 1;
571         }
572
573         tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
574         if (mrgctx)
575                 tramp_size += 4;
576         else
577                 tramp_size += 12;
578         if (aot)
579                 tramp_size += 32;
580
581         code = buf = mono_global_codeman_reserve (tramp_size);
582
583         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
584
585         if (mrgctx) {
586                 /* get mrgctx ptr */
587                 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
588         } else {
589                 /* load rgctx ptr from vtable */
590                 ppc_ldptr (code, ppc_r4, G_STRUCT_OFFSET (MonoVTable, runtime_generic_context), PPC_FIRST_ARG_REG);
591                 /* is the rgctx ptr null? */
592                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
593                 /* if yes, jump to actual trampoline */
594                 rgctx_null_jumps [0] = code;
595                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
596         }
597
598         for (i = 0; i < depth; ++i) {
599                 /* load ptr to next array */
600                 if (mrgctx && i == 0)
601                         ppc_ldptr (code, ppc_r4, sizeof (MonoMethodRuntimeGenericContext), ppc_r4);
602                 else
603                         ppc_ldptr (code, ppc_r4, 0, ppc_r4);
604                 /* is the ptr null? */
605                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
606                 /* if yes, jump to actual trampoline */
607                 rgctx_null_jumps [i + 1] = code;
608                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
609         }
610
611         /* fetch slot */
612         ppc_ldptr (code, ppc_r4, sizeof (gpointer) * (index  + 1), ppc_r4);
613         /* is the slot null? */
614         ppc_compare_reg_imm (code, 0, ppc_r4, 0);
615         /* if yes, jump to actual trampoline */
616         rgctx_null_jumps [depth + 1] = code;
617         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
618         /* otherwise return r4 */
619         /* FIXME: if we use r3 as the work register we can avoid this copy */
620         ppc_mr (code, ppc_r3, ppc_r4);
621         ppc_blr (code);
622
623         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
624                 ppc_patch (rgctx_null_jumps [i], code);
625
626         g_free (rgctx_null_jumps);
627
628         /* move the rgctx pointer to the VTABLE register */
629         ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
630
631         if (aot) {
632                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
633                 /* Branch to the trampoline */
634 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
635                 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
636 #endif
637                 ppc_mtctr (code, ppc_r11);
638                 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
639         } else {
640                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
641                         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
642
643                 /* jump to the actual trampoline */
644                 code = emit_trampoline_jump (code, tramp);
645         }
646
647         mono_arch_flush_icache (buf, code - buf);
648
649         g_assert (code - buf <= tramp_size);
650
651         *code_size = code - buf;
652
653         return buf;
654 #else
655         g_assert_not_reached ();
656 #endif
657 }
658
659 gpointer
660 mono_arch_create_generic_class_init_trampoline (void)
661 {
662         guint32 code_size;
663         MonoJumpInfo *ji;
664
665         return mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, FALSE);
666 }
667
668 gpointer
669 mono_arch_create_generic_class_init_trampoline_full (guint32 *code_size, MonoJumpInfo **ji, gboolean aot)
670 {
671         guint8 *tramp;
672         guint8 *code, *buf;
673         static int byte_offset = -1;
674         static guint8 bitmask;
675         guint8 *jump;
676         int tramp_size;
677
678         tramp_size = MONO_PPC_32_64_CASE (32, 44);
679         if (aot)
680                 tramp_size += 32;
681
682         code = buf = mono_global_codeman_reserve (tramp_size);
683
684         *ji = NULL;
685
686         if (byte_offset < 0)
687                 mono_marshal_find_bitfield_offset (MonoVTable, initialized, &byte_offset, &bitmask);
688
689         ppc_lbz (code, ppc_r4, byte_offset, PPC_FIRST_ARG_REG);
690         ppc_andid (code, ppc_r4, ppc_r4, bitmask);
691         jump = code;
692         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
693
694         ppc_blr (code);
695
696         ppc_patch (jump, code);
697
698         if (aot) {
699                 code = mono_arch_emit_load_aotconst (buf, code, ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, "specific_trampoline_generic_class_init");
700                 /* Branch to the trampoline */
701 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
702                 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
703 #endif
704                 ppc_mtctr (code, ppc_r11);
705                 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
706         } else {
707                 tramp = mono_arch_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT,
708                         mono_get_root_domain (), NULL);
709
710                 /* jump to the actual trampoline */
711                 code = emit_trampoline_jump (code, tramp);
712         }
713
714         mono_arch_flush_icache (buf, code - buf);
715
716         *code_size = code - buf;
717
718         g_assert (code - buf <= tramp_size);
719
720         return buf;
721 }
722
723 gpointer
724 mono_arch_get_nullified_class_init_trampoline (guint32 *code_len)
725 {
726         guint8 *code, *buf;
727         guint32 tramp_size = 64;
728
729         code = buf = mono_global_codeman_reserve (tramp_size);
730         ppc_blr (code);
731
732         mono_arch_flush_icache (buf, code - buf);
733
734         *code_len = code - buf;
735
736         g_assert (code - buf <= tramp_size);
737
738         return buf;
739 }