architecture independent marshaling code
[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 static void
21 arch_remoting_invoke (MonoMethod *method, gpointer ip, gpointer first_arg)
22 {
23         MonoMethodSignature *sig = method->signature;
24         MonoMethodMessage *msg;
25         MonoTransparentProxy *this;
26         MonoObject *res, *exc;
27         MonoArray *out_args;
28         int this_pos = 0;
29
30         //printf ("REMOTING %s.%s:%s\n", method->klass->name_space, method->klass->name,
31         //method->name);
32
33         if (ISSTRUCT (sig->ret))
34                 this_pos += 4;
35
36         this = *(MonoTransparentProxy **)(((char *)&first_arg) + this_pos);
37
38         g_assert (((MonoObject *)this)->vtable->klass == mono_defaults.transparent_proxy_class);
39
40         msg = arch_method_call_message_new (method, &first_arg, NULL, NULL, NULL);
41
42         res = mono_remoting_invoke ((MonoObject *)this->rp, msg, &exc, &out_args);
43
44         if (exc)
45                 mono_raise_exception ((MonoException *)exc);
46
47         arch_method_return_message_restore (method, &first_arg, res, out_args);
48
49         /* WARNING: do not write any code here, because that would destroy 
50          * the return value 
51          */
52 }
53
54 /*
55  * get_unbox_trampoline:
56  * @m: method pointer
57  * @addr: pointer to native code for @m
58  *
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
62  */
63 static gpointer
64 get_unbox_trampoline (MonoMethod *m, gpointer addr)
65 {
66         guint8 *code, *start;
67         int this_pos = 4;
68
69         if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
70                 this_pos = 8;
71             
72         start = code = g_malloc (16);
73
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);
77
78         return start;
79 }
80
81 /**
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
91  *
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.
100  */
101 static gpointer
102 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
103                       int ebx, guint8 *code, MonoMethod *m)
104 {
105         guint8 reg;
106         gint32 disp;
107         char *o;
108         gpointer addr;
109
110         EnterCriticalSection (metadata_section);
111         addr = mono_compile_method (m);
112         LeaveCriticalSection (metadata_section);
113         g_assert (addr);
114
115         /* go to the start of the call instruction
116          *
117          * address_byte = (m << 6) | (o << 3) | reg
118          * call opcode: 0xff address_byte displacement
119          * 0xff m=1,o=2 imm8
120          * 0xff m=2,o=2 imm32
121          */
122         code -= 6;
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];
126         } else {
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; 
132                         return addr;
133                 } else {
134                         printf ("%x %x %x %x %x %x \n", code [0], code [1], code [2], code [3],
135                                 code [4], code [5]);
136                         g_assert_not_reached ();
137                 }
138         }
139
140         switch (reg) {
141         case X86_EAX:
142                 o = (gpointer)eax;
143                 break;
144         case X86_EDX:
145                 o = (gpointer)edx;
146                 break;
147         case X86_ECX:
148                 o = (gpointer)ecx;
149                 break;
150         case X86_ESI:
151                 o = (gpointer)esi;
152                 break;
153         case X86_EDI:
154                 o = (gpointer)edi;
155                 break;
156         case X86_EBX:
157                 o = (gpointer)ebx;
158                 break;
159         default:
160                 g_assert_not_reached ();
161         }
162
163         o += disp;
164
165         if (m->klass->valuetype) {
166                 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
167         } else {
168                 return *((gpointer *)o) = addr;
169         }
170 }
171
172 /**
173  * arch_create_jit_trampoline:
174  * @method: pointer to the method info
175  *
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).
180  * 
181  * Returns: a pointer to the newly created code 
182  */
183 gpointer
184 arch_create_jit_trampoline (MonoMethod *method)
185 {
186         MonoDomain *domain = mono_domain_get ();
187         guint8 *code, *buf;
188         static guint8 *vc = NULL;
189         GHashTable *jit_code_hash;
190
191         /* previously created trampoline code */
192         if (method->info)
193                 return method->info;
194
195         /* we immediately compile runtime provided functions */
196         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
197                 method->info = mono_compile_method (method);
198                 return method->info;
199         }
200
201         /* icalls use method->addr */
202         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
203             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
204                 MonoMethod *nm;
205                 
206                 nm = mono_marshal_get_native_wrapper (method);
207                 method->info = mono_compile_method (nm);
208                 return method->info;
209         }
210
211         /* check if we already have JITed code */
212         if (mono_jit_share_code)
213                 jit_code_hash = mono_root_domain->jit_code_hash;
214         else
215                 jit_code_hash = domain->jit_code_hash;
216
217         if ((code = g_hash_table_lookup (jit_code_hash, method))) {
218                 mono_jit_stats.methods_lookups++;
219                 return code;
220         }
221
222         if (!vc) {
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);
228
229                 /* save LMF begin */
230
231                 /* save the IP (caller ip) */
232                 x86_push_membase (buf, X86_ESP, 16);
233
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);
238
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);
243                 /* push lmf */
244                 x86_push_reg (buf, X86_EAX); 
245                 /* push *lfm (previous_lmf) */
246                 x86_push_membase (buf, X86_EAX, 0);
247                 /* *(lmf) = ESP */
248                 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
249                 /* save LFM end */
250
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);
255
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 */
263
264                 x86_call_code (buf, x86_magic_trampoline);
265                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
266
267                 /* restore LMF start */
268                 /* ebx = previous_lmf */
269                 x86_pop_reg (buf, X86_EBX);
270                 /* edi = lmf */
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 */
284
285                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
286
287                 /* call the compiled method */
288                 x86_jump_reg (buf, X86_EAX);
289
290                 g_assert ((buf - vc) <= 256);
291         }
292
293         code = buf = g_malloc (16);
294         x86_push_imm (buf, method);
295         x86_jump_code (buf, vc);
296         g_assert ((buf - code) <= 16);
297
298         /* store trampoline address */
299         method->info = code;
300
301         mono_jit_stats.method_trampolines++;
302
303         return code;
304 }
305
306 /* arch_create_remoting_trampoline:
307  * @method: pointer to the method info
308  *
309  * Creates a trampoline which calls the remoting functions. This
310  * is used in the vtable of transparent proxies.
311  * 
312  * Returns: a pointer to the newly created code 
313  */
314 gpointer
315 arch_create_remoting_trampoline (MonoMethod *method)
316 {
317         MonoJitInfo *ji;
318         guint8 *code, *buf;
319
320         if (method->remoting_tramp)
321                 return method->remoting_tramp;
322         
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);
327         x86_ret (buf);
328         
329         g_assert ((buf - code) <= 16);
330
331         method->remoting_tramp = code;
332
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));
336         ji->method = method;
337         ji->code_start = code;
338         ji->code_size = buf - code;
339         ji->used_regs = 0;
340         ji->num_clauses = 0;
341         mono_jit_info_table_add (mono_root_domain, ji);
342
343         return code;
344 }