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