2004-05-06 Gonzalo Paniagua Javier <gonzalo@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  * @fp: address of the stack frame for the caller
76  *
77  * This method is called by the trampoline functions for methods. It calls the
78  * JIT compiler to compile the method, then patches the calling instruction so
79  * further calls will bypass the trampoline. For virtual methods, it finds the
80  * address of the vtable slot and updates it.
81  */
82 static gpointer
83 sparc_magic_trampoline (MonoMethod *m, guint32 *code, guint32 *fp)
84 {
85         gpointer addr;
86         gpointer *vtable_slot;
87
88         addr = mono_compile_method (m);
89         g_assert (addr);
90
91         /*
92          * Check whenever this is a virtual call, and call an unbox trampoline if
93          * needed.
94          */
95         if (mono_sparc_is_virtual_call (code)) {
96                 if (m->klass->valuetype)
97                         addr = get_unbox_trampoline (m, addr);
98
99                 /* Compute address of vtable slot */
100                 vtable_slot = mono_sparc_get_vcall_slot_addr (code, fp);
101                 *vtable_slot = addr;
102         }
103         else {
104                 /* Patch calling code */
105                 if (sparc_inst_op (*code) == 0x1)
106                         sparc_call_simple (code, (guint8*)addr - (guint8*)code);
107         }
108
109         return addr;
110 }
111
112 static void
113 sparc_class_init_trampoline (MonoVTable *vtable, guint32 *code)
114 {
115         mono_runtime_class_init (vtable);
116
117         /* Patch calling code */
118         sparc_nop (code);
119 }
120
121 static guchar*
122 create_trampoline_code (MonoTrampolineType tramp_type)
123 {
124         guint8 *buf, *code, *tramp_addr;
125         guint32 lmf_offset;
126         static guint8* generic_jump_trampoline = NULL;
127         static guint8 *generic_class_init_trampoline = NULL;
128
129         switch (tramp_type) {
130         case MONO_TRAMPOLINE_GENERIC:
131                 if (mono_generic_trampoline_code)
132                         return mono_generic_trampoline_code;
133                 break;
134         case MONO_TRAMPOLINE_JUMP:
135                 if (generic_jump_trampoline)
136                         return generic_jump_trampoline;
137                 break;
138         case MONO_TRAMPOLINE_CLASS_INIT:
139                 if (generic_class_init_trampoline)
140                         return generic_class_init_trampoline;
141                 break;
142         }
143
144         code = buf = g_malloc (256);
145
146         /* FIXME: save lmf etc */
147
148         sparc_save_imm (code, sparc_sp, -200, sparc_sp);
149
150         /* We receive the method address in %r1, so save it here */
151         sparc_st_imm (code, sparc_g1, sparc_sp, 128);
152
153         /* Save lmf since compilation can raise exceptions */
154         lmf_offset = - sizeof (MonoLMF);
155
156         /* Save the data for the parent (managed) frame */
157
158         /* Save ip */
159         sparc_st_imm (code, sparc_i7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
160         /* Save sp */
161         sparc_st_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
162         /* Save fp */
163         /* Load previous fp from the saved register window */
164         sparc_flushw (code);
165         sparc_ld_imm (code, sparc_fp, (sparc_i6 - 16) * 4, sparc_o7);
166         sparc_st_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
167         /* Save method */
168         sparc_st_imm (code, sparc_g1, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
169
170         sparc_call_simple (code, (guint8*)mono_get_lmf_addr - (guint8*)code);
171         sparc_nop (code);
172
173         code = mono_sparc_emit_save_lmf (code, lmf_offset);
174
175         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
176                 tramp_addr = &sparc_class_init_trampoline;
177         else
178                 tramp_addr = &sparc_magic_trampoline;
179         sparc_ld_imm (code, sparc_sp, 128, sparc_o0);
180         /* pass parent frame address as third argument */
181         sparc_mov_reg_reg (code, sparc_fp, sparc_o2);
182         sparc_call_simple (code, tramp_addr - code);
183         /* set %o1 to caller address in delay slot */
184         sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
185
186         /* Save result */
187         sparc_st_imm (code, sparc_o0, sparc_sp, 128);
188
189         /* Restore lmf */
190         code = mono_sparc_emit_restore_lmf (code, lmf_offset);
191
192         /* Reload result */
193         sparc_ld_imm (code, sparc_sp, 128, sparc_o0);
194
195         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
196                 sparc_ret (code);
197         else
198                 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
199
200         /* restore previous frame in delay slot */
201         sparc_restore_simple (code);
202
203         g_assert ((code - buf) <= 256);
204
205         switch (tramp_type) {
206         case MONO_TRAMPOLINE_GENERIC:
207                 mono_generic_trampoline_code = buf;
208                 break;
209         case MONO_TRAMPOLINE_JUMP:
210                 generic_jump_trampoline = buf;
211                 break;
212         case MONO_TRAMPOLINE_CLASS_INIT:
213                 generic_class_init_trampoline = buf;
214                 break;
215         }
216
217         mono_arch_flush_icache (buf, code - buf);
218
219         return buf;
220 }
221
222 #define TRAMPOLINE_SIZE (((SPARC_SET_MAX_SIZE >> 2) * 2) + 2)
223
224 static MonoJitInfo*
225 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type)
226 {
227         MonoJitInfo *ji;
228         guint32 *code, *buf, *tramp;
229
230         tramp = create_trampoline_code (tramp_type);
231
232         code = buf = g_malloc (TRAMPOLINE_SIZE * 4);
233
234         /* We have to use g5 here because there is no other free register */
235         sparc_set (code, tramp, sparc_g5);
236         sparc_set (code, arg1, sparc_r1);
237         sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
238         sparc_nop (code);
239
240         g_assert ((code - buf) <= TRAMPOLINE_SIZE);
241
242         ji = g_new0 (MonoJitInfo, 1);
243         ji->code_start = buf;
244         ji->code_size = (code - buf) * 4;
245
246         mono_jit_stats.method_trampolines++;
247
248         mono_arch_flush_icache (ji->code_start, ji->code_size);
249
250         return ji;
251 }       
252
253 MonoJitInfo*
254 mono_arch_create_jump_trampoline (MonoMethod *method)
255 {
256         MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP);
257
258         ji->method = method;
259         return ji;
260 }
261
262 /**
263  * mono_arch_create_jit_trampoline:
264  * @method: pointer to the method info
265  *
266  * Creates a trampoline function for virtual methods. If the created
267  * code is called it first starts JIT compilation of method,
268  * and then calls the newly created method. I also replaces the
269  * corresponding vtable entry (see sparc_magic_trampoline).
270  * 
271  * Returns: a pointer to the newly created code 
272  */
273 gpointer
274 mono_arch_create_jit_trampoline (MonoMethod *method)
275 {
276         MonoJitInfo *ji;
277
278         /* previously created trampoline code */
279         if (method->info)
280                 return method->info;
281
282         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
283                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
284
285         ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC);
286         method->info = ji->code_start;
287         g_free (ji);
288
289         return method->info;
290 }
291
292 /**
293  * mono_arch_create_class_init_trampoline:
294  *  @vtable: the type to initialize
295  *
296  * Creates a trampoline function to run a type initializer. 
297  * If the trampoline is called, it calls mono_runtime_class_init with the
298  * given vtable, then patches the caller code so it does not get called any
299  * more.
300  * 
301  * Returns: a pointer to the newly created code 
302  */
303 gpointer
304 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
305 {
306         MonoJitInfo *ji;
307         gpointer code;
308
309         ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT);
310         code = ji->code_start;
311         g_free (ji);
312
313         return code;
314 }
315
316 /*
317  * This method is only called when running in the Mono Debugger.
318  */
319 gpointer
320 mono_debugger_create_notification_function (gpointer *notification_address)
321 {
322         guint8 *ptr, *buf;
323
324         ptr = buf = g_malloc0 (16);
325         //x86_breakpoint (buf);
326         if (notification_address)
327                 *notification_address = buf;
328         //x86_ret (buf);
329
330         return ptr;
331 }