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