Fixed compilation error in i386-asm.c
[tinycc/kirr.git] / tccgen.c
blob78a5161feb369c6baf1903f4614025b56fafbad1
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 void swap(int *p, int *q)
23 int t;
24 t = *p;
25 *p = *q;
26 *q = t;
29 void vsetc(CType *type, int r, CValue *vc)
31 int v;
33 if (vtop >= vstack + (VSTACK_SIZE - 1))
34 error("memory full");
35 /* cannot let cpu flags if other instruction are generated. Also
36 avoid leaving VT_JMP anywhere except on the top of the stack
37 because it would complicate the code generator. */
38 if (vtop >= vstack) {
39 v = vtop->r & VT_VALMASK;
40 if (v == VT_CMP || (v & ~1) == VT_JMP)
41 gv(RC_INT);
43 vtop++;
44 vtop->type = *type;
45 vtop->r = r;
46 vtop->r2 = VT_CONST;
47 vtop->c = *vc;
50 /* push integer constant */
51 void vpushi(int v)
53 CValue cval;
54 cval.i = v;
55 vsetc(&int_type, VT_CONST, &cval);
58 /* push long long constant */
59 void vpushll(long long v)
61 CValue cval;
62 CType ctype;
63 ctype.t = VT_LLONG;
64 cval.ull = v;
65 vsetc(&ctype, VT_CONST, &cval);
68 /* Return a static symbol pointing to a section */
69 static Sym *get_sym_ref(CType *type, Section *sec,
70 unsigned long offset, unsigned long size)
72 int v;
73 Sym *sym;
75 v = anon_sym++;
76 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
77 sym->type.ref = type->ref;
78 sym->r = VT_CONST | VT_SYM;
79 put_extern_sym(sym, sec, offset, size);
80 return sym;
83 /* push a reference to a section offset by adding a dummy symbol */
84 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
86 CValue cval;
88 cval.ul = 0;
89 vsetc(type, VT_CONST | VT_SYM, &cval);
90 vtop->sym = get_sym_ref(type, sec, offset, size);
93 /* define a new external reference to a symbol 'v' of type 'u' */
94 static Sym *external_global_sym(int v, CType *type, int r)
96 Sym *s;
98 s = sym_find(v);
99 if (!s) {
100 /* push forward reference */
101 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
102 s->type.ref = type->ref;
103 s->r = r | VT_CONST | VT_SYM;
105 return s;
108 /* define a new external reference to a symbol 'v' of type 'u' */
109 static Sym *external_sym(int v, CType *type, int r)
111 Sym *s;
113 s = sym_find(v);
114 if (!s) {
115 /* push forward reference */
116 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
117 s->type.t |= VT_EXTERN;
118 } else if (s->type.ref == func_old_type.ref) {
119 s->type.ref = type->ref;
120 s->r = r | VT_CONST | VT_SYM;
121 s->type.t |= VT_EXTERN;
122 } else if (!is_compatible_types(&s->type, type)) {
123 error("incompatible types for redefinition of '%s'",
124 get_tok_str(v, NULL));
126 return s;
129 /* push a reference to global symbol v */
130 static void vpush_global_sym(CType *type, int v)
132 Sym *sym;
133 CValue cval;
135 sym = external_global_sym(v, type, 0);
136 cval.ul = 0;
137 vsetc(type, VT_CONST | VT_SYM, &cval);
138 vtop->sym = sym;
141 void vset(CType *type, int r, int v)
143 CValue cval;
145 cval.i = v;
146 vsetc(type, r, &cval);
149 void vseti(int r, int v)
151 CType type;
152 type.t = VT_INT;
153 vset(&type, r, v);
156 void vswap(void)
158 SValue tmp;
160 tmp = vtop[0];
161 vtop[0] = vtop[-1];
162 vtop[-1] = tmp;
165 void vpushv(SValue *v)
167 if (vtop >= vstack + (VSTACK_SIZE - 1))
168 error("memory full");
169 vtop++;
170 *vtop = *v;
173 void vdup(void)
175 vpushv(vtop);
178 /* save r to the memory stack, and mark it as being free */
179 void save_reg(int r)
181 int l, saved, size, align;
182 SValue *p, sv;
183 CType *type;
185 /* modify all stack values */
186 saved = 0;
187 l = 0;
188 for(p=vstack;p<=vtop;p++) {
189 if ((p->r & VT_VALMASK) == r ||
190 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
191 /* must save value on stack if not already done */
192 if (!saved) {
193 /* NOTE: must reload 'r' because r might be equal to r2 */
194 r = p->r & VT_VALMASK;
195 /* store register in the stack */
196 type = &p->type;
197 if ((p->r & VT_LVAL) ||
198 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
199 #ifdef TCC_TARGET_X86_64
200 type = &char_pointer_type;
201 #else
202 type = &int_type;
203 #endif
204 size = type_size(type, &align);
205 loc = (loc - size) & -align;
206 sv.type.t = type->t;
207 sv.r = VT_LOCAL | VT_LVAL;
208 sv.c.ul = loc;
209 store(r, &sv);
210 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
211 /* x86 specific: need to pop fp register ST0 if saved */
212 if (r == TREG_ST0) {
213 o(0xd8dd); /* fstp %st(0) */
215 #endif
216 #ifndef TCC_TARGET_X86_64
217 /* special long long case */
218 if ((type->t & VT_BTYPE) == VT_LLONG) {
219 sv.c.ul += 4;
220 store(p->r2, &sv);
222 #endif
223 l = loc;
224 saved = 1;
226 /* mark that stack entry as being saved on the stack */
227 if (p->r & VT_LVAL) {
228 /* also clear the bounded flag because the
229 relocation address of the function was stored in
230 p->c.ul */
231 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
232 } else {
233 p->r = lvalue_type(p->type.t) | VT_LOCAL;
235 p->r2 = VT_CONST;
236 p->c.ul = l;
241 /* find a register of class 'rc2' with at most one reference on stack.
242 * If none, call get_reg(rc) */
243 int get_reg_ex(int rc, int rc2)
245 int r;
246 SValue *p;
248 for(r=0;r<NB_REGS;r++) {
249 if (reg_classes[r] & rc2) {
250 int n;
251 n=0;
252 for(p = vstack; p <= vtop; p++) {
253 if ((p->r & VT_VALMASK) == r ||
254 (p->r2 & VT_VALMASK) == r)
255 n++;
257 if (n <= 1)
258 return r;
261 return get_reg(rc);
264 /* find a free register of class 'rc'. If none, save one register */
265 int get_reg(int rc)
267 int r;
268 SValue *p;
270 /* find a free register */
271 for(r=0;r<NB_REGS;r++) {
272 if (reg_classes[r] & rc) {
273 for(p=vstack;p<=vtop;p++) {
274 if ((p->r & VT_VALMASK) == r ||
275 (p->r2 & VT_VALMASK) == r)
276 goto notfound;
278 return r;
280 notfound: ;
283 /* no register left : free the first one on the stack (VERY
284 IMPORTANT to start from the bottom to ensure that we don't
285 spill registers used in gen_opi()) */
286 for(p=vstack;p<=vtop;p++) {
287 r = p->r & VT_VALMASK;
288 if (r < VT_CONST && (reg_classes[r] & rc))
289 goto save_found;
290 /* also look at second register (if long long) */
291 r = p->r2 & VT_VALMASK;
292 if (r < VT_CONST && (reg_classes[r] & rc)) {
293 save_found:
294 save_reg(r);
295 return r;
298 /* Should never comes here */
299 return -1;
302 /* save registers up to (vtop - n) stack entry */
303 void save_regs(int n)
305 int r;
306 SValue *p, *p1;
307 p1 = vtop - n;
308 for(p = vstack;p <= p1; p++) {
309 r = p->r & VT_VALMASK;
310 if (r < VT_CONST) {
311 save_reg(r);
316 /* move register 's' to 'r', and flush previous value of r to memory
317 if needed */
318 void move_reg(int r, int s)
320 SValue sv;
322 if (r != s) {
323 save_reg(r);
324 sv.type.t = VT_INT;
325 sv.r = s;
326 sv.c.ul = 0;
327 load(r, &sv);
331 /* get address of vtop (vtop MUST BE an lvalue) */
332 void gaddrof(void)
334 vtop->r &= ~VT_LVAL;
335 /* tricky: if saved lvalue, then we can go back to lvalue */
336 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
337 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
340 #ifdef CONFIG_TCC_BCHECK
341 /* generate lvalue bound code */
342 void gbound(void)
344 int lval_type;
345 CType type1;
347 vtop->r &= ~VT_MUSTBOUND;
348 /* if lvalue, then use checking code before dereferencing */
349 if (vtop->r & VT_LVAL) {
350 /* if not VT_BOUNDED value, then make one */
351 if (!(vtop->r & VT_BOUNDED)) {
352 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
353 /* must save type because we must set it to int to get pointer */
354 type1 = vtop->type;
355 vtop->type.t = VT_INT;
356 gaddrof();
357 vpushi(0);
358 gen_bounded_ptr_add();
359 vtop->r |= lval_type;
360 vtop->type = type1;
362 /* then check for dereferencing */
363 gen_bounded_ptr_deref();
366 #endif
368 /* store vtop a register belonging to class 'rc'. lvalues are
369 converted to values. Cannot be used if cannot be converted to
370 register value (such as structures). */
371 int gv(int rc)
373 int r, rc2, bit_pos, bit_size, size, align, i;
375 /* NOTE: get_reg can modify vstack[] */
376 if (vtop->type.t & VT_BITFIELD) {
377 CType type;
378 int bits = 32;
379 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
380 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
381 /* remove bit field info to avoid loops */
382 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
383 /* cast to int to propagate signedness in following ops */
384 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
385 type.t = VT_LLONG;
386 bits = 64;
387 } else
388 type.t = VT_INT;
389 if((vtop->type.t & VT_UNSIGNED) ||
390 (vtop->type.t & VT_BTYPE) == VT_BOOL)
391 type.t |= VT_UNSIGNED;
392 gen_cast(&type);
393 /* generate shifts */
394 vpushi(bits - (bit_pos + bit_size));
395 gen_op(TOK_SHL);
396 vpushi(bits - bit_size);
397 /* NOTE: transformed to SHR if unsigned */
398 gen_op(TOK_SAR);
399 r = gv(rc);
400 } else {
401 if (is_float(vtop->type.t) &&
402 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
403 Sym *sym;
404 int *ptr;
405 unsigned long offset;
406 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
407 CValue check;
408 #endif
410 /* XXX: unify with initializers handling ? */
411 /* CPUs usually cannot use float constants, so we store them
412 generically in data segment */
413 size = type_size(&vtop->type, &align);
414 offset = (data_section->data_offset + align - 1) & -align;
415 data_section->data_offset = offset;
416 /* XXX: not portable yet */
417 #if defined(__i386__) || defined(__x86_64__)
418 /* Zero pad x87 tenbyte long doubles */
419 if (size == LDOUBLE_SIZE)
420 vtop->c.tab[2] &= 0xffff;
421 #endif
422 ptr = section_ptr_add(data_section, size);
423 size = size >> 2;
424 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
425 check.d = 1;
426 if(check.tab[0])
427 for(i=0;i<size;i++)
428 ptr[i] = vtop->c.tab[size-1-i];
429 else
430 #endif
431 for(i=0;i<size;i++)
432 ptr[i] = vtop->c.tab[i];
433 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
434 vtop->r |= VT_LVAL | VT_SYM;
435 vtop->sym = sym;
436 vtop->c.ul = 0;
438 #ifdef CONFIG_TCC_BCHECK
439 if (vtop->r & VT_MUSTBOUND)
440 gbound();
441 #endif
443 r = vtop->r & VT_VALMASK;
444 rc2 = RC_INT;
445 if (rc == RC_IRET)
446 rc2 = RC_LRET;
447 /* need to reload if:
448 - constant
449 - lvalue (need to dereference pointer)
450 - already a register, but not in the right class */
451 if (r >= VT_CONST ||
452 (vtop->r & VT_LVAL) ||
453 !(reg_classes[r] & rc) ||
454 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
455 !(reg_classes[vtop->r2] & rc2))) {
456 r = get_reg(rc);
457 #ifndef TCC_TARGET_X86_64
458 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
459 int r2;
460 unsigned long long ll;
461 /* two register type load : expand to two words
462 temporarily */
463 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
464 /* load constant */
465 ll = vtop->c.ull;
466 vtop->c.ui = ll; /* first word */
467 load(r, vtop);
468 vtop->r = r; /* save register value */
469 vpushi(ll >> 32); /* second word */
470 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
471 (vtop->r & VT_LVAL)) {
472 /* We do not want to modifier the long long
473 pointer here, so the safest (and less
474 efficient) is to save all the other registers
475 in the stack. XXX: totally inefficient. */
476 save_regs(1);
477 /* load from memory */
478 load(r, vtop);
479 vdup();
480 vtop[-1].r = r; /* save register value */
481 /* increment pointer to get second word */
482 vtop->type.t = VT_INT;
483 gaddrof();
484 vpushi(4);
485 gen_op('+');
486 vtop->r |= VT_LVAL;
487 } else {
488 /* move registers */
489 load(r, vtop);
490 vdup();
491 vtop[-1].r = r; /* save register value */
492 vtop->r = vtop[-1].r2;
494 /* allocate second register */
495 r2 = get_reg(rc2);
496 load(r2, vtop);
497 vpop();
498 /* write second register */
499 vtop->r2 = r2;
500 } else
501 #endif
502 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
503 int t1, t;
504 /* lvalue of scalar type : need to use lvalue type
505 because of possible cast */
506 t = vtop->type.t;
507 t1 = t;
508 /* compute memory access type */
509 if (vtop->r & VT_LVAL_BYTE)
510 t = VT_BYTE;
511 else if (vtop->r & VT_LVAL_SHORT)
512 t = VT_SHORT;
513 if (vtop->r & VT_LVAL_UNSIGNED)
514 t |= VT_UNSIGNED;
515 vtop->type.t = t;
516 load(r, vtop);
517 /* restore wanted type */
518 vtop->type.t = t1;
519 } else {
520 /* one register type load */
521 load(r, vtop);
524 vtop->r = r;
525 #ifdef TCC_TARGET_C67
526 /* uses register pairs for doubles */
527 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
528 vtop->r2 = r+1;
529 #endif
531 return r;
534 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
535 void gv2(int rc1, int rc2)
537 int v;
539 /* generate more generic register first. But VT_JMP or VT_CMP
540 values must be generated first in all cases to avoid possible
541 reload errors */
542 v = vtop[0].r & VT_VALMASK;
543 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
544 vswap();
545 gv(rc1);
546 vswap();
547 gv(rc2);
548 /* test if reload is needed for first register */
549 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
550 vswap();
551 gv(rc1);
552 vswap();
554 } else {
555 gv(rc2);
556 vswap();
557 gv(rc1);
558 vswap();
559 /* test if reload is needed for first register */
560 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
561 gv(rc2);
566 /* wrapper around RC_FRET to return a register by type */
567 int rc_fret(int t)
569 #ifdef TCC_TARGET_X86_64
570 if (t == VT_LDOUBLE) {
571 return RC_ST0;
573 #endif
574 return RC_FRET;
577 /* wrapper around REG_FRET to return a register by type */
578 int reg_fret(int t)
580 #ifdef TCC_TARGET_X86_64
581 if (t == VT_LDOUBLE) {
582 return TREG_ST0;
584 #endif
585 return REG_FRET;
588 /* expand long long on stack in two int registers */
589 void lexpand(void)
591 int u;
593 u = vtop->type.t & VT_UNSIGNED;
594 gv(RC_INT);
595 vdup();
596 vtop[0].r = vtop[-1].r2;
597 vtop[0].r2 = VT_CONST;
598 vtop[-1].r2 = VT_CONST;
599 vtop[0].type.t = VT_INT | u;
600 vtop[-1].type.t = VT_INT | u;
603 #ifdef TCC_TARGET_ARM
604 /* expand long long on stack */
605 void lexpand_nr(void)
607 int u,v;
609 u = vtop->type.t & VT_UNSIGNED;
610 vdup();
611 vtop->r2 = VT_CONST;
612 vtop->type.t = VT_INT | u;
613 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
614 if (v == VT_CONST) {
615 vtop[-1].c.ui = vtop->c.ull;
616 vtop->c.ui = vtop->c.ull >> 32;
617 vtop->r = VT_CONST;
618 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
619 vtop->c.ui += 4;
620 vtop->r = vtop[-1].r;
621 } else if (v > VT_CONST) {
622 vtop--;
623 lexpand();
624 } else
625 vtop->r = vtop[-1].r2;
626 vtop[-1].r2 = VT_CONST;
627 vtop[-1].type.t = VT_INT | u;
629 #endif
631 /* build a long long from two ints */
632 void lbuild(int t)
634 gv2(RC_INT, RC_INT);
635 vtop[-1].r2 = vtop[0].r;
636 vtop[-1].type.t = t;
637 vpop();
640 /* rotate n first stack elements to the bottom
641 I1 ... In -> I2 ... In I1 [top is right]
643 void vrotb(int n)
645 int i;
646 SValue tmp;
648 tmp = vtop[-n + 1];
649 for(i=-n+1;i!=0;i++)
650 vtop[i] = vtop[i+1];
651 vtop[0] = tmp;
654 /* rotate n first stack elements to the top
655 I1 ... In -> In I1 ... I(n-1) [top is right]
657 void vrott(int n)
659 int i;
660 SValue tmp;
662 tmp = vtop[0];
663 for(i = 0;i < n - 1; i++)
664 vtop[-i] = vtop[-i - 1];
665 vtop[-n + 1] = tmp;
668 #ifdef TCC_TARGET_ARM
669 /* like vrott but in other direction
670 In ... I1 -> I(n-1) ... I1 In [top is right]
672 void vnrott(int n)
674 int i;
675 SValue tmp;
677 tmp = vtop[-n + 1];
678 for(i = n - 1; i > 0; i--)
679 vtop[-i] = vtop[-i + 1];
680 vtop[0] = tmp;
682 #endif
684 /* pop stack value */
685 void vpop(void)
687 int v;
688 v = vtop->r & VT_VALMASK;
689 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
690 /* for x86, we need to pop the FP stack */
691 if (v == TREG_ST0 && !nocode_wanted) {
692 o(0xd8dd); /* fstp %st(0) */
693 } else
694 #endif
695 if (v == VT_JMP || v == VT_JMPI) {
696 /* need to put correct jump if && or || without test */
697 gsym(vtop->c.ul);
699 vtop--;
702 /* convert stack entry to register and duplicate its value in another
703 register */
704 void gv_dup(void)
706 int rc, t, r, r1;
707 SValue sv;
709 t = vtop->type.t;
710 if ((t & VT_BTYPE) == VT_LLONG) {
711 lexpand();
712 gv_dup();
713 vswap();
714 vrotb(3);
715 gv_dup();
716 vrotb(4);
717 /* stack: H L L1 H1 */
718 lbuild(t);
719 vrotb(3);
720 vrotb(3);
721 vswap();
722 lbuild(t);
723 vswap();
724 } else {
725 /* duplicate value */
726 rc = RC_INT;
727 sv.type.t = VT_INT;
728 if (is_float(t)) {
729 rc = RC_FLOAT;
730 #ifdef TCC_TARGET_X86_64
731 if ((t & VT_BTYPE) == VT_LDOUBLE) {
732 rc = RC_ST0;
734 #endif
735 sv.type.t = t;
737 r = gv(rc);
738 r1 = get_reg(rc);
739 sv.r = r;
740 sv.c.ul = 0;
741 load(r1, &sv); /* move r to r1 */
742 vdup();
743 /* duplicates value */
744 if (r != r1)
745 vtop->r = r1;
749 #ifndef TCC_TARGET_X86_64
750 /* generate CPU independent (unsigned) long long operations */
751 void gen_opl(int op)
753 int t, a, b, op1, c, i;
754 int func;
755 unsigned short reg_iret = REG_IRET;
756 unsigned short reg_lret = REG_LRET;
757 SValue tmp;
759 switch(op) {
760 case '/':
761 case TOK_PDIV:
762 func = TOK___divdi3;
763 goto gen_func;
764 case TOK_UDIV:
765 func = TOK___udivdi3;
766 goto gen_func;
767 case '%':
768 func = TOK___moddi3;
769 goto gen_mod_func;
770 case TOK_UMOD:
771 func = TOK___umoddi3;
772 gen_mod_func:
773 #ifdef TCC_ARM_EABI
774 reg_iret = TREG_R2;
775 reg_lret = TREG_R3;
776 #endif
777 gen_func:
778 /* call generic long long function */
779 vpush_global_sym(&func_old_type, func);
780 vrott(3);
781 gfunc_call(2);
782 vpushi(0);
783 vtop->r = reg_iret;
784 vtop->r2 = reg_lret;
785 break;
786 case '^':
787 case '&':
788 case '|':
789 case '*':
790 case '+':
791 case '-':
792 t = vtop->type.t;
793 vswap();
794 lexpand();
795 vrotb(3);
796 lexpand();
797 /* stack: L1 H1 L2 H2 */
798 tmp = vtop[0];
799 vtop[0] = vtop[-3];
800 vtop[-3] = tmp;
801 tmp = vtop[-2];
802 vtop[-2] = vtop[-3];
803 vtop[-3] = tmp;
804 vswap();
805 /* stack: H1 H2 L1 L2 */
806 if (op == '*') {
807 vpushv(vtop - 1);
808 vpushv(vtop - 1);
809 gen_op(TOK_UMULL);
810 lexpand();
811 /* stack: H1 H2 L1 L2 ML MH */
812 for(i=0;i<4;i++)
813 vrotb(6);
814 /* stack: ML MH H1 H2 L1 L2 */
815 tmp = vtop[0];
816 vtop[0] = vtop[-2];
817 vtop[-2] = tmp;
818 /* stack: ML MH H1 L2 H2 L1 */
819 gen_op('*');
820 vrotb(3);
821 vrotb(3);
822 gen_op('*');
823 /* stack: ML MH M1 M2 */
824 gen_op('+');
825 gen_op('+');
826 } else if (op == '+' || op == '-') {
827 /* XXX: add non carry method too (for MIPS or alpha) */
828 if (op == '+')
829 op1 = TOK_ADDC1;
830 else
831 op1 = TOK_SUBC1;
832 gen_op(op1);
833 /* stack: H1 H2 (L1 op L2) */
834 vrotb(3);
835 vrotb(3);
836 gen_op(op1 + 1); /* TOK_xxxC2 */
837 } else {
838 gen_op(op);
839 /* stack: H1 H2 (L1 op L2) */
840 vrotb(3);
841 vrotb(3);
842 /* stack: (L1 op L2) H1 H2 */
843 gen_op(op);
844 /* stack: (L1 op L2) (H1 op H2) */
846 /* stack: L H */
847 lbuild(t);
848 break;
849 case TOK_SAR:
850 case TOK_SHR:
851 case TOK_SHL:
852 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
853 t = vtop[-1].type.t;
854 vswap();
855 lexpand();
856 vrotb(3);
857 /* stack: L H shift */
858 c = (int)vtop->c.i;
859 /* constant: simpler */
860 /* NOTE: all comments are for SHL. the other cases are
861 done by swaping words */
862 vpop();
863 if (op != TOK_SHL)
864 vswap();
865 if (c >= 32) {
866 /* stack: L H */
867 vpop();
868 if (c > 32) {
869 vpushi(c - 32);
870 gen_op(op);
872 if (op != TOK_SAR) {
873 vpushi(0);
874 } else {
875 gv_dup();
876 vpushi(31);
877 gen_op(TOK_SAR);
879 vswap();
880 } else {
881 vswap();
882 gv_dup();
883 /* stack: H L L */
884 vpushi(c);
885 gen_op(op);
886 vswap();
887 vpushi(32 - c);
888 if (op == TOK_SHL)
889 gen_op(TOK_SHR);
890 else
891 gen_op(TOK_SHL);
892 vrotb(3);
893 /* stack: L L H */
894 vpushi(c);
895 if (op == TOK_SHL)
896 gen_op(TOK_SHL);
897 else
898 gen_op(TOK_SHR);
899 gen_op('|');
901 if (op != TOK_SHL)
902 vswap();
903 lbuild(t);
904 } else {
905 /* XXX: should provide a faster fallback on x86 ? */
906 switch(op) {
907 case TOK_SAR:
908 func = TOK___ashrdi3;
909 goto gen_func;
910 case TOK_SHR:
911 func = TOK___lshrdi3;
912 goto gen_func;
913 case TOK_SHL:
914 func = TOK___ashldi3;
915 goto gen_func;
918 break;
919 default:
920 /* compare operations */
921 t = vtop->type.t;
922 vswap();
923 lexpand();
924 vrotb(3);
925 lexpand();
926 /* stack: L1 H1 L2 H2 */
927 tmp = vtop[-1];
928 vtop[-1] = vtop[-2];
929 vtop[-2] = tmp;
930 /* stack: L1 L2 H1 H2 */
931 /* compare high */
932 op1 = op;
933 /* when values are equal, we need to compare low words. since
934 the jump is inverted, we invert the test too. */
935 if (op1 == TOK_LT)
936 op1 = TOK_LE;
937 else if (op1 == TOK_GT)
938 op1 = TOK_GE;
939 else if (op1 == TOK_ULT)
940 op1 = TOK_ULE;
941 else if (op1 == TOK_UGT)
942 op1 = TOK_UGE;
943 a = 0;
944 b = 0;
945 gen_op(op1);
946 if (op1 != TOK_NE) {
947 a = gtst(1, 0);
949 if (op != TOK_EQ) {
950 /* generate non equal test */
951 /* XXX: NOT PORTABLE yet */
952 if (a == 0) {
953 b = gtst(0, 0);
954 } else {
955 #if defined(TCC_TARGET_I386)
956 b = psym(0x850f, 0);
957 #elif defined(TCC_TARGET_ARM)
958 b = ind;
959 o(0x1A000000 | encbranch(ind, 0, 1));
960 #elif defined(TCC_TARGET_C67)
961 error("not implemented");
962 #else
963 #error not supported
964 #endif
967 /* compare low. Always unsigned */
968 op1 = op;
969 if (op1 == TOK_LT)
970 op1 = TOK_ULT;
971 else if (op1 == TOK_LE)
972 op1 = TOK_ULE;
973 else if (op1 == TOK_GT)
974 op1 = TOK_UGT;
975 else if (op1 == TOK_GE)
976 op1 = TOK_UGE;
977 gen_op(op1);
978 a = gtst(1, a);
979 gsym(b);
980 vseti(VT_JMPI, a);
981 break;
984 #endif
986 /* handle integer constant optimizations and various machine
987 independent opt */
988 void gen_opic(int op)
990 int c1, c2, t1, t2, n;
991 SValue *v1, *v2;
992 long long l1, l2;
993 typedef unsigned long long U;
995 v1 = vtop - 1;
996 v2 = vtop;
997 t1 = v1->type.t & VT_BTYPE;
998 t2 = v2->type.t & VT_BTYPE;
1000 if (t1 == VT_LLONG)
1001 l1 = v1->c.ll;
1002 else if (v1->type.t & VT_UNSIGNED)
1003 l1 = v1->c.ui;
1004 else
1005 l1 = v1->c.i;
1007 if (t2 == VT_LLONG)
1008 l2 = v2->c.ll;
1009 else if (v2->type.t & VT_UNSIGNED)
1010 l2 = v2->c.ui;
1011 else
1012 l2 = v2->c.i;
1014 /* currently, we cannot do computations with forward symbols */
1015 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1016 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1017 if (c1 && c2) {
1018 switch(op) {
1019 case '+': l1 += l2; break;
1020 case '-': l1 -= l2; break;
1021 case '&': l1 &= l2; break;
1022 case '^': l1 ^= l2; break;
1023 case '|': l1 |= l2; break;
1024 case '*': l1 *= l2; break;
1026 case TOK_PDIV:
1027 case '/':
1028 case '%':
1029 case TOK_UDIV:
1030 case TOK_UMOD:
1031 /* if division by zero, generate explicit division */
1032 if (l2 == 0) {
1033 if (const_wanted)
1034 error("division by zero in constant");
1035 goto general_case;
1037 switch(op) {
1038 default: l1 /= l2; break;
1039 case '%': l1 %= l2; break;
1040 case TOK_UDIV: l1 = (U)l1 / l2; break;
1041 case TOK_UMOD: l1 = (U)l1 % l2; break;
1043 break;
1044 case TOK_SHL: l1 <<= l2; break;
1045 case TOK_SHR: l1 = (U)l1 >> l2; break;
1046 case TOK_SAR: l1 >>= l2; break;
1047 /* tests */
1048 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1049 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1050 case TOK_EQ: l1 = l1 == l2; break;
1051 case TOK_NE: l1 = l1 != l2; break;
1052 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1053 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1054 case TOK_LT: l1 = l1 < l2; break;
1055 case TOK_GE: l1 = l1 >= l2; break;
1056 case TOK_LE: l1 = l1 <= l2; break;
1057 case TOK_GT: l1 = l1 > l2; break;
1058 /* logical */
1059 case TOK_LAND: l1 = l1 && l2; break;
1060 case TOK_LOR: l1 = l1 || l2; break;
1061 default:
1062 goto general_case;
1064 v1->c.ll = l1;
1065 vtop--;
1066 } else {
1067 /* if commutative ops, put c2 as constant */
1068 if (c1 && (op == '+' || op == '&' || op == '^' ||
1069 op == '|' || op == '*')) {
1070 vswap();
1071 c2 = c1; //c = c1, c1 = c2, c2 = c;
1072 l2 = l1; //l = l1, l1 = l2, l2 = l;
1074 /* Filter out NOP operations like x*1, x-0, x&-1... */
1075 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1076 op == TOK_PDIV) &&
1077 l2 == 1) ||
1078 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1079 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1080 l2 == 0) ||
1081 (op == '&' &&
1082 l2 == -1))) {
1083 /* nothing to do */
1084 vtop--;
1085 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1086 /* try to use shifts instead of muls or divs */
1087 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1088 n = -1;
1089 while (l2) {
1090 l2 >>= 1;
1091 n++;
1093 vtop->c.ll = n;
1094 if (op == '*')
1095 op = TOK_SHL;
1096 else if (op == TOK_PDIV)
1097 op = TOK_SAR;
1098 else
1099 op = TOK_SHR;
1101 goto general_case;
1102 } else if (c2 && (op == '+' || op == '-') &&
1103 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM)
1104 && !(vtop[-1].sym->type.t & VT_IMPORT))
1106 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1107 /* symbol + constant case */
1108 if (op == '-')
1109 l2 = -l2;
1110 vtop--;
1111 vtop->c.ll += l2;
1112 } else {
1113 general_case:
1114 if (!nocode_wanted) {
1115 /* call low level op generator */
1116 if (t1 == VT_LLONG || t2 == VT_LLONG)
1117 gen_opl(op);
1118 else
1119 gen_opi(op);
1120 } else {
1121 vtop--;
1127 /* generate a floating point operation with constant propagation */
1128 void gen_opif(int op)
1130 int c1, c2;
1131 SValue *v1, *v2;
1132 long double f1, f2;
1134 v1 = vtop - 1;
1135 v2 = vtop;
1136 /* currently, we cannot do computations with forward symbols */
1137 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1138 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1139 if (c1 && c2) {
1140 if (v1->type.t == VT_FLOAT) {
1141 f1 = v1->c.f;
1142 f2 = v2->c.f;
1143 } else if (v1->type.t == VT_DOUBLE) {
1144 f1 = v1->c.d;
1145 f2 = v2->c.d;
1146 } else {
1147 f1 = v1->c.ld;
1148 f2 = v2->c.ld;
1151 /* NOTE: we only do constant propagation if finite number (not
1152 NaN or infinity) (ANSI spec) */
1153 if (!ieee_finite(f1) || !ieee_finite(f2))
1154 goto general_case;
1156 switch(op) {
1157 case '+': f1 += f2; break;
1158 case '-': f1 -= f2; break;
1159 case '*': f1 *= f2; break;
1160 case '/':
1161 if (f2 == 0.0) {
1162 if (const_wanted)
1163 error("division by zero in constant");
1164 goto general_case;
1166 f1 /= f2;
1167 break;
1168 /* XXX: also handles tests ? */
1169 default:
1170 goto general_case;
1172 /* XXX: overflow test ? */
1173 if (v1->type.t == VT_FLOAT) {
1174 v1->c.f = f1;
1175 } else if (v1->type.t == VT_DOUBLE) {
1176 v1->c.d = f1;
1177 } else {
1178 v1->c.ld = f1;
1180 vtop--;
1181 } else {
1182 general_case:
1183 if (!nocode_wanted) {
1184 gen_opf(op);
1185 } else {
1186 vtop--;
1191 static int pointed_size(CType *type)
1193 int align;
1194 return type_size(pointed_type(type), &align);
1197 static inline int is_null_pointer(SValue *p)
1199 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1200 return 0;
1201 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1202 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1205 static inline int is_integer_btype(int bt)
1207 return (bt == VT_BYTE || bt == VT_SHORT ||
1208 bt == VT_INT || bt == VT_LLONG);
1211 /* check types for comparison or substraction of pointers */
1212 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1214 CType *type1, *type2, tmp_type1, tmp_type2;
1215 int bt1, bt2;
1217 /* null pointers are accepted for all comparisons as gcc */
1218 if (is_null_pointer(p1) || is_null_pointer(p2))
1219 return;
1220 type1 = &p1->type;
1221 type2 = &p2->type;
1222 bt1 = type1->t & VT_BTYPE;
1223 bt2 = type2->t & VT_BTYPE;
1224 /* accept comparison between pointer and integer with a warning */
1225 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1226 if (op != TOK_LOR && op != TOK_LAND )
1227 warning("comparison between pointer and integer");
1228 return;
1231 /* both must be pointers or implicit function pointers */
1232 if (bt1 == VT_PTR) {
1233 type1 = pointed_type(type1);
1234 } else if (bt1 != VT_FUNC)
1235 goto invalid_operands;
1237 if (bt2 == VT_PTR) {
1238 type2 = pointed_type(type2);
1239 } else if (bt2 != VT_FUNC) {
1240 invalid_operands:
1241 error("invalid operands to binary %s", get_tok_str(op, NULL));
1243 if ((type1->t & VT_BTYPE) == VT_VOID ||
1244 (type2->t & VT_BTYPE) == VT_VOID)
1245 return;
1246 tmp_type1 = *type1;
1247 tmp_type2 = *type2;
1248 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1249 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1250 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1251 /* gcc-like error if '-' is used */
1252 if (op == '-')
1253 goto invalid_operands;
1254 else
1255 warning("comparison of distinct pointer types lacks a cast");
1259 /* generic gen_op: handles types problems */
1260 void gen_op(int op)
1262 int u, t1, t2, bt1, bt2, t;
1263 CType type1;
1265 t1 = vtop[-1].type.t;
1266 t2 = vtop[0].type.t;
1267 bt1 = t1 & VT_BTYPE;
1268 bt2 = t2 & VT_BTYPE;
1270 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1271 /* at least one operand is a pointer */
1272 /* relationnal op: must be both pointers */
1273 if (op >= TOK_ULT && op <= TOK_LOR) {
1274 check_comparison_pointer_types(vtop - 1, vtop, op);
1275 /* pointers are handled are unsigned */
1276 #ifdef TCC_TARGET_X86_64
1277 t = VT_LLONG | VT_UNSIGNED;
1278 #else
1279 t = VT_INT | VT_UNSIGNED;
1280 #endif
1281 goto std_op;
1283 /* if both pointers, then it must be the '-' op */
1284 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1285 if (op != '-')
1286 error("cannot use pointers here");
1287 check_comparison_pointer_types(vtop - 1, vtop, op);
1288 /* XXX: check that types are compatible */
1289 u = pointed_size(&vtop[-1].type);
1290 gen_opic(op);
1291 /* set to integer type */
1292 #ifdef TCC_TARGET_X86_64
1293 vtop->type.t = VT_LLONG;
1294 #else
1295 vtop->type.t = VT_INT;
1296 #endif
1297 vpushi(u);
1298 gen_op(TOK_PDIV);
1299 } else {
1300 /* exactly one pointer : must be '+' or '-'. */
1301 if (op != '-' && op != '+')
1302 error("cannot use pointers here");
1303 /* Put pointer as first operand */
1304 if (bt2 == VT_PTR) {
1305 vswap();
1306 swap(&t1, &t2);
1308 type1 = vtop[-1].type;
1309 type1.t &= ~VT_ARRAY;
1310 #ifdef TCC_TARGET_X86_64
1311 vpushll(pointed_size(&vtop[-1].type));
1312 #else
1313 /* XXX: cast to int ? (long long case) */
1314 vpushi(pointed_size(&vtop[-1].type));
1315 #endif
1316 gen_op('*');
1317 #ifdef CONFIG_TCC_BCHECK
1318 /* if evaluating constant expression, no code should be
1319 generated, so no bound check */
1320 if (tcc_state->do_bounds_check && !const_wanted) {
1321 /* if bounded pointers, we generate a special code to
1322 test bounds */
1323 if (op == '-') {
1324 vpushi(0);
1325 vswap();
1326 gen_op('-');
1328 gen_bounded_ptr_add();
1329 } else
1330 #endif
1332 gen_opic(op);
1334 /* put again type if gen_opic() swaped operands */
1335 vtop->type = type1;
1337 } else if (is_float(bt1) || is_float(bt2)) {
1338 /* compute bigger type and do implicit casts */
1339 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1340 t = VT_LDOUBLE;
1341 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1342 t = VT_DOUBLE;
1343 } else {
1344 t = VT_FLOAT;
1346 /* floats can only be used for a few operations */
1347 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1348 (op < TOK_ULT || op > TOK_GT))
1349 error("invalid operands for binary operation");
1350 goto std_op;
1351 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1352 /* cast to biggest op */
1353 t = VT_LLONG;
1354 /* convert to unsigned if it does not fit in a long long */
1355 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1356 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1357 t |= VT_UNSIGNED;
1358 goto std_op;
1359 } else {
1360 /* integer operations */
1361 t = VT_INT;
1362 /* convert to unsigned if it does not fit in an integer */
1363 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1364 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1365 t |= VT_UNSIGNED;
1366 std_op:
1367 /* XXX: currently, some unsigned operations are explicit, so
1368 we modify them here */
1369 if (t & VT_UNSIGNED) {
1370 if (op == TOK_SAR)
1371 op = TOK_SHR;
1372 else if (op == '/')
1373 op = TOK_UDIV;
1374 else if (op == '%')
1375 op = TOK_UMOD;
1376 else if (op == TOK_LT)
1377 op = TOK_ULT;
1378 else if (op == TOK_GT)
1379 op = TOK_UGT;
1380 else if (op == TOK_LE)
1381 op = TOK_ULE;
1382 else if (op == TOK_GE)
1383 op = TOK_UGE;
1385 vswap();
1386 type1.t = t;
1387 gen_cast(&type1);
1388 vswap();
1389 /* special case for shifts and long long: we keep the shift as
1390 an integer */
1391 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1392 type1.t = VT_INT;
1393 gen_cast(&type1);
1394 if (is_float(t))
1395 gen_opif(op);
1396 else
1397 gen_opic(op);
1398 if (op >= TOK_ULT && op <= TOK_GT) {
1399 /* relationnal op: the result is an int */
1400 vtop->type.t = VT_INT;
1401 } else {
1402 vtop->type.t = t;
1407 #ifndef TCC_TARGET_ARM
1408 /* generic itof for unsigned long long case */
1409 void gen_cvt_itof1(int t)
1411 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1412 (VT_LLONG | VT_UNSIGNED)) {
1414 if (t == VT_FLOAT)
1415 vpush_global_sym(&func_old_type, TOK___floatundisf);
1416 #if LDOUBLE_SIZE != 8
1417 else if (t == VT_LDOUBLE)
1418 vpush_global_sym(&func_old_type, TOK___floatundixf);
1419 #endif
1420 else
1421 vpush_global_sym(&func_old_type, TOK___floatundidf);
1422 vrott(2);
1423 gfunc_call(1);
1424 vpushi(0);
1425 vtop->r = reg_fret(t);
1426 } else {
1427 gen_cvt_itof(t);
1430 #endif
1432 /* generic ftoi for unsigned long long case */
1433 void gen_cvt_ftoi1(int t)
1435 int st;
1437 if (t == (VT_LLONG | VT_UNSIGNED)) {
1438 /* not handled natively */
1439 st = vtop->type.t & VT_BTYPE;
1440 if (st == VT_FLOAT)
1441 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1442 #if LDOUBLE_SIZE != 8
1443 else if (st == VT_LDOUBLE)
1444 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1445 #endif
1446 else
1447 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1448 vrott(2);
1449 gfunc_call(1);
1450 vpushi(0);
1451 vtop->r = REG_IRET;
1452 vtop->r2 = REG_LRET;
1453 } else {
1454 gen_cvt_ftoi(t);
1458 /* force char or short cast */
1459 void force_charshort_cast(int t)
1461 int bits, dbt;
1462 dbt = t & VT_BTYPE;
1463 /* XXX: add optimization if lvalue : just change type and offset */
1464 if (dbt == VT_BYTE)
1465 bits = 8;
1466 else
1467 bits = 16;
1468 if (t & VT_UNSIGNED) {
1469 vpushi((1 << bits) - 1);
1470 gen_op('&');
1471 } else {
1472 bits = 32 - bits;
1473 vpushi(bits);
1474 gen_op(TOK_SHL);
1475 /* result must be signed or the SAR is converted to an SHL
1476 This was not the case when "t" was a signed short
1477 and the last value on the stack was an unsigned int */
1478 vtop->type.t &= ~VT_UNSIGNED;
1479 vpushi(bits);
1480 gen_op(TOK_SAR);
1484 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1485 static void gen_cast(CType *type)
1487 int sbt, dbt, sf, df, c, p;
1489 /* special delayed cast for char/short */
1490 /* XXX: in some cases (multiple cascaded casts), it may still
1491 be incorrect */
1492 if (vtop->r & VT_MUSTCAST) {
1493 vtop->r &= ~VT_MUSTCAST;
1494 force_charshort_cast(vtop->type.t);
1497 /* bitfields first get cast to ints */
1498 if (vtop->type.t & VT_BITFIELD) {
1499 gv(RC_INT);
1502 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1503 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1505 if (sbt != dbt) {
1506 sf = is_float(sbt);
1507 df = is_float(dbt);
1508 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1509 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1510 if (c) {
1511 /* constant case: we can do it now */
1512 /* XXX: in ISOC, cannot do it if error in convert */
1513 if (sbt == VT_FLOAT)
1514 vtop->c.ld = vtop->c.f;
1515 else if (sbt == VT_DOUBLE)
1516 vtop->c.ld = vtop->c.d;
1518 if (df) {
1519 if ((sbt & VT_BTYPE) == VT_LLONG) {
1520 if (sbt & VT_UNSIGNED)
1521 vtop->c.ld = vtop->c.ull;
1522 else
1523 vtop->c.ld = vtop->c.ll;
1524 } else if(!sf) {
1525 if (sbt & VT_UNSIGNED)
1526 vtop->c.ld = vtop->c.ui;
1527 else
1528 vtop->c.ld = vtop->c.i;
1531 if (dbt == VT_FLOAT)
1532 vtop->c.f = (float)vtop->c.ld;
1533 else if (dbt == VT_DOUBLE)
1534 vtop->c.d = (double)vtop->c.ld;
1535 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1536 vtop->c.ull = (unsigned long long)vtop->c.ld;
1537 } else if (sf && dbt == VT_BOOL) {
1538 vtop->c.i = (vtop->c.ld != 0);
1539 } else {
1540 if(sf)
1541 vtop->c.ll = (long long)vtop->c.ld;
1542 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1543 vtop->c.ll = vtop->c.ull;
1544 else if (sbt & VT_UNSIGNED)
1545 vtop->c.ll = vtop->c.ui;
1546 else if (sbt != VT_LLONG)
1547 vtop->c.ll = vtop->c.i;
1549 if (dbt == (VT_LLONG|VT_UNSIGNED))
1550 vtop->c.ull = vtop->c.ll;
1551 else if (dbt == VT_BOOL)
1552 vtop->c.i = (vtop->c.ll != 0);
1553 else if (dbt != VT_LLONG) {
1554 int s = 0;
1555 if ((dbt & VT_BTYPE) == VT_BYTE)
1556 s = 24;
1557 else if ((dbt & VT_BTYPE) == VT_SHORT)
1558 s = 16;
1560 if(dbt & VT_UNSIGNED)
1561 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1562 else
1563 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1566 } else if (p && dbt == VT_BOOL) {
1567 vtop->r = VT_CONST;
1568 vtop->c.i = 1;
1569 } else if (!nocode_wanted) {
1570 /* non constant case: generate code */
1571 if (sf && df) {
1572 /* convert from fp to fp */
1573 gen_cvt_ftof(dbt);
1574 } else if (df) {
1575 /* convert int to fp */
1576 gen_cvt_itof1(dbt);
1577 } else if (sf) {
1578 /* convert fp to int */
1579 if (dbt == VT_BOOL) {
1580 vpushi(0);
1581 gen_op(TOK_NE);
1582 } else {
1583 /* we handle char/short/etc... with generic code */
1584 if (dbt != (VT_INT | VT_UNSIGNED) &&
1585 dbt != (VT_LLONG | VT_UNSIGNED) &&
1586 dbt != VT_LLONG)
1587 dbt = VT_INT;
1588 gen_cvt_ftoi1(dbt);
1589 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1590 /* additional cast for char/short... */
1591 vtop->type.t = dbt;
1592 gen_cast(type);
1595 #ifndef TCC_TARGET_X86_64
1596 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1597 if ((sbt & VT_BTYPE) != VT_LLONG) {
1598 /* scalar to long long */
1599 /* machine independent conversion */
1600 gv(RC_INT);
1601 /* generate high word */
1602 if (sbt == (VT_INT | VT_UNSIGNED)) {
1603 vpushi(0);
1604 gv(RC_INT);
1605 } else {
1606 if (sbt == VT_PTR) {
1607 /* cast from pointer to int before we apply
1608 shift operation, which pointers don't support*/
1609 gen_cast(&int_type);
1611 gv_dup();
1612 vpushi(31);
1613 gen_op(TOK_SAR);
1615 /* patch second register */
1616 vtop[-1].r2 = vtop->r;
1617 vpop();
1619 #else
1620 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1621 (dbt & VT_BTYPE) == VT_PTR) {
1622 /* XXX: not sure if this is perfect... need more tests */
1623 if ((sbt & VT_BTYPE) != VT_LLONG) {
1624 int r = gv(RC_INT);
1625 if (sbt != (VT_INT | VT_UNSIGNED) &&
1626 sbt != VT_PTR && sbt != VT_FUNC) {
1627 /* x86_64 specific: movslq */
1628 o(0x6348);
1629 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1632 #endif
1633 } else if (dbt == VT_BOOL) {
1634 /* scalar to bool */
1635 vpushi(0);
1636 gen_op(TOK_NE);
1637 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1638 (dbt & VT_BTYPE) == VT_SHORT) {
1639 if (sbt == VT_PTR) {
1640 vtop->type.t = VT_INT;
1641 warning("nonportable conversion from pointer to char/short");
1643 force_charshort_cast(dbt);
1644 } else if ((dbt & VT_BTYPE) == VT_INT) {
1645 /* scalar to int */
1646 if (sbt == VT_LLONG) {
1647 /* from long long: just take low order word */
1648 lexpand();
1649 vpop();
1651 /* if lvalue and single word type, nothing to do because
1652 the lvalue already contains the real type size (see
1653 VT_LVAL_xxx constants) */
1656 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1657 /* if we are casting between pointer types,
1658 we must update the VT_LVAL_xxx size */
1659 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1660 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1662 vtop->type = *type;
1665 /* return type size. Put alignment at 'a' */
1666 static int type_size(CType *type, int *a)
1668 Sym *s;
1669 int bt;
1671 bt = type->t & VT_BTYPE;
1672 if (bt == VT_STRUCT) {
1673 /* struct/union */
1674 s = type->ref;
1675 *a = s->r;
1676 return s->c;
1677 } else if (bt == VT_PTR) {
1678 if (type->t & VT_ARRAY) {
1679 int ts;
1681 s = type->ref;
1682 ts = type_size(&s->type, a);
1684 if (ts < 0 && s->c < 0)
1685 ts = -ts;
1687 return ts * s->c;
1688 } else {
1689 *a = PTR_SIZE;
1690 return PTR_SIZE;
1692 } else if (bt == VT_LDOUBLE) {
1693 *a = LDOUBLE_ALIGN;
1694 return LDOUBLE_SIZE;
1695 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1696 #ifdef TCC_TARGET_I386
1697 #ifdef TCC_TARGET_PE
1698 *a = 8;
1699 #else
1700 *a = 4;
1701 #endif
1702 #elif defined(TCC_TARGET_ARM)
1703 #ifdef TCC_ARM_EABI
1704 *a = 8;
1705 #else
1706 *a = 4;
1707 #endif
1708 #else
1709 *a = 8;
1710 #endif
1711 return 8;
1712 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1713 *a = 4;
1714 return 4;
1715 } else if (bt == VT_SHORT) {
1716 *a = 2;
1717 return 2;
1718 } else {
1719 /* char, void, function, _Bool */
1720 *a = 1;
1721 return 1;
1725 /* return the pointed type of t */
1726 static inline CType *pointed_type(CType *type)
1728 return &type->ref->type;
1731 /* modify type so that its it is a pointer to type. */
1732 static void mk_pointer(CType *type)
1734 Sym *s;
1735 s = sym_push(SYM_FIELD, type, 0, -1);
1736 type->t = VT_PTR | (type->t & ~VT_TYPE);
1737 type->ref = s;
1740 /* compare function types. OLD functions match any new functions */
1741 static int is_compatible_func(CType *type1, CType *type2)
1743 Sym *s1, *s2;
1745 s1 = type1->ref;
1746 s2 = type2->ref;
1747 if (!is_compatible_types(&s1->type, &s2->type))
1748 return 0;
1749 /* check func_call */
1750 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
1751 return 0;
1752 /* XXX: not complete */
1753 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
1754 return 1;
1755 if (s1->c != s2->c)
1756 return 0;
1757 while (s1 != NULL) {
1758 if (s2 == NULL)
1759 return 0;
1760 if (!is_compatible_parameter_types(&s1->type, &s2->type))
1761 return 0;
1762 s1 = s1->next;
1763 s2 = s2->next;
1765 if (s2)
1766 return 0;
1767 return 1;
1770 /* return true if type1 and type2 are the same. If unqualified is
1771 true, qualifiers on the types are ignored.
1773 - enums are not checked as gcc __builtin_types_compatible_p ()
1775 static int compare_types(CType *type1, CType *type2, int unqualified)
1777 int bt1, t1, t2;
1779 t1 = type1->t & VT_TYPE;
1780 t2 = type2->t & VT_TYPE;
1781 if (unqualified) {
1782 /* strip qualifiers before comparing */
1783 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
1784 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
1786 /* XXX: bitfields ? */
1787 if (t1 != t2)
1788 return 0;
1789 /* test more complicated cases */
1790 bt1 = t1 & VT_BTYPE;
1791 if (bt1 == VT_PTR) {
1792 type1 = pointed_type(type1);
1793 type2 = pointed_type(type2);
1794 return is_compatible_types(type1, type2);
1795 } else if (bt1 == VT_STRUCT) {
1796 return (type1->ref == type2->ref);
1797 } else if (bt1 == VT_FUNC) {
1798 return is_compatible_func(type1, type2);
1799 } else {
1800 return 1;
1804 /* return true if type1 and type2 are exactly the same (including
1805 qualifiers).
1807 static int is_compatible_types(CType *type1, CType *type2)
1809 return compare_types(type1,type2,0);
1812 /* return true if type1 and type2 are the same (ignoring qualifiers).
1814 static int is_compatible_parameter_types(CType *type1, CType *type2)
1816 return compare_types(type1,type2,1);
1819 /* print a type. If 'varstr' is not NULL, then the variable is also
1820 printed in the type */
1821 /* XXX: union */
1822 /* XXX: add array and function pointers */
1823 void type_to_str(char *buf, int buf_size,
1824 CType *type, const char *varstr)
1826 int bt, v, t;
1827 Sym *s, *sa;
1828 char buf1[256];
1829 const char *tstr;
1831 t = type->t & VT_TYPE;
1832 bt = t & VT_BTYPE;
1833 buf[0] = '\0';
1834 if (t & VT_CONSTANT)
1835 pstrcat(buf, buf_size, "const ");
1836 if (t & VT_VOLATILE)
1837 pstrcat(buf, buf_size, "volatile ");
1838 if (t & VT_UNSIGNED)
1839 pstrcat(buf, buf_size, "unsigned ");
1840 switch(bt) {
1841 case VT_VOID:
1842 tstr = "void";
1843 goto add_tstr;
1844 case VT_BOOL:
1845 tstr = "_Bool";
1846 goto add_tstr;
1847 case VT_BYTE:
1848 tstr = "char";
1849 goto add_tstr;
1850 case VT_SHORT:
1851 tstr = "short";
1852 goto add_tstr;
1853 case VT_INT:
1854 tstr = "int";
1855 goto add_tstr;
1856 case VT_LONG:
1857 tstr = "long";
1858 goto add_tstr;
1859 case VT_LLONG:
1860 tstr = "long long";
1861 goto add_tstr;
1862 case VT_FLOAT:
1863 tstr = "float";
1864 goto add_tstr;
1865 case VT_DOUBLE:
1866 tstr = "double";
1867 goto add_tstr;
1868 case VT_LDOUBLE:
1869 tstr = "long double";
1870 add_tstr:
1871 pstrcat(buf, buf_size, tstr);
1872 break;
1873 case VT_ENUM:
1874 case VT_STRUCT:
1875 if (bt == VT_STRUCT)
1876 tstr = "struct ";
1877 else
1878 tstr = "enum ";
1879 pstrcat(buf, buf_size, tstr);
1880 v = type->ref->v & ~SYM_STRUCT;
1881 if (v >= SYM_FIRST_ANOM)
1882 pstrcat(buf, buf_size, "<anonymous>");
1883 else
1884 pstrcat(buf, buf_size, get_tok_str(v, NULL));
1885 break;
1886 case VT_FUNC:
1887 s = type->ref;
1888 type_to_str(buf, buf_size, &s->type, varstr);
1889 pstrcat(buf, buf_size, "(");
1890 sa = s->next;
1891 while (sa != NULL) {
1892 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
1893 pstrcat(buf, buf_size, buf1);
1894 sa = sa->next;
1895 if (sa)
1896 pstrcat(buf, buf_size, ", ");
1898 pstrcat(buf, buf_size, ")");
1899 goto no_var;
1900 case VT_PTR:
1901 s = type->ref;
1902 pstrcpy(buf1, sizeof(buf1), "*");
1903 if (varstr)
1904 pstrcat(buf1, sizeof(buf1), varstr);
1905 type_to_str(buf, buf_size, &s->type, buf1);
1906 goto no_var;
1908 if (varstr) {
1909 pstrcat(buf, buf_size, " ");
1910 pstrcat(buf, buf_size, varstr);
1912 no_var: ;
1915 /* verify type compatibility to store vtop in 'dt' type, and generate
1916 casts if needed. */
1917 static void gen_assign_cast(CType *dt)
1919 CType *st, *type1, *type2, tmp_type1, tmp_type2;
1920 char buf1[256], buf2[256];
1921 int dbt, sbt;
1923 st = &vtop->type; /* source type */
1924 dbt = dt->t & VT_BTYPE;
1925 sbt = st->t & VT_BTYPE;
1926 if (dt->t & VT_CONSTANT)
1927 warning("assignment of read-only location");
1928 switch(dbt) {
1929 case VT_PTR:
1930 /* special cases for pointers */
1931 /* '0' can also be a pointer */
1932 if (is_null_pointer(vtop))
1933 goto type_ok;
1934 /* accept implicit pointer to integer cast with warning */
1935 if (is_integer_btype(sbt)) {
1936 warning("assignment makes pointer from integer without a cast");
1937 goto type_ok;
1939 type1 = pointed_type(dt);
1940 /* a function is implicitely a function pointer */
1941 if (sbt == VT_FUNC) {
1942 if ((type1->t & VT_BTYPE) != VT_VOID &&
1943 !is_compatible_types(pointed_type(dt), st))
1944 warning("assignment from incompatible pointer type");
1945 goto type_ok;
1947 if (sbt != VT_PTR)
1948 goto error;
1949 type2 = pointed_type(st);
1950 if ((type1->t & VT_BTYPE) == VT_VOID ||
1951 (type2->t & VT_BTYPE) == VT_VOID) {
1952 /* void * can match anything */
1953 } else {
1954 /* exact type match, except for unsigned */
1955 tmp_type1 = *type1;
1956 tmp_type2 = *type2;
1957 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1958 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1959 if (!is_compatible_types(&tmp_type1, &tmp_type2))
1960 warning("assignment from incompatible pointer type");
1962 /* check const and volatile */
1963 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
1964 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
1965 warning("assignment discards qualifiers from pointer target type");
1966 break;
1967 case VT_BYTE:
1968 case VT_SHORT:
1969 case VT_INT:
1970 case VT_LLONG:
1971 if (sbt == VT_PTR || sbt == VT_FUNC) {
1972 warning("assignment makes integer from pointer without a cast");
1974 /* XXX: more tests */
1975 break;
1976 case VT_STRUCT:
1977 tmp_type1 = *dt;
1978 tmp_type2 = *st;
1979 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
1980 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
1981 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1982 error:
1983 type_to_str(buf1, sizeof(buf1), st, NULL);
1984 type_to_str(buf2, sizeof(buf2), dt, NULL);
1985 error("cannot cast '%s' to '%s'", buf1, buf2);
1987 break;
1989 type_ok:
1990 gen_cast(dt);
1993 /* store vtop in lvalue pushed on stack */
1994 void vstore(void)
1996 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
1998 ft = vtop[-1].type.t;
1999 sbt = vtop->type.t & VT_BTYPE;
2000 dbt = ft & VT_BTYPE;
2001 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2002 (sbt == VT_INT && dbt == VT_SHORT)) {
2003 /* optimize char/short casts */
2004 delayed_cast = VT_MUSTCAST;
2005 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2006 /* XXX: factorize */
2007 if (ft & VT_CONSTANT)
2008 warning("assignment of read-only location");
2009 } else {
2010 delayed_cast = 0;
2011 if (!(ft & VT_BITFIELD))
2012 gen_assign_cast(&vtop[-1].type);
2015 if (sbt == VT_STRUCT) {
2016 /* if structure, only generate pointer */
2017 /* structure assignment : generate memcpy */
2018 /* XXX: optimize if small size */
2019 if (!nocode_wanted) {
2020 size = type_size(&vtop->type, &align);
2022 #ifdef TCC_ARM_EABI
2023 if(!(align & 7))
2024 vpush_global_sym(&func_old_type, TOK_memcpy8);
2025 else if(!(align & 3))
2026 vpush_global_sym(&func_old_type, TOK_memcpy4);
2027 else
2028 #endif
2029 vpush_global_sym(&func_old_type, TOK_memcpy);
2031 /* destination */
2032 vpushv(vtop - 2);
2033 vtop->type.t = VT_PTR;
2034 gaddrof();
2035 /* source */
2036 vpushv(vtop - 2);
2037 vtop->type.t = VT_PTR;
2038 gaddrof();
2039 /* type size */
2040 vpushi(size);
2041 gfunc_call(3);
2043 vswap();
2044 vpop();
2045 } else {
2046 vswap();
2047 vpop();
2049 /* leave source on stack */
2050 } else if (ft & VT_BITFIELD) {
2051 /* bitfield store handling */
2052 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2053 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2054 /* remove bit field info to avoid loops */
2055 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2057 /* duplicate source into other register */
2058 gv_dup();
2059 vswap();
2060 vrott(3);
2062 if((ft & VT_BTYPE) == VT_BOOL) {
2063 gen_cast(&vtop[-1].type);
2064 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2067 /* duplicate destination */
2068 vdup();
2069 vtop[-1] = vtop[-2];
2071 /* mask and shift source */
2072 if((ft & VT_BTYPE) != VT_BOOL) {
2073 if((ft & VT_BTYPE) == VT_LLONG) {
2074 vpushll((1ULL << bit_size) - 1ULL);
2075 } else {
2076 vpushi((1 << bit_size) - 1);
2078 gen_op('&');
2080 vpushi(bit_pos);
2081 gen_op(TOK_SHL);
2082 /* load destination, mask and or with source */
2083 vswap();
2084 if((ft & VT_BTYPE) == VT_LLONG) {
2085 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2086 } else {
2087 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2089 gen_op('&');
2090 gen_op('|');
2091 /* store result */
2092 vstore();
2094 /* pop off shifted source from "duplicate source..." above */
2095 vpop();
2097 } else {
2098 #ifdef CONFIG_TCC_BCHECK
2099 /* bound check case */
2100 if (vtop[-1].r & VT_MUSTBOUND) {
2101 vswap();
2102 gbound();
2103 vswap();
2105 #endif
2106 if (!nocode_wanted) {
2107 rc = RC_INT;
2108 if (is_float(ft)) {
2109 rc = RC_FLOAT;
2110 #ifdef TCC_TARGET_X86_64
2111 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2112 rc = RC_ST0;
2114 #endif
2116 r = gv(rc); /* generate value */
2117 /* if lvalue was saved on stack, must read it */
2118 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2119 SValue sv;
2120 t = get_reg(RC_INT);
2121 #ifdef TCC_TARGET_X86_64
2122 sv.type.t = VT_PTR;
2123 #else
2124 sv.type.t = VT_INT;
2125 #endif
2126 sv.r = VT_LOCAL | VT_LVAL;
2127 sv.c.ul = vtop[-1].c.ul;
2128 load(t, &sv);
2129 vtop[-1].r = t | VT_LVAL;
2131 store(r, vtop - 1);
2132 #ifndef TCC_TARGET_X86_64
2133 /* two word case handling : store second register at word + 4 */
2134 if ((ft & VT_BTYPE) == VT_LLONG) {
2135 vswap();
2136 /* convert to int to increment easily */
2137 vtop->type.t = VT_INT;
2138 gaddrof();
2139 vpushi(4);
2140 gen_op('+');
2141 vtop->r |= VT_LVAL;
2142 vswap();
2143 /* XXX: it works because r2 is spilled last ! */
2144 store(vtop->r2, vtop - 1);
2146 #endif
2148 vswap();
2149 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2150 vtop->r |= delayed_cast;
2154 /* post defines POST/PRE add. c is the token ++ or -- */
2155 void inc(int post, int c)
2157 test_lvalue();
2158 vdup(); /* save lvalue */
2159 if (post) {
2160 gv_dup(); /* duplicate value */
2161 vrotb(3);
2162 vrotb(3);
2164 /* add constant */
2165 vpushi(c - TOK_MID);
2166 gen_op('+');
2167 vstore(); /* store value */
2168 if (post)
2169 vpop(); /* if post op, return saved value */
2172 /* Parse GNUC __attribute__ extension. Currently, the following
2173 extensions are recognized:
2174 - aligned(n) : set data/function alignment.
2175 - packed : force data alignment to 1
2176 - section(x) : generate data/code in this section.
2177 - unused : currently ignored, but may be used someday.
2178 - regparm(n) : pass function parameters in registers (i386 only)
2180 static void parse_attribute(AttributeDef *ad)
2182 int t, n;
2184 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2185 next();
2186 skip('(');
2187 skip('(');
2188 while (tok != ')') {
2189 if (tok < TOK_IDENT)
2190 expect("attribute name");
2191 t = tok;
2192 next();
2193 switch(t) {
2194 case TOK_SECTION1:
2195 case TOK_SECTION2:
2196 skip('(');
2197 if (tok != TOK_STR)
2198 expect("section name");
2199 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2200 next();
2201 skip(')');
2202 break;
2203 case TOK_ALIGNED1:
2204 case TOK_ALIGNED2:
2205 if (tok == '(') {
2206 next();
2207 n = expr_const();
2208 if (n <= 0 || (n & (n - 1)) != 0)
2209 error("alignment must be a positive power of two");
2210 skip(')');
2211 } else {
2212 n = MAX_ALIGN;
2214 ad->aligned = n;
2215 break;
2216 case TOK_PACKED1:
2217 case TOK_PACKED2:
2218 ad->packed = 1;
2219 break;
2220 case TOK_UNUSED1:
2221 case TOK_UNUSED2:
2222 /* currently, no need to handle it because tcc does not
2223 track unused objects */
2224 break;
2225 case TOK_NORETURN1:
2226 case TOK_NORETURN2:
2227 /* currently, no need to handle it because tcc does not
2228 track unused objects */
2229 break;
2230 case TOK_CDECL1:
2231 case TOK_CDECL2:
2232 case TOK_CDECL3:
2233 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
2234 break;
2235 case TOK_STDCALL1:
2236 case TOK_STDCALL2:
2237 case TOK_STDCALL3:
2238 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
2239 break;
2240 #ifdef TCC_TARGET_I386
2241 case TOK_REGPARM1:
2242 case TOK_REGPARM2:
2243 skip('(');
2244 n = expr_const();
2245 if (n > 3)
2246 n = 3;
2247 else if (n < 0)
2248 n = 0;
2249 if (n > 0)
2250 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
2251 skip(')');
2252 break;
2253 case TOK_FASTCALL1:
2254 case TOK_FASTCALL2:
2255 case TOK_FASTCALL3:
2256 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
2257 break;
2258 #endif
2259 case TOK_DLLEXPORT:
2260 FUNC_EXPORT(ad->func_attr) = 1;
2261 break;
2262 case TOK_DLLIMPORT:
2263 FUNC_IMPORT(ad->func_attr) = 1;
2264 break;
2265 default:
2266 if (tcc_state->warn_unsupported)
2267 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2268 /* skip parameters */
2269 if (tok == '(') {
2270 int parenthesis = 0;
2271 do {
2272 if (tok == '(')
2273 parenthesis++;
2274 else if (tok == ')')
2275 parenthesis--;
2276 next();
2277 } while (parenthesis && tok != -1);
2279 break;
2281 if (tok != ',')
2282 break;
2283 next();
2285 skip(')');
2286 skip(')');
2290 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2291 static void struct_decl(CType *type, int u)
2293 int a, v, size, align, maxalign, c, offset;
2294 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2295 Sym *s, *ss, *ass, **ps;
2296 AttributeDef ad;
2297 CType type1, btype;
2299 a = tok; /* save decl type */
2300 next();
2301 if (tok != '{') {
2302 v = tok;
2303 next();
2304 /* struct already defined ? return it */
2305 if (v < TOK_IDENT)
2306 expect("struct/union/enum name");
2307 s = struct_find(v);
2308 if (s) {
2309 if (s->type.t != a)
2310 error("invalid type");
2311 goto do_decl;
2313 } else {
2314 v = anon_sym++;
2316 type1.t = a;
2317 /* we put an undefined size for struct/union */
2318 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2319 s->r = 0; /* default alignment is zero as gcc */
2320 /* put struct/union/enum name in type */
2321 do_decl:
2322 type->t = u;
2323 type->ref = s;
2325 if (tok == '{') {
2326 next();
2327 if (s->c != -1)
2328 error("struct/union/enum already defined");
2329 /* cannot be empty */
2330 c = 0;
2331 /* non empty enums are not allowed */
2332 if (a == TOK_ENUM) {
2333 for(;;) {
2334 v = tok;
2335 if (v < TOK_UIDENT)
2336 expect("identifier");
2337 next();
2338 if (tok == '=') {
2339 next();
2340 c = expr_const();
2342 /* enum symbols have static storage */
2343 ss = sym_push(v, &int_type, VT_CONST, c);
2344 ss->type.t |= VT_STATIC;
2345 if (tok != ',')
2346 break;
2347 next();
2348 c++;
2349 /* NOTE: we accept a trailing comma */
2350 if (tok == '}')
2351 break;
2353 skip('}');
2354 } else {
2355 maxalign = 1;
2356 ps = &s->next;
2357 prevbt = VT_INT;
2358 bit_pos = 0;
2359 offset = 0;
2360 while (tok != '}') {
2361 parse_btype(&btype, &ad);
2362 while (1) {
2363 bit_size = -1;
2364 v = 0;
2365 type1 = btype;
2366 if (tok != ':') {
2367 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2368 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2369 expect("identifier");
2370 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2371 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2372 error("invalid type for '%s'",
2373 get_tok_str(v, NULL));
2375 if (tok == ':') {
2376 next();
2377 bit_size = expr_const();
2378 /* XXX: handle v = 0 case for messages */
2379 if (bit_size < 0)
2380 error("negative width in bit-field '%s'",
2381 get_tok_str(v, NULL));
2382 if (v && bit_size == 0)
2383 error("zero width for bit-field '%s'",
2384 get_tok_str(v, NULL));
2386 size = type_size(&type1, &align);
2387 if (ad.aligned) {
2388 if (align < ad.aligned)
2389 align = ad.aligned;
2390 } else if (ad.packed) {
2391 align = 1;
2392 } else if (*tcc_state->pack_stack_ptr) {
2393 if (align > *tcc_state->pack_stack_ptr)
2394 align = *tcc_state->pack_stack_ptr;
2396 lbit_pos = 0;
2397 if (bit_size >= 0) {
2398 bt = type1.t & VT_BTYPE;
2399 if (bt != VT_INT &&
2400 bt != VT_BYTE &&
2401 bt != VT_SHORT &&
2402 bt != VT_BOOL &&
2403 bt != VT_ENUM &&
2404 bt != VT_LLONG)
2405 error("bitfields must have scalar type");
2406 bsize = size * 8;
2407 if (bit_size > bsize) {
2408 error("width of '%s' exceeds its type",
2409 get_tok_str(v, NULL));
2410 } else if (bit_size == bsize) {
2411 /* no need for bit fields */
2412 bit_pos = 0;
2413 } else if (bit_size == 0) {
2414 /* XXX: what to do if only padding in a
2415 structure ? */
2416 /* zero size: means to pad */
2417 bit_pos = 0;
2418 } else {
2419 /* we do not have enough room ?
2420 did the type change?
2421 is it a union? */
2422 if ((bit_pos + bit_size) > bsize ||
2423 bt != prevbt || a == TOK_UNION)
2424 bit_pos = 0;
2425 lbit_pos = bit_pos;
2426 /* XXX: handle LSB first */
2427 type1.t |= VT_BITFIELD |
2428 (bit_pos << VT_STRUCT_SHIFT) |
2429 (bit_size << (VT_STRUCT_SHIFT + 6));
2430 bit_pos += bit_size;
2432 prevbt = bt;
2433 } else {
2434 bit_pos = 0;
2436 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2437 /* add new memory data only if starting
2438 bit field */
2439 if (lbit_pos == 0) {
2440 if (a == TOK_STRUCT) {
2441 c = (c + align - 1) & -align;
2442 offset = c;
2443 if (size > 0)
2444 c += size;
2445 } else {
2446 offset = 0;
2447 if (size > c)
2448 c = size;
2450 if (align > maxalign)
2451 maxalign = align;
2453 #if 0
2454 printf("add field %s offset=%d",
2455 get_tok_str(v, NULL), offset);
2456 if (type1.t & VT_BITFIELD) {
2457 printf(" pos=%d size=%d",
2458 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2459 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2461 printf("\n");
2462 #endif
2464 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2465 ass = type1.ref;
2466 while ((ass = ass->next) != NULL) {
2467 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2468 *ps = ss;
2469 ps = &ss->next;
2471 } else if (v) {
2472 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2473 *ps = ss;
2474 ps = &ss->next;
2476 if (tok == ';' || tok == TOK_EOF)
2477 break;
2478 skip(',');
2480 skip(';');
2482 skip('}');
2483 /* store size and alignment */
2484 s->c = (c + maxalign - 1) & -maxalign;
2485 s->r = maxalign;
2490 /* return 0 if no type declaration. otherwise, return the basic type
2491 and skip it.
2493 static int parse_btype(CType *type, AttributeDef *ad)
2495 int t, u, type_found, typespec_found, typedef_found;
2496 Sym *s;
2497 CType type1;
2499 memset(ad, 0, sizeof(AttributeDef));
2500 type_found = 0;
2501 typespec_found = 0;
2502 typedef_found = 0;
2503 t = 0;
2504 while(1) {
2505 switch(tok) {
2506 case TOK_EXTENSION:
2507 /* currently, we really ignore extension */
2508 next();
2509 continue;
2511 /* basic types */
2512 case TOK_CHAR:
2513 u = VT_BYTE;
2514 basic_type:
2515 next();
2516 basic_type1:
2517 if ((t & VT_BTYPE) != 0)
2518 error("too many basic types");
2519 t |= u;
2520 typespec_found = 1;
2521 break;
2522 case TOK_VOID:
2523 u = VT_VOID;
2524 goto basic_type;
2525 case TOK_SHORT:
2526 u = VT_SHORT;
2527 goto basic_type;
2528 case TOK_INT:
2529 next();
2530 typespec_found = 1;
2531 break;
2532 case TOK_LONG:
2533 next();
2534 if ((t & VT_BTYPE) == VT_DOUBLE) {
2535 #ifndef TCC_TARGET_PE
2536 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2537 #endif
2538 } else if ((t & VT_BTYPE) == VT_LONG) {
2539 t = (t & ~VT_BTYPE) | VT_LLONG;
2540 } else {
2541 u = VT_LONG;
2542 goto basic_type1;
2544 break;
2545 case TOK_BOOL:
2546 u = VT_BOOL;
2547 goto basic_type;
2548 case TOK_FLOAT:
2549 u = VT_FLOAT;
2550 goto basic_type;
2551 case TOK_DOUBLE:
2552 next();
2553 if ((t & VT_BTYPE) == VT_LONG) {
2554 #ifdef TCC_TARGET_PE
2555 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2556 #else
2557 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2558 #endif
2559 } else {
2560 u = VT_DOUBLE;
2561 goto basic_type1;
2563 break;
2564 case TOK_ENUM:
2565 struct_decl(&type1, VT_ENUM);
2566 basic_type2:
2567 u = type1.t;
2568 type->ref = type1.ref;
2569 goto basic_type1;
2570 case TOK_STRUCT:
2571 case TOK_UNION:
2572 struct_decl(&type1, VT_STRUCT);
2573 goto basic_type2;
2575 /* type modifiers */
2576 case TOK_CONST1:
2577 case TOK_CONST2:
2578 case TOK_CONST3:
2579 t |= VT_CONSTANT;
2580 next();
2581 break;
2582 case TOK_VOLATILE1:
2583 case TOK_VOLATILE2:
2584 case TOK_VOLATILE3:
2585 t |= VT_VOLATILE;
2586 next();
2587 break;
2588 case TOK_SIGNED1:
2589 case TOK_SIGNED2:
2590 case TOK_SIGNED3:
2591 typespec_found = 1;
2592 t |= VT_SIGNED;
2593 next();
2594 break;
2595 case TOK_REGISTER:
2596 case TOK_AUTO:
2597 case TOK_RESTRICT1:
2598 case TOK_RESTRICT2:
2599 case TOK_RESTRICT3:
2600 next();
2601 break;
2602 case TOK_UNSIGNED:
2603 t |= VT_UNSIGNED;
2604 next();
2605 typespec_found = 1;
2606 break;
2608 /* storage */
2609 case TOK_EXTERN:
2610 t |= VT_EXTERN;
2611 next();
2612 break;
2613 case TOK_STATIC:
2614 t |= VT_STATIC;
2615 next();
2616 break;
2617 case TOK_TYPEDEF:
2618 t |= VT_TYPEDEF;
2619 next();
2620 break;
2621 case TOK_INLINE1:
2622 case TOK_INLINE2:
2623 case TOK_INLINE3:
2624 t |= VT_INLINE;
2625 next();
2626 break;
2628 /* GNUC attribute */
2629 case TOK_ATTRIBUTE1:
2630 case TOK_ATTRIBUTE2:
2631 parse_attribute(ad);
2632 break;
2633 /* GNUC typeof */
2634 case TOK_TYPEOF1:
2635 case TOK_TYPEOF2:
2636 case TOK_TYPEOF3:
2637 next();
2638 parse_expr_type(&type1);
2639 goto basic_type2;
2640 default:
2641 if (typespec_found || typedef_found)
2642 goto the_end;
2643 s = sym_find(tok);
2644 if (!s || !(s->type.t & VT_TYPEDEF))
2645 goto the_end;
2646 typedef_found = 1;
2647 t |= (s->type.t & ~VT_TYPEDEF);
2648 type->ref = s->type.ref;
2649 next();
2650 typespec_found = 1;
2651 break;
2653 type_found = 1;
2655 the_end:
2656 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2657 error("signed and unsigned modifier");
2658 if (tcc_state->char_is_unsigned) {
2659 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2660 t |= VT_UNSIGNED;
2662 t &= ~VT_SIGNED;
2664 /* long is never used as type */
2665 if ((t & VT_BTYPE) == VT_LONG)
2666 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2667 t = (t & ~VT_BTYPE) | VT_INT;
2668 #else
2669 t = (t & ~VT_BTYPE) | VT_LLONG;
2670 #endif
2671 type->t = t;
2672 return type_found;
2675 /* convert a function parameter type (array to pointer and function to
2676 function pointer) */
2677 static inline void convert_parameter_type(CType *pt)
2679 /* remove const and volatile qualifiers (XXX: const could be used
2680 to indicate a const function parameter */
2681 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2682 /* array must be transformed to pointer according to ANSI C */
2683 pt->t &= ~VT_ARRAY;
2684 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2685 mk_pointer(pt);
2689 static void post_type(CType *type, AttributeDef *ad)
2691 int n, l, t1, arg_size, align;
2692 Sym **plast, *s, *first;
2693 AttributeDef ad1;
2694 CType pt;
2696 if (tok == '(') {
2697 /* function declaration */
2698 next();
2699 l = 0;
2700 first = NULL;
2701 plast = &first;
2702 arg_size = 0;
2703 if (tok != ')') {
2704 for(;;) {
2705 /* read param name and compute offset */
2706 if (l != FUNC_OLD) {
2707 if (!parse_btype(&pt, &ad1)) {
2708 if (l) {
2709 error("invalid type");
2710 } else {
2711 l = FUNC_OLD;
2712 goto old_proto;
2715 l = FUNC_NEW;
2716 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2717 break;
2718 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2719 if ((pt.t & VT_BTYPE) == VT_VOID)
2720 error("parameter declared as void");
2721 arg_size += (type_size(&pt, &align) + 3) & ~3;
2722 } else {
2723 old_proto:
2724 n = tok;
2725 if (n < TOK_UIDENT)
2726 expect("identifier");
2727 pt.t = VT_INT;
2728 next();
2730 convert_parameter_type(&pt);
2731 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
2732 *plast = s;
2733 plast = &s->next;
2734 if (tok == ')')
2735 break;
2736 skip(',');
2737 if (l == FUNC_NEW && tok == TOK_DOTS) {
2738 l = FUNC_ELLIPSIS;
2739 next();
2740 break;
2744 /* if no parameters, then old type prototype */
2745 if (l == 0)
2746 l = FUNC_OLD;
2747 skip(')');
2748 t1 = type->t & VT_STORAGE;
2749 /* NOTE: const is ignored in returned type as it has a special
2750 meaning in gcc / C++ */
2751 type->t &= ~(VT_STORAGE | VT_CONSTANT);
2752 post_type(type, ad);
2753 /* we push a anonymous symbol which will contain the function prototype */
2754 FUNC_ARGS(ad->func_attr) = arg_size;
2755 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
2756 s->next = first;
2757 type->t = t1 | VT_FUNC;
2758 type->ref = s;
2759 } else if (tok == '[') {
2760 /* array definition */
2761 next();
2762 if (tok == TOK_RESTRICT1)
2763 next();
2764 n = -1;
2765 if (tok != ']') {
2766 n = expr_const();
2767 if (n < 0)
2768 error("invalid array size");
2770 skip(']');
2771 /* parse next post type */
2772 t1 = type->t & VT_STORAGE;
2773 type->t &= ~VT_STORAGE;
2774 post_type(type, ad);
2776 /* we push a anonymous symbol which will contain the array
2777 element type */
2778 s = sym_push(SYM_FIELD, type, 0, n);
2779 type->t = t1 | VT_ARRAY | VT_PTR;
2780 type->ref = s;
2784 /* Parse a type declaration (except basic type), and return the type
2785 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2786 expected. 'type' should contain the basic type. 'ad' is the
2787 attribute definition of the basic type. It can be modified by
2788 type_decl().
2790 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
2792 Sym *s;
2793 CType type1, *type2;
2794 int qualifiers;
2796 while (tok == '*') {
2797 qualifiers = 0;
2798 redo:
2799 next();
2800 switch(tok) {
2801 case TOK_CONST1:
2802 case TOK_CONST2:
2803 case TOK_CONST3:
2804 qualifiers |= VT_CONSTANT;
2805 goto redo;
2806 case TOK_VOLATILE1:
2807 case TOK_VOLATILE2:
2808 case TOK_VOLATILE3:
2809 qualifiers |= VT_VOLATILE;
2810 goto redo;
2811 case TOK_RESTRICT1:
2812 case TOK_RESTRICT2:
2813 case TOK_RESTRICT3:
2814 goto redo;
2816 mk_pointer(type);
2817 type->t |= qualifiers;
2820 /* XXX: clarify attribute handling */
2821 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2822 parse_attribute(ad);
2824 /* recursive type */
2825 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2826 type1.t = 0; /* XXX: same as int */
2827 if (tok == '(') {
2828 next();
2829 /* XXX: this is not correct to modify 'ad' at this point, but
2830 the syntax is not clear */
2831 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2832 parse_attribute(ad);
2833 type_decl(&type1, ad, v, td);
2834 skip(')');
2835 } else {
2836 /* type identifier */
2837 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
2838 *v = tok;
2839 next();
2840 } else {
2841 if (!(td & TYPE_ABSTRACT))
2842 expect("identifier");
2843 *v = 0;
2846 post_type(type, ad);
2847 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2848 parse_attribute(ad);
2849 if (!type1.t)
2850 return;
2851 /* append type at the end of type1 */
2852 type2 = &type1;
2853 for(;;) {
2854 s = type2->ref;
2855 type2 = &s->type;
2856 if (!type2->t) {
2857 *type2 = *type;
2858 break;
2861 *type = type1;
2864 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2865 static int lvalue_type(int t)
2867 int bt, r;
2868 r = VT_LVAL;
2869 bt = t & VT_BTYPE;
2870 if (bt == VT_BYTE || bt == VT_BOOL)
2871 r |= VT_LVAL_BYTE;
2872 else if (bt == VT_SHORT)
2873 r |= VT_LVAL_SHORT;
2874 else
2875 return r;
2876 if (t & VT_UNSIGNED)
2877 r |= VT_LVAL_UNSIGNED;
2878 return r;
2881 /* indirection with full error checking and bound check */
2882 static void indir(void)
2884 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
2885 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
2886 return;
2887 expect("pointer");
2889 if ((vtop->r & VT_LVAL) && !nocode_wanted)
2890 gv(RC_INT);
2891 vtop->type = *pointed_type(&vtop->type);
2892 /* Arrays and functions are never lvalues */
2893 if (!(vtop->type.t & VT_ARRAY)
2894 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
2895 vtop->r |= lvalue_type(vtop->type.t);
2896 /* if bound checking, the referenced pointer must be checked */
2897 if (tcc_state->do_bounds_check)
2898 vtop->r |= VT_MUSTBOUND;
2902 /* pass a parameter to a function and do type checking and casting */
2903 static void gfunc_param_typed(Sym *func, Sym *arg)
2905 int func_type;
2906 CType type;
2908 func_type = func->c;
2909 if (func_type == FUNC_OLD ||
2910 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
2911 /* default casting : only need to convert float to double */
2912 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
2913 type.t = VT_DOUBLE;
2914 gen_cast(&type);
2916 } else if (arg == NULL) {
2917 error("too many arguments to function");
2918 } else {
2919 type = arg->type;
2920 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
2921 gen_assign_cast(&type);
2925 /* parse an expression of the form '(type)' or '(expr)' and return its
2926 type */
2927 static void parse_expr_type(CType *type)
2929 int n;
2930 AttributeDef ad;
2932 skip('(');
2933 if (parse_btype(type, &ad)) {
2934 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2935 } else {
2936 expr_type(type);
2938 skip(')');
2941 static void parse_type(CType *type)
2943 AttributeDef ad;
2944 int n;
2946 if (!parse_btype(type, &ad)) {
2947 expect("type");
2949 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2952 static void vpush_tokc(int t)
2954 CType type;
2955 type.t = t;
2956 vsetc(&type, VT_CONST, &tokc);
2959 static void unary(void)
2961 int n, t, align, size, r;
2962 CType type;
2963 Sym *s;
2964 AttributeDef ad;
2966 /* XXX: GCC 2.95.3 does not generate a table although it should be
2967 better here */
2968 tok_next:
2969 switch(tok) {
2970 case TOK_EXTENSION:
2971 next();
2972 goto tok_next;
2973 case TOK_CINT:
2974 case TOK_CCHAR:
2975 case TOK_LCHAR:
2976 vpushi(tokc.i);
2977 next();
2978 break;
2979 case TOK_CUINT:
2980 vpush_tokc(VT_INT | VT_UNSIGNED);
2981 next();
2982 break;
2983 case TOK_CLLONG:
2984 vpush_tokc(VT_LLONG);
2985 next();
2986 break;
2987 case TOK_CULLONG:
2988 vpush_tokc(VT_LLONG | VT_UNSIGNED);
2989 next();
2990 break;
2991 case TOK_CFLOAT:
2992 vpush_tokc(VT_FLOAT);
2993 next();
2994 break;
2995 case TOK_CDOUBLE:
2996 vpush_tokc(VT_DOUBLE);
2997 next();
2998 break;
2999 case TOK_CLDOUBLE:
3000 vpush_tokc(VT_LDOUBLE);
3001 next();
3002 break;
3003 case TOK___FUNCTION__:
3004 if (!gnu_ext)
3005 goto tok_identifier;
3006 /* fall thru */
3007 case TOK___FUNC__:
3009 void *ptr;
3010 int len;
3011 /* special function name identifier */
3012 len = strlen(funcname) + 1;
3013 /* generate char[len] type */
3014 type.t = VT_BYTE;
3015 mk_pointer(&type);
3016 type.t |= VT_ARRAY;
3017 type.ref->c = len;
3018 vpush_ref(&type, data_section, data_section->data_offset, len);
3019 ptr = section_ptr_add(data_section, len);
3020 memcpy(ptr, funcname, len);
3021 next();
3023 break;
3024 case TOK_LSTR:
3025 #ifdef TCC_TARGET_PE
3026 t = VT_SHORT | VT_UNSIGNED;
3027 #else
3028 t = VT_INT;
3029 #endif
3030 goto str_init;
3031 case TOK_STR:
3032 /* string parsing */
3033 t = VT_BYTE;
3034 str_init:
3035 if (tcc_state->warn_write_strings)
3036 t |= VT_CONSTANT;
3037 type.t = t;
3038 mk_pointer(&type);
3039 type.t |= VT_ARRAY;
3040 memset(&ad, 0, sizeof(AttributeDef));
3041 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3042 break;
3043 case '(':
3044 next();
3045 /* cast ? */
3046 if (parse_btype(&type, &ad)) {
3047 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3048 skip(')');
3049 /* check ISOC99 compound literal */
3050 if (tok == '{') {
3051 /* data is allocated locally by default */
3052 if (global_expr)
3053 r = VT_CONST;
3054 else
3055 r = VT_LOCAL;
3056 /* all except arrays are lvalues */
3057 if (!(type.t & VT_ARRAY))
3058 r |= lvalue_type(type.t);
3059 memset(&ad, 0, sizeof(AttributeDef));
3060 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3061 } else {
3062 unary();
3063 gen_cast(&type);
3065 } else if (tok == '{') {
3066 /* save all registers */
3067 save_regs(0);
3068 /* statement expression : we do not accept break/continue
3069 inside as GCC does */
3070 block(NULL, NULL, NULL, NULL, 0, 1);
3071 skip(')');
3072 } else {
3073 gexpr();
3074 skip(')');
3076 break;
3077 case '*':
3078 next();
3079 unary();
3080 indir();
3081 break;
3082 case '&':
3083 next();
3084 unary();
3085 /* functions names must be treated as function pointers,
3086 except for unary '&' and sizeof. Since we consider that
3087 functions are not lvalues, we only have to handle it
3088 there and in function calls. */
3089 /* arrays can also be used although they are not lvalues */
3090 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3091 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3092 test_lvalue();
3093 mk_pointer(&vtop->type);
3094 gaddrof();
3095 break;
3096 case '!':
3097 next();
3098 unary();
3099 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3100 CType boolean;
3101 boolean.t = VT_BOOL;
3102 gen_cast(&boolean);
3103 vtop->c.i = !vtop->c.i;
3104 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3105 vtop->c.i = vtop->c.i ^ 1;
3106 else {
3107 save_regs(1);
3108 vseti(VT_JMP, gtst(1, 0));
3110 break;
3111 case '~':
3112 next();
3113 unary();
3114 vpushi(-1);
3115 gen_op('^');
3116 break;
3117 case '+':
3118 next();
3119 /* in order to force cast, we add zero */
3120 unary();
3121 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3122 error("pointer not accepted for unary plus");
3123 vpushi(0);
3124 gen_op('+');
3125 break;
3126 case TOK_SIZEOF:
3127 case TOK_ALIGNOF1:
3128 case TOK_ALIGNOF2:
3129 t = tok;
3130 next();
3131 if (tok == '(') {
3132 parse_expr_type(&type);
3133 } else {
3134 unary_type(&type);
3136 size = type_size(&type, &align);
3137 if (t == TOK_SIZEOF) {
3138 if (size < 0)
3139 error("sizeof applied to an incomplete type");
3140 vpushi(size);
3141 } else {
3142 vpushi(align);
3144 vtop->type.t |= VT_UNSIGNED;
3145 break;
3147 case TOK_builtin_types_compatible_p:
3149 CType type1, type2;
3150 next();
3151 skip('(');
3152 parse_type(&type1);
3153 skip(',');
3154 parse_type(&type2);
3155 skip(')');
3156 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3157 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3158 vpushi(is_compatible_types(&type1, &type2));
3160 break;
3161 case TOK_builtin_constant_p:
3163 int saved_nocode_wanted, res;
3164 next();
3165 skip('(');
3166 saved_nocode_wanted = nocode_wanted;
3167 nocode_wanted = 1;
3168 gexpr();
3169 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3170 vpop();
3171 nocode_wanted = saved_nocode_wanted;
3172 skip(')');
3173 vpushi(res);
3175 break;
3176 case TOK_builtin_frame_address:
3178 CType type;
3179 next();
3180 skip('(');
3181 if (tok != TOK_CINT) {
3182 error("__builtin_frame_address only takes integers");
3184 if (tokc.i != 0) {
3185 error("TCC only supports __builtin_frame_address(0)");
3187 next();
3188 skip(')');
3189 type.t = VT_VOID;
3190 mk_pointer(&type);
3191 vset(&type, VT_LOCAL, 0);
3193 break;
3194 #ifdef TCC_TARGET_X86_64
3195 case TOK_builtin_malloc:
3196 tok = TOK_malloc;
3197 goto tok_identifier;
3198 case TOK_builtin_free:
3199 tok = TOK_free;
3200 goto tok_identifier;
3201 #endif
3202 case TOK_INC:
3203 case TOK_DEC:
3204 t = tok;
3205 next();
3206 unary();
3207 inc(0, t);
3208 break;
3209 case '-':
3210 next();
3211 vpushi(0);
3212 unary();
3213 gen_op('-');
3214 break;
3215 case TOK_LAND:
3216 if (!gnu_ext)
3217 goto tok_identifier;
3218 next();
3219 /* allow to take the address of a label */
3220 if (tok < TOK_UIDENT)
3221 expect("label identifier");
3222 s = label_find(tok);
3223 if (!s) {
3224 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3225 } else {
3226 if (s->r == LABEL_DECLARED)
3227 s->r = LABEL_FORWARD;
3229 if (!s->type.t) {
3230 s->type.t = VT_VOID;
3231 mk_pointer(&s->type);
3232 s->type.t |= VT_STATIC;
3234 vset(&s->type, VT_CONST | VT_SYM, 0);
3235 vtop->sym = s;
3236 next();
3237 break;
3238 default:
3239 tok_identifier:
3240 t = tok;
3241 next();
3242 if (t < TOK_UIDENT)
3243 expect("identifier");
3244 s = sym_find(t);
3245 if (!s) {
3246 if (tok != '(')
3247 error("'%s' undeclared", get_tok_str(t, NULL));
3248 /* for simple function calls, we tolerate undeclared
3249 external reference to int() function */
3250 if (tcc_state->warn_implicit_function_declaration)
3251 warning("implicit declaration of function '%s'",
3252 get_tok_str(t, NULL));
3253 s = external_global_sym(t, &func_old_type, 0);
3255 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3256 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3257 /* if referencing an inline function, then we generate a
3258 symbol to it if not already done. It will have the
3259 effect to generate code for it at the end of the
3260 compilation unit. Inline function as always
3261 generated in the text section. */
3262 if (!s->c)
3263 put_extern_sym(s, text_section, 0, 0);
3264 r = VT_SYM | VT_CONST;
3265 } else {
3266 r = s->r;
3268 vset(&s->type, r, s->c);
3269 /* if forward reference, we must point to s */
3270 if (vtop->r & VT_SYM) {
3271 vtop->sym = s;
3272 vtop->c.ul = 0;
3274 break;
3277 /* post operations */
3278 while (1) {
3279 if (tok == TOK_INC || tok == TOK_DEC) {
3280 inc(1, tok);
3281 next();
3282 } else if (tok == '.' || tok == TOK_ARROW) {
3283 int qualifiers;
3284 /* field */
3285 if (tok == TOK_ARROW)
3286 indir();
3287 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3288 test_lvalue();
3289 gaddrof();
3290 next();
3291 /* expect pointer on structure */
3292 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3293 expect("struct or union");
3294 s = vtop->type.ref;
3295 /* find field */
3296 tok |= SYM_FIELD;
3297 while ((s = s->next) != NULL) {
3298 if (s->v == tok)
3299 break;
3301 if (!s)
3302 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3303 /* add field offset to pointer */
3304 vtop->type = char_pointer_type; /* change type to 'char *' */
3305 vpushi(s->c);
3306 gen_op('+');
3307 /* change type to field type, and set to lvalue */
3308 vtop->type = s->type;
3309 vtop->type.t |= qualifiers;
3310 /* an array is never an lvalue */
3311 if (!(vtop->type.t & VT_ARRAY)) {
3312 vtop->r |= lvalue_type(vtop->type.t);
3313 /* if bound checking, the referenced pointer must be checked */
3314 if (tcc_state->do_bounds_check)
3315 vtop->r |= VT_MUSTBOUND;
3317 next();
3318 } else if (tok == '[') {
3319 next();
3320 gexpr();
3321 gen_op('+');
3322 indir();
3323 skip(']');
3324 } else if (tok == '(') {
3325 SValue ret;
3326 Sym *sa;
3327 int nb_args;
3329 /* function call */
3330 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3331 /* pointer test (no array accepted) */
3332 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3333 vtop->type = *pointed_type(&vtop->type);
3334 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3335 goto error_func;
3336 } else {
3337 error_func:
3338 expect("function pointer");
3340 } else {
3341 vtop->r &= ~VT_LVAL; /* no lvalue */
3343 /* get return type */
3344 s = vtop->type.ref;
3345 next();
3346 sa = s->next; /* first parameter */
3347 nb_args = 0;
3348 ret.r2 = VT_CONST;
3349 /* compute first implicit argument if a structure is returned */
3350 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3351 /* get some space for the returned structure */
3352 size = type_size(&s->type, &align);
3353 loc = (loc - size) & -align;
3354 ret.type = s->type;
3355 ret.r = VT_LOCAL | VT_LVAL;
3356 /* pass it as 'int' to avoid structure arg passing
3357 problems */
3358 vseti(VT_LOCAL, loc);
3359 ret.c = vtop->c;
3360 nb_args++;
3361 } else {
3362 ret.type = s->type;
3363 /* return in register */
3364 if (is_float(ret.type.t)) {
3365 ret.r = reg_fret(ret.type.t);
3366 } else {
3367 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3368 ret.r2 = REG_LRET;
3369 ret.r = REG_IRET;
3371 ret.c.i = 0;
3373 if (tok != ')') {
3374 for(;;) {
3375 expr_eq();
3376 gfunc_param_typed(s, sa);
3377 nb_args++;
3378 if (sa)
3379 sa = sa->next;
3380 if (tok == ')')
3381 break;
3382 skip(',');
3385 if (sa)
3386 error("too few arguments to function");
3387 skip(')');
3388 if (!nocode_wanted) {
3389 gfunc_call(nb_args);
3390 } else {
3391 vtop -= (nb_args + 1);
3393 /* return value */
3394 vsetc(&ret.type, ret.r, &ret.c);
3395 vtop->r2 = ret.r2;
3396 } else {
3397 break;
3402 static void uneq(void)
3404 int t;
3406 unary();
3407 if (tok == '=' ||
3408 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3409 tok == TOK_A_XOR || tok == TOK_A_OR ||
3410 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3411 test_lvalue();
3412 t = tok;
3413 next();
3414 if (t == '=') {
3415 expr_eq();
3416 } else {
3417 vdup();
3418 expr_eq();
3419 gen_op(t & 0x7f);
3421 vstore();
3425 static void expr_prod(void)
3427 int t;
3429 uneq();
3430 while (tok == '*' || tok == '/' || tok == '%') {
3431 t = tok;
3432 next();
3433 uneq();
3434 gen_op(t);
3438 static void expr_sum(void)
3440 int t;
3442 expr_prod();
3443 while (tok == '+' || tok == '-') {
3444 t = tok;
3445 next();
3446 expr_prod();
3447 gen_op(t);
3451 static void expr_shift(void)
3453 int t;
3455 expr_sum();
3456 while (tok == TOK_SHL || tok == TOK_SAR) {
3457 t = tok;
3458 next();
3459 expr_sum();
3460 gen_op(t);
3464 static void expr_cmp(void)
3466 int t;
3468 expr_shift();
3469 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3470 tok == TOK_ULT || tok == TOK_UGE) {
3471 t = tok;
3472 next();
3473 expr_shift();
3474 gen_op(t);
3478 static void expr_cmpeq(void)
3480 int t;
3482 expr_cmp();
3483 while (tok == TOK_EQ || tok == TOK_NE) {
3484 t = tok;
3485 next();
3486 expr_cmp();
3487 gen_op(t);
3491 static void expr_and(void)
3493 expr_cmpeq();
3494 while (tok == '&') {
3495 next();
3496 expr_cmpeq();
3497 gen_op('&');
3501 static void expr_xor(void)
3503 expr_and();
3504 while (tok == '^') {
3505 next();
3506 expr_and();
3507 gen_op('^');
3511 static void expr_or(void)
3513 expr_xor();
3514 while (tok == '|') {
3515 next();
3516 expr_xor();
3517 gen_op('|');
3521 /* XXX: fix this mess */
3522 static void expr_land_const(void)
3524 expr_or();
3525 while (tok == TOK_LAND) {
3526 next();
3527 expr_or();
3528 gen_op(TOK_LAND);
3532 /* XXX: fix this mess */
3533 static void expr_lor_const(void)
3535 expr_land_const();
3536 while (tok == TOK_LOR) {
3537 next();
3538 expr_land_const();
3539 gen_op(TOK_LOR);
3543 /* only used if non constant */
3544 static void expr_land(void)
3546 int t;
3548 expr_or();
3549 if (tok == TOK_LAND) {
3550 t = 0;
3551 save_regs(1);
3552 for(;;) {
3553 t = gtst(1, t);
3554 if (tok != TOK_LAND) {
3555 vseti(VT_JMPI, t);
3556 break;
3558 next();
3559 expr_or();
3564 static void expr_lor(void)
3566 int t;
3568 expr_land();
3569 if (tok == TOK_LOR) {
3570 t = 0;
3571 save_regs(1);
3572 for(;;) {
3573 t = gtst(0, t);
3574 if (tok != TOK_LOR) {
3575 vseti(VT_JMP, t);
3576 break;
3578 next();
3579 expr_land();
3584 /* XXX: better constant handling */
3585 static void expr_eq(void)
3587 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3588 SValue sv;
3589 CType type, type1, type2;
3591 if (const_wanted) {
3592 expr_lor_const();
3593 if (tok == '?') {
3594 CType boolean;
3595 int c;
3596 boolean.t = VT_BOOL;
3597 vdup();
3598 gen_cast(&boolean);
3599 c = vtop->c.i;
3600 vpop();
3601 next();
3602 if (tok != ':' || !gnu_ext) {
3603 vpop();
3604 gexpr();
3606 if (!c)
3607 vpop();
3608 skip(':');
3609 expr_eq();
3610 if (c)
3611 vpop();
3613 } else {
3614 expr_lor();
3615 if (tok == '?') {
3616 next();
3617 if (vtop != vstack) {
3618 /* needed to avoid having different registers saved in
3619 each branch */
3620 if (is_float(vtop->type.t)) {
3621 rc = RC_FLOAT;
3622 #ifdef TCC_TARGET_X86_64
3623 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3624 rc = RC_ST0;
3626 #endif
3628 else
3629 rc = RC_INT;
3630 gv(rc);
3631 save_regs(1);
3633 if (tok == ':' && gnu_ext) {
3634 gv_dup();
3635 tt = gtst(1, 0);
3636 } else {
3637 tt = gtst(1, 0);
3638 gexpr();
3640 type1 = vtop->type;
3641 sv = *vtop; /* save value to handle it later */
3642 vtop--; /* no vpop so that FP stack is not flushed */
3643 skip(':');
3644 u = gjmp(0);
3645 gsym(tt);
3646 expr_eq();
3647 type2 = vtop->type;
3649 t1 = type1.t;
3650 bt1 = t1 & VT_BTYPE;
3651 t2 = type2.t;
3652 bt2 = t2 & VT_BTYPE;
3653 /* cast operands to correct type according to ISOC rules */
3654 if (is_float(bt1) || is_float(bt2)) {
3655 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3656 type.t = VT_LDOUBLE;
3657 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3658 type.t = VT_DOUBLE;
3659 } else {
3660 type.t = VT_FLOAT;
3662 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3663 /* cast to biggest op */
3664 type.t = VT_LLONG;
3665 /* convert to unsigned if it does not fit in a long long */
3666 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3667 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3668 type.t |= VT_UNSIGNED;
3669 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3670 /* XXX: test pointer compatibility */
3671 type = type1;
3672 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3673 /* XXX: test function pointer compatibility */
3674 type = type1;
3675 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3676 /* XXX: test structure compatibility */
3677 type = type1;
3678 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3679 /* NOTE: as an extension, we accept void on only one side */
3680 type.t = VT_VOID;
3681 } else {
3682 /* integer operations */
3683 type.t = VT_INT;
3684 /* convert to unsigned if it does not fit in an integer */
3685 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3686 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3687 type.t |= VT_UNSIGNED;
3690 /* now we convert second operand */
3691 gen_cast(&type);
3692 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3693 gaddrof();
3694 rc = RC_INT;
3695 if (is_float(type.t)) {
3696 rc = RC_FLOAT;
3697 #ifdef TCC_TARGET_X86_64
3698 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3699 rc = RC_ST0;
3701 #endif
3702 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3703 /* for long longs, we use fixed registers to avoid having
3704 to handle a complicated move */
3705 rc = RC_IRET;
3708 r2 = gv(rc);
3709 /* this is horrible, but we must also convert first
3710 operand */
3711 tt = gjmp(0);
3712 gsym(u);
3713 /* put again first value and cast it */
3714 *vtop = sv;
3715 gen_cast(&type);
3716 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3717 gaddrof();
3718 r1 = gv(rc);
3719 move_reg(r2, r1);
3720 vtop->r = r2;
3721 gsym(tt);
3726 static void gexpr(void)
3728 while (1) {
3729 expr_eq();
3730 if (tok != ',')
3731 break;
3732 vpop();
3733 next();
3737 /* parse an expression and return its type without any side effect. */
3738 static void expr_type(CType *type)
3740 int saved_nocode_wanted;
3742 saved_nocode_wanted = nocode_wanted;
3743 nocode_wanted = 1;
3744 gexpr();
3745 *type = vtop->type;
3746 vpop();
3747 nocode_wanted = saved_nocode_wanted;
3750 /* parse a unary expression and return its type without any side
3751 effect. */
3752 static void unary_type(CType *type)
3754 int a;
3756 a = nocode_wanted;
3757 nocode_wanted = 1;
3758 unary();
3759 *type = vtop->type;
3760 vpop();
3761 nocode_wanted = a;
3764 /* parse a constant expression and return value in vtop. */
3765 static void expr_const1(void)
3767 int a;
3768 a = const_wanted;
3769 const_wanted = 1;
3770 expr_eq();
3771 const_wanted = a;
3774 /* parse an integer constant and return its value. */
3775 static int expr_const(void)
3777 int c;
3778 expr_const1();
3779 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
3780 expect("constant expression");
3781 c = vtop->c.i;
3782 vpop();
3783 return c;
3786 /* return the label token if current token is a label, otherwise
3787 return zero */
3788 static int is_label(void)
3790 int last_tok;
3792 /* fast test first */
3793 if (tok < TOK_UIDENT)
3794 return 0;
3795 /* no need to save tokc because tok is an identifier */
3796 last_tok = tok;
3797 next();
3798 if (tok == ':') {
3799 next();
3800 return last_tok;
3801 } else {
3802 unget_tok(last_tok);
3803 return 0;
3807 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
3808 int case_reg, int is_expr)
3810 int a, b, c, d;
3811 Sym *s;
3813 /* generate line number info */
3814 if (tcc_state->do_debug &&
3815 (last_line_num != file->line_num || last_ind != ind)) {
3816 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
3817 last_ind = ind;
3818 last_line_num = file->line_num;
3821 if (is_expr) {
3822 /* default return value is (void) */
3823 vpushi(0);
3824 vtop->type.t = VT_VOID;
3827 if (tok == TOK_IF) {
3828 /* if test */
3829 next();
3830 skip('(');
3831 gexpr();
3832 skip(')');
3833 a = gtst(1, 0);
3834 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3835 c = tok;
3836 if (c == TOK_ELSE) {
3837 next();
3838 d = gjmp(0);
3839 gsym(a);
3840 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3841 gsym(d); /* patch else jmp */
3842 } else
3843 gsym(a);
3844 } else if (tok == TOK_WHILE) {
3845 next();
3846 d = ind;
3847 skip('(');
3848 gexpr();
3849 skip(')');
3850 a = gtst(1, 0);
3851 b = 0;
3852 block(&a, &b, case_sym, def_sym, case_reg, 0);
3853 gjmp_addr(d);
3854 gsym(a);
3855 gsym_addr(b, d);
3856 } else if (tok == '{') {
3857 Sym *llabel;
3859 next();
3860 /* record local declaration stack position */
3861 s = local_stack;
3862 llabel = local_label_stack;
3863 /* handle local labels declarations */
3864 if (tok == TOK_LABEL) {
3865 next();
3866 for(;;) {
3867 if (tok < TOK_UIDENT)
3868 expect("label identifier");
3869 label_push(&local_label_stack, tok, LABEL_DECLARED);
3870 next();
3871 if (tok == ',') {
3872 next();
3873 } else {
3874 skip(';');
3875 break;
3879 while (tok != '}') {
3880 decl(VT_LOCAL);
3881 if (tok != '}') {
3882 if (is_expr)
3883 vpop();
3884 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
3887 /* pop locally defined labels */
3888 label_pop(&local_label_stack, llabel);
3889 /* pop locally defined symbols */
3890 if(is_expr) {
3891 /* XXX: this solution makes only valgrind happy...
3892 triggered by gcc.c-torture/execute/20000917-1.c */
3893 Sym *p;
3894 switch(vtop->type.t & VT_BTYPE) {
3895 case VT_PTR:
3896 case VT_STRUCT:
3897 case VT_ENUM:
3898 case VT_FUNC:
3899 for(p=vtop->type.ref;p;p=p->prev)
3900 if(p->prev==s)
3901 error("unsupported expression type");
3904 sym_pop(&local_stack, s);
3905 next();
3906 } else if (tok == TOK_RETURN) {
3907 next();
3908 if (tok != ';') {
3909 gexpr();
3910 gen_assign_cast(&func_vt);
3911 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
3912 CType type;
3913 /* if returning structure, must copy it to implicit
3914 first pointer arg location */
3915 #ifdef TCC_ARM_EABI
3916 int align, size;
3917 size = type_size(&func_vt,&align);
3918 if(size <= 4)
3920 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
3921 && (align & 3))
3923 int addr;
3924 loc = (loc - size) & -4;
3925 addr = loc;
3926 type = func_vt;
3927 vset(&type, VT_LOCAL | VT_LVAL, addr);
3928 vswap();
3929 vstore();
3930 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
3932 vtop->type = int_type;
3933 gv(RC_IRET);
3934 } else {
3935 #endif
3936 type = func_vt;
3937 mk_pointer(&type);
3938 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
3939 indir();
3940 vswap();
3941 /* copy structure value to pointer */
3942 vstore();
3943 #ifdef TCC_ARM_EABI
3945 #endif
3946 } else if (is_float(func_vt.t)) {
3947 gv(rc_fret(func_vt.t));
3948 } else {
3949 gv(RC_IRET);
3951 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3953 skip(';');
3954 rsym = gjmp(rsym); /* jmp */
3955 } else if (tok == TOK_BREAK) {
3956 /* compute jump */
3957 if (!bsym)
3958 error("cannot break");
3959 *bsym = gjmp(*bsym);
3960 next();
3961 skip(';');
3962 } else if (tok == TOK_CONTINUE) {
3963 /* compute jump */
3964 if (!csym)
3965 error("cannot continue");
3966 *csym = gjmp(*csym);
3967 next();
3968 skip(';');
3969 } else if (tok == TOK_FOR) {
3970 int e;
3971 next();
3972 skip('(');
3973 if (tok != ';') {
3974 gexpr();
3975 vpop();
3977 skip(';');
3978 d = ind;
3979 c = ind;
3980 a = 0;
3981 b = 0;
3982 if (tok != ';') {
3983 gexpr();
3984 a = gtst(1, 0);
3986 skip(';');
3987 if (tok != ')') {
3988 e = gjmp(0);
3989 c = ind;
3990 gexpr();
3991 vpop();
3992 gjmp_addr(d);
3993 gsym(e);
3995 skip(')');
3996 block(&a, &b, case_sym, def_sym, case_reg, 0);
3997 gjmp_addr(c);
3998 gsym(a);
3999 gsym_addr(b, c);
4000 } else
4001 if (tok == TOK_DO) {
4002 next();
4003 a = 0;
4004 b = 0;
4005 d = ind;
4006 block(&a, &b, case_sym, def_sym, case_reg, 0);
4007 skip(TOK_WHILE);
4008 skip('(');
4009 gsym(b);
4010 gexpr();
4011 c = gtst(0, 0);
4012 gsym_addr(c, d);
4013 skip(')');
4014 gsym(a);
4015 skip(';');
4016 } else
4017 if (tok == TOK_SWITCH) {
4018 next();
4019 skip('(');
4020 gexpr();
4021 /* XXX: other types than integer */
4022 case_reg = gv(RC_INT);
4023 vpop();
4024 skip(')');
4025 a = 0;
4026 b = gjmp(0); /* jump to first case */
4027 c = 0;
4028 block(&a, csym, &b, &c, case_reg, 0);
4029 /* if no default, jmp after switch */
4030 if (c == 0)
4031 c = ind;
4032 /* default label */
4033 gsym_addr(b, c);
4034 /* break label */
4035 gsym(a);
4036 } else
4037 if (tok == TOK_CASE) {
4038 int v1, v2;
4039 if (!case_sym)
4040 expect("switch");
4041 next();
4042 v1 = expr_const();
4043 v2 = v1;
4044 if (gnu_ext && tok == TOK_DOTS) {
4045 next();
4046 v2 = expr_const();
4047 if (v2 < v1)
4048 warning("empty case range");
4050 /* since a case is like a label, we must skip it with a jmp */
4051 b = gjmp(0);
4052 gsym(*case_sym);
4053 vseti(case_reg, 0);
4054 vpushi(v1);
4055 if (v1 == v2) {
4056 gen_op(TOK_EQ);
4057 *case_sym = gtst(1, 0);
4058 } else {
4059 gen_op(TOK_GE);
4060 *case_sym = gtst(1, 0);
4061 vseti(case_reg, 0);
4062 vpushi(v2);
4063 gen_op(TOK_LE);
4064 *case_sym = gtst(1, *case_sym);
4066 gsym(b);
4067 skip(':');
4068 is_expr = 0;
4069 goto block_after_label;
4070 } else
4071 if (tok == TOK_DEFAULT) {
4072 next();
4073 skip(':');
4074 if (!def_sym)
4075 expect("switch");
4076 if (*def_sym)
4077 error("too many 'default'");
4078 *def_sym = ind;
4079 is_expr = 0;
4080 goto block_after_label;
4081 } else
4082 if (tok == TOK_GOTO) {
4083 next();
4084 if (tok == '*' && gnu_ext) {
4085 /* computed goto */
4086 next();
4087 gexpr();
4088 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4089 expect("pointer");
4090 ggoto();
4091 } else if (tok >= TOK_UIDENT) {
4092 s = label_find(tok);
4093 /* put forward definition if needed */
4094 if (!s) {
4095 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4096 } else {
4097 if (s->r == LABEL_DECLARED)
4098 s->r = LABEL_FORWARD;
4100 /* label already defined */
4101 if (s->r & LABEL_FORWARD)
4102 s->jnext = gjmp(s->jnext);
4103 else
4104 gjmp_addr(s->jnext);
4105 next();
4106 } else {
4107 expect("label identifier");
4109 skip(';');
4110 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4111 asm_instr();
4112 } else {
4113 b = is_label();
4114 if (b) {
4115 /* label case */
4116 s = label_find(b);
4117 if (s) {
4118 if (s->r == LABEL_DEFINED)
4119 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4120 gsym(s->jnext);
4121 s->r = LABEL_DEFINED;
4122 } else {
4123 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4125 s->jnext = ind;
4126 /* we accept this, but it is a mistake */
4127 block_after_label:
4128 if (tok == '}') {
4129 warning("deprecated use of label at end of compound statement");
4130 } else {
4131 if (is_expr)
4132 vpop();
4133 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4135 } else {
4136 /* expression case */
4137 if (tok != ';') {
4138 if (is_expr) {
4139 vpop();
4140 gexpr();
4141 } else {
4142 gexpr();
4143 vpop();
4146 skip(';');
4151 /* t is the array or struct type. c is the array or struct
4152 address. cur_index/cur_field is the pointer to the current
4153 value. 'size_only' is true if only size info is needed (only used
4154 in arrays) */
4155 static void decl_designator(CType *type, Section *sec, unsigned long c,
4156 int *cur_index, Sym **cur_field,
4157 int size_only)
4159 Sym *s, *f;
4160 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4161 CType type1;
4163 notfirst = 0;
4164 elem_size = 0;
4165 nb_elems = 1;
4166 if (gnu_ext && (l = is_label()) != 0)
4167 goto struct_field;
4168 while (tok == '[' || tok == '.') {
4169 if (tok == '[') {
4170 if (!(type->t & VT_ARRAY))
4171 expect("array type");
4172 s = type->ref;
4173 next();
4174 index = expr_const();
4175 if (index < 0 || (s->c >= 0 && index >= s->c))
4176 expect("invalid index");
4177 if (tok == TOK_DOTS && gnu_ext) {
4178 next();
4179 index_last = expr_const();
4180 if (index_last < 0 ||
4181 (s->c >= 0 && index_last >= s->c) ||
4182 index_last < index)
4183 expect("invalid index");
4184 } else {
4185 index_last = index;
4187 skip(']');
4188 if (!notfirst)
4189 *cur_index = index_last;
4190 type = pointed_type(type);
4191 elem_size = type_size(type, &align);
4192 c += index * elem_size;
4193 /* NOTE: we only support ranges for last designator */
4194 nb_elems = index_last - index + 1;
4195 if (nb_elems != 1) {
4196 notfirst = 1;
4197 break;
4199 } else {
4200 next();
4201 l = tok;
4202 next();
4203 struct_field:
4204 if ((type->t & VT_BTYPE) != VT_STRUCT)
4205 expect("struct/union type");
4206 s = type->ref;
4207 l |= SYM_FIELD;
4208 f = s->next;
4209 while (f) {
4210 if (f->v == l)
4211 break;
4212 f = f->next;
4214 if (!f)
4215 expect("field");
4216 if (!notfirst)
4217 *cur_field = f;
4218 /* XXX: fix this mess by using explicit storage field */
4219 type1 = f->type;
4220 type1.t |= (type->t & ~VT_TYPE);
4221 type = &type1;
4222 c += f->c;
4224 notfirst = 1;
4226 if (notfirst) {
4227 if (tok == '=') {
4228 next();
4229 } else {
4230 if (!gnu_ext)
4231 expect("=");
4233 } else {
4234 if (type->t & VT_ARRAY) {
4235 index = *cur_index;
4236 type = pointed_type(type);
4237 c += index * type_size(type, &align);
4238 } else {
4239 f = *cur_field;
4240 if (!f)
4241 error("too many field init");
4242 /* XXX: fix this mess by using explicit storage field */
4243 type1 = f->type;
4244 type1.t |= (type->t & ~VT_TYPE);
4245 type = &type1;
4246 c += f->c;
4249 decl_initializer(type, sec, c, 0, size_only);
4251 /* XXX: make it more general */
4252 if (!size_only && nb_elems > 1) {
4253 unsigned long c_end;
4254 uint8_t *src, *dst;
4255 int i;
4257 if (!sec)
4258 error("range init not supported yet for dynamic storage");
4259 c_end = c + nb_elems * elem_size;
4260 if (c_end > sec->data_allocated)
4261 section_realloc(sec, c_end);
4262 src = sec->data + c;
4263 dst = src;
4264 for(i = 1; i < nb_elems; i++) {
4265 dst += elem_size;
4266 memcpy(dst, src, elem_size);
4271 #define EXPR_VAL 0
4272 #define EXPR_CONST 1
4273 #define EXPR_ANY 2
4275 /* store a value or an expression directly in global data or in local array */
4276 static void init_putv(CType *type, Section *sec, unsigned long c,
4277 int v, int expr_type)
4279 int saved_global_expr, bt, bit_pos, bit_size;
4280 void *ptr;
4281 unsigned long long bit_mask;
4282 CType dtype;
4284 switch(expr_type) {
4285 case EXPR_VAL:
4286 vpushi(v);
4287 break;
4288 case EXPR_CONST:
4289 /* compound literals must be allocated globally in this case */
4290 saved_global_expr = global_expr;
4291 global_expr = 1;
4292 expr_const1();
4293 global_expr = saved_global_expr;
4294 /* NOTE: symbols are accepted */
4295 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4296 error("initializer element is not constant");
4297 break;
4298 case EXPR_ANY:
4299 expr_eq();
4300 break;
4303 dtype = *type;
4304 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4306 if (sec) {
4307 /* XXX: not portable */
4308 /* XXX: generate error if incorrect relocation */
4309 gen_assign_cast(&dtype);
4310 bt = type->t & VT_BTYPE;
4311 /* we'll write at most 12 bytes */
4312 if (c + 12 > sec->data_allocated) {
4313 section_realloc(sec, c + 12);
4315 ptr = sec->data + c;
4316 /* XXX: make code faster ? */
4317 if (!(type->t & VT_BITFIELD)) {
4318 bit_pos = 0;
4319 bit_size = 32;
4320 bit_mask = -1LL;
4321 } else {
4322 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4323 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4324 bit_mask = (1LL << bit_size) - 1;
4326 if ((vtop->r & VT_SYM) &&
4327 (bt == VT_BYTE ||
4328 bt == VT_SHORT ||
4329 bt == VT_DOUBLE ||
4330 bt == VT_LDOUBLE ||
4331 bt == VT_LLONG ||
4332 (bt == VT_INT && bit_size != 32)))
4333 error("initializer element is not computable at load time");
4334 switch(bt) {
4335 case VT_BOOL:
4336 vtop->c.i = (vtop->c.i != 0);
4337 case VT_BYTE:
4338 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4339 break;
4340 case VT_SHORT:
4341 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4342 break;
4343 case VT_DOUBLE:
4344 *(double *)ptr = vtop->c.d;
4345 break;
4346 case VT_LDOUBLE:
4347 *(long double *)ptr = vtop->c.ld;
4348 break;
4349 case VT_LLONG:
4350 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4351 break;
4352 default:
4353 if (vtop->r & VT_SYM) {
4354 greloc(sec, vtop->sym, c, R_DATA_PTR);
4356 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4357 break;
4359 vtop--;
4360 } else {
4361 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4362 vswap();
4363 vstore();
4364 vpop();
4368 /* put zeros for variable based init */
4369 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4371 if (sec) {
4372 /* nothing to do because globals are already set to zero */
4373 } else {
4374 vpush_global_sym(&func_old_type, TOK_memset);
4375 vseti(VT_LOCAL, c);
4376 vpushi(0);
4377 vpushi(size);
4378 gfunc_call(3);
4382 /* 't' contains the type and storage info. 'c' is the offset of the
4383 object in section 'sec'. If 'sec' is NULL, it means stack based
4384 allocation. 'first' is true if array '{' must be read (multi
4385 dimension implicit array init handling). 'size_only' is true if
4386 size only evaluation is wanted (only for arrays). */
4387 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4388 int first, int size_only)
4390 int index, array_length, n, no_oblock, nb, parlevel, i;
4391 int size1, align1, expr_type;
4392 Sym *s, *f;
4393 CType *t1;
4395 if (type->t & VT_ARRAY) {
4396 s = type->ref;
4397 n = s->c;
4398 array_length = 0;
4399 t1 = pointed_type(type);
4400 size1 = type_size(t1, &align1);
4402 no_oblock = 1;
4403 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4404 tok == '{') {
4405 skip('{');
4406 no_oblock = 0;
4409 /* only parse strings here if correct type (otherwise: handle
4410 them as ((w)char *) expressions */
4411 if ((tok == TOK_LSTR &&
4412 #ifdef TCC_TARGET_PE
4413 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4414 #else
4415 (t1->t & VT_BTYPE) == VT_INT
4416 #endif
4417 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4418 while (tok == TOK_STR || tok == TOK_LSTR) {
4419 int cstr_len, ch;
4420 CString *cstr;
4422 cstr = tokc.cstr;
4423 /* compute maximum number of chars wanted */
4424 if (tok == TOK_STR)
4425 cstr_len = cstr->size;
4426 else
4427 cstr_len = cstr->size / sizeof(nwchar_t);
4428 cstr_len--;
4429 nb = cstr_len;
4430 if (n >= 0 && nb > (n - array_length))
4431 nb = n - array_length;
4432 if (!size_only) {
4433 if (cstr_len > nb)
4434 warning("initializer-string for array is too long");
4435 /* in order to go faster for common case (char
4436 string in global variable, we handle it
4437 specifically */
4438 if (sec && tok == TOK_STR && size1 == 1) {
4439 memcpy(sec->data + c + array_length, cstr->data, nb);
4440 } else {
4441 for(i=0;i<nb;i++) {
4442 if (tok == TOK_STR)
4443 ch = ((unsigned char *)cstr->data)[i];
4444 else
4445 ch = ((nwchar_t *)cstr->data)[i];
4446 init_putv(t1, sec, c + (array_length + i) * size1,
4447 ch, EXPR_VAL);
4451 array_length += nb;
4452 next();
4454 /* only add trailing zero if enough storage (no
4455 warning in this case since it is standard) */
4456 if (n < 0 || array_length < n) {
4457 if (!size_only) {
4458 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4460 array_length++;
4462 } else {
4463 index = 0;
4464 while (tok != '}') {
4465 decl_designator(type, sec, c, &index, NULL, size_only);
4466 if (n >= 0 && index >= n)
4467 error("index too large");
4468 /* must put zero in holes (note that doing it that way
4469 ensures that it even works with designators) */
4470 if (!size_only && array_length < index) {
4471 init_putz(t1, sec, c + array_length * size1,
4472 (index - array_length) * size1);
4474 index++;
4475 if (index > array_length)
4476 array_length = index;
4477 /* special test for multi dimensional arrays (may not
4478 be strictly correct if designators are used at the
4479 same time) */
4480 if (index >= n && no_oblock)
4481 break;
4482 if (tok == '}')
4483 break;
4484 skip(',');
4487 if (!no_oblock)
4488 skip('}');
4489 /* put zeros at the end */
4490 if (!size_only && n >= 0 && array_length < n) {
4491 init_putz(t1, sec, c + array_length * size1,
4492 (n - array_length) * size1);
4494 /* patch type size if needed */
4495 if (n < 0)
4496 s->c = array_length;
4497 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4498 (sec || !first || tok == '{')) {
4499 int par_count;
4501 /* NOTE: the previous test is a specific case for automatic
4502 struct/union init */
4503 /* XXX: union needs only one init */
4505 /* XXX: this test is incorrect for local initializers
4506 beginning with ( without {. It would be much more difficult
4507 to do it correctly (ideally, the expression parser should
4508 be used in all cases) */
4509 par_count = 0;
4510 if (tok == '(') {
4511 AttributeDef ad1;
4512 CType type1;
4513 next();
4514 while (tok == '(') {
4515 par_count++;
4516 next();
4518 if (!parse_btype(&type1, &ad1))
4519 expect("cast");
4520 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4521 #if 0
4522 if (!is_assignable_types(type, &type1))
4523 error("invalid type for cast");
4524 #endif
4525 skip(')');
4527 no_oblock = 1;
4528 if (first || tok == '{') {
4529 skip('{');
4530 no_oblock = 0;
4532 s = type->ref;
4533 f = s->next;
4534 array_length = 0;
4535 index = 0;
4536 n = s->c;
4537 while (tok != '}') {
4538 decl_designator(type, sec, c, NULL, &f, size_only);
4539 index = f->c;
4540 if (!size_only && array_length < index) {
4541 init_putz(type, sec, c + array_length,
4542 index - array_length);
4544 index = index + type_size(&f->type, &align1);
4545 if (index > array_length)
4546 array_length = index;
4548 /* gr: skip fields from same union - ugly. */
4549 while (f->next) {
4550 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4551 /* test for same offset */
4552 if (f->next->c != f->c)
4553 break;
4554 /* if yes, test for bitfield shift */
4555 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4556 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4557 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4558 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4559 if (bit_pos_1 != bit_pos_2)
4560 break;
4562 f = f->next;
4565 f = f->next;
4566 if (no_oblock && f == NULL)
4567 break;
4568 if (tok == '}')
4569 break;
4570 skip(',');
4572 /* put zeros at the end */
4573 if (!size_only && array_length < n) {
4574 init_putz(type, sec, c + array_length,
4575 n - array_length);
4577 if (!no_oblock)
4578 skip('}');
4579 while (par_count) {
4580 skip(')');
4581 par_count--;
4583 } else if (tok == '{') {
4584 next();
4585 decl_initializer(type, sec, c, first, size_only);
4586 skip('}');
4587 } else if (size_only) {
4588 /* just skip expression */
4589 parlevel = 0;
4590 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4591 tok != -1) {
4592 if (tok == '(')
4593 parlevel++;
4594 else if (tok == ')')
4595 parlevel--;
4596 next();
4598 } else {
4599 /* currently, we always use constant expression for globals
4600 (may change for scripting case) */
4601 expr_type = EXPR_CONST;
4602 if (!sec)
4603 expr_type = EXPR_ANY;
4604 init_putv(type, sec, c, 0, expr_type);
4608 /* parse an initializer for type 't' if 'has_init' is non zero, and
4609 allocate space in local or global data space ('r' is either
4610 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4611 variable 'v' of scope 'scope' is declared before initializers are
4612 parsed. If 'v' is zero, then a reference to the new object is put
4613 in the value stack. If 'has_init' is 2, a special parsing is done
4614 to handle string constants. */
4615 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4616 int has_init, int v, int scope)
4618 int size, align, addr, data_offset;
4619 int level;
4620 ParseState saved_parse_state = {0};
4621 TokenString init_str;
4622 Section *sec;
4624 size = type_size(type, &align);
4625 /* If unknown size, we must evaluate it before
4626 evaluating initializers because
4627 initializers can generate global data too
4628 (e.g. string pointers or ISOC99 compound
4629 literals). It also simplifies local
4630 initializers handling */
4631 tok_str_new(&init_str);
4632 if (size < 0) {
4633 if (!has_init)
4634 error("unknown type size");
4635 /* get all init string */
4636 if (has_init == 2) {
4637 /* only get strings */
4638 while (tok == TOK_STR || tok == TOK_LSTR) {
4639 tok_str_add_tok(&init_str);
4640 next();
4642 } else {
4643 level = 0;
4644 while (level > 0 || (tok != ',' && tok != ';')) {
4645 if (tok < 0)
4646 error("unexpected end of file in initializer");
4647 tok_str_add_tok(&init_str);
4648 if (tok == '{')
4649 level++;
4650 else if (tok == '}') {
4651 level--;
4652 if (level <= 0) {
4653 next();
4654 break;
4657 next();
4660 tok_str_add(&init_str, -1);
4661 tok_str_add(&init_str, 0);
4663 /* compute size */
4664 save_parse_state(&saved_parse_state);
4666 macro_ptr = init_str.str;
4667 next();
4668 decl_initializer(type, NULL, 0, 1, 1);
4669 /* prepare second initializer parsing */
4670 macro_ptr = init_str.str;
4671 next();
4673 /* if still unknown size, error */
4674 size = type_size(type, &align);
4675 if (size < 0)
4676 error("unknown type size");
4678 /* take into account specified alignment if bigger */
4679 if (ad->aligned) {
4680 if (ad->aligned > align)
4681 align = ad->aligned;
4682 } else if (ad->packed) {
4683 align = 1;
4685 if ((r & VT_VALMASK) == VT_LOCAL) {
4686 sec = NULL;
4687 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4688 loc--;
4689 loc = (loc - size) & -align;
4690 addr = loc;
4691 /* handles bounds */
4692 /* XXX: currently, since we do only one pass, we cannot track
4693 '&' operators, so we add only arrays */
4694 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4695 unsigned long *bounds_ptr;
4696 /* add padding between regions */
4697 loc--;
4698 /* then add local bound info */
4699 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4700 bounds_ptr[0] = addr;
4701 bounds_ptr[1] = size;
4703 if (v) {
4704 /* local variable */
4705 sym_push(v, type, r, addr);
4706 } else {
4707 /* push local reference */
4708 vset(type, r, addr);
4710 } else {
4711 Sym *sym;
4713 sym = NULL;
4714 if (v && scope == VT_CONST) {
4715 /* see if the symbol was already defined */
4716 sym = sym_find(v);
4717 if (sym) {
4718 if (!is_compatible_types(&sym->type, type))
4719 error("incompatible types for redefinition of '%s'",
4720 get_tok_str(v, NULL));
4721 if (sym->type.t & VT_EXTERN) {
4722 /* if the variable is extern, it was not allocated */
4723 sym->type.t &= ~VT_EXTERN;
4724 /* set array size if it was ommited in extern
4725 declaration */
4726 if ((sym->type.t & VT_ARRAY) &&
4727 sym->type.ref->c < 0 &&
4728 type->ref->c >= 0)
4729 sym->type.ref->c = type->ref->c;
4730 } else {
4731 /* we accept several definitions of the same
4732 global variable. this is tricky, because we
4733 must play with the SHN_COMMON type of the symbol */
4734 /* XXX: should check if the variable was already
4735 initialized. It is incorrect to initialized it
4736 twice */
4737 /* no init data, we won't add more to the symbol */
4738 if (!has_init)
4739 goto no_alloc;
4744 /* allocate symbol in corresponding section */
4745 sec = ad->section;
4746 if (!sec) {
4747 if (has_init)
4748 sec = data_section;
4749 else if (tcc_state->nocommon)
4750 sec = bss_section;
4752 if (sec) {
4753 data_offset = sec->data_offset;
4754 data_offset = (data_offset + align - 1) & -align;
4755 addr = data_offset;
4756 /* very important to increment global pointer at this time
4757 because initializers themselves can create new initializers */
4758 data_offset += size;
4759 /* add padding if bound check */
4760 if (tcc_state->do_bounds_check)
4761 data_offset++;
4762 sec->data_offset = data_offset;
4763 /* allocate section space to put the data */
4764 if (sec->sh_type != SHT_NOBITS &&
4765 data_offset > sec->data_allocated)
4766 section_realloc(sec, data_offset);
4767 /* align section if needed */
4768 if (align > sec->sh_addralign)
4769 sec->sh_addralign = align;
4770 } else {
4771 addr = 0; /* avoid warning */
4774 if (v) {
4775 if (scope != VT_CONST || !sym) {
4776 sym = sym_push(v, type, r | VT_SYM, 0);
4778 /* update symbol definition */
4779 if (sec) {
4780 put_extern_sym(sym, sec, addr, size);
4781 } else {
4782 ElfW(Sym) *esym;
4783 /* put a common area */
4784 put_extern_sym(sym, NULL, align, size);
4785 /* XXX: find a nicer way */
4786 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
4787 esym->st_shndx = SHN_COMMON;
4789 } else {
4790 CValue cval;
4792 /* push global reference */
4793 sym = get_sym_ref(type, sec, addr, size);
4794 cval.ul = 0;
4795 vsetc(type, VT_CONST | VT_SYM, &cval);
4796 vtop->sym = sym;
4799 /* handles bounds now because the symbol must be defined
4800 before for the relocation */
4801 if (tcc_state->do_bounds_check) {
4802 unsigned long *bounds_ptr;
4804 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
4805 /* then add global bound info */
4806 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
4807 bounds_ptr[0] = 0; /* relocated */
4808 bounds_ptr[1] = size;
4811 if (has_init) {
4812 decl_initializer(type, sec, addr, 1, 0);
4813 /* restore parse state if needed */
4814 if (init_str.str) {
4815 tok_str_free(init_str.str);
4816 restore_parse_state(&saved_parse_state);
4819 no_alloc: ;
4822 void put_func_debug(Sym *sym)
4824 char buf[512];
4826 /* stabs info */
4827 /* XXX: we put here a dummy type */
4828 snprintf(buf, sizeof(buf), "%s:%c1",
4829 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
4830 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
4831 cur_text_section, sym->c);
4832 /* //gr gdb wants a line at the function */
4833 put_stabn(N_SLINE, 0, file->line_num, 0);
4834 last_ind = 0;
4835 last_line_num = 0;
4838 /* parse an old style function declaration list */
4839 /* XXX: check multiple parameter */
4840 static void func_decl_list(Sym *func_sym)
4842 AttributeDef ad;
4843 int v;
4844 Sym *s;
4845 CType btype, type;
4847 /* parse each declaration */
4848 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
4849 if (!parse_btype(&btype, &ad))
4850 expect("declaration list");
4851 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4852 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4853 tok == ';') {
4854 /* we accept no variable after */
4855 } else {
4856 for(;;) {
4857 type = btype;
4858 type_decl(&type, &ad, &v, TYPE_DIRECT);
4859 /* find parameter in function parameter list */
4860 s = func_sym->next;
4861 while (s != NULL) {
4862 if ((s->v & ~SYM_FIELD) == v)
4863 goto found;
4864 s = s->next;
4866 error("declaration for parameter '%s' but no such parameter",
4867 get_tok_str(v, NULL));
4868 found:
4869 /* check that no storage specifier except 'register' was given */
4870 if (type.t & VT_STORAGE)
4871 error("storage class specified for '%s'", get_tok_str(v, NULL));
4872 convert_parameter_type(&type);
4873 /* we can add the type (NOTE: it could be local to the function) */
4874 s->type = type;
4875 /* accept other parameters */
4876 if (tok == ',')
4877 next();
4878 else
4879 break;
4882 skip(';');
4886 /* parse a function defined by symbol 'sym' and generate its code in
4887 'cur_text_section' */
4888 static void gen_function(Sym *sym)
4890 int saved_nocode_wanted = nocode_wanted;
4891 nocode_wanted = 0;
4892 ind = cur_text_section->data_offset;
4893 /* NOTE: we patch the symbol size later */
4894 put_extern_sym(sym, cur_text_section, ind, 0);
4895 funcname = get_tok_str(sym->v, NULL);
4896 func_ind = ind;
4897 /* put debug symbol */
4898 if (tcc_state->do_debug)
4899 put_func_debug(sym);
4900 /* push a dummy symbol to enable local sym storage */
4901 sym_push2(&local_stack, SYM_FIELD, 0, 0);
4902 gfunc_prolog(&sym->type);
4903 rsym = 0;
4904 block(NULL, NULL, NULL, NULL, 0, 0);
4905 gsym(rsym);
4906 gfunc_epilog();
4907 cur_text_section->data_offset = ind;
4908 label_pop(&global_label_stack, NULL);
4909 sym_pop(&local_stack, NULL); /* reset local stack */
4910 /* end of function */
4911 /* patch symbol size */
4912 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
4913 ind - func_ind;
4914 if (tcc_state->do_debug) {
4915 put_stabn(N_FUN, 0, 0, ind - func_ind);
4917 /* It's better to crash than to generate wrong code */
4918 cur_text_section = NULL;
4919 funcname = ""; /* for safety */
4920 func_vt.t = VT_VOID; /* for safety */
4921 ind = 0; /* for safety */
4922 nocode_wanted = saved_nocode_wanted;
4925 static void gen_inline_functions(void)
4927 Sym *sym;
4928 int *str, inline_generated, i;
4929 struct InlineFunc *fn;
4931 /* iterate while inline function are referenced */
4932 for(;;) {
4933 inline_generated = 0;
4934 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4935 fn = tcc_state->inline_fns[i];
4936 sym = fn->sym;
4937 if (sym && sym->c) {
4938 /* the function was used: generate its code and
4939 convert it to a normal function */
4940 str = fn->token_str;
4941 fn->sym = NULL;
4942 if (file)
4943 strcpy(file->filename, fn->filename);
4944 sym->r = VT_SYM | VT_CONST;
4945 sym->type.t &= ~VT_INLINE;
4947 macro_ptr = str;
4948 next();
4949 cur_text_section = text_section;
4950 gen_function(sym);
4951 macro_ptr = NULL; /* fail safe */
4953 inline_generated = 1;
4956 if (!inline_generated)
4957 break;
4959 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4960 fn = tcc_state->inline_fns[i];
4961 str = fn->token_str;
4962 tok_str_free(str);
4964 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
4967 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4968 static void decl(int l)
4970 int v, has_init, r;
4971 CType type, btype;
4972 Sym *sym;
4973 AttributeDef ad;
4975 while (1) {
4976 if (!parse_btype(&btype, &ad)) {
4977 /* skip redundant ';' */
4978 /* XXX: find more elegant solution */
4979 if (tok == ';') {
4980 next();
4981 continue;
4983 if (l == VT_CONST &&
4984 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
4985 /* global asm block */
4986 asm_global_instr();
4987 continue;
4989 /* special test for old K&R protos without explicit int
4990 type. Only accepted when defining global data */
4991 if (l == VT_LOCAL || tok < TOK_DEFINE)
4992 break;
4993 btype.t = VT_INT;
4995 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4996 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4997 tok == ';') {
4998 /* we accept no variable after */
4999 next();
5000 continue;
5002 while (1) { /* iterate thru each declaration */
5003 type = btype;
5004 type_decl(&type, &ad, &v, TYPE_DIRECT);
5005 #if 0
5007 char buf[500];
5008 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5009 printf("type = '%s'\n", buf);
5011 #endif
5012 if ((type.t & VT_BTYPE) == VT_FUNC) {
5013 /* if old style function prototype, we accept a
5014 declaration list */
5015 sym = type.ref;
5016 if (sym->c == FUNC_OLD)
5017 func_decl_list(sym);
5020 if (tok == '{') {
5021 if (l == VT_LOCAL)
5022 error("cannot use local functions");
5023 if ((type.t & VT_BTYPE) != VT_FUNC)
5024 expect("function definition");
5026 /* reject abstract declarators in function definition */
5027 sym = type.ref;
5028 while ((sym = sym->next) != NULL)
5029 if (!(sym->v & ~SYM_FIELD))
5030 expect("identifier");
5032 /* XXX: cannot do better now: convert extern line to static inline */
5033 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5034 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5036 sym = sym_find(v);
5037 if (sym) {
5038 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5039 goto func_error1;
5041 r = sym->type.ref->r;
5042 /* use func_call from prototype if not defined */
5043 if (FUNC_CALL(r) != FUNC_CDECL
5044 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5045 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5047 /* use export from prototype */
5048 if (FUNC_EXPORT(r))
5049 FUNC_EXPORT(type.ref->r) = 1;
5051 /* use static from prototype */
5052 if (sym->type.t & VT_STATIC)
5053 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5055 if (!is_compatible_types(&sym->type, &type)) {
5056 func_error1:
5057 error("incompatible types for redefinition of '%s'",
5058 get_tok_str(v, NULL));
5060 /* if symbol is already defined, then put complete type */
5061 sym->type = type;
5062 } else {
5063 /* put function symbol */
5064 sym = global_identifier_push(v, type.t, 0);
5065 sym->type.ref = type.ref;
5068 /* static inline functions are just recorded as a kind
5069 of macro. Their code will be emitted at the end of
5070 the compilation unit only if they are used */
5071 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5072 (VT_INLINE | VT_STATIC)) {
5073 TokenString func_str;
5074 int block_level;
5075 struct InlineFunc *fn;
5076 const char *filename;
5078 tok_str_new(&func_str);
5080 block_level = 0;
5081 for(;;) {
5082 int t;
5083 if (tok == TOK_EOF)
5084 error("unexpected end of file");
5085 tok_str_add_tok(&func_str);
5086 t = tok;
5087 next();
5088 if (t == '{') {
5089 block_level++;
5090 } else if (t == '}') {
5091 block_level--;
5092 if (block_level == 0)
5093 break;
5096 tok_str_add(&func_str, -1);
5097 tok_str_add(&func_str, 0);
5098 filename = file ? file->filename : "";
5099 fn = tcc_malloc(sizeof *fn + strlen(filename));
5100 strcpy(fn->filename, filename);
5101 fn->sym = sym;
5102 fn->token_str = func_str.str;
5103 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5105 } else {
5106 /* compute text section */
5107 cur_text_section = ad.section;
5108 if (!cur_text_section)
5109 cur_text_section = text_section;
5110 sym->r = VT_SYM | VT_CONST;
5111 gen_function(sym);
5113 break;
5114 } else {
5115 if (btype.t & VT_TYPEDEF) {
5116 /* save typedefed type */
5117 /* XXX: test storage specifiers ? */
5118 sym = sym_push(v, &type, 0, 0);
5119 sym->type.t |= VT_TYPEDEF;
5120 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5121 /* external function definition */
5122 /* specific case for func_call attribute */
5123 if (ad.func_attr)
5124 type.ref->r = ad.func_attr;
5125 external_sym(v, &type, 0);
5126 } else {
5127 /* not lvalue if array */
5128 r = 0;
5129 if (!(type.t & VT_ARRAY))
5130 r |= lvalue_type(type.t);
5131 has_init = (tok == '=');
5132 if ((btype.t & VT_EXTERN) ||
5133 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5134 !has_init && l == VT_CONST && type.ref->c < 0)) {
5135 /* external variable */
5136 /* NOTE: as GCC, uninitialized global static
5137 arrays of null size are considered as
5138 extern */
5139 #ifdef TCC_TARGET_PE
5140 if (FUNC_IMPORT(ad.func_attr))
5141 type.t |= VT_IMPORT;
5142 #endif
5143 external_sym(v, &type, r);
5144 } else {
5145 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5146 if (type.t & VT_STATIC)
5147 r |= VT_CONST;
5148 else
5149 r |= l;
5150 if (has_init)
5151 next();
5152 decl_initializer_alloc(&type, &ad, r,
5153 has_init, v, l);
5156 if (tok != ',') {
5157 skip(';');
5158 break;
5160 next();