ATOMIC_CAS with 3 sregs.
[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 #ifdef PLATFORM_WIN32
6 #include <windows.h>
7 /* use SIG* defines if possible */
8 #ifdef HAVE_SIGNAL_H
9 #include <signal.h>
10 #endif
11
12 /* sigcontext surrogate */
13 struct sigcontext {
14         unsigned int eax;
15         unsigned int ebx;
16         unsigned int ecx;
17         unsigned int edx;
18         unsigned int ebp;
19         unsigned int esp;
20         unsigned int esi;
21         unsigned int edi;
22         unsigned int eip;
23 };
24
25 typedef void (* MonoW32ExceptionHandler) (int);
26 void win32_seh_init(void);
27 void win32_seh_cleanup(void);
28 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
29
30 #ifndef SIGFPE
31 #define SIGFPE 4
32 #endif
33
34 #ifndef SIGILL
35 #define SIGILL 8
36 #endif
37
38 #ifndef SIGSEGV
39 #define SIGSEGV 11
40 #endif
41
42 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
43
44 #endif /* PLATFORM_WIN32 */
45
46 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__)
47 #define MONO_ARCH_USE_SIGACTION
48 #endif
49
50 #ifndef PLATFORM_WIN32
51
52 #ifdef HAVE_WORKING_SIGALTSTACK
53 /* 
54  * solaris doesn't have pthread_getattr_np () needed by the sigaltstack setup
55  * code.
56  */
57 #ifndef __sun
58 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
59 #endif
60 #define MONO_ARCH_USE_SIGACTION
61
62 #endif /* HAVE_WORKING_SIGALTSTACK */
63 #endif /* !PLATFORM_WIN32 */
64
65 #define MONO_ARCH_SUPPORT_SIMD_INTRINSICS 1
66
67 #ifndef DISABLE_SIMD
68 #define MONO_ARCH_SIMD_INTRINSICS 1
69 #define MONO_ARCH_NEED_SIMD_BANK 1
70 #endif
71
72 /* we should lower this size and make sure we don't call heavy stack users in the segv handler */
73 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
74 #define MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT 1
75
76 #define MONO_ARCH_CPU_SPEC x86_desc
77
78 #define MONO_MAX_IREGS 8
79 #define MONO_MAX_FREGS 8
80 #define MONO_MAX_XREGS 8
81
82 /* Parameters used by the register allocator */
83 #define MONO_ARCH_CALLEE_REGS X86_CALLEE_REGS
84 #define MONO_ARCH_CALLEE_SAVED_REGS X86_CALLER_REGS
85
86 #define MONO_ARCH_CALLEE_FREGS (0xff & ~(regmask (MONO_ARCH_FPSTACK_SIZE)))
87 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
88
89 /* All registers are clobered by a call */
90 #define MONO_ARCH_CALLEE_XREGS (0xff & ~(regmask (MONO_MAX_XREGS)))
91 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
92
93 #define MONO_ARCH_USE_FPSTACK TRUE
94 #define MONO_ARCH_FPSTACK_SIZE 6
95
96 #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)))))
97
98 #define MONO_ARCH_INST_FIXED_MASK(desc) ((desc == 'y') ? (X86_BYTE_REGS) : 0)
99
100 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
101 /* 
102  * Originally this contained X86_EDX for div/rem opcodes, but that led to unsolvable 
103  * situations since there are only 3 usable registers for local register allocation.
104  * Instead, we handle the sreg2==edx case in the opcodes.
105  */
106 #define MONO_ARCH_INST_SREG2_MASK(ins) 0
107
108 /*
109  * L is a generic register pair, while l means eax:rdx
110  */
111 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l' || desc == 'L')
112 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? X86_EDX : -1)
113
114 /* must be at a power of 2 and >= 8 */
115 #define MONO_ARCH_FRAME_ALIGNMENT 16
116
117 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
118  * reproduceable results for benchmarks */
119 #define MONO_ARCH_CODE_ALIGNMENT 32
120
121 #define MONO_ARCH_RETREG1 X86_EAX
122 #define MONO_ARCH_RETREG2 X86_EDX
123
124 struct MonoLMF {
125         /* Offset by 1 if this is a trampoline LMF frame */
126         guint32    previous_lmf;
127         gpointer    lmf_addr;
128         /* Only set in trampoline LMF frames */
129         MonoMethod *method;
130         /* Only set in trampoline LMF frames */
131         guint32     esp;
132         guint32     ebx;
133         guint32     edi;
134         guint32     esi;
135         guint32     ebp;
136         guint32     eip;
137 };
138
139 typedef struct {
140         gboolean need_stack_frame_inited;
141         gboolean need_stack_frame;
142 } MonoCompileArch;
143
144 #if defined(__FreeBSD__) || defined(__APPLE__)
145 #include <ucontext.h>
146 #endif 
147
148 #if defined(__FreeBSD__)
149         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.mc_eax)
150         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.mc_ebx)
151         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.mc_ecx)
152         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.mc_edx)
153         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.mc_ebp)
154         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.mc_esp)
155         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.mc_esi)
156         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.mc_edi)
157         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.mc_eip)
158 #elif defined(__APPLE__) && defined(_STRUCT_MCONTEXT)
159         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext->__ss.__eax)
160         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext->__ss.__ebx)
161         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext->__ss.__ecx)
162         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext->__ss.__edx)
163         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext->__ss.__ebp)
164         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext->__ss.__esp)
165         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext->__ss.__esi)
166         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext->__ss.__edi)
167         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext->__ss.__eip)
168 #elif defined(__APPLE__) && !defined(_STRUCT_MCONTEXT)
169         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext->ss.eax)
170         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext->ss.ebx)
171         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext->ss.ecx)
172         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext->ss.edx)
173         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext->ss.ebp)
174         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext->ss.esp)
175         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext->ss.esi)
176         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext->ss.edi)
177         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext->ss.eip)
178 #elif defined(__NetBSD__)
179         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EAX])
180         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EBX])
181         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ECX])
182         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EDX])
183         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EBP])
184         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ESP])
185         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ESI])
186         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EDI])
187         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EIP])
188 #else
189         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.gregs [REG_EAX])
190         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.gregs [REG_EBX])
191         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.gregs [REG_ECX])
192         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.gregs [REG_EDX])
193         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.gregs [REG_EBP])
194         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.gregs [REG_ESP])
195         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.gregs [REG_ESI])
196         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.gregs [REG_EDI])
197         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.gregs [REG_EIP])
198 #endif
199
200 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
201 # define SC_EAX sc_eax
202 # define SC_EBX sc_ebx
203 # define SC_ECX sc_ecx
204 # define SC_EDX sc_edx
205 # define SC_EBP sc_ebp
206 # define SC_EIP sc_eip
207 # define SC_ESP sc_esp
208 # define SC_EDI sc_edi
209 # define SC_ESI sc_esi
210 #else
211 # define SC_EAX eax
212 # define SC_EBX ebx
213 # define SC_ECX ecx
214 # define SC_EDX edx
215 # define SC_EBP ebp
216 # define SC_EIP eip
217 # define SC_ESP esp
218 # define SC_EDI edi
219 # define SC_ESI esi
220 #endif
221
222 typedef struct {
223         guint32 eax;
224         guint32 ebx;
225         guint32 ecx;
226         guint32 edx;
227         guint32 ebp;
228         guint32 esp;
229     guint32 esi;
230         guint32 edi;
231         guint32 eip;
232 } MonoContext;
233
234 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (long)(ip); } while (0); 
235 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (long)(bp); } while (0); 
236 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (long)(sp); } while (0); 
237
238 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
239 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
240 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
241
242 #ifdef _MSC_VER
243
244 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
245     unsigned int stackptr; \
246         mono_arch_flush_register_windows (); \
247     { \
248            __asm mov stackptr, ebp \
249     } \
250         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
251         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
252         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
253 } while (0)
254
255 #else
256
257 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
258                 mono_arch_flush_register_windows ();    \
259                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
260                 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0));       \
261                 MONO_CONTEXT_SET_SP ((ctx), __builtin_frame_address (0));       \
262         } while (0)
263
264 #endif
265
266 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
267 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
268
269 #define MONO_ARCH_BIGMUL_INTRINS 1
270 #define MONO_ARCH_NEED_DIV_CHECK 1
271 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
272 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
273 #define MONO_ARCH_NEED_GOT_VAR 1
274 #define MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION 1
275 #define MONO_ARCH_ENABLE_EMIT_STATE_OPT 1
276 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
277 #define MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN 1
278 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
279 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
280 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
281 #define MONO_ARCH_HAVE_ATOMIC_CAS 1
282 #define MONO_ARCH_HAVE_IMT 1
283 #define MONO_ARCH_HAVE_TLS_GET 1
284 #define MONO_ARCH_IMT_REG X86_EDX
285 #define MONO_ARCH_VTABLE_REG X86_EDX
286 #define MONO_ARCH_COMMON_VTABLE_TRAMPOLINE 1
287 #define MONO_ARCH_RGCTX_REG X86_EDX
288 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
289 #define MONO_ARCH_HAVE_LIVERANGE_OPS 1
290 #define MONO_ARCH_HAVE_XP_UNWIND 1
291 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
292 #if defined(__linux__) && !defined(HAVE_MOVING_COLLECTOR)
293 #define MONO_ARCH_MONITOR_OBJECT_REG X86_EAX
294 #endif
295
296 #define MONO_ARCH_HAVE_CMOV_OPS 1
297
298 #ifdef MONO_ARCH_SIMD_INTRINSICS
299 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
300 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
301 #endif
302
303 #if !defined(__APPLE__)
304 #define MONO_ARCH_AOT_SUPPORTED 1
305 #endif
306
307 #if defined(__linux__) || defined(__sun)
308 #define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
309 #endif
310
311 /* Used for optimization, not complete */
312 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
313
314 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
315             MonoInst *inst; \
316             MONO_INST_NEW ((cfg), inst, OP_X86_COMPARE_MEMBASE_REG); \
317             inst->inst_basereg = array_reg; \
318             inst->inst_offset = offset; \
319             inst->sreg2 = index_reg; \
320             MONO_ADD_INS ((cfg)->cbb, inst); \
321                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
322         } while (0)
323
324 typedef struct {
325         guint8 *address;
326         guint8 saved_byte;
327 } MonoBreakpointInfo;
328
329 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
330
331 guint8*
332 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
333
334 #endif /* __MONO_MINI_X86_H__ */  
335