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