2004-01-04 Daniel Morgan <danielmorgan@verizon.net>
[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 NotReady
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         addr = mono_compile_method (m);
91         g_assert (addr);
92
93         /* the method was jumped to */
94         if (!code)
95                 return addr;
96
97         /* go to the start of the call instruction
98          *
99          * address_byte = (m << 6) | (o << 3) | reg
100          * call opcode: 0xff address_byte displacement
101          * 0xff m=1,o=2 imm8
102          * 0xff m=2,o=2 imm32
103          */
104         code -= 6;
105         if ((code [1] != 0xe8) && (code [3] == 0xff) && ((code [4] & 0x18) == 0x10) && ((code [4] >> 6) == 1)) {
106                 reg = code [4] & 0x07;
107                 disp = (signed char)code [5];
108         } else {
109                 if ((code [0] == 0xff) && ((code [1] & 0x18) == 0x10) && ((code [1] >> 6) == 2)) {
110                         reg = code [1] & 0x07;
111                         disp = *((gint32*)(code + 2));
112                 } else if ((code [1] == 0xe8)) {
113                         *((guint32*)(code + 2)) = (guint)addr - ((guint)code + 1) - 5; 
114                         return addr;
115                 } else if ((code [4] == 0xff) && (((code [5] >> 6) & 0x3) == 0) && (((code [5] >> 3) & 0x7) == 2)) {
116                         /*
117                          * This is a interface call: should check the above code can't catch it earlier 
118                          * 8b 40 30   mov    0x30(%eax),%eax
119                          * ff 10      call   *(%eax)
120                          */
121                         disp = 0;
122                         reg = code [5] & 0x07;
123                 } else {
124                         printf ("Invalid trampoline sequence: %x %x %x %x %x %x %x\n", code [0], code [1], code [2], code [3],
125                                 code [4], code [5], code [6]);
126                         g_assert_not_reached ();
127                 }
128         }
129
130         o += disp;
131
132         if (m->klass->valuetype) {
133                 return *((gpointer *)o) = get_unbox_trampoline (m, addr);
134         } else {
135                 return *((gpointer *)o) = addr;
136         }
137 }
138
139 static guchar*
140 create_trampoline_code (int is_jump)
141 {
142         guint8 *buf, *code;
143         static guint8* generic_jump_trampoline = NULL;
144         
145         if (is_jump) {
146                 if (generic_jump_trampoline)
147                         return generic_jump_trampoline;
148         } else {
149                 if (mono_generic_trampoline_code)
150                         return mono_generic_trampoline_code;
151         }
152         
153         code = buf = g_malloc (256);
154         /* save caller save regs because we need to do a call */ 
155         //x86_push_reg (buf, X86_EDX);
156         //x86_push_reg (buf, X86_EAX);
157         //x86_push_reg (buf, X86_ECX);
158
159 #if 0
160         /* save the IP (caller ip) */
161         if (is_jump)
162                 x86_push_imm (buf, 0);
163         else
164                 x86_push_membase (buf, X86_ESP, 16);
165
166         x86_push_reg (buf, X86_EBX);
167         x86_push_reg (buf, X86_EDI);
168         x86_push_reg (buf, X86_ESI);
169         x86_push_reg (buf, X86_EBP);
170
171         /* save method info */
172         x86_push_membase (buf, X86_ESP, 32);
173         /* get the address of lmf for the current thread */
174         x86_call_code (buf, mono_get_lmf_addr);
175         /* push lmf */
176         x86_push_reg (buf, X86_EAX); 
177         /* push *lfm (previous_lmf) */
178         x86_push_membase (buf, X86_EAX, 0);
179         /* *(lmf) = ESP */
180         x86_mov_membase_reg (buf, X86_EAX, 0, X86_ESP, 4);
181         /* save LFM end */
182
183         /* push the method info */
184         x86_push_membase (buf, X86_ESP, 44);
185         /* push the return address onto the stack */
186         if (is_jump)
187                 x86_push_imm (buf, 0);
188         else
189                 x86_push_membase (buf, X86_ESP, 52);
190
191         /* save all register values */
192         x86_push_reg (buf, X86_EBX);
193         x86_push_reg (buf, X86_EDI);
194         x86_push_reg (buf, X86_ESI);
195         x86_push_membase (buf, X86_ESP, 64); /* EDX */
196         x86_push_membase (buf, X86_ESP, 64); /* ECX */
197         x86_push_membase (buf, X86_ESP, 64); /* EAX */
198
199         x86_call_code (buf, x86_magic_trampoline);
200         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 8*4);
201
202         /* restore LMF start */
203         /* ebx = previous_lmf */
204         x86_pop_reg (buf, X86_EBX);
205         /* edi = lmf */
206         x86_pop_reg (buf, X86_EDI);
207         /* *(lmf) = previous_lmf */
208         x86_mov_membase_reg (buf, X86_EDI, 0, X86_EBX, 4);
209         /* discard method info */
210         x86_pop_reg (buf, X86_ESI);
211         /* restore caller saved regs */
212         x86_pop_reg (buf, X86_EBP);
213         x86_pop_reg (buf, X86_ESI);
214         x86_pop_reg (buf, X86_EDI);
215         x86_pop_reg (buf, X86_EBX);
216         /* discard save IP */
217         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 4);             
218         /* restore LMF end */
219
220         x86_alu_reg_imm (buf, X86_ADD, X86_ESP, 16);
221
222         /* call the compiled method */
223         x86_jump_reg (buf, X86_EAX);
224 #endif
225
226         g_assert ((buf - code) <= 256);
227
228         if (is_jump) {
229                 return generic_jump_trampoline = code;
230         } else {
231                 return mono_generic_trampoline_code = code;
232         }
233 }
234
235 #define TRAMPOLINE_SIZE 10
236
237 gpointer
238 mono_arch_create_jump_trampoline (MonoMethod *method)
239 {
240         guint8 *code, *buf, *tramp;
241
242         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
243                 return mono_arch_create_jump_trampoline (mono_marshal_get_synchronized_wrapper (method));
244
245         /* icalls use method->addr */
246         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
247             (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
248                 MonoMethod *nm;
249
250                 if (!method->addr) {
251                         if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
252                                 method->addr = mono_lookup_internal_call (method);
253                         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
254                                 mono_lookup_pinvoke_call (method);
255                 }               
256 #ifdef MONO_USE_EXC_TABLES
257                 if (mono_method_blittable (method)) {
258                         return method->addr;
259                 } else {
260 #endif
261                         nm = mono_marshal_get_native_wrapper (method);
262                         return mono_compile_method (nm);
263 #ifdef MONO_USE_EXC_TABLES
264                 }
265 #endif
266         }
267         
268         tramp = create_trampoline_code (TRUE);
269
270         code = buf = g_malloc (TRAMPOLINE_SIZE);
271         //x86_push_imm (buf, method);
272         //x86_jump_code (buf, tramp);
273         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
274
275         mono_jit_stats.method_trampolines++;
276
277         return code;
278
279 }
280
281 /**
282  * mono_arch_create_jit_trampoline:
283  * @method: pointer to the method info
284  *
285  * Creates a trampoline function for virtual methods. If the created
286  * code is called it first starts JIT compilation of method,
287  * and then calls the newly created method. I also replaces the
288  * corresponding vtable entry (see x86_magic_trampoline).
289  * 
290  * Returns: a pointer to the newly created code 
291  */
292 gpointer
293 mono_arch_create_jit_trampoline (MonoMethod *method)
294 {
295         guint8 *code, *buf;
296
297         /* previously created trampoline code */
298         if (method->info)
299                 return method->info;
300
301         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
302                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
303
304         create_trampoline_code (FALSE);
305
306         code = buf = g_malloc (TRAMPOLINE_SIZE);
307         //x86_push_imm (buf, method);
308         //x86_jump_code (buf, mono_generic_trampoline_code);
309         g_assert ((buf - code) <= TRAMPOLINE_SIZE);
310
311         /* store trampoline address */
312         method->info = code;
313
314         mono_jit_stats.method_trampolines++;
315
316         return code;
317 }
318
319 /*
320  * This method is only called when running in the Mono Debugger.
321  */
322 gpointer
323 mono_debugger_create_notification_function (gpointer *notification_address)
324 {
325         guint8 *ptr, *buf;
326
327         ptr = buf = g_malloc0 (16);
328         //x86_breakpoint (buf);
329         if (notification_address)
330                 *notification_address = buf;
331         //x86_ret (buf);
332
333         return ptr;
334 }
335