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