Wed Oct 6 12:40:28 CEST 2004 Paolo Molaro <lupus@ximian.com>
[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 <glib.h>
6
7 #ifdef PLATFORM_WIN32
8 #include <windows.h>
9 /* use SIG* defines if possible */
10 #ifdef HAVE_SIGNAL_H
11 #include <signal.h>
12 #endif
13
14 /* sigcontext surrogate */
15 struct sigcontext {
16         guint64 eax;
17         guint64 ebx;
18         guint64 ecx;
19         guint64 edx;
20         guint64 ebp;
21         guint64 esp;
22     guint64 esi;
23         guint64 edi;
24         guint64 eip;
25 };
26
27 typedef void (* MonoW32ExceptionHandler) (int);
28 void win32_seh_init(void);
29 void win32_seh_cleanup(void);
30 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
31
32 #ifndef SIGFPE
33 #define SIGFPE 4
34 #endif
35
36 #ifndef SIGILL
37 #define SIGILL 8
38 #endif
39
40 #ifndef SIGSEGV
41 #define SIGSEGV 11
42 #endif
43
44 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
45
46 #endif /* PLATFORM_WIN32 */
47
48 #ifdef sun    // Solaris x86
49 #  undef SIGSEGV_ON_ALTSTACK
50 #  define MONO_ARCH_USE_SIGACTION 1
51 struct sigcontext {
52         unsigned short gs, __gsh;
53         unsigned short fs, __fsh;
54         unsigned short es, __esh;
55         unsigned short ds, __dsh;
56         unsigned long edi;
57         unsigned long esi;
58         unsigned long ebp;
59         unsigned long esp;
60         unsigned long ebx;
61         unsigned long edx;
62         unsigned long ecx;
63         unsigned long eax;
64         unsigned long trapno;
65         unsigned long err;
66         unsigned long eip;
67         unsigned short cs, __csh;
68         unsigned long eflags;
69         unsigned long esp_at_signal;
70         unsigned short ss, __ssh;
71         unsigned long fpstate[95];
72       unsigned long filler[5];
73 };
74 #endif  // sun, Solaris x86
75
76 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
77 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
78
79 #define MONO_MAX_IREGS 16
80
81 #define MONO_MAX_FREGS AMD64_XMM_NREG
82
83 #define MONO_ARCH_FRAME_ALIGNMENT 16
84
85 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
86  * reproduceable results for benchmarks */
87 #define MONO_ARCH_CODE_ALIGNMENT 32
88
89 #define MONO_ARCH_BASEREG X86_EBP
90 #define MONO_ARCH_RETREG1 X86_EAX
91 #define MONO_ARCH_RETREG2 X86_EDX
92
93 #define MONO_ARCH_ENCODE_LREG(r1,r2) (r1 | (r2<<3))
94
95 #define inst_dreg_low dreg&7 
96 #define inst_dreg_high dreg>>3
97 #define inst_sreg1_low sreg1&7 
98 #define inst_sreg1_high sreg1>>3
99 #define inst_sreg2_low sreg2&7 
100 #define inst_sreg2_high sreg2>>3
101
102 struct MonoLMF {
103         gpointer    previous_lmf;
104         gpointer    lmf_addr;
105         MonoMethod *method;
106         guint64     rip;
107         guint64     rbx;
108         guint64     ebp;
109         guint64     r12;
110         guint64     r13;
111         guint64     r14;
112         guint64     r15;
113 };
114
115 typedef struct MonoCompileArch {
116         gint32 lmf_offset;
117         gint32 localloc_offset;    
118 } MonoCompileArch;
119
120 typedef struct {
121         guint64 rax;
122         guint64 rbx;
123         guint64 rcx;
124         guint64 rdx;
125         guint64 rbp;
126         guint64 rsp;
127     guint64 rsi;
128         guint64 rdi;
129         guint64 rip;
130         guint64 r12;
131         guint64 r13;
132         guint64 r14;
133         guint64 r15;
134 } MonoContext;
135
136 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
137 # define SC_EAX sc_eax
138 # define SC_EBX sc_ebx
139 # define SC_ECX sc_ecx
140 # define SC_EDX sc_edx
141 # define SC_EBP sc_ebp
142 # define SC_EIP sc_eip
143 # define SC_ESP sc_esp
144 # define SC_EDI sc_edi
145 # define SC_ESI sc_esi
146 #else
147 # define SC_EAX rax
148 # define SC_EBX rbx
149 # define SC_ECX rcx
150 # define SC_EDX rdx
151 # define SC_EBP rbp
152 # define SC_EIP rip
153 # define SC_ESP rsp
154 # define SC_EDI rdi
155 # define SC_ESI rsi
156
157 # define SC_RIP rip
158 # define SC_RSP rsp
159 # define SC_RBP rbp
160 # define SC_RBX rbx
161 # define SC_R12 r12
162 # define SC_R13 r13
163 # define SC_R14 r14
164 # define SC_R15 r15
165
166 #endif
167
168 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->rip = (long)(ip); } while (0); 
169 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->rbp = (long)(bp); } while (0); 
170 #define MONO_CONTEXT_SET_SP(ctx,esp) do { (ctx)->rsp = (long)(esp); } while (0); 
171
172 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->rip))
173 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->rbp))
174 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->rsp))
175
176 #define MONO_ARCH_USE_SIGACTION 1
177
178 /*
179  * some icalls like mono_array_new_va needs to be called using a different 
180  * calling convention.
181  */
182 #define MONO_ARCH_VARARG_ICALLS 1
183
184 #ifndef PLATFORM_WIN32
185
186 #ifdef HAVE_WORKING_SIGALTSTACK
187
188 /*
189  * FIXME: For some reason, when sigaltstack is enabled, the uc_mcontext member
190  * in ucontext_t is not at the offset indicated by the definition of ucontext_t.
191  */
192
193 //#define MONO_ARCH_SIGSEGV_ON_ALTSTACK
194
195 /* NetBSD doesn't define SA_STACK */
196 #ifndef SA_STACK
197 #define SA_STACK SA_ONSTACK
198 #endif
199 #endif
200
201 #endif
202
203 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
204 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
205
206 #define MONO_ARCH_EMULATE_CONV_R8_UN    1
207 #define MONO_ARCH_EMULATE_LCONV_TO_R8_UN 1
208 #define MONO_ARCH_EMULATE_FREM 1
209 #define MONO_ARCH_NEED_DIV_CHECK 1
210 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
211
212 #define MONO_ARCH_ENABLE_EMIT_STATE_OPT 1
213 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
214
215 gpointer*
216 mono_amd64_get_vcall_slot_addr (guint8* code, guint64 *regs);
217
218 void
219 mono_amd64_exceptions_init (void);
220
221 void
222 mono_amd64_tramp_init (void);
223
224 /* FIXME: */
225 //#define MONO_ARCH_BIGMUL_INTRINS 1
226
227 #endif /* __MONO_MINI_AMD64_H__ */  
228