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