tccgen: propagate alignment from typedef
[tinycc.git] / tccgen.c
blob58d0d89d719fcd074940e20370099c30e05c96da
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 /* destination */
2023 vswap();
2024 vtop->type.t = VT_PTR;
2025 gaddrof();
2027 /* address of memcpy() */
2028 #ifdef TCC_ARM_EABI
2029 if(!(align & 7))
2030 vpush_global_sym(&func_old_type, TOK_memcpy8);
2031 else if(!(align & 3))
2032 vpush_global_sym(&func_old_type, TOK_memcpy4);
2033 else
2034 #endif
2035 vpush_global_sym(&func_old_type, TOK_memcpy);
2037 vswap();
2038 /* source */
2039 vpushv(vtop - 2);
2040 vtop->type.t = VT_PTR;
2041 gaddrof();
2042 /* type size */
2043 vpushi(size);
2044 gfunc_call(3);
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 ad->func_call = FUNC_CDECL;
2234 break;
2235 case TOK_STDCALL1:
2236 case TOK_STDCALL2:
2237 case TOK_STDCALL3:
2238 ad->func_call = 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 ad->func_call = FUNC_FASTCALL1 + n - 1;
2251 skip(')');
2252 break;
2253 case TOK_FASTCALL1:
2254 case TOK_FASTCALL2:
2255 case TOK_FASTCALL3:
2256 ad->func_call = FUNC_FASTCALLW;
2257 break;
2258 #endif
2259 case TOK_DLLEXPORT:
2260 ad->func_export = 1;
2261 break;
2262 case TOK_DLLIMPORT:
2263 ad->func_import = 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 if (s->r) {
2650 /* get attributes from typedef */
2651 if (0 == ad->aligned)
2652 ad->aligned = FUNC_ALIGN(s->r);
2653 if (0 == ad->func_call)
2654 ad->func_call = FUNC_CALL(s->r);
2655 ad->packed |= FUNC_PACKED(s->r);
2657 next();
2658 typespec_found = 1;
2659 break;
2661 type_found = 1;
2663 the_end:
2664 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2665 error("signed and unsigned modifier");
2666 if (tcc_state->char_is_unsigned) {
2667 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2668 t |= VT_UNSIGNED;
2670 t &= ~VT_SIGNED;
2672 /* long is never used as type */
2673 if ((t & VT_BTYPE) == VT_LONG)
2674 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2675 t = (t & ~VT_BTYPE) | VT_INT;
2676 #else
2677 t = (t & ~VT_BTYPE) | VT_LLONG;
2678 #endif
2679 type->t = t;
2680 return type_found;
2683 /* convert a function parameter type (array to pointer and function to
2684 function pointer) */
2685 static inline void convert_parameter_type(CType *pt)
2687 /* remove const and volatile qualifiers (XXX: const could be used
2688 to indicate a const function parameter */
2689 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2690 /* array must be transformed to pointer according to ANSI C */
2691 pt->t &= ~VT_ARRAY;
2692 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2693 mk_pointer(pt);
2697 static void post_type(CType *type, AttributeDef *ad)
2699 int n, l, t1, arg_size, align;
2700 Sym **plast, *s, *first;
2701 AttributeDef ad1;
2702 CType pt;
2704 if (tok == '(') {
2705 /* function declaration */
2706 next();
2707 l = 0;
2708 first = NULL;
2709 plast = &first;
2710 arg_size = 0;
2711 if (tok != ')') {
2712 for(;;) {
2713 /* read param name and compute offset */
2714 if (l != FUNC_OLD) {
2715 if (!parse_btype(&pt, &ad1)) {
2716 if (l) {
2717 error("invalid type");
2718 } else {
2719 l = FUNC_OLD;
2720 goto old_proto;
2723 l = FUNC_NEW;
2724 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2725 break;
2726 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2727 if ((pt.t & VT_BTYPE) == VT_VOID)
2728 error("parameter declared as void");
2729 arg_size += (type_size(&pt, &align) + 3) & ~3;
2730 } else {
2731 old_proto:
2732 n = tok;
2733 if (n < TOK_UIDENT)
2734 expect("identifier");
2735 pt.t = VT_INT;
2736 next();
2738 convert_parameter_type(&pt);
2739 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
2740 *plast = s;
2741 plast = &s->next;
2742 if (tok == ')')
2743 break;
2744 skip(',');
2745 if (l == FUNC_NEW && tok == TOK_DOTS) {
2746 l = FUNC_ELLIPSIS;
2747 next();
2748 break;
2752 /* if no parameters, then old type prototype */
2753 if (l == 0)
2754 l = FUNC_OLD;
2755 skip(')');
2756 t1 = type->t & VT_STORAGE;
2757 /* NOTE: const is ignored in returned type as it has a special
2758 meaning in gcc / C++ */
2759 type->t &= ~(VT_STORAGE | VT_CONSTANT);
2760 post_type(type, ad);
2761 /* we push a anonymous symbol which will contain the function prototype */
2762 ad->func_args = arg_size;
2763 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
2764 s->next = first;
2765 type->t = t1 | VT_FUNC;
2766 type->ref = s;
2767 } else if (tok == '[') {
2768 /* array definition */
2769 next();
2770 if (tok == TOK_RESTRICT1)
2771 next();
2772 n = -1;
2773 if (tok != ']') {
2774 n = expr_const();
2775 if (n < 0)
2776 error("invalid array size");
2778 skip(']');
2779 /* parse next post type */
2780 t1 = type->t & VT_STORAGE;
2781 type->t &= ~VT_STORAGE;
2782 post_type(type, ad);
2784 /* we push a anonymous symbol which will contain the array
2785 element type */
2786 s = sym_push(SYM_FIELD, type, 0, n);
2787 type->t = t1 | VT_ARRAY | VT_PTR;
2788 type->ref = s;
2792 /* Parse a type declaration (except basic type), and return the type
2793 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2794 expected. 'type' should contain the basic type. 'ad' is the
2795 attribute definition of the basic type. It can be modified by
2796 type_decl().
2798 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
2800 Sym *s;
2801 CType type1, *type2;
2802 int qualifiers;
2804 while (tok == '*') {
2805 qualifiers = 0;
2806 redo:
2807 next();
2808 switch(tok) {
2809 case TOK_CONST1:
2810 case TOK_CONST2:
2811 case TOK_CONST3:
2812 qualifiers |= VT_CONSTANT;
2813 goto redo;
2814 case TOK_VOLATILE1:
2815 case TOK_VOLATILE2:
2816 case TOK_VOLATILE3:
2817 qualifiers |= VT_VOLATILE;
2818 goto redo;
2819 case TOK_RESTRICT1:
2820 case TOK_RESTRICT2:
2821 case TOK_RESTRICT3:
2822 goto redo;
2824 mk_pointer(type);
2825 type->t |= qualifiers;
2828 /* XXX: clarify attribute handling */
2829 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2830 parse_attribute(ad);
2832 /* recursive type */
2833 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2834 type1.t = 0; /* XXX: same as int */
2835 if (tok == '(') {
2836 next();
2837 /* XXX: this is not correct to modify 'ad' at this point, but
2838 the syntax is not clear */
2839 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2840 parse_attribute(ad);
2841 type_decl(&type1, ad, v, td);
2842 skip(')');
2843 } else {
2844 /* type identifier */
2845 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
2846 *v = tok;
2847 next();
2848 } else {
2849 if (!(td & TYPE_ABSTRACT))
2850 expect("identifier");
2851 *v = 0;
2854 post_type(type, ad);
2855 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2856 parse_attribute(ad);
2857 if (!type1.t)
2858 return;
2859 /* append type at the end of type1 */
2860 type2 = &type1;
2861 for(;;) {
2862 s = type2->ref;
2863 type2 = &s->type;
2864 if (!type2->t) {
2865 *type2 = *type;
2866 break;
2869 *type = type1;
2872 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2873 static int lvalue_type(int t)
2875 int bt, r;
2876 r = VT_LVAL;
2877 bt = t & VT_BTYPE;
2878 if (bt == VT_BYTE || bt == VT_BOOL)
2879 r |= VT_LVAL_BYTE;
2880 else if (bt == VT_SHORT)
2881 r |= VT_LVAL_SHORT;
2882 else
2883 return r;
2884 if (t & VT_UNSIGNED)
2885 r |= VT_LVAL_UNSIGNED;
2886 return r;
2889 /* indirection with full error checking and bound check */
2890 static void indir(void)
2892 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
2893 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
2894 return;
2895 expect("pointer");
2897 if ((vtop->r & VT_LVAL) && !nocode_wanted)
2898 gv(RC_INT);
2899 vtop->type = *pointed_type(&vtop->type);
2900 /* Arrays and functions are never lvalues */
2901 if (!(vtop->type.t & VT_ARRAY)
2902 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
2903 vtop->r |= lvalue_type(vtop->type.t);
2904 /* if bound checking, the referenced pointer must be checked */
2905 if (tcc_state->do_bounds_check)
2906 vtop->r |= VT_MUSTBOUND;
2910 /* pass a parameter to a function and do type checking and casting */
2911 static void gfunc_param_typed(Sym *func, Sym *arg)
2913 int func_type;
2914 CType type;
2916 func_type = func->c;
2917 if (func_type == FUNC_OLD ||
2918 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
2919 /* default casting : only need to convert float to double */
2920 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
2921 type.t = VT_DOUBLE;
2922 gen_cast(&type);
2924 } else if (arg == NULL) {
2925 error("too many arguments to function");
2926 } else {
2927 type = arg->type;
2928 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
2929 gen_assign_cast(&type);
2933 /* parse an expression of the form '(type)' or '(expr)' and return its
2934 type */
2935 static void parse_expr_type(CType *type)
2937 int n;
2938 AttributeDef ad;
2940 skip('(');
2941 if (parse_btype(type, &ad)) {
2942 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2943 } else {
2944 expr_type(type);
2946 skip(')');
2949 static void parse_type(CType *type)
2951 AttributeDef ad;
2952 int n;
2954 if (!parse_btype(type, &ad)) {
2955 expect("type");
2957 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2960 static void vpush_tokc(int t)
2962 CType type;
2963 type.t = t;
2964 vsetc(&type, VT_CONST, &tokc);
2967 static void unary(void)
2969 int n, t, align, size, r;
2970 CType type;
2971 Sym *s;
2972 AttributeDef ad;
2974 /* XXX: GCC 2.95.3 does not generate a table although it should be
2975 better here */
2976 tok_next:
2977 switch(tok) {
2978 case TOK_EXTENSION:
2979 next();
2980 goto tok_next;
2981 case TOK_CINT:
2982 case TOK_CCHAR:
2983 case TOK_LCHAR:
2984 vpushi(tokc.i);
2985 next();
2986 break;
2987 case TOK_CUINT:
2988 vpush_tokc(VT_INT | VT_UNSIGNED);
2989 next();
2990 break;
2991 case TOK_CLLONG:
2992 vpush_tokc(VT_LLONG);
2993 next();
2994 break;
2995 case TOK_CULLONG:
2996 vpush_tokc(VT_LLONG | VT_UNSIGNED);
2997 next();
2998 break;
2999 case TOK_CFLOAT:
3000 vpush_tokc(VT_FLOAT);
3001 next();
3002 break;
3003 case TOK_CDOUBLE:
3004 vpush_tokc(VT_DOUBLE);
3005 next();
3006 break;
3007 case TOK_CLDOUBLE:
3008 vpush_tokc(VT_LDOUBLE);
3009 next();
3010 break;
3011 case TOK___FUNCTION__:
3012 if (!gnu_ext)
3013 goto tok_identifier;
3014 /* fall thru */
3015 case TOK___FUNC__:
3017 void *ptr;
3018 int len;
3019 /* special function name identifier */
3020 len = strlen(funcname) + 1;
3021 /* generate char[len] type */
3022 type.t = VT_BYTE;
3023 mk_pointer(&type);
3024 type.t |= VT_ARRAY;
3025 type.ref->c = len;
3026 vpush_ref(&type, data_section, data_section->data_offset, len);
3027 ptr = section_ptr_add(data_section, len);
3028 memcpy(ptr, funcname, len);
3029 next();
3031 break;
3032 case TOK_LSTR:
3033 #ifdef TCC_TARGET_PE
3034 t = VT_SHORT | VT_UNSIGNED;
3035 #else
3036 t = VT_INT;
3037 #endif
3038 goto str_init;
3039 case TOK_STR:
3040 /* string parsing */
3041 t = VT_BYTE;
3042 str_init:
3043 if (tcc_state->warn_write_strings)
3044 t |= VT_CONSTANT;
3045 type.t = t;
3046 mk_pointer(&type);
3047 type.t |= VT_ARRAY;
3048 memset(&ad, 0, sizeof(AttributeDef));
3049 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3050 break;
3051 case '(':
3052 next();
3053 /* cast ? */
3054 if (parse_btype(&type, &ad)) {
3055 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3056 skip(')');
3057 /* check ISOC99 compound literal */
3058 if (tok == '{') {
3059 /* data is allocated locally by default */
3060 if (global_expr)
3061 r = VT_CONST;
3062 else
3063 r = VT_LOCAL;
3064 /* all except arrays are lvalues */
3065 if (!(type.t & VT_ARRAY))
3066 r |= lvalue_type(type.t);
3067 memset(&ad, 0, sizeof(AttributeDef));
3068 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3069 } else {
3070 unary();
3071 gen_cast(&type);
3073 } else if (tok == '{') {
3074 /* save all registers */
3075 save_regs(0);
3076 /* statement expression : we do not accept break/continue
3077 inside as GCC does */
3078 block(NULL, NULL, NULL, NULL, 0, 1);
3079 skip(')');
3080 } else {
3081 gexpr();
3082 skip(')');
3084 break;
3085 case '*':
3086 next();
3087 unary();
3088 indir();
3089 break;
3090 case '&':
3091 next();
3092 unary();
3093 /* functions names must be treated as function pointers,
3094 except for unary '&' and sizeof. Since we consider that
3095 functions are not lvalues, we only have to handle it
3096 there and in function calls. */
3097 /* arrays can also be used although they are not lvalues */
3098 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3099 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3100 test_lvalue();
3101 mk_pointer(&vtop->type);
3102 gaddrof();
3103 break;
3104 case '!':
3105 next();
3106 unary();
3107 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3108 CType boolean;
3109 boolean.t = VT_BOOL;
3110 gen_cast(&boolean);
3111 vtop->c.i = !vtop->c.i;
3112 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3113 vtop->c.i = vtop->c.i ^ 1;
3114 else {
3115 save_regs(1);
3116 vseti(VT_JMP, gtst(1, 0));
3118 break;
3119 case '~':
3120 next();
3121 unary();
3122 vpushi(-1);
3123 gen_op('^');
3124 break;
3125 case '+':
3126 next();
3127 /* in order to force cast, we add zero */
3128 unary();
3129 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3130 error("pointer not accepted for unary plus");
3131 vpushi(0);
3132 gen_op('+');
3133 break;
3134 case TOK_SIZEOF:
3135 case TOK_ALIGNOF1:
3136 case TOK_ALIGNOF2:
3137 t = tok;
3138 next();
3139 if (tok == '(') {
3140 parse_expr_type(&type);
3141 } else {
3142 unary_type(&type);
3144 size = type_size(&type, &align);
3145 if (t == TOK_SIZEOF) {
3146 if (size < 0)
3147 error("sizeof applied to an incomplete type");
3148 vpushi(size);
3149 } else {
3150 vpushi(align);
3152 vtop->type.t |= VT_UNSIGNED;
3153 break;
3155 case TOK_builtin_types_compatible_p:
3157 CType type1, type2;
3158 next();
3159 skip('(');
3160 parse_type(&type1);
3161 skip(',');
3162 parse_type(&type2);
3163 skip(')');
3164 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3165 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3166 vpushi(is_compatible_types(&type1, &type2));
3168 break;
3169 case TOK_builtin_constant_p:
3171 int saved_nocode_wanted, res;
3172 next();
3173 skip('(');
3174 saved_nocode_wanted = nocode_wanted;
3175 nocode_wanted = 1;
3176 gexpr();
3177 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3178 vpop();
3179 nocode_wanted = saved_nocode_wanted;
3180 skip(')');
3181 vpushi(res);
3183 break;
3184 case TOK_builtin_frame_address:
3186 CType type;
3187 next();
3188 skip('(');
3189 if (tok != TOK_CINT) {
3190 error("__builtin_frame_address only takes integers");
3192 if (tokc.i != 0) {
3193 error("TCC only supports __builtin_frame_address(0)");
3195 next();
3196 skip(')');
3197 type.t = VT_VOID;
3198 mk_pointer(&type);
3199 vset(&type, VT_LOCAL, 0);
3201 break;
3202 #ifdef TCC_TARGET_X86_64
3203 case TOK_builtin_malloc:
3204 tok = TOK_malloc;
3205 goto tok_identifier;
3206 case TOK_builtin_free:
3207 tok = TOK_free;
3208 goto tok_identifier;
3209 #endif
3210 case TOK_INC:
3211 case TOK_DEC:
3212 t = tok;
3213 next();
3214 unary();
3215 inc(0, t);
3216 break;
3217 case '-':
3218 next();
3219 vpushi(0);
3220 unary();
3221 gen_op('-');
3222 break;
3223 case TOK_LAND:
3224 if (!gnu_ext)
3225 goto tok_identifier;
3226 next();
3227 /* allow to take the address of a label */
3228 if (tok < TOK_UIDENT)
3229 expect("label identifier");
3230 s = label_find(tok);
3231 if (!s) {
3232 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3233 } else {
3234 if (s->r == LABEL_DECLARED)
3235 s->r = LABEL_FORWARD;
3237 if (!s->type.t) {
3238 s->type.t = VT_VOID;
3239 mk_pointer(&s->type);
3240 s->type.t |= VT_STATIC;
3242 vset(&s->type, VT_CONST | VT_SYM, 0);
3243 vtop->sym = s;
3244 next();
3245 break;
3246 default:
3247 tok_identifier:
3248 t = tok;
3249 next();
3250 if (t < TOK_UIDENT)
3251 expect("identifier");
3252 s = sym_find(t);
3253 if (!s) {
3254 if (tok != '(')
3255 error("'%s' undeclared", get_tok_str(t, NULL));
3256 /* for simple function calls, we tolerate undeclared
3257 external reference to int() function */
3258 if (tcc_state->warn_implicit_function_declaration)
3259 warning("implicit declaration of function '%s'",
3260 get_tok_str(t, NULL));
3261 s = external_global_sym(t, &func_old_type, 0);
3263 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3264 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3265 /* if referencing an inline function, then we generate a
3266 symbol to it if not already done. It will have the
3267 effect to generate code for it at the end of the
3268 compilation unit. Inline function as always
3269 generated in the text section. */
3270 if (!s->c)
3271 put_extern_sym(s, text_section, 0, 0);
3272 r = VT_SYM | VT_CONST;
3273 } else {
3274 r = s->r;
3276 vset(&s->type, r, s->c);
3277 /* if forward reference, we must point to s */
3278 if (vtop->r & VT_SYM) {
3279 vtop->sym = s;
3280 vtop->c.ul = 0;
3282 break;
3285 /* post operations */
3286 while (1) {
3287 if (tok == TOK_INC || tok == TOK_DEC) {
3288 inc(1, tok);
3289 next();
3290 } else if (tok == '.' || tok == TOK_ARROW) {
3291 int qualifiers;
3292 /* field */
3293 if (tok == TOK_ARROW)
3294 indir();
3295 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3296 test_lvalue();
3297 gaddrof();
3298 next();
3299 /* expect pointer on structure */
3300 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3301 expect("struct or union");
3302 s = vtop->type.ref;
3303 /* find field */
3304 tok |= SYM_FIELD;
3305 while ((s = s->next) != NULL) {
3306 if (s->v == tok)
3307 break;
3309 if (!s)
3310 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3311 /* add field offset to pointer */
3312 vtop->type = char_pointer_type; /* change type to 'char *' */
3313 vpushi(s->c);
3314 gen_op('+');
3315 /* change type to field type, and set to lvalue */
3316 vtop->type = s->type;
3317 vtop->type.t |= qualifiers;
3318 /* an array is never an lvalue */
3319 if (!(vtop->type.t & VT_ARRAY)) {
3320 vtop->r |= lvalue_type(vtop->type.t);
3321 /* if bound checking, the referenced pointer must be checked */
3322 if (tcc_state->do_bounds_check)
3323 vtop->r |= VT_MUSTBOUND;
3325 next();
3326 } else if (tok == '[') {
3327 next();
3328 gexpr();
3329 gen_op('+');
3330 indir();
3331 skip(']');
3332 } else if (tok == '(') {
3333 SValue ret;
3334 Sym *sa;
3335 int nb_args;
3337 /* function call */
3338 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3339 /* pointer test (no array accepted) */
3340 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3341 vtop->type = *pointed_type(&vtop->type);
3342 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3343 goto error_func;
3344 } else {
3345 error_func:
3346 expect("function pointer");
3348 } else {
3349 vtop->r &= ~VT_LVAL; /* no lvalue */
3351 /* get return type */
3352 s = vtop->type.ref;
3353 next();
3354 sa = s->next; /* first parameter */
3355 nb_args = 0;
3356 ret.r2 = VT_CONST;
3357 /* compute first implicit argument if a structure is returned */
3358 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3359 /* get some space for the returned structure */
3360 size = type_size(&s->type, &align);
3361 loc = (loc - size) & -align;
3362 ret.type = s->type;
3363 ret.r = VT_LOCAL | VT_LVAL;
3364 /* pass it as 'int' to avoid structure arg passing
3365 problems */
3366 vseti(VT_LOCAL, loc);
3367 ret.c = vtop->c;
3368 nb_args++;
3369 } else {
3370 ret.type = s->type;
3371 /* return in register */
3372 if (is_float(ret.type.t)) {
3373 ret.r = reg_fret(ret.type.t);
3374 } else {
3375 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3376 ret.r2 = REG_LRET;
3377 ret.r = REG_IRET;
3379 ret.c.i = 0;
3381 if (tok != ')') {
3382 for(;;) {
3383 expr_eq();
3384 gfunc_param_typed(s, sa);
3385 nb_args++;
3386 if (sa)
3387 sa = sa->next;
3388 if (tok == ')')
3389 break;
3390 skip(',');
3393 if (sa)
3394 error("too few arguments to function");
3395 skip(')');
3396 if (!nocode_wanted) {
3397 gfunc_call(nb_args);
3398 } else {
3399 vtop -= (nb_args + 1);
3401 /* return value */
3402 vsetc(&ret.type, ret.r, &ret.c);
3403 vtop->r2 = ret.r2;
3404 } else {
3405 break;
3410 static void uneq(void)
3412 int t;
3414 unary();
3415 if (tok == '=' ||
3416 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3417 tok == TOK_A_XOR || tok == TOK_A_OR ||
3418 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3419 test_lvalue();
3420 t = tok;
3421 next();
3422 if (t == '=') {
3423 expr_eq();
3424 } else {
3425 vdup();
3426 expr_eq();
3427 gen_op(t & 0x7f);
3429 vstore();
3433 static void expr_prod(void)
3435 int t;
3437 uneq();
3438 while (tok == '*' || tok == '/' || tok == '%') {
3439 t = tok;
3440 next();
3441 uneq();
3442 gen_op(t);
3446 static void expr_sum(void)
3448 int t;
3450 expr_prod();
3451 while (tok == '+' || tok == '-') {
3452 t = tok;
3453 next();
3454 expr_prod();
3455 gen_op(t);
3459 static void expr_shift(void)
3461 int t;
3463 expr_sum();
3464 while (tok == TOK_SHL || tok == TOK_SAR) {
3465 t = tok;
3466 next();
3467 expr_sum();
3468 gen_op(t);
3472 static void expr_cmp(void)
3474 int t;
3476 expr_shift();
3477 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3478 tok == TOK_ULT || tok == TOK_UGE) {
3479 t = tok;
3480 next();
3481 expr_shift();
3482 gen_op(t);
3486 static void expr_cmpeq(void)
3488 int t;
3490 expr_cmp();
3491 while (tok == TOK_EQ || tok == TOK_NE) {
3492 t = tok;
3493 next();
3494 expr_cmp();
3495 gen_op(t);
3499 static void expr_and(void)
3501 expr_cmpeq();
3502 while (tok == '&') {
3503 next();
3504 expr_cmpeq();
3505 gen_op('&');
3509 static void expr_xor(void)
3511 expr_and();
3512 while (tok == '^') {
3513 next();
3514 expr_and();
3515 gen_op('^');
3519 static void expr_or(void)
3521 expr_xor();
3522 while (tok == '|') {
3523 next();
3524 expr_xor();
3525 gen_op('|');
3529 /* XXX: fix this mess */
3530 static void expr_land_const(void)
3532 expr_or();
3533 while (tok == TOK_LAND) {
3534 next();
3535 expr_or();
3536 gen_op(TOK_LAND);
3540 /* XXX: fix this mess */
3541 static void expr_lor_const(void)
3543 expr_land_const();
3544 while (tok == TOK_LOR) {
3545 next();
3546 expr_land_const();
3547 gen_op(TOK_LOR);
3551 /* only used if non constant */
3552 static void expr_land(void)
3554 int t;
3556 expr_or();
3557 if (tok == TOK_LAND) {
3558 t = 0;
3559 save_regs(1);
3560 for(;;) {
3561 t = gtst(1, t);
3562 if (tok != TOK_LAND) {
3563 vseti(VT_JMPI, t);
3564 break;
3566 next();
3567 expr_or();
3572 static void expr_lor(void)
3574 int t;
3576 expr_land();
3577 if (tok == TOK_LOR) {
3578 t = 0;
3579 save_regs(1);
3580 for(;;) {
3581 t = gtst(0, t);
3582 if (tok != TOK_LOR) {
3583 vseti(VT_JMP, t);
3584 break;
3586 next();
3587 expr_land();
3592 /* XXX: better constant handling */
3593 static void expr_eq(void)
3595 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3596 SValue sv;
3597 CType type, type1, type2;
3599 if (const_wanted) {
3600 expr_lor_const();
3601 if (tok == '?') {
3602 CType boolean;
3603 int c;
3604 boolean.t = VT_BOOL;
3605 vdup();
3606 gen_cast(&boolean);
3607 c = vtop->c.i;
3608 vpop();
3609 next();
3610 if (tok != ':' || !gnu_ext) {
3611 vpop();
3612 gexpr();
3614 if (!c)
3615 vpop();
3616 skip(':');
3617 expr_eq();
3618 if (c)
3619 vpop();
3621 } else {
3622 expr_lor();
3623 if (tok == '?') {
3624 next();
3625 if (vtop != vstack) {
3626 /* needed to avoid having different registers saved in
3627 each branch */
3628 if (is_float(vtop->type.t)) {
3629 rc = RC_FLOAT;
3630 #ifdef TCC_TARGET_X86_64
3631 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3632 rc = RC_ST0;
3634 #endif
3636 else
3637 rc = RC_INT;
3638 gv(rc);
3639 save_regs(1);
3641 if (tok == ':' && gnu_ext) {
3642 gv_dup();
3643 tt = gtst(1, 0);
3644 } else {
3645 tt = gtst(1, 0);
3646 gexpr();
3648 type1 = vtop->type;
3649 sv = *vtop; /* save value to handle it later */
3650 vtop--; /* no vpop so that FP stack is not flushed */
3651 skip(':');
3652 u = gjmp(0);
3653 gsym(tt);
3654 expr_eq();
3655 type2 = vtop->type;
3657 t1 = type1.t;
3658 bt1 = t1 & VT_BTYPE;
3659 t2 = type2.t;
3660 bt2 = t2 & VT_BTYPE;
3661 /* cast operands to correct type according to ISOC rules */
3662 if (is_float(bt1) || is_float(bt2)) {
3663 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3664 type.t = VT_LDOUBLE;
3665 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3666 type.t = VT_DOUBLE;
3667 } else {
3668 type.t = VT_FLOAT;
3670 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3671 /* cast to biggest op */
3672 type.t = VT_LLONG;
3673 /* convert to unsigned if it does not fit in a long long */
3674 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3675 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3676 type.t |= VT_UNSIGNED;
3677 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3678 /* XXX: test pointer compatibility */
3679 type = type1;
3680 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3681 /* XXX: test function pointer compatibility */
3682 type = type1;
3683 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3684 /* XXX: test structure compatibility */
3685 type = type1;
3686 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3687 /* NOTE: as an extension, we accept void on only one side */
3688 type.t = VT_VOID;
3689 } else {
3690 /* integer operations */
3691 type.t = VT_INT;
3692 /* convert to unsigned if it does not fit in an integer */
3693 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3694 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3695 type.t |= VT_UNSIGNED;
3698 /* now we convert second operand */
3699 gen_cast(&type);
3700 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3701 gaddrof();
3702 rc = RC_INT;
3703 if (is_float(type.t)) {
3704 rc = RC_FLOAT;
3705 #ifdef TCC_TARGET_X86_64
3706 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3707 rc = RC_ST0;
3709 #endif
3710 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3711 /* for long longs, we use fixed registers to avoid having
3712 to handle a complicated move */
3713 rc = RC_IRET;
3716 r2 = gv(rc);
3717 /* this is horrible, but we must also convert first
3718 operand */
3719 tt = gjmp(0);
3720 gsym(u);
3721 /* put again first value and cast it */
3722 *vtop = sv;
3723 gen_cast(&type);
3724 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3725 gaddrof();
3726 r1 = gv(rc);
3727 move_reg(r2, r1);
3728 vtop->r = r2;
3729 gsym(tt);
3734 static void gexpr(void)
3736 while (1) {
3737 expr_eq();
3738 if (tok != ',')
3739 break;
3740 vpop();
3741 next();
3745 /* parse an expression and return its type without any side effect. */
3746 static void expr_type(CType *type)
3748 int saved_nocode_wanted;
3750 saved_nocode_wanted = nocode_wanted;
3751 nocode_wanted = 1;
3752 gexpr();
3753 *type = vtop->type;
3754 vpop();
3755 nocode_wanted = saved_nocode_wanted;
3758 /* parse a unary expression and return its type without any side
3759 effect. */
3760 static void unary_type(CType *type)
3762 int a;
3764 a = nocode_wanted;
3765 nocode_wanted = 1;
3766 unary();
3767 *type = vtop->type;
3768 vpop();
3769 nocode_wanted = a;
3772 /* parse a constant expression and return value in vtop. */
3773 static void expr_const1(void)
3775 int a;
3776 a = const_wanted;
3777 const_wanted = 1;
3778 expr_eq();
3779 const_wanted = a;
3782 /* parse an integer constant and return its value. */
3783 static int expr_const(void)
3785 int c;
3786 expr_const1();
3787 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
3788 expect("constant expression");
3789 c = vtop->c.i;
3790 vpop();
3791 return c;
3794 /* return the label token if current token is a label, otherwise
3795 return zero */
3796 static int is_label(void)
3798 int last_tok;
3800 /* fast test first */
3801 if (tok < TOK_UIDENT)
3802 return 0;
3803 /* no need to save tokc because tok is an identifier */
3804 last_tok = tok;
3805 next();
3806 if (tok == ':') {
3807 next();
3808 return last_tok;
3809 } else {
3810 unget_tok(last_tok);
3811 return 0;
3815 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
3816 int case_reg, int is_expr)
3818 int a, b, c, d;
3819 Sym *s;
3821 /* generate line number info */
3822 if (tcc_state->do_debug &&
3823 (last_line_num != file->line_num || last_ind != ind)) {
3824 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
3825 last_ind = ind;
3826 last_line_num = file->line_num;
3829 if (is_expr) {
3830 /* default return value is (void) */
3831 vpushi(0);
3832 vtop->type.t = VT_VOID;
3835 if (tok == TOK_IF) {
3836 /* if test */
3837 next();
3838 skip('(');
3839 gexpr();
3840 skip(')');
3841 a = gtst(1, 0);
3842 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3843 c = tok;
3844 if (c == TOK_ELSE) {
3845 next();
3846 d = gjmp(0);
3847 gsym(a);
3848 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3849 gsym(d); /* patch else jmp */
3850 } else
3851 gsym(a);
3852 } else if (tok == TOK_WHILE) {
3853 next();
3854 d = ind;
3855 skip('(');
3856 gexpr();
3857 skip(')');
3858 a = gtst(1, 0);
3859 b = 0;
3860 block(&a, &b, case_sym, def_sym, case_reg, 0);
3861 gjmp_addr(d);
3862 gsym(a);
3863 gsym_addr(b, d);
3864 } else if (tok == '{') {
3865 Sym *llabel;
3867 next();
3868 /* record local declaration stack position */
3869 s = local_stack;
3870 llabel = local_label_stack;
3871 /* handle local labels declarations */
3872 if (tok == TOK_LABEL) {
3873 next();
3874 for(;;) {
3875 if (tok < TOK_UIDENT)
3876 expect("label identifier");
3877 label_push(&local_label_stack, tok, LABEL_DECLARED);
3878 next();
3879 if (tok == ',') {
3880 next();
3881 } else {
3882 skip(';');
3883 break;
3887 while (tok != '}') {
3888 decl(VT_LOCAL);
3889 if (tok != '}') {
3890 if (is_expr)
3891 vpop();
3892 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
3895 /* pop locally defined labels */
3896 label_pop(&local_label_stack, llabel);
3897 /* pop locally defined symbols */
3898 if(is_expr) {
3899 /* XXX: this solution makes only valgrind happy...
3900 triggered by gcc.c-torture/execute/20000917-1.c */
3901 Sym *p;
3902 switch(vtop->type.t & VT_BTYPE) {
3903 case VT_PTR:
3904 case VT_STRUCT:
3905 case VT_ENUM:
3906 case VT_FUNC:
3907 for(p=vtop->type.ref;p;p=p->prev)
3908 if(p->prev==s)
3909 error("unsupported expression type");
3912 sym_pop(&local_stack, s);
3913 next();
3914 } else if (tok == TOK_RETURN) {
3915 next();
3916 if (tok != ';') {
3917 gexpr();
3918 gen_assign_cast(&func_vt);
3919 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
3920 CType type;
3921 /* if returning structure, must copy it to implicit
3922 first pointer arg location */
3923 #ifdef TCC_ARM_EABI
3924 int align, size;
3925 size = type_size(&func_vt,&align);
3926 if(size <= 4)
3928 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
3929 && (align & 3))
3931 int addr;
3932 loc = (loc - size) & -4;
3933 addr = loc;
3934 type = func_vt;
3935 vset(&type, VT_LOCAL | VT_LVAL, addr);
3936 vswap();
3937 vstore();
3938 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
3940 vtop->type = int_type;
3941 gv(RC_IRET);
3942 } else {
3943 #endif
3944 type = func_vt;
3945 mk_pointer(&type);
3946 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
3947 indir();
3948 vswap();
3949 /* copy structure value to pointer */
3950 vstore();
3951 #ifdef TCC_ARM_EABI
3953 #endif
3954 } else if (is_float(func_vt.t)) {
3955 gv(rc_fret(func_vt.t));
3956 } else {
3957 gv(RC_IRET);
3959 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3961 skip(';');
3962 rsym = gjmp(rsym); /* jmp */
3963 } else if (tok == TOK_BREAK) {
3964 /* compute jump */
3965 if (!bsym)
3966 error("cannot break");
3967 *bsym = gjmp(*bsym);
3968 next();
3969 skip(';');
3970 } else if (tok == TOK_CONTINUE) {
3971 /* compute jump */
3972 if (!csym)
3973 error("cannot continue");
3974 *csym = gjmp(*csym);
3975 next();
3976 skip(';');
3977 } else if (tok == TOK_FOR) {
3978 int e;
3979 next();
3980 skip('(');
3981 if (tok != ';') {
3982 gexpr();
3983 vpop();
3985 skip(';');
3986 d = ind;
3987 c = ind;
3988 a = 0;
3989 b = 0;
3990 if (tok != ';') {
3991 gexpr();
3992 a = gtst(1, 0);
3994 skip(';');
3995 if (tok != ')') {
3996 e = gjmp(0);
3997 c = ind;
3998 gexpr();
3999 vpop();
4000 gjmp_addr(d);
4001 gsym(e);
4003 skip(')');
4004 block(&a, &b, case_sym, def_sym, case_reg, 0);
4005 gjmp_addr(c);
4006 gsym(a);
4007 gsym_addr(b, c);
4008 } else
4009 if (tok == TOK_DO) {
4010 next();
4011 a = 0;
4012 b = 0;
4013 d = ind;
4014 block(&a, &b, case_sym, def_sym, case_reg, 0);
4015 skip(TOK_WHILE);
4016 skip('(');
4017 gsym(b);
4018 gexpr();
4019 c = gtst(0, 0);
4020 gsym_addr(c, d);
4021 skip(')');
4022 gsym(a);
4023 skip(';');
4024 } else
4025 if (tok == TOK_SWITCH) {
4026 next();
4027 skip('(');
4028 gexpr();
4029 /* XXX: other types than integer */
4030 case_reg = gv(RC_INT);
4031 vpop();
4032 skip(')');
4033 a = 0;
4034 b = gjmp(0); /* jump to first case */
4035 c = 0;
4036 block(&a, csym, &b, &c, case_reg, 0);
4037 /* if no default, jmp after switch */
4038 if (c == 0)
4039 c = ind;
4040 /* default label */
4041 gsym_addr(b, c);
4042 /* break label */
4043 gsym(a);
4044 } else
4045 if (tok == TOK_CASE) {
4046 int v1, v2;
4047 if (!case_sym)
4048 expect("switch");
4049 next();
4050 v1 = expr_const();
4051 v2 = v1;
4052 if (gnu_ext && tok == TOK_DOTS) {
4053 next();
4054 v2 = expr_const();
4055 if (v2 < v1)
4056 warning("empty case range");
4058 /* since a case is like a label, we must skip it with a jmp */
4059 b = gjmp(0);
4060 gsym(*case_sym);
4061 vseti(case_reg, 0);
4062 vpushi(v1);
4063 if (v1 == v2) {
4064 gen_op(TOK_EQ);
4065 *case_sym = gtst(1, 0);
4066 } else {
4067 gen_op(TOK_GE);
4068 *case_sym = gtst(1, 0);
4069 vseti(case_reg, 0);
4070 vpushi(v2);
4071 gen_op(TOK_LE);
4072 *case_sym = gtst(1, *case_sym);
4074 gsym(b);
4075 skip(':');
4076 is_expr = 0;
4077 goto block_after_label;
4078 } else
4079 if (tok == TOK_DEFAULT) {
4080 next();
4081 skip(':');
4082 if (!def_sym)
4083 expect("switch");
4084 if (*def_sym)
4085 error("too many 'default'");
4086 *def_sym = ind;
4087 is_expr = 0;
4088 goto block_after_label;
4089 } else
4090 if (tok == TOK_GOTO) {
4091 next();
4092 if (tok == '*' && gnu_ext) {
4093 /* computed goto */
4094 next();
4095 gexpr();
4096 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4097 expect("pointer");
4098 ggoto();
4099 } else if (tok >= TOK_UIDENT) {
4100 s = label_find(tok);
4101 /* put forward definition if needed */
4102 if (!s) {
4103 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4104 } else {
4105 if (s->r == LABEL_DECLARED)
4106 s->r = LABEL_FORWARD;
4108 /* label already defined */
4109 if (s->r & LABEL_FORWARD)
4110 s->jnext = gjmp(s->jnext);
4111 else
4112 gjmp_addr(s->jnext);
4113 next();
4114 } else {
4115 expect("label identifier");
4117 skip(';');
4118 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4119 asm_instr();
4120 } else {
4121 b = is_label();
4122 if (b) {
4123 /* label case */
4124 s = label_find(b);
4125 if (s) {
4126 if (s->r == LABEL_DEFINED)
4127 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4128 gsym(s->jnext);
4129 s->r = LABEL_DEFINED;
4130 } else {
4131 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4133 s->jnext = ind;
4134 /* we accept this, but it is a mistake */
4135 block_after_label:
4136 if (tok == '}') {
4137 warning("deprecated use of label at end of compound statement");
4138 } else {
4139 if (is_expr)
4140 vpop();
4141 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4143 } else {
4144 /* expression case */
4145 if (tok != ';') {
4146 if (is_expr) {
4147 vpop();
4148 gexpr();
4149 } else {
4150 gexpr();
4151 vpop();
4154 skip(';');
4159 /* t is the array or struct type. c is the array or struct
4160 address. cur_index/cur_field is the pointer to the current
4161 value. 'size_only' is true if only size info is needed (only used
4162 in arrays) */
4163 static void decl_designator(CType *type, Section *sec, unsigned long c,
4164 int *cur_index, Sym **cur_field,
4165 int size_only)
4167 Sym *s, *f;
4168 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4169 CType type1;
4171 notfirst = 0;
4172 elem_size = 0;
4173 nb_elems = 1;
4174 if (gnu_ext && (l = is_label()) != 0)
4175 goto struct_field;
4176 while (tok == '[' || tok == '.') {
4177 if (tok == '[') {
4178 if (!(type->t & VT_ARRAY))
4179 expect("array type");
4180 s = type->ref;
4181 next();
4182 index = expr_const();
4183 if (index < 0 || (s->c >= 0 && index >= s->c))
4184 expect("invalid index");
4185 if (tok == TOK_DOTS && gnu_ext) {
4186 next();
4187 index_last = expr_const();
4188 if (index_last < 0 ||
4189 (s->c >= 0 && index_last >= s->c) ||
4190 index_last < index)
4191 expect("invalid index");
4192 } else {
4193 index_last = index;
4195 skip(']');
4196 if (!notfirst)
4197 *cur_index = index_last;
4198 type = pointed_type(type);
4199 elem_size = type_size(type, &align);
4200 c += index * elem_size;
4201 /* NOTE: we only support ranges for last designator */
4202 nb_elems = index_last - index + 1;
4203 if (nb_elems != 1) {
4204 notfirst = 1;
4205 break;
4207 } else {
4208 next();
4209 l = tok;
4210 next();
4211 struct_field:
4212 if ((type->t & VT_BTYPE) != VT_STRUCT)
4213 expect("struct/union type");
4214 s = type->ref;
4215 l |= SYM_FIELD;
4216 f = s->next;
4217 while (f) {
4218 if (f->v == l)
4219 break;
4220 f = f->next;
4222 if (!f)
4223 expect("field");
4224 if (!notfirst)
4225 *cur_field = f;
4226 /* XXX: fix this mess by using explicit storage field */
4227 type1 = f->type;
4228 type1.t |= (type->t & ~VT_TYPE);
4229 type = &type1;
4230 c += f->c;
4232 notfirst = 1;
4234 if (notfirst) {
4235 if (tok == '=') {
4236 next();
4237 } else {
4238 if (!gnu_ext)
4239 expect("=");
4241 } else {
4242 if (type->t & VT_ARRAY) {
4243 index = *cur_index;
4244 type = pointed_type(type);
4245 c += index * type_size(type, &align);
4246 } else {
4247 f = *cur_field;
4248 if (!f)
4249 error("too many field init");
4250 /* XXX: fix this mess by using explicit storage field */
4251 type1 = f->type;
4252 type1.t |= (type->t & ~VT_TYPE);
4253 type = &type1;
4254 c += f->c;
4257 decl_initializer(type, sec, c, 0, size_only);
4259 /* XXX: make it more general */
4260 if (!size_only && nb_elems > 1) {
4261 unsigned long c_end;
4262 uint8_t *src, *dst;
4263 int i;
4265 if (!sec)
4266 error("range init not supported yet for dynamic storage");
4267 c_end = c + nb_elems * elem_size;
4268 if (c_end > sec->data_allocated)
4269 section_realloc(sec, c_end);
4270 src = sec->data + c;
4271 dst = src;
4272 for(i = 1; i < nb_elems; i++) {
4273 dst += elem_size;
4274 memcpy(dst, src, elem_size);
4279 #define EXPR_VAL 0
4280 #define EXPR_CONST 1
4281 #define EXPR_ANY 2
4283 /* store a value or an expression directly in global data or in local array */
4284 static void init_putv(CType *type, Section *sec, unsigned long c,
4285 int v, int expr_type)
4287 int saved_global_expr, bt, bit_pos, bit_size;
4288 void *ptr;
4289 unsigned long long bit_mask;
4290 CType dtype;
4292 switch(expr_type) {
4293 case EXPR_VAL:
4294 vpushi(v);
4295 break;
4296 case EXPR_CONST:
4297 /* compound literals must be allocated globally in this case */
4298 saved_global_expr = global_expr;
4299 global_expr = 1;
4300 expr_const1();
4301 global_expr = saved_global_expr;
4302 /* NOTE: symbols are accepted */
4303 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4304 error("initializer element is not constant");
4305 break;
4306 case EXPR_ANY:
4307 expr_eq();
4308 break;
4311 dtype = *type;
4312 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4314 if (sec) {
4315 /* XXX: not portable */
4316 /* XXX: generate error if incorrect relocation */
4317 gen_assign_cast(&dtype);
4318 bt = type->t & VT_BTYPE;
4319 /* we'll write at most 12 bytes */
4320 if (c + 12 > sec->data_allocated) {
4321 section_realloc(sec, c + 12);
4323 ptr = sec->data + c;
4324 /* XXX: make code faster ? */
4325 if (!(type->t & VT_BITFIELD)) {
4326 bit_pos = 0;
4327 bit_size = 32;
4328 bit_mask = -1LL;
4329 } else {
4330 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4331 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4332 bit_mask = (1LL << bit_size) - 1;
4334 if ((vtop->r & VT_SYM) &&
4335 (bt == VT_BYTE ||
4336 bt == VT_SHORT ||
4337 bt == VT_DOUBLE ||
4338 bt == VT_LDOUBLE ||
4339 bt == VT_LLONG ||
4340 (bt == VT_INT && bit_size != 32)))
4341 error("initializer element is not computable at load time");
4342 switch(bt) {
4343 case VT_BOOL:
4344 vtop->c.i = (vtop->c.i != 0);
4345 case VT_BYTE:
4346 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4347 break;
4348 case VT_SHORT:
4349 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4350 break;
4351 case VT_DOUBLE:
4352 *(double *)ptr = vtop->c.d;
4353 break;
4354 case VT_LDOUBLE:
4355 *(long double *)ptr = vtop->c.ld;
4356 break;
4357 case VT_LLONG:
4358 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4359 break;
4360 default:
4361 if (vtop->r & VT_SYM) {
4362 greloc(sec, vtop->sym, c, R_DATA_PTR);
4364 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4365 break;
4367 vtop--;
4368 } else {
4369 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4370 vswap();
4371 vstore();
4372 vpop();
4376 /* put zeros for variable based init */
4377 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4379 if (sec) {
4380 /* nothing to do because globals are already set to zero */
4381 } else {
4382 vpush_global_sym(&func_old_type, TOK_memset);
4383 vseti(VT_LOCAL, c);
4384 vpushi(0);
4385 vpushi(size);
4386 gfunc_call(3);
4390 /* 't' contains the type and storage info. 'c' is the offset of the
4391 object in section 'sec'. If 'sec' is NULL, it means stack based
4392 allocation. 'first' is true if array '{' must be read (multi
4393 dimension implicit array init handling). 'size_only' is true if
4394 size only evaluation is wanted (only for arrays). */
4395 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4396 int first, int size_only)
4398 int index, array_length, n, no_oblock, nb, parlevel, i;
4399 int size1, align1, expr_type;
4400 Sym *s, *f;
4401 CType *t1;
4403 if (type->t & VT_ARRAY) {
4404 s = type->ref;
4405 n = s->c;
4406 array_length = 0;
4407 t1 = pointed_type(type);
4408 size1 = type_size(t1, &align1);
4410 no_oblock = 1;
4411 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4412 tok == '{') {
4413 skip('{');
4414 no_oblock = 0;
4417 /* only parse strings here if correct type (otherwise: handle
4418 them as ((w)char *) expressions */
4419 if ((tok == TOK_LSTR &&
4420 #ifdef TCC_TARGET_PE
4421 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4422 #else
4423 (t1->t & VT_BTYPE) == VT_INT
4424 #endif
4425 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4426 while (tok == TOK_STR || tok == TOK_LSTR) {
4427 int cstr_len, ch;
4428 CString *cstr;
4430 cstr = tokc.cstr;
4431 /* compute maximum number of chars wanted */
4432 if (tok == TOK_STR)
4433 cstr_len = cstr->size;
4434 else
4435 cstr_len = cstr->size / sizeof(nwchar_t);
4436 cstr_len--;
4437 nb = cstr_len;
4438 if (n >= 0 && nb > (n - array_length))
4439 nb = n - array_length;
4440 if (!size_only) {
4441 if (cstr_len > nb)
4442 warning("initializer-string for array is too long");
4443 /* in order to go faster for common case (char
4444 string in global variable, we handle it
4445 specifically */
4446 if (sec && tok == TOK_STR && size1 == 1) {
4447 memcpy(sec->data + c + array_length, cstr->data, nb);
4448 } else {
4449 for(i=0;i<nb;i++) {
4450 if (tok == TOK_STR)
4451 ch = ((unsigned char *)cstr->data)[i];
4452 else
4453 ch = ((nwchar_t *)cstr->data)[i];
4454 init_putv(t1, sec, c + (array_length + i) * size1,
4455 ch, EXPR_VAL);
4459 array_length += nb;
4460 next();
4462 /* only add trailing zero if enough storage (no
4463 warning in this case since it is standard) */
4464 if (n < 0 || array_length < n) {
4465 if (!size_only) {
4466 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4468 array_length++;
4470 } else {
4471 index = 0;
4472 while (tok != '}') {
4473 decl_designator(type, sec, c, &index, NULL, size_only);
4474 if (n >= 0 && index >= n)
4475 error("index too large");
4476 /* must put zero in holes (note that doing it that way
4477 ensures that it even works with designators) */
4478 if (!size_only && array_length < index) {
4479 init_putz(t1, sec, c + array_length * size1,
4480 (index - array_length) * size1);
4482 index++;
4483 if (index > array_length)
4484 array_length = index;
4485 /* special test for multi dimensional arrays (may not
4486 be strictly correct if designators are used at the
4487 same time) */
4488 if (index >= n && no_oblock)
4489 break;
4490 if (tok == '}')
4491 break;
4492 skip(',');
4495 if (!no_oblock)
4496 skip('}');
4497 /* put zeros at the end */
4498 if (!size_only && n >= 0 && array_length < n) {
4499 init_putz(t1, sec, c + array_length * size1,
4500 (n - array_length) * size1);
4502 /* patch type size if needed */
4503 if (n < 0)
4504 s->c = array_length;
4505 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4506 (sec || !first || tok == '{')) {
4507 int par_count;
4509 /* NOTE: the previous test is a specific case for automatic
4510 struct/union init */
4511 /* XXX: union needs only one init */
4513 /* XXX: this test is incorrect for local initializers
4514 beginning with ( without {. It would be much more difficult
4515 to do it correctly (ideally, the expression parser should
4516 be used in all cases) */
4517 par_count = 0;
4518 if (tok == '(') {
4519 AttributeDef ad1;
4520 CType type1;
4521 next();
4522 while (tok == '(') {
4523 par_count++;
4524 next();
4526 if (!parse_btype(&type1, &ad1))
4527 expect("cast");
4528 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4529 #if 0
4530 if (!is_assignable_types(type, &type1))
4531 error("invalid type for cast");
4532 #endif
4533 skip(')');
4535 no_oblock = 1;
4536 if (first || tok == '{') {
4537 skip('{');
4538 no_oblock = 0;
4540 s = type->ref;
4541 f = s->next;
4542 array_length = 0;
4543 index = 0;
4544 n = s->c;
4545 while (tok != '}') {
4546 decl_designator(type, sec, c, NULL, &f, size_only);
4547 index = f->c;
4548 if (!size_only && array_length < index) {
4549 init_putz(type, sec, c + array_length,
4550 index - array_length);
4552 index = index + type_size(&f->type, &align1);
4553 if (index > array_length)
4554 array_length = index;
4556 /* gr: skip fields from same union - ugly. */
4557 while (f->next) {
4558 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4559 /* test for same offset */
4560 if (f->next->c != f->c)
4561 break;
4562 /* if yes, test for bitfield shift */
4563 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4564 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4565 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4566 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4567 if (bit_pos_1 != bit_pos_2)
4568 break;
4570 f = f->next;
4573 f = f->next;
4574 if (no_oblock && f == NULL)
4575 break;
4576 if (tok == '}')
4577 break;
4578 skip(',');
4580 /* put zeros at the end */
4581 if (!size_only && array_length < n) {
4582 init_putz(type, sec, c + array_length,
4583 n - array_length);
4585 if (!no_oblock)
4586 skip('}');
4587 while (par_count) {
4588 skip(')');
4589 par_count--;
4591 } else if (tok == '{') {
4592 next();
4593 decl_initializer(type, sec, c, first, size_only);
4594 skip('}');
4595 } else if (size_only) {
4596 /* just skip expression */
4597 parlevel = 0;
4598 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4599 tok != -1) {
4600 if (tok == '(')
4601 parlevel++;
4602 else if (tok == ')')
4603 parlevel--;
4604 next();
4606 } else {
4607 /* currently, we always use constant expression for globals
4608 (may change for scripting case) */
4609 expr_type = EXPR_CONST;
4610 if (!sec)
4611 expr_type = EXPR_ANY;
4612 init_putv(type, sec, c, 0, expr_type);
4616 /* parse an initializer for type 't' if 'has_init' is non zero, and
4617 allocate space in local or global data space ('r' is either
4618 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4619 variable 'v' of scope 'scope' is declared before initializers are
4620 parsed. If 'v' is zero, then a reference to the new object is put
4621 in the value stack. If 'has_init' is 2, a special parsing is done
4622 to handle string constants. */
4623 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4624 int has_init, int v, int scope)
4626 int size, align, addr, data_offset;
4627 int level;
4628 ParseState saved_parse_state = {0};
4629 TokenString init_str;
4630 Section *sec;
4632 size = type_size(type, &align);
4633 /* If unknown size, we must evaluate it before
4634 evaluating initializers because
4635 initializers can generate global data too
4636 (e.g. string pointers or ISOC99 compound
4637 literals). It also simplifies local
4638 initializers handling */
4639 tok_str_new(&init_str);
4640 if (size < 0) {
4641 if (!has_init)
4642 error("unknown type size");
4643 /* get all init string */
4644 if (has_init == 2) {
4645 /* only get strings */
4646 while (tok == TOK_STR || tok == TOK_LSTR) {
4647 tok_str_add_tok(&init_str);
4648 next();
4650 } else {
4651 level = 0;
4652 while (level > 0 || (tok != ',' && tok != ';')) {
4653 if (tok < 0)
4654 error("unexpected end of file in initializer");
4655 tok_str_add_tok(&init_str);
4656 if (tok == '{')
4657 level++;
4658 else if (tok == '}') {
4659 level--;
4660 if (level <= 0) {
4661 next();
4662 break;
4665 next();
4668 tok_str_add(&init_str, -1);
4669 tok_str_add(&init_str, 0);
4671 /* compute size */
4672 save_parse_state(&saved_parse_state);
4674 macro_ptr = init_str.str;
4675 next();
4676 decl_initializer(type, NULL, 0, 1, 1);
4677 /* prepare second initializer parsing */
4678 macro_ptr = init_str.str;
4679 next();
4681 /* if still unknown size, error */
4682 size = type_size(type, &align);
4683 if (size < 0)
4684 error("unknown type size");
4686 /* take into account specified alignment if bigger */
4687 if (ad->aligned) {
4688 if (ad->aligned > align)
4689 align = ad->aligned;
4690 } else if (ad->packed) {
4691 align = 1;
4693 if ((r & VT_VALMASK) == VT_LOCAL) {
4694 sec = NULL;
4695 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4696 loc--;
4697 loc = (loc - size) & -align;
4698 addr = loc;
4699 /* handles bounds */
4700 /* XXX: currently, since we do only one pass, we cannot track
4701 '&' operators, so we add only arrays */
4702 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4703 unsigned long *bounds_ptr;
4704 /* add padding between regions */
4705 loc--;
4706 /* then add local bound info */
4707 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4708 bounds_ptr[0] = addr;
4709 bounds_ptr[1] = size;
4711 if (v) {
4712 /* local variable */
4713 sym_push(v, type, r, addr);
4714 } else {
4715 /* push local reference */
4716 vset(type, r, addr);
4718 } else {
4719 Sym *sym;
4721 sym = NULL;
4722 if (v && scope == VT_CONST) {
4723 /* see if the symbol was already defined */
4724 sym = sym_find(v);
4725 if (sym) {
4726 if (!is_compatible_types(&sym->type, type))
4727 error("incompatible types for redefinition of '%s'",
4728 get_tok_str(v, NULL));
4729 if (sym->type.t & VT_EXTERN) {
4730 /* if the variable is extern, it was not allocated */
4731 sym->type.t &= ~VT_EXTERN;
4732 /* set array size if it was ommited in extern
4733 declaration */
4734 if ((sym->type.t & VT_ARRAY) &&
4735 sym->type.ref->c < 0 &&
4736 type->ref->c >= 0)
4737 sym->type.ref->c = type->ref->c;
4738 } else {
4739 /* we accept several definitions of the same
4740 global variable. this is tricky, because we
4741 must play with the SHN_COMMON type of the symbol */
4742 /* XXX: should check if the variable was already
4743 initialized. It is incorrect to initialized it
4744 twice */
4745 /* no init data, we won't add more to the symbol */
4746 if (!has_init)
4747 goto no_alloc;
4752 /* allocate symbol in corresponding section */
4753 sec = ad->section;
4754 if (!sec) {
4755 if (has_init)
4756 sec = data_section;
4757 else if (tcc_state->nocommon)
4758 sec = bss_section;
4760 if (sec) {
4761 data_offset = sec->data_offset;
4762 data_offset = (data_offset + align - 1) & -align;
4763 addr = data_offset;
4764 /* very important to increment global pointer at this time
4765 because initializers themselves can create new initializers */
4766 data_offset += size;
4767 /* add padding if bound check */
4768 if (tcc_state->do_bounds_check)
4769 data_offset++;
4770 sec->data_offset = data_offset;
4771 /* allocate section space to put the data */
4772 if (sec->sh_type != SHT_NOBITS &&
4773 data_offset > sec->data_allocated)
4774 section_realloc(sec, data_offset);
4775 /* align section if needed */
4776 if (align > sec->sh_addralign)
4777 sec->sh_addralign = align;
4778 } else {
4779 addr = 0; /* avoid warning */
4782 if (v) {
4783 if (scope != VT_CONST || !sym) {
4784 sym = sym_push(v, type, r | VT_SYM, 0);
4786 /* update symbol definition */
4787 if (sec) {
4788 put_extern_sym(sym, sec, addr, size);
4789 } else {
4790 ElfW(Sym) *esym;
4791 /* put a common area */
4792 put_extern_sym(sym, NULL, align, size);
4793 /* XXX: find a nicer way */
4794 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
4795 esym->st_shndx = SHN_COMMON;
4797 } else {
4798 CValue cval;
4800 /* push global reference */
4801 sym = get_sym_ref(type, sec, addr, size);
4802 cval.ul = 0;
4803 vsetc(type, VT_CONST | VT_SYM, &cval);
4804 vtop->sym = sym;
4807 /* handles bounds now because the symbol must be defined
4808 before for the relocation */
4809 if (tcc_state->do_bounds_check) {
4810 unsigned long *bounds_ptr;
4812 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
4813 /* then add global bound info */
4814 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
4815 bounds_ptr[0] = 0; /* relocated */
4816 bounds_ptr[1] = size;
4819 if (has_init) {
4820 decl_initializer(type, sec, addr, 1, 0);
4821 /* restore parse state if needed */
4822 if (init_str.str) {
4823 tok_str_free(init_str.str);
4824 restore_parse_state(&saved_parse_state);
4827 no_alloc: ;
4830 void put_func_debug(Sym *sym)
4832 char buf[512];
4834 /* stabs info */
4835 /* XXX: we put here a dummy type */
4836 snprintf(buf, sizeof(buf), "%s:%c1",
4837 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
4838 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
4839 cur_text_section, sym->c);
4840 /* //gr gdb wants a line at the function */
4841 put_stabn(N_SLINE, 0, file->line_num, 0);
4842 last_ind = 0;
4843 last_line_num = 0;
4846 /* parse an old style function declaration list */
4847 /* XXX: check multiple parameter */
4848 static void func_decl_list(Sym *func_sym)
4850 AttributeDef ad;
4851 int v;
4852 Sym *s;
4853 CType btype, type;
4855 /* parse each declaration */
4856 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
4857 if (!parse_btype(&btype, &ad))
4858 expect("declaration list");
4859 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4860 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4861 tok == ';') {
4862 /* we accept no variable after */
4863 } else {
4864 for(;;) {
4865 type = btype;
4866 type_decl(&type, &ad, &v, TYPE_DIRECT);
4867 /* find parameter in function parameter list */
4868 s = func_sym->next;
4869 while (s != NULL) {
4870 if ((s->v & ~SYM_FIELD) == v)
4871 goto found;
4872 s = s->next;
4874 error("declaration for parameter '%s' but no such parameter",
4875 get_tok_str(v, NULL));
4876 found:
4877 /* check that no storage specifier except 'register' was given */
4878 if (type.t & VT_STORAGE)
4879 error("storage class specified for '%s'", get_tok_str(v, NULL));
4880 convert_parameter_type(&type);
4881 /* we can add the type (NOTE: it could be local to the function) */
4882 s->type = type;
4883 /* accept other parameters */
4884 if (tok == ',')
4885 next();
4886 else
4887 break;
4890 skip(';');
4894 /* parse a function defined by symbol 'sym' and generate its code in
4895 'cur_text_section' */
4896 static void gen_function(Sym *sym)
4898 int saved_nocode_wanted = nocode_wanted;
4899 nocode_wanted = 0;
4900 ind = cur_text_section->data_offset;
4901 /* NOTE: we patch the symbol size later */
4902 put_extern_sym(sym, cur_text_section, ind, 0);
4903 funcname = get_tok_str(sym->v, NULL);
4904 func_ind = ind;
4905 /* put debug symbol */
4906 if (tcc_state->do_debug)
4907 put_func_debug(sym);
4908 /* push a dummy symbol to enable local sym storage */
4909 sym_push2(&local_stack, SYM_FIELD, 0, 0);
4910 gfunc_prolog(&sym->type);
4911 rsym = 0;
4912 block(NULL, NULL, NULL, NULL, 0, 0);
4913 gsym(rsym);
4914 gfunc_epilog();
4915 cur_text_section->data_offset = ind;
4916 label_pop(&global_label_stack, NULL);
4917 sym_pop(&local_stack, NULL); /* reset local stack */
4918 /* end of function */
4919 /* patch symbol size */
4920 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
4921 ind - func_ind;
4922 if (tcc_state->do_debug) {
4923 put_stabn(N_FUN, 0, 0, ind - func_ind);
4925 /* It's better to crash than to generate wrong code */
4926 cur_text_section = NULL;
4927 funcname = ""; /* for safety */
4928 func_vt.t = VT_VOID; /* for safety */
4929 ind = 0; /* for safety */
4930 nocode_wanted = saved_nocode_wanted;
4933 static void gen_inline_functions(void)
4935 Sym *sym;
4936 int *str, inline_generated, i;
4937 struct InlineFunc *fn;
4939 /* iterate while inline function are referenced */
4940 for(;;) {
4941 inline_generated = 0;
4942 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4943 fn = tcc_state->inline_fns[i];
4944 sym = fn->sym;
4945 if (sym && sym->c) {
4946 /* the function was used: generate its code and
4947 convert it to a normal function */
4948 str = fn->token_str;
4949 fn->sym = NULL;
4950 if (file)
4951 strcpy(file->filename, fn->filename);
4952 sym->r = VT_SYM | VT_CONST;
4953 sym->type.t &= ~VT_INLINE;
4955 macro_ptr = str;
4956 next();
4957 cur_text_section = text_section;
4958 gen_function(sym);
4959 macro_ptr = NULL; /* fail safe */
4961 inline_generated = 1;
4964 if (!inline_generated)
4965 break;
4967 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4968 fn = tcc_state->inline_fns[i];
4969 str = fn->token_str;
4970 tok_str_free(str);
4972 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
4975 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4976 static void decl(int l)
4978 int v, has_init, r;
4979 CType type, btype;
4980 Sym *sym;
4981 AttributeDef ad;
4983 while (1) {
4984 if (!parse_btype(&btype, &ad)) {
4985 /* skip redundant ';' */
4986 /* XXX: find more elegant solution */
4987 if (tok == ';') {
4988 next();
4989 continue;
4991 if (l == VT_CONST &&
4992 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
4993 /* global asm block */
4994 asm_global_instr();
4995 continue;
4997 /* special test for old K&R protos without explicit int
4998 type. Only accepted when defining global data */
4999 if (l == VT_LOCAL || tok < TOK_DEFINE)
5000 break;
5001 btype.t = VT_INT;
5003 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5004 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5005 tok == ';') {
5006 /* we accept no variable after */
5007 next();
5008 continue;
5010 while (1) { /* iterate thru each declaration */
5011 type = btype;
5012 type_decl(&type, &ad, &v, TYPE_DIRECT);
5013 #if 0
5015 char buf[500];
5016 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5017 printf("type = '%s'\n", buf);
5019 #endif
5020 if ((type.t & VT_BTYPE) == VT_FUNC) {
5021 /* if old style function prototype, we accept a
5022 declaration list */
5023 sym = type.ref;
5024 if (sym->c == FUNC_OLD)
5025 func_decl_list(sym);
5028 if (tok == '{') {
5029 if (l == VT_LOCAL)
5030 error("cannot use local functions");
5031 if ((type.t & VT_BTYPE) != VT_FUNC)
5032 expect("function definition");
5034 /* reject abstract declarators in function definition */
5035 sym = type.ref;
5036 while ((sym = sym->next) != NULL)
5037 if (!(sym->v & ~SYM_FIELD))
5038 expect("identifier");
5040 /* XXX: cannot do better now: convert extern line to static inline */
5041 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5042 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5044 sym = sym_find(v);
5045 if (sym) {
5046 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5047 goto func_error1;
5049 r = sym->type.ref->r;
5050 /* use func_call from prototype if not defined */
5051 if (FUNC_CALL(r) != FUNC_CDECL
5052 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5053 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5055 /* use export from prototype */
5056 if (FUNC_EXPORT(r))
5057 FUNC_EXPORT(type.ref->r) = 1;
5059 /* use static from prototype */
5060 if (sym->type.t & VT_STATIC)
5061 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5063 if (!is_compatible_types(&sym->type, &type)) {
5064 func_error1:
5065 error("incompatible types for redefinition of '%s'",
5066 get_tok_str(v, NULL));
5068 /* if symbol is already defined, then put complete type */
5069 sym->type = type;
5070 } else {
5071 /* put function symbol */
5072 sym = global_identifier_push(v, type.t, 0);
5073 sym->type.ref = type.ref;
5076 /* static inline functions are just recorded as a kind
5077 of macro. Their code will be emitted at the end of
5078 the compilation unit only if they are used */
5079 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5080 (VT_INLINE | VT_STATIC)) {
5081 TokenString func_str;
5082 int block_level;
5083 struct InlineFunc *fn;
5084 const char *filename;
5086 tok_str_new(&func_str);
5088 block_level = 0;
5089 for(;;) {
5090 int t;
5091 if (tok == TOK_EOF)
5092 error("unexpected end of file");
5093 tok_str_add_tok(&func_str);
5094 t = tok;
5095 next();
5096 if (t == '{') {
5097 block_level++;
5098 } else if (t == '}') {
5099 block_level--;
5100 if (block_level == 0)
5101 break;
5104 tok_str_add(&func_str, -1);
5105 tok_str_add(&func_str, 0);
5106 filename = file ? file->filename : "";
5107 fn = tcc_malloc(sizeof *fn + strlen(filename));
5108 strcpy(fn->filename, filename);
5109 fn->sym = sym;
5110 fn->token_str = func_str.str;
5111 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5113 } else {
5114 /* compute text section */
5115 cur_text_section = ad.section;
5116 if (!cur_text_section)
5117 cur_text_section = text_section;
5118 sym->r = VT_SYM | VT_CONST;
5119 gen_function(sym);
5121 break;
5122 } else {
5123 if (btype.t & VT_TYPEDEF) {
5124 /* save typedefed type */
5125 /* XXX: test storage specifiers ? */
5126 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5127 sym->type.t |= VT_TYPEDEF;
5128 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5129 /* external function definition */
5130 /* specific case for func_call attribute */
5131 type.ref->r = INT_ATTR(&ad);
5132 external_sym(v, &type, 0);
5133 } else {
5134 /* not lvalue if array */
5135 r = 0;
5136 if (!(type.t & VT_ARRAY))
5137 r |= lvalue_type(type.t);
5138 has_init = (tok == '=');
5139 if ((btype.t & VT_EXTERN) ||
5140 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5141 !has_init && l == VT_CONST && type.ref->c < 0)) {
5142 /* external variable */
5143 /* NOTE: as GCC, uninitialized global static
5144 arrays of null size are considered as
5145 extern */
5146 #ifdef TCC_TARGET_PE
5147 if (ad.func_import)
5148 type.t |= VT_IMPORT;
5149 #endif
5150 external_sym(v, &type, r);
5151 } else {
5152 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5153 if (type.t & VT_STATIC)
5154 r |= VT_CONST;
5155 else
5156 r |= l;
5157 if (has_init)
5158 next();
5159 decl_initializer_alloc(&type, &ad, r,
5160 has_init, v, l);
5163 if (tok != ',') {
5164 skip(';');
5165 break;
5167 next();