pass constness from structs to members
[tinycc.git] / tccgen.c
blobfcfaa7ee38e873a3d5fb8e7f4bf4ba542c74d1c2
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 {
119 if (!is_compatible_types(&s->type, type))
120 error("incompatible types for redefinition of '%s'",
121 get_tok_str(v, NULL));
123 return s;
126 /* push a reference to global symbol v */
127 static void vpush_global_sym(CType *type, int v)
129 Sym *sym;
130 CValue cval;
132 sym = external_global_sym(v, type, 0);
133 cval.ul = 0;
134 vsetc(type, VT_CONST | VT_SYM, &cval);
135 vtop->sym = sym;
138 void vset(CType *type, int r, int v)
140 CValue cval;
142 cval.i = v;
143 vsetc(type, r, &cval);
146 void vseti(int r, int v)
148 CType type;
149 type.t = VT_INT;
150 vset(&type, r, v);
153 void vswap(void)
155 SValue tmp;
157 tmp = vtop[0];
158 vtop[0] = vtop[-1];
159 vtop[-1] = tmp;
162 void vpushv(SValue *v)
164 if (vtop >= vstack + (VSTACK_SIZE - 1))
165 error("memory full");
166 vtop++;
167 *vtop = *v;
170 void vdup(void)
172 vpushv(vtop);
175 /* save r to the memory stack, and mark it as being free */
176 void save_reg(int r)
178 int l, saved, size, align;
179 SValue *p, sv;
180 CType *type;
182 /* modify all stack values */
183 saved = 0;
184 l = 0;
185 for(p=vstack;p<=vtop;p++) {
186 if ((p->r & VT_VALMASK) == r ||
187 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
188 /* must save value on stack if not already done */
189 if (!saved) {
190 /* NOTE: must reload 'r' because r might be equal to r2 */
191 r = p->r & VT_VALMASK;
192 /* store register in the stack */
193 type = &p->type;
194 if ((p->r & VT_LVAL) ||
195 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
196 #ifdef TCC_TARGET_X86_64
197 type = &char_pointer_type;
198 #else
199 type = &int_type;
200 #endif
201 size = type_size(type, &align);
202 loc = (loc - size) & -align;
203 sv.type.t = type->t;
204 sv.r = VT_LOCAL | VT_LVAL;
205 sv.c.ul = loc;
206 store(r, &sv);
207 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
208 /* x86 specific: need to pop fp register ST0 if saved */
209 if (r == TREG_ST0) {
210 o(0xd9dd); /* fstp %st(1) */
212 #endif
213 #ifndef TCC_TARGET_X86_64
214 /* special long long case */
215 if ((type->t & VT_BTYPE) == VT_LLONG) {
216 sv.c.ul += 4;
217 store(p->r2, &sv);
219 #endif
220 l = loc;
221 saved = 1;
223 /* mark that stack entry as being saved on the stack */
224 if (p->r & VT_LVAL) {
225 /* also clear the bounded flag because the
226 relocation address of the function was stored in
227 p->c.ul */
228 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
229 } else {
230 p->r = lvalue_type(p->type.t) | VT_LOCAL;
232 p->r2 = VT_CONST;
233 p->c.ul = l;
238 /* find a register of class 'rc2' with at most one reference on stack.
239 * If none, call get_reg(rc) */
240 int get_reg_ex(int rc, int rc2)
242 int r;
243 SValue *p;
245 for(r=0;r<NB_REGS;r++) {
246 if (reg_classes[r] & rc2) {
247 int n;
248 n=0;
249 for(p = vstack; p <= vtop; p++) {
250 if ((p->r & VT_VALMASK) == r ||
251 (p->r2 & VT_VALMASK) == r)
252 n++;
254 if (n <= 1)
255 return r;
258 return get_reg(rc);
261 /* find a free register of class 'rc'. If none, save one register */
262 int get_reg(int rc)
264 int r;
265 SValue *p;
267 /* find a free register */
268 for(r=0;r<NB_REGS;r++) {
269 if (reg_classes[r] & rc) {
270 for(p=vstack;p<=vtop;p++) {
271 if ((p->r & VT_VALMASK) == r ||
272 (p->r2 & VT_VALMASK) == r)
273 goto notfound;
275 return r;
277 notfound: ;
280 /* no register left : free the first one on the stack (VERY
281 IMPORTANT to start from the bottom to ensure that we don't
282 spill registers used in gen_opi()) */
283 for(p=vstack;p<=vtop;p++) {
284 r = p->r & VT_VALMASK;
285 if (r < VT_CONST && (reg_classes[r] & rc))
286 goto save_found;
287 /* also look at second register (if long long) */
288 r = p->r2 & VT_VALMASK;
289 if (r < VT_CONST && (reg_classes[r] & rc)) {
290 save_found:
291 save_reg(r);
292 return r;
295 /* Should never comes here */
296 return -1;
299 /* save registers up to (vtop - n) stack entry */
300 void save_regs(int n)
302 int r;
303 SValue *p, *p1;
304 p1 = vtop - n;
305 for(p = vstack;p <= p1; p++) {
306 r = p->r & VT_VALMASK;
307 if (r < VT_CONST) {
308 save_reg(r);
313 /* move register 's' to 'r', and flush previous value of r to memory
314 if needed */
315 void move_reg(int r, int s)
317 SValue sv;
319 if (r != s) {
320 save_reg(r);
321 sv.type.t = VT_INT;
322 sv.r = s;
323 sv.c.ul = 0;
324 load(r, &sv);
328 /* get address of vtop (vtop MUST BE an lvalue) */
329 void gaddrof(void)
331 vtop->r &= ~VT_LVAL;
332 /* tricky: if saved lvalue, then we can go back to lvalue */
333 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
334 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
337 #ifdef CONFIG_TCC_BCHECK
338 /* generate lvalue bound code */
339 void gbound(void)
341 int lval_type;
342 CType type1;
344 vtop->r &= ~VT_MUSTBOUND;
345 /* if lvalue, then use checking code before dereferencing */
346 if (vtop->r & VT_LVAL) {
347 /* if not VT_BOUNDED value, then make one */
348 if (!(vtop->r & VT_BOUNDED)) {
349 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
350 /* must save type because we must set it to int to get pointer */
351 type1 = vtop->type;
352 vtop->type.t = VT_INT;
353 gaddrof();
354 vpushi(0);
355 gen_bounded_ptr_add();
356 vtop->r |= lval_type;
357 vtop->type = type1;
359 /* then check for dereferencing */
360 gen_bounded_ptr_deref();
363 #endif
365 /* store vtop a register belonging to class 'rc'. lvalues are
366 converted to values. Cannot be used if cannot be converted to
367 register value (such as structures). */
368 int gv(int rc)
370 int r, rc2, bit_pos, bit_size, size, align, i;
372 /* NOTE: get_reg can modify vstack[] */
373 if (vtop->type.t & VT_BITFIELD) {
374 CType type;
375 int bits = 32;
376 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
377 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
378 /* remove bit field info to avoid loops */
379 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
380 /* cast to int to propagate signedness in following ops */
381 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
382 type.t = VT_LLONG;
383 bits = 64;
384 } else
385 type.t = VT_INT;
386 if((vtop->type.t & VT_UNSIGNED) ||
387 (vtop->type.t & VT_BTYPE) == VT_BOOL)
388 type.t |= VT_UNSIGNED;
389 gen_cast(&type);
390 /* generate shifts */
391 vpushi(bits - (bit_pos + bit_size));
392 gen_op(TOK_SHL);
393 vpushi(bits - bit_size);
394 /* NOTE: transformed to SHR if unsigned */
395 gen_op(TOK_SAR);
396 r = gv(rc);
397 } else {
398 if (is_float(vtop->type.t) &&
399 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
400 Sym *sym;
401 int *ptr;
402 unsigned long offset;
403 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
404 CValue check;
405 #endif
407 /* XXX: unify with initializers handling ? */
408 /* CPUs usually cannot use float constants, so we store them
409 generically in data segment */
410 size = type_size(&vtop->type, &align);
411 offset = (data_section->data_offset + align - 1) & -align;
412 data_section->data_offset = offset;
413 /* XXX: not portable yet */
414 #if defined(__i386__) || defined(__x86_64__)
415 /* Zero pad x87 tenbyte long doubles */
416 if (size == LDOUBLE_SIZE)
417 vtop->c.tab[2] &= 0xffff;
418 #endif
419 ptr = section_ptr_add(data_section, size);
420 size = size >> 2;
421 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
422 check.d = 1;
423 if(check.tab[0])
424 for(i=0;i<size;i++)
425 ptr[i] = vtop->c.tab[size-1-i];
426 else
427 #endif
428 for(i=0;i<size;i++)
429 ptr[i] = vtop->c.tab[i];
430 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
431 vtop->r |= VT_LVAL | VT_SYM;
432 vtop->sym = sym;
433 vtop->c.ul = 0;
435 #ifdef CONFIG_TCC_BCHECK
436 if (vtop->r & VT_MUSTBOUND)
437 gbound();
438 #endif
440 r = vtop->r & VT_VALMASK;
441 rc2 = RC_INT;
442 if (rc == RC_IRET)
443 rc2 = RC_LRET;
444 /* need to reload if:
445 - constant
446 - lvalue (need to dereference pointer)
447 - already a register, but not in the right class */
448 if (r >= VT_CONST ||
449 (vtop->r & VT_LVAL) ||
450 !(reg_classes[r] & rc) ||
451 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
452 !(reg_classes[vtop->r2] & rc2))) {
453 r = get_reg(rc);
454 #ifndef TCC_TARGET_X86_64
455 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
456 int r2;
457 unsigned long long ll;
458 /* two register type load : expand to two words
459 temporarily */
460 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
461 /* load constant */
462 ll = vtop->c.ull;
463 vtop->c.ui = ll; /* first word */
464 load(r, vtop);
465 vtop->r = r; /* save register value */
466 vpushi(ll >> 32); /* second word */
467 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
468 (vtop->r & VT_LVAL)) {
469 /* We do not want to modifier the long long
470 pointer here, so the safest (and less
471 efficient) is to save all the other registers
472 in the stack. XXX: totally inefficient. */
473 save_regs(1);
474 /* load from memory */
475 load(r, vtop);
476 vdup();
477 vtop[-1].r = r; /* save register value */
478 /* increment pointer to get second word */
479 vtop->type.t = VT_INT;
480 gaddrof();
481 vpushi(4);
482 gen_op('+');
483 vtop->r |= VT_LVAL;
484 } else {
485 /* move registers */
486 load(r, vtop);
487 vdup();
488 vtop[-1].r = r; /* save register value */
489 vtop->r = vtop[-1].r2;
491 /* allocate second register */
492 r2 = get_reg(rc2);
493 load(r2, vtop);
494 vpop();
495 /* write second register */
496 vtop->r2 = r2;
497 } else
498 #endif
499 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
500 int t1, t;
501 /* lvalue of scalar type : need to use lvalue type
502 because of possible cast */
503 t = vtop->type.t;
504 t1 = t;
505 /* compute memory access type */
506 if (vtop->r & VT_LVAL_BYTE)
507 t = VT_BYTE;
508 else if (vtop->r & VT_LVAL_SHORT)
509 t = VT_SHORT;
510 if (vtop->r & VT_LVAL_UNSIGNED)
511 t |= VT_UNSIGNED;
512 vtop->type.t = t;
513 load(r, vtop);
514 /* restore wanted type */
515 vtop->type.t = t1;
516 } else {
517 /* one register type load */
518 load(r, vtop);
521 vtop->r = r;
522 #ifdef TCC_TARGET_C67
523 /* uses register pairs for doubles */
524 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
525 vtop->r2 = r+1;
526 #endif
528 return r;
531 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
532 void gv2(int rc1, int rc2)
534 int v;
536 /* generate more generic register first. But VT_JMP or VT_CMP
537 values must be generated first in all cases to avoid possible
538 reload errors */
539 v = vtop[0].r & VT_VALMASK;
540 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
541 vswap();
542 gv(rc1);
543 vswap();
544 gv(rc2);
545 /* test if reload is needed for first register */
546 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
547 vswap();
548 gv(rc1);
549 vswap();
551 } else {
552 gv(rc2);
553 vswap();
554 gv(rc1);
555 vswap();
556 /* test if reload is needed for first register */
557 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
558 gv(rc2);
563 /* wrapper around RC_FRET to return a register by type */
564 int rc_fret(int t)
566 #ifdef TCC_TARGET_X86_64
567 if (t == VT_LDOUBLE) {
568 return RC_ST0;
570 #endif
571 return RC_FRET;
574 /* wrapper around REG_FRET to return a register by type */
575 int reg_fret(int t)
577 #ifdef TCC_TARGET_X86_64
578 if (t == VT_LDOUBLE) {
579 return TREG_ST0;
581 #endif
582 return REG_FRET;
585 /* expand long long on stack in two int registers */
586 void lexpand(void)
588 int u;
590 u = vtop->type.t & VT_UNSIGNED;
591 gv(RC_INT);
592 vdup();
593 vtop[0].r = vtop[-1].r2;
594 vtop[0].r2 = VT_CONST;
595 vtop[-1].r2 = VT_CONST;
596 vtop[0].type.t = VT_INT | u;
597 vtop[-1].type.t = VT_INT | u;
600 #ifdef TCC_TARGET_ARM
601 /* expand long long on stack */
602 void lexpand_nr(void)
604 int u,v;
606 u = vtop->type.t & VT_UNSIGNED;
607 vdup();
608 vtop->r2 = VT_CONST;
609 vtop->type.t = VT_INT | u;
610 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
611 if (v == VT_CONST) {
612 vtop[-1].c.ui = vtop->c.ull;
613 vtop->c.ui = vtop->c.ull >> 32;
614 vtop->r = VT_CONST;
615 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
616 vtop->c.ui += 4;
617 vtop->r = vtop[-1].r;
618 } else if (v > VT_CONST) {
619 vtop--;
620 lexpand();
621 } else
622 vtop->r = vtop[-1].r2;
623 vtop[-1].r2 = VT_CONST;
624 vtop[-1].type.t = VT_INT | u;
626 #endif
628 /* build a long long from two ints */
629 void lbuild(int t)
631 gv2(RC_INT, RC_INT);
632 vtop[-1].r2 = vtop[0].r;
633 vtop[-1].type.t = t;
634 vpop();
637 /* rotate n first stack elements to the bottom
638 I1 ... In -> I2 ... In I1 [top is right]
640 void vrotb(int n)
642 int i;
643 SValue tmp;
645 tmp = vtop[-n + 1];
646 for(i=-n+1;i!=0;i++)
647 vtop[i] = vtop[i+1];
648 vtop[0] = tmp;
651 /* rotate n first stack elements to the top
652 I1 ... In -> In I1 ... I(n-1) [top is right]
654 void vrott(int n)
656 int i;
657 SValue tmp;
659 tmp = vtop[0];
660 for(i = 0;i < n - 1; i++)
661 vtop[-i] = vtop[-i - 1];
662 vtop[-n + 1] = tmp;
665 #ifdef TCC_TARGET_ARM
666 /* like vrott but in other direction
667 In ... I1 -> I(n-1) ... I1 In [top is right]
669 void vnrott(int n)
671 int i;
672 SValue tmp;
674 tmp = vtop[-n + 1];
675 for(i = n - 1; i > 0; i--)
676 vtop[-i] = vtop[-i + 1];
677 vtop[0] = tmp;
679 #endif
681 /* pop stack value */
682 void vpop(void)
684 int v;
685 v = vtop->r & VT_VALMASK;
686 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
687 /* for x86, we need to pop the FP stack */
688 if (v == TREG_ST0 && !nocode_wanted) {
689 o(0xd9dd); /* fstp %st(1) */
690 } else
691 #endif
692 if (v == VT_JMP || v == VT_JMPI) {
693 /* need to put correct jump if && or || without test */
694 gsym(vtop->c.ul);
696 vtop--;
699 /* convert stack entry to register and duplicate its value in another
700 register */
701 void gv_dup(void)
703 int rc, t, r, r1;
704 SValue sv;
706 t = vtop->type.t;
707 if ((t & VT_BTYPE) == VT_LLONG) {
708 lexpand();
709 gv_dup();
710 vswap();
711 vrotb(3);
712 gv_dup();
713 vrotb(4);
714 /* stack: H L L1 H1 */
715 lbuild(t);
716 vrotb(3);
717 vrotb(3);
718 vswap();
719 lbuild(t);
720 vswap();
721 } else {
722 /* duplicate value */
723 rc = RC_INT;
724 sv.type.t = VT_INT;
725 if (is_float(t)) {
726 rc = RC_FLOAT;
727 #ifdef TCC_TARGET_X86_64
728 if ((t & VT_BTYPE) == VT_LDOUBLE) {
729 rc = RC_ST0;
731 #endif
732 sv.type.t = t;
734 r = gv(rc);
735 r1 = get_reg(rc);
736 sv.r = r;
737 sv.c.ul = 0;
738 load(r1, &sv); /* move r to r1 */
739 vdup();
740 /* duplicates value */
741 vtop->r = r1;
745 #ifndef TCC_TARGET_X86_64
746 /* generate CPU independent (unsigned) long long operations */
747 void gen_opl(int op)
749 int t, a, b, op1, c, i;
750 int func;
751 unsigned short reg_iret = REG_IRET;
752 unsigned short reg_lret = REG_LRET;
753 SValue tmp;
755 switch(op) {
756 case '/':
757 case TOK_PDIV:
758 func = TOK___divdi3;
759 goto gen_func;
760 case TOK_UDIV:
761 func = TOK___udivdi3;
762 goto gen_func;
763 case '%':
764 func = TOK___moddi3;
765 goto gen_mod_func;
766 case TOK_UMOD:
767 func = TOK___umoddi3;
768 gen_mod_func:
769 #ifdef TCC_ARM_EABI
770 reg_iret = TREG_R2;
771 reg_lret = TREG_R3;
772 #endif
773 gen_func:
774 /* call generic long long function */
775 vpush_global_sym(&func_old_type, func);
776 vrott(3);
777 gfunc_call(2);
778 vpushi(0);
779 vtop->r = reg_iret;
780 vtop->r2 = reg_lret;
781 break;
782 case '^':
783 case '&':
784 case '|':
785 case '*':
786 case '+':
787 case '-':
788 t = vtop->type.t;
789 vswap();
790 lexpand();
791 vrotb(3);
792 lexpand();
793 /* stack: L1 H1 L2 H2 */
794 tmp = vtop[0];
795 vtop[0] = vtop[-3];
796 vtop[-3] = tmp;
797 tmp = vtop[-2];
798 vtop[-2] = vtop[-3];
799 vtop[-3] = tmp;
800 vswap();
801 /* stack: H1 H2 L1 L2 */
802 if (op == '*') {
803 vpushv(vtop - 1);
804 vpushv(vtop - 1);
805 gen_op(TOK_UMULL);
806 lexpand();
807 /* stack: H1 H2 L1 L2 ML MH */
808 for(i=0;i<4;i++)
809 vrotb(6);
810 /* stack: ML MH H1 H2 L1 L2 */
811 tmp = vtop[0];
812 vtop[0] = vtop[-2];
813 vtop[-2] = tmp;
814 /* stack: ML MH H1 L2 H2 L1 */
815 gen_op('*');
816 vrotb(3);
817 vrotb(3);
818 gen_op('*');
819 /* stack: ML MH M1 M2 */
820 gen_op('+');
821 gen_op('+');
822 } else if (op == '+' || op == '-') {
823 /* XXX: add non carry method too (for MIPS or alpha) */
824 if (op == '+')
825 op1 = TOK_ADDC1;
826 else
827 op1 = TOK_SUBC1;
828 gen_op(op1);
829 /* stack: H1 H2 (L1 op L2) */
830 vrotb(3);
831 vrotb(3);
832 gen_op(op1 + 1); /* TOK_xxxC2 */
833 } else {
834 gen_op(op);
835 /* stack: H1 H2 (L1 op L2) */
836 vrotb(3);
837 vrotb(3);
838 /* stack: (L1 op L2) H1 H2 */
839 gen_op(op);
840 /* stack: (L1 op L2) (H1 op H2) */
842 /* stack: L H */
843 lbuild(t);
844 break;
845 case TOK_SAR:
846 case TOK_SHR:
847 case TOK_SHL:
848 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
849 t = vtop[-1].type.t;
850 vswap();
851 lexpand();
852 vrotb(3);
853 /* stack: L H shift */
854 c = (int)vtop->c.i;
855 /* constant: simpler */
856 /* NOTE: all comments are for SHL. the other cases are
857 done by swaping words */
858 vpop();
859 if (op != TOK_SHL)
860 vswap();
861 if (c >= 32) {
862 /* stack: L H */
863 vpop();
864 if (c > 32) {
865 vpushi(c - 32);
866 gen_op(op);
868 if (op != TOK_SAR) {
869 vpushi(0);
870 } else {
871 gv_dup();
872 vpushi(31);
873 gen_op(TOK_SAR);
875 vswap();
876 } else {
877 vswap();
878 gv_dup();
879 /* stack: H L L */
880 vpushi(c);
881 gen_op(op);
882 vswap();
883 vpushi(32 - c);
884 if (op == TOK_SHL)
885 gen_op(TOK_SHR);
886 else
887 gen_op(TOK_SHL);
888 vrotb(3);
889 /* stack: L L H */
890 vpushi(c);
891 if (op == TOK_SHL)
892 gen_op(TOK_SHL);
893 else
894 gen_op(TOK_SHR);
895 gen_op('|');
897 if (op != TOK_SHL)
898 vswap();
899 lbuild(t);
900 } else {
901 /* XXX: should provide a faster fallback on x86 ? */
902 switch(op) {
903 case TOK_SAR:
904 func = TOK___ashrdi3;
905 goto gen_func;
906 case TOK_SHR:
907 func = TOK___lshrdi3;
908 goto gen_func;
909 case TOK_SHL:
910 func = TOK___ashldi3;
911 goto gen_func;
914 break;
915 default:
916 /* compare operations */
917 t = vtop->type.t;
918 vswap();
919 lexpand();
920 vrotb(3);
921 lexpand();
922 /* stack: L1 H1 L2 H2 */
923 tmp = vtop[-1];
924 vtop[-1] = vtop[-2];
925 vtop[-2] = tmp;
926 /* stack: L1 L2 H1 H2 */
927 /* compare high */
928 op1 = op;
929 /* when values are equal, we need to compare low words. since
930 the jump is inverted, we invert the test too. */
931 if (op1 == TOK_LT)
932 op1 = TOK_LE;
933 else if (op1 == TOK_GT)
934 op1 = TOK_GE;
935 else if (op1 == TOK_ULT)
936 op1 = TOK_ULE;
937 else if (op1 == TOK_UGT)
938 op1 = TOK_UGE;
939 a = 0;
940 b = 0;
941 gen_op(op1);
942 if (op1 != TOK_NE) {
943 a = gtst(1, 0);
945 if (op != TOK_EQ) {
946 /* generate non equal test */
947 /* XXX: NOT PORTABLE yet */
948 if (a == 0) {
949 b = gtst(0, 0);
950 } else {
951 #if defined(TCC_TARGET_I386)
952 b = psym(0x850f, 0);
953 #elif defined(TCC_TARGET_ARM)
954 b = ind;
955 o(0x1A000000 | encbranch(ind, 0, 1));
956 #elif defined(TCC_TARGET_C67)
957 error("not implemented");
958 #else
959 #error not supported
960 #endif
963 /* compare low. Always unsigned */
964 op1 = op;
965 if (op1 == TOK_LT)
966 op1 = TOK_ULT;
967 else if (op1 == TOK_LE)
968 op1 = TOK_ULE;
969 else if (op1 == TOK_GT)
970 op1 = TOK_UGT;
971 else if (op1 == TOK_GE)
972 op1 = TOK_UGE;
973 gen_op(op1);
974 a = gtst(1, a);
975 gsym(b);
976 vseti(VT_JMPI, a);
977 break;
980 #endif
982 /* handle integer constant optimizations and various machine
983 independent opt */
984 void gen_opic(int op)
986 int c1, c2, t1, t2, n;
987 SValue *v1, *v2;
988 long long l1, l2;
989 typedef unsigned long long U;
991 v1 = vtop - 1;
992 v2 = vtop;
993 t1 = v1->type.t & VT_BTYPE;
994 t2 = v2->type.t & VT_BTYPE;
996 if (t1 == VT_LLONG)
997 l1 = v1->c.ll;
998 else if (v1->type.t & VT_UNSIGNED)
999 l1 = v1->c.ui;
1000 else
1001 l1 = v1->c.i;
1003 if (t2 == VT_LLONG)
1004 l2 = v2->c.ll;
1005 else if (v2->type.t & VT_UNSIGNED)
1006 l2 = v2->c.ui;
1007 else
1008 l2 = v2->c.i;
1010 /* currently, we cannot do computations with forward symbols */
1011 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1012 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1013 if (c1 && c2) {
1014 switch(op) {
1015 case '+': l1 += l2; break;
1016 case '-': l1 -= l2; break;
1017 case '&': l1 &= l2; break;
1018 case '^': l1 ^= l2; break;
1019 case '|': l1 |= l2; break;
1020 case '*': l1 *= l2; break;
1022 case TOK_PDIV:
1023 case '/':
1024 case '%':
1025 case TOK_UDIV:
1026 case TOK_UMOD:
1027 /* if division by zero, generate explicit division */
1028 if (l2 == 0) {
1029 if (const_wanted)
1030 error("division by zero in constant");
1031 goto general_case;
1033 switch(op) {
1034 default: l1 /= l2; break;
1035 case '%': l1 %= l2; break;
1036 case TOK_UDIV: l1 = (U)l1 / l2; break;
1037 case TOK_UMOD: l1 = (U)l1 % l2; break;
1039 break;
1040 case TOK_SHL: l1 <<= l2; break;
1041 case TOK_SHR: l1 = (U)l1 >> l2; break;
1042 case TOK_SAR: l1 >>= l2; break;
1043 /* tests */
1044 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1045 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1046 case TOK_EQ: l1 = l1 == l2; break;
1047 case TOK_NE: l1 = l1 != l2; break;
1048 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1049 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1050 case TOK_LT: l1 = l1 < l2; break;
1051 case TOK_GE: l1 = l1 >= l2; break;
1052 case TOK_LE: l1 = l1 <= l2; break;
1053 case TOK_GT: l1 = l1 > l2; break;
1054 /* logical */
1055 case TOK_LAND: l1 = l1 && l2; break;
1056 case TOK_LOR: l1 = l1 || l2; break;
1057 default:
1058 goto general_case;
1060 v1->c.ll = l1;
1061 vtop--;
1062 } else {
1063 /* if commutative ops, put c2 as constant */
1064 if (c1 && (op == '+' || op == '&' || op == '^' ||
1065 op == '|' || op == '*')) {
1066 vswap();
1067 c2 = c1; //c = c1, c1 = c2, c2 = c;
1068 l2 = l1; //l = l1, l1 = l2, l2 = l;
1070 /* Filter out NOP operations like x*1, x-0, x&-1... */
1071 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1072 op == TOK_PDIV) &&
1073 l2 == 1) ||
1074 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1075 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1076 l2 == 0) ||
1077 (op == '&' &&
1078 l2 == -1))) {
1079 /* nothing to do */
1080 vtop--;
1081 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1082 /* try to use shifts instead of muls or divs */
1083 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1084 n = -1;
1085 while (l2) {
1086 l2 >>= 1;
1087 n++;
1089 vtop->c.ll = n;
1090 if (op == '*')
1091 op = TOK_SHL;
1092 else if (op == TOK_PDIV)
1093 op = TOK_SAR;
1094 else
1095 op = TOK_SHR;
1097 goto general_case;
1098 } else if (c2 && (op == '+' || op == '-') &&
1099 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
1100 (VT_CONST | VT_SYM) ||
1101 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1102 /* symbol + constant case */
1103 if (op == '-')
1104 l2 = -l2;
1105 vtop--;
1106 vtop->c.ll += l2;
1107 } else {
1108 general_case:
1109 if (!nocode_wanted) {
1110 /* call low level op generator */
1111 if (t1 == VT_LLONG || t2 == VT_LLONG)
1112 gen_opl(op);
1113 else
1114 gen_opi(op);
1115 } else {
1116 vtop--;
1122 /* generate a floating point operation with constant propagation */
1123 void gen_opif(int op)
1125 int c1, c2;
1126 SValue *v1, *v2;
1127 long double f1, f2;
1129 v1 = vtop - 1;
1130 v2 = vtop;
1131 /* currently, we cannot do computations with forward symbols */
1132 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1133 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1134 if (c1 && c2) {
1135 if (v1->type.t == VT_FLOAT) {
1136 f1 = v1->c.f;
1137 f2 = v2->c.f;
1138 } else if (v1->type.t == VT_DOUBLE) {
1139 f1 = v1->c.d;
1140 f2 = v2->c.d;
1141 } else {
1142 f1 = v1->c.ld;
1143 f2 = v2->c.ld;
1146 /* NOTE: we only do constant propagation if finite number (not
1147 NaN or infinity) (ANSI spec) */
1148 if (!ieee_finite(f1) || !ieee_finite(f2))
1149 goto general_case;
1151 switch(op) {
1152 case '+': f1 += f2; break;
1153 case '-': f1 -= f2; break;
1154 case '*': f1 *= f2; break;
1155 case '/':
1156 if (f2 == 0.0) {
1157 if (const_wanted)
1158 error("division by zero in constant");
1159 goto general_case;
1161 f1 /= f2;
1162 break;
1163 /* XXX: also handles tests ? */
1164 default:
1165 goto general_case;
1167 /* XXX: overflow test ? */
1168 if (v1->type.t == VT_FLOAT) {
1169 v1->c.f = f1;
1170 } else if (v1->type.t == VT_DOUBLE) {
1171 v1->c.d = f1;
1172 } else {
1173 v1->c.ld = f1;
1175 vtop--;
1176 } else {
1177 general_case:
1178 if (!nocode_wanted) {
1179 gen_opf(op);
1180 } else {
1181 vtop--;
1186 static int pointed_size(CType *type)
1188 int align;
1189 return type_size(pointed_type(type), &align);
1192 static inline int is_null_pointer(SValue *p)
1194 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1195 return 0;
1196 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1197 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1200 static inline int is_integer_btype(int bt)
1202 return (bt == VT_BYTE || bt == VT_SHORT ||
1203 bt == VT_INT || bt == VT_LLONG);
1206 /* check types for comparison or substraction of pointers */
1207 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1209 CType *type1, *type2, tmp_type1, tmp_type2;
1210 int bt1, bt2;
1212 /* null pointers are accepted for all comparisons as gcc */
1213 if (is_null_pointer(p1) || is_null_pointer(p2))
1214 return;
1215 type1 = &p1->type;
1216 type2 = &p2->type;
1217 bt1 = type1->t & VT_BTYPE;
1218 bt2 = type2->t & VT_BTYPE;
1219 /* accept comparison between pointer and integer with a warning */
1220 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1221 if (op != TOK_LOR && op != TOK_LAND )
1222 warning("comparison between pointer and integer");
1223 return;
1226 /* both must be pointers or implicit function pointers */
1227 if (bt1 == VT_PTR) {
1228 type1 = pointed_type(type1);
1229 } else if (bt1 != VT_FUNC)
1230 goto invalid_operands;
1232 if (bt2 == VT_PTR) {
1233 type2 = pointed_type(type2);
1234 } else if (bt2 != VT_FUNC) {
1235 invalid_operands:
1236 error("invalid operands to binary %s", get_tok_str(op, NULL));
1238 if ((type1->t & VT_BTYPE) == VT_VOID ||
1239 (type2->t & VT_BTYPE) == VT_VOID)
1240 return;
1241 tmp_type1 = *type1;
1242 tmp_type2 = *type2;
1243 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1244 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1245 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1246 /* gcc-like error if '-' is used */
1247 if (op == '-')
1248 goto invalid_operands;
1249 else
1250 warning("comparison of distinct pointer types lacks a cast");
1254 /* generic gen_op: handles types problems */
1255 void gen_op(int op)
1257 int u, t1, t2, bt1, bt2, t;
1258 CType type1;
1260 t1 = vtop[-1].type.t;
1261 t2 = vtop[0].type.t;
1262 bt1 = t1 & VT_BTYPE;
1263 bt2 = t2 & VT_BTYPE;
1265 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1266 /* at least one operand is a pointer */
1267 /* relationnal op: must be both pointers */
1268 if (op >= TOK_ULT && op <= TOK_LOR) {
1269 check_comparison_pointer_types(vtop - 1, vtop, op);
1270 /* pointers are handled are unsigned */
1271 #ifdef TCC_TARGET_X86_64
1272 t = VT_LLONG | VT_UNSIGNED;
1273 #else
1274 t = VT_INT | VT_UNSIGNED;
1275 #endif
1276 goto std_op;
1278 /* if both pointers, then it must be the '-' op */
1279 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1280 if (op != '-')
1281 error("cannot use pointers here");
1282 check_comparison_pointer_types(vtop - 1, vtop, op);
1283 /* XXX: check that types are compatible */
1284 u = pointed_size(&vtop[-1].type);
1285 gen_opic(op);
1286 /* set to integer type */
1287 #ifdef TCC_TARGET_X86_64
1288 vtop->type.t = VT_LLONG;
1289 #else
1290 vtop->type.t = VT_INT;
1291 #endif
1292 vpushi(u);
1293 gen_op(TOK_PDIV);
1294 } else {
1295 /* exactly one pointer : must be '+' or '-'. */
1296 if (op != '-' && op != '+')
1297 error("cannot use pointers here");
1298 /* Put pointer as first operand */
1299 if (bt2 == VT_PTR) {
1300 vswap();
1301 swap(&t1, &t2);
1303 type1 = vtop[-1].type;
1304 #ifdef TCC_TARGET_X86_64
1305 vpushll(pointed_size(&vtop[-1].type));
1306 #else
1307 /* XXX: cast to int ? (long long case) */
1308 vpushi(pointed_size(&vtop[-1].type));
1309 #endif
1310 gen_op('*');
1311 #ifdef CONFIG_TCC_BCHECK
1312 /* if evaluating constant expression, no code should be
1313 generated, so no bound check */
1314 if (tcc_state->do_bounds_check && !const_wanted) {
1315 /* if bounded pointers, we generate a special code to
1316 test bounds */
1317 if (op == '-') {
1318 vpushi(0);
1319 vswap();
1320 gen_op('-');
1322 gen_bounded_ptr_add();
1323 } else
1324 #endif
1326 gen_opic(op);
1328 /* put again type if gen_opic() swaped operands */
1329 vtop->type = type1;
1331 } else if (is_float(bt1) || is_float(bt2)) {
1332 /* compute bigger type and do implicit casts */
1333 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1334 t = VT_LDOUBLE;
1335 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1336 t = VT_DOUBLE;
1337 } else {
1338 t = VT_FLOAT;
1340 /* floats can only be used for a few operations */
1341 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1342 (op < TOK_ULT || op > TOK_GT))
1343 error("invalid operands for binary operation");
1344 goto std_op;
1345 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1346 /* cast to biggest op */
1347 t = VT_LLONG;
1348 /* convert to unsigned if it does not fit in a long long */
1349 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1350 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1351 t |= VT_UNSIGNED;
1352 goto std_op;
1353 } else {
1354 /* integer operations */
1355 t = VT_INT;
1356 /* convert to unsigned if it does not fit in an integer */
1357 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1358 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1359 t |= VT_UNSIGNED;
1360 std_op:
1361 /* XXX: currently, some unsigned operations are explicit, so
1362 we modify them here */
1363 if (t & VT_UNSIGNED) {
1364 if (op == TOK_SAR)
1365 op = TOK_SHR;
1366 else if (op == '/')
1367 op = TOK_UDIV;
1368 else if (op == '%')
1369 op = TOK_UMOD;
1370 else if (op == TOK_LT)
1371 op = TOK_ULT;
1372 else if (op == TOK_GT)
1373 op = TOK_UGT;
1374 else if (op == TOK_LE)
1375 op = TOK_ULE;
1376 else if (op == TOK_GE)
1377 op = TOK_UGE;
1379 vswap();
1380 type1.t = t;
1381 gen_cast(&type1);
1382 vswap();
1383 /* special case for shifts and long long: we keep the shift as
1384 an integer */
1385 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1386 type1.t = VT_INT;
1387 gen_cast(&type1);
1388 if (is_float(t))
1389 gen_opif(op);
1390 else
1391 gen_opic(op);
1392 if (op >= TOK_ULT && op <= TOK_GT) {
1393 /* relationnal op: the result is an int */
1394 vtop->type.t = VT_INT;
1395 } else {
1396 vtop->type.t = t;
1401 #ifndef TCC_TARGET_ARM
1402 /* generic itof for unsigned long long case */
1403 void gen_cvt_itof1(int t)
1405 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1406 (VT_LLONG | VT_UNSIGNED)) {
1408 if (t == VT_FLOAT)
1409 vpush_global_sym(&func_old_type, TOK___floatundisf);
1410 #if LDOUBLE_SIZE != 8
1411 else if (t == VT_LDOUBLE)
1412 vpush_global_sym(&func_old_type, TOK___floatundixf);
1413 #endif
1414 else
1415 vpush_global_sym(&func_old_type, TOK___floatundidf);
1416 vrott(2);
1417 gfunc_call(1);
1418 vpushi(0);
1419 vtop->r = reg_fret(t);
1420 } else {
1421 gen_cvt_itof(t);
1424 #endif
1426 /* generic ftoi for unsigned long long case */
1427 void gen_cvt_ftoi1(int t)
1429 int st;
1431 if (t == (VT_LLONG | VT_UNSIGNED)) {
1432 /* not handled natively */
1433 st = vtop->type.t & VT_BTYPE;
1434 if (st == VT_FLOAT)
1435 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1436 #if LDOUBLE_SIZE != 8
1437 else if (st == VT_LDOUBLE)
1438 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1439 #endif
1440 else
1441 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1442 vrott(2);
1443 gfunc_call(1);
1444 vpushi(0);
1445 vtop->r = REG_IRET;
1446 vtop->r2 = REG_LRET;
1447 } else {
1448 gen_cvt_ftoi(t);
1452 /* force char or short cast */
1453 void force_charshort_cast(int t)
1455 int bits, dbt;
1456 dbt = t & VT_BTYPE;
1457 /* XXX: add optimization if lvalue : just change type and offset */
1458 if (dbt == VT_BYTE)
1459 bits = 8;
1460 else
1461 bits = 16;
1462 if (t & VT_UNSIGNED) {
1463 vpushi((1 << bits) - 1);
1464 gen_op('&');
1465 } else {
1466 bits = 32 - bits;
1467 vpushi(bits);
1468 gen_op(TOK_SHL);
1469 /* result must be signed or the SAR is converted to an SHL
1470 This was not the case when "t" was a signed short
1471 and the last value on the stack was an unsigned int */
1472 vtop->type.t &= ~VT_UNSIGNED;
1473 vpushi(bits);
1474 gen_op(TOK_SAR);
1478 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1479 static void gen_cast(CType *type)
1481 int sbt, dbt, sf, df, c, p;
1483 /* special delayed cast for char/short */
1484 /* XXX: in some cases (multiple cascaded casts), it may still
1485 be incorrect */
1486 if (vtop->r & VT_MUSTCAST) {
1487 vtop->r &= ~VT_MUSTCAST;
1488 force_charshort_cast(vtop->type.t);
1491 /* bitfields first get cast to ints */
1492 if (vtop->type.t & VT_BITFIELD) {
1493 gv(RC_INT);
1496 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1497 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1499 if (sbt != dbt) {
1500 sf = is_float(sbt);
1501 df = is_float(dbt);
1502 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1503 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1504 if (c) {
1505 /* constant case: we can do it now */
1506 /* XXX: in ISOC, cannot do it if error in convert */
1507 if (sbt == VT_FLOAT)
1508 vtop->c.ld = vtop->c.f;
1509 else if (sbt == VT_DOUBLE)
1510 vtop->c.ld = vtop->c.d;
1512 if (df) {
1513 if ((sbt & VT_BTYPE) == VT_LLONG) {
1514 if (sbt & VT_UNSIGNED)
1515 vtop->c.ld = vtop->c.ull;
1516 else
1517 vtop->c.ld = vtop->c.ll;
1518 } else if(!sf) {
1519 if (sbt & VT_UNSIGNED)
1520 vtop->c.ld = vtop->c.ui;
1521 else
1522 vtop->c.ld = vtop->c.i;
1525 if (dbt == VT_FLOAT)
1526 vtop->c.f = (float)vtop->c.ld;
1527 else if (dbt == VT_DOUBLE)
1528 vtop->c.d = (double)vtop->c.ld;
1529 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1530 vtop->c.ull = (unsigned long long)vtop->c.ld;
1531 } else if (sf && dbt == VT_BOOL) {
1532 vtop->c.i = (vtop->c.ld != 0);
1533 } else {
1534 if(sf)
1535 vtop->c.ll = (long long)vtop->c.ld;
1536 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1537 vtop->c.ll = vtop->c.ull;
1538 else if (sbt & VT_UNSIGNED)
1539 vtop->c.ll = vtop->c.ui;
1540 else if (sbt != VT_LLONG)
1541 vtop->c.ll = vtop->c.i;
1543 if (dbt == (VT_LLONG|VT_UNSIGNED))
1544 vtop->c.ull = vtop->c.ll;
1545 else if (dbt == VT_BOOL)
1546 vtop->c.i = (vtop->c.ll != 0);
1547 else if (dbt != VT_LLONG) {
1548 int s = 0;
1549 if ((dbt & VT_BTYPE) == VT_BYTE)
1550 s = 24;
1551 else if ((dbt & VT_BTYPE) == VT_SHORT)
1552 s = 16;
1554 if(dbt & VT_UNSIGNED)
1555 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1556 else
1557 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1560 } else if (p && dbt == VT_BOOL) {
1561 vtop->r = VT_CONST;
1562 vtop->c.i = 1;
1563 } else if (!nocode_wanted) {
1564 /* non constant case: generate code */
1565 if (sf && df) {
1566 /* convert from fp to fp */
1567 gen_cvt_ftof(dbt);
1568 } else if (df) {
1569 /* convert int to fp */
1570 gen_cvt_itof1(dbt);
1571 } else if (sf) {
1572 /* convert fp to int */
1573 if (dbt == VT_BOOL) {
1574 vpushi(0);
1575 gen_op(TOK_NE);
1576 } else {
1577 /* we handle char/short/etc... with generic code */
1578 if (dbt != (VT_INT | VT_UNSIGNED) &&
1579 dbt != (VT_LLONG | VT_UNSIGNED) &&
1580 dbt != VT_LLONG)
1581 dbt = VT_INT;
1582 gen_cvt_ftoi1(dbt);
1583 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1584 /* additional cast for char/short... */
1585 vtop->type.t = dbt;
1586 gen_cast(type);
1589 #ifndef TCC_TARGET_X86_64
1590 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1591 if ((sbt & VT_BTYPE) != VT_LLONG) {
1592 /* scalar to long long */
1593 /* machine independent conversion */
1594 gv(RC_INT);
1595 /* generate high word */
1596 if (sbt == (VT_INT | VT_UNSIGNED)) {
1597 vpushi(0);
1598 gv(RC_INT);
1599 } else {
1600 if (sbt == VT_PTR) {
1601 /* cast from pointer to int before we apply
1602 shift operation, which pointers don't support*/
1603 gen_cast(&int_type);
1605 gv_dup();
1606 vpushi(31);
1607 gen_op(TOK_SAR);
1609 /* patch second register */
1610 vtop[-1].r2 = vtop->r;
1611 vpop();
1613 #else
1614 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1615 (dbt & VT_BTYPE) == VT_PTR) {
1616 /* XXX: not sure if this is perfect... need more tests */
1617 if ((sbt & VT_BTYPE) != VT_LLONG) {
1618 int r = gv(RC_INT);
1619 if (sbt != (VT_INT | VT_UNSIGNED) &&
1620 sbt != VT_PTR && sbt != VT_FUNC) {
1621 /* x86_64 specific: movslq */
1622 o(0x6348);
1623 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1626 #endif
1627 } else if (dbt == VT_BOOL) {
1628 /* scalar to bool */
1629 vpushi(0);
1630 gen_op(TOK_NE);
1631 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1632 (dbt & VT_BTYPE) == VT_SHORT) {
1633 if (sbt == VT_PTR) {
1634 vtop->type.t = VT_INT;
1635 warning("nonportable conversion from pointer to char/short");
1637 force_charshort_cast(dbt);
1638 } else if ((dbt & VT_BTYPE) == VT_INT) {
1639 /* scalar to int */
1640 if (sbt == VT_LLONG) {
1641 /* from long long: just take low order word */
1642 lexpand();
1643 vpop();
1645 /* if lvalue and single word type, nothing to do because
1646 the lvalue already contains the real type size (see
1647 VT_LVAL_xxx constants) */
1650 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1651 /* if we are casting between pointer types,
1652 we must update the VT_LVAL_xxx size */
1653 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1654 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1656 vtop->type = *type;
1659 /* return type size. Put alignment at 'a' */
1660 static int type_size(CType *type, int *a)
1662 Sym *s;
1663 int bt;
1665 bt = type->t & VT_BTYPE;
1666 if (bt == VT_STRUCT) {
1667 /* struct/union */
1668 s = type->ref;
1669 *a = s->r;
1670 return s->c;
1671 } else if (bt == VT_PTR) {
1672 if (type->t & VT_ARRAY) {
1673 int ts;
1675 s = type->ref;
1676 ts = type_size(&s->type, a);
1678 if (ts < 0 && s->c < 0)
1679 ts = -ts;
1681 return ts * s->c;
1682 } else {
1683 *a = PTR_SIZE;
1684 return PTR_SIZE;
1686 } else if (bt == VT_LDOUBLE) {
1687 *a = LDOUBLE_ALIGN;
1688 return LDOUBLE_SIZE;
1689 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1690 #ifdef TCC_TARGET_I386
1691 #ifdef TCC_TARGET_PE
1692 *a = 8;
1693 #else
1694 *a = 4;
1695 #endif
1696 #elif defined(TCC_TARGET_ARM)
1697 #ifdef TCC_ARM_EABI
1698 *a = 8;
1699 #else
1700 *a = 4;
1701 #endif
1702 #else
1703 *a = 8;
1704 #endif
1705 return 8;
1706 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1707 *a = 4;
1708 return 4;
1709 } else if (bt == VT_SHORT) {
1710 *a = 2;
1711 return 2;
1712 } else {
1713 /* char, void, function, _Bool */
1714 *a = 1;
1715 return 1;
1719 /* return the pointed type of t */
1720 static inline CType *pointed_type(CType *type)
1722 return &type->ref->type;
1725 /* modify type so that its it is a pointer to type. */
1726 static void mk_pointer(CType *type)
1728 Sym *s;
1729 s = sym_push(SYM_FIELD, type, 0, -1);
1730 type->t = VT_PTR | (type->t & ~VT_TYPE);
1731 type->ref = s;
1734 /* compare function types. OLD functions match any new functions */
1735 static int is_compatible_func(CType *type1, CType *type2)
1737 Sym *s1, *s2;
1739 s1 = type1->ref;
1740 s2 = type2->ref;
1741 if (!is_compatible_types(&s1->type, &s2->type))
1742 return 0;
1743 /* check func_call */
1744 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
1745 return 0;
1746 /* XXX: not complete */
1747 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
1748 return 1;
1749 if (s1->c != s2->c)
1750 return 0;
1751 while (s1 != NULL) {
1752 if (s2 == NULL)
1753 return 0;
1754 if (!is_compatible_parameter_types(&s1->type, &s2->type))
1755 return 0;
1756 s1 = s1->next;
1757 s2 = s2->next;
1759 if (s2)
1760 return 0;
1761 return 1;
1764 /* return true if type1 and type2 are the same. If unqualified is
1765 true, qualifiers on the types are ignored.
1767 - enums are not checked as gcc __builtin_types_compatible_p ()
1769 static int compare_types(CType *type1, CType *type2, int unqualified)
1771 int bt1, t1, t2;
1773 t1 = type1->t & VT_TYPE;
1774 t2 = type2->t & VT_TYPE;
1775 if (unqualified) {
1776 /* strip qualifiers before comparing */
1777 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
1778 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
1780 /* XXX: bitfields ? */
1781 if (t1 != t2)
1782 return 0;
1783 /* test more complicated cases */
1784 bt1 = t1 & VT_BTYPE;
1785 if (bt1 == VT_PTR) {
1786 type1 = pointed_type(type1);
1787 type2 = pointed_type(type2);
1788 return is_compatible_types(type1, type2);
1789 } else if (bt1 == VT_STRUCT) {
1790 return (type1->ref == type2->ref);
1791 } else if (bt1 == VT_FUNC) {
1792 return is_compatible_func(type1, type2);
1793 } else {
1794 return 1;
1798 /* return true if type1 and type2 are exactly the same (including
1799 qualifiers).
1801 static int is_compatible_types(CType *type1, CType *type2)
1803 return compare_types(type1,type2,0);
1806 /* return true if type1 and type2 are the same (ignoring qualifiers).
1808 static int is_compatible_parameter_types(CType *type1, CType *type2)
1810 return compare_types(type1,type2,1);
1813 /* print a type. If 'varstr' is not NULL, then the variable is also
1814 printed in the type */
1815 /* XXX: union */
1816 /* XXX: add array and function pointers */
1817 void type_to_str(char *buf, int buf_size,
1818 CType *type, const char *varstr)
1820 int bt, v, t;
1821 Sym *s, *sa;
1822 char buf1[256];
1823 const char *tstr;
1825 t = type->t & VT_TYPE;
1826 bt = t & VT_BTYPE;
1827 buf[0] = '\0';
1828 if (t & VT_CONSTANT)
1829 pstrcat(buf, buf_size, "const ");
1830 if (t & VT_VOLATILE)
1831 pstrcat(buf, buf_size, "volatile ");
1832 if (t & VT_UNSIGNED)
1833 pstrcat(buf, buf_size, "unsigned ");
1834 switch(bt) {
1835 case VT_VOID:
1836 tstr = "void";
1837 goto add_tstr;
1838 case VT_BOOL:
1839 tstr = "_Bool";
1840 goto add_tstr;
1841 case VT_BYTE:
1842 tstr = "char";
1843 goto add_tstr;
1844 case VT_SHORT:
1845 tstr = "short";
1846 goto add_tstr;
1847 case VT_INT:
1848 tstr = "int";
1849 goto add_tstr;
1850 case VT_LONG:
1851 tstr = "long";
1852 goto add_tstr;
1853 case VT_LLONG:
1854 tstr = "long long";
1855 goto add_tstr;
1856 case VT_FLOAT:
1857 tstr = "float";
1858 goto add_tstr;
1859 case VT_DOUBLE:
1860 tstr = "double";
1861 goto add_tstr;
1862 case VT_LDOUBLE:
1863 tstr = "long double";
1864 add_tstr:
1865 pstrcat(buf, buf_size, tstr);
1866 break;
1867 case VT_ENUM:
1868 case VT_STRUCT:
1869 if (bt == VT_STRUCT)
1870 tstr = "struct ";
1871 else
1872 tstr = "enum ";
1873 pstrcat(buf, buf_size, tstr);
1874 v = type->ref->v & ~SYM_STRUCT;
1875 if (v >= SYM_FIRST_ANOM)
1876 pstrcat(buf, buf_size, "<anonymous>");
1877 else
1878 pstrcat(buf, buf_size, get_tok_str(v, NULL));
1879 break;
1880 case VT_FUNC:
1881 s = type->ref;
1882 type_to_str(buf, buf_size, &s->type, varstr);
1883 pstrcat(buf, buf_size, "(");
1884 sa = s->next;
1885 while (sa != NULL) {
1886 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
1887 pstrcat(buf, buf_size, buf1);
1888 sa = sa->next;
1889 if (sa)
1890 pstrcat(buf, buf_size, ", ");
1892 pstrcat(buf, buf_size, ")");
1893 goto no_var;
1894 case VT_PTR:
1895 s = type->ref;
1896 pstrcpy(buf1, sizeof(buf1), "*");
1897 if (varstr)
1898 pstrcat(buf1, sizeof(buf1), varstr);
1899 type_to_str(buf, buf_size, &s->type, buf1);
1900 goto no_var;
1902 if (varstr) {
1903 pstrcat(buf, buf_size, " ");
1904 pstrcat(buf, buf_size, varstr);
1906 no_var: ;
1909 /* verify type compatibility to store vtop in 'dt' type, and generate
1910 casts if needed. */
1911 static void gen_assign_cast(CType *dt)
1913 CType *st, *type1, *type2, tmp_type1, tmp_type2;
1914 char buf1[256], buf2[256];
1915 int dbt, sbt;
1917 st = &vtop->type; /* source type */
1918 dbt = dt->t & VT_BTYPE;
1919 sbt = st->t & VT_BTYPE;
1920 if (dt->t & VT_CONSTANT)
1921 warning("assignment of read-only location");
1922 switch(dbt) {
1923 case VT_PTR:
1924 /* special cases for pointers */
1925 /* '0' can also be a pointer */
1926 if (is_null_pointer(vtop))
1927 goto type_ok;
1928 /* accept implicit pointer to integer cast with warning */
1929 if (is_integer_btype(sbt)) {
1930 warning("assignment makes pointer from integer without a cast");
1931 goto type_ok;
1933 type1 = pointed_type(dt);
1934 /* a function is implicitely a function pointer */
1935 if (sbt == VT_FUNC) {
1936 if ((type1->t & VT_BTYPE) != VT_VOID &&
1937 !is_compatible_types(pointed_type(dt), st))
1938 goto error;
1939 else
1940 goto type_ok;
1942 if (sbt != VT_PTR)
1943 goto error;
1944 type2 = pointed_type(st);
1945 if ((type1->t & VT_BTYPE) == VT_VOID ||
1946 (type2->t & VT_BTYPE) == VT_VOID) {
1947 /* void * can match anything */
1948 } else {
1949 /* exact type match, except for unsigned */
1950 tmp_type1 = *type1;
1951 tmp_type2 = *type2;
1952 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1953 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1954 if (!is_compatible_types(&tmp_type1, &tmp_type2))
1955 warning("assignment from incompatible pointer type");
1957 /* check const and volatile */
1958 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
1959 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
1960 warning("assignment discards qualifiers from pointer target type");
1961 break;
1962 case VT_BYTE:
1963 case VT_SHORT:
1964 case VT_INT:
1965 case VT_LLONG:
1966 if (sbt == VT_PTR || sbt == VT_FUNC) {
1967 warning("assignment makes integer from pointer without a cast");
1969 /* XXX: more tests */
1970 break;
1971 case VT_STRUCT:
1972 tmp_type1 = *dt;
1973 tmp_type2 = *st;
1974 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
1975 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
1976 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1977 error:
1978 type_to_str(buf1, sizeof(buf1), st, NULL);
1979 type_to_str(buf2, sizeof(buf2), dt, NULL);
1980 error("cannot cast '%s' to '%s'", buf1, buf2);
1982 break;
1984 type_ok:
1985 gen_cast(dt);
1988 /* store vtop in lvalue pushed on stack */
1989 void vstore(void)
1991 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
1993 ft = vtop[-1].type.t;
1994 sbt = vtop->type.t & VT_BTYPE;
1995 dbt = ft & VT_BTYPE;
1996 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
1997 (sbt == VT_INT && dbt == VT_SHORT)) {
1998 /* optimize char/short casts */
1999 delayed_cast = VT_MUSTCAST;
2000 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2001 /* XXX: factorize */
2002 if (ft & VT_CONSTANT)
2003 warning("assignment of read-only location");
2004 } else {
2005 delayed_cast = 0;
2006 if (!(ft & VT_BITFIELD))
2007 gen_assign_cast(&vtop[-1].type);
2010 if (sbt == VT_STRUCT) {
2011 /* if structure, only generate pointer */
2012 /* structure assignment : generate memcpy */
2013 /* XXX: optimize if small size */
2014 if (!nocode_wanted) {
2015 size = type_size(&vtop->type, &align);
2017 #ifdef TCC_ARM_EABI
2018 if(!(align & 7))
2019 vpush_global_sym(&func_old_type, TOK_memcpy8);
2020 else if(!(align & 3))
2021 vpush_global_sym(&func_old_type, TOK_memcpy4);
2022 else
2023 #endif
2024 vpush_global_sym(&func_old_type, TOK_memcpy);
2026 /* destination */
2027 vpushv(vtop - 2);
2028 vtop->type.t = VT_PTR;
2029 gaddrof();
2030 /* source */
2031 vpushv(vtop - 2);
2032 vtop->type.t = VT_PTR;
2033 gaddrof();
2034 /* type size */
2035 vpushi(size);
2036 gfunc_call(3);
2038 vswap();
2039 vpop();
2040 } else {
2041 vswap();
2042 vpop();
2044 /* leave source on stack */
2045 } else if (ft & VT_BITFIELD) {
2046 /* bitfield store handling */
2047 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2048 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2049 /* remove bit field info to avoid loops */
2050 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2052 /* duplicate source into other register */
2053 gv_dup();
2054 vswap();
2055 vrott(3);
2057 if((ft & VT_BTYPE) == VT_BOOL) {
2058 gen_cast(&vtop[-1].type);
2059 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2062 /* duplicate destination */
2063 vdup();
2064 vtop[-1] = vtop[-2];
2066 /* mask and shift source */
2067 if((ft & VT_BTYPE) != VT_BOOL) {
2068 if((ft & VT_BTYPE) == VT_LLONG) {
2069 vpushll((1ULL << bit_size) - 1ULL);
2070 } else {
2071 vpushi((1 << bit_size) - 1);
2073 gen_op('&');
2075 vpushi(bit_pos);
2076 gen_op(TOK_SHL);
2077 /* load destination, mask and or with source */
2078 vswap();
2079 if((ft & VT_BTYPE) == VT_LLONG) {
2080 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2081 } else {
2082 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2084 gen_op('&');
2085 gen_op('|');
2086 /* store result */
2087 vstore();
2089 /* pop off shifted source from "duplicate source..." above */
2090 vpop();
2092 } else {
2093 #ifdef CONFIG_TCC_BCHECK
2094 /* bound check case */
2095 if (vtop[-1].r & VT_MUSTBOUND) {
2096 vswap();
2097 gbound();
2098 vswap();
2100 #endif
2101 if (!nocode_wanted) {
2102 rc = RC_INT;
2103 if (is_float(ft)) {
2104 rc = RC_FLOAT;
2105 #ifdef TCC_TARGET_X86_64
2106 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2107 rc = RC_ST0;
2109 #endif
2111 r = gv(rc); /* generate value */
2112 /* if lvalue was saved on stack, must read it */
2113 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2114 SValue sv;
2115 t = get_reg(RC_INT);
2116 #ifdef TCC_TARGET_X86_64
2117 sv.type.t = VT_PTR;
2118 #else
2119 sv.type.t = VT_INT;
2120 #endif
2121 sv.r = VT_LOCAL | VT_LVAL;
2122 sv.c.ul = vtop[-1].c.ul;
2123 load(t, &sv);
2124 vtop[-1].r = t | VT_LVAL;
2126 store(r, vtop - 1);
2127 #ifndef TCC_TARGET_X86_64
2128 /* two word case handling : store second register at word + 4 */
2129 if ((ft & VT_BTYPE) == VT_LLONG) {
2130 vswap();
2131 /* convert to int to increment easily */
2132 vtop->type.t = VT_INT;
2133 gaddrof();
2134 vpushi(4);
2135 gen_op('+');
2136 vtop->r |= VT_LVAL;
2137 vswap();
2138 /* XXX: it works because r2 is spilled last ! */
2139 store(vtop->r2, vtop - 1);
2141 #endif
2143 vswap();
2144 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2145 vtop->r |= delayed_cast;
2149 /* post defines POST/PRE add. c is the token ++ or -- */
2150 void inc(int post, int c)
2152 test_lvalue();
2153 vdup(); /* save lvalue */
2154 if (post) {
2155 gv_dup(); /* duplicate value */
2156 vrotb(3);
2157 vrotb(3);
2159 /* add constant */
2160 vpushi(c - TOK_MID);
2161 gen_op('+');
2162 vstore(); /* store value */
2163 if (post)
2164 vpop(); /* if post op, return saved value */
2167 /* Parse GNUC __attribute__ extension. Currently, the following
2168 extensions are recognized:
2169 - aligned(n) : set data/function alignment.
2170 - packed : force data alignment to 1
2171 - section(x) : generate data/code in this section.
2172 - unused : currently ignored, but may be used someday.
2173 - regparm(n) : pass function parameters in registers (i386 only)
2175 static void parse_attribute(AttributeDef *ad)
2177 int t, n;
2179 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2180 next();
2181 skip('(');
2182 skip('(');
2183 while (tok != ')') {
2184 if (tok < TOK_IDENT)
2185 expect("attribute name");
2186 t = tok;
2187 next();
2188 switch(t) {
2189 case TOK_SECTION1:
2190 case TOK_SECTION2:
2191 skip('(');
2192 if (tok != TOK_STR)
2193 expect("section name");
2194 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2195 next();
2196 skip(')');
2197 break;
2198 case TOK_ALIGNED1:
2199 case TOK_ALIGNED2:
2200 if (tok == '(') {
2201 next();
2202 n = expr_const();
2203 if (n <= 0 || (n & (n - 1)) != 0)
2204 error("alignment must be a positive power of two");
2205 skip(')');
2206 } else {
2207 n = MAX_ALIGN;
2209 ad->aligned = n;
2210 break;
2211 case TOK_PACKED1:
2212 case TOK_PACKED2:
2213 ad->packed = 1;
2214 break;
2215 case TOK_UNUSED1:
2216 case TOK_UNUSED2:
2217 /* currently, no need to handle it because tcc does not
2218 track unused objects */
2219 break;
2220 case TOK_NORETURN1:
2221 case TOK_NORETURN2:
2222 /* currently, no need to handle it because tcc does not
2223 track unused objects */
2224 break;
2225 case TOK_CDECL1:
2226 case TOK_CDECL2:
2227 case TOK_CDECL3:
2228 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
2229 break;
2230 case TOK_STDCALL1:
2231 case TOK_STDCALL2:
2232 case TOK_STDCALL3:
2233 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
2234 break;
2235 #ifdef TCC_TARGET_I386
2236 case TOK_REGPARM1:
2237 case TOK_REGPARM2:
2238 skip('(');
2239 n = expr_const();
2240 if (n > 3)
2241 n = 3;
2242 else if (n < 0)
2243 n = 0;
2244 if (n > 0)
2245 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
2246 skip(')');
2247 break;
2248 case TOK_FASTCALL1:
2249 case TOK_FASTCALL2:
2250 case TOK_FASTCALL3:
2251 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
2252 break;
2253 #endif
2254 case TOK_DLLEXPORT:
2255 FUNC_EXPORT(ad->func_attr) = 1;
2256 break;
2257 default:
2258 if (tcc_state->warn_unsupported)
2259 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2260 /* skip parameters */
2261 if (tok == '(') {
2262 int parenthesis = 0;
2263 do {
2264 if (tok == '(')
2265 parenthesis++;
2266 else if (tok == ')')
2267 parenthesis--;
2268 next();
2269 } while (parenthesis && tok != -1);
2271 break;
2273 if (tok != ',')
2274 break;
2275 next();
2277 skip(')');
2278 skip(')');
2282 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2283 static void struct_decl(CType *type, int u)
2285 int a, v, size, align, maxalign, c, offset;
2286 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2287 Sym *s, *ss, *ass, **ps;
2288 AttributeDef ad;
2289 CType type1, btype;
2291 a = tok; /* save decl type */
2292 next();
2293 if (tok != '{') {
2294 v = tok;
2295 next();
2296 /* struct already defined ? return it */
2297 if (v < TOK_IDENT)
2298 expect("struct/union/enum name");
2299 s = struct_find(v);
2300 if (s) {
2301 if (s->type.t != a)
2302 error("invalid type");
2303 goto do_decl;
2305 } else {
2306 v = anon_sym++;
2308 type1.t = a;
2309 /* we put an undefined size for struct/union */
2310 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2311 s->r = 0; /* default alignment is zero as gcc */
2312 /* put struct/union/enum name in type */
2313 do_decl:
2314 type->t = u;
2315 type->ref = s;
2317 if (tok == '{') {
2318 next();
2319 if (s->c != -1)
2320 error("struct/union/enum already defined");
2321 /* cannot be empty */
2322 c = 0;
2323 /* non empty enums are not allowed */
2324 if (a == TOK_ENUM) {
2325 for(;;) {
2326 v = tok;
2327 if (v < TOK_UIDENT)
2328 expect("identifier");
2329 next();
2330 if (tok == '=') {
2331 next();
2332 c = expr_const();
2334 /* enum symbols have static storage */
2335 ss = sym_push(v, &int_type, VT_CONST, c);
2336 ss->type.t |= VT_STATIC;
2337 if (tok != ',')
2338 break;
2339 next();
2340 c++;
2341 /* NOTE: we accept a trailing comma */
2342 if (tok == '}')
2343 break;
2345 skip('}');
2346 } else {
2347 maxalign = 1;
2348 ps = &s->next;
2349 prevbt = VT_INT;
2350 bit_pos = 0;
2351 offset = 0;
2352 while (tok != '}') {
2353 parse_btype(&btype, &ad);
2354 while (1) {
2355 bit_size = -1;
2356 v = 0;
2357 type1 = btype;
2358 if (tok != ':') {
2359 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2360 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2361 expect("identifier");
2362 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2363 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2364 error("invalid type for '%s'",
2365 get_tok_str(v, NULL));
2367 if (tok == ':') {
2368 next();
2369 bit_size = expr_const();
2370 /* XXX: handle v = 0 case for messages */
2371 if (bit_size < 0)
2372 error("negative width in bit-field '%s'",
2373 get_tok_str(v, NULL));
2374 if (v && bit_size == 0)
2375 error("zero width for bit-field '%s'",
2376 get_tok_str(v, NULL));
2378 size = type_size(&type1, &align);
2379 if (ad.aligned) {
2380 if (align < ad.aligned)
2381 align = ad.aligned;
2382 } else if (ad.packed) {
2383 align = 1;
2384 } else if (*tcc_state->pack_stack_ptr) {
2385 if (align > *tcc_state->pack_stack_ptr)
2386 align = *tcc_state->pack_stack_ptr;
2388 lbit_pos = 0;
2389 if (bit_size >= 0) {
2390 bt = type1.t & VT_BTYPE;
2391 if (bt != VT_INT &&
2392 bt != VT_BYTE &&
2393 bt != VT_SHORT &&
2394 bt != VT_BOOL &&
2395 bt != VT_ENUM &&
2396 bt != VT_LLONG)
2397 error("bitfields must have scalar type");
2398 bsize = size * 8;
2399 if (bit_size > bsize) {
2400 error("width of '%s' exceeds its type",
2401 get_tok_str(v, NULL));
2402 } else if (bit_size == bsize) {
2403 /* no need for bit fields */
2404 bit_pos = 0;
2405 } else if (bit_size == 0) {
2406 /* XXX: what to do if only padding in a
2407 structure ? */
2408 /* zero size: means to pad */
2409 bit_pos = 0;
2410 } else {
2411 /* we do not have enough room ?
2412 did the type change?
2413 is it a union? */
2414 if ((bit_pos + bit_size) > bsize ||
2415 bt != prevbt || a == TOK_UNION)
2416 bit_pos = 0;
2417 lbit_pos = bit_pos;
2418 /* XXX: handle LSB first */
2419 type1.t |= VT_BITFIELD |
2420 (bit_pos << VT_STRUCT_SHIFT) |
2421 (bit_size << (VT_STRUCT_SHIFT + 6));
2422 bit_pos += bit_size;
2424 prevbt = bt;
2425 } else {
2426 bit_pos = 0;
2428 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2429 /* add new memory data only if starting
2430 bit field */
2431 if (lbit_pos == 0) {
2432 if (a == TOK_STRUCT) {
2433 c = (c + align - 1) & -align;
2434 offset = c;
2435 if (size > 0)
2436 c += size;
2437 } else {
2438 offset = 0;
2439 if (size > c)
2440 c = size;
2442 if (align > maxalign)
2443 maxalign = align;
2445 #if 0
2446 printf("add field %s offset=%d",
2447 get_tok_str(v, NULL), offset);
2448 if (type1.t & VT_BITFIELD) {
2449 printf(" pos=%d size=%d",
2450 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2451 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2453 printf("\n");
2454 #endif
2456 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2457 ass = type1.ref;
2458 while ((ass = ass->next) != NULL) {
2459 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2460 *ps = ss;
2461 ps = &ss->next;
2463 } else if (v) {
2464 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2465 *ps = ss;
2466 ps = &ss->next;
2468 if (tok == ';' || tok == TOK_EOF)
2469 break;
2470 skip(',');
2472 skip(';');
2474 skip('}');
2475 /* store size and alignment */
2476 s->c = (c + maxalign - 1) & -maxalign;
2477 s->r = maxalign;
2482 /* return 0 if no type declaration. otherwise, return the basic type
2483 and skip it.
2485 static int parse_btype(CType *type, AttributeDef *ad)
2487 int t, u, type_found, typespec_found, typedef_found;
2488 Sym *s;
2489 CType type1;
2491 memset(ad, 0, sizeof(AttributeDef));
2492 type_found = 0;
2493 typespec_found = 0;
2494 typedef_found = 0;
2495 t = 0;
2496 while(1) {
2497 switch(tok) {
2498 case TOK_EXTENSION:
2499 /* currently, we really ignore extension */
2500 next();
2501 continue;
2503 /* basic types */
2504 case TOK_CHAR:
2505 u = VT_BYTE;
2506 basic_type:
2507 next();
2508 basic_type1:
2509 if ((t & VT_BTYPE) != 0)
2510 error("too many basic types");
2511 t |= u;
2512 typespec_found = 1;
2513 break;
2514 case TOK_VOID:
2515 u = VT_VOID;
2516 goto basic_type;
2517 case TOK_SHORT:
2518 u = VT_SHORT;
2519 goto basic_type;
2520 case TOK_INT:
2521 next();
2522 typespec_found = 1;
2523 break;
2524 case TOK_LONG:
2525 next();
2526 if ((t & VT_BTYPE) == VT_DOUBLE) {
2527 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2528 } else if ((t & VT_BTYPE) == VT_LONG) {
2529 t = (t & ~VT_BTYPE) | VT_LLONG;
2530 } else {
2531 u = VT_LONG;
2532 goto basic_type1;
2534 break;
2535 case TOK_BOOL:
2536 u = VT_BOOL;
2537 goto basic_type;
2538 case TOK_FLOAT:
2539 u = VT_FLOAT;
2540 goto basic_type;
2541 case TOK_DOUBLE:
2542 next();
2543 if ((t & VT_BTYPE) == VT_LONG) {
2544 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2545 } else {
2546 u = VT_DOUBLE;
2547 goto basic_type1;
2549 break;
2550 case TOK_ENUM:
2551 struct_decl(&type1, VT_ENUM);
2552 basic_type2:
2553 u = type1.t;
2554 type->ref = type1.ref;
2555 goto basic_type1;
2556 case TOK_STRUCT:
2557 case TOK_UNION:
2558 struct_decl(&type1, VT_STRUCT);
2559 goto basic_type2;
2561 /* type modifiers */
2562 case TOK_CONST1:
2563 case TOK_CONST2:
2564 case TOK_CONST3:
2565 t |= VT_CONSTANT;
2566 next();
2567 break;
2568 case TOK_VOLATILE1:
2569 case TOK_VOLATILE2:
2570 case TOK_VOLATILE3:
2571 t |= VT_VOLATILE;
2572 next();
2573 break;
2574 case TOK_SIGNED1:
2575 case TOK_SIGNED2:
2576 case TOK_SIGNED3:
2577 typespec_found = 1;
2578 t |= VT_SIGNED;
2579 next();
2580 break;
2581 case TOK_REGISTER:
2582 case TOK_AUTO:
2583 case TOK_RESTRICT1:
2584 case TOK_RESTRICT2:
2585 case TOK_RESTRICT3:
2586 next();
2587 break;
2588 case TOK_UNSIGNED:
2589 t |= VT_UNSIGNED;
2590 next();
2591 typespec_found = 1;
2592 break;
2594 /* storage */
2595 case TOK_EXTERN:
2596 t |= VT_EXTERN;
2597 next();
2598 break;
2599 case TOK_STATIC:
2600 t |= VT_STATIC;
2601 next();
2602 break;
2603 case TOK_TYPEDEF:
2604 t |= VT_TYPEDEF;
2605 next();
2606 break;
2607 case TOK_INLINE1:
2608 case TOK_INLINE2:
2609 case TOK_INLINE3:
2610 t |= VT_INLINE;
2611 next();
2612 break;
2614 /* GNUC attribute */
2615 case TOK_ATTRIBUTE1:
2616 case TOK_ATTRIBUTE2:
2617 parse_attribute(ad);
2618 break;
2619 /* GNUC typeof */
2620 case TOK_TYPEOF1:
2621 case TOK_TYPEOF2:
2622 case TOK_TYPEOF3:
2623 next();
2624 parse_expr_type(&type1);
2625 goto basic_type2;
2626 default:
2627 if (typespec_found || typedef_found)
2628 goto the_end;
2629 s = sym_find(tok);
2630 if (!s || !(s->type.t & VT_TYPEDEF))
2631 goto the_end;
2632 typedef_found = 1;
2633 t |= (s->type.t & ~VT_TYPEDEF);
2634 type->ref = s->type.ref;
2635 next();
2636 typespec_found = 1;
2637 break;
2639 type_found = 1;
2641 the_end:
2642 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2643 error("signed and unsigned modifier");
2644 if (tcc_state->char_is_unsigned) {
2645 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2646 t |= VT_UNSIGNED;
2648 t &= ~VT_SIGNED;
2650 /* long is never used as type */
2651 if ((t & VT_BTYPE) == VT_LONG)
2652 #ifndef TCC_TARGET_X86_64
2653 t = (t & ~VT_BTYPE) | VT_INT;
2654 #else
2655 t = (t & ~VT_BTYPE) | VT_LLONG;
2656 #endif
2657 type->t = t;
2658 return type_found;
2661 /* convert a function parameter type (array to pointer and function to
2662 function pointer) */
2663 static inline void convert_parameter_type(CType *pt)
2665 /* remove const and volatile qualifiers (XXX: const could be used
2666 to indicate a const function parameter */
2667 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2668 /* array must be transformed to pointer according to ANSI C */
2669 pt->t &= ~VT_ARRAY;
2670 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2671 mk_pointer(pt);
2675 static void post_type(CType *type, AttributeDef *ad)
2677 int n, l, t1, arg_size, align;
2678 Sym **plast, *s, *first;
2679 AttributeDef ad1;
2680 CType pt;
2682 if (tok == '(') {
2683 /* function declaration */
2684 next();
2685 l = 0;
2686 first = NULL;
2687 plast = &first;
2688 arg_size = 0;
2689 if (tok != ')') {
2690 for(;;) {
2691 /* read param name and compute offset */
2692 if (l != FUNC_OLD) {
2693 if (!parse_btype(&pt, &ad1)) {
2694 if (l) {
2695 error("invalid type");
2696 } else {
2697 l = FUNC_OLD;
2698 goto old_proto;
2701 l = FUNC_NEW;
2702 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2703 break;
2704 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2705 if ((pt.t & VT_BTYPE) == VT_VOID)
2706 error("parameter declared as void");
2707 arg_size += (type_size(&pt, &align) + 3) & ~3;
2708 } else {
2709 old_proto:
2710 n = tok;
2711 if (n < TOK_UIDENT)
2712 expect("identifier");
2713 pt.t = VT_INT;
2714 next();
2716 convert_parameter_type(&pt);
2717 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
2718 *plast = s;
2719 plast = &s->next;
2720 if (tok == ')')
2721 break;
2722 skip(',');
2723 if (l == FUNC_NEW && tok == TOK_DOTS) {
2724 l = FUNC_ELLIPSIS;
2725 next();
2726 break;
2730 /* if no parameters, then old type prototype */
2731 if (l == 0)
2732 l = FUNC_OLD;
2733 skip(')');
2734 t1 = type->t & VT_STORAGE;
2735 /* NOTE: const is ignored in returned type as it has a special
2736 meaning in gcc / C++ */
2737 type->t &= ~(VT_STORAGE | VT_CONSTANT);
2738 post_type(type, ad);
2739 /* we push a anonymous symbol which will contain the function prototype */
2740 FUNC_ARGS(ad->func_attr) = arg_size;
2741 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
2742 s->next = first;
2743 type->t = t1 | VT_FUNC;
2744 type->ref = s;
2745 } else if (tok == '[') {
2746 /* array definition */
2747 next();
2748 if (tok == TOK_RESTRICT1)
2749 next();
2750 n = -1;
2751 if (tok != ']') {
2752 n = expr_const();
2753 if (n < 0)
2754 error("invalid array size");
2756 skip(']');
2757 /* parse next post type */
2758 t1 = type->t & VT_STORAGE;
2759 type->t &= ~VT_STORAGE;
2760 post_type(type, ad);
2762 /* we push a anonymous symbol which will contain the array
2763 element type */
2764 s = sym_push(SYM_FIELD, type, 0, n);
2765 type->t = t1 | VT_ARRAY | VT_PTR;
2766 type->ref = s;
2770 /* Parse a type declaration (except basic type), and return the type
2771 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2772 expected. 'type' should contain the basic type. 'ad' is the
2773 attribute definition of the basic type. It can be modified by
2774 type_decl().
2776 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
2778 Sym *s;
2779 CType type1, *type2;
2780 int qualifiers;
2782 while (tok == '*') {
2783 qualifiers = 0;
2784 redo:
2785 next();
2786 switch(tok) {
2787 case TOK_CONST1:
2788 case TOK_CONST2:
2789 case TOK_CONST3:
2790 qualifiers |= VT_CONSTANT;
2791 goto redo;
2792 case TOK_VOLATILE1:
2793 case TOK_VOLATILE2:
2794 case TOK_VOLATILE3:
2795 qualifiers |= VT_VOLATILE;
2796 goto redo;
2797 case TOK_RESTRICT1:
2798 case TOK_RESTRICT2:
2799 case TOK_RESTRICT3:
2800 goto redo;
2802 mk_pointer(type);
2803 type->t |= qualifiers;
2806 /* XXX: clarify attribute handling */
2807 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2808 parse_attribute(ad);
2810 /* recursive type */
2811 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2812 type1.t = 0; /* XXX: same as int */
2813 if (tok == '(') {
2814 next();
2815 /* XXX: this is not correct to modify 'ad' at this point, but
2816 the syntax is not clear */
2817 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2818 parse_attribute(ad);
2819 type_decl(&type1, ad, v, td);
2820 skip(')');
2821 } else {
2822 /* type identifier */
2823 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
2824 *v = tok;
2825 next();
2826 } else {
2827 if (!(td & TYPE_ABSTRACT))
2828 expect("identifier");
2829 *v = 0;
2832 post_type(type, ad);
2833 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2834 parse_attribute(ad);
2835 if (!type1.t)
2836 return;
2837 /* append type at the end of type1 */
2838 type2 = &type1;
2839 for(;;) {
2840 s = type2->ref;
2841 type2 = &s->type;
2842 if (!type2->t) {
2843 *type2 = *type;
2844 break;
2847 *type = type1;
2850 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2851 static int lvalue_type(int t)
2853 int bt, r;
2854 r = VT_LVAL;
2855 bt = t & VT_BTYPE;
2856 if (bt == VT_BYTE || bt == VT_BOOL)
2857 r |= VT_LVAL_BYTE;
2858 else if (bt == VT_SHORT)
2859 r |= VT_LVAL_SHORT;
2860 else
2861 return r;
2862 if (t & VT_UNSIGNED)
2863 r |= VT_LVAL_UNSIGNED;
2864 return r;
2867 /* indirection with full error checking and bound check */
2868 static void indir(void)
2870 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
2871 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
2872 return;
2873 expect("pointer");
2875 if ((vtop->r & VT_LVAL) && !nocode_wanted)
2876 gv(RC_INT);
2877 vtop->type = *pointed_type(&vtop->type);
2878 /* Arrays and functions are never lvalues */
2879 if (!(vtop->type.t & VT_ARRAY)
2880 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
2881 vtop->r |= lvalue_type(vtop->type.t);
2882 /* if bound checking, the referenced pointer must be checked */
2883 if (tcc_state->do_bounds_check)
2884 vtop->r |= VT_MUSTBOUND;
2888 /* pass a parameter to a function and do type checking and casting */
2889 static void gfunc_param_typed(Sym *func, Sym *arg)
2891 int func_type;
2892 CType type;
2894 func_type = func->c;
2895 if (func_type == FUNC_OLD ||
2896 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
2897 /* default casting : only need to convert float to double */
2898 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
2899 type.t = VT_DOUBLE;
2900 gen_cast(&type);
2902 } else if (arg == NULL) {
2903 error("too many arguments to function");
2904 } else {
2905 type = arg->type;
2906 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
2907 gen_assign_cast(&type);
2911 /* parse an expression of the form '(type)' or '(expr)' and return its
2912 type */
2913 static void parse_expr_type(CType *type)
2915 int n;
2916 AttributeDef ad;
2918 skip('(');
2919 if (parse_btype(type, &ad)) {
2920 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2921 } else {
2922 expr_type(type);
2924 skip(')');
2927 static void parse_type(CType *type)
2929 AttributeDef ad;
2930 int n;
2932 if (!parse_btype(type, &ad)) {
2933 expect("type");
2935 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2938 static void vpush_tokc(int t)
2940 CType type;
2941 type.t = t;
2942 vsetc(&type, VT_CONST, &tokc);
2945 static void unary(void)
2947 int n, t, align, size, r;
2948 CType type;
2949 Sym *s;
2950 AttributeDef ad;
2952 /* XXX: GCC 2.95.3 does not generate a table although it should be
2953 better here */
2954 tok_next:
2955 switch(tok) {
2956 case TOK_EXTENSION:
2957 next();
2958 goto tok_next;
2959 case TOK_CINT:
2960 case TOK_CCHAR:
2961 case TOK_LCHAR:
2962 vpushi(tokc.i);
2963 next();
2964 break;
2965 case TOK_CUINT:
2966 vpush_tokc(VT_INT | VT_UNSIGNED);
2967 next();
2968 break;
2969 case TOK_CLLONG:
2970 vpush_tokc(VT_LLONG);
2971 next();
2972 break;
2973 case TOK_CULLONG:
2974 vpush_tokc(VT_LLONG | VT_UNSIGNED);
2975 next();
2976 break;
2977 case TOK_CFLOAT:
2978 vpush_tokc(VT_FLOAT);
2979 next();
2980 break;
2981 case TOK_CDOUBLE:
2982 vpush_tokc(VT_DOUBLE);
2983 next();
2984 break;
2985 case TOK_CLDOUBLE:
2986 vpush_tokc(VT_LDOUBLE);
2987 next();
2988 break;
2989 case TOK___FUNCTION__:
2990 if (!gnu_ext)
2991 goto tok_identifier;
2992 /* fall thru */
2993 case TOK___FUNC__:
2995 void *ptr;
2996 int len;
2997 /* special function name identifier */
2998 len = strlen(funcname) + 1;
2999 /* generate char[len] type */
3000 type.t = VT_BYTE;
3001 mk_pointer(&type);
3002 type.t |= VT_ARRAY;
3003 type.ref->c = len;
3004 vpush_ref(&type, data_section, data_section->data_offset, len);
3005 ptr = section_ptr_add(data_section, len);
3006 memcpy(ptr, funcname, len);
3007 next();
3009 break;
3010 case TOK_LSTR:
3011 #ifdef TCC_TARGET_PE
3012 t = VT_SHORT | VT_UNSIGNED;
3013 #else
3014 t = VT_INT;
3015 #endif
3016 goto str_init;
3017 case TOK_STR:
3018 /* string parsing */
3019 t = VT_BYTE;
3020 str_init:
3021 if (tcc_state->warn_write_strings)
3022 t |= VT_CONSTANT;
3023 type.t = t;
3024 mk_pointer(&type);
3025 type.t |= VT_ARRAY;
3026 memset(&ad, 0, sizeof(AttributeDef));
3027 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3028 break;
3029 case '(':
3030 next();
3031 /* cast ? */
3032 if (parse_btype(&type, &ad)) {
3033 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3034 skip(')');
3035 /* check ISOC99 compound literal */
3036 if (tok == '{') {
3037 /* data is allocated locally by default */
3038 if (global_expr)
3039 r = VT_CONST;
3040 else
3041 r = VT_LOCAL;
3042 /* all except arrays are lvalues */
3043 if (!(type.t & VT_ARRAY))
3044 r |= lvalue_type(type.t);
3045 memset(&ad, 0, sizeof(AttributeDef));
3046 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3047 } else {
3048 unary();
3049 gen_cast(&type);
3051 } else if (tok == '{') {
3052 /* save all registers */
3053 save_regs(0);
3054 /* statement expression : we do not accept break/continue
3055 inside as GCC does */
3056 block(NULL, NULL, NULL, NULL, 0, 1);
3057 skip(')');
3058 } else {
3059 gexpr();
3060 skip(')');
3062 break;
3063 case '*':
3064 next();
3065 unary();
3066 indir();
3067 break;
3068 case '&':
3069 next();
3070 unary();
3071 /* functions names must be treated as function pointers,
3072 except for unary '&' and sizeof. Since we consider that
3073 functions are not lvalues, we only have to handle it
3074 there and in function calls. */
3075 /* arrays can also be used although they are not lvalues */
3076 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3077 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3078 test_lvalue();
3079 mk_pointer(&vtop->type);
3080 gaddrof();
3081 break;
3082 case '!':
3083 next();
3084 unary();
3085 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3086 CType boolean;
3087 boolean.t = VT_BOOL;
3088 gen_cast(&boolean);
3089 vtop->c.i = !vtop->c.i;
3090 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3091 vtop->c.i = vtop->c.i ^ 1;
3092 else {
3093 save_regs(1);
3094 vseti(VT_JMP, gtst(1, 0));
3096 break;
3097 case '~':
3098 next();
3099 unary();
3100 vpushi(-1);
3101 gen_op('^');
3102 break;
3103 case '+':
3104 next();
3105 /* in order to force cast, we add zero */
3106 unary();
3107 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3108 error("pointer not accepted for unary plus");
3109 vpushi(0);
3110 gen_op('+');
3111 break;
3112 case TOK_SIZEOF:
3113 case TOK_ALIGNOF1:
3114 case TOK_ALIGNOF2:
3115 t = tok;
3116 next();
3117 if (tok == '(') {
3118 parse_expr_type(&type);
3119 } else {
3120 unary_type(&type);
3122 size = type_size(&type, &align);
3123 if (t == TOK_SIZEOF) {
3124 if (size < 0)
3125 error("sizeof applied to an incomplete type");
3126 vpushi(size);
3127 } else {
3128 vpushi(align);
3130 vtop->type.t |= VT_UNSIGNED;
3131 break;
3133 case TOK_builtin_types_compatible_p:
3135 CType type1, type2;
3136 next();
3137 skip('(');
3138 parse_type(&type1);
3139 skip(',');
3140 parse_type(&type2);
3141 skip(')');
3142 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3143 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3144 vpushi(is_compatible_types(&type1, &type2));
3146 break;
3147 case TOK_builtin_constant_p:
3149 int saved_nocode_wanted, res;
3150 next();
3151 skip('(');
3152 saved_nocode_wanted = nocode_wanted;
3153 nocode_wanted = 1;
3154 gexpr();
3155 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3156 vpop();
3157 nocode_wanted = saved_nocode_wanted;
3158 skip(')');
3159 vpushi(res);
3161 break;
3162 case TOK_builtin_frame_address:
3164 CType type;
3165 next();
3166 skip('(');
3167 if (tok != TOK_CINT) {
3168 error("__builtin_frame_address only takes integers");
3170 if (tokc.i != 0) {
3171 error("TCC only supports __builtin_frame_address(0)");
3173 next();
3174 skip(')');
3175 type.t = VT_VOID;
3176 mk_pointer(&type);
3177 vset(&type, VT_LOCAL, 0);
3179 break;
3180 #ifdef TCC_TARGET_X86_64
3181 case TOK_builtin_malloc:
3182 tok = TOK_malloc;
3183 goto tok_identifier;
3184 case TOK_builtin_free:
3185 tok = TOK_free;
3186 goto tok_identifier;
3187 #endif
3188 case TOK_INC:
3189 case TOK_DEC:
3190 t = tok;
3191 next();
3192 unary();
3193 inc(0, t);
3194 break;
3195 case '-':
3196 next();
3197 vpushi(0);
3198 unary();
3199 gen_op('-');
3200 break;
3201 case TOK_LAND:
3202 if (!gnu_ext)
3203 goto tok_identifier;
3204 next();
3205 /* allow to take the address of a label */
3206 if (tok < TOK_UIDENT)
3207 expect("label identifier");
3208 s = label_find(tok);
3209 if (!s) {
3210 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3211 } else {
3212 if (s->r == LABEL_DECLARED)
3213 s->r = LABEL_FORWARD;
3215 if (!s->type.t) {
3216 s->type.t = VT_VOID;
3217 mk_pointer(&s->type);
3218 s->type.t |= VT_STATIC;
3220 vset(&s->type, VT_CONST | VT_SYM, 0);
3221 vtop->sym = s;
3222 next();
3223 break;
3224 default:
3225 tok_identifier:
3226 t = tok;
3227 next();
3228 if (t < TOK_UIDENT)
3229 expect("identifier");
3230 s = sym_find(t);
3231 if (!s) {
3232 if (tok != '(')
3233 error("'%s' undeclared", get_tok_str(t, NULL));
3234 /* for simple function calls, we tolerate undeclared
3235 external reference to int() function */
3236 if (tcc_state->warn_implicit_function_declaration)
3237 warning("implicit declaration of function '%s'",
3238 get_tok_str(t, NULL));
3239 s = external_global_sym(t, &func_old_type, 0);
3241 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3242 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3243 /* if referencing an inline function, then we generate a
3244 symbol to it if not already done. It will have the
3245 effect to generate code for it at the end of the
3246 compilation unit. Inline function as always
3247 generated in the text section. */
3248 if (!s->c)
3249 put_extern_sym(s, text_section, 0, 0);
3250 r = VT_SYM | VT_CONST;
3251 } else {
3252 r = s->r;
3254 vset(&s->type, r, s->c);
3255 /* if forward reference, we must point to s */
3256 if (vtop->r & VT_SYM) {
3257 vtop->sym = s;
3258 vtop->c.ul = 0;
3260 break;
3263 /* post operations */
3264 while (1) {
3265 if (tok == TOK_INC || tok == TOK_DEC) {
3266 inc(1, tok);
3267 next();
3268 } else if (tok == '.' || tok == TOK_ARROW) {
3269 int qualifiers;
3270 /* field */
3271 if (tok == TOK_ARROW)
3272 indir();
3273 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3274 test_lvalue();
3275 gaddrof();
3276 next();
3277 /* expect pointer on structure */
3278 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3279 expect("struct or union");
3280 s = vtop->type.ref;
3281 /* find field */
3282 tok |= SYM_FIELD;
3283 while ((s = s->next) != NULL) {
3284 if (s->v == tok)
3285 break;
3287 if (!s)
3288 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3289 /* add field offset to pointer */
3290 vtop->type = char_pointer_type; /* change type to 'char *' */
3291 vpushi(s->c);
3292 gen_op('+');
3293 /* change type to field type, and set to lvalue */
3294 vtop->type = s->type;
3295 vtop->type.t |= qualifiers;
3296 /* an array is never an lvalue */
3297 if (!(vtop->type.t & VT_ARRAY)) {
3298 vtop->r |= lvalue_type(vtop->type.t);
3299 /* if bound checking, the referenced pointer must be checked */
3300 if (tcc_state->do_bounds_check)
3301 vtop->r |= VT_MUSTBOUND;
3303 next();
3304 } else if (tok == '[') {
3305 next();
3306 gexpr();
3307 gen_op('+');
3308 indir();
3309 skip(']');
3310 } else if (tok == '(') {
3311 SValue ret;
3312 Sym *sa;
3313 int nb_args;
3315 /* function call */
3316 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3317 /* pointer test (no array accepted) */
3318 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3319 vtop->type = *pointed_type(&vtop->type);
3320 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3321 goto error_func;
3322 } else {
3323 error_func:
3324 expect("function pointer");
3326 } else {
3327 vtop->r &= ~VT_LVAL; /* no lvalue */
3329 /* get return type */
3330 s = vtop->type.ref;
3331 next();
3332 sa = s->next; /* first parameter */
3333 nb_args = 0;
3334 ret.r2 = VT_CONST;
3335 /* compute first implicit argument if a structure is returned */
3336 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3337 /* get some space for the returned structure */
3338 size = type_size(&s->type, &align);
3339 loc = (loc - size) & -align;
3340 ret.type = s->type;
3341 ret.r = VT_LOCAL | VT_LVAL;
3342 /* pass it as 'int' to avoid structure arg passing
3343 problems */
3344 vseti(VT_LOCAL, loc);
3345 ret.c = vtop->c;
3346 nb_args++;
3347 } else {
3348 ret.type = s->type;
3349 /* return in register */
3350 if (is_float(ret.type.t)) {
3351 ret.r = reg_fret(ret.type.t);
3352 } else {
3353 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3354 ret.r2 = REG_LRET;
3355 ret.r = REG_IRET;
3357 ret.c.i = 0;
3359 if (tok != ')') {
3360 for(;;) {
3361 expr_eq();
3362 gfunc_param_typed(s, sa);
3363 nb_args++;
3364 if (sa)
3365 sa = sa->next;
3366 if (tok == ')')
3367 break;
3368 skip(',');
3371 if (sa)
3372 error("too few arguments to function");
3373 skip(')');
3374 if (!nocode_wanted) {
3375 gfunc_call(nb_args);
3376 } else {
3377 vtop -= (nb_args + 1);
3379 /* return value */
3380 vsetc(&ret.type, ret.r, &ret.c);
3381 vtop->r2 = ret.r2;
3382 } else {
3383 break;
3388 static void uneq(void)
3390 int t;
3392 unary();
3393 if (tok == '=' ||
3394 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3395 tok == TOK_A_XOR || tok == TOK_A_OR ||
3396 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3397 test_lvalue();
3398 t = tok;
3399 next();
3400 if (t == '=') {
3401 expr_eq();
3402 } else {
3403 vdup();
3404 expr_eq();
3405 gen_op(t & 0x7f);
3407 vstore();
3411 static void expr_prod(void)
3413 int t;
3415 uneq();
3416 while (tok == '*' || tok == '/' || tok == '%') {
3417 t = tok;
3418 next();
3419 uneq();
3420 gen_op(t);
3424 static void expr_sum(void)
3426 int t;
3428 expr_prod();
3429 while (tok == '+' || tok == '-') {
3430 t = tok;
3431 next();
3432 expr_prod();
3433 gen_op(t);
3437 static void expr_shift(void)
3439 int t;
3441 expr_sum();
3442 while (tok == TOK_SHL || tok == TOK_SAR) {
3443 t = tok;
3444 next();
3445 expr_sum();
3446 gen_op(t);
3450 static void expr_cmp(void)
3452 int t;
3454 expr_shift();
3455 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3456 tok == TOK_ULT || tok == TOK_UGE) {
3457 t = tok;
3458 next();
3459 expr_shift();
3460 gen_op(t);
3464 static void expr_cmpeq(void)
3466 int t;
3468 expr_cmp();
3469 while (tok == TOK_EQ || tok == TOK_NE) {
3470 t = tok;
3471 next();
3472 expr_cmp();
3473 gen_op(t);
3477 static void expr_and(void)
3479 expr_cmpeq();
3480 while (tok == '&') {
3481 next();
3482 expr_cmpeq();
3483 gen_op('&');
3487 static void expr_xor(void)
3489 expr_and();
3490 while (tok == '^') {
3491 next();
3492 expr_and();
3493 gen_op('^');
3497 static void expr_or(void)
3499 expr_xor();
3500 while (tok == '|') {
3501 next();
3502 expr_xor();
3503 gen_op('|');
3507 /* XXX: fix this mess */
3508 static void expr_land_const(void)
3510 expr_or();
3511 while (tok == TOK_LAND) {
3512 next();
3513 expr_or();
3514 gen_op(TOK_LAND);
3518 /* XXX: fix this mess */
3519 static void expr_lor_const(void)
3521 expr_land_const();
3522 while (tok == TOK_LOR) {
3523 next();
3524 expr_land_const();
3525 gen_op(TOK_LOR);
3529 /* only used if non constant */
3530 static void expr_land(void)
3532 int t;
3534 expr_or();
3535 if (tok == TOK_LAND) {
3536 t = 0;
3537 save_regs(1);
3538 for(;;) {
3539 t = gtst(1, t);
3540 if (tok != TOK_LAND) {
3541 vseti(VT_JMPI, t);
3542 break;
3544 next();
3545 expr_or();
3550 static void expr_lor(void)
3552 int t;
3554 expr_land();
3555 if (tok == TOK_LOR) {
3556 t = 0;
3557 save_regs(1);
3558 for(;;) {
3559 t = gtst(0, t);
3560 if (tok != TOK_LOR) {
3561 vseti(VT_JMP, t);
3562 break;
3564 next();
3565 expr_land();
3570 /* XXX: better constant handling */
3571 static void expr_eq(void)
3573 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3574 SValue sv;
3575 CType type, type1, type2;
3577 if (const_wanted) {
3578 expr_lor_const();
3579 if (tok == '?') {
3580 CType boolean;
3581 int c;
3582 boolean.t = VT_BOOL;
3583 vdup();
3584 gen_cast(&boolean);
3585 c = vtop->c.i;
3586 vpop();
3587 next();
3588 if (tok != ':' || !gnu_ext) {
3589 vpop();
3590 gexpr();
3592 if (!c)
3593 vpop();
3594 skip(':');
3595 expr_eq();
3596 if (c)
3597 vpop();
3599 } else {
3600 expr_lor();
3601 if (tok == '?') {
3602 next();
3603 if (vtop != vstack) {
3604 /* needed to avoid having different registers saved in
3605 each branch */
3606 if (is_float(vtop->type.t)) {
3607 rc = RC_FLOAT;
3608 #ifdef TCC_TARGET_X86_64
3609 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3610 rc = RC_ST0;
3612 #endif
3614 else
3615 rc = RC_INT;
3616 gv(rc);
3617 save_regs(1);
3619 if (tok == ':' && gnu_ext) {
3620 gv_dup();
3621 tt = gtst(1, 0);
3622 } else {
3623 tt = gtst(1, 0);
3624 gexpr();
3626 type1 = vtop->type;
3627 sv = *vtop; /* save value to handle it later */
3628 vtop--; /* no vpop so that FP stack is not flushed */
3629 skip(':');
3630 u = gjmp(0);
3631 gsym(tt);
3632 expr_eq();
3633 type2 = vtop->type;
3635 t1 = type1.t;
3636 bt1 = t1 & VT_BTYPE;
3637 t2 = type2.t;
3638 bt2 = t2 & VT_BTYPE;
3639 /* cast operands to correct type according to ISOC rules */
3640 if (is_float(bt1) || is_float(bt2)) {
3641 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3642 type.t = VT_LDOUBLE;
3643 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3644 type.t = VT_DOUBLE;
3645 } else {
3646 type.t = VT_FLOAT;
3648 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3649 /* cast to biggest op */
3650 type.t = VT_LLONG;
3651 /* convert to unsigned if it does not fit in a long long */
3652 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3653 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3654 type.t |= VT_UNSIGNED;
3655 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3656 /* XXX: test pointer compatibility */
3657 type = type1;
3658 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3659 /* XXX: test function pointer compatibility */
3660 type = type1;
3661 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3662 /* XXX: test structure compatibility */
3663 type = type1;
3664 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3665 /* NOTE: as an extension, we accept void on only one side */
3666 type.t = VT_VOID;
3667 } else {
3668 /* integer operations */
3669 type.t = VT_INT;
3670 /* convert to unsigned if it does not fit in an integer */
3671 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3672 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3673 type.t |= VT_UNSIGNED;
3676 /* now we convert second operand */
3677 gen_cast(&type);
3678 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3679 gaddrof();
3680 rc = RC_INT;
3681 if (is_float(type.t)) {
3682 rc = RC_FLOAT;
3683 #ifdef TCC_TARGET_X86_64
3684 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3685 rc = RC_ST0;
3687 #endif
3688 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3689 /* for long longs, we use fixed registers to avoid having
3690 to handle a complicated move */
3691 rc = RC_IRET;
3694 r2 = gv(rc);
3695 /* this is horrible, but we must also convert first
3696 operand */
3697 tt = gjmp(0);
3698 gsym(u);
3699 /* put again first value and cast it */
3700 *vtop = sv;
3701 gen_cast(&type);
3702 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3703 gaddrof();
3704 r1 = gv(rc);
3705 move_reg(r2, r1);
3706 vtop->r = r2;
3707 gsym(tt);
3712 static void gexpr(void)
3714 while (1) {
3715 expr_eq();
3716 if (tok != ',')
3717 break;
3718 vpop();
3719 next();
3723 /* parse an expression and return its type without any side effect. */
3724 static void expr_type(CType *type)
3726 int saved_nocode_wanted;
3728 saved_nocode_wanted = nocode_wanted;
3729 nocode_wanted = 1;
3730 gexpr();
3731 *type = vtop->type;
3732 vpop();
3733 nocode_wanted = saved_nocode_wanted;
3736 /* parse a unary expression and return its type without any side
3737 effect. */
3738 static void unary_type(CType *type)
3740 int a;
3742 a = nocode_wanted;
3743 nocode_wanted = 1;
3744 unary();
3745 *type = vtop->type;
3746 vpop();
3747 nocode_wanted = a;
3750 /* parse a constant expression and return value in vtop. */
3751 static void expr_const1(void)
3753 int a;
3754 a = const_wanted;
3755 const_wanted = 1;
3756 expr_eq();
3757 const_wanted = a;
3760 /* parse an integer constant and return its value. */
3761 static int expr_const(void)
3763 int c;
3764 expr_const1();
3765 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
3766 expect("constant expression");
3767 c = vtop->c.i;
3768 vpop();
3769 return c;
3772 /* return the label token if current token is a label, otherwise
3773 return zero */
3774 static int is_label(void)
3776 int last_tok;
3778 /* fast test first */
3779 if (tok < TOK_UIDENT)
3780 return 0;
3781 /* no need to save tokc because tok is an identifier */
3782 last_tok = tok;
3783 next();
3784 if (tok == ':') {
3785 next();
3786 return last_tok;
3787 } else {
3788 unget_tok(last_tok);
3789 return 0;
3793 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
3794 int case_reg, int is_expr)
3796 int a, b, c, d;
3797 Sym *s;
3799 /* generate line number info */
3800 if (tcc_state->do_debug &&
3801 (last_line_num != file->line_num || last_ind != ind)) {
3802 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
3803 last_ind = ind;
3804 last_line_num = file->line_num;
3807 if (is_expr) {
3808 /* default return value is (void) */
3809 vpushi(0);
3810 vtop->type.t = VT_VOID;
3813 if (tok == TOK_IF) {
3814 /* if test */
3815 next();
3816 skip('(');
3817 gexpr();
3818 skip(')');
3819 a = gtst(1, 0);
3820 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3821 c = tok;
3822 if (c == TOK_ELSE) {
3823 next();
3824 d = gjmp(0);
3825 gsym(a);
3826 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3827 gsym(d); /* patch else jmp */
3828 } else
3829 gsym(a);
3830 } else if (tok == TOK_WHILE) {
3831 next();
3832 d = ind;
3833 skip('(');
3834 gexpr();
3835 skip(')');
3836 a = gtst(1, 0);
3837 b = 0;
3838 block(&a, &b, case_sym, def_sym, case_reg, 0);
3839 gjmp_addr(d);
3840 gsym(a);
3841 gsym_addr(b, d);
3842 } else if (tok == '{') {
3843 Sym *llabel;
3845 next();
3846 /* record local declaration stack position */
3847 s = local_stack;
3848 llabel = local_label_stack;
3849 /* handle local labels declarations */
3850 if (tok == TOK_LABEL) {
3851 next();
3852 for(;;) {
3853 if (tok < TOK_UIDENT)
3854 expect("label identifier");
3855 label_push(&local_label_stack, tok, LABEL_DECLARED);
3856 next();
3857 if (tok == ',') {
3858 next();
3859 } else {
3860 skip(';');
3861 break;
3865 while (tok != '}') {
3866 decl(VT_LOCAL);
3867 if (tok != '}') {
3868 if (is_expr)
3869 vpop();
3870 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
3873 /* pop locally defined labels */
3874 label_pop(&local_label_stack, llabel);
3875 /* pop locally defined symbols */
3876 if(is_expr) {
3877 /* XXX: this solution makes only valgrind happy...
3878 triggered by gcc.c-torture/execute/20000917-1.c */
3879 Sym *p;
3880 switch(vtop->type.t & VT_BTYPE) {
3881 case VT_PTR:
3882 case VT_STRUCT:
3883 case VT_ENUM:
3884 case VT_FUNC:
3885 for(p=vtop->type.ref;p;p=p->prev)
3886 if(p->prev==s)
3887 error("unsupported expression type");
3890 sym_pop(&local_stack, s);
3891 next();
3892 } else if (tok == TOK_RETURN) {
3893 next();
3894 if (tok != ';') {
3895 gexpr();
3896 gen_assign_cast(&func_vt);
3897 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
3898 CType type;
3899 /* if returning structure, must copy it to implicit
3900 first pointer arg location */
3901 #ifdef TCC_ARM_EABI
3902 int align, size;
3903 size = type_size(&func_vt,&align);
3904 if(size <= 4)
3906 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
3907 && (align & 3))
3909 int addr;
3910 loc = (loc - size) & -4;
3911 addr = loc;
3912 type = func_vt;
3913 vset(&type, VT_LOCAL | VT_LVAL, addr);
3914 vswap();
3915 vstore();
3916 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
3918 vtop->type = int_type;
3919 gv(RC_IRET);
3920 } else {
3921 #endif
3922 type = func_vt;
3923 mk_pointer(&type);
3924 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
3925 indir();
3926 vswap();
3927 /* copy structure value to pointer */
3928 vstore();
3929 #ifdef TCC_ARM_EABI
3931 #endif
3932 } else if (is_float(func_vt.t)) {
3933 gv(rc_fret(func_vt.t));
3934 } else {
3935 gv(RC_IRET);
3937 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3939 skip(';');
3940 rsym = gjmp(rsym); /* jmp */
3941 } else if (tok == TOK_BREAK) {
3942 /* compute jump */
3943 if (!bsym)
3944 error("cannot break");
3945 *bsym = gjmp(*bsym);
3946 next();
3947 skip(';');
3948 } else if (tok == TOK_CONTINUE) {
3949 /* compute jump */
3950 if (!csym)
3951 error("cannot continue");
3952 *csym = gjmp(*csym);
3953 next();
3954 skip(';');
3955 } else if (tok == TOK_FOR) {
3956 int e;
3957 next();
3958 skip('(');
3959 if (tok != ';') {
3960 gexpr();
3961 vpop();
3963 skip(';');
3964 d = ind;
3965 c = ind;
3966 a = 0;
3967 b = 0;
3968 if (tok != ';') {
3969 gexpr();
3970 a = gtst(1, 0);
3972 skip(';');
3973 if (tok != ')') {
3974 e = gjmp(0);
3975 c = ind;
3976 gexpr();
3977 vpop();
3978 gjmp_addr(d);
3979 gsym(e);
3981 skip(')');
3982 block(&a, &b, case_sym, def_sym, case_reg, 0);
3983 gjmp_addr(c);
3984 gsym(a);
3985 gsym_addr(b, c);
3986 } else
3987 if (tok == TOK_DO) {
3988 next();
3989 a = 0;
3990 b = 0;
3991 d = ind;
3992 block(&a, &b, case_sym, def_sym, case_reg, 0);
3993 skip(TOK_WHILE);
3994 skip('(');
3995 gsym(b);
3996 gexpr();
3997 c = gtst(0, 0);
3998 gsym_addr(c, d);
3999 skip(')');
4000 gsym(a);
4001 skip(';');
4002 } else
4003 if (tok == TOK_SWITCH) {
4004 next();
4005 skip('(');
4006 gexpr();
4007 /* XXX: other types than integer */
4008 case_reg = gv(RC_INT);
4009 vpop();
4010 skip(')');
4011 a = 0;
4012 b = gjmp(0); /* jump to first case */
4013 c = 0;
4014 block(&a, csym, &b, &c, case_reg, 0);
4015 /* if no default, jmp after switch */
4016 if (c == 0)
4017 c = ind;
4018 /* default label */
4019 gsym_addr(b, c);
4020 /* break label */
4021 gsym(a);
4022 } else
4023 if (tok == TOK_CASE) {
4024 int v1, v2;
4025 if (!case_sym)
4026 expect("switch");
4027 next();
4028 v1 = expr_const();
4029 v2 = v1;
4030 if (gnu_ext && tok == TOK_DOTS) {
4031 next();
4032 v2 = expr_const();
4033 if (v2 < v1)
4034 warning("empty case range");
4036 /* since a case is like a label, we must skip it with a jmp */
4037 b = gjmp(0);
4038 gsym(*case_sym);
4039 vseti(case_reg, 0);
4040 vpushi(v1);
4041 if (v1 == v2) {
4042 gen_op(TOK_EQ);
4043 *case_sym = gtst(1, 0);
4044 } else {
4045 gen_op(TOK_GE);
4046 *case_sym = gtst(1, 0);
4047 vseti(case_reg, 0);
4048 vpushi(v2);
4049 gen_op(TOK_LE);
4050 *case_sym = gtst(1, *case_sym);
4052 gsym(b);
4053 skip(':');
4054 is_expr = 0;
4055 goto block_after_label;
4056 } else
4057 if (tok == TOK_DEFAULT) {
4058 next();
4059 skip(':');
4060 if (!def_sym)
4061 expect("switch");
4062 if (*def_sym)
4063 error("too many 'default'");
4064 *def_sym = ind;
4065 is_expr = 0;
4066 goto block_after_label;
4067 } else
4068 if (tok == TOK_GOTO) {
4069 next();
4070 if (tok == '*' && gnu_ext) {
4071 /* computed goto */
4072 next();
4073 gexpr();
4074 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4075 expect("pointer");
4076 ggoto();
4077 } else if (tok >= TOK_UIDENT) {
4078 s = label_find(tok);
4079 /* put forward definition if needed */
4080 if (!s) {
4081 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4082 } else {
4083 if (s->r == LABEL_DECLARED)
4084 s->r = LABEL_FORWARD;
4086 /* label already defined */
4087 if (s->r & LABEL_FORWARD)
4088 s->next = (void *)gjmp((long)s->next);
4089 else
4090 gjmp_addr((long)s->next);
4091 next();
4092 } else {
4093 expect("label identifier");
4095 skip(';');
4096 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4097 asm_instr();
4098 } else {
4099 b = is_label();
4100 if (b) {
4101 /* label case */
4102 s = label_find(b);
4103 if (s) {
4104 if (s->r == LABEL_DEFINED)
4105 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4106 gsym((long)s->next);
4107 s->r = LABEL_DEFINED;
4108 } else {
4109 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4111 s->next = (void *)ind;
4112 /* we accept this, but it is a mistake */
4113 block_after_label:
4114 if (tok == '}') {
4115 warning("deprecated use of label at end of compound statement");
4116 } else {
4117 if (is_expr)
4118 vpop();
4119 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4121 } else {
4122 /* expression case */
4123 if (tok != ';') {
4124 if (is_expr) {
4125 vpop();
4126 gexpr();
4127 } else {
4128 gexpr();
4129 vpop();
4132 skip(';');
4137 /* t is the array or struct type. c is the array or struct
4138 address. cur_index/cur_field is the pointer to the current
4139 value. 'size_only' is true if only size info is needed (only used
4140 in arrays) */
4141 static void decl_designator(CType *type, Section *sec, unsigned long c,
4142 int *cur_index, Sym **cur_field,
4143 int size_only)
4145 Sym *s, *f;
4146 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4147 CType type1;
4149 notfirst = 0;
4150 elem_size = 0;
4151 nb_elems = 1;
4152 if (gnu_ext && (l = is_label()) != 0)
4153 goto struct_field;
4154 while (tok == '[' || tok == '.') {
4155 if (tok == '[') {
4156 if (!(type->t & VT_ARRAY))
4157 expect("array type");
4158 s = type->ref;
4159 next();
4160 index = expr_const();
4161 if (index < 0 || (s->c >= 0 && index >= s->c))
4162 expect("invalid index");
4163 if (tok == TOK_DOTS && gnu_ext) {
4164 next();
4165 index_last = expr_const();
4166 if (index_last < 0 ||
4167 (s->c >= 0 && index_last >= s->c) ||
4168 index_last < index)
4169 expect("invalid index");
4170 } else {
4171 index_last = index;
4173 skip(']');
4174 if (!notfirst)
4175 *cur_index = index_last;
4176 type = pointed_type(type);
4177 elem_size = type_size(type, &align);
4178 c += index * elem_size;
4179 /* NOTE: we only support ranges for last designator */
4180 nb_elems = index_last - index + 1;
4181 if (nb_elems != 1) {
4182 notfirst = 1;
4183 break;
4185 } else {
4186 next();
4187 l = tok;
4188 next();
4189 struct_field:
4190 if ((type->t & VT_BTYPE) != VT_STRUCT)
4191 expect("struct/union type");
4192 s = type->ref;
4193 l |= SYM_FIELD;
4194 f = s->next;
4195 while (f) {
4196 if (f->v == l)
4197 break;
4198 f = f->next;
4200 if (!f)
4201 expect("field");
4202 if (!notfirst)
4203 *cur_field = f;
4204 /* XXX: fix this mess by using explicit storage field */
4205 type1 = f->type;
4206 type1.t |= (type->t & ~VT_TYPE);
4207 type = &type1;
4208 c += f->c;
4210 notfirst = 1;
4212 if (notfirst) {
4213 if (tok == '=') {
4214 next();
4215 } else {
4216 if (!gnu_ext)
4217 expect("=");
4219 } else {
4220 if (type->t & VT_ARRAY) {
4221 index = *cur_index;
4222 type = pointed_type(type);
4223 c += index * type_size(type, &align);
4224 } else {
4225 f = *cur_field;
4226 if (!f)
4227 error("too many field init");
4228 /* XXX: fix this mess by using explicit storage field */
4229 type1 = f->type;
4230 type1.t |= (type->t & ~VT_TYPE);
4231 type = &type1;
4232 c += f->c;
4235 decl_initializer(type, sec, c, 0, size_only);
4237 /* XXX: make it more general */
4238 if (!size_only && nb_elems > 1) {
4239 unsigned long c_end;
4240 uint8_t *src, *dst;
4241 int i;
4243 if (!sec)
4244 error("range init not supported yet for dynamic storage");
4245 c_end = c + nb_elems * elem_size;
4246 if (c_end > sec->data_allocated)
4247 section_realloc(sec, c_end);
4248 src = sec->data + c;
4249 dst = src;
4250 for(i = 1; i < nb_elems; i++) {
4251 dst += elem_size;
4252 memcpy(dst, src, elem_size);
4257 #define EXPR_VAL 0
4258 #define EXPR_CONST 1
4259 #define EXPR_ANY 2
4261 /* store a value or an expression directly in global data or in local array */
4262 static void init_putv(CType *type, Section *sec, unsigned long c,
4263 int v, int expr_type)
4265 int saved_global_expr, bt, bit_pos, bit_size;
4266 void *ptr;
4267 unsigned long long bit_mask;
4268 CType dtype;
4270 switch(expr_type) {
4271 case EXPR_VAL:
4272 vpushi(v);
4273 break;
4274 case EXPR_CONST:
4275 /* compound literals must be allocated globally in this case */
4276 saved_global_expr = global_expr;
4277 global_expr = 1;
4278 expr_const1();
4279 global_expr = saved_global_expr;
4280 /* NOTE: symbols are accepted */
4281 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4282 error("initializer element is not constant");
4283 break;
4284 case EXPR_ANY:
4285 expr_eq();
4286 break;
4289 dtype = *type;
4290 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4292 if (sec) {
4293 /* XXX: not portable */
4294 /* XXX: generate error if incorrect relocation */
4295 gen_assign_cast(&dtype);
4296 bt = type->t & VT_BTYPE;
4297 /* we'll write at most 12 bytes */
4298 if (c + 12 > sec->data_allocated) {
4299 section_realloc(sec, c + 12);
4301 ptr = sec->data + c;
4302 /* XXX: make code faster ? */
4303 if (!(type->t & VT_BITFIELD)) {
4304 bit_pos = 0;
4305 bit_size = 32;
4306 bit_mask = -1LL;
4307 } else {
4308 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4309 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4310 bit_mask = (1LL << bit_size) - 1;
4312 if ((vtop->r & VT_SYM) &&
4313 (bt == VT_BYTE ||
4314 bt == VT_SHORT ||
4315 bt == VT_DOUBLE ||
4316 bt == VT_LDOUBLE ||
4317 bt == VT_LLONG ||
4318 (bt == VT_INT && bit_size != 32)))
4319 error("initializer element is not computable at load time");
4320 switch(bt) {
4321 case VT_BOOL:
4322 vtop->c.i = (vtop->c.i != 0);
4323 case VT_BYTE:
4324 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4325 break;
4326 case VT_SHORT:
4327 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4328 break;
4329 case VT_DOUBLE:
4330 *(double *)ptr = vtop->c.d;
4331 break;
4332 case VT_LDOUBLE:
4333 *(long double *)ptr = vtop->c.ld;
4334 break;
4335 case VT_LLONG:
4336 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4337 break;
4338 default:
4339 if (vtop->r & VT_SYM) {
4340 greloc(sec, vtop->sym, c, R_DATA_32);
4342 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4343 break;
4345 vtop--;
4346 } else {
4347 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4348 vswap();
4349 vstore();
4350 vpop();
4354 /* put zeros for variable based init */
4355 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4357 if (sec) {
4358 /* nothing to do because globals are already set to zero */
4359 } else {
4360 vpush_global_sym(&func_old_type, TOK_memset);
4361 vseti(VT_LOCAL, c);
4362 vpushi(0);
4363 vpushi(size);
4364 gfunc_call(3);
4368 /* 't' contains the type and storage info. 'c' is the offset of the
4369 object in section 'sec'. If 'sec' is NULL, it means stack based
4370 allocation. 'first' is true if array '{' must be read (multi
4371 dimension implicit array init handling). 'size_only' is true if
4372 size only evaluation is wanted (only for arrays). */
4373 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4374 int first, int size_only)
4376 int index, array_length, n, no_oblock, nb, parlevel, i;
4377 int size1, align1, expr_type;
4378 Sym *s, *f;
4379 CType *t1;
4381 if (type->t & VT_ARRAY) {
4382 s = type->ref;
4383 n = s->c;
4384 array_length = 0;
4385 t1 = pointed_type(type);
4386 size1 = type_size(t1, &align1);
4388 no_oblock = 1;
4389 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4390 tok == '{') {
4391 skip('{');
4392 no_oblock = 0;
4395 /* only parse strings here if correct type (otherwise: handle
4396 them as ((w)char *) expressions */
4397 if ((tok == TOK_LSTR &&
4398 #ifdef TCC_TARGET_PE
4399 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4400 #else
4401 (t1->t & VT_BTYPE) == VT_INT
4402 #endif
4403 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4404 while (tok == TOK_STR || tok == TOK_LSTR) {
4405 int cstr_len, ch;
4406 CString *cstr;
4408 cstr = tokc.cstr;
4409 /* compute maximum number of chars wanted */
4410 if (tok == TOK_STR)
4411 cstr_len = cstr->size;
4412 else
4413 cstr_len = cstr->size / sizeof(nwchar_t);
4414 cstr_len--;
4415 nb = cstr_len;
4416 if (n >= 0 && nb > (n - array_length))
4417 nb = n - array_length;
4418 if (!size_only) {
4419 if (cstr_len > nb)
4420 warning("initializer-string for array is too long");
4421 /* in order to go faster for common case (char
4422 string in global variable, we handle it
4423 specifically */
4424 if (sec && tok == TOK_STR && size1 == 1) {
4425 memcpy(sec->data + c + array_length, cstr->data, nb);
4426 } else {
4427 for(i=0;i<nb;i++) {
4428 if (tok == TOK_STR)
4429 ch = ((unsigned char *)cstr->data)[i];
4430 else
4431 ch = ((nwchar_t *)cstr->data)[i];
4432 init_putv(t1, sec, c + (array_length + i) * size1,
4433 ch, EXPR_VAL);
4437 array_length += nb;
4438 next();
4440 /* only add trailing zero if enough storage (no
4441 warning in this case since it is standard) */
4442 if (n < 0 || array_length < n) {
4443 if (!size_only) {
4444 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4446 array_length++;
4448 } else {
4449 index = 0;
4450 while (tok != '}') {
4451 decl_designator(type, sec, c, &index, NULL, size_only);
4452 if (n >= 0 && index >= n)
4453 error("index too large");
4454 /* must put zero in holes (note that doing it that way
4455 ensures that it even works with designators) */
4456 if (!size_only && array_length < index) {
4457 init_putz(t1, sec, c + array_length * size1,
4458 (index - array_length) * size1);
4460 index++;
4461 if (index > array_length)
4462 array_length = index;
4463 /* special test for multi dimensional arrays (may not
4464 be strictly correct if designators are used at the
4465 same time) */
4466 if (index >= n && no_oblock)
4467 break;
4468 if (tok == '}')
4469 break;
4470 skip(',');
4473 if (!no_oblock)
4474 skip('}');
4475 /* put zeros at the end */
4476 if (!size_only && n >= 0 && array_length < n) {
4477 init_putz(t1, sec, c + array_length * size1,
4478 (n - array_length) * size1);
4480 /* patch type size if needed */
4481 if (n < 0)
4482 s->c = array_length;
4483 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4484 (sec || !first || tok == '{')) {
4485 int par_count;
4487 /* NOTE: the previous test is a specific case for automatic
4488 struct/union init */
4489 /* XXX: union needs only one init */
4491 /* XXX: this test is incorrect for local initializers
4492 beginning with ( without {. It would be much more difficult
4493 to do it correctly (ideally, the expression parser should
4494 be used in all cases) */
4495 par_count = 0;
4496 if (tok == '(') {
4497 AttributeDef ad1;
4498 CType type1;
4499 next();
4500 while (tok == '(') {
4501 par_count++;
4502 next();
4504 if (!parse_btype(&type1, &ad1))
4505 expect("cast");
4506 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4507 #if 0
4508 if (!is_assignable_types(type, &type1))
4509 error("invalid type for cast");
4510 #endif
4511 skip(')');
4513 no_oblock = 1;
4514 if (first || tok == '{') {
4515 skip('{');
4516 no_oblock = 0;
4518 s = type->ref;
4519 f = s->next;
4520 array_length = 0;
4521 index = 0;
4522 n = s->c;
4523 while (tok != '}') {
4524 decl_designator(type, sec, c, NULL, &f, size_only);
4525 index = f->c;
4526 if (!size_only && array_length < index) {
4527 init_putz(type, sec, c + array_length,
4528 index - array_length);
4530 index = index + type_size(&f->type, &align1);
4531 if (index > array_length)
4532 array_length = index;
4533 f = f->next;
4534 if (no_oblock && f == NULL)
4535 break;
4536 if (tok == '}')
4537 break;
4538 skip(',');
4540 /* put zeros at the end */
4541 if (!size_only && array_length < n) {
4542 init_putz(type, sec, c + array_length,
4543 n - array_length);
4545 if (!no_oblock)
4546 skip('}');
4547 while (par_count) {
4548 skip(')');
4549 par_count--;
4551 } else if (tok == '{') {
4552 next();
4553 decl_initializer(type, sec, c, first, size_only);
4554 skip('}');
4555 } else if (size_only) {
4556 /* just skip expression */
4557 parlevel = 0;
4558 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4559 tok != -1) {
4560 if (tok == '(')
4561 parlevel++;
4562 else if (tok == ')')
4563 parlevel--;
4564 next();
4566 } else {
4567 /* currently, we always use constant expression for globals
4568 (may change for scripting case) */
4569 expr_type = EXPR_CONST;
4570 if (!sec)
4571 expr_type = EXPR_ANY;
4572 init_putv(type, sec, c, 0, expr_type);
4576 /* parse an initializer for type 't' if 'has_init' is non zero, and
4577 allocate space in local or global data space ('r' is either
4578 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4579 variable 'v' of scope 'scope' is declared before initializers are
4580 parsed. If 'v' is zero, then a reference to the new object is put
4581 in the value stack. If 'has_init' is 2, a special parsing is done
4582 to handle string constants. */
4583 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4584 int has_init, int v, int scope)
4586 int size, align, addr, data_offset;
4587 int level;
4588 ParseState saved_parse_state = {0};
4589 TokenString init_str;
4590 Section *sec;
4592 size = type_size(type, &align);
4593 /* If unknown size, we must evaluate it before
4594 evaluating initializers because
4595 initializers can generate global data too
4596 (e.g. string pointers or ISOC99 compound
4597 literals). It also simplifies local
4598 initializers handling */
4599 tok_str_new(&init_str);
4600 if (size < 0) {
4601 if (!has_init)
4602 error("unknown type size");
4603 /* get all init string */
4604 if (has_init == 2) {
4605 /* only get strings */
4606 while (tok == TOK_STR || tok == TOK_LSTR) {
4607 tok_str_add_tok(&init_str);
4608 next();
4610 } else {
4611 level = 0;
4612 while (level > 0 || (tok != ',' && tok != ';')) {
4613 if (tok < 0)
4614 error("unexpected end of file in initializer");
4615 tok_str_add_tok(&init_str);
4616 if (tok == '{')
4617 level++;
4618 else if (tok == '}') {
4619 level--;
4620 if (level <= 0) {
4621 next();
4622 break;
4625 next();
4628 tok_str_add(&init_str, -1);
4629 tok_str_add(&init_str, 0);
4631 /* compute size */
4632 save_parse_state(&saved_parse_state);
4634 macro_ptr = init_str.str;
4635 next();
4636 decl_initializer(type, NULL, 0, 1, 1);
4637 /* prepare second initializer parsing */
4638 macro_ptr = init_str.str;
4639 next();
4641 /* if still unknown size, error */
4642 size = type_size(type, &align);
4643 if (size < 0)
4644 error("unknown type size");
4646 /* take into account specified alignment if bigger */
4647 if (ad->aligned) {
4648 if (ad->aligned > align)
4649 align = ad->aligned;
4650 } else if (ad->packed) {
4651 align = 1;
4653 if ((r & VT_VALMASK) == VT_LOCAL) {
4654 sec = NULL;
4655 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4656 loc--;
4657 loc = (loc - size) & -align;
4658 addr = loc;
4659 /* handles bounds */
4660 /* XXX: currently, since we do only one pass, we cannot track
4661 '&' operators, so we add only arrays */
4662 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4663 unsigned long *bounds_ptr;
4664 /* add padding between regions */
4665 loc--;
4666 /* then add local bound info */
4667 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4668 bounds_ptr[0] = addr;
4669 bounds_ptr[1] = size;
4671 if (v) {
4672 /* local variable */
4673 sym_push(v, type, r, addr);
4674 } else {
4675 /* push local reference */
4676 vset(type, r, addr);
4678 } else {
4679 Sym *sym;
4681 sym = NULL;
4682 if (v && scope == VT_CONST) {
4683 /* see if the symbol was already defined */
4684 sym = sym_find(v);
4685 if (sym) {
4686 if (!is_compatible_types(&sym->type, type))
4687 error("incompatible types for redefinition of '%s'",
4688 get_tok_str(v, NULL));
4689 if (sym->type.t & VT_EXTERN) {
4690 /* if the variable is extern, it was not allocated */
4691 sym->type.t &= ~VT_EXTERN;
4692 /* set array size if it was ommited in extern
4693 declaration */
4694 if ((sym->type.t & VT_ARRAY) &&
4695 sym->type.ref->c < 0 &&
4696 type->ref->c >= 0)
4697 sym->type.ref->c = type->ref->c;
4698 } else {
4699 /* we accept several definitions of the same
4700 global variable. this is tricky, because we
4701 must play with the SHN_COMMON type of the symbol */
4702 /* XXX: should check if the variable was already
4703 initialized. It is incorrect to initialized it
4704 twice */
4705 /* no init data, we won't add more to the symbol */
4706 if (!has_init)
4707 goto no_alloc;
4712 /* allocate symbol in corresponding section */
4713 sec = ad->section;
4714 if (!sec) {
4715 if (has_init)
4716 sec = data_section;
4717 else if (tcc_state->nocommon)
4718 sec = bss_section;
4720 if (sec) {
4721 data_offset = sec->data_offset;
4722 data_offset = (data_offset + align - 1) & -align;
4723 addr = data_offset;
4724 /* very important to increment global pointer at this time
4725 because initializers themselves can create new initializers */
4726 data_offset += size;
4727 /* add padding if bound check */
4728 if (tcc_state->do_bounds_check)
4729 data_offset++;
4730 sec->data_offset = data_offset;
4731 /* allocate section space to put the data */
4732 if (sec->sh_type != SHT_NOBITS &&
4733 data_offset > sec->data_allocated)
4734 section_realloc(sec, data_offset);
4735 /* align section if needed */
4736 if (align > sec->sh_addralign)
4737 sec->sh_addralign = align;
4738 } else {
4739 addr = 0; /* avoid warning */
4742 if (v) {
4743 if (scope != VT_CONST || !sym) {
4744 sym = sym_push(v, type, r | VT_SYM, 0);
4746 /* update symbol definition */
4747 if (sec) {
4748 put_extern_sym(sym, sec, addr, size);
4749 } else {
4750 ElfW(Sym) *esym;
4751 /* put a common area */
4752 put_extern_sym(sym, NULL, align, size);
4753 /* XXX: find a nicer way */
4754 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
4755 esym->st_shndx = SHN_COMMON;
4757 } else {
4758 CValue cval;
4760 /* push global reference */
4761 sym = get_sym_ref(type, sec, addr, size);
4762 cval.ul = 0;
4763 vsetc(type, VT_CONST | VT_SYM, &cval);
4764 vtop->sym = sym;
4767 /* handles bounds now because the symbol must be defined
4768 before for the relocation */
4769 if (tcc_state->do_bounds_check) {
4770 unsigned long *bounds_ptr;
4772 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
4773 /* then add global bound info */
4774 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
4775 bounds_ptr[0] = 0; /* relocated */
4776 bounds_ptr[1] = size;
4779 if (has_init) {
4780 decl_initializer(type, sec, addr, 1, 0);
4781 /* restore parse state if needed */
4782 if (init_str.str) {
4783 tok_str_free(init_str.str);
4784 restore_parse_state(&saved_parse_state);
4787 no_alloc: ;
4790 void put_func_debug(Sym *sym)
4792 char buf[512];
4794 /* stabs info */
4795 /* XXX: we put here a dummy type */
4796 snprintf(buf, sizeof(buf), "%s:%c1",
4797 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
4798 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
4799 cur_text_section, sym->c);
4800 /* //gr gdb wants a line at the function */
4801 put_stabn(N_SLINE, 0, file->line_num, 0);
4802 last_ind = 0;
4803 last_line_num = 0;
4806 /* parse an old style function declaration list */
4807 /* XXX: check multiple parameter */
4808 static void func_decl_list(Sym *func_sym)
4810 AttributeDef ad;
4811 int v;
4812 Sym *s;
4813 CType btype, type;
4815 /* parse each declaration */
4816 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
4817 if (!parse_btype(&btype, &ad))
4818 expect("declaration list");
4819 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4820 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4821 tok == ';') {
4822 /* we accept no variable after */
4823 } else {
4824 for(;;) {
4825 type = btype;
4826 type_decl(&type, &ad, &v, TYPE_DIRECT);
4827 /* find parameter in function parameter list */
4828 s = func_sym->next;
4829 while (s != NULL) {
4830 if ((s->v & ~SYM_FIELD) == v)
4831 goto found;
4832 s = s->next;
4834 error("declaration for parameter '%s' but no such parameter",
4835 get_tok_str(v, NULL));
4836 found:
4837 /* check that no storage specifier except 'register' was given */
4838 if (type.t & VT_STORAGE)
4839 error("storage class specified for '%s'", get_tok_str(v, NULL));
4840 convert_parameter_type(&type);
4841 /* we can add the type (NOTE: it could be local to the function) */
4842 s->type = type;
4843 /* accept other parameters */
4844 if (tok == ',')
4845 next();
4846 else
4847 break;
4850 skip(';');
4854 /* parse a function defined by symbol 'sym' and generate its code in
4855 'cur_text_section' */
4856 static void gen_function(Sym *sym)
4858 int saved_nocode_wanted = nocode_wanted;
4859 nocode_wanted = 0;
4860 ind = cur_text_section->data_offset;
4861 /* NOTE: we patch the symbol size later */
4862 put_extern_sym(sym, cur_text_section, ind, 0);
4863 funcname = get_tok_str(sym->v, NULL);
4864 func_ind = ind;
4865 /* put debug symbol */
4866 if (tcc_state->do_debug)
4867 put_func_debug(sym);
4868 /* push a dummy symbol to enable local sym storage */
4869 sym_push2(&local_stack, SYM_FIELD, 0, 0);
4870 gfunc_prolog(&sym->type);
4871 rsym = 0;
4872 block(NULL, NULL, NULL, NULL, 0, 0);
4873 gsym(rsym);
4874 gfunc_epilog();
4875 cur_text_section->data_offset = ind;
4876 label_pop(&global_label_stack, NULL);
4877 sym_pop(&local_stack, NULL); /* reset local stack */
4878 /* end of function */
4879 /* patch symbol size */
4880 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
4881 ind - func_ind;
4882 if (tcc_state->do_debug) {
4883 put_stabn(N_FUN, 0, 0, ind - func_ind);
4885 /* It's better to crash than to generate wrong code */
4886 cur_text_section = NULL;
4887 funcname = ""; /* for safety */
4888 func_vt.t = VT_VOID; /* for safety */
4889 ind = 0; /* for safety */
4890 nocode_wanted = saved_nocode_wanted;
4893 static void gen_inline_functions(void)
4895 Sym *sym;
4896 CType *type;
4897 int *str, inline_generated;
4899 /* iterate while inline function are referenced */
4900 for(;;) {
4901 inline_generated = 0;
4902 for(sym = global_stack; sym != NULL; sym = sym->prev) {
4903 type = &sym->type;
4904 if (((type->t & VT_BTYPE) == VT_FUNC) &&
4905 (type->t & (VT_STATIC | VT_INLINE)) ==
4906 (VT_STATIC | VT_INLINE) &&
4907 sym->c != 0) {
4908 /* the function was used: generate its code and
4909 convert it to a normal function */
4910 str = INLINE_DEF(sym->r);
4911 sym->r = VT_SYM | VT_CONST;
4912 sym->type.t &= ~VT_INLINE;
4914 macro_ptr = str;
4915 next();
4916 cur_text_section = text_section;
4917 gen_function(sym);
4918 macro_ptr = NULL; /* fail safe */
4920 tok_str_free(str);
4921 inline_generated = 1;
4924 if (!inline_generated)
4925 break;
4928 /* free all remaining inline function tokens */
4929 for(sym = global_stack; sym != NULL; sym = sym->prev) {
4930 type = &sym->type;
4931 if (((type->t & VT_BTYPE) == VT_FUNC) &&
4932 (type->t & (VT_STATIC | VT_INLINE)) ==
4933 (VT_STATIC | VT_INLINE)) {
4934 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
4935 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
4936 continue;
4937 str = INLINE_DEF(sym->r);
4938 tok_str_free(str);
4939 sym->r = 0; /* fail safe */
4944 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4945 static void decl(int l)
4947 int v, has_init, r;
4948 CType type, btype;
4949 Sym *sym;
4950 AttributeDef ad;
4952 while (1) {
4953 if (!parse_btype(&btype, &ad)) {
4954 /* skip redundant ';' */
4955 /* XXX: find more elegant solution */
4956 if (tok == ';') {
4957 next();
4958 continue;
4960 if (l == VT_CONST &&
4961 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
4962 /* global asm block */
4963 asm_global_instr();
4964 continue;
4966 /* special test for old K&R protos without explicit int
4967 type. Only accepted when defining global data */
4968 if (l == VT_LOCAL || tok < TOK_DEFINE)
4969 break;
4970 btype.t = VT_INT;
4972 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4973 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4974 tok == ';') {
4975 /* we accept no variable after */
4976 next();
4977 continue;
4979 while (1) { /* iterate thru each declaration */
4980 type = btype;
4981 type_decl(&type, &ad, &v, TYPE_DIRECT);
4982 #if 0
4984 char buf[500];
4985 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
4986 printf("type = '%s'\n", buf);
4988 #endif
4989 if ((type.t & VT_BTYPE) == VT_FUNC) {
4990 /* if old style function prototype, we accept a
4991 declaration list */
4992 sym = type.ref;
4993 if (sym->c == FUNC_OLD)
4994 func_decl_list(sym);
4997 if (tok == '{') {
4998 if (l == VT_LOCAL)
4999 error("cannot use local functions");
5000 if ((type.t & VT_BTYPE) != VT_FUNC)
5001 expect("function definition");
5003 /* reject abstract declarators in function definition */
5004 sym = type.ref;
5005 while ((sym = sym->next) != NULL)
5006 if (!(sym->v & ~SYM_FIELD))
5007 expect("identifier");
5009 /* XXX: cannot do better now: convert extern line to static inline */
5010 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5011 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5013 sym = sym_find(v);
5014 if (sym) {
5015 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5016 goto func_error1;
5017 /* specific case: if not func_call defined, we put
5018 the one of the prototype */
5019 /* XXX: should have default value */
5020 r = sym->type.ref->r;
5021 if (FUNC_CALL(r) != FUNC_CDECL
5022 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5023 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5024 if (FUNC_EXPORT(r))
5025 FUNC_EXPORT(type.ref->r) = 1;
5027 if (!is_compatible_types(&sym->type, &type)) {
5028 func_error1:
5029 error("incompatible types for redefinition of '%s'",
5030 get_tok_str(v, NULL));
5032 /* if symbol is already defined, then put complete type */
5033 sym->type = type;
5034 } else {
5035 /* put function symbol */
5036 sym = global_identifier_push(v, type.t, 0);
5037 sym->type.ref = type.ref;
5040 /* static inline functions are just recorded as a kind
5041 of macro. Their code will be emitted at the end of
5042 the compilation unit only if they are used */
5043 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5044 (VT_INLINE | VT_STATIC)) {
5045 TokenString func_str;
5046 int block_level;
5048 tok_str_new(&func_str);
5050 block_level = 0;
5051 for(;;) {
5052 int t;
5053 if (tok == TOK_EOF)
5054 error("unexpected end of file");
5055 tok_str_add_tok(&func_str);
5056 t = tok;
5057 next();
5058 if (t == '{') {
5059 block_level++;
5060 } else if (t == '}') {
5061 block_level--;
5062 if (block_level == 0)
5063 break;
5066 tok_str_add(&func_str, -1);
5067 tok_str_add(&func_str, 0);
5068 INLINE_DEF(sym->r) = func_str.str;
5069 } else {
5070 /* compute text section */
5071 cur_text_section = ad.section;
5072 if (!cur_text_section)
5073 cur_text_section = text_section;
5074 sym->r = VT_SYM | VT_CONST;
5075 gen_function(sym);
5077 break;
5078 } else {
5079 if (btype.t & VT_TYPEDEF) {
5080 /* save typedefed type */
5081 /* XXX: test storage specifiers ? */
5082 sym = sym_push(v, &type, 0, 0);
5083 sym->type.t |= VT_TYPEDEF;
5084 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5085 /* external function definition */
5086 /* specific case for func_call attribute */
5087 if (ad.func_attr)
5088 type.ref->r = ad.func_attr;
5089 external_sym(v, &type, 0);
5090 } else {
5091 /* not lvalue if array */
5092 r = 0;
5093 if (!(type.t & VT_ARRAY))
5094 r |= lvalue_type(type.t);
5095 has_init = (tok == '=');
5096 if ((btype.t & VT_EXTERN) ||
5097 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5098 !has_init && l == VT_CONST && type.ref->c < 0)) {
5099 /* external variable */
5100 /* NOTE: as GCC, uninitialized global static
5101 arrays of null size are considered as
5102 extern */
5103 external_sym(v, &type, r);
5104 } else {
5105 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5106 if (type.t & VT_STATIC)
5107 r |= VT_CONST;
5108 else
5109 r |= l;
5110 if (has_init)
5111 next();
5112 decl_initializer_alloc(&type, &ad, r,
5113 has_init, v, l);
5116 if (tok != ',') {
5117 skip(';');
5118 break;
5120 next();