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