* Removed all Id tags.
[cacao.git] / src / vm / jit / arm / codegen.h
1 /* src/vm/jit/arm/codegen.h - code generation macros and definitions for ARM
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #ifndef _CODEGEN_H
29 #define _CODEGEN_H
30
31 #include "config.h"
32
33
34 /* helper macros for generating code ******************************************/
35
36 #if defined(__ARMEL__)
37 #define SPLIT_OPEN(type, reg, tmpreg) \
38         if (IS_2_WORD_TYPE(type) && GET_HIGH_REG(reg)==REG_SPLIT) { \
39                 /*dolog("SPLIT_OPEN({R%d;SPL} > {R%d;R%d})", GET_LOW_REG(reg), GET_LOW_REG(reg), tmpreg);*/ \
40                 /*assert(GET_LOW_REG(reg) == 3);*/ \
41                 (reg) = PACK_REGS(GET_LOW_REG(reg), tmpreg); \
42         }
43 #define SPLIT_LOAD(type, reg, offset) \
44         if (IS_2_WORD_TYPE(type) && GET_LOW_REG(reg)==3) { \
45                 /*dolog("SPLIT_LOAD({R%d;R%d} from [%x])", GET_LOW_REG(reg), GET_HIGH_REG(reg), offset);*/ \
46                 M_LDR(GET_HIGH_REG(reg), REG_SP, 4 * (offset)); \
47         }
48 #define SPLIT_STORE_AND_CLOSE(type, reg, offset) \
49         if (IS_2_WORD_TYPE(type) && GET_LOW_REG(reg)==3) { \
50                 /*dolog("SPLIT_STORE({R%d;R%d} to [%x])", GET_LOW_REG(reg), GET_HIGH_REG(reg), offset);*/ \
51                 M_STR(GET_HIGH_REG(reg), REG_SP, 4 * (offset)); \
52                 (reg) = PACK_REGS(GET_LOW_REG(reg), REG_SPLIT); \
53         }
54 #else /* defined(__ARMEB__) */
55 #define SPLIT_OPEN(type, reg, tmpreg) \
56         if (IS_2_WORD_TYPE(type) && GET_LOW_REG(reg)==REG_SPLIT) { \
57                 /*dolog("SPLIT_OPEN({SPL;R%d} > {R%d;R%d})", GET_HIGH_REG(reg), tmpreg, GET_HIGH_REG(reg));*/ \
58                 /*assert(GET_HIGH_REG(reg) == 3);*/ \
59                 (reg) = PACK_REGS(tmpreg, GET_HIGH_REG(reg)); \
60         }
61 #define SPLIT_LOAD(type, reg, offset) \
62         if (IS_2_WORD_TYPE(type) && GET_HIGH_REG(reg)==3) { \
63                 /*dolog("SPLIT_LOAD({R%d;R%d} from [%x])", GET_LOW_REG(reg), GET_HIGH_REG(reg), offset);*/ \
64                 M_LDR(GET_LOW_REG(reg), REG_SP, 4 * (offset)); \
65         }
66 #define SPLIT_STORE_AND_CLOSE(type, reg, offset) \
67         if (IS_2_WORD_TYPE(type) && GET_HIGH_REG(reg)==3) { \
68                 /*dolog("SPLIT_STORE({R%d;R%d} to [%x])", GET_LOW_REG(reg), GET_HIGH_REG(reg), offset);*/ \
69                 M_STR(GET_LOW_REG(reg), REG_SP, 4 * (offset)); \
70                 (reg) = PACK_REGS(REG_SPLIT, GET_HIGH_REG(reg)); \
71         }
72 #endif
73
74
75 #define MCODECHECK(icnt) \
76     do { \
77         if ((cd->mcodeptr + (icnt) * 4) > cd->mcodeend) \
78             codegen_increase(cd); \
79     } while (0)
80
81
82 /* TODO: correct this! */
83 #define IS_IMM(val) ( ((val) >= 0) && ((val) <= 255) )
84 #define IS_OFFSET(off,max) ((s4)(off) <= (max) && (s4)(off) >= -(max))
85
86 #if !defined(NDEBUG)
87 # define CHECK_INT_REG(r) if ((r)<0 || (r)>15) printf("CHECK_INT_REG: this is not an integer register: %d\n", r); assert((r)>=0 && (r)<=15)
88 # define CHECK_FLT_REG(r) if ((r)<0 || (r)>7) printf("CHECK_FLT_REG: this is not an float register: %d\n", r); assert((r)>=0 && (r)<=7)
89 # define CHECK_OFFSET(off,max) \
90         if (!IS_OFFSET(off,max)) printf("CHECK_OFFSET: offset out of range: %x (>%x) SEVERE ERROR!!!\n", ((off)<0)?-(off):off, max); \
91         assert(IS_OFFSET(off,max))
92 #else
93 # define CHECK_INT_REG(r)
94 # define CHECK_FLT_REG(r)
95 # define CHECK_OFFSET(off,max)
96 #endif
97
98
99 /* branch defines *************************************************************/
100
101 #define BRANCH_NOPS \
102     do { \
103         M_NOP; \
104     } while (0)
105
106
107 /* patcher defines ************************************************************/
108
109 #define PATCHER_CALL_SIZE    1 * 4      /* an instruction is 4-bytes long     */
110
111 #define PATCHER_NOPS \
112     do { \
113         M_NOP; \
114     } while (0)
115
116
117 /* stub defines ***************************************************************/
118
119 #define COMPILERSTUB_CODESIZE    2 * 4
120
121
122 /* lazy debugger **************************************************************/
123
124 #if !defined(NDEBUG)
125 void asm_debug(int a1, int a2, int a3, int a4);
126 void asm_debug_intern(int a1, int a2, int a3, int a4);
127
128 /* if called with this macros, it can be placed nearly anywhere */
129 /* almost all registers are saved and restored afterwards       */
130 /* it uses a long branch to call the asm_debug_intern (no exit) */
131 #define ASM_DEBUG_PREPARE \
132         M_STMFD(0x7fff, REG_SP)
133 #define ASM_DEBUG_EXECUTE \
134         M_LONGBRANCH(asm_debug_intern); \
135         M_LDMFD(0x7fff, REG_SP)
136 #endif
137
138
139 /* macros to create code ******************************************************/
140
141 /* the condition field */
142 #define COND_EQ 0x0  /* Equal        Z set   */
143 #define COND_NE 0x1  /* Not equal    Z clear */
144 #define COND_CS 0x2  /* Carry set    C set   */
145 #define COND_CC 0x3  /* Carry clear  C clear */
146 #define COND_MI 0x4  /* Negative     N set   */
147 #define COND_PL 0x5  /* Positive     N clear */
148 #define COND_VS 0x6  /* Overflow     V set   */
149 #define COND_VC 0x7  /* No overflow  V clear */
150 #define COND_HI 0x8  /* Unsigned higher      */
151 #define COND_LS 0x9  /* Unsigned lower, same */
152 #define COND_GE 0xA  /* Sig. greater, equal  */
153 #define COND_LT 0xB  /* Sig. less than       */
154 #define COND_GT 0xC  /* Sig. greater than    */
155 #define COND_LE 0xD  /* Sig. less, equal     */
156 #define COND_AL 0xE  /* Always               */
157 #define CONDNV  0xF  /* Special (see A3-5)   */
158 #define UNCOND COND_AL
159
160 /* data processing operation: M_DAT
161    cond ... conditional execution
162    op ..... opcode
163    d ...... destination reg
164    n ...... source reg
165    S ...... update condition codes
166    I ...... switch to immediate mode
167    shift .. shifter operand
168 */
169
170 #define M_DAT(cond,op,d,n,S,I,shift) \
171     do { \
172         *((u4 *) cd->mcodeptr) = (((cond) << 28) | ((op) << 21) | ((d) << 12) | ((n) << 16) | ((I) << 25) | ((S) << 20) | ((shift) & 0x00000fff)); \
173         cd->mcodeptr += 4; \
174     } while (0)
175
176
177 /* load and store instruction: M_MEM
178    cond ... conditional execution
179    L ...... load (L=1) or store (L=0)
180    B ...... unsigned byte (B=1) or word (B=0)
181    d ...... destination reg
182    n ...... base reg for addressing
183    adr .... addressing mode specific
184 */
185
186 #define M_MEM(cond,L,B,d,n,adr,I,P,U,W) \
187     do { \
188         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (1 << 26) | ((L) << 20) | ((B) << 22) | ((d) << 12) | ((n) << 16) | ((adr) & 0x0fff) | ((I) << 25) | ((P) << 24) | ((U) << 23) | ((W) << 21)); \
189         cd->mcodeptr += 4; \
190     } while (0)
191
192
193 /* load and store instruction: M_MEM2
194    cond ... conditional execution
195    L ...... load (L=1) or store (L=0)
196    H ...... halfword (H=1) or signed byte (H=0)
197    S ...... signed (S=1) or unsigned (S=0) halfword
198    d ...... destination reg
199    n ...... base reg for addressing
200    adr .... addressing mode specific
201 */
202
203 #define M_MEM2(cond,L,H,S,d,n,adr,I,P,U,W) \
204     do { \
205         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (1 << 22) | (0x9 << 4) | ((L) << 20) | ((H) << 5) | ((S) << 6) | ((d) << 12) | ((n) << 16) | ((adr) & 0x0f) | (((adr) & 0xf0) << (8-4)) | ((I) << 22) | ((P) << 24) | ((U) << 23) | ((W) << 21)); \
206         cd->mcodeptr += 4; \
207     } while (0)
208
209
210 /* load and store multiple instruction: M_MEM_MULTI
211    cond ... conditional execution
212    L ...... load (L=1) or store (L=0)
213    S ...... special (see "The ARM ARM A3-21")
214    regs ... register list
215    n ...... base reg for addressing
216 */
217
218 #define M_MEM_MULTI(cond,L,S,regs,n,P,U,W) \
219     do { \
220         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (1 << 27) | ((L) << 20) | ((S) << 22) | ((n) << 16) | ((regs) & 0xffff) | ((P) << 24) | ((U) << 23) | ((W) << 21)); \
221         cd->mcodeptr += 4; \
222     } while (0)
223
224
225 /* branch and branch with link: M_BRA
226    cond ... conditional execution
227    L ...... branch with link (L=1)
228    offset . 24bit offset
229 */
230
231 #define M_BRA(cond,L,offset) \
232     do { \
233         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x5 << 25) | ((L) << 24) | ((offset) & 0x00ffffff)); \
234         cd->mcodeptr += 4; \
235     } while (0)
236
237
238 /* multiplies: M_MULT
239    cond ... conditional execution
240    d ...... destination register
241    n, m ... source registers
242    S ...... update conditional codes
243    A ...... accumulate flag (enables third source)
244    s ...... third source register
245 */
246
247 #define M_MULT(cond,d,n,m,S,A,s) \
248     do { \
249         *((u4 *) cd->mcodeptr) = (((cond) << 28) | ((d) << 16) | ((n) << 8) | (m) | (0x09 << 4) | ((S) << 20) | ((A) << 21) | ((s) << 12)); \
250         cd->mcodeptr += 4; \
251     } while (0)
252
253
254 /* no operation (mov r0,r0): M_NOP */
255
256 #define M_NOP \
257     do { \
258         *((u4 *) cd->mcodeptr) = (0xe1a00000); \
259         cd->mcodeptr += 4; \
260     } while (0)
261
262
263 /* software breakpoint (only v5 and above): M_BREAKPOINT */
264
265 #define M_BREAKPOINT(imm) \
266     do { \
267         *((u4 *) cd->mcodeptr) = (0x0e12 << 20) | (0x07 << 4) | (((imm) & 0xfff0) << (8-4)) | ((imm) & 0x0f); \
268         cd->mcodeptr += 4; \
269     } while (0)
270
271
272 /* undefined instruction used for hardware exceptions */
273
274 #define M_UNDEFINED(cond,imm,n) \
275         do { \
276                 *((u4 *) cd->mcodeptr) = ((cond) << 28) | (0x7f << 20) | (((imm) & 0x0fff) << 8) | (0x0f << 4) | (n); \
277                 cd->mcodeptr += 4; \
278         } while (0)
279
280
281 #if !defined(ENABLE_SOFTFLOAT)
282
283 /* M_CPDO **********************************************************************
284
285    Floating-Point Coprocessor Data Operations
286
287    cond ... conditional execution
288    op ..... opcode
289    D ...... dyadic (D=0) or monadic (D=1) instruction
290    Fd ..... destination float-register
291    Fn ..... source float-register
292    Fm ..... source float-register or immediate
293
294 *******************************************************************************/
295
296 #define M_CPDOS(cond,op,D,Fd,Fn,Fm) \
297     do { \
298         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0e << 24) | (1 << 8) | ((op) << 20) | ((D) << 15) | ((Fd) << 12) | ((Fn) << 16) | ((Fm) & 0x0f)); \
299         cd->mcodeptr += 4; \
300     } while (0)
301
302
303 #define M_CPDOD(cond,op,D,Fd,Fn,Fm) \
304     do { \
305         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0e << 24) | (1 << 8) | ((op) << 20) | ((D) << 15) | ((Fd) << 12) | ((Fn) << 16) | ((Fm) & 0x0f) | (1 << 7)); \
306         cd->mcodeptr += 4; \
307     } while (0)
308
309
310 /* M_CPDT **********************************************************************
311
312    Floating-Point Coprocessor Data Transfer
313
314    cond ... conditional execution
315    L ...... load (L=1) or store (L=0)
316    Fd ..... destination float-register
317    n ...... base reg for addressing
318
319 *******************************************************************************/
320
321 #define M_CPDT(cond,L,T1,T0,Fd,n,off,P,U,W) \
322     do { \
323         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0c << 24) | (1 << 8) | ((L) << 20) | ((T1) << 22) | ((T0) << 15) | ((Fd) << 12) | ((n) << 16) | ((off) & 0xff) | ((P) << 24) | ((U) << 23) | ((W) << 21)); \
324         cd->mcodeptr += 4; \
325     } while (0)
326
327
328 /* M_CPRT **********************************************************************
329
330    Floating-Point Coprocessor Register Transfer
331
332    XXX
333
334 *******************************************************************************/
335
336 #define M_CPRTS(cond,L,d,Fn,Fm) \
337     do { \
338         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0e << 24) | (1 << 8) | (1 << 4) | ((L) << 20) | ((d) << 12) | ((Fn) << 16) | (Fm)); \
339         cd->mcodeptr += 4; \
340     } while (0)
341
342
343 #define M_CPRTD(cond,L,d,Fn,Fm) \
344     do { \
345         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0e << 24) | (1 << 8) | (1 << 4) | ((L) << 20) | ((d) << 12) | ((Fn) << 16) | (Fm) | (1 << 7)); \
346         cd->mcodeptr += 4; \
347     } while (0)
348
349
350 #define M_CPRTI(cond,L,d,Fn,Fm) \
351     do { \
352         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0e << 24) | (1 << 8) | (1 << 4) | ((L) << 20) | ((d) << 12) | ((Fn) << 16) | (Fm) | (3 << 5)); \
353         cd->mcodeptr += 4; \
354     } while (0)
355
356
357 /* XXX TWISTI: replace X by something useful */
358
359 #define M_CPRTX(cond,L,d,Fn,Fm) \
360     do { \
361         *((u4 *) cd->mcodeptr) = (((cond) << 28) | (0x0e << 24) | (1 << 8) | (1 << 4) | ((L) << 20) | ((d) << 12) | ((Fn) << 16) | (Fm) | (1 << 23)); \
362         cd->mcodeptr += 4; \
363     } while (0)
364
365 #endif /* !defined(ENABLE_SOFTFLOAT) */
366
367
368 /* used to store values! */
369 #define DCD(val) \
370     do { \
371         *((u4 *) cd->mcodeptr) = val; \
372         cd->mcodeptr += 4; \
373     } while (0)
374
375
376 /* used to directly access shifter; insert this as shifter operand! */
377 #define REG_LSL(reg, shift) ( (((shift) & 0x1f) << 7) | ((reg) & 0x0f) )
378 #define REG_LSR(reg, shift) ( (((shift) & 0x1f) << 7) | ((reg) & 0x0f) | (1 << 5) )
379 #define REG_ASR(reg, shift) ( (((shift) & 0x1f) << 7) | ((reg) & 0x0f) | (1 << 6) )
380 #define REG_LSL_REG(reg, s) ( (((s) & 0x0f) << 8) | ((reg) & 0x0f) | (1 << 4) )
381 #define REG_LSR_REG(reg, s) ( (((s) & 0x0f) << 8) | ((reg) & 0x0f) | (1 << 4) | (1 << 5) )
382 #define REG_ASR_REG(reg, s) ( (((s) & 0x0f) << 8) | ((reg) & 0x0f) | (1 << 4) | (1 << 6) )
383
384 /* used to directly rotate immediate values; insert this as immediate! */
385 /* ATTENTION: this rotates the immediate right by (2 * rot) bits */
386 #define IMM_ROTR(imm, rot) ( ((imm) & 0xff) | (((rot) & 0x0f) << 8) )
387 #define IMM_ROTL(imm, rot) IMM_ROTR(imm, 16-(rot))
388
389 /* macros for all arm instructions ********************************************/
390
391 #define M_ADD(d,a,b)       M_DAT(UNCOND,0x04,d,a,0,0,b)         /* d = a +  b */
392 #define M_ADC(d,a,b)       M_DAT(UNCOND,0x05,d,a,0,0,b)         /* d = a +  b (with Carry) */
393 #define M_SUB(d,a,b)       M_DAT(UNCOND,0x02,d,a,0,0,b)         /* d = a -  b */
394 #define M_SBC(d,a,b)       M_DAT(UNCOND,0x06,d,a,0,0,b)         /* d = a -  b (with Carry) */
395 #define M_AND(a,b,d)       M_DAT(UNCOND,0x00,d,a,0,0,b)         /* d = a &  b */
396 #define M_ORR(a,b,d)       M_DAT(UNCOND,0x0c,d,a,0,0,b)         /* d = a |  b */
397 #define M_EOR(a,b,d)       M_DAT(UNCOND,0x01,d,a,0,0,b)         /* d = a ^  b */
398 #define M_TST(a,b)         M_DAT(UNCOND,0x08,0,a,1,0,b)         /* TST a &  b */
399 #define M_TEQ(a,b)         M_DAT(UNCOND,0x09,0,a,1,0,b)         /* TST a ^  b */
400 #define M_CMP(a,b)         M_DAT(UNCOND,0x0a,0,a,1,0,b)         /* TST a -  b */
401 #define M_MOV(d,b)         M_DAT(UNCOND,0x0d,d,0,0,0,b)         /* d =      b */
402 #define M_ADD_S(d,a,b)     M_DAT(UNCOND,0x04,d,a,1,0,b)         /* d = a +  b (update Flags) */
403 #define M_SUB_S(d,a,b)     M_DAT(UNCOND,0x02,d,a,1,0,b)         /* d = a -  b (update Flags) */
404 #define M_ORR_S(a,b,d)     M_DAT(UNCOND,0x0c,d,a,1,0,b)         /* d = a |  b (update flags) */
405 #define M_MOV_S(d,b)       M_DAT(UNCOND,0x0d,d,0,1,0,b)         /* d =      b (update Flags) */
406
407 #define M_ADD_IMM(d,a,i)   M_DAT(UNCOND,0x04,d,a,0,1,i)         /* d = a +  i */
408 #define M_ADC_IMM(d,a,i)   M_DAT(UNCOND,0x05,d,a,0,1,i)         /* d = a +  i (with Carry) */
409 #define M_SUB_IMM(d,a,i)   M_DAT(UNCOND,0x02,d,a,0,1,i)         /* d = a -  i */
410 #define M_SBC_IMM(d,a,i)   M_DAT(UNCOND,0x06,d,a,0,1,i)         /* d = a -  i (with Carry) */
411 #define M_RSB_IMM(d,a,i)   M_DAT(UNCOND,0x03,d,a,0,1,i)         /* d = -a + i */
412 #define M_RSC_IMM(d,a,i)   M_DAT(UNCOND,0x07,d,a,0,1,i)         /* d = -a + i (with Carry) */
413 #define M_AND_IMM(a,i,d)   M_DAT(UNCOND,0x00,d,a,0,1,i)         /* d = a &  i */
414 #define M_TST_IMM(a,i)     M_DAT(UNCOND,0x08,0,a,1,1,i)         /* TST a &  i */
415 #define M_TEQ_IMM(a,i)     M_DAT(UNCOND,0x09,0,a,1,1,i)         /* TST a ^  i */
416 #define M_CMP_IMM(a,i)     M_DAT(UNCOND,0x0a,0,a,1,1,i)         /* TST a -  i */
417 #define M_CMN_IMM(a,i)     M_DAT(UNCOND,0x0b,0,a,1,1,i)         /* TST a +  i */
418 #define M_MOV_IMM(d,i)     M_DAT(UNCOND,0x0d,d,0,0,1,i)         /* d =      i */
419 #define M_ADD_IMMS(d,a,i)  M_DAT(UNCOND,0x04,d,a,1,1,i)         /* d = a +  i (update Flags) */
420 #define M_SUB_IMMS(d,a,i)  M_DAT(UNCOND,0x02,d,a,1,1,i)         /* d = a -  i (update Flags) */
421 #define M_RSB_IMMS(d,a,i)  M_DAT(UNCOND,0x03,d,a,1,1,i)         /* d = -a + i (update Flags) */
422
423 #define M_ADDSUB_IMM(d,a,i) if((i)>=0) M_ADD_IMM(d,a,i); else M_SUB_IMM(d,a,-(i))
424 #define M_MOVEQ(a,d)       M_DAT(COND_EQ,0x0d,d,0,0,0,a)
425 #define M_EORLE(d,a,b)     M_DAT(COND_LE,0x01,d,a,0,0,b)
426
427 #define M_MOVVS_IMM(i,d)   M_DAT(COND_VS,0x0d,d,0,0,1,i)
428 #define M_MOVEQ_IMM(i,d)   M_DAT(COND_EQ,0x0d,d,0,0,1,i)
429 #define M_MOVNE_IMM(i,d)   M_DAT(COND_NE,0x0d,d,0,0,1,i)
430 #define M_MOVLT_IMM(i,d)   M_DAT(COND_LT,0x0d,d,0,0,1,i)
431 #define M_MOVGT_IMM(i,d)   M_DAT(COND_GT,0x0d,d,0,0,1,i)
432 #define M_MOVLS_IMM(i,d)   M_DAT(COND_LS,0x0d,d,0,0,1,i)
433
434 #define M_ADDHI_IMM(d,a,i) M_DAT(COND_HI,0x04,d,a,0,1,i)
435 #define M_ADDLT_IMM(d,a,i) M_DAT(COND_LT,0x04,d,a,0,1,i)
436 #define M_ADDGT_IMM(d,a,i) M_DAT(COND_GT,0x04,d,a,0,1,i)
437 #define M_SUBLO_IMM(d,a,i) M_DAT(COND_CC,0x02,d,a,0,1,i)
438 #define M_SUBLT_IMM(d,a,i) M_DAT(COND_LT,0x02,d,a,0,1,i)
439 #define M_SUBGT_IMM(d,a,i) M_DAT(COND_GT,0x02,d,a,0,1,i)
440 #define M_RSBMI_IMM(d,a,i) M_DAT(COND_MI,0x03,d,a,0,1,i)
441 #define M_ADCMI_IMM(d,a,i) M_DAT(COND_MI,0x05,d,a,0,1,i)
442
443 #define M_CMPEQ(a,b)       M_DAT(COND_EQ,0x0a,0,a,1,0,b)        /* TST a -  b */
444 #define M_CMPLE(a,b)       M_DAT(COND_LE,0x0a,0,a,1,0,b)        /* TST a -  b */
445
446 #define M_CMPEQ_IMM(a,i)   M_DAT(COND_EQ,0x0a,0,a,1,1,i)
447
448 #define M_MUL(d,a,b)       M_MULT(UNCOND,d,a,b,0,0,0x0)         /* d = a *  b */
449
450
451 #define M_LDMFD(regs,base) M_MEM_MULTI(UNCOND,1,0,regs,base,0,1,1)
452 #define M_STMFD(regs,base) M_MEM_MULTI(UNCOND,0,0,regs,base,1,0,1)
453
454 #define M_LDR_INTERN(d,base,off) \
455     do { \
456         CHECK_OFFSET(off, 0x0fff); \
457         M_MEM(UNCOND,1,0,d,base,(((off) < 0) ? -(off) : off),0,1,(((off) < 0) ? 0 : 1),0); \
458     } while (0)
459
460 #define M_STR_INTERN(d,base,off) \
461     do { \
462         CHECK_OFFSET(off, 0x0fff); \
463         M_MEM(UNCOND,0,0,d,base,(((off) < 0) ? -(off) : off),0,1,(((off) < 0) ? 0 : 1),0); \
464     } while (0)
465
466 #define M_LDR_UPDATE(d,base,off) \
467     do { \
468         CHECK_OFFSET(off, 0x0fff); \
469         M_MEM(UNCOND,1,0,d,base,(((off) < 0) ? -(off) : off),0,0,(((off) < 0) ? 0 : 1),0); \
470     } while (0)
471
472 #define M_STR_UPDATE(d,base,off) \
473     do { \
474         CHECK_OFFSET(off,0x0fff); \
475         M_MEM(UNCOND,0,0,d,base,(((off) < 0) ? -(off) : off),0,1,(((off) < 0) ? 0 : 1),1); \
476     } while (0)
477
478
479 #define M_LDRH(d,base,off) \
480     do { \
481         CHECK_OFFSET(off, 0x00ff); \
482         assert(off >= 0); \
483         M_MEM2(UNCOND,1,1,0,d,base,off,1,1,1,0); \
484     } while (0)
485
486 #define M_LDRSH(d,base,off) \
487     do { \
488         CHECK_OFFSET(off, 0x00ff); \
489         assert(off >= 0); \
490         M_MEM2(UNCOND,1,1,1,d,base,off,1,1,1,0); \
491     } while (0)
492
493 #define M_LDRSB(d,base,off) \
494     do { \
495         CHECK_OFFSET(off, 0x00ff); \
496         assert(off >= 0); \
497         M_MEM2(UNCOND,1,0,1,d,base,off,1,1,1,0); \
498     } while (0)
499
500 #define M_STRH(d,base,off) \
501     do { \
502         CHECK_OFFSET(off, 0x00ff); \
503         assert(off >= 0); \
504         M_MEM2(UNCOND,0,1,0,d,base,off,1,1,1,0); \
505     } while (0)
506
507 #define M_STRB(d,base,off) \
508     do { \
509         CHECK_OFFSET(off, 0x0fff); \
510         assert(off >= 0); \
511         M_MEM(UNCOND,0,1,d,base,off,0,1,1,0); \
512     } while (0)
513
514                                                                                                       
515 #if !defined(ENABLE_SOFTFLOAT)
516
517 #define M_LDFS_INTERN(d,base,off) \
518     do { \
519         CHECK_OFFSET(off, 0x03ff); \
520         M_CPDT(UNCOND,1,0,0,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),1,(((off) < 0) ? 0 : 1),0); \
521     } while (0)
522
523 #define M_LDFD_INTERN(d,base,off) \
524     do { \
525         CHECK_OFFSET(off, 0x03ff); \
526         M_CPDT(UNCOND,1,0,1,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),1,(((off) < 0) ? 0 : 1),0); \
527     } while (0)
528
529 #define M_STFS_INTERN(d,base,off) \
530     do { \
531         CHECK_OFFSET(off, 0x03ff); \
532         M_CPDT(UNCOND,0,0,0,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),1,(((off) < 0) ? 0 : 1),0); \
533     } while (0)
534
535 #define M_STFD_INTERN(d,base,off) \
536     do { \
537         CHECK_OFFSET(off, 0x03ff); \
538         M_CPDT(UNCOND,0,0,1,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),1,(((off) < 0) ? 0 : 1),0); \
539     } while (0)
540
541 #define M_LDFS_UPDATE(d,base,off) \
542     do { \
543         CHECK_OFFSET(off, 0x03ff); \
544         M_CPDT(UNCOND,1,0,0,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),0,(((off) < 0) ? 0 : 1),1); \
545     } while (0)
546
547 #define M_LDFD_UPDATE(d,base,off) \
548     do { \
549         CHECK_OFFSET(off, 0x03ff); \
550         M_CPDT(UNCOND,1,0,1,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),0,(((off) < 0) ? 0 : 1),1); \
551     } while (0)
552
553 #define M_STFS_UPDATE(d,base,off) \
554     do { \
555         CHECK_OFFSET(off, 0x03ff); \
556         M_CPDT(UNCOND,0,0,0,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),1,(((off) < 0) ? 0 : 1),1); \
557     } while (0)
558
559 #define M_STFD_UPDATE(d,base,off) \
560     do { \
561         CHECK_OFFSET(off, 0x03ff); \
562         M_CPDT(UNCOND,0,0,1,d,base,(((off) < 0) ? -(off) >> 2 : (off) >> 2),1,(((off) < 0) ? 0 : 1),1); \
563     } while (0)
564
565 #define M_ADFS(d,a,b)      M_CPDOS(UNCOND,0x00,0,d,a,b)         /* d = a +  b */
566 #define M_SUFS(d,a,b)      M_CPDOS(UNCOND,0x02,0,d,a,b)         /* d = a -  b */
567 #define M_RSFS(d,a,b)      M_CPDOS(UNCOND,0x03,0,d,a,b)         /* d = b -  a */
568 #define M_MUFS(d,a,b)      M_CPDOS(UNCOND,0x01,0,d,a,b)         /* d = a *  b */
569 #define M_DVFS(d,a,b)      M_CPDOS(UNCOND,0x04,0,d,a,b)         /* d = a /  b */
570 #define M_RMFS(d,a,b)      M_CPDOS(UNCOND,0x08,0,d,a,b)         /* d = a %  b */
571 #define M_ADFD(d,a,b)      M_CPDOD(UNCOND,0x00,0,d,a,b)         /* d = a +  b */
572 #define M_SUFD(d,a,b)      M_CPDOD(UNCOND,0x02,0,d,a,b)         /* d = a -  b */
573 #define M_RSFD(d,a,b)      M_CPDOD(UNCOND,0x03,0,d,a,b)         /* d = b -  a */
574 #define M_MUFD(d,a,b)      M_CPDOD(UNCOND,0x01,0,d,a,b)         /* d = a *  b */
575 #define M_DVFD(d,a,b)      M_CPDOD(UNCOND,0x04,0,d,a,b)         /* d = a /  b */
576 #define M_RMFD(d,a,b)      M_CPDOD(UNCOND,0x08,0,d,a,b)         /* d = a %  b */
577 #define M_MVFS(d,a)        M_CPDOS(UNCOND,0x00,1,d,0,a)         /* d =      a */
578 #define M_MVFD(d,a)        M_CPDOD(UNCOND,0x00,1,d,0,a)         /* d =      a */
579 #define M_MNFS(d,a)        M_CPDOS(UNCOND,0x01,1,d,0,a)         /* d =    - a */
580 #define M_MNFD(d,a)        M_CPDOD(UNCOND,0x01,1,d,0,a)         /* d =    - a */
581 #define M_CMF(a,b)         M_CPRTX(UNCOND,1,0x0f,a,b)           /* COMPARE a;  b */
582 #define M_FLTS(d,a)        M_CPRTS(UNCOND,0,a,d,0)              /* d = (float) a */
583 #define M_FLTD(d,a)        M_CPRTD(UNCOND,0,a,d,0)              /* d = (float) a */
584 #define M_FIX(d,a)         M_CPRTI(UNCOND,1,d,0,a)              /* d = (int)   a */
585
586 #endif /* !defined(ENABLE_SOFTFLOAT) */
587
588
589 #define M_B(off)           M_BRA(UNCOND,0,off)    /* unconditional branch */
590 #define M_BL(off)          M_BRA(UNCOND,1,off)    /* branch and link      */
591 #define M_BEQ(off)         M_BRA(COND_EQ,0,off)   /* conditional branches */
592 #define M_BNE(off)         M_BRA(COND_NE,0,off)
593 #define M_BGE(off)         M_BRA(COND_GE,0,off)
594 #define M_BGT(off)         M_BRA(COND_GT,0,off)
595 #define M_BLT(off)         M_BRA(COND_LT,0,off)
596 #define M_BLE(off)         M_BRA(COND_LE,0,off)
597 #define M_BHI(off)         M_BRA(COND_HI,0,off)   /* unsigned conditional */
598 #define M_BHS(off)         M_BRA(COND_CS,0,off)
599 #define M_BLO(off)         M_BRA(COND_CC,0,off)
600 #define M_BLS(off)         M_BRA(COND_LS,0,off)
601
602
603 #define M_FMOV(a,b)        M_MVFS(b,a)
604 #define M_DMOV(a,b)        M_MVFD(b,a)
605
606
607 #define M_TRAP(a,i)        M_UNDEFINED(UNCOND,i,a);
608 #define M_TRAPEQ(a,i)      M_UNDEFINED(COND_EQ,i,a);
609 #define M_TRAPLE(a,i)      M_UNDEFINED(COND_LE,i,a);
610 #define M_TRAPHI(a,i)      M_UNDEFINED(COND_HI,i,a);
611 #define M_TRAPHS(a,i)      M_UNDEFINED(COND_CS,i,a);
612
613
614 /* if we do not have double-word load/store command, we can fake them */
615 /* ATTENTION: the original LDRD/STRD of ARMv5e would always use (Rd/Rd+1),
616    so these faked versions are more "powerful" */
617
618 #if defined(__ARMEL__)
619
620 #define M_LDRD_INTERN(d,base,off) \
621     do { \
622         M_LDR_INTERN(GET_LOW_REG(d), base, off); \
623         M_LDR_INTERN(GET_HIGH_REG(d), base, (off) + 4); \
624     } while (0)
625
626 #define M_STRD_INTERN(d,base,off) \
627     do { \
628         M_STR_INTERN(GET_LOW_REG(d), base, off); \
629         M_STR_INTERN(GET_HIGH_REG(d), base, (off) + 4); \
630     } while (0)
631
632 #define M_LDRD_ALTERN(d,base,off) \
633     do { \
634         M_LDR_INTERN(GET_HIGH_REG(d), base, (off) + 4); \
635         M_LDR_INTERN(GET_LOW_REG(d), base, off); \
636     } while (0)
637
638 #define M_LDRD_UPDATE(d,base,off) \
639     do { \
640         assert((off) == +8); \
641         M_LDR_UPDATE(GET_LOW_REG(d), base, 4); \
642         M_LDR_UPDATE(GET_HIGH_REG(d), base, 4); \
643     } while (0)
644
645 #define M_STRD_UPDATE(d,base,off) \
646     do { \
647         assert((off) == -8); \
648         M_STR_UPDATE(GET_HIGH_REG(d), base, -4); \
649         M_STR_UPDATE(GET_LOW_REG(d), base, -4); \
650     } while (0)
651
652 #define GET_FIRST_REG(d)  GET_LOW_REG(d)
653 #define GET_SECOND_REG(d) GET_HIGH_REG(d)
654
655 #else /* defined(__ARMEB__) */
656
657 #define M_LDRD_INTERN(d,base,off) \
658     do { \
659         M_LDR_INTERN(GET_HIGH_REG(d), base, off); \
660         M_LDR_INTERN(GET_LOW_REG(d), base, (off) + 4); \
661     } while (0)
662
663 #define M_STRD_INTERN(d,base,off) \
664     do { \
665         M_STR_INTERN(GET_HIGH_REG(d), base, off); \
666         M_STR_INTERN(GET_LOW_REG(d), base, (off) + 4); \
667     } while (0)
668
669 #define M_LDRD_ALTERN(d,base,off) \
670     do { \
671         M_LDR_INTERN(GET_LOW_REG(d), base, (off) + 4); \
672         M_LDR_INTERN(GET_HIGH_REG(d), base, off); \
673     } while (0)
674
675 #define M_LDRD_UPDATE(d,base,off) \
676     do { \
677         assert((off) == +8); \
678         M_LDR_UPDATE(GET_HIGH_REG(d), base, 4); \
679         M_LDR_UPDATE(GET_LOW_REG(d), base, 4); \
680     } while (0)
681
682 #define M_STRD_UPDATE(d,base,off) \
683     do { \
684         assert((off) == -8); \
685         M_STR_UPDATE(GET_LOW_REG(d), base, -4); \
686         M_STR_UPDATE(GET_HIGH_REG(d) ,base, -4); \
687     } while (0)
688
689 #define GET_FIRST_REG(d)  GET_HIGH_REG(d)
690 #define GET_SECOND_REG(d) GET_LOW_REG(d)
691
692 #endif /* defined(__ARMEB__) */
693
694
695 /* M_LDR/M_STR:
696    these are replacements for the original LDR/STR instructions, which can
697    handle longer offsets (up to 20bits). the original functions are now
698    called M_xxx_INTERN.
699 */
700 /* ATTENTION: We use ITMP3 here, take into account that it gets destroyed.
701    This means that only ITMP1 and ITMP2 can be used in reg_of_var()!!!
702 */
703 /* ATTENTION2: It is possible to use ITMP3 as base reg. Remember that when
704    changing these macros!!!
705 */
706
707 #define M_LDR(d, base, offset) \
708 do { \
709         CHECK_OFFSET(offset, 0x0fffff); \
710         if (IS_OFFSET(offset, 0x000fff)) { \
711                 M_LDR_INTERN(d, base, offset); \
712         } else { \
713                 /* we cannot handle REG_PC here */ \
714                 assert((d) != REG_PC); \
715                 if ((offset) > 0) { \
716                         M_ADD_IMM(d, base, IMM_ROTL((offset) >> 12, 6)); \
717                         M_LDR_INTERN(d, d, (offset) & 0x000fff); \
718                 } else { \
719                         M_SUB_IMM(d, base, IMM_ROTL((-(offset)) >> 12, 6)); \
720                         M_LDR_INTERN(d, d, -(-(offset) & 0x000fff)); \
721                 } \
722         } \
723 } while (0)
724
725 #define M_LDR_NEGATIVE(d, base, offset) { \
726         /*assert((offset) <= 0);*/ \
727         if (IS_OFFSET(offset, 0x000fff)) { \
728                 M_LDR_INTERN(d, base, offset); \
729         } else { \
730                 /* we cannot handle REG_PC here */ \
731                 assert((d) != REG_PC); \
732                 M_SUB_IMM(d, base, IMM_ROTL((-(offset)) >> 12, 6)); \
733                 M_LDR_INTERN(d, d, -(-(offset) & 0x000fff)); \
734         } \
735 }
736
737 #define M_LDRD(d, base, offset) \
738 do { \
739         CHECK_OFFSET(offset, 0x0fffff - 4); \
740         if (IS_OFFSET(offset, 0x000fff - 4)) { \
741                 if (GET_FIRST_REG(d) != (base)) { \
742                         M_LDRD_INTERN(d, base, offset); \
743                 } else { \
744                         M_LDRD_ALTERN(d, base, offset); \
745                 } \
746         } else if (IS_OFFSET(offset, 0x000fff)) { \
747                 dolog("M_LDRD: this offset seems to be complicated (%d)", offset); \
748                 assert(0); \
749         } else { \
750                 if ((offset) > 0) { \
751                         M_ADD_IMM(GET_SECOND_REG(d), base, IMM_ROTL((offset) >> 12, 6)); \
752                         M_LDRD_INTERN(d, GET_SECOND_REG(d), (offset) & 0x000fff); \
753                 } else { \
754                         M_SUB_IMM(GET_SECOND_REG(d), base, IMM_ROTL((-(offset)) >> 12, 6)); \
755                         M_LDRD_INTERN(d, GET_SECOND_REG(d), -(-(offset) & 0x000fff)); \
756                 } \
757         } \
758 } while (0)
759
760 #if !defined(ENABLE_SOFTFLOAT)
761 #define M_LDFS(d, base, offset) \
762 do { \
763         CHECK_OFFSET(offset, 0x03ffff); \
764         if (IS_OFFSET(offset, 0x03ff)) { \
765                 M_LDFS_INTERN(d, base, offset); \
766         } else { \
767                 if ((offset) > 0) { \
768                         M_ADD_IMM(REG_ITMP3, base, IMM_ROTL((offset) >> 10, 5)); \
769                         M_LDFS_INTERN(d, REG_ITMP3, (offset) & 0x03ff); \
770                 } else { \
771                         M_SUB_IMM(REG_ITMP3, base, IMM_ROTL((-(offset)) >> 10, 5)); \
772                         M_LDFS_INTERN(d, REG_ITMP3, -(-(offset) & 0x03ff)); \
773                 } \
774         } \
775 } while (0)
776
777 #define M_LDFD(d, base, offset) \
778 do { \
779         CHECK_OFFSET(offset, 0x03ffff); \
780         if (IS_OFFSET(offset, 0x03ff)) { \
781                 M_LDFD_INTERN(d, base, offset); \
782         } else { \
783                 if ((offset) > 0) { \
784                         M_ADD_IMM(REG_ITMP3, base, IMM_ROTL((offset) >> 10, 5)); \
785                         M_LDFD_INTERN(d, REG_ITMP3, (offset) & 0x03ff); \
786                 } else { \
787                         M_SUB_IMM(REG_ITMP3, base, IMM_ROTL((-(offset)) >> 10, 5)); \
788                         M_LDFD_INTERN(d, REG_ITMP3, -(-(offset) & 0x03ff)); \
789                 } \
790         } \
791 } while (0)
792
793 #endif /* !defined(ENABLE_SOFTFLOAT) */
794
795 #define M_STR(d, base, offset) \
796 do { \
797         assert((d) != REG_ITMP3); \
798         CHECK_OFFSET(offset, 0x0fffff); \
799         if (IS_OFFSET(offset, 0x000fff)) { \
800                 M_STR_INTERN(d, base, offset); \
801         } else { \
802                 if ((offset) > 0) { \
803                         M_ADD_IMM(REG_ITMP3, base, IMM_ROTL((offset) >> 12, 6)); \
804                         M_STR_INTERN(d, REG_ITMP3, (offset) & 0x000fff); \
805                 } else { \
806                         M_SUB_IMM(REG_ITMP3, base, IMM_ROTL((-(offset)) >> 12, 6)); \
807                         M_STR_INTERN(d, REG_ITMP3, -(-(offset) & 0x000fff)); \
808                 } \
809         } \
810 } while (0)
811
812 #define M_STRD(d, base, offset) \
813 do { \
814         assert(GET_LOW_REG(d) != REG_ITMP3); \
815         assert(GET_HIGH_REG(d) != REG_ITMP3); \
816         CHECK_OFFSET(offset, 0x0fffff - 4); \
817         if (IS_OFFSET(offset, 0x000fff - 4)) { \
818                 M_STRD_INTERN(d,base,offset); \
819         } else if (IS_OFFSET(offset, 0x000fff)) { \
820                 dolog("M_STRD: this offset seems to be complicated (%d)", offset); \
821                 assert(0); \
822         } else { \
823                 if ((offset) > 0) { \
824                         M_ADD_IMM(REG_ITMP3, base, IMM_ROTL((offset) >> 12, 6)); \
825                         M_STRD_INTERN(d, REG_ITMP3, (offset) & 0x000fff); \
826                 } else { \
827                         M_SUB_IMM(REG_ITMP3, base, IMM_ROTL((-(offset)) >> 12, 6)); \
828                         M_STRD_INTERN(d, REG_ITMP3, -(-(offset) & 0x000fff)); \
829                 } \
830         } \
831 } while (0)
832
833 #if !defined(ENABLE_SOFTFLOAT)
834
835 #define M_STFS(d, base, offset) \
836 do { \
837         CHECK_OFFSET(offset, 0x03ffff); \
838         if (IS_OFFSET(offset, 0x03ff)) { \
839                 M_STFS_INTERN(d, base, offset); \
840         } else { \
841                 if ((offset) > 0) { \
842                         M_ADD_IMM(REG_ITMP3, base, IMM_ROTL((offset) >> 10, 5)); \
843                         M_STFS_INTERN(d, REG_ITMP3, (offset) & 0x03ff); \
844                 } else { \
845                         M_SUB_IMM(REG_ITMP3, base, IMM_ROTL((-(offset)) >> 10, 5)); \
846                         M_STFS_INTERN(d, REG_ITMP3, -(-(offset) & 0x03ff)); \
847                 } \
848         } \
849 } while (0)
850
851 #define M_STFD(d, base, offset) \
852 do { \
853         CHECK_OFFSET(offset, 0x03ffff); \
854         if (IS_OFFSET(offset, 0x03ff)) { \
855                 M_STFD_INTERN(d, base, offset); \
856         } else { \
857                 if ((offset) > 0) { \
858                         M_ADD_IMM(REG_ITMP3, base, IMM_ROTL((offset) >> 10, 5)); \
859                         M_STFD_INTERN(d, REG_ITMP3, (offset) & 0x03ff); \
860                 } else { \
861                         M_SUB_IMM(REG_ITMP3, base, IMM_ROTL((-(offset)) >> 10, 5)); \
862                         M_STFD_INTERN(d, REG_ITMP3, -(-(offset) & 0x03ff)); \
863                 } \
864         } \
865 } while (0)
866
867 #endif /* !defined(ENABLE_SOFTFLOAT) */
868
869 /* M_???_IMM_EXT_MUL4:
870    extended immediate operations, to handle immediates lager than 8bit.
871    ATTENTION: the immediate is rotatet left by 2 (multiplied by 4)!!!
872 */
873
874 #define M_ADD_IMM_EXT_MUL4(d,n,imm) \
875     do { \
876         assert(d != REG_PC); \
877         assert((imm) >= 0 && (imm) <= 0x00ffffff); \
878         M_ADD_IMM(d, n, IMM_ROTL(imm, 1)); \
879         if ((imm) > 0x000000ff) M_ADD_IMM(d, d, IMM_ROTL((imm) >>  8, 5)); \
880         if ((imm) > 0x0000ffff) M_ADD_IMM(d, d, IMM_ROTL((imm) >> 16, 9)); \
881     } while (0)
882
883 #define M_SUB_IMM_EXT_MUL4(d,n,imm) \
884     do { \
885         assert(d != REG_PC); \
886         assert((imm) >= 0 && (imm) <= 0x00ffffff); \
887         M_SUB_IMM(d, n, IMM_ROTL(imm, 1)); \
888         if ((imm) > 0x000000ff) M_SUB_IMM(d, d, IMM_ROTL((imm) >>  8, 5)); \
889         if ((imm) > 0x0000ffff) M_SUB_IMM(d, d, IMM_ROTL((imm) >> 16, 9)); \
890     } while (0)
891
892
893 /* ICONST/LCONST:
894    loads the integer/long value const into register d.
895 */
896
897 #define ICONST(d,c)                     emit_iconst(cd, (d), (c))
898
899 #define ICONST_CONDITIONAL(cond,d,const) \
900         if (IS_IMM(const)) { \
901                 /* M_MOV_IMM */ M_DAT(cond,0x0d,d,0,0,1,const); \
902         } else { \
903                 disp = dseg_adds4(cd, const); \
904                 /* TODO: implement this using M_DSEG_LOAD!!! */ \
905                 /* M_LDR_INTERN */ CHECK_OFFSET(disp,0x0fff); M_MEM(cond,1,0,d,REG_PV,(disp<0)?-disp:disp,0,1,(disp<0)?0:1,0); \
906         }
907
908 #define LCONST(d,c) \
909         if (IS_IMM((c) >> 32)) { \
910                 M_MOV_IMM(GET_HIGH_REG(d), (s4) ((s8) (c) >> 32)); \
911                 ICONST(GET_LOW_REG(d), (s4) ((s8) (c) & 0xffffffff)); \
912         } else if (IS_IMM((c) & 0xffffffff)) { \
913                 M_MOV_IMM(GET_LOW_REG(d), (s4) ((s8) (c) & 0xffffffff)); \
914                 ICONST(GET_HIGH_REG(d), (s4) ((s8) (c) >> 32)); \
915         } else { \
916                 disp = dseg_add_s8(cd, (c)); \
917                 M_LDRD(d, REG_PV, disp); \
918         }
919
920
921 #if !defined(ENABLE_SOFTFLOAT)
922
923 #define FCONST(d,c) \
924     do { \
925         disp = dseg_add_float(cd, (c)); \
926         M_LDFS(d, REG_PV, disp); \
927     } while (0)
928
929 #define DCONST(d,c) \
930     do { \
931         disp = dseg_add_double(cd, (c)); \
932         M_LDFD(d, REG_PV, disp); \
933     } while (0)
934
935 #endif /* !defined(ENABLE_SOFTFLOAT) */
936
937
938 /* M_RECOMPUTE_PV:
939    used to recompute our PV (we use the IP for this) out of the current PC
940    ATTENTION: if you change this, you have to look at other functions as well!
941    Following things depend on it: asm_call_jit_compiler(); codegen_findmethod();
942 */
943 #define M_RECOMPUTE_PV(disp) \
944         disp += 8; /* we use PC relative addr.  */ \
945         assert((disp & 0x03) == 0); \
946         assert(disp >= 0 && disp <= 0x03ffffff); \
947         M_SUB_IMM(REG_PV, REG_PC, IMM_ROTL(disp >> 2, 1)); \
948         if (disp > 0x000003ff) M_SUB_IMM(REG_PV, REG_PV, IMM_ROTL(disp >> 10, 5)); \
949         if (disp > 0x0003ffff) M_SUB_IMM(REG_PV, REG_PV, IMM_ROTL(disp >> 18, 9)); \
950
951 /* M_INTMOVE:
952    generates an integer-move from register a to b.
953    if a and b are the same int-register, no code will be generated.
954 */
955
956 #define M_INTMOVE(a,b) \
957     do { \
958         if ((a) != (b)) \
959             M_MOV(b, a); \
960     } while (0)
961
962 #define M_LNGMOVE(a,b) \
963     do { \
964         if (GET_HIGH_REG(a) == GET_LOW_REG(b)) { \
965             assert((GET_LOW_REG(a) != GET_HIGH_REG(b))); \
966             M_INTMOVE(GET_HIGH_REG(a), GET_HIGH_REG(b)); \
967             M_INTMOVE(GET_LOW_REG(a), GET_LOW_REG(b)); \
968         } else { \
969             M_INTMOVE(GET_LOW_REG(a), GET_LOW_REG(b)); \
970             M_INTMOVE(GET_HIGH_REG(a), GET_HIGH_REG(b)); \
971         } \
972     } while (0)
973
974
975 #if !defined(ENABLE_SOFTFLOAT)
976
977 /* M_FLTMOVE:
978    generates a floating-point-move from register a to b.
979    if a and b are the same float-register, no code will be generated.
980 */
981
982 #define M_FLTMOVE(a,b) \
983     do { \
984         if ((a) != (b)) \
985             M_FMOV(a, b); \
986     } while (0)
987
988 #define M_DBLMOVE(a,b) \
989     do { \
990         if ((a) != (b)) \
991             M_DMOV(a, b); \
992     } while (0)
993
994 #endif
995
996 #if !defined(ENABLE_SOFTFLOAT)
997 /* M_CAST_INT_TO_FLT_TYPED:
998    loads the value of the integer-register a (argument or result) into
999    float-register Fb. (and vice versa)
1000 */
1001 #define M_CAST_INT_TO_FLT_TYPED(t,a,Fb) \
1002         CHECK_FLT_REG(Fb); \
1003         if ((t) == TYPE_FLT) { \
1004                 CHECK_INT_REG(a); \
1005                 M_STR_UPDATE(a, REG_SP, -4); \
1006                 M_LDFS_UPDATE(Fb, REG_SP, 4); \
1007         } else { \
1008                 CHECK_INT_REG(GET_LOW_REG(a)); \
1009                 CHECK_INT_REG(GET_HIGH_REG(a)); \
1010                 M_STRD_UPDATE(a, REG_SP, -8); \
1011                 M_LDFD_UPDATE(Fb, REG_SP, 8); \
1012         }
1013 #define M_CAST_FLT_TO_INT_TYPED(t,Fa,b) \
1014         CHECK_FLT_REG(Fa); \
1015         if ((t) == TYPE_FLT) { \
1016                 CHECK_INT_REG(b); \
1017                 M_STFS_UPDATE(Fa, REG_SP, -4); \
1018                 M_LDR_UPDATE(b, REG_SP, 4); \
1019         } else { \
1020                 CHECK_INT_REG(GET_LOW_REG(b)); \
1021                 CHECK_INT_REG(GET_HIGH_REG(b)); \
1022                 M_STFD_UPDATE(Fa, REG_SP, -8); \
1023                 M_LDRD_UPDATE(b, REG_SP, 8); \
1024         }
1025 #endif /* !defined(ENABLE_SOFTFLOAT) */
1026
1027
1028 /* M_COMPARE:
1029    generates the compare part of an if-sequece
1030    uses M_CMP or M_CMP_IMM to do the compare
1031    ATTENTION: uses REG_ITMP3 as intermediate register
1032 */
1033 #define M_COMPARE(reg, val) \
1034         if (IS_IMM(val)) { \
1035                 M_CMP_IMM(reg, (val)); \
1036         } else if(IS_IMM(-(val))) { \
1037                 M_CMN_IMM(reg, -(val)); \
1038         } else { \
1039                 ICONST(REG_ITMP3, (val)); \
1040                 M_CMP(reg, REG_ITMP3); \
1041         }
1042
1043 /* M_LONGBRANCH:
1044    performs a long branch to an absolute address with return address in LR
1045    takes up 3 bytes of code space; address is hard-coded into code
1046 */
1047 #define M_LONGBRANCH(adr) \
1048         M_ADD_IMM(REG_LR, REG_PC, 4); \
1049         M_LDR_INTERN(REG_PC, REG_PC, -4); \
1050         DCD((s4) adr);
1051
1052 /* M_DSEG_LOAD/BRANCH:
1053    TODO: document me
1054    ATTENTION: if you change this, you have to look at the asm_call_jit_compiler!
1055    ATTENTION: we use M_LDR, so the same restrictions apply to us!
1056 */
1057 #define M_DSEG_LOAD(reg, offset) \
1058         M_LDR_NEGATIVE(reg, REG_PV, offset)
1059
1060 #define M_DSEG_BRANCH(offset) \
1061         if (IS_OFFSET(offset, 0x0fff)) { \
1062                 M_MOV(REG_LR, REG_PC); \
1063                 M_LDR_INTERN(REG_PC, REG_PV, offset); \
1064         } else { \
1065                 /*assert((offset) <= 0);*/ \
1066                 CHECK_OFFSET(offset,0x0fffff); \
1067                 M_SUB_IMM(REG_ITMP3, REG_PV, ((-(offset) >>  12) & 0xff) | (((10) & 0x0f) << 8)); /*TODO: more to go*/ \
1068                 M_MOV(REG_LR, REG_PC); \
1069                 M_LDR_INTERN(REG_PC, REG_ITMP3, -(-(offset) & 0x0fff)); /*TODO: this looks ugly*/ \
1070         }
1071
1072
1073 #define M_ILD(a,b,c)                    M_LDR(a,b,c)
1074 #define M_LLD(a,b,c)                    M_LDRD(a,b,c)
1075
1076 #define M_ILD_INTERN(a,b,c)             M_LDR_INTERN(a,b,c)
1077 #define M_LLD_INTERN(a,b,c)             M_LDRD_INTERN(a,b,c)
1078
1079 #define M_ALD(a,b,c)                    M_ILD(a,b,c)
1080 #define M_ALD_INTERN(a,b,c)             M_ILD_INTERN(a,b,c)
1081
1082
1083 #define M_IST(a,b,c)                    M_STR(a,b,c)
1084 #define M_LST(a,b,c)                    M_STRD(a,b,c)
1085
1086 #define M_IST_INTERN(a,b,c)             M_STR_INTERN(a,b,c)
1087 #define M_LST_INTERN(a,b,c)             M_STRD_INTERN(a,b,c)
1088
1089 #define M_AST(a,b,c)                    M_IST(a,b,c)
1090 #define M_AST_INTERN(a,b,c)             M_IST_INTERN(a,b,c)
1091
1092
1093 #if !defined(ENABLE_SOFTFLOAT)
1094
1095 #define M_FLD(a,b,c)                    M_LDFS(a,b,c)
1096 #define M_DLD(a,b,c)                    M_LDFD(a,b,c)
1097
1098 #define M_FLD_INTERN(a,b,c)             M_LDFS_INTERN(a,b,c)
1099 #define M_DLD_INTERN(a,b,c)             M_LDFD_INTERN(a,b,c)
1100
1101
1102 #define M_FST(a,b,c)                    M_STFS(a,b,c)
1103 #define M_DST(a,b,c)                    M_STFD(a,b,c)
1104
1105 #define M_FST_INTERN(a,b,c)             M_STFS_INTERN(a,b,c)
1106 #define M_DST_INTERN(a,b,c)             M_STFD_INTERN(a,b,c)
1107
1108 #endif /* !defined(ENABLE_SOFTFLOAT) */
1109
1110
1111 #endif /* _CODEGEN_H */
1112
1113
1114 /*
1115  * These are local overrides for various environment variables in Emacs.
1116  * Please do not remove this and leave it at the end of the file, where
1117  * Emacs will automagically detect them.
1118  * ---------------------------------------------------------------------
1119  * Local variables:
1120  * mode: c
1121  * indent-tabs-mode: t
1122  * c-basic-offset: 4
1123  * tab-width: 4
1124  * End:
1125  * vim:noexpandtab:sw=4:ts=4:
1126  */