* src/vm/jit/x86_64/codegen.c (codegen) [ENABLE_PROFILING]: #ifdef
[cacao.git] / src / vm / jit / x86_64 / codegen.h
1 /* src/vm/jit/x86_64/codegen.h - code generation macros for x86_64
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    $Id: codegen.h 7281 2007-02-03 19:51:36Z twisti $
26
27 */
28
29
30 #ifndef _CODEGEN_H
31 #define _CODEGEN_H
32
33 #include "config.h"
34
35 #include <ucontext.h>
36
37 #include "vm/types.h"
38
39 #include "vm/jit/x86_64/emit.h"
40
41 #include "vm/jit/jit.h"
42
43
44 /* additional functions and macros to generate code ***************************/
45
46 #define CALCOFFSETBYTES(var, reg, val) \
47     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
48     else if ((s4) (val) != 0) (var) += 1; \
49     else if ((reg) == RBP || (reg) == RSP || (reg) == R12 || (reg) == R13) (var) += 1;
50
51
52 #define CALCIMMEDIATEBYTES(var, val) \
53     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
54     else (var) += 1;
55
56
57 /* MCODECHECK(icnt) */
58
59 #define MCODECHECK(icnt) \
60     do { \
61         if ((cd->mcodeptr + (icnt)) > cd->mcodeend) \
62             codegen_increase(cd); \
63     } while (0)
64
65
66 #define ALIGNCODENOP \
67     if ((s4) (((ptrint) cd->mcodeptr) & 7)) { \
68         M_NOP; \
69     }
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(reg,dreg) \
78     do { \
79         if ((reg) != (dreg)) { \
80             M_MOV(reg, dreg); \
81         } \
82     } while (0)
83
84
85 /* M_FLTMOVE:
86     generates a floating-point-move from register a to b.
87     if a and b are the same float-register, no code will be generated
88 */ 
89
90 #define M_FLTMOVE(reg,dreg) \
91     do { \
92         if ((reg) != (dreg)) { \
93             M_FMOV(reg, dreg); \
94         } \
95     } while (0)
96
97
98 #define ICONST(r,c) \
99     do { \
100         if ((c) == 0) \
101             M_CLR((d)); \
102         else \
103             M_IMOV_IMM((c), (d)); \
104     } while (0)
105 /*     do { \ */
106 /*        M_IMOV_IMM((c), (d)); \ */
107 /*     } while (0) */
108
109
110 #define LCONST(r,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 /* branch defines *************************************************************/
120
121 #define BRANCH_NOPS \
122     do { \
123         M_NOP; \
124         M_NOP; \
125         M_NOP; \
126         M_NOP; \
127         M_NOP; \
128     } while (0)
129
130
131 /* patcher defines ************************************************************/
132
133 #define PATCHER_CALL_SIZE    5          /* size in bytes of a patcher call    */
134
135 #define PATCHER_NOPS \
136     do { \
137         M_NOP; \
138         M_NOP; \
139         M_NOP; \
140         M_NOP; \
141         M_NOP; \
142     } while (0)
143
144
145 /* macros to create code ******************************************************/
146
147 #define M_MOV(a,b)              emit_mov_reg_reg(cd, (a), (b))
148 #define M_MOV_IMM(a,b)          emit_mov_imm_reg(cd, (u8) (a), (b))
149
150 #define M_IMOV(a,b)             emit_movl_reg_reg(cd, (a), (b))
151 #define M_IMOV_IMM(a,b)         emit_movl_imm_reg(cd, (u4) (a), (b))
152
153 #define M_FMOV(a,b)             emit_movq_reg_reg(cd, (a), (b))
154
155 #define M_ILD(a,b,disp)         emit_movl_membase_reg(cd, (b), (disp), (a))
156 #define M_LLD(a,b,disp)         emit_mov_membase_reg(cd, (b), (disp), (a))
157
158 #define M_ILD32(a,b,disp)       emit_movl_membase32_reg(cd, (b), (disp), (a))
159 #define M_LLD32(a,b,disp)       emit_mov_membase32_reg(cd, (b), (disp), (a))
160
161 #define M_IST(a,b,disp)         emit_movl_reg_membase(cd, (a), (b), (disp))
162 #define M_LST(a,b,disp)         emit_mov_reg_membase(cd, (a), (b), (disp))
163
164 #define M_IST_IMM(a,b,disp)     emit_movl_imm_membase(cd, (a), (b), (disp))
165 #define M_LST_IMM32(a,b,disp)   emit_mov_imm_membase(cd, (a), (b), (disp))
166
167 #define M_IST32(a,b,disp)       emit_movl_reg_membase32(cd, (a), (b), (disp))
168 #define M_LST32(a,b,disp)       emit_mov_reg_membase32(cd, (a), (b), (disp))
169
170 #define M_IST32_IMM(a,b,disp)   emit_movl_imm_membase32(cd, (a), (b), (disp))
171 #define M_LST32_IMM32(a,b,disp) emit_mov_imm_membase32(cd, (a), (b), (disp))
172
173 #define M_IADD(a,b)             emit_alul_reg_reg(cd, ALU_ADD, (a), (b))
174 #define M_ISUB(a,b)             emit_alul_reg_reg(cd, ALU_SUB, (a), (b))
175 #define M_IMUL(a,b)             emit_imull_reg_reg(cd, (a), (b))
176
177 #define M_IADD_IMM(a,b)         emit_alul_imm_reg(cd, ALU_ADD, (a), (b))
178 #define M_ISUB_IMM(a,b)         emit_alul_imm_reg(cd, ALU_SUB, (a), (b))
179 #define M_IMUL_IMM(a,b,c)       emit_imull_imm_reg_reg(cd, (b), (a), (c))
180
181 #define M_LADD(a,b)             emit_alu_reg_reg(cd, ALU_ADD, (a), (b))
182 #define M_LSUB(a,b)             emit_alu_reg_reg(cd, ALU_SUB, (a), (b))
183 #define M_LMUL(a,b)             emit_imul_reg_reg(cd, (a), (b))
184
185 #define M_LADD_IMM(a,b)         emit_alu_imm_reg(cd, ALU_ADD, (a), (b))
186 #define M_LSUB_IMM(a,b)         emit_alu_imm_reg(cd, ALU_SUB, (a), (b))
187 #define M_LMUL_IMM(a,b,c)       emit_imul_imm_reg_reg(cd, (b), (a), (c))
188
189 #define M_IINC(a)               emit_incl_reg(cd, (a))
190 #define M_IDEC(a)               emit_decl_reg(cd, (a))
191
192 #define M_ALD(a,b,disp)         M_LLD(a,b,disp)
193 #define M_ALD32(a,b,disp)       M_LLD32(a,b,disp)
194
195 #define M_AST(a,b,c)            M_LST(a,b,c)
196 #define M_AST_IMM32(a,b,c)      M_LST_IMM32(a,b,c)
197
198 #define M_AADD(a,b)             M_LADD(a,b)
199 #define M_AADD_IMM(a,b)         M_LADD_IMM(a,b)
200 #define M_ASUB_IMM(a,b)         M_LSUB_IMM(a,b)
201
202 #define M_LADD_IMM32(a,b)       emit_alu_imm32_reg(cd, ALU_ADD, (a), (b))
203 #define M_AADD_IMM32(a,b)       M_LADD_IMM32(a,b)
204 #define M_LSUB_IMM32(a,b)       emit_alu_imm32_reg(cd, ALU_SUB, (a), (b))
205
206 #define M_ILEA(a,b,c)           emit_leal_membase_reg(cd, (a), (b), (c))
207 #define M_LLEA(a,b,c)           emit_lea_membase_reg(cd, (a), (b), (c))
208 #define M_ALEA(a,b,c)           M_LLEA(a,b,c)
209
210 #define M_INEG(a)               emit_negl_reg(cd, (a))
211 #define M_LNEG(a)               emit_neg_reg(cd, (a))
212
213 #define M_IAND(a,b)             emit_alul_reg_reg(cd, ALU_AND, (a), (b))
214 #define M_IOR(a,b)              emit_alul_reg_reg(cd, ALU_OR, (a), (b))
215 #define M_IXOR(a,b)             emit_alul_reg_reg(cd, ALU_XOR, (a), (b))
216
217 #define M_IAND_IMM(a,b)         emit_alul_imm_reg(cd, ALU_AND, (a), (b))
218 #define M_IOR_IMM(a,b)          emit_alul_imm_reg(cd, ALU_OR, (a), (b))
219 #define M_IXOR_IMM(a,b)         emit_alul_imm_reg(cd, ALU_XOR, (a), (b))
220
221 #define M_LAND(a,b)             emit_alu_reg_reg(cd, ALU_AND, (a), (b))
222 #define M_LOR(a,b)              emit_alu_reg_reg(cd, ALU_OR, (a), (b))
223 #define M_LXOR(a,b)             emit_alu_reg_reg(cd, ALU_XOR, (a), (b))
224
225 #define M_LAND_IMM(a,b)         emit_alu_imm_reg(cd, ALU_AND, (a), (b))
226 #define M_LOR_IMM(a,b)          emit_alu_imm_reg(cd, ALU_OR, (a), (b))
227 #define M_LXOR_IMM(a,b)         emit_alu_imm_reg(cd, ALU_XOR, (a), (b))
228
229 #define M_BSEXT(a,b)            emit_movsbq_reg_reg(cd, (a), (b))
230 #define M_SSEXT(a,b)            emit_movswq_reg_reg(cd, (a), (b))
231 #define M_ISEXT(a,b)            emit_movslq_reg_reg(cd, (a), (b))
232
233 #define M_CZEXT(a,b)            emit_movzwq_reg_reg(cd, (a), (b))
234
235 #define M_ISLL_IMM(a,b)         emit_shiftl_imm_reg(cd, SHIFT_SHL, (a), (b))
236 #define M_ISRA_IMM(a,b)         emit_shiftl_imm_reg(cd, SHIFT_SAR, (a), (b))
237 #define M_ISRL_IMM(a,b)         emit_shiftl_imm_reg(cd, SHIFT_SHR, (a), (b))
238
239 #define M_LSLL_IMM(a,b)         emit_shift_imm_reg(cd, SHIFT_SHL, (a), (b))
240 #define M_LSRA_IMM(a,b)         emit_shift_imm_reg(cd, SHIFT_SAR, (a), (b))
241 #define M_LSRL_IMM(a,b)         emit_shift_imm_reg(cd, SHIFT_SHR, (a), (b))
242
243 #define M_TEST(a)               emit_test_reg_reg(cd, (a), (a))
244 #define M_ITEST(a)              emit_testl_reg_reg(cd, (a), (a))
245
246 #define M_LCMP(a,b)             emit_alu_reg_reg(cd, ALU_CMP, (a), (b))
247 #define M_LCMP_IMM(a,b)         emit_alu_imm_reg(cd, ALU_CMP, (a), (b))
248 #define M_LCMP_IMM_MEMBASE(a,b,c) emit_alu_imm_membase(cd, ALU_CMP, (a), (b), (c))
249 #define M_LCMP_MEMBASE(a,b,c)   emit_alu_membase_reg(cd, ALU_CMP, (a), (b), (c))
250
251 #define M_ICMP(a,b)             emit_alul_reg_reg(cd, ALU_CMP, (a), (b))
252 #define M_ICMP_IMM(a,b)         emit_alul_imm_reg(cd, ALU_CMP, (a), (b))
253 #define M_ICMP_IMM_MEMBASE(a,b,c) emit_alul_imm_membase(cd, ALU_CMP, (a), (b), (c))
254 #define M_ICMP_MEMBASE(a,b,c)   emit_alul_membase_reg(cd, ALU_CMP, (a), (b), (c))
255
256 #define M_BEQ(disp)             emit_jcc(cd, CC_E, (disp))
257 #define M_BNE(disp)             emit_jcc(cd, CC_NE, (disp))
258 #define M_BLT(disp)             emit_jcc(cd, CC_L, (disp))
259 #define M_BLE(disp)             emit_jcc(cd, CC_LE, (disp))
260 #define M_BGE(disp)             emit_jcc(cd, CC_GE, (disp))
261 #define M_BGT(disp)             emit_jcc(cd, CC_G, (disp))
262 #define M_BAE(disp)             emit_jcc(cd, CC_AE, (disp))
263 #define M_BA(disp)              emit_jcc(cd, CC_A, (disp))
264
265 #define M_CMOVEQ(a,b)           emit_cmovcc_reg_reg(cd, CC_E, (a), (b))
266 #define M_CMOVNE(a,b)           emit_cmovcc_reg_reg(cd, CC_NE, (a), (b))
267 #define M_CMOVLT(a,b)           emit_cmovcc_reg_reg(cd, CC_L, (a), (b))
268 #define M_CMOVLE(a,b)           emit_cmovcc_reg_reg(cd, CC_LE, (a), (b))
269 #define M_CMOVGE(a,b)           emit_cmovcc_reg_reg(cd, CC_GE, (a), (b))
270 #define M_CMOVGT(a,b)           emit_cmovcc_reg_reg(cd, CC_G, (a), (b))
271
272 #define M_CMOVEQ_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_E, (a), (b))
273 #define M_CMOVNE_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_NE, (a), (b))
274 #define M_CMOVLT_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_L, (a), (b))
275 #define M_CMOVLE_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_LE, (a), (b))
276 #define M_CMOVGE_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_GE, (a), (b))
277 #define M_CMOVGT_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_G, (a), (b))
278
279 #define M_CMOVB(a,b)            emit_cmovcc_reg_reg(cd, CC_B, (a), (b))
280 #define M_CMOVA(a,b)            emit_cmovcc_reg_reg(cd, CC_A, (a), (b))
281 #define M_CMOVP(a,b)            emit_cmovcc_reg_reg(cd, CC_P, (a), (b))
282
283 #define M_PUSH(a)               emit_push_reg(cd, (a))
284 #define M_PUSH_IMM(a)           emit_push_imm(cd, (a))
285 #define M_POP(a)                emit_pop_reg(cd, (a))
286
287 #define M_JMP(a)                emit_jmp_reg(cd, (a))
288 #define M_JMP_IMM(a)            emit_jmp_imm(cd, (a))
289 #define M_CALL(a)               emit_call_reg(cd, (a))
290 #define M_CALL_IMM(a)           emit_call_imm(cd, (a))
291 #define M_RET                   emit_ret(cd)
292
293 #define M_NOP                   emit_nop(cd)
294
295 #define M_CLR(a)                M_LXOR(a,a)
296
297
298 #define M_FLD(a,b,disp)         emit_movss_membase_reg(cd, (b), (disp), (a))
299 #define M_DLD(a,b,disp)         emit_movsd_membase_reg(cd, (b), (disp), (a))
300
301 #define M_FLD32(a,b,disp)       emit_movss_membase32_reg(cd, (b), (disp), (a))
302 #define M_DLD32(a,b,disp)       emit_movsd_membase32_reg(cd, (b), (disp), (a))
303
304 #define M_FST(a,b,disp)         emit_movss_reg_membase(cd, (a), (b), (disp))
305 #define M_DST(a,b,disp)         emit_movsd_reg_membase(cd, (a), (b), (disp))
306
307 #define M_FST32(a,b,disp)       emit_movss_reg_membase32(cd, (a), (b), (disp))
308 #define M_DST32(a,b,disp)       emit_movsd_reg_membase32(cd, (a), (b), (disp))
309
310 #define M_FADD(a,b)             emit_addss_reg_reg(cd, (a), (b))
311 #define M_DADD(a,b)             emit_addsd_reg_reg(cd, (a), (b))
312 #define M_FSUB(a,b)             emit_subss_reg_reg(cd, (a), (b))
313 #define M_DSUB(a,b)             emit_subsd_reg_reg(cd, (a), (b))
314 #define M_FMUL(a,b)             emit_mulss_reg_reg(cd, (a), (b))
315 #define M_DMUL(a,b)             emit_mulsd_reg_reg(cd, (a), (b))
316 #define M_FDIV(a,b)             emit_divss_reg_reg(cd, (a), (b))
317 #define M_DDIV(a,b)             emit_divsd_reg_reg(cd, (a), (b))
318
319 #define M_CVTIF(a,b)            emit_cvtsi2ss_reg_reg(cd, (a), (b))
320 #define M_CVTID(a,b)            emit_cvtsi2sd_reg_reg(cd, (a), (b))
321 #define M_CVTLF(a,b)            emit_cvtsi2ssq_reg_reg(cd, (a), (b))
322 #define M_CVTLD(a,b)            emit_cvtsi2sdq_reg_reg(cd, (a), (b))
323 #define M_CVTFI(a,b)            emit_cvttss2si_reg_reg(cd, (a), (b))
324 #define M_CVTDI(a,b)            emit_cvttsd2si_reg_reg(cd, (a), (b))
325 #define M_CVTFL(a,b)            emit_cvttss2siq_reg_reg(cd, (a), (b))
326 #define M_CVTDL(a,b)            emit_cvttsd2siq_reg_reg(cd, (a), (b))
327
328 #define M_CVTFD(a,b)            emit_cvtss2sd_reg_reg(cd, (a), (b))
329 #define M_CVTDF(a,b)            emit_cvtsd2ss_reg_reg(cd, (a), (b))
330
331
332 /* system instructions ********************************************************/
333
334 #define M_RDTSC                 emit_rdtsc(cd)
335
336 #define M_IINC_MEMBASE(a,b)     emit_incl_membase(cd, (a), (b))
337
338 #define M_IADD_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_ADD, (a), (b), (c))
339 #define M_IADC_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_ADC, (a), (b), (c))
340 #define M_ISUB_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_SUB, (a), (b), (c))
341 #define M_ISBB_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_SBB, (a), (b), (c))
342
343
344 #if defined(ENABLE_PROFILING)
345
346 #define PROFILE_CYCLE_START \
347     do { \
348         if (JITDATA_HAS_FLAG_INSTRUMENT(jd)) { \
349             M_PUSH(RAX); \
350             M_PUSH(RDX); \
351             \
352             M_MOV_IMM(code, REG_ITMP3); \
353             M_RDTSC; \
354             M_ISUB_MEMBASE(RAX, REG_ITMP3, OFFSET(codeinfo, cycles)); \
355             M_ISBB_MEMBASE(RDX, REG_ITMP3, OFFSET(codeinfo, cycles) + 4); \
356             \
357             M_POP(RDX); \
358             M_POP(RAX); \
359         } \
360     } while (0)
361
362 #define PROFILE_CYCLE_STOP \
363     do { \
364         if (JITDATA_HAS_FLAG_INSTRUMENT(jd)) { \
365             M_PUSH(RAX); \
366             M_PUSH(RDX); \
367             \
368             M_MOV_IMM(code, REG_ITMP3); \
369             M_RDTSC; \
370             M_IADD_MEMBASE(RAX, REG_ITMP3, OFFSET(codeinfo, cycles)); \
371             M_IADC_MEMBASE(RDX, REG_ITMP3, OFFSET(codeinfo, cycles) + 4); \
372             \
373             M_POP(RDX); \
374             M_POP(RAX); \
375         } \
376     } while (0)
377
378 #else
379
380 #define PROFILE_CYCLE_START
381 #define PROFILE_CYCLE_STOP
382
383 #endif
384
385 #endif /* _CODEGEN_H */
386
387
388 /*
389  * These are local overrides for various environment variables in Emacs.
390  * Please do not remove this and leave it at the end of the file, where
391  * Emacs will automagically detect them.
392  * ---------------------------------------------------------------------
393  * Local variables:
394  * mode: c
395  * indent-tabs-mode: t
396  * c-basic-offset: 4
397  * tab-width: 4
398  * End:
399  */