Merge pull request #4373 from NattyNarwhal/haiku
[mono.git] / mono / mini / mini-x86.h
1 #ifndef __MONO_MINI_X86_H__
2 #define __MONO_MINI_X86_H__
3
4 #include <mono/arch/x86/x86-codegen.h>
5 #include <mono/utils/mono-sigcontext.h>
6 #include <mono/utils/mono-context.h>
7
8 #ifdef HOST_WIN32
9 #include <windows.h>
10 /* use SIG* defines if possible */
11 #ifdef HAVE_SIGNAL_H
12 #include <signal.h>
13 #endif
14
15 typedef void (* MonoW32ExceptionHandler) (int _dummy, EXCEPTION_POINTERS *info, void *context);
16
17 void win32_seh_init(void);
18 void win32_seh_cleanup(void);
19 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
20
21 #ifndef SIGFPE
22 #define SIGFPE 4
23 #endif
24
25 #ifndef SIGILL
26 #define SIGILL 8
27 #endif
28
29 #ifndef SIGSEGV
30 #define SIGSEGV 11
31 #endif
32
33 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
34
35 #endif /* HOST_WIN32 */
36
37 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
38        defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
39 #define MONO_ARCH_USE_SIGACTION
40 #endif
41
42 #if defined(HOST_WATCHOS)
43 #undef MONO_ARCH_USE_SIGACTION
44 #endif
45
46 #ifndef HOST_WIN32
47
48 #ifdef HAVE_WORKING_SIGALTSTACK
49 /* 
50  * solaris doesn't have pthread_getattr_np () needed by the sigaltstack setup
51  * code.
52  */
53 #ifndef __sun
54 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
55 #endif
56 /* Haiku doesn't have SA_SIGINFO */
57 #ifndef __HAIKU__
58 #define MONO_ARCH_USE_SIGACTION
59 #endif /* __HAIKU__ */
60
61 #endif /* HAVE_WORKING_SIGALTSTACK */
62 #endif /* !HOST_WIN32 */
63
64 #define MONO_ARCH_SUPPORT_TASKLETS 1
65
66 #ifndef DISABLE_SIMD
67 #define MONO_ARCH_SIMD_INTRINSICS 1
68 #define MONO_ARCH_NEED_SIMD_BANK 1
69 #endif
70
71 /* we should lower this size and make sure we don't call heavy stack users in the segv handler */
72 #if defined(__APPLE__)
73 #define MONO_ARCH_SIGNAL_STACK_SIZE MINSIGSTKSZ
74 #else
75 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
76 #endif
77 #define MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT 1
78
79 #define MONO_ARCH_CPU_SPEC mono_x86_desc
80
81 #define MONO_MAX_IREGS 8
82 #define MONO_MAX_FREGS 8
83 #define MONO_MAX_XREGS 8
84
85 /* Parameters used by the register allocator */
86 #define MONO_ARCH_CALLEE_REGS X86_CALLEE_REGS
87 #define MONO_ARCH_CALLEE_SAVED_REGS X86_CALLER_REGS
88
89 #define MONO_ARCH_CALLEE_FREGS (0xff & ~(regmask (MONO_ARCH_FPSTACK_SIZE)))
90 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
91
92 /* All registers are clobered by a call */
93 #define MONO_ARCH_CALLEE_XREGS (0xff & ~(regmask (MONO_MAX_XREGS)))
94 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
95
96 #define MONO_ARCH_USE_FPSTACK TRUE
97 #define MONO_ARCH_FPSTACK_SIZE 6
98
99 #define MONO_ARCH_INST_FIXED_REG(desc) (((desc == ' ') || (desc == 'i')) ? -1 : ((desc == 's') ? X86_ECX : ((desc == 'a') ? X86_EAX : ((desc == 'd') ? X86_EDX : ((desc == 'l') ? X86_EAX : -1)))))
100
101 #define MONO_ARCH_INST_FIXED_MASK(desc) ((desc == 'y') ? (X86_BYTE_REGS) : 0)
102
103 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
104 /* 
105  * Originally this contained X86_EDX for div/rem opcodes, but that led to unsolvable 
106  * situations since there are only 3 usable registers for local register allocation.
107  * Instead, we handle the sreg2==edx case in the opcodes.
108  */
109 #define MONO_ARCH_INST_SREG2_MASK(ins) 0
110
111 /*
112  * L is a generic register pair, while l means eax:rdx
113  */
114 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l' || desc == 'L')
115 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? X86_EDX : -1)
116
117 /* must be at a power of 2 and >= 8 */
118 #define MONO_ARCH_FRAME_ALIGNMENT 16
119
120 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
121  * reproduceable results for benchmarks */
122 #define MONO_ARCH_CODE_ALIGNMENT 32
123
124 /*This is the max size of the locals area of a given frame. I think 1MB is a safe default for now*/
125 #define MONO_ARCH_MAX_FRAME_SIZE 0x100000
126
127 /*This is how much a try block must be extended when is is preceeded by a Monitor.Enter() call.
128 It's 4 bytes as this is how many bytes + 1 that 'add 0x10, %esp' takes. It is used to pop the arguments from
129 the monitor.enter call and must be already protected.*/
130 #define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT 4
131
132 struct MonoLMF {
133         /* 
134          * If the lowest bit is set to 1, then this is a trampoline LMF frame.
135          * If the second lowest bit is set to 1, then this is a MonoLMFExt structure, and
136          * the other fields are not valid.
137          */
138         guint32    previous_lmf;
139         gpointer    lmf_addr;
140         /* Only set in trampoline LMF frames */
141         MonoMethod *method;
142         /* Only set in trampoline LMF frames */
143         guint32     esp;
144         guint32     ebx;
145         guint32     edi;
146         guint32     esi;
147         guint32     ebp;
148         guint32     eip;
149 };
150
151 typedef struct {
152         gboolean need_stack_frame_inited;
153         gboolean need_stack_frame;
154         int sp_fp_offset, param_area_size;
155         gpointer cinfo;
156         gpointer ss_tramp_var;
157         gpointer bp_tramp_var;
158 } MonoCompileArch;
159
160 #define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->eax = (gsize)exc; } while (0)
161
162 #ifdef _MSC_VER
163
164 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
165     unsigned int stackptr; \
166         mono_arch_flush_register_windows (); \
167     { \
168            __asm mov stackptr, ebp \
169     } \
170         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
171         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
172         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
173 } while (0)
174
175 #else
176
177 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
178                 mono_arch_flush_register_windows ();    \
179                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
180                 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0));       \
181                 MONO_CONTEXT_SET_SP ((ctx), __builtin_frame_address (0));       \
182         } while (0)
183
184 #endif
185
186 #define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf) do { (lmf)->ebp = -1; } while (0)
187
188 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
189 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
190
191 #define MONO_ARCH_NEED_DIV_CHECK 1
192 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
193 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
194 #define MONO_ARCH_NEED_GOT_VAR 1
195 #ifndef HOST_WIN32
196 /* X86 uses jit_tls->lmf (See emit_push_lmf ()) */
197 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
198 #endif
199 #define MONO_ARCH_IMT_REG X86_EDX
200 #define MONO_ARCH_VTABLE_REG X86_EDX
201 #define MONO_ARCH_RGCTX_REG MONO_ARCH_IMT_REG
202 #define MONO_ARCH_EXC_REG X86_EAX
203 #define MONO_ARCH_HAVE_GENERALIZED_IMT_TRAMPOLINE 1
204 #define MONO_ARCH_HAVE_LIVERANGE_OPS 1
205 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
206 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
207 #define MONO_ARCH_GOT_REG X86_EBX
208 #define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
209 #define MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE 1
210
211 #define MONO_ARCH_HAVE_CMOV_OPS 1
212
213 #ifdef MONO_ARCH_SIMD_INTRINSICS
214 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
215 #endif
216
217 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
218
219 #define MONO_ARCH_AOT_SUPPORTED 1
220
221 #define MONO_ARCH_GSHARED_SUPPORTED 1
222 #define MONO_ARCH_LLVM_SUPPORTED 1
223
224 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
225
226 #define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
227 #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
228
229 #define MONO_ARCH_HAVE_CARD_TABLE_WBARRIER 1
230 #define MONO_ARCH_HAVE_SETUP_RESUME_FROM_SIGNAL_HANDLER_CTX 1
231 #define MONO_ARCH_GC_MAPS_SUPPORTED 1
232 #define MONO_ARCH_HAVE_CONTEXT_SET_INT_REG 1
233 #define MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK 1
234 #define MONO_ARCH_GSHAREDVT_SUPPORTED 1
235 #define MONO_ARCH_HAVE_OP_TAIL_CALL 1
236 #define MONO_ARCH_HAVE_TRANSLATE_TLS_OFFSET 1
237 #define MONO_ARCH_HAVE_DUMMY_INIT 1
238 #define MONO_ARCH_HAVE_SDB_TRAMPOLINES 1
239 #define MONO_ARCH_HAVE_PATCH_CODE_NEW 1
240
241 /* Used for optimization, not complete */
242 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
243
244 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
245             MonoInst *inst; \
246             MONO_INST_NEW ((cfg), inst, OP_X86_COMPARE_MEMBASE_REG); \
247             inst->inst_basereg = array_reg; \
248             inst->inst_offset = offset; \
249             inst->sreg2 = index_reg; \
250             MONO_ADD_INS ((cfg)->cbb, inst); \
251                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
252         } while (0)
253
254 /* Return value marshalling for calls between gsharedvt and normal code */
255 typedef enum {
256         GSHAREDVT_RET_NONE = 0,
257         GSHAREDVT_RET_IREGS = 1,
258         GSHAREDVT_RET_DOUBLE_FPSTACK = 2,
259         GSHAREDVT_RET_FLOAT_FPSTACK = 3,
260         GSHAREDVT_RET_STACK_POP = 4,
261         GSHAREDVT_RET_I1 = 5,
262         GSHAREDVT_RET_U1 = 6,
263         GSHAREDVT_RET_I2 = 7,
264         GSHAREDVT_RET_U2 = 8,
265         GSHAREDVT_RET_IREG = 9
266 } GSharedVtRetMarshal;
267
268 typedef struct {
269         /* Method address to call */
270         gpointer addr;
271         /* The trampoline reads this, so keep the size explicit */
272         int ret_marshal;
273         /* If ret_marshal != NONE, this is the stack slot of the vret arg, else -1 */
274         int vret_arg_slot;
275         /* The stack slot where the return value will be stored */
276         int vret_slot;
277         int stack_usage, map_count;
278         /* If not -1, then make a virtual call using this vtable offset */
279         int vcall_offset;
280         /* If 1, make an indirect call to the address in the rgctx reg */
281         int calli;
282         /* Whenever this is a in or an out call */
283         int gsharedvt_in;
284         int map [MONO_ZERO_LEN_ARRAY];
285 } GSharedVtCallInfo;
286
287 typedef enum {
288         ArgInIReg,
289         ArgInFloatSSEReg,
290         ArgInDoubleSSEReg,
291         ArgOnStack,
292         ArgValuetypeInReg,
293         ArgOnFloatFpStack,
294         ArgOnDoubleFpStack,
295         /* gsharedvt argument passed by addr */
296         ArgGSharedVt,
297         ArgNone
298 } ArgStorage;
299
300 typedef struct {
301         gint16 offset;
302         gint8  reg;
303         ArgStorage storage;
304         int nslots;
305         gboolean is_pair;
306
307         /* Only if storage == ArgValuetypeInReg */
308         ArgStorage pair_storage [2];
309         gint8 pair_regs [2];
310         guint8 pass_empty_struct : 1; // Set in scenarios when empty structs needs to be represented as argument.
311 } ArgInfo;
312
313 typedef struct {
314         int nargs;
315         guint32 stack_usage;
316         guint32 reg_usage;
317         guint32 freg_usage;
318         gboolean need_stack_align;
319         guint32 stack_align_amount;
320         gboolean vtype_retaddr;
321         /* The index of the vret arg in the argument list */
322         int vret_arg_index;
323         int vret_arg_offset;
324         /* Argument space popped by the callee */
325         int callee_stack_pop;
326         ArgInfo ret;
327         ArgInfo sig_cookie;
328         ArgInfo args [1];
329 } CallInfo;
330
331 guint32
332 mono_x86_get_this_arg_offset (MonoMethodSignature *sig);
333
334 void
335 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc, 
336                                                   mgreg_t eip, gboolean rethrow);
337
338 void
339 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, 
340                                                                  mgreg_t eip, gint32 pc_offset);
341
342 void 
343 mono_x86_patch (unsigned char* code, gpointer target);
344
345 gpointer
346 mono_x86_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg);
347
348 CallInfo*
349 mono_arch_get_call_info (MonoMemPool *mp, MonoMethodSignature *sig);
350
351 #endif /* __MONO_MINI_X86_H__ */  
352