2002-09-26 Martin Baulig <martin@gnome.org>
[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 (8);
74
75                 x86_breakpoint (buf);
76                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8);
77                 x86_ret (buf);
78
79                 g_assert ((buf - mono_breakpoint_trampoline_code) <= 8);
80         }
81
82         start = code = g_malloc (22);
83         x86_push_imm (code, addr);
84         x86_push_imm (code, breakpoint_id);
85         x86_push_imm (code, m);
86         x86_jump_code (code, mono_breakpoint_trampoline_code);
87         g_assert ((code - start) <= 22);
88
89         return start;
90 }
91
92 /**
93  * x86_magic_trampoline:
94  * @eax: saved x86 register 
95  * @ecx: saved x86 register 
96  * @edx: saved x86 register 
97  * @esi: saved x86 register 
98  * @edi: saved x86 register 
99  * @ebx: saved x86 register
100  * @code: pointer into caller code
101  * @method: the method to translate
102  *
103  * This method is called by the trampoline functions for virtual
104  * methods. It inspects the caller code to find the address of the
105  * vtable slot, then calls the JIT compiler and writes the address
106  * of the compiled method back to the vtable. All virtual methods 
107  * are called with: x86_call_membase (inst, basereg, disp). We always
108  * use 32 bit displacement to ensure that the length of the call 
109  * instruction is 6 bytes. We need to get the value of the basereg 
110  * and the constant displacement.
111  */
112 static gpointer
113 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
114                       int ebx, guint8 *code, MonoMethod *m)
115 {
116         guint8 reg;
117         gint32 disp;
118         char *o;
119         guint32 breakpoint_id;
120         gpointer addr, trampoline;
121
122         EnterCriticalSection (metadata_section);
123         addr = mono_compile_method (m);
124         LeaveCriticalSection (metadata_section);
125         g_assert (addr);
126
127         /* go to the start of the call instruction
128          *
129          * address_byte = (m << 6) | (o << 3) | reg
130          * call opcode: 0xff address_byte displacement
131          * 0xff m=1,o=2 imm8
132          * 0xff m=2,o=2 imm32
133          */
134         code -= 6;
135         if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
136                 reg = code [4] & 0x07;
137                 disp = (signed char)code [5];
138         } else {
139                 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
140                         reg = code [1] & 0x07;
141                         disp = *((gint32*)(code + 2));
142                 } else if ((code [1] == 0xe8)) {
143                         breakpoint_id = mono_method_has_breakpoint (m, TRUE);
144                         if (breakpoint_id) {
145                                 mono_remove_breakpoint (breakpoint_id);
146                                 trampoline = get_breakpoint_trampoline (m, breakpoint_id, addr);
147                         } else
148                                 trampoline = addr;
149                         *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5; 
150                         return trampoline;
151                 } else {
152                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
153                                 code [4], code [5], code [6]);
154                         g_assert_not_reached ();
155                 }
156         }
157
158         switch (reg) {
159         case X86_EAX:
160                 o = (gpointer)eax;
161                 break;
162         case X86_EDX:
163                 o = (gpointer)edx;
164                 break;
165         case X86_ECX:
166                 o = (gpointer)ecx;
167                 break;
168         case X86_ESI:
169                 o = (gpointer)esi;
170                 break;
171         case X86_EDI:
172                 o = (gpointer)edi;
173                 break;
174         case X86_EBX:
175                 o = (gpointer)ebx;
176                 break;
177         default:
178                 g_assert_not_reached ();
179         }
180
181         o += disp;
182
183         if (m->klass->valuetype) {
184                 trampoline = *((gpointer *)o) = get_unbox_trampoline (m, addr);
185         } else {
186                 trampoline = *((gpointer *)o) = addr;
187         }
188
189         breakpoint_id = mono_method_has_breakpoint (m, TRUE);
190         if (breakpoint_id) {
191                 mono_remove_breakpoint (breakpoint_id);
192                 return get_breakpoint_trampoline (m, breakpoint_id, trampoline);
193         } else {
194                 return trampoline;
195         }
196 }
197
198 /**
199  * arch_create_jit_trampoline:
200  * @method: pointer to the method info
201  *
202  * Creates a trampoline function for virtual methods. If the created
203  * code is called it first starts JIT compilation of method,
204  * and then calls the newly created method. I also replaces the
205  * corresponding vtable entry (see x86_magic_trampoline).
206  * 
207  * Returns: a pointer to the newly created code 
208  */
209 gpointer
210 arch_create_jit_trampoline (MonoMethod *method)
211 {
212         MonoDomain *domain = mono_domain_get ();
213         guint8 *code, *buf;
214         GHashTable *jit_code_hash;
215
216         /* previously created trampoline code */
217         if (method->info)
218                 return method->info;
219
220         /* we immediately compile runtime provided functions */
221         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
222                 method->info = mono_compile_method (method);
223                 return method->info;
224         }
225
226         /* icalls use method->addr */
227         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
228             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
229                 MonoMethod *nm;
230                 
231                 nm = mono_marshal_get_native_wrapper (method);
232                 method->info = mono_compile_method (nm);
233                 return method->info;
234         }
235
236         /* check if we already have JITed code */
237         if (mono_jit_share_code)
238                 jit_code_hash = mono_root_domain->jit_code_hash;
239         else
240                 jit_code_hash = domain->jit_code_hash;
241
242         if ((code = g_hash_table_lookup (jit_code_hash, method))) {
243                 mono_jit_stats.methods_lookups++;
244                 return code;
245         }
246
247         if (!mono_generic_trampoline_code) {
248                 mono_generic_trampoline_code = buf = g_malloc (256);
249                 /* save caller save regs because we need to do a call */ 
250                 x86_push_reg (buf, X86_EDX);
251                 x86_push_reg (buf, X86_EAX);
252                 x86_push_reg (buf, X86_ECX);
253
254                 /* save LMF begin */
255
256                 /* save the IP (caller ip) */
257                 x86_push_membase (buf, X86_ESP, 16);
258
259                 x86_push_reg (buf, X86_EBX);
260                 x86_push_reg (buf, X86_EDI);
261                 x86_push_reg (buf, X86_ESI);
262                 x86_push_reg (buf, X86_EBP);
263
264                 /* save method info */
265                 x86_push_membase (buf, X86_ESP, 32);
266                 /* get the address of lmf for the current thread */
267                 x86_call_code (buf, mono_get_lmf_addr);
268                 /* push lmf */
269                 x86_push_reg (buf, X86_EAX); 
270                 /* push *lfm (previous_lmf) */
271                 x86_push_membase (buf, X86_EAX, 0);
272                 /* *(lmf) = ESP */
273                 x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
274                 /* save LFM end */
275
276                 /* push the method info */
277                 x86_push_membase (buf, X86_ESP, 44);
278                 /* push the return address onto the stack */
279                 x86_push_membase (buf, X86_ESP, 52);
280
281                 /* save all register values */
282                 x86_push_reg (buf, X86_EBX);
283                 x86_push_reg (buf, X86_EDI);
284                 x86_push_reg (buf, X86_ESI);
285                 x86_push_membase (buf, X86_ESP, 64); /* EDX */
286                 x86_push_membase (buf, X86_ESP, 64); /* ECX */
287                 x86_push_membase (buf, X86_ESP, 64); /* EAX */
288
289                 x86_call_code (buf, x86_magic_trampoline);
290                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
291
292                 /* restore LMF start */
293                 /* ebx = previous_lmf */
294                 x86_pop_reg (buf, X86_EBX);
295                 /* edi = lmf */
296                 x86_pop_reg (buf, X86_EDI);
297                 /* *(lmf) = previous_lmf */
298                 x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
299                 /* discard method info */
300                 x86_pop_reg (buf, X86_ESI);
301                 /* restore caller saved regs */
302                 x86_pop_reg (buf, X86_EBP);
303                 x86_pop_reg (buf, X86_ESI);
304                 x86_pop_reg (buf, X86_EDI);
305                 x86_pop_reg (buf, X86_EBX);
306                 /* discard save IP */
307                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
308                 /* restore LMF end */
309
310                 x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
311
312                 /* call the compiled method */
313                 x86_jump_reg (buf, X86_EAX);
314
315                 g_assert ((buf - mono_generic_trampoline_code) <= 256);
316         }
317
318         code = buf = g_malloc (16);
319         x86_push_imm (buf, method);
320         x86_jump_code (buf, mono_generic_trampoline_code);
321         g_assert ((buf - code) <= 16);
322
323         /* store trampoline address */
324         method->info = code;
325
326         mono_jit_stats.method_trampolines++;
327
328         return code;
329 }