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 EnterCriticalSection (metadata_section);
91 addr = mono_compile_method (m);
92 LeaveCriticalSection (metadata_section);
95 /* the method was jumped to */
99 /* go to the start of the call instruction
101 * address_byte = (m << 6) | (o << 3) | reg
102 * call opcode: 0xff address_byte displacement
107 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
108 reg = code [4] & 0x07;
109 disp = (signed char)code [5];
111 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
112 reg = code [1] & 0x07;
113 disp = *((gint32*)(code + 2));
114 } else if ((code [1] == 0xe8)) {
115 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
117 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
119 * This is a interface call: should check the above code can't catch it earlier
120 * 8b 40 30 mov 0x30(%eax),%eax
124 reg = code [5] & 0x07;
126 printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
127 code [4], code [5], code [6]);
128 g_assert_not_reached ();
134 if (m->klass->valuetype) {
135 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
137 return *((gpointer *)o) = addr;
142 create_trampoline_code (int is_jump)
145 static guint8* generic_jump_trampoline = NULL;
148 if (generic_jump_trampoline)
149 return generic_jump_trampoline;
151 if (mono_generic_trampoline_code)
152 return mono_generic_trampoline_code;
155 code = buf = g_malloc (256);
156 /* save caller save regs because we need to do a call */
157 //x86_push_reg (buf, X86_EDX);
158 //x86_push_reg (buf, X86_EAX);
159 //x86_push_reg (buf, X86_ECX);
162 /* save the IP (caller ip) */
164 x86_push_imm (buf, 0);
166 x86_push_membase (buf, X86_ESP, 16);
168 x86_push_reg (buf, X86_EBX);
169 x86_push_reg (buf, X86_EDI);
170 x86_push_reg (buf, X86_ESI);
171 x86_push_reg (buf, X86_EBP);
173 /* save method info */
174 x86_push_membase (buf, X86_ESP, 32);
175 /* get the address of lmf for the current thread */
176 x86_call_code (buf, mono_get_lmf_addr);
178 x86_push_reg (buf, X86_EAX);
179 /* push *lfm (previous_lmf) */
180 x86_push_membase (buf, X86_EAX, 0);
182 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
185 /* push the method info */
186 x86_push_membase (buf, X86_ESP, 44);
187 /* push the return address onto the stack */
189 x86_push_imm (buf, 0);
191 x86_push_membase (buf, X86_ESP, 52);
193 /* save all register values */
194 x86_push_reg (buf, X86_EBX);
195 x86_push_reg (buf, X86_EDI);
196 x86_push_reg (buf, X86_ESI);
197 x86_push_membase (buf, X86_ESP, 64); /* EDX */
198 x86_push_membase (buf, X86_ESP, 64); /* ECX */
199 x86_push_membase (buf, X86_ESP, 64); /* EAX */
201 x86_call_code (buf, x86_magic_trampoline);
202 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
204 /* restore LMF start */
205 /* ebx = previous_lmf */
206 x86_pop_reg (buf, X86_EBX);
208 x86_pop_reg (buf, X86_EDI);
209 /* *(lmf) = previous_lmf */
210 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
211 /* discard method info */
212 x86_pop_reg (buf, X86_ESI);
213 /* restore caller saved regs */
214 x86_pop_reg (buf, X86_EBP);
215 x86_pop_reg (buf, X86_ESI);
216 x86_pop_reg (buf, X86_EDI);
217 x86_pop_reg (buf, X86_EBX);
218 /* discard save IP */
219 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
220 /* restore LMF end */
222 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
224 /* call the compiled method */
225 x86_jump_reg (buf, X86_EAX);
228 g_assert ((buf - code) <= 256);
231 return generic_jump_trampoline = code;
233 return mono_generic_trampoline_code = code;
237 #define TRAMPOLINE_SIZE 10
240 mono_arch_create_jump_trampoline (MonoMethod *method)
242 guint8 *code, *buf, *tramp;
244 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
245 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
247 /* icalls use method->addr */
248 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
249 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
252 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
253 mono_lookup_pinvoke_call (method);
255 #ifdef MONO_USE_EXC_TABLES
256 if (mono_method_blittable (method)) {
260 nm = mono_marshal_get_native_wrapper (method);
261 return mono_compile_method (nm);
262 #ifdef MONO_USE_EXC_TABLES
267 tramp = create_trampoline_code (TRUE);
269 code = buf = g_malloc (TRAMPOLINE_SIZE);
270 //x86_push_imm (buf, method);
271 //x86_jump_code (buf, tramp);
272 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
274 mono_jit_stats.method_trampolines++;
281 * mono_arch_create_jit_trampoline:
282 * @method: pointer to the method info
284 * Creates a trampoline function for virtual methods. If the created
285 * code is called it first starts JIT compilation of method,
286 * and then calls the newly created method. I also replaces the
287 * corresponding vtable entry (see x86_magic_trampoline).
289 * Returns: a pointer to the newly created code
292 mono_arch_create_jit_trampoline (MonoMethod *method)
296 /* previously created trampoline code */
300 /* we immediately compile runtime provided functions */
301 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
302 method->info = mono_compile_method (method);
306 /* icalls use method->addr */
307 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
308 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
311 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
312 mono_lookup_pinvoke_call (method);
314 #ifdef MONO_USE_EXC_TABLES
315 if (mono_method_blittable (method)) {
316 method->info = method->addr;
319 nm = mono_marshal_get_native_wrapper (method);
320 method->info = mono_compile_method (nm);
321 #ifdef MONO_USE_EXC_TABLES
327 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
328 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
330 create_trampoline_code (FALSE);
332 code = buf = g_malloc (TRAMPOLINE_SIZE);
333 //x86_push_imm (buf, method);
334 //x86_jump_code (buf, mono_generic_trampoline_code);
335 g_assert ((buf - code) <= TRAMPOLINE_SIZE);
337 /* store trampoline address */
340 mono_jit_stats.method_trampolines++;
346 * This method is only called when running in the Mono Debugger.
349 mono_debugger_create_notification_function (gpointer *notification_address)
353 ptr = buf = g_malloc0 (16);
354 //x86_breakpoint (buf);
355 if (notification_address)
356 *notification_address = buf;