Merge remote branch 'upstream/master'
[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 /*This is how much a try block must be extended when is is preceeded by a Monitor.Enter() call.
152 It's 4 bytes as this is how many bytes + 1 that 'add 0x10, %esp' takes. It is used to pop the arguments from
153 the monitor.enter call and must be already protected.*/
154 #define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT 4
155
156 struct MonoLMF {
157         /* 
158          * If the lowest bit is set to 1, then this is a trampoline LMF frame.
159          * If the second lowest bit is set to 1, then this is a MonoLMFExt structure, and
160          * the other fields are not valid.
161          */
162         guint32    previous_lmf;
163         gpointer    lmf_addr;
164         /* Only set in trampoline LMF frames */
165         MonoMethod *method;
166         /* Only set in trampoline LMF frames */
167         guint32     esp;
168         guint32     ebx;
169         guint32     edi;
170         guint32     esi;
171         guint32     ebp;
172         guint32     eip;
173 };
174
175 typedef struct {
176         gboolean need_stack_frame_inited;
177         gboolean need_stack_frame;
178 } MonoCompileArch;
179
180 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
181 # define SC_EAX sc_eax
182 # define SC_EBX sc_ebx
183 # define SC_ECX sc_ecx
184 # define SC_EDX sc_edx
185 # define SC_EBP sc_ebp
186 # define SC_EIP sc_eip
187 # define SC_ESP sc_esp
188 # define SC_EDI sc_edi
189 # define SC_ESI sc_esi
190 #elif defined(__HAIKU__)
191 # define SC_EAX regs.eax
192 # define SC_EBX regs._reserved_2[2]
193 # define SC_ECX regs.ecx
194 # define SC_EDX regs.edx
195 # define SC_EBP regs.ebp
196 # define SC_EIP regs.eip
197 # define SC_ESP regs.esp
198 # define SC_EDI regs._reserved_2[0]
199 # define SC_ESI regs._reserved_2[1]
200 #else
201 # define SC_EAX eax
202 # define SC_EBX ebx
203 # define SC_ECX ecx
204 # define SC_EDX edx
205 # define SC_EBP ebp
206 # define SC_EIP eip
207 # define SC_ESP esp
208 # define SC_EDI edi
209 # define SC_ESI esi
210 #endif
211
212 typedef struct {
213         guint32 eax;
214         guint32 ebx;
215         guint32 ecx;
216         guint32 edx;
217         guint32 ebp;
218         guint32 esp;
219     guint32 esi;
220         guint32 edi;
221         guint32 eip;
222 } MonoContext;
223
224 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (long)(ip); } while (0); 
225 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (long)(bp); } while (0); 
226 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (long)(sp); } while (0); 
227
228 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
229 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
230 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
231
232 #define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->eax = (gsize)exc; } while (0)
233
234 #ifdef _MSC_VER
235
236 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
237     unsigned int stackptr; \
238         mono_arch_flush_register_windows (); \
239     { \
240            __asm mov stackptr, ebp \
241     } \
242         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
243         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
244         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
245 } while (0)
246
247 #else
248
249 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
250                 mono_arch_flush_register_windows ();    \
251                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
252                 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0));       \
253                 MONO_CONTEXT_SET_SP ((ctx), __builtin_frame_address (0));       \
254         } while (0)
255
256 #endif
257
258 #define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf) do { (lmf)->ebp = -1; } while (0)
259
260 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
261 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
262
263 #define MONO_ARCH_BIGMUL_INTRINS 1
264 #define MONO_ARCH_NEED_DIV_CHECK 1
265 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
266 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
267 #define MONO_ARCH_NEED_GOT_VAR 1
268 #if !defined(__APPLE__)
269 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
270 #endif
271 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
272 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
273 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
274 #define MONO_ARCH_HAVE_ATOMIC_CAS 1
275 #define MONO_ARCH_HAVE_IMT 1
276 #define MONO_ARCH_HAVE_TLS_GET (mono_x86_have_tls_get ())
277 #define MONO_ARCH_IMT_REG X86_EDX
278 #define MONO_ARCH_VTABLE_REG X86_EDX
279 #define MONO_ARCH_RGCTX_REG X86_EDX
280 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
281 #define MONO_ARCH_HAVE_LIVERANGE_OPS 1
282 #define MONO_ARCH_HAVE_XP_UNWIND 1
283 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
284 #if defined(__linux__)
285 #define MONO_ARCH_MONITOR_OBJECT_REG X86_EAX
286 #endif
287 #define MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE 1
288 #if !defined (__APPLE__) || defined(__native_client_codegen__)
289 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
290 #endif
291 #define MONO_ARCH_GOT_REG X86_EBX
292 #define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
293
294 #define MONO_ARCH_HAVE_CMOV_OPS 1
295
296 #ifdef MONO_ARCH_SIMD_INTRINSICS
297 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
298 #endif
299
300 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
301
302 #define MONO_ARCH_AOT_SUPPORTED 1
303
304 #if defined(__linux__) || defined(__sun)
305 #define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
306 #endif
307
308 #define MONO_ARCH_GSHARED_SUPPORTED 1
309 #define MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE 1
310 #define MONO_ARCH_LLVM_SUPPORTED 1
311 #define MONO_ARCH_THIS_AS_FIRST_ARG 1
312
313 #if defined(MONO_ARCH_USE_SIGACTION) || defined(TARGET_WIN32)
314 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
315 #endif
316
317 #define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
318 #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
319
320 #define MONO_ARCH_HAVE_CARD_TABLE_WBARRIER 1
321 #define MONO_ARCH_GC_MAPS_SUPPORTED 1
322
323 gboolean
324 mono_x86_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig) MONO_INTERNAL;
325
326 #define MONO_ARCH_USE_OP_TAIL_CALL(caller_sig, callee_sig) mono_x86_tail_call_supported (caller_sig, callee_sig)
327
328 /* Used for optimization, not complete */
329 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
330
331 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
332             MonoInst *inst; \
333             MONO_INST_NEW ((cfg), inst, OP_X86_COMPARE_MEMBASE_REG); \
334             inst->inst_basereg = array_reg; \
335             inst->inst_offset = offset; \
336             inst->sreg2 = index_reg; \
337             MONO_ADD_INS ((cfg)->cbb, inst); \
338                         MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
339         } while (0)
340
341 typedef struct {
342         guint8 *address;
343         guint8 saved_byte;
344 } MonoBreakpointInfo;
345
346 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
347
348 guint8*
349 mono_x86_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
350
351 guint32
352 mono_x86_get_this_arg_offset (MonoGenericSharingContext *gsctx, MonoMethodSignature *sig) MONO_INTERNAL;
353
354 gboolean
355 mono_x86_have_tls_get (void) MONO_INTERNAL;
356
357 void
358 mono_x86_throw_exception (mgreg_t *regs, MonoObject *exc, 
359                                                   mgreg_t eip, gboolean rethrow) MONO_INTERNAL;
360
361 void
362 mono_x86_throw_corlib_exception (mgreg_t *regs, guint32 ex_token_index, 
363                                                                  mgreg_t eip, gint32 pc_offset) MONO_INTERNAL;
364
365 void 
366 mono_x86_patch (unsigned char* code, gpointer target) MONO_INTERNAL;
367
368 #endif /* __MONO_MINI_X86_H__ */  
369