move parser/generator to tccgen.c
[tinycc.git] / tccgen.c
blob9f1ebf724f827d133aa2a80cddefa8acb3e99203
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 (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 (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 /* field */
3270 if (tok == TOK_ARROW)
3271 indir();
3272 test_lvalue();
3273 gaddrof();
3274 next();
3275 /* expect pointer on structure */
3276 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3277 expect("struct or union");
3278 s = vtop->type.ref;
3279 /* find field */
3280 tok |= SYM_FIELD;
3281 while ((s = s->next) != NULL) {
3282 if (s->v == tok)
3283 break;
3285 if (!s)
3286 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3287 /* add field offset to pointer */
3288 vtop->type = char_pointer_type; /* change type to 'char *' */
3289 vpushi(s->c);
3290 gen_op('+');
3291 /* change type to field type, and set to lvalue */
3292 vtop->type = s->type;
3293 /* an array is never an lvalue */
3294 if (!(vtop->type.t & VT_ARRAY)) {
3295 vtop->r |= lvalue_type(vtop->type.t);
3296 /* if bound checking, the referenced pointer must be checked */
3297 if (do_bounds_check)
3298 vtop->r |= VT_MUSTBOUND;
3300 next();
3301 } else if (tok == '[') {
3302 next();
3303 gexpr();
3304 gen_op('+');
3305 indir();
3306 skip(']');
3307 } else if (tok == '(') {
3308 SValue ret;
3309 Sym *sa;
3310 int nb_args;
3312 /* function call */
3313 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3314 /* pointer test (no array accepted) */
3315 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3316 vtop->type = *pointed_type(&vtop->type);
3317 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3318 goto error_func;
3319 } else {
3320 error_func:
3321 expect("function pointer");
3323 } else {
3324 vtop->r &= ~VT_LVAL; /* no lvalue */
3326 /* get return type */
3327 s = vtop->type.ref;
3328 next();
3329 sa = s->next; /* first parameter */
3330 nb_args = 0;
3331 ret.r2 = VT_CONST;
3332 /* compute first implicit argument if a structure is returned */
3333 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3334 /* get some space for the returned structure */
3335 size = type_size(&s->type, &align);
3336 loc = (loc - size) & -align;
3337 ret.type = s->type;
3338 ret.r = VT_LOCAL | VT_LVAL;
3339 /* pass it as 'int' to avoid structure arg passing
3340 problems */
3341 vseti(VT_LOCAL, loc);
3342 ret.c = vtop->c;
3343 nb_args++;
3344 } else {
3345 ret.type = s->type;
3346 /* return in register */
3347 if (is_float(ret.type.t)) {
3348 ret.r = reg_fret(ret.type.t);
3349 } else {
3350 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3351 ret.r2 = REG_LRET;
3352 ret.r = REG_IRET;
3354 ret.c.i = 0;
3356 if (tok != ')') {
3357 for(;;) {
3358 expr_eq();
3359 gfunc_param_typed(s, sa);
3360 nb_args++;
3361 if (sa)
3362 sa = sa->next;
3363 if (tok == ')')
3364 break;
3365 skip(',');
3368 if (sa)
3369 error("too few arguments to function");
3370 skip(')');
3371 if (!nocode_wanted) {
3372 gfunc_call(nb_args);
3373 } else {
3374 vtop -= (nb_args + 1);
3376 /* return value */
3377 vsetc(&ret.type, ret.r, &ret.c);
3378 vtop->r2 = ret.r2;
3379 } else {
3380 break;
3385 static void uneq(void)
3387 int t;
3389 unary();
3390 if (tok == '=' ||
3391 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3392 tok == TOK_A_XOR || tok == TOK_A_OR ||
3393 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3394 test_lvalue();
3395 t = tok;
3396 next();
3397 if (t == '=') {
3398 expr_eq();
3399 } else {
3400 vdup();
3401 expr_eq();
3402 gen_op(t & 0x7f);
3404 vstore();
3408 static void expr_prod(void)
3410 int t;
3412 uneq();
3413 while (tok == '*' || tok == '/' || tok == '%') {
3414 t = tok;
3415 next();
3416 uneq();
3417 gen_op(t);
3421 static void expr_sum(void)
3423 int t;
3425 expr_prod();
3426 while (tok == '+' || tok == '-') {
3427 t = tok;
3428 next();
3429 expr_prod();
3430 gen_op(t);
3434 static void expr_shift(void)
3436 int t;
3438 expr_sum();
3439 while (tok == TOK_SHL || tok == TOK_SAR) {
3440 t = tok;
3441 next();
3442 expr_sum();
3443 gen_op(t);
3447 static void expr_cmp(void)
3449 int t;
3451 expr_shift();
3452 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3453 tok == TOK_ULT || tok == TOK_UGE) {
3454 t = tok;
3455 next();
3456 expr_shift();
3457 gen_op(t);
3461 static void expr_cmpeq(void)
3463 int t;
3465 expr_cmp();
3466 while (tok == TOK_EQ || tok == TOK_NE) {
3467 t = tok;
3468 next();
3469 expr_cmp();
3470 gen_op(t);
3474 static void expr_and(void)
3476 expr_cmpeq();
3477 while (tok == '&') {
3478 next();
3479 expr_cmpeq();
3480 gen_op('&');
3484 static void expr_xor(void)
3486 expr_and();
3487 while (tok == '^') {
3488 next();
3489 expr_and();
3490 gen_op('^');
3494 static void expr_or(void)
3496 expr_xor();
3497 while (tok == '|') {
3498 next();
3499 expr_xor();
3500 gen_op('|');
3504 /* XXX: fix this mess */
3505 static void expr_land_const(void)
3507 expr_or();
3508 while (tok == TOK_LAND) {
3509 next();
3510 expr_or();
3511 gen_op(TOK_LAND);
3515 /* XXX: fix this mess */
3516 static void expr_lor_const(void)
3518 expr_land_const();
3519 while (tok == TOK_LOR) {
3520 next();
3521 expr_land_const();
3522 gen_op(TOK_LOR);
3526 /* only used if non constant */
3527 static void expr_land(void)
3529 int t;
3531 expr_or();
3532 if (tok == TOK_LAND) {
3533 t = 0;
3534 save_regs(1);
3535 for(;;) {
3536 t = gtst(1, t);
3537 if (tok != TOK_LAND) {
3538 vseti(VT_JMPI, t);
3539 break;
3541 next();
3542 expr_or();
3547 static void expr_lor(void)
3549 int t;
3551 expr_land();
3552 if (tok == TOK_LOR) {
3553 t = 0;
3554 save_regs(1);
3555 for(;;) {
3556 t = gtst(0, t);
3557 if (tok != TOK_LOR) {
3558 vseti(VT_JMP, t);
3559 break;
3561 next();
3562 expr_land();
3567 /* XXX: better constant handling */
3568 static void expr_eq(void)
3570 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3571 SValue sv;
3572 CType type, type1, type2;
3574 if (const_wanted) {
3575 expr_lor_const();
3576 if (tok == '?') {
3577 CType boolean;
3578 int c;
3579 boolean.t = VT_BOOL;
3580 vdup();
3581 gen_cast(&boolean);
3582 c = vtop->c.i;
3583 vpop();
3584 next();
3585 if (tok != ':' || !gnu_ext) {
3586 vpop();
3587 gexpr();
3589 if (!c)
3590 vpop();
3591 skip(':');
3592 expr_eq();
3593 if (c)
3594 vpop();
3596 } else {
3597 expr_lor();
3598 if (tok == '?') {
3599 next();
3600 if (vtop != vstack) {
3601 /* needed to avoid having different registers saved in
3602 each branch */
3603 if (is_float(vtop->type.t)) {
3604 rc = RC_FLOAT;
3605 #ifdef TCC_TARGET_X86_64
3606 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3607 rc = RC_ST0;
3609 #endif
3611 else
3612 rc = RC_INT;
3613 gv(rc);
3614 save_regs(1);
3616 if (tok == ':' && gnu_ext) {
3617 gv_dup();
3618 tt = gtst(1, 0);
3619 } else {
3620 tt = gtst(1, 0);
3621 gexpr();
3623 type1 = vtop->type;
3624 sv = *vtop; /* save value to handle it later */
3625 vtop--; /* no vpop so that FP stack is not flushed */
3626 skip(':');
3627 u = gjmp(0);
3628 gsym(tt);
3629 expr_eq();
3630 type2 = vtop->type;
3632 t1 = type1.t;
3633 bt1 = t1 & VT_BTYPE;
3634 t2 = type2.t;
3635 bt2 = t2 & VT_BTYPE;
3636 /* cast operands to correct type according to ISOC rules */
3637 if (is_float(bt1) || is_float(bt2)) {
3638 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3639 type.t = VT_LDOUBLE;
3640 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3641 type.t = VT_DOUBLE;
3642 } else {
3643 type.t = VT_FLOAT;
3645 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3646 /* cast to biggest op */
3647 type.t = VT_LLONG;
3648 /* convert to unsigned if it does not fit in a long long */
3649 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3650 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3651 type.t |= VT_UNSIGNED;
3652 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3653 /* XXX: test pointer compatibility */
3654 type = type1;
3655 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3656 /* XXX: test function pointer compatibility */
3657 type = type1;
3658 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3659 /* XXX: test structure compatibility */
3660 type = type1;
3661 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3662 /* NOTE: as an extension, we accept void on only one side */
3663 type.t = VT_VOID;
3664 } else {
3665 /* integer operations */
3666 type.t = VT_INT;
3667 /* convert to unsigned if it does not fit in an integer */
3668 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3669 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3670 type.t |= VT_UNSIGNED;
3673 /* now we convert second operand */
3674 gen_cast(&type);
3675 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3676 gaddrof();
3677 rc = RC_INT;
3678 if (is_float(type.t)) {
3679 rc = RC_FLOAT;
3680 #ifdef TCC_TARGET_X86_64
3681 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3682 rc = RC_ST0;
3684 #endif
3685 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3686 /* for long longs, we use fixed registers to avoid having
3687 to handle a complicated move */
3688 rc = RC_IRET;
3691 r2 = gv(rc);
3692 /* this is horrible, but we must also convert first
3693 operand */
3694 tt = gjmp(0);
3695 gsym(u);
3696 /* put again first value and cast it */
3697 *vtop = sv;
3698 gen_cast(&type);
3699 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3700 gaddrof();
3701 r1 = gv(rc);
3702 move_reg(r2, r1);
3703 vtop->r = r2;
3704 gsym(tt);
3709 static void gexpr(void)
3711 while (1) {
3712 expr_eq();
3713 if (tok != ',')
3714 break;
3715 vpop();
3716 next();
3720 /* parse an expression and return its type without any side effect. */
3721 static void expr_type(CType *type)
3723 int saved_nocode_wanted;
3725 saved_nocode_wanted = nocode_wanted;
3726 nocode_wanted = 1;
3727 gexpr();
3728 *type = vtop->type;
3729 vpop();
3730 nocode_wanted = saved_nocode_wanted;
3733 /* parse a unary expression and return its type without any side
3734 effect. */
3735 static void unary_type(CType *type)
3737 int a;
3739 a = nocode_wanted;
3740 nocode_wanted = 1;
3741 unary();
3742 *type = vtop->type;
3743 vpop();
3744 nocode_wanted = a;
3747 /* parse a constant expression and return value in vtop. */
3748 static void expr_const1(void)
3750 int a;
3751 a = const_wanted;
3752 const_wanted = 1;
3753 expr_eq();
3754 const_wanted = a;
3757 /* parse an integer constant and return its value. */
3758 static int expr_const(void)
3760 int c;
3761 expr_const1();
3762 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
3763 expect("constant expression");
3764 c = vtop->c.i;
3765 vpop();
3766 return c;
3769 /* return the label token if current token is a label, otherwise
3770 return zero */
3771 static int is_label(void)
3773 int last_tok;
3775 /* fast test first */
3776 if (tok < TOK_UIDENT)
3777 return 0;
3778 /* no need to save tokc because tok is an identifier */
3779 last_tok = tok;
3780 next();
3781 if (tok == ':') {
3782 next();
3783 return last_tok;
3784 } else {
3785 unget_tok(last_tok);
3786 return 0;
3790 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
3791 int case_reg, int is_expr)
3793 int a, b, c, d;
3794 Sym *s;
3796 /* generate line number info */
3797 if (do_debug &&
3798 (last_line_num != file->line_num || last_ind != ind)) {
3799 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
3800 last_ind = ind;
3801 last_line_num = file->line_num;
3804 if (is_expr) {
3805 /* default return value is (void) */
3806 vpushi(0);
3807 vtop->type.t = VT_VOID;
3810 if (tok == TOK_IF) {
3811 /* if test */
3812 next();
3813 skip('(');
3814 gexpr();
3815 skip(')');
3816 a = gtst(1, 0);
3817 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3818 c = tok;
3819 if (c == TOK_ELSE) {
3820 next();
3821 d = gjmp(0);
3822 gsym(a);
3823 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3824 gsym(d); /* patch else jmp */
3825 } else
3826 gsym(a);
3827 } else if (tok == TOK_WHILE) {
3828 next();
3829 d = ind;
3830 skip('(');
3831 gexpr();
3832 skip(')');
3833 a = gtst(1, 0);
3834 b = 0;
3835 block(&a, &b, case_sym, def_sym, case_reg, 0);
3836 gjmp_addr(d);
3837 gsym(a);
3838 gsym_addr(b, d);
3839 } else if (tok == '{') {
3840 Sym *llabel;
3842 next();
3843 /* record local declaration stack position */
3844 s = local_stack;
3845 llabel = local_label_stack;
3846 /* handle local labels declarations */
3847 if (tok == TOK_LABEL) {
3848 next();
3849 for(;;) {
3850 if (tok < TOK_UIDENT)
3851 expect("label identifier");
3852 label_push(&local_label_stack, tok, LABEL_DECLARED);
3853 next();
3854 if (tok == ',') {
3855 next();
3856 } else {
3857 skip(';');
3858 break;
3862 while (tok != '}') {
3863 decl(VT_LOCAL);
3864 if (tok != '}') {
3865 if (is_expr)
3866 vpop();
3867 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
3870 /* pop locally defined labels */
3871 label_pop(&local_label_stack, llabel);
3872 /* pop locally defined symbols */
3873 if(is_expr) {
3874 /* XXX: this solution makes only valgrind happy...
3875 triggered by gcc.c-torture/execute/20000917-1.c */
3876 Sym *p;
3877 switch(vtop->type.t & VT_BTYPE) {
3878 case VT_PTR:
3879 case VT_STRUCT:
3880 case VT_ENUM:
3881 case VT_FUNC:
3882 for(p=vtop->type.ref;p;p=p->prev)
3883 if(p->prev==s)
3884 error("unsupported expression type");
3887 sym_pop(&local_stack, s);
3888 next();
3889 } else if (tok == TOK_RETURN) {
3890 next();
3891 if (tok != ';') {
3892 gexpr();
3893 gen_assign_cast(&func_vt);
3894 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
3895 CType type;
3896 /* if returning structure, must copy it to implicit
3897 first pointer arg location */
3898 #ifdef TCC_ARM_EABI
3899 int align, size;
3900 size = type_size(&func_vt,&align);
3901 if(size <= 4)
3903 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
3904 && (align & 3))
3906 int addr;
3907 loc = (loc - size) & -4;
3908 addr = loc;
3909 type = func_vt;
3910 vset(&type, VT_LOCAL | VT_LVAL, addr);
3911 vswap();
3912 vstore();
3913 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
3915 vtop->type = int_type;
3916 gv(RC_IRET);
3917 } else {
3918 #endif
3919 type = func_vt;
3920 mk_pointer(&type);
3921 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
3922 indir();
3923 vswap();
3924 /* copy structure value to pointer */
3925 vstore();
3926 #ifdef TCC_ARM_EABI
3928 #endif
3929 } else if (is_float(func_vt.t)) {
3930 gv(rc_fret(func_vt.t));
3931 } else {
3932 gv(RC_IRET);
3934 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3936 skip(';');
3937 rsym = gjmp(rsym); /* jmp */
3938 } else if (tok == TOK_BREAK) {
3939 /* compute jump */
3940 if (!bsym)
3941 error("cannot break");
3942 *bsym = gjmp(*bsym);
3943 next();
3944 skip(';');
3945 } else if (tok == TOK_CONTINUE) {
3946 /* compute jump */
3947 if (!csym)
3948 error("cannot continue");
3949 *csym = gjmp(*csym);
3950 next();
3951 skip(';');
3952 } else if (tok == TOK_FOR) {
3953 int e;
3954 next();
3955 skip('(');
3956 if (tok != ';') {
3957 gexpr();
3958 vpop();
3960 skip(';');
3961 d = ind;
3962 c = ind;
3963 a = 0;
3964 b = 0;
3965 if (tok != ';') {
3966 gexpr();
3967 a = gtst(1, 0);
3969 skip(';');
3970 if (tok != ')') {
3971 e = gjmp(0);
3972 c = ind;
3973 gexpr();
3974 vpop();
3975 gjmp_addr(d);
3976 gsym(e);
3978 skip(')');
3979 block(&a, &b, case_sym, def_sym, case_reg, 0);
3980 gjmp_addr(c);
3981 gsym(a);
3982 gsym_addr(b, c);
3983 } else
3984 if (tok == TOK_DO) {
3985 next();
3986 a = 0;
3987 b = 0;
3988 d = ind;
3989 block(&a, &b, case_sym, def_sym, case_reg, 0);
3990 skip(TOK_WHILE);
3991 skip('(');
3992 gsym(b);
3993 gexpr();
3994 c = gtst(0, 0);
3995 gsym_addr(c, d);
3996 skip(')');
3997 gsym(a);
3998 skip(';');
3999 } else
4000 if (tok == TOK_SWITCH) {
4001 next();
4002 skip('(');
4003 gexpr();
4004 /* XXX: other types than integer */
4005 case_reg = gv(RC_INT);
4006 vpop();
4007 skip(')');
4008 a = 0;
4009 b = gjmp(0); /* jump to first case */
4010 c = 0;
4011 block(&a, csym, &b, &c, case_reg, 0);
4012 /* if no default, jmp after switch */
4013 if (c == 0)
4014 c = ind;
4015 /* default label */
4016 gsym_addr(b, c);
4017 /* break label */
4018 gsym(a);
4019 } else
4020 if (tok == TOK_CASE) {
4021 int v1, v2;
4022 if (!case_sym)
4023 expect("switch");
4024 next();
4025 v1 = expr_const();
4026 v2 = v1;
4027 if (gnu_ext && tok == TOK_DOTS) {
4028 next();
4029 v2 = expr_const();
4030 if (v2 < v1)
4031 warning("empty case range");
4033 /* since a case is like a label, we must skip it with a jmp */
4034 b = gjmp(0);
4035 gsym(*case_sym);
4036 vseti(case_reg, 0);
4037 vpushi(v1);
4038 if (v1 == v2) {
4039 gen_op(TOK_EQ);
4040 *case_sym = gtst(1, 0);
4041 } else {
4042 gen_op(TOK_GE);
4043 *case_sym = gtst(1, 0);
4044 vseti(case_reg, 0);
4045 vpushi(v2);
4046 gen_op(TOK_LE);
4047 *case_sym = gtst(1, *case_sym);
4049 gsym(b);
4050 skip(':');
4051 is_expr = 0;
4052 goto block_after_label;
4053 } else
4054 if (tok == TOK_DEFAULT) {
4055 next();
4056 skip(':');
4057 if (!def_sym)
4058 expect("switch");
4059 if (*def_sym)
4060 error("too many 'default'");
4061 *def_sym = ind;
4062 is_expr = 0;
4063 goto block_after_label;
4064 } else
4065 if (tok == TOK_GOTO) {
4066 next();
4067 if (tok == '*' && gnu_ext) {
4068 /* computed goto */
4069 next();
4070 gexpr();
4071 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4072 expect("pointer");
4073 ggoto();
4074 } else if (tok >= TOK_UIDENT) {
4075 s = label_find(tok);
4076 /* put forward definition if needed */
4077 if (!s) {
4078 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4079 } else {
4080 if (s->r == LABEL_DECLARED)
4081 s->r = LABEL_FORWARD;
4083 /* label already defined */
4084 if (s->r & LABEL_FORWARD)
4085 s->next = (void *)gjmp((long)s->next);
4086 else
4087 gjmp_addr((long)s->next);
4088 next();
4089 } else {
4090 expect("label identifier");
4092 skip(';');
4093 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4094 asm_instr();
4095 } else {
4096 b = is_label();
4097 if (b) {
4098 /* label case */
4099 s = label_find(b);
4100 if (s) {
4101 if (s->r == LABEL_DEFINED)
4102 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4103 gsym((long)s->next);
4104 s->r = LABEL_DEFINED;
4105 } else {
4106 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4108 s->next = (void *)ind;
4109 /* we accept this, but it is a mistake */
4110 block_after_label:
4111 if (tok == '}') {
4112 warning("deprecated use of label at end of compound statement");
4113 } else {
4114 if (is_expr)
4115 vpop();
4116 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4118 } else {
4119 /* expression case */
4120 if (tok != ';') {
4121 if (is_expr) {
4122 vpop();
4123 gexpr();
4124 } else {
4125 gexpr();
4126 vpop();
4129 skip(';');
4134 /* t is the array or struct type. c is the array or struct
4135 address. cur_index/cur_field is the pointer to the current
4136 value. 'size_only' is true if only size info is needed (only used
4137 in arrays) */
4138 static void decl_designator(CType *type, Section *sec, unsigned long c,
4139 int *cur_index, Sym **cur_field,
4140 int size_only)
4142 Sym *s, *f;
4143 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4144 CType type1;
4146 notfirst = 0;
4147 elem_size = 0;
4148 nb_elems = 1;
4149 if (gnu_ext && (l = is_label()) != 0)
4150 goto struct_field;
4151 while (tok == '[' || tok == '.') {
4152 if (tok == '[') {
4153 if (!(type->t & VT_ARRAY))
4154 expect("array type");
4155 s = type->ref;
4156 next();
4157 index = expr_const();
4158 if (index < 0 || (s->c >= 0 && index >= s->c))
4159 expect("invalid index");
4160 if (tok == TOK_DOTS && gnu_ext) {
4161 next();
4162 index_last = expr_const();
4163 if (index_last < 0 ||
4164 (s->c >= 0 && index_last >= s->c) ||
4165 index_last < index)
4166 expect("invalid index");
4167 } else {
4168 index_last = index;
4170 skip(']');
4171 if (!notfirst)
4172 *cur_index = index_last;
4173 type = pointed_type(type);
4174 elem_size = type_size(type, &align);
4175 c += index * elem_size;
4176 /* NOTE: we only support ranges for last designator */
4177 nb_elems = index_last - index + 1;
4178 if (nb_elems != 1) {
4179 notfirst = 1;
4180 break;
4182 } else {
4183 next();
4184 l = tok;
4185 next();
4186 struct_field:
4187 if ((type->t & VT_BTYPE) != VT_STRUCT)
4188 expect("struct/union type");
4189 s = type->ref;
4190 l |= SYM_FIELD;
4191 f = s->next;
4192 while (f) {
4193 if (f->v == l)
4194 break;
4195 f = f->next;
4197 if (!f)
4198 expect("field");
4199 if (!notfirst)
4200 *cur_field = f;
4201 /* XXX: fix this mess by using explicit storage field */
4202 type1 = f->type;
4203 type1.t |= (type->t & ~VT_TYPE);
4204 type = &type1;
4205 c += f->c;
4207 notfirst = 1;
4209 if (notfirst) {
4210 if (tok == '=') {
4211 next();
4212 } else {
4213 if (!gnu_ext)
4214 expect("=");
4216 } else {
4217 if (type->t & VT_ARRAY) {
4218 index = *cur_index;
4219 type = pointed_type(type);
4220 c += index * type_size(type, &align);
4221 } else {
4222 f = *cur_field;
4223 if (!f)
4224 error("too many field init");
4225 /* XXX: fix this mess by using explicit storage field */
4226 type1 = f->type;
4227 type1.t |= (type->t & ~VT_TYPE);
4228 type = &type1;
4229 c += f->c;
4232 decl_initializer(type, sec, c, 0, size_only);
4234 /* XXX: make it more general */
4235 if (!size_only && nb_elems > 1) {
4236 unsigned long c_end;
4237 uint8_t *src, *dst;
4238 int i;
4240 if (!sec)
4241 error("range init not supported yet for dynamic storage");
4242 c_end = c + nb_elems * elem_size;
4243 if (c_end > sec->data_allocated)
4244 section_realloc(sec, c_end);
4245 src = sec->data + c;
4246 dst = src;
4247 for(i = 1; i < nb_elems; i++) {
4248 dst += elem_size;
4249 memcpy(dst, src, elem_size);
4254 #define EXPR_VAL 0
4255 #define EXPR_CONST 1
4256 #define EXPR_ANY 2
4258 /* store a value or an expression directly in global data or in local array */
4259 static void init_putv(CType *type, Section *sec, unsigned long c,
4260 int v, int expr_type)
4262 int saved_global_expr, bt, bit_pos, bit_size;
4263 void *ptr;
4264 unsigned long long bit_mask;
4265 CType dtype;
4267 switch(expr_type) {
4268 case EXPR_VAL:
4269 vpushi(v);
4270 break;
4271 case EXPR_CONST:
4272 /* compound literals must be allocated globally in this case */
4273 saved_global_expr = global_expr;
4274 global_expr = 1;
4275 expr_const1();
4276 global_expr = saved_global_expr;
4277 /* NOTE: symbols are accepted */
4278 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4279 error("initializer element is not constant");
4280 break;
4281 case EXPR_ANY:
4282 expr_eq();
4283 break;
4286 dtype = *type;
4287 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4289 if (sec) {
4290 /* XXX: not portable */
4291 /* XXX: generate error if incorrect relocation */
4292 gen_assign_cast(&dtype);
4293 bt = type->t & VT_BTYPE;
4294 /* we'll write at most 12 bytes */
4295 if (c + 12 > sec->data_allocated) {
4296 section_realloc(sec, c + 12);
4298 ptr = sec->data + c;
4299 /* XXX: make code faster ? */
4300 if (!(type->t & VT_BITFIELD)) {
4301 bit_pos = 0;
4302 bit_size = 32;
4303 bit_mask = -1LL;
4304 } else {
4305 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4306 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4307 bit_mask = (1LL << bit_size) - 1;
4309 if ((vtop->r & VT_SYM) &&
4310 (bt == VT_BYTE ||
4311 bt == VT_SHORT ||
4312 bt == VT_DOUBLE ||
4313 bt == VT_LDOUBLE ||
4314 bt == VT_LLONG ||
4315 (bt == VT_INT && bit_size != 32)))
4316 error("initializer element is not computable at load time");
4317 switch(bt) {
4318 case VT_BOOL:
4319 vtop->c.i = (vtop->c.i != 0);
4320 case VT_BYTE:
4321 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4322 break;
4323 case VT_SHORT:
4324 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4325 break;
4326 case VT_DOUBLE:
4327 *(double *)ptr = vtop->c.d;
4328 break;
4329 case VT_LDOUBLE:
4330 *(long double *)ptr = vtop->c.ld;
4331 break;
4332 case VT_LLONG:
4333 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4334 break;
4335 default:
4336 if (vtop->r & VT_SYM) {
4337 greloc(sec, vtop->sym, c, R_DATA_32);
4339 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4340 break;
4342 vtop--;
4343 } else {
4344 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4345 vswap();
4346 vstore();
4347 vpop();
4351 /* put zeros for variable based init */
4352 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4354 if (sec) {
4355 /* nothing to do because globals are already set to zero */
4356 } else {
4357 vpush_global_sym(&func_old_type, TOK_memset);
4358 vseti(VT_LOCAL, c);
4359 vpushi(0);
4360 vpushi(size);
4361 gfunc_call(3);
4365 /* 't' contains the type and storage info. 'c' is the offset of the
4366 object in section 'sec'. If 'sec' is NULL, it means stack based
4367 allocation. 'first' is true if array '{' must be read (multi
4368 dimension implicit array init handling). 'size_only' is true if
4369 size only evaluation is wanted (only for arrays). */
4370 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4371 int first, int size_only)
4373 int index, array_length, n, no_oblock, nb, parlevel, i;
4374 int size1, align1, expr_type;
4375 Sym *s, *f;
4376 CType *t1;
4378 if (type->t & VT_ARRAY) {
4379 s = type->ref;
4380 n = s->c;
4381 array_length = 0;
4382 t1 = pointed_type(type);
4383 size1 = type_size(t1, &align1);
4385 no_oblock = 1;
4386 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4387 tok == '{') {
4388 skip('{');
4389 no_oblock = 0;
4392 /* only parse strings here if correct type (otherwise: handle
4393 them as ((w)char *) expressions */
4394 if ((tok == TOK_LSTR &&
4395 #ifdef TCC_TARGET_PE
4396 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4397 #else
4398 (t1->t & VT_BTYPE) == VT_INT
4399 #endif
4400 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4401 while (tok == TOK_STR || tok == TOK_LSTR) {
4402 int cstr_len, ch;
4403 CString *cstr;
4405 cstr = tokc.cstr;
4406 /* compute maximum number of chars wanted */
4407 if (tok == TOK_STR)
4408 cstr_len = cstr->size;
4409 else
4410 cstr_len = cstr->size / sizeof(nwchar_t);
4411 cstr_len--;
4412 nb = cstr_len;
4413 if (n >= 0 && nb > (n - array_length))
4414 nb = n - array_length;
4415 if (!size_only) {
4416 if (cstr_len > nb)
4417 warning("initializer-string for array is too long");
4418 /* in order to go faster for common case (char
4419 string in global variable, we handle it
4420 specifically */
4421 if (sec && tok == TOK_STR && size1 == 1) {
4422 memcpy(sec->data + c + array_length, cstr->data, nb);
4423 } else {
4424 for(i=0;i<nb;i++) {
4425 if (tok == TOK_STR)
4426 ch = ((unsigned char *)cstr->data)[i];
4427 else
4428 ch = ((nwchar_t *)cstr->data)[i];
4429 init_putv(t1, sec, c + (array_length + i) * size1,
4430 ch, EXPR_VAL);
4434 array_length += nb;
4435 next();
4437 /* only add trailing zero if enough storage (no
4438 warning in this case since it is standard) */
4439 if (n < 0 || array_length < n) {
4440 if (!size_only) {
4441 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4443 array_length++;
4445 } else {
4446 index = 0;
4447 while (tok != '}') {
4448 decl_designator(type, sec, c, &index, NULL, size_only);
4449 if (n >= 0 && index >= n)
4450 error("index too large");
4451 /* must put zero in holes (note that doing it that way
4452 ensures that it even works with designators) */
4453 if (!size_only && array_length < index) {
4454 init_putz(t1, sec, c + array_length * size1,
4455 (index - array_length) * size1);
4457 index++;
4458 if (index > array_length)
4459 array_length = index;
4460 /* special test for multi dimensional arrays (may not
4461 be strictly correct if designators are used at the
4462 same time) */
4463 if (index >= n && no_oblock)
4464 break;
4465 if (tok == '}')
4466 break;
4467 skip(',');
4470 if (!no_oblock)
4471 skip('}');
4472 /* put zeros at the end */
4473 if (!size_only && n >= 0 && array_length < n) {
4474 init_putz(t1, sec, c + array_length * size1,
4475 (n - array_length) * size1);
4477 /* patch type size if needed */
4478 if (n < 0)
4479 s->c = array_length;
4480 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4481 (sec || !first || tok == '{')) {
4482 int par_count;
4484 /* NOTE: the previous test is a specific case for automatic
4485 struct/union init */
4486 /* XXX: union needs only one init */
4488 /* XXX: this test is incorrect for local initializers
4489 beginning with ( without {. It would be much more difficult
4490 to do it correctly (ideally, the expression parser should
4491 be used in all cases) */
4492 par_count = 0;
4493 if (tok == '(') {
4494 AttributeDef ad1;
4495 CType type1;
4496 next();
4497 while (tok == '(') {
4498 par_count++;
4499 next();
4501 if (!parse_btype(&type1, &ad1))
4502 expect("cast");
4503 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4504 #if 0
4505 if (!is_assignable_types(type, &type1))
4506 error("invalid type for cast");
4507 #endif
4508 skip(')');
4510 no_oblock = 1;
4511 if (first || tok == '{') {
4512 skip('{');
4513 no_oblock = 0;
4515 s = type->ref;
4516 f = s->next;
4517 array_length = 0;
4518 index = 0;
4519 n = s->c;
4520 while (tok != '}') {
4521 decl_designator(type, sec, c, NULL, &f, size_only);
4522 index = f->c;
4523 if (!size_only && array_length < index) {
4524 init_putz(type, sec, c + array_length,
4525 index - array_length);
4527 index = index + type_size(&f->type, &align1);
4528 if (index > array_length)
4529 array_length = index;
4530 f = f->next;
4531 if (no_oblock && f == NULL)
4532 break;
4533 if (tok == '}')
4534 break;
4535 skip(',');
4537 /* put zeros at the end */
4538 if (!size_only && array_length < n) {
4539 init_putz(type, sec, c + array_length,
4540 n - array_length);
4542 if (!no_oblock)
4543 skip('}');
4544 while (par_count) {
4545 skip(')');
4546 par_count--;
4548 } else if (tok == '{') {
4549 next();
4550 decl_initializer(type, sec, c, first, size_only);
4551 skip('}');
4552 } else if (size_only) {
4553 /* just skip expression */
4554 parlevel = 0;
4555 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4556 tok != -1) {
4557 if (tok == '(')
4558 parlevel++;
4559 else if (tok == ')')
4560 parlevel--;
4561 next();
4563 } else {
4564 /* currently, we always use constant expression for globals
4565 (may change for scripting case) */
4566 expr_type = EXPR_CONST;
4567 if (!sec)
4568 expr_type = EXPR_ANY;
4569 init_putv(type, sec, c, 0, expr_type);
4573 /* parse an initializer for type 't' if 'has_init' is non zero, and
4574 allocate space in local or global data space ('r' is either
4575 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4576 variable 'v' of scope 'scope' is declared before initializers are
4577 parsed. If 'v' is zero, then a reference to the new object is put
4578 in the value stack. If 'has_init' is 2, a special parsing is done
4579 to handle string constants. */
4580 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4581 int has_init, int v, int scope)
4583 int size, align, addr, data_offset;
4584 int level;
4585 ParseState saved_parse_state = {0};
4586 TokenString init_str;
4587 Section *sec;
4589 size = type_size(type, &align);
4590 /* If unknown size, we must evaluate it before
4591 evaluating initializers because
4592 initializers can generate global data too
4593 (e.g. string pointers or ISOC99 compound
4594 literals). It also simplifies local
4595 initializers handling */
4596 tok_str_new(&init_str);
4597 if (size < 0) {
4598 if (!has_init)
4599 error("unknown type size");
4600 /* get all init string */
4601 if (has_init == 2) {
4602 /* only get strings */
4603 while (tok == TOK_STR || tok == TOK_LSTR) {
4604 tok_str_add_tok(&init_str);
4605 next();
4607 } else {
4608 level = 0;
4609 while (level > 0 || (tok != ',' && tok != ';')) {
4610 if (tok < 0)
4611 error("unexpected end of file in initializer");
4612 tok_str_add_tok(&init_str);
4613 if (tok == '{')
4614 level++;
4615 else if (tok == '}') {
4616 level--;
4617 if (level <= 0) {
4618 next();
4619 break;
4622 next();
4625 tok_str_add(&init_str, -1);
4626 tok_str_add(&init_str, 0);
4628 /* compute size */
4629 save_parse_state(&saved_parse_state);
4631 macro_ptr = init_str.str;
4632 next();
4633 decl_initializer(type, NULL, 0, 1, 1);
4634 /* prepare second initializer parsing */
4635 macro_ptr = init_str.str;
4636 next();
4638 /* if still unknown size, error */
4639 size = type_size(type, &align);
4640 if (size < 0)
4641 error("unknown type size");
4643 /* take into account specified alignment if bigger */
4644 if (ad->aligned) {
4645 if (ad->aligned > align)
4646 align = ad->aligned;
4647 } else if (ad->packed) {
4648 align = 1;
4650 if ((r & VT_VALMASK) == VT_LOCAL) {
4651 sec = NULL;
4652 if (do_bounds_check && (type->t & VT_ARRAY))
4653 loc--;
4654 loc = (loc - size) & -align;
4655 addr = loc;
4656 /* handles bounds */
4657 /* XXX: currently, since we do only one pass, we cannot track
4658 '&' operators, so we add only arrays */
4659 if (do_bounds_check && (type->t & VT_ARRAY)) {
4660 unsigned long *bounds_ptr;
4661 /* add padding between regions */
4662 loc--;
4663 /* then add local bound info */
4664 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4665 bounds_ptr[0] = addr;
4666 bounds_ptr[1] = size;
4668 if (v) {
4669 /* local variable */
4670 sym_push(v, type, r, addr);
4671 } else {
4672 /* push local reference */
4673 vset(type, r, addr);
4675 } else {
4676 Sym *sym;
4678 sym = NULL;
4679 if (v && scope == VT_CONST) {
4680 /* see if the symbol was already defined */
4681 sym = sym_find(v);
4682 if (sym) {
4683 if (!is_compatible_types(&sym->type, type))
4684 error("incompatible types for redefinition of '%s'",
4685 get_tok_str(v, NULL));
4686 if (sym->type.t & VT_EXTERN) {
4687 /* if the variable is extern, it was not allocated */
4688 sym->type.t &= ~VT_EXTERN;
4689 /* set array size if it was ommited in extern
4690 declaration */
4691 if ((sym->type.t & VT_ARRAY) &&
4692 sym->type.ref->c < 0 &&
4693 type->ref->c >= 0)
4694 sym->type.ref->c = type->ref->c;
4695 } else {
4696 /* we accept several definitions of the same
4697 global variable. this is tricky, because we
4698 must play with the SHN_COMMON type of the symbol */
4699 /* XXX: should check if the variable was already
4700 initialized. It is incorrect to initialized it
4701 twice */
4702 /* no init data, we won't add more to the symbol */
4703 if (!has_init)
4704 goto no_alloc;
4709 /* allocate symbol in corresponding section */
4710 sec = ad->section;
4711 if (!sec) {
4712 if (has_init)
4713 sec = data_section;
4714 else if (tcc_state->nocommon)
4715 sec = bss_section;
4717 if (sec) {
4718 data_offset = sec->data_offset;
4719 data_offset = (data_offset + align - 1) & -align;
4720 addr = data_offset;
4721 /* very important to increment global pointer at this time
4722 because initializers themselves can create new initializers */
4723 data_offset += size;
4724 /* add padding if bound check */
4725 if (do_bounds_check)
4726 data_offset++;
4727 sec->data_offset = data_offset;
4728 /* allocate section space to put the data */
4729 if (sec->sh_type != SHT_NOBITS &&
4730 data_offset > sec->data_allocated)
4731 section_realloc(sec, data_offset);
4732 /* align section if needed */
4733 if (align > sec->sh_addralign)
4734 sec->sh_addralign = align;
4735 } else {
4736 addr = 0; /* avoid warning */
4739 if (v) {
4740 if (scope != VT_CONST || !sym) {
4741 sym = sym_push(v, type, r | VT_SYM, 0);
4743 /* update symbol definition */
4744 if (sec) {
4745 put_extern_sym(sym, sec, addr, size);
4746 } else {
4747 ElfW(Sym) *esym;
4748 /* put a common area */
4749 put_extern_sym(sym, NULL, align, size);
4750 /* XXX: find a nicer way */
4751 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
4752 esym->st_shndx = SHN_COMMON;
4754 } else {
4755 CValue cval;
4757 /* push global reference */
4758 sym = get_sym_ref(type, sec, addr, size);
4759 cval.ul = 0;
4760 vsetc(type, VT_CONST | VT_SYM, &cval);
4761 vtop->sym = sym;
4764 /* handles bounds now because the symbol must be defined
4765 before for the relocation */
4766 if (do_bounds_check) {
4767 unsigned long *bounds_ptr;
4769 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
4770 /* then add global bound info */
4771 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
4772 bounds_ptr[0] = 0; /* relocated */
4773 bounds_ptr[1] = size;
4776 if (has_init) {
4777 decl_initializer(type, sec, addr, 1, 0);
4778 /* restore parse state if needed */
4779 if (init_str.str) {
4780 tok_str_free(init_str.str);
4781 restore_parse_state(&saved_parse_state);
4784 no_alloc: ;
4787 void put_func_debug(Sym *sym)
4789 char buf[512];
4791 /* stabs info */
4792 /* XXX: we put here a dummy type */
4793 snprintf(buf, sizeof(buf), "%s:%c1",
4794 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
4795 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
4796 cur_text_section, sym->c);
4797 /* //gr gdb wants a line at the function */
4798 put_stabn(N_SLINE, 0, file->line_num, 0);
4799 last_ind = 0;
4800 last_line_num = 0;
4803 /* parse an old style function declaration list */
4804 /* XXX: check multiple parameter */
4805 static void func_decl_list(Sym *func_sym)
4807 AttributeDef ad;
4808 int v;
4809 Sym *s;
4810 CType btype, type;
4812 /* parse each declaration */
4813 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
4814 if (!parse_btype(&btype, &ad))
4815 expect("declaration list");
4816 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4817 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4818 tok == ';') {
4819 /* we accept no variable after */
4820 } else {
4821 for(;;) {
4822 type = btype;
4823 type_decl(&type, &ad, &v, TYPE_DIRECT);
4824 /* find parameter in function parameter list */
4825 s = func_sym->next;
4826 while (s != NULL) {
4827 if ((s->v & ~SYM_FIELD) == v)
4828 goto found;
4829 s = s->next;
4831 error("declaration for parameter '%s' but no such parameter",
4832 get_tok_str(v, NULL));
4833 found:
4834 /* check that no storage specifier except 'register' was given */
4835 if (type.t & VT_STORAGE)
4836 error("storage class specified for '%s'", get_tok_str(v, NULL));
4837 convert_parameter_type(&type);
4838 /* we can add the type (NOTE: it could be local to the function) */
4839 s->type = type;
4840 /* accept other parameters */
4841 if (tok == ',')
4842 next();
4843 else
4844 break;
4847 skip(';');
4851 /* parse a function defined by symbol 'sym' and generate its code in
4852 'cur_text_section' */
4853 static void gen_function(Sym *sym)
4855 int saved_nocode_wanted = nocode_wanted;
4856 nocode_wanted = 0;
4857 ind = cur_text_section->data_offset;
4858 /* NOTE: we patch the symbol size later */
4859 put_extern_sym(sym, cur_text_section, ind, 0);
4860 funcname = get_tok_str(sym->v, NULL);
4861 func_ind = ind;
4862 /* put debug symbol */
4863 if (do_debug)
4864 put_func_debug(sym);
4865 /* push a dummy symbol to enable local sym storage */
4866 sym_push2(&local_stack, SYM_FIELD, 0, 0);
4867 gfunc_prolog(&sym->type);
4868 rsym = 0;
4869 block(NULL, NULL, NULL, NULL, 0, 0);
4870 gsym(rsym);
4871 gfunc_epilog();
4872 cur_text_section->data_offset = ind;
4873 label_pop(&global_label_stack, NULL);
4874 sym_pop(&local_stack, NULL); /* reset local stack */
4875 /* end of function */
4876 /* patch symbol size */
4877 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
4878 ind - func_ind;
4879 if (do_debug) {
4880 put_stabn(N_FUN, 0, 0, ind - func_ind);
4882 /* It's better to crash than to generate wrong code */
4883 cur_text_section = NULL;
4884 funcname = ""; /* for safety */
4885 func_vt.t = VT_VOID; /* for safety */
4886 ind = 0; /* for safety */
4887 nocode_wanted = saved_nocode_wanted;
4890 static void gen_inline_functions(void)
4892 Sym *sym;
4893 CType *type;
4894 int *str, inline_generated;
4896 /* iterate while inline function are referenced */
4897 for(;;) {
4898 inline_generated = 0;
4899 for(sym = global_stack; sym != NULL; sym = sym->prev) {
4900 type = &sym->type;
4901 if (((type->t & VT_BTYPE) == VT_FUNC) &&
4902 (type->t & (VT_STATIC | VT_INLINE)) ==
4903 (VT_STATIC | VT_INLINE) &&
4904 sym->c != 0) {
4905 /* the function was used: generate its code and
4906 convert it to a normal function */
4907 str = INLINE_DEF(sym->r);
4908 sym->r = VT_SYM | VT_CONST;
4909 sym->type.t &= ~VT_INLINE;
4911 macro_ptr = str;
4912 next();
4913 cur_text_section = text_section;
4914 gen_function(sym);
4915 macro_ptr = NULL; /* fail safe */
4917 tok_str_free(str);
4918 inline_generated = 1;
4921 if (!inline_generated)
4922 break;
4925 /* free all remaining inline function tokens */
4926 for(sym = global_stack; sym != NULL; sym = sym->prev) {
4927 type = &sym->type;
4928 if (((type->t & VT_BTYPE) == VT_FUNC) &&
4929 (type->t & (VT_STATIC | VT_INLINE)) ==
4930 (VT_STATIC | VT_INLINE)) {
4931 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
4932 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
4933 continue;
4934 str = INLINE_DEF(sym->r);
4935 tok_str_free(str);
4936 sym->r = 0; /* fail safe */
4941 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4942 static void decl(int l)
4944 int v, has_init, r;
4945 CType type, btype;
4946 Sym *sym;
4947 AttributeDef ad;
4949 while (1) {
4950 if (!parse_btype(&btype, &ad)) {
4951 /* skip redundant ';' */
4952 /* XXX: find more elegant solution */
4953 if (tok == ';') {
4954 next();
4955 continue;
4957 if (l == VT_CONST &&
4958 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
4959 /* global asm block */
4960 asm_global_instr();
4961 continue;
4963 /* special test for old K&R protos without explicit int
4964 type. Only accepted when defining global data */
4965 if (l == VT_LOCAL || tok < TOK_DEFINE)
4966 break;
4967 btype.t = VT_INT;
4969 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4970 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4971 tok == ';') {
4972 /* we accept no variable after */
4973 next();
4974 continue;
4976 while (1) { /* iterate thru each declaration */
4977 type = btype;
4978 type_decl(&type, &ad, &v, TYPE_DIRECT);
4979 #if 0
4981 char buf[500];
4982 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
4983 printf("type = '%s'\n", buf);
4985 #endif
4986 if ((type.t & VT_BTYPE) == VT_FUNC) {
4987 /* if old style function prototype, we accept a
4988 declaration list */
4989 sym = type.ref;
4990 if (sym->c == FUNC_OLD)
4991 func_decl_list(sym);
4994 if (tok == '{') {
4995 if (l == VT_LOCAL)
4996 error("cannot use local functions");
4997 if ((type.t & VT_BTYPE) != VT_FUNC)
4998 expect("function definition");
5000 /* reject abstract declarators in function definition */
5001 sym = type.ref;
5002 while ((sym = sym->next) != NULL)
5003 if (!(sym->v & ~SYM_FIELD))
5004 expect("identifier");
5006 /* XXX: cannot do better now: convert extern line to static inline */
5007 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5008 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5010 sym = sym_find(v);
5011 if (sym) {
5012 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5013 goto func_error1;
5014 /* specific case: if not func_call defined, we put
5015 the one of the prototype */
5016 /* XXX: should have default value */
5017 r = sym->type.ref->r;
5018 if (FUNC_CALL(r) != FUNC_CDECL
5019 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5020 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5021 if (FUNC_EXPORT(r))
5022 FUNC_EXPORT(type.ref->r) = 1;
5024 if (!is_compatible_types(&sym->type, &type)) {
5025 func_error1:
5026 error("incompatible types for redefinition of '%s'",
5027 get_tok_str(v, NULL));
5029 /* if symbol is already defined, then put complete type */
5030 sym->type = type;
5031 } else {
5032 /* put function symbol */
5033 sym = global_identifier_push(v, type.t, 0);
5034 sym->type.ref = type.ref;
5037 /* static inline functions are just recorded as a kind
5038 of macro. Their code will be emitted at the end of
5039 the compilation unit only if they are used */
5040 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5041 (VT_INLINE | VT_STATIC)) {
5042 TokenString func_str;
5043 int block_level;
5045 tok_str_new(&func_str);
5047 block_level = 0;
5048 for(;;) {
5049 int t;
5050 if (tok == TOK_EOF)
5051 error("unexpected end of file");
5052 tok_str_add_tok(&func_str);
5053 t = tok;
5054 next();
5055 if (t == '{') {
5056 block_level++;
5057 } else if (t == '}') {
5058 block_level--;
5059 if (block_level == 0)
5060 break;
5063 tok_str_add(&func_str, -1);
5064 tok_str_add(&func_str, 0);
5065 INLINE_DEF(sym->r) = func_str.str;
5066 } else {
5067 /* compute text section */
5068 cur_text_section = ad.section;
5069 if (!cur_text_section)
5070 cur_text_section = text_section;
5071 sym->r = VT_SYM | VT_CONST;
5072 gen_function(sym);
5074 break;
5075 } else {
5076 if (btype.t & VT_TYPEDEF) {
5077 /* save typedefed type */
5078 /* XXX: test storage specifiers ? */
5079 sym = sym_push(v, &type, 0, 0);
5080 sym->type.t |= VT_TYPEDEF;
5081 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5082 /* external function definition */
5083 /* specific case for func_call attribute */
5084 if (ad.func_attr)
5085 type.ref->r = ad.func_attr;
5086 external_sym(v, &type, 0);
5087 } else {
5088 /* not lvalue if array */
5089 r = 0;
5090 if (!(type.t & VT_ARRAY))
5091 r |= lvalue_type(type.t);
5092 has_init = (tok == '=');
5093 if ((btype.t & VT_EXTERN) ||
5094 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5095 !has_init && l == VT_CONST && type.ref->c < 0)) {
5096 /* external variable */
5097 /* NOTE: as GCC, uninitialized global static
5098 arrays of null size are considered as
5099 extern */
5100 external_sym(v, &type, r);
5101 } else {
5102 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5103 if (type.t & VT_STATIC)
5104 r |= VT_CONST;
5105 else
5106 r |= l;
5107 if (has_init)
5108 next();
5109 decl_initializer_alloc(&type, &ad, r,
5110 has_init, v, l);
5113 if (tok != ',') {
5114 skip(';');
5115 break;
5117 next();