2 * trampolinesparc.c: JIT trampoline code for Sparc 64
5 * Mark Crichton (crichton@gimp.org)
6 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2003 Ximian, Inc.
14 #include <mono/arch/sparc/sparc-codegen.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/mono-debug-debugger.h>
21 #include "mini-sparc.h"
25 /* adapt to mini later... */
26 #define mono_jit_share_code (1)
29 * Address of the Sparc trampoline code. This is used by the debugger to check
30 * whether a method is a trampoline.
32 guint8 *mono_generic_trampoline_code = NULL;
35 * get_unbox_trampoline:
37 * @addr: pointer to native code for @m
39 * when value type methods are called through the vtable we need to unbox the
40 * this argument. This method returns a pointer to a trampoline which does
41 * unboxing before calling the method
44 get_unbox_trampoline (MonoMethod *m, gpointer addr)
49 if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
52 start = code = g_malloc (16);
54 //x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
55 //x86_jump_code (code, addr);
56 g_assert ((code - start) < 16);
62 * x86_magic_trampoline:
63 * @eax: saved x86 register
64 * @ecx: saved x86 register
65 * @edx: saved x86 register
66 * @esi: saved x86 register
67 * @edi: saved x86 register
68 * @ebx: saved x86 register
69 * @code: pointer into caller code
70 * @method: the method to translate
72 * This method is called by the trampoline functions for virtual
73 * methods. It inspects the caller code to find the address of the
74 * vtable slot, then calls the JIT compiler and writes the address
75 * of the compiled method back to the vtable. All virtual methods
76 * are called with: x86_call_membase (inst, basereg, disp). We always
77 * use 32 bit displacement to ensure that the length of the call
78 * instruction is 6 bytes. We need to get the value of the basereg
79 * and the constant displacement.
82 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
83 int ebx, guint8 *code, MonoMethod *m)
90 addr = mono_compile_method (m);
93 /* the method was jumped to */
97 /* go to the start of the call instruction
99 * address_byte = (m << 6) | (o << 3) | reg
100 * call opcode: 0xff address_byte displacement
105 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
106 reg = code [4] & 0x07;
107 disp = (signed char)code [5];
109 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
110 reg = code [1] & 0x07;
111 disp = *((gint32*)(code + 2));
112 } else if ((code [1] == 0xe8)) {
113 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
115 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
117 * This is a interface call: should check the above code can't catch it earlier
118 * 8b 40 30 mov 0x30(%eax),%eax
122 reg = code [5] & 0x07;
124 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
125 code [4], code [5], code [6]);
126 g_assert_not_reached ();
132 if (m->klass->valuetype) {
133 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
135 return *((gpointer *)o) = addr;
140 create_trampoline_code (int is_jump)
143 static guint8* generic_jump_trampoline = NULL;
146 if (generic_jump_trampoline)
147 return generic_jump_trampoline;
149 if (mono_generic_trampoline_code)
150 return mono_generic_trampoline_code;
153 code = buf = g_malloc (256);
154 /* save caller save regs because we need to do a call */
155 //x86_push_reg (buf, X86_EDX);
156 //x86_push_reg (buf, X86_EAX);
157 //x86_push_reg (buf, X86_ECX);
160 /* save the IP (caller ip) */
162 x86_push_imm (buf, 0);
164 x86_push_membase (buf, X86_ESP, 16);
166 x86_push_reg (buf, X86_EBX);
167 x86_push_reg (buf, X86_EDI);
168 x86_push_reg (buf, X86_ESI);
169 x86_push_reg (buf, X86_EBP);
171 /* save method info */
172 x86_push_membase (buf, X86_ESP, 32);
173 /* get the address of lmf for the current thread */
174 x86_call_code (buf, mono_get_lmf_addr);
176 x86_push_reg (buf, X86_EAX);
177 /* push *lfm (previous_lmf) */
178 x86_push_membase (buf, X86_EAX, 0);
180 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
183 /* push the method info */
184 x86_push_membase (buf, X86_ESP, 44);
185 /* push the return address onto the stack */
187 x86_push_imm (buf, 0);
189 x86_push_membase (buf, X86_ESP, 52);
191 /* save all register values */
192 x86_push_reg (buf, X86_EBX);
193 x86_push_reg (buf, X86_EDI);
194 x86_push_reg (buf, X86_ESI);
195 x86_push_membase (buf, X86_ESP, 64); /* EDX */
196 x86_push_membase (buf, X86_ESP, 64); /* ECX */
197 x86_push_membase (buf, X86_ESP, 64); /* EAX */
199 x86_call_code (buf, x86_magic_trampoline);
200 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
202 /* restore LMF start */
203 /* ebx = previous_lmf */
204 x86_pop_reg (buf, X86_EBX);
206 x86_pop_reg (buf, X86_EDI);
207 /* *(lmf) = previous_lmf */
208 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
209 /* discard method info */
210 x86_pop_reg (buf, X86_ESI);
211 /* restore caller saved regs */
212 x86_pop_reg (buf, X86_EBP);
213 x86_pop_reg (buf, X86_ESI);
214 x86_pop_reg (buf, X86_EDI);
215 x86_pop_reg (buf, X86_EBX);
216 /* discard save IP */
217 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
218 /* restore LMF end */
220 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
222 /* call the compiled method */
223 x86_jump_reg (buf, X86_EAX);
226 g_assert ((buf - code) <= 256);
229 return generic_jump_trampoline = code;
231 return mono_generic_trampoline_code = code;
235 #define TRAMPOLINE_SIZE 10
238 mono_arch_create_jump_trampoline (MonoMethod *method)
240 guint8 *code, *buf, *tramp;
242 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
243 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
245 /* icalls use method->addr */
246 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
247 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
250 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
251 mono_lookup_pinvoke_call (method);
253 #ifdef MONO_USE_EXC_TABLES
254 if (mono_method_blittable (method)) {
258 nm = mono_marshal_get_native_wrapper (method);
259 return mono_compile_method (nm);
260 #ifdef MONO_USE_EXC_TABLES
265 tramp = create_trampoline_code (TRUE);
267 code = buf = g_malloc (TRAMPOLINE_SIZE);
268 //x86_push_imm (buf, method);
269 //x86_jump_code (buf, tramp);
270 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
272 mono_jit_stats.method_trampolines++;
279 * mono_arch_create_jit_trampoline:
280 * @method: pointer to the method info
282 * Creates a trampoline function for virtual methods. If the created
283 * code is called it first starts JIT compilation of method,
284 * and then calls the newly created method. I also replaces the
285 * corresponding vtable entry (see x86_magic_trampoline).
287 * Returns: a pointer to the newly created code
290 mono_arch_create_jit_trampoline (MonoMethod *method)
294 /* previously created trampoline code */
298 /* we immediately compile runtime provided functions */
299 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
300 method->info = mono_compile_method (method);
304 /* icalls use method->addr */
305 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
306 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
309 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
310 mono_lookup_pinvoke_call (method);
312 #ifdef MONO_USE_EXC_TABLES
313 if (mono_method_blittable (method)) {
314 method->info = method->addr;
317 nm = mono_marshal_get_native_wrapper (method);
318 method->info = mono_compile_method (nm);
319 #ifdef MONO_USE_EXC_TABLES
325 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
326 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
328 create_trampoline_code (FALSE);
330 code = buf = g_malloc (TRAMPOLINE_SIZE);
331 //x86_push_imm (buf, method);
332 //x86_jump_code (buf, mono_generic_trampoline_code);
333 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
335 /* store trampoline address */
338 mono_jit_stats.method_trampolines++;
344 * This method is only called when running in the Mono Debugger.
347 mono_debugger_create_notification_function (gpointer *notification_address)
351 ptr = buf = g_malloc0 (16);
352 //x86_breakpoint (buf);
353 if (notification_address)
354 *notification_address = buf;