GNU header update.
[cacao.git] / src / vm / jit / i386 / codegen.h
1 /* vm/jit/i386/codegen.h - code generation macros and definitions for i386
2
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
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Andreas Krall
28             Christian Thalinger
29
30    $Id: codegen.h 1735 2004-12-07 14:33:27Z twisti $
31
32 */
33
34
35 #ifndef _CODEGEN_H
36 #define _CODEGEN_H
37
38 #include <ucontext.h>
39
40
41 /* additional functions and macros to generate code ***************************/
42
43 #define BlockPtrOfPC(pc)  ((basicblock *) iptr->target)
44
45
46 #ifdef STATISTICS
47 #define COUNT_SPILLS count_spills++
48 #else
49 #define COUNT_SPILLS
50 #endif
51
52
53 #define CALCOFFSETBYTES(var, reg, val) \
54     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
55     else if ((s4) (val) != 0) (var) += 1; \
56     else if ((reg) == EBP) (var) += 1;
57
58
59 #define CALCIMMEDIATEBYTES(var, val) \
60     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
61     else (var) += 1;
62
63
64 /* gen_nullptr_check(objreg) */
65
66 #define gen_nullptr_check(objreg) \
67         if (checknull) { \
68         i386_test_reg_reg(cd, (objreg), (objreg)); \
69         i386_jcc(cd, I386_CC_E, 0); \
70             codegen_addxnullrefs(cd, cd->mcodeptr); \
71         }
72
73 #define gen_bound_check \
74     if (checkbounds) { \
75         i386_alu_membase_reg(cd, I386_CMP, s1, OFFSET(java_arrayheader, size), s2); \
76         i386_jcc(cd, I386_CC_AE, 0); \
77         codegen_addxboundrefs(cd, cd->mcodeptr, s2); \
78     }
79
80 #define gen_div_check(v) \
81     if (checknull) { \
82         if ((v)->flags & INMEMORY) { \
83             i386_alu_imm_membase(cd, I386_CMP, 0, REG_SP, src->regoff * 8); \
84         } else { \
85             i386_test_reg_reg(cd, src->regoff, src->regoff); \
86         } \
87         i386_jcc(cd, I386_CC_E, 0); \
88         codegen_addxdivrefs(cd, cd->mcodeptr); \
89     }
90
91
92 /* MCODECHECK(icnt) */
93
94 #define MCODECHECK(icnt) \
95         if ((cd->mcodeptr + (icnt)) > (u1 *) cd->mcodeend) \
96         cd->mcodeptr = (u1 *) codegen_increase(cd, cd->mcodeptr)
97
98
99 /* XXX Do we need code padding on i386? */
100 /*  #define ALIGNCODENOP {if((int)((long)cd->mcodeptr&7)){M_NOP;}} */
101 #define ALIGNCODENOP
102
103
104 /* M_INTMOVE:
105      generates an integer-move from register a to b.
106      if a and b are the same int-register, no code will be generated.
107 */ 
108
109 #define M_INTMOVE(reg,dreg) \
110     if ((reg) != (dreg)) { \
111         i386_mov_reg_reg(cd, (reg),(dreg)); \
112     }
113
114
115 /* M_FLTMOVE:
116     generates a floating-point-move from register a to b.
117     if a and b are the same float-register, no code will be generated
118 */
119
120 #define M_FLTMOVE(reg,dreg) panic("M_FLTMOVE");
121
122 #define M_LNGMEMMOVE(reg,dreg) \
123     do { \
124         i386_mov_membase_reg(cd, REG_SP, (reg) * 8, REG_ITMP1); \
125         i386_mov_reg_membase(cd, REG_ITMP1, REG_SP, (dreg) * 8); \
126         i386_mov_membase_reg(cd, REG_SP, (reg) * 8 + 4, REG_ITMP1); \
127         i386_mov_reg_membase(cd, REG_ITMP1, REG_SP, (dreg) * 8 + 4); \
128     } while (0)
129
130
131 /* var_to_reg_xxx:
132     this function generates code to fetch data from a pseudo-register
133     into a real register. 
134     If the pseudo-register has actually been assigned to a real 
135     register, no code will be emitted, since following operations
136     can use this register directly.
137     
138     v: pseudoregister to be fetched from
139     tempregnum: temporary register to be used if v is actually spilled to ram
140
141     return: the register number, where the operand can be found after 
142             fetching (this wil be either tempregnum or the register
143             number allready given to v)
144 */
145
146 #define var_to_reg_int(regnr,v,tempnr) \
147     if ((v)->flags & INMEMORY) { \
148         COUNT_SPILLS; \
149         i386_mov_membase_reg(cd, REG_SP, (v)->regoff * 8, tempnr); \
150         regnr = tempnr; \
151     } else { \
152         regnr = (v)->regoff; \
153     }
154
155
156
157 #define var_to_reg_flt(regnr,v,tempnr) \
158     if ((v)->type == TYPE_FLT) { \
159         if ((v)->flags & INMEMORY) { \
160             COUNT_SPILLS; \
161             i386_flds_membase(cd, REG_SP, (v)->regoff * 8); \
162             fpu_st_offset++; \
163             regnr = tempnr; \
164         } else { \
165             i386_fld_reg(cd, (v)->regoff + fpu_st_offset); \
166             fpu_st_offset++; \
167             regnr = (v)->regoff; \
168         } \
169     } else { \
170         if ((v)->flags & INMEMORY) { \
171             COUNT_SPILLS; \
172             i386_fldl_membase(cd, REG_SP, (v)->regoff * 8); \
173             fpu_st_offset++; \
174             regnr = tempnr; \
175         } else { \
176             i386_fld_reg(cd, (v)->regoff + fpu_st_offset); \
177             fpu_st_offset++; \
178             regnr = (v)->regoff; \
179         } \
180     }
181
182 #define NEW_var_to_reg_flt(regnr,v,tempnr) \
183     if ((v)->type == TYPE_FLT) { \
184        if ((v)->flags & INMEMORY) { \
185             COUNT_SPILLS; \
186             i386_flds_membase(cd, REG_SP, (v)->regoff * 8); \
187             fpu_st_offset++; \
188             regnr = tempnr; \
189         } else { \
190             regnr = (v)->regoff; \
191         } \
192     } else { \
193         if ((v)->flags & INMEMORY) { \
194             COUNT_SPILLS; \
195             i386_fldl_membase(cd, REG_SP, (v)->regoff * 8); \
196             fpu_st_offset++; \
197             regnr = tempnr; \
198         } else { \
199             regnr = (v)->regoff; \
200         } \
201     }
202
203
204 /* store_reg_to_var_xxx:
205     This function generates the code to store the result of an operation
206     back into a spilled pseudo-variable.
207     If the pseudo-variable has not been spilled in the first place, this 
208     function will generate nothing.
209     
210     v ............ Pseudovariable
211     tempregnum ... Number of the temporary registers as returned by
212                    reg_of_var.
213 */      
214
215 #define store_reg_to_var_int(sptr, tempregnum) \
216     if ((sptr)->flags & INMEMORY) { \
217         COUNT_SPILLS; \
218         i386_mov_reg_membase(cd, tempregnum, REG_SP, (sptr)->regoff * 8); \
219     }
220
221
222 #define store_reg_to_var_flt(sptr, tempregnum) \
223     if ((sptr)->type == TYPE_FLT) { \
224         if ((sptr)->flags & INMEMORY) { \
225              COUNT_SPILLS; \
226              i386_fstps_membase(cd, REG_SP, (sptr)->regoff * 8); \
227              fpu_st_offset--; \
228         } else { \
229 /*                  i386_fxch_reg((sptr)->regoff);*/ \
230              i386_fstp_reg(cd, (sptr)->regoff + fpu_st_offset); \
231              fpu_st_offset--; \
232         } \
233     } else { \
234         if ((sptr)->flags & INMEMORY) { \
235             COUNT_SPILLS; \
236             i386_fstpl_membase(cd, REG_SP, (sptr)->regoff * 8); \
237             fpu_st_offset--; \
238         } else { \
239 /*                  i386_fxch_reg((sptr)->regoff);*/ \
240             i386_fstp_reg(cd, (sptr)->regoff + fpu_st_offset); \
241             fpu_st_offset--; \
242         } \
243     }
244
245
246 #define M_COPY(from,to) \
247     d = reg_of_var(rd, to, REG_ITMP1); \
248         if ((from->regoff != to->regoff) || \
249             ((from->flags ^ to->flags) & INMEMORY)) { \
250                 if (IS_FLT_DBL_TYPE(from->type)) { \
251                         var_to_reg_flt(s1, from, d); \
252                         /*M_FLTMOVE(s1, d);*/ \
253                         store_reg_to_var_flt(to, d); \
254                 } else { \
255             if (!IS_2_WORD_TYPE(from->type)) { \
256                 if (to->flags & INMEMORY) { \
257                      if (from->flags & INMEMORY) { \
258                          i386_mov_membase_reg(cd, REG_SP, from->regoff * 8, REG_ITMP1); \
259                          i386_mov_reg_membase(cd, REG_ITMP1, REG_SP, to->regoff * 8); \
260                      } else { \
261                          i386_mov_reg_membase(cd, from->regoff, REG_SP, to->regoff * 8); \
262                      } \
263                 } else { \
264                      if (from->flags & INMEMORY) { \
265                          i386_mov_membase_reg(cd, REG_SP, from->regoff * 8, to->regoff); \
266                      } else { \
267                          i386_mov_reg_reg(cd, from->regoff, to->regoff); \
268                      } \
269                 } \
270             } else { \
271                 M_LNGMEMMOVE(from->regoff, to->regoff); \
272             } \
273                 } \
274         }
275
276 /* macros to create code ******************************************************/
277
278 typedef enum {
279     I386_AL = 0,
280     I386_CL = 1,
281     I386_DL = 2,
282     I386_BL = 3,
283     I386_AH = 4,
284     I386_CH = 5,
285     I386_DH = 6,
286     I386_BH = 7,
287     I386_NREGB
288 } I386_RegB_No;
289
290
291 /* opcodes for alu instructions */
292
293 typedef enum {
294     I386_ADD = 0,
295     I386_OR  = 1,
296     I386_ADC = 2,
297     I386_SBB = 3,
298     I386_AND = 4,
299     I386_SUB = 5,
300     I386_XOR = 6,
301     I386_CMP = 7,
302     I386_NALU
303 } I386_ALU_Opcode;
304
305 typedef enum {
306     I386_ROL = 0,
307     I386_ROR = 1,
308     I386_RCL = 2,
309     I386_RCR = 3,
310     I386_SHL = 4,
311     I386_SHR = 5,
312     I386_SAR = 7,
313     I386_NSHIFT = 8
314 } I386_Shift_Opcode;
315
316 typedef enum {
317     I386_CC_O = 0,
318     I386_CC_NO = 1,
319     I386_CC_B = 2, I386_CC_C = 2, I386_CC_NAE = 2,
320     I386_CC_BE = 6, I386_CC_NA = 6,
321     I386_CC_AE = 3, I386_CC_NB = 3, I386_CC_NC = 3,
322     I386_CC_E = 4, I386_CC_Z = 4,
323     I386_CC_NE = 5, I386_CC_NZ = 5,
324     I386_CC_A = 7, I386_CC_NBE = 7,
325     I386_CC_S = 8, I386_CC_LZ = 8,
326     I386_CC_NS = 9, I386_CC_GEZ = 9,
327     I386_CC_P = 0x0a, I386_CC_PE = 0x0a,
328     I386_CC_NP = 0x0b, I386_CC_PO = 0x0b,
329     I386_CC_L = 0x0c, I386_CC_NGE = 0x0c,
330     I386_CC_GE = 0x0d, I386_CC_NL = 0x0d,
331     I386_CC_LE = 0x0e, I386_CC_NG = 0x0e,
332     I386_CC_G = 0x0f, I386_CC_NLE = 0x0f,
333     I386_NCC
334 } I386_CC;
335
336
337 /* modrm and stuff */
338
339 #define i386_address_byte(mod,reg,rm) \
340     *(cd->mcodeptr++) = ((((mod) & 0x03) << 6) | (((reg) & 0x07) << 3) | (((rm) & 0x07)));
341
342
343 #define i386_emit_reg(reg,rm) \
344     i386_address_byte(3,(reg),(rm));
345
346
347 #define i386_is_imm8(imm) \
348     (((int)(imm) >= -128 && (int)(imm) <= 127))
349
350
351 #define i386_emit_imm8(imm) \
352     *(cd->mcodeptr++) = (u1) ((imm) & 0xff);
353
354
355 #define i386_emit_imm16(imm) \
356     do { \
357         imm_union imb; \
358         imb.i = (int) (imm); \
359         *(cd->mcodeptr++) = imb.b[0]; \
360         *(cd->mcodeptr++) = imb.b[1]; \
361     } while (0)
362
363
364 #define i386_emit_imm32(imm) \
365     do { \
366         imm_union imb; \
367         imb.i = (int) (imm); \
368         *(cd->mcodeptr++) = imb.b[0]; \
369         *(cd->mcodeptr++) = imb.b[1]; \
370         *(cd->mcodeptr++) = imb.b[2]; \
371         *(cd->mcodeptr++) = imb.b[3]; \
372     } while (0)
373
374
375 #define i386_emit_mem(r,mem) \
376     do { \
377         i386_address_byte(0,(r),5); \
378         i386_emit_imm32((mem)); \
379     } while (0)
380
381
382 #define i386_emit_membase(basereg,disp,dreg) \
383     do { \
384         if ((basereg) == ESP) { \
385             if ((disp) == 0) { \
386                 i386_address_byte(0, (dreg), ESP); \
387                 i386_address_byte(0, ESP, ESP); \
388             } else if (i386_is_imm8((disp))) { \
389                 i386_address_byte(1, (dreg), ESP); \
390                 i386_address_byte(0, ESP, ESP); \
391                 i386_emit_imm8((disp)); \
392             } else { \
393                 i386_address_byte(2, (dreg), ESP); \
394                 i386_address_byte(0, ESP, ESP); \
395                 i386_emit_imm32((disp)); \
396             } \
397             break; \
398         } \
399         \
400         if ((disp) == 0 && (basereg) != EBP) { \
401             i386_address_byte(0, (dreg), (basereg)); \
402             break; \
403         } \
404         \
405         if (i386_is_imm8((disp))) { \
406             i386_address_byte(1, (dreg), (basereg)); \
407             i386_emit_imm8((disp)); \
408         } else { \
409             i386_address_byte(2, (dreg), (basereg)); \
410             i386_emit_imm32((disp)); \
411         } \
412     } while (0)
413
414
415 #define i386_emit_memindex(reg,disp,basereg,indexreg,scale) \
416     do { \
417         if ((basereg) == -1) { \
418             i386_address_byte(0, (reg), 4); \
419             i386_address_byte((scale), (indexreg), 5); \
420             i386_emit_imm32((disp)); \
421         \
422         } else if ((disp) == 0 && (basereg) != EBP) { \
423             i386_address_byte(0, (reg), 4); \
424             i386_address_byte((scale), (indexreg), (basereg)); \
425         \
426         } else if (i386_is_imm8((disp))) { \
427             i386_address_byte(1, (reg), 4); \
428             i386_address_byte((scale), (indexreg), (basereg)); \
429             i386_emit_imm8 ((disp)); \
430         \
431         } else { \
432             i386_address_byte(2, (reg), 4); \
433             i386_address_byte((scale), (indexreg), (basereg)); \
434             i386_emit_imm32((disp)); \
435         }    \
436      } while (0)
437
438
439 /* function gen_resolvebranch **************************************************
440
441     backpatches a branch instruction
442
443     parameters: ip ... pointer to instruction after branch (void*)
444                 so ... offset of instruction after branch  (s4)
445                 to ... offset of branch target             (s4)
446
447 *******************************************************************************/
448
449 #define gen_resolvebranch(ip,so,to) \
450     *((void **) ((ip) - 4)) = (void **) ((to) - (so));
451
452
453 /* function prototypes */
454
455 void thread_restartcriticalsection(ucontext_t *);
456
457 #endif /* _CODEGEN_H */
458
459
460 /*
461  * These are local overrides for various environment variables in Emacs.
462  * Please do not remove this and leave it at the end of the file, where
463  * Emacs will automagically detect them.
464  * ---------------------------------------------------------------------
465  * Local variables:
466  * mode: c
467  * indent-tabs-mode: t
468  * c-basic-offset: 4
469  * tab-width: 4
470  * End:
471  */