2008-12-08 Atsushi Enomoto <atsushi@ximian.com>
[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 void* MonoCompileArch;
140
141 #if defined(__FreeBSD__) || defined(__APPLE__)
142 #include <ucontext.h>
143 #endif 
144
145 #if defined(__FreeBSD__)
146         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.mc_eax)
147         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.mc_ebx)
148         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.mc_ecx)
149         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.mc_edx)
150         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.mc_ebp)
151         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.mc_esp)
152         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.mc_esi)
153         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.mc_edi)
154         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.mc_eip)
155 #elif defined(__APPLE__) && defined(_STRUCT_MCONTEXT)
156         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext->__ss.__eax)
157         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext->__ss.__ebx)
158         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext->__ss.__ecx)
159         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext->__ss.__edx)
160         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext->__ss.__ebp)
161         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext->__ss.__esp)
162         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext->__ss.__esi)
163         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext->__ss.__edi)
164         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext->__ss.__eip)
165 #elif defined(__APPLE__) && !defined(_STRUCT_MCONTEXT)
166         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext->ss.eax)
167         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext->ss.ebx)
168         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext->ss.ecx)
169         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext->ss.edx)
170         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext->ss.ebp)
171         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext->ss.esp)
172         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext->ss.esi)
173         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext->ss.edi)
174         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext->ss.eip)
175 #elif defined(__NetBSD__)
176         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EAX])
177         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EBX])
178         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ECX])
179         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EDX])
180         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EBP])
181         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ESP])
182         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.__gregs [_REG_ESI])
183         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EDI])
184         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.__gregs [_REG_EIP])
185 #else
186         #define UCONTEXT_REG_EAX(ctx) ((ctx)->uc_mcontext.gregs [REG_EAX])
187         #define UCONTEXT_REG_EBX(ctx) ((ctx)->uc_mcontext.gregs [REG_EBX])
188         #define UCONTEXT_REG_ECX(ctx) ((ctx)->uc_mcontext.gregs [REG_ECX])
189         #define UCONTEXT_REG_EDX(ctx) ((ctx)->uc_mcontext.gregs [REG_EDX])
190         #define UCONTEXT_REG_EBP(ctx) ((ctx)->uc_mcontext.gregs [REG_EBP])
191         #define UCONTEXT_REG_ESP(ctx) ((ctx)->uc_mcontext.gregs [REG_ESP])
192         #define UCONTEXT_REG_ESI(ctx) ((ctx)->uc_mcontext.gregs [REG_ESI])
193         #define UCONTEXT_REG_EDI(ctx) ((ctx)->uc_mcontext.gregs [REG_EDI])
194         #define UCONTEXT_REG_EIP(ctx) ((ctx)->uc_mcontext.gregs [REG_EIP])
195 #endif
196
197 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
198 # define SC_EAX sc_eax
199 # define SC_EBX sc_ebx
200 # define SC_ECX sc_ecx
201 # define SC_EDX sc_edx
202 # define SC_EBP sc_ebp
203 # define SC_EIP sc_eip
204 # define SC_ESP sc_esp
205 # define SC_EDI sc_edi
206 # define SC_ESI sc_esi
207 #else
208 # define SC_EAX eax
209 # define SC_EBX ebx
210 # define SC_ECX ecx
211 # define SC_EDX edx
212 # define SC_EBP ebp
213 # define SC_EIP eip
214 # define SC_ESP esp
215 # define SC_EDI edi
216 # define SC_ESI esi
217 #endif
218
219 typedef struct {
220         guint32 eax;
221         guint32 ebx;
222         guint32 ecx;
223         guint32 edx;
224         guint32 ebp;
225         guint32 esp;
226     guint32 esi;
227         guint32 edi;
228         guint32 eip;
229 } MonoContext;
230
231 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (long)(ip); } while (0); 
232 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (long)(bp); } while (0); 
233 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (long)(sp); } while (0); 
234
235 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
236 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
237 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
238
239 #ifdef _MSC_VER
240
241 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
242     unsigned int stackptr; \
243         mono_arch_flush_register_windows (); \
244     { \
245            __asm mov stackptr, ebp \
246     } \
247         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
248         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
249         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
250 } while (0)
251
252 #else
253
254 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
255                 mono_arch_flush_register_windows ();    \
256                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
257                 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0));       \
258                 MONO_CONTEXT_SET_SP ((ctx), __builtin_frame_address (0));       \
259         } while (0)
260
261 #endif
262
263 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
264 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
265
266 #define MONO_ARCH_BIGMUL_INTRINS 1
267 #define MONO_ARCH_NEED_DIV_CHECK 1
268 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
269 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
270 #define MONO_ARCH_NEED_GOT_VAR 1
271 #define MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION 1
272 #define MONO_ARCH_ENABLE_EMIT_STATE_OPT 1
273 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
274 #define MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN 1
275 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
276 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
277 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
278 #define MONO_ARCH_HAVE_ATOMIC_CAS_IMM 1
279 #define MONO_ARCH_HAVE_IMT 1
280 #define MONO_ARCH_HAVE_TLS_GET 1
281 #define MONO_ARCH_IMT_REG X86_EDX
282 #define MONO_ARCH_VTABLE_REG X86_EDX
283 #define MONO_ARCH_COMMON_VTABLE_TRAMPOLINE 1
284 #define MONO_ARCH_RGCTX_REG X86_EDX
285 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
286 #if defined(__linux__) && !defined(HAVE_MOVING_COLLECTOR)
287 #define MONO_ARCH_MONITOR_OBJECT_REG X86_EAX
288 #endif
289
290 #define MONO_ARCH_HAVE_CMOV_OPS 1
291
292 #ifdef MONO_ARCH_SIMD_INTRINSICS
293 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
294 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
295 #endif
296
297 #if !defined(__APPLE__)
298 #define MONO_ARCH_AOT_SUPPORTED 1
299 #endif
300
301 #if defined(__linux__) || defined(__sun)
302 #define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
303 #endif
304
305 /* Used for optimization, not complete */
306 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
307
308 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
309             MonoInst *inst; \
310             MONO_INST_NEW ((cfg), inst, OP_X86_COMPARE_MEMBASE_REG); \
311             inst->inst_basereg = array_reg; \
312             inst->inst_offset = offset; \
313             inst->sreg2 = index_reg; \
314             MONO_ADD_INS ((cfg)->cbb, inst); \
315                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
316         } while (0)
317
318 typedef struct {
319         guint8 *address;
320         guint8 saved_byte;
321 } MonoBreakpointInfo;
322
323 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
324
325 guint8*
326 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
327
328 #endif /* __MONO_MINI_X86_H__ */  
329