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