2002-04-05 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mono / jit / trampoline.c
1 /*
2  * trampoline.c: JIT trampoline code
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <glib.h>
12
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/tabledefs.h>
15 #include <mono/arch/x86/x86-codegen.h>
16
17 #include "jit.h"
18 #include "codegen.h"
19 /**
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
25  *
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.
30  *
31  * Returns: the result object.
32  */
33 MonoObject *
34 mono_remoting_invoke (MonoObject *real_proxy, MonoMethodMessage *msg, 
35                       MonoObject **exc, MonoArray **out_args)
36 {
37         static MonoObject *(*invoke) (gpointer, gpointer, MonoObject **, MonoArray **) = NULL;
38
39         /* fixme: make this domain dependent */
40         if (!invoke) {
41                 MonoClass *klass;
42                 int i;
43
44                 klass = mono_defaults.real_proxy_class; 
45                        
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]);
50                                 break;
51                         }
52                 }
53         
54                 g_assert (invoke);
55         }
56
57         return invoke (real_proxy, msg, exc, out_args);
58 }
59
60 static void
61 arch_remoting_invoke (MonoMethod *method, gpointer ip, gpointer first_arg)
62 {
63         MonoMethodSignature *sig = method->signature;
64         MonoMethodMessage *msg;
65         MonoTransparentProxy *this;
66         MonoObject *res, *exc;
67         MonoArray *out_args;
68         int this_pos = 0;
69
70         //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
71         //method->name);
72
73         if (ISSTRUCT (sig->ret))
74                 this_pos += 4;
75
76         this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
77
78         g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
79
80         msg = arch_method_call_message_new (method, &first_arg);
81
82         res = mono_remoting_invoke ((MonoObject *)this->rp, msg, &exc, &out_args);
83
84         if (exc)
85                 mono_raise_exception ((MonoException *)exc);
86
87         arch_method_return_message_restore (method, &first_arg, res, out_args);
88
89         /* WARNING: do not write any code here, because that would destroy 
90          * the return value 
91          */
92 }
93
94 /*
95  * get_unbox_trampoline:
96  * @m: method pointer
97  * @addr: pointer to native code for @m
98  *
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
102  */
103 static gpointer
104 get_unbox_trampoline (MonoMethod *m, gpointer addr)
105 {
106         guint8 *code, *start;
107         int this_pos = 4;
108
109         if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
110                 this_pos = 8;
111             
112         start = code = g_malloc (16);
113
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);
117
118         return start;
119 }
120
121 /**
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
131  *
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.
140  */
141 static gpointer
142 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
143                       int ebx, guint8 *code, MonoMethod *m)
144 {
145         guint8 reg;
146         gint32 disp;
147         char *o;
148         gpointer addr;
149
150         EnterCriticalSection (metadata_section);
151         addr = arch_compile_method (m);
152         LeaveCriticalSection (metadata_section);
153         g_assert (addr);
154
155         /* go to the start of the call instruction
156          *
157          * address_byte = (m << 6) | (o << 3) | reg
158          * call opcode: 0xff address_byte displacement
159          * 0xff m=1,o=2 imm8
160          * 0xff m=2,o=2 imm32
161          */
162         code -= 6;
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];
166         } else {
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; 
172                         return addr;
173                 } else {
174                         printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
175                                 code [4], code [5]);
176                         g_assert_not_reached ();
177                 }
178         }
179
180         switch (reg) {
181         case X86_EAX:
182                 o = (gpointer)eax;
183                 break;
184         case X86_EDX:
185                 o = (gpointer)edx;
186                 break;
187         case X86_ECX:
188                 o = (gpointer)ecx;
189                 break;
190         case X86_ESI:
191                 o = (gpointer)esi;
192                 break;
193         case X86_EDI:
194                 o = (gpointer)edi;
195                 break;
196         case X86_EBX:
197                 o = (gpointer)ebx;
198                 break;
199         default:
200                 g_assert_not_reached ();
201         }
202
203         o += disp;
204
205         if (m->klass->valuetype) {
206                 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
207         } else {
208                 return *((gpointer *)o) = addr;
209         }
210 }
211
212 /**
213  * arch_create_jit_trampoline:
214  * @method: pointer to the method info
215  *
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).
220  * 
221  * Returns: a pointer to the newly created code 
222  */
223 gpointer
224 arch_create_jit_trampoline (MonoMethod *method)
225 {
226         MonoDomain *domain = mono_domain_get ();
227         guint8 *code, *buf;
228         static guint8 *vc = NULL;
229         GHashTable *jit_code_hash;
230
231         /* previously created trampoline code */
232         if (method->info)
233                 return method->info;
234
235         /* we immediately compile runtime provided functions */
236         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
237                 method->info = arch_compile_method (method);
238                 return method->info;
239         }
240
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);
245                 return method->info;
246         }
247
248         /* check if we already have JITed code */
249         if (mono_jit_share_code)
250                 jit_code_hash = mono_root_domain->jit_code_hash;
251         else
252                 jit_code_hash = domain->jit_code_hash;
253
254         if ((code = g_hash_table_lookup (jit_code_hash, method))) {
255                 mono_jit_stats.methods_lookups++;
256                 return code;
257         }
258
259         if (!vc) {
260                 vc = buf = g_malloc (256);
261
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);
266
267                 /* save LMF begin */
268
269                 /* save the IP (caller ip) */
270                 x86_push_membase (buf, X86_ESP, 16);
271
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);
276
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);
281                 /* push lmf */
282                 x86_push_reg (buf, X86_EAX); 
283                 /* push *lfm (previous_lmf) */
284                 x86_push_membase (buf, X86_EAX, 0);
285                 /* *(lmf) = ESP */
286                 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
287                 /* save LFM end */
288
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);
293
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 */
301
302                 x86_call_code (buf, x86_magic_trampoline);
303                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
304
305                 /* restore LMF start */
306                 /* ebx = previous_lmf */
307                 x86_pop_reg (buf, X86_EBX);
308                 /* edi = lmf */
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 */
322
323                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
324
325                 /* call the compiled method */
326                 x86_jump_reg (buf, X86_EAX);
327
328                 g_assert ((buf - vc) <= 256);
329         }
330
331         code = buf = g_malloc (16);
332         x86_push_imm (buf, method);
333         x86_jump_code (buf, vc);
334         g_assert ((buf - code) <= 16);
335
336         /* store trampoline address */
337         method->info = code;
338
339         mono_jit_stats.method_trampolines++;
340
341         return code;
342 }
343
344 /* arch_create_remoting_trampoline:
345  * @method: pointer to the method info
346  *
347  * Creates a trampoline which calls the remoting functions. This
348  * is used in the vtable of transparent proxies.
349  * 
350  * Returns: a pointer to the newly created code 
351  */
352 gpointer
353 arch_create_remoting_trampoline (MonoMethod *method)
354 {
355         MonoJitInfo *ji;
356         guint8 *code, *buf;
357
358         if (method->remoting_tramp)
359                 return method->remoting_tramp;
360         
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);
365         x86_ret (buf);
366         
367         g_assert ((buf - code) <= 16);
368
369         method->remoting_tramp = code;
370
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));
374         ji->method = method;
375         ji->code_start = code;
376         ji->code_size = buf - code;
377         ji->used_regs = 0;
378         ji->num_clauses = 0;
379         mono_jit_info_table_add (mono_root_domain, ji);
380
381         return code;
382 }