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