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