win32: explain usage of mingw w32api package
[tinycc/kirr.git] / arm-gen.c
blobe0866ffd79b481acd3a01731aa60ff5d53db8d7b
1 /*
2 * ARMv4 code generator for TCC
3 *
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 TCC_ARM_EABI
24 #define TCC_ARM_VFP
25 #endif
28 /* number of available registers */
29 #ifdef TCC_ARM_VFP
30 #define NB_REGS 13
31 #else
32 #define NB_REGS 9
33 #endif
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 */
40 #define RC_R0 0x0004
41 #define RC_R1 0x0008
42 #define RC_R2 0x0010
43 #define RC_R3 0x0020
44 #define RC_R12 0x0040
45 #define RC_F0 0x0080
46 #define RC_F1 0x0100
47 #define RC_F2 0x0200
48 #define RC_F3 0x0400
49 #ifdef TCC_ARM_VFP
50 #define RC_F4 0x0800
51 #define RC_F5 0x1000
52 #define RC_F6 0x2000
53 #define RC_F7 0x4000
54 #endif
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 */
60 enum {
61 TREG_R0 = 0,
62 TREG_R1,
63 TREG_R2,
64 TREG_R3,
65 TREG_R12,
66 TREG_F0,
67 TREG_F1,
68 TREG_F2,
69 TREG_F3,
70 #ifdef TCC_ARM_VFP
71 TREG_F4,
72 TREG_F5,
73 TREG_F6,
74 TREG_F7,
75 #endif
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,
88 #ifdef TCC_ARM_VFP
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,
93 #endif
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);
104 #ifdef TCC_ARM_VFP
105 #define T2CPR(t) (((t) & VT_BTYPE) != VT_FLOAT ? 0x100 : 0)
106 #endif
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 */
113 #ifdef TCC_ARM_EABI
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
118 #endif
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
130 #else
131 #define func_float_type func_old_type
132 #define func_double_type func_old_type
133 #define func_ldouble_type func_old_type
134 #endif
136 /* pointer size, in bytes */
137 #define PTR_SIZE 4
139 /* long double size and alignment, in bytes */
140 #ifdef TCC_ARM_VFP
141 #define LDOUBLE_SIZE 8
142 #endif
144 #ifndef LDOUBLE_SIZE
145 #define LDOUBLE_SIZE 8
146 #endif
148 #ifdef TCC_ARM_EABI
149 #define LDOUBLE_ALIGN 8
150 #else
151 #define LDOUBLE_ALIGN 4
152 #endif
154 /* maximum alignment (for aligned attribute support) */
155 #define MAX_ALIGN 8
157 #define CHAR_IS_UNSIGNED
159 /******************************************************/
160 /* ELF defines */
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;
175 static int leaffunc;
177 void o(unsigned long i)
179 /* this is a good place to start adding big-endian support*/
180 int ind1;
182 ind1 = ind + 4;
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;
189 i>>=8;
190 cur_text_section->data[ind++] = i&255;
191 i>>=8;
192 cur_text_section->data[ind++] = i&255;
193 i>>=8;
194 cur_text_section->data[ind++] = i;
197 static unsigned long stuff_const(unsigned long op,unsigned long c)
199 int try_neg=0;
200 unsigned long nc = 0,negop = 0;
202 switch(op&0x1F00000)
204 case 0x800000: //add
205 case 0x400000: //sub
206 try_neg=1;
207 negop=op^0xC00000;
208 nc=-c;
209 break;
210 case 0x1A00000: //mov
211 case 0x1E00000: //mvn
212 try_neg=1;
213 negop=op^0x400000;
214 nc=~c;
215 break;
216 case 0x200000: //xor
217 if(c==~0)
218 return (op&0xF010F000)|((op>>16)&0xF)|0x1E00000;
219 break;
220 case 0x0: //and
221 if(c==~0)
222 return (op&0xF010F000)|((op>>16)&0xF)|0x1A00000;
223 case 0x1C00000: //bic
224 try_neg=1;
225 negop=op^0x1C00000;
226 nc=~c;
227 break;
228 case 0x1800000: //orr
229 if(c==~0)
230 return (op&0xFFF0FFFF)|0x1E00000;
231 break;
233 do {
234 unsigned long m;
235 int i;
236 if(c<256) /* catch undefined <<32 */
237 return op|c;
238 for(i=2;i<32;i+=2) {
239 m=(0xff>>i)|(0xff<<(32-i));
240 if(!(c&~m))
241 return op|(i<<7)|(c<<i)|(c>>(32-i));
243 op=negop;
244 c=nc;
245 } while(try_neg--);
246 return 0;
250 //only add,sub
251 void stuff_const_harder(unsigned long op,unsigned long v) {
252 unsigned long x;
253 x=stuff_const(op,v);
254 if(x)
255 o(x);
256 else {
257 unsigned long a[16],nv,no,o2,n2;
258 int i,j,k;
259 a[0]=0xff;
260 o2=(op&0xfff0ffff)|((op&0xf000)<<4);;
261 for(i=1;i<16;i++)
262 a[i]=(a[i-1]>>2)|(a[i-1]<<30);
263 for(i=0;i<12;i++)
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]));
268 return;
270 no=op^0xC00000;
271 n2=o2^0xC00000;
272 nv=-v;
273 for(i=0;i<12;i++)
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]));
278 return;
280 for(i=0;i<8;i++)
281 for(j=i+4;j<12;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]));
287 return;
289 no=op^0xC00000;
290 nv=-v;
291 for(i=0;i<8;i++)
292 for(j=i+4;j<12;j++)
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]));
298 return;
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)
309 addr-=pos+8;
310 addr/=4;
311 if(addr>=0x1000000 || addr<-0x1000000) {
312 if(fail)
313 error("FIXME: function bigger than 32MB");
314 return 0;
316 return 0x0A000000|(addr&0xffffff);
319 int decbranch(int pos)
321 int x;
322 x=*(int *)(cur_text_section->data + pos);
323 x&=0x00ffffff;
324 if(x&0x800000)
325 x-=0x1000000;
326 return x*4+pos+8;
329 /* output a symbol and patch all calls to it */
330 void gsym_addr(int t, int a)
332 unsigned long *x;
333 int lt;
334 while(t) {
335 x=(unsigned long *)(cur_text_section->data + t);
336 t=decbranch(lt=t);
337 if(a==lt+4)
338 *x=0xE1A00000; // nop
339 else {
340 *x &= 0xff000000;
341 *x |= encbranch(lt,a,1);
346 void gsym(int t)
348 gsym_addr(t, ind);
351 #ifdef TCC_ARM_VFP
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);
356 return r-5;
358 #else
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);
363 return r-5;
365 #endif
367 static unsigned long intr(int r)
369 if(r==4)
370 return 12;
371 if((r<0 || r>4) && r!=14)
372 error("compiler error! register %i is no int register",r);
373 return r;
376 static void calcaddr(unsigned long *base,int *off,int *sgn,int maxoff,unsigned shift)
378 if(*off>maxoff || *off&((1<<shift)-1)) {
379 unsigned long x,y;
380 x=0xE280E000;
381 if(*sgn)
382 x=0xE240E000;
383 x|=(*base)<<16;
384 *base=14; // lr
385 y=stuff_const(x,*off&~maxoff);
386 if(y) {
387 o(y);
388 *off&=maxoff;
389 return;
391 y=stuff_const(x,(*off+maxoff)&~maxoff);
392 if(y) {
393 o(y);
394 *sgn=!*sgn;
395 *off=((*off+maxoff)&~maxoff)-*off;
396 return;
398 stuff_const_harder(x,*off&~maxoff);
399 *off&=maxoff;
403 static unsigned long mapcc(int cc)
405 switch(cc)
407 case TOK_ULT:
408 return 0x30000000; /* CC/LO */
409 case TOK_UGE:
410 return 0x20000000; /* CS/HS */
411 case TOK_EQ:
412 return 0x00000000; /* EQ */
413 case TOK_NE:
414 return 0x10000000; /* NE */
415 case TOK_ULE:
416 return 0x90000000; /* LS */
417 case TOK_UGT:
418 return 0x80000000; /* HI */
419 case TOK_Nset:
420 return 0x40000000; /* MI */
421 case TOK_Nclear:
422 return 0x50000000; /* PL */
423 case TOK_LT:
424 return 0xB0000000; /* LT */
425 case TOK_GE:
426 return 0xA0000000; /* GE */
427 case TOK_LE:
428 return 0xD0000000; /* LE */
429 case TOK_GT:
430 return 0xC0000000; /* GT */
432 error("unexpected condition code");
433 return 0xE0000000; /* AL */
436 static int negcc(int cc)
438 switch(cc)
440 case TOK_ULT:
441 return TOK_UGE;
442 case TOK_UGE:
443 return TOK_ULT;
444 case TOK_EQ:
445 return TOK_NE;
446 case TOK_NE:
447 return TOK_EQ;
448 case TOK_ULE:
449 return TOK_UGT;
450 case TOK_UGT:
451 return TOK_ULE;
452 case TOK_Nset:
453 return TOK_Nclear;
454 case TOK_Nclear:
455 return TOK_Nset;
456 case TOK_LT:
457 return TOK_GE;
458 case TOK_GE:
459 return TOK_LT;
460 case TOK_LE:
461 return TOK_GT;
462 case TOK_GT:
463 return TOK_LE;
465 error("unexpected condition code");
466 return TOK_NE;
469 /* load 'r' from value 'sv' */
470 void load(int r, SValue *sv)
472 int v, ft, fc, fr, sign;
473 unsigned long op;
474 SValue v1;
476 fr = sv->r;
477 ft = sv->type.t;
478 fc = sv->c.ul;
480 if(fc>=0)
481 sign=0;
482 else {
483 sign=1;
484 fc=-fc;
487 v = fr & VT_VALMASK;
488 if (fr & VT_LVAL) {
489 unsigned long base=0xB; // fp
490 if(v == VT_LLOCAL) {
491 v1.type.t = VT_PTR;
492 v1.r = VT_LOCAL | VT_LVAL;
493 v1.c.ul = sv->c.ul;
494 load(base=14 /* lr */, &v1);
495 fc=sign=0;
496 v=VT_LOCAL;
497 } else if(v == VT_CONST) {
498 v1.type.t = VT_PTR;
499 v1.r = fr&~VT_LVAL;
500 v1.c.ul = sv->c.ul;
501 v1.sym=sv->sym;
502 load(base=14, &v1);
503 fc=sign=0;
504 v=VT_LOCAL;
505 } else if(v < VT_CONST) {
506 base=intr(v);
507 fc=sign=0;
508 v=VT_LOCAL;
510 if(v == VT_LOCAL) {
511 if(is_float(ft)) {
512 calcaddr(&base,&fc,&sign,1020,2);
513 #ifdef TCC_ARM_VFP
514 op=0xED100A00; /* flds */
515 if(!sign)
516 op|=0x800000;
517 if ((ft & VT_BTYPE) != VT_FLOAT)
518 op|=0x100; /* flds -> fldd */
519 o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16));
520 #else
521 op=0xED100100;
522 if(!sign)
523 op|=0x800000;
524 #if LDOUBLE_SIZE == 8
525 if ((ft & VT_BTYPE) != VT_FLOAT)
526 op|=0x8000;
527 #else
528 if ((ft & VT_BTYPE) == VT_DOUBLE)
529 op|=0x8000;
530 else if ((ft & VT_BTYPE) == VT_LDOUBLE)
531 op|=0x400000;
532 #endif
533 o(op|(fpr(r)<<12)|(fc>>2)|(base<<16));
534 #endif
535 } else if((ft & (VT_BTYPE|VT_UNSIGNED)) == VT_BYTE
536 || (ft & VT_BTYPE) == VT_SHORT) {
537 calcaddr(&base,&fc,&sign,255,0);
538 op=0xE1500090;
539 if ((ft & VT_BTYPE) == VT_SHORT)
540 op|=0x20;
541 if ((ft & VT_UNSIGNED) == 0)
542 op|=0x40;
543 if(!sign)
544 op|=0x800000;
545 o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf));
546 } else {
547 calcaddr(&base,&fc,&sign,4095,0);
548 op=0xE5100000;
549 if(!sign)
550 op|=0x800000;
551 if ((ft & VT_BTYPE) == VT_BYTE)
552 op|=0x400000;
553 o(op|(intr(r)<<12)|fc|(base<<16));
555 return;
557 } else {
558 if (v == VT_CONST) {
559 op=stuff_const(0xE3A00000|(intr(r)<<12),sv->c.ul);
560 if (fr & VT_SYM || !op) {
561 o(0xE59F0000|(intr(r)<<12));
562 o(0xEA000000);
563 if(fr & VT_SYM)
564 greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32);
565 o(sv->c.ul);
566 } else
567 o(op);
568 return;
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));
573 o(0xEA000000);
574 if(fr & VT_SYM) // needed ?
575 greloc(cur_text_section, sv->sym, ind, R_ARM_ABS32);
576 o(sv->c.ul);
577 o(0xE08B0000|(intr(r)<<12)|intr(r));
578 } else
579 o(op);
580 return;
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));
584 return;
585 } else if (v == VT_JMP || v == VT_JMPI) {
586 int t;
587 t = v & 1;
588 o(0xE3A00000|(intr(r)<<12)|t);
589 o(0xEA000000);
590 gsym(sv->c.ul);
591 o(0xE3A00000|(intr(r)<<12)|(t^1));
592 return;
593 } else if (v < VT_CONST) {
594 if(is_float(ft))
595 #ifdef TCC_ARM_VFP
596 o(0xEEB00A40|(vfpr(r)<<12)|vfpr(v)|T2CPR(ft)); /* fcpyX */
597 #else
598 o(0xEE008180|(fpr(r)<<12)|fpr(v));
599 #endif
600 else
601 o(0xE1A00000|(intr(r)<<12)|intr(v));
602 return;
605 error("load unimplemented!");
608 /* store register 'r' in lvalue 'v' */
609 void store(int r, SValue *sv)
611 SValue v1;
612 int v, ft, fc, fr, sign;
613 unsigned long op;
615 fr = sv->r;
616 ft = sv->type.t;
617 fc = sv->c.ul;
619 if(fc>=0)
620 sign=0;
621 else {
622 sign=1;
623 fc=-fc;
626 v = fr & VT_VALMASK;
627 if (fr & VT_LVAL || fr == VT_LOCAL) {
628 unsigned long base=0xb;
629 if(v < VT_CONST) {
630 base=intr(v);
631 v=VT_LOCAL;
632 fc=sign=0;
633 } else if(v == VT_CONST) {
634 v1.type.t = ft;
635 v1.r = fr&~VT_LVAL;
636 v1.c.ul = sv->c.ul;
637 v1.sym=sv->sym;
638 load(base=14, &v1);
639 fc=sign=0;
640 v=VT_LOCAL;
642 if(v == VT_LOCAL) {
643 if(is_float(ft)) {
644 calcaddr(&base,&fc,&sign,1020,2);
645 #ifdef TCC_ARM_VFP
646 op=0xED000A00; /* fsts */
647 if(!sign)
648 op|=0x800000;
649 if ((ft & VT_BTYPE) != VT_FLOAT)
650 op|=0x100; /* fsts -> fstd */
651 o(op|(vfpr(r)<<12)|(fc>>2)|(base<<16));
652 #else
653 op=0xED000100;
654 if(!sign)
655 op|=0x800000;
656 #if LDOUBLE_SIZE == 8
657 if ((ft & VT_BTYPE) != VT_FLOAT)
658 op|=0x8000;
659 #else
660 if ((ft & VT_BTYPE) == VT_DOUBLE)
661 op|=0x8000;
662 if ((ft & VT_BTYPE) == VT_LDOUBLE)
663 op|=0x400000;
664 #endif
665 o(op|(fpr(r)<<12)|(fc>>2)|(base<<16));
666 #endif
667 return;
668 } else if((ft & VT_BTYPE) == VT_SHORT) {
669 calcaddr(&base,&fc,&sign,255,0);
670 op=0xE14000B0;
671 if(!sign)
672 op|=0x800000;
673 o(op|(intr(r)<<12)|(base<<16)|((fc&0xf0)<<4)|(fc&0xf));
674 } else {
675 calcaddr(&base,&fc,&sign,4095,0);
676 op=0xE5000000;
677 if(!sign)
678 op|=0x800000;
679 if ((ft & VT_BTYPE) == VT_BYTE)
680 op|=0x400000;
681 o(op|(intr(r)<<12)|fc|(base<<16));
683 return;
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)
697 int r;
698 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
699 unsigned long x;
700 /* constant case */
701 x=encbranch(ind,ind+vtop->c.ul,0);
702 if(x) {
703 if (vtop->r & VT_SYM) {
704 /* relocation case */
705 greloc(cur_text_section, vtop->sym, ind, R_ARM_PC24);
706 } else
707 put_elf_reloc(symtab_section, cur_text_section, ind, R_ARM_PC24, 0);
708 o(x|(is_jmp?0xE0000000:0xE1000000));
709 } else {
710 if(!is_jmp)
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);
715 o(vtop->c.ul);
717 } else {
718 /* otherwise, indirect call */
719 r = gv(RC_INT);
720 if(!is_jmp)
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;
732 Sym *func_sym;
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)
738 gv(RC_INT);
739 #ifdef TCC_ARM_EABI
740 if((vtop[-nb_args].type.ref->type.t & VT_BTYPE) == VT_STRUCT
741 && type_size(&vtop[-nb_args].type, &align) <= 4) {
742 SValue tmp;
743 tmp=vtop[-nb_args];
744 vtop[-nb_args]=vtop[-nb_args+1];
745 vtop[-nb_args+1]=tmp;
746 --nb_args;
749 vpushi(0);
750 vtop->type.t = VT_LLONG;
751 args_size = 0;
752 for(i = nb_args + 1 ; i-- ;) {
753 size = type_size(&vtop[-i].type, &align);
754 if(args_size & (align-1)) {
755 vpushi(0);
756 vtop->type.t = VT_VOID; /* padding */
757 vrott(i+2);
758 args_size += 4;
759 ++nb_args;
761 args_size += (size + 3) & -4;
763 vtop--;
764 #endif
765 args_size = 0;
766 for(i = nb_args ; i-- && args_size < 16 ;) {
767 switch(vtop[-i].type.t & VT_BTYPE) {
768 case VT_STRUCT:
769 case VT_FLOAT:
770 case VT_DOUBLE:
771 case VT_LDOUBLE:
772 size = type_size(&vtop[-i].type, &align);
773 size = (size + 3) & -4;
774 args_size += size;
775 break;
776 default:
777 plan[nb_args-1-i][0]=args_size/4;
778 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;
781 args_size += 4;
785 args_size = keep = 0;
786 for(i = 0;i < nb_args; i++) {
787 vnrott(keep+1);
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 */
793 gadd_sp(-size);
794 /* generate structure store */
795 r = get_reg(RC_INT);
796 o(0xE1A0000D|(intr(r)<<12));
797 vset(&vtop->type, r | VT_LVAL, 0);
798 vswap();
799 vstore();
800 vtop--;
801 args_size += size;
802 } else if (is_float(vtop->type.t)) {
803 #ifdef TCC_ARM_VFP
804 r=vfpr(gv(RC_FLOAT))<<12;
805 size=4;
806 if ((vtop->type.t & VT_BTYPE) != VT_FLOAT)
808 size=8;
809 r|=0x101; /* fstms -> fstmd */
811 o(0xED2D0A01+r);
812 #else
813 r=fpr(gv(RC_FLOAT))<<12;
814 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
815 size = 4;
816 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
817 size = 8;
818 else
819 size = LDOUBLE_SIZE;
821 if (size == 12)
822 r|=0x400000;
823 else if(size == 8)
824 r|=0x8000;
826 o(0xED2D0100|r|(size>>2));
827 #endif
828 vtop--;
829 args_size += size;
830 } else {
831 int s;
832 /* simple type (currently always same size) */
833 /* XXX: implicit cast ? */
834 size=4;
835 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
836 lexpand_nr();
837 s=RC_INT;
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]);
842 if(s==RC_INT) {
843 r = gv(s);
844 o(0xE52D0004|(intr(r)<<12)); /* str r,[sp,#-4]! */
845 vtop--;
846 } else {
847 plan2[keep]=s;
848 keep++;
849 vswap();
851 size = 8;
853 s=RC_INT;
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]);
858 #ifdef TCC_ARM_EABI
859 if(vtop->type.t == VT_VOID) {
860 if(s == RC_INT)
861 o(0xE24DD004); /* sub sp,sp,#4 */
862 vtop--;
863 } else
864 #endif
865 if(s == RC_INT) {
866 r = gv(s);
867 o(0xE52D0004|(intr(r)<<12)); /* str r,[sp,#-4]! */
868 vtop--;
869 } else {
870 plan2[keep]=s;
871 keep++;
873 args_size += size;
876 for(i=keep;i--;) {
877 gv(plan2[i]);
878 vrott(keep);
880 save_regs(keep); /* save used temporary registers */
881 keep++;
882 if(args_size) {
883 int n;
884 n=args_size/4;
885 if(n>4)
886 n=4;
887 todo&=((1<<n)-1);
888 if(todo) {
889 int i;
890 o(0xE8BD0000|todo);
891 for(i=0;i<4;i++)
892 if(todo&(1<<i)) {
893 vpushi(0);
894 vtop->r=i;
895 keep++;
898 args_size-=n*4;
900 vnrott(keep);
901 func_sym = vtop->type.ref;
902 gcall_or_jmp(0);
903 if (args_size)
904 gadd_sp(args_size);
905 #ifdef TCC_ARM_EABI
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);
910 ++keep;
912 #ifdef TCC_ARM_VFP
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 */
916 } else {
917 o(0xEE000B10); /* fmdlr d0,r0 */
918 o(0xEE201B10); /* fmdhr d0,r1 */
921 #endif
922 #endif
923 vtop-=keep;
924 leaffunc = 0;
927 /* generate function prolog of type 't' */
928 void gfunc_prolog(CType *func_type)
930 Sym *sym,*sym2;
931 int n,addr,size,align;
933 sym = func_type->ref;
934 func_vt = sym->type;
936 n = 0;
937 addr = 0;
938 if((func_vt.t & VT_BTYPE) == VT_STRUCT
939 && type_size(&func_vt,&align) > 4)
941 func_vc = addr;
942 addr += 4;
943 n++;
945 for(sym2=sym->next;sym2 && n<4;sym2=sym2->next) {
946 size = type_size(&sym2->type, &align);
947 n += (size + 3) / 4;
949 o(0xE1A0C00D); /* mov ip,sp */
950 if(func_type->ref->c == FUNC_ELLIPSIS)
951 n=4;
952 if(n) {
953 if(n>4)
954 n=4;
955 #ifdef TCC_ARM_EABI
956 n=(n+1)&-2;
957 #endif
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)) {
965 CType *type;
966 type = &sym->type;
967 size = type_size(type, &align);
968 size = (size + 3) & -4;
969 #ifdef TCC_ARM_EABI
970 addr = (addr + align - 1) & -align;
971 #endif
972 sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | lvalue_type(type->t), addr);
973 addr += size;
975 last_itod_magic=0;
976 leaffunc = 1;
977 loc = -12;
980 /* generate function epilog */
981 void gfunc_epilog(void)
983 unsigned long x;
984 int diff;
985 #ifdef TCC_ARM_EABI
986 if(is_float(func_vt.t)) {
987 if((func_vt.t & VT_BTYPE) == VT_FLOAT)
988 o(0xEE100A10); /* fmrs r0, s0 */
989 else {
990 o(0xEE100B10); /* fmrdl r0, d0 */
991 o(0xEE301B10); /* fmrdh r1, d0 */
994 #endif
995 o(0xE91BA800); /* restore fp, sp, pc */
996 diff = (-loc + 3) & -4;
997 #ifdef TCC_ARM_EABI
998 if(!leaffunc)
999 diff = (diff + 7) & -8;
1000 #endif
1001 if(diff > 12) {
1002 x=stuff_const(0xE24BD000, diff); /* sub sp,fp,# */
1003 if(x)
1004 *(unsigned long *)(cur_text_section->data + func_sub_sp_offset) = x;
1005 else {
1006 unsigned long addr;
1007 addr=ind;
1008 o(0xE59FC004); /* ldr ip,[pc+4] */
1009 o(0xE04BD00C); /* sub sp,fp,ip */
1010 o(0xE1A0F00E); /* mov pc,lr */
1011 o(diff);
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 */
1018 int gjmp(int t)
1020 int r;
1021 r=ind;
1022 o(0xE0000000|encbranch(r,t,1));
1023 return r;
1026 /* generate a jump to a fixed address */
1027 void gjmp_addr(int a)
1029 gjmp(a);
1032 /* generate a test. set 'inv' to invert test. Stack entry is popped */
1033 int gtst(int inv, int t)
1035 int v, r;
1036 unsigned long op;
1037 v = vtop->r & VT_VALMASK;
1038 r=ind;
1039 if (v == VT_CMP) {
1040 op=mapcc(inv?negcc(vtop->c.i):vtop->c.i);
1041 op|=encbranch(r,t,1);
1042 o(op);
1043 t=r;
1044 } else if (v == VT_JMP || v == VT_JMPI) {
1045 if ((v & 1) == inv) {
1046 if(!vtop->c.i)
1047 vtop->c.i=t;
1048 else {
1049 unsigned long *x;
1050 int p,lp;
1051 if(t) {
1052 p = vtop->c.i;
1053 do {
1054 p = decbranch(lp=p);
1055 } while(p);
1056 x = (unsigned long *)(cur_text_section->data + lp);
1057 *x &= 0xff000000;
1058 *x |= encbranch(lp,t,1);
1060 t = vtop->c.i;
1062 } else {
1063 t = gjmp(t);
1064 gsym(vtop->c.i);
1066 } else {
1067 if (is_float(vtop->type.t)) {
1068 r=gv(RC_FLOAT);
1069 #ifdef TCC_ARM_VFP
1070 o(0xEEB50A40|(vfpr(r)<<12)|T2CPR(vtop->type.t)); /* fcmpzX */
1071 o(0xEEF1FA10); /* fmstat */
1072 #else
1073 o(0xEE90F118|(fpr(r)<<16));
1074 #endif
1075 vtop->r = VT_CMP;
1076 vtop->c.i = TOK_NE;
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)
1081 t = gjmp(t);
1082 } else {
1083 v = gv(RC_INT);
1084 o(0xE3300000|(intr(v)<<16));
1085 vtop->r = VT_CMP;
1086 vtop->c.i = TOK_NE;
1087 return gtst(inv, t);
1090 vtop--;
1091 return t;
1094 /* generate an integer binary operation */
1095 void gen_opi(int op)
1097 int c, func = 0;
1098 unsigned long opc = 0,r,fr;
1099 unsigned short retreg = REG_IRET;
1101 c=0;
1102 switch(op) {
1103 case '+':
1104 opc = 0x8;
1105 c=1;
1106 break;
1107 case TOK_ADDC1: /* add with carry generation */
1108 opc = 0x9;
1109 c=1;
1110 break;
1111 case '-':
1112 opc = 0x4;
1113 c=1;
1114 break;
1115 case TOK_SUBC1: /* sub with carry generation */
1116 opc = 0x5;
1117 c=1;
1118 break;
1119 case TOK_ADDC2: /* add with carry use */
1120 opc = 0xA;
1121 c=1;
1122 break;
1123 case TOK_SUBC2: /* sub with carry use */
1124 opc = 0xC;
1125 c=1;
1126 break;
1127 case '&':
1128 opc = 0x0;
1129 c=1;
1130 break;
1131 case '^':
1132 opc = 0x2;
1133 c=1;
1134 break;
1135 case '|':
1136 opc = 0x18;
1137 c=1;
1138 break;
1139 case '*':
1140 gv2(RC_INT, RC_INT);
1141 r = vtop[-1].r;
1142 fr = vtop[0].r;
1143 vtop--;
1144 o(0xE0000090|(intr(r)<<16)|(intr(r)<<8)|intr(fr));
1145 return;
1146 case TOK_SHL:
1147 opc = 0;
1148 c=2;
1149 break;
1150 case TOK_SHR:
1151 opc = 1;
1152 c=2;
1153 break;
1154 case TOK_SAR:
1155 opc = 2;
1156 c=2;
1157 break;
1158 case '/':
1159 case TOK_PDIV:
1160 func=TOK___divsi3;
1161 c=3;
1162 break;
1163 case TOK_UDIV:
1164 func=TOK___udivsi3;
1165 c=3;
1166 break;
1167 case '%':
1168 #ifdef TCC_ARM_EABI
1169 func=TOK___aeabi_idivmod;
1170 retreg=REG_LRET;
1171 #else
1172 func=TOK___modsi3;
1173 #endif
1174 c=3;
1175 break;
1176 case TOK_UMOD:
1177 #ifdef TCC_ARM_EABI
1178 func=TOK___aeabi_uidivmod;
1179 retreg=REG_LRET;
1180 #else
1181 func=TOK___umodsi3;
1182 #endif
1183 c=3;
1184 break;
1185 case TOK_UMULL:
1186 gv2(RC_INT, RC_INT);
1187 r=intr(vtop[-1].r2=get_reg(RC_INT));
1188 c=vtop[-1].r;
1189 vtop[-1].r=get_reg_ex(RC_INT,regmask(c));
1190 vtop--;
1191 o(0xE0800090|(r<<16)|(intr(vtop->r)<<12)|(intr(c)<<8)|intr(vtop[1].r));
1192 return;
1193 default:
1194 opc = 0x15;
1195 c=1;
1196 break;
1198 switch(c) {
1199 case 1:
1200 if((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1201 if(opc == 4 || opc == 5 || opc == 0xc) {
1202 vswap();
1203 opc|=2; // sub -> rsb
1206 if ((vtop->r & VT_VALMASK) == VT_CMP ||
1207 (vtop->r & (VT_VALMASK & ~1)) == VT_JMP)
1208 gv(RC_INT);
1209 vswap();
1210 c=intr(gv(RC_INT));
1211 vswap();
1212 opc=0xE0000000|(opc<<20)|(c<<16);
1213 if((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1214 unsigned long x;
1215 x=stuff_const(opc|0x2000000,vtop->c.i);
1216 if(x) {
1217 r=intr(vtop[-1].r=get_reg_ex(RC_INT,regmask(vtop[-1].r)));
1218 o(x|(r<<12));
1219 goto done;
1222 fr=intr(gv(RC_INT));
1223 r=intr(vtop[-1].r=get_reg_ex(RC_INT,two2mask(vtop->r,vtop[-1].r)));
1224 o(opc|(r<<12)|fr);
1225 done:
1226 vtop--;
1227 if (op >= TOK_ULT && op <= TOK_GT) {
1228 vtop->r = VT_CMP;
1229 vtop->c.i = op;
1231 break;
1232 case 2:
1233 opc=0xE1A00000|(opc<<5);
1234 if ((vtop->r & VT_VALMASK) == VT_CMP ||
1235 (vtop->r & (VT_VALMASK & ~1)) == VT_JMP)
1236 gv(RC_INT);
1237 vswap();
1238 r=intr(gv(RC_INT));
1239 vswap();
1240 opc|=r;
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));
1245 } else {
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);
1250 vtop--;
1251 break;
1252 case 3:
1253 vpush_global_sym(&func_old_type, func);
1254 vrott(3);
1255 gfunc_call(2);
1256 vpushi(0);
1257 vtop->r = retreg;
1258 break;
1259 default:
1260 error("gen_opi %i unimplemented!",op);
1264 #ifdef TCC_ARM_VFP
1265 static int is_zero(int i)
1267 if((vtop[i].r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1268 return 0;
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)
1280 unsigned long x;
1281 int fneg=0,r;
1282 x=0xEE000A00|T2CPR(vtop->type.t);
1283 switch(op) {
1284 case '+':
1285 if(is_zero(-1))
1286 vswap();
1287 if(is_zero(0)) {
1288 vtop--;
1289 return;
1291 x|=0x300000;
1292 break;
1293 case '-':
1294 x|=0x300040;
1295 if(is_zero(0)) {
1296 vtop--;
1297 return;
1299 if(is_zero(-1)) {
1300 x|=0x810000; /* fsubX -> fnegX */
1301 vswap();
1302 vtop--;
1303 fneg=1;
1305 break;
1306 case '*':
1307 x|=0x200000;
1308 break;
1309 case '/':
1310 x|=0x800000;
1311 break;
1312 default:
1313 if(op < TOK_ULT && op > TOK_GT) {
1314 error("unknown fp op %x!",op);
1315 return;
1317 if(is_zero(-1)) {
1318 vswap();
1319 switch(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 */
1329 if(is_zero(0)) {
1330 vtop--;
1331 o(x|0x10000|(vfpr(gv(RC_FLOAT))<<12)); /* fcmp(e)X -> fcmp(e)zX */
1332 } else {
1333 x|=vfpr(gv(RC_FLOAT));
1334 vswap();
1335 o(x|(vfpr(gv(RC_FLOAT))<<12));
1336 vtop--;
1338 o(0xEEF1FA10); /* fmstat */
1340 switch(op) {
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;
1347 vtop->r = VT_CMP;
1348 vtop->c.i = op;
1349 return;
1351 r=gv(RC_FLOAT);
1352 x|=vfpr(r);
1353 r=regmask(r);
1354 if(!fneg) {
1355 int r2;
1356 vswap();
1357 r2=gv(RC_FLOAT);
1358 x|=vfpr(r2)<<16;
1359 r|=regmask(r2);
1361 vtop->r=get_reg_ex(RC_FLOAT,r);
1362 if(!fneg)
1363 vtop--;
1364 o(x|(vfpr(vtop->r)<<12));
1367 #else
1368 static int is_fconst()
1370 long double f;
1371 int r;
1372 if((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1373 return 0;
1374 if (vtop->type.t == VT_FLOAT)
1375 f = vtop->c.f;
1376 else if (vtop->type.t == VT_DOUBLE)
1377 f = vtop->c.d;
1378 else
1379 f = vtop->c.ld;
1380 if(!ieee_finite(f))
1381 return 0;
1382 r=0x8;
1383 if(f<0.0) {
1384 r=0x18;
1385 f=-f;
1387 if(f==0.0)
1388 return r;
1389 if(f==1.0)
1390 return r|1;
1391 if(f==2.0)
1392 return r|2;
1393 if(f==3.0)
1394 return r|3;
1395 if(f==4.0)
1396 return r|4;
1397 if(f==5.0)
1398 return r|5;
1399 if(f==0.5)
1400 return r|6;
1401 if(f==10.0)
1402 return r|7;
1403 return 0;
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)
1410 unsigned long x;
1411 int r,r2,c1,c2;
1412 //fputs("gen_opf\n",stderr);
1413 vswap();
1414 c1 = is_fconst();
1415 vswap();
1416 c2 = is_fconst();
1417 x=0xEE000100;
1418 #if LDOUBLE_SIZE == 8
1419 if ((vtop->type.t & VT_BTYPE) != VT_FLOAT)
1420 x|=0x80;
1421 #else
1422 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1423 x|=0x80;
1424 else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE)
1425 x|=0x80000;
1426 #endif
1427 switch(op)
1429 case '+':
1430 if(!c2) {
1431 vswap();
1432 c2=c1;
1434 vswap();
1435 r=fpr(gv(RC_FLOAT));
1436 vswap();
1437 if(c2) {
1438 if(c2>0xf)
1439 x|=0x200000; // suf
1440 r2=c2&0xf;
1441 } else {
1442 r2=fpr(gv(RC_FLOAT));
1444 break;
1445 case '-':
1446 if(c2) {
1447 if(c2<=0xf)
1448 x|=0x200000; // suf
1449 r2=c2&0xf;
1450 vswap();
1451 r=fpr(gv(RC_FLOAT));
1452 vswap();
1453 } else if(c1 && c1<=0xf) {
1454 x|=0x300000; // rsf
1455 r2=c1;
1456 r=fpr(gv(RC_FLOAT));
1457 vswap();
1458 } else {
1459 x|=0x200000; // suf
1460 vswap();
1461 r=fpr(gv(RC_FLOAT));
1462 vswap();
1463 r2=fpr(gv(RC_FLOAT));
1465 break;
1466 case '*':
1467 if(!c2 || c2>0xf) {
1468 vswap();
1469 c2=c1;
1471 vswap();
1472 r=fpr(gv(RC_FLOAT));
1473 vswap();
1474 if(c2 && c2<=0xf)
1475 r2=c2;
1476 else
1477 r2=fpr(gv(RC_FLOAT));
1478 x|=0x100000; // muf
1479 break;
1480 case '/':
1481 if(c2 && c2<=0xf) {
1482 x|=0x400000; // dvf
1483 r2=c2;
1484 vswap();
1485 r=fpr(gv(RC_FLOAT));
1486 vswap();
1487 } else if(c1 && c1<=0xf) {
1488 x|=0x500000; // rdf
1489 r2=c1;
1490 r=fpr(gv(RC_FLOAT));
1491 vswap();
1492 } else {
1493 x|=0x400000; // dvf
1494 vswap();
1495 r=fpr(gv(RC_FLOAT));
1496 vswap();
1497 r2=fpr(gv(RC_FLOAT));
1499 break;
1500 default:
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 */
1505 switch(op) {
1506 case TOK_ULT:
1507 case TOK_UGE:
1508 case TOK_ULE:
1509 case TOK_UGT:
1510 error("unsigned comparision on floats?");
1511 break;
1512 case TOK_LT:
1513 op=TOK_Nset;
1514 break;
1515 case TOK_LE:
1516 op=TOK_ULE; /* correct in unordered case only if AC bit in FPSR set */
1517 break;
1518 case TOK_EQ:
1519 case TOK_NE:
1520 x&=~0x400000; // cmfe -> cmf
1521 break;
1523 if(c1 && !c2) {
1524 c2=c1;
1525 vswap();
1526 switch(op) {
1527 case TOK_Nset:
1528 op=TOK_GT;
1529 break;
1530 case TOK_GE:
1531 op=TOK_ULE;
1532 break;
1533 case TOK_ULE:
1534 op=TOK_GE;
1535 break;
1536 case TOK_GT:
1537 op=TOK_Nset;
1538 break;
1541 vswap();
1542 r=fpr(gv(RC_FLOAT));
1543 vswap();
1544 if(c2) {
1545 if(c2>0xf)
1546 x|=0x200000;
1547 r2=c2&0xf;
1548 } else {
1549 r2=fpr(gv(RC_FLOAT));
1551 vtop[-1].r = VT_CMP;
1552 vtop[-1].c.i = op;
1553 } else {
1554 error("unknown fp op %x!",op);
1555 return;
1558 if(vtop[-1].r == VT_CMP)
1559 c1=15;
1560 else {
1561 c1=vtop->r;
1562 if(r2&0x8)
1563 c1=vtop[-1].r;
1564 vtop[-1].r=get_reg_ex(RC_FLOAT,two2mask(vtop[-1].r,c1));
1565 c1=fpr(vtop[-1].r);
1567 vtop--;
1568 o(x|(r<<16)|(c1<<12)|r2);
1570 #endif
1572 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1573 and 'long long' cases. */
1574 void gen_cvt_itof1(int t)
1576 int r,r2,bt;
1577 bt=vtop->type.t & VT_BTYPE;
1578 if(bt == VT_INT || bt == VT_SHORT || bt == VT_BYTE) {
1579 #ifndef TCC_ARM_VFP
1580 unsigned int dsize=0;
1581 #endif
1582 r=intr(gv(RC_INT));
1583 #ifdef TCC_ARM_VFP
1584 r2=vfpr(vtop->r=get_reg(RC_FLOAT));
1585 o(0xEE000A10|(r<<12)|(r2<<16)); /* fmsr */
1586 r2<<=12;
1587 if(!(vtop->type.t & VT_UNSIGNED))
1588 r2|=0x80; /* fuitoX -> fsituX */
1589 o(0xEEB80A40|r2|T2CPR(t)); /* fYitoX*/
1590 #else
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)) {
1596 unsigned int off=0;
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;
1601 off/=4;
1602 if(off>255)
1603 off=0;
1605 o(0xBD1F0100|(r<<12)|off); /* ldflts */
1606 if(!off) {
1607 o(0xEA000000); /* b */
1608 last_itod_magic=ind;
1609 o(0x4F800000); /* 4294967296.0f */
1611 o(0xBE000100|dsize|(r2<<16)|(r2<<12)|r); /* adflt */
1613 #endif
1614 return;
1615 } else if(bt == VT_LLONG) {
1616 int func;
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;
1622 else
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;
1629 else
1630 func=TOK___floatdixf;
1631 } else if((t & VT_BTYPE) == VT_DOUBLE) {
1632 #else
1633 } else if((t & VT_BTYPE) == VT_DOUBLE || (t & VT_BTYPE) == VT_LDOUBLE) {
1634 #endif
1635 func_type = &func_double_type;
1636 if(vtop->type.t & VT_UNSIGNED)
1637 func=TOK___floatundidf;
1638 else
1639 func=TOK___floatdidf;
1641 if(func_type) {
1642 vpush_global_sym(func_type, func);
1643 vswap();
1644 gfunc_call(1);
1645 vpushi(0);
1646 vtop->r=TREG_F0;
1647 return;
1650 error("unimplemented gen_cvt_itof %x!",vtop->type.t);
1653 /* convert fp to int 't' type */
1654 void gen_cvt_ftoi(int t)
1656 int r,r2,u,func=0;
1657 u=t&VT_UNSIGNED;
1658 t&=VT_BTYPE;
1659 r2=vtop->type.t & VT_BTYPE;
1660 if(t==VT_INT) {
1661 #ifdef TCC_ARM_VFP
1662 r=vfpr(gv(RC_FLOAT));
1663 u=u?0:0x10000;
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));
1667 return;
1668 #else
1669 if(u) {
1670 if(r2 == VT_FLOAT)
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)
1676 #else
1677 else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE)
1678 #endif
1679 func=TOK___fixunsdfsi;
1680 } else {
1681 r=fpr(gv(RC_FLOAT));
1682 r2=intr(vtop->r=get_reg(RC_INT));
1683 o(0xEE100170|(r2<<12)|r);
1684 return;
1686 #endif
1687 } else if(t == VT_LLONG) { // unsigned handled in gen_cvt_ftoi1
1688 if(r2 == VT_FLOAT)
1689 func=TOK___fixsfdi;
1690 #if LDOUBLE_SIZE != 8
1691 else if(r2 == VT_LDOUBLE)
1692 func=TOK___fixxfdi;
1693 else if(r2 == VT_DOUBLE)
1694 #else
1695 else if(r2 == VT_LDOUBLE || r2 == VT_DOUBLE)
1696 #endif
1697 func=TOK___fixdfdi;
1699 if(func) {
1700 vpush_global_sym(&func_old_type, func);
1701 vswap();
1702 gfunc_call(1);
1703 vpushi(0);
1704 if(t == VT_LLONG)
1705 vtop->r2 = REG_LRET;
1706 vtop->r = REG_IRET;
1707 return;
1709 error("unimplemented gen_cvt_ftoi!");
1712 /* convert from one floating point type to another */
1713 void gen_cvt_ftof(int t)
1715 #ifdef TCC_ARM_VFP
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));
1720 #else
1721 /* all we have to do on i386 and FPA ARM is to put the float in a register */
1722 gv(RC_FLOAT);
1723 #endif
1726 /* computed goto support */
1727 void ggoto(void)
1729 gcall_or_jmp(1);
1730 vtop--;
1733 /* end of ARM code generator */
1734 /*************************************************************/