1 /* vm/jit/i386/codegen.h - code generation macros and definitions for i386
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Andreas Krall
30 $Id: codegen.h 2218 2005-04-05 15:26:35Z christian $
40 /* Macro for stack.c to set Argument Stackslots */
42 #define SET_ARG_STACKSLOTS { \
44 if (i > rd->ifmemuse) \
47 if (!(copy->flags & SAVEDVAR)) { \
48 copy->varkind = ARGVAR; \
50 copy->flags |= INMEMORY; \
58 /* additional functions and macros to generate code ***************************/
60 #define BlockPtrOfPC(pc) ((basicblock *) iptr->target)
64 #define COUNT_SPILLS count_spills++
70 #define CALCOFFSETBYTES(var, reg, val) \
71 if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
72 else if ((s4) (val) != 0) (var) += 1; \
73 else if ((reg) == EBP) (var) += 1;
76 #define CALCIMMEDIATEBYTES(var, val) \
77 if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
81 /* gen_nullptr_check(objreg) */
83 #define gen_nullptr_check(objreg) \
85 i386_test_reg_reg(cd, (objreg), (objreg)); \
86 i386_jcc(cd, I386_CC_E, 0); \
87 codegen_addxnullrefs(cd, cd->mcodeptr); \
90 #define gen_bound_check \
92 i386_alu_membase_reg(cd, I386_CMP, s1, OFFSET(java_arrayheader, size), s2); \
93 i386_jcc(cd, I386_CC_AE, 0); \
94 codegen_addxboundrefs(cd, cd->mcodeptr, s2); \
97 #define gen_div_check(v) \
99 if ((v)->flags & INMEMORY) { \
100 i386_alu_imm_membase(cd, I386_CMP, 0, REG_SP, src->regoff * 8); \
102 i386_test_reg_reg(cd, src->regoff, src->regoff); \
104 i386_jcc(cd, I386_CC_E, 0); \
105 codegen_addxdivrefs(cd, cd->mcodeptr); \
109 /* MCODECHECK(icnt) */
111 #define MCODECHECK(icnt) \
112 if ((cd->mcodeptr + (icnt)) > (u1 *) cd->mcodeend) \
113 cd->mcodeptr = (u1 *) codegen_increase(cd, cd->mcodeptr)
116 /* XXX Do we need code padding on i386? */
117 /* #define ALIGNCODENOP {if((int)((long)cd->mcodeptr&7)){M_NOP;}} */
122 generates an integer-move from register a to b.
123 if a and b are the same int-register, no code will be generated.
126 #define M_INTMOVE(reg,dreg) \
127 if ((reg) != (dreg)) { \
128 i386_mov_reg_reg(cd, (reg),(dreg)); \
133 generates a floating-point-move from register a to b.
134 if a and b are the same float-register, no code will be generated
137 #define M_FLTMOVE(reg,dreg) panic("M_FLTMOVE");
139 #define M_LNGMEMMOVE(reg,dreg) \
141 i386_mov_membase_reg(cd, REG_SP, (reg) * 8, REG_ITMP1); \
142 i386_mov_reg_membase(cd, REG_ITMP1, REG_SP, (dreg) * 8); \
143 i386_mov_membase_reg(cd, REG_SP, (reg) * 8 + 4, REG_ITMP1); \
144 i386_mov_reg_membase(cd, REG_ITMP1, REG_SP, (dreg) * 8 + 4); \
149 this function generates code to fetch data from a pseudo-register
150 into a real register.
151 If the pseudo-register has actually been assigned to a real
152 register, no code will be emitted, since following operations
153 can use this register directly.
155 v: pseudoregister to be fetched from
156 tempregnum: temporary register to be used if v is actually spilled to ram
158 return: the register number, where the operand can be found after
159 fetching (this wil be either tempregnum or the register
160 number allready given to v)
163 #define var_to_reg_int(regnr,v,tempnr) \
164 if ((v)->flags & INMEMORY) { \
166 i386_mov_membase_reg(cd, REG_SP, (v)->regoff * 8, tempnr); \
169 regnr = (v)->regoff; \
174 #define var_to_reg_flt(regnr,v,tempnr) \
175 if ((v)->type == TYPE_FLT) { \
176 if ((v)->flags & INMEMORY) { \
178 i386_flds_membase(cd, REG_SP, (v)->regoff * 8); \
182 i386_fld_reg(cd, (v)->regoff + fpu_st_offset); \
184 regnr = (v)->regoff; \
187 if ((v)->flags & INMEMORY) { \
189 i386_fldl_membase(cd, REG_SP, (v)->regoff * 8); \
193 i386_fld_reg(cd, (v)->regoff + fpu_st_offset); \
195 regnr = (v)->regoff; \
199 #define NEW_var_to_reg_flt(regnr,v,tempnr) \
200 if ((v)->type == TYPE_FLT) { \
201 if ((v)->flags & INMEMORY) { \
203 i386_flds_membase(cd, REG_SP, (v)->regoff * 8); \
207 regnr = (v)->regoff; \
210 if ((v)->flags & INMEMORY) { \
212 i386_fldl_membase(cd, REG_SP, (v)->regoff * 8); \
216 regnr = (v)->regoff; \
221 /* store_reg_to_var_xxx:
222 This function generates the code to store the result of an operation
223 back into a spilled pseudo-variable.
224 If the pseudo-variable has not been spilled in the first place, this
225 function will generate nothing.
227 v ............ Pseudovariable
228 tempregnum ... Number of the temporary registers as returned by
232 #define store_reg_to_var_int(sptr, tempregnum) \
233 if ((sptr)->flags & INMEMORY) { \
235 i386_mov_reg_membase(cd, tempregnum, REG_SP, (sptr)->regoff * 8); \
239 #define store_reg_to_var_flt(sptr, tempregnum) \
240 if ((sptr)->type == TYPE_FLT) { \
241 if ((sptr)->flags & INMEMORY) { \
243 i386_fstps_membase(cd, REG_SP, (sptr)->regoff * 8); \
246 /* i386_fxch_reg((sptr)->regoff);*/ \
247 i386_fstp_reg(cd, (sptr)->regoff + fpu_st_offset); \
251 if ((sptr)->flags & INMEMORY) { \
253 i386_fstpl_membase(cd, REG_SP, (sptr)->regoff * 8); \
256 /* i386_fxch_reg((sptr)->regoff);*/ \
257 i386_fstp_reg(cd, (sptr)->regoff + fpu_st_offset); \
263 #define M_COPY(from,to) \
264 d = reg_of_var(rd, to, REG_ITMP1); \
265 if ((from->regoff != to->regoff) || \
266 ((from->flags ^ to->flags) & INMEMORY)) { \
267 if (IS_FLT_DBL_TYPE(from->type)) { \
268 var_to_reg_flt(s1, from, d); \
269 /*M_FLTMOVE(s1, d);*/ \
270 store_reg_to_var_flt(to, d); \
272 if (!IS_2_WORD_TYPE(from->type)) { \
273 if (to->flags & INMEMORY) { \
274 if (from->flags & INMEMORY) { \
275 i386_mov_membase_reg(cd, REG_SP, from->regoff * 8, REG_ITMP1); \
276 i386_mov_reg_membase(cd, REG_ITMP1, REG_SP, to->regoff * 8); \
278 i386_mov_reg_membase(cd, from->regoff, REG_SP, to->regoff * 8); \
281 if (from->flags & INMEMORY) { \
282 i386_mov_membase_reg(cd, REG_SP, from->regoff * 8, to->regoff); \
284 i386_mov_reg_reg(cd, from->regoff, to->regoff); \
288 M_LNGMEMMOVE(from->regoff, to->regoff); \
293 /* macros to create code ******************************************************/
308 /* opcodes for alu instructions */
336 I386_CC_B = 2, I386_CC_C = 2, I386_CC_NAE = 2,
337 I386_CC_BE = 6, I386_CC_NA = 6,
338 I386_CC_AE = 3, I386_CC_NB = 3, I386_CC_NC = 3,
339 I386_CC_E = 4, I386_CC_Z = 4,
340 I386_CC_NE = 5, I386_CC_NZ = 5,
341 I386_CC_A = 7, I386_CC_NBE = 7,
342 I386_CC_S = 8, I386_CC_LZ = 8,
343 I386_CC_NS = 9, I386_CC_GEZ = 9,
344 I386_CC_P = 0x0a, I386_CC_PE = 0x0a,
345 I386_CC_NP = 0x0b, I386_CC_PO = 0x0b,
346 I386_CC_L = 0x0c, I386_CC_NGE = 0x0c,
347 I386_CC_GE = 0x0d, I386_CC_NL = 0x0d,
348 I386_CC_LE = 0x0e, I386_CC_NG = 0x0e,
349 I386_CC_G = 0x0f, I386_CC_NLE = 0x0f,
354 /* modrm and stuff */
356 #define i386_address_byte(mod,reg,rm) \
357 *(cd->mcodeptr++) = ((((mod) & 0x03) << 6) | (((reg) & 0x07) << 3) | (((rm) & 0x07)));
360 #define i386_emit_reg(reg,rm) \
361 i386_address_byte(3,(reg),(rm));
364 #define i386_is_imm8(imm) \
365 (((int)(imm) >= -128 && (int)(imm) <= 127))
368 #define i386_emit_imm8(imm) \
369 *(cd->mcodeptr++) = (u1) ((imm) & 0xff);
372 #define i386_emit_imm16(imm) \
375 imb.i = (int) (imm); \
376 *(cd->mcodeptr++) = imb.b[0]; \
377 *(cd->mcodeptr++) = imb.b[1]; \
381 #define i386_emit_imm32(imm) \
384 imb.i = (int) (imm); \
385 *(cd->mcodeptr++) = imb.b[0]; \
386 *(cd->mcodeptr++) = imb.b[1]; \
387 *(cd->mcodeptr++) = imb.b[2]; \
388 *(cd->mcodeptr++) = imb.b[3]; \
392 #define i386_emit_mem(r,mem) \
394 i386_address_byte(0,(r),5); \
395 i386_emit_imm32((mem)); \
399 #define i386_emit_membase(basereg,disp,dreg) \
401 if ((basereg) == ESP) { \
403 i386_address_byte(0, (dreg), ESP); \
404 i386_address_byte(0, ESP, ESP); \
405 } else if (i386_is_imm8((disp))) { \
406 i386_address_byte(1, (dreg), ESP); \
407 i386_address_byte(0, ESP, ESP); \
408 i386_emit_imm8((disp)); \
410 i386_address_byte(2, (dreg), ESP); \
411 i386_address_byte(0, ESP, ESP); \
412 i386_emit_imm32((disp)); \
417 if ((disp) == 0 && (basereg) != EBP) { \
418 i386_address_byte(0, (dreg), (basereg)); \
422 if (i386_is_imm8((disp))) { \
423 i386_address_byte(1, (dreg), (basereg)); \
424 i386_emit_imm8((disp)); \
426 i386_address_byte(2, (dreg), (basereg)); \
427 i386_emit_imm32((disp)); \
432 #define i386_emit_memindex(reg,disp,basereg,indexreg,scale) \
434 if ((basereg) == -1) { \
435 i386_address_byte(0, (reg), 4); \
436 i386_address_byte((scale), (indexreg), 5); \
437 i386_emit_imm32((disp)); \
439 } else if ((disp) == 0 && (basereg) != EBP) { \
440 i386_address_byte(0, (reg), 4); \
441 i386_address_byte((scale), (indexreg), (basereg)); \
443 } else if (i386_is_imm8((disp))) { \
444 i386_address_byte(1, (reg), 4); \
445 i386_address_byte((scale), (indexreg), (basereg)); \
446 i386_emit_imm8 ((disp)); \
449 i386_address_byte(2, (reg), 4); \
450 i386_address_byte((scale), (indexreg), (basereg)); \
451 i386_emit_imm32((disp)); \
456 /* function gen_resolvebranch **************************************************
458 backpatches a branch instruction
460 parameters: ip ... pointer to instruction after branch (void*)
461 so ... offset of instruction after branch (s4)
462 to ... offset of branch target (s4)
464 *******************************************************************************/
466 #define gen_resolvebranch(ip,so,to) \
467 *((void **) ((ip) - 4)) = (void **) ((to) - (so));
470 /* function prototypes */
472 void thread_restartcriticalsection(ucontext_t *);
474 #endif /* _CODEGEN_H */
478 * These are local overrides for various environment variables in Emacs.
479 * Please do not remove this and leave it at the end of the file, where
480 * Emacs will automagically detect them.
481 * ---------------------------------------------------------------------
484 * indent-tabs-mode: t