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