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