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>
20 * mono_remoting_invoke:
21 * @real_proxy: pointer to a RealProxy object
22 * @msg: The MonoMethodMessage to execute
23 * @exc: used to store exceptions
24 * @out_args: used to store output arguments
26 * This is used to call RealProxy::Invoke(). RealProxy::Invoke() returns an
27 * IMessage interface and it is not trivial to extract results from there. So
28 * we call an helper method PrivateInvoke instead of calling
29 * RealProxy::Invoke() directly.
31 * Returns: the result object.
34 mono_remoting_invoke (MonoObject *real_proxy, MonoMethodMessage *msg,
35 MonoObject **exc, MonoArray **out_args)
37 static MonoObject *(*invoke) (gpointer, gpointer, MonoObject **, MonoArray **) = NULL;
39 /* fixme: make this domain dependent */
44 klass = mono_defaults.real_proxy_class;
46 for (i = 0; i < klass->method.count; ++i) {
47 if (!strcmp ("PrivateInvoke", klass->methods [i]->name) &&
48 klass->methods [i]->signature->param_count == 4) {
49 invoke = arch_compile_method (klass->methods [i]);
57 return invoke (real_proxy, msg, exc, out_args);
61 arch_remoting_invoke (MonoMethod *method, gpointer ip, gpointer first_arg)
63 MonoMethodSignature *sig = method->signature;
64 MonoMethodMessage *msg;
65 MonoTransparentProxy *this;
66 MonoObject *res, *exc;
70 //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
73 if (ISSTRUCT (sig->ret))
76 this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
78 g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
80 msg = arch_method_call_message_new (method, &first_arg);
82 res = mono_remoting_invoke ((MonoObject *)this->rp, msg, &exc, &out_args);
85 mono_raise_exception ((MonoException *)exc);
87 arch_method_return_message_restore (method, &first_arg, res, out_args);
89 /* WARNING: do not write any code here, because that would destroy
95 * get_unbox_trampoline:
97 * @addr: pointer to native code for @m
99 * when value type methods are called through the vtable we need to unbox the
100 * this argument. This method returns a pointer to a trampoline which does
101 * unboxing before calling the method
104 get_unbox_trampoline (MonoMethod *m, gpointer addr)
106 guint8 *code, *start;
109 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
112 start = code = g_malloc (16);
114 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
115 x86_jump_code (code, addr);
116 g_assert ((code - start) < 16);
122 * x86_magic_trampoline:
123 * @eax: saved x86 register
124 * @ecx: saved x86 register
125 * @edx: saved x86 register
126 * @esi: saved x86 register
127 * @edi: saved x86 register
128 * @ebx: saved x86 register
129 * @code: pointer into caller code
130 * @method: the method to translate
132 * This method is called by the trampoline functions for virtual
133 * methods. It inspects the caller code to find the address of the
134 * vtable slot, then calls the JIT compiler and writes the address
135 * of the compiled method back to the vtable. All virtual methods
136 * are called with: x86_call_membase (inst, basereg, disp). We always
137 * use 32 bit displacement to ensure that the length of the call
138 * instruction is 6 bytes. We need to get the value of the basereg
139 * and the constant displacement.
142 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
143 int ebx, guint8 *code, MonoMethod *m)
150 EnterCriticalSection (metadata_section);
151 addr = arch_compile_method (m);
152 LeaveCriticalSection (metadata_section);
155 /* go to the start of the call instruction
157 * address_byte = (m << 6) | (o << 3) | reg
158 * call opcode: 0xff address_byte displacement
163 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
164 reg = code [4] & 0x07;
165 disp = (signed char)code [5];
167 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
168 reg = code [1] & 0x07;
169 disp = *((gint32*)(code + 2));
170 } else if ((code [1] == 0xe8)) {
171 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
174 printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
176 g_assert_not_reached ();
200 g_assert_not_reached ();
205 if (m->klass->valuetype) {
206 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
208 return *((gpointer *)o) = addr;
213 * arch_create_jit_trampoline:
214 * @method: pointer to the method info
216 * Creates a trampoline function for virtual methods. If the created
217 * code is called it first starts JIT compilation of method,
218 * and then calls the newly created method. I also replaces the
219 * corresponding vtable entry (see x86_magic_trampoline).
221 * Returns: a pointer to the newly created code
224 arch_create_jit_trampoline (MonoMethod *method)
226 MonoDomain *domain = mono_domain_get ();
228 static guint8 *vc = NULL;
229 GHashTable *jit_code_hash;
231 /* previously created trampoline code */
235 /* we immediately compile runtime provided functions */
236 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
237 method->info = arch_compile_method (method);
241 /* icalls use method->addr */
242 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
243 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
244 method->info = arch_create_native_wrapper (method);
248 /* check if we already have JITed code */
249 if (mono_jit_share_code)
250 jit_code_hash = mono_root_domain->jit_code_hash;
252 jit_code_hash = domain->jit_code_hash;
254 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
255 mono_jit_stats.methods_lookups++;
260 vc = buf = g_malloc (256);
262 /* save caller save regs because we need to do a call */
263 x86_push_reg (buf, X86_EDX);
264 x86_push_reg (buf, X86_EAX);
265 x86_push_reg (buf, X86_ECX);
269 /* save the IP (caller ip) */
270 x86_push_membase (buf, X86_ESP, 16);
272 x86_push_reg (buf, X86_EBX);
273 x86_push_reg (buf, X86_EDI);
274 x86_push_reg (buf, X86_ESI);
275 x86_push_reg (buf, X86_EBP);
277 /* save method info */
278 x86_push_membase (buf, X86_ESP, 32);
279 /* get the address of lmf for the current thread */
280 x86_call_code (buf, arch_get_lmf_addr);
282 x86_push_reg (buf, X86_EAX);
283 /* push *lfm (previous_lmf) */
284 x86_push_membase (buf, X86_EAX, 0);
286 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
289 /* push the method info */
290 x86_push_membase (buf, X86_ESP, 44);
291 /* push the return address onto the stack */
292 x86_push_membase (buf, X86_ESP, 52);
294 /* save all register values */
295 x86_push_reg (buf, X86_EBX);
296 x86_push_reg (buf, X86_EDI);
297 x86_push_reg (buf, X86_ESI);
298 x86_push_membase (buf, X86_ESP, 64); /* EDX */
299 x86_push_membase (buf, X86_ESP, 64); /* ECX */
300 x86_push_membase (buf, X86_ESP, 64); /* EAX */
302 x86_call_code (buf, x86_magic_trampoline);
303 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
305 /* restore LMF start */
306 /* ebx = previous_lmf */
307 x86_pop_reg (buf, X86_EBX);
309 x86_pop_reg (buf, X86_EDI);
310 /* *(lmf) = previous_lmf */
311 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
312 /* discard method info */
313 x86_pop_reg (buf, X86_ESI);
314 /* restore caller saved regs */
315 x86_pop_reg (buf, X86_EBP);
316 x86_pop_reg (buf, X86_ESI);
317 x86_pop_reg (buf, X86_EDI);
318 x86_pop_reg (buf, X86_EBX);
319 /* discard save IP */
320 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
321 /* restore LMF end */
323 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
325 /* call the compiled method */
326 x86_jump_reg (buf, X86_EAX);
328 g_assert ((buf - vc) <= 256);
331 code = buf = g_malloc (16);
332 x86_push_imm (buf, method);
333 x86_jump_code (buf, vc);
334 g_assert ((buf - code) <= 16);
336 /* store trampoline address */
339 mono_jit_stats.method_trampolines++;
344 /* arch_create_remoting_trampoline:
345 * @method: pointer to the method info
347 * Creates a trampoline which calls the remoting functions. This
348 * is used in the vtable of transparent proxies.
350 * Returns: a pointer to the newly created code
353 arch_create_remoting_trampoline (MonoMethod *method)
358 if (method->remoting_tramp)
359 return method->remoting_tramp;
361 code = buf = g_malloc (16);
362 x86_push_imm (buf, method);
363 x86_call_code (buf, arch_remoting_invoke);
364 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
367 g_assert ((buf - code) <= 16);
369 method->remoting_tramp = code;
371 /* we store a jit info, so that mono_delegate_ctor
372 * is able to find a method info */
373 ji = mono_mempool_alloc0 (mono_root_domain->mp, sizeof (MonoJitInfo));
375 ji->code_start = code;
376 ji->code_size = buf - code;
379 mono_jit_info_table_add (mono_root_domain, ji);