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
23 #ifdef TARGET_DEFS_ONLY
29 /* number of available registers */
36 /* a register can belong to several classes. The classes must be
37 sorted from more general to more precise (see gv2() code which does
38 assumptions on it). */
39 #define RC_INT 0x0001 /* generic integer register */
40 #define RC_FLOAT 0x0002 /* generic float register */
56 #define RC_IRET RC_R0 /* function return: integer register */
57 #define RC_LRET RC_R1 /* function return: second integer register */
58 #define RC_FRET RC_F0 /* function return: float register */
60 /* pretty names for the registers */
80 #define T2CPR(t) (((t) & VT_BTYPE) != VT_FLOAT ? 0x100 : 0)
83 /* return registers for function */
84 #define REG_IRET TREG_R0 /* single word int return register */
85 #define REG_LRET TREG_R1 /* second word return register (for long long) */
86 #define REG_FRET TREG_F0 /* float return register */
89 #define TOK___divdi3 TOK___aeabi_ldivmod
90 #define TOK___moddi3 TOK___aeabi_ldivmod
91 #define TOK___udivdi3 TOK___aeabi_uldivmod
92 #define TOK___umoddi3 TOK___aeabi_uldivmod
95 /* defined if function parameters must be evaluated in reverse order */
96 #define INVERT_FUNC_PARAMS
98 /* defined if structures are passed as pointers. Otherwise structures
99 are directly pushed on stack. */
100 //#define FUNC_STRUCT_PARAM_AS_PTR
102 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
103 static CType float_type
, double_type
, func_float_type
, func_double_type
;
104 #define func_ldouble_type func_double_type
106 #define func_float_type func_old_type
107 #define func_double_type func_old_type
108 #define func_ldouble_type func_old_type
111 /* pointer size, in bytes */
114 /* long double size and alignment, in bytes */
116 #define LDOUBLE_SIZE 8
120 #define LDOUBLE_SIZE 8
124 #define LDOUBLE_ALIGN 8
126 #define LDOUBLE_ALIGN 4
129 /* maximum alignment (for aligned attribute support) */
132 #define CHAR_IS_UNSIGNED
134 /******************************************************/
137 #define EM_TCC_TARGET EM_ARM
139 /* relocation type for 32 bit data relocation */
140 #define R_DATA_32 R_ARM_ABS32
141 #define R_DATA_PTR R_ARM_ABS32
142 #define R_JMP_SLOT R_ARM_JUMP_SLOT
143 #define R_COPY R_ARM_COPY
145 #define ELF_START_ADDR 0x00008000
146 #define ELF_PAGE_SIZE 0x1000
148 /******************************************************/
149 #else /* ! TARGET_DEFS_ONLY */
150 /******************************************************/
153 ST_DATA
const int reg_classes
[NB_REGS
] = {
154 /* r0 */ RC_INT
| RC_R0
,
155 /* r1 */ RC_INT
| RC_R1
,
156 /* r2 */ RC_INT
| RC_R2
,
157 /* r3 */ RC_INT
| RC_R3
,
158 /* r12 */ RC_INT
| RC_R12
,
159 /* f0 */ RC_FLOAT
| RC_F0
,
160 /* f1 */ RC_FLOAT
| RC_F1
,
161 /* f2 */ RC_FLOAT
| RC_F2
,
162 /* f3 */ RC_FLOAT
| RC_F3
,
164 /* d4/s8 */ RC_FLOAT
| RC_F4
,
165 /* d5/s10 */ RC_FLOAT
| RC_F5
,
166 /* d6/s12 */ RC_FLOAT
| RC_F6
,
167 /* d7/s14 */ RC_FLOAT
| RC_F7
,
171 static int func_sub_sp_offset
, last_itod_magic
;
174 static int two2mask(int a
,int b
) {
175 return (reg_classes
[a
]|reg_classes
[b
])&~(RC_INT
|RC_FLOAT
);
178 static int regmask(int r
) {
179 return reg_classes
[r
]&~(RC_INT
|RC_FLOAT
);
182 /******************************************************/
186 /* this is a good place to start adding big-endian support*/
190 if (!cur_text_section
)
191 error("compiler error! This happens f.ex. if the compiler\n"
192 "can't evaluate constant expressions outside of a function.");
193 if (ind1
> cur_text_section
->data_allocated
)
194 section_realloc(cur_text_section
, ind1
);
195 cur_text_section
->data
[ind
++] = i
&255;
197 cur_text_section
->data
[ind
++] = i
&255;
199 cur_text_section
->data
[ind
++] = i
&255;
201 cur_text_section
->data
[ind
++] = i
;
204 static uint32_t stuff_const(uint32_t op
, uint32_t c
)
207 uint32_t nc
= 0, negop
= 0;
217 case 0x1A00000: //mov
218 case 0x1E00000: //mvn
225 return (op
&0xF010F000)|((op
>>16)&0xF)|0x1E00000;
229 return (op
&0xF010F000)|((op
>>16)&0xF)|0x1A00000;
230 case 0x1C00000: //bic
235 case 0x1800000: //orr
237 return (op
&0xFFF0FFFF)|0x1E00000;
243 if(c
<256) /* catch undefined <<32 */
246 m
=(0xff>>i
)|(0xff<<(32-i
));
248 return op
|(i
<<7)|(c
<<i
)|(c
>>(32-i
));
258 void stuff_const_harder(uint32_t op
, uint32_t v
) {
264 uint32_t a
[16], nv
, no
, o2
, n2
;
267 o2
=(op
&0xfff0ffff)|((op
&0xf000)<<4);;
269 a
[i
]=(a
[i
-1]>>2)|(a
[i
-1]<<30);
271 for(j
=i
<4?i
+12:15;j
>=i
+4;j
--)
272 if((v
&(a
[i
]|a
[j
]))==v
) {
273 o(stuff_const(op
,v
&a
[i
]));
274 o(stuff_const(o2
,v
&a
[j
]));
281 for(j
=i
<4?i
+12:15;j
>=i
+4;j
--)
282 if((nv
&(a
[i
]|a
[j
]))==nv
) {
283 o(stuff_const(no
,nv
&a
[i
]));
284 o(stuff_const(n2
,nv
&a
[j
]));
289 for(k
=i
<4?i
+12:15;k
>=j
+4;k
--)
290 if((v
&(a
[i
]|a
[j
]|a
[k
]))==v
) {
291 o(stuff_const(op
,v
&a
[i
]));
292 o(stuff_const(o2
,v
&a
[j
]));
293 o(stuff_const(o2
,v
&a
[k
]));
300 for(k
=i
<4?i
+12:15;k
>=j
+4;k
--)
301 if((nv
&(a
[i
]|a
[j
]|a
[k
]))==nv
) {
302 o(stuff_const(no
,nv
&a
[i
]));
303 o(stuff_const(n2
,nv
&a
[j
]));
304 o(stuff_const(n2
,nv
&a
[k
]));
307 o(stuff_const(op
,v
&a
[0]));
308 o(stuff_const(o2
,v
&a
[4]));
309 o(stuff_const(o2
,v
&a
[8]));
310 o(stuff_const(o2
,v
&a
[12]));
314 ST_FUNC
uint32_t encbranch(int pos
, int addr
, int fail
)
318 if(addr
>=0x1000000 || addr
<-0x1000000) {
320 error("FIXME: function bigger than 32MB");
323 return 0x0A000000|(addr
&0xffffff);
326 int decbranch(int pos
)
329 x
=*(uint32_t *)(cur_text_section
->data
+ pos
);
336 /* output a symbol and patch all calls to it */
337 void gsym_addr(int t
, int a
)
342 x
=(uint32_t *)(cur_text_section
->data
+ t
);
345 *x
=0xE1A00000; // nop
348 *x
|= encbranch(lt
,a
,1);
359 static uint32_t vfpr(int r
)
361 if(r
<TREG_F0
|| r
>TREG_F7
)
362 error("compiler error! register %i is no vfp register",r
);
366 static uint32_t fpr(int r
)
368 if(r
<TREG_F0
|| r
>TREG_F3
)
369 error("compiler error! register %i is no fpa register",r
);
374 static uint32_t intr(int r
)
378 if((r
<0 || r
>4) && r
!=14)
379 error("compiler error! register %i is no int register",r
);
383 static void calcaddr(uint32_t *base
, int *off
, int *sgn
, int maxoff
, unsigned shift
)
385 if(*off
>maxoff
|| *off
&((1<<shift
)-1)) {
392 y
=stuff_const(x
,*off
&~maxoff
);
398 y
=stuff_const(x
,(*off
+maxoff
)&~maxoff
);
402 *off
=((*off
+maxoff
)&~maxoff
)-*off
;
405 stuff_const_harder(x
,*off
&~maxoff
);
410 static uint32_t mapcc(int cc
)
415 return 0x30000000; /* CC/LO */
417 return 0x20000000; /* CS/HS */
419 return 0x00000000; /* EQ */
421 return 0x10000000; /* NE */
423 return 0x90000000; /* LS */
425 return 0x80000000; /* HI */
427 return 0x40000000; /* MI */
429 return 0x50000000; /* PL */
431 return 0xB0000000; /* LT */
433 return 0xA0000000; /* GE */
435 return 0xD0000000; /* LE */
437 return 0xC0000000; /* GT */
439 error("unexpected condition code");
440 return 0xE0000000; /* AL */
443 static int negcc(int cc
)
472 error("unexpected condition code");
476 /* load 'r' from value 'sv' */
477 void load(int r
, SValue
*sv
)
479 int v
, ft
, fc
, fr
, sign
;
496 uint32_t base
= 0xB; // fp
499 v1
.r
= VT_LOCAL
| VT_LVAL
;
501 load(base
=14 /* lr */, &v1
);
504 } else if(v
== VT_CONST
) {
512 } else if(v
< VT_CONST
) {
519 calcaddr(&base
,&fc
,&sign
,1020,2);
521 op
=0xED100A00; /* flds */
524 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
525 op
|=0x100; /* flds -> fldd */
526 o(op
|(vfpr(r
)<<12)|(fc
>>2)|(base
<<16));
531 #if LDOUBLE_SIZE == 8
532 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
535 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
537 else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
)
540 o(op
|(fpr(r
)<<12)|(fc
>>2)|(base
<<16));
542 } else if((ft
& (VT_BTYPE
|VT_UNSIGNED
)) == VT_BYTE
543 || (ft
& VT_BTYPE
) == VT_SHORT
) {
544 calcaddr(&base
,&fc
,&sign
,255,0);
546 if ((ft
& VT_BTYPE
) == VT_SHORT
)
548 if ((ft
& VT_UNSIGNED
) == 0)
552 o(op
|(intr(r
)<<12)|(base
<<16)|((fc
&0xf0)<<4)|(fc
&0xf));
554 calcaddr(&base
,&fc
,&sign
,4095,0);
558 if ((ft
& VT_BTYPE
) == VT_BYTE
)
560 o(op
|(intr(r
)<<12)|fc
|(base
<<16));
566 op
=stuff_const(0xE3A00000|(intr(r
)<<12),sv
->c
.ul
);
567 if (fr
& VT_SYM
|| !op
) {
568 o(0xE59F0000|(intr(r
)<<12));
571 greloc(cur_text_section
, sv
->sym
, ind
, R_ARM_ABS32
);
576 } else if (v
== VT_LOCAL
) {
577 op
=stuff_const(0xE28B0000|(intr(r
)<<12),sv
->c
.ul
);
578 if (fr
& VT_SYM
|| !op
) {
579 o(0xE59F0000|(intr(r
)<<12));
581 if(fr
& VT_SYM
) // needed ?
582 greloc(cur_text_section
, sv
->sym
, ind
, R_ARM_ABS32
);
584 o(0xE08B0000|(intr(r
)<<12)|intr(r
));
588 } else if(v
== VT_CMP
) {
589 o(mapcc(sv
->c
.ul
)|0x3A00001|(intr(r
)<<12));
590 o(mapcc(negcc(sv
->c
.ul
))|0x3A00000|(intr(r
)<<12));
592 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
595 o(0xE3A00000|(intr(r
)<<12)|t
);
598 o(0xE3A00000|(intr(r
)<<12)|(t
^1));
600 } else if (v
< VT_CONST
) {
603 o(0xEEB00A40|(vfpr(r
)<<12)|vfpr(v
)|T2CPR(ft
)); /* fcpyX */
605 o(0xEE008180|(fpr(r
)<<12)|fpr(v
));
608 o(0xE1A00000|(intr(r
)<<12)|intr(v
));
612 error("load unimplemented!");
615 /* store register 'r' in lvalue 'v' */
616 void store(int r
, SValue
*sv
)
619 int v
, ft
, fc
, fr
, sign
;
634 if (fr
& VT_LVAL
|| fr
== VT_LOCAL
) {
640 } else if(v
== VT_CONST
) {
651 calcaddr(&base
,&fc
,&sign
,1020,2);
653 op
=0xED000A00; /* fsts */
656 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
657 op
|=0x100; /* fsts -> fstd */
658 o(op
|(vfpr(r
)<<12)|(fc
>>2)|(base
<<16));
663 #if LDOUBLE_SIZE == 8
664 if ((ft
& VT_BTYPE
) != VT_FLOAT
)
667 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
669 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
)
672 o(op
|(fpr(r
)<<12)|(fc
>>2)|(base
<<16));
675 } else if((ft
& VT_BTYPE
) == VT_SHORT
) {
676 calcaddr(&base
,&fc
,&sign
,255,0);
680 o(op
|(intr(r
)<<12)|(base
<<16)|((fc
&0xf0)<<4)|(fc
&0xf));
682 calcaddr(&base
,&fc
,&sign
,4095,0);
686 if ((ft
& VT_BTYPE
) == VT_BYTE
)
688 o(op
|(intr(r
)<<12)|fc
|(base
<<16));
693 error("store unimplemented");
696 static void gadd_sp(int val
)
698 stuff_const_harder(0xE28DD000,val
);
701 /* 'is_jmp' is '1' if it is a jump */
702 static void gcall_or_jmp(int is_jmp
)
705 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
708 x
=encbranch(ind
,ind
+vtop
->c
.ul
,0);
710 if (vtop
->r
& VT_SYM
) {
711 /* relocation case */
712 greloc(cur_text_section
, vtop
->sym
, ind
, R_ARM_PC24
);
714 put_elf_reloc(symtab_section
, cur_text_section
, ind
, R_ARM_PC24
, 0);
715 o(x
|(is_jmp
?0xE0000000:0xE1000000));
718 o(0xE28FE004); // add lr,pc,#4
719 o(0xE51FF004); // ldr pc,[pc,#-4]
720 if (vtop
->r
& VT_SYM
)
721 greloc(cur_text_section
, vtop
->sym
, ind
, R_ARM_ABS32
);
725 /* otherwise, indirect call */
728 o(0xE1A0E00F); // mov lr,pc
729 o(0xE1A0F000|intr(r
)); // mov pc,r
733 /* Generate function call. The function address is pushed first, then
734 all the parameters in call order. This functions pops all the
735 parameters and the function address. */
736 void gfunc_call(int nb_args
)
738 int size
, align
, r
, args_size
, i
;
740 signed char plan
[4][2]={{-1,-1},{-1,-1},{-1,-1},{-1,-1}};
741 int todo
=0xf, keep
, plan2
[4]={0,0,0,0};
743 r
= vtop
->r
& VT_VALMASK
;
744 if (r
== VT_CMP
|| (r
& ~1) == VT_JMP
)
747 if((vtop
[-nb_args
].type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
748 && type_size(&vtop
[-nb_args
].type
, &align
) <= 4) {
751 vtop
[-nb_args
]=vtop
[-nb_args
+1];
752 vtop
[-nb_args
+1]=tmp
;
757 vtop
->type
.t
= VT_LLONG
;
759 for(i
= nb_args
+ 1 ; i
-- ;) {
760 size
= type_size(&vtop
[-i
].type
, &align
);
761 if(args_size
& (align
-1)) {
763 vtop
->type
.t
= VT_VOID
; /* padding */
768 args_size
+= (size
+ 3) & -4;
773 for(i
= nb_args
; i
-- && args_size
< 16 ;) {
774 switch(vtop
[-i
].type
.t
& VT_BTYPE
) {
779 size
= type_size(&vtop
[-i
].type
, &align
);
780 size
= (size
+ 3) & -4;
784 plan
[nb_args
-1-i
][0]=args_size
/4;
786 if ((vtop
[-i
].type
.t
& VT_BTYPE
) == VT_LLONG
&& args_size
< 16) {
787 plan
[nb_args
-1-i
][1]=args_size
/4;
792 args_size
= keep
= 0;
793 for(i
= 0;i
< nb_args
; i
++) {
795 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
796 size
= type_size(&vtop
->type
, &align
);
797 /* align to stack align size */
798 size
= (size
+ 3) & -4;
799 /* allocate the necessary size on stack */
801 /* generate structure store */
803 o(0xE1A0000D|(intr(r
)<<12));
804 vset(&vtop
->type
, r
| VT_LVAL
, 0);
809 } else if (is_float(vtop
->type
.t
)) {
811 r
=vfpr(gv(RC_FLOAT
))<<12;
813 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FLOAT
)
816 r
|=0x101; /* fstms -> fstmd */
820 r
=fpr(gv(RC_FLOAT
))<<12;
821 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
823 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
833 o(0xED2D0100|r
|(size
>>2));
839 /* simple type (currently always same size) */
840 /* XXX: implicit cast ? */
842 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
845 if(nb_args
-i
<5 && plan
[nb_args
-i
-1][1]!=-1) {
846 s
=regmask(plan
[nb_args
-i
-1][1]);
847 todo
&=~(1<<plan
[nb_args
-i
-1][1]);
851 o(0xE52D0004|(intr(r
)<<12)); /* str r,[sp,#-4]! */
861 if(nb_args
-i
<5 && plan
[nb_args
-i
-1][0]!=-1) {
862 s
=regmask(plan
[nb_args
-i
-1][0]);
863 todo
&=~(1<<plan
[nb_args
-i
-1][0]);
866 if(vtop
->type
.t
== VT_VOID
) {
868 o(0xE24DD004); /* sub sp,sp,#4 */
874 o(0xE52D0004|(intr(r
)<<12)); /* str r,[sp,#-4]! */
887 save_regs(keep
); /* save used temporary registers */
908 func_sym
= vtop
->type
.ref
;
913 if((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
914 && type_size(&vtop
->type
.ref
->type
, &align
) <= 4)
916 store(REG_IRET
,vtop
-keep
);
920 else if(is_float(vtop
->type
.ref
->type
.t
)) {
921 if((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
922 o(0xEE000A10); /* fmsr s0,r0 */
924 o(0xEE000B10); /* fmdlr d0,r0 */
925 o(0xEE201B10); /* fmdhr d0,r1 */
934 /* generate function prolog of type 't' */
935 void gfunc_prolog(CType
*func_type
)
938 int n
,addr
,size
,align
;
940 sym
= func_type
->ref
;
945 if((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
946 && type_size(&func_vt
,&align
) > 4)
952 for(sym2
=sym
->next
;sym2
&& n
<4;sym2
=sym2
->next
) {
953 size
= type_size(&sym2
->type
, &align
);
956 o(0xE1A0C00D); /* mov ip,sp */
957 if(func_type
->ref
->c
== FUNC_ELLIPSIS
)
965 o(0xE92D0000|((1<<n
)-1)); /* save r0-r4 on stack if needed */
967 o(0xE92D5800); /* save fp, ip, lr */
968 o(0xE28DB00C); /* add fp, sp, #12 */
969 func_sub_sp_offset
= ind
;
970 o(0xE1A00000); /* nop, leave space for stack adjustment */
971 while ((sym
= sym
->next
)) {
974 size
= type_size(type
, &align
);
975 size
= (size
+ 3) & -4;
977 addr
= (addr
+ align
- 1) & -align
;
979 sym_push(sym
->v
& ~SYM_FIELD
, type
, VT_LOCAL
| lvalue_type(type
->t
), addr
);
987 /* generate function epilog */
988 void gfunc_epilog(void)
993 if(is_float(func_vt
.t
)) {
994 if((func_vt
.t
& VT_BTYPE
) == VT_FLOAT
)
995 o(0xEE100A10); /* fmrs r0, s0 */
997 o(0xEE100B10); /* fmrdl r0, d0 */
998 o(0xEE301B10); /* fmrdh r1, d0 */
1002 o(0xE91BA800); /* restore fp, sp, pc */
1003 diff
= (-loc
+ 3) & -4;
1006 diff
= (diff
+ 7) & -8;
1009 x
=stuff_const(0xE24BD000, diff
); /* sub sp,fp,# */
1011 *(uint32_t *)(cur_text_section
->data
+ func_sub_sp_offset
) = x
;
1015 o(0xE59FC004); /* ldr ip,[pc+4] */
1016 o(0xE04BD00C); /* sub sp,fp,ip */
1017 o(0xE1A0F00E); /* mov pc,lr */
1019 *(uint32_t *)(cur_text_section
->data
+ func_sub_sp_offset
) = 0xE1000000|encbranch(func_sub_sp_offset
,addr
,1);
1024 /* generate a jump to a label */
1029 o(0xE0000000|encbranch(r
,t
,1));
1033 /* generate a jump to a fixed address */
1034 void gjmp_addr(int a
)
1039 /* generate a test. set 'inv' to invert test. Stack entry is popped */
1040 int gtst(int inv
, int t
)
1044 v
= vtop
->r
& VT_VALMASK
;
1047 op
=mapcc(inv
?negcc(vtop
->c
.i
):vtop
->c
.i
);
1048 op
|=encbranch(r
,t
,1);
1051 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
1052 if ((v
& 1) == inv
) {
1061 p
= decbranch(lp
=p
);
1063 x
= (uint32_t *)(cur_text_section
->data
+ lp
);
1065 *x
|= encbranch(lp
,t
,1);
1074 if (is_float(vtop
->type
.t
)) {
1077 o(0xEEB50A40|(vfpr(r
)<<12)|T2CPR(vtop
->type
.t
)); /* fcmpzX */
1078 o(0xEEF1FA10); /* fmstat */
1080 o(0xEE90F118|(fpr(r
)<<16));
1084 return gtst(inv
, t
);
1085 } else if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1086 /* constant jmp optimization */
1087 if ((vtop
->c
.i
!= 0) != inv
)
1091 o(0xE3300000|(intr(v
)<<16));
1094 return gtst(inv
, t
);
1101 /* generate an integer binary operation */
1102 void gen_opi(int op
)
1105 uint32_t opc
= 0, r
, fr
;
1106 unsigned short retreg
= REG_IRET
;
1114 case TOK_ADDC1
: /* add with carry generation */
1122 case TOK_SUBC1
: /* sub with carry generation */
1126 case TOK_ADDC2
: /* add with carry use */
1130 case TOK_SUBC2
: /* sub with carry use */
1147 gv2(RC_INT
, RC_INT
);
1151 o(0xE0000090|(intr(r
)<<16)|(intr(r
)<<8)|intr(fr
));
1176 func
=TOK___aeabi_idivmod
;
1185 func
=TOK___aeabi_uidivmod
;
1193 gv2(RC_INT
, RC_INT
);
1194 r
=intr(vtop
[-1].r2
=get_reg(RC_INT
));
1196 vtop
[-1].r
=get_reg_ex(RC_INT
,regmask(c
));
1198 o(0xE0800090|(r
<<16)|(intr(vtop
->r
)<<12)|(intr(c
)<<8)|intr(vtop
[1].r
));
1207 if((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1208 if(opc
== 4 || opc
== 5 || opc
== 0xc) {
1210 opc
|=2; // sub -> rsb
1213 if ((vtop
->r
& VT_VALMASK
) == VT_CMP
||
1214 (vtop
->r
& (VT_VALMASK
& ~1)) == VT_JMP
)
1219 opc
=0xE0000000|(opc
<<20)|(c
<<16);
1220 if((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1222 x
=stuff_const(opc
|0x2000000,vtop
->c
.i
);
1224 r
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,regmask(vtop
[-1].r
)));
1229 fr
=intr(gv(RC_INT
));
1230 r
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,two2mask(vtop
->r
,vtop
[-1].r
)));
1234 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1240 opc
=0xE1A00000|(opc
<<5);
1241 if ((vtop
->r
& VT_VALMASK
) == VT_CMP
||
1242 (vtop
->r
& (VT_VALMASK
& ~1)) == VT_JMP
)
1248 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
1249 fr
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,regmask(vtop
[-1].r
)));
1250 c
= vtop
->c
.i
& 0x1f;
1251 o(opc
|(c
<<7)|(fr
<<12));
1253 fr
=intr(gv(RC_INT
));
1254 c
=intr(vtop
[-1].r
=get_reg_ex(RC_INT
,two2mask(vtop
->r
,vtop
[-1].r
)));
1255 o(opc
|(c
<<12)|(fr
<<8)|0x10);
1260 vpush_global_sym(&func_old_type
, func
);
1267 error("gen_opi %i unimplemented!",op
);
1272 static int is_zero(int i
)
1274 if((vtop
[i
].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1276 if (vtop
[i
].type
.t
== VT_FLOAT
)
1277 return (vtop
[i
].c
.f
== 0.f
);
1278 else if (vtop
[i
].type
.t
== VT_DOUBLE
)
1279 return (vtop
[i
].c
.d
== 0.0);
1280 return (vtop
[i
].c
.ld
== 0.l
);
1283 /* generate a floating point operation 'v = t1 op t2' instruction. The
1284 * two operands are guaranted to have the same floating point type */
1285 void gen_opf(int op
)
1289 x
=0xEE000A00|T2CPR(vtop
->type
.t
);
1307 x
|=0x810000; /* fsubX -> fnegX */
1320 if(op
< TOK_ULT
&& op
> TOK_GT
) {
1321 error("unknown fp op %x!",op
);
1327 case TOK_LT
: op
=TOK_GT
; break;
1328 case TOK_GE
: op
=TOK_ULE
; break;
1329 case TOK_LE
: op
=TOK_GE
; break;
1330 case TOK_GT
: op
=TOK_ULT
; break;
1333 x
|=0xB40040; /* fcmpX */
1334 if(op
!=TOK_EQ
&& op
!=TOK_NE
)
1335 x
|=0x80; /* fcmpX -> fcmpeX */
1338 o(x
|0x10000|(vfpr(gv(RC_FLOAT
))<<12)); /* fcmp(e)X -> fcmp(e)zX */
1340 x
|=vfpr(gv(RC_FLOAT
));
1342 o(x
|(vfpr(gv(RC_FLOAT
))<<12));
1345 o(0xEEF1FA10); /* fmstat */
1348 case TOK_LE
: op
=TOK_ULE
; break;
1349 case TOK_LT
: op
=TOK_ULT
; break;
1350 case TOK_UGE
: op
=TOK_GE
; break;
1351 case TOK_UGT
: op
=TOK_GT
; break;
1368 vtop
->r
=get_reg_ex(RC_FLOAT
,r
);
1371 o(x
|(vfpr(vtop
->r
)<<12));
1375 static uint32_t is_fconst()
1379 if((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
1381 if (vtop
->type
.t
== VT_FLOAT
)
1383 else if (vtop
->type
.t
== VT_DOUBLE
)
1413 /* generate a floating point operation 'v = t1 op t2' instruction. The
1414 two operands are guaranted to have the same floating point type */
1415 void gen_opf(int op
)
1417 uint32_t x
, r
, r2
, c1
, c2
;
1418 //fputs("gen_opf\n",stderr);
1424 #if LDOUBLE_SIZE == 8
1425 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FLOAT
)
1428 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
1430 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
)
1441 r
=fpr(gv(RC_FLOAT
));
1448 r2
=fpr(gv(RC_FLOAT
));
1457 r
=fpr(gv(RC_FLOAT
));
1459 } else if(c1
&& c1
<=0xf) {
1462 r
=fpr(gv(RC_FLOAT
));
1467 r
=fpr(gv(RC_FLOAT
));
1469 r2
=fpr(gv(RC_FLOAT
));
1478 r
=fpr(gv(RC_FLOAT
));
1483 r2
=fpr(gv(RC_FLOAT
));
1491 r
=fpr(gv(RC_FLOAT
));
1493 } else if(c1
&& c1
<=0xf) {
1496 r
=fpr(gv(RC_FLOAT
));
1501 r
=fpr(gv(RC_FLOAT
));
1503 r2
=fpr(gv(RC_FLOAT
));
1507 if(op
>= TOK_ULT
&& op
<= TOK_GT
) {
1508 x
|=0xd0f110; // cmfe
1509 /* bug (intention?) in Linux FPU emulator
1510 doesn't set carry if equal */
1516 error("unsigned comparision on floats?");
1522 op
=TOK_ULE
; /* correct in unordered case only if AC bit in FPSR set */
1526 x
&=~0x400000; // cmfe -> cmf
1548 r
=fpr(gv(RC_FLOAT
));
1555 r2
=fpr(gv(RC_FLOAT
));
1557 vtop
[-1].r
= VT_CMP
;
1560 error("unknown fp op %x!",op
);
1564 if(vtop
[-1].r
== VT_CMP
)
1570 vtop
[-1].r
=get_reg_ex(RC_FLOAT
,two2mask(vtop
[-1].r
,c1
));
1574 o(x
|(r
<<16)|(c1
<<12)|r2
);
1578 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1579 and 'long long' cases. */
1580 ST_FUNC
void gen_cvt_itof1(int t
)
1584 bt
=vtop
->type
.t
& VT_BTYPE
;
1585 if(bt
== VT_INT
|| bt
== VT_SHORT
|| bt
== VT_BYTE
) {
1591 r2
=vfpr(vtop
->r
=get_reg(RC_FLOAT
));
1592 o(0xEE000A10|(r
<<12)|(r2
<<16)); /* fmsr */
1594 if(!(vtop
->type
.t
& VT_UNSIGNED
))
1595 r2
|=0x80; /* fuitoX -> fsituX */
1596 o(0xEEB80A40|r2
|T2CPR(t
)); /* fYitoX*/
1598 r2
=fpr(vtop
->r
=get_reg(RC_FLOAT
));
1599 if((t
& VT_BTYPE
) != VT_FLOAT
)
1600 dsize
=0x80; /* flts -> fltd */
1601 o(0xEE000110|dsize
|(r2
<<16)|(r
<<12)); /* flts */
1602 if((vtop
->type
.t
& (VT_UNSIGNED
|VT_BTYPE
)) == (VT_UNSIGNED
|VT_INT
)) {
1604 o(0xE3500000|(r
<<12)); /* cmp */
1605 r
=fpr(get_reg(RC_FLOAT
));
1606 if(last_itod_magic
) {
1607 off
=ind
+8-last_itod_magic
;
1612 o(0xBD1F0100|(r
<<12)|off
); /* ldflts */
1614 o(0xEA000000); /* b */
1615 last_itod_magic
=ind
;
1616 o(0x4F800000); /* 4294967296.0f */
1618 o(0xBE000100|dsize
|(r2
<<16)|(r2
<<12)|r
); /* adflt */
1622 } else if(bt
== VT_LLONG
) {
1624 CType
*func_type
= 0;
1625 if((t
& VT_BTYPE
) == VT_FLOAT
) {
1626 func_type
= &func_float_type
;
1627 if(vtop
->type
.t
& VT_UNSIGNED
)
1628 func
=TOK___floatundisf
;
1630 func
=TOK___floatdisf
;
1631 #if LDOUBLE_SIZE != 8
1632 } else if((t
& VT_BTYPE
) == VT_LDOUBLE
) {
1633 func_type
= &func_ldouble_type
;
1634 if(vtop
->type
.t
& VT_UNSIGNED
)
1635 func
=TOK___floatundixf
;
1637 func
=TOK___floatdixf
;
1638 } else if((t
& VT_BTYPE
) == VT_DOUBLE
) {
1640 } else if((t
& VT_BTYPE
) == VT_DOUBLE
|| (t
& VT_BTYPE
) == VT_LDOUBLE
) {
1642 func_type
= &func_double_type
;
1643 if(vtop
->type
.t
& VT_UNSIGNED
)
1644 func
=TOK___floatundidf
;
1646 func
=TOK___floatdidf
;
1649 vpush_global_sym(func_type
, func
);
1657 error("unimplemented gen_cvt_itof %x!",vtop
->type
.t
);
1660 /* convert fp to int 't' type */
1661 void gen_cvt_ftoi(int t
)
1667 r2
=vtop
->type
.t
& VT_BTYPE
;
1670 r
=vfpr(gv(RC_FLOAT
));
1672 o(0xEEBC0A40|(r
<<12)|r
|T2CPR(r2
)); /* ftoXiY */
1673 r2
=intr(vtop
->r
=get_reg(RC_INT
));
1674 o(0xEE100A10|(r
<<16)|(r2
<<12));
1679 func
=TOK___fixunssfsi
;
1680 #if LDOUBLE_SIZE != 8
1681 else if(r2
== VT_LDOUBLE
)
1682 func
=TOK___fixunsxfsi
;
1683 else if(r2
== VT_DOUBLE
)
1685 else if(r2
== VT_LDOUBLE
|| r2
== VT_DOUBLE
)
1687 func
=TOK___fixunsdfsi
;
1689 r
=fpr(gv(RC_FLOAT
));
1690 r2
=intr(vtop
->r
=get_reg(RC_INT
));
1691 o(0xEE100170|(r2
<<12)|r
);
1695 } else if(t
== VT_LLONG
) { // unsigned handled in gen_cvt_ftoi1
1698 #if LDOUBLE_SIZE != 8
1699 else if(r2
== VT_LDOUBLE
)
1701 else if(r2
== VT_DOUBLE
)
1703 else if(r2
== VT_LDOUBLE
|| r2
== VT_DOUBLE
)
1708 vpush_global_sym(&func_old_type
, func
);
1713 vtop
->r2
= REG_LRET
;
1717 error("unimplemented gen_cvt_ftoi!");
1720 /* convert from one floating point type to another */
1721 void gen_cvt_ftof(int t
)
1724 if(((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) != ((t
& VT_BTYPE
) == VT_FLOAT
)) {
1725 uint32_t r
= vfpr(gv(RC_FLOAT
));
1726 o(0xEEB70AC0|(r
<<12)|r
|T2CPR(vtop
->type
.t
));
1729 /* all we have to do on i386 and FPA ARM is to put the float in a register */
1734 /* computed goto support */
1741 /* end of ARM code generator */
1742 /*************************************************************/
1744 /*************************************************************/