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