2 * ARMv4 code generator for TCC
4 * Copyright (c) 2003 Daniel Glöckner
6 * Based on i386-gen.c by Fabrice Bellard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* number of available registers */
35 /* a register can belong to several classes. The classes must be
36 sorted from more general to more precise (see gv2() code which does
37 assumptions on it). */
38 #define RC_INT 0x0001 /* generic integer register */
39 #define RC_FLOAT 0x0002 /* generic float register */
55 #define RC_IRET RC_R0 /* function return: integer register */
56 #define RC_LRET RC_R1 /* function return: second integer register */
57 #define RC_FRET RC_F0 /* function return: float register */
59 /* pretty names for the registers */
78 const int reg_classes
[NB_REGS
] = {
79 /* r0 */ RC_INT
| RC_R0
,
80 /* r1 */ RC_INT
| RC_R1
,
81 /* r2 */ RC_INT
| RC_R2
,
82 /* r3 */ RC_INT
| RC_R3
,
83 /* r12 */ RC_INT
| RC_R12
,
84 /* f0 */ RC_FLOAT
| RC_F0
,
85 /* f1 */ RC_FLOAT
| RC_F1
,
86 /* f2 */ RC_FLOAT
| RC_F2
,
87 /* f3 */ RC_FLOAT
| RC_F3
,
89 /* d4/s8 */ RC_FLOAT
| RC_F4
,
90 /* d5/s10 */ RC_FLOAT
| RC_F5
,
91 /* d6/s12 */ RC_FLOAT
| RC_F6
,
92 /* d7/s14 */ RC_FLOAT
| RC_F7
,
96 static int two2mask(int a
,int b
) {
97 return (reg_classes
[a
]|reg_classes
[b
])&~(RC_INT
|RC_FLOAT
);
100 static int regmask(int r
) {
101 return reg_classes
[r
]&~(RC_INT
|RC_FLOAT
);
105 #define T2CPR(t) (((t) & VT_BTYPE) != VT_FLOAT ? 0x100 : 0)
108 /* return registers for function */
109 #define REG_IRET TREG_R0 /* single word int return register */
110 #define REG_LRET TREG_R1 /* second word return register (for long long) */
111 #define REG_FRET TREG_F0 /* float return register */
114 #define TOK___divdi3 TOK___aeabi_ldivmod
115 #define TOK___moddi3 TOK___aeabi_ldivmod
116 #define TOK___udivdi3 TOK___aeabi_uldivmod
117 #define TOK___umoddi3 TOK___aeabi_uldivmod
120 /* defined if function parameters must be evaluated in reverse order */
121 #define INVERT_FUNC_PARAMS
123 /* defined if structures are passed as pointers. Otherwise structures
124 are directly pushed on stack. */
125 //#define FUNC_STRUCT_PARAM_AS_PTR
127 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
128 static CType float_type
, double_type
, func_float_type
, func_double_type
;
129 #define func_ldouble_type func_double_type
131 #define func_float_type func_old_type
132 #define func_double_type func_old_type
133 #define func_ldouble_type func_old_type
136 /* pointer size, in bytes */
139 /* long double size and alignment, in bytes */
141 #define LDOUBLE_SIZE 8
145 #define LDOUBLE_SIZE 8
149 #define LDOUBLE_ALIGN 8
151 #define LDOUBLE_ALIGN 4
154 /* maximum alignment (for aligned attribute support) */
157 #define CHAR_IS_UNSIGNED
159 /******************************************************/
162 #define EM_TCC_TARGET EM_ARM
164 /* relocation type for 32 bit data relocation */
165 #define R_DATA_32 R_ARM_ABS32
166 #define R_DATA_PTR R_ARM_ABS32
167 #define R_JMP_SLOT R_ARM_JUMP_SLOT
168 #define R_COPY R_ARM_COPY
170 #define ELF_START_ADDR 0x00008000
171 #define ELF_PAGE_SIZE 0x1000
173 /******************************************************/
174 static unsigned long func_sub_sp_offset
,last_itod_magic
;
177 void o(unsigned long i
)
179 /* this is a good place to start adding big-endian support*/
183 if (!cur_text_section
)
184 error("compiler error! This happens f.ex. if the compiler\n"
185 "can't evaluate constant expressions outside of a function.");
186 if (ind1
> cur_text_section
->data_allocated
)
187 section_realloc(cur_text_section
, ind1
);
188 cur_text_section
->data
[ind
++] = i
&255;
190 cur_text_section
->data
[ind
++] = i
&255;
192 cur_text_section
->data
[ind
++] = i
&255;
194 cur_text_section
->data
[ind
++] = i
;
197 static unsigned long stuff_const(unsigned long op
,unsigned long c
)
200 unsigned long nc
= 0,negop
= 0;
210 case 0x1A00000: //mov
211 case 0x1E00000: //mvn
218 return (op
&0xF010F000)|((op
>>16)&0xF)|0x1E00000;
222 return (op
&0xF010F000)|((op
>>16)&0xF)|0x1A00000;
223 case 0x1C00000: //bic
228 case 0x1800000: //orr
230 return (op
&0xFFF0FFFF)|0x1E00000;
236 if(c
<256) /* catch undefined <<32 */
239 m
=(0xff>>i
)|(0xff<<(32-i
));
241 return op
|(i
<<7)|(c
<<i
)|(c
>>(32-i
));
251 void stuff_const_harder(unsigned long op
,unsigned long v
) {
257 unsigned long a
[16],nv
,no
,o2
,n2
;
260 o2
=(op
&0xfff0ffff)|((op
&0xf000)<<4);;
262 a
[i
]=(a
[i
-1]>>2)|(a
[i
-1]<<30);
264 for(j
=i
<4?i
+12:15;j
>=i
+4;j
--)
265 if((v
&(a
[i
]|a
[j
]))==v
) {
266 o(stuff_const(op
,v
&a
[i
]));
267 o(stuff_const(o2
,v
&a
[j
]));
274 for(j
=i
<4?i
+12:15;j
>=i
+4;j
--)
275 if((nv
&(a
[i
]|a
[j
]))==nv
) {
276 o(stuff_const(no
,nv
&a
[i
]));
277 o(stuff_const(n2
,nv
&a
[j
]));
282 for(k
=i
<4?i
+12:15;k
>=j
+4;k
--)
283 if((v
&(a
[i
]|a
[j
]|a
[k
]))==v
) {
284 o(stuff_const(op
,v
&a
[i
]));
285 o(stuff_const(o2
,v
&a
[j
]));
286 o(stuff_const(o2
,v
&a
[k
]));
293 for(k
=i
<4?i
+12:15;k
>=j
+4;k
--)
294 if((nv
&(a
[i
]|a
[j
]|a
[k
]))==nv
) {
295 o(stuff_const(no
,nv
&a
[i
]));
296 o(stuff_const(n2
,nv
&a
[j
]));
297 o(stuff_const(n2
,nv
&a
[k
]));
300 o(stuff_const(op
,v
&a
[0]));
301 o(stuff_const(o2
,v
&a
[4]));
302 o(stuff_const(o2
,v
&a
[8]));
303 o(stuff_const(o2
,v
&a
[12]));
307 unsigned long encbranch(int pos
,int addr
,int fail
)
311 if(addr
>=0x1000000 || addr
<-0x1000000) {
313 error("FIXME: function bigger than 32MB");
316 return 0x0A000000|(addr
&0xffffff);
319 int decbranch(int pos
)
322 x
=*(int *)(cur_text_section
->data
+ pos
);
329 /* output a symbol and patch all calls to it */
330 void gsym_addr(int t
, int a
)
335 x
=(unsigned long *)(cur_text_section
->data
+ t
);
338 *x
=0xE1A00000; // nop
341 *x
|= encbranch(lt
,a
,1);
352 static unsigned long vfpr(int r
)
354 if(r
<TREG_F0
|| r
>TREG_F7
)
355 error("compiler error! register %i is no vfp register",r
);
359 static unsigned long fpr(int r
)
361 if(r
<TREG_F0
|| r
>TREG_F3
)
362 error("compiler error! register %i is no fpa register",r
);
367 static unsigned long intr(int r
)
371 if((r
<0 || r
>4) && r
!=14)
372 error("compiler error! register %i is no int register",r
);
376 static void calcaddr(unsigned long *base
,int *off
,int *sgn
,int maxoff
,unsigned shift
)
378 if(*off
>maxoff
|| *off
&((1<<shift
)-1)) {
385 y
=stuff_const(x
,*off
&~maxoff
);
391 y
=stuff_const(x
,(*off
+maxoff
)&~maxoff
);
395 *off
=((*off
+maxoff
)&~maxoff
)-*off
;
398 stuff_const_harder(x
,*off
&~maxoff
);
403 static unsigned long mapcc(int cc
)
408 return 0x30000000; /* CC/LO */
410 return 0x20000000; /* CS/HS */
412 return 0x00000000; /* EQ */
414 return 0x10000000; /* NE */
416 return 0x90000000; /* LS */
418 return 0x80000000; /* HI */
420 return 0x40000000; /* MI */
422 return 0x50000000; /* PL */
424 return 0xB0000000; /* LT */
426 return 0xA0000000; /* GE */
428 return 0xD0000000; /* LE */
430 return 0xC0000000; /* GT */
432 error("unexpected condition code");
433 return 0xE0000000; /* AL */
436 static int negcc(int cc
)
465 error("unexpected condition code");
469 /* load 'r' from value 'sv' */
470 void load(int r
, SValue
*sv
)
472 int v
, ft
, fc
, fr
, sign
;
489 unsigned long base
=0xB; // fp
492 v1
.r
= VT_LOCAL
| VT_LVAL
;
494 load(base
=14 /* lr */, &v1
);
497 } else if(v
== VT_CONST
) {
505 } else if(v
< VT_CONST
) {
512 calcaddr(&base
,&fc
,&sign
,1020,2);
514 op
=0xED100A00; /* flds */
517 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
518 op
|=0x100; /* flds -> fldd */
519 o(op
|(vfpr(r
)<<12)|(fc
>>2)|(base
<<16));
524 #if LDOUBLE_SIZE == 8
525 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
528 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
530 else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
)
533 o(op
|(fpr(r
)<<12)|(fc
>>2)|(base
<<16));
535 } else if((ft
& (VT_BTYPE
|VT_UNSIGNED
)) == VT_BYTE
536 || (ft
& VT_BTYPE
) == VT_SHORT
) {
537 calcaddr(&base
,&fc
,&sign
,255,0);
539 if ((ft
& VT_BTYPE
) == VT_SHORT
)
541 if ((ft
& VT_UNSIGNED
) == 0)
545 o(op
|(intr(r
)<<12)|(base
<<16)|((fc
&0xf0)<<4)|(fc
&0xf));
547 calcaddr(&base
,&fc
,&sign
,4095,0);
551 if ((ft
& VT_BTYPE
) == VT_BYTE
)
553 o(op
|(intr(r
)<<12)|fc
|(base
<<16));
559 op
=stuff_const(0xE3A00000|(intr(r
)<<12),sv
->c
.ul
);
560 if (fr
& VT_SYM
|| !op
) {
561 o(0xE59F0000|(intr(r
)<<12));
564 greloc(cur_text_section
, sv
->sym
, ind
, R_ARM_ABS32
);
569 } else if (v
== VT_LOCAL
) {
570 op
=stuff_const(0xE28B0000|(intr(r
)<<12),sv
->c
.ul
);
571 if (fr
& VT_SYM
|| !op
) {
572 o(0xE59F0000|(intr(r
)<<12));
574 if(fr
& VT_SYM
) // needed ?
575 greloc(cur_text_section
, sv
->sym
, ind
, R_ARM_ABS32
);
577 o(0xE08B0000|(intr(r
)<<12)|intr(r
));
581 } else if(v
== VT_CMP
) {
582 o(mapcc(sv
->c
.ul
)|0x3A00001|(intr(r
)<<12));
583 o(mapcc(negcc(sv
->c
.ul
))|0x3A00000|(intr(r
)<<12));
585 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
588 o(0xE3A00000|(intr(r
)<<12)|t
);
591 o(0xE3A00000|(intr(r
)<<12)|(t
^1));
593 } else if (v
< VT_CONST
) {
596 o(0xEEB00A40|(vfpr(r
)<<12)|vfpr(v
)|T2CPR(ft
)); /* fcpyX */
598 o(0xEE008180|(fpr(r
)<<12)|fpr(v
));
601 o(0xE1A00000|(intr(r
)<<12)|intr(v
));
605 error("load unimplemented!");
608 /* store register 'r' in lvalue 'v' */
609 void store(int r
, SValue
*sv
)
612 int v
, ft
, fc
, fr
, sign
;
627 if (fr
& VT_LVAL
|| fr
== VT_LOCAL
) {
628 unsigned long base
=0xb;
633 } else if(v
== VT_CONST
) {
644 calcaddr(&base
,&fc
,&sign
,1020,2);
646 op
=0xED000A00; /* fsts */
649 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
650 op
|=0x100; /* fsts -> fstd */
651 o(op
|(vfpr(r
)<<12)|(fc
>>2)|(base
<<16));
656 #if LDOUBLE_SIZE == 8
657 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
660 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
662 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
)
665 o(op
|(fpr(r
)<<12)|(fc
>>2)|(base
<<16));
668 } else if((ft
& VT_BTYPE
) == VT_SHORT
) {
669 calcaddr(&base
,&fc
,&sign
,255,0);
673 o(op
|(intr(r
)<<12)|(base
<<16)|((fc
&0xf0)<<4)|(fc
&0xf));
675 calcaddr(&base
,&fc
,&sign
,4095,0);
679 if ((ft
& VT_BTYPE
) == VT_BYTE
)
681 o(op
|(intr(r
)<<12)|fc
|(base
<<16));
686 error("store unimplemented");
689 static void gadd_sp(int val
)
691 stuff_const_harder(0xE28DD000,val
);
694 /* 'is_jmp' is '1' if it is a jump */
695 static void gcall_or_jmp(int is_jmp
)
698 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
701 x
=encbranch(ind
,ind
+vtop
->c
.ul
,0);
703 if (vtop
->r
& VT_SYM
) {
704 /* relocation case */
705 greloc(cur_text_section
, vtop
->sym
, ind
, R_ARM_PC24
);
707 put_elf_reloc(symtab_section
, cur_text_section
, ind
, R_ARM_PC24
, 0);
708 o(x
|(is_jmp
?0xE0000000:0xE1000000));
711 o(0xE28FE004); // add lr,pc,#4
712 o(0xE51FF004); // ldr pc,[pc,#-4]
713 if (vtop
->r
& VT_SYM
)
714 greloc(cur_text_section
, vtop
->sym
, ind
, R_ARM_ABS32
);
718 /* otherwise, indirect call */
721 o(0xE1A0E00F); // mov lr,pc
722 o(0xE1A0F000|intr(r
)); // mov pc,r
726 /* Generate function call. The function address is pushed first, then
727 all the parameters in call order. This functions pops all the
728 parameters and the function address. */
729 void gfunc_call(int nb_args
)
731 int size
, align
, r
, args_size
, i
;
733 signed char plan
[4][2]={{-1,-1},{-1,-1},{-1,-1},{-1,-1}};
734 int todo
=0xf, keep
, plan2
[4]={0,0,0,0};
736 r
= vtop
->r
& VT_VALMASK
;
737 if (r
== VT_CMP
|| (r
& ~1) == VT_JMP
)
740 if((vtop
[-nb_args
].type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
741 && type_size(&vtop
[-nb_args
].type
, &align
) <= 4) {
744 vtop
[-nb_args
]=vtop
[-nb_args
+1];
745 vtop
[-nb_args
+1]=tmp
;
750 vtop
->type
.t
= VT_LLONG
;
752 for(i
= nb_args
+ 1 ; i
-- ;) {
753 size
= type_size(&vtop
[-i
].type
, &align
);
754 if(args_size
& (align
-1)) {
756 vtop
->type
.t
= VT_VOID
; /* padding */
761 args_size
+= (size
+ 3) & -4;
766 for(i
= nb_args
; i
-- && args_size
< 16 ;) {
767 switch(vtop
[-i
].type
.t
& VT_BTYPE
) {
772 size
= type_size(&vtop
[-i
].type
, &align
);
773 size
= (size
+ 3) & -4;
777 plan
[nb_args
-1-i
][0]=args_size
/4;
779 if ((vtop
[-i
].type
.t
& VT_BTYPE
) == VT_LLONG
&& args_size
< 16) {
780 plan
[nb_args
-1-i
][1]=args_size
/4;
785 args_size
= keep
= 0;
786 for(i
= 0;i
< nb_args
; i
++) {
788 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
789 size
= type_size(&vtop
->type
, &align
);
790 /* align to stack align size */
791 size
= (size
+ 3) & -4;
792 /* allocate the necessary size on stack */
794 /* generate structure store */
796 o(0xE1A0000D|(intr(r
)<<12));
797 vset(&vtop
->type
, r
| VT_LVAL
, 0);
802 } else if (is_float(vtop
->type
.t
)) {
804 r
=vfpr(gv(RC_FLOAT
))<<12;
806 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FLOAT
)
809 r
|=0x101; /* fstms -> fstmd */
813 r
=fpr(gv(RC_FLOAT
))<<12;
814 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
816 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
826 o(0xED2D0100|r
|(size
>>2));
832 /* simple type (currently always same size) */
833 /* XXX: implicit cast ? */
835 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
838 if(nb_args
-i
<5 && plan
[nb_args
-i
-1][1]!=-1) {
839 s
=regmask(plan
[nb_args
-i
-1][1]);
840 todo
&=~(1<<plan
[nb_args
-i
-1][1]);
844 o(0xE52D0004|(intr(r
)<<12)); /* str r,[sp,#-4]! */
854 if(nb_args
-i
<5 && plan
[nb_args
-i
-1][0]!=-1) {
855 s
=regmask(plan
[nb_args
-i
-1][0]);
856 todo
&=~(1<<plan
[nb_args
-i
-1][0]);
859 if(vtop
->type
.t
== VT_VOID
) {
861 o(0xE24DD004); /* sub sp,sp,#4 */
867 o(0xE52D0004|(intr(r
)<<12)); /* str r,[sp,#-4]! */
880 save_regs(keep
); /* save used temporary registers */
901 func_sym
= vtop
->type
.ref
;
906 if((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
907 && type_size(&vtop
->type
.ref
->type
, &align
) <= 4)
909 store(REG_IRET
,vtop
-keep
);
913 else if(is_float(vtop
->type
.ref
->type
.t
)) {
914 if((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
915 o(0xEE000A10); /* fmsr s0,r0 */
917 o(0xEE000B10); /* fmdlr d0,r0 */
918 o(0xEE201B10); /* fmdhr d0,r1 */
927 /* generate function prolog of type 't' */
928 void gfunc_prolog(CType
*func_type
)
931 int n
,addr
,size
,align
;
933 sym
= func_type
->ref
;
938 if((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
939 && type_size(&func_vt
,&align
) > 4)
945 for(sym2
=sym
->next
;sym2
&& n
<4;sym2
=sym2
->next
) {
946 size
= type_size(&sym2
->type
, &align
);
949 o(0xE1A0C00D); /* mov ip,sp */
950 if(func_type
->ref
->c
== FUNC_ELLIPSIS
)
958 o(0xE92D0000|((1<<n
)-1)); /* save r0-r4 on stack if needed */
960 o(0xE92D5800); /* save fp, ip, lr */
961 o(0xE28DB00C); /* add fp, sp, #12 */
962 func_sub_sp_offset
= ind
;
963 o(0xE1A00000); /* nop, leave space for stack adjustment */
964 while ((sym
= sym
->next
)) {
967 size
= type_size(type
, &align
);
968 size
= (size
+ 3) & -4;
970 addr
= (addr
+ align
- 1) & -align
;
972 sym_push(sym
->v
& ~SYM_FIELD
, type
, VT_LOCAL
| lvalue_type(type
->t
), addr
);
980 /* generate function epilog */
981 void gfunc_epilog(void)
986 if(is_float(func_vt
.t
)) {
987 if((func_vt
.t
& VT_BTYPE
) == VT_FLOAT
)
988 o(0xEE100A10); /* fmrs r0, s0 */
990 o(0xEE100B10); /* fmrdl r0, d0 */
991 o(0xEE301B10); /* fmrdh r1, d0 */
995 o(0xE91BA800); /* restore fp, sp, pc */
996 diff
= (-loc
+ 3) & -4;
999 diff
= (diff
+ 7) & -8;
1002 x
=stuff_const(0xE24BD000, diff
); /* sub sp,fp,# */
1004 *(unsigned long *)(cur_text_section
->data
+ func_sub_sp_offset
) = x
;
1008 o(0xE59FC004); /* ldr ip,[pc+4] */
1009 o(0xE04BD00C); /* sub sp,fp,ip */
1010 o(0xE1A0F00E); /* mov pc,lr */
1012 *(unsigned long *)(cur_text_section
->data
+ func_sub_sp_offset
) = 0xE1000000|encbranch(func_sub_sp_offset
,addr
,1);
1017 /* generate a jump to a label */
1022 o(0xE0000000|encbranch(r
,t
,1));
1026 /* generate a jump to a fixed address */
1027 void gjmp_addr(int a
)
1032 /* generate a test. set 'inv' to invert test. Stack entry is popped */
1033 int gtst(int inv
, int t
)
1037 v
= vtop
->r
& VT_VALMASK
;
1040 op
=mapcc(inv
?negcc(vtop
->c
.i
):vtop
->c
.i
);
1041 op
|=encbranch(r
,t
,1);
1044 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
1045 if ((v
& 1) == inv
) {
1054 p
= decbranch(lp
=p
);
1056 x
= (unsigned long *)(cur_text_section
->data
+ lp
);
1058 *x
|= encbranch(lp
,t
,1);
1067 if (is_float(vtop
->type
.t
)) {
1070 o(0xEEB50A40|(vfpr(r
)<<12)|T2CPR(vtop
->type
.t
)); /* fcmpzX */
1071 o(0xEEF1FA10); /* fmstat */
1073 o(0xEE90F118|(fpr(r
)<<16));
1077 return gtst(inv
, t
);
1078 } else if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1079 /* constant jmp optimization */
1080 if ((vtop
->c
.i
!= 0) != inv
)
1084 o(0xE3300000|(intr(v
)<<16));
1087 return gtst(inv
, t
);
1094 /* generate an integer binary operation */
1095 void gen_opi(int op
)
1098 unsigned long opc
= 0,r
,fr
;
1099 unsigned short retreg
= REG_IRET
;
1107 case TOK_ADDC1
: /* add with carry generation */
1115 case TOK_SUBC1
: /* sub with carry generation */
1119 case TOK_ADDC2
: /* add with carry use */
1123 case TOK_SUBC2
: /* sub with carry use */
1140 gv2(RC_INT
, RC_INT
);
1144 o(0xE0000090|(intr(r
)<<16)|(intr(r
)<<8)|intr(fr
));
1169 func
=TOK___aeabi_idivmod
;
1178 func
=TOK___aeabi_uidivmod
;
1186 gv2(RC_INT
, RC_INT
);
1187 r
=intr(vtop
[-1].r2
=get_reg(RC_INT
));
1189 vtop
[-1].r
=get_reg_ex(RC_INT
,regmask(c
));
1191 o(0xE0800090|(r
<<16)|(intr(vtop
->r
)<<12)|(intr(c
)<<8)|intr(vtop
[1].r
));
1200 if((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1201 if(opc
== 4 || opc
== 5 || opc
== 0xc) {
1203 opc
|=2; // sub -> rsb
1206 if ((vtop
->r
& VT_VALMASK
) == VT_CMP
||
1207 (vtop
->r
& (VT_VALMASK
& ~1)) == VT_JMP
)
1212 opc
=0xE0000000|(opc
<<20)|(c
<<16);
1213 if((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1215 x
=stuff_const(opc
|0x2000000,vtop
->c
.i
);
1217 r
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,regmask(vtop
[-1].r
)));
1222 fr
=intr(gv(RC_INT
));
1223 r
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,two2mask(vtop
->r
,vtop
[-1].r
)));
1227 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1233 opc
=0xE1A00000|(opc
<<5);
1234 if ((vtop
->r
& VT_VALMASK
) == VT_CMP
||
1235 (vtop
->r
& (VT_VALMASK
& ~1)) == VT_JMP
)
1241 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1242 fr
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,regmask(vtop
[-1].r
)));
1243 c
= vtop
->c
.i
& 0x1f;
1244 o(opc
|(c
<<7)|(fr
<<12));
1246 fr
=intr(gv(RC_INT
));
1247 c
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,two2mask(vtop
->r
,vtop
[-1].r
)));
1248 o(opc
|(c
<<12)|(fr
<<8)|0x10);
1253 vpush_global_sym(&func_old_type
, func
);
1260 error("gen_opi %i unimplemented!",op
);
1265 static int is_zero(int i
)
1267 if((vtop
[i
].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1269 if (vtop
[i
].type
.t
== VT_FLOAT
)
1270 return (vtop
[i
].c
.f
== 0.f
);
1271 else if (vtop
[i
].type
.t
== VT_DOUBLE
)
1272 return (vtop
[i
].c
.d
== 0.0);
1273 return (vtop
[i
].c
.ld
== 0.l
);
1276 /* generate a floating point operation 'v = t1 op t2' instruction. The
1277 * two operands are guaranted to have the same floating point type */
1278 void gen_opf(int op
)
1282 x
=0xEE000A00|T2CPR(vtop
->type
.t
);
1300 x
|=0x810000; /* fsubX -> fnegX */
1313 if(op
< TOK_ULT
&& op
> TOK_GT
) {
1314 error("unknown fp op %x!",op
);
1320 case TOK_LT
: op
=TOK_GT
; break;
1321 case TOK_GE
: op
=TOK_ULE
; break;
1322 case TOK_LE
: op
=TOK_GE
; break;
1323 case TOK_GT
: op
=TOK_ULT
; break;
1326 x
|=0xB40040; /* fcmpX */
1327 if(op
!=TOK_EQ
&& op
!=TOK_NE
)
1328 x
|=0x80; /* fcmpX -> fcmpeX */
1331 o(x
|0x10000|(vfpr(gv(RC_FLOAT
))<<12)); /* fcmp(e)X -> fcmp(e)zX */
1333 x
|=vfpr(gv(RC_FLOAT
));
1335 o(x
|(vfpr(gv(RC_FLOAT
))<<12));
1338 o(0xEEF1FA10); /* fmstat */
1341 case TOK_LE
: op
=TOK_ULE
; break;
1342 case TOK_LT
: op
=TOK_ULT
; break;
1343 case TOK_UGE
: op
=TOK_GE
; break;
1344 case TOK_UGT
: op
=TOK_GT
; break;
1361 vtop
->r
=get_reg_ex(RC_FLOAT
,r
);
1364 o(x
|(vfpr(vtop
->r
)<<12));
1368 static int is_fconst()
1372 if((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1374 if (vtop
->type
.t
== VT_FLOAT
)
1376 else if (vtop
->type
.t
== VT_DOUBLE
)
1406 /* generate a floating point operation 'v = t1 op t2' instruction. The
1407 two operands are guaranted to have the same floating point type */
1408 void gen_opf(int op
)
1412 //fputs("gen_opf\n",stderr);
1418 #if LDOUBLE_SIZE == 8
1419 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FLOAT
)
1422 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1424 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
)
1435 r
=fpr(gv(RC_FLOAT
));
1442 r2
=fpr(gv(RC_FLOAT
));
1451 r
=fpr(gv(RC_FLOAT
));
1453 } else if(c1
&& c1
<=0xf) {
1456 r
=fpr(gv(RC_FLOAT
));
1461 r
=fpr(gv(RC_FLOAT
));
1463 r2
=fpr(gv(RC_FLOAT
));
1472 r
=fpr(gv(RC_FLOAT
));
1477 r2
=fpr(gv(RC_FLOAT
));
1485 r
=fpr(gv(RC_FLOAT
));
1487 } else if(c1
&& c1
<=0xf) {
1490 r
=fpr(gv(RC_FLOAT
));
1495 r
=fpr(gv(RC_FLOAT
));
1497 r2
=fpr(gv(RC_FLOAT
));
1501 if(op
>= TOK_ULT
&& op
<= TOK_GT
) {
1502 x
|=0xd0f110; // cmfe
1503 /* bug (intention?) in Linux FPU emulator
1504 doesn't set carry if equal */
1510 error("unsigned comparision on floats?");
1516 op
=TOK_ULE
; /* correct in unordered case only if AC bit in FPSR set */
1520 x
&=~0x400000; // cmfe -> cmf
1542 r
=fpr(gv(RC_FLOAT
));
1549 r2
=fpr(gv(RC_FLOAT
));
1551 vtop
[-1].r
= VT_CMP
;
1554 error("unknown fp op %x!",op
);
1558 if(vtop
[-1].r
== VT_CMP
)
1564 vtop
[-1].r
=get_reg_ex(RC_FLOAT
,two2mask(vtop
[-1].r
,c1
));
1568 o(x
|(r
<<16)|(c1
<<12)|r2
);
1572 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1573 and 'long long' cases. */
1574 void gen_cvt_itof1(int t
)
1577 bt
=vtop
->type
.t
& VT_BTYPE
;
1578 if(bt
== VT_INT
|| bt
== VT_SHORT
|| bt
== VT_BYTE
) {
1580 unsigned int dsize
=0;
1584 r2
=vfpr(vtop
->r
=get_reg(RC_FLOAT
));
1585 o(0xEE000A10|(r
<<12)|(r2
<<16)); /* fmsr */
1587 if(!(vtop
->type
.t
& VT_UNSIGNED
))
1588 r2
|=0x80; /* fuitoX -> fsituX */
1589 o(0xEEB80A40|r2
|T2CPR(t
)); /* fYitoX*/
1591 r2
=fpr(vtop
->r
=get_reg(RC_FLOAT
));
1592 if((t
& VT_BTYPE
) != VT_FLOAT
)
1593 dsize
=0x80; /* flts -> fltd */
1594 o(0xEE000110|dsize
|(r2
<<16)|(r
<<12)); /* flts */
1595 if((vtop
->type
.t
& (VT_UNSIGNED
|VT_BTYPE
)) == (VT_UNSIGNED
|VT_INT
)) {
1597 o(0xE3500000|(r
<<12)); /* cmp */
1598 r
=fpr(get_reg(RC_FLOAT
));
1599 if(last_itod_magic
) {
1600 off
=ind
+8-last_itod_magic
;
1605 o(0xBD1F0100|(r
<<12)|off
); /* ldflts */
1607 o(0xEA000000); /* b */
1608 last_itod_magic
=ind
;
1609 o(0x4F800000); /* 4294967296.0f */
1611 o(0xBE000100|dsize
|(r2
<<16)|(r2
<<12)|r
); /* adflt */
1615 } else if(bt
== VT_LLONG
) {
1617 CType
*func_type
= 0;
1618 if((t
& VT_BTYPE
) == VT_FLOAT
) {
1619 func_type
= &func_float_type
;
1620 if(vtop
->type
.t
& VT_UNSIGNED
)
1621 func
=TOK___floatundisf
;
1623 func
=TOK___floatdisf
;
1624 #if LDOUBLE_SIZE != 8
1625 } else if((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1626 func_type
= &func_ldouble_type
;
1627 if(vtop
->type
.t
& VT_UNSIGNED
)
1628 func
=TOK___floatundixf
;
1630 func
=TOK___floatdixf
;
1631 } else if((t
& VT_BTYPE
) == VT_DOUBLE
) {
1633 } else if((t
& VT_BTYPE
) == VT_DOUBLE
|| (t
& VT_BTYPE
) == VT_LDOUBLE
) {
1635 func_type
= &func_double_type
;
1636 if(vtop
->type
.t
& VT_UNSIGNED
)
1637 func
=TOK___floatundidf
;
1639 func
=TOK___floatdidf
;
1642 vpush_global_sym(func_type
, func
);
1650 error("unimplemented gen_cvt_itof %x!",vtop
->type
.t
);
1653 /* convert fp to int 't' type */
1654 void gen_cvt_ftoi(int t
)
1659 r2
=vtop
->type
.t
& VT_BTYPE
;
1662 r
=vfpr(gv(RC_FLOAT
));
1664 o(0xEEBC0A40|(r
<<12)|r
|T2CPR(r2
)); /* ftoXiY */
1665 r2
=intr(vtop
->r
=get_reg(RC_INT
));
1666 o(0xEE100A10|(r
<<16)|(r2
<<12));
1671 func
=TOK___fixunssfsi
;
1672 #if LDOUBLE_SIZE != 8
1673 else if(r2
== VT_LDOUBLE
)
1674 func
=TOK___fixunsxfsi
;
1675 else if(r2
== VT_DOUBLE
)
1677 else if(r2
== VT_LDOUBLE
|| r2
== VT_DOUBLE
)
1679 func
=TOK___fixunsdfsi
;
1681 r
=fpr(gv(RC_FLOAT
));
1682 r2
=intr(vtop
->r
=get_reg(RC_INT
));
1683 o(0xEE100170|(r2
<<12)|r
);
1687 } else if(t
== VT_LLONG
) { // unsigned handled in gen_cvt_ftoi1
1690 #if LDOUBLE_SIZE != 8
1691 else if(r2
== VT_LDOUBLE
)
1693 else if(r2
== VT_DOUBLE
)
1695 else if(r2
== VT_LDOUBLE
|| r2
== VT_DOUBLE
)
1700 vpush_global_sym(&func_old_type
, func
);
1705 vtop
->r2
= REG_LRET
;
1709 error("unimplemented gen_cvt_ftoi!");
1712 /* convert from one floating point type to another */
1713 void gen_cvt_ftof(int t
)
1716 if(((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) != ((t
& VT_BTYPE
) == VT_FLOAT
)) {
1717 int r
=vfpr(gv(RC_FLOAT
));
1718 o(0xEEB70AC0|(r
<<12)|r
|T2CPR(vtop
->type
.t
));
1721 /* all we have to do on i386 and FPA ARM is to put the float in a register */
1726 /* computed goto support */
1733 /* end of ARM code generator */
1734 /*************************************************************/