2003-01-07 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 "debug.h"
20
21 /*
22  * Address of the x86 trampoline code.  This is used by the debugger to check
23  * whether a method is a trampoline.
24  */
25 guint8 *mono_generic_trampoline_code = NULL;
26
27 /*
28  * Address of a special breakpoint trampoline code for the debugger.
29  */
30 guint8 *mono_breakpoint_trampoline_code = NULL;
31
32 /*
33  * get_unbox_trampoline:
34  * @m: method pointer
35  * @addr: pointer to native code for @m
36  *
37  * when value type methods are called through the vtable we need to unbox the
38  * this argument. This method returns a pointer to a trampoline which does
39  * unboxing before calling the method
40  */
41 static gpointer
42 get_unbox_trampoline (MonoMethod *m, gpointer addr)
43 {
44         guint8 *code, *start;
45         int this_pos = 4;
46
47         if (!m->signature->ret->byref && m->signature->ret->type == MONO_TYPE_VALUETYPE)
48                 this_pos = 8;
49             
50         start = code = g_malloc (16);
51
52         x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
53         x86_jump_code (code, addr);
54         g_assert ((code - start) < 16);
55
56         return start;
57 }
58
59 /*
60  * get_breakpoint_trampoline:
61  * @m: method pointer
62  * @addr: pointer to native code for @m
63  *
64  * creates a special trampoline for the debugger which is used to get
65  * a breakpoint after compiling a method.
66  */
67 static gpointer
68 get_breakpoint_trampoline (MonoMethod *m, guint32 breakpoint_id, gpointer addr)
69 {
70         guint8 *code, *start, *buf;
71
72         if (!mono_breakpoint_trampoline_code) {
73                 mono_breakpoint_trampoline_code = buf = g_malloc (13);
74
75                 x86_call_code (buf, mono_debugger_trampoline_breakpoint_callback);
76                 x86_breakpoint (buf);
77                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8);
78                 x86_ret (buf);
79
80                 g_assert ((buf - mono_breakpoint_trampoline_code) <= 13);
81         }
82
83         start = code = g_malloc (22);
84         x86_push_imm (code, addr);
85         x86_push_imm (code, breakpoint_id);
86         x86_push_imm (code, m);
87         x86_jump_code (code, mono_breakpoint_trampoline_code);
88         g_assert ((code - start) <= 22);
89
90         return start;
91 }
92
93 /**
94  * x86_magic_trampoline:
95  * @eax: saved x86 register 
96  * @ecx: saved x86 register 
97  * @edx: saved x86 register 
98  * @esi: saved x86 register 
99  * @edi: saved x86 register 
100  * @ebx: saved x86 register
101  * @code: pointer into caller code
102  * @method: the method to translate
103  *
104  * This method is called by the trampoline functions for virtual
105  * methods. It inspects the caller code to find the address of the
106  * vtable slot, then calls the JIT compiler and writes the address
107  * of the compiled method back to the vtable. All virtual methods 
108  * are called with: x86_call_membase (inst, basereg, disp). We always
109  * use 32 bit displacement to ensure that the length of the call 
110  * instruction is 6 bytes. We need to get the value of the basereg 
111  * and the constant displacement.
112  */
113 static gpointer
114 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
115                       int ebx, guint8 *code, MonoMethod *m)
116 {
117         guint8 reg;
118         gint32 disp;
119         char *o;
120         guint32 breakpoint_id;
121         gpointer addr, trampoline;
122
123         EnterCriticalSection (metadata_section);
124         addr = mono_compile_method (m);
125         LeaveCriticalSection (metadata_section);
126         g_assert (addr);
127
128         /* go to the start of the call instruction
129          *
130          * address_byte = (m << 6) | (o << 3) | reg
131          * call opcode: 0xff address_byte displacement
132          * 0xff m=1,o=2 imm8
133          * 0xff m=2,o=2 imm32
134          */
135         code -= 6;
136         if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
137                 reg = code [4] & 0x07;
138                 disp = (signed char)code [5];
139         } else {
140                 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
141                         reg = code [1] & 0x07;
142                         disp = *((gint32*)(code + 2));
143                 } else if ((code [1] == 0xe8)) {
144                         breakpoint_id = mono_method_has_breakpoint (m, TRUE);
145                         if (breakpoint_id) {
146                                 mono_remove_breakpoint (breakpoint_id);
147                                 trampoline = get_breakpoint_trampoline (m, breakpoint_id, addr);
148                         } else
149                                 trampoline = addr;
150                         *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5; 
151                         return trampoline;
152                 } else {
153                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
154                                 code [4], code [5], code [6]);
155                         g_assert_not_reached ();
156                 }
157         }
158
159         switch (reg) {
160         case X86_EAX:
161                 o = (gpointer)eax;
162                 break;
163         case X86_EDX:
164                 o = (gpointer)edx;
165                 break;
166         case X86_ECX:
167                 o = (gpointer)ecx;
168                 break;
169         case X86_ESI:
170                 o = (gpointer)esi;
171                 break;
172         case X86_EDI:
173                 o = (gpointer)edi;
174                 break;
175         case X86_EBX:
176                 o = (gpointer)ebx;
177                 break;
178         default:
179                 g_assert_not_reached ();
180         }
181
182         o += disp;
183
184         if (m->klass->valuetype) {
185                 trampoline = *((gpointer *)o) = get_unbox_trampoline (m, addr);
186         } else {
187                 trampoline = *((gpointer *)o) = addr;
188         }
189
190         breakpoint_id = mono_method_has_breakpoint (m, TRUE);
191         if (breakpoint_id) {
192                 mono_remove_breakpoint (breakpoint_id);
193                 return get_breakpoint_trampoline (m, breakpoint_id, trampoline);
194         } else {
195                 return trampoline;
196         }
197 }
198
199 /**
200  * arch_create_jit_trampoline:
201  * @method: pointer to the method info
202  *
203  * Creates a trampoline function for virtual methods. If the created
204  * code is called it first starts JIT compilation of method,
205  * and then calls the newly created method. I also replaces the
206  * corresponding vtable entry (see x86_magic_trampoline).
207  * 
208  * Returns: a pointer to the newly created code 
209  */
210 gpointer
211 arch_create_jit_trampoline (MonoMethod *method)
212 {
213         MonoDomain *domain = mono_domain_get ();
214         guint8 *code, *buf;
215         GHashTable *jit_code_hash;
216
217         /* previously created trampoline code */
218         if (method->info)
219                 return method->info;
220
221         /* we immediately compile runtime provided functions */
222         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
223                 method->info = mono_compile_method (method);
224                 return method->info;
225         }
226
227         /* icalls use method->addr */
228         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
229             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
230                 MonoMethod *nm;
231                 
232                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
233                         mono_lookup_pinvoke_call (method);
234
235 #ifdef MONO_USE_EXC_TABLES
236                 if (mono_method_blittable (method)) {
237                         method->info = method->addr;
238                 } else {
239 #endif
240                         nm = mono_marshal_get_native_wrapper (method);
241                         method->info = mono_compile_method (nm);
242 #ifdef MONO_USE_EXC_TABLES
243                 }
244 #endif
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 (!mono_generic_trampoline_code) {
260                 mono_generic_trampoline_code = buf = g_malloc (256);
261                 /* save caller save regs because we need to do a call */ 
262                 x86_push_reg (buf, X86_EDX);
263                 x86_push_reg (buf, X86_EAX);
264                 x86_push_reg (buf, X86_ECX);
265
266                 /* save LMF begin */
267
268                 /* save the IP (caller ip) */
269                 x86_push_membase (buf, X86_ESP, 16);
270
271                 x86_push_reg (buf, X86_EBX);
272                 x86_push_reg (buf, X86_EDI);
273                 x86_push_reg (buf, X86_ESI);
274                 x86_push_reg (buf, X86_EBP);
275
276                 /* save method info */
277                 x86_push_membase (buf, X86_ESP, 32);
278                 /* get the address of lmf for the current thread */
279                 x86_call_code (buf, mono_get_lmf_addr);
280                 /* push lmf */
281                 x86_push_reg (buf, X86_EAX); 
282                 /* push *lfm (previous_lmf) */
283                 x86_push_membase (buf, X86_EAX, 0);
284                 /* *(lmf) = ESP */
285                 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
286                 /* save LFM end */
287
288                 /* push the method info */
289                 x86_push_membase (buf, X86_ESP, 44);
290                 /* push the return address onto the stack */
291                 x86_push_membase (buf, X86_ESP, 52);
292
293                 /* save all register values */
294                 x86_push_reg (buf, X86_EBX);
295                 x86_push_reg (buf, X86_EDI);
296                 x86_push_reg (buf, X86_ESI);
297                 x86_push_membase (buf, X86_ESP, 64); /* EDX */
298                 x86_push_membase (buf, X86_ESP, 64); /* ECX */
299                 x86_push_membase (buf, X86_ESP, 64); /* EAX */
300
301                 x86_call_code (buf, x86_magic_trampoline);
302                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
303
304                 /* restore LMF start */
305                 /* ebx = previous_lmf */
306                 x86_pop_reg (buf, X86_EBX);
307                 /* edi = lmf */
308                 x86_pop_reg (buf, X86_EDI);
309                 /* *(lmf) = previous_lmf */
310                 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
311                 /* discard method info */
312                 x86_pop_reg (buf, X86_ESI);
313                 /* restore caller saved regs */
314                 x86_pop_reg (buf, X86_EBP);
315                 x86_pop_reg (buf, X86_ESI);
316                 x86_pop_reg (buf, X86_EDI);
317                 x86_pop_reg (buf, X86_EBX);
318                 /* discard save IP */
319                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
320                 /* restore LMF end */
321
322                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
323
324                 /* call the compiled method */
325                 x86_jump_reg (buf, X86_EAX);
326
327                 g_assert ((buf - mono_generic_trampoline_code) <= 256);
328         }
329
330         code = buf = g_malloc (16);
331         x86_push_imm (buf, method);
332         x86_jump_code (buf, mono_generic_trampoline_code);
333         g_assert ((buf - code) <= 16);
334
335         /* store trampoline address */
336         method->info = code;
337
338         mono_jit_stats.method_trampolines++;
339
340         return code;
341 }