2006-01-02 Zoltan Varga <vargaz@gmail.com>
[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 #ifdef PLATFORM_WIN32
6 #include <windows.h>
7 /* use SIG* defines if possible */
8 #ifdef HAVE_SIGNAL_H
9 #include <signal.h>
10 #endif
11
12 /* sigcontext surrogate */
13 struct sigcontext {
14         unsigned int eax;
15         unsigned int ebx;
16         unsigned int ecx;
17         unsigned int edx;
18         unsigned int ebp;
19         unsigned int esp;
20         unsigned int esi;
21         unsigned int edi;
22         unsigned int eip;
23 };
24
25 typedef void (* MonoW32ExceptionHandler) (int);
26 void win32_seh_init(void);
27 void win32_seh_cleanup(void);
28 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
29
30 #ifndef SIGFPE
31 #define SIGFPE 4
32 #endif
33
34 #ifndef SIGILL
35 #define SIGILL 8
36 #endif
37
38 #ifndef SIGSEGV
39 #define SIGSEGV 11
40 #endif
41
42 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
43
44 #endif /* PLATFORM_WIN32 */
45
46 #if defined( __linux__) || defined(__sun)
47 #define MONO_ARCH_USE_SIGACTION
48 #endif
49
50 #ifndef PLATFORM_WIN32
51
52 #ifdef HAVE_WORKING_SIGALTSTACK
53 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
54 #define MONO_ARCH_USE_SIGACTION
55
56 /* FreeBSD and NetBSD need SA_STACK and MAP_ANON re-definitions */
57 #       if defined(__FreeBSD__) || defined(__NetBSD__) 
58 #               ifndef SA_STACK
59 #                       define SA_STACK SA_ONSTACK
60 #               endif
61 #               ifndef MAP_ANONYMOUS
62 #                       define MAP_ANONYMOUS MAP_ANON
63 #               endif
64 #       endif /* BSDs */
65
66 #endif /* HAVE_WORKING_SIGALTSTACK */
67 #endif /* !PLATFORM_WIN32 */
68
69 #define MONO_ARCH_SIGNAL_STACK_SIZE (64 * 1024)
70
71 /* Enables OP_LSHL, OP_LSHL_IMM, OP_LSHR, OP_LSHR_IMM, OP_LSHR_UN, OP_LSHR_UN_IMM */
72 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
73
74 #define MONO_MAX_IREGS 8
75 #define MONO_MAX_FREGS 6
76
77 /* Parameters used by the register allocator */
78 #define MONO_ARCH_HAS_XP_LOCAL_REGALLOC
79
80 #define MONO_ARCH_CALLEE_REGS X86_CALLEE_REGS
81 #define MONO_ARCH_CALLEE_SAVED_REGS X86_CALLER_REGS
82
83 #define MONO_ARCH_CALLEE_FREGS 0
84 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
85
86 #define MONO_ARCH_USE_FPSTACK TRUE
87 #define MONO_ARCH_FPSTACK_SIZE 6
88
89 /* FIXME: Don't constrain byte regs to eax */
90 #define MONO_ARCH_INST_FIXED_REG(desc) ((desc == 's') ? X86_ECX : ((desc == 'a') ? X86_EAX : ((desc == 'd') ? X86_EDX : ((desc == 'y') ? X86_EAX : ((desc == 'l') ? X86_EAX : -1)))))
91
92 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
93 #define MONO_ARCH_INST_SREG2_MASK(ins) (((ins [MONO_INST_CLOB] == 'a') || (ins [MONO_INST_CLOB] == 'd')) ? (1 << X86_EDX) : 0)
94
95 /*
96  * L is a generic register pair, while l means eax:rdx
97  */
98 #define MONO_ARCH_INST_IS_REGPAIR(desc) (desc == 'l' || desc == 'L')
99 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (desc == 'l' ? X86_EDX : -1)
100
101 #define MONO_ARCH_FRAME_ALIGNMENT 4
102
103 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
104  * reproduceable results for benchmarks */
105 #define MONO_ARCH_CODE_ALIGNMENT 32
106
107 #define MONO_ARCH_BASEREG X86_EBP
108 #define MONO_ARCH_RETREG1 X86_EAX
109 #define MONO_ARCH_RETREG2 X86_EDX
110
111 #define MONO_ARCH_ENCODE_LREG(r1,r2) (r1 | (r2<<3))
112
113 #define inst_dreg_low dreg&7 
114 #define inst_dreg_high dreg>>3
115 #define inst_sreg1_low sreg1&7 
116 #define inst_sreg1_high sreg1>>3
117 #define inst_sreg2_low sreg2&7 
118 #define inst_sreg2_high sreg2>>3
119
120 struct MonoLMF {
121         gpointer    previous_lmf;
122         gpointer    lmf_addr;
123         MonoMethod *method;
124         guint32     ebx;
125         guint32     edi;
126         guint32     esi;
127         guint32     ebp;
128         guint32     eip;
129 };
130
131 typedef void* MonoCompileArch;
132
133 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
134 # define SC_EAX sc_eax
135 # define SC_EBX sc_ebx
136 # define SC_ECX sc_ecx
137 # define SC_EDX sc_edx
138 # define SC_EBP sc_ebp
139 # define SC_EIP sc_eip
140 # define SC_ESP sc_esp
141 # define SC_EDI sc_edi
142 # define SC_ESI sc_esi
143 #else
144 # define SC_EAX eax
145 # define SC_EBX ebx
146 # define SC_ECX ecx
147 # define SC_EDX edx
148 # define SC_EBP ebp
149 # define SC_EIP eip
150 # define SC_ESP esp
151 # define SC_EDI edi
152 # define SC_ESI esi
153 #endif
154
155 typedef struct {
156         guint32 eax;
157         guint32 ebx;
158         guint32 ecx;
159         guint32 edx;
160         guint32 ebp;
161         guint32 esp;
162     guint32 esi;
163         guint32 edi;
164         guint32 eip;
165 } MonoContext;
166
167 #define MONO_CONTEXT_SET_IP(ctx,ip) do { (ctx)->eip = (long)(ip); } while (0); 
168 #define MONO_CONTEXT_SET_BP(ctx,bp) do { (ctx)->ebp = (long)(bp); } while (0); 
169 #define MONO_CONTEXT_SET_SP(ctx,sp) do { (ctx)->esp = (long)(sp); } while (0); 
170
171 #define MONO_CONTEXT_GET_IP(ctx) ((gpointer)((ctx)->eip))
172 #define MONO_CONTEXT_GET_BP(ctx) ((gpointer)((ctx)->ebp))
173 #define MONO_CONTEXT_GET_SP(ctx) ((gpointer)((ctx)->esp))
174
175 #ifdef _MSC_VER
176
177 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
178     unsigned int stackptr; \
179         mono_arch_flush_register_windows (); \
180     { \
181            __asm mov stackptr, ebp \
182     } \
183         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
184         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
185 } while (0)
186
187 #define MONO_INIT_CONTEXT_FROM_CALLER(ctx) do { \
188         unsigned int stackptr, retaddr; \
189     { \
190             __asm mov stackptr, ebp \
191         __asm mov eax, DWORD PTR [ebp + 4] \
192             __asm mov retaddr, eax \
193     } \
194         MONO_CONTEXT_SET_IP ((ctx), retaddr); \
195         /* FIXME: NOT WORKING -- THIS IS __builtin_frame_address (0) NOT (1) */ \
196         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
197 } while (0)
198
199 #else
200
201 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
202                 mono_arch_flush_register_windows ();    \
203                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
204                 MONO_CONTEXT_SET_BP ((ctx), __builtin_frame_address (0));       \
205         } while (0)
206
207 #endif
208
209 #define MONO_ARCH_BIGMUL_INTRINS 1
210 #define MONO_ARCH_NEED_DIV_CHECK 1
211 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
212 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
213 #define MONO_ARCH_HAVE_PIC_AOT 1
214 #define MONO_ARCH_NEED_GOT_VAR 1
215 #define MONO_ARCH_HAVE_THROW_CORLIB_EXCEPTION 1
216 #define MONO_ARCH_ENABLE_EMIT_STATE_OPT 1
217 #define MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN 1
218 #define MONO_ARCH_HAVE_CREATE_SPECIFIC_TRAMPOLINE 1
219 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
220 #define MONO_ARCH_HAVE_CREATE_VARS 1
221
222 #endif /* __MONO_MINI_X86_H__ */  
223