2002-03-30 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 #include "message.h"
20
21 static void
22 arch_remoting_invoke (MonoMethod *method, gpointer ip, gpointer first_arg)
23 {
24         MonoMethodSignature *sig = method->signature;
25         MonoMethodMessage *msg;
26         MonoTransparentProxy *this;
27         MonoObject *res, *exc;
28         MonoArray *out_args;
29         int this_pos = 0;
30         static MonoObject *(*invoke) (gpointer proxy, gpointer msg, 
31                                       MonoObject **exc, MonoArray **out_args) = NULL;
32
33         //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
34         //method->name);
35
36         if (ISSTRUCT (sig->ret))
37                 this_pos += 4;
38
39         this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
40
41         g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
42
43         msg = mono_method_call_message_new (method, &first_arg);
44
45         /* fixme: make this domain dependent */
46         if (!invoke) {
47                 MonoClass *klass;
48                 int i;
49
50                 klass = mono_defaults.real_proxy_class; 
51                        
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]);
56                                 break;
57                         }
58                 }
59         
60                 g_assert (invoke);
61         }
62
63
64         res = invoke (this->rp, msg, &exc, &out_args);
65
66         if (exc)
67                 mono_raise_exception ((MonoException *)exc);
68
69         mono_method_return_message_restore (method, &first_arg, res, out_args);
70
71         /* WARNING: do not write any code here, because that would destroy 
72          * the return value 
73          */
74 }
75
76 /*
77  * get_unbox_trampoline:
78  * @m: method pointer
79  * @addr: pointer to native code for @m
80  *
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
84  */
85 static gpointer
86 get_unbox_trampoline (MonoMethod *m, gpointer addr)
87 {
88         guint8 *code, *start;
89         int this_pos = 4;
90
91         if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
92                 this_pos = 8;
93             
94         start = code = g_malloc (16);
95
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);
99
100         return start;
101 }
102
103 /**
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
113  *
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.
122  */
123 static gpointer
124 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
125                       int ebx, const guint8 *code, MonoMethod *m)
126 {
127         guint8 reg;
128         gint32 disp;
129         gpointer o;
130         gpointer addr;
131
132         EnterCriticalSection (metadata_section);
133         addr = arch_compile_method (m);
134         LeaveCriticalSection (metadata_section);
135         g_assert (addr);
136
137         /* go to the start of the call instruction
138          *
139          * address_byte = (m << 6) | (o << 3) | reg
140          * call opcode: 0xff address_byte displacement
141          * 0xff m=1,o=2 imm8
142          * 0xff m=2,o=2 imm32
143          */
144         code -= 6;
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];
148         } else {
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; 
154                         return addr;
155                 } else {
156                         printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
157                                 code [4], code [5]);
158                         g_assert_not_reached ();
159                 }
160         }
161
162         switch (reg) {
163         case X86_EAX:
164                 o = (gpointer)eax;
165                 break;
166         case X86_EDX:
167                 o = (gpointer)edx;
168                 break;
169         case X86_ECX:
170                 o = (gpointer)ecx;
171                 break;
172         case X86_ESI:
173                 o = (gpointer)esi;
174                 break;
175         case X86_EDI:
176                 o = (gpointer)edi;
177                 break;
178         case X86_EBX:
179                 o = (gpointer)ebx;
180                 break;
181         default:
182                 g_assert_not_reached ();
183         }
184
185         o += disp;
186
187         if (m->klass->valuetype) {
188                 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
189         } else {
190                 return *((gpointer *)o) = addr;
191         }
192 }
193
194 /**
195  * arch_create_jit_trampoline:
196  * @method: pointer to the method info
197  *
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).
202  * 
203  * Returns: a pointer to the newly created code 
204  */
205 gpointer
206 arch_create_jit_trampoline (MonoMethod *method)
207 {
208         MonoDomain *domain = mono_domain_get ();
209         guint8 *code, *buf;
210         static guint8 *vc = NULL;
211         GHashTable *jit_code_hash;
212
213         /* previously created trampoline code */
214         if (method->info)
215                 return method->info;
216
217         /* we immediately compile runtime provided functions */
218         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
219                 method->info = arch_compile_method (method);
220                 return method->info;
221         }
222
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);
227                 return method->info;
228         }
229
230         /* check if we already have JITed code */
231         if (mono_jit_share_code)
232                 jit_code_hash = mono_root_domain->jit_code_hash;
233         else
234                 jit_code_hash = domain->jit_code_hash;
235
236         if ((code = g_hash_table_lookup (jit_code_hash, method))) {
237                 mono_jit_stats.methods_lookups++;
238                 return code;
239         }
240
241         if (!vc) {
242                 vc = buf = g_malloc (256);
243
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);
248
249                 /* save LMF begin */
250
251                 /* save the IP (caller ip) */
252                 x86_push_membase (buf, X86_ESP, 16);
253
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);
258
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);
263                 /* push lmf */
264                 x86_push_reg (buf, X86_EAX); 
265                 /* push *lfm (previous_lmf) */
266                 x86_push_membase (buf, X86_EAX, 0);
267                 /* *(lmf) = ESP */
268                 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
269                 /* save LFM end */
270
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);
275
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 */
283
284                 x86_call_code (buf, x86_magic_trampoline);
285                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
286
287                 /* restore LMF start */
288                 /* ebx = previous_lmf */
289                 x86_pop_reg (buf, X86_EBX);
290                 /* edi = lmf */
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 */
304
305                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
306
307                 /* call the compiled method */
308                 x86_jump_reg (buf, X86_EAX);
309
310                 g_assert ((buf - vc) <= 256);
311         }
312
313         code = buf = g_malloc (16);
314         x86_push_imm (buf, method);
315         x86_jump_code (buf, vc);
316         g_assert ((buf - code) <= 16);
317
318         /* store trampoline address */
319         method->info = code;
320
321         mono_jit_stats.method_trampolines++;
322
323         return code;
324 }
325
326 /* arch_create_remoting_trampoline:
327  * @method: pointer to the method info
328  *
329  * Creates a trampoline which calls the remoting functions. This
330  * is used in the vtable of transparent proxies.
331  * 
332  * Returns: a pointer to the newly created code 
333  */
334 gpointer
335 arch_create_remoting_trampoline (MonoMethod *method)
336 {
337         guint8 *code, *buf;
338
339         if (method->remoting_tramp)
340                 return method->remoting_tramp;
341         
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);
346         x86_ret (buf);
347         
348         g_assert ((buf - code) <= 16);
349
350         method->remoting_tramp = code;
351
352         return code;
353 }