[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mono / mini / tramp-ppc.c
blob54ae419e97770f26e24f2abca747a70b720e11c5
1 /**
2 * \file
3 * JIT trampoline code for PowerPC
5 * Authors:
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
8 * Carlos Valiente <yo@virutass.net>
9 * Andreas Faerber <andreas.faerber@web.de>
11 * (C) 2001 Ximian, Inc.
12 * (C) 2007-2008 Andreas Faerber
15 #include <config.h>
16 #include <glib.h>
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>
24 #include "mini.h"
25 #include "mini-ppc.h"
26 #include "mini-runtime.h"
27 #include "mono/utils/mono-tls-inline.h"
29 #if 0
30 /* Same as mono_create_ftnptr, but doesn't require a domain */
31 static gpointer
32 mono_ppc_create_ftnptr (guint8 *code)
34 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
35 MonoPPCFunctionDescriptor *ftnptr = mono_global_codeman_reserve (sizeof (MonoPPCFunctionDescriptor));
37 ftnptr->code = code;
38 ftnptr->toc = NULL;
39 ftnptr->env = NULL;
41 MONO_PROFILER_RAISE (jit_code_buffer, (ftnptr, sizeof (MonoPPCFunctionDescriptor), MONO_PROFILER_CODE_BUFFER_HELPER, NULL));
43 return ftnptr;
44 #else
45 return code;
46 #endif
48 #endif
51 * Return the instruction to jump from code to target, 0 if not
52 * reachable with a single instruction
54 static guint32
55 branch_for_target_reachable (guint8 *branch, guint8 *target)
57 gint diff = target - branch;
58 g_assert ((diff & 3) == 0);
59 if (diff >= 0) {
60 if (diff <= 33554431)
61 return (18 << 26) | (diff);
62 } else {
63 /* diff between 0 and -33554432 */
64 if (diff >= -33554432)
65 return (18 << 26) | (diff & ~0xfc000000);
67 return 0;
71 * get_unbox_trampoline:
72 * @m: method pointer
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
79 gpointer
80 mono_arch_get_unbox_trampoline (MonoMethod *m, gpointer addr)
82 guint8 *code, *start;
83 int this_pos = 3;
84 guint32 short_branch;
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);
94 if (short_branch)
95 mono_mem_manager_code_commit (mem_manager, code, size, 8);
97 if (short_branch) {
98 ppc_addi (code, this_pos, this_pos, MONO_ABI_SIZEOF (MonoObject));
99 ppc_emit32 (code, short_branch);
100 } else {
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);
114 return start;
118 * mono_arch_get_static_rgctx_trampoline:
120 * Create a trampoline which sets RGCTX_REG to ARG, then jumps to ADDR.
122 gpointer
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 ();
129 int imm_size;
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 */
135 p = imm_buf;
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);
142 if (short_branch)
143 mono_mem_manager_code_commit (mem_manager, code, size, imm_size + 4);
145 if (short_branch) {
146 ppc_load_ptr (code, MONO_ARCH_RGCTX_REG, arg);
147 ppc_emit32 (code, short_branch);
148 } else {
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);
160 return start;
163 void
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 */
171 --code;
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);
180 return;
183 /* Sanity check */
184 g_assert (mono_ppc_is_direct_call_sequence (code));
186 ppc_patch ((guint8*)code, addr);
189 void
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 */
204 if (!got)
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
223 #else
224 #define PPC_TOC_REG -1
225 #endif
228 * Stack frame description when the generic trampoline is called.
229 * caller frame
230 * --------------------
231 * MonoLMF
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 * -------------------
239 * linkage area
240 * -------------------
242 guchar*
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);
284 if (i == ppc_r14) {
285 offset_r14 = offset;
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
300 if (aot) {
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);
305 #endif
306 ppc_mtlr (code, ppc_r12);
307 ppc_blrl (code);
308 } else {
309 ppc_load_func (code, PPC_CALL_REG, mono_get_lmf_addr);
310 ppc_mtlr (code, PPC_CALL_REG);
311 ppc_blrl (code);
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);
327 } else {
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);
337 } else {
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);
352 else
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);
358 if (aot) {
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);
363 #endif
364 ppc_mtlr (code, ppc_r12);
365 ppc_blrl (code);
366 } else {
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);
370 ppc_blrl (code);
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);
378 #endif
379 ppc_mr (code, ppc_r14, ppc_r3);
380 } else {
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);
395 /* r12 = lmf_addr */
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 */
401 if (aot) {
402 g_error ("Not implemented");
403 } else {
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);
408 ppc_blrl (code);
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);
414 /* exception case */
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);
422 if (aot) {
423 g_error ("Not implemented");
424 } else {
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);
439 } else {
440 ppc_mr (code, ppc_r3, ppc_r14);
443 ppc_addi (code, ppc_r12, ppc_r1, STACK - sizeof (MonoLMF));
444 /* restore iregs */
445 ppc_ldr_multiple (code, ppc_r13, G_STRUCT_OFFSET(MonoLMF, iregs), ppc_r12);
446 /* restore fregs */
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))
473 ppc_blr (code);
474 else
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));
481 /* Sanity check */
482 g_assert ((code - buf) <= size);
484 g_assert (info);
485 tramp_name = mono_get_generic_trampoline_name (tramp_type);
486 *info = mono_tramp_info_create (tramp_name, buf, code - buf, ji, unwind_ops);
488 return buf;
491 #define TRAMPOLINE_SIZE (MONO_PPC_32_64_CASE (24, (5+5+1+1)*4))
492 gpointer
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 */
504 #else
505 if (short_branch)
506 mono_mem_manager_code_commit (mem_manager, code, TRAMPOLINE_SIZE, 12);
507 #endif
509 if (short_branch) {
510 ppc_load_sequence (code, ppc_r0, (target_mgreg_t)(gsize) arg1);
511 ppc_emit32 (code, short_branch);
512 } else {
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);
528 if (code_len)
529 *code_len = code - buf;
531 return buf;
534 static guint8*
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 */
541 if (short_branch) {
542 ppc_emit32 (code, short_branch);
543 } else {
544 ppc_load_ptr (code, ppc_r0, tramp);
545 ppc_mtctr (code, ppc_r0);
546 ppc_bcctr (code, 20, 0);
549 return code;
552 gpointer
553 mono_arch_create_rgctx_lazy_fetch_trampoline (guint32 slot, MonoTrampInfo **info, gboolean aot)
555 guint8 *tramp;
556 guint8 *code, *buf;
557 guint8 **rgctx_null_jumps;
558 int tramp_size;
559 int depth, index;
560 int i;
561 gboolean mrgctx;
562 MonoJumpInfo *ji = NULL;
563 GSList *unwind_ops = NULL;
565 mrgctx = MONO_RGCTX_SLOT_IS_MRGCTX (slot);
566 index = MONO_RGCTX_SLOT_INDEX (slot);
567 if (mrgctx)
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)
573 break;
574 index -= size - 1;
577 tramp_size = MONO_PPC_32_64_CASE (40, 52) + 12 * depth;
578 if (mrgctx)
579 tramp_size += 4;
580 else
581 tramp_size += 12;
582 if (aot)
583 tramp_size += 32;
585 code = buf = mono_global_codeman_reserve (tramp_size);
587 rgctx_null_jumps = g_malloc (sizeof (guint8*) * (depth + 2));
589 if (mrgctx) {
590 /* get mrgctx ptr */
591 ppc_mr (code, ppc_r4, PPC_FIRST_ARG_REG);
592 } else {
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);
606 else
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);
615 /* fetch slot */
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);
625 ppc_blr (code);
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);
635 if (aot) {
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);
640 #endif
641 ppc_mtctr (code, ppc_r12);
642 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
643 } else {
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);
659 g_free (name);
661 return buf;
664 guint8*
665 mono_arch_get_call_target (guint8 *code)
667 /* Should be a bl */
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);
674 return target;
675 } else {
676 return NULL;
680 guint32
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];
685 #else
686 return ((guint32*)plt_entry) [6];
687 #endif