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