Merged trunk and subtype.
[cacao.git] / src / vm / jit / i386 / codegen.h
1 /* src/vm/jit/i386/codegen.h - code generation macros and definitions for i386
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CODEGEN_H
27 #define _CODEGEN_H
28
29 #include "config.h"
30 #include "vm/types.h"
31
32 #include "vm/jit/i386/emit.h"
33
34 #include "vm/jit/jit.hpp"
35
36
37 #if defined(ENABLE_LSRA)
38 /* let LSRA allocate reserved registers (REG_ITMP[1|2|3]) */
39 # define LSRA_USES_REG_RES
40 #endif
41
42
43 /* additional functions and macros to generate code ***************************/
44
45 #define CALCOFFSETBYTES(var, reg, val) \
46     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
47     else if ((s4) (val) != 0) (var) += 1; \
48     else if ((reg) == EBP) (var) += 1;
49
50
51 #define CALCIMMEDIATEBYTES(var, val) \
52     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
53     else (var) += 1;
54
55
56 #define ALIGNCODENOP \
57     do { \
58         for (s1 = 0; s1 < (s4) (((ptrint) cd->mcodeptr) & 7); s1++) \
59             M_NOP; \
60     } while (0)
61
62
63 /* MCODECHECK(icnt) */
64
65 #define MCODECHECK(icnt) \
66     do { \
67         if ((cd->mcodeptr + (icnt)) > (u1 *) cd->mcodeend) \
68             codegen_increase(cd); \
69     } while (0)
70
71
72 /* M_INTMOVE:
73      generates an integer-move from register a to b.
74      if a and b are the same int-register, no code will be generated.
75 */ 
76
77 #define M_INTMOVE(a,b) \
78     do { \
79         if ((a) != (b)) \
80             M_MOV(a, b); \
81     } while (0)
82
83 #define M_LNGMOVE(a,b) \
84     do { \
85         if (GET_HIGH_REG(a) == GET_LOW_REG(b)) { \
86             assert((GET_LOW_REG(a) != GET_HIGH_REG(b))); \
87             M_INTMOVE(GET_HIGH_REG(a), GET_HIGH_REG(b)); \
88             M_INTMOVE(GET_LOW_REG(a), GET_LOW_REG(b)); \
89         } else { \
90             M_INTMOVE(GET_LOW_REG(a), GET_LOW_REG(b)); \
91             M_INTMOVE(GET_HIGH_REG(a), GET_HIGH_REG(b)); \
92         } \
93     } while (0)
94
95
96 /* M_FLTMOVE:
97     generates a floating-point-move from register a to b.
98     if a and b are the same float-register, no code will be generated
99 */
100
101 #define M_FLTMOVE(reg,dreg)                                          \
102     do {                                                             \
103         if ((reg) != (dreg)) {                                       \
104             log_text("M_FLTMOVE");                                   \
105             assert(0);                                               \
106         }                                                            \
107     } while (0)
108
109
110 #define ICONST(d,c) \
111     do { \
112         if ((c) == 0) \
113             M_CLR(d); \
114         else \
115             M_MOV_IMM((c), d); \
116     } while (0)
117
118
119 #define LCONST(d,c) \
120     do { \
121         if ((c) == 0) { \
122             M_CLR(GET_LOW_REG(d)); \
123             M_CLR(GET_HIGH_REG(d)); \
124         } else { \
125             M_MOV_IMM((c), GET_LOW_REG(d)); \
126             M_MOV_IMM((c) >> 32, GET_HIGH_REG(d)); \
127         } \
128     } while (0)
129
130
131 /* branch defines *************************************************************/
132
133 #define BRANCH_UNCONDITIONAL_SIZE    5  /* size in bytes of a branch          */
134 #define BRANCH_CONDITIONAL_SIZE      6  /* size in bytes of a branch          */
135
136 #define BRANCH_NOPS \
137     do { \
138         M_NOP; \
139         M_NOP; \
140         M_NOP; \
141         M_NOP; \
142         M_NOP; \
143         M_NOP; \
144     } while (0)
145
146
147 /* patcher defines ************************************************************/
148
149 #define PATCHER_CALL_SIZE    2          /* size in bytes of a patcher call    */
150
151 #define PATCHER_NOPS \
152     do { \
153         M_NOP; \
154         M_NOP; \
155     } while (0)
156
157
158 /* macros to create code ******************************************************/
159
160 #define M_BYTE1(a) \
161     do { \
162         *(cd->mcodeptr) = (a); \
163         cd->mcodeptr++; \
164     } while (0)
165
166
167 #define M_BYTE2(a, b) \
168     do { \
169         M_BYTE1(a); \
170         M_BYTE1(b); \
171     } while (0)
172
173
174 #define M_ILD(a,b,disp)         emit_mov_membase_reg(cd, (b), (disp), (a))
175 #define M_ILD32(a,b,disp)       emit_mov_membase32_reg(cd, (b), (disp), (a))
176
177 #define M_ALD(a,b,disp)         M_ILD(a,b,disp)
178 #define M_ALD32(a,b,disp)       M_ILD32(a,b,disp)
179
180 #define M_ALD_MEM(a,disp)       emit_mov_mem_reg(cd, (disp), (a))
181
182 #define M_ALD_MEM_GET_OPC(p)    (*(p))
183 #define M_ALD_MEM_GET_MOD(p)    (((*(p + 1)) >> 6) & 0x03)
184 #define M_ALD_MEM_GET_REG(p)    (((*(p + 1)) >> 3) & 0x07)
185 #define M_ALD_MEM_GET_RM(p)     (((*(p + 1))     ) & 0x07)
186 #define M_ALD_MEM_GET_DISP(p)   (*((u4 *) (p + 2)))
187
188 #define M_LLD(a,b,disp) \
189     do { \
190         M_ILD(GET_LOW_REG(a),b,disp); \
191         M_ILD(GET_HIGH_REG(a),b,disp + 4); \
192     } while (0)
193
194 #define M_LLD32(a,b,disp) \
195     do { \
196         M_ILD32(GET_LOW_REG(a),b,disp); \
197         M_ILD32(GET_HIGH_REG(a),b,disp + 4); \
198     } while (0)
199
200 #define M_IST(a,b,disp)         emit_mov_reg_membase(cd, (a), (b), (disp))
201 #define M_IST_IMM(a,b,disp)     emit_mov_imm_membase(cd, (u4) (a), (b), (disp))
202 #define M_AST(a,b,disp)         M_IST(a,b,disp)
203 #define M_AST_IMM(a,b,disp)     M_IST_IMM(a,b,disp)
204
205 #define M_IST32(a,b,disp)       emit_mov_reg_membase32(cd, (a), (b), (disp))
206 #define M_IST32_IMM(a,b,disp)   emit_mov_imm_membase32(cd, (u4) (a), (b), (disp))
207
208 #define M_LST(a,b,disp) \
209     do { \
210         M_IST(GET_LOW_REG(a),b,disp); \
211         M_IST(GET_HIGH_REG(a),b,disp + 4); \
212     } while (0)
213
214 #define M_LST32(a,b,disp) \
215     do { \
216         M_IST32(GET_LOW_REG(a),b,disp); \
217         M_IST32(GET_HIGH_REG(a),b,disp + 4); \
218     } while (0)
219
220 #define M_LST_IMM(a,b,disp) \
221     do { \
222         M_IST_IMM(a,b,disp); \
223         M_IST_IMM(a >> 32,b,disp + 4); \
224     } while (0)
225
226 #define M_LST32_IMM(a,b,disp) \
227     do { \
228         M_IST32_IMM(a,b,disp); \
229         M_IST32_IMM(a >> 32,b,disp + 4); \
230     } while (0)
231
232 #define M_IADD(a,b)             emit_alu_reg_reg(cd, ALU_ADD, (a), (b))
233 #define M_ISUB(a,b)             emit_alu_reg_reg(cd, ALU_SUB, (a), (b))
234 #define M_IMUL(a,b)             emit_imul_reg_reg(cd, (a), (b))
235 #define M_IDIV(a)               emit_idiv_reg(cd, (a))
236
237 #define M_MUL(a)                emit_mul_reg(cd, (a))
238
239 #define M_IADD_IMM(a,b)         emit_alu_imm_reg(cd, ALU_ADD, (a), (b))
240 #define M_ISUB_IMM(a,b)         emit_alu_imm_reg(cd, ALU_SUB, (a), (b))
241 #define M_IMUL_IMM(a,b,c)       emit_imul_imm_reg_reg(cd, (b), (a), (c))
242
243 #define M_IADD_IMM32(a,b)       emit_alu_imm32_reg(cd, ALU_ADD, (a), (b))
244 #define M_ISUB_IMM32(a,b)       emit_alu_imm32_reg(cd, ALU_SUB, (a), (b))
245
246 #define M_IADD_IMM_MEMBASE(a,b,c) emit_alu_imm_membase(cd, ALU_ADD, (a), (b), (c))
247
248 #define M_ISUB_IMM_MEMABS(a,b)  emit_alu_imm_memabs(cd, ALU_SUB, (a), (b))
249
250 #define M_IINC(a)               emit_inc_reg(cd, (a))
251
252 #define M_IADDC(a,b)            emit_alu_reg_reg(cd, ALU_ADC, (a), (b))
253 #define M_ISUBB(a,b)            emit_alu_reg_reg(cd, ALU_SBB, (a), (b))
254
255 #define M_IADDC_IMM(a,b)        emit_alu_imm_reg(cd, ALU_ADC, (a), (b))
256 #define M_ISUBB_IMM(a,b)        emit_alu_imm_reg(cd, ALU_SBB, (a), (b))
257
258 #define M_AADD_IMM(a,b)         M_IADD_IMM(a,b)
259 #define M_AADD_IMM32(a,b)       M_IADD_IMM32(a,b)
260 #define M_ASUB_IMM(a,b)         M_ISUB_IMM(a,b)
261
262 #define M_NEG(a)                emit_neg_reg(cd, (a))
263
264 #define M_AND(a,b)              emit_alu_reg_reg(cd, ALU_AND, (a), (b))
265 #define M_OR(a,b)               emit_alu_reg_reg(cd, ALU_OR, (a), (b))
266 #define M_XOR(a,b)              emit_alu_reg_reg(cd, ALU_XOR, (a), (b))
267
268 #define M_AND_IMM(a,b)          emit_alu_imm_reg(cd, ALU_AND, (a), (b))
269 #define M_OR_IMM(a,b)           emit_alu_imm_reg(cd, ALU_OR, (a), (b))
270 #define M_XOR_IMM(a,b)          emit_alu_imm_reg(cd, ALU_XOR, (a), (b))
271
272 #define M_AND_IMM32(a,b)        emit_alu_imm32_reg(cd, ALU_AND, (a), (b))
273
274 #define M_CLR(a)                M_XOR(a,a)
275
276 #define M_PUSH(a)               emit_push_reg(cd, (a))
277 #define M_PUSH_IMM(a)           emit_push_imm(cd, (s4) (a))
278 #define M_POP(a)                emit_pop_reg(cd, (a))
279
280 #define M_MOV(a,b)              emit_mov_reg_reg(cd, (a), (b))
281 #define M_MOV_IMM(a,b)          emit_mov_imm_reg(cd, (u4) (a), (b))
282
283 #define M_TEST(a)               emit_test_reg_reg(cd, (a), (a))
284 #define M_TEST_IMM(a,b)         emit_test_imm_reg(cd, (a), (b))
285
286 #define M_CMP(a,b)              emit_alu_reg_reg(cd, ALU_CMP, (a), (b))
287 #define M_CMP_MEMBASE(a,b,c)    emit_alu_membase_reg(cd, ALU_CMP, (a), (b), (c))
288 #define M_CMP_MEMINDEX(a,b,c,d,e)    emit_alu_memindex_reg(cd, ALU_CMP, (b), (a), (c), (d), (e))
289
290 #define M_CMP_IMM(a,b)          emit_alu_imm_reg(cd, ALU_CMP, (a), (b))
291 #define M_CMP_IMM_MEMBASE(a,b,c) emit_alu_imm_membase(cd, ALU_CMP, (a), (b), (c))
292
293 #define M_CMP_IMM32(a,b)        emit_alu_imm32_reg(cd, ALU_CMP, (a), (b))
294
295 #define M_BSEXT(a,b)            emit_movsbl_reg_reg(cd, (a), (b))
296 #define M_SSEXT(a,b)            emit_movswl_reg_reg(cd, (a), (b))
297
298 #define M_BZEXT(a,b)            emit_movzbl_reg_reg(cd, (a), (b))
299 #define M_CZEXT(a,b)            emit_movzwl_reg_reg(cd, (a), (b))
300
301 #define M_CLTD                  M_BYTE1(0x99)
302
303 #define M_SLL(a)                emit_shift_reg(cd, SHIFT_SHL, (a))
304 #define M_SRA(a)                emit_shift_reg(cd, SHIFT_SAR, (a))
305 #define M_SRL(a)                emit_shift_reg(cd, SHIFT_SHR, (a))
306
307 #define M_SLL_IMM(a,b)          emit_shift_imm_reg(cd, SHIFT_SHL, (a), (b))
308 #define M_SRA_IMM(a,b)          emit_shift_imm_reg(cd, SHIFT_SAR, (a), (b))
309 #define M_SRL_IMM(a,b)          emit_shift_imm_reg(cd, SHIFT_SHR, (a), (b))
310
311 #define M_SLLD(a,b)             emit_shld_reg_reg(cd, (a), (b))
312 #define M_SRLD(a,b)             emit_shrd_reg_reg(cd, (a), (b))
313
314 #define M_SLLD_IMM(a,b,c)       emit_shld_imm_reg_reg(cd, (a), (b), (c))
315 #define M_SRLD_IMM(a,b,c)       emit_shrd_imm_reg_reg(cd, (a), (b), (c))
316
317 #define M_CALL(a)               emit_call_reg(cd, (a))
318 #define M_CALL_IMM(a)           emit_call_imm(cd, (a))
319 #define M_RET                   M_BYTE1(0xc3)
320
321 #define M_ICMP_IMM(a,b)         emit_alu_imm_reg(cd, ALU_CMP, (a), (b))
322
323 #define M_BEQ(a)                emit_jcc(cd, CC_E, (a))
324 #define M_BNE(a)                emit_jcc(cd, CC_NE, (a))
325 #define M_BLT(a)                emit_jcc(cd, CC_L, (a))
326 #define M_BLE(a)                emit_jcc(cd, CC_LE, (a))
327 #define M_BGE(a)                emit_jcc(cd, CC_GE, (a))
328 #define M_BGT(a)                emit_jcc(cd, CC_G, (a))
329
330 #define M_BB(a)                 emit_jcc(cd, CC_B, (a))
331 #define M_BBE(a)                emit_jcc(cd, CC_BE, (a))
332 #define M_BAE(a)                emit_jcc(cd, CC_AE, (a))
333 #define M_BA(a)                 emit_jcc(cd, CC_A, (a))
334 #define M_BNS(a)                emit_jcc(cd, CC_NS, (a))
335 #define M_BS(a)                 emit_jcc(cd, CC_S, (a))
336
337 #define M_SETE(a)               emit_setcc_reg(cd, CC_E, (a))
338
339 #define M_JMP(a)                emit_jmp_reg(cd, (a))
340 #define M_JMP_IMM(a)            emit_jmp_imm(cd, (a))
341
342 #define M_NOP                   M_BYTE1(0x90)
343 #define M_UD2                   M_BYTE2(0x0f, 0x0b)
344
345
346 #define M_FLD(a,b,disp)         emit_flds_membase(cd, (b), (disp))
347 #define M_DLD(a,b,disp)         emit_fldl_membase(cd, (b), (disp))
348
349 #define M_FLD32(a,b,disp)       emit_flds_membase32(cd, (b), (disp))
350 #define M_DLD32(a,b,disp)       emit_fldl_membase32(cd, (b), (disp))
351
352 #define M_FST(a,b,disp)         emit_fstps_membase(cd, (b), (disp))
353 #define M_DST(a,b,disp)         emit_fstpl_membase(cd, (b), (disp))
354
355 #define M_FSTNP(a,b,disp)       emit_fsts_membase(cd, (b), (disp))
356 #define M_DSTNP(a,b,disp)       emit_fstl_membase(cd, (b), (disp))
357
358 #endif /* _CODEGEN_H */
359
360
361 /*
362  * These are local overrides for various environment variables in Emacs.
363  * Please do not remove this and leave it at the end of the file, where
364  * Emacs will automagically detect them.
365  * ---------------------------------------------------------------------
366  * Local variables:
367  * mode: c
368  * indent-tabs-mode: t
369  * c-basic-offset: 4
370  * tab-width: 4
371  * End:
372  */