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