Merge pull request #1218 from AndreyAkinshin/master
[mono.git] / mono / mini / mini-ppc.h
1 #ifndef __MONO_MINI_PPC_H__
2 #define __MONO_MINI_PPC_H__
3
4 #include <mono/arch/ppc/ppc-codegen.h>
5 #include <mono/utils/mono-sigcontext.h>
6 #include <mono/utils/mono-context.h>
7 #include <mono/metadata/object.h>
8 #include <glib.h>
9
10 #ifdef __mono_ppc64__
11 #define MONO_ARCH_CPU_SPEC mono_ppc64_cpu_desc
12 #else
13 #define MONO_ARCH_CPU_SPEC mono_ppcg4
14 #endif
15
16 #define MONO_MAX_IREGS 32
17 #define MONO_MAX_FREGS 32
18
19 #define MONO_SAVED_GREGS 19
20 #define MONO_SAVED_FREGS 18
21
22 #define MONO_ARCH_FRAME_ALIGNMENT 16
23
24 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
25  * reproduceable results for benchmarks */
26 #define MONO_ARCH_CODE_ALIGNMENT 32
27
28 void ppc_patch (guchar *code, const guchar *target);
29 void ppc_patch_full (guchar *code, const guchar *target, gboolean is_fd);
30
31 struct MonoLMF {
32         /*
33          * If the second lowest bit is set to 1, then this is a MonoLMFExt structure, and
34          * the other fields are not valid.
35          */
36         gpointer    previous_lmf;
37         gpointer    lmf_addr;
38         MonoMethod *method;
39         gulong     ebp;
40         gulong     eip;
41         /* Add a dummy field to force iregs to be aligned when cross compiling from x86 */
42         gulong     dummy;
43         mgreg_t    iregs [MONO_SAVED_GREGS]; /* 13..31 */
44         gdouble    fregs [MONO_SAVED_FREGS]; /* 14..31 */
45 };
46
47
48 typedef struct MonoCompileArch {
49         int fp_conv_var_offset;
50 } MonoCompileArch;
51
52 /*
53  * ILP32 uses a version of the ppc64 abi with sizeof(void*)==sizeof(long)==4.
54  * To support this, code needs to follow the following conventions:
55  * - for the size of a pointer use sizeof (gpointer)
56  * - for the size of a register/stack slot use SIZEOF_REGISTER.
57  * - for variables which contain values of registers, use mgreg_t.
58  * - for loading/saving pointers/ints, use the normal ppc_load_reg/ppc_save_reg ()
59  *   macros.
60  * - for loading/saving register sized quantities, use the ppc_ldr/ppc_str 
61  *   macros.
62  * - make sure to not mix the two kinds of macros for the same memory location, 
63  *   since ppc is big endian, so a 8 byte store followed by a 4 byte load will
64  *   load the upper 32 bit of the value.
65  * - use OP_LOADR_MEMBASE/OP_STORER_MEMBASE to load/store register sized
66  *   quantities.
67  */
68
69 #ifdef __mono_ppc64__
70 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
71 #define MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
72
73 /* ELFv2 ABI doesn't use function descriptors.  */
74 #if _CALL_ELF == 2
75 #undef PPC_USES_FUNCTION_DESCRIPTOR
76 #else
77 #define PPC_USES_FUNCTION_DESCRIPTOR
78 #endif
79
80 #ifndef __mono_ilp32__
81 #define MONO_ARCH_HAVE_TLS_GET 1
82 #endif
83
84 #else /* must be __mono_ppc__ */
85
86 #define MONO_ARCH_HAVE_TLS_GET 1
87 #define MONO_ARCH_EMULATE_FCONV_TO_I8 1
88 #define MONO_ARCH_EMULATE_LCONV_TO_R8 1
89 #define MONO_ARCH_EMULATE_LCONV_TO_R4 1
90 #endif
91
92 #define MONO_ARCH_EMULATE_LCONV_TO_R8_UN 1
93 #define MONO_ARCH_EMULATE_FREM 1
94 #define MONO_ARCH_GC_MAPS_SUPPORTED 1
95
96 /* Parameters used by the register allocator */
97 #define MONO_ARCH_CALLEE_REGS ((0xff << ppc_r3) | (1 << ppc_r12) | (1 << ppc_r11))
98 #define MONO_ARCH_CALLEE_SAVED_REGS (0xfffff << ppc_r13) /* ppc_13 - ppc_31 */
99
100 #if defined(__APPLE__) || defined(__mono_ppc64__)
101 #define MONO_ARCH_CALLEE_FREGS (0x1fff << ppc_f1)
102 #else
103 #define MONO_ARCH_CALLEE_FREGS (0xff << ppc_f1)
104 #endif
105 #define MONO_ARCH_CALLEE_SAVED_FREGS (~(MONO_ARCH_CALLEE_FREGS | 1))
106
107 #define MONO_ARCH_USE_FPSTACK FALSE
108 #define MONO_ARCH_FPSTACK_SIZE 0
109
110 #ifdef __mono_ppc64__
111 #define MONO_ARCH_INST_FIXED_REG(desc) (((desc) == 'a')? ppc_r3:\
112                                         ((desc) == 'g'? ppc_f1:-1))
113 #define MONO_ARCH_INST_IS_REGPAIR(desc) FALSE
114 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (-1)
115 #else
116 #define MONO_ARCH_INST_FIXED_REG(desc) (((desc) == 'a')? ppc_r3:\
117                                         ((desc) == 'l')? ppc_r4:\
118                                         ((desc) == 'g'? ppc_f1:-1))
119 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l')
120 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? ppc_r3 : -1)
121 #endif
122
123 #define MONO_ARCH_INST_SREG2_MASK(ins) (0)
124
125 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc == 'f') || (desc == 'g'))
126
127 /* deal with some of the ABI differences here */
128 #ifdef __APPLE__
129 #define PPC_RET_ADDR_OFFSET 8
130 #define PPC_STACK_PARAM_OFFSET 24
131 #define PPC_MINIMAL_STACK_SIZE 24
132 #define PPC_MINIMAL_PARAM_AREA_SIZE 0
133 #define PPC_FIRST_ARG_REG ppc_r3
134 #define PPC_LAST_ARG_REG ppc_r10
135 #define PPC_FIRST_FPARG_REG ppc_f1
136 #define PPC_LAST_FPARG_REG ppc_f13
137 #define PPC_PASS_STRUCTS_BY_VALUE 1
138 #define PPC_PASS_SMALL_FLOAT_STRUCTS_IN_FR_REGS 0
139 #define MONO_ARCH_HAVE_DECOMPOSE_VTYPE_OPTS 0
140 #else
141 /* Linux */
142 #ifdef __mono_ppc64__
143 #define PPC_RET_ADDR_OFFSET 16
144  // Power LE abvi2
145  #if (_CALL_ELF == 2)
146   #define PPC_STACK_PARAM_OFFSET 32
147   #define PPC_MINIMAL_STACK_SIZE 32
148   #define PPC_PASS_SMALL_FLOAT_STRUCTS_IN_FR_REGS 1
149   #define MONO_ARCH_HAVE_DECOMPOSE_VTYPE_OPTS 1
150 // FIXME: To get the test case  finally_block_ending_in_dead_bb  to work properly we need to define the following
151 // and then implement the fuction mono_arch_create_handler_block_trampoline.
152 //  #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
153
154 //  #define DEBUG_ELFABIV2
155
156  #else
157   #define PPC_STACK_PARAM_OFFSET 48
158   #define PPC_MINIMAL_STACK_SIZE 48
159   #define PPC_PASS_SMALL_FLOAT_STRUCTS_IN_FR_REGS 0
160   #define MONO_ARCH_HAVE_DECOMPOSE_VTYPE_OPTS 0
161  #endif
162 #define MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK 1
163 #define PPC_MINIMAL_PARAM_AREA_SIZE 64
164 #define PPC_LAST_FPARG_REG ppc_f13
165 #define PPC_PASS_STRUCTS_BY_VALUE 1
166 #define PPC_THREAD_PTR_REG ppc_r13
167 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
168 #else
169 #define PPC_RET_ADDR_OFFSET 4
170 #define PPC_STACK_PARAM_OFFSET 8
171 #define PPC_MINIMAL_STACK_SIZE 8
172 #define PPC_MINIMAL_PARAM_AREA_SIZE 0
173 #define PPC_LAST_FPARG_REG ppc_f8
174 #define PPC_PASS_STRUCTS_BY_VALUE 0
175 #define PPC_LARGEST_STRUCT_SIZE_TO_RETURN_VIA_REGISTERS 0
176 #define PPC_MOST_FLOAT_STRUCT_MEMBERS_TO_RETURN_VIA_REGISTERS 0
177 #define PPC_PASS_SMALL_FLOAT_STRUCTS_IN_FR_REGS 0
178 #define PPC_RETURN_SMALL_FLOAT_STRUCTS_IN_FR_REGS 0
179 #define PPC_RETURN_SMALL_STRUCTS_IN_REGS 0
180 #define MONO_ARCH_HAVE_DECOMPOSE_VTYPE_OPTS 0
181 #define MONO_ARCH_RETURN_CAN_USE_MULTIPLE_REGISTERS 0
182 #define PPC_THREAD_PTR_REG ppc_r2
183 #endif
184 #define PPC_FIRST_ARG_REG ppc_r3
185 #define PPC_LAST_ARG_REG ppc_r10
186 #define PPC_FIRST_FPARG_REG ppc_f1
187 #endif
188
189 #define PPC_CALL_REG ppc_r12
190
191 #if defined(HAVE_WORKING_SIGALTSTACK) && !defined(__APPLE__)
192 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK 1
193 #define MONO_ARCH_SIGNAL_STACK_SIZE (12 * 1024)
194 #endif /* HAVE_WORKING_SIGALTSTACK */
195
196 #define MONO_ARCH_IMT_REG ppc_r11
197
198 #define MONO_ARCH_VTABLE_REG    ppc_r11
199 #define MONO_ARCH_RGCTX_REG     MONO_ARCH_IMT_REG
200
201 #define MONO_ARCH_NO_IOV_CHECK 1
202 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
203 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
204
205 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
206
207 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
208
209 #define MONO_ARCH_GSHARED_SUPPORTED 1
210
211 #define MONO_ARCH_NEED_DIV_CHECK 1
212 #define MONO_ARCH_AOT_SUPPORTED 1
213 #define MONO_ARCH_NEED_GOT_VAR 1
214 #if !defined(MONO_CROSS_COMPILE) && !defined(TARGET_PS3)
215 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
216 #endif
217 #define MONO_ARCH_HAVE_OP_TAIL_CALL 1
218
219 #define PPC_NUM_REG_ARGS (PPC_LAST_ARG_REG-PPC_FIRST_ARG_REG+1)
220 #define PPC_NUM_REG_FPARGS (PPC_LAST_FPARG_REG-PPC_FIRST_FPARG_REG+1)
221
222 #ifdef MONO_CROSS_COMPILE
223
224 typedef struct {
225         unsigned long sp;
226         unsigned long unused1;
227         unsigned long lr;
228 } MonoPPCStackFrame;
229
230 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) g_assert_not_reached ()
231
232 #elif defined(__APPLE__)
233
234 typedef struct {
235         unsigned long sp;
236         unsigned long unused1;
237         unsigned long lr;
238 } MonoPPCStackFrame;
239
240 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
241                 gpointer r1;                                    \
242                 __asm__ volatile("mr   %0,r1" : "=r" (r1));     \
243                 MONO_CONTEXT_SET_BP ((ctx), r1);                \
244                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
245         } while (0)
246
247 #else
248
249 typedef struct {
250         mgreg_t sp;
251 #ifdef __mono_ppc64__
252         mgreg_t cr;
253 #endif
254         mgreg_t lr;
255 } MonoPPCStackFrame;
256
257 #ifdef G_COMPILER_CODEWARRIOR
258 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
259                 register gpointer r1_var;                                       \
260                 asm { mr r1_var, r1 };  \
261                 MONO_CONTEXT_SET_BP ((ctx), r1);                \
262                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
263         } while (0)
264
265 #else
266 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
267                 gpointer r1;                                    \
268                 __asm__ volatile("mr   %0,1" : "=r" (r1));      \
269                 MONO_CONTEXT_SET_BP ((ctx), r1);                \
270                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
271         } while (0)
272
273 #endif
274 #endif
275
276 #define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf) do { (lmf)->ebp = -1; } while (0)
277
278 typedef struct {
279         gint8 reg;
280         gint8 size;
281         int vtsize;
282         int offset;
283 } MonoPPCArgInfo;
284
285 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
286 typedef struct {
287         gpointer code;
288         gpointer toc;
289         gpointer env;
290 } MonoPPCFunctionDescriptor;
291
292 #define PPC_FTNPTR_SIZE                 sizeof (MonoPPCFunctionDescriptor)
293 extern guint8* mono_ppc_create_pre_code_ftnptr (guint8 *code);
294 #else
295 #define PPC_FTNPTR_SIZE                 0
296 #define mono_ppc_create_pre_code_ftnptr(c)      c
297 #endif
298
299 #if defined(__linux__)
300 #define MONO_ARCH_USE_SIGACTION 1
301 #elif defined (__APPLE__) && defined (_STRUCT_MCONTEXT)
302 #define MONO_ARCH_USE_SIGACTION 1
303 #elif defined (__APPLE__) && !defined (_STRUCT_MCONTEXT)
304 #define MONO_ARCH_USE_SIGACTION 1
305 #elif defined(__NetBSD__)
306 #define MONO_ARCH_USE_SIGACTION 1
307 #elif defined(__FreeBSD__)
308 #define MONO_ARCH_USE_SIGACTION 1
309 #elif defined(MONO_CROSS_COMPILE)
310         typedef MonoContext ucontext_t;
311 /*      typedef struct {
312                 int dummy;
313         } ucontext_t;*/
314         #define UCONTEXT_REG_Rn(ctx, n)
315         #define UCONTEXT_REG_FPRn(ctx, n)
316         #define UCONTEXT_REG_NIP(ctx)
317         #define UCONTEXT_REG_LNK(ctx)
318
319 #else
320 /* For other operating systems, we pull the definition from an external file */
321 #include "mini-ppc-os.h"
322 #endif
323
324 gboolean
325 mono_ppc_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig);
326
327 void
328 mono_ppc_patch (guchar *code, const guchar *target);
329
330 void
331 mono_ppc_throw_exception (MonoObject *exc, unsigned long eip, unsigned long esp, mgreg_t *int_regs, gdouble *fp_regs, gboolean rethrow);
332
333 #ifdef __mono_ppc64__
334 #define MONO_PPC_32_64_CASE(c32,c64)    c64
335 extern void mono_ppc_emitted (guint8 *code, gint64 length, const char *format, ...);
336 #else
337 #define MONO_PPC_32_64_CASE(c32,c64)    c32
338 #endif
339
340 gboolean mono_ppc_is_direct_call_sequence (guint32 *code);
341
342 void mono_ppc_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr);
343
344 void mono_ppc_set_func_into_sigctx (void *sigctx, void *func);
345
346
347 // Debugging macros for ELF ABI v2
348 #ifdef DEBUG_ELFABIV2
349
350 #define DEBUG_ELFABIV2_printf(a, ...) \
351 {if (getenv("DEBUG_ELFABIV2")) { printf(a, ##__VA_ARGS__); fflush(stdout); } }
352
353 #define DEBUG_ELFABIV2_mono_print_ins(a) \
354 {if (getenv("DEBUG_ELFABIV2")) { if (!a) {printf("null\n");} else {mono_print_ins(a);} fflush(stdout); } }
355
356 extern char* mono_type_full_name (MonoType *type);
357
358 #define DEBUG_ELFABIV2_mono_print_type(a) \
359 {if (getenv("DEBUG_ELFABIV2")) { printf("%s, size: %d\n", mono_type_get_name(&a->klass->byval_arg), mini_type_stack_size (NULL, a, 0)); fflush(stdout); } }
360
361 #define DEBUG_ELFABIV2_mono_print_class(a) \
362 {if (getenv("DEBUG_ELFABIV2")) { printf("%s\n", mono_type_get_name(&a->byval_arg)); fflush(stdout); } }
363
364 #else
365
366 #define DEBUG_ELFABIV2_printf(a, ...)
367 #define DEBUG_ELFABIV2_mono_print_ins(a)
368 #define DEBUG_ELFABIV2_mono_print_type(a)
369 #define DEBUG_ELFABIV2_mono_print_class(a)
370
371 #endif
372
373 #endif /* __MONO_MINI_PPC_H__ */