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