Initial revision
[cacao.git] / alpha / ngen.h
1 /***************************** alpha/ngen.h ************************************
2
3         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
4
5         See file COPYRIGHT for information on usage and disclaimer of warranties
6
7         Contains the machine dependent code generator definitions and macros for an
8         Alpha processor.
9
10         Authors: Andreas  Krall      EMAIL: cacao@complang.tuwien.ac.at
11                  Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
12
13         Last Change: 1998/08/10
14
15 *******************************************************************************/
16
17 /************************ Preallocated registers ******************************/
18
19 /* integer registers */
20   
21 #define REG_RESULT      0    /* to deliver method results                     */ 
22 #define REG_EXCEPTION   1    /* to throw an exception across method bounds    */
23
24 #define REG_RA          26   /* return address                                */
25 #define REG_PV          27   /* procedure vector, must be provided by caller  */
26 #define REG_METHODPTR   28   /* pointer to the place from where the procedure */
27                              /* vector has been fetched                       */
28 #define REG_ITMP1       25   /* temporary register                            */
29 #define REG_ITMP2       28   /* temporary register                            */
30 #define REG_ITMP3       29   /* temporary register                            */
31
32 #define REG_ITMP1_XPTR  25   /* exception pointer = temporary register 1      */
33 #define REG_ITMP2_XPC   28   /* exception pc = temporary register 2           */
34
35 #define REG_SP          30   /* stack pointer                                 */
36 #define REG_ZERO        31   /* allways zero                                  */
37
38 /* floating point registers */
39
40 #define REG_FRESULT     0    /* to deliver floating point method results      */ 
41 #define REG_FTMP1       28   /* temporary floating point register             */
42 #define REG_FTMP2       29   /* temporary floating point register             */
43 #define REG_FTMP3       30   /* temporary floating point register             */
44
45 #define REG_IFTMP       28   /* temporary integer and floating point register */
46
47 /******************** register descripton - array *****************************/
48
49 /* #define REG_RES   0         reserved register for OS or code generator */
50 /* #define REG_RET   1         return value register */
51 /* #define REG_EXC   2         exception value register */
52 /* #define REG_SAV   3         (callee) saved register */
53 /* #define REG_TMP   4         scratch temporary register (caller saved) */
54 /* #define REG_ARG   5         argument register (caller saved) */
55
56 /* #define REG_END   -1        last entry in tables */
57  
58 int nregdescint[] = {
59         REG_RET, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, 
60         REG_TMP, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, 
61         REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_TMP, REG_TMP,
62         REG_TMP, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES, REG_RES,
63         REG_END };
64
65 #define INT_SAV_CNT      7   /* number of int callee saved registers          */
66 #define INT_ARG_CNT      6   /* number of int argument registers              */
67
68 /* for use of reserved registers, see comment above */
69         
70 int nregdescfloat[] = {
71         REG_RET, REG_TMP, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV, REG_SAV,
72         REG_SAV, REG_SAV, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_TMP, 
73         REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_ARG, REG_TMP, REG_TMP,
74         REG_TMP, REG_TMP, REG_TMP, REG_TMP, REG_RES, REG_RES, REG_RES, REG_RES,
75         REG_END };
76
77 #define FLT_SAV_CNT      8   /* number of flt callee saved registers          */
78 #define FLT_ARG_CNT      6   /* number of flt argument registers              */
79
80 /* for use of reserved registers, see comment above */
81
82
83 /*** parameter allocation mode ***/
84
85 int nreg_parammode = PARAMMODE_NUMBERED;  
86
87    /* parameter-registers will be allocated by assigning the
88       1. parameter:   int/float-reg 16
89       2. parameter:   int/float-reg 17  
90       3. parameter:   int/float-reg 18 ....
91    */
92
93
94 /************************** stackframe-infos **********************************/
95
96 int parentargs_base; /* offset in stackframe for the parameter from the caller*/
97
98 /* -> see file 'calling.doc' */
99
100
101 /******************** macros to create code ***********************************/
102
103 /* 3-address-operations: M_OP3
104       op ..... opcode
105       fu ..... function-number
106       a  ..... register number source 1
107       b  ..... register number or constant integer source 2
108       c  ..... register number destination
109       const .. switch to use b as constant integer 
110                  (0 means: use b as register number)
111                  (1 means: use b as constant 8-bit-integer)
112 */      
113 #define M_OP3(op,fu,a,b,c,const) \
114         *(mcodeptr++) = ( (((s4)(op))<<26)|((a)<<21)|((b)<<(16-3*(const)))| \
115         ((const)<<12)|((fu)<<5)|((c)) )
116
117 /* 3-address-floating-point-operation: M_FOP3 
118      op .... opcode
119      fu .... function-number
120      a,b ... source floating-point registers
121      c ..... destination register
122 */ 
123 #define M_FOP3(op,fu,a,b,c) \
124         *(mcodeptr++) = ( (((s4)(op))<<26)|((a)<<21)|((b)<<16)|((fu)<<5)|(c) )
125
126 /* branch instructions: M_BRA 
127       op ..... opcode
128       a ...... register to be tested
129       disp ... relative address to be jumped to (divided by 4)
130 */
131 #define M_BRA(op,a,disp) \
132         *(mcodeptr++) = ( (((s4)(op))<<26)|((a)<<21)|((disp)&0x1fffff) )
133
134
135 /* memory operations: M_MEM
136       op ..... opcode
137       a ...... source/target register for memory access
138       b ...... base register
139       disp ... displacement (16 bit signed) to be added to b
140 */ 
141 #define M_MEM(op,a,b,disp) \
142         *(mcodeptr++) = ( (((s4)(op))<<26)|((a)<<21)|((b)<<16)|((disp)&0xffff) )
143
144
145 /***** macros for all used commands (see an Alpha-manual for description) *****/ 
146
147 #define M_LDA(a,b,disp)         M_MEM (0x08,a,b,disp)           /* low const  */
148 #define M_LDAH(a,b,disp)        M_MEM (0x09,a,b,disp)           /* high const */
149 #define M_BLDU(a,b,disp)        M_MEM (0x0a,a,b,disp)           /*  8 load    */
150 #define M_SLDU(a,b,disp)        M_MEM (0x0c,a,b,disp)           /* 16 load    */
151 #define M_ILD(a,b,disp)         M_MEM (0x28,a,b,disp)           /* 32 load    */
152 #define M_LLD(a,b,disp)         M_MEM (0x29,a,b,disp)           /* 64 load    */
153 #define M_BST(a,b,disp)         M_MEM (0x0e,a,b,disp)           /*  8 store   */
154 #define M_SST(a,b,disp)         M_MEM (0x0d,a,b,disp)           /* 16 store   */
155 #define M_IST(a,b,disp)         M_MEM (0x2c,a,b,disp)           /* 32 store   */
156 #define M_LST(a,b,disp)         M_MEM (0x2d,a,b,disp)           /* 64 store   */
157
158 #define M_BSEXT(b,c)            M_OP3 (0x1c,0x0,REG_ZERO,b,c,0) /*  8 signext */
159 #define M_SSEXT(b,c)            M_OP3 (0x1c,0x1,REG_ZERO,b,c,0) /* 16 signext */
160
161 #define M_BR(disp)              M_BRA (0x30,REG_ZERO,disp)      /* branch     */
162 #define M_BSR(ra,disp)          M_BRA (0x34,ra,disp)            /* branch sbr */
163 #define M_BEQZ(a,disp)          M_BRA (0x39,a,disp)             /* br a == 0  */
164 #define M_BLTZ(a,disp)          M_BRA (0x3a,a,disp)             /* br a <  0  */
165 #define M_BLEZ(a,disp)          M_BRA (0x3b,a,disp)             /* br a <= 0  */
166 #define M_BNEZ(a,disp)          M_BRA (0x3d,a,disp)             /* br a != 0  */
167 #define M_BGEZ(a,disp)          M_BRA (0x3e,a,disp)             /* br a >= 0  */
168 #define M_BGTZ(a,disp)          M_BRA (0x3f,a,disp)             /* br a >  0  */
169
170 #define M_JMP(a,b)              M_MEM (0x1a,a,b,0x0000)         /* jump       */
171 #define M_JSR(a,b)              M_MEM (0x1a,a,b,0x4000)         /* call sbr   */
172 #define M_RET(a,b)              M_MEM (0x1a,a,b,0x8000)         /* return     */
173
174 #define M_IADD(a,b,c,const)     M_OP3 (0x10,0x0,  a,b,c,const)  /* 32 add     */
175 #define M_LADD(a,b,c,const)     M_OP3 (0x10,0x20, a,b,c,const)  /* 64 add     */
176 #define M_ISUB(a,b,c,const)     M_OP3 (0x10,0x09, a,b,c,const)  /* 32 sub     */
177 #define M_LSUB(a,b,c,const)     M_OP3 (0x10,0x29, a,b,c,const)  /* 64 sub     */
178 #define M_IMUL(a,b,c,const)     M_OP3 (0x13,0x00, a,b,c,const)  /* 32 mul     */
179 #define M_LMUL(a,b,c,const)     M_OP3 (0x13,0x20, a,b,c,const)  /* 64 mul     */
180
181 #define M_CMPEQ(a,b,c,const)    M_OP3 (0x10,0x2d, a,b,c,const)  /* c = a == b */
182 #define M_CMPLT(a,b,c,const)    M_OP3 (0x10,0x4d, a,b,c,const)  /* c = a <  b */
183 #define M_CMPLE(a,b,c,const)    M_OP3 (0x10,0x6d, a,b,c,const)  /* c = a <= b */
184
185 #define M_CMPULE(a,b,c,const)   M_OP3 (0x10,0x3d, a,b,c,const)  /* c = a <= b */
186 #define M_CMPULT(a,b,c,const)   M_OP3 (0x10,0x1d, a,b,c,const)  /* c = a <= b */
187
188 #define M_AND(a,b,c,const)      M_OP3 (0x11,0x00, a,b,c,const)  /* c = a &  b */
189 #define M_OR(a,b,c,const)       M_OP3 (0x11,0x20, a,b,c,const)  /* c = a |  b */
190 #define M_XOR(a,b,c,const)      M_OP3 (0x11,0x40, a,b,c,const)  /* c = a ^  b */
191
192 #define M_NOP                   M_OR (31,31,31,0)
193
194 #define M_SLL(a,b,c,const)      M_OP3 (0x12,0x39, a,b,c,const)  /* c = a << b */
195 #define M_SRA(a,b,c,const)      M_OP3 (0x12,0x3c, a,b,c,const)  /* c = a >> b */
196 #define M_SRL(a,b,c,const)      M_OP3 (0x12,0x34, a,b,c,const)  /* c = a >>>b */
197
198 #define M_FLD(a,b,disp)         M_MEM (0x22,a,b,disp)           /* load flt   */
199 #define M_DLD(a,b,disp)         M_MEM (0x23,a,b,disp)           /* load dbl   */
200 #define M_FST(a,b,disp)         M_MEM (0x26,a,b,disp)           /* store flt  */
201 #define M_DST(a,b,disp)         M_MEM (0x27,a,b,disp)           /* store dbl  */
202
203 #define M_FADD(a,b,c)           M_FOP3 (0x16, 0x080, a,b,c)     /* flt add    */
204 #define M_DADD(a,b,c)           M_FOP3 (0x16, 0x0a0, a,b,c)     /* dbl add    */
205 #define M_FSUB(a,b,c)           M_FOP3 (0x16, 0x081, a,b,c)     /* flt sub    */
206 #define M_DSUB(a,b,c)           M_FOP3 (0x16, 0x0a1, a,b,c)     /* dbl sub    */
207 #define M_FMUL(a,b,c)           M_FOP3 (0x16, 0x082, a,b,c)     /* flt mul    */
208 #define M_DMUL(a,b,c)           M_FOP3 (0x16, 0x0a2, a,b,c)     /* dbl mul    */
209 #define M_FDIV(a,b,c)           M_FOP3 (0x16, 0x083, a,b,c)     /* flt div    */
210 #define M_DDIV(a,b,c)           M_FOP3 (0x16, 0x0a3, a,b,c)     /* dbl div    */
211
212 #define M_FADDS(a,b,c)          M_FOP3 (0x16, 0x580, a,b,c)     /* flt add    */
213 #define M_DADDS(a,b,c)          M_FOP3 (0x16, 0x5a0, a,b,c)     /* dbl add    */
214 #define M_FSUBS(a,b,c)          M_FOP3 (0x16, 0x581, a,b,c)     /* flt sub    */
215 #define M_DSUBS(a,b,c)          M_FOP3 (0x16, 0x5a1, a,b,c)     /* dbl sub    */
216 #define M_FMULS(a,b,c)          M_FOP3 (0x16, 0x582, a,b,c)     /* flt mul    */
217 #define M_DMULS(a,b,c)          M_FOP3 (0x16, 0x5a2, a,b,c)     /* dbl mul    */
218 #define M_FDIVS(a,b,c)          M_FOP3 (0x16, 0x583, a,b,c)     /* flt div    */
219 #define M_DDIVS(a,b,c)          M_FOP3 (0x16, 0x5a3, a,b,c)     /* dbl div    */
220
221 #define M_CVTDF(a,b,c)          M_FOP3 (0x16, 0x0ac, a,b,c)     /* dbl2long   */
222 #define M_CVTLF(a,b,c)          M_FOP3 (0x16, 0x0bc, a,b,c)     /* long2flt   */
223 #define M_CVTLD(a,b,c)          M_FOP3 (0x16, 0x0be, a,b,c)     /* long2dbl   */
224 #define M_CVTDL(a,b,c)          M_FOP3 (0x16, 0x1af, a,b,c)     /* dbl2long   */
225 #define M_CVTDL_C(a,b,c)        M_FOP3 (0x16, 0x12f, a,b,c)     /* dbl2long   */
226 #define M_CVTLI(a,b)            M_FOP3 (0x17, 0x130, 31,a,b)    /* long2int   */
227
228 #define M_CVTDFS(a,b,c)         M_FOP3 (0x16, 0x5ac, a,b,c)     /* dbl2long   */
229 #define M_CVTDLS(a,b,c)         M_FOP3 (0x16, 0x5af, a,b,c)     /* dbl2long   */
230 #define M_CVTDL_CS(a,b,c)       M_FOP3 (0x16, 0x52f, a,b,c)     /* dbl2long   */
231 #define M_CVTLIS(a,b)           M_FOP3 (0x17, 0x530, 31,a,b)    /* long2int   */
232
233 #define M_FCMPEQ(a,b,c)         M_FOP3 (0x16, 0x0a5, a,b,c)     /* c = a==b   */
234 #define M_FCMPLT(a,b,c)         M_FOP3 (0x16, 0x0a6, a,b,c)     /* c = a<b    */
235
236 #define M_FCMPEQS(a,b,c)        M_FOP3 (0x16, 0x5a5, a,b,c)     /* c = a==b   */
237 #define M_FCMPLTS(a,b,c)        M_FOP3 (0x16, 0x5a6, a,b,c)     /* c = a<b    */
238
239 #define M_FMOV(fa,fb)           M_FOP3 (0x17, 0x020, fa,fa,fb)  /* b = a      */
240 #define M_FMOVN(fa,fb)          M_FOP3 (0x17, 0x021, fa,fa,fb)  /* b = -a     */
241
242 #define M_FNOP                  M_FMOV (31,31)
243
244 #define M_FBEQZ(fa,disp)        M_BRA (0x31,fa,disp)            /* br a == 0.0*/
245
246 /****** macros for special commands (see an Alpha-manual for description) *****/ 
247
248 #define M_TRAPB                 M_MEM (0x18,0,0,0x0000)        /* trap barrier*/
249
250 #define M_S4ADDL(a,b,c,const)   M_OP3 (0x10,0x02, a,b,c,const) /* c = a*4 + b */
251 #define M_S4ADDQ(a,b,c,const)   M_OP3 (0x10,0x22, a,b,c,const) /* c = a*4 + b */
252 #define M_S4SUBL(a,b,c,const)   M_OP3 (0x10,0x0b, a,b,c,const) /* c = a*4 - b */
253 #define M_S4SUBQ(a,b,c,const)   M_OP3 (0x10,0x2b, a,b,c,const) /* c = a*4 - b */
254 #define M_S8ADDL(a,b,c,const)   M_OP3 (0x10,0x12, a,b,c,const) /* c = a*8 + b */
255 #define M_S8ADDQ(a,b,c,const)   M_OP3 (0x10,0x32, a,b,c,const) /* c = a*8 + b */
256 #define M_S8SUBL(a,b,c,const)   M_OP3 (0x10,0x1b, a,b,c,const) /* c = a*8 - b */
257 #define M_S8SUBQ(a,b,c,const)   M_OP3 (0x10,0x3b, a,b,c,const) /* c = a*8 - b */
258
259 #define M_LLD_U(a,b,disp)       M_MEM (0x0b,a,b,disp)          /* unalign ld  */
260 #define M_LST_U(a,b,disp)       M_MEM (0x0f,a,b,disp)          /* unalign st  */
261
262 #define M_ZAP(a,b,c,const)      M_OP3 (0x12,0x30, a,b,c,const)
263 #define M_ZAPNOT(a,b,c,const)   M_OP3 (0x12,0x31, a,b,c,const)
264 #define M_EXTBL(a,b,c,const)    M_OP3 (0x12,0x06, a,b,c,const)
265 #define M_EXTWL(a,b,c,const)    M_OP3 (0x12,0x16, a,b,c,const)
266 #define M_EXTLL(a,b,c,const)    M_OP3 (0x12,0x26, a,b,c,const)
267 #define M_EXTQL(a,b,c,const)    M_OP3 (0x12,0x36, a,b,c,const)
268 #define M_EXTWH(a,b,c,const)    M_OP3 (0x12,0x5a, a,b,c,const)
269 #define M_EXTLH(a,b,c,const)    M_OP3 (0x12,0x6a, a,b,c,const)
270 #define M_EXTQH(a,b,c,const)    M_OP3 (0x12,0x7a, a,b,c,const)
271 #define M_INSBL(a,b,c,const)    M_OP3 (0x12,0x0b, a,b,c,const)
272 #define M_INSWL(a,b,c,const)    M_OP3 (0x12,0x1b, a,b,c,const)
273 #define M_INSLL(a,b,c,const)    M_OP3 (0x12,0x2b, a,b,c,const)
274 #define M_INSQL(a,b,c,const)    M_OP3 (0x12,0x3b, a,b,c,const)
275 #define M_INSWH(a,b,c,const)    M_OP3 (0x12,0x57, a,b,c,const)
276 #define M_INSLH(a,b,c,const)    M_OP3 (0x12,0x67, a,b,c,const)
277 #define M_INSQH(a,b,c,const)    M_OP3 (0x12,0x77, a,b,c,const)
278 #define M_MSKBL(a,b,c,const)    M_OP3 (0x12,0x02, a,b,c,const)
279 #define M_MSKWL(a,b,c,const)    M_OP3 (0x12,0x12, a,b,c,const)
280 #define M_MSKLL(a,b,c,const)    M_OP3 (0x12,0x22, a,b,c,const)
281 #define M_MSKQL(a,b,c,const)    M_OP3 (0x12,0x32, a,b,c,const)
282 #define M_MSKWH(a,b,c,const)    M_OP3 (0x12,0x52, a,b,c,const)
283 #define M_MSKLH(a,b,c,const)    M_OP3 (0x12,0x62, a,b,c,const)
284 #define M_MSKQH(a,b,c,const)    M_OP3 (0x12,0x72, a,b,c,const)
285
286
287 /****** macros for unused commands (see an Alpha-manual for description) ******/ 
288
289 #define M_ANDNOT(a,b,c,const)   M_OP3 (0x11,0x08, a,b,c,const) /* c = a &~ b  */
290 #define M_ORNOT(a,b,c,const)    M_OP3 (0x11,0x28, a,b,c,const) /* c = a |~ b  */
291 #define M_XORNOT(a,b,c,const)   M_OP3 (0x11,0x48, a,b,c,const) /* c = a ^~ b  */
292
293 #define M_CMOVEQ(a,b,c,const)   M_OP3 (0x11,0x24, a,b,c,const) /* a==0 ? c=b  */
294 #define M_CMOVNE(a,b,c,const)   M_OP3 (0x11,0x26, a,b,c,const) /* a!=0 ? c=b  */
295 #define M_CMOVLT(a,b,c,const)   M_OP3 (0x11,0x44, a,b,c,const) /* a< 0 ? c=b  */
296 #define M_CMOVGE(a,b,c,const)   M_OP3 (0x11,0x46, a,b,c,const) /* a>=0 ? c=b  */
297 #define M_CMOVLE(a,b,c,const)   M_OP3 (0x11,0x64, a,b,c,const) /* a<=0 ? c=b  */
298 #define M_CMOVGT(a,b,c,const)   M_OP3 (0x11,0x66, a,b,c,const) /* a> 0 ? c=b  */
299
300 #define M_CMPBGE(a,b,c,const)   M_OP3 (0x10,0x0f, a,b,c,const)
301
302 #define M_FCMPUN(a,b,c)         M_FOP3 (0x16, 0x0a4, a,b,c)    /* unordered   */
303 #define M_FCMPLE(a,b,c)         M_FOP3 (0x16, 0x0a7, a,b,c)    /* c = a<=b    */
304
305 #define M_FCMPUNS(a,b,c)        M_FOP3 (0x16, 0x5a4, a,b,c)    /* unordered   */
306 #define M_FCMPLES(a,b,c)        M_FOP3 (0x16, 0x5a7, a,b,c)    /* c = a<=b    */
307
308 #define M_FBNEZ(fa,disp)        M_BRA (0x35,fa,disp)
309 #define M_FBLEZ(fa,disp)        M_BRA (0x33,fa,disp)
310
311 #define M_JMP_CO(a,b)           M_MEM (0x1a,a,b,0xc000)        /* call cosub  */
312
313
314 /************************ function gen_resolvebranch ***************************
315
316         backpatches a branch instruction; Alpha branch instructions are very
317         regular, so it is only necessary to overwrite some fixed bits in the
318         instruction.
319
320         parameters: ip ... pointer to instruction after branch (void*)
321                     so ... offset of instruction after branch  (s4)
322                     to ... offset of branch target             (s4)
323
324 *******************************************************************************/
325
326 #define gen_resolvebranch(ip,so,to) ((s4*)(ip))[-1]|=((s4)(to)-(so))>>2&0x1fffff
327
328 #define SOFTNULLPTRCHECK