[runtime] Transition the trampoline code to use memory managers for memory allocation...
[mono-project.git] / mono / mini / mini-x86.h
blob6d44cb5f2bf187c99f8e4d519d15c93548eaf94c
1 /**
2 * \file
3 */
5 #ifndef __MONO_MINI_X86_H__
6 #define __MONO_MINI_X86_H__
8 #include <mono/arch/x86/x86-codegen.h>
9 #include <mono/utils/mono-sigcontext.h>
10 #include <mono/utils/mono-context.h>
12 #ifdef HOST_WIN32
13 #include <windows.h>
14 #include <signal.h>
16 typedef void MONO_SIG_HANDLER_SIGNATURE ((*MonoW32ExceptionHandler));
18 void win32_seh_init(void);
19 void win32_seh_cleanup(void);
20 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
22 #ifndef SIGFPE
23 #define SIGFPE 4
24 #endif
26 #ifndef SIGILL
27 #define SIGILL 8
28 #endif
30 #ifndef SIGSEGV
31 #define SIGSEGV 11
32 #endif
34 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
36 #endif /* HOST_WIN32 */
38 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
39 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__)
40 #define MONO_ARCH_USE_SIGACTION
41 #endif
43 #if defined(HOST_WATCHOS)
44 #undef MONO_ARCH_USE_SIGACTION
45 #endif
47 #ifndef HOST_WIN32
49 #ifdef HAVE_WORKING_SIGALTSTACK
50 /*
51 * solaris doesn't have pthread_getattr_np () needed by the sigaltstack setup
52 * code.
54 #ifndef __sun
55 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
56 #endif
57 /* Haiku doesn't have SA_SIGINFO */
58 #ifndef __HAIKU__
59 #define MONO_ARCH_USE_SIGACTION
60 #endif /* __HAIKU__ */
62 #endif /* HAVE_WORKING_SIGALTSTACK */
63 #endif /* !HOST_WIN32 */
65 #define MONO_ARCH_SUPPORT_TASKLETS 1
67 #ifndef DISABLE_SIMD
68 #ifndef ENABLE_NETCORE
69 #define MONO_ARCH_SIMD_INTRINSICS 1
70 #define MONO_ARCH_NEED_SIMD_BANK 1
71 #endif
72 #endif
74 /* we should lower this size and make sure we don't call heavy stack users in the segv handler */
75 #if defined(__APPLE__)
76 #define MONO_ARCH_SIGNAL_STACK_SIZE MINSIGSTKSZ
77 #else
78 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
79 #endif
81 #define MONO_ARCH_CPU_SPEC mono_x86_desc
83 #define MONO_MAX_IREGS 8
84 #define MONO_MAX_FREGS 8
85 #define MONO_MAX_XREGS 8
87 /* Parameters used by the register allocator */
88 #define MONO_ARCH_CALLEE_REGS X86_CALLEE_REGS
89 #define MONO_ARCH_CALLEE_SAVED_REGS X86_CALLER_REGS
91 #define MONO_ARCH_CALLEE_FREGS (0xff & ~(regmask (MONO_ARCH_FPSTACK_SIZE)))
92 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
94 /* All registers are clobered by a call */
95 #define MONO_ARCH_CALLEE_XREGS (0xff & ~(regmask (MONO_MAX_XREGS)))
96 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
98 #define MONO_ARCH_USE_FPSTACK TRUE
99 #define MONO_ARCH_FPSTACK_SIZE 6
101 #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)))))
103 #define MONO_ARCH_INST_FIXED_MASK(desc) ((desc == 'y') ? (X86_BYTE_REGS) : 0)
105 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
107 * Originally this contained X86_EDX for div/rem opcodes, but that led to unsolvable
108 * situations since there are only 3 usable registers for local register allocation.
109 * Instead, we handle the sreg2==edx case in the opcodes.
111 #define MONO_ARCH_INST_SREG2_MASK(ins) 0
114 * L is a generic register pair, while l means eax:rdx
116 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l' || desc == 'L')
117 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? X86_EDX : -1)
119 /* must be at a power of 2 and >= 8 */
120 #define MONO_ARCH_FRAME_ALIGNMENT 16
122 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get
123 * reproduceable results for benchmarks */
124 #define MONO_ARCH_CODE_ALIGNMENT 32
126 /*This is the max size of the locals area of a given frame. I think 1MB is a safe default for now*/
127 #define MONO_ARCH_MAX_FRAME_SIZE 0x100000
129 /*This is how much a try block must be extended when is is preceeded by a Monitor.Enter() call.
130 It's 4 bytes as this is how many bytes + 1 that 'add 0x10, %esp' takes. It is used to pop the arguments from
131 the monitor.enter call and must be already protected.*/
132 #define MONO_ARCH_MONITOR_ENTER_ADJUSTMENT 4
134 struct MonoLMF {
136 * If the lowest bit is set to 1, then this is a trampoline LMF frame.
137 * If the second lowest bit is set to 1, then this is a MonoLMFExt structure, and
138 * the other fields are not valid.
140 gpointer previous_lmf;
141 gpointer lmf_addr;
142 /* Only set in trampoline LMF frames */
143 MonoMethod *method;
144 /* Only set in trampoline LMF frames */
145 guint32 esp;
146 guint32 ebx;
147 guint32 edi;
148 guint32 esi;
149 guint32 ebp;
150 guint32 eip;
153 typedef struct {
154 gboolean need_stack_frame_inited;
155 gboolean need_stack_frame;
156 int sp_fp_offset, param_area_size;
157 CallInfo *cinfo;
158 MonoInst *ss_tramp_var;
159 MonoInst *bp_tramp_var;
160 } MonoCompileArch;
162 #define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->eax = (gsize)exc; } while (0)
164 #ifdef _MSC_VER
166 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
167 unsigned int stackptr; \
169 __asm mov stackptr, ebp \
171 MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
172 MONO_CONTEXT_SET_BP ((ctx), stackptr); \
173 MONO_CONTEXT_SET_SP ((ctx), stackptr); \
174 } while (0)
176 #else
178 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do { \
179 MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
180 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0)); \
181 MONO_CONTEXT_SET_SP ((ctx), __builtin_frame_address (0)); \
182 } while (0)
184 #endif
186 #define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf) do { (lmf)->ebp = -1; } while (0)
188 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
189 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
191 #define MONO_ARCH_EMULATE_FCONV_TO_U8 1
192 #define MONO_ARCH_EMULATE_FCONV_TO_U4 1
194 #define MONO_ARCH_NEED_DIV_CHECK 1
195 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
196 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
197 #define MONO_ARCH_NEED_GOT_VAR 1
198 #define MONO_ARCH_IMT_REG X86_EDX
199 #define MONO_ARCH_VTABLE_REG X86_EDX
200 #define MONO_ARCH_RGCTX_REG MONO_ARCH_IMT_REG
201 #define MONO_ARCH_HAVE_GENERALIZED_IMT_TRAMPOLINE 1
202 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
203 #define MONO_ARCH_GOT_REG X86_EBX
204 #define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
205 #define MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE 1
207 #define MONO_ARCH_INTERPRETER_SUPPORTED 1
208 #define MONO_ARCH_HAVE_INTERP_NATIVE_TO_MANAGED 1
209 #define MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP 1
211 #define MONO_ARCH_HAVE_CMOV_OPS 1
213 #ifdef MONO_ARCH_SIMD_INTRINSICS
214 #define MONO_ARCH_HAVE_DECOMPOSE_OPTS 1
215 #endif
217 #define MONO_ARCH_HAVE_DECOMPOSE_LONG_OPTS 1
219 #define MONO_ARCH_AOT_SUPPORTED 1
221 #define MONO_ARCH_GSHARED_SUPPORTED 1
223 #define MONO_ARCH_LLVM_SUPPORTED 1
224 #if defined(HOST_WIN32) && defined(TARGET_WIN32)
225 // Only supported for Windows cross compiler builds, host == Win32, target != Win32.
226 #undef MONO_ARCH_LLVM_SUPPORTED
227 #endif
229 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
231 #define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
233 #define MONO_ARCH_HAVE_CARD_TABLE_WBARRIER 1
234 #define MONO_ARCH_HAVE_SETUP_RESUME_FROM_SIGNAL_HANDLER_CTX 1
235 #define MONO_ARCH_GC_MAPS_SUPPORTED 1
236 #define MONO_ARCH_HAVE_CONTEXT_SET_INT_REG 1
237 #define MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK 1
238 #define MONO_ARCH_GSHAREDVT_SUPPORTED 1
239 #define MONO_ARCH_HAVE_OP_TAILCALL_MEMBASE 1
240 #define MONO_ARCH_HAVE_OP_TAILCALL_REG 1
241 #define MONO_ARCH_HAVE_SDB_TRAMPOLINES 1
242 #define MONO_ARCH_HAVE_PATCH_CODE_NEW 1
243 #define MONO_ARCH_LLVM_TARGET_LAYOUT "e-p:32:32-n32-S128"
245 /* Used for optimization, not complete */
246 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
248 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg, ex_name) do { \
249 MonoInst *inst; \
250 MONO_INST_NEW ((cfg), inst, OP_X86_COMPARE_MEMBASE_REG); \
251 inst->inst_basereg = array_reg; \
252 inst->inst_offset = offset; \
253 inst->sreg2 = index_reg; \
254 MONO_ADD_INS ((cfg)->cbb, inst); \
255 MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, ex_name); \
256 } while (0)
258 // Does the ABI have a volatile non-parameter register, so tailcall
259 // can pass context to generics or interfaces?
260 #define MONO_ARCH_HAVE_VOLATILE_NON_PARAM_REGISTER 1
262 /* Return value marshalling for calls between gsharedvt and normal code */
263 typedef enum {
264 GSHAREDVT_RET_NONE = 0,
265 GSHAREDVT_RET_IREGS = 1,
266 GSHAREDVT_RET_DOUBLE_FPSTACK = 2,
267 GSHAREDVT_RET_FLOAT_FPSTACK = 3,
268 GSHAREDVT_RET_STACK_POP = 4,
269 GSHAREDVT_RET_I1 = 5,
270 GSHAREDVT_RET_U1 = 6,
271 GSHAREDVT_RET_I2 = 7,
272 GSHAREDVT_RET_U2 = 8,
273 GSHAREDVT_RET_IREG = 9
274 } GSharedVtRetMarshal;
276 typedef struct {
277 /* Method address to call */
278 gpointer addr;
279 /* The trampoline reads this, so keep the size explicit */
280 int ret_marshal;
281 /* If ret_marshal != NONE, this is the stack slot of the vret arg, else -1 */
282 int vret_arg_slot;
283 /* The stack slot where the return value will be stored */
284 int vret_slot;
285 int stack_usage, map_count;
286 /* If not -1, then make a virtual call using this vtable offset */
287 int vcall_offset;
288 /* If 1, make an indirect call to the address in the rgctx reg */
289 int calli;
290 /* Whenever this is a in or an out call */
291 int gsharedvt_in;
292 int map [MONO_ZERO_LEN_ARRAY];
293 } GSharedVtCallInfo;
295 typedef enum {
296 ArgInIReg,
297 ArgInFloatSSEReg,
298 ArgInDoubleSSEReg,
299 ArgOnStack,
300 ArgValuetypeInReg,
301 ArgOnFloatFpStack,
302 ArgOnDoubleFpStack,
303 /* gsharedvt argument passed by addr */
304 ArgGSharedVt,
305 ArgNone
306 } ArgStorage;
308 typedef struct {
309 gint16 offset;
310 gint8 reg;
311 ArgStorage storage;
312 int nslots;
313 gboolean is_pair;
315 /* Only if storage == ArgValuetypeInReg */
316 ArgStorage pair_storage [2];
317 gint8 pair_regs [2];
318 guint8 pass_empty_struct : 1; // Set in scenarios when empty structs needs to be represented as argument.
319 } ArgInfo;
321 struct CallInfo {
322 int nargs;
323 guint32 stack_usage;
324 guint32 reg_usage;
325 guint32 freg_usage;
326 gboolean need_stack_align;
327 guint32 stack_align_amount;
328 gboolean vtype_retaddr;
329 /* The index of the vret arg in the argument list */
330 int vret_arg_index;
331 int vret_arg_offset;
332 /* Argument space popped by the callee */
333 int callee_stack_pop;
334 ArgInfo ret;
335 ArgInfo sig_cookie;
336 ArgInfo args [1];
339 typedef struct {
340 /* EAX:EDX */
341 host_mgreg_t eax;
342 host_mgreg_t edx;
343 /* Floating point return value read from the top of x86 fpstack */
344 double fret;
345 /* Stack usage, used for passing params on stack */
346 guint32 stack_size;
347 guint8 *stack;
348 } CallContext;
350 guint32
351 mono_x86_get_this_arg_offset (MonoMethodSignature *sig);
353 void
354 mono_x86_throw_exception (host_mgreg_t *regs, MonoObject *exc,
355 host_mgreg_t eip, gboolean rethrow, gboolean preserve_ips);
357 void
358 mono_x86_throw_corlib_exception (host_mgreg_t *regs, guint32 ex_token_index,
359 host_mgreg_t eip, gint32 pc_offset);
361 void
362 mono_x86_patch (unsigned char* code, gpointer target);
364 gpointer
365 mono_x86_start_gsharedvt_call (GSharedVtCallInfo *info, gpointer *caller, gpointer *callee, gpointer mrgctx_reg);
367 CallInfo*
368 mono_arch_get_call_info (MonoMemPool *mp, MonoMethodSignature *sig);
370 #endif /* __MONO_MINI_X86_H__ */