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>
21 arch_remoting_invoke (MonoMethod *method, gpointer ip, gpointer first_arg)
23 MonoMethodSignature *sig = method->signature;
24 MonoMethodMessage *msg;
25 MonoTransparentProxy *this;
26 MonoObject *res, *exc;
30 //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
33 if (ISSTRUCT (sig->ret))
36 this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
38 g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
40 msg = arch_method_call_message_new (method, &first_arg, NULL, NULL, NULL);
42 res = mono_remoting_invoke ((MonoObject *)this->rp, msg, &exc, &out_args);
45 mono_raise_exception ((MonoException *)exc);
47 arch_method_return_message_restore (method, &first_arg, res, out_args);
49 /* WARNING: do not write any code here, because that would destroy
55 * get_unbox_trampoline:
57 * @addr: pointer to native code for @m
59 * when value type methods are called through the vtable we need to unbox the
60 * this argument. This method returns a pointer to a trampoline which does
61 * unboxing before calling the method
64 get_unbox_trampoline (MonoMethod *m, gpointer addr)
69 if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
72 start = code = g_malloc (16);
74 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
75 x86_jump_code (code, addr);
76 g_assert ((code - start) < 16);
82 * x86_magic_trampoline:
83 * @eax: saved x86 register
84 * @ecx: saved x86 register
85 * @edx: saved x86 register
86 * @esi: saved x86 register
87 * @edi: saved x86 register
88 * @ebx: saved x86 register
89 * @code: pointer into caller code
90 * @method: the method to translate
92 * This method is called by the trampoline functions for virtual
93 * methods. It inspects the caller code to find the address of the
94 * vtable slot, then calls the JIT compiler and writes the address
95 * of the compiled method back to the vtable. All virtual methods
96 * are called with: x86_call_membase (inst, basereg, disp). We always
97 * use 32 bit displacement to ensure that the length of the call
98 * instruction is 6 bytes. We need to get the value of the basereg
99 * and the constant displacement.
102 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi,
103 int ebx, guint8 *code, MonoMethod *m)
110 EnterCriticalSection (metadata_section);
111 addr = mono_compile_method (m);
112 LeaveCriticalSection (metadata_section);
115 /* go to the start of the call instruction
117 * address_byte = (m << 6) | (o << 3) | reg
118 * call opcode: 0xff address_byte displacement
123 if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
124 reg = code [4] & 0x07;
125 disp = (signed char)code [5];
127 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
128 reg = code [1] & 0x07;
129 disp = *((gint32*)(code + 2));
130 } else if ((code [1] == 0xe8)) {
131 *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5;
134 printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
136 g_assert_not_reached ();
160 g_assert_not_reached ();
165 if (m->klass->valuetype) {
166 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
168 return *((gpointer *)o) = addr;
173 * arch_create_jit_trampoline:
174 * @method: pointer to the method info
176 * Creates a trampoline function for virtual methods. If the created
177 * code is called it first starts JIT compilation of method,
178 * and then calls the newly created method. I also replaces the
179 * corresponding vtable entry (see x86_magic_trampoline).
181 * Returns: a pointer to the newly created code
184 arch_create_jit_trampoline (MonoMethod *method)
186 MonoDomain *domain = mono_domain_get ();
188 static guint8 *vc = NULL;
189 GHashTable *jit_code_hash;
191 /* previously created trampoline code */
195 /* we immediately compile runtime provided functions */
196 if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
197 method->info = mono_compile_method (method);
201 /* icalls use method->addr */
202 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
203 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
206 nm = mono_marshal_get_native_wrapper (method);
207 method->info = mono_compile_method (nm);
211 /* check if we already have JITed code */
212 if (mono_jit_share_code)
213 jit_code_hash = mono_root_domain->jit_code_hash;
215 jit_code_hash = domain->jit_code_hash;
217 if ((code = g_hash_table_lookup (jit_code_hash, method))) {
218 mono_jit_stats.methods_lookups++;
223 vc = buf = g_malloc (256);
224 /* save caller save regs because we need to do a call */
225 x86_push_reg (buf, X86_EDX);
226 x86_push_reg (buf, X86_EAX);
227 x86_push_reg (buf, X86_ECX);
231 /* save the IP (caller ip) */
232 x86_push_membase (buf, X86_ESP, 16);
234 x86_push_reg (buf, X86_EBX);
235 x86_push_reg (buf, X86_EDI);
236 x86_push_reg (buf, X86_ESI);
237 x86_push_reg (buf, X86_EBP);
239 /* save method info */
240 x86_push_membase (buf, X86_ESP, 32);
241 /* get the address of lmf for the current thread */
242 x86_call_code (buf, mono_get_lmf_addr);
244 x86_push_reg (buf, X86_EAX);
245 /* push *lfm (previous_lmf) */
246 x86_push_membase (buf, X86_EAX, 0);
248 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
251 /* push the method info */
252 x86_push_membase (buf, X86_ESP, 44);
253 /* push the return address onto the stack */
254 x86_push_membase (buf, X86_ESP, 52);
256 /* save all register values */
257 x86_push_reg (buf, X86_EBX);
258 x86_push_reg (buf, X86_EDI);
259 x86_push_reg (buf, X86_ESI);
260 x86_push_membase (buf, X86_ESP, 64); /* EDX */
261 x86_push_membase (buf, X86_ESP, 64); /* ECX */
262 x86_push_membase (buf, X86_ESP, 64); /* EAX */
264 x86_call_code (buf, x86_magic_trampoline);
265 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
267 /* restore LMF start */
268 /* ebx = previous_lmf */
269 x86_pop_reg (buf, X86_EBX);
271 x86_pop_reg (buf, X86_EDI);
272 /* *(lmf) = previous_lmf */
273 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
274 /* discard method info */
275 x86_pop_reg (buf, X86_ESI);
276 /* restore caller saved regs */
277 x86_pop_reg (buf, X86_EBP);
278 x86_pop_reg (buf, X86_ESI);
279 x86_pop_reg (buf, X86_EDI);
280 x86_pop_reg (buf, X86_EBX);
281 /* discard save IP */
282 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
283 /* restore LMF end */
285 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
287 /* call the compiled method */
288 x86_jump_reg (buf, X86_EAX);
290 g_assert ((buf - vc) <= 256);
293 code = buf = g_malloc (16);
294 x86_push_imm (buf, method);
295 x86_jump_code (buf, vc);
296 g_assert ((buf - code) <= 16);
298 /* store trampoline address */
301 mono_jit_stats.method_trampolines++;
306 /* arch_create_remoting_trampoline:
307 * @method: pointer to the method info
309 * Creates a trampoline which calls the remoting functions. This
310 * is used in the vtable of transparent proxies.
312 * Returns: a pointer to the newly created code
315 arch_create_remoting_trampoline (MonoMethod *method)
320 if (method->remoting_tramp)
321 return method->remoting_tramp;
323 code = buf = g_malloc (16);
324 x86_push_imm (buf, method);
325 x86_call_code (buf, arch_remoting_invoke);
326 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);
329 g_assert ((buf - code) <= 16);
331 method->remoting_tramp = code;
333 /* we store a jit info, so that mono_delegate_ctor
334 * is able to find a method info */
335 ji = mono_mempool_alloc0 (mono_root_domain->mp, sizeof (MonoJitInfo));
337 ji->code_start = code;
338 ji->code_size = buf - code;
341 mono_jit_info_table_add (mono_root_domain, ji);