Wrap always_inline and noinline attributes in compiler checks and use MSVC equivalent.
[mono.git] / mono / mini / mini-amd64.h
1 #ifndef __MONO_MINI_AMD64_H__
2 #define __MONO_MINI_AMD64_H__
3
4 #include <mono/arch/amd64/amd64-codegen.h>
5 #include <mono/utils/mono-sigcontext.h>
6 #include <mono/utils/mono-context.h>
7 #include <glib.h>
8
9 #ifdef __native_client_codegen__
10 #define kNaClAlignmentAMD64 32
11 #define kNaClAlignmentMaskAMD64 (kNaClAlignmentAMD64 - 1)
12
13 /* TODO: use kamd64NaClLengthOfCallImm    */
14 /* temporarily using kNaClAlignmentAMD64 so padding in */
15 /* image-writer.c doesn't happen                       */
16 #define kNaClLengthOfCallImm kNaClAlignmentAMD64
17
18 int is_nacl_call_reg_sequence(guint8* code);
19 #endif
20
21 #ifdef HOST_WIN32
22 #include <windows.h>
23 /* use SIG* defines if possible */
24 #ifdef HAVE_SIGNAL_H
25 #include <signal.h>
26 #endif
27
28
29 /* sigcontext surrogate */
30 struct sigcontext {
31         guint64 eax;
32         guint64 ebx;
33         guint64 ecx;
34         guint64 edx;
35         guint64 ebp;
36         guint64 esp;
37     guint64 esi;
38         guint64 edi;
39         guint64 eip;
40 };
41
42 typedef void (* MonoW32ExceptionHandler) (int _dummy, EXCEPTION_RECORD *info, void *context);
43 void win32_seh_init(void);
44 void win32_seh_cleanup(void);
45 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
46
47 #ifndef SIGFPE
48 #define SIGFPE 4
49 #endif
50
51 #ifndef SIGILL
52 #define SIGILL 8
53 #endif
54
55 #ifndef SIGSEGV
56 #define SIGSEGV 11
57 #endif
58
59 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
60
61 #endif /* HOST_WIN32 */
62
63 #ifdef sun    // Solaris x86
64 #  undef SIGSEGV_ON_ALTSTACK
65 #  define MONO_ARCH_NOMAP32BIT
66
67 struct sigcontext {
68         unsigned short gs, __gsh;
69         unsigned short fs, __fsh;
70         unsigned short es, __esh;
71         unsigned short ds, __dsh;
72         unsigned long edi;
73         unsigned long esi;
74         unsigned long ebp;
75         unsigned long esp;
76         unsigned long ebx;
77         unsigned long edx;
78         unsigned long ecx;
79         unsigned long eax;
80         unsigned long trapno;
81         unsigned long err;
82         unsigned long eip;
83         unsigned short cs, __csh;
84         unsigned long eflags;
85         unsigned long esp_at_signal;
86         unsigned short ss, __ssh;
87         unsigned long fpstate[95];
88       unsigned long filler[5];
89 };
90 #endif  // sun, Solaris x86
91
92 #define MONO_ARCH_SUPPORT_SIMD_INTRINSICS 1
93
94 #ifndef DISABLE_SIMD
95 #define MONO_ARCH_SIMD_INTRINSICS 1
96 #define MONO_ARCH_NEED_SIMD_BANK 1
97 #define MONO_ARCH_USE_SHARED_FP_SIMD_BANK 1
98 #endif
99
100
101
102 #if defined(__APPLE__)
103 #define MONO_ARCH_SIGNAL_STACK_SIZE MINSIGSTKSZ
104 #else
105 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
106 #endif
107
108 #define MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT 1
109
110 #define MONO_ARCH_CPU_SPEC amd64_desc
111
112 #define MONO_MAX_IREGS 16
113
114 #define MONO_MAX_FREGS AMD64_XMM_NREG
115
116 #define MONO_ARCH_FP_RETURN_REG AMD64_XMM0
117
118 /* xmm15 is reserved for use by some opcodes */
119 #define MONO_ARCH_CALLEE_FREGS 0x7fff
120 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
121
122 #define MONO_MAX_XREGS MONO_MAX_FREGS
123
124 #define MONO_ARCH_CALLEE_XREGS 0x7fff
125 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
126
127
128 #define MONO_ARCH_CALLEE_REGS AMD64_CALLEE_REGS
129 #define MONO_ARCH_CALLEE_SAVED_REGS AMD64_CALLEE_SAVED_REGS
130
131 #define MONO_ARCH_USE_FPSTACK FALSE
132 #define MONO_ARCH_FPSTACK_SIZE 0
133
134 #define MONO_ARCH_INST_FIXED_REG(desc) ((desc == '\0') ? -1 : ((desc == 'i' ? -1 : ((desc == 'a') ? AMD64_RAX : ((desc == 's') ? AMD64_RCX : ((desc == 'd') ? AMD64_RDX : -1))))))
135
136 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
137 #define MONO_ARCH_INST_SREG2_MASK(ins) (((ins [MONO_INST_CLOB] == 'a') || (ins [MONO_INST_CLOB] == 'd')) ? (1 << AMD64_RDX) : 0)
138
139 #define MONO_ARCH_INST_IS_REGPAIR(desc) FALSE
140 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (-1)
141
142 #define MONO_ARCH_FRAME_ALIGNMENT 16
143
144 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
145  * reproduceable results for benchmarks */
146 #define MONO_ARCH_CODE_ALIGNMENT 32
147
148 #define MONO_ARCH_RETREG1 X86_EAX
149 #define MONO_ARCH_RETREG2 X86_EDX
150
151 /*This is the max size of the locals area of a given frame. I think 1MB is a safe default for now*/
152 #define MONO_ARCH_MAX_FRAME_SIZE 0x100000
153
154 struct MonoLMF {
155         /* 
156          * If the lowest bit is set to 1, then this LMF has the rip field set. Otherwise,
157          * the rip field is not set, and the rsp field points to the stack location where
158          * the caller ip is saved.
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         gpointer    previous_lmf;
163         gpointer    lmf_addr;
164         /* This is only set in trampoline LMF frames */
165         MonoMethod *method;
166 #if defined(__default_codegen__) || defined(HOST_WIN32)
167         guint64     rip;
168 #elif defined(__native_client_codegen__)
169         /* On 64-bit compilers, default alignment is 8 for this field, */
170         /* this allows the structure to match for 32-bit compilers.    */
171         guint64     rip __attribute__ ((aligned(8)));
172 #endif
173         guint64     rbx;
174         guint64     rbp;
175         guint64     rsp;
176         guint64     r12;
177         guint64     r13;
178         guint64     r14;
179         guint64     r15;
180 #ifdef HOST_WIN32
181         guint64     rdi;
182         guint64     rsi;
183 #endif
184 };
185
186 typedef struct MonoCompileArch {
187         gint32 localloc_offset;
188         gint32 reg_save_area_offset;
189         gint32 stack_alloc_size;
190         gint32 sp_fp_offset;
191         gboolean omit_fp, omit_fp_computed, no_pushes;
192         gpointer cinfo;
193         gint32 async_point_count;
194         gpointer vret_addr_loc;
195 #ifdef HOST_WIN32
196         gpointer        unwindinfo;
197 #endif
198         gpointer seq_point_info_var;
199         gpointer ss_trigger_page_var;
200         gpointer lmf_var;
201 } MonoCompileArch;
202
203 #define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->rax = (gsize)exc; } while (0)
204
205 #define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf)
206
207 #ifdef _MSC_VER
208
209 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
210     guint64 stackptr; \
211         mono_arch_flush_register_windows (); \
212         stackptr = ((guint64)_AddressOfReturnAddress () - sizeof (void*));\
213         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
214         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
215         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
216 } while (0)
217
218 #else
219
220 /* 
221  * __builtin_frame_address () is broken on some older gcc versions in the presence of
222  * frame pointer elimination, see bug #82095.
223  */
224 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
225         int tmp; \
226         guint64 stackptr = (guint64)&tmp; \
227                 mono_arch_flush_register_windows ();    \
228                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
229                 MONO_CONTEXT_SET_BP ((ctx), stackptr);  \
230                 MONO_CONTEXT_SET_SP ((ctx), stackptr);  \
231         } while (0)
232
233 #endif
234
235 /*
236  * some icalls like mono_array_new_va needs to be called using a different 
237  * calling convention.
238  */
239 #define MONO_ARCH_VARARG_ICALLS 1
240
241 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
242
243 #define MONO_ARCH_USE_SIGACTION 1
244
245 #ifdef HAVE_WORKING_SIGALTSTACK
246
247 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
248
249 #endif
250
251 #endif /* !HOST_WIN32 && !__native_client__ */
252
253 #if defined (__APPLE__)
254
255 #define MONO_ARCH_NOMAP32BIT
256
257 #elif defined (__NetBSD__)
258
259 #define REG_RAX 14
260 #define REG_RCX 3
261 #define REG_RDX 2
262 #define REG_RBX 13
263 #define REG_RSP 24
264 #define REG_RBP 12
265 #define REG_RSI 1
266 #define REG_RDI 0
267 #define REG_R8 4
268 #define REG_R9 5
269 #define REG_R10 6
270 #define REG_R11 7
271 #define REG_R12 8
272 #define REG_R13 9
273 #define REG_R14 10
274 #define REG_R15 11
275 #define REG_RIP 21
276
277 #define MONO_ARCH_NOMAP32BIT
278
279 #elif defined (__OpenBSD__)
280
281 #define MONO_ARCH_NOMAP32BIT
282
283 #elif defined (__DragonFly__)
284
285 #define MONO_ARCH_NOMAP32BIT
286
287 #elif defined (__FreeBSD__)
288
289 #define REG_RAX 7
290 #define REG_RCX 4
291 #define REG_RDX 3
292 #define REG_RBX 8
293 #define REG_RSP 23
294 #define REG_RBP 9
295 #define REG_RSI 2
296 #define REG_RDI 1
297 #define REG_R8  5
298 #define REG_R9  6
299 #define REG_R10 10
300 #define REG_R11 11
301 #define REG_R12 12
302 #define REG_R13 13
303 #define REG_R14 14
304 #define REG_R15 15
305 #define REG_RIP 20
306
307 /* 
308  * FreeBSD does not have MAP_32BIT, so code allocated by the code manager might not have a
309  * 32 bit address.
310  */
311 #define MONO_ARCH_NOMAP32BIT
312
313 #endif /* __FreeBSD__ */
314
315 #ifdef HOST_WIN32
316 #define MONO_AMD64_ARG_REG1 AMD64_RCX
317 #define MONO_AMD64_ARG_REG2 AMD64_RDX
318 #else
319 #define MONO_AMD64_ARG_REG1 AMD64_RDI
320 #define MONO_AMD64_ARG_REG2 AMD64_RSI
321 #endif
322
323 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
324 #define MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
325
326 #define MONO_ARCH_EMULATE_CONV_R8_UN    1
327 #define MONO_ARCH_EMULATE_FREM 1
328 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
329
330 #define MONO_ARCH_ENABLE_REGALLOC_IN_EH_BLOCKS 1
331 #if !defined(__APPLE__)
332 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
333 #endif
334 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
335 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
336 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
337 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
338 #define MONO_ARCH_HAVE_ATOMIC_CAS 1
339 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
340 #define MONO_ARCH_HAVE_IMT 1
341 #define MONO_ARCH_HAVE_TLS_GET (mono_amd64_have_tls_get ())
342 #define MONO_ARCH_IMT_REG AMD64_R10
343 #define MONO_ARCH_IMT_SCRATCH_REG AMD64_R11
344 #define MONO_ARCH_VTABLE_REG MONO_AMD64_ARG_REG1
345 /*
346  * We use r10 for the imt/rgctx register rather than r11 because r11 is
347  * used by the trampoline as a scratch register and hence might be
348  * clobbered across method call boundaries.
349  */
350 #define MONO_ARCH_RGCTX_REG MONO_ARCH_IMT_REG
351 #define MONO_ARCH_HAVE_CMOV_OPS 1
352 #define MONO_ARCH_HAVE_NOTIFY_PENDING_EXC 1
353 #define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
354 #define MONO_ARCH_ENABLE_GLOBAL_RA 1
355 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
356 #define MONO_ARCH_HAVE_LIVERANGE_OPS 1
357 #define MONO_ARCH_HAVE_XP_UNWIND 1
358 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
359 #if !defined(HOST_WIN32)
360 #define MONO_ARCH_MONITOR_OBJECT_REG MONO_AMD64_ARG_REG1
361 #endif
362 #define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
363
364 #define MONO_ARCH_AOT_SUPPORTED 1
365 #if !defined( HOST_WIN32 ) && !defined( __native_client__ )
366 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
367 #endif
368
369 #if !defined(HOST_WIN32) || defined(__sun)
370 #define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
371 #endif
372
373 #define MONO_ARCH_SUPPORT_TASKLETS 1
374
375 #ifndef HOST_WIN32
376 #define MONO_AMD64_NO_PUSHES 1
377 #endif
378
379 #define MONO_ARCH_GSHARED_SUPPORTED 1
380 #define MONO_ARCH_DYN_CALL_SUPPORTED 1
381 #define MONO_ARCH_DYN_CALL_PARAM_AREA 0
382
383 #define MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE 1
384 #define MONO_ARCH_LLVM_SUPPORTED 1
385 #define MONO_ARCH_THIS_AS_FIRST_ARG 1
386 #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
387 #define MONO_ARCH_HAVE_CARD_TABLE_WBARRIER 1
388 #define MONO_ARCH_HAVE_SETUP_RESUME_FROM_SIGNAL_HANDLER_CTX 1
389 #define MONO_ARCH_GC_MAPS_SUPPORTED 1
390 #define MONO_ARCH_HAVE_CONTEXT_SET_INT_REG 1
391 #define MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK 1
392
393 gboolean
394 mono_amd64_tail_call_supported (MonoMethodSignature *caller_sig, MonoMethodSignature *callee_sig) MONO_INTERNAL;
395
396 #define MONO_ARCH_USE_OP_TAIL_CALL(caller_sig, callee_sig) mono_amd64_tail_call_supported (caller_sig, callee_sig)
397
398 /* Used for optimization, not complete */
399 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
400
401 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
402             MonoInst *inst; \
403             MONO_INST_NEW ((cfg), inst, OP_AMD64_ICOMPARE_MEMBASE_REG); \
404             inst->inst_basereg = array_reg; \
405             inst->inst_offset = offset; \
406             inst->sreg2 = index_reg; \
407             MONO_ADD_INS ((cfg)->cbb, inst); \
408             MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
409        } while (0)
410
411 void 
412 mono_amd64_patch (unsigned char* code, gpointer target) MONO_INTERNAL;
413
414 void
415 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
416                                                         guint64 dummy5, guint64 dummy6,
417                                                         mgreg_t *regs, mgreg_t rip,
418                                                         MonoObject *exc, gboolean rethrow) MONO_INTERNAL;
419
420 void
421 mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
422                                                                    guint64 dummy5, guint64 dummy6,
423                                                                    mgreg_t *regs, mgreg_t rip,
424                                                                    guint32 ex_token_index, gint64 pc_offset) MONO_INTERNAL;
425
426 guint64
427 mono_amd64_get_original_ip (void) MONO_INTERNAL;
428
429 guint8*
430 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
431
432 gboolean
433 mono_amd64_have_tls_get (void) MONO_INTERNAL;
434
435 GSList*
436 mono_amd64_get_exception_trampolines (gboolean aot) MONO_INTERNAL;
437
438 typedef struct {
439         guint8 *address;
440         guint8 saved_byte;
441 } MonoBreakpointInfo;
442
443 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
444
445 #ifdef HOST_WIN32
446
447 void mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg );
448 void mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg );
449 void mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size );
450 guint mono_arch_unwindinfo_get_size (gpointer monoui);
451 void mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size);
452
453 #define MONO_ARCH_HAVE_UNWIND_TABLE 1
454 #endif
455
456 #endif /* __MONO_MINI_AMD64_H__ */  
457