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