Wed Oct 6 12:40:28 CEST 2004 Paolo Molaro <lupus@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, reg;
52
53         if (!m->signature->ret->byref && MONO_TYPE_ISSTRUCT (m->signature->ret))
54                 this_pos = 8;
55             
56         start = code = g_malloc (36);
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 #ifdef SPARCV9
61         reg = sparc_g4;
62 #else
63         reg = sparc_g1;
64 #endif
65         sparc_set (code, addr, reg);
66         sparc_jmpl (code, reg, sparc_g0, sparc_g0);
67         sparc_nop (code);
68
69         g_assert ((code - start) <= 36);
70
71         mono_arch_flush_icache (start, code - start);
72
73         return start;
74 }
75
76 /**
77  * sparc_magic_trampoline:
78  * @m: the method to translate
79  * @code: the address of the call instruction
80  * @fp: address of the stack frame for the caller
81  *
82  * This method is called by the trampoline functions for methods. It calls the
83  * JIT compiler to compile the method, then patches the calling instruction so
84  * further calls will bypass the trampoline. For virtual methods, it finds the
85  * address of the vtable slot and updates it.
86  */
87 static gpointer
88 sparc_magic_trampoline (MonoMethod *m, guint32 *code, guint32 *fp)
89 {
90         gpointer addr;
91         gpointer *vtable_slot;
92
93         addr = mono_compile_method (m);
94         g_assert (addr);
95
96         /*
97          * Check whenever this is a virtual call, and call an unbox trampoline if
98          * needed.
99          */
100         if (mono_sparc_is_virtual_call (code)) {
101                 if (m->klass->valuetype)
102                         addr = get_unbox_trampoline (m, addr);
103
104                 /* Compute address of vtable slot */
105                 vtable_slot = mono_sparc_get_vcall_slot_addr (code, fp);
106                 *vtable_slot = addr;
107         }
108         else {
109                 /* Patch calling code */
110                 if (sparc_inst_op (*code) == 0x1) {
111                         MonoJitInfo *ji = 
112                                 mono_jit_info_table_find (mono_domain_get (), code);
113                         MonoJitInfo *target_ji = 
114                                 mono_jit_info_table_find (mono_domain_get (), addr);
115
116                         /* The first part of the condition means an icall without a wrapper */
117                         if ((!target_ji && m->addr) || mono_method_same_domain (ji, target_ji)) {
118                                 sparc_call_simple (code, (guint8*)addr - (guint8*)code);
119                         }
120                 }
121         }
122
123         return addr;
124 }
125
126 static void
127 sparc_class_init_trampoline (MonoVTable *vtable, guint32 *code)
128 {
129         mono_runtime_class_init (vtable);
130
131         /* Patch calling code */
132         sparc_nop (code);
133 }
134
135 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
136
137 static guchar*
138 create_trampoline_code (MonoTrampolineType tramp_type)
139 {
140         guint8 *buf, *code, *tramp_addr;
141         guint32 lmf_offset, method_reg, i;
142         static guint8* generic_jump_trampoline = NULL;
143         static guint8 *generic_class_init_trampoline = NULL;
144
145         switch (tramp_type) {
146         case MONO_TRAMPOLINE_GENERIC:
147                 if (mono_generic_trampoline_code)
148                         return mono_generic_trampoline_code;
149                 break;
150         case MONO_TRAMPOLINE_JUMP:
151                 if (generic_jump_trampoline)
152                         return generic_jump_trampoline;
153                 break;
154         case MONO_TRAMPOLINE_CLASS_INIT:
155                 if (generic_class_init_trampoline)
156                         return generic_class_init_trampoline;
157                 break;
158         }
159
160         code = buf = g_malloc (512);
161
162         sparc_save_imm (code, sparc_sp, -608, sparc_sp);
163
164 #ifdef SPARCV9
165         method_reg = sparc_g4;
166 #else
167         method_reg = sparc_g1;
168 #endif
169
170 #ifdef SPARCV9
171         /* Save fp regs since they are not preserved by calls */
172         for (i = 0; i < 16; i ++)
173                 sparc_stdf_imm (code, sparc_f0 + (i * 2), sparc_sp, MONO_SPARC_STACK_BIAS + 320 + (i * 8));
174 #endif  
175
176         /* We receive the method address in %r1, so save it here */
177         sparc_sti_imm (code, method_reg, sparc_sp, MONO_SPARC_STACK_BIAS + 200);
178
179         /* Save lmf since compilation can raise exceptions */
180         lmf_offset = MONO_SPARC_STACK_BIAS - sizeof (MonoLMF);
181
182         /* Save the data for the parent (managed) frame */
183
184         /* Save ip */
185         sparc_sti_imm (code, sparc_i7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
186         /* Save sp */
187         sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
188         /* Save fp */
189         /* Load previous fp from the saved register window */
190         sparc_flushw (code);
191         sparc_ldi_imm (code, sparc_fp, MONO_SPARC_STACK_BIAS + (sparc_i6 - 16) * sizeof (gpointer), sparc_o7);
192         sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
193         /* Save method */
194         sparc_sti_imm (code, method_reg, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
195
196         sparc_set (code, mono_get_lmf_addr, sparc_o7);
197         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
198         sparc_nop (code);
199
200         code = mono_sparc_emit_save_lmf (code, lmf_offset);
201
202         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
203                 tramp_addr = &sparc_class_init_trampoline;
204         else
205                 tramp_addr = &sparc_magic_trampoline;
206         sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 200, sparc_o0);
207         /* pass parent frame address as third argument */
208         sparc_mov_reg_reg (code, sparc_fp, sparc_o2);
209         sparc_set (code, tramp_addr, sparc_o7);
210         /* set %o1 to caller address */
211         sparc_mov_reg_reg (code, sparc_i7, sparc_o1);
212         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
213         sparc_nop (code);
214
215         /* Save result */
216         sparc_sti_imm (code, sparc_o0, sparc_sp, MONO_SPARC_STACK_BIAS + 304);
217
218         /* Restore lmf */
219         code = mono_sparc_emit_restore_lmf (code, lmf_offset);
220
221         /* Reload result */
222         sparc_ldi_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 304, sparc_o0);
223
224 #ifdef SPARCV9
225         /* Reload fp regs */
226         for (i = 0; i < 16; i ++)
227                 sparc_lddf_imm (code, sparc_sp, MONO_SPARC_STACK_BIAS + 320 + (i * 8), sparc_f0 + (i * 2));
228 #endif  
229
230         if (tramp_type == MONO_TRAMPOLINE_CLASS_INIT)
231                 sparc_ret (code);
232         else
233                 sparc_jmpl (code, sparc_o0, sparc_g0, sparc_g0);
234
235         /* restore previous frame in delay slot */
236         sparc_restore_simple (code);
237
238 /*
239 {
240         gpointer addr;
241
242         sparc_save_imm (code, sparc_sp, -608, sparc_sp);
243         addr = code;
244         sparc_call_simple (code, 16);
245         sparc_nop (code);
246         sparc_rett_simple (code);
247         sparc_nop (code);
248
249         sparc_save_imm (code, sparc_sp, -608, sparc_sp);
250         sparc_ta (code, 1);
251         tramp_addr = &sparc_magic_trampoline;
252         sparc_call_simple (code, tramp_addr - code);
253         sparc_nop (code);
254         sparc_rett_simple (code);
255         sparc_nop (code);
256 }
257 */
258
259         g_assert ((code - buf) <= 512);
260
261         switch (tramp_type) {
262         case MONO_TRAMPOLINE_GENERIC:
263                 mono_generic_trampoline_code = buf;
264                 break;
265         case MONO_TRAMPOLINE_JUMP:
266                 generic_jump_trampoline = buf;
267                 break;
268         case MONO_TRAMPOLINE_CLASS_INIT:
269                 generic_class_init_trampoline = buf;
270                 break;
271         }
272
273         mono_arch_flush_icache (buf, code - buf);
274
275         return buf;
276 }
277
278 #define TRAMPOLINE_SIZE (((SPARC_SET_MAX_SIZE >> 2) * 2) + 2)
279
280 static MonoJitInfo*
281 create_specific_trampoline (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain)
282 {
283         MonoJitInfo *ji;
284         guint32 *code, *buf, *tramp;
285
286         tramp = create_trampoline_code (tramp_type);
287
288         mono_domain_lock (domain);
289         code = buf = mono_code_manager_reserve (domain->code_mp, TRAMPOLINE_SIZE * 4);
290         mono_domain_unlock (domain);
291
292         /* We have to use g5 here because there is no other free register */
293         sparc_set (code, tramp, sparc_g5);
294 #ifdef SPARCV9
295         sparc_set (code, arg1, sparc_g4);
296 #else
297         sparc_set (code, arg1, sparc_g1);
298 #endif
299         sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
300         sparc_nop (code);
301
302         g_assert ((code - buf) <= TRAMPOLINE_SIZE);
303
304         ji = g_new0 (MonoJitInfo, 1);
305         ji->code_start = buf;
306         ji->code_size = (code - buf) * 4;
307
308         mono_jit_stats.method_trampolines++;
309
310         mono_arch_flush_icache (ji->code_start, ji->code_size);
311
312         return ji;
313 }       
314
315 MonoJitInfo*
316 mono_arch_create_jump_trampoline (MonoMethod *method)
317 {
318         MonoJitInfo *ji = create_specific_trampoline (method, MONO_TRAMPOLINE_JUMP, mono_domain_get ());
319
320         ji->method = method;
321         return ji;
322 }
323
324 /**
325  * mono_arch_create_jit_trampoline:
326  * @method: pointer to the method info
327  *
328  * Creates a trampoline function for virtual methods. If the created
329  * code is called it first starts JIT compilation of method,
330  * and then calls the newly created method. I also replaces the
331  * corresponding vtable entry (see sparc_magic_trampoline).
332  * 
333  * Returns: a pointer to the newly created code 
334  */
335 gpointer
336 mono_arch_create_jit_trampoline (MonoMethod *method)
337 {
338         MonoJitInfo *ji;
339
340         /* previously created trampoline code */
341         if (method->info)
342                 return method->info;
343
344         if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
345                 return mono_arch_create_jit_trampoline (mono_marshal_get_synchronized_wrapper (method));
346
347         ji = create_specific_trampoline (method, MONO_TRAMPOLINE_GENERIC, mono_domain_get ());
348         method->info = ji->code_start;
349         g_free (ji);
350
351         return method->info;
352 }
353
354 /**
355  * mono_arch_create_class_init_trampoline:
356  *  @vtable: the type to initialize
357  *
358  * Creates a trampoline function to run a type initializer. 
359  * If the trampoline is called, it calls mono_runtime_class_init with the
360  * given vtable, then patches the caller code so it does not get called any
361  * more.
362  * 
363  * Returns: a pointer to the newly created code 
364  */
365 gpointer
366 mono_arch_create_class_init_trampoline (MonoVTable *vtable)
367 {
368         MonoJitInfo *ji;
369         gpointer code;
370
371         ji = create_specific_trampoline (vtable, MONO_TRAMPOLINE_CLASS_INIT, vtable->domain);
372         code = ji->code_start;
373         g_free (ji);
374
375         return code;
376 }
377
378 /*
379  * This method is only called when running in the Mono Debugger.
380  */
381 gpointer
382 mono_debugger_create_notification_function (gpointer *notification_address)
383 {
384         guint8 *ptr, *buf;
385
386         ptr = buf = g_malloc0 (16);
387         if (notification_address)
388                 *notification_address = buf;
389
390         g_assert_not_reached ();
391
392         return ptr;
393 }