Wed Jul 30 17:52:41 CEST 2003 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / mini / tramp-sparc.c
1 /*
2  * trampolinesparc.c: JIT trampoline code for Sparc 64
3  *
4  * Authors:
5  *   Mark Crichton (crichton@gimp.org)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * (C) 2003 Ximian, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13
14 #include <mono/arch/sparc/sparc-codegen.h>
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/marshal.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/mono-debug-debugger.h>
19
20 #include "mini.h"
21 #include "mini-sparc.h"
22
23 #warning Not Sparc ready!  MEEP!
24
25 /* adapt to mini later... */
26 #define mono_jit_share_code (1)
27
28 /*
29  * Address of the Sparc trampoline code.  This is used by the debugger to check
30  * whether a method is a trampoline.
31  */
32 guint8 *mono_generic_trampoline_code = NULL;
33
34 /*
35  * get_unbox_trampoline:
36  * @m: method pointer
37  * @addr: pointer to native code for @m
38  *
39  * when value type methods are called through the vtable we need to unbox the
40  * this argument. This method returns a pointer to a trampoline which does
41  * unboxing before calling the method
42  */
43 static gpointer
44 get_unbox_trampoline (MonoMethod *m, gpointer addr)
45 {
46         guint8 *code, *start;
47         int this_pos = 4;
48
49         if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
50                 this_pos = 8;
51             
52         start = code = g_malloc (16);
53
54         //x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
55         //x86_jump_code (code, addr);
56         g_assert ((code - start) < 16);
57
58         return start;
59 }
60
61 /**
62  * x86_magic_trampoline:
63  * @eax: saved x86 register 
64  * @ecx: saved x86 register 
65  * @edx: saved x86 register 
66  * @esi: saved x86 register 
67  * @edi: saved x86 register 
68  * @ebx: saved x86 register
69  * @code: pointer into caller code
70  * @method: the method to translate
71  *
72  * This method is called by the trampoline functions for virtual
73  * methods. It inspects the caller code to find the address of the
74  * vtable slot, then calls the JIT compiler and writes the address
75  * of the compiled method back to the vtable. All virtual methods 
76  * are called with: x86_call_membase (inst, basereg, disp). We always
77  * use 32 bit displacement to ensure that the length of the call 
78  * instruction is 6 bytes. We need to get the value of the basereg 
79  * and the constant displacement.
80  */
81 static gpointer
82 x86_magic_trampoline (int eax, int ecx, int edx, int esi, int edi, 
83                       int ebx, guint8 *code, MonoMethod *m)
84 {
85         guint8 reg;
86         gint32 disp;
87         char *o;
88         gpointer addr;
89
90         EnterCriticalSection (metadata_section);
91         addr = mono_compile_method (m);
92         LeaveCriticalSection (metadata_section);
93         g_assert (addr);
94
95         /* the method was jumped to */
96         if (!code)
97                 return addr;
98
99         /* go to the start of the call instruction
100          *
101          * address_byte = (m << 6) | (o << 3) | reg
102          * call opcode: 0xff address_byte displacement
103          * 0xff m=1,o=2 imm8
104          * 0xff m=2,o=2 imm32
105          */
106         code -= 6;
107         if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
108                 reg = code [4] & 0x07;
109                 disp = (signed char)code [5];
110         } else {
111                 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
112                         reg = code [1] & 0x07;
113                         disp = *((gint32*)(code + 2));
114                 } else if ((code [1] == 0xe8)) {
115                         *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5; 
116                         return addr;
117                 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
118                         /*
119                          * This is a interface call: should check the above code can't catch it earlier 
120                          * 8b 40 30   mov    0x30(%eax),%eax
121                          * ff 10      call   *(%eax)
122                          */
123                         disp = 0;
124                         reg = code [5] & 0x07;
125                 } else {
126                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
127                                 code [4], code [5], code [6]);
128                         g_assert_not_reached ();
129                 }
130         }
131
132         o += disp;
133
134         if (m->klass->valuetype) {
135                 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
136         } else {
137                 return *((gpointer *)o) = addr;
138         }
139 }
140
141 static guchar*
142 create_trampoline_code (int is_jump)
143 {
144         guint8 *buf, *code;
145         static guint8* generic_jump_trampoline = NULL;
146         
147         if (is_jump) {
148                 if (generic_jump_trampoline)
149                         return generic_jump_trampoline;
150         } else {
151                 if (mono_generic_trampoline_code)
152                         return mono_generic_trampoline_code;
153         }
154         
155         code = buf = g_malloc (256);
156         /* save caller save regs because we need to do a call */ 
157         //x86_push_reg (buf, X86_EDX);
158         //x86_push_reg (buf, X86_EAX);
159         //x86_push_reg (buf, X86_ECX);
160
161 #if 0
162         /* save the IP (caller ip) */
163         if (is_jump)
164                 x86_push_imm (buf, 0);
165         else
166                 x86_push_membase (buf, X86_ESP, 16);
167
168         x86_push_reg (buf, X86_EBX);
169         x86_push_reg (buf, X86_EDI);
170         x86_push_reg (buf, X86_ESI);
171         x86_push_reg (buf, X86_EBP);
172
173         /* save method info */
174         x86_push_membase (buf, X86_ESP, 32);
175         /* get the address of lmf for the current thread */
176         x86_call_code (buf, mono_get_lmf_addr);
177         /* push lmf */
178         x86_push_reg (buf, X86_EAX); 
179         /* push *lfm (previous_lmf) */
180         x86_push_membase (buf, X86_EAX, 0);
181         /* *(lmf) = ESP */
182         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
183         /* save LFM end */
184
185         /* push the method info */
186         x86_push_membase (buf, X86_ESP, 44);
187         /* push the return address onto the stack */
188         if (is_jump)
189                 x86_push_imm (buf, 0);
190         else
191                 x86_push_membase (buf, X86_ESP, 52);
192
193         /* save all register values */
194         x86_push_reg (buf, X86_EBX);
195         x86_push_reg (buf, X86_EDI);
196         x86_push_reg (buf, X86_ESI);
197         x86_push_membase (buf, X86_ESP, 64); /* EDX */
198         x86_push_membase (buf, X86_ESP, 64); /* ECX */
199         x86_push_membase (buf, X86_ESP, 64); /* EAX */
200
201         x86_call_code (buf, x86_magic_trampoline);
202         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
203
204         /* restore LMF start */
205         /* ebx = previous_lmf */
206         x86_pop_reg (buf, X86_EBX);
207         /* edi = lmf */
208         x86_pop_reg (buf, X86_EDI);
209         /* *(lmf) = previous_lmf */
210         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
211         /* discard method info */
212         x86_pop_reg (buf, X86_ESI);
213         /* restore caller saved regs */
214         x86_pop_reg (buf, X86_EBP);
215         x86_pop_reg (buf, X86_ESI);
216         x86_pop_reg (buf, X86_EDI);
217         x86_pop_reg (buf, X86_EBX);
218         /* discard save IP */
219         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
220         /* restore LMF end */
221
222         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
223
224         /* call the compiled method */
225         x86_jump_reg (buf, X86_EAX);
226 #endif
227
228         g_assert ((buf - code) <= 256);
229
230         if (is_jump) {
231                 return generic_jump_trampoline = code;
232         } else {
233                 return mono_generic_trampoline_code = code;
234         }
235 }
236
237 #define TRAMPOLINE_SIZE 10
238
239 gpointer
240 mono_arch_create_jump_trampoline (MonoMethod *method)
241 {
242         guint8 *code, *buf, *tramp;
243
244         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
245                 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
246
247         /* icalls use method->addr */
248         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
249             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
250                 MonoMethod *nm;
251                 
252                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
253                         mono_lookup_pinvoke_call (method);
254
255 #ifdef MONO_USE_EXC_TABLES
256                 if (mono_method_blittable (method)) {
257                         return method->addr;
258                 } else {
259 #endif
260                         nm = mono_marshal_get_native_wrapper (method);
261                         return mono_compile_method (nm);
262 #ifdef MONO_USE_EXC_TABLES
263                 }
264 #endif
265         }
266         
267         tramp = create_trampoline_code (TRUE);
268
269         code = buf = g_malloc (TRAMPOLINE_SIZE);
270         //x86_push_imm (buf, method);
271         //x86_jump_code (buf, tramp);
272         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
273
274         mono_jit_stats.method_trampolines++;
275
276         return code;
277
278 }
279
280 /**
281  * mono_arch_create_jit_trampoline:
282  * @method: pointer to the method info
283  *
284  * Creates a trampoline function for virtual methods. If the created
285  * code is called it first starts JIT compilation of method,
286  * and then calls the newly created method. I also replaces the
287  * corresponding vtable entry (see x86_magic_trampoline).
288  * 
289  * Returns: a pointer to the newly created code 
290  */
291 gpointer
292 mono_arch_create_jit_trampoline (MonoMethod *method)
293 {
294         guint8 *code, *buf;
295
296         /* previously created trampoline code */
297         if (method->info)
298                 return method->info;
299
300         /* we immediately compile runtime provided functions */
301         if (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) {
302                 method->info = mono_compile_method (method);
303                 return method->info;
304         }
305
306         /* icalls use method->addr */
307         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
308             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
309                 MonoMethod *nm;
310                 
311                 if (!method->addr && (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
312                         mono_lookup_pinvoke_call (method);
313
314 #ifdef MONO_USE_EXC_TABLES
315                 if (mono_method_blittable (method)) {
316                         method->info = method->addr;
317                 } else {
318 #endif
319                         nm = mono_marshal_get_native_wrapper (method);
320                         method->info = mono_compile_method (nm);
321 #ifdef MONO_USE_EXC_TABLES
322                 }
323 #endif
324                 return method->info;
325         }
326         
327         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
328                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
329
330         create_trampoline_code (FALSE);
331
332         code = buf = g_malloc (TRAMPOLINE_SIZE);
333         //x86_push_imm (buf, method);
334         //x86_jump_code (buf, mono_generic_trampoline_code);
335         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
336
337         /* store trampoline address */
338         method->info = code;
339
340         mono_jit_stats.method_trampolines++;
341
342         return code;
343 }
344
345 /*
346  * This method is only called when running in the Mono Debugger.
347  */
348 gpointer
349 mono_debugger_create_notification_function (gpointer *notification_address)
350 {
351         guint8 *ptr, *buf;
352
353         ptr = buf = g_malloc0 (16);
354         //x86_breakpoint (buf);
355         if (notification_address)
356                 *notification_address = buf;
357         //x86_ret (buf);
358
359         return ptr;
360 }
361