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