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