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