414f2fcdbbdd47935d87ee8569dce508c4e34a77
[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 2218 2005-04-05 15:26:35Z christian $
31
32 */
33
34
35 #ifndef _CODEGEN_H
36 #define _CODEGEN_H
37
38 #include <ucontext.h>
39
40 /* Macro for stack.c to set Argument Stackslots */
41
42 #define SET_ARG_STACKSLOTS {                                    \
43                 copy = curstack;                                                \
44                 if (i > rd->ifmemuse)                                   \
45                         rd->ifmemuse = i;                                       \
46                 while (--i >= 0) {                                              \
47                         if (!(copy->flags & SAVEDVAR)) {        \
48                                 copy->varkind = ARGVAR;                 \
49                                 copy->varnum = i;                               \
50                                 copy->flags |= INMEMORY;                \
51                                 copy->regoff = i;                               \
52                         }                                                                       \
53                         copy = copy->prev;                                      \
54                 }                                                                               \
55         }                                                                                       \
56
57
58 /* additional functions and macros to generate code ***************************/
59
60 #define BlockPtrOfPC(pc)  ((basicblock *) iptr->target)
61
62
63 #ifdef STATISTICS
64 #define COUNT_SPILLS count_spills++
65 #else
66 #define COUNT_SPILLS
67 #endif
68
69
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;
74
75
76 #define CALCIMMEDIATEBYTES(var, val) \
77     if ((s4) (val) < -128 || (s4) (val) > 127) (var) += 4; \
78     else (var) += 1;
79
80
81 /* gen_nullptr_check(objreg) */
82
83 #define gen_nullptr_check(objreg) \
84         if (checknull) { \
85         i386_test_reg_reg(cd, (objreg), (objreg)); \
86         i386_jcc(cd, I386_CC_E, 0); \
87             codegen_addxnullrefs(cd, cd->mcodeptr); \
88         }
89
90 #define gen_bound_check \
91     if (checkbounds) { \
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); \
95     }
96
97 #define gen_div_check(v) \
98     if (checknull) { \
99         if ((v)->flags & INMEMORY) { \
100             i386_alu_imm_membase(cd, I386_CMP, 0, REG_SP, src->regoff * 8); \
101         } else { \
102             i386_test_reg_reg(cd, src->regoff, src->regoff); \
103         } \
104         i386_jcc(cd, I386_CC_E, 0); \
105         codegen_addxdivrefs(cd, cd->mcodeptr); \
106     }
107
108
109 /* MCODECHECK(icnt) */
110
111 #define MCODECHECK(icnt) \
112         if ((cd->mcodeptr + (icnt)) > (u1 *) cd->mcodeend) \
113         cd->mcodeptr = (u1 *) codegen_increase(cd, cd->mcodeptr)
114
115
116 /* XXX Do we need code padding on i386? */
117 /*  #define ALIGNCODENOP {if((int)((long)cd->mcodeptr&7)){M_NOP;}} */
118 #define ALIGNCODENOP
119
120
121 /* M_INTMOVE:
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.
124 */ 
125
126 #define M_INTMOVE(reg,dreg) \
127     if ((reg) != (dreg)) { \
128         i386_mov_reg_reg(cd, (reg),(dreg)); \
129     }
130
131
132 /* M_FLTMOVE:
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
135 */
136
137 #define M_FLTMOVE(reg,dreg) panic("M_FLTMOVE");
138
139 #define M_LNGMEMMOVE(reg,dreg) \
140     do { \
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); \
145     } while (0)
146
147
148 /* var_to_reg_xxx:
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.
154     
155     v: pseudoregister to be fetched from
156     tempregnum: temporary register to be used if v is actually spilled to ram
157
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)
161 */
162
163 #define var_to_reg_int(regnr,v,tempnr) \
164     if ((v)->flags & INMEMORY) { \
165         COUNT_SPILLS; \
166         i386_mov_membase_reg(cd, REG_SP, (v)->regoff * 8, tempnr); \
167         regnr = tempnr; \
168     } else { \
169         regnr = (v)->regoff; \
170     }
171
172
173
174 #define var_to_reg_flt(regnr,v,tempnr) \
175     if ((v)->type == TYPE_FLT) { \
176         if ((v)->flags & INMEMORY) { \
177             COUNT_SPILLS; \
178             i386_flds_membase(cd, REG_SP, (v)->regoff * 8); \
179             fpu_st_offset++; \
180             regnr = tempnr; \
181         } else { \
182             i386_fld_reg(cd, (v)->regoff + fpu_st_offset); \
183             fpu_st_offset++; \
184             regnr = (v)->regoff; \
185         } \
186     } else { \
187         if ((v)->flags & INMEMORY) { \
188             COUNT_SPILLS; \
189             i386_fldl_membase(cd, REG_SP, (v)->regoff * 8); \
190             fpu_st_offset++; \
191             regnr = tempnr; \
192         } else { \
193             i386_fld_reg(cd, (v)->regoff + fpu_st_offset); \
194             fpu_st_offset++; \
195             regnr = (v)->regoff; \
196         } \
197     }
198
199 #define NEW_var_to_reg_flt(regnr,v,tempnr) \
200     if ((v)->type == TYPE_FLT) { \
201        if ((v)->flags & INMEMORY) { \
202             COUNT_SPILLS; \
203             i386_flds_membase(cd, REG_SP, (v)->regoff * 8); \
204             fpu_st_offset++; \
205             regnr = tempnr; \
206         } else { \
207             regnr = (v)->regoff; \
208         } \
209     } else { \
210         if ((v)->flags & INMEMORY) { \
211             COUNT_SPILLS; \
212             i386_fldl_membase(cd, REG_SP, (v)->regoff * 8); \
213             fpu_st_offset++; \
214             regnr = tempnr; \
215         } else { \
216             regnr = (v)->regoff; \
217         } \
218     }
219
220
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.
226     
227     v ............ Pseudovariable
228     tempregnum ... Number of the temporary registers as returned by
229                    reg_of_var.
230 */      
231
232 #define store_reg_to_var_int(sptr, tempregnum) \
233     if ((sptr)->flags & INMEMORY) { \
234         COUNT_SPILLS; \
235         i386_mov_reg_membase(cd, tempregnum, REG_SP, (sptr)->regoff * 8); \
236     }
237
238
239 #define store_reg_to_var_flt(sptr, tempregnum) \
240     if ((sptr)->type == TYPE_FLT) { \
241         if ((sptr)->flags & INMEMORY) { \
242              COUNT_SPILLS; \
243              i386_fstps_membase(cd, REG_SP, (sptr)->regoff * 8); \
244              fpu_st_offset--; \
245         } else { \
246 /*                  i386_fxch_reg((sptr)->regoff);*/ \
247              i386_fstp_reg(cd, (sptr)->regoff + fpu_st_offset); \
248              fpu_st_offset--; \
249         } \
250     } else { \
251         if ((sptr)->flags & INMEMORY) { \
252             COUNT_SPILLS; \
253             i386_fstpl_membase(cd, REG_SP, (sptr)->regoff * 8); \
254             fpu_st_offset--; \
255         } else { \
256 /*                  i386_fxch_reg((sptr)->regoff);*/ \
257             i386_fstp_reg(cd, (sptr)->regoff + fpu_st_offset); \
258             fpu_st_offset--; \
259         } \
260     }
261
262
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); \
271                 } else { \
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); \
277                      } else { \
278                          i386_mov_reg_membase(cd, from->regoff, REG_SP, to->regoff * 8); \
279                      } \
280                 } else { \
281                      if (from->flags & INMEMORY) { \
282                          i386_mov_membase_reg(cd, REG_SP, from->regoff * 8, to->regoff); \
283                      } else { \
284                          i386_mov_reg_reg(cd, from->regoff, to->regoff); \
285                      } \
286                 } \
287             } else { \
288                 M_LNGMEMMOVE(from->regoff, to->regoff); \
289             } \
290                 } \
291         }
292
293 /* macros to create code ******************************************************/
294
295 typedef enum {
296     I386_AL = 0,
297     I386_CL = 1,
298     I386_DL = 2,
299     I386_BL = 3,
300     I386_AH = 4,
301     I386_CH = 5,
302     I386_DH = 6,
303     I386_BH = 7,
304     I386_NREGB
305 } I386_RegB_No;
306
307
308 /* opcodes for alu instructions */
309
310 typedef enum {
311     I386_ADD = 0,
312     I386_OR  = 1,
313     I386_ADC = 2,
314     I386_SBB = 3,
315     I386_AND = 4,
316     I386_SUB = 5,
317     I386_XOR = 6,
318     I386_CMP = 7,
319     I386_NALU
320 } I386_ALU_Opcode;
321
322 typedef enum {
323     I386_ROL = 0,
324     I386_ROR = 1,
325     I386_RCL = 2,
326     I386_RCR = 3,
327     I386_SHL = 4,
328     I386_SHR = 5,
329     I386_SAR = 7,
330     I386_NSHIFT = 8
331 } I386_Shift_Opcode;
332
333 typedef enum {
334     I386_CC_O = 0,
335     I386_CC_NO = 1,
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,
350     I386_NCC
351 } I386_CC;
352
353
354 /* modrm and stuff */
355
356 #define i386_address_byte(mod,reg,rm) \
357     *(cd->mcodeptr++) = ((((mod) & 0x03) << 6) | (((reg) & 0x07) << 3) | (((rm) & 0x07)));
358
359
360 #define i386_emit_reg(reg,rm) \
361     i386_address_byte(3,(reg),(rm));
362
363
364 #define i386_is_imm8(imm) \
365     (((int)(imm) >= -128 && (int)(imm) <= 127))
366
367
368 #define i386_emit_imm8(imm) \
369     *(cd->mcodeptr++) = (u1) ((imm) & 0xff);
370
371
372 #define i386_emit_imm16(imm) \
373     do { \
374         imm_union imb; \
375         imb.i = (int) (imm); \
376         *(cd->mcodeptr++) = imb.b[0]; \
377         *(cd->mcodeptr++) = imb.b[1]; \
378     } while (0)
379
380
381 #define i386_emit_imm32(imm) \
382     do { \
383         imm_union imb; \
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]; \
389     } while (0)
390
391
392 #define i386_emit_mem(r,mem) \
393     do { \
394         i386_address_byte(0,(r),5); \
395         i386_emit_imm32((mem)); \
396     } while (0)
397
398
399 #define i386_emit_membase(basereg,disp,dreg) \
400     do { \
401         if ((basereg) == ESP) { \
402             if ((disp) == 0) { \
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)); \
409             } else { \
410                 i386_address_byte(2, (dreg), ESP); \
411                 i386_address_byte(0, ESP, ESP); \
412                 i386_emit_imm32((disp)); \
413             } \
414             break; \
415         } \
416         \
417         if ((disp) == 0 && (basereg) != EBP) { \
418             i386_address_byte(0, (dreg), (basereg)); \
419             break; \
420         } \
421         \
422         if (i386_is_imm8((disp))) { \
423             i386_address_byte(1, (dreg), (basereg)); \
424             i386_emit_imm8((disp)); \
425         } else { \
426             i386_address_byte(2, (dreg), (basereg)); \
427             i386_emit_imm32((disp)); \
428         } \
429     } while (0)
430
431
432 #define i386_emit_memindex(reg,disp,basereg,indexreg,scale) \
433     do { \
434         if ((basereg) == -1) { \
435             i386_address_byte(0, (reg), 4); \
436             i386_address_byte((scale), (indexreg), 5); \
437             i386_emit_imm32((disp)); \
438         \
439         } else if ((disp) == 0 && (basereg) != EBP) { \
440             i386_address_byte(0, (reg), 4); \
441             i386_address_byte((scale), (indexreg), (basereg)); \
442         \
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)); \
447         \
448         } else { \
449             i386_address_byte(2, (reg), 4); \
450             i386_address_byte((scale), (indexreg), (basereg)); \
451             i386_emit_imm32((disp)); \
452         }    \
453      } while (0)
454
455
456 /* function gen_resolvebranch **************************************************
457
458     backpatches a branch instruction
459
460     parameters: ip ... pointer to instruction after branch (void*)
461                 so ... offset of instruction after branch  (s4)
462                 to ... offset of branch target             (s4)
463
464 *******************************************************************************/
465
466 #define gen_resolvebranch(ip,so,to) \
467     *((void **) ((ip) - 4)) = (void **) ((to) - (so));
468
469
470 /* function prototypes */
471
472 void thread_restartcriticalsection(ucontext_t *);
473
474 #endif /* _CODEGEN_H */
475
476
477 /*
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  * ---------------------------------------------------------------------
482  * Local variables:
483  * mode: c
484  * indent-tabs-mode: t
485  * c-basic-offset: 4
486  * tab-width: 4
487  * End:
488  */