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