Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / tramp-ppc.c
1 /**
2  * \file
3  * JIT trampoline code for PowerPC
4  *
5  * Authors:
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *   Paolo Molaro (lupus@ximian.com)
8  *   Carlos Valiente <yo@virutass.net>
9  *   Andreas Faerber <andreas.faerber@web.de>
10  *
11  * (C) 2001 Ximian, Inc.
12  * (C) 2007-2008 Andreas Faerber
13  */
14
15 #include <config.h>
16 #include <glib.h>
17
18 #include <mono/metadata/abi-details.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/marshal.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/arch/ppc/ppc-codegen.h>
23
24 #include "mini.h"
25 #include "mini-ppc.h"
26
27 #if 0
28 /* Same as mono_create_ftnptr, but doesn't require a domain */
29 static gpointer
30 mono_ppc_create_ftnptr (guint8 *code)
31 {
32 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
33         MonoPPCFunctionDescriptor *ftnptr = mono_global_codeman_reserve (sizeof (MonoPPCFunctionDescriptor));
34
35         ftnptr->code = code;
36         ftnptr->toc = NULL;
37         ftnptr->env = NULL;
38
39         return ftnptr;
40 #else
41         return code;
42 #endif
43 }
44 #endif
45
46 /*
47  * Return the instruction to jump from code to target, 0 if not
48  * reachable with a single instruction
49  */
50 static guint32
51 branch_for_target_reachable (guint8 *branch, guint8 *target)
52 {
53         gint diff = target - branch;
54         g_assert ((diff & 3) == 0);
55         if (diff >= 0) {
56                 if (diff <= 33554431)
57                         return (18 << 26) | (diff);
58         } else {
59                 /* diff between 0 and -33554432 */
60                 if (diff >= -33554432)
61                         return (18 << 26) | (diff & ~0xfc000000);
62         }
63         return 0;
64 }
65
66 /*
67  * get_unbox_trampoline:
68  * @m: method pointer
69  * @addr: pointer to native code for @m
70  *
71  * when value type methods are called through the vtable we need to unbox the
72  * this argument. This method returns a pointer to a trampoline which does
73  * unboxing before calling the method
74  */
75 gpointer
76 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
77 {
78         guint8 *code, *start;
79         int this_pos = 3;
80         guint32 short_branch;
81         MonoDomain *domain = mono_domain_get ();
82         int size = MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE;
83
84         addr = mono_get_addr_from_ftnptr (addr);
85
86         mono_domain_lock (domain);
87         start = code = mono_domain_code_reserve (domain, size);
88         code = mono_ppc_create_pre_code_ftnptr (code);
89         short_branch = branch_for_target_reachable (code + 4, addr);
90         if (short_branch)
91                 mono_domain_code_commit (domain, code, size, 8);
92         mono_domain_unlock (domain);
93
94         if (short_branch) {
95                 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
96                 ppc_emit32 (code, short_branch);
97         } else {
98                 ppc_load_ptr (code, ppc_r0, addr);
99                 ppc_mtctr (code, ppc_r0);
100                 ppc_addi (code, this_pos, this_pos, sizeof (MonoObject));
101                 ppc_bcctr (code, 20, 0);
102         }
103         mono_arch_flush_icache (start, code - start);
104         g_assert ((code - start) <= size);
105         /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
106         g_print ("unbox code is at %p for method at %p\n", start, addr);*/
107
108         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL), domain);
109
110         return start;
111 }
112
113 /*
114  * mono_arch_get_static_rgctx_trampoline:
115  *
116  *   Create a trampoline which sets RGCTX_REG to ARG, then jumps to ADDR.
117  */
118 gpointer
119 mono_arch_get_static_rgctx_trampoline (gpointer arg, 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 the arg */
131         p = imm_buf;
132         ppc_load_ptr (p, MONO_ARCH_RGCTX_REG, arg);
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_ptr (code, MONO_ARCH_RGCTX_REG, arg);
145                 ppc_emit32 (code, short_branch);
146         } else {
147                 ppc_load_ptr (code, ppc_r0, addr);
148                 ppc_mtctr (code, ppc_r0);
149                 ppc_load_ptr (code, MONO_ARCH_RGCTX_REG, arg);
150                 ppc_bcctr (code, 20, 0);
151         }
152         mono_arch_flush_icache (start, code - start);
153         g_assert ((code - start) <= size);
154
155         mono_tramp_info_register (mono_tramp_info_create (NULL, start, code - start, NULL, NULL), domain);
156
157         return start;
158 }
159
160 void
161 mono_arch_patch_callsite (guint8 *method_start, guint8 *code_ptr, guint8 *addr)
162 {
163         guint32 *code = (guint32*)code_ptr;
164
165         addr = mono_get_addr_from_ftnptr (addr);
166
167         /* This is the 'blrl' instruction */
168         --code;
169         
170         /*
171          * Note that methods are called also with the bl opcode.
172          */
173         if (((*code) >> 26) == 18) {
174                 /*g_print ("direct patching\n");*/
175                 ppc_patch ((guint8*)code, addr);
176                 mono_arch_flush_icache ((guint8*)code, 4);
177                 return;
178         }
179         
180         /* Sanity check */
181         g_assert (mono_ppc_is_direct_call_sequence (code));
182
183         ppc_patch ((guint8*)code, addr);
184 }
185
186 void
187 mono_arch_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
188 {
189         guint32 ins1, ins2, offset;
190
191         /* Patch the jump table entry used by the plt entry */
192
193         /* Should be a lis+ori */
194         ins1 = ((guint32*)code)[0];
195         g_assert (ins1 >> 26 == 15);
196         ins2 = ((guint32*)code)[1];
197         g_assert (ins2 >> 26 == 24);
198         offset = ((ins1 & 0xffff) << 16) | (ins2 & 0xffff);
199
200         /* Either got or regs is set */
201         if (!got)
202                 got = (gpointer*)(gsize) regs [30];
203         *(guint8**)((guint8*)got + offset) = addr;
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_r12);
298                 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
299 #endif
300                 ppc_mtlr (code, ppc_r12);
301                 ppc_blrl (code);
302         }  else {
303                 ppc_load_func (code, PPC_CALL_REG, mono_get_lmf_addr);
304                 ppc_mtlr (code, PPC_CALL_REG);
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_r12.
309          */
310         ppc_addi (code, ppc_r12, ppc_sp, STACK - sizeof (MonoLMF));
311         ppc_stptr (code, ppc_r3, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r12);
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_r12);
315         /* *(lmf_addr) = r12 */
316         ppc_stptr (code, ppc_r12, 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_r12);
321         } else {
322                 ppc_load (code, ppc_r0, 0);
323                 ppc_stptr (code, ppc_r0, G_STRUCT_OFFSET(MonoLMF, method), ppc_r12);
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_r12);
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_r12);
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         ppc_ldr (code, ppc_r5, GREGS_OFFSET, ppc_r1);
351
352         if (aot) {
353                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("trampoline_func_%d", tramp_type));
354 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
355                 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r12);
356                 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
357 #endif
358                 ppc_mtlr (code, ppc_r12);
359                 ppc_blrl (code);
360         } else {
361                 tramp_handler = mono_get_trampoline_func (tramp_type);
362                 ppc_load_func (code, PPC_CALL_REG, tramp_handler);
363                 ppc_mtlr (code, PPC_CALL_REG);
364                 ppc_blrl (code);
365         }
366                 
367         /* OK, code address is now on r3. Move it to the counter reg
368          * so it will be ready for the final jump: this is safe since we
369          * won't do any more calls.
370          */
371         if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type)) {
372 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
373                 ppc_ldptr (code, ppc_r2, sizeof (gpointer), ppc_r3);
374                 ppc_ldptr (code, ppc_r3, 0, ppc_r3);
375 #endif
376                 ppc_mtctr (code, ppc_r3);
377         }
378
379         /*
380          * Now we restore the MonoLMF (see emit_epilogue in mini-ppc.c)
381          * and the rest of the registers, so the method called will see
382          * the same state as before we executed.
383          * The pointer to MonoLMF is in ppc_r12.
384          */
385         ppc_addi (code, ppc_r12, ppc_r1, STACK - sizeof (MonoLMF));
386         /* r5 = previous_lmf */
387         ppc_ldptr (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r12);
388         /* r6 = lmf_addr */
389         ppc_ldptr (code, ppc_r6, G_STRUCT_OFFSET(MonoLMF, lmf_addr), ppc_r12);
390         /* *(lmf_addr) = previous_lmf */
391         ppc_stptr (code, ppc_r5, G_STRUCT_OFFSET(MonoLMF, previous_lmf), ppc_r6);
392         /* restore iregs */
393         ppc_ldr_multiple (code, ppc_r13, G_STRUCT_OFFSET(MonoLMF, iregs), ppc_r12);
394         /* restore fregs */
395         for (i = 14; i < 32; i++)
396                 ppc_lfd (code, i, G_STRUCT_OFFSET(MonoLMF, fregs) + ((i-14) * sizeof (gdouble)), ppc_r12);
397
398         /* restore the volatile registers, we skip r1, of course */
399         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double));
400         for (i = 0; i < 14; i++) {
401                 ppc_lfd (code, i, offset, ppc_r1);
402                 offset += sizeof (double);
403         }
404         offset = STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (mgreg_t));
405         ppc_ldr (code, ppc_r0, offset, ppc_r1);
406         offset += 2 * sizeof (mgreg_t);
407         for (i = 2; i < 13; i++) {
408                 if (i != PPC_TOC_REG && (i != 3 || tramp_type != MONO_TRAMPOLINE_RGCTX_LAZY_FETCH))
409                         ppc_ldr (code, i, offset, ppc_r1);
410                 offset += sizeof (mgreg_t);
411         }
412
413         /* Non-standard function epilogue. Instead of doing a proper
414          * return, we just jump to the compiled code.
415          */
416         /* Restore stack pointer and LR and jump to the code */
417         ppc_ldr  (code, ppc_r1,  0, ppc_r1);
418         ppc_ldr  (code, ppc_r12, PPC_RET_ADDR_OFFSET, ppc_r1);
419         ppc_mtlr (code, ppc_r12);
420         if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type))
421                 ppc_blr (code);
422         else
423                 ppc_bcctr (code, 20, 0);
424
425         /* Flush instruction cache, since we've generated code */
426         mono_arch_flush_icache (buf, code - buf);
427         
428         /* Sanity check */
429         g_assert ((code - buf) <= size);
430
431         g_assert (info);
432         tramp_name = mono_get_generic_trampoline_name (tramp_type);
433         *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
434         g_free (tramp_name);
435
436         return buf;
437 }
438
439 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
440 gpointer
441 mono_arch_create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
442 {
443         guint8 *code, *buf, *tramp;
444         guint32 short_branch;
445
446         tramp = mono_get_trampoline_code (tramp_type);
447
448         mono_domain_lock (domain);
449         code = buf = mono_domain_code_reserve_align (domain, TRAMPOLINE_SIZE, 4);
450         short_branch = branch_for_target_reachable (code + MONO_PPC_32_64_CASE (8, 5*4), tramp);
451 #ifdef __mono_ppc64__
452         /* FIXME: make shorter if possible */
453 #else
454         if (short_branch)
455                 mono_domain_code_commit (domain, code, TRAMPOLINE_SIZE, 12);
456 #endif
457         mono_domain_unlock (domain);
458
459         if (short_branch) {
460                 ppc_load_sequence (code, ppc_r0, (mgreg_t)(gsize) arg1);
461                 ppc_emit32 (code, short_branch);
462         } else {
463                 /* Prepare the jump to the generic trampoline code.*/
464                 ppc_load_ptr (code, ppc_r0, tramp);
465                 ppc_mtctr (code, ppc_r0);
466         
467                 /* And finally put 'arg1' in r0 and fly! */
468                 ppc_load_ptr (code, ppc_r0, arg1);
469                 ppc_bcctr (code, 20, 0);
470         }
471         
472         /* Flush instruction cache, since we've generated code */
473         mono_arch_flush_icache (buf, code - buf);
474
475         g_assert ((code - buf) <= TRAMPOLINE_SIZE);
476
477         if (code_len)
478                 *code_len = code - buf;
479
480         return buf;
481 }
482
483 static guint8*
484 emit_trampoline_jump (guint8 *code, guint8 *tramp)
485 {
486         guint32 short_branch = branch_for_target_reachable (code, tramp);
487
488         /* FIXME: we can save a few bytes here by committing if the
489            short branch is possible */
490         if (short_branch) {
491                 ppc_emit32 (code, short_branch);
492         } else {
493                 ppc_load_ptr (code, ppc_r0, tramp);
494                 ppc_mtctr (code, ppc_r0);
495                 ppc_bcctr (code, 20, 0);
496         }
497
498         return code;
499 }
500
501 gpointer
502 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
503 {
504         guint8 *tramp;
505         guint8 *code, *buf;
506         guint8 **rgctx_null_jumps;
507         int tramp_size;
508         int depth, index;
509         int i;
510         gboolean mrgctx;
511         MonoJumpInfo *ji = NULL;
512         GSList *unwind_ops = NULL;
513
514         mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
515         index = MONO_RGCTX_SLOT_INDEX (slot);
516         if (mrgctx)
517                 index += MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT / sizeof (gpointer);
518         for (depth = 0; ; ++depth) {
519                 int size = mono_class_rgctx_get_array_size (depth, mrgctx);
520
521                 if (index < size - 1)
522                         break;
523                 index -= size - 1;
524         }
525
526         tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
527         if (mrgctx)
528                 tramp_size += 4;
529         else
530                 tramp_size += 12;
531         if (aot)
532                 tramp_size += 32;
533
534         code = buf = mono_global_codeman_reserve (tramp_size);
535
536         rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
537
538         if (mrgctx) {
539                 /* get mrgctx ptr */
540                 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
541         } else {
542                 /* load rgctx ptr from vtable */
543                 ppc_ldptr (code, ppc_r4, MONO_STRUCT_OFFSET (MonoVTable, runtime_generic_context), PPC_FIRST_ARG_REG);
544                 /* is the rgctx ptr null? */
545                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
546                 /* if yes, jump to actual trampoline */
547                 rgctx_null_jumps [0] = code;
548                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
549         }
550
551         for (i = 0; i < depth; ++i) {
552                 /* load ptr to next array */
553                 if (mrgctx && i == 0)
554                         ppc_ldptr (code, ppc_r4, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT, ppc_r4);
555                 else
556                         ppc_ldptr (code, ppc_r4, 0, ppc_r4);
557                 /* is the ptr null? */
558                 ppc_compare_reg_imm (code, 0, ppc_r4, 0);
559                 /* if yes, jump to actual trampoline */
560                 rgctx_null_jumps [i + 1] = code;
561                 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
562         }
563
564         /* fetch slot */
565         ppc_ldptr (code, ppc_r4, sizeof (gpointer) * (index  + 1), ppc_r4);
566         /* is the slot null? */
567         ppc_compare_reg_imm (code, 0, ppc_r4, 0);
568         /* if yes, jump to actual trampoline */
569         rgctx_null_jumps [depth + 1] = code;
570         ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
571         /* otherwise return r4 */
572         /* FIXME: if we use r3 as the work register we can avoid this copy */
573         ppc_mr (code, ppc_r3, ppc_r4);
574         ppc_blr (code);
575
576         for (i = mrgctx ? 1 : 0; i <= depth + 1; ++i)
577                 ppc_patch (rgctx_null_jumps [i], code);
578
579         g_free (rgctx_null_jumps);
580
581         /* move the rgctx pointer to the VTABLE register */
582         ppc_mr (code, MONO_ARCH_VTABLE_REG, ppc_r3);
583
584         if (aot) {
585                 code = mono_arch_emit_load_aotconst (buf, code, &ji, MONO_PATCH_INFO_JIT_ICALL_ADDR, g_strdup_printf ("specific_trampoline_lazy_fetch_%u", slot));
586                 /* Branch to the trampoline */
587 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
588                 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
589 #endif
590                 ppc_mtctr (code, ppc_r12);
591                 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
592         } else {
593                 tramp = mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot),
594                         MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
595
596                 /* jump to the actual trampoline */
597                 code = emit_trampoline_jump (code, tramp);
598         }
599
600         mono_arch_flush_icache (buf, code - buf);
601
602         g_assert (code - buf <= tramp_size);
603
604         char *name = mono_get_rgctx_fetch_trampoline_name (slot);
605         *info = mono_tramp_info_create (name, buf, code - buf, ji, unwind_ops);
606         g_free (name);
607
608         return buf;
609 }
610
611 guint8*
612 mono_arch_get_call_target (guint8 *code)
613 {
614         /* Should be a bl */
615         guint32 ins = ((guint32*)(gpointer)code) [-1];
616
617         if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
618                 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
619                 guint8 *target = code - 4 + (disp * 4);
620
621                 return target;
622         } else {
623                 return NULL;
624         }
625 }
626
627 guint32
628 mono_arch_get_plt_info_offset (guint8 *plt_entry, mgreg_t *regs, guint8 *code)
629 {
630 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
631         return ((guint32*)plt_entry) [8];
632 #else
633         return ((guint32*)plt_entry) [6];
634 #endif
635 }
636
637 gpointer
638 mono_arch_get_enter_icall_trampoline (MonoTrampInfo **info)
639 {
640         g_assert_not_reached ();
641         return NULL;
642 }