2 * trampoline.c: JIT trampoline code
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/tabledefs.h>
15 #include <mono/arch/x86/x86-codegen.h>
22 arch_remoting_invoke (MonoMethod *method, gpointer ip, gpointer first_arg)
24 MonoMethodSignature *sig = method->signature;
25 MonoMethodMessage *msg;
26 MonoTransparentProxy *this;
27 MonoObject *res, *exc;
30 static MonoObject *(*invoke) (gpointer proxy, gpointer msg,
31 MonoObject **exc, MonoArray **out_args) = NULL;
33 //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
36 if (ISSTRUCT (sig->ret))
39 this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
41 g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
43 msg = mono_method_call_message_new (method, &first_arg);
45 /* fixme: make this domain dependent */
50 klass = mono_defaults.real_proxy_class;
52 for (i = 0; i < klass->method.count; ++i) {
53 if (!strcmp ("PrivateInvoke", klass->methods [i]->name) &&
54 klass->methods [i]->signature->param_count == 4) {
55 invoke = arch_compile_method (klass->methods [i]);
64 res = invoke (this->rp, msg, &exc, &out_args);
67 mono_raise_exception ((MonoException *)exc);
69 mono_method_return_message_restore (method, &first_arg, res, out_args);
71 /* WARNING: do not write any code here, because that would destroy
77 * get_unbox_trampoline:
79 * @addr: pointer to native code for @m
81 * when value type methods are called through the vtable we need to unbox the
82 * this argument. This method returns a pointer to a trampoline which does
83 * unboxing before calling the method
86 get_unbox_trampoline (MonoMethod *m, gpointer addr)
91 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
94 start = code = g_malloc (16);
96 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
97 x86_jump_code (code, addr);
98 g_assert ((code - start) < 16);
104 * x86_magic_trampoline:
105 * @eax: saved x86 register
106 * @ecx: saved x86 register
107 * @edx: saved x86 register
108 * @esi: saved x86 register
109 * @edi: saved x86 register
110 * @ebx: saved x86 register
111 * @code: pointer into caller code
112 * @method: the method to translate
114 * This method is called by the trampoline functions for virtual
115 * methods. It inspects the caller code to find the address of the
116 * vtable slot, then calls the JIT compiler and writes the address
117 * of the compiled method back to the vtable. All virtual methods
118 * are called with: x86_call_membase (inst, basereg, disp). We always
119 * use 32 bit displacement to ensure that the length of the call
120 * instruction is 6 bytes. We need to get the value of the basereg
121 * and the constant displacement.
124 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
125 int ebx, const guint8 *code, MonoMethod *m)
132 EnterCriticalSection (metadata_section);
133 addr = arch_compile_method (m);
134 LeaveCriticalSection (metadata_section);
137 /* go to the start of the call instruction
139 * address_byte = (m << 6) | (o << 3) | reg
140 * call opcode: 0xff address_byte displacement
145 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
146 reg = code [4] & 0x07;
147 disp = (signed char)code [5];
149 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
150 reg = code [1] & 0x07;
151 disp = *((gint32*)(code + 2));
152 } else if ((code [1] == 0xe8)) {
153 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
156 printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
158 g_assert_not_reached ();
182 g_assert_not_reached ();
187 if (m->klass->valuetype) {
188 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
190 return *((gpointer *)o) = addr;
195 * arch_create_jit_trampoline:
196 * @method: pointer to the method info
198 * Creates a trampoline function for virtual methods. If the created
199 * code is called it first starts JIT compilation of method,
200 * and then calls the newly created method. I also replaces the
201 * corresponding vtable entry (see x86_magic_trampoline).
203 * Returns: a pointer to the newly created code
206 arch_create_jit_trampoline (MonoMethod *method)
208 MonoDomain *domain = mono_domain_get ();
210 static guint8 *vc = NULL;
211 GHashTable *jit_code_hash;
213 /* previously created trampoline code */
217 /* we immediately compile runtime provided functions */
218 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
219 method->info = arch_compile_method (method);
223 /* icalls use method->addr */
224 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
225 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
226 method->info = arch_create_native_wrapper (method);
230 /* check if we already have JITed code */
231 if (mono_jit_share_code)
232 jit_code_hash = mono_root_domain->jit_code_hash;
234 jit_code_hash = domain->jit_code_hash;
236 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
237 mono_jit_stats.methods_lookups++;
242 vc = buf = g_malloc (256);
244 /* save caller save regs because we need to do a call */
245 x86_push_reg (buf, X86_EDX);
246 x86_push_reg (buf, X86_EAX);
247 x86_push_reg (buf, X86_ECX);
251 /* save the IP (caller ip) */
252 x86_push_membase (buf, X86_ESP, 16);
254 x86_push_reg (buf, X86_EBX);
255 x86_push_reg (buf, X86_EDI);
256 x86_push_reg (buf, X86_ESI);
257 x86_push_reg (buf, X86_EBP);
259 /* save method info */
260 x86_push_membase (buf, X86_ESP, 32);
261 /* get the address of lmf for the current thread */
262 x86_call_code (buf, arch_get_lmf_addr);
264 x86_push_reg (buf, X86_EAX);
265 /* push *lfm (previous_lmf) */
266 x86_push_membase (buf, X86_EAX, 0);
268 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
271 /* push the method info */
272 x86_push_membase (buf, X86_ESP, 44);
273 /* push the return address onto the stack */
274 x86_push_membase (buf, X86_ESP, 52);
276 /* save all register values */
277 x86_push_reg (buf, X86_EBX);
278 x86_push_reg (buf, X86_EDI);
279 x86_push_reg (buf, X86_ESI);
280 x86_push_membase (buf, X86_ESP, 64); /* EDX */
281 x86_push_membase (buf, X86_ESP, 64); /* ECX */
282 x86_push_membase (buf, X86_ESP, 64); /* EAX */
284 x86_call_code (buf, x86_magic_trampoline);
285 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
287 /* restore LMF start */
288 /* ebx = previous_lmf */
289 x86_pop_reg (buf, X86_EBX);
291 x86_pop_reg (buf, X86_EDI);
292 /* *(lmf) = previous_lmf */
293 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
294 /* discard method info */
295 x86_pop_reg (buf, X86_ESI);
296 /* restore caller saved regs */
297 x86_pop_reg (buf, X86_EBP);
298 x86_pop_reg (buf, X86_ESI);
299 x86_pop_reg (buf, X86_EDI);
300 x86_pop_reg (buf, X86_EBX);
301 /* discard save IP */
302 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
303 /* restore LMF end */
305 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
307 /* call the compiled method */
308 x86_jump_reg (buf, X86_EAX);
310 g_assert ((buf - vc) <= 256);
313 code = buf = g_malloc (16);
314 x86_push_imm (buf, method);
315 x86_jump_code (buf, vc);
316 g_assert ((buf - code) <= 16);
318 /* store trampoline address */
321 mono_jit_stats.method_trampolines++;
326 /* arch_create_remoting_trampoline:
327 * @method: pointer to the method info
329 * Creates a trampoline which calls the remoting functions. This
330 * is used in the vtable of transparent proxies.
332 * Returns: a pointer to the newly created code
335 arch_create_remoting_trampoline (MonoMethod *method)
339 if (method->remoting_tramp)
340 return method->remoting_tramp;
342 code = buf = g_malloc (16);
343 x86_push_imm (buf, method);
344 x86_call_code (buf, arch_remoting_invoke);
345 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
348 g_assert ((buf - code) <= 16);
350 method->remoting_tramp = code;