2004-02-15 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mono / mini / tramp-sparc.c
1 /*
2  * tramp-sparc.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 typedef enum {
24         MONO_TRAMPOLINE_GENERIC,
25         MONO_TRAMPOLINE_JUMP,
26         MONO_TRAMPOLINE_CLASS_INIT
27 } MonoTrampolineType;
28
29 /* adapt to mini later... */
30 #define mono_jit_share_code (1)
31
32 /*
33  * Address of the Sparc trampoline code.  This is used by the debugger to check
34  * whether a method is a trampoline.
35  */
36 guint8 *mono_generic_trampoline_code = NULL;
37
38 /*
39  * get_unbox_trampoline:
40  * @m: method pointer
41  * @addr: pointer to native code for @m
42  *
43  * when value type methods are called through the vtable we need to unbox the
44  * this argument. This method returns a pointer to a trampoline which does
45  * unboxing before calling the method
46  */
47 static gpointer
48 get_unbox_trampoline (MonoMethod *m, gpointer addr)
49 {
50         guint8 *code, *start;
51         int this_pos = 4;
52
53         if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
54                 this_pos = 8;
55             
56         start = code = g_malloc (32);
57
58         /* This executes in the context of the caller, hence o0 */
59         sparc_add_imm (code, 0, sparc_o0, sizeof (MonoObject), sparc_o0);
60         sparc_set (code, addr, sparc_g1);
61         sparc_jmpl (code, sparc_g1, sparc_g0, sparc_g0);
62         sparc_nop (code);
63
64         g_assert ((code - start) <= 32);
65
66         mono_arch_flush_icache (start, code - start);
67
68         return start;
69 }
70
71 /**
72  * sparc_magic_trampoline:
73  * @m: the method to translate
74  * @code: the address of the call instruction
75  *
76  * This method is called by the trampoline functions for methods. It calls the
77  * JIT compiler to compile the method, then patches the calling instruction so
78  * further calls will bypass the trampoline. For virtual methods, it finds the
79  * address of the vtable slot and updates it.
80  */
81 static gpointer
82 sparc_magic_trampoline (MonoMethod *m, guint32 *code)
83 {
84         gpointer addr;
85
86         addr = mono_compile_method (m);
87         g_assert (addr);
88
89         /* FIXME: patch calling code and vtable */
90         if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
91                 /* FIXME: is this allways a vcall ? */
92                 /* indirect call through a vtable */
93
94                 if (m->klass->valuetype)
95                         addr = get_unbox_trampoline (m, addr);
96         }
97
98         return addr;
99 }
100
101 static void
102 sparc_class_init_trampoline (MonoVTable *vtable, guint32 *code)
103 {
104         mono_runtime_class_init (vtable);
105
106         /* FIXME: patch calling code */
107 }
108
109 static guchar*
110 create_trampoline_code (MonoTrampolineType tramp_type)
111 {
112         guint8 *buf, *code, *tramp_addr;
113         static guint8* generic_jump_trampoline = NULL;
114         static guint8 *generic_class_init_trampoline = NULL;
115
116         switch (tramp_type) {
117         case MONO_TRAMPOLINE_GENERIC:
118                 if (mono_generic_trampoline_code)
119                         return mono_generic_trampoline_code;
120                 break;
121         case MONO_TRAMPOLINE_JUMP:
122                 if (generic_jump_trampoline)
123                         return generic_jump_trampoline;
124                 break;
125         case MONO_TRAMPOLINE_CLASS_INIT:
126                 if (generic_class_init_trampoline)
127                         return generic_class_init_trampoline;
128                 break;
129         }
130
131         code = buf = g_malloc (256);
132
133         /* FIXME: save lmf etc */
134
135         sparc_save_imm (code, sparc_sp, -200, sparc_sp);
136
137         /* We receive the method address in %r1 */
138         sparc_mov_reg_reg (code, sparc_g1, sparc_o0);
139
140         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
141                 tramp_addr = &sparc_class_init_trampoline;
142         else
143                 tramp_addr = &sparc_magic_trampoline;
144         sparc_call_simple (code, tramp_addr - code);
145         /* set %o1 to caller address in delay slot */
146         sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
147
148         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
149                 sparc_ret (code);
150         else
151                 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
152
153         /* restore previous frame in delay slot */
154         sparc_restore_simple (code);
155
156         g_assert ((code - buf) <= 256);
157
158         switch (tramp_type) {
159         case MONO_TRAMPOLINE_GENERIC:
160                 mono_generic_trampoline_code = buf;
161                 break;
162         case MONO_TRAMPOLINE_JUMP:
163                 generic_jump_trampoline = buf;
164                 break;
165         case MONO_TRAMPOLINE_CLASS_INIT:
166                 generic_class_init_trampoline = buf;
167                 break;
168         }
169
170         /* FIXME: flush icache */
171
172         return buf;
173 }
174
175 #define TRAMPOLINE_SIZE (((SPARC_SET_MAX_SIZE >> 2) * 2) + 2)
176
177 static MonoJitInfo*
178 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type)
179 {
180         MonoJitInfo *ji;
181         guint32 *code, *buf, *tramp;
182
183         tramp = create_trampoline_code (tramp_type);
184
185         code = buf = g_malloc (TRAMPOLINE_SIZE * 4);
186
187         /* %l0 is caller saved so we can use it */
188         sparc_set (code, tramp, sparc_l0);
189         sparc_set (code, arg1, sparc_r1);
190         sparc_jmpl (code, sparc_l0, sparc_g0, sparc_g0);
191         sparc_nop (code);
192
193         g_assert ((code - buf) <= TRAMPOLINE_SIZE);
194
195         ji = g_new0 (MonoJitInfo, 1);
196         ji->code_start = buf;
197         ji->code_size = (code - buf) * 4;
198
199         mono_jit_stats.method_trampolines++;
200
201         /* FIXME: flush icache */
202
203         return ji;
204 }       
205
206 MonoJitInfo*
207 mono_arch_create_jump_trampoline (MonoMethod *method)
208 {
209         MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC);
210
211         ji->method = method;
212         return ji;
213 }
214
215 /**
216  * mono_arch_create_jit_trampoline:
217  * @method: pointer to the method info
218  *
219  * Creates a trampoline function for virtual methods. If the created
220  * code is called it first starts JIT compilation of method,
221  * and then calls the newly created method. I also replaces the
222  * corresponding vtable entry (see sparc_magic_trampoline).
223  * 
224  * Returns: a pointer to the newly created code 
225  */
226 gpointer
227 mono_arch_create_jit_trampoline (MonoMethod *method)
228 {
229         MonoJitInfo *ji;
230
231         /* previously created trampoline code */
232         if (method->info)
233                 return method->info;
234
235         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
236                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
237
238         ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC);
239         method->info = ji->code_start;
240         g_free (ji);
241
242         return method->info;
243 }
244
245 /**
246  * mono_arch_create_class_init_trampoline:
247  *  @vtable: the type to initialize
248  *
249  * Creates a trampoline function to run a type initializer. 
250  * If the trampoline is called, it calls mono_runtime_class_init with the
251  * given vtable, then patches the caller code so it does not get called any
252  * more.
253  * 
254  * Returns: a pointer to the newly created code 
255  */
256 gpointer
257 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
258 {
259         MonoJitInfo *ji;
260         gpointer code;
261
262         ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT);
263         code = ji->code_start;
264         g_free (ji);
265
266         return code;
267 }
268
269 /*
270  * This method is only called when running in the Mono Debugger.
271  */
272 gpointer
273 mono_debugger_create_notification_function (gpointer *notification_address)
274 {
275         guint8 *ptr, *buf;
276
277         ptr = buf = g_malloc0 (16);
278         //x86_breakpoint (buf);
279         if (notification_address)
280                 *notification_address = buf;
281         //x86_ret (buf);
282
283         return ptr;
284 }