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