2cce3c6356254d9534a9d22ece2b878ff681d419
[cacao.git] / src / vm / jit / s390 / codegen.h
1 /* src/vm/jit/s390/codegen.h - code generation macros for s390
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CODEGEN_H
27 #define _CODEGEN_H
28
29 #include "config.h"
30
31 #include <ucontext.h>
32
33 #include "vm/types.h"
34
35 #include "vm/jit/jit.hpp"
36
37
38 /* MCODECHECK(icnt) */
39
40 #define MCODECHECK(icnt) \
41     do { \
42         if ((cd->mcodeptr + (icnt)) > cd->mcodeend) \
43             codegen_increase(cd); \
44     } while (0)
45
46 /* some patcher defines *******************************************************/
47
48 #define PATCHER_CALL_SIZE    2          /* size in bytes of a patcher call    */
49 #define PATCHER_NOPS M_NOP3
50
51 /* branch defines ************************************************************/
52
53 #define BRANCH_NOPS \
54         do { \
55                 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) { \
56                         M_NOP2; M_NOP2; /* brc */ \
57                         M_NOP2; M_NOP2; M_NOP2; M_NOP2; M_NOP2; M_NOP2; M_NOP2; M_NOP2; /* ild */ \
58                         M_NOP2; /* ar, bcr */ \
59                 } else { \
60                         M_NOP; /* brc */ \
61                 } \
62         } while (0) 
63
64
65 /* *** BIG TODO ***
66  * Make all this inline functions !!!!!!!!!!
67  */
68
69 /* macros to create code ******************************************************/
70
71 /* Conventions:
72  * N_foo:   defines the instrucition foo as in `ESA/390 Principles of operations'
73  * SZ_foo:  defines the size of the instruction N_foo
74  * DD_foo:  defines a condition code as used by s390 GCC
75  * M_foo:   defines the alpha like instruction used in cacao
76  *          the instruction is defined by an equivalent N_ instruction
77  * CC_foo:  defines a condition code as used
78  *          the instruction is defined as an equivalent DD_ condition code
79  */
80
81 /* S390 specific code */
82
83 /* Argument checks for debug mode */
84
85 /* Some instructions with register arguments treat %r0 as "value not given".
86  * To prevent bugs, in debug mode we use a special value RN (reg none) with 
87  * the meaning "value not given".
88  * In debug mode, the instructions assert that %r0 was not given as argument.
89  */
90
91 #if !defined(NDEBUG)
92
93 #       include <stdlib.h>
94
95         /* register none */
96 #       define RN 16
97
98         static inline int _OR_IMPL(const char *file, int line, int r) {
99                 if(!(
100                         ((0 < r) && (r < 16)) ||
101                         (r == RN)
102                 )) {
103                         fprintf(stdout, "%d is not a valid register at %s:%d.\n", r, file, line);
104                         abort();
105                 }
106                 return ((r == RN) ? 0 : r);
107         }
108 #       define _OR(r) _OR_IMPL(__FILE__, __LINE__, r)
109
110 #       define _SMIN(b) (-(1 << (bits - 1)))
111 #       define _SMAX(b) ((1 << (b - 1)) - 1)
112 #       define _UMIN(b) 0
113 #       define _UMAX(b) ((1 << b) - 1)
114
115         static inline int _UBITS_IMPL(const char *file, int line, int i, int bits) {
116                 if (!((_UMIN(bits) <= i) && (i <= _UMAX(bits)))) {
117                         fprintf(stdout, "%d (0x%X) is not an unsigned %d bit integer at %s:%d.\n", i, i, bits, file, line);
118                         abort();
119                 }
120                 return i;
121         }
122
123 #       define _UBITS(i, bits) _UBITS_IMPL(__FILE__, __LINE__, i, bits)
124
125         static inline int _SBITS_IMPL(const char *file, int line, int i, int bits) {
126                 if(!((_SMIN(bits) <= i) && (i <= _SMAX(bits)))) {
127                         fprintf(stdout, "%d (0x%X) is not an signed %d bit integer at %s:%d.\n", i, i, bits, file, line);
128                         abort();
129                 }
130                 return i;
131         }
132
133 #       define _SBITS(i, bits) _SBITS_IMPL(__FILE__, __LINE__, i, bits)
134
135         static inline int _BITS_IMPL(const char *file, int line, int i, int bits) {
136                 if (!(
137                         ((_UMIN(bits) <= i) && (i <= _UMAX(bits))) ||
138                         ((_SMIN(bits) <= i) && (i <= _SMAX(bits)))
139                 )) {
140                         fprintf(stdout, "%d (0x%X) is not an %d bit integer at %s:%d.\n", i, i, bits, file, line);
141                         abort();
142                 }
143                 return i;
144         }
145
146 #       define _BITS(i, bits) _BITS_IMPL(__FILE__, __LINE__, i, bits)
147
148 #else
149 #       define RN 0
150 #       define _OR(x) (x)
151 #       define _BITS(x, b) (x)
152 #       define _UBITS(x, b) (x)
153 #       define _SBITS(x, b) (x)
154 #endif
155
156 /* Register */
157 #define _R(x) _UBITS((x), 4)
158 /* Displacement */
159 #define _D(x) _UBITS((x), 12)
160 /* 4 bit Immediate */
161 #define _I4(x) _BITS((x), 4)
162 #define _UI4(x) _UBITS((x), 4)
163 #define _SI4(x) _SBITS((x), 4)
164 /* 8 bit Immediate */
165 #define _I8(x) _BITS((x), 8)
166 #define _UI8(x) _UBITS((x), 8)
167 #define _SI8(x) _SBITS((x), 8)
168 /* 12 bit Immediate */
169 #define _I12(x) _BITS((x), 12)
170 #define _UI12(x) _UBITS((x), 12)
171 #define _SI12(x) _SBITS((x), 12)
172 /* 16 bit Immediate */
173 #define _I16(x) _BITS((x), 16)
174 #define _UI16(x) _UBITS((x), 16)
175 #define _SI16(x) _SBITS((x), 16)
176 /* Opcode */
177 #define _OP(x) _UBITS((x), 8)
178 /* Second part of opcode */
179 #define _OP4(x) _UBITS((x), 4)
180 /* Extended opcode */
181 #define _OP16(x) _UBITS((x), 16)
182
183 /* Instruction formats */
184
185 #define _CODE(t, code) \
186         do { \
187                 *((t *) cd->mcodeptr) = (code); \
188                 cd->mcodeptr += sizeof(t); \
189         } while (0)
190
191 #define _CODE2(code) _CODE(u2, code)
192 #define _CODE4(code) _CODE(u4, code)
193
194 #define _IF(cond, t, f) \
195         do { if (cond) { t ; } else { f ; } } while (0)
196
197 #define _IFNEG(val, neg, pos) _IF((val) < 0, neg, pos)
198
199 #define N_RR(op, r1, r2) \
200         _CODE2( (_OP(op) << 8) | (_R(r1) << 4) | _R(r2) )
201
202 #define SZ_RR 2
203
204 static inline uint8_t N_RR_GET_OPC(uint8_t *instrp) {
205         return instrp[0];
206 }
207
208 static inline uint8_t N_RR_GET_REG1(uint8_t *instrp) {
209         return (instrp[1] >> 4) & 0xF;
210 }
211
212 static inline uint8_t N_RR_GET_REG2(uint8_t *instrp) {
213         return (instrp[1] & 0xF);
214 }
215
216 #define N_RR2(op, i) \
217         _CODE2( (_OP(op) << 8) | _I8(i) )
218
219 #define N_RX(op, r1, d2, x2, b2) \
220         _CODE4( (_OP(op) << 24) | (_R(r1) << 20) | (_OR(x2) << 16) | (_OR(b2) << 12) | (_D(d2) << 0) )
221
222 #define SZ_RX 4
223
224 static inline uint8_t N_RX_GET_OPC(uint8_t *instrp) {
225         return instrp[0];
226 }
227
228 static inline uint8_t N_RX_GET_REG(uint8_t *instrp) {
229         return (instrp[1] >> 4) & 0xF;  
230 }
231
232 static inline uint8_t N_RX_GET_INDEX(uint8_t *instrp) {
233         return (instrp[1] & 0xF);       
234 }
235
236 static inline uint8_t N_RX_GET_BASE(uint8_t *instrp) {
237         return (instrp[2] >> 4) & 0xF;
238 }
239
240 static inline uint16_t N_RX_GET_DISP(uint8_t *instrp) {
241         return *(uint16_t *)(instrp + 2) & 0xFFF;
242 }
243
244 static inline void N_RX_SET_DISP(uint8_t *instrp, uint16_t disp) {
245         *(uint16_t *)(instrp + 2) |= (disp & 0xFFF);
246 }
247
248 #define N_RI(op1, op2, r1, i2) \
249         _CODE4( (_OP(op1) << 24) | (_R(r1) << 20) | (_OP4(op2) << 16) | (u2)_SI16(i2) )
250
251 static inline int16_t N_RI_GET_IMM(uint8_t *instrp) {
252         return *(int16_t *)(instrp + 2);
253 }
254
255 static inline void N_RI_SET_IMM(uint8_t *instrp, int16_t imm) {
256         *(int16_t *)(instrp + 2) = imm;
257 }
258
259 #define N_RI2(op1, op2, r1, i2) \
260         _CODE4( (_OP(op1) << 24) | (_R(r1) << 20) | (_OP4(op2) << 16) | (u2)_UI16(i2) )
261
262 #define SZ_RI 4
263
264 #define N_SI(op, d1, b1, i2) \
265         _CODE4( (_OP(op) << 24) | (_OR(i2) << 16) | (_OR(b1) << 12) | _D(d1) )
266
267 #define SZ_SI 4
268
269 #define N_SS(op, d1, l, b1, d2, b2) \
270         do { \
271                 _CODE4( (_OP(op) << 24) | (_I8(l) << 16) | (_OR(b1) << 12) | _D(d1) ); \
272                 _CODE2( (_OR(b2) << 12) | _D(d2) ); \
273         } while (0)
274
275 #define SZ_SS 6
276
277 #define N_SS2(op, d1, l1, b1, d2, l2, b2) \
278         N_SS(op, d1, (_I4(l1) << 4) | _I4(l2), b1, d2, l2)
279
280 #define N_RS(op, r1, r3, d2, b2) \
281         _CODE4( (_OP(op) << 24) | (_R(r1) << 20) | (_R(r3) << 16) | (_OR(b2) << 12) | _D(d2) )
282
283 #define SZ_RS 4
284
285 #define N_RSI(op, r1, r2, i2) \
286         _CODE4( ((op) << 24) | (_R(r1) << 20) | (_R(r3) << 16) | (u2)_16(i2) )
287
288 #define SZ_RSI 4
289
290 #define N_RRE(op, r1, r2) \
291         _CODE4( (_OP16(op) << 16) | (_R(r1) << 4) | _R(r2) )
292
293 #define SZ_RRE 4
294
295 #define N_S2(d2, b2) \
296         _CODE4( (_OP16(op) << 16) | (_OR(b2) << 12) | _D(d2)  )
297
298 #define SZ_S2 4
299
300 #define N_E(op) \
301         _CODE2( _OP16(op) )
302
303 #define SZ_E 2
304
305 #define N_RXE(op, r1, d2, x2, b2) \
306         do { \
307                 _CODE4( ((_OP16(op) >> 8) << 24) | (_R(r1) << 20) | \
308                         (_R(x2) << 16) | (_R(b2) << 12) | _UI12(d2) ); \
309                 _CODE2( _OP16(op) & 0xFF ); \
310         } while (0) 
311
312 #define S_RXE 6
313
314 #define N_RRF(op, r1, m3, r2) \
315         _CODE4( (_OP16(op) << 16) | (_R(m3) << 12) | (_R(r1) << 4) | _R(r2) )
316
317 #define S_RRF 4
318
319 #define N_IMM_MIN -32768
320 #define N_IMM_MAX 32767
321 #define N_VALID_IMM(x) ((N_IMM_MIN <= (x)) && ((x) <= N_IMM_MAX))
322 #define ASSERT_VALID_IMM(x) assert(N_VALID_IMM(x))
323
324 #define N_DISP_MIN 0
325 #define N_DISP_MAX 0xFFF
326 #define N_VALID_DISP(x) ((N_DISP_MIN <= (x)) && ((x) <= N_DISP_MAX))
327 #define ASSERT_VALID_DISP(x) assert(N_VALID_DISP(x))
328
329 #define N_PV_OFFSET (-0xFFC)
330 #define N_DSEG_DISP(x) ((x) - N_PV_OFFSET)
331 #define N_VALID_DSEG_DISP(x) N_VALID_DISP(N_DSEG_DISP(x))
332
333 #define N_BRANCH_MIN (-32768 * 2)
334 #define N_BRANCH_MAX (32767 * 2)
335 #define N_VALID_BRANCH(x) ((N_BRANCH_MIN <= (x)) && ((x) <= N_BRANCH_MAX))
336 #define ASSERT_VALID_BRANCH(x) assert(N_VALID_BRANCH(x))
337
338 #define N_IS_EVEN_ODD(x) \
339         (((GET_HIGH_REG(x) % 2) == 0) && (GET_LOW_REG(x) == (GET_HIGH_REG(x) + 1)))
340
341 /* Condition codes */
342
343 #define DD_O 1
344 #define DD_H 2
345 #define DD_NLE 3
346 #define DD_L 4
347 #define DD_NHE 5
348 #define DD_LH 6
349 #define DD_NE 7
350 #define DD_E 8
351 #define DD_NLH 9
352 #define DD_HE 10
353 #define DD_NL 11
354 #define DD_LE 12
355 #define DD_NH 13
356 #define DD_NO 14
357 #define DD_ANY 15
358
359 #define DD_0 8
360 #define DD_1 4
361 #define DD_2 2
362 #define DD_3 1
363
364 /* Misc */
365
366 /* Trap instruction.
367  * If most significant bits of first opcode byte are 00, then
368  * format is RR (1 byte opcode) or E (2 bytes opcode). 
369  * There seems to be no opcode 0x02 or 0x02**, so we'll define
370  * our trap instruction as:
371  * +--------+--------+
372  * |  0x02  |  data  |
373  * +--------+--------+
374  * 0                 15
375  */
376 #define N_ILL(data) _CODE2(0x0200 | _UBITS(data, 8))
377 #       define OPC_ILL 0x02
378 #       define SZ_ILL 2
379
380 static inline uint8_t N_ILL_GET_REG(uint8_t *instrp) {
381         return (instrp[1] >> 4) & 0xF;
382 }
383
384 static inline uint8_t N_ILL_GET_TYPE(uint8_t *instrp) {
385         return (instrp[1] & 0xF);
386 }
387
388 #define N_LONG(l) _CODE4(l)
389 #define SZ_LONG 4
390
391 /* Chapter 7. General instructions */
392
393 #define N_AR(r1, r2) N_RR(0x1A, r1, r2)
394 #define N_A(r1, d2, x2, b2) N_RX(0x5A, r1, d2, x2, b2)
395 #define N_AH(r1, d2, x2, b2) N_RX(0x4A, r1, d2, x2, b2)
396 #define N_AHI(r1, i2) N_RI(0xA7, 0xA, r1, i2)
397 #       define SZ_AHI SZ_RI
398 #define N_ALR(r1, r2) N_RR(0x1E, r1, r2)
399 #define N_AL(r1, d2, x2, b2) N_RX(0x5E, r1, d2, x2, b2)
400 #define N_NR(r1, r2) N_RR(0x14, r1, r2)
401 #       define SZ_NR SZ_RR
402 #define N_N(r1, d2, x2, b2) N_RX(0x54, r1, d2, x2, b2)
403 #define N_NI(d1, b1, i2) N_SI(0x94, d1, b1, i2)
404 #define N_NC(d1, l, b1, d2, b2) N_SS(0xD4, (l - 1), b1, d1, b2, d2)
405 #define N_BALR(r1, r2) N_RR(0x05, r1, _OR(r2))
406 #define N_BAL(r1, d2, x2, b2) N_RX(0x45, r1, d2, x2, b2)
407 #define N_BASR(r1, r2) N_RR(0x0D, r1, _OR(r2))
408 #       define SZ_BASR SZ_RR
409 #define N_BAS(r1, d2, x2, b2) N_RX(0x4D, r1, d2, x2, b2)
410 #define N_BASSM(r1, r2) N_RR(0x0C, r1, _OR(r2))
411 #define N_BSM(r1, r2) N_RR(0x0B, r1, _OR(r2))
412 #define N_BCR(m1, r2) N_RR(0x07, m1, _OR(r2))
413 #       define SZ_BCR SZ_RR
414 #       define N_BR(r2) N_BCR(DD_ANY, r2)
415 #define N_BC(m1, d2, x2, b2) N_RX(0x47, m1, d2, x2, b2)
416 #       define SZ_BC SZ_RS
417 #define N_BCTR(r1, r2) N_RR(0x06, r1, _OR(r2))
418 #define N_BCT(r1, d2, x2, b2) N_RX(0x46, r1, d2, x2, b2)
419 #define N_BHX(r1, r2, d2, b2) N_RS(0xB6, r1, r3, d2, b2)
420 #define N_BXLE(r1, r3, d2, b2) N_RS(0xB7, r1, r3, d2, b2)
421 #define N_BRAS(r1, i2) N_RI(0xA7, 0x5, r1, (i2) / 2)
422 #       define SZ_BRAS SZ_RI
423 #define N_BRC(m1, i2) N_RI(0xA7, 0x4, m1, (i2) / 2)
424 #       define N_J(i2) N_BRC(DD_ANY, i2)
425 #       define SZ_BRC SZ_RI
426 #       define SZ_J SZ_RI
427 #       define N_BRC_BACK_PATCH(brc_pos) \
428                 do { \
429                         *(u4 *)(brc_pos) |= (u4)(cd->mcodeptr - (brc_pos)) / 2; \
430                 } while (0)
431 #define N_BRCT(r1, i2) N_RI(0xA7, 0x6, r1, (i2) / 2)
432 #define N_BRXH(r1, r3, i2) N_RSI(0x84, r1, r3, (i2) / 2)
433 #define N_BRXLE(r1, r3, i2) N_RSI(0x85, r1, r2, (i2) / 2)
434 #define N_CKSM(r1, r2) N_RRE(0xB241, r1, r2)
435 #define N_CR(r1, r2) N_RR(0x19, r1, r2)
436 #       define SZ_CR SZ_RR
437 #define N_C(r1, d2, x2, b2) N_RX(0x59, r1, d2, x2, b2)
438 #define N_CFC(d2, b2) N_S2(0xB21A, d2, b2)
439 #define N_CS(r1, r3, d2, b2) N_RS(0xBA, r1, r3, d2, b2)
440 #define N_CDS(r1, r3, d2, b2) N_RS(0xBB, r1, r3, d2, b2)
441 #define N_CH(r1, d2, x2, b2) N_CH(0x49, r1, d2, x2, b2)
442 #define N_CHI(r1, i2) N_RI(0xA7, 0xE, r1, i2)
443 #define N_CLR(r1, r2) N_RR(0x15, r1, r2)
444 #define N_CL(r1, d2, x2, b2) N_RX(0x55, r1, d2, x2, b2)
445 #       define OPC_CL 0x55
446 #define N_CLI(d1, b1, i2) N_SI(0x95, d1, b1, i2)
447 #define N_CLC(d1, l, b1, d2, b2) N_SS(0xD5, d1, (l - 1), b1, d2, b2)
448 #define N_CLM(r1, m3, d2, b2) N_RS(0xBD, r1, m3, d2, b2)
449 #define N_CLCL(r1, r2) N_RR(0x0F, r1, r2)
450 #define N_CLCLE(r1, r3, d2, b2) N_RS(0xA9, r1, r3, d2, b2)
451 #define N_CLST(r1, r2) N_RRE(0xB25D, r1, r2)
452 #define N_CUSE(r1, r2) N_RRE(0xB257, r1, r2)
453 #define N_CVB(r1, d2, x2, b2) N_RX(0x4F, r1, r2, x2, b2)
454 #define N_CVD(r1, d2, x2, b2) N_RX(0x4E, r1, d2, x2, b2)
455 #define N_CUUTF(r1, r2) N_RRE(0xB2A6, r1, r2)
456 #define N_CUTFU(r1, r2) N_RRE(0xB2A7, r1, r2)
457 #define N_CPYA(r1, r2) N_RRE(0xB240, r1, r2)
458 #define N_DR(r1, r2) N_RR(0x1D, r1, r2)
459 #       define OPC_DR 0x1D
460 #define N_D(r1, d2, x2, b2) N_RX(0x5D, r1, d2, x2, b2)
461 #define N_XR(r1, r2) N_RR(0x17, r1, r2)
462 #define N_X(r1, d2, x2, b2) N_RX(0x57, r1, d2, x2, b2)
463 #define N_XI(d1, b1, i2) N_SI(0x97, d1, b1, i2)
464 #define N_XC(d1, l, b1, d2, b2) N_SS(0xD7, d1, (l - 1), b1, d2, b2)
465 #define N_EX(r1, d2, x2, b2) N_RX(0x44, r1, d2, x2, b2)
466 #define N_EAR(r1, r2) N_RRE(0xB24F, r1, r2)
467 #define N_IC(r1, d2, x2, b2) N_RX(0x43, r1, d2, x2, b2)
468 #define N_ICM(r1, m3, d2, b2) N_RS(0xBF, r1, m3, d2, b2)
469 #define N_IPM(r1) N_RRE(0xB222, r1, 0)
470 #define N_LR(r1, r2) N_RR(0x18, r1, r2)
471 #define N_L(r1, d2, x2, b2) N_RX(0x58, r1, d2, x2, b2)
472 #       define SZ_L SZ_RX
473 #       define OPC_L 0x58
474 #define N_LAM(r1, r3, d2, b2) N_RS(0x9A, r1, r3, d2, b2)
475 #define N_LA(r1, d2, x2, b2) N_RX(0x41, r1, d2, x2, b2)
476 #define N_LAE(r1, d2, x2, b2) N_RX(0x51, r1, d2, x2, b2)
477 #define N_LTR(r1, r2) N_RR(0x12, r1, r2)
478 #define N_LCR(r1, r2) N_RR(0x13, r1, r2)
479 #       define SZ_LCR SZ_RR
480 #define N_LH(r1, d2, x2, b2) N_RX(0x48, r1, d2, x2, b2)
481 #define N_LHI(r1, i2) N_RI(0xA7, 0x8, r1, i2)
482 #       define SZ_LHI SZ_RI
483 #define N_LM(r1, r3, d2, b2) N_RS(0x98, r1, r3, d2, b2)
484 #define N_LNR(r1, r2) N_RR(0x11, r1, r2)
485 #define N_LPR(r1, r2) N_RR(0x10, r1, r2)
486 #define N_MC(d1, b1, i2) N_SI(0xAF, d1, b1, i2)
487 #define N_MVI(d1, b1, i2) N_SI(0x92, d1, b1, i2)
488 #define N_MVC(d1, l, b1, d2, b2) N_SS(0xD2, d1, (l - 1), b1, d2, b2)
489 #define N_MVCIN(d1, l, b1, d2, b2) N_SS(0xEB, d1, (l - 1), b1, d2, b2)
490 #define N_MVCL(r1, r2) N_RR(0x0E, r1, r2)
491 #define N_MVCLE(r1, r3, d2, b2)  N_RS(0xAB, r1, r3, d2, b2)
492 #define N_MVN(d1, l, b1, d2, b2) N_SS(0xD1, d1, (l - 1), b1, d2, b2)
493 #define N_MVPG(r1, r2) N_RRE(0xB254, r1, r2)
494 #define N_MVST(r1, r2) N_RRE(0xB255, r1, r2)
495 #define N_MVO(d1, l1, b1, d2, l2, b2) N_SS2(0xF1, d1, (l1 - 1), b1, d2, (l2 - 1), b2)
496 #define N_MVZ(d1, l, b1, d2, b2) N_SS(0xD3, d1, (l - 1), b1, d2, b2)
497 #define N_MR(r1, r2) N_RR(0x1C, r1, r2)
498 #define N_M(r1, d2, x2, b2) N_RX(0x5C, r1, d2, x2, b2)
499 #define N_MH(r1, d2, x2, b2) N_RX(0x4C, r1, d2, x2, b2)
500 #define N_MHI(r1, i2) N_RI(0xA7, 0xC, r1, i2)
501 #define N_MSR(r1, r2) N_RRE(0xB252, r1, r2)
502 #define N_MS(r1, d2, x2, b2) N_RX(0x71, r1, d2, x2, b2)
503 #define N_OR(r1, r2) N_RR(0x16, r1, r2)
504 #define N_O(r1, d2, x2, b2) N_RX(0x56, r1, d2, x2, b2)
505 #define N_OI(d1, b1, i2) N_SI(0x96, d1, b1, i2)
506 #define N_OC(d1, l, b1, d2, b2) N_SS(0xD6, d1, (l - 1), b1, d2, b2)
507 #define N_PACK(d1, l1, b1, d2, l2, b2) N_SS2(0xF2, d1, (l1 - 1), b1, d2, (l2 - 1), b2)
508 #define N_PLO(r1, d2, b2, r3, d4, b4) N_SS2(0xEE, d2, r1, b2, d4, r3, b4)
509 #define N_SRST(r1, r2) N_RRE(0xB25E, r1, r2)
510 #define N_SAR(r1, r2) N_RRE(0xB24E, r1, r2)
511 #define N_SPM(r1) N_RR(0x04, r1, 0x00)
512 #define N_SLDA(r1, d2, b2) N_RS(0x8F, r1, 0x00, d2, b2)
513 #define N_SLDL(r1, d2, b2) N_RS(0x8D, r1, 0x00, d2, b2)
514 #define N_SLA(r1, d2, b2) N_RS(0x8B, r1, 0x00, d2, b2)
515 #define N_SLL(r1, d2, b2) N_RS(0x89, r1, 0x00, d2, b2)
516 #define N_SRDA(r1, d2, b2) N_RS(0x8E, r1, 0x00, d2, b2)
517 #define N_SRDL(r1, d2, b2) N_RS(0x8C, r1, 0x00, d2, b2)
518 #define N_SRA(r1, d2, b2) N_RS(0x8A, r1, 0x00, d2, b2)
519 #define N_SRL(r1, d2, b2) N_RS(0x88, r1, 0x00, d2, b2)
520 #define N_ST(r1, d2, x2, b2) N_RX(0x50, r1, d2, x2, b2)
521 #       define OPC_ST 0x50
522 #define N_STAM(r1, r3, d2, b2) N_RS(0x9B, r1, r3, d2, b2)
523 #define N_STC(r1, d2, x2, b2) N_RX(0x42, r1, d2, x2, b2)
524 #define N_STCM(r1, m3, d2, b2) N_RS(0xBE, r1, m3, d2, b2)
525 #define N_STCK(d2, b2) N_S2(0xB205, d2, b2)
526 #define N_STCKE(d2, b2) N_S2(0xB278, d2, b2)
527 #define N_STH(r1, d2, x2, b2) N_RX(0x40, r1, d2, x2, b2)
528 #define N_STM(r1, r3, d2, b2) N_RS(0x90, r1, r3, d2, b2)
529 #define N_SR(r1, r2) N_RR(0x1B, r1, r2)
530 #define N_S(r1, d2, x2, b2) N_RX(0x5B, r1, d2, x2, b2)
531 #define N_SH(r1, d2, x2, b2) N_RX(0x4B, r1, d2, x2, b2)
532 #define N_SLR(r1, r2) N_RR(0x1F, r1, r2)
533 #define N_SL(r1, d2, x2, b2) N_RX(0x5F, r1, d2, x2, b2)
534 #define N_SVC(i) N_RR2(0x0A, i)
535 #define N_TS(d2, b2) N_S2(0x93, d2, b2)
536 #define N_TM(d1, b1, i2) N_SI(0x91, d1, b1, i2)
537 #define N_TMH(r1, i2) N_RI2(0xA7, 0x00, r1, i2)
538 #define N_TML(r1, i2) N_RI2(0xA7, 0x01, r1, i2)
539 #define N_TR(d1, l, b1, d2, b2) N_SS(0xDC, d1, (l - 1), b1, d2, b2)
540 #define N_TRT(d1, l, b1, d2, b2) N_SS(0xDD, d1, (l - 1), b1, d2, b2)
541 #define N_TRE(r1, r2) N_RRE(0xB2A5, r1, r2)
542 #define N_UNPK(d1, l1, b1, d2, l2, b2) N_SS2(0xF3, d1, (l1 - 1), b1, d2, (l2 - 2), b2)
543 #define N_UPT() N_E(0x0102)
544
545 /* Chapter 9. Floating point instructions */
546
547 #define N_LER(r1, r2) N_RR(0x38, r1, r2)
548 #define N_LDR(r1, r2) N_RR(0x28, r1, r2)
549 #define N_LXR(r1, r2) N_RRE(0xB365, r1, r2)
550 #define N_LE(r1, d2, x2, b2) N_RX(0x78, r1, d2, x2, b2)
551 #define N_LD(r1, d2, x2, b2) N_RX(0x68, r1, d2, x2, b2)
552 #define N_LZER(r1) N_RRE(0xB374, r1, 0x0)
553 #define N_LZDR(r1) N_RRE(0xB375, r1, 0x0)
554 #define N_LZXR(r1) N_RRE(0xB376, r1, 0x0)
555 #define N_STE(r1, d2, x2, b2) N_RX(0x70, r1, d2, x2, b2)
556 #define N_STD(r1, d2, x2, b2) N_RX(0x60, r1, d2, x2, b2)
557
558 /* chapter 19. Binary floating point instructions */
559
560 #define N_AEBR(r1, r2) N_RRE(0xB30A, r1, r2)
561 #define N_ADBR(r1, r2) N_RRE(0xB31A, r1, r2)
562 #define N_AXBR(r1, r2) N_RRE(0xB34A, r1, r2)
563 #define N_AEB(r1, d2, x2, b2) N_RXE(0xED0A, r1, d2, x2, b2)
564 #define N_ADB(r1, d2, x2, b2) N_RXE(0xED1A, r1, d2, x2, b2)
565
566 #define N_CEBR(r1, r2) N_RRE(0xB309, r1, r2)
567 #define N_CDBR(r1, r2) N_RRE(0xB319, r1, r2)
568 #define N_CXBR(r1, r2) N_RRE(0xB349, r1, r2)
569 #define N_CEB(r1, d2, x2, b2) N_RXE(0xED09, r1, d2, x2, b2)
570 #define N_CDB(r1, d2, x2, b2) N_RXE(0xED19, r1, d2, x2, b2)
571
572 #define N_CEFBR(r1, r2) N_RRE(0xB394, r1, r2)
573 #define N_CDFBR(r1, r2) N_RRE(0xB395, r1, r2)
574 #define N_CXFBR(r1, r2) N_RRE(0xB396, r1, r2)
575
576 #define N_CFEBR(r1, m3, r2) N_RRF(0xB398, r1, m3, r2)
577 #define N_CFDBR(r1, m3, r2) N_RRF(0xB399, r1, m3, r2)
578 #define N_CFXBR(r1, m3, r2) N_RRF(0xB39A, r1, m3, r2)
579
580 #define N_DEBR(r1, r2) N_RRE(0xB30D, r1, r2)
581 #define N_DDBR(r1, r2) N_RRE(0xB31D, r1, r2)
582 #define N_DXBR(r1, r2) N_RRE(0xB34D, r1, r2)
583 #define N_DEB(r1, d2, x2, b2) N_RXE(0xED0D, r1, d2, x2, b2)
584 #define N_DDB(r1, d2, x2, b2) N_RXE(0xED1D, r1, d2, x2, b2)
585
586 #define N_LCEBR(r1, r2) N_RRE(0xB303, r1, r2)
587 #define N_LCDBR(r1, r2) N_RRE(0xB313, r1, r2)
588 #define N_LCXBR(r1, r2) N_RRE(0xB343, r1, r2)
589
590 #define N_LDEBR(r1, r2) N_RRE(0xB304, r1, r2)
591 #       define SZ_LDEBR SZ_RRE
592 #define N_LXDBR(r1, r2) N_RRE(0xB305, r1, r2)
593 #define N_LXEBR(r1, r2) N_RRE(0xB306, r1, r2)
594
595 #define N_LEDBR(r1, r2) N_RRE(0xB344, r1, r2)
596 #define N_LDXBR(r1, r2) N_RRE(0xB345, r1, r2)
597 #define N_LEXBR(r1, r2) N_RRE(0xB346, r1, r2)
598
599 #define N_LTEBR(r1, r2) N_RRE(0xB302, r1, r2)
600 #define N_LTDBR(r1, r2) N_RRE(0xB312, r1, r2)
601 #define N_LTXBR(r1, r2) N_RRE(0xB342, r1, r2)
602
603 #define N_MEEBR(r1, r2) N_RRE(0xB317, r1, r2)
604 #define N_MDBR(r1, r2) N_RRE(0xB31C, r1, r2)
605 #define N_MXBR(r1, r2) N_RRE(0xB34C, r1, r2)
606 #define N_MDEBR(r1, r2) N_RRE(0xB30C, r1, r2)
607 #define N_MXDBR(r1, r2) N_RRE(0xB307, r1, r2)
608
609 #define N_SEBR(r1, r2) N_RRE(0xB30B, r1, r2)
610 #define N_SDBR(r1, r2) N_RRE(0xB31B, r1, r2)
611 #define N_SXBR(r1, r2) N_RRE(0xB34B, r1, r2)
612 #define N_SEB(r1, d2, x2, b2) N_RXE(0xED0B, r1, d2, x2, b2)
613 #define N_SDB(r1, d2, x2, b2) N_RXE(0xED1B, r1, d2, x2, b2)
614
615 /* Alpha like instructions */
616
617 #define M_CALL(r2) N_BASR(R14, r2)
618 #define M_ILL(data) N_ILL(data)
619 #define M_ILL2(data1, data2) N_ILL((_UBITS(data1, 4) << 4) | _UBITS(data2, 4))
620 #define M_LONG(l) N_LONG(l)
621
622 #define M_ILD(r, b, d) \
623         do { \
624                 if (N_VALID_DISP(d)) { \
625                         N_L(r, d, RN, b); \
626                 } else if ((r == R0) && N_VALID_IMM(d)) { \
627                         N_LR(R0, R1); \
628                         N_LHI(R1, d); \
629                         N_L(R1, 0, R1, b); \
630                         N_XR(R1, R0); \
631                         N_XR(R0, R1); \
632                         N_XR(R1, R0); \
633                 } else if ((r != R0) && N_VALID_IMM(d)) { \
634                         N_LHI(r, d); N_L(r, 0, r, b); \
635                 } else { \
636                         N_BRAS(r, SZ_BRAS + SZ_LONG); \
637                         N_LONG(d); \
638                         N_L(r, 0, RN, r); \
639                         N_L(r, 0, r, b); \
640                 } \
641         } while (0)
642
643 #define M_ILD_DSEG(r, d) M_ILD(r, REG_PV, N_DSEG_DISP(d))
644
645 #define M_ALD(r, b, d) M_ILD(r, b, d)
646 #define M_ALD_DSEG(r, d) M_ALD(r, REG_PV, N_DSEG_DISP(d))
647
648 #define M_LDA(r, b, d) \
649         do { \
650                 if (N_VALID_DISP(d)) { \
651                         N_LA(r, d, RN, b); \
652                 } else if (N_VALID_IMM(d)) { \
653                         N_LHI(r, d); \
654                         N_LA(r, 0, r, b); \
655                 } else { \
656                         N_BRAS(r, SZ_BRAS + SZ_LONG); \
657                         N_LONG(d); \
658                         N_L(r, 0, RN, r); \
659                         N_LA(r, 0, r, b); \
660                 } \
661         } while (0)
662 #define M_LDA_DSEG(r, d) M_LDA(r, REG_PV, N_DSEG_DISP(d))
663
664 #define M_FLD(r, b, d) N_LE(r, d, RN, b)
665 #define M_FLDN(r, b, d, t) _IFNEG( \
666         d, \
667         N_LHI(t, d); N_LE(r, 0, t, b), \
668         N_LE(r, d, RN, b) \
669 )
670 #define M_FLD_DSEG(r, d, t) M_FLDN(r, REG_PV, N_DSEG_DISP(d), t)
671
672 #define M_DLD(r, b, d) N_LD(r, d, RN, b)
673 #define M_DLDN(r, b, d, t) _IFNEG( \
674         d, \
675         N_LHI(t, d); N_LD(r, 0, t, b), \
676         N_LD(r, d, RN, b) \
677 )
678 #define M_DLD_DSEG(r, d, t) M_DLDN(r, REG_PV, N_DSEG_DISP(d), t)
679
680 #define M_LLD(r, b, d) _IFNEG( \
681         d, \
682         N_LHI(GET_LOW_REG(r), d); \
683                 N_L(GET_HIGH_REG(r), 0, GET_LOW_REG(r), b); \
684                 N_L(GET_LOW_REG(r), 4, GET_LOW_REG(r), b), \
685         N_L(GET_HIGH_REG(r), (d) + 0, RN, b); N_L(GET_LOW_REG(r), (d) + 4, RN, b) \
686 )
687 #define M_LLD_DSEG(r, d) M_LLD(r, REG_PV, N_DSEG_DISP(d)
688
689 /* MOV(a, b) -> mov from A to B */
690
691 #define M_MOV(a, b) N_LR(b, a)
692 #define M_FMOV(a, b) N_LDR(b, a)
693 #define M_DST(r, b, d) _IFNEG(d, assert(0), N_STD(r, d, RN, b))
694 #define M_FST(r, b, d) _IFNEG(d, assert(0), N_STE(r, d, RN, b))
695 #define M_IST(r, b, d) _IFNEG( \
696         d, \
697         assert(0), \
698         N_ST(r, d, RN, b) \
699 )
700 #define M_AST(r, b, d) M_IST(r, b, d)
701 #define M_LST(r, b, d) _IFNEG( \
702         d, \
703         assert(0), \
704         N_ST(GET_HIGH_REG(r), (d) + 0, RN, b); N_ST(GET_LOW_REG(r), (d) + 4, RN, b) \
705 )
706 #define M_TEST(r) N_LTR(r, r)
707 #define M_BEQ(off) N_BRC(DD_E, off)
708 #define M_BNE(off) N_BRC(DD_NE, off)
709 #define M_BLE(off) N_BRC(DD_LE, off)
710 #define M_BGT(off) N_BRC(DD_H, off)
711 #define M_BLT(off) N_BRC(DD_L, off)
712 #define M_BGE(off) N_BRC(DD_HE, off)
713 #define M_BO(off) N_BRC(DD_O, off)
714
715 #define M_CMP(r1, r2) N_CR(r1, r2)
716 #define M_CMPU(r1, r2) N_CLR(r1, r2)
717 #define M_CLR(r) N_LHI(r, 0)
718 #define M_AADD_IMM(val, reg) N_AHI(reg, val)
719 #define M_IADD_IMM(val, reg) N_AHI(reg, val)
720 #define M_ISUB_IMM(val, reg) N_AHI(reg, -(val))
721 #define M_ASUB_IMM(val, reg) N_AHI(reg, -(val))
722 #define M_RET N_BCR(DD_ANY, R14)
723 #define M_BSR(ret_reg, disp) N_BRAS(ret_reg, disp)
724 #define M_BR(disp) N_BRC(DD_ANY, disp)
725 #define M_JMP(rs, rd) _IF(rs == RN, N_BCR(DD_ANY, rd), N_BASR(rs, rd))
726 #define M_NOP N_BC(0, 0, RN, RN)
727 #define M_NOP2 N_BCR(0, RN)
728 #define M_NOP3 N_BCR(0, 1)
729 #define M_JSR(reg_ret, reg_addr) N_BASR(reg_ret, reg_addr)
730 #define M_ICMP(a, b) N_CR(a, b)
731 #define M_ICMPU(a, b) N_CLR(a, b)
732 #define M_ICMP_IMM(a, b) N_CHI(a, b)
733 #define M_ACMP(a, b) N_CR(a, b)
734 #define M_CVTIF(src, dst) N_CEFBR(dst, src)
735 #define M_CVTID(src, dst) N_CDFBR(dst, src)
736 #define M_FMUL(a, dest) N_MEEBR(dest, a)
737 #define M_FSUB(a, dest) N_SEBR(dest, a)
738 #define M_FADD(a, dest) N_AEBR(dest, a)
739 #define M_FDIV(a, dest) N_DEBR(dest, a)
740 #define M_DMUL(a, dest) N_MDBR(dest, a)
741 #define M_DSUB(a, dest) N_SDBR(dest, a)
742 #define M_DADD(a, dest) N_ADBR(dest, a)
743 #define M_DDIV(a, dest) N_DDBR(dest, a)
744 #define M_CVTFI(src, dst) N_CFEBR(dst, 5, src)
745 #define M_CVTDI(src, dst) N_CFDBR(dst, 5, src)
746 #define M_IADD(a, dest) N_AR(dest, a)
747 #define M_AADD(a, dest) N_AR(dest, a)
748 #define M_ISUB(a, dest) N_SR(dest, a)
749 #define M_ASUB(a, dest) N_SR(dest, a)
750 #define M_IAND(a, dest) N_NR(dest, a)
751 #define M_IOR(a, dest) N_OR(dest, a)
752 #define M_IXOR(a, dest) N_XR(dest, a)
753 #define M_CVTFD(src,dst) N_LDEBR(dst, src)
754 #define M_CVTDF(src,dst) N_LEDBR(dst, src)
755
756 #define M_SLL_IMM(imm, reg) N_SLL(reg, imm, RN) 
757 #define M_SLA_IMM(imm, reg) N_SLA(reg, imm, RN) 
758
759 #define M_SLDL_IMM(imm, reg) N_SLDL(reg, imm, RN) 
760 #define M_SLDA_IMM(imm, reg) N_SLDA(reg, imm, RN) 
761
762 #define M_SRL_IMM(imm, reg) N_SRL(reg, imm, RN)
763 #define M_SRA_IMM(imm, reg) N_SRA(reg, imm, RN)
764
765 #define M_SRDL_IMM(imm, reg) N_SRDL(reg, imm, RN)
766 #define M_SRDA_IMM(imm, reg) N_SRDA(reg, imm, RN)
767
768 #define M_SLL(op, dst) N_SLL(dst, 0, op)
769 #define M_SLA(op, dst) N_SLA(dst, 0, op)
770
771 #define M_SLDL(op, dst) N_SLDL(dst, 0, op)
772 #define M_SLDA(op, dst) N_SLDA(dst, 0, op)
773
774 #define M_SRL(op, dst) N_SRL(dst, 0, op)
775 #define M_SRA(op, dst) N_SRA(dst, 0, op)
776
777 #define M_SRDL(op, dst) N_SRDL(dst, 0, op)
778 #define M_SRDA(op, dst) N_SRDA(dst, 0, op)
779
780 #define M_IMUL_IMM(val, reg) N_MHI(reg, val)
781 #define M_IMUL(a, dest) N_MSR(dest, a)
782
783 #define M_INEG(a, dest) N_LCR(dest, a)
784
785 #define M_FCMP(a, b) N_CEBR(a, b)
786 #define M_DCMP(a, b) N_CDBR(a, b)
787
788 #define M_FMOVN(r, dst) N_LCEBR(dst, r)
789 #define M_DMOVN(r, dst) N_LCDBR(dst, r)
790
791 #define ICONST(reg, i) \
792         do { \
793                 if (N_VALID_IMM(i)) { \
794                         N_LHI(reg, i); \
795                 } else { \
796                         disp = dseg_add_s4(cd, (i)); \
797                         M_ILD_DSEG(reg, disp); \
798                 } \
799         } while (0) 
800
801 #define LCONST(reg,c) \
802         do { \
803             ICONST(GET_HIGH_REG((reg)), (s4) ((s8) (c) >> 32)); \
804             ICONST(GET_LOW_REG((reg)), (s4) ((s8) (c))); \
805         } while (0)
806
807 #define M_ISUB_IMM32(imm, tmpreg, reg) \
808         do { \
809                 if (N_VALID_IMM(imm)) { \
810                         M_ISUB_IMM(imm, reg); \
811                 } else { \
812                         ICONST(tmpreg, imm); \
813                         M_ISUB(tmpreg, reg); \
814                 } \
815         } while (0)
816
817 #define M_ASUB_IMM32(imm, tmpreg, reg) M_ISUB_IMM32(imm, tmpreg, reg)
818
819 #endif /* _CODEGEN_H */
820
821 /*
822  * These are local overrides for various environment variables in Emacs.
823  * Please do not remove this and leave it at the end of the file, where
824  * Emacs will automagically detect them.
825  * ---------------------------------------------------------------------
826  * Local variables:
827  * mode: c
828  * indent-tabs-mode: t
829  * c-basic-offset: 4
830  * tab-width: 4
831  * End:
832  */