* src/vm/jit/x86_64/emit.c (emit_movl_reg_reg): New function.
[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    Changes:
31
32    $Id: codegen.h 4941 2006-05-23 08:25:14Z twisti $
33
34 */
35
36
37 #ifndef _CODEGEN_H
38 #define _CODEGEN_H
39
40 #include "config.h"
41
42 #include <ucontext.h>
43
44 #include "vm/types.h"
45
46 #include "vm/jit/jit.h"
47
48
49 /* some defines ***************************************************************/
50
51 #define PATCHER_CALL_SIZE    5          /* size in bytes of a patcher call    */
52
53
54 /* additional functions and macros to generate code ***************************/
55
56 #define CALCOFFSETBYTES(var, reg, val) \
57     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
58     else if ((s4) (val) != 0) (var) += 1; \
59     else if ((reg) == RBP || (reg) == RSP || (reg) == R12 || (reg) == R13) (var) += 1;
60
61
62 #define CALCIMMEDIATEBYTES(var, val) \
63     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
64     else (var) += 1;
65
66
67 /* gen_nullptr_check(objreg) */
68
69 #define gen_nullptr_check(objreg) \
70         if (checknull) { \
71         M_TEST(objreg); \
72         M_BEQ(0); \
73             codegen_add_nullpointerexception_ref(cd); \
74         }
75
76
77 #define gen_bound_check \
78     if (checkbounds) { \
79         M_ICMP_MEMBASE(s1, OFFSET(java_arrayheader, size), s2); \
80         M_BAE(0); \
81         codegen_add_arrayindexoutofboundsexception_ref(cd, s2); \
82     }
83
84
85 #define gen_div_check(v) \
86     if (checknull) { \
87         if ((v)->flags & INMEMORY) { \
88             M_CMP_IMM_MEMBASE(0, REG_SP, src->regoff * 8); \
89         } else { \
90             M_TEST(src->regoff); \
91         } \
92         M_BEQ(0); \
93         codegen_add_arithmeticexception_ref(cd); \
94     }
95
96
97 /* MCODECHECK(icnt) */
98
99 #define MCODECHECK(icnt) \
100     do { \
101         if ((cd->mcodeptr + (icnt)) > cd->mcodeend) \
102             codegen_increase(cd); \
103     } while (0)
104
105
106 #define ALIGNCODENOP \
107     if ((s4) (((ptrint) cd->mcodeptr) & 7)) { \
108         M_NOP; \
109     }
110
111
112 /* M_INTMOVE:
113     generates an integer-move from register a to b.
114     if a and b are the same int-register, no code will be generated.
115 */ 
116
117 #define M_INTMOVE(reg,dreg) \
118     do { \
119         if ((reg) != (dreg)) { \
120             M_MOV(reg, dreg); \
121         } \
122     } while (0)
123
124
125 /* M_FLTMOVE:
126     generates a floating-point-move from register a to b.
127     if a and b are the same float-register, no code will be generated
128 */ 
129
130 #define M_FLTMOVE(reg,dreg) \
131     do { \
132         if ((reg) != (dreg)) { \
133             M_FMOV(reg, dreg); \
134         } \
135     } while (0)
136
137
138 #define M_COPY(s,d)                     emit_copy(jd, iptr, (s), (d))
139
140 #define ICONST(r,c) \
141     do { \
142         if (iptr->val.i == 0) \
143             M_CLR(d); \
144         else \
145             M_IMOV_IMM(iptr->val.i, d); \
146     } while (0)
147 /*     do { \ */
148 /*        M_IMOV_IMM(iptr->val.i, d); \ */
149 /*     } while (0) */
150
151
152 #define LCONST(r,c) \
153     do { \
154         if (iptr->val.l == 0) \
155             M_CLR(d); \
156         else \
157             M_MOV_IMM(iptr->val.l, d); \
158     } while (0)
159
160
161 /* macros to create code ******************************************************/
162
163 #define M_MOV(a,b)              emit_mov_reg_reg(cd, (a), (b))
164 #define M_MOV_IMM(a,b)          emit_mov_imm_reg(cd, (u8) (a), (b))
165
166 #define M_IMOV(a,b)             emit_movl_reg_reg(cd, (a), (b))
167 #define M_IMOV_IMM(a,b)         emit_movl_imm_reg(cd, (u4) (a), (b))
168
169 #define M_FMOV(a,b)             emit_movq_reg_reg(cd, (a), (b))
170
171 #define M_ILD(a,b,disp)         emit_movl_membase_reg(cd, (b), (disp), (a))
172 #define M_LLD(a,b,disp)         emit_mov_membase_reg(cd, (b), (disp), (a))
173
174 #define M_ILD32(a,b,disp)       emit_movl_membase32_reg(cd, (b), (disp), (a))
175 #define M_LLD32(a,b,disp)       emit_mov_membase32_reg(cd, (b), (disp), (a))
176
177 #define M_IST(a,b,disp)         emit_movl_reg_membase(cd, (a), (b), (disp))
178 #define M_LST(a,b,disp)         emit_mov_reg_membase(cd, (a), (b), (disp))
179
180 #define M_IST_IMM(a,b,disp)     emit_movl_imm_membase(cd, (a), (b), (disp))
181 #define M_LST_IMM32(a,b,disp)   emit_mov_imm_membase(cd, (a), (b), (disp))
182
183 #define M_IST32(a,b,disp)       emit_movl_reg_membase32(cd, (a), (b), (disp))
184 #define M_LST32(a,b,disp)       emit_mov_reg_membase32(cd, (a), (b), (disp))
185
186 #define M_IST32_IMM(a,b,disp)   emit_movl_imm_membase32(cd, (a), (b), (disp))
187 #define M_LST32_IMM32(a,b,disp) emit_mov_imm_membase32(cd, (a), (b), (disp))
188
189 #define M_IADD(a,b)             emit_alul_reg_reg(cd, ALU_ADD, (a), (b))
190 #define M_IADD_IMM(a,b)         emit_alul_reg_reg(cd, ALU_ADD, (a), (b))
191
192 #define M_LADD(a,b)             emit_alu_reg_reg(cd, ALU_ADD, (a), (b))
193 #define M_LADD_IMM(a,b)         emit_alu_imm_reg(cd, ALU_ADD, (a), (b))
194 #define M_LSUB(a,b)             emit_alu_reg_reg(cd, ALU_SUB, (a), (b))
195 #define M_LSUB_IMM(a,b)         emit_alu_imm_reg(cd, ALU_SUB, (a), (b))
196
197 #define M_IINC_MEMBASE(a,b)     emit_incl_membase(cd, (a), (b))
198
199 #define M_IADD_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_ADD, (a), (b), (c))
200 #define M_IADC_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_ADC, (a), (b), (c))
201 #define M_ISUB_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_SUB, (a), (b), (c))
202 #define M_ISBB_MEMBASE(a,b,c)   emit_alul_reg_membase(cd, ALU_SBB, (a), (b), (c))
203
204 #define M_ALD(a,b,disp)         M_LLD(a,b,disp)
205 #define M_ALD32(a,b,disp)       M_LLD32(a,b,disp)
206
207 #define M_AST(a,b,c)            M_LST(a,b,c)
208 #define M_AST_IMM32(a,b,c)      M_LST_IMM32(a,b,c)
209
210 #define M_AADD(a,b)             M_LADD(a,b)
211 #define M_AADD_IMM(a,b)         M_LADD_IMM(a,b)
212 #define M_ASUB_IMM(a,b)         M_LSUB_IMM(a,b)
213
214 #define M_LADD_IMM32(a,b)       emit_alu_imm32_reg(cd, ALU_ADD, (a), (b))
215 #define M_AADD_IMM32(a,b)       M_LADD_IMM32(a,b)
216 #define M_LSUB_IMM32(a,b)       emit_alu_imm32_reg(cd, ALU_SUB, (a), (b))
217
218 #define M_ILEA(a,b,c)           emit_leal_membase_reg(cd, (a), (b), (c))
219 #define M_LLEA(a,b,c)           emit_lea_membase_reg(cd, (a), (b), (c))
220 #define M_ALEA(a,b,c)           M_LLEA(a,b,c)
221
222 #define M_INEG(a)               emit_negl_reg(cd, (a))
223 #define M_LNEG(a)               emit_neg_reg(cd, (a))
224
225 #define M_INEG_MEMBASE(a,b)     emit_negl_membase(cd, (a), (b))
226 #define M_LNEG_MEMBASE(a,b)     emit_neg_membase(cd, (a), (b))
227
228 #define M_AND(a,b)              emit_alu_reg_reg(cd, ALU_AND, (a), (b))
229 #define M_XOR(a,b)              emit_alu_reg_reg(cd, ALU_XOR, (a), (b))
230
231 #define M_IAND(a,b)             emit_alul_reg_reg(cd, ALU_AND, (a), (b))
232 #define M_IAND_IMM(a,b)         emit_alul_imm_reg(cd, ALU_AND, (a), (b))
233 #define M_IXOR(a,b)             emit_alul_reg_reg(cd, ALU_XOR, (a), (b))
234
235 #define M_BSEXT(a,b)            emit_movsbq_reg_reg(cd, (a), (b))
236 #define M_SSEXT(a,b)            emit_movswq_reg_reg(cd, (a), (b))
237 #define M_ISEXT(a,b)            emit_movslq_reg_reg(cd, (a), (b))
238
239 #define M_CZEXT(a,b)            emit_movzwq_reg_reg(cd, (a), (b))
240
241 #define M_BSEXT_MEMBASE(a,disp,b) emit_movsbq_membase_reg(cd, (a), (disp), (b))
242 #define M_SSEXT_MEMBASE(a,disp,b) emit_movswq_membase_reg(cd, (a), (disp), (b))
243 #define M_ISEXT_MEMBASE(a,disp,b) emit_movslq_membase_reg(cd, (a), (disp), (b))
244
245 #define M_CZEXT_MEMBASE(a,disp,b) emit_movzwq_membase_reg(cd, (a), (disp), (b))
246
247 #define M_TEST(a)               emit_test_reg_reg(cd, (a), (a))
248 #define M_ITEST(a)              emit_testl_reg_reg(cd, (a), (a))
249
250 #define M_CMP(a,b)              emit_alu_reg_reg(cd, ALU_CMP, (a), (b))
251 #define M_CMP_IMM(a,b)          emit_alu_imm_reg(cd, ALU_CMP, (a), (b))
252 #define M_CMP_IMM_MEMBASE(a,b,c) emit_alu_imm_membase(cd, ALU_CMP, (a), (b), (c))
253 #define M_CMP_MEMBASE(a,b,c)    emit_alu_membase_reg(cd, ALU_CMP, (a), (b), (c))
254
255 #define M_ICMP(a,b)             emit_alul_reg_reg(cd, ALU_CMP, (a), (b))
256 #define M_ICMP_IMM(a,b)         emit_alul_imm_reg(cd, ALU_CMP, (a), (b))
257 #define M_ICMP_IMM_MEMBASE(a,b,c) emit_alul_imm_membase(cd, ALU_CMP, (a), (b), (c))
258 #define M_ICMP_MEMBASE(a,b,c)   emit_alul_membase_reg(cd, ALU_CMP, (a), (b), (c))
259
260 #define M_BEQ(disp)             emit_jcc(cd, CC_E, (disp))
261 #define M_BNE(disp)             emit_jcc(cd, CC_NE, (disp))
262 #define M_BLT(disp)             emit_jcc(cd, CC_L, (disp))
263 #define M_BLE(disp)             emit_jcc(cd, CC_LE, (disp))
264 #define M_BAE(disp)             emit_jcc(cd, CC_AE, (disp))
265 #define M_BA(disp)              emit_jcc(cd, CC_A, (disp))
266
267 #define M_CMOVEQ(a,b)           emit_cmovcc_reg_reg(cd, CC_E, (a), (b))
268 #define M_CMOVNE(a,b)           emit_cmovcc_reg_reg(cd, CC_NE, (a), (b))
269 #define M_CMOVLT(a,b)           emit_cmovcc_reg_reg(cd, CC_L, (a), (b))
270 #define M_CMOVLE(a,b)           emit_cmovcc_reg_reg(cd, CC_LE, (a), (b))
271 #define M_CMOVGE(a,b)           emit_cmovcc_reg_reg(cd, CC_GE, (a), (b))
272 #define M_CMOVGT(a,b)           emit_cmovcc_reg_reg(cd, CC_G, (a), (b))
273
274 #define M_CMOVEQ_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_E, (a), (b))
275 #define M_CMOVNE_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_NE, (a), (b))
276 #define M_CMOVLT_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_L, (a), (b))
277 #define M_CMOVLE_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_LE, (a), (b))
278 #define M_CMOVGE_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_GE, (a), (b))
279 #define M_CMOVGT_MEMBASE(a,b,c) emit_cmovcc_reg_membase(cd, CC_G, (a), (b))
280
281 #define M_CMOVB(a,b)            emit_cmovcc_reg_reg(cd, CC_B, (a), (b))
282 #define M_CMOVA(a,b)            emit_cmovcc_reg_reg(cd, CC_A, (a), (b))
283 #define M_CMOVP(a,b)            emit_cmovcc_reg_reg(cd, CC_P, (a), (b))
284
285 #define M_PUSH(a)               emit_push_reg(cd, (a))
286 #define M_PUSH_IMM(a)           emit_push_imm(cd, (a))
287 #define M_POP(a)                emit_pop_reg(cd, (a))
288
289 #define M_JMP(a)                emit_jmp_reg(cd, (a))
290 #define M_JMP_IMM(a)            emit_jmp_imm(cd, (a))
291 #define M_CALL(a)               emit_call_reg(cd, (a))
292 #define M_CALL_IMM(a)           emit_call_imm(cd, (a))
293 #define M_RET                   emit_ret(cd)
294
295 #define M_NOP                   emit_nop(cd)
296
297 #define M_CLR(a)                M_XOR(a,a)
298
299
300 #if 0
301 #define M_FLD(a,b,c)            emit_movlps_membase_reg(cd, (a), (b), (c))
302 #define M_DLD(a,b,c)            emit_movlpd_membase_reg(cd, (a), (b), (c))
303
304 #define M_FST(a,b,c)            emit_movlps_reg_membase(cd, (a), (b), (c))
305 #define M_DST(a,b,c)            emit_movlpd_reg_membase(cd, (a), (b), (c))
306 #endif
307
308 #define M_DLD(a,b,disp)         emit_movq_membase_reg(cd, (b), (disp), (a))
309 #define M_DST(a,b,disp)         emit_movq_reg_membase(cd, (a), (b), (disp))
310
311
312 /* system instructions ********************************************************/
313
314 #define M_RDTSC                 emit_rdtsc(cd)
315
316 #define PROFILE_CYCLE_START \
317     do { \
318         if (opt_prof) { \
319             M_PUSH(RAX); \
320             M_PUSH(RDX); \
321             \
322             M_MOV_IMM((ptrint) m, REG_ITMP3); \
323             M_RDTSC; \
324             M_ISUB_MEMBASE(RAX, REG_ITMP3, OFFSET(methodinfo, cycles)); \
325             M_ISBB_MEMBASE(RDX, REG_ITMP3, OFFSET(methodinfo, cycles) + 4); \
326             \
327             M_POP(RDX); \
328             M_POP(RAX); \
329         } \
330     } while (0)
331
332 #define PROFILE_CYCLE_STOP \
333     do { \
334         if (opt_prof) { \
335             M_PUSH(RAX); \
336             M_PUSH(RDX); \
337             \
338             M_MOV_IMM((ptrint) m, REG_ITMP3); \
339             M_RDTSC; \
340             M_IADD_MEMBASE(RAX, REG_ITMP3, OFFSET(methodinfo, cycles)); \
341             M_IADC_MEMBASE(RDX, REG_ITMP3, OFFSET(methodinfo, cycles) + 4); \
342             \
343             M_POP(RDX); \
344             M_POP(RAX); \
345         } \
346     } while (0)
347
348
349 /* function gen_resolvebranch **************************************************
350
351     backpatches a branch instruction
352
353     parameters: ip ... pointer to instruction after branch (void*)
354                 so ... offset of instruction after branch  (s8)
355                 to ... offset of branch target             (s8)
356
357 *******************************************************************************/
358
359 #define gen_resolvebranch(ip,so,to) \
360     *((s4*) ((ip) - 4)) = (s4) ((to) - (so));
361
362 #endif /* _CODEGEN_H */
363
364
365 /*
366  * These are local overrides for various environment variables in Emacs.
367  * Please do not remove this and leave it at the end of the file, where
368  * Emacs will automagically detect them.
369  * ---------------------------------------------------------------------
370  * Local variables:
371  * mode: c
372  * indent-tabs-mode: t
373  * c-basic-offset: 4
374  * tab-width: 4
375  * End:
376  */