New tests.
[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 HOST_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 _dummy, EXCEPTION_RECORD *info, void *context);
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 /* HOST_WIN32 */
46
47 #ifdef __HAIKU__
48 struct sigcontext {
49         vregs regs;
50 };
51 #endif /* __HAIKU__ */
52
53 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
54        defined(__FreeBSD__) || defined(__OpenBSD__)
55 #define MONO_ARCH_USE_SIGACTION
56 #endif
57
58 #ifndef HOST_WIN32
59
60 #ifdef HAVE_WORKING_SIGALTSTACK
61 /* 
62  * solaris doesn't have pthread_getattr_np () needed by the sigaltstack setup
63  * code.
64  */
65 #ifndef __sun
66 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
67 #endif
68 /* Haiku doesn't have SA_SIGINFO */
69 #ifndef __HAIKU__
70 #define MONO_ARCH_USE_SIGACTION
71 #endif /* __HAIKU__ */
72
73 #endif /* HAVE_WORKING_SIGALTSTACK */
74 #endif /* !HOST_WIN32 */
75
76 #define MONO_ARCH_SUPPORT_SIMD_INTRINSICS 1
77 #define MONO_ARCH_SUPPORT_TASKLETS 1
78
79 #ifndef DISABLE_SIMD
80 #define MONO_ARCH_SIMD_INTRINSICS 1
81 #define MONO_ARCH_NEED_SIMD_BANK 1
82 #endif
83
84 /* we should lower this size and make sure we don't call heavy stack users in the segv handler */
85 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
86 #define MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT 1
87
88 #define MONO_ARCH_CPU_SPEC x86_desc
89
90 #define MONO_MAX_IREGS 8
91 #define MONO_MAX_FREGS 8
92 #define MONO_MAX_XREGS 8
93
94 /* Parameters used by the register allocator */
95 #define MONO_ARCH_CALLEE_REGS X86_CALLEE_REGS
96 #define MONO_ARCH_CALLEE_SAVED_REGS X86_CALLER_REGS
97
98 #define MONO_ARCH_CALLEE_FREGS (0xff & ~(regmask (MONO_ARCH_FPSTACK_SIZE)))
99 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
100
101 /* All registers are clobered by a call */
102 #define MONO_ARCH_CALLEE_XREGS (0xff & ~(regmask (MONO_MAX_XREGS)))
103 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
104
105 #define MONO_ARCH_USE_FPSTACK TRUE
106 #define MONO_ARCH_FPSTACK_SIZE 6
107
108 #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)))))
109
110 #define MONO_ARCH_INST_FIXED_MASK(desc) ((desc == 'y') ? (X86_BYTE_REGS) : 0)
111
112 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
113 /* 
114  * Originally this contained X86_EDX for div/rem opcodes, but that led to unsolvable 
115  * situations since there are only 3 usable registers for local register allocation.
116  * Instead, we handle the sreg2==edx case in the opcodes.
117  */
118 #define MONO_ARCH_INST_SREG2_MASK(ins) 0
119
120 /*
121  * L is a generic register pair, while l means eax:rdx
122  */
123 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l' || desc == 'L')
124 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? X86_EDX : -1)
125
126 /* must be at a power of 2 and >= 8 */
127 #define MONO_ARCH_FRAME_ALIGNMENT 16
128
129 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
130  * reproduceable results for benchmarks */
131 #define MONO_ARCH_CODE_ALIGNMENT 32
132
133 #define MONO_ARCH_RETREG1 X86_EAX
134 #define MONO_ARCH_RETREG2 X86_EDX
135
136 /*This is the max size of the locals area of a given frame. I think 1MB is a safe default for now*/
137 #define MONO_ARCH_MAX_FRAME_SIZE 0x100000
138
139 struct MonoLMF {
140         /* 
141          * If the lowest bit is set to 1, then this is a trampoline LMF frame.
142          * If the second lowest bit is set to 1, then this is a MonoLMFExt structure, and
143          * the other fields are not valid.
144          */
145         guint32    previous_lmf;
146         gpointer    lmf_addr;
147         /* Only set in trampoline LMF frames */
148         MonoMethod *method;
149         /* Only set in trampoline LMF frames */
150         guint32     esp;
151         guint32     ebx;
152         guint32     edi;
153         guint32     esi;
154         guint32     ebp;
155         guint32     eip;
156 };
157
158 typedef struct {
159         gboolean need_stack_frame_inited;
160         gboolean need_stack_frame;
161 } MonoCompileArch;
162
163 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
164 # define SC_EAX sc_eax
165 # define SC_EBX sc_ebx
166 # define SC_ECX sc_ecx
167 # define SC_EDX sc_edx
168 # define SC_EBP sc_ebp
169 # define SC_EIP sc_eip
170 # define SC_ESP sc_esp
171 # define SC_EDI sc_edi
172 # define SC_ESI sc_esi
173 #elif defined(__HAIKU__)
174 # define SC_EAX regs.eax
175 # define SC_EBX regs._reserved_2[2]
176 # define SC_ECX regs.ecx
177 # define SC_EDX regs.edx
178 # define SC_EBP regs.ebp
179 # define SC_EIP regs.eip
180 # define SC_ESP regs.esp
181 # define SC_EDI regs._reserved_2[0]
182 # define SC_ESI regs._reserved_2[1]
183 #else
184 # define SC_EAX eax
185 # define SC_EBX ebx
186 # define SC_ECX ecx
187 # define SC_EDX edx
188 # define SC_EBP ebp
189 # define SC_EIP eip
190 # define SC_ESP esp
191 # define SC_EDI edi
192 # define SC_ESI esi
193 #endif
194
195 typedef struct {
196         guint32 eax;
197         guint32 ebx;
198         guint32 ecx;
199         guint32 edx;
200         guint32 ebp;
201         guint32 esp;
202     guint32 esi;
203         guint32 edi;
204         guint32 eip;
205 } MonoContext;
206
207 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (long)(ip); } while (0); 
208 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (long)(bp); } while (0); 
209 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (long)(sp); } while (0); 
210
211 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
212 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
213 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
214
215 #define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->eax = (gsize)exc; } while (0)
216
217 #ifdef _MSC_VER
218
219 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
220     unsigned int stackptr; \
221         mono_arch_flush_register_windows (); \
222     { \
223            __asm mov stackptr, ebp \
224     } \
225         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
226         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
227         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
228 } while (0)
229
230 #else
231
232 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
233                 mono_arch_flush_register_windows ();    \
234                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
235                 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0));       \
236                 MONO_CONTEXT_SET_SP ((ctx), __builtin_frame_address (0));       \
237         } while (0)
238
239 #endif
240
241 /*
242  * This structure is an extension of MonoLMF and contains extra information.
243  */
244 typedef struct {
245         struct MonoLMF lmf;
246         gboolean debugger_invoke;
247         MonoContext ctx; /* if debugger_invoke is TRUE */
248 } MonoLMFExt;
249
250 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
251 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
252
253 #define MONO_ARCH_BIGMUL_INTRINS 1
254 #define MONO_ARCH_NEED_DIV_CHECK 1
255 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
256 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
257 #define MONO_ARCH_NEED_GOT_VAR 1
258 #define MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION 1
259 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
260 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
261 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
262 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
263 #define MONO_ARCH_HAVE_ATOMIC_CAS 1
264 #define MONO_ARCH_HAVE_IMT 1
265 #define MONO_ARCH_HAVE_TLS_GET (mono_x86_have_tls_get ())
266 #define MONO_ARCH_IMT_REG X86_EDX
267 #define MONO_ARCH_VTABLE_REG X86_EDX
268 #define MONO_ARCH_RGCTX_REG X86_EDX
269 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
270 #define MONO_ARCH_HAVE_LIVERANGE_OPS 1
271 #define MONO_ARCH_HAVE_XP_UNWIND 1
272 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
273 #if defined(__linux__) && !defined(HAVE_MOVING_COLLECTOR)
274 #define MONO_ARCH_MONITOR_OBJECT_REG X86_EAX
275 #endif
276 #define MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE 1
277
278 #define MONO_ARCH_HAVE_CMOV_OPS 1
279
280 #ifdef MONO_ARCH_SIMD_INTRINSICS
281 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
282 #endif
283
284 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
285
286 #if !defined(__APPLE__)
287 #define MONO_ARCH_AOT_SUPPORTED 1
288 #endif
289
290 #if defined(__linux__) || defined(__sun)
291 #define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
292 #endif
293
294 #define MONO_ARCH_GSHARED_SUPPORTED 1
295 #define MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE 1
296 #define MONO_ARCH_LLVM_SUPPORTED 1
297
298 #if defined(MONO_ARCH_USE_SIGACTION) || defined(TARGET_WIN32)
299 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
300 #endif
301
302 #define MONO_ARCH_HAVE_FIND_JIT_INFO_EXT 1
303 #define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
304 #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
305
306 /* Used for optimization, not complete */
307 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
308
309 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
310             MonoInst *inst; \
311             MONO_INST_NEW ((cfg), inst, OP_X86_COMPARE_MEMBASE_REG); \
312             inst->inst_basereg = array_reg; \
313             inst->inst_offset = offset; \
314             inst->sreg2 = index_reg; \
315             MONO_ADD_INS ((cfg)->cbb, inst); \
316                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
317         } while (0)
318
319 typedef struct {
320         guint8 *address;
321         guint8 saved_byte;
322 } MonoBreakpointInfo;
323
324 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
325
326 guint8*
327 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
328
329 guint32
330 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig) MONO_INTERNAL;
331
332 gboolean
333 mono_x86_have_tls_get (void) MONO_INTERNAL;
334
335 #endif /* __MONO_MINI_X86_H__ */  
336