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, gpointer, MonoObject **, MonoArray **) = NULL;
32 //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
35 if (ISSTRUCT (sig->ret))
38 this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
40 g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
42 msg = mono_method_call_message_new (method, &first_arg);
44 /* fixme: make this domain dependent */
49 klass = mono_defaults.real_proxy_class;
51 for (i = 0; i < klass->method.count; ++i) {
52 if (!strcmp ("PrivateInvoke", klass->methods [i]->name) &&
53 klass->methods [i]->signature->param_count == 4) {
54 invoke = arch_compile_method (klass->methods [i]);
63 res = invoke (this->rp, msg, &exc, &out_args);
66 mono_raise_exception ((MonoException *)exc);
68 mono_method_return_message_restore (method, &first_arg, res, out_args);
70 /* WARNING: do not write any code here, because that would destroy
76 * get_unbox_trampoline:
78 * @addr: pointer to native code for @m
80 * when value type methods are called through the vtable we need to unbox the
81 * this argument. This method returns a pointer to a trampoline which does
82 * unboxing before calling the method
85 get_unbox_trampoline (MonoMethod *m, gpointer addr)
90 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
93 start = code = g_malloc (16);
95 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
96 x86_jump_code (code, addr);
97 g_assert ((code - start) < 16);
103 * x86_magic_trampoline:
104 * @eax: saved x86 register
105 * @ecx: saved x86 register
106 * @edx: saved x86 register
107 * @esi: saved x86 register
108 * @edi: saved x86 register
109 * @ebx: saved x86 register
110 * @code: pointer into caller code
111 * @method: the method to translate
113 * This method is called by the trampoline functions for virtual
114 * methods. It inspects the caller code to find the address of the
115 * vtable slot, then calls the JIT compiler and writes the address
116 * of the compiled method back to the vtable. All virtual methods
117 * are called with: x86_call_membase (inst, basereg, disp). We always
118 * use 32 bit displacement to ensure that the length of the call
119 * instruction is 6 bytes. We need to get the value of the basereg
120 * and the constant displacement.
123 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
124 int ebx, guint8 *code, MonoMethod *m)
131 EnterCriticalSection (metadata_section);
132 addr = arch_compile_method (m);
133 LeaveCriticalSection (metadata_section);
136 /* go to the start of the call instruction
138 * address_byte = (m << 6) | (o << 3) | reg
139 * call opcode: 0xff address_byte displacement
144 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
145 reg = code [4] & 0x07;
146 disp = (signed char)code [5];
148 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
149 reg = code [1] & 0x07;
150 disp = *((gint32*)(code + 2));
151 } else if ((code [1] == 0xe8)) {
152 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
155 printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
157 g_assert_not_reached ();
181 g_assert_not_reached ();
186 if (m->klass->valuetype) {
187 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
189 return *((gpointer *)o) = addr;
194 * arch_create_jit_trampoline:
195 * @method: pointer to the method info
197 * Creates a trampoline function for virtual methods. If the created
198 * code is called it first starts JIT compilation of method,
199 * and then calls the newly created method. I also replaces the
200 * corresponding vtable entry (see x86_magic_trampoline).
202 * Returns: a pointer to the newly created code
205 arch_create_jit_trampoline (MonoMethod *method)
207 MonoDomain *domain = mono_domain_get ();
209 static guint8 *vc = NULL;
210 GHashTable *jit_code_hash;
212 /* previously created trampoline code */
216 /* we immediately compile runtime provided functions */
217 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
218 method->info = arch_compile_method (method);
222 /* icalls use method->addr */
223 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
224 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
225 method->info = arch_create_native_wrapper (method);
229 /* check if we already have JITed code */
230 if (mono_jit_share_code)
231 jit_code_hash = mono_root_domain->jit_code_hash;
233 jit_code_hash = domain->jit_code_hash;
235 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
236 mono_jit_stats.methods_lookups++;
241 vc = buf = g_malloc (256);
243 /* save caller save regs because we need to do a call */
244 x86_push_reg (buf, X86_EDX);
245 x86_push_reg (buf, X86_EAX);
246 x86_push_reg (buf, X86_ECX);
250 /* save the IP (caller ip) */
251 x86_push_membase (buf, X86_ESP, 16);
253 x86_push_reg (buf, X86_EBX);
254 x86_push_reg (buf, X86_EDI);
255 x86_push_reg (buf, X86_ESI);
256 x86_push_reg (buf, X86_EBP);
258 /* save method info */
259 x86_push_membase (buf, X86_ESP, 32);
260 /* get the address of lmf for the current thread */
261 x86_call_code (buf, arch_get_lmf_addr);
263 x86_push_reg (buf, X86_EAX);
264 /* push *lfm (previous_lmf) */
265 x86_push_membase (buf, X86_EAX, 0);
267 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
270 /* push the method info */
271 x86_push_membase (buf, X86_ESP, 44);
272 /* push the return address onto the stack */
273 x86_push_membase (buf, X86_ESP, 52);
275 /* save all register values */
276 x86_push_reg (buf, X86_EBX);
277 x86_push_reg (buf, X86_EDI);
278 x86_push_reg (buf, X86_ESI);
279 x86_push_membase (buf, X86_ESP, 64); /* EDX */
280 x86_push_membase (buf, X86_ESP, 64); /* ECX */
281 x86_push_membase (buf, X86_ESP, 64); /* EAX */
283 x86_call_code (buf, x86_magic_trampoline);
284 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
286 /* restore LMF start */
287 /* ebx = previous_lmf */
288 x86_pop_reg (buf, X86_EBX);
290 x86_pop_reg (buf, X86_EDI);
291 /* *(lmf) = previous_lmf */
292 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
293 /* discard method info */
294 x86_pop_reg (buf, X86_ESI);
295 /* restore caller saved regs */
296 x86_pop_reg (buf, X86_EBP);
297 x86_pop_reg (buf, X86_ESI);
298 x86_pop_reg (buf, X86_EDI);
299 x86_pop_reg (buf, X86_EBX);
300 /* discard save IP */
301 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
302 /* restore LMF end */
304 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
306 /* call the compiled method */
307 x86_jump_reg (buf, X86_EAX);
309 g_assert ((buf - vc) <= 256);
312 code = buf = g_malloc (16);
313 x86_push_imm (buf, method);
314 x86_jump_code (buf, vc);
315 g_assert ((buf - code) <= 16);
317 /* store trampoline address */
320 mono_jit_stats.method_trampolines++;
325 /* arch_create_remoting_trampoline:
326 * @method: pointer to the method info
328 * Creates a trampoline which calls the remoting functions. This
329 * is used in the vtable of transparent proxies.
331 * Returns: a pointer to the newly created code
334 arch_create_remoting_trampoline (MonoMethod *method)
338 if (method->remoting_tramp)
339 return method->remoting_tramp;
341 code = buf = g_malloc (16);
342 x86_push_imm (buf, method);
343 x86_call_code (buf, arch_remoting_invoke);
344 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
347 g_assert ((buf - code) <= 16);
349 method->remoting_tramp = code;