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