win32: enable bounds checker & exception handler
[tinycc.git] / tccgen.c
blob9866c55e9b191c80cf0745863aa9544e8b733bcc
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 ctype.ref = 0;
65 cval.ull = v;
66 vsetc(&ctype, VT_CONST, &cval);
69 /* Return a static symbol pointing to a section */
70 static Sym *get_sym_ref(CType *type, Section *sec,
71 unsigned long offset, unsigned long size)
73 int v;
74 Sym *sym;
76 v = anon_sym++;
77 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
78 sym->type.ref = type->ref;
79 sym->r = VT_CONST | VT_SYM;
80 put_extern_sym(sym, sec, offset, size);
81 return sym;
84 /* push a reference to a section offset by adding a dummy symbol */
85 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
87 CValue cval;
89 cval.ul = 0;
90 vsetc(type, VT_CONST | VT_SYM, &cval);
91 vtop->sym = get_sym_ref(type, sec, offset, size);
94 /* define a new external reference to a symbol 'v' of type 'u' */
95 static Sym *external_global_sym(int v, CType *type, int r)
97 Sym *s;
99 s = sym_find(v);
100 if (!s) {
101 /* push forward reference */
102 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
103 s->type.ref = type->ref;
104 s->r = r | VT_CONST | VT_SYM;
106 return s;
109 /* define a new external reference to a symbol 'v' of type 'u' */
110 static Sym *external_sym(int v, CType *type, int r)
112 Sym *s;
114 s = sym_find(v);
115 if (!s) {
116 /* push forward reference */
117 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
118 s->type.t |= VT_EXTERN;
119 } else if (s->type.ref == func_old_type.ref) {
120 s->type.ref = type->ref;
121 s->r = r | VT_CONST | VT_SYM;
122 s->type.t |= VT_EXTERN;
123 } else if (!is_compatible_types(&s->type, type)) {
124 error("incompatible types for redefinition of '%s'",
125 get_tok_str(v, NULL));
127 return s;
130 /* push a reference to global symbol v */
131 static void vpush_global_sym(CType *type, int v)
133 Sym *sym;
134 CValue cval;
136 sym = external_global_sym(v, type, 0);
137 cval.ul = 0;
138 vsetc(type, VT_CONST | VT_SYM, &cval);
139 vtop->sym = sym;
142 void vset(CType *type, int r, int v)
144 CValue cval;
146 cval.i = v;
147 vsetc(type, r, &cval);
150 void vseti(int r, int v)
152 CType type;
153 type.t = VT_INT;
154 type.ref = 0;
155 vset(&type, r, v);
158 void vswap(void)
160 SValue tmp;
162 tmp = vtop[0];
163 vtop[0] = vtop[-1];
164 vtop[-1] = tmp;
167 void vpushv(SValue *v)
169 if (vtop >= vstack + (VSTACK_SIZE - 1))
170 error("memory full");
171 vtop++;
172 *vtop = *v;
175 void vdup(void)
177 vpushv(vtop);
180 /* save r to the memory stack, and mark it as being free */
181 void save_reg(int r)
183 int l, saved, size, align;
184 SValue *p, sv;
185 CType *type;
187 /* modify all stack values */
188 saved = 0;
189 l = 0;
190 for(p=vstack;p<=vtop;p++) {
191 if ((p->r & VT_VALMASK) == r ||
192 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
193 /* must save value on stack if not already done */
194 if (!saved) {
195 /* NOTE: must reload 'r' because r might be equal to r2 */
196 r = p->r & VT_VALMASK;
197 /* store register in the stack */
198 type = &p->type;
199 if ((p->r & VT_LVAL) ||
200 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
201 #ifdef TCC_TARGET_X86_64
202 type = &char_pointer_type;
203 #else
204 type = &int_type;
205 #endif
206 size = type_size(type, &align);
207 loc = (loc - size) & -align;
208 sv.type.t = type->t;
209 sv.r = VT_LOCAL | VT_LVAL;
210 sv.c.ul = loc;
211 store(r, &sv);
212 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
213 /* x86 specific: need to pop fp register ST0 if saved */
214 if (r == TREG_ST0) {
215 o(0xd8dd); /* fstp %st(0) */
217 #endif
218 #ifndef TCC_TARGET_X86_64
219 /* special long long case */
220 if ((type->t & VT_BTYPE) == VT_LLONG) {
221 sv.c.ul += 4;
222 store(p->r2, &sv);
224 #endif
225 l = loc;
226 saved = 1;
228 /* mark that stack entry as being saved on the stack */
229 if (p->r & VT_LVAL) {
230 /* also clear the bounded flag because the
231 relocation address of the function was stored in
232 p->c.ul */
233 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
234 } else {
235 p->r = lvalue_type(p->type.t) | VT_LOCAL;
237 p->r2 = VT_CONST;
238 p->c.ul = l;
243 /* find a register of class 'rc2' with at most one reference on stack.
244 * If none, call get_reg(rc) */
245 int get_reg_ex(int rc, int rc2)
247 int r;
248 SValue *p;
250 for(r=0;r<NB_REGS;r++) {
251 if (reg_classes[r] & rc2) {
252 int n;
253 n=0;
254 for(p = vstack; p <= vtop; p++) {
255 if ((p->r & VT_VALMASK) == r ||
256 (p->r2 & VT_VALMASK) == r)
257 n++;
259 if (n <= 1)
260 return r;
263 return get_reg(rc);
266 /* find a free register of class 'rc'. If none, save one register */
267 int get_reg(int rc)
269 int r;
270 SValue *p;
272 /* find a free register */
273 for(r=0;r<NB_REGS;r++) {
274 if (reg_classes[r] & rc) {
275 for(p=vstack;p<=vtop;p++) {
276 if ((p->r & VT_VALMASK) == r ||
277 (p->r2 & VT_VALMASK) == r)
278 goto notfound;
280 return r;
282 notfound: ;
285 /* no register left : free the first one on the stack (VERY
286 IMPORTANT to start from the bottom to ensure that we don't
287 spill registers used in gen_opi()) */
288 for(p=vstack;p<=vtop;p++) {
289 r = p->r & VT_VALMASK;
290 if (r < VT_CONST && (reg_classes[r] & rc))
291 goto save_found;
292 /* also look at second register (if long long) */
293 r = p->r2 & VT_VALMASK;
294 if (r < VT_CONST && (reg_classes[r] & rc)) {
295 save_found:
296 save_reg(r);
297 return r;
300 /* Should never comes here */
301 return -1;
304 /* save registers up to (vtop - n) stack entry */
305 void save_regs(int n)
307 int r;
308 SValue *p, *p1;
309 p1 = vtop - n;
310 for(p = vstack;p <= p1; p++) {
311 r = p->r & VT_VALMASK;
312 if (r < VT_CONST) {
313 save_reg(r);
318 /* move register 's' to 'r', and flush previous value of r to memory
319 if needed */
320 void move_reg(int r, int s)
322 SValue sv;
324 if (r != s) {
325 save_reg(r);
326 sv.type.t = VT_INT;
327 sv.r = s;
328 sv.c.ul = 0;
329 load(r, &sv);
333 /* get address of vtop (vtop MUST BE an lvalue) */
334 void gaddrof(void)
336 vtop->r &= ~VT_LVAL;
337 /* tricky: if saved lvalue, then we can go back to lvalue */
338 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
339 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
342 #ifdef CONFIG_TCC_BCHECK
343 /* generate lvalue bound code */
344 void gbound(void)
346 int lval_type;
347 CType type1;
349 vtop->r &= ~VT_MUSTBOUND;
350 /* if lvalue, then use checking code before dereferencing */
351 if (vtop->r & VT_LVAL) {
352 /* if not VT_BOUNDED value, then make one */
353 if (!(vtop->r & VT_BOUNDED)) {
354 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
355 /* must save type because we must set it to int to get pointer */
356 type1 = vtop->type;
357 vtop->type.t = VT_INT;
358 gaddrof();
359 vpushi(0);
360 gen_bounded_ptr_add();
361 vtop->r |= lval_type;
362 vtop->type = type1;
364 /* then check for dereferencing */
365 gen_bounded_ptr_deref();
368 #endif
370 /* store vtop a register belonging to class 'rc'. lvalues are
371 converted to values. Cannot be used if cannot be converted to
372 register value (such as structures). */
373 int gv(int rc)
375 int r, rc2, bit_pos, bit_size, size, align, i;
377 /* NOTE: get_reg can modify vstack[] */
378 if (vtop->type.t & VT_BITFIELD) {
379 CType type;
380 int bits = 32;
381 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
382 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
383 /* remove bit field info to avoid loops */
384 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
385 /* cast to int to propagate signedness in following ops */
386 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
387 type.t = VT_LLONG;
388 bits = 64;
389 } else
390 type.t = VT_INT;
391 if((vtop->type.t & VT_UNSIGNED) ||
392 (vtop->type.t & VT_BTYPE) == VT_BOOL)
393 type.t |= VT_UNSIGNED;
394 gen_cast(&type);
395 /* generate shifts */
396 vpushi(bits - (bit_pos + bit_size));
397 gen_op(TOK_SHL);
398 vpushi(bits - bit_size);
399 /* NOTE: transformed to SHR if unsigned */
400 gen_op(TOK_SAR);
401 r = gv(rc);
402 } else {
403 if (is_float(vtop->type.t) &&
404 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
405 Sym *sym;
406 int *ptr;
407 unsigned long offset;
408 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
409 CValue check;
410 #endif
412 /* XXX: unify with initializers handling ? */
413 /* CPUs usually cannot use float constants, so we store them
414 generically in data segment */
415 size = type_size(&vtop->type, &align);
416 offset = (data_section->data_offset + align - 1) & -align;
417 data_section->data_offset = offset;
418 /* XXX: not portable yet */
419 #if defined(__i386__) || defined(__x86_64__)
420 /* Zero pad x87 tenbyte long doubles */
421 if (size == LDOUBLE_SIZE)
422 vtop->c.tab[2] &= 0xffff;
423 #endif
424 ptr = section_ptr_add(data_section, size);
425 size = size >> 2;
426 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
427 check.d = 1;
428 if(check.tab[0])
429 for(i=0;i<size;i++)
430 ptr[i] = vtop->c.tab[size-1-i];
431 else
432 #endif
433 for(i=0;i<size;i++)
434 ptr[i] = vtop->c.tab[i];
435 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
436 vtop->r |= VT_LVAL | VT_SYM;
437 vtop->sym = sym;
438 vtop->c.ul = 0;
440 #ifdef CONFIG_TCC_BCHECK
441 if (vtop->r & VT_MUSTBOUND)
442 gbound();
443 #endif
445 r = vtop->r & VT_VALMASK;
446 rc2 = RC_INT;
447 if (rc == RC_IRET)
448 rc2 = RC_LRET;
449 /* need to reload if:
450 - constant
451 - lvalue (need to dereference pointer)
452 - already a register, but not in the right class */
453 if (r >= VT_CONST
454 || (vtop->r & VT_LVAL)
455 || !(reg_classes[r] & rc)
456 #ifndef TCC_TARGET_X86_64
457 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
458 #endif
461 r = get_reg(rc);
462 #ifndef TCC_TARGET_X86_64
463 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
464 int r2;
465 unsigned long long ll;
466 /* two register type load : expand to two words
467 temporarily */
468 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
469 /* load constant */
470 ll = vtop->c.ull;
471 vtop->c.ui = ll; /* first word */
472 load(r, vtop);
473 vtop->r = r; /* save register value */
474 vpushi(ll >> 32); /* second word */
475 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
476 (vtop->r & VT_LVAL)) {
477 /* We do not want to modifier the long long
478 pointer here, so the safest (and less
479 efficient) is to save all the other registers
480 in the stack. XXX: totally inefficient. */
481 save_regs(1);
482 /* load from memory */
483 load(r, vtop);
484 vdup();
485 vtop[-1].r = r; /* save register value */
486 /* increment pointer to get second word */
487 vtop->type.t = VT_INT;
488 gaddrof();
489 vpushi(4);
490 gen_op('+');
491 vtop->r |= VT_LVAL;
492 } else {
493 /* move registers */
494 load(r, vtop);
495 vdup();
496 vtop[-1].r = r; /* save register value */
497 vtop->r = vtop[-1].r2;
499 /* allocate second register */
500 r2 = get_reg(rc2);
501 load(r2, vtop);
502 vpop();
503 /* write second register */
504 vtop->r2 = r2;
505 } else
506 #endif
507 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
508 int t1, t;
509 /* lvalue of scalar type : need to use lvalue type
510 because of possible cast */
511 t = vtop->type.t;
512 t1 = t;
513 /* compute memory access type */
514 if (vtop->r & VT_LVAL_BYTE)
515 t = VT_BYTE;
516 else if (vtop->r & VT_LVAL_SHORT)
517 t = VT_SHORT;
518 if (vtop->r & VT_LVAL_UNSIGNED)
519 t |= VT_UNSIGNED;
520 vtop->type.t = t;
521 load(r, vtop);
522 /* restore wanted type */
523 vtop->type.t = t1;
524 } else {
525 /* one register type load */
526 load(r, vtop);
529 vtop->r = r;
530 #ifdef TCC_TARGET_C67
531 /* uses register pairs for doubles */
532 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
533 vtop->r2 = r+1;
534 #endif
536 return r;
539 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
540 void gv2(int rc1, int rc2)
542 int v;
544 /* generate more generic register first. But VT_JMP or VT_CMP
545 values must be generated first in all cases to avoid possible
546 reload errors */
547 v = vtop[0].r & VT_VALMASK;
548 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
549 vswap();
550 gv(rc1);
551 vswap();
552 gv(rc2);
553 /* test if reload is needed for first register */
554 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
555 vswap();
556 gv(rc1);
557 vswap();
559 } else {
560 gv(rc2);
561 vswap();
562 gv(rc1);
563 vswap();
564 /* test if reload is needed for first register */
565 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
566 gv(rc2);
571 /* wrapper around RC_FRET to return a register by type */
572 int rc_fret(int t)
574 #ifdef TCC_TARGET_X86_64
575 if (t == VT_LDOUBLE) {
576 return RC_ST0;
578 #endif
579 return RC_FRET;
582 /* wrapper around REG_FRET to return a register by type */
583 int reg_fret(int t)
585 #ifdef TCC_TARGET_X86_64
586 if (t == VT_LDOUBLE) {
587 return TREG_ST0;
589 #endif
590 return REG_FRET;
593 /* expand long long on stack in two int registers */
594 void lexpand(void)
596 int u;
598 u = vtop->type.t & VT_UNSIGNED;
599 gv(RC_INT);
600 vdup();
601 vtop[0].r = vtop[-1].r2;
602 vtop[0].r2 = VT_CONST;
603 vtop[-1].r2 = VT_CONST;
604 vtop[0].type.t = VT_INT | u;
605 vtop[-1].type.t = VT_INT | u;
608 #ifdef TCC_TARGET_ARM
609 /* expand long long on stack */
610 void lexpand_nr(void)
612 int u,v;
614 u = vtop->type.t & VT_UNSIGNED;
615 vdup();
616 vtop->r2 = VT_CONST;
617 vtop->type.t = VT_INT | u;
618 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
619 if (v == VT_CONST) {
620 vtop[-1].c.ui = vtop->c.ull;
621 vtop->c.ui = vtop->c.ull >> 32;
622 vtop->r = VT_CONST;
623 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
624 vtop->c.ui += 4;
625 vtop->r = vtop[-1].r;
626 } else if (v > VT_CONST) {
627 vtop--;
628 lexpand();
629 } else
630 vtop->r = vtop[-1].r2;
631 vtop[-1].r2 = VT_CONST;
632 vtop[-1].type.t = VT_INT | u;
634 #endif
636 /* build a long long from two ints */
637 void lbuild(int t)
639 gv2(RC_INT, RC_INT);
640 vtop[-1].r2 = vtop[0].r;
641 vtop[-1].type.t = t;
642 vpop();
645 /* rotate n first stack elements to the bottom
646 I1 ... In -> I2 ... In I1 [top is right]
648 void vrotb(int n)
650 int i;
651 SValue tmp;
653 tmp = vtop[-n + 1];
654 for(i=-n+1;i!=0;i++)
655 vtop[i] = vtop[i+1];
656 vtop[0] = tmp;
659 /* rotate n first stack elements to the top
660 I1 ... In -> In I1 ... I(n-1) [top is right]
662 void vrott(int n)
664 int i;
665 SValue tmp;
667 tmp = vtop[0];
668 for(i = 0;i < n - 1; i++)
669 vtop[-i] = vtop[-i - 1];
670 vtop[-n + 1] = tmp;
673 #ifdef TCC_TARGET_ARM
674 /* like vrott but in other direction
675 In ... I1 -> I(n-1) ... I1 In [top is right]
677 void vnrott(int n)
679 int i;
680 SValue tmp;
682 tmp = vtop[-n + 1];
683 for(i = n - 1; i > 0; i--)
684 vtop[-i] = vtop[-i + 1];
685 vtop[0] = tmp;
687 #endif
689 /* pop stack value */
690 void vpop(void)
692 int v;
693 v = vtop->r & VT_VALMASK;
694 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
695 /* for x86, we need to pop the FP stack */
696 if (v == TREG_ST0 && !nocode_wanted) {
697 o(0xd8dd); /* fstp %st(0) */
698 } else
699 #endif
700 if (v == VT_JMP || v == VT_JMPI) {
701 /* need to put correct jump if && or || without test */
702 gsym(vtop->c.ul);
704 vtop--;
707 /* convert stack entry to register and duplicate its value in another
708 register */
709 void gv_dup(void)
711 int rc, t, r, r1;
712 SValue sv;
714 t = vtop->type.t;
715 if ((t & VT_BTYPE) == VT_LLONG) {
716 lexpand();
717 gv_dup();
718 vswap();
719 vrotb(3);
720 gv_dup();
721 vrotb(4);
722 /* stack: H L L1 H1 */
723 lbuild(t);
724 vrotb(3);
725 vrotb(3);
726 vswap();
727 lbuild(t);
728 vswap();
729 } else {
730 /* duplicate value */
731 rc = RC_INT;
732 sv.type.t = VT_INT;
733 if (is_float(t)) {
734 rc = RC_FLOAT;
735 #ifdef TCC_TARGET_X86_64
736 if ((t & VT_BTYPE) == VT_LDOUBLE) {
737 rc = RC_ST0;
739 #endif
740 sv.type.t = t;
742 r = gv(rc);
743 r1 = get_reg(rc);
744 sv.r = r;
745 sv.c.ul = 0;
746 load(r1, &sv); /* move r to r1 */
747 vdup();
748 /* duplicates value */
749 if (r != r1)
750 vtop->r = r1;
754 #ifndef TCC_TARGET_X86_64
755 /* generate CPU independent (unsigned) long long operations */
756 void gen_opl(int op)
758 int t, a, b, op1, c, i;
759 int func;
760 unsigned short reg_iret = REG_IRET;
761 unsigned short reg_lret = REG_LRET;
762 SValue tmp;
764 switch(op) {
765 case '/':
766 case TOK_PDIV:
767 func = TOK___divdi3;
768 goto gen_func;
769 case TOK_UDIV:
770 func = TOK___udivdi3;
771 goto gen_func;
772 case '%':
773 func = TOK___moddi3;
774 goto gen_mod_func;
775 case TOK_UMOD:
776 func = TOK___umoddi3;
777 gen_mod_func:
778 #ifdef TCC_ARM_EABI
779 reg_iret = TREG_R2;
780 reg_lret = TREG_R3;
781 #endif
782 gen_func:
783 /* call generic long long function */
784 vpush_global_sym(&func_old_type, func);
785 vrott(3);
786 gfunc_call(2);
787 vpushi(0);
788 vtop->r = reg_iret;
789 vtop->r2 = reg_lret;
790 break;
791 case '^':
792 case '&':
793 case '|':
794 case '*':
795 case '+':
796 case '-':
797 t = vtop->type.t;
798 vswap();
799 lexpand();
800 vrotb(3);
801 lexpand();
802 /* stack: L1 H1 L2 H2 */
803 tmp = vtop[0];
804 vtop[0] = vtop[-3];
805 vtop[-3] = tmp;
806 tmp = vtop[-2];
807 vtop[-2] = vtop[-3];
808 vtop[-3] = tmp;
809 vswap();
810 /* stack: H1 H2 L1 L2 */
811 if (op == '*') {
812 vpushv(vtop - 1);
813 vpushv(vtop - 1);
814 gen_op(TOK_UMULL);
815 lexpand();
816 /* stack: H1 H2 L1 L2 ML MH */
817 for(i=0;i<4;i++)
818 vrotb(6);
819 /* stack: ML MH H1 H2 L1 L2 */
820 tmp = vtop[0];
821 vtop[0] = vtop[-2];
822 vtop[-2] = tmp;
823 /* stack: ML MH H1 L2 H2 L1 */
824 gen_op('*');
825 vrotb(3);
826 vrotb(3);
827 gen_op('*');
828 /* stack: ML MH M1 M2 */
829 gen_op('+');
830 gen_op('+');
831 } else if (op == '+' || op == '-') {
832 /* XXX: add non carry method too (for MIPS or alpha) */
833 if (op == '+')
834 op1 = TOK_ADDC1;
835 else
836 op1 = TOK_SUBC1;
837 gen_op(op1);
838 /* stack: H1 H2 (L1 op L2) */
839 vrotb(3);
840 vrotb(3);
841 gen_op(op1 + 1); /* TOK_xxxC2 */
842 } else {
843 gen_op(op);
844 /* stack: H1 H2 (L1 op L2) */
845 vrotb(3);
846 vrotb(3);
847 /* stack: (L1 op L2) H1 H2 */
848 gen_op(op);
849 /* stack: (L1 op L2) (H1 op H2) */
851 /* stack: L H */
852 lbuild(t);
853 break;
854 case TOK_SAR:
855 case TOK_SHR:
856 case TOK_SHL:
857 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
858 t = vtop[-1].type.t;
859 vswap();
860 lexpand();
861 vrotb(3);
862 /* stack: L H shift */
863 c = (int)vtop->c.i;
864 /* constant: simpler */
865 /* NOTE: all comments are for SHL. the other cases are
866 done by swaping words */
867 vpop();
868 if (op != TOK_SHL)
869 vswap();
870 if (c >= 32) {
871 /* stack: L H */
872 vpop();
873 if (c > 32) {
874 vpushi(c - 32);
875 gen_op(op);
877 if (op != TOK_SAR) {
878 vpushi(0);
879 } else {
880 gv_dup();
881 vpushi(31);
882 gen_op(TOK_SAR);
884 vswap();
885 } else {
886 vswap();
887 gv_dup();
888 /* stack: H L L */
889 vpushi(c);
890 gen_op(op);
891 vswap();
892 vpushi(32 - c);
893 if (op == TOK_SHL)
894 gen_op(TOK_SHR);
895 else
896 gen_op(TOK_SHL);
897 vrotb(3);
898 /* stack: L L H */
899 vpushi(c);
900 if (op == TOK_SHL)
901 gen_op(TOK_SHL);
902 else
903 gen_op(TOK_SHR);
904 gen_op('|');
906 if (op != TOK_SHL)
907 vswap();
908 lbuild(t);
909 } else {
910 /* XXX: should provide a faster fallback on x86 ? */
911 switch(op) {
912 case TOK_SAR:
913 func = TOK___ashrdi3;
914 goto gen_func;
915 case TOK_SHR:
916 func = TOK___lshrdi3;
917 goto gen_func;
918 case TOK_SHL:
919 func = TOK___ashldi3;
920 goto gen_func;
923 break;
924 default:
925 /* compare operations */
926 t = vtop->type.t;
927 vswap();
928 lexpand();
929 vrotb(3);
930 lexpand();
931 /* stack: L1 H1 L2 H2 */
932 tmp = vtop[-1];
933 vtop[-1] = vtop[-2];
934 vtop[-2] = tmp;
935 /* stack: L1 L2 H1 H2 */
936 /* compare high */
937 op1 = op;
938 /* when values are equal, we need to compare low words. since
939 the jump is inverted, we invert the test too. */
940 if (op1 == TOK_LT)
941 op1 = TOK_LE;
942 else if (op1 == TOK_GT)
943 op1 = TOK_GE;
944 else if (op1 == TOK_ULT)
945 op1 = TOK_ULE;
946 else if (op1 == TOK_UGT)
947 op1 = TOK_UGE;
948 a = 0;
949 b = 0;
950 gen_op(op1);
951 if (op1 != TOK_NE) {
952 a = gtst(1, 0);
954 if (op != TOK_EQ) {
955 /* generate non equal test */
956 /* XXX: NOT PORTABLE yet */
957 if (a == 0) {
958 b = gtst(0, 0);
959 } else {
960 #if defined(TCC_TARGET_I386)
961 b = psym(0x850f, 0);
962 #elif defined(TCC_TARGET_ARM)
963 b = ind;
964 o(0x1A000000 | encbranch(ind, 0, 1));
965 #elif defined(TCC_TARGET_C67)
966 error("not implemented");
967 #else
968 #error not supported
969 #endif
972 /* compare low. Always unsigned */
973 op1 = op;
974 if (op1 == TOK_LT)
975 op1 = TOK_ULT;
976 else if (op1 == TOK_LE)
977 op1 = TOK_ULE;
978 else if (op1 == TOK_GT)
979 op1 = TOK_UGT;
980 else if (op1 == TOK_GE)
981 op1 = TOK_UGE;
982 gen_op(op1);
983 a = gtst(1, a);
984 gsym(b);
985 vseti(VT_JMPI, a);
986 break;
989 #endif
991 /* handle integer constant optimizations and various machine
992 independent opt */
993 void gen_opic(int op)
995 int c1, c2, t1, t2, n;
996 SValue *v1, *v2;
997 long long l1, l2;
998 typedef unsigned long long U;
1000 v1 = vtop - 1;
1001 v2 = vtop;
1002 t1 = v1->type.t & VT_BTYPE;
1003 t2 = v2->type.t & VT_BTYPE;
1005 if (t1 == VT_LLONG)
1006 l1 = v1->c.ll;
1007 else if (v1->type.t & VT_UNSIGNED)
1008 l1 = v1->c.ui;
1009 else
1010 l1 = v1->c.i;
1012 if (t2 == VT_LLONG)
1013 l2 = v2->c.ll;
1014 else if (v2->type.t & VT_UNSIGNED)
1015 l2 = v2->c.ui;
1016 else
1017 l2 = v2->c.i;
1019 /* currently, we cannot do computations with forward symbols */
1020 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1021 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1022 if (c1 && c2) {
1023 switch(op) {
1024 case '+': l1 += l2; break;
1025 case '-': l1 -= l2; break;
1026 case '&': l1 &= l2; break;
1027 case '^': l1 ^= l2; break;
1028 case '|': l1 |= l2; break;
1029 case '*': l1 *= l2; break;
1031 case TOK_PDIV:
1032 case '/':
1033 case '%':
1034 case TOK_UDIV:
1035 case TOK_UMOD:
1036 /* if division by zero, generate explicit division */
1037 if (l2 == 0) {
1038 if (const_wanted)
1039 error("division by zero in constant");
1040 goto general_case;
1042 switch(op) {
1043 default: l1 /= l2; break;
1044 case '%': l1 %= l2; break;
1045 case TOK_UDIV: l1 = (U)l1 / l2; break;
1046 case TOK_UMOD: l1 = (U)l1 % l2; break;
1048 break;
1049 case TOK_SHL: l1 <<= l2; break;
1050 case TOK_SHR: l1 = (U)l1 >> l2; break;
1051 case TOK_SAR: l1 >>= l2; break;
1052 /* tests */
1053 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1054 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1055 case TOK_EQ: l1 = l1 == l2; break;
1056 case TOK_NE: l1 = l1 != l2; break;
1057 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1058 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1059 case TOK_LT: l1 = l1 < l2; break;
1060 case TOK_GE: l1 = l1 >= l2; break;
1061 case TOK_LE: l1 = l1 <= l2; break;
1062 case TOK_GT: l1 = l1 > l2; break;
1063 /* logical */
1064 case TOK_LAND: l1 = l1 && l2; break;
1065 case TOK_LOR: l1 = l1 || l2; break;
1066 default:
1067 goto general_case;
1069 v1->c.ll = l1;
1070 vtop--;
1071 } else {
1072 /* if commutative ops, put c2 as constant */
1073 if (c1 && (op == '+' || op == '&' || op == '^' ||
1074 op == '|' || op == '*')) {
1075 vswap();
1076 c2 = c1; //c = c1, c1 = c2, c2 = c;
1077 l2 = l1; //l = l1, l1 = l2, l2 = l;
1079 /* Filter out NOP operations like x*1, x-0, x&-1... */
1080 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1081 op == TOK_PDIV) &&
1082 l2 == 1) ||
1083 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1084 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1085 l2 == 0) ||
1086 (op == '&' &&
1087 l2 == -1))) {
1088 /* nothing to do */
1089 vtop--;
1090 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1091 /* try to use shifts instead of muls or divs */
1092 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1093 n = -1;
1094 while (l2) {
1095 l2 >>= 1;
1096 n++;
1098 vtop->c.ll = n;
1099 if (op == '*')
1100 op = TOK_SHL;
1101 else if (op == TOK_PDIV)
1102 op = TOK_SAR;
1103 else
1104 op = TOK_SHR;
1106 goto general_case;
1107 } else if (c2 && (op == '+' || op == '-') &&
1108 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM)
1109 && !(vtop[-1].sym->type.t & VT_IMPORT))
1111 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1112 /* symbol + constant case */
1113 if (op == '-')
1114 l2 = -l2;
1115 vtop--;
1116 vtop->c.ll += l2;
1117 } else {
1118 general_case:
1119 if (!nocode_wanted) {
1120 /* call low level op generator */
1121 if (t1 == VT_LLONG || t2 == VT_LLONG)
1122 gen_opl(op);
1123 else
1124 gen_opi(op);
1125 } else {
1126 vtop--;
1132 /* generate a floating point operation with constant propagation */
1133 void gen_opif(int op)
1135 int c1, c2;
1136 SValue *v1, *v2;
1137 long double f1, f2;
1139 v1 = vtop - 1;
1140 v2 = vtop;
1141 /* currently, we cannot do computations with forward symbols */
1142 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1143 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1144 if (c1 && c2) {
1145 if (v1->type.t == VT_FLOAT) {
1146 f1 = v1->c.f;
1147 f2 = v2->c.f;
1148 } else if (v1->type.t == VT_DOUBLE) {
1149 f1 = v1->c.d;
1150 f2 = v2->c.d;
1151 } else {
1152 f1 = v1->c.ld;
1153 f2 = v2->c.ld;
1156 /* NOTE: we only do constant propagation if finite number (not
1157 NaN or infinity) (ANSI spec) */
1158 if (!ieee_finite(f1) || !ieee_finite(f2))
1159 goto general_case;
1161 switch(op) {
1162 case '+': f1 += f2; break;
1163 case '-': f1 -= f2; break;
1164 case '*': f1 *= f2; break;
1165 case '/':
1166 if (f2 == 0.0) {
1167 if (const_wanted)
1168 error("division by zero in constant");
1169 goto general_case;
1171 f1 /= f2;
1172 break;
1173 /* XXX: also handles tests ? */
1174 default:
1175 goto general_case;
1177 /* XXX: overflow test ? */
1178 if (v1->type.t == VT_FLOAT) {
1179 v1->c.f = f1;
1180 } else if (v1->type.t == VT_DOUBLE) {
1181 v1->c.d = f1;
1182 } else {
1183 v1->c.ld = f1;
1185 vtop--;
1186 } else {
1187 general_case:
1188 if (!nocode_wanted) {
1189 gen_opf(op);
1190 } else {
1191 vtop--;
1196 static int pointed_size(CType *type)
1198 int align;
1199 return type_size(pointed_type(type), &align);
1202 static inline int is_null_pointer(SValue *p)
1204 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1205 return 0;
1206 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1207 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1210 static inline int is_integer_btype(int bt)
1212 return (bt == VT_BYTE || bt == VT_SHORT ||
1213 bt == VT_INT || bt == VT_LLONG);
1216 /* check types for comparison or substraction of pointers */
1217 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1219 CType *type1, *type2, tmp_type1, tmp_type2;
1220 int bt1, bt2;
1222 /* null pointers are accepted for all comparisons as gcc */
1223 if (is_null_pointer(p1) || is_null_pointer(p2))
1224 return;
1225 type1 = &p1->type;
1226 type2 = &p2->type;
1227 bt1 = type1->t & VT_BTYPE;
1228 bt2 = type2->t & VT_BTYPE;
1229 /* accept comparison between pointer and integer with a warning */
1230 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1231 if (op != TOK_LOR && op != TOK_LAND )
1232 warning("comparison between pointer and integer");
1233 return;
1236 /* both must be pointers or implicit function pointers */
1237 if (bt1 == VT_PTR) {
1238 type1 = pointed_type(type1);
1239 } else if (bt1 != VT_FUNC)
1240 goto invalid_operands;
1242 if (bt2 == VT_PTR) {
1243 type2 = pointed_type(type2);
1244 } else if (bt2 != VT_FUNC) {
1245 invalid_operands:
1246 error("invalid operands to binary %s", get_tok_str(op, NULL));
1248 if ((type1->t & VT_BTYPE) == VT_VOID ||
1249 (type2->t & VT_BTYPE) == VT_VOID)
1250 return;
1251 tmp_type1 = *type1;
1252 tmp_type2 = *type2;
1253 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1254 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1255 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1256 /* gcc-like error if '-' is used */
1257 if (op == '-')
1258 goto invalid_operands;
1259 else
1260 warning("comparison of distinct pointer types lacks a cast");
1264 /* generic gen_op: handles types problems */
1265 void gen_op(int op)
1267 int u, t1, t2, bt1, bt2, t;
1268 CType type1;
1270 t1 = vtop[-1].type.t;
1271 t2 = vtop[0].type.t;
1272 bt1 = t1 & VT_BTYPE;
1273 bt2 = t2 & VT_BTYPE;
1275 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1276 /* at least one operand is a pointer */
1277 /* relationnal op: must be both pointers */
1278 if (op >= TOK_ULT && op <= TOK_LOR) {
1279 check_comparison_pointer_types(vtop - 1, vtop, op);
1280 /* pointers are handled are unsigned */
1281 #ifdef TCC_TARGET_X86_64
1282 t = VT_LLONG | VT_UNSIGNED;
1283 #else
1284 t = VT_INT | VT_UNSIGNED;
1285 #endif
1286 goto std_op;
1288 /* if both pointers, then it must be the '-' op */
1289 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1290 if (op != '-')
1291 error("cannot use pointers here");
1292 check_comparison_pointer_types(vtop - 1, vtop, op);
1293 /* XXX: check that types are compatible */
1294 u = pointed_size(&vtop[-1].type);
1295 gen_opic(op);
1296 /* set to integer type */
1297 #ifdef TCC_TARGET_X86_64
1298 vtop->type.t = VT_LLONG;
1299 #else
1300 vtop->type.t = VT_INT;
1301 #endif
1302 vpushi(u);
1303 gen_op(TOK_PDIV);
1304 } else {
1305 /* exactly one pointer : must be '+' or '-'. */
1306 if (op != '-' && op != '+')
1307 error("cannot use pointers here");
1308 /* Put pointer as first operand */
1309 if (bt2 == VT_PTR) {
1310 vswap();
1311 swap(&t1, &t2);
1313 type1 = vtop[-1].type;
1314 type1.t &= ~VT_ARRAY;
1315 #ifdef TCC_TARGET_X86_64
1316 vpushll(pointed_size(&vtop[-1].type));
1317 #else
1318 /* XXX: cast to int ? (long long case) */
1319 vpushi(pointed_size(&vtop[-1].type));
1320 #endif
1321 gen_op('*');
1322 #ifdef CONFIG_TCC_BCHECK
1323 /* if evaluating constant expression, no code should be
1324 generated, so no bound check */
1325 if (tcc_state->do_bounds_check && !const_wanted) {
1326 /* if bounded pointers, we generate a special code to
1327 test bounds */
1328 if (op == '-') {
1329 vpushi(0);
1330 vswap();
1331 gen_op('-');
1333 gen_bounded_ptr_add();
1334 } else
1335 #endif
1337 gen_opic(op);
1339 /* put again type if gen_opic() swaped operands */
1340 vtop->type = type1;
1342 } else if (is_float(bt1) || is_float(bt2)) {
1343 /* compute bigger type and do implicit casts */
1344 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1345 t = VT_LDOUBLE;
1346 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1347 t = VT_DOUBLE;
1348 } else {
1349 t = VT_FLOAT;
1351 /* floats can only be used for a few operations */
1352 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1353 (op < TOK_ULT || op > TOK_GT))
1354 error("invalid operands for binary operation");
1355 goto std_op;
1356 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1357 /* cast to biggest op */
1358 t = VT_LLONG;
1359 /* convert to unsigned if it does not fit in a long long */
1360 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1361 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1362 t |= VT_UNSIGNED;
1363 goto std_op;
1364 } else {
1365 /* integer operations */
1366 t = VT_INT;
1367 /* convert to unsigned if it does not fit in an integer */
1368 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1369 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1370 t |= VT_UNSIGNED;
1371 std_op:
1372 /* XXX: currently, some unsigned operations are explicit, so
1373 we modify them here */
1374 if (t & VT_UNSIGNED) {
1375 if (op == TOK_SAR)
1376 op = TOK_SHR;
1377 else if (op == '/')
1378 op = TOK_UDIV;
1379 else if (op == '%')
1380 op = TOK_UMOD;
1381 else if (op == TOK_LT)
1382 op = TOK_ULT;
1383 else if (op == TOK_GT)
1384 op = TOK_UGT;
1385 else if (op == TOK_LE)
1386 op = TOK_ULE;
1387 else if (op == TOK_GE)
1388 op = TOK_UGE;
1390 vswap();
1391 type1.t = t;
1392 gen_cast(&type1);
1393 vswap();
1394 /* special case for shifts and long long: we keep the shift as
1395 an integer */
1396 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1397 type1.t = VT_INT;
1398 gen_cast(&type1);
1399 if (is_float(t))
1400 gen_opif(op);
1401 else
1402 gen_opic(op);
1403 if (op >= TOK_ULT && op <= TOK_GT) {
1404 /* relationnal op: the result is an int */
1405 vtop->type.t = VT_INT;
1406 } else {
1407 vtop->type.t = t;
1412 #ifndef TCC_TARGET_ARM
1413 /* generic itof for unsigned long long case */
1414 void gen_cvt_itof1(int t)
1416 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1417 (VT_LLONG | VT_UNSIGNED)) {
1419 if (t == VT_FLOAT)
1420 vpush_global_sym(&func_old_type, TOK___floatundisf);
1421 #if LDOUBLE_SIZE != 8
1422 else if (t == VT_LDOUBLE)
1423 vpush_global_sym(&func_old_type, TOK___floatundixf);
1424 #endif
1425 else
1426 vpush_global_sym(&func_old_type, TOK___floatundidf);
1427 vrott(2);
1428 gfunc_call(1);
1429 vpushi(0);
1430 vtop->r = reg_fret(t);
1431 } else {
1432 gen_cvt_itof(t);
1435 #endif
1437 /* generic ftoi for unsigned long long case */
1438 void gen_cvt_ftoi1(int t)
1440 int st;
1442 if (t == (VT_LLONG | VT_UNSIGNED)) {
1443 /* not handled natively */
1444 st = vtop->type.t & VT_BTYPE;
1445 if (st == VT_FLOAT)
1446 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1447 #if LDOUBLE_SIZE != 8
1448 else if (st == VT_LDOUBLE)
1449 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1450 #endif
1451 else
1452 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1453 vrott(2);
1454 gfunc_call(1);
1455 vpushi(0);
1456 vtop->r = REG_IRET;
1457 vtop->r2 = REG_LRET;
1458 } else {
1459 gen_cvt_ftoi(t);
1463 /* force char or short cast */
1464 void force_charshort_cast(int t)
1466 int bits, dbt;
1467 dbt = t & VT_BTYPE;
1468 /* XXX: add optimization if lvalue : just change type and offset */
1469 if (dbt == VT_BYTE)
1470 bits = 8;
1471 else
1472 bits = 16;
1473 if (t & VT_UNSIGNED) {
1474 vpushi((1 << bits) - 1);
1475 gen_op('&');
1476 } else {
1477 bits = 32 - bits;
1478 vpushi(bits);
1479 gen_op(TOK_SHL);
1480 /* result must be signed or the SAR is converted to an SHL
1481 This was not the case when "t" was a signed short
1482 and the last value on the stack was an unsigned int */
1483 vtop->type.t &= ~VT_UNSIGNED;
1484 vpushi(bits);
1485 gen_op(TOK_SAR);
1489 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1490 static void gen_cast(CType *type)
1492 int sbt, dbt, sf, df, c, p;
1494 /* special delayed cast for char/short */
1495 /* XXX: in some cases (multiple cascaded casts), it may still
1496 be incorrect */
1497 if (vtop->r & VT_MUSTCAST) {
1498 vtop->r &= ~VT_MUSTCAST;
1499 force_charshort_cast(vtop->type.t);
1502 /* bitfields first get cast to ints */
1503 if (vtop->type.t & VT_BITFIELD) {
1504 gv(RC_INT);
1507 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1508 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1510 if (sbt != dbt) {
1511 sf = is_float(sbt);
1512 df = is_float(dbt);
1513 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1514 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1515 if (c) {
1516 /* constant case: we can do it now */
1517 /* XXX: in ISOC, cannot do it if error in convert */
1518 if (sbt == VT_FLOAT)
1519 vtop->c.ld = vtop->c.f;
1520 else if (sbt == VT_DOUBLE)
1521 vtop->c.ld = vtop->c.d;
1523 if (df) {
1524 if ((sbt & VT_BTYPE) == VT_LLONG) {
1525 if (sbt & VT_UNSIGNED)
1526 vtop->c.ld = vtop->c.ull;
1527 else
1528 vtop->c.ld = vtop->c.ll;
1529 } else if(!sf) {
1530 if (sbt & VT_UNSIGNED)
1531 vtop->c.ld = vtop->c.ui;
1532 else
1533 vtop->c.ld = vtop->c.i;
1536 if (dbt == VT_FLOAT)
1537 vtop->c.f = (float)vtop->c.ld;
1538 else if (dbt == VT_DOUBLE)
1539 vtop->c.d = (double)vtop->c.ld;
1540 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1541 vtop->c.ull = (unsigned long long)vtop->c.ld;
1542 } else if (sf && dbt == VT_BOOL) {
1543 vtop->c.i = (vtop->c.ld != 0);
1544 } else {
1545 if(sf)
1546 vtop->c.ll = (long long)vtop->c.ld;
1547 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1548 vtop->c.ll = vtop->c.ull;
1549 else if (sbt & VT_UNSIGNED)
1550 vtop->c.ll = vtop->c.ui;
1551 #ifdef TCC_TARGET_X86_64
1552 else if (sbt == VT_PTR)
1554 #endif
1555 else if (sbt != VT_LLONG)
1556 vtop->c.ll = vtop->c.i;
1558 if (dbt == (VT_LLONG|VT_UNSIGNED))
1559 vtop->c.ull = vtop->c.ll;
1560 else if (dbt == VT_BOOL)
1561 vtop->c.i = (vtop->c.ll != 0);
1562 else if (dbt != VT_LLONG) {
1563 int s = 0;
1564 if ((dbt & VT_BTYPE) == VT_BYTE)
1565 s = 24;
1566 else if ((dbt & VT_BTYPE) == VT_SHORT)
1567 s = 16;
1569 if(dbt & VT_UNSIGNED)
1570 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1571 else
1572 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1575 } else if (p && dbt == VT_BOOL) {
1576 vtop->r = VT_CONST;
1577 vtop->c.i = 1;
1578 } else if (!nocode_wanted) {
1579 /* non constant case: generate code */
1580 if (sf && df) {
1581 /* convert from fp to fp */
1582 gen_cvt_ftof(dbt);
1583 } else if (df) {
1584 /* convert int to fp */
1585 gen_cvt_itof1(dbt);
1586 } else if (sf) {
1587 /* convert fp to int */
1588 if (dbt == VT_BOOL) {
1589 vpushi(0);
1590 gen_op(TOK_NE);
1591 } else {
1592 /* we handle char/short/etc... with generic code */
1593 if (dbt != (VT_INT | VT_UNSIGNED) &&
1594 dbt != (VT_LLONG | VT_UNSIGNED) &&
1595 dbt != VT_LLONG)
1596 dbt = VT_INT;
1597 gen_cvt_ftoi1(dbt);
1598 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1599 /* additional cast for char/short... */
1600 vtop->type.t = dbt;
1601 gen_cast(type);
1604 #ifndef TCC_TARGET_X86_64
1605 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1606 if ((sbt & VT_BTYPE) != VT_LLONG) {
1607 /* scalar to long long */
1608 /* machine independent conversion */
1609 gv(RC_INT);
1610 /* generate high word */
1611 if (sbt == (VT_INT | VT_UNSIGNED)) {
1612 vpushi(0);
1613 gv(RC_INT);
1614 } else {
1615 if (sbt == VT_PTR) {
1616 /* cast from pointer to int before we apply
1617 shift operation, which pointers don't support*/
1618 gen_cast(&int_type);
1620 gv_dup();
1621 vpushi(31);
1622 gen_op(TOK_SAR);
1624 /* patch second register */
1625 vtop[-1].r2 = vtop->r;
1626 vpop();
1628 #else
1629 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1630 (dbt & VT_BTYPE) == VT_PTR) {
1631 /* XXX: not sure if this is perfect... need more tests */
1632 if ((sbt & VT_BTYPE) != VT_LLONG) {
1633 int r = gv(RC_INT);
1634 if (sbt != (VT_INT | VT_UNSIGNED) &&
1635 sbt != VT_PTR && sbt != VT_FUNC) {
1636 /* x86_64 specific: movslq */
1637 o(0x6348);
1638 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1641 #endif
1642 } else if (dbt == VT_BOOL) {
1643 /* scalar to bool */
1644 vpushi(0);
1645 gen_op(TOK_NE);
1646 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1647 (dbt & VT_BTYPE) == VT_SHORT) {
1648 if (sbt == VT_PTR) {
1649 vtop->type.t = VT_INT;
1650 warning("nonportable conversion from pointer to char/short");
1652 force_charshort_cast(dbt);
1653 } else if ((dbt & VT_BTYPE) == VT_INT) {
1654 /* scalar to int */
1655 if (sbt == VT_LLONG) {
1656 /* from long long: just take low order word */
1657 lexpand();
1658 vpop();
1660 /* if lvalue and single word type, nothing to do because
1661 the lvalue already contains the real type size (see
1662 VT_LVAL_xxx constants) */
1665 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1666 /* if we are casting between pointer types,
1667 we must update the VT_LVAL_xxx size */
1668 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1669 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1671 vtop->type = *type;
1674 /* return type size. Put alignment at 'a' */
1675 static int type_size(CType *type, int *a)
1677 Sym *s;
1678 int bt;
1680 bt = type->t & VT_BTYPE;
1681 if (bt == VT_STRUCT) {
1682 /* struct/union */
1683 s = type->ref;
1684 *a = s->r;
1685 return s->c;
1686 } else if (bt == VT_PTR) {
1687 if (type->t & VT_ARRAY) {
1688 int ts;
1690 s = type->ref;
1691 ts = type_size(&s->type, a);
1693 if (ts < 0 && s->c < 0)
1694 ts = -ts;
1696 return ts * s->c;
1697 } else {
1698 *a = PTR_SIZE;
1699 return PTR_SIZE;
1701 } else if (bt == VT_LDOUBLE) {
1702 *a = LDOUBLE_ALIGN;
1703 return LDOUBLE_SIZE;
1704 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1705 #ifdef TCC_TARGET_I386
1706 #ifdef TCC_TARGET_PE
1707 *a = 8;
1708 #else
1709 *a = 4;
1710 #endif
1711 #elif defined(TCC_TARGET_ARM)
1712 #ifdef TCC_ARM_EABI
1713 *a = 8;
1714 #else
1715 *a = 4;
1716 #endif
1717 #else
1718 *a = 8;
1719 #endif
1720 return 8;
1721 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1722 *a = 4;
1723 return 4;
1724 } else if (bt == VT_SHORT) {
1725 *a = 2;
1726 return 2;
1727 } else {
1728 /* char, void, function, _Bool */
1729 *a = 1;
1730 return 1;
1734 /* return the pointed type of t */
1735 static inline CType *pointed_type(CType *type)
1737 return &type->ref->type;
1740 /* modify type so that its it is a pointer to type. */
1741 static void mk_pointer(CType *type)
1743 Sym *s;
1744 s = sym_push(SYM_FIELD, type, 0, -1);
1745 type->t = VT_PTR | (type->t & ~VT_TYPE);
1746 type->ref = s;
1749 /* compare function types. OLD functions match any new functions */
1750 static int is_compatible_func(CType *type1, CType *type2)
1752 Sym *s1, *s2;
1754 s1 = type1->ref;
1755 s2 = type2->ref;
1756 if (!is_compatible_types(&s1->type, &s2->type))
1757 return 0;
1758 /* check func_call */
1759 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
1760 return 0;
1761 /* XXX: not complete */
1762 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
1763 return 1;
1764 if (s1->c != s2->c)
1765 return 0;
1766 while (s1 != NULL) {
1767 if (s2 == NULL)
1768 return 0;
1769 if (!is_compatible_parameter_types(&s1->type, &s2->type))
1770 return 0;
1771 s1 = s1->next;
1772 s2 = s2->next;
1774 if (s2)
1775 return 0;
1776 return 1;
1779 /* return true if type1 and type2 are the same. If unqualified is
1780 true, qualifiers on the types are ignored.
1782 - enums are not checked as gcc __builtin_types_compatible_p ()
1784 static int compare_types(CType *type1, CType *type2, int unqualified)
1786 int bt1, t1, t2;
1788 t1 = type1->t & VT_TYPE;
1789 t2 = type2->t & VT_TYPE;
1790 if (unqualified) {
1791 /* strip qualifiers before comparing */
1792 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
1793 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
1795 /* XXX: bitfields ? */
1796 if (t1 != t2)
1797 return 0;
1798 /* test more complicated cases */
1799 bt1 = t1 & VT_BTYPE;
1800 if (bt1 == VT_PTR) {
1801 type1 = pointed_type(type1);
1802 type2 = pointed_type(type2);
1803 return is_compatible_types(type1, type2);
1804 } else if (bt1 == VT_STRUCT) {
1805 return (type1->ref == type2->ref);
1806 } else if (bt1 == VT_FUNC) {
1807 return is_compatible_func(type1, type2);
1808 } else {
1809 return 1;
1813 /* return true if type1 and type2 are exactly the same (including
1814 qualifiers).
1816 static int is_compatible_types(CType *type1, CType *type2)
1818 return compare_types(type1,type2,0);
1821 /* return true if type1 and type2 are the same (ignoring qualifiers).
1823 static int is_compatible_parameter_types(CType *type1, CType *type2)
1825 return compare_types(type1,type2,1);
1828 /* print a type. If 'varstr' is not NULL, then the variable is also
1829 printed in the type */
1830 /* XXX: union */
1831 /* XXX: add array and function pointers */
1832 void type_to_str(char *buf, int buf_size,
1833 CType *type, const char *varstr)
1835 int bt, v, t;
1836 Sym *s, *sa;
1837 char buf1[256];
1838 const char *tstr;
1840 t = type->t & VT_TYPE;
1841 bt = t & VT_BTYPE;
1842 buf[0] = '\0';
1843 if (t & VT_CONSTANT)
1844 pstrcat(buf, buf_size, "const ");
1845 if (t & VT_VOLATILE)
1846 pstrcat(buf, buf_size, "volatile ");
1847 if (t & VT_UNSIGNED)
1848 pstrcat(buf, buf_size, "unsigned ");
1849 switch(bt) {
1850 case VT_VOID:
1851 tstr = "void";
1852 goto add_tstr;
1853 case VT_BOOL:
1854 tstr = "_Bool";
1855 goto add_tstr;
1856 case VT_BYTE:
1857 tstr = "char";
1858 goto add_tstr;
1859 case VT_SHORT:
1860 tstr = "short";
1861 goto add_tstr;
1862 case VT_INT:
1863 tstr = "int";
1864 goto add_tstr;
1865 case VT_LONG:
1866 tstr = "long";
1867 goto add_tstr;
1868 case VT_LLONG:
1869 tstr = "long long";
1870 goto add_tstr;
1871 case VT_FLOAT:
1872 tstr = "float";
1873 goto add_tstr;
1874 case VT_DOUBLE:
1875 tstr = "double";
1876 goto add_tstr;
1877 case VT_LDOUBLE:
1878 tstr = "long double";
1879 add_tstr:
1880 pstrcat(buf, buf_size, tstr);
1881 break;
1882 case VT_ENUM:
1883 case VT_STRUCT:
1884 if (bt == VT_STRUCT)
1885 tstr = "struct ";
1886 else
1887 tstr = "enum ";
1888 pstrcat(buf, buf_size, tstr);
1889 v = type->ref->v & ~SYM_STRUCT;
1890 if (v >= SYM_FIRST_ANOM)
1891 pstrcat(buf, buf_size, "<anonymous>");
1892 else
1893 pstrcat(buf, buf_size, get_tok_str(v, NULL));
1894 break;
1895 case VT_FUNC:
1896 s = type->ref;
1897 type_to_str(buf, buf_size, &s->type, varstr);
1898 pstrcat(buf, buf_size, "(");
1899 sa = s->next;
1900 while (sa != NULL) {
1901 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
1902 pstrcat(buf, buf_size, buf1);
1903 sa = sa->next;
1904 if (sa)
1905 pstrcat(buf, buf_size, ", ");
1907 pstrcat(buf, buf_size, ")");
1908 goto no_var;
1909 case VT_PTR:
1910 s = type->ref;
1911 pstrcpy(buf1, sizeof(buf1), "*");
1912 if (varstr)
1913 pstrcat(buf1, sizeof(buf1), varstr);
1914 type_to_str(buf, buf_size, &s->type, buf1);
1915 goto no_var;
1917 if (varstr) {
1918 pstrcat(buf, buf_size, " ");
1919 pstrcat(buf, buf_size, varstr);
1921 no_var: ;
1924 /* verify type compatibility to store vtop in 'dt' type, and generate
1925 casts if needed. */
1926 static void gen_assign_cast(CType *dt)
1928 CType *st, *type1, *type2, tmp_type1, tmp_type2;
1929 char buf1[256], buf2[256];
1930 int dbt, sbt;
1932 st = &vtop->type; /* source type */
1933 dbt = dt->t & VT_BTYPE;
1934 sbt = st->t & VT_BTYPE;
1935 if (dt->t & VT_CONSTANT)
1936 warning("assignment of read-only location");
1937 switch(dbt) {
1938 case VT_PTR:
1939 /* special cases for pointers */
1940 /* '0' can also be a pointer */
1941 if (is_null_pointer(vtop))
1942 goto type_ok;
1943 /* accept implicit pointer to integer cast with warning */
1944 if (is_integer_btype(sbt)) {
1945 warning("assignment makes pointer from integer without a cast");
1946 goto type_ok;
1948 type1 = pointed_type(dt);
1949 /* a function is implicitely a function pointer */
1950 if (sbt == VT_FUNC) {
1951 if ((type1->t & VT_BTYPE) != VT_VOID &&
1952 !is_compatible_types(pointed_type(dt), st))
1953 warning("assignment from incompatible pointer type");
1954 goto type_ok;
1956 if (sbt != VT_PTR)
1957 goto error;
1958 type2 = pointed_type(st);
1959 if ((type1->t & VT_BTYPE) == VT_VOID ||
1960 (type2->t & VT_BTYPE) == VT_VOID) {
1961 /* void * can match anything */
1962 } else {
1963 /* exact type match, except for unsigned */
1964 tmp_type1 = *type1;
1965 tmp_type2 = *type2;
1966 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1967 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1968 if (!is_compatible_types(&tmp_type1, &tmp_type2))
1969 warning("assignment from incompatible pointer type");
1971 /* check const and volatile */
1972 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
1973 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
1974 warning("assignment discards qualifiers from pointer target type");
1975 break;
1976 case VT_BYTE:
1977 case VT_SHORT:
1978 case VT_INT:
1979 case VT_LLONG:
1980 if (sbt == VT_PTR || sbt == VT_FUNC) {
1981 warning("assignment makes integer from pointer without a cast");
1983 /* XXX: more tests */
1984 break;
1985 case VT_STRUCT:
1986 tmp_type1 = *dt;
1987 tmp_type2 = *st;
1988 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
1989 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
1990 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1991 error:
1992 type_to_str(buf1, sizeof(buf1), st, NULL);
1993 type_to_str(buf2, sizeof(buf2), dt, NULL);
1994 error("cannot cast '%s' to '%s'", buf1, buf2);
1996 break;
1998 type_ok:
1999 gen_cast(dt);
2002 /* store vtop in lvalue pushed on stack */
2003 void vstore(void)
2005 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2007 ft = vtop[-1].type.t;
2008 sbt = vtop->type.t & VT_BTYPE;
2009 dbt = ft & VT_BTYPE;
2010 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2011 (sbt == VT_INT && dbt == VT_SHORT)) {
2012 /* optimize char/short casts */
2013 delayed_cast = VT_MUSTCAST;
2014 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2015 /* XXX: factorize */
2016 if (ft & VT_CONSTANT)
2017 warning("assignment of read-only location");
2018 } else {
2019 delayed_cast = 0;
2020 if (!(ft & VT_BITFIELD))
2021 gen_assign_cast(&vtop[-1].type);
2024 if (sbt == VT_STRUCT) {
2025 /* if structure, only generate pointer */
2026 /* structure assignment : generate memcpy */
2027 /* XXX: optimize if small size */
2028 if (!nocode_wanted) {
2029 size = type_size(&vtop->type, &align);
2031 /* destination */
2032 vswap();
2033 vtop->type.t = VT_PTR;
2034 gaddrof();
2036 /* address of memcpy() */
2037 #ifdef TCC_ARM_EABI
2038 if(!(align & 7))
2039 vpush_global_sym(&func_old_type, TOK_memcpy8);
2040 else if(!(align & 3))
2041 vpush_global_sym(&func_old_type, TOK_memcpy4);
2042 else
2043 #endif
2044 vpush_global_sym(&func_old_type, TOK_memcpy);
2046 vswap();
2047 /* source */
2048 vpushv(vtop - 2);
2049 vtop->type.t = VT_PTR;
2050 gaddrof();
2051 /* type size */
2052 vpushi(size);
2053 gfunc_call(3);
2054 } else {
2055 vswap();
2056 vpop();
2058 /* leave source on stack */
2059 } else if (ft & VT_BITFIELD) {
2060 /* bitfield store handling */
2061 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2062 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2063 /* remove bit field info to avoid loops */
2064 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2066 /* duplicate source into other register */
2067 gv_dup();
2068 vswap();
2069 vrott(3);
2071 if((ft & VT_BTYPE) == VT_BOOL) {
2072 gen_cast(&vtop[-1].type);
2073 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2076 /* duplicate destination */
2077 vdup();
2078 vtop[-1] = vtop[-2];
2080 /* mask and shift source */
2081 if((ft & VT_BTYPE) != VT_BOOL) {
2082 if((ft & VT_BTYPE) == VT_LLONG) {
2083 vpushll((1ULL << bit_size) - 1ULL);
2084 } else {
2085 vpushi((1 << bit_size) - 1);
2087 gen_op('&');
2089 vpushi(bit_pos);
2090 gen_op(TOK_SHL);
2091 /* load destination, mask and or with source */
2092 vswap();
2093 if((ft & VT_BTYPE) == VT_LLONG) {
2094 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2095 } else {
2096 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2098 gen_op('&');
2099 gen_op('|');
2100 /* store result */
2101 vstore();
2103 /* pop off shifted source from "duplicate source..." above */
2104 vpop();
2106 } else {
2107 #ifdef CONFIG_TCC_BCHECK
2108 /* bound check case */
2109 if (vtop[-1].r & VT_MUSTBOUND) {
2110 vswap();
2111 gbound();
2112 vswap();
2114 #endif
2115 if (!nocode_wanted) {
2116 rc = RC_INT;
2117 if (is_float(ft)) {
2118 rc = RC_FLOAT;
2119 #ifdef TCC_TARGET_X86_64
2120 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2121 rc = RC_ST0;
2123 #endif
2125 r = gv(rc); /* generate value */
2126 /* if lvalue was saved on stack, must read it */
2127 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2128 SValue sv;
2129 t = get_reg(RC_INT);
2130 #ifdef TCC_TARGET_X86_64
2131 sv.type.t = VT_PTR;
2132 #else
2133 sv.type.t = VT_INT;
2134 #endif
2135 sv.r = VT_LOCAL | VT_LVAL;
2136 sv.c.ul = vtop[-1].c.ul;
2137 load(t, &sv);
2138 vtop[-1].r = t | VT_LVAL;
2140 store(r, vtop - 1);
2141 #ifndef TCC_TARGET_X86_64
2142 /* two word case handling : store second register at word + 4 */
2143 if ((ft & VT_BTYPE) == VT_LLONG) {
2144 vswap();
2145 /* convert to int to increment easily */
2146 vtop->type.t = VT_INT;
2147 gaddrof();
2148 vpushi(4);
2149 gen_op('+');
2150 vtop->r |= VT_LVAL;
2151 vswap();
2152 /* XXX: it works because r2 is spilled last ! */
2153 store(vtop->r2, vtop - 1);
2155 #endif
2157 vswap();
2158 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2159 vtop->r |= delayed_cast;
2163 /* post defines POST/PRE add. c is the token ++ or -- */
2164 void inc(int post, int c)
2166 test_lvalue();
2167 vdup(); /* save lvalue */
2168 if (post) {
2169 gv_dup(); /* duplicate value */
2170 vrotb(3);
2171 vrotb(3);
2173 /* add constant */
2174 vpushi(c - TOK_MID);
2175 gen_op('+');
2176 vstore(); /* store value */
2177 if (post)
2178 vpop(); /* if post op, return saved value */
2181 /* Parse GNUC __attribute__ extension. Currently, the following
2182 extensions are recognized:
2183 - aligned(n) : set data/function alignment.
2184 - packed : force data alignment to 1
2185 - section(x) : generate data/code in this section.
2186 - unused : currently ignored, but may be used someday.
2187 - regparm(n) : pass function parameters in registers (i386 only)
2189 static void parse_attribute(AttributeDef *ad)
2191 int t, n;
2193 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2194 next();
2195 skip('(');
2196 skip('(');
2197 while (tok != ')') {
2198 if (tok < TOK_IDENT)
2199 expect("attribute name");
2200 t = tok;
2201 next();
2202 switch(t) {
2203 case TOK_SECTION1:
2204 case TOK_SECTION2:
2205 skip('(');
2206 if (tok != TOK_STR)
2207 expect("section name");
2208 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2209 next();
2210 skip(')');
2211 break;
2212 case TOK_ALIGNED1:
2213 case TOK_ALIGNED2:
2214 if (tok == '(') {
2215 next();
2216 n = expr_const();
2217 if (n <= 0 || (n & (n - 1)) != 0)
2218 error("alignment must be a positive power of two");
2219 skip(')');
2220 } else {
2221 n = MAX_ALIGN;
2223 ad->aligned = n;
2224 break;
2225 case TOK_PACKED1:
2226 case TOK_PACKED2:
2227 ad->packed = 1;
2228 break;
2229 case TOK_UNUSED1:
2230 case TOK_UNUSED2:
2231 /* currently, no need to handle it because tcc does not
2232 track unused objects */
2233 break;
2234 case TOK_NORETURN1:
2235 case TOK_NORETURN2:
2236 /* currently, no need to handle it because tcc does not
2237 track unused objects */
2238 break;
2239 case TOK_CDECL1:
2240 case TOK_CDECL2:
2241 case TOK_CDECL3:
2242 ad->func_call = FUNC_CDECL;
2243 break;
2244 case TOK_STDCALL1:
2245 case TOK_STDCALL2:
2246 case TOK_STDCALL3:
2247 ad->func_call = FUNC_STDCALL;
2248 break;
2249 #ifdef TCC_TARGET_I386
2250 case TOK_REGPARM1:
2251 case TOK_REGPARM2:
2252 skip('(');
2253 n = expr_const();
2254 if (n > 3)
2255 n = 3;
2256 else if (n < 0)
2257 n = 0;
2258 if (n > 0)
2259 ad->func_call = FUNC_FASTCALL1 + n - 1;
2260 skip(')');
2261 break;
2262 case TOK_FASTCALL1:
2263 case TOK_FASTCALL2:
2264 case TOK_FASTCALL3:
2265 ad->func_call = FUNC_FASTCALLW;
2266 break;
2267 #endif
2268 case TOK_DLLEXPORT:
2269 ad->func_export = 1;
2270 break;
2271 case TOK_DLLIMPORT:
2272 ad->func_import = 1;
2273 break;
2274 default:
2275 if (tcc_state->warn_unsupported)
2276 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2277 /* skip parameters */
2278 if (tok == '(') {
2279 int parenthesis = 0;
2280 do {
2281 if (tok == '(')
2282 parenthesis++;
2283 else if (tok == ')')
2284 parenthesis--;
2285 next();
2286 } while (parenthesis && tok != -1);
2288 break;
2290 if (tok != ',')
2291 break;
2292 next();
2294 skip(')');
2295 skip(')');
2299 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2300 static void struct_decl(CType *type, int u)
2302 int a, v, size, align, maxalign, c, offset;
2303 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2304 Sym *s, *ss, *ass, **ps;
2305 AttributeDef ad;
2306 CType type1, btype;
2308 a = tok; /* save decl type */
2309 next();
2310 if (tok != '{') {
2311 v = tok;
2312 next();
2313 /* struct already defined ? return it */
2314 if (v < TOK_IDENT)
2315 expect("struct/union/enum name");
2316 s = struct_find(v);
2317 if (s) {
2318 if (s->type.t != a)
2319 error("invalid type");
2320 goto do_decl;
2322 } else {
2323 v = anon_sym++;
2325 type1.t = a;
2326 /* we put an undefined size for struct/union */
2327 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2328 s->r = 0; /* default alignment is zero as gcc */
2329 /* put struct/union/enum name in type */
2330 do_decl:
2331 type->t = u;
2332 type->ref = s;
2334 if (tok == '{') {
2335 next();
2336 if (s->c != -1)
2337 error("struct/union/enum already defined");
2338 /* cannot be empty */
2339 c = 0;
2340 /* non empty enums are not allowed */
2341 if (a == TOK_ENUM) {
2342 for(;;) {
2343 v = tok;
2344 if (v < TOK_UIDENT)
2345 expect("identifier");
2346 next();
2347 if (tok == '=') {
2348 next();
2349 c = expr_const();
2351 /* enum symbols have static storage */
2352 ss = sym_push(v, &int_type, VT_CONST, c);
2353 ss->type.t |= VT_STATIC;
2354 if (tok != ',')
2355 break;
2356 next();
2357 c++;
2358 /* NOTE: we accept a trailing comma */
2359 if (tok == '}')
2360 break;
2362 skip('}');
2363 } else {
2364 maxalign = 1;
2365 ps = &s->next;
2366 prevbt = VT_INT;
2367 bit_pos = 0;
2368 offset = 0;
2369 while (tok != '}') {
2370 parse_btype(&btype, &ad);
2371 while (1) {
2372 bit_size = -1;
2373 v = 0;
2374 type1 = btype;
2375 if (tok != ':') {
2376 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2377 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2378 expect("identifier");
2379 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2380 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2381 error("invalid type for '%s'",
2382 get_tok_str(v, NULL));
2384 if (tok == ':') {
2385 next();
2386 bit_size = expr_const();
2387 /* XXX: handle v = 0 case for messages */
2388 if (bit_size < 0)
2389 error("negative width in bit-field '%s'",
2390 get_tok_str(v, NULL));
2391 if (v && bit_size == 0)
2392 error("zero width for bit-field '%s'",
2393 get_tok_str(v, NULL));
2395 size = type_size(&type1, &align);
2396 if (ad.aligned) {
2397 if (align < ad.aligned)
2398 align = ad.aligned;
2399 } else if (ad.packed) {
2400 align = 1;
2401 } else if (*tcc_state->pack_stack_ptr) {
2402 if (align > *tcc_state->pack_stack_ptr)
2403 align = *tcc_state->pack_stack_ptr;
2405 lbit_pos = 0;
2406 if (bit_size >= 0) {
2407 bt = type1.t & VT_BTYPE;
2408 if (bt != VT_INT &&
2409 bt != VT_BYTE &&
2410 bt != VT_SHORT &&
2411 bt != VT_BOOL &&
2412 bt != VT_ENUM &&
2413 bt != VT_LLONG)
2414 error("bitfields must have scalar type");
2415 bsize = size * 8;
2416 if (bit_size > bsize) {
2417 error("width of '%s' exceeds its type",
2418 get_tok_str(v, NULL));
2419 } else if (bit_size == bsize) {
2420 /* no need for bit fields */
2421 bit_pos = 0;
2422 } else if (bit_size == 0) {
2423 /* XXX: what to do if only padding in a
2424 structure ? */
2425 /* zero size: means to pad */
2426 bit_pos = 0;
2427 } else {
2428 /* we do not have enough room ?
2429 did the type change?
2430 is it a union? */
2431 if ((bit_pos + bit_size) > bsize ||
2432 bt != prevbt || a == TOK_UNION)
2433 bit_pos = 0;
2434 lbit_pos = bit_pos;
2435 /* XXX: handle LSB first */
2436 type1.t |= VT_BITFIELD |
2437 (bit_pos << VT_STRUCT_SHIFT) |
2438 (bit_size << (VT_STRUCT_SHIFT + 6));
2439 bit_pos += bit_size;
2441 prevbt = bt;
2442 } else {
2443 bit_pos = 0;
2445 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2446 /* add new memory data only if starting
2447 bit field */
2448 if (lbit_pos == 0) {
2449 if (a == TOK_STRUCT) {
2450 c = (c + align - 1) & -align;
2451 offset = c;
2452 if (size > 0)
2453 c += size;
2454 } else {
2455 offset = 0;
2456 if (size > c)
2457 c = size;
2459 if (align > maxalign)
2460 maxalign = align;
2462 #if 0
2463 printf("add field %s offset=%d",
2464 get_tok_str(v, NULL), offset);
2465 if (type1.t & VT_BITFIELD) {
2466 printf(" pos=%d size=%d",
2467 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2468 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2470 printf("\n");
2471 #endif
2473 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2474 ass = type1.ref;
2475 while ((ass = ass->next) != NULL) {
2476 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2477 *ps = ss;
2478 ps = &ss->next;
2480 } else if (v) {
2481 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2482 *ps = ss;
2483 ps = &ss->next;
2485 if (tok == ';' || tok == TOK_EOF)
2486 break;
2487 skip(',');
2489 skip(';');
2491 skip('}');
2492 /* store size and alignment */
2493 s->c = (c + maxalign - 1) & -maxalign;
2494 s->r = maxalign;
2499 /* return 0 if no type declaration. otherwise, return the basic type
2500 and skip it.
2502 static int parse_btype(CType *type, AttributeDef *ad)
2504 int t, u, type_found, typespec_found, typedef_found;
2505 Sym *s;
2506 CType type1;
2508 memset(ad, 0, sizeof(AttributeDef));
2509 type_found = 0;
2510 typespec_found = 0;
2511 typedef_found = 0;
2512 t = 0;
2513 while(1) {
2514 switch(tok) {
2515 case TOK_EXTENSION:
2516 /* currently, we really ignore extension */
2517 next();
2518 continue;
2520 /* basic types */
2521 case TOK_CHAR:
2522 u = VT_BYTE;
2523 basic_type:
2524 next();
2525 basic_type1:
2526 if ((t & VT_BTYPE) != 0)
2527 error("too many basic types");
2528 t |= u;
2529 typespec_found = 1;
2530 break;
2531 case TOK_VOID:
2532 u = VT_VOID;
2533 goto basic_type;
2534 case TOK_SHORT:
2535 u = VT_SHORT;
2536 goto basic_type;
2537 case TOK_INT:
2538 next();
2539 typespec_found = 1;
2540 break;
2541 case TOK_LONG:
2542 next();
2543 if ((t & VT_BTYPE) == VT_DOUBLE) {
2544 #ifndef TCC_TARGET_PE
2545 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2546 #endif
2547 } else if ((t & VT_BTYPE) == VT_LONG) {
2548 t = (t & ~VT_BTYPE) | VT_LLONG;
2549 } else {
2550 u = VT_LONG;
2551 goto basic_type1;
2553 break;
2554 case TOK_BOOL:
2555 u = VT_BOOL;
2556 goto basic_type;
2557 case TOK_FLOAT:
2558 u = VT_FLOAT;
2559 goto basic_type;
2560 case TOK_DOUBLE:
2561 next();
2562 if ((t & VT_BTYPE) == VT_LONG) {
2563 #ifdef TCC_TARGET_PE
2564 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2565 #else
2566 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2567 #endif
2568 } else {
2569 u = VT_DOUBLE;
2570 goto basic_type1;
2572 break;
2573 case TOK_ENUM:
2574 struct_decl(&type1, VT_ENUM);
2575 basic_type2:
2576 u = type1.t;
2577 type->ref = type1.ref;
2578 goto basic_type1;
2579 case TOK_STRUCT:
2580 case TOK_UNION:
2581 struct_decl(&type1, VT_STRUCT);
2582 goto basic_type2;
2584 /* type modifiers */
2585 case TOK_CONST1:
2586 case TOK_CONST2:
2587 case TOK_CONST3:
2588 t |= VT_CONSTANT;
2589 next();
2590 break;
2591 case TOK_VOLATILE1:
2592 case TOK_VOLATILE2:
2593 case TOK_VOLATILE3:
2594 t |= VT_VOLATILE;
2595 next();
2596 break;
2597 case TOK_SIGNED1:
2598 case TOK_SIGNED2:
2599 case TOK_SIGNED3:
2600 typespec_found = 1;
2601 t |= VT_SIGNED;
2602 next();
2603 break;
2604 case TOK_REGISTER:
2605 case TOK_AUTO:
2606 case TOK_RESTRICT1:
2607 case TOK_RESTRICT2:
2608 case TOK_RESTRICT3:
2609 next();
2610 break;
2611 case TOK_UNSIGNED:
2612 t |= VT_UNSIGNED;
2613 next();
2614 typespec_found = 1;
2615 break;
2617 /* storage */
2618 case TOK_EXTERN:
2619 t |= VT_EXTERN;
2620 next();
2621 break;
2622 case TOK_STATIC:
2623 t |= VT_STATIC;
2624 next();
2625 break;
2626 case TOK_TYPEDEF:
2627 t |= VT_TYPEDEF;
2628 next();
2629 break;
2630 case TOK_INLINE1:
2631 case TOK_INLINE2:
2632 case TOK_INLINE3:
2633 t |= VT_INLINE;
2634 next();
2635 break;
2637 /* GNUC attribute */
2638 case TOK_ATTRIBUTE1:
2639 case TOK_ATTRIBUTE2:
2640 parse_attribute(ad);
2641 break;
2642 /* GNUC typeof */
2643 case TOK_TYPEOF1:
2644 case TOK_TYPEOF2:
2645 case TOK_TYPEOF3:
2646 next();
2647 parse_expr_type(&type1);
2648 goto basic_type2;
2649 default:
2650 if (typespec_found || typedef_found)
2651 goto the_end;
2652 s = sym_find(tok);
2653 if (!s || !(s->type.t & VT_TYPEDEF))
2654 goto the_end;
2655 typedef_found = 1;
2656 t |= (s->type.t & ~VT_TYPEDEF);
2657 type->ref = s->type.ref;
2658 if (s->r) {
2659 /* get attributes from typedef */
2660 if (0 == ad->aligned)
2661 ad->aligned = FUNC_ALIGN(s->r);
2662 if (0 == ad->func_call)
2663 ad->func_call = FUNC_CALL(s->r);
2664 ad->packed |= FUNC_PACKED(s->r);
2666 next();
2667 typespec_found = 1;
2668 break;
2670 type_found = 1;
2672 the_end:
2673 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2674 error("signed and unsigned modifier");
2675 if (tcc_state->char_is_unsigned) {
2676 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2677 t |= VT_UNSIGNED;
2679 t &= ~VT_SIGNED;
2681 /* long is never used as type */
2682 if ((t & VT_BTYPE) == VT_LONG)
2683 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2684 t = (t & ~VT_BTYPE) | VT_INT;
2685 #else
2686 t = (t & ~VT_BTYPE) | VT_LLONG;
2687 #endif
2688 type->t = t;
2689 return type_found;
2692 /* convert a function parameter type (array to pointer and function to
2693 function pointer) */
2694 static inline void convert_parameter_type(CType *pt)
2696 /* remove const and volatile qualifiers (XXX: const could be used
2697 to indicate a const function parameter */
2698 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2699 /* array must be transformed to pointer according to ANSI C */
2700 pt->t &= ~VT_ARRAY;
2701 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2702 mk_pointer(pt);
2706 static void post_type(CType *type, AttributeDef *ad)
2708 int n, l, t1, arg_size, align;
2709 Sym **plast, *s, *first;
2710 AttributeDef ad1;
2711 CType pt;
2713 if (tok == '(') {
2714 /* function declaration */
2715 next();
2716 l = 0;
2717 first = NULL;
2718 plast = &first;
2719 arg_size = 0;
2720 if (tok != ')') {
2721 for(;;) {
2722 /* read param name and compute offset */
2723 if (l != FUNC_OLD) {
2724 if (!parse_btype(&pt, &ad1)) {
2725 if (l) {
2726 error("invalid type");
2727 } else {
2728 l = FUNC_OLD;
2729 goto old_proto;
2732 l = FUNC_NEW;
2733 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2734 break;
2735 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2736 if ((pt.t & VT_BTYPE) == VT_VOID)
2737 error("parameter declared as void");
2738 arg_size += (type_size(&pt, &align) + 3) & ~3;
2739 } else {
2740 old_proto:
2741 n = tok;
2742 if (n < TOK_UIDENT)
2743 expect("identifier");
2744 pt.t = VT_INT;
2745 next();
2747 convert_parameter_type(&pt);
2748 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
2749 *plast = s;
2750 plast = &s->next;
2751 if (tok == ')')
2752 break;
2753 skip(',');
2754 if (l == FUNC_NEW && tok == TOK_DOTS) {
2755 l = FUNC_ELLIPSIS;
2756 next();
2757 break;
2761 /* if no parameters, then old type prototype */
2762 if (l == 0)
2763 l = FUNC_OLD;
2764 skip(')');
2765 t1 = type->t & VT_STORAGE;
2766 /* NOTE: const is ignored in returned type as it has a special
2767 meaning in gcc / C++ */
2768 type->t &= ~(VT_STORAGE | VT_CONSTANT);
2769 post_type(type, ad);
2770 /* we push a anonymous symbol which will contain the function prototype */
2771 ad->func_args = arg_size;
2772 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
2773 s->next = first;
2774 type->t = t1 | VT_FUNC;
2775 type->ref = s;
2776 } else if (tok == '[') {
2777 /* array definition */
2778 next();
2779 if (tok == TOK_RESTRICT1)
2780 next();
2781 n = -1;
2782 if (tok != ']') {
2783 n = expr_const();
2784 if (n < 0)
2785 error("invalid array size");
2787 skip(']');
2788 /* parse next post type */
2789 t1 = type->t & VT_STORAGE;
2790 type->t &= ~VT_STORAGE;
2791 post_type(type, ad);
2793 /* we push a anonymous symbol which will contain the array
2794 element type */
2795 s = sym_push(SYM_FIELD, type, 0, n);
2796 type->t = t1 | VT_ARRAY | VT_PTR;
2797 type->ref = s;
2801 /* Parse a type declaration (except basic type), and return the type
2802 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2803 expected. 'type' should contain the basic type. 'ad' is the
2804 attribute definition of the basic type. It can be modified by
2805 type_decl().
2807 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
2809 Sym *s;
2810 CType type1, *type2;
2811 int qualifiers;
2813 while (tok == '*') {
2814 qualifiers = 0;
2815 redo:
2816 next();
2817 switch(tok) {
2818 case TOK_CONST1:
2819 case TOK_CONST2:
2820 case TOK_CONST3:
2821 qualifiers |= VT_CONSTANT;
2822 goto redo;
2823 case TOK_VOLATILE1:
2824 case TOK_VOLATILE2:
2825 case TOK_VOLATILE3:
2826 qualifiers |= VT_VOLATILE;
2827 goto redo;
2828 case TOK_RESTRICT1:
2829 case TOK_RESTRICT2:
2830 case TOK_RESTRICT3:
2831 goto redo;
2833 mk_pointer(type);
2834 type->t |= qualifiers;
2837 /* XXX: clarify attribute handling */
2838 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2839 parse_attribute(ad);
2841 /* recursive type */
2842 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2843 type1.t = 0; /* XXX: same as int */
2844 if (tok == '(') {
2845 next();
2846 /* XXX: this is not correct to modify 'ad' at this point, but
2847 the syntax is not clear */
2848 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2849 parse_attribute(ad);
2850 type_decl(&type1, ad, v, td);
2851 skip(')');
2852 } else {
2853 /* type identifier */
2854 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
2855 *v = tok;
2856 next();
2857 } else {
2858 if (!(td & TYPE_ABSTRACT))
2859 expect("identifier");
2860 *v = 0;
2863 post_type(type, ad);
2864 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2865 parse_attribute(ad);
2866 if (!type1.t)
2867 return;
2868 /* append type at the end of type1 */
2869 type2 = &type1;
2870 for(;;) {
2871 s = type2->ref;
2872 type2 = &s->type;
2873 if (!type2->t) {
2874 *type2 = *type;
2875 break;
2878 *type = type1;
2881 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2882 static int lvalue_type(int t)
2884 int bt, r;
2885 r = VT_LVAL;
2886 bt = t & VT_BTYPE;
2887 if (bt == VT_BYTE || bt == VT_BOOL)
2888 r |= VT_LVAL_BYTE;
2889 else if (bt == VT_SHORT)
2890 r |= VT_LVAL_SHORT;
2891 else
2892 return r;
2893 if (t & VT_UNSIGNED)
2894 r |= VT_LVAL_UNSIGNED;
2895 return r;
2898 /* indirection with full error checking and bound check */
2899 static void indir(void)
2901 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
2902 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
2903 return;
2904 expect("pointer");
2906 if ((vtop->r & VT_LVAL) && !nocode_wanted)
2907 gv(RC_INT);
2908 vtop->type = *pointed_type(&vtop->type);
2909 /* Arrays and functions are never lvalues */
2910 if (!(vtop->type.t & VT_ARRAY)
2911 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
2912 vtop->r |= lvalue_type(vtop->type.t);
2913 /* if bound checking, the referenced pointer must be checked */
2914 #ifdef CONFIG_TCC_BCHECK
2915 if (tcc_state->do_bounds_check)
2916 vtop->r |= VT_MUSTBOUND;
2917 #endif
2921 /* pass a parameter to a function and do type checking and casting */
2922 static void gfunc_param_typed(Sym *func, Sym *arg)
2924 int func_type;
2925 CType type;
2927 func_type = func->c;
2928 if (func_type == FUNC_OLD ||
2929 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
2930 /* default casting : only need to convert float to double */
2931 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
2932 type.t = VT_DOUBLE;
2933 gen_cast(&type);
2935 } else if (arg == NULL) {
2936 error("too many arguments to function");
2937 } else {
2938 type = arg->type;
2939 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
2940 gen_assign_cast(&type);
2944 /* parse an expression of the form '(type)' or '(expr)' and return its
2945 type */
2946 static void parse_expr_type(CType *type)
2948 int n;
2949 AttributeDef ad;
2951 skip('(');
2952 if (parse_btype(type, &ad)) {
2953 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2954 } else {
2955 expr_type(type);
2957 skip(')');
2960 static void parse_type(CType *type)
2962 AttributeDef ad;
2963 int n;
2965 if (!parse_btype(type, &ad)) {
2966 expect("type");
2968 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2971 static void vpush_tokc(int t)
2973 CType type;
2974 type.t = t;
2975 type.ref = 0;
2976 vsetc(&type, VT_CONST, &tokc);
2979 static void unary(void)
2981 int n, t, align, size, r;
2982 CType type;
2983 Sym *s;
2984 AttributeDef ad;
2986 /* XXX: GCC 2.95.3 does not generate a table although it should be
2987 better here */
2988 tok_next:
2989 switch(tok) {
2990 case TOK_EXTENSION:
2991 next();
2992 goto tok_next;
2993 case TOK_CINT:
2994 case TOK_CCHAR:
2995 case TOK_LCHAR:
2996 vpushi(tokc.i);
2997 next();
2998 break;
2999 case TOK_CUINT:
3000 vpush_tokc(VT_INT | VT_UNSIGNED);
3001 next();
3002 break;
3003 case TOK_CLLONG:
3004 vpush_tokc(VT_LLONG);
3005 next();
3006 break;
3007 case TOK_CULLONG:
3008 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3009 next();
3010 break;
3011 case TOK_CFLOAT:
3012 vpush_tokc(VT_FLOAT);
3013 next();
3014 break;
3015 case TOK_CDOUBLE:
3016 vpush_tokc(VT_DOUBLE);
3017 next();
3018 break;
3019 case TOK_CLDOUBLE:
3020 vpush_tokc(VT_LDOUBLE);
3021 next();
3022 break;
3023 case TOK___FUNCTION__:
3024 if (!gnu_ext)
3025 goto tok_identifier;
3026 /* fall thru */
3027 case TOK___FUNC__:
3029 void *ptr;
3030 int len;
3031 /* special function name identifier */
3032 len = strlen(funcname) + 1;
3033 /* generate char[len] type */
3034 type.t = VT_BYTE;
3035 mk_pointer(&type);
3036 type.t |= VT_ARRAY;
3037 type.ref->c = len;
3038 vpush_ref(&type, data_section, data_section->data_offset, len);
3039 ptr = section_ptr_add(data_section, len);
3040 memcpy(ptr, funcname, len);
3041 next();
3043 break;
3044 case TOK_LSTR:
3045 #ifdef TCC_TARGET_PE
3046 t = VT_SHORT | VT_UNSIGNED;
3047 #else
3048 t = VT_INT;
3049 #endif
3050 goto str_init;
3051 case TOK_STR:
3052 /* string parsing */
3053 t = VT_BYTE;
3054 str_init:
3055 if (tcc_state->warn_write_strings)
3056 t |= VT_CONSTANT;
3057 type.t = t;
3058 mk_pointer(&type);
3059 type.t |= VT_ARRAY;
3060 memset(&ad, 0, sizeof(AttributeDef));
3061 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3062 break;
3063 case '(':
3064 next();
3065 /* cast ? */
3066 if (parse_btype(&type, &ad)) {
3067 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3068 skip(')');
3069 /* check ISOC99 compound literal */
3070 if (tok == '{') {
3071 /* data is allocated locally by default */
3072 if (global_expr)
3073 r = VT_CONST;
3074 else
3075 r = VT_LOCAL;
3076 /* all except arrays are lvalues */
3077 if (!(type.t & VT_ARRAY))
3078 r |= lvalue_type(type.t);
3079 memset(&ad, 0, sizeof(AttributeDef));
3080 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3081 } else {
3082 unary();
3083 gen_cast(&type);
3085 } else if (tok == '{') {
3086 /* save all registers */
3087 save_regs(0);
3088 /* statement expression : we do not accept break/continue
3089 inside as GCC does */
3090 block(NULL, NULL, NULL, NULL, 0, 1);
3091 skip(')');
3092 } else {
3093 gexpr();
3094 skip(')');
3096 break;
3097 case '*':
3098 next();
3099 unary();
3100 indir();
3101 break;
3102 case '&':
3103 next();
3104 unary();
3105 /* functions names must be treated as function pointers,
3106 except for unary '&' and sizeof. Since we consider that
3107 functions are not lvalues, we only have to handle it
3108 there and in function calls. */
3109 /* arrays can also be used although they are not lvalues */
3110 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3111 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3112 test_lvalue();
3113 mk_pointer(&vtop->type);
3114 gaddrof();
3115 break;
3116 case '!':
3117 next();
3118 unary();
3119 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3120 CType boolean;
3121 boolean.t = VT_BOOL;
3122 gen_cast(&boolean);
3123 vtop->c.i = !vtop->c.i;
3124 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3125 vtop->c.i = vtop->c.i ^ 1;
3126 else {
3127 save_regs(1);
3128 vseti(VT_JMP, gtst(1, 0));
3130 break;
3131 case '~':
3132 next();
3133 unary();
3134 vpushi(-1);
3135 gen_op('^');
3136 break;
3137 case '+':
3138 next();
3139 /* in order to force cast, we add zero */
3140 unary();
3141 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3142 error("pointer not accepted for unary plus");
3143 vpushi(0);
3144 gen_op('+');
3145 break;
3146 case TOK_SIZEOF:
3147 case TOK_ALIGNOF1:
3148 case TOK_ALIGNOF2:
3149 t = tok;
3150 next();
3151 if (tok == '(') {
3152 parse_expr_type(&type);
3153 } else {
3154 unary_type(&type);
3156 size = type_size(&type, &align);
3157 if (t == TOK_SIZEOF) {
3158 if (size < 0)
3159 error("sizeof applied to an incomplete type");
3160 vpushi(size);
3161 } else {
3162 vpushi(align);
3164 vtop->type.t |= VT_UNSIGNED;
3165 break;
3167 case TOK_builtin_types_compatible_p:
3169 CType type1, type2;
3170 next();
3171 skip('(');
3172 parse_type(&type1);
3173 skip(',');
3174 parse_type(&type2);
3175 skip(')');
3176 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3177 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3178 vpushi(is_compatible_types(&type1, &type2));
3180 break;
3181 case TOK_builtin_constant_p:
3183 int saved_nocode_wanted, res;
3184 next();
3185 skip('(');
3186 saved_nocode_wanted = nocode_wanted;
3187 nocode_wanted = 1;
3188 gexpr();
3189 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3190 vpop();
3191 nocode_wanted = saved_nocode_wanted;
3192 skip(')');
3193 vpushi(res);
3195 break;
3196 case TOK_builtin_frame_address:
3198 CType type;
3199 next();
3200 skip('(');
3201 if (tok != TOK_CINT) {
3202 error("__builtin_frame_address only takes integers");
3204 if (tokc.i != 0) {
3205 error("TCC only supports __builtin_frame_address(0)");
3207 next();
3208 skip(')');
3209 type.t = VT_VOID;
3210 mk_pointer(&type);
3211 vset(&type, VT_LOCAL, 0);
3213 break;
3214 #ifdef TCC_TARGET_X86_64
3215 case TOK_builtin_malloc:
3216 tok = TOK_malloc;
3217 goto tok_identifier;
3218 case TOK_builtin_free:
3219 tok = TOK_free;
3220 goto tok_identifier;
3221 #endif
3222 case TOK_INC:
3223 case TOK_DEC:
3224 t = tok;
3225 next();
3226 unary();
3227 inc(0, t);
3228 break;
3229 case '-':
3230 next();
3231 vpushi(0);
3232 unary();
3233 gen_op('-');
3234 break;
3235 case TOK_LAND:
3236 if (!gnu_ext)
3237 goto tok_identifier;
3238 next();
3239 /* allow to take the address of a label */
3240 if (tok < TOK_UIDENT)
3241 expect("label identifier");
3242 s = label_find(tok);
3243 if (!s) {
3244 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3245 } else {
3246 if (s->r == LABEL_DECLARED)
3247 s->r = LABEL_FORWARD;
3249 if (!s->type.t) {
3250 s->type.t = VT_VOID;
3251 mk_pointer(&s->type);
3252 s->type.t |= VT_STATIC;
3254 vset(&s->type, VT_CONST | VT_SYM, 0);
3255 vtop->sym = s;
3256 next();
3257 break;
3258 default:
3259 tok_identifier:
3260 t = tok;
3261 next();
3262 if (t < TOK_UIDENT)
3263 expect("identifier");
3264 s = sym_find(t);
3265 if (!s) {
3266 if (tok != '(')
3267 error("'%s' undeclared", get_tok_str(t, NULL));
3268 /* for simple function calls, we tolerate undeclared
3269 external reference to int() function */
3270 if (tcc_state->warn_implicit_function_declaration)
3271 warning("implicit declaration of function '%s'",
3272 get_tok_str(t, NULL));
3273 s = external_global_sym(t, &func_old_type, 0);
3275 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3276 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3277 /* if referencing an inline function, then we generate a
3278 symbol to it if not already done. It will have the
3279 effect to generate code for it at the end of the
3280 compilation unit. Inline function as always
3281 generated in the text section. */
3282 if (!s->c)
3283 put_extern_sym(s, text_section, 0, 0);
3284 r = VT_SYM | VT_CONST;
3285 } else {
3286 r = s->r;
3288 vset(&s->type, r, s->c);
3289 /* if forward reference, we must point to s */
3290 if (vtop->r & VT_SYM) {
3291 vtop->sym = s;
3292 vtop->c.ul = 0;
3294 break;
3297 /* post operations */
3298 while (1) {
3299 if (tok == TOK_INC || tok == TOK_DEC) {
3300 inc(1, tok);
3301 next();
3302 } else if (tok == '.' || tok == TOK_ARROW) {
3303 int qualifiers;
3304 /* field */
3305 if (tok == TOK_ARROW)
3306 indir();
3307 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3308 test_lvalue();
3309 gaddrof();
3310 next();
3311 /* expect pointer on structure */
3312 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3313 expect("struct or union");
3314 s = vtop->type.ref;
3315 /* find field */
3316 tok |= SYM_FIELD;
3317 while ((s = s->next) != NULL) {
3318 if (s->v == tok)
3319 break;
3321 if (!s)
3322 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3323 /* add field offset to pointer */
3324 vtop->type = char_pointer_type; /* change type to 'char *' */
3325 vpushi(s->c);
3326 gen_op('+');
3327 /* change type to field type, and set to lvalue */
3328 vtop->type = s->type;
3329 vtop->type.t |= qualifiers;
3330 /* an array is never an lvalue */
3331 if (!(vtop->type.t & VT_ARRAY)) {
3332 vtop->r |= lvalue_type(vtop->type.t);
3333 #ifdef CONFIG_TCC_BCHECK
3334 /* if bound checking, the referenced pointer must be checked */
3335 if (tcc_state->do_bounds_check)
3336 vtop->r |= VT_MUSTBOUND;
3337 #endif
3339 next();
3340 } else if (tok == '[') {
3341 next();
3342 gexpr();
3343 gen_op('+');
3344 indir();
3345 skip(']');
3346 } else if (tok == '(') {
3347 SValue ret;
3348 Sym *sa;
3349 int nb_args;
3351 /* function call */
3352 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3353 /* pointer test (no array accepted) */
3354 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3355 vtop->type = *pointed_type(&vtop->type);
3356 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3357 goto error_func;
3358 } else {
3359 error_func:
3360 expect("function pointer");
3362 } else {
3363 vtop->r &= ~VT_LVAL; /* no lvalue */
3365 /* get return type */
3366 s = vtop->type.ref;
3367 next();
3368 sa = s->next; /* first parameter */
3369 nb_args = 0;
3370 ret.r2 = VT_CONST;
3371 /* compute first implicit argument if a structure is returned */
3372 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3373 /* get some space for the returned structure */
3374 size = type_size(&s->type, &align);
3375 loc = (loc - size) & -align;
3376 ret.type = s->type;
3377 ret.r = VT_LOCAL | VT_LVAL;
3378 /* pass it as 'int' to avoid structure arg passing
3379 problems */
3380 vseti(VT_LOCAL, loc);
3381 ret.c = vtop->c;
3382 nb_args++;
3383 } else {
3384 ret.type = s->type;
3385 /* return in register */
3386 if (is_float(ret.type.t)) {
3387 ret.r = reg_fret(ret.type.t);
3388 } else {
3389 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3390 ret.r2 = REG_LRET;
3391 ret.r = REG_IRET;
3393 ret.c.i = 0;
3395 if (tok != ')') {
3396 for(;;) {
3397 expr_eq();
3398 gfunc_param_typed(s, sa);
3399 nb_args++;
3400 if (sa)
3401 sa = sa->next;
3402 if (tok == ')')
3403 break;
3404 skip(',');
3407 if (sa)
3408 error("too few arguments to function");
3409 skip(')');
3410 if (!nocode_wanted) {
3411 gfunc_call(nb_args);
3412 } else {
3413 vtop -= (nb_args + 1);
3415 /* return value */
3416 vsetc(&ret.type, ret.r, &ret.c);
3417 vtop->r2 = ret.r2;
3418 } else {
3419 break;
3424 static void uneq(void)
3426 int t;
3428 unary();
3429 if (tok == '=' ||
3430 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3431 tok == TOK_A_XOR || tok == TOK_A_OR ||
3432 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3433 test_lvalue();
3434 t = tok;
3435 next();
3436 if (t == '=') {
3437 expr_eq();
3438 } else {
3439 vdup();
3440 expr_eq();
3441 gen_op(t & 0x7f);
3443 vstore();
3447 static void expr_prod(void)
3449 int t;
3451 uneq();
3452 while (tok == '*' || tok == '/' || tok == '%') {
3453 t = tok;
3454 next();
3455 uneq();
3456 gen_op(t);
3460 static void expr_sum(void)
3462 int t;
3464 expr_prod();
3465 while (tok == '+' || tok == '-') {
3466 t = tok;
3467 next();
3468 expr_prod();
3469 gen_op(t);
3473 static void expr_shift(void)
3475 int t;
3477 expr_sum();
3478 while (tok == TOK_SHL || tok == TOK_SAR) {
3479 t = tok;
3480 next();
3481 expr_sum();
3482 gen_op(t);
3486 static void expr_cmp(void)
3488 int t;
3490 expr_shift();
3491 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3492 tok == TOK_ULT || tok == TOK_UGE) {
3493 t = tok;
3494 next();
3495 expr_shift();
3496 gen_op(t);
3500 static void expr_cmpeq(void)
3502 int t;
3504 expr_cmp();
3505 while (tok == TOK_EQ || tok == TOK_NE) {
3506 t = tok;
3507 next();
3508 expr_cmp();
3509 gen_op(t);
3513 static void expr_and(void)
3515 expr_cmpeq();
3516 while (tok == '&') {
3517 next();
3518 expr_cmpeq();
3519 gen_op('&');
3523 static void expr_xor(void)
3525 expr_and();
3526 while (tok == '^') {
3527 next();
3528 expr_and();
3529 gen_op('^');
3533 static void expr_or(void)
3535 expr_xor();
3536 while (tok == '|') {
3537 next();
3538 expr_xor();
3539 gen_op('|');
3543 /* XXX: fix this mess */
3544 static void expr_land_const(void)
3546 expr_or();
3547 while (tok == TOK_LAND) {
3548 next();
3549 expr_or();
3550 gen_op(TOK_LAND);
3554 /* XXX: fix this mess */
3555 static void expr_lor_const(void)
3557 expr_land_const();
3558 while (tok == TOK_LOR) {
3559 next();
3560 expr_land_const();
3561 gen_op(TOK_LOR);
3565 /* only used if non constant */
3566 static void expr_land(void)
3568 int t;
3570 expr_or();
3571 if (tok == TOK_LAND) {
3572 t = 0;
3573 save_regs(1);
3574 for(;;) {
3575 t = gtst(1, t);
3576 if (tok != TOK_LAND) {
3577 vseti(VT_JMPI, t);
3578 break;
3580 next();
3581 expr_or();
3586 static void expr_lor(void)
3588 int t;
3590 expr_land();
3591 if (tok == TOK_LOR) {
3592 t = 0;
3593 save_regs(1);
3594 for(;;) {
3595 t = gtst(0, t);
3596 if (tok != TOK_LOR) {
3597 vseti(VT_JMP, t);
3598 break;
3600 next();
3601 expr_land();
3606 /* XXX: better constant handling */
3607 static void expr_eq(void)
3609 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3610 SValue sv;
3611 CType type, type1, type2;
3613 if (const_wanted) {
3614 expr_lor_const();
3615 if (tok == '?') {
3616 CType boolean;
3617 int c;
3618 boolean.t = VT_BOOL;
3619 vdup();
3620 gen_cast(&boolean);
3621 c = vtop->c.i;
3622 vpop();
3623 next();
3624 if (tok != ':' || !gnu_ext) {
3625 vpop();
3626 gexpr();
3628 if (!c)
3629 vpop();
3630 skip(':');
3631 expr_eq();
3632 if (c)
3633 vpop();
3635 } else {
3636 expr_lor();
3637 if (tok == '?') {
3638 next();
3639 if (vtop != vstack) {
3640 /* needed to avoid having different registers saved in
3641 each branch */
3642 if (is_float(vtop->type.t)) {
3643 rc = RC_FLOAT;
3644 #ifdef TCC_TARGET_X86_64
3645 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3646 rc = RC_ST0;
3648 #endif
3650 else
3651 rc = RC_INT;
3652 gv(rc);
3653 save_regs(1);
3655 if (tok == ':' && gnu_ext) {
3656 gv_dup();
3657 tt = gtst(1, 0);
3658 } else {
3659 tt = gtst(1, 0);
3660 gexpr();
3662 type1 = vtop->type;
3663 sv = *vtop; /* save value to handle it later */
3664 vtop--; /* no vpop so that FP stack is not flushed */
3665 skip(':');
3666 u = gjmp(0);
3667 gsym(tt);
3668 expr_eq();
3669 type2 = vtop->type;
3671 t1 = type1.t;
3672 bt1 = t1 & VT_BTYPE;
3673 t2 = type2.t;
3674 bt2 = t2 & VT_BTYPE;
3675 /* cast operands to correct type according to ISOC rules */
3676 if (is_float(bt1) || is_float(bt2)) {
3677 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3678 type.t = VT_LDOUBLE;
3679 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3680 type.t = VT_DOUBLE;
3681 } else {
3682 type.t = VT_FLOAT;
3684 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3685 /* cast to biggest op */
3686 type.t = VT_LLONG;
3687 /* convert to unsigned if it does not fit in a long long */
3688 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3689 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3690 type.t |= VT_UNSIGNED;
3691 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3692 /* XXX: test pointer compatibility */
3693 type = type1;
3694 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3695 /* XXX: test function pointer compatibility */
3696 type = type1;
3697 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3698 /* XXX: test structure compatibility */
3699 type = type1;
3700 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3701 /* NOTE: as an extension, we accept void on only one side */
3702 type.t = VT_VOID;
3703 } else {
3704 /* integer operations */
3705 type.t = VT_INT;
3706 /* convert to unsigned if it does not fit in an integer */
3707 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3708 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3709 type.t |= VT_UNSIGNED;
3712 /* now we convert second operand */
3713 gen_cast(&type);
3714 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3715 gaddrof();
3716 rc = RC_INT;
3717 if (is_float(type.t)) {
3718 rc = RC_FLOAT;
3719 #ifdef TCC_TARGET_X86_64
3720 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3721 rc = RC_ST0;
3723 #endif
3724 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3725 /* for long longs, we use fixed registers to avoid having
3726 to handle a complicated move */
3727 rc = RC_IRET;
3730 r2 = gv(rc);
3731 /* this is horrible, but we must also convert first
3732 operand */
3733 tt = gjmp(0);
3734 gsym(u);
3735 /* put again first value and cast it */
3736 *vtop = sv;
3737 gen_cast(&type);
3738 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3739 gaddrof();
3740 r1 = gv(rc);
3741 move_reg(r2, r1);
3742 vtop->r = r2;
3743 gsym(tt);
3748 static void gexpr(void)
3750 while (1) {
3751 expr_eq();
3752 if (tok != ',')
3753 break;
3754 vpop();
3755 next();
3759 /* parse an expression and return its type without any side effect. */
3760 static void expr_type(CType *type)
3762 int saved_nocode_wanted;
3764 saved_nocode_wanted = nocode_wanted;
3765 nocode_wanted = 1;
3766 gexpr();
3767 *type = vtop->type;
3768 vpop();
3769 nocode_wanted = saved_nocode_wanted;
3772 /* parse a unary expression and return its type without any side
3773 effect. */
3774 static void unary_type(CType *type)
3776 int a;
3778 a = nocode_wanted;
3779 nocode_wanted = 1;
3780 unary();
3781 *type = vtop->type;
3782 vpop();
3783 nocode_wanted = a;
3786 /* parse a constant expression and return value in vtop. */
3787 static void expr_const1(void)
3789 int a;
3790 a = const_wanted;
3791 const_wanted = 1;
3792 expr_eq();
3793 const_wanted = a;
3796 /* parse an integer constant and return its value. */
3797 static int expr_const(void)
3799 int c;
3800 expr_const1();
3801 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
3802 expect("constant expression");
3803 c = vtop->c.i;
3804 vpop();
3805 return c;
3808 /* return the label token if current token is a label, otherwise
3809 return zero */
3810 static int is_label(void)
3812 int last_tok;
3814 /* fast test first */
3815 if (tok < TOK_UIDENT)
3816 return 0;
3817 /* no need to save tokc because tok is an identifier */
3818 last_tok = tok;
3819 next();
3820 if (tok == ':') {
3821 next();
3822 return last_tok;
3823 } else {
3824 unget_tok(last_tok);
3825 return 0;
3829 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
3830 int case_reg, int is_expr)
3832 int a, b, c, d;
3833 Sym *s;
3835 /* generate line number info */
3836 if (tcc_state->do_debug &&
3837 (last_line_num != file->line_num || last_ind != ind)) {
3838 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
3839 last_ind = ind;
3840 last_line_num = file->line_num;
3843 if (is_expr) {
3844 /* default return value is (void) */
3845 vpushi(0);
3846 vtop->type.t = VT_VOID;
3849 if (tok == TOK_IF) {
3850 /* if test */
3851 next();
3852 skip('(');
3853 gexpr();
3854 skip(')');
3855 a = gtst(1, 0);
3856 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3857 c = tok;
3858 if (c == TOK_ELSE) {
3859 next();
3860 d = gjmp(0);
3861 gsym(a);
3862 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3863 gsym(d); /* patch else jmp */
3864 } else
3865 gsym(a);
3866 } else if (tok == TOK_WHILE) {
3867 next();
3868 d = ind;
3869 skip('(');
3870 gexpr();
3871 skip(')');
3872 a = gtst(1, 0);
3873 b = 0;
3874 block(&a, &b, case_sym, def_sym, case_reg, 0);
3875 gjmp_addr(d);
3876 gsym(a);
3877 gsym_addr(b, d);
3878 } else if (tok == '{') {
3879 Sym *llabel;
3881 next();
3882 /* record local declaration stack position */
3883 s = local_stack;
3884 llabel = local_label_stack;
3885 /* handle local labels declarations */
3886 if (tok == TOK_LABEL) {
3887 next();
3888 for(;;) {
3889 if (tok < TOK_UIDENT)
3890 expect("label identifier");
3891 label_push(&local_label_stack, tok, LABEL_DECLARED);
3892 next();
3893 if (tok == ',') {
3894 next();
3895 } else {
3896 skip(';');
3897 break;
3901 while (tok != '}') {
3902 decl(VT_LOCAL);
3903 if (tok != '}') {
3904 if (is_expr)
3905 vpop();
3906 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
3909 /* pop locally defined labels */
3910 label_pop(&local_label_stack, llabel);
3911 /* pop locally defined symbols */
3912 if(is_expr) {
3913 /* XXX: this solution makes only valgrind happy...
3914 triggered by gcc.c-torture/execute/20000917-1.c */
3915 Sym *p;
3916 switch(vtop->type.t & VT_BTYPE) {
3917 case VT_PTR:
3918 case VT_STRUCT:
3919 case VT_ENUM:
3920 case VT_FUNC:
3921 for(p=vtop->type.ref;p;p=p->prev)
3922 if(p->prev==s)
3923 error("unsupported expression type");
3926 sym_pop(&local_stack, s);
3927 next();
3928 } else if (tok == TOK_RETURN) {
3929 next();
3930 if (tok != ';') {
3931 gexpr();
3932 gen_assign_cast(&func_vt);
3933 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
3934 CType type;
3935 /* if returning structure, must copy it to implicit
3936 first pointer arg location */
3937 #ifdef TCC_ARM_EABI
3938 int align, size;
3939 size = type_size(&func_vt,&align);
3940 if(size <= 4)
3942 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
3943 && (align & 3))
3945 int addr;
3946 loc = (loc - size) & -4;
3947 addr = loc;
3948 type = func_vt;
3949 vset(&type, VT_LOCAL | VT_LVAL, addr);
3950 vswap();
3951 vstore();
3952 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
3954 vtop->type = int_type;
3955 gv(RC_IRET);
3956 } else {
3957 #endif
3958 type = func_vt;
3959 mk_pointer(&type);
3960 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
3961 indir();
3962 vswap();
3963 /* copy structure value to pointer */
3964 vstore();
3965 #ifdef TCC_ARM_EABI
3967 #endif
3968 } else if (is_float(func_vt.t)) {
3969 gv(rc_fret(func_vt.t));
3970 } else {
3971 gv(RC_IRET);
3973 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3975 skip(';');
3976 rsym = gjmp(rsym); /* jmp */
3977 } else if (tok == TOK_BREAK) {
3978 /* compute jump */
3979 if (!bsym)
3980 error("cannot break");
3981 *bsym = gjmp(*bsym);
3982 next();
3983 skip(';');
3984 } else if (tok == TOK_CONTINUE) {
3985 /* compute jump */
3986 if (!csym)
3987 error("cannot continue");
3988 *csym = gjmp(*csym);
3989 next();
3990 skip(';');
3991 } else if (tok == TOK_FOR) {
3992 int e;
3993 next();
3994 skip('(');
3995 if (tok != ';') {
3996 gexpr();
3997 vpop();
3999 skip(';');
4000 d = ind;
4001 c = ind;
4002 a = 0;
4003 b = 0;
4004 if (tok != ';') {
4005 gexpr();
4006 a = gtst(1, 0);
4008 skip(';');
4009 if (tok != ')') {
4010 e = gjmp(0);
4011 c = ind;
4012 gexpr();
4013 vpop();
4014 gjmp_addr(d);
4015 gsym(e);
4017 skip(')');
4018 block(&a, &b, case_sym, def_sym, case_reg, 0);
4019 gjmp_addr(c);
4020 gsym(a);
4021 gsym_addr(b, c);
4022 } else
4023 if (tok == TOK_DO) {
4024 next();
4025 a = 0;
4026 b = 0;
4027 d = ind;
4028 block(&a, &b, case_sym, def_sym, case_reg, 0);
4029 skip(TOK_WHILE);
4030 skip('(');
4031 gsym(b);
4032 gexpr();
4033 c = gtst(0, 0);
4034 gsym_addr(c, d);
4035 skip(')');
4036 gsym(a);
4037 skip(';');
4038 } else
4039 if (tok == TOK_SWITCH) {
4040 next();
4041 skip('(');
4042 gexpr();
4043 /* XXX: other types than integer */
4044 case_reg = gv(RC_INT);
4045 vpop();
4046 skip(')');
4047 a = 0;
4048 b = gjmp(0); /* jump to first case */
4049 c = 0;
4050 block(&a, csym, &b, &c, case_reg, 0);
4051 /* if no default, jmp after switch */
4052 if (c == 0)
4053 c = ind;
4054 /* default label */
4055 gsym_addr(b, c);
4056 /* break label */
4057 gsym(a);
4058 } else
4059 if (tok == TOK_CASE) {
4060 int v1, v2;
4061 if (!case_sym)
4062 expect("switch");
4063 next();
4064 v1 = expr_const();
4065 v2 = v1;
4066 if (gnu_ext && tok == TOK_DOTS) {
4067 next();
4068 v2 = expr_const();
4069 if (v2 < v1)
4070 warning("empty case range");
4072 /* since a case is like a label, we must skip it with a jmp */
4073 b = gjmp(0);
4074 gsym(*case_sym);
4075 vseti(case_reg, 0);
4076 vpushi(v1);
4077 if (v1 == v2) {
4078 gen_op(TOK_EQ);
4079 *case_sym = gtst(1, 0);
4080 } else {
4081 gen_op(TOK_GE);
4082 *case_sym = gtst(1, 0);
4083 vseti(case_reg, 0);
4084 vpushi(v2);
4085 gen_op(TOK_LE);
4086 *case_sym = gtst(1, *case_sym);
4088 gsym(b);
4089 skip(':');
4090 is_expr = 0;
4091 goto block_after_label;
4092 } else
4093 if (tok == TOK_DEFAULT) {
4094 next();
4095 skip(':');
4096 if (!def_sym)
4097 expect("switch");
4098 if (*def_sym)
4099 error("too many 'default'");
4100 *def_sym = ind;
4101 is_expr = 0;
4102 goto block_after_label;
4103 } else
4104 if (tok == TOK_GOTO) {
4105 next();
4106 if (tok == '*' && gnu_ext) {
4107 /* computed goto */
4108 next();
4109 gexpr();
4110 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4111 expect("pointer");
4112 ggoto();
4113 } else if (tok >= TOK_UIDENT) {
4114 s = label_find(tok);
4115 /* put forward definition if needed */
4116 if (!s) {
4117 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4118 } else {
4119 if (s->r == LABEL_DECLARED)
4120 s->r = LABEL_FORWARD;
4122 /* label already defined */
4123 if (s->r & LABEL_FORWARD)
4124 s->jnext = gjmp(s->jnext);
4125 else
4126 gjmp_addr(s->jnext);
4127 next();
4128 } else {
4129 expect("label identifier");
4131 skip(';');
4132 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4133 asm_instr();
4134 } else {
4135 b = is_label();
4136 if (b) {
4137 /* label case */
4138 s = label_find(b);
4139 if (s) {
4140 if (s->r == LABEL_DEFINED)
4141 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4142 gsym(s->jnext);
4143 s->r = LABEL_DEFINED;
4144 } else {
4145 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4147 s->jnext = ind;
4148 /* we accept this, but it is a mistake */
4149 block_after_label:
4150 if (tok == '}') {
4151 warning("deprecated use of label at end of compound statement");
4152 } else {
4153 if (is_expr)
4154 vpop();
4155 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4157 } else {
4158 /* expression case */
4159 if (tok != ';') {
4160 if (is_expr) {
4161 vpop();
4162 gexpr();
4163 } else {
4164 gexpr();
4165 vpop();
4168 skip(';');
4173 /* t is the array or struct type. c is the array or struct
4174 address. cur_index/cur_field is the pointer to the current
4175 value. 'size_only' is true if only size info is needed (only used
4176 in arrays) */
4177 static void decl_designator(CType *type, Section *sec, unsigned long c,
4178 int *cur_index, Sym **cur_field,
4179 int size_only)
4181 Sym *s, *f;
4182 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4183 CType type1;
4185 notfirst = 0;
4186 elem_size = 0;
4187 nb_elems = 1;
4188 if (gnu_ext && (l = is_label()) != 0)
4189 goto struct_field;
4190 while (tok == '[' || tok == '.') {
4191 if (tok == '[') {
4192 if (!(type->t & VT_ARRAY))
4193 expect("array type");
4194 s = type->ref;
4195 next();
4196 index = expr_const();
4197 if (index < 0 || (s->c >= 0 && index >= s->c))
4198 expect("invalid index");
4199 if (tok == TOK_DOTS && gnu_ext) {
4200 next();
4201 index_last = expr_const();
4202 if (index_last < 0 ||
4203 (s->c >= 0 && index_last >= s->c) ||
4204 index_last < index)
4205 expect("invalid index");
4206 } else {
4207 index_last = index;
4209 skip(']');
4210 if (!notfirst)
4211 *cur_index = index_last;
4212 type = pointed_type(type);
4213 elem_size = type_size(type, &align);
4214 c += index * elem_size;
4215 /* NOTE: we only support ranges for last designator */
4216 nb_elems = index_last - index + 1;
4217 if (nb_elems != 1) {
4218 notfirst = 1;
4219 break;
4221 } else {
4222 next();
4223 l = tok;
4224 next();
4225 struct_field:
4226 if ((type->t & VT_BTYPE) != VT_STRUCT)
4227 expect("struct/union type");
4228 s = type->ref;
4229 l |= SYM_FIELD;
4230 f = s->next;
4231 while (f) {
4232 if (f->v == l)
4233 break;
4234 f = f->next;
4236 if (!f)
4237 expect("field");
4238 if (!notfirst)
4239 *cur_field = f;
4240 /* XXX: fix this mess by using explicit storage field */
4241 type1 = f->type;
4242 type1.t |= (type->t & ~VT_TYPE);
4243 type = &type1;
4244 c += f->c;
4246 notfirst = 1;
4248 if (notfirst) {
4249 if (tok == '=') {
4250 next();
4251 } else {
4252 if (!gnu_ext)
4253 expect("=");
4255 } else {
4256 if (type->t & VT_ARRAY) {
4257 index = *cur_index;
4258 type = pointed_type(type);
4259 c += index * type_size(type, &align);
4260 } else {
4261 f = *cur_field;
4262 if (!f)
4263 error("too many field init");
4264 /* XXX: fix this mess by using explicit storage field */
4265 type1 = f->type;
4266 type1.t |= (type->t & ~VT_TYPE);
4267 type = &type1;
4268 c += f->c;
4271 decl_initializer(type, sec, c, 0, size_only);
4273 /* XXX: make it more general */
4274 if (!size_only && nb_elems > 1) {
4275 unsigned long c_end;
4276 uint8_t *src, *dst;
4277 int i;
4279 if (!sec)
4280 error("range init not supported yet for dynamic storage");
4281 c_end = c + nb_elems * elem_size;
4282 if (c_end > sec->data_allocated)
4283 section_realloc(sec, c_end);
4284 src = sec->data + c;
4285 dst = src;
4286 for(i = 1; i < nb_elems; i++) {
4287 dst += elem_size;
4288 memcpy(dst, src, elem_size);
4293 #define EXPR_VAL 0
4294 #define EXPR_CONST 1
4295 #define EXPR_ANY 2
4297 /* store a value or an expression directly in global data or in local array */
4298 static void init_putv(CType *type, Section *sec, unsigned long c,
4299 int v, int expr_type)
4301 int saved_global_expr, bt, bit_pos, bit_size;
4302 void *ptr;
4303 unsigned long long bit_mask;
4304 CType dtype;
4306 switch(expr_type) {
4307 case EXPR_VAL:
4308 vpushi(v);
4309 break;
4310 case EXPR_CONST:
4311 /* compound literals must be allocated globally in this case */
4312 saved_global_expr = global_expr;
4313 global_expr = 1;
4314 expr_const1();
4315 global_expr = saved_global_expr;
4316 /* NOTE: symbols are accepted */
4317 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4318 error("initializer element is not constant");
4319 break;
4320 case EXPR_ANY:
4321 expr_eq();
4322 break;
4325 dtype = *type;
4326 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4328 if (sec) {
4329 /* XXX: not portable */
4330 /* XXX: generate error if incorrect relocation */
4331 gen_assign_cast(&dtype);
4332 bt = type->t & VT_BTYPE;
4333 /* we'll write at most 12 bytes */
4334 if (c + 12 > sec->data_allocated) {
4335 section_realloc(sec, c + 12);
4337 ptr = sec->data + c;
4338 /* XXX: make code faster ? */
4339 if (!(type->t & VT_BITFIELD)) {
4340 bit_pos = 0;
4341 bit_size = 32;
4342 bit_mask = -1LL;
4343 } else {
4344 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4345 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4346 bit_mask = (1LL << bit_size) - 1;
4348 if ((vtop->r & VT_SYM) &&
4349 (bt == VT_BYTE ||
4350 bt == VT_SHORT ||
4351 bt == VT_DOUBLE ||
4352 bt == VT_LDOUBLE ||
4353 bt == VT_LLONG ||
4354 (bt == VT_INT && bit_size != 32)))
4355 error("initializer element is not computable at load time");
4356 switch(bt) {
4357 case VT_BOOL:
4358 vtop->c.i = (vtop->c.i != 0);
4359 case VT_BYTE:
4360 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4361 break;
4362 case VT_SHORT:
4363 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4364 break;
4365 case VT_DOUBLE:
4366 *(double *)ptr = vtop->c.d;
4367 break;
4368 case VT_LDOUBLE:
4369 *(long double *)ptr = vtop->c.ld;
4370 break;
4371 case VT_LLONG:
4372 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4373 break;
4374 default:
4375 if (vtop->r & VT_SYM) {
4376 greloc(sec, vtop->sym, c, R_DATA_PTR);
4378 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4379 break;
4381 vtop--;
4382 } else {
4383 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4384 vswap();
4385 vstore();
4386 vpop();
4390 /* put zeros for variable based init */
4391 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4393 if (sec) {
4394 /* nothing to do because globals are already set to zero */
4395 } else {
4396 vpush_global_sym(&func_old_type, TOK_memset);
4397 vseti(VT_LOCAL, c);
4398 vpushi(0);
4399 vpushi(size);
4400 gfunc_call(3);
4404 /* 't' contains the type and storage info. 'c' is the offset of the
4405 object in section 'sec'. If 'sec' is NULL, it means stack based
4406 allocation. 'first' is true if array '{' must be read (multi
4407 dimension implicit array init handling). 'size_only' is true if
4408 size only evaluation is wanted (only for arrays). */
4409 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4410 int first, int size_only)
4412 int index, array_length, n, no_oblock, nb, parlevel, i;
4413 int size1, align1, expr_type;
4414 Sym *s, *f;
4415 CType *t1;
4417 if (type->t & VT_ARRAY) {
4418 s = type->ref;
4419 n = s->c;
4420 array_length = 0;
4421 t1 = pointed_type(type);
4422 size1 = type_size(t1, &align1);
4424 no_oblock = 1;
4425 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4426 tok == '{') {
4427 skip('{');
4428 no_oblock = 0;
4431 /* only parse strings here if correct type (otherwise: handle
4432 them as ((w)char *) expressions */
4433 if ((tok == TOK_LSTR &&
4434 #ifdef TCC_TARGET_PE
4435 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4436 #else
4437 (t1->t & VT_BTYPE) == VT_INT
4438 #endif
4439 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4440 while (tok == TOK_STR || tok == TOK_LSTR) {
4441 int cstr_len, ch;
4442 CString *cstr;
4444 cstr = tokc.cstr;
4445 /* compute maximum number of chars wanted */
4446 if (tok == TOK_STR)
4447 cstr_len = cstr->size;
4448 else
4449 cstr_len = cstr->size / sizeof(nwchar_t);
4450 cstr_len--;
4451 nb = cstr_len;
4452 if (n >= 0 && nb > (n - array_length))
4453 nb = n - array_length;
4454 if (!size_only) {
4455 if (cstr_len > nb)
4456 warning("initializer-string for array is too long");
4457 /* in order to go faster for common case (char
4458 string in global variable, we handle it
4459 specifically */
4460 if (sec && tok == TOK_STR && size1 == 1) {
4461 memcpy(sec->data + c + array_length, cstr->data, nb);
4462 } else {
4463 for(i=0;i<nb;i++) {
4464 if (tok == TOK_STR)
4465 ch = ((unsigned char *)cstr->data)[i];
4466 else
4467 ch = ((nwchar_t *)cstr->data)[i];
4468 init_putv(t1, sec, c + (array_length + i) * size1,
4469 ch, EXPR_VAL);
4473 array_length += nb;
4474 next();
4476 /* only add trailing zero if enough storage (no
4477 warning in this case since it is standard) */
4478 if (n < 0 || array_length < n) {
4479 if (!size_only) {
4480 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4482 array_length++;
4484 } else {
4485 index = 0;
4486 while (tok != '}') {
4487 decl_designator(type, sec, c, &index, NULL, size_only);
4488 if (n >= 0 && index >= n)
4489 error("index too large");
4490 /* must put zero in holes (note that doing it that way
4491 ensures that it even works with designators) */
4492 if (!size_only && array_length < index) {
4493 init_putz(t1, sec, c + array_length * size1,
4494 (index - array_length) * size1);
4496 index++;
4497 if (index > array_length)
4498 array_length = index;
4499 /* special test for multi dimensional arrays (may not
4500 be strictly correct if designators are used at the
4501 same time) */
4502 if (index >= n && no_oblock)
4503 break;
4504 if (tok == '}')
4505 break;
4506 skip(',');
4509 if (!no_oblock)
4510 skip('}');
4511 /* put zeros at the end */
4512 if (!size_only && n >= 0 && array_length < n) {
4513 init_putz(t1, sec, c + array_length * size1,
4514 (n - array_length) * size1);
4516 /* patch type size if needed */
4517 if (n < 0)
4518 s->c = array_length;
4519 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4520 (sec || !first || tok == '{')) {
4521 int par_count;
4523 /* NOTE: the previous test is a specific case for automatic
4524 struct/union init */
4525 /* XXX: union needs only one init */
4527 /* XXX: this test is incorrect for local initializers
4528 beginning with ( without {. It would be much more difficult
4529 to do it correctly (ideally, the expression parser should
4530 be used in all cases) */
4531 par_count = 0;
4532 if (tok == '(') {
4533 AttributeDef ad1;
4534 CType type1;
4535 next();
4536 while (tok == '(') {
4537 par_count++;
4538 next();
4540 if (!parse_btype(&type1, &ad1))
4541 expect("cast");
4542 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4543 #if 0
4544 if (!is_assignable_types(type, &type1))
4545 error("invalid type for cast");
4546 #endif
4547 skip(')');
4549 no_oblock = 1;
4550 if (first || tok == '{') {
4551 skip('{');
4552 no_oblock = 0;
4554 s = type->ref;
4555 f = s->next;
4556 array_length = 0;
4557 index = 0;
4558 n = s->c;
4559 while (tok != '}') {
4560 decl_designator(type, sec, c, NULL, &f, size_only);
4561 index = f->c;
4562 if (!size_only && array_length < index) {
4563 init_putz(type, sec, c + array_length,
4564 index - array_length);
4566 index = index + type_size(&f->type, &align1);
4567 if (index > array_length)
4568 array_length = index;
4570 /* gr: skip fields from same union - ugly. */
4571 while (f->next) {
4572 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4573 /* test for same offset */
4574 if (f->next->c != f->c)
4575 break;
4576 /* if yes, test for bitfield shift */
4577 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4578 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4579 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4580 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4581 if (bit_pos_1 != bit_pos_2)
4582 break;
4584 f = f->next;
4587 f = f->next;
4588 if (no_oblock && f == NULL)
4589 break;
4590 if (tok == '}')
4591 break;
4592 skip(',');
4594 /* put zeros at the end */
4595 if (!size_only && array_length < n) {
4596 init_putz(type, sec, c + array_length,
4597 n - array_length);
4599 if (!no_oblock)
4600 skip('}');
4601 while (par_count) {
4602 skip(')');
4603 par_count--;
4605 } else if (tok == '{') {
4606 next();
4607 decl_initializer(type, sec, c, first, size_only);
4608 skip('}');
4609 } else if (size_only) {
4610 /* just skip expression */
4611 parlevel = 0;
4612 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4613 tok != -1) {
4614 if (tok == '(')
4615 parlevel++;
4616 else if (tok == ')')
4617 parlevel--;
4618 next();
4620 } else {
4621 /* currently, we always use constant expression for globals
4622 (may change for scripting case) */
4623 expr_type = EXPR_CONST;
4624 if (!sec)
4625 expr_type = EXPR_ANY;
4626 init_putv(type, sec, c, 0, expr_type);
4630 /* parse an initializer for type 't' if 'has_init' is non zero, and
4631 allocate space in local or global data space ('r' is either
4632 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4633 variable 'v' of scope 'scope' is declared before initializers are
4634 parsed. If 'v' is zero, then a reference to the new object is put
4635 in the value stack. If 'has_init' is 2, a special parsing is done
4636 to handle string constants. */
4637 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4638 int has_init, int v, int scope)
4640 int size, align, addr, data_offset;
4641 int level;
4642 ParseState saved_parse_state = {0};
4643 TokenString init_str;
4644 Section *sec;
4646 size = type_size(type, &align);
4647 /* If unknown size, we must evaluate it before
4648 evaluating initializers because
4649 initializers can generate global data too
4650 (e.g. string pointers or ISOC99 compound
4651 literals). It also simplifies local
4652 initializers handling */
4653 tok_str_new(&init_str);
4654 if (size < 0) {
4655 if (!has_init)
4656 error("unknown type size");
4657 /* get all init string */
4658 if (has_init == 2) {
4659 /* only get strings */
4660 while (tok == TOK_STR || tok == TOK_LSTR) {
4661 tok_str_add_tok(&init_str);
4662 next();
4664 } else {
4665 level = 0;
4666 while (level > 0 || (tok != ',' && tok != ';')) {
4667 if (tok < 0)
4668 error("unexpected end of file in initializer");
4669 tok_str_add_tok(&init_str);
4670 if (tok == '{')
4671 level++;
4672 else if (tok == '}') {
4673 level--;
4674 if (level <= 0) {
4675 next();
4676 break;
4679 next();
4682 tok_str_add(&init_str, -1);
4683 tok_str_add(&init_str, 0);
4685 /* compute size */
4686 save_parse_state(&saved_parse_state);
4688 macro_ptr = init_str.str;
4689 next();
4690 decl_initializer(type, NULL, 0, 1, 1);
4691 /* prepare second initializer parsing */
4692 macro_ptr = init_str.str;
4693 next();
4695 /* if still unknown size, error */
4696 size = type_size(type, &align);
4697 if (size < 0)
4698 error("unknown type size");
4700 /* take into account specified alignment if bigger */
4701 if (ad->aligned) {
4702 if (ad->aligned > align)
4703 align = ad->aligned;
4704 } else if (ad->packed) {
4705 align = 1;
4707 if ((r & VT_VALMASK) == VT_LOCAL) {
4708 sec = NULL;
4709 #ifdef CONFIG_TCC_BCHECK
4710 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4711 loc--;
4712 #endif
4713 loc = (loc - size) & -align;
4714 addr = loc;
4715 #ifdef CONFIG_TCC_BCHECK
4716 /* handles bounds */
4717 /* XXX: currently, since we do only one pass, we cannot track
4718 '&' operators, so we add only arrays */
4719 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4720 unsigned long *bounds_ptr;
4721 /* add padding between regions */
4722 loc--;
4723 /* then add local bound info */
4724 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4725 bounds_ptr[0] = addr;
4726 bounds_ptr[1] = size;
4728 #endif
4729 if (v) {
4730 /* local variable */
4731 sym_push(v, type, r, addr);
4732 } else {
4733 /* push local reference */
4734 vset(type, r, addr);
4736 } else {
4737 Sym *sym;
4739 sym = NULL;
4740 if (v && scope == VT_CONST) {
4741 /* see if the symbol was already defined */
4742 sym = sym_find(v);
4743 if (sym) {
4744 if (!is_compatible_types(&sym->type, type))
4745 error("incompatible types for redefinition of '%s'",
4746 get_tok_str(v, NULL));
4747 if (sym->type.t & VT_EXTERN) {
4748 /* if the variable is extern, it was not allocated */
4749 sym->type.t &= ~VT_EXTERN;
4750 /* set array size if it was ommited in extern
4751 declaration */
4752 if ((sym->type.t & VT_ARRAY) &&
4753 sym->type.ref->c < 0 &&
4754 type->ref->c >= 0)
4755 sym->type.ref->c = type->ref->c;
4756 } else {
4757 /* we accept several definitions of the same
4758 global variable. this is tricky, because we
4759 must play with the SHN_COMMON type of the symbol */
4760 /* XXX: should check if the variable was already
4761 initialized. It is incorrect to initialized it
4762 twice */
4763 /* no init data, we won't add more to the symbol */
4764 if (!has_init)
4765 goto no_alloc;
4770 /* allocate symbol in corresponding section */
4771 sec = ad->section;
4772 if (!sec) {
4773 if (has_init)
4774 sec = data_section;
4775 else if (tcc_state->nocommon)
4776 sec = bss_section;
4778 if (sec) {
4779 data_offset = sec->data_offset;
4780 data_offset = (data_offset + align - 1) & -align;
4781 addr = data_offset;
4782 /* very important to increment global pointer at this time
4783 because initializers themselves can create new initializers */
4784 data_offset += size;
4785 #ifdef CONFIG_TCC_BCHECK
4786 /* add padding if bound check */
4787 if (tcc_state->do_bounds_check)
4788 data_offset++;
4789 #endif
4790 sec->data_offset = data_offset;
4791 /* allocate section space to put the data */
4792 if (sec->sh_type != SHT_NOBITS &&
4793 data_offset > sec->data_allocated)
4794 section_realloc(sec, data_offset);
4795 /* align section if needed */
4796 if (align > sec->sh_addralign)
4797 sec->sh_addralign = align;
4798 } else {
4799 addr = 0; /* avoid warning */
4802 if (v) {
4803 if (scope != VT_CONST || !sym) {
4804 sym = sym_push(v, type, r | VT_SYM, 0);
4806 /* update symbol definition */
4807 if (sec) {
4808 put_extern_sym(sym, sec, addr, size);
4809 } else {
4810 ElfW(Sym) *esym;
4811 /* put a common area */
4812 put_extern_sym(sym, NULL, align, size);
4813 /* XXX: find a nicer way */
4814 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
4815 esym->st_shndx = SHN_COMMON;
4817 } else {
4818 CValue cval;
4820 /* push global reference */
4821 sym = get_sym_ref(type, sec, addr, size);
4822 cval.ul = 0;
4823 vsetc(type, VT_CONST | VT_SYM, &cval);
4824 vtop->sym = sym;
4826 #ifdef CONFIG_TCC_BCHECK
4827 /* handles bounds now because the symbol must be defined
4828 before for the relocation */
4829 if (tcc_state->do_bounds_check) {
4830 unsigned long *bounds_ptr;
4832 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
4833 /* then add global bound info */
4834 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
4835 bounds_ptr[0] = 0; /* relocated */
4836 bounds_ptr[1] = size;
4838 #endif
4840 if (has_init) {
4841 decl_initializer(type, sec, addr, 1, 0);
4842 /* restore parse state if needed */
4843 if (init_str.str) {
4844 tok_str_free(init_str.str);
4845 restore_parse_state(&saved_parse_state);
4848 no_alloc: ;
4851 void put_func_debug(Sym *sym)
4853 char buf[512];
4855 /* stabs info */
4856 /* XXX: we put here a dummy type */
4857 snprintf(buf, sizeof(buf), "%s:%c1",
4858 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
4859 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
4860 cur_text_section, sym->c);
4861 /* //gr gdb wants a line at the function */
4862 put_stabn(N_SLINE, 0, file->line_num, 0);
4863 last_ind = 0;
4864 last_line_num = 0;
4867 /* parse an old style function declaration list */
4868 /* XXX: check multiple parameter */
4869 static void func_decl_list(Sym *func_sym)
4871 AttributeDef ad;
4872 int v;
4873 Sym *s;
4874 CType btype, type;
4876 /* parse each declaration */
4877 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
4878 if (!parse_btype(&btype, &ad))
4879 expect("declaration list");
4880 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4881 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4882 tok == ';') {
4883 /* we accept no variable after */
4884 } else {
4885 for(;;) {
4886 type = btype;
4887 type_decl(&type, &ad, &v, TYPE_DIRECT);
4888 /* find parameter in function parameter list */
4889 s = func_sym->next;
4890 while (s != NULL) {
4891 if ((s->v & ~SYM_FIELD) == v)
4892 goto found;
4893 s = s->next;
4895 error("declaration for parameter '%s' but no such parameter",
4896 get_tok_str(v, NULL));
4897 found:
4898 /* check that no storage specifier except 'register' was given */
4899 if (type.t & VT_STORAGE)
4900 error("storage class specified for '%s'", get_tok_str(v, NULL));
4901 convert_parameter_type(&type);
4902 /* we can add the type (NOTE: it could be local to the function) */
4903 s->type = type;
4904 /* accept other parameters */
4905 if (tok == ',')
4906 next();
4907 else
4908 break;
4911 skip(';');
4915 /* parse a function defined by symbol 'sym' and generate its code in
4916 'cur_text_section' */
4917 static void gen_function(Sym *sym)
4919 int saved_nocode_wanted = nocode_wanted;
4920 nocode_wanted = 0;
4921 ind = cur_text_section->data_offset;
4922 /* NOTE: we patch the symbol size later */
4923 put_extern_sym(sym, cur_text_section, ind, 0);
4924 funcname = get_tok_str(sym->v, NULL);
4925 func_ind = ind;
4926 /* put debug symbol */
4927 if (tcc_state->do_debug)
4928 put_func_debug(sym);
4929 /* push a dummy symbol to enable local sym storage */
4930 sym_push2(&local_stack, SYM_FIELD, 0, 0);
4931 gfunc_prolog(&sym->type);
4932 rsym = 0;
4933 block(NULL, NULL, NULL, NULL, 0, 0);
4934 gsym(rsym);
4935 gfunc_epilog();
4936 cur_text_section->data_offset = ind;
4937 label_pop(&global_label_stack, NULL);
4938 sym_pop(&local_stack, NULL); /* reset local stack */
4939 /* end of function */
4940 /* patch symbol size */
4941 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
4942 ind - func_ind;
4943 if (tcc_state->do_debug) {
4944 put_stabn(N_FUN, 0, 0, ind - func_ind);
4946 /* It's better to crash than to generate wrong code */
4947 cur_text_section = NULL;
4948 funcname = ""; /* for safety */
4949 func_vt.t = VT_VOID; /* for safety */
4950 ind = 0; /* for safety */
4951 nocode_wanted = saved_nocode_wanted;
4954 static void gen_inline_functions(void)
4956 Sym *sym;
4957 int *str, inline_generated, i;
4958 struct InlineFunc *fn;
4960 /* iterate while inline function are referenced */
4961 for(;;) {
4962 inline_generated = 0;
4963 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4964 fn = tcc_state->inline_fns[i];
4965 sym = fn->sym;
4966 if (sym && sym->c) {
4967 /* the function was used: generate its code and
4968 convert it to a normal function */
4969 str = fn->token_str;
4970 fn->sym = NULL;
4971 if (file)
4972 strcpy(file->filename, fn->filename);
4973 sym->r = VT_SYM | VT_CONST;
4974 sym->type.t &= ~VT_INLINE;
4976 macro_ptr = str;
4977 next();
4978 cur_text_section = text_section;
4979 gen_function(sym);
4980 macro_ptr = NULL; /* fail safe */
4982 inline_generated = 1;
4985 if (!inline_generated)
4986 break;
4988 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4989 fn = tcc_state->inline_fns[i];
4990 str = fn->token_str;
4991 tok_str_free(str);
4993 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
4996 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4997 static void decl(int l)
4999 int v, has_init, r;
5000 CType type, btype;
5001 Sym *sym;
5002 AttributeDef ad;
5004 while (1) {
5005 if (!parse_btype(&btype, &ad)) {
5006 /* skip redundant ';' */
5007 /* XXX: find more elegant solution */
5008 if (tok == ';') {
5009 next();
5010 continue;
5012 if (l == VT_CONST &&
5013 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5014 /* global asm block */
5015 asm_global_instr();
5016 continue;
5018 /* special test for old K&R protos without explicit int
5019 type. Only accepted when defining global data */
5020 if (l == VT_LOCAL || tok < TOK_DEFINE)
5021 break;
5022 btype.t = VT_INT;
5024 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5025 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5026 tok == ';') {
5027 /* we accept no variable after */
5028 next();
5029 continue;
5031 while (1) { /* iterate thru each declaration */
5032 type = btype;
5033 type_decl(&type, &ad, &v, TYPE_DIRECT);
5034 #if 0
5036 char buf[500];
5037 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5038 printf("type = '%s'\n", buf);
5040 #endif
5041 if ((type.t & VT_BTYPE) == VT_FUNC) {
5042 /* if old style function prototype, we accept a
5043 declaration list */
5044 sym = type.ref;
5045 if (sym->c == FUNC_OLD)
5046 func_decl_list(sym);
5049 if (tok == '{') {
5050 if (l == VT_LOCAL)
5051 error("cannot use local functions");
5052 if ((type.t & VT_BTYPE) != VT_FUNC)
5053 expect("function definition");
5055 /* reject abstract declarators in function definition */
5056 sym = type.ref;
5057 while ((sym = sym->next) != NULL)
5058 if (!(sym->v & ~SYM_FIELD))
5059 expect("identifier");
5061 /* XXX: cannot do better now: convert extern line to static inline */
5062 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5063 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5065 sym = sym_find(v);
5066 if (sym) {
5067 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5068 goto func_error1;
5070 r = sym->type.ref->r;
5071 /* use func_call from prototype if not defined */
5072 if (FUNC_CALL(r) != FUNC_CDECL
5073 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5074 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5076 /* use export from prototype */
5077 if (FUNC_EXPORT(r))
5078 FUNC_EXPORT(type.ref->r) = 1;
5080 /* use static from prototype */
5081 if (sym->type.t & VT_STATIC)
5082 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5084 if (!is_compatible_types(&sym->type, &type)) {
5085 func_error1:
5086 error("incompatible types for redefinition of '%s'",
5087 get_tok_str(v, NULL));
5089 /* if symbol is already defined, then put complete type */
5090 sym->type = type;
5091 } else {
5092 /* put function symbol */
5093 sym = global_identifier_push(v, type.t, 0);
5094 sym->type.ref = type.ref;
5097 /* static inline functions are just recorded as a kind
5098 of macro. Their code will be emitted at the end of
5099 the compilation unit only if they are used */
5100 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5101 (VT_INLINE | VT_STATIC)) {
5102 TokenString func_str;
5103 int block_level;
5104 struct InlineFunc *fn;
5105 const char *filename;
5107 tok_str_new(&func_str);
5109 block_level = 0;
5110 for(;;) {
5111 int t;
5112 if (tok == TOK_EOF)
5113 error("unexpected end of file");
5114 tok_str_add_tok(&func_str);
5115 t = tok;
5116 next();
5117 if (t == '{') {
5118 block_level++;
5119 } else if (t == '}') {
5120 block_level--;
5121 if (block_level == 0)
5122 break;
5125 tok_str_add(&func_str, -1);
5126 tok_str_add(&func_str, 0);
5127 filename = file ? file->filename : "";
5128 fn = tcc_malloc(sizeof *fn + strlen(filename));
5129 strcpy(fn->filename, filename);
5130 fn->sym = sym;
5131 fn->token_str = func_str.str;
5132 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5134 } else {
5135 /* compute text section */
5136 cur_text_section = ad.section;
5137 if (!cur_text_section)
5138 cur_text_section = text_section;
5139 sym->r = VT_SYM | VT_CONST;
5140 gen_function(sym);
5142 break;
5143 } else {
5144 if (btype.t & VT_TYPEDEF) {
5145 /* save typedefed type */
5146 /* XXX: test storage specifiers ? */
5147 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5148 sym->type.t |= VT_TYPEDEF;
5149 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5150 /* external function definition */
5151 /* specific case for func_call attribute */
5152 type.ref->r = INT_ATTR(&ad);
5153 external_sym(v, &type, 0);
5154 } else {
5155 /* not lvalue if array */
5156 r = 0;
5157 if (!(type.t & VT_ARRAY))
5158 r |= lvalue_type(type.t);
5159 has_init = (tok == '=');
5160 if ((btype.t & VT_EXTERN) ||
5161 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5162 !has_init && l == VT_CONST && type.ref->c < 0)) {
5163 /* external variable */
5164 /* NOTE: as GCC, uninitialized global static
5165 arrays of null size are considered as
5166 extern */
5167 #ifdef TCC_TARGET_PE
5168 if (ad.func_import)
5169 type.t |= VT_IMPORT;
5170 #endif
5171 external_sym(v, &type, r);
5172 } else {
5173 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5174 if (type.t & VT_STATIC)
5175 r |= VT_CONST;
5176 else
5177 r |= l;
5178 if (has_init)
5179 next();
5180 #ifdef TCC_TARGET_PE
5181 if (ad.func_export)
5182 type.t |= VT_EXPORT;
5183 #endif
5184 decl_initializer_alloc(&type, &ad, r,
5185 has_init, v, l);
5188 if (tok != ',') {
5189 skip(';');
5190 break;
5192 next();