3 * JIT trampoline code for PowerPC
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>
11 * (C) 2001 Ximian, Inc.
12 * (C) 2007-2008 Andreas Faerber
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>
26 #include "mini-runtime.h"
27 #include "mono/utils/mono-tls-inline.h"
30 /* Same as mono_create_ftnptr, but doesn't require a domain */
32 mono_ppc_create_ftnptr (guint8
*code
)
34 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
35 MonoPPCFunctionDescriptor
*ftnptr
= mono_global_codeman_reserve (sizeof (MonoPPCFunctionDescriptor
));
41 MONO_PROFILER_RAISE (jit_code_buffer
, (ftnptr
, sizeof (MonoPPCFunctionDescriptor
), MONO_PROFILER_CODE_BUFFER_HELPER
, NULL
));
51 * Return the instruction to jump from code to target, 0 if not
52 * reachable with a single instruction
55 branch_for_target_reachable (guint8
*branch
, guint8
*target
)
57 gint diff
= target
- branch
;
58 g_assert ((diff
& 3) == 0);
61 return (18 << 26) | (diff
);
63 /* diff between 0 and -33554432 */
64 if (diff
>= -33554432)
65 return (18 << 26) | (diff
& ~0xfc000000);
71 * get_unbox_trampoline:
73 * @addr: pointer to native code for @m
75 * when value type methods are called through the vtable we need to unbox the
76 * this argument. This method returns a pointer to a trampoline which does
77 * unboxing before calling the method
80 mono_arch_get_unbox_trampoline (MonoMethod
*m
, gpointer addr
)
85 MonoDomain
*domain
= mono_domain_get ();
86 MonoMemoryManager
*mem_manager
= m_method_get_mem_manager (domain
, m
);
87 int size
= MONO_PPC_32_64_CASE (20, 32) + PPC_FTNPTR_SIZE
;
89 addr
= mono_get_addr_from_ftnptr (addr
);
91 start
= code
= mono_mem_manager_code_reserve (mem_manager
, size
);
92 code
= mono_ppc_create_pre_code_ftnptr (code
);
93 short_branch
= branch_for_target_reachable (code
+ 4, (guint8
*)addr
);
95 mono_mem_manager_code_commit (mem_manager
, code
, size
, 8);
98 ppc_addi (code
, this_pos
, this_pos
, MONO_ABI_SIZEOF (MonoObject
));
99 ppc_emit32 (code
, short_branch
);
101 ppc_load_ptr (code
, ppc_r0
, addr
);
102 ppc_mtctr (code
, ppc_r0
);
103 ppc_addi (code
, this_pos
, this_pos
, MONO_ABI_SIZEOF (MonoObject
));
104 ppc_bcctr (code
, 20, 0);
106 mono_arch_flush_icache (start
, code
- start
);
107 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_UNBOX_TRAMPOLINE
, m
));
108 g_assert ((code
- start
) <= size
);
109 /*g_print ("unbox trampoline at %d for %s:%s\n", this_pos, m->klass->name, m->name);
110 g_print ("unbox code is at %p for method at %p\n", start, addr);*/
112 mono_tramp_info_register (mono_tramp_info_create (NULL
, start
, code
- start
, NULL
, NULL
), domain
);
118 * mono_arch_get_static_rgctx_trampoline:
120 * Create a trampoline which sets RGCTX_REG to ARG, then jumps to ADDR.
123 mono_arch_get_static_rgctx_trampoline (MonoMemoryManager
*mem_manager
, gpointer arg
, gpointer addr
)
125 guint8
*code
, *start
, *p
;
126 guint8 imm_buf
[128];
127 guint32 short_branch
;
128 MonoDomain
*domain
= mono_domain_get ();
130 int size
= MONO_PPC_32_64_CASE (24, (PPC_LOAD_SEQUENCE_LENGTH
* 2) + 8) + PPC_FTNPTR_SIZE
;
132 addr
= mono_get_addr_from_ftnptr (addr
);
134 /* Compute size of code needed to emit the arg */
136 ppc_load_ptr (p
, MONO_ARCH_RGCTX_REG
, arg
);
137 imm_size
= p
- imm_buf
;
139 start
= code
= mono_mem_manager_code_reserve (mem_manager
, size
);
140 code
= mono_ppc_create_pre_code_ftnptr (code
);
141 short_branch
= branch_for_target_reachable (code
+ imm_size
, (guint8
*)addr
);
143 mono_mem_manager_code_commit (mem_manager
, code
, size
, imm_size
+ 4);
146 ppc_load_ptr (code
, MONO_ARCH_RGCTX_REG
, arg
);
147 ppc_emit32 (code
, short_branch
);
149 ppc_load_ptr (code
, ppc_r0
, addr
);
150 ppc_mtctr (code
, ppc_r0
);
151 ppc_load_ptr (code
, MONO_ARCH_RGCTX_REG
, arg
);
152 ppc_bcctr (code
, 20, 0);
154 mono_arch_flush_icache (start
, code
- start
);
155 MONO_PROFILER_RAISE (jit_code_buffer
, (start
, code
- start
, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
, NULL
));
156 g_assert ((code
- start
) <= size
);
158 mono_tramp_info_register (mono_tramp_info_create (NULL
, start
, code
- start
, NULL
, NULL
), domain
);
164 mono_arch_patch_callsite (guint8
*method_start
, guint8
*code_ptr
, guint8
*addr
)
166 guint32
*code
= (guint32
*)code_ptr
;
168 addr
= (guint8
*)mono_get_addr_from_ftnptr (addr
);
170 /* This is the 'blrl' instruction */
174 * Note that methods are called also with the bl opcode.
176 if (((*code
) >> 26) == 18) {
177 /*g_print ("direct patching\n");*/
178 ppc_patch ((guint8
*)code
, addr
);
179 mono_arch_flush_icache ((guint8
*)code
, 4);
184 g_assert (mono_ppc_is_direct_call_sequence (code
));
186 ppc_patch ((guint8
*)code
, addr
);
190 mono_arch_patch_plt_entry (guint8
*code
, gpointer
*got
, host_mgreg_t
*regs
, guint8
*addr
)
192 guint32 ins1
, ins2
, offset
;
194 /* Patch the jump table entry used by the plt entry */
196 /* Should be a lis+ori */
197 ins1
= ((guint32
*)code
)[0];
198 g_assert (ins1
>> 26 == 15);
199 ins2
= ((guint32
*)code
)[1];
200 g_assert (ins2
>> 26 == 24);
201 offset
= ((ins1
& 0xffff) << 16) | (ins2
& 0xffff);
203 /* Either got or regs is set */
205 got
= (gpointer
*)(gsize
) regs
[30];
206 *(guint8
**)((guint8
*)got
+ offset
) = addr
;
209 /* Stack size for trampoline function
210 * PPC_MINIMAL_STACK_SIZE + 16 (args + alignment to ppc_magic_trampoline)
211 * + MonoLMF + 14 fp regs + 13 gregs + alignment
213 #define STACK (((PPC_MINIMAL_STACK_SIZE + 4 * sizeof (target_mgreg_t) + sizeof (MonoLMF) + 14 * sizeof (double) + 31 * sizeof (target_mgreg_t)) + (MONO_ARCH_FRAME_ALIGNMENT - 1)) & ~(MONO_ARCH_FRAME_ALIGNMENT - 1))
215 /* Method-specific trampoline code fragment size */
216 #define METHOD_TRAMPOLINE_SIZE 64
218 /* Jump-specific trampoline code fragment size */
219 #define JUMP_TRAMPOLINE_SIZE 64
221 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
222 #define PPC_TOC_REG ppc_r2
224 #define PPC_TOC_REG -1
228 * Stack frame description when the generic trampoline is called.
230 * --------------------
232 * -------------------
233 * Saved FP registers 0-13
234 * -------------------
235 * Saved general registers 0-30
236 * -------------------
237 * param area for 3 args to ppc_magic_trampoline
238 * -------------------
240 * -------------------
243 mono_arch_create_generic_trampoline (MonoTrampolineType tramp_type
, MonoTrampInfo
**info
, gboolean aot
)
245 const char *tramp_name
;
246 guint8
*buf
, *code
= NULL
, *exception_branch
;
247 int i
, offset
, offset_r14
= 0;
248 gconstpointer tramp_handler
;
249 int size
= MONO_PPC_32_64_CASE (700, 900);
250 GSList
*unwind_ops
= NULL
;
251 MonoJumpInfo
*ji
= NULL
;
253 /* Now we'll create in 'buf' the PowerPC trampoline code. This
254 is the trampoline code common to all methods */
256 code
= buf
= mono_global_codeman_reserve (size
);
258 ppc_str_update (code
, ppc_r1
, -STACK
, ppc_r1
);
260 /* start building the MonoLMF on the stack */
261 offset
= STACK
- sizeof (double) * MONO_SAVED_FREGS
;
262 for (i
= 14; i
< 32; i
++) {
263 ppc_stfd (code
, i
, offset
, ppc_r1
);
264 offset
+= sizeof (double);
267 * now the integer registers.
269 offset
= STACK
- sizeof (MonoLMF
) + G_STRUCT_OFFSET (MonoLMF
, iregs
);
270 ppc_str_multiple (code
, ppc_r13
, offset
, ppc_r1
);
272 /* Now save the rest of the registers below the MonoLMF struct, first 14
273 * fp regs and then the 31 gregs.
275 offset
= STACK
- sizeof (MonoLMF
) - (14 * sizeof (double));
276 for (i
= 0; i
< 14; i
++) {
277 ppc_stfd (code
, i
, offset
, ppc_r1
);
278 offset
+= sizeof (double);
280 #define GREGS_OFFSET (STACK - sizeof (MonoLMF) - (14 * sizeof (double)) - (31 * sizeof (target_mgreg_t)))
281 offset
= GREGS_OFFSET
;
282 for (i
= 0; i
< 31; i
++) {
283 ppc_str (code
, i
, offset
, ppc_r1
);
287 offset
+= sizeof (target_mgreg_t
);
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)
294 ppc_mflr (code
, ppc_r0
);
295 ppc_str (code
, ppc_r0
, STACK
+ PPC_RET_ADDR_OFFSET
, ppc_r1
);
297 /* ok, now we can continue with the MonoLMF setup, mostly untouched
298 * from emit_prolog in mini-ppc.c
301 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, GUINT_TO_POINTER (MONO_JIT_ICALL_mono_get_lmf_addr
));
302 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
303 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r12
);
304 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
306 ppc_mtlr (code
, ppc_r12
);
309 ppc_load_func (code
, PPC_CALL_REG
, mono_get_lmf_addr
);
310 ppc_mtlr (code
, PPC_CALL_REG
);
313 /* we build the MonoLMF structure on the stack - see mini-ppc.h
314 * The pointer to the struct is put in ppc_r12.
316 ppc_addi (code
, ppc_r12
, ppc_sp
, STACK
- sizeof (MonoLMF
));
317 ppc_stptr (code
, ppc_r3
, G_STRUCT_OFFSET(MonoLMF
, lmf_addr
), ppc_r12
);
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_r12
);
321 /* *(lmf_addr) = r12 */
322 ppc_stptr (code
, ppc_r12
, 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_r12
);
328 ppc_load (code
, ppc_r0
, 0);
329 ppc_stptr (code
, ppc_r0
, G_STRUCT_OFFSET(MonoLMF
, method
), ppc_r12
);
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_r12
);
334 /* save the IP (caller ip) */
335 if (tramp_type
== MONO_TRAMPOLINE_JUMP
) {
336 ppc_li (code
, ppc_r0
, 0);
338 ppc_ldr (code
, ppc_r0
, STACK
+ PPC_RET_ADDR_OFFSET
, ppc_r1
);
340 ppc_stptr (code
, ppc_r0
, G_STRUCT_OFFSET(MonoLMF
, eip
), ppc_r12
);
343 * Now we are ready to call trampoline (target_mgreg_t *regs, guint8 *code, gpointer value, guint8 *tramp)
344 * Note that the last argument is unused.
346 /* Arg 1: a pointer to the registers */
347 ppc_addi (code
, ppc_r3
, ppc_r1
, GREGS_OFFSET
);
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);
353 ppc_ldr (code
, ppc_r4
, STACK
+ PPC_RET_ADDR_OFFSET
, ppc_r1
);
355 /* Arg 3: trampoline argument */
356 ppc_ldr (code
, ppc_r5
, GREGS_OFFSET
, ppc_r1
);
359 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_JIT_ICALL_ADDR
, GINT_TO_POINTER (mono_trampoline_type_to_jit_icall_id (tramp_type
)));
360 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
361 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r12
);
362 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
364 ppc_mtlr (code
, ppc_r12
);
367 tramp_handler
= mono_get_trampoline_func (tramp_type
);
368 ppc_load_func (code
, PPC_CALL_REG
, tramp_handler
);
369 ppc_mtlr (code
, PPC_CALL_REG
);
373 /* OK, code address is now on r3, move it to r14 for now. */
374 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type
)) {
375 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
376 ppc_ldptr (code
, ppc_r2
, sizeof (target_mgreg_t
), ppc_r3
);
377 ppc_ldptr (code
, ppc_r3
, 0, ppc_r3
);
379 ppc_mr (code
, ppc_r14
, ppc_r3
);
381 // TODO: is here function descriptor unpacking necessary?
382 /* we clobber r3 during interruption checking, so move it somewhere else */
383 ppc_mr (code
, ppc_r14
, ppc_r3
);
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_r12.
392 ppc_addi (code
, ppc_r12
, ppc_r1
, STACK
- sizeof (MonoLMF
));
393 /* r3 = previous_lmf */
394 ppc_ldptr (code
, ppc_r3
, G_STRUCT_OFFSET(MonoLMF
, previous_lmf
), ppc_r12
);
396 ppc_ldptr (code
, ppc_r12
, G_STRUCT_OFFSET(MonoLMF
, lmf_addr
), ppc_r12
);
397 /* *(lmf_addr) = previous_lmf */
398 ppc_stptr (code
, ppc_r3
, G_STRUCT_OFFSET(MonoLMF
, previous_lmf
), ppc_r12
);
400 /* thread interruption check */
402 g_error ("Not implemented");
404 gconstpointer checkpoint
= (gconstpointer
)mono_thread_force_interruption_checkpoint_noraise
;
405 ppc_load_func (code
, PPC_CALL_REG
, checkpoint
);
406 ppc_mtlr (code
, PPC_CALL_REG
);
410 ppc_compare_reg_imm (code
, 0, ppc_r3
, 0);
411 exception_branch
= code
;
412 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
416 /* restore caller frame, as we want to throw from there */
417 ppc_ldr (code
, ppc_r14
, offset_r14
, ppc_r1
); /* unclobber r14 */
418 ppc_ldr (code
, ppc_r1
, 0, ppc_r1
);
419 ppc_ldr (code
, ppc_r12
, PPC_RET_ADDR_OFFSET
, ppc_r1
);
420 ppc_mtlr (code
, ppc_r12
);
423 g_error ("Not implemented");
425 ppc_load_func (code
, PPC_CALL_REG
, mono_get_rethrow_preserve_exception_addr ());
426 ppc_ldr (code
, PPC_CALL_REG
, 0, PPC_CALL_REG
);
427 ppc_mtctr (code
, PPC_CALL_REG
);
429 ppc_bcctr (code
, 20, 0);
430 ppc_break (code
); /* never reached */
432 ppc_patch (exception_branch
, code
);
434 /* no exception case */
435 if (!MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type
)) {
436 /* we don't do any calls anymore, so code address can be safely moved
437 * into counter register */
438 ppc_mtctr (code
, ppc_r14
);
440 ppc_mr (code
, ppc_r3
, ppc_r14
);
443 ppc_addi (code
, ppc_r12
, ppc_r1
, STACK
- sizeof (MonoLMF
));
445 ppc_ldr_multiple (code
, ppc_r13
, G_STRUCT_OFFSET(MonoLMF
, iregs
), ppc_r12
);
447 for (i
= 14; i
< 32; i
++)
448 ppc_lfd (code
, i
, G_STRUCT_OFFSET(MonoLMF
, fregs
) + ((i
-14) * sizeof (gdouble
)), ppc_r12
);
450 /* restore the volatile registers, we skip r1, of course */
451 offset
= STACK
- sizeof (MonoLMF
) - (14 * sizeof (double));
452 for (i
= 0; i
< 14; i
++) {
453 ppc_lfd (code
, i
, offset
, ppc_r1
);
454 offset
+= sizeof (double);
456 offset
= STACK
- sizeof (MonoLMF
) - (14 * sizeof (double)) - (31 * sizeof (target_mgreg_t
));
457 ppc_ldr (code
, ppc_r0
, offset
, ppc_r1
);
458 offset
+= 2 * sizeof (target_mgreg_t
);
459 for (i
= 2; i
< 13; i
++) {
460 if (i
!= PPC_TOC_REG
&& (i
!= 3 || tramp_type
!= MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
))
461 ppc_ldr (code
, i
, offset
, ppc_r1
);
462 offset
+= sizeof (target_mgreg_t
);
465 /* Non-standard function epilogue. Instead of doing a proper
466 * return, we just jump to the compiled code.
468 /* Restore stack pointer and LR and jump to the code */
469 ppc_ldr (code
, ppc_r1
, 0, ppc_r1
);
470 ppc_ldr (code
, ppc_r12
, PPC_RET_ADDR_OFFSET
, ppc_r1
);
471 ppc_mtlr (code
, ppc_r12
);
472 if (MONO_TRAMPOLINE_TYPE_MUST_RETURN (tramp_type
))
475 ppc_bcctr (code
, 20, 0);
477 /* Flush instruction cache, since we've generated code */
478 mono_arch_flush_icache (buf
, code
- buf
);
479 MONO_PROFILER_RAISE (jit_code_buffer
, (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_HELPER
, NULL
));
482 g_assert ((code
- buf
) <= size
);
485 tramp_name
= mono_get_generic_trampoline_name (tramp_type
);
486 *info
= mono_tramp_info_create (tramp_name
, buf
, code
- buf
, ji
, unwind_ops
);
491 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
493 mono_arch_create_specific_trampoline (gpointer arg1
, MonoTrampolineType tramp_type
, MonoMemoryManager
*mem_manager
, guint32
*code_len
)
495 guint8
*code
, *buf
, *tramp
;
496 guint32 short_branch
;
498 tramp
= mono_get_trampoline_code (tramp_type
);
500 code
= buf
= (guint8
*)mono_mem_manager_code_reserve_align (mem_manager
, TRAMPOLINE_SIZE
, 4);
501 short_branch
= branch_for_target_reachable (code
+ MONO_PPC_32_64_CASE (8, 5*4), tramp
);
502 #ifdef __mono_ppc64__
503 /* FIXME: make shorter if possible */
506 mono_mem_manager_code_commit (mem_manager
, code
, TRAMPOLINE_SIZE
, 12);
510 ppc_load_sequence (code
, ppc_r0
, (target_mgreg_t
)(gsize
) arg1
);
511 ppc_emit32 (code
, short_branch
);
513 /* Prepare the jump to the generic trampoline code.*/
514 ppc_load_ptr (code
, ppc_r0
, tramp
);
515 ppc_mtctr (code
, ppc_r0
);
517 /* And finally put 'arg1' in r0 and fly! */
518 ppc_load_ptr (code
, ppc_r0
, arg1
);
519 ppc_bcctr (code
, 20, 0);
522 /* Flush instruction cache, since we've generated code */
523 mono_arch_flush_icache (buf
, code
- buf
);
524 MONO_PROFILER_RAISE (jit_code_buffer
, (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_SPECIFIC_TRAMPOLINE
, mono_get_generic_trampoline_simple_name (tramp_type
)));
526 g_assert ((code
- buf
) <= TRAMPOLINE_SIZE
);
529 *code_len
= code
- buf
;
535 emit_trampoline_jump (guint8
*code
, guint8
*tramp
)
537 guint32 short_branch
= branch_for_target_reachable (code
, tramp
);
539 /* FIXME: we can save a few bytes here by committing if the
540 short branch is possible */
542 ppc_emit32 (code
, short_branch
);
544 ppc_load_ptr (code
, ppc_r0
, tramp
);
545 ppc_mtctr (code
, ppc_r0
);
546 ppc_bcctr (code
, 20, 0);
553 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot
, MonoTrampInfo
**info
, gboolean aot
)
557 guint8
**rgctx_null_jumps
;
562 MonoJumpInfo
*ji
= NULL
;
563 GSList
*unwind_ops
= NULL
;
565 mrgctx
= MONO_RGCTX_SLOT_IS_MRGCTX (slot
);
566 index
= MONO_RGCTX_SLOT_INDEX (slot
);
568 index
+= MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT
/ sizeof (target_mgreg_t
);
569 for (depth
= 0; ; ++depth
) {
570 int size
= mono_class_rgctx_get_array_size (depth
, mrgctx
);
572 if (index
< size
- 1)
577 tramp_size
= MONO_PPC_32_64_CASE (40, 52) + 12 * depth
;
585 code
= buf
= mono_global_codeman_reserve (tramp_size
);
587 rgctx_null_jumps
= g_malloc (sizeof (guint8
*) * (depth
+ 2));
591 ppc_mr (code
, ppc_r4
, PPC_FIRST_ARG_REG
);
593 /* load rgctx ptr from vtable */
594 ppc_ldptr (code
, ppc_r4
, MONO_STRUCT_OFFSET (MonoVTable
, runtime_generic_context
), PPC_FIRST_ARG_REG
);
595 /* is the rgctx ptr null? */
596 ppc_compare_reg_imm (code
, 0, ppc_r4
, 0);
597 /* if yes, jump to actual trampoline */
598 rgctx_null_jumps
[0] = code
;
599 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
602 for (i
= 0; i
< depth
; ++i
) {
603 /* load ptr to next array */
604 if (mrgctx
&& i
== 0)
605 ppc_ldptr (code
, ppc_r4
, MONO_SIZEOF_METHOD_RUNTIME_GENERIC_CONTEXT
, ppc_r4
);
607 ppc_ldptr (code
, ppc_r4
, 0, ppc_r4
);
608 /* is the ptr null? */
609 ppc_compare_reg_imm (code
, 0, ppc_r4
, 0);
610 /* if yes, jump to actual trampoline */
611 rgctx_null_jumps
[i
+ 1] = code
;
612 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
616 ppc_ldptr (code
, ppc_r4
, sizeof (target_mgreg_t
) * (index
+ 1), ppc_r4
);
617 /* is the slot null? */
618 ppc_compare_reg_imm (code
, 0, ppc_r4
, 0);
619 /* if yes, jump to actual trampoline */
620 rgctx_null_jumps
[depth
+ 1] = code
;
621 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
622 /* otherwise return r4 */
623 /* FIXME: if we use r3 as the work register we can avoid this copy */
624 ppc_mr (code
, ppc_r3
, ppc_r4
);
627 for (i
= mrgctx
? 1 : 0; i
<= depth
+ 1; ++i
)
628 ppc_patch (rgctx_null_jumps
[i
], code
);
630 g_free (rgctx_null_jumps
);
632 /* move the rgctx pointer to the VTABLE register */
633 ppc_mr (code
, MONO_ARCH_VTABLE_REG
, ppc_r3
);
636 code
= mono_arch_emit_load_aotconst (buf
, code
, &ji
, MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
, GUINT_TO_POINTER (slot
));
637 /* Branch to the trampoline */
638 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
639 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
641 ppc_mtctr (code
, ppc_r12
);
642 ppc_bcctr (code
, PPC_BR_ALWAYS
, 0);
644 MonoMemoryManager
*mem_manager
= mono_domain_ambient_memory_manager (mono_get_root_domain ());
645 tramp
= (guint8
*)mono_arch_create_specific_trampoline (GUINT_TO_POINTER (slot
),
646 MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
, mem_manager
, NULL
);
648 /* jump to the actual trampoline */
649 code
= emit_trampoline_jump (code
, tramp
);
652 mono_arch_flush_icache (buf
, code
- buf
);
653 MONO_PROFILER_RAISE (jit_code_buffer
, (buf
, code
- buf
, MONO_PROFILER_CODE_BUFFER_GENERICS_TRAMPOLINE
, NULL
));
655 g_assert (code
- buf
<= tramp_size
);
657 char *name
= mono_get_rgctx_fetch_trampoline_name (slot
);
658 *info
= mono_tramp_info_create (name
, buf
, code
- buf
, ji
, unwind_ops
);
665 mono_arch_get_call_target (guint8
*code
)
668 guint32 ins
= ((guint32
*)code
) [-1];
670 if ((ins
>> 26 == 18) && ((ins
& 1) == 1) && ((ins
& 2) == 0)) {
671 gint32 disp
= (((gint32
)ins
) >> 2) & 0xffffff;
672 guint8
*target
= code
- 4 + (disp
* 4);
681 mono_arch_get_plt_info_offset (guint8
*plt_entry
, host_mgreg_t
*regs
, guint8
*code
)
683 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
684 return ((guint32
*)plt_entry
) [8];
686 return ((guint32
*)plt_entry
) [6];