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