re-apply VLA by Thomas Preud'homme
[tinycc.git] / tccgen.c
blobc9629d4f6a69619035f88ae6bc06c96c92389b51
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 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
31 ST_DATA int rsym, anon_sym, ind, loc;
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *define_stack;
54 ST_DATA Sym *global_label_stack;
55 ST_DATA Sym *local_label_stack;
57 ST_DATA SValue vstack[VSTACK_SIZE], *vtop;
59 ST_DATA int const_wanted; /* true if constant wanted */
60 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
61 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
63 ST_DATA int func_vc;
64 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
65 ST_DATA char *funcname;
67 ST_DATA CType char_pointer_type, func_old_type, int_type;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType *type);
71 static inline CType *pointed_type(CType *type);
72 static int is_compatible_types(CType *type1, CType *type2);
73 static int parse_btype(CType *type, AttributeDef *ad);
74 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
75 static void parse_expr_type(CType *type);
76 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
77 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
78 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
79 static int decl0(int l, int is_for_loop_init);
80 static void expr_eq(void);
81 static void unary_type(CType *type);
82 static void vla_runtime_type_size(CType *type, int *a);
83 static int is_compatible_parameter_types(CType *type1, CType *type2);
84 static void expr_type(CType *type);
86 ST_INLN int is_float(int t)
88 int bt;
89 bt = t & VT_BTYPE;
90 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
93 ST_FUNC void test_lvalue(void)
95 if (!(vtop->r & VT_LVAL))
96 expect("lvalue");
99 /* ------------------------------------------------------------------------- */
100 /* symbol allocator */
101 static Sym *__sym_malloc(void)
103 Sym *sym_pool, *sym, *last_sym;
104 int i;
106 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
107 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
109 last_sym = sym_free_first;
110 sym = sym_pool;
111 for(i = 0; i < SYM_POOL_NB; i++) {
112 sym->next = last_sym;
113 last_sym = sym;
114 sym++;
116 sym_free_first = last_sym;
117 return last_sym;
120 static inline Sym *sym_malloc(void)
122 Sym *sym;
123 sym = sym_free_first;
124 if (!sym)
125 sym = __sym_malloc();
126 sym_free_first = sym->next;
127 return sym;
130 ST_INLN void sym_free(Sym *sym)
132 sym->next = sym_free_first;
133 tcc_free(sym->asm_label);
134 sym_free_first = sym;
137 /* push, without hashing */
138 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
140 Sym *s;
141 s = sym_malloc();
142 s->asm_label = NULL;
143 s->v = v;
144 s->type.t = t;
145 s->type.ref = NULL;
146 #ifdef _WIN64
147 s->d = NULL;
148 #endif
149 s->c = c;
150 s->next = NULL;
151 /* add in stack */
152 s->prev = *ps;
153 *ps = s;
154 return s;
157 /* find a symbol and return its associated structure. 's' is the top
158 of the symbol stack */
159 ST_FUNC Sym *sym_find2(Sym *s, int v)
161 while (s) {
162 if (s->v == v)
163 return s;
164 s = s->prev;
166 return NULL;
169 /* structure lookup */
170 ST_INLN Sym *struct_find(int v)
172 v -= TOK_IDENT;
173 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
174 return NULL;
175 return table_ident[v]->sym_struct;
178 /* find an identifier */
179 ST_INLN Sym *sym_find(int v)
181 v -= TOK_IDENT;
182 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
183 return NULL;
184 return table_ident[v]->sym_identifier;
187 /* push a given symbol on the symbol stack */
188 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
190 Sym *s, **ps;
191 TokenSym *ts;
193 if (local_stack)
194 ps = &local_stack;
195 else
196 ps = &global_stack;
197 s = sym_push2(ps, v, type->t, c);
198 s->type.ref = type->ref;
199 s->r = r;
200 /* don't record fields or anonymous symbols */
201 /* XXX: simplify */
202 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
203 /* record symbol in token array */
204 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
205 if (v & SYM_STRUCT)
206 ps = &ts->sym_struct;
207 else
208 ps = &ts->sym_identifier;
209 s->prev_tok = *ps;
210 *ps = s;
212 return s;
215 /* push a global identifier */
216 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
218 Sym *s, **ps;
219 s = sym_push2(&global_stack, v, t, c);
220 /* don't record anonymous symbol */
221 if (v < SYM_FIRST_ANOM) {
222 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
223 /* modify the top most local identifier, so that
224 sym_identifier will point to 's' when popped */
225 while (*ps != NULL)
226 ps = &(*ps)->prev_tok;
227 s->prev_tok = NULL;
228 *ps = s;
230 return s;
233 /* pop symbols until top reaches 'b' */
234 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
236 Sym *s, *ss, **ps;
237 TokenSym *ts;
238 int v;
240 s = *ptop;
241 while(s != b) {
242 ss = s->prev;
243 v = s->v;
244 /* remove symbol in token array */
245 /* XXX: simplify */
246 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
247 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
248 if (v & SYM_STRUCT)
249 ps = &ts->sym_struct;
250 else
251 ps = &ts->sym_identifier;
252 *ps = s->prev_tok;
254 sym_free(s);
255 s = ss;
257 *ptop = b;
260 static void weaken_symbol(Sym *sym)
262 sym->type.t |= VT_WEAK;
263 if (sym->c > 0) {
264 int esym_type;
265 ElfW(Sym) *esym;
267 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
268 esym_type = ELFW(ST_TYPE)(esym->st_info);
269 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
273 /* ------------------------------------------------------------------------- */
275 ST_FUNC void swap(int *p, int *q)
277 int t;
278 t = *p;
279 *p = *q;
280 *q = t;
283 static void vsetc(CType *type, int r, CValue *vc)
285 int v;
287 if (vtop >= vstack + (VSTACK_SIZE - 1))
288 error("memory full");
289 /* cannot let cpu flags if other instruction are generated. Also
290 avoid leaving VT_JMP anywhere except on the top of the stack
291 because it would complicate the code generator. */
292 if (vtop >= vstack) {
293 v = vtop->r & VT_VALMASK;
294 if (v == VT_CMP || (v & ~1) == VT_JMP)
295 gv(RC_INT);
297 vtop++;
298 vtop->type = *type;
299 vtop->r = r;
300 vtop->r2 = VT_CONST;
301 vtop->c = *vc;
304 /* push constant of type "type" with useless value */
305 void vpush(CType *type)
307 CValue cval;
308 vsetc(type, VT_CONST, &cval);
311 /* push integer constant */
312 ST_FUNC void vpushi(int v)
314 CValue cval;
315 cval.i = v;
316 vsetc(&int_type, VT_CONST, &cval);
319 /* push long long constant */
320 static void vpushll(long long v)
322 CValue cval;
323 CType ctype;
324 ctype.t = VT_LLONG;
325 ctype.ref = 0;
326 cval.ull = v;
327 vsetc(&ctype, VT_CONST, &cval);
330 /* push arbitrary 64bit constant */
331 void vpush64(int ty, unsigned long long v)
333 CValue cval;
334 CType ctype;
335 ctype.t = ty;
336 cval.ull = v;
337 vsetc(&ctype, VT_CONST, &cval);
340 /* Return a static symbol pointing to a section */
341 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
343 int v;
344 Sym *sym;
346 v = anon_sym++;
347 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
348 sym->type.ref = type->ref;
349 sym->r = VT_CONST | VT_SYM;
350 put_extern_sym(sym, sec, offset, size);
351 return sym;
354 /* push a reference to a section offset by adding a dummy symbol */
355 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
357 CValue cval;
359 cval.ul = 0;
360 vsetc(type, VT_CONST | VT_SYM, &cval);
361 vtop->sym = get_sym_ref(type, sec, offset, size);
364 /* define a new external reference to a symbol 'v' of type 'u' */
365 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
367 Sym *s;
369 s = sym_find(v);
370 if (!s) {
371 /* push forward reference */
372 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
373 s->type.ref = type->ref;
374 s->r = r | VT_CONST | VT_SYM;
376 return s;
379 /* define a new external reference to a symbol 'v' with alternate asm
380 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
381 is no alternate name (most cases) */
382 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
384 Sym *s;
386 s = sym_find(v);
387 if (!s) {
388 /* push forward reference */
389 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
390 s->asm_label = asm_label;
391 s->type.t |= VT_EXTERN;
392 } else if (s->type.ref == func_old_type.ref) {
393 s->type.ref = type->ref;
394 s->r = r | VT_CONST | VT_SYM;
395 s->type.t |= VT_EXTERN;
396 } else if (!is_compatible_types(&s->type, type)) {
397 error("incompatible types for redefinition of '%s'",
398 get_tok_str(v, NULL));
400 return s;
403 /* push a reference to global symbol v */
404 ST_FUNC void vpush_global_sym(CType *type, int v)
406 Sym *sym;
407 CValue cval;
409 sym = external_global_sym(v, type, 0);
410 cval.ul = 0;
411 vsetc(type, VT_CONST | VT_SYM, &cval);
412 vtop->sym = sym;
415 ST_FUNC void vset(CType *type, int r, int v)
417 CValue cval;
419 cval.i = v;
420 vsetc(type, r, &cval);
423 static void vseti(int r, int v)
425 CType type;
426 type.t = VT_INT;
427 type.ref = 0;
428 vset(&type, r, v);
431 ST_FUNC void vswap(void)
433 SValue tmp;
435 tmp = vtop[0];
436 vtop[0] = vtop[-1];
437 vtop[-1] = tmp;
440 ST_FUNC void vpushv(SValue *v)
442 if (vtop >= vstack + (VSTACK_SIZE - 1))
443 error("memory full");
444 vtop++;
445 *vtop = *v;
448 static void vdup(void)
450 vpushv(vtop);
453 /* save r to the memory stack, and mark it as being free */
454 ST_FUNC void save_reg(int r)
456 int l, saved, size, align;
457 SValue *p, sv;
458 CType *type;
460 /* modify all stack values */
461 saved = 0;
462 l = 0;
463 for(p=vstack;p<=vtop;p++) {
464 if ((p->r & VT_VALMASK) == r ||
465 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
466 /* must save value on stack if not already done */
467 if (!saved) {
468 /* NOTE: must reload 'r' because r might be equal to r2 */
469 r = p->r & VT_VALMASK;
470 /* store register in the stack */
471 type = &p->type;
472 if ((p->r & VT_LVAL) ||
473 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
474 #ifdef TCC_TARGET_X86_64
475 type = &char_pointer_type;
476 #else
477 type = &int_type;
478 #endif
479 size = type_size(type, &align);
480 loc = (loc - size) & -align;
481 sv.type.t = type->t;
482 sv.r = VT_LOCAL | VT_LVAL;
483 sv.c.ul = loc;
484 store(r, &sv);
485 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
486 /* x86 specific: need to pop fp register ST0 if saved */
487 if (r == TREG_ST0) {
488 o(0xd8dd); /* fstp %st(0) */
490 #endif
491 #ifndef TCC_TARGET_X86_64
492 /* special long long case */
493 if ((type->t & VT_BTYPE) == VT_LLONG) {
494 sv.c.ul += 4;
495 store(p->r2, &sv);
497 #endif
498 l = loc;
499 saved = 1;
501 /* mark that stack entry as being saved on the stack */
502 if (p->r & VT_LVAL) {
503 /* also clear the bounded flag because the
504 relocation address of the function was stored in
505 p->c.ul */
506 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
507 } else {
508 p->r = lvalue_type(p->type.t) | VT_LOCAL;
510 p->r2 = VT_CONST;
511 p->c.ul = l;
516 #ifdef TCC_TARGET_ARM
517 /* find a register of class 'rc2' with at most one reference on stack.
518 * If none, call get_reg(rc) */
519 ST_FUNC int get_reg_ex(int rc, int rc2)
521 int r;
522 SValue *p;
524 for(r=0;r<NB_REGS;r++) {
525 if (reg_classes[r] & rc2) {
526 int n;
527 n=0;
528 for(p = vstack; p <= vtop; p++) {
529 if ((p->r & VT_VALMASK) == r ||
530 (p->r2 & VT_VALMASK) == r)
531 n++;
533 if (n <= 1)
534 return r;
537 return get_reg(rc);
539 #endif
541 /* find a free register of class 'rc'. If none, save one register */
542 ST_FUNC int get_reg(int rc)
544 int r;
545 SValue *p;
547 /* find a free register */
548 for(r=0;r<NB_REGS;r++) {
549 if (reg_classes[r] & rc) {
550 for(p=vstack;p<=vtop;p++) {
551 if ((p->r & VT_VALMASK) == r ||
552 (p->r2 & VT_VALMASK) == r)
553 goto notfound;
555 return r;
557 notfound: ;
560 /* no register left : free the first one on the stack (VERY
561 IMPORTANT to start from the bottom to ensure that we don't
562 spill registers used in gen_opi()) */
563 for(p=vstack;p<=vtop;p++) {
564 r = p->r & VT_VALMASK;
565 if (r < VT_CONST && (reg_classes[r] & rc))
566 goto save_found;
567 /* also look at second register (if long long) */
568 r = p->r2 & VT_VALMASK;
569 if (r < VT_CONST && (reg_classes[r] & rc)) {
570 save_found:
571 save_reg(r);
572 return r;
575 /* Should never comes here */
576 return -1;
579 /* save registers up to (vtop - n) stack entry */
580 ST_FUNC void save_regs(int n)
582 int r;
583 SValue *p, *p1;
584 p1 = vtop - n;
585 for(p = vstack;p <= p1; p++) {
586 r = p->r & VT_VALMASK;
587 if (r < VT_CONST) {
588 save_reg(r);
593 /* move register 's' to 'r', and flush previous value of r to memory
594 if needed */
595 static void move_reg(int r, int s)
597 SValue sv;
599 if (r != s) {
600 save_reg(r);
601 sv.type.t = VT_INT;
602 sv.r = s;
603 sv.c.ul = 0;
604 load(r, &sv);
608 /* get address of vtop (vtop MUST BE an lvalue) */
609 static void gaddrof(void)
611 vtop->r &= ~VT_LVAL;
612 /* tricky: if saved lvalue, then we can go back to lvalue */
613 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
614 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
617 #ifdef CONFIG_TCC_BCHECK
618 /* generate lvalue bound code */
619 static void gbound(void)
621 int lval_type;
622 CType type1;
624 vtop->r &= ~VT_MUSTBOUND;
625 /* if lvalue, then use checking code before dereferencing */
626 if (vtop->r & VT_LVAL) {
627 /* if not VT_BOUNDED value, then make one */
628 if (!(vtop->r & VT_BOUNDED)) {
629 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
630 /* must save type because we must set it to int to get pointer */
631 type1 = vtop->type;
632 vtop->type.t = VT_INT;
633 gaddrof();
634 vpushi(0);
635 gen_bounded_ptr_add();
636 vtop->r |= lval_type;
637 vtop->type = type1;
639 /* then check for dereferencing */
640 gen_bounded_ptr_deref();
643 #endif
645 /* store vtop a register belonging to class 'rc'. lvalues are
646 converted to values. Cannot be used if cannot be converted to
647 register value (such as structures). */
648 ST_FUNC int gv(int rc)
650 int r, rc2, bit_pos, bit_size, size, align, i;
652 /* NOTE: get_reg can modify vstack[] */
653 if (vtop->type.t & VT_BITFIELD) {
654 CType type;
655 int bits = 32;
656 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
657 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
658 /* remove bit field info to avoid loops */
659 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
660 /* cast to int to propagate signedness in following ops */
661 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
662 type.t = VT_LLONG;
663 bits = 64;
664 } else
665 type.t = VT_INT;
666 if((vtop->type.t & VT_UNSIGNED) ||
667 (vtop->type.t & VT_BTYPE) == VT_BOOL)
668 type.t |= VT_UNSIGNED;
669 gen_cast(&type);
670 /* generate shifts */
671 vpushi(bits - (bit_pos + bit_size));
672 gen_op(TOK_SHL);
673 vpushi(bits - bit_size);
674 /* NOTE: transformed to SHR if unsigned */
675 gen_op(TOK_SAR);
676 r = gv(rc);
677 } else {
678 if (is_float(vtop->type.t) &&
679 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
680 Sym *sym;
681 int *ptr;
682 unsigned long offset;
683 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
684 CValue check;
685 #endif
687 /* XXX: unify with initializers handling ? */
688 /* CPUs usually cannot use float constants, so we store them
689 generically in data segment */
690 size = type_size(&vtop->type, &align);
691 offset = (data_section->data_offset + align - 1) & -align;
692 data_section->data_offset = offset;
693 /* XXX: not portable yet */
694 #if defined(__i386__) || defined(__x86_64__)
695 /* Zero pad x87 tenbyte long doubles */
696 if (size == LDOUBLE_SIZE)
697 vtop->c.tab[2] &= 0xffff;
698 #endif
699 ptr = section_ptr_add(data_section, size);
700 size = size >> 2;
701 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
702 check.d = 1;
703 if(check.tab[0])
704 for(i=0;i<size;i++)
705 ptr[i] = vtop->c.tab[size-1-i];
706 else
707 #endif
708 for(i=0;i<size;i++)
709 ptr[i] = vtop->c.tab[i];
710 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
711 vtop->r |= VT_LVAL | VT_SYM;
712 vtop->sym = sym;
713 vtop->c.ul = 0;
715 #ifdef CONFIG_TCC_BCHECK
716 if (vtop->r & VT_MUSTBOUND)
717 gbound();
718 #endif
720 r = vtop->r & VT_VALMASK;
721 rc2 = RC_INT;
722 if (rc == RC_IRET)
723 rc2 = RC_LRET;
724 /* need to reload if:
725 - constant
726 - lvalue (need to dereference pointer)
727 - already a register, but not in the right class */
728 if (r >= VT_CONST
729 || (vtop->r & VT_LVAL)
730 || !(reg_classes[r] & rc)
731 #ifndef TCC_TARGET_X86_64
732 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
733 #endif
736 r = get_reg(rc);
737 #ifndef TCC_TARGET_X86_64
738 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
739 int r2;
740 unsigned long long ll;
741 /* two register type load : expand to two words
742 temporarily */
743 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
744 /* load constant */
745 ll = vtop->c.ull;
746 vtop->c.ui = ll; /* first word */
747 load(r, vtop);
748 vtop->r = r; /* save register value */
749 vpushi(ll >> 32); /* second word */
750 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
751 (vtop->r & VT_LVAL)) {
752 /* We do not want to modifier the long long
753 pointer here, so the safest (and less
754 efficient) is to save all the other registers
755 in the stack. XXX: totally inefficient. */
756 save_regs(1);
757 /* load from memory */
758 load(r, vtop);
759 vdup();
760 vtop[-1].r = r; /* save register value */
761 /* increment pointer to get second word */
762 vtop->type.t = VT_INT;
763 gaddrof();
764 vpushi(4);
765 gen_op('+');
766 vtop->r |= VT_LVAL;
767 } else {
768 /* move registers */
769 load(r, vtop);
770 vdup();
771 vtop[-1].r = r; /* save register value */
772 vtop->r = vtop[-1].r2;
774 /* allocate second register */
775 r2 = get_reg(rc2);
776 load(r2, vtop);
777 vpop();
778 /* write second register */
779 vtop->r2 = r2;
780 } else
781 #endif
782 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
783 int t1, t;
784 /* lvalue of scalar type : need to use lvalue type
785 because of possible cast */
786 t = vtop->type.t;
787 t1 = t;
788 /* compute memory access type */
789 if (vtop->r & VT_LVAL_BYTE)
790 t = VT_BYTE;
791 else if (vtop->r & VT_LVAL_SHORT)
792 t = VT_SHORT;
793 if (vtop->r & VT_LVAL_UNSIGNED)
794 t |= VT_UNSIGNED;
795 vtop->type.t = t;
796 load(r, vtop);
797 /* restore wanted type */
798 vtop->type.t = t1;
799 } else {
800 /* one register type load */
801 load(r, vtop);
804 vtop->r = r;
805 #ifdef TCC_TARGET_C67
806 /* uses register pairs for doubles */
807 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
808 vtop->r2 = r+1;
809 #endif
811 return r;
814 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
815 ST_FUNC void gv2(int rc1, int rc2)
817 int v;
819 /* generate more generic register first. But VT_JMP or VT_CMP
820 values must be generated first in all cases to avoid possible
821 reload errors */
822 v = vtop[0].r & VT_VALMASK;
823 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
824 vswap();
825 gv(rc1);
826 vswap();
827 gv(rc2);
828 /* test if reload is needed for first register */
829 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
830 vswap();
831 gv(rc1);
832 vswap();
834 } else {
835 gv(rc2);
836 vswap();
837 gv(rc1);
838 vswap();
839 /* test if reload is needed for first register */
840 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
841 gv(rc2);
846 /* wrapper around RC_FRET to return a register by type */
847 static int rc_fret(int t)
849 #ifdef TCC_TARGET_X86_64
850 if (t == VT_LDOUBLE) {
851 return RC_ST0;
853 #endif
854 return RC_FRET;
857 /* wrapper around REG_FRET to return a register by type */
858 static int reg_fret(int t)
860 #ifdef TCC_TARGET_X86_64
861 if (t == VT_LDOUBLE) {
862 return TREG_ST0;
864 #endif
865 return REG_FRET;
868 /* expand long long on stack in two int registers */
869 static void lexpand(void)
871 int u;
873 u = vtop->type.t & VT_UNSIGNED;
874 gv(RC_INT);
875 vdup();
876 vtop[0].r = vtop[-1].r2;
877 vtop[0].r2 = VT_CONST;
878 vtop[-1].r2 = VT_CONST;
879 vtop[0].type.t = VT_INT | u;
880 vtop[-1].type.t = VT_INT | u;
883 #ifdef TCC_TARGET_ARM
884 /* expand long long on stack */
885 ST_FUNC void lexpand_nr(void)
887 int u,v;
889 u = vtop->type.t & VT_UNSIGNED;
890 vdup();
891 vtop->r2 = VT_CONST;
892 vtop->type.t = VT_INT | u;
893 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
894 if (v == VT_CONST) {
895 vtop[-1].c.ui = vtop->c.ull;
896 vtop->c.ui = vtop->c.ull >> 32;
897 vtop->r = VT_CONST;
898 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
899 vtop->c.ui += 4;
900 vtop->r = vtop[-1].r;
901 } else if (v > VT_CONST) {
902 vtop--;
903 lexpand();
904 } else
905 vtop->r = vtop[-1].r2;
906 vtop[-1].r2 = VT_CONST;
907 vtop[-1].type.t = VT_INT | u;
909 #endif
911 /* build a long long from two ints */
912 static void lbuild(int t)
914 gv2(RC_INT, RC_INT);
915 vtop[-1].r2 = vtop[0].r;
916 vtop[-1].type.t = t;
917 vpop();
920 /* rotate n first stack elements to the bottom
921 I1 ... In -> I2 ... In I1 [top is right]
923 static void vrotb(int n)
925 int i;
926 SValue tmp;
928 tmp = vtop[-n + 1];
929 for(i=-n+1;i!=0;i++)
930 vtop[i] = vtop[i+1];
931 vtop[0] = tmp;
934 /* rotate n first stack elements to the top
935 I1 ... In -> In I1 ... I(n-1) [top is right]
937 ST_FUNC void vrott(int n)
939 int i;
940 SValue tmp;
942 tmp = vtop[0];
943 for(i = 0;i < n - 1; i++)
944 vtop[-i] = vtop[-i - 1];
945 vtop[-n + 1] = tmp;
948 #ifdef TCC_TARGET_ARM
949 /* like vrott but in other direction
950 In ... I1 -> I(n-1) ... I1 In [top is right]
952 ST_FUNC void vnrott(int n)
954 int i;
955 SValue tmp;
957 tmp = vtop[-n + 1];
958 for(i = n - 1; i > 0; i--)
959 vtop[-i] = vtop[-i + 1];
960 vtop[0] = tmp;
962 #endif
964 /* pop stack value */
965 ST_FUNC void vpop(void)
967 int v;
968 v = vtop->r & VT_VALMASK;
969 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
970 /* for x86, we need to pop the FP stack */
971 if (v == TREG_ST0 && !nocode_wanted) {
972 o(0xd8dd); /* fstp %st(0) */
973 } else
974 #endif
975 if (v == VT_JMP || v == VT_JMPI) {
976 /* need to put correct jump if && or || without test */
977 gsym(vtop->c.ul);
979 vtop--;
982 /* convert stack entry to register and duplicate its value in another
983 register */
984 static void gv_dup(void)
986 int rc, t, r, r1;
987 SValue sv;
989 t = vtop->type.t;
990 if ((t & VT_BTYPE) == VT_LLONG) {
991 lexpand();
992 gv_dup();
993 vswap();
994 vrotb(3);
995 gv_dup();
996 vrotb(4);
997 /* stack: H L L1 H1 */
998 lbuild(t);
999 vrotb(3);
1000 vrotb(3);
1001 vswap();
1002 lbuild(t);
1003 vswap();
1004 } else {
1005 /* duplicate value */
1006 rc = RC_INT;
1007 sv.type.t = VT_INT;
1008 if (is_float(t)) {
1009 rc = RC_FLOAT;
1010 #ifdef TCC_TARGET_X86_64
1011 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1012 rc = RC_ST0;
1014 #endif
1015 sv.type.t = t;
1017 r = gv(rc);
1018 r1 = get_reg(rc);
1019 sv.r = r;
1020 sv.c.ul = 0;
1021 load(r1, &sv); /* move r to r1 */
1022 vdup();
1023 /* duplicates value */
1024 if (r != r1)
1025 vtop->r = r1;
1029 #ifndef TCC_TARGET_X86_64
1030 /* generate CPU independent (unsigned) long long operations */
1031 static void gen_opl(int op)
1033 int t, a, b, op1, c, i;
1034 int func;
1035 unsigned short reg_iret = REG_IRET;
1036 unsigned short reg_lret = REG_LRET;
1037 SValue tmp;
1039 switch(op) {
1040 case '/':
1041 case TOK_PDIV:
1042 func = TOK___divdi3;
1043 goto gen_func;
1044 case TOK_UDIV:
1045 func = TOK___udivdi3;
1046 goto gen_func;
1047 case '%':
1048 func = TOK___moddi3;
1049 goto gen_mod_func;
1050 case TOK_UMOD:
1051 func = TOK___umoddi3;
1052 gen_mod_func:
1053 #ifdef TCC_ARM_EABI
1054 reg_iret = TREG_R2;
1055 reg_lret = TREG_R3;
1056 #endif
1057 gen_func:
1058 /* call generic long long function */
1059 vpush_global_sym(&func_old_type, func);
1060 vrott(3);
1061 gfunc_call(2);
1062 vpushi(0);
1063 vtop->r = reg_iret;
1064 vtop->r2 = reg_lret;
1065 break;
1066 case '^':
1067 case '&':
1068 case '|':
1069 case '*':
1070 case '+':
1071 case '-':
1072 t = vtop->type.t;
1073 vswap();
1074 lexpand();
1075 vrotb(3);
1076 lexpand();
1077 /* stack: L1 H1 L2 H2 */
1078 tmp = vtop[0];
1079 vtop[0] = vtop[-3];
1080 vtop[-3] = tmp;
1081 tmp = vtop[-2];
1082 vtop[-2] = vtop[-3];
1083 vtop[-3] = tmp;
1084 vswap();
1085 /* stack: H1 H2 L1 L2 */
1086 if (op == '*') {
1087 vpushv(vtop - 1);
1088 vpushv(vtop - 1);
1089 gen_op(TOK_UMULL);
1090 lexpand();
1091 /* stack: H1 H2 L1 L2 ML MH */
1092 for(i=0;i<4;i++)
1093 vrotb(6);
1094 /* stack: ML MH H1 H2 L1 L2 */
1095 tmp = vtop[0];
1096 vtop[0] = vtop[-2];
1097 vtop[-2] = tmp;
1098 /* stack: ML MH H1 L2 H2 L1 */
1099 gen_op('*');
1100 vrotb(3);
1101 vrotb(3);
1102 gen_op('*');
1103 /* stack: ML MH M1 M2 */
1104 gen_op('+');
1105 gen_op('+');
1106 } else if (op == '+' || op == '-') {
1107 /* XXX: add non carry method too (for MIPS or alpha) */
1108 if (op == '+')
1109 op1 = TOK_ADDC1;
1110 else
1111 op1 = TOK_SUBC1;
1112 gen_op(op1);
1113 /* stack: H1 H2 (L1 op L2) */
1114 vrotb(3);
1115 vrotb(3);
1116 gen_op(op1 + 1); /* TOK_xxxC2 */
1117 } else {
1118 gen_op(op);
1119 /* stack: H1 H2 (L1 op L2) */
1120 vrotb(3);
1121 vrotb(3);
1122 /* stack: (L1 op L2) H1 H2 */
1123 gen_op(op);
1124 /* stack: (L1 op L2) (H1 op H2) */
1126 /* stack: L H */
1127 lbuild(t);
1128 break;
1129 case TOK_SAR:
1130 case TOK_SHR:
1131 case TOK_SHL:
1132 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1133 t = vtop[-1].type.t;
1134 vswap();
1135 lexpand();
1136 vrotb(3);
1137 /* stack: L H shift */
1138 c = (int)vtop->c.i;
1139 /* constant: simpler */
1140 /* NOTE: all comments are for SHL. the other cases are
1141 done by swaping words */
1142 vpop();
1143 if (op != TOK_SHL)
1144 vswap();
1145 if (c >= 32) {
1146 /* stack: L H */
1147 vpop();
1148 if (c > 32) {
1149 vpushi(c - 32);
1150 gen_op(op);
1152 if (op != TOK_SAR) {
1153 vpushi(0);
1154 } else {
1155 gv_dup();
1156 vpushi(31);
1157 gen_op(TOK_SAR);
1159 vswap();
1160 } else {
1161 vswap();
1162 gv_dup();
1163 /* stack: H L L */
1164 vpushi(c);
1165 gen_op(op);
1166 vswap();
1167 vpushi(32 - c);
1168 if (op == TOK_SHL)
1169 gen_op(TOK_SHR);
1170 else
1171 gen_op(TOK_SHL);
1172 vrotb(3);
1173 /* stack: L L H */
1174 vpushi(c);
1175 if (op == TOK_SHL)
1176 gen_op(TOK_SHL);
1177 else
1178 gen_op(TOK_SHR);
1179 gen_op('|');
1181 if (op != TOK_SHL)
1182 vswap();
1183 lbuild(t);
1184 } else {
1185 /* XXX: should provide a faster fallback on x86 ? */
1186 switch(op) {
1187 case TOK_SAR:
1188 func = TOK___ashrdi3;
1189 goto gen_func;
1190 case TOK_SHR:
1191 func = TOK___lshrdi3;
1192 goto gen_func;
1193 case TOK_SHL:
1194 func = TOK___ashldi3;
1195 goto gen_func;
1198 break;
1199 default:
1200 /* compare operations */
1201 t = vtop->type.t;
1202 vswap();
1203 lexpand();
1204 vrotb(3);
1205 lexpand();
1206 /* stack: L1 H1 L2 H2 */
1207 tmp = vtop[-1];
1208 vtop[-1] = vtop[-2];
1209 vtop[-2] = tmp;
1210 /* stack: L1 L2 H1 H2 */
1211 /* compare high */
1212 op1 = op;
1213 /* when values are equal, we need to compare low words. since
1214 the jump is inverted, we invert the test too. */
1215 if (op1 == TOK_LT)
1216 op1 = TOK_LE;
1217 else if (op1 == TOK_GT)
1218 op1 = TOK_GE;
1219 else if (op1 == TOK_ULT)
1220 op1 = TOK_ULE;
1221 else if (op1 == TOK_UGT)
1222 op1 = TOK_UGE;
1223 a = 0;
1224 b = 0;
1225 gen_op(op1);
1226 if (op1 != TOK_NE) {
1227 a = gtst(1, 0);
1229 if (op != TOK_EQ) {
1230 /* generate non equal test */
1231 /* XXX: NOT PORTABLE yet */
1232 if (a == 0) {
1233 b = gtst(0, 0);
1234 } else {
1235 #if defined(TCC_TARGET_I386)
1236 b = psym(0x850f, 0);
1237 #elif defined(TCC_TARGET_ARM)
1238 b = ind;
1239 o(0x1A000000 | encbranch(ind, 0, 1));
1240 #elif defined(TCC_TARGET_C67)
1241 error("not implemented");
1242 #else
1243 #error not supported
1244 #endif
1247 /* compare low. Always unsigned */
1248 op1 = op;
1249 if (op1 == TOK_LT)
1250 op1 = TOK_ULT;
1251 else if (op1 == TOK_LE)
1252 op1 = TOK_ULE;
1253 else if (op1 == TOK_GT)
1254 op1 = TOK_UGT;
1255 else if (op1 == TOK_GE)
1256 op1 = TOK_UGE;
1257 gen_op(op1);
1258 a = gtst(1, a);
1259 gsym(b);
1260 vseti(VT_JMPI, a);
1261 break;
1264 #endif
1266 /* handle integer constant optimizations and various machine
1267 independent opt */
1268 static void gen_opic(int op)
1270 int c1, c2, t1, t2, n;
1271 SValue *v1, *v2;
1272 long long l1, l2;
1273 typedef unsigned long long U;
1275 v1 = vtop - 1;
1276 v2 = vtop;
1277 t1 = v1->type.t & VT_BTYPE;
1278 t2 = v2->type.t & VT_BTYPE;
1280 if (t1 == VT_LLONG)
1281 l1 = v1->c.ll;
1282 else if (v1->type.t & VT_UNSIGNED)
1283 l1 = v1->c.ui;
1284 else
1285 l1 = v1->c.i;
1287 if (t2 == VT_LLONG)
1288 l2 = v2->c.ll;
1289 else if (v2->type.t & VT_UNSIGNED)
1290 l2 = v2->c.ui;
1291 else
1292 l2 = v2->c.i;
1294 /* currently, we cannot do computations with forward symbols */
1295 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1296 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1297 if (c1 && c2) {
1298 switch(op) {
1299 case '+': l1 += l2; break;
1300 case '-': l1 -= l2; break;
1301 case '&': l1 &= l2; break;
1302 case '^': l1 ^= l2; break;
1303 case '|': l1 |= l2; break;
1304 case '*': l1 *= l2; break;
1306 case TOK_PDIV:
1307 case '/':
1308 case '%':
1309 case TOK_UDIV:
1310 case TOK_UMOD:
1311 /* if division by zero, generate explicit division */
1312 if (l2 == 0) {
1313 if (const_wanted)
1314 error("division by zero in constant");
1315 goto general_case;
1317 switch(op) {
1318 default: l1 /= l2; break;
1319 case '%': l1 %= l2; break;
1320 case TOK_UDIV: l1 = (U)l1 / l2; break;
1321 case TOK_UMOD: l1 = (U)l1 % l2; break;
1323 break;
1324 case TOK_SHL: l1 <<= l2; break;
1325 case TOK_SHR: l1 = (U)l1 >> l2; break;
1326 case TOK_SAR: l1 >>= l2; break;
1327 /* tests */
1328 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1329 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1330 case TOK_EQ: l1 = l1 == l2; break;
1331 case TOK_NE: l1 = l1 != l2; break;
1332 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1333 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1334 case TOK_LT: l1 = l1 < l2; break;
1335 case TOK_GE: l1 = l1 >= l2; break;
1336 case TOK_LE: l1 = l1 <= l2; break;
1337 case TOK_GT: l1 = l1 > l2; break;
1338 /* logical */
1339 case TOK_LAND: l1 = l1 && l2; break;
1340 case TOK_LOR: l1 = l1 || l2; break;
1341 default:
1342 goto general_case;
1344 v1->c.ll = l1;
1345 vtop--;
1346 } else {
1347 /* if commutative ops, put c2 as constant */
1348 if (c1 && (op == '+' || op == '&' || op == '^' ||
1349 op == '|' || op == '*')) {
1350 vswap();
1351 c2 = c1; //c = c1, c1 = c2, c2 = c;
1352 l2 = l1; //l = l1, l1 = l2, l2 = l;
1354 /* Filter out NOP operations like x*1, x-0, x&-1... */
1355 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1356 op == TOK_PDIV) &&
1357 l2 == 1) ||
1358 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1359 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1360 l2 == 0) ||
1361 (op == '&' &&
1362 l2 == -1))) {
1363 /* nothing to do */
1364 vtop--;
1365 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1366 /* try to use shifts instead of muls or divs */
1367 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1368 n = -1;
1369 while (l2) {
1370 l2 >>= 1;
1371 n++;
1373 vtop->c.ll = n;
1374 if (op == '*')
1375 op = TOK_SHL;
1376 else if (op == TOK_PDIV)
1377 op = TOK_SAR;
1378 else
1379 op = TOK_SHR;
1381 goto general_case;
1382 } else if (c2 && (op == '+' || op == '-') &&
1383 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1384 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1385 /* symbol + constant case */
1386 if (op == '-')
1387 l2 = -l2;
1388 vtop--;
1389 vtop->c.ll += l2;
1390 } else {
1391 general_case:
1392 if (!nocode_wanted) {
1393 /* call low level op generator */
1394 if (t1 == VT_LLONG || t2 == VT_LLONG)
1395 gen_opl(op);
1396 else
1397 gen_opi(op);
1398 } else {
1399 vtop--;
1405 /* generate a floating point operation with constant propagation */
1406 static void gen_opif(int op)
1408 int c1, c2;
1409 SValue *v1, *v2;
1410 long double f1, f2;
1412 v1 = vtop - 1;
1413 v2 = vtop;
1414 /* currently, we cannot do computations with forward symbols */
1415 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1416 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1417 if (c1 && c2) {
1418 if (v1->type.t == VT_FLOAT) {
1419 f1 = v1->c.f;
1420 f2 = v2->c.f;
1421 } else if (v1->type.t == VT_DOUBLE) {
1422 f1 = v1->c.d;
1423 f2 = v2->c.d;
1424 } else {
1425 f1 = v1->c.ld;
1426 f2 = v2->c.ld;
1429 /* NOTE: we only do constant propagation if finite number (not
1430 NaN or infinity) (ANSI spec) */
1431 if (!ieee_finite(f1) || !ieee_finite(f2))
1432 goto general_case;
1434 switch(op) {
1435 case '+': f1 += f2; break;
1436 case '-': f1 -= f2; break;
1437 case '*': f1 *= f2; break;
1438 case '/':
1439 if (f2 == 0.0) {
1440 if (const_wanted)
1441 error("division by zero in constant");
1442 goto general_case;
1444 f1 /= f2;
1445 break;
1446 /* XXX: also handles tests ? */
1447 default:
1448 goto general_case;
1450 /* XXX: overflow test ? */
1451 if (v1->type.t == VT_FLOAT) {
1452 v1->c.f = f1;
1453 } else if (v1->type.t == VT_DOUBLE) {
1454 v1->c.d = f1;
1455 } else {
1456 v1->c.ld = f1;
1458 vtop--;
1459 } else {
1460 general_case:
1461 if (!nocode_wanted) {
1462 gen_opf(op);
1463 } else {
1464 vtop--;
1469 static int pointed_size(CType *type)
1471 int align;
1472 return type_size(pointed_type(type), &align);
1475 static void vla_runtime_pointed_size(CType *type)
1477 int align;
1478 vla_runtime_type_size(pointed_type(type), &align);
1481 static inline int is_null_pointer(SValue *p)
1483 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1484 return 0;
1485 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1486 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1489 static inline int is_integer_btype(int bt)
1491 return (bt == VT_BYTE || bt == VT_SHORT ||
1492 bt == VT_INT || bt == VT_LLONG);
1495 /* check types for comparison or substraction of pointers */
1496 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1498 CType *type1, *type2, tmp_type1, tmp_type2;
1499 int bt1, bt2;
1501 /* null pointers are accepted for all comparisons as gcc */
1502 if (is_null_pointer(p1) || is_null_pointer(p2))
1503 return;
1504 type1 = &p1->type;
1505 type2 = &p2->type;
1506 bt1 = type1->t & VT_BTYPE;
1507 bt2 = type2->t & VT_BTYPE;
1508 /* accept comparison between pointer and integer with a warning */
1509 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1510 if (op != TOK_LOR && op != TOK_LAND )
1511 warning("comparison between pointer and integer");
1512 return;
1515 /* both must be pointers or implicit function pointers */
1516 if (bt1 == VT_PTR) {
1517 type1 = pointed_type(type1);
1518 } else if (bt1 != VT_FUNC)
1519 goto invalid_operands;
1521 if (bt2 == VT_PTR) {
1522 type2 = pointed_type(type2);
1523 } else if (bt2 != VT_FUNC) {
1524 invalid_operands:
1525 error("invalid operands to binary %s", get_tok_str(op, NULL));
1527 if ((type1->t & VT_BTYPE) == VT_VOID ||
1528 (type2->t & VT_BTYPE) == VT_VOID)
1529 return;
1530 tmp_type1 = *type1;
1531 tmp_type2 = *type2;
1532 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1533 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1534 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1535 /* gcc-like error if '-' is used */
1536 if (op == '-')
1537 goto invalid_operands;
1538 else
1539 warning("comparison of distinct pointer types lacks a cast");
1543 /* generic gen_op: handles types problems */
1544 ST_FUNC void gen_op(int op)
1546 int u, t1, t2, bt1, bt2, t;
1547 CType type1;
1549 t1 = vtop[-1].type.t;
1550 t2 = vtop[0].type.t;
1551 bt1 = t1 & VT_BTYPE;
1552 bt2 = t2 & VT_BTYPE;
1554 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1555 /* at least one operand is a pointer */
1556 /* relationnal op: must be both pointers */
1557 if (op >= TOK_ULT && op <= TOK_LOR) {
1558 check_comparison_pointer_types(vtop - 1, vtop, op);
1559 /* pointers are handled are unsigned */
1560 #ifdef TCC_TARGET_X86_64
1561 t = VT_LLONG | VT_UNSIGNED;
1562 #else
1563 t = VT_INT | VT_UNSIGNED;
1564 #endif
1565 goto std_op;
1567 /* if both pointers, then it must be the '-' op */
1568 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1569 int ptr1_is_vla;
1571 ptr1_is_vla = 0;
1572 if (op != '-')
1573 error("cannot use pointers here");
1574 check_comparison_pointer_types(vtop - 1, vtop, op);
1575 /* XXX: check that types are compatible */
1576 if (vtop[-1].type.t & VT_VLA) {
1577 vla_runtime_pointed_size(&vtop[-1].type);
1578 vrott(3);
1579 ptr1_is_vla = 1;
1580 } else {
1581 u = pointed_size(&vtop[-1].type);
1583 gen_opic(op);
1584 /* set to integer type */
1585 #ifdef TCC_TARGET_X86_64
1586 vtop->type.t = VT_LLONG;
1587 #else
1588 vtop->type.t = VT_INT;
1589 #endif
1590 if (ptr1_is_vla)
1591 vswap();
1592 else
1593 vpushi(u);
1594 gen_op(TOK_PDIV);
1595 } else {
1596 /* exactly one pointer : must be '+' or '-'. */
1597 if (op != '-' && op != '+')
1598 error("cannot use pointers here");
1599 /* Put pointer as first operand */
1600 if (bt2 == VT_PTR) {
1601 vswap();
1602 swap(&t1, &t2);
1604 type1 = vtop[-1].type;
1605 type1.t &= ~(VT_ARRAY|VT_VLA);
1606 if (vtop[-1].type.t & VT_VLA)
1607 vla_runtime_pointed_size(&vtop[-1].type);
1608 else {
1609 u = pointed_size(&vtop[-1].type);
1610 if (u < 0)
1611 error("unknown array element size");
1612 #ifdef TCC_TARGET_X86_64
1613 vpushll(u);
1614 #else
1615 /* XXX: cast to int ? (long long case) */
1616 vpushi(u);
1617 #endif
1619 gen_op('*');
1620 #ifdef CONFIG_TCC_BCHECK
1621 /* if evaluating constant expression, no code should be
1622 generated, so no bound check */
1623 if (tcc_state->do_bounds_check && !const_wanted) {
1624 /* if bounded pointers, we generate a special code to
1625 test bounds */
1626 if (op == '-') {
1627 vpushi(0);
1628 vswap();
1629 gen_op('-');
1631 gen_bounded_ptr_add();
1632 } else
1633 #endif
1635 gen_opic(op);
1637 /* put again type if gen_opic() swaped operands */
1638 vtop->type = type1;
1640 } else if (is_float(bt1) || is_float(bt2)) {
1641 /* compute bigger type and do implicit casts */
1642 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1643 t = VT_LDOUBLE;
1644 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1645 t = VT_DOUBLE;
1646 } else {
1647 t = VT_FLOAT;
1649 /* floats can only be used for a few operations */
1650 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1651 (op < TOK_ULT || op > TOK_GT))
1652 error("invalid operands for binary operation");
1653 goto std_op;
1654 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1655 /* cast to biggest op */
1656 t = VT_LLONG;
1657 /* convert to unsigned if it does not fit in a long long */
1658 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1659 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1660 t |= VT_UNSIGNED;
1661 goto std_op;
1662 } else {
1663 /* integer operations */
1664 t = VT_INT;
1665 /* convert to unsigned if it does not fit in an integer */
1666 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1667 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1668 t |= VT_UNSIGNED;
1669 std_op:
1670 /* XXX: currently, some unsigned operations are explicit, so
1671 we modify them here */
1672 if (t & VT_UNSIGNED) {
1673 if (op == TOK_SAR)
1674 op = TOK_SHR;
1675 else if (op == '/')
1676 op = TOK_UDIV;
1677 else if (op == '%')
1678 op = TOK_UMOD;
1679 else if (op == TOK_LT)
1680 op = TOK_ULT;
1681 else if (op == TOK_GT)
1682 op = TOK_UGT;
1683 else if (op == TOK_LE)
1684 op = TOK_ULE;
1685 else if (op == TOK_GE)
1686 op = TOK_UGE;
1688 vswap();
1689 type1.t = t;
1690 gen_cast(&type1);
1691 vswap();
1692 /* special case for shifts and long long: we keep the shift as
1693 an integer */
1694 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1695 type1.t = VT_INT;
1696 gen_cast(&type1);
1697 if (is_float(t))
1698 gen_opif(op);
1699 else
1700 gen_opic(op);
1701 if (op >= TOK_ULT && op <= TOK_GT) {
1702 /* relationnal op: the result is an int */
1703 vtop->type.t = VT_INT;
1704 } else {
1705 vtop->type.t = t;
1710 #ifndef TCC_TARGET_ARM
1711 /* generic itof for unsigned long long case */
1712 static void gen_cvt_itof1(int t)
1714 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1715 (VT_LLONG | VT_UNSIGNED)) {
1717 if (t == VT_FLOAT)
1718 vpush_global_sym(&func_old_type, TOK___floatundisf);
1719 #if LDOUBLE_SIZE != 8
1720 else if (t == VT_LDOUBLE)
1721 vpush_global_sym(&func_old_type, TOK___floatundixf);
1722 #endif
1723 else
1724 vpush_global_sym(&func_old_type, TOK___floatundidf);
1725 vrott(2);
1726 gfunc_call(1);
1727 vpushi(0);
1728 vtop->r = reg_fret(t);
1729 } else {
1730 gen_cvt_itof(t);
1733 #endif
1735 /* generic ftoi for unsigned long long case */
1736 static void gen_cvt_ftoi1(int t)
1738 int st;
1740 if (t == (VT_LLONG | VT_UNSIGNED)) {
1741 /* not handled natively */
1742 st = vtop->type.t & VT_BTYPE;
1743 if (st == VT_FLOAT)
1744 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1745 #if LDOUBLE_SIZE != 8
1746 else if (st == VT_LDOUBLE)
1747 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1748 #endif
1749 else
1750 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1751 vrott(2);
1752 gfunc_call(1);
1753 vpushi(0);
1754 vtop->r = REG_IRET;
1755 vtop->r2 = REG_LRET;
1756 } else {
1757 gen_cvt_ftoi(t);
1761 /* force char or short cast */
1762 static void force_charshort_cast(int t)
1764 int bits, dbt;
1765 dbt = t & VT_BTYPE;
1766 /* XXX: add optimization if lvalue : just change type and offset */
1767 if (dbt == VT_BYTE)
1768 bits = 8;
1769 else
1770 bits = 16;
1771 if (t & VT_UNSIGNED) {
1772 vpushi((1 << bits) - 1);
1773 gen_op('&');
1774 } else {
1775 bits = 32 - bits;
1776 vpushi(bits);
1777 gen_op(TOK_SHL);
1778 /* result must be signed or the SAR is converted to an SHL
1779 This was not the case when "t" was a signed short
1780 and the last value on the stack was an unsigned int */
1781 vtop->type.t &= ~VT_UNSIGNED;
1782 vpushi(bits);
1783 gen_op(TOK_SAR);
1787 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1788 static void gen_cast(CType *type)
1790 int sbt, dbt, sf, df, c, p;
1792 /* special delayed cast for char/short */
1793 /* XXX: in some cases (multiple cascaded casts), it may still
1794 be incorrect */
1795 if (vtop->r & VT_MUSTCAST) {
1796 vtop->r &= ~VT_MUSTCAST;
1797 force_charshort_cast(vtop->type.t);
1800 /* bitfields first get cast to ints */
1801 if (vtop->type.t & VT_BITFIELD) {
1802 gv(RC_INT);
1805 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1806 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1808 if (sbt != dbt) {
1809 sf = is_float(sbt);
1810 df = is_float(dbt);
1811 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1812 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1813 if (c) {
1814 /* constant case: we can do it now */
1815 /* XXX: in ISOC, cannot do it if error in convert */
1816 if (sbt == VT_FLOAT)
1817 vtop->c.ld = vtop->c.f;
1818 else if (sbt == VT_DOUBLE)
1819 vtop->c.ld = vtop->c.d;
1821 if (df) {
1822 if ((sbt & VT_BTYPE) == VT_LLONG) {
1823 if (sbt & VT_UNSIGNED)
1824 vtop->c.ld = vtop->c.ull;
1825 else
1826 vtop->c.ld = vtop->c.ll;
1827 } else if(!sf) {
1828 if (sbt & VT_UNSIGNED)
1829 vtop->c.ld = vtop->c.ui;
1830 else
1831 vtop->c.ld = vtop->c.i;
1834 if (dbt == VT_FLOAT)
1835 vtop->c.f = (float)vtop->c.ld;
1836 else if (dbt == VT_DOUBLE)
1837 vtop->c.d = (double)vtop->c.ld;
1838 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1839 vtop->c.ull = (unsigned long long)vtop->c.ld;
1840 } else if (sf && dbt == VT_BOOL) {
1841 vtop->c.i = (vtop->c.ld != 0);
1842 } else {
1843 if(sf)
1844 vtop->c.ll = (long long)vtop->c.ld;
1845 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1846 vtop->c.ll = vtop->c.ull;
1847 else if (sbt & VT_UNSIGNED)
1848 vtop->c.ll = vtop->c.ui;
1849 #ifdef TCC_TARGET_X86_64
1850 else if (sbt == VT_PTR)
1852 #endif
1853 else if (sbt != VT_LLONG)
1854 vtop->c.ll = vtop->c.i;
1856 if (dbt == (VT_LLONG|VT_UNSIGNED))
1857 vtop->c.ull = vtop->c.ll;
1858 else if (dbt == VT_BOOL)
1859 vtop->c.i = (vtop->c.ll != 0);
1860 else if (dbt != VT_LLONG) {
1861 int s = 0;
1862 if ((dbt & VT_BTYPE) == VT_BYTE)
1863 s = 24;
1864 else if ((dbt & VT_BTYPE) == VT_SHORT)
1865 s = 16;
1867 if(dbt & VT_UNSIGNED)
1868 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1869 else
1870 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1873 } else if (p && dbt == VT_BOOL) {
1874 vtop->r = VT_CONST;
1875 vtop->c.i = 1;
1876 } else if (!nocode_wanted) {
1877 /* non constant case: generate code */
1878 if (sf && df) {
1879 /* convert from fp to fp */
1880 gen_cvt_ftof(dbt);
1881 } else if (df) {
1882 /* convert int to fp */
1883 gen_cvt_itof1(dbt);
1884 } else if (sf) {
1885 /* convert fp to int */
1886 if (dbt == VT_BOOL) {
1887 vpushi(0);
1888 gen_op(TOK_NE);
1889 } else {
1890 /* we handle char/short/etc... with generic code */
1891 if (dbt != (VT_INT | VT_UNSIGNED) &&
1892 dbt != (VT_LLONG | VT_UNSIGNED) &&
1893 dbt != VT_LLONG)
1894 dbt = VT_INT;
1895 gen_cvt_ftoi1(dbt);
1896 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1897 /* additional cast for char/short... */
1898 vtop->type.t = dbt;
1899 gen_cast(type);
1902 #ifndef TCC_TARGET_X86_64
1903 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1904 if ((sbt & VT_BTYPE) != VT_LLONG) {
1905 /* scalar to long long */
1906 /* machine independent conversion */
1907 gv(RC_INT);
1908 /* generate high word */
1909 if (sbt == (VT_INT | VT_UNSIGNED)) {
1910 vpushi(0);
1911 gv(RC_INT);
1912 } else {
1913 if (sbt == VT_PTR) {
1914 /* cast from pointer to int before we apply
1915 shift operation, which pointers don't support*/
1916 gen_cast(&int_type);
1918 gv_dup();
1919 vpushi(31);
1920 gen_op(TOK_SAR);
1922 /* patch second register */
1923 vtop[-1].r2 = vtop->r;
1924 vpop();
1926 #else
1927 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1928 (dbt & VT_BTYPE) == VT_PTR ||
1929 (dbt & VT_BTYPE) == VT_FUNC) {
1930 if ((sbt & VT_BTYPE) != VT_LLONG &&
1931 (sbt & VT_BTYPE) != VT_PTR &&
1932 (sbt & VT_BTYPE) != VT_FUNC) {
1933 /* need to convert from 32bit to 64bit */
1934 int r = gv(RC_INT);
1935 if (sbt != (VT_INT | VT_UNSIGNED)) {
1936 /* x86_64 specific: movslq */
1937 o(0x6348);
1938 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1941 #endif
1942 } else if (dbt == VT_BOOL) {
1943 /* scalar to bool */
1944 vpushi(0);
1945 gen_op(TOK_NE);
1946 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1947 (dbt & VT_BTYPE) == VT_SHORT) {
1948 if (sbt == VT_PTR) {
1949 vtop->type.t = VT_INT;
1950 warning("nonportable conversion from pointer to char/short");
1952 force_charshort_cast(dbt);
1953 } else if ((dbt & VT_BTYPE) == VT_INT) {
1954 /* scalar to int */
1955 if (sbt == VT_LLONG) {
1956 /* from long long: just take low order word */
1957 lexpand();
1958 vpop();
1960 /* if lvalue and single word type, nothing to do because
1961 the lvalue already contains the real type size (see
1962 VT_LVAL_xxx constants) */
1965 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1966 /* if we are casting between pointer types,
1967 we must update the VT_LVAL_xxx size */
1968 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1969 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1971 vtop->type = *type;
1974 /* return type size as known at compile time. Put alignment at 'a' */
1975 ST_FUNC int type_size(CType *type, int *a)
1977 Sym *s;
1978 int bt;
1980 bt = type->t & VT_BTYPE;
1981 if (bt == VT_STRUCT) {
1982 /* struct/union */
1983 s = type->ref;
1984 *a = s->r;
1985 return s->c;
1986 } else if (bt == VT_PTR) {
1987 if (type->t & VT_VLA) {
1988 *a = 1;
1989 return 0;
1990 } else if (type->t & VT_ARRAY) {
1991 int ts;
1993 s = type->ref;
1994 ts = type_size(&s->type, a);
1996 if (ts < 0 && s->c < 0)
1997 ts = -ts;
1999 return ts * s->c;
2000 } else {
2001 *a = PTR_SIZE;
2002 return PTR_SIZE;
2004 } else if (bt == VT_LDOUBLE) {
2005 *a = LDOUBLE_ALIGN;
2006 return LDOUBLE_SIZE;
2007 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2008 #ifdef TCC_TARGET_I386
2009 #ifdef TCC_TARGET_PE
2010 *a = 8;
2011 #else
2012 *a = 4;
2013 #endif
2014 #elif defined(TCC_TARGET_ARM)
2015 #ifdef TCC_ARM_EABI
2016 *a = 8;
2017 #else
2018 *a = 4;
2019 #endif
2020 #else
2021 *a = 8;
2022 #endif
2023 return 8;
2024 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2025 *a = 4;
2026 return 4;
2027 } else if (bt == VT_SHORT) {
2028 *a = 2;
2029 return 2;
2030 } else {
2031 /* char, void, function, _Bool */
2032 *a = 1;
2033 return 1;
2037 /* push type size as known at runtime time on top of value stack. Put
2038 alignment at 'a' */
2039 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2041 if (type->t & VT_VLA) {
2042 Sym *s;
2044 s = type->ref;
2045 vla_runtime_type_size(&s->type, a);
2046 vpushv(s->s);
2047 if ((vtop->r & (VT_SYM|VT_LVAL|VT_VALMASK)) != VT_CONST) {
2048 gv_dup();
2049 vswap();
2050 vpop();
2052 gen_op('*');
2053 } else {
2054 int size;
2056 size = type_size(type, a);
2057 vpushi(size);
2061 /* return the pointed type of t */
2062 static inline CType *pointed_type(CType *type)
2064 return &type->ref->type;
2067 /* modify type so that its it is a pointer to type. */
2068 ST_FUNC void mk_pointer(CType *type)
2070 Sym *s;
2071 s = sym_push(SYM_FIELD, type, 0, -1);
2072 type->t = VT_PTR | (type->t & ~VT_TYPE);
2073 type->ref = s;
2076 /* compare function types. OLD functions match any new functions */
2077 static int is_compatible_func(CType *type1, CType *type2)
2079 Sym *s1, *s2;
2081 s1 = type1->ref;
2082 s2 = type2->ref;
2083 if (!is_compatible_types(&s1->type, &s2->type))
2084 return 0;
2085 /* check func_call */
2086 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2087 return 0;
2088 /* XXX: not complete */
2089 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2090 return 1;
2091 if (s1->c != s2->c)
2092 return 0;
2093 while (s1 != NULL) {
2094 if (s2 == NULL)
2095 return 0;
2096 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2097 return 0;
2098 s1 = s1->next;
2099 s2 = s2->next;
2101 if (s2)
2102 return 0;
2103 return 1;
2106 /* return true if type1 and type2 are the same. If unqualified is
2107 true, qualifiers on the types are ignored.
2109 - enums are not checked as gcc __builtin_types_compatible_p ()
2111 static int compare_types(CType *type1, CType *type2, int unqualified)
2113 int bt1, t1, t2;
2115 t1 = type1->t & VT_TYPE;
2116 t2 = type2->t & VT_TYPE;
2117 if (unqualified) {
2118 /* strip qualifiers before comparing */
2119 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2120 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2122 /* XXX: bitfields ? */
2123 if (t1 != t2)
2124 return 0;
2125 /* test more complicated cases */
2126 bt1 = t1 & VT_BTYPE;
2127 if (bt1 == VT_PTR) {
2128 type1 = pointed_type(type1);
2129 type2 = pointed_type(type2);
2130 return is_compatible_types(type1, type2);
2131 } else if (bt1 == VT_STRUCT) {
2132 return (type1->ref == type2->ref);
2133 } else if (bt1 == VT_FUNC) {
2134 return is_compatible_func(type1, type2);
2135 } else {
2136 return 1;
2140 /* return true if type1 and type2 are exactly the same (including
2141 qualifiers).
2143 static int is_compatible_types(CType *type1, CType *type2)
2145 return compare_types(type1,type2,0);
2148 /* return true if type1 and type2 are the same (ignoring qualifiers).
2150 static int is_compatible_parameter_types(CType *type1, CType *type2)
2152 return compare_types(type1,type2,1);
2155 /* print a type. If 'varstr' is not NULL, then the variable is also
2156 printed in the type */
2157 /* XXX: union */
2158 /* XXX: add array and function pointers */
2159 static void type_to_str(char *buf, int buf_size,
2160 CType *type, const char *varstr)
2162 int bt, v, t;
2163 Sym *s, *sa;
2164 char buf1[256];
2165 const char *tstr;
2167 t = type->t & VT_TYPE;
2168 bt = t & VT_BTYPE;
2169 buf[0] = '\0';
2170 if (t & VT_CONSTANT)
2171 pstrcat(buf, buf_size, "const ");
2172 if (t & VT_VOLATILE)
2173 pstrcat(buf, buf_size, "volatile ");
2174 if (t & VT_UNSIGNED)
2175 pstrcat(buf, buf_size, "unsigned ");
2176 switch(bt) {
2177 case VT_VOID:
2178 tstr = "void";
2179 goto add_tstr;
2180 case VT_BOOL:
2181 tstr = "_Bool";
2182 goto add_tstr;
2183 case VT_BYTE:
2184 tstr = "char";
2185 goto add_tstr;
2186 case VT_SHORT:
2187 tstr = "short";
2188 goto add_tstr;
2189 case VT_INT:
2190 tstr = "int";
2191 goto add_tstr;
2192 case VT_LONG:
2193 tstr = "long";
2194 goto add_tstr;
2195 case VT_LLONG:
2196 tstr = "long long";
2197 goto add_tstr;
2198 case VT_FLOAT:
2199 tstr = "float";
2200 goto add_tstr;
2201 case VT_DOUBLE:
2202 tstr = "double";
2203 goto add_tstr;
2204 case VT_LDOUBLE:
2205 tstr = "long double";
2206 add_tstr:
2207 pstrcat(buf, buf_size, tstr);
2208 break;
2209 case VT_ENUM:
2210 case VT_STRUCT:
2211 if (bt == VT_STRUCT)
2212 tstr = "struct ";
2213 else
2214 tstr = "enum ";
2215 pstrcat(buf, buf_size, tstr);
2216 v = type->ref->v & ~SYM_STRUCT;
2217 if (v >= SYM_FIRST_ANOM)
2218 pstrcat(buf, buf_size, "<anonymous>");
2219 else
2220 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2221 break;
2222 case VT_FUNC:
2223 s = type->ref;
2224 type_to_str(buf, buf_size, &s->type, varstr);
2225 pstrcat(buf, buf_size, "(");
2226 sa = s->next;
2227 while (sa != NULL) {
2228 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2229 pstrcat(buf, buf_size, buf1);
2230 sa = sa->next;
2231 if (sa)
2232 pstrcat(buf, buf_size, ", ");
2234 pstrcat(buf, buf_size, ")");
2235 goto no_var;
2236 case VT_PTR:
2237 s = type->ref;
2238 pstrcpy(buf1, sizeof(buf1), "*");
2239 if (varstr)
2240 pstrcat(buf1, sizeof(buf1), varstr);
2241 type_to_str(buf, buf_size, &s->type, buf1);
2242 goto no_var;
2244 if (varstr) {
2245 pstrcat(buf, buf_size, " ");
2246 pstrcat(buf, buf_size, varstr);
2248 no_var: ;
2251 /* verify type compatibility to store vtop in 'dt' type, and generate
2252 casts if needed. */
2253 static void gen_assign_cast(CType *dt)
2255 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2256 char buf1[256], buf2[256];
2257 int dbt, sbt;
2259 st = &vtop->type; /* source type */
2260 dbt = dt->t & VT_BTYPE;
2261 sbt = st->t & VT_BTYPE;
2262 if (dt->t & VT_CONSTANT)
2263 warning("assignment of read-only location");
2264 switch(dbt) {
2265 case VT_PTR:
2266 /* special cases for pointers */
2267 /* '0' can also be a pointer */
2268 if (is_null_pointer(vtop))
2269 goto type_ok;
2270 /* accept implicit pointer to integer cast with warning */
2271 if (is_integer_btype(sbt)) {
2272 warning("assignment makes pointer from integer without a cast");
2273 goto type_ok;
2275 type1 = pointed_type(dt);
2276 /* a function is implicitely a function pointer */
2277 if (sbt == VT_FUNC) {
2278 if ((type1->t & VT_BTYPE) != VT_VOID &&
2279 !is_compatible_types(pointed_type(dt), st))
2280 warning("assignment from incompatible pointer type");
2281 goto type_ok;
2283 if (sbt != VT_PTR)
2284 goto error;
2285 type2 = pointed_type(st);
2286 if ((type1->t & VT_BTYPE) == VT_VOID ||
2287 (type2->t & VT_BTYPE) == VT_VOID) {
2288 /* void * can match anything */
2289 } else {
2290 /* exact type match, except for unsigned */
2291 tmp_type1 = *type1;
2292 tmp_type2 = *type2;
2293 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2294 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2295 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2296 warning("assignment from incompatible pointer type");
2298 /* check const and volatile */
2299 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2300 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2301 warning("assignment discards qualifiers from pointer target type");
2302 break;
2303 case VT_BYTE:
2304 case VT_SHORT:
2305 case VT_INT:
2306 case VT_LLONG:
2307 if (sbt == VT_PTR || sbt == VT_FUNC) {
2308 warning("assignment makes integer from pointer without a cast");
2310 /* XXX: more tests */
2311 break;
2312 case VT_STRUCT:
2313 tmp_type1 = *dt;
2314 tmp_type2 = *st;
2315 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2316 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2317 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2318 error:
2319 type_to_str(buf1, sizeof(buf1), st, NULL);
2320 type_to_str(buf2, sizeof(buf2), dt, NULL);
2321 error("cannot cast '%s' to '%s'", buf1, buf2);
2323 break;
2325 type_ok:
2326 gen_cast(dt);
2329 /* store vtop in lvalue pushed on stack */
2330 ST_FUNC void vstore(void)
2332 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2334 ft = vtop[-1].type.t;
2335 sbt = vtop->type.t & VT_BTYPE;
2336 dbt = ft & VT_BTYPE;
2337 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2338 (sbt == VT_INT && dbt == VT_SHORT)) {
2339 /* optimize char/short casts */
2340 delayed_cast = VT_MUSTCAST;
2341 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2342 /* XXX: factorize */
2343 if (ft & VT_CONSTANT)
2344 warning("assignment of read-only location");
2345 } else {
2346 delayed_cast = 0;
2347 if (!(ft & VT_BITFIELD))
2348 gen_assign_cast(&vtop[-1].type);
2351 if (sbt == VT_STRUCT) {
2352 /* if structure, only generate pointer */
2353 /* structure assignment : generate memcpy */
2354 /* XXX: optimize if small size */
2355 if (!nocode_wanted) {
2356 size = type_size(&vtop->type, &align);
2358 /* destination */
2359 vswap();
2360 vtop->type.t = VT_PTR;
2361 gaddrof();
2363 /* address of memcpy() */
2364 #ifdef TCC_ARM_EABI
2365 if(!(align & 7))
2366 vpush_global_sym(&func_old_type, TOK_memcpy8);
2367 else if(!(align & 3))
2368 vpush_global_sym(&func_old_type, TOK_memcpy4);
2369 else
2370 #endif
2371 vpush_global_sym(&func_old_type, TOK_memcpy);
2373 vswap();
2374 /* source */
2375 vpushv(vtop - 2);
2376 vtop->type.t = VT_PTR;
2377 gaddrof();
2378 /* type size */
2379 vpushi(size);
2380 gfunc_call(3);
2381 } else {
2382 vswap();
2383 vpop();
2385 /* leave source on stack */
2386 } else if (ft & VT_BITFIELD) {
2387 /* bitfield store handling */
2388 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2389 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2390 /* remove bit field info to avoid loops */
2391 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2393 /* duplicate source into other register */
2394 gv_dup();
2395 vswap();
2396 vrott(3);
2398 if((ft & VT_BTYPE) == VT_BOOL) {
2399 gen_cast(&vtop[-1].type);
2400 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2403 /* duplicate destination */
2404 vdup();
2405 vtop[-1] = vtop[-2];
2407 /* mask and shift source */
2408 if((ft & VT_BTYPE) != VT_BOOL) {
2409 if((ft & VT_BTYPE) == VT_LLONG) {
2410 vpushll((1ULL << bit_size) - 1ULL);
2411 } else {
2412 vpushi((1 << bit_size) - 1);
2414 gen_op('&');
2416 vpushi(bit_pos);
2417 gen_op(TOK_SHL);
2418 /* load destination, mask and or with source */
2419 vswap();
2420 if((ft & VT_BTYPE) == VT_LLONG) {
2421 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2422 } else {
2423 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2425 gen_op('&');
2426 gen_op('|');
2427 /* store result */
2428 vstore();
2430 /* pop off shifted source from "duplicate source..." above */
2431 vpop();
2433 } else {
2434 #ifdef CONFIG_TCC_BCHECK
2435 /* bound check case */
2436 if (vtop[-1].r & VT_MUSTBOUND) {
2437 vswap();
2438 gbound();
2439 vswap();
2441 #endif
2442 if (!nocode_wanted) {
2443 rc = RC_INT;
2444 if (is_float(ft)) {
2445 rc = RC_FLOAT;
2446 #ifdef TCC_TARGET_X86_64
2447 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2448 rc = RC_ST0;
2450 #endif
2452 r = gv(rc); /* generate value */
2453 /* if lvalue was saved on stack, must read it */
2454 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2455 SValue sv;
2456 t = get_reg(RC_INT);
2457 #ifdef TCC_TARGET_X86_64
2458 sv.type.t = VT_PTR;
2459 #else
2460 sv.type.t = VT_INT;
2461 #endif
2462 sv.r = VT_LOCAL | VT_LVAL;
2463 sv.c.ul = vtop[-1].c.ul;
2464 load(t, &sv);
2465 vtop[-1].r = t | VT_LVAL;
2467 store(r, vtop - 1);
2468 #ifndef TCC_TARGET_X86_64
2469 /* two word case handling : store second register at word + 4 */
2470 if ((ft & VT_BTYPE) == VT_LLONG) {
2471 vswap();
2472 /* convert to int to increment easily */
2473 vtop->type.t = VT_INT;
2474 gaddrof();
2475 vpushi(4);
2476 gen_op('+');
2477 vtop->r |= VT_LVAL;
2478 vswap();
2479 /* XXX: it works because r2 is spilled last ! */
2480 store(vtop->r2, vtop - 1);
2482 #endif
2484 vswap();
2485 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2486 vtop->r |= delayed_cast;
2490 /* post defines POST/PRE add. c is the token ++ or -- */
2491 ST_FUNC void inc(int post, int c)
2493 test_lvalue();
2494 vdup(); /* save lvalue */
2495 if (post) {
2496 gv_dup(); /* duplicate value */
2497 vrotb(3);
2498 vrotb(3);
2500 /* add constant */
2501 vpushi(c - TOK_MID);
2502 gen_op('+');
2503 vstore(); /* store value */
2504 if (post)
2505 vpop(); /* if post op, return saved value */
2508 /* Parse GNUC __attribute__ extension. Currently, the following
2509 extensions are recognized:
2510 - aligned(n) : set data/function alignment.
2511 - packed : force data alignment to 1
2512 - section(x) : generate data/code in this section.
2513 - unused : currently ignored, but may be used someday.
2514 - regparm(n) : pass function parameters in registers (i386 only)
2516 static void parse_attribute(AttributeDef *ad)
2518 int t, n;
2520 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2521 next();
2522 skip('(');
2523 skip('(');
2524 while (tok != ')') {
2525 if (tok < TOK_IDENT)
2526 expect("attribute name");
2527 t = tok;
2528 next();
2529 switch(t) {
2530 case TOK_SECTION1:
2531 case TOK_SECTION2:
2532 skip('(');
2533 if (tok != TOK_STR)
2534 expect("section name");
2535 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2536 next();
2537 skip(')');
2538 break;
2539 case TOK_ALIAS1:
2540 case TOK_ALIAS2:
2541 skip('(');
2542 if (tok != TOK_STR)
2543 expect("alias(\"target\")");
2544 ad->alias_target = /* save string as token, for later */
2545 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2546 next();
2547 skip(')');
2548 break;
2549 case TOK_ALIGNED1:
2550 case TOK_ALIGNED2:
2551 if (tok == '(') {
2552 next();
2553 n = expr_const();
2554 if (n <= 0 || (n & (n - 1)) != 0)
2555 error("alignment must be a positive power of two");
2556 skip(')');
2557 } else {
2558 n = MAX_ALIGN;
2560 ad->aligned = n;
2561 break;
2562 case TOK_PACKED1:
2563 case TOK_PACKED2:
2564 ad->packed = 1;
2565 break;
2566 case TOK_WEAK1:
2567 case TOK_WEAK2:
2568 ad->weak = 1;
2569 break;
2570 case TOK_UNUSED1:
2571 case TOK_UNUSED2:
2572 /* currently, no need to handle it because tcc does not
2573 track unused objects */
2574 break;
2575 case TOK_NORETURN1:
2576 case TOK_NORETURN2:
2577 /* currently, no need to handle it because tcc does not
2578 track unused objects */
2579 break;
2580 case TOK_CDECL1:
2581 case TOK_CDECL2:
2582 case TOK_CDECL3:
2583 ad->func_call = FUNC_CDECL;
2584 break;
2585 case TOK_STDCALL1:
2586 case TOK_STDCALL2:
2587 case TOK_STDCALL3:
2588 ad->func_call = FUNC_STDCALL;
2589 break;
2590 #ifdef TCC_TARGET_I386
2591 case TOK_REGPARM1:
2592 case TOK_REGPARM2:
2593 skip('(');
2594 n = expr_const();
2595 if (n > 3)
2596 n = 3;
2597 else if (n < 0)
2598 n = 0;
2599 if (n > 0)
2600 ad->func_call = FUNC_FASTCALL1 + n - 1;
2601 skip(')');
2602 break;
2603 case TOK_FASTCALL1:
2604 case TOK_FASTCALL2:
2605 case TOK_FASTCALL3:
2606 ad->func_call = FUNC_FASTCALLW;
2607 break;
2608 #endif
2609 case TOK_MODE:
2610 skip('(');
2611 switch(tok) {
2612 case TOK_MODE_DI:
2613 ad->mode = VT_LLONG + 1;
2614 break;
2615 case TOK_MODE_HI:
2616 ad->mode = VT_SHORT + 1;
2617 break;
2618 case TOK_MODE_SI:
2619 ad->mode = VT_INT + 1;
2620 break;
2621 default:
2622 warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2623 break;
2625 next();
2626 skip(')');
2627 break;
2628 case TOK_DLLEXPORT:
2629 ad->func_export = 1;
2630 break;
2631 case TOK_DLLIMPORT:
2632 ad->func_import = 1;
2633 break;
2634 default:
2635 if (tcc_state->warn_unsupported)
2636 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2637 /* skip parameters */
2638 if (tok == '(') {
2639 int parenthesis = 0;
2640 do {
2641 if (tok == '(')
2642 parenthesis++;
2643 else if (tok == ')')
2644 parenthesis--;
2645 next();
2646 } while (parenthesis && tok != -1);
2648 break;
2650 if (tok != ',')
2651 break;
2652 next();
2654 skip(')');
2655 skip(')');
2659 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2660 static void struct_decl(CType *type, int u)
2662 int a, v, size, align, maxalign, c, offset;
2663 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2664 Sym *s, *ss, *ass, **ps;
2665 AttributeDef ad;
2666 CType type1, btype;
2668 a = tok; /* save decl type */
2669 next();
2670 if (tok != '{') {
2671 v = tok;
2672 next();
2673 /* struct already defined ? return it */
2674 if (v < TOK_IDENT)
2675 expect("struct/union/enum name");
2676 s = struct_find(v);
2677 if (s) {
2678 if (s->type.t != a)
2679 error("invalid type");
2680 goto do_decl;
2682 } else {
2683 v = anon_sym++;
2685 type1.t = a;
2686 /* we put an undefined size for struct/union */
2687 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2688 s->r = 0; /* default alignment is zero as gcc */
2689 /* put struct/union/enum name in type */
2690 do_decl:
2691 type->t = u;
2692 type->ref = s;
2694 if (tok == '{') {
2695 next();
2696 if (s->c != -1)
2697 error("struct/union/enum already defined");
2698 /* cannot be empty */
2699 c = 0;
2700 /* non empty enums are not allowed */
2701 if (a == TOK_ENUM) {
2702 for(;;) {
2703 v = tok;
2704 if (v < TOK_UIDENT)
2705 expect("identifier");
2706 next();
2707 if (tok == '=') {
2708 next();
2709 c = expr_const();
2711 /* enum symbols have static storage */
2712 ss = sym_push(v, &int_type, VT_CONST, c);
2713 ss->type.t |= VT_STATIC;
2714 if (tok != ',')
2715 break;
2716 next();
2717 c++;
2718 /* NOTE: we accept a trailing comma */
2719 if (tok == '}')
2720 break;
2722 skip('}');
2723 } else {
2724 maxalign = 1;
2725 ps = &s->next;
2726 prevbt = VT_INT;
2727 bit_pos = 0;
2728 offset = 0;
2729 while (tok != '}') {
2730 parse_btype(&btype, &ad);
2731 while (1) {
2732 bit_size = -1;
2733 v = 0;
2734 type1 = btype;
2735 if (tok != ':') {
2736 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2737 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2738 expect("identifier");
2739 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2740 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2741 error("invalid type for '%s'",
2742 get_tok_str(v, NULL));
2744 if (tok == ':') {
2745 next();
2746 bit_size = expr_const();
2747 /* XXX: handle v = 0 case for messages */
2748 if (bit_size < 0)
2749 error("negative width in bit-field '%s'",
2750 get_tok_str(v, NULL));
2751 if (v && bit_size == 0)
2752 error("zero width for bit-field '%s'",
2753 get_tok_str(v, NULL));
2755 size = type_size(&type1, &align);
2756 if (ad.aligned) {
2757 if (align < ad.aligned)
2758 align = ad.aligned;
2759 } else if (ad.packed) {
2760 align = 1;
2761 } else if (*tcc_state->pack_stack_ptr) {
2762 if (align > *tcc_state->pack_stack_ptr)
2763 align = *tcc_state->pack_stack_ptr;
2765 lbit_pos = 0;
2766 if (bit_size >= 0) {
2767 bt = type1.t & VT_BTYPE;
2768 if (bt != VT_INT &&
2769 bt != VT_BYTE &&
2770 bt != VT_SHORT &&
2771 bt != VT_BOOL &&
2772 bt != VT_ENUM &&
2773 bt != VT_LLONG)
2774 error("bitfields must have scalar type");
2775 bsize = size * 8;
2776 if (bit_size > bsize) {
2777 error("width of '%s' exceeds its type",
2778 get_tok_str(v, NULL));
2779 } else if (bit_size == bsize) {
2780 /* no need for bit fields */
2781 bit_pos = 0;
2782 } else if (bit_size == 0) {
2783 /* XXX: what to do if only padding in a
2784 structure ? */
2785 /* zero size: means to pad */
2786 bit_pos = 0;
2787 } else {
2788 /* we do not have enough room ?
2789 did the type change?
2790 is it a union? */
2791 if ((bit_pos + bit_size) > bsize ||
2792 bt != prevbt || a == TOK_UNION)
2793 bit_pos = 0;
2794 lbit_pos = bit_pos;
2795 /* XXX: handle LSB first */
2796 type1.t |= VT_BITFIELD |
2797 (bit_pos << VT_STRUCT_SHIFT) |
2798 (bit_size << (VT_STRUCT_SHIFT + 6));
2799 bit_pos += bit_size;
2801 prevbt = bt;
2802 } else {
2803 bit_pos = 0;
2805 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2806 /* add new memory data only if starting
2807 bit field */
2808 if (lbit_pos == 0) {
2809 if (a == TOK_STRUCT) {
2810 c = (c + align - 1) & -align;
2811 offset = c;
2812 if (size > 0)
2813 c += size;
2814 } else {
2815 offset = 0;
2816 if (size > c)
2817 c = size;
2819 if (align > maxalign)
2820 maxalign = align;
2822 #if 0
2823 printf("add field %s offset=%d",
2824 get_tok_str(v, NULL), offset);
2825 if (type1.t & VT_BITFIELD) {
2826 printf(" pos=%d size=%d",
2827 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2828 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2830 printf("\n");
2831 #endif
2833 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2834 ass = type1.ref;
2835 while ((ass = ass->next) != NULL) {
2836 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2837 *ps = ss;
2838 ps = &ss->next;
2840 } else if (v) {
2841 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2842 *ps = ss;
2843 ps = &ss->next;
2845 if (tok == ';' || tok == TOK_EOF)
2846 break;
2847 skip(',');
2849 skip(';');
2851 skip('}');
2852 /* store size and alignment */
2853 s->c = (c + maxalign - 1) & -maxalign;
2854 s->r = maxalign;
2859 /* return 0 if no type declaration. otherwise, return the basic type
2860 and skip it.
2862 static int parse_btype(CType *type, AttributeDef *ad)
2864 int t, u, type_found, typespec_found, typedef_found;
2865 Sym *s;
2866 CType type1;
2868 memset(ad, 0, sizeof(AttributeDef));
2869 type_found = 0;
2870 typespec_found = 0;
2871 typedef_found = 0;
2872 t = 0;
2873 while(1) {
2874 switch(tok) {
2875 case TOK_EXTENSION:
2876 /* currently, we really ignore extension */
2877 next();
2878 continue;
2880 /* basic types */
2881 case TOK_CHAR:
2882 u = VT_BYTE;
2883 basic_type:
2884 next();
2885 basic_type1:
2886 if ((t & VT_BTYPE) != 0)
2887 error("too many basic types");
2888 t |= u;
2889 typespec_found = 1;
2890 break;
2891 case TOK_VOID:
2892 u = VT_VOID;
2893 goto basic_type;
2894 case TOK_SHORT:
2895 u = VT_SHORT;
2896 goto basic_type;
2897 case TOK_INT:
2898 next();
2899 typespec_found = 1;
2900 break;
2901 case TOK_LONG:
2902 next();
2903 if ((t & VT_BTYPE) == VT_DOUBLE) {
2904 #ifndef TCC_TARGET_PE
2905 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2906 #endif
2907 } else if ((t & VT_BTYPE) == VT_LONG) {
2908 t = (t & ~VT_BTYPE) | VT_LLONG;
2909 } else {
2910 u = VT_LONG;
2911 goto basic_type1;
2913 break;
2914 case TOK_BOOL:
2915 u = VT_BOOL;
2916 goto basic_type;
2917 case TOK_FLOAT:
2918 u = VT_FLOAT;
2919 goto basic_type;
2920 case TOK_DOUBLE:
2921 next();
2922 if ((t & VT_BTYPE) == VT_LONG) {
2923 #ifdef TCC_TARGET_PE
2924 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2925 #else
2926 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2927 #endif
2928 } else {
2929 u = VT_DOUBLE;
2930 goto basic_type1;
2932 break;
2933 case TOK_ENUM:
2934 struct_decl(&type1, VT_ENUM);
2935 basic_type2:
2936 u = type1.t;
2937 type->ref = type1.ref;
2938 goto basic_type1;
2939 case TOK_STRUCT:
2940 case TOK_UNION:
2941 struct_decl(&type1, VT_STRUCT);
2942 goto basic_type2;
2944 /* type modifiers */
2945 case TOK_CONST1:
2946 case TOK_CONST2:
2947 case TOK_CONST3:
2948 t |= VT_CONSTANT;
2949 next();
2950 break;
2951 case TOK_VOLATILE1:
2952 case TOK_VOLATILE2:
2953 case TOK_VOLATILE3:
2954 t |= VT_VOLATILE;
2955 next();
2956 break;
2957 case TOK_SIGNED1:
2958 case TOK_SIGNED2:
2959 case TOK_SIGNED3:
2960 typespec_found = 1;
2961 t |= VT_SIGNED;
2962 next();
2963 break;
2964 case TOK_REGISTER:
2965 case TOK_AUTO:
2966 case TOK_RESTRICT1:
2967 case TOK_RESTRICT2:
2968 case TOK_RESTRICT3:
2969 next();
2970 break;
2971 case TOK_UNSIGNED:
2972 t |= VT_UNSIGNED;
2973 next();
2974 typespec_found = 1;
2975 break;
2977 /* storage */
2978 case TOK_EXTERN:
2979 t |= VT_EXTERN;
2980 next();
2981 break;
2982 case TOK_STATIC:
2983 t |= VT_STATIC;
2984 next();
2985 break;
2986 case TOK_TYPEDEF:
2987 t |= VT_TYPEDEF;
2988 next();
2989 break;
2990 case TOK_INLINE1:
2991 case TOK_INLINE2:
2992 case TOK_INLINE3:
2993 t |= VT_INLINE;
2994 next();
2995 break;
2997 /* GNUC attribute */
2998 case TOK_ATTRIBUTE1:
2999 case TOK_ATTRIBUTE2:
3000 parse_attribute(ad);
3001 if (ad->mode) {
3002 u = ad->mode -1;
3003 t = (t & ~VT_BTYPE) | u;
3005 break;
3006 /* GNUC typeof */
3007 case TOK_TYPEOF1:
3008 case TOK_TYPEOF2:
3009 case TOK_TYPEOF3:
3010 next();
3011 parse_expr_type(&type1);
3012 /* remove all storage modifiers except typedef */
3013 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3014 goto basic_type2;
3015 default:
3016 if (typespec_found || typedef_found)
3017 goto the_end;
3018 s = sym_find(tok);
3019 if (!s || !(s->type.t & VT_TYPEDEF))
3020 goto the_end;
3021 typedef_found = 1;
3022 t |= (s->type.t & ~VT_TYPEDEF);
3023 type->ref = s->type.ref;
3024 if (s->r) {
3025 /* get attributes from typedef */
3026 if (0 == ad->aligned)
3027 ad->aligned = FUNC_ALIGN(s->r);
3028 if (0 == ad->func_call)
3029 ad->func_call = FUNC_CALL(s->r);
3030 ad->packed |= FUNC_PACKED(s->r);
3032 next();
3033 typespec_found = 1;
3034 break;
3036 type_found = 1;
3038 the_end:
3039 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3040 error("signed and unsigned modifier");
3041 if (tcc_state->char_is_unsigned) {
3042 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3043 t |= VT_UNSIGNED;
3045 t &= ~VT_SIGNED;
3047 /* long is never used as type */
3048 if ((t & VT_BTYPE) == VT_LONG)
3049 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3050 t = (t & ~VT_BTYPE) | VT_INT;
3051 #else
3052 t = (t & ~VT_BTYPE) | VT_LLONG;
3053 #endif
3054 type->t = t;
3055 return type_found;
3058 /* convert a function parameter type (array to pointer and function to
3059 function pointer) */
3060 static inline void convert_parameter_type(CType *pt)
3062 /* remove const and volatile qualifiers (XXX: const could be used
3063 to indicate a const function parameter */
3064 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3065 /* array must be transformed to pointer according to ANSI C */
3066 pt->t &= ~VT_ARRAY;
3067 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3068 mk_pointer(pt);
3072 ST_FUNC void parse_asm_str(CString *astr)
3074 skip('(');
3075 /* read the string */
3076 if (tok != TOK_STR)
3077 expect("string constant");
3078 cstr_new(astr);
3079 while (tok == TOK_STR) {
3080 /* XXX: add \0 handling too ? */
3081 cstr_cat(astr, tokc.cstr->data);
3082 next();
3084 cstr_ccat(astr, '\0');
3087 /* Parse an asm label and return the label
3088 * Don't forget to free the CString in the caller! */
3089 static void asm_label_instr(CString *astr)
3091 next();
3092 parse_asm_str(astr);
3093 skip(')');
3094 #ifdef ASM_DEBUG
3095 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3096 #endif
3099 static void post_type(CType *type, AttributeDef *ad)
3101 int n, l, t1, arg_size, align;
3102 Sym **plast, *s, *first;
3103 AttributeDef ad1;
3104 CType pt;
3106 if (tok == '(') {
3107 /* function declaration */
3108 next();
3109 l = 0;
3110 first = NULL;
3111 plast = &first;
3112 arg_size = 0;
3113 if (tok != ')') {
3114 for(;;) {
3115 /* read param name and compute offset */
3116 if (l != FUNC_OLD) {
3117 if (!parse_btype(&pt, &ad1)) {
3118 if (l) {
3119 error("invalid type");
3120 } else {
3121 l = FUNC_OLD;
3122 goto old_proto;
3125 l = FUNC_NEW;
3126 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3127 break;
3128 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3129 if ((pt.t & VT_BTYPE) == VT_VOID)
3130 error("parameter declared as void");
3131 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3132 } else {
3133 old_proto:
3134 n = tok;
3135 if (n < TOK_UIDENT)
3136 expect("identifier");
3137 pt.t = VT_INT;
3138 next();
3140 convert_parameter_type(&pt);
3141 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3142 *plast = s;
3143 plast = &s->next;
3144 if (tok == ')')
3145 break;
3146 skip(',');
3147 if (l == FUNC_NEW && tok == TOK_DOTS) {
3148 l = FUNC_ELLIPSIS;
3149 next();
3150 break;
3154 /* if no parameters, then old type prototype */
3155 if (l == 0)
3156 l = FUNC_OLD;
3157 skip(')');
3158 /* NOTE: const is ignored in returned type as it has a special
3159 meaning in gcc / C++ */
3160 type->t &= ~VT_CONSTANT;
3161 /* some ancient pre-K&R C allows a function to return an array
3162 and the array brackets to be put after the arguments, such
3163 that "int c()[]" means something like "int[] c()" */
3164 if (tok == '[') {
3165 next();
3166 skip(']'); /* only handle simple "[]" */
3167 type->t |= VT_PTR;
3169 /* we push a anonymous symbol which will contain the function prototype */
3170 ad->func_args = arg_size;
3171 t1 = type->t & VT_STORAGE;
3172 type->t &= ~VT_STORAGE;
3173 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3174 s->next = first;
3175 type->t = t1 | VT_FUNC;
3176 type->ref = s;
3177 } else if (tok == '[') {
3178 SValue *last_vtop = NULL;
3180 /* array definition */
3181 next();
3182 if (tok == TOK_RESTRICT1)
3183 next();
3184 n = -1;
3185 if (tok != ']') {
3186 gexpr();
3187 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3188 n = vtop->c.i;
3189 last_vtop = vtop;
3190 if (n < 0)
3191 error("invalid array size");
3192 } else {
3193 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3194 error("size of variable length array should be an integer");
3195 type->t |= VT_VLA;
3196 last_vtop = vtop;
3199 skip(']');
3200 /* parse next post type */
3201 t1 = type->t & (VT_STORAGE|VT_VLA);
3202 type->t &= ~(VT_STORAGE|VT_VLA);
3203 post_type(type, ad);
3204 t1 |= type->t & VT_VLA;
3206 /* we push an anonymous symbol which will contain the array
3207 element type */
3208 s = sym_push(SYM_FIELD, type, 0, n);
3209 if (t1 & VT_VLA) {
3210 s->s = last_vtop; // That's ok, we don't need n with VLA
3211 } else {
3212 if (n >= 0)
3213 vpop();
3215 type->t = t1 | VT_ARRAY | VT_PTR;
3216 type->ref = s;
3220 /* Parse a type declaration (except basic type), and return the type
3221 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3222 expected. 'type' should contain the basic type. 'ad' is the
3223 attribute definition of the basic type. It can be modified by
3224 type_decl().
3226 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3228 Sym *s;
3229 CType type1, *type2;
3230 int qualifiers;
3232 while (tok == '*') {
3233 qualifiers = 0;
3234 redo:
3235 next();
3236 switch(tok) {
3237 case TOK_CONST1:
3238 case TOK_CONST2:
3239 case TOK_CONST3:
3240 qualifiers |= VT_CONSTANT;
3241 goto redo;
3242 case TOK_VOLATILE1:
3243 case TOK_VOLATILE2:
3244 case TOK_VOLATILE3:
3245 qualifiers |= VT_VOLATILE;
3246 goto redo;
3247 case TOK_RESTRICT1:
3248 case TOK_RESTRICT2:
3249 case TOK_RESTRICT3:
3250 goto redo;
3252 mk_pointer(type);
3253 type->t |= qualifiers;
3256 /* XXX: clarify attribute handling */
3257 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3258 parse_attribute(ad);
3260 /* recursive type */
3261 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3262 type1.t = 0; /* XXX: same as int */
3263 if (tok == '(') {
3264 next();
3265 /* XXX: this is not correct to modify 'ad' at this point, but
3266 the syntax is not clear */
3267 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3268 parse_attribute(ad);
3269 type_decl(&type1, ad, v, td);
3270 skip(')');
3271 } else {
3272 /* type identifier */
3273 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3274 *v = tok;
3275 next();
3276 } else {
3277 if (!(td & TYPE_ABSTRACT))
3278 expect("identifier");
3279 *v = 0;
3282 post_type(type, ad);
3283 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3284 parse_attribute(ad);
3286 if (!type1.t)
3287 return;
3288 /* append type at the end of type1 */
3289 type2 = &type1;
3290 for(;;) {
3291 s = type2->ref;
3292 type2 = &s->type;
3293 if (!type2->t) {
3294 *type2 = *type;
3295 break;
3298 *type = type1;
3301 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3302 ST_FUNC int lvalue_type(int t)
3304 int bt, r;
3305 r = VT_LVAL;
3306 bt = t & VT_BTYPE;
3307 if (bt == VT_BYTE || bt == VT_BOOL)
3308 r |= VT_LVAL_BYTE;
3309 else if (bt == VT_SHORT)
3310 r |= VT_LVAL_SHORT;
3311 else
3312 return r;
3313 if (t & VT_UNSIGNED)
3314 r |= VT_LVAL_UNSIGNED;
3315 return r;
3318 /* indirection with full error checking and bound check */
3319 ST_FUNC void indir(void)
3321 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3322 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3323 return;
3324 expect("pointer");
3326 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3327 gv(RC_INT);
3328 vtop->type = *pointed_type(&vtop->type);
3329 /* Arrays and functions are never lvalues */
3330 if (!(vtop->type.t & VT_ARRAY)
3331 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3332 vtop->r |= lvalue_type(vtop->type.t);
3333 /* if bound checking, the referenced pointer must be checked */
3334 #ifdef CONFIG_TCC_BCHECK
3335 if (tcc_state->do_bounds_check)
3336 vtop->r |= VT_MUSTBOUND;
3337 #endif
3341 /* pass a parameter to a function and do type checking and casting */
3342 static void gfunc_param_typed(Sym *func, Sym *arg)
3344 int func_type;
3345 CType type;
3347 func_type = func->c;
3348 if (func_type == FUNC_OLD ||
3349 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3350 /* default casting : only need to convert float to double */
3351 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3352 type.t = VT_DOUBLE;
3353 gen_cast(&type);
3355 } else if (arg == NULL) {
3356 error("too many arguments to function");
3357 } else {
3358 type = arg->type;
3359 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3360 gen_assign_cast(&type);
3364 /* parse an expression of the form '(type)' or '(expr)' and return its
3365 type */
3366 static void parse_expr_type(CType *type)
3368 int n;
3369 AttributeDef ad;
3371 skip('(');
3372 if (parse_btype(type, &ad)) {
3373 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3374 } else {
3375 expr_type(type);
3377 skip(')');
3380 static void parse_type(CType *type)
3382 AttributeDef ad;
3383 int n;
3385 if (!parse_btype(type, &ad)) {
3386 expect("type");
3388 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3391 static void vpush_tokc(int t)
3393 CType type;
3394 type.t = t;
3395 type.ref = 0;
3396 vsetc(&type, VT_CONST, &tokc);
3399 ST_FUNC void unary(void)
3401 int n, t, align, size, r, sizeof_caller;
3402 CType type;
3403 Sym *s;
3404 AttributeDef ad;
3405 static int in_sizeof = 0;
3407 sizeof_caller = in_sizeof;
3408 in_sizeof = 0;
3409 /* XXX: GCC 2.95.3 does not generate a table although it should be
3410 better here */
3411 tok_next:
3412 switch(tok) {
3413 case TOK_EXTENSION:
3414 next();
3415 goto tok_next;
3416 case TOK_CINT:
3417 case TOK_CCHAR:
3418 case TOK_LCHAR:
3419 vpushi(tokc.i);
3420 next();
3421 break;
3422 case TOK_CUINT:
3423 vpush_tokc(VT_INT | VT_UNSIGNED);
3424 next();
3425 break;
3426 case TOK_CLLONG:
3427 vpush_tokc(VT_LLONG);
3428 next();
3429 break;
3430 case TOK_CULLONG:
3431 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3432 next();
3433 break;
3434 case TOK_CFLOAT:
3435 vpush_tokc(VT_FLOAT);
3436 next();
3437 break;
3438 case TOK_CDOUBLE:
3439 vpush_tokc(VT_DOUBLE);
3440 next();
3441 break;
3442 case TOK_CLDOUBLE:
3443 vpush_tokc(VT_LDOUBLE);
3444 next();
3445 break;
3446 case TOK___FUNCTION__:
3447 if (!gnu_ext)
3448 goto tok_identifier;
3449 /* fall thru */
3450 case TOK___FUNC__:
3452 void *ptr;
3453 int len;
3454 /* special function name identifier */
3455 len = strlen(funcname) + 1;
3456 /* generate char[len] type */
3457 type.t = VT_BYTE;
3458 mk_pointer(&type);
3459 type.t |= VT_ARRAY;
3460 type.ref->c = len;
3461 vpush_ref(&type, data_section, data_section->data_offset, len);
3462 ptr = section_ptr_add(data_section, len);
3463 memcpy(ptr, funcname, len);
3464 next();
3466 break;
3467 case TOK_LSTR:
3468 #ifdef TCC_TARGET_PE
3469 t = VT_SHORT | VT_UNSIGNED;
3470 #else
3471 t = VT_INT;
3472 #endif
3473 goto str_init;
3474 case TOK_STR:
3475 /* string parsing */
3476 t = VT_BYTE;
3477 str_init:
3478 if (tcc_state->warn_write_strings)
3479 t |= VT_CONSTANT;
3480 type.t = t;
3481 mk_pointer(&type);
3482 type.t |= VT_ARRAY;
3483 memset(&ad, 0, sizeof(AttributeDef));
3484 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3485 break;
3486 case '(':
3487 next();
3488 /* cast ? */
3489 if (parse_btype(&type, &ad)) {
3490 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3491 skip(')');
3492 /* check ISOC99 compound literal */
3493 if (tok == '{') {
3494 /* data is allocated locally by default */
3495 if (global_expr)
3496 r = VT_CONST;
3497 else
3498 r = VT_LOCAL;
3499 /* all except arrays are lvalues */
3500 if (!(type.t & VT_ARRAY))
3501 r |= lvalue_type(type.t);
3502 memset(&ad, 0, sizeof(AttributeDef));
3503 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3504 } else {
3505 if (sizeof_caller) {
3506 vpush(&type);
3507 return;
3509 unary();
3510 gen_cast(&type);
3512 } else if (tok == '{') {
3513 /* save all registers */
3514 save_regs(0);
3515 /* statement expression : we do not accept break/continue
3516 inside as GCC does */
3517 block(NULL, NULL, NULL, NULL, 0, 1);
3518 skip(')');
3519 } else {
3520 gexpr();
3521 skip(')');
3523 break;
3524 case '*':
3525 next();
3526 unary();
3527 indir();
3528 break;
3529 case '&':
3530 next();
3531 unary();
3532 /* functions names must be treated as function pointers,
3533 except for unary '&' and sizeof. Since we consider that
3534 functions are not lvalues, we only have to handle it
3535 there and in function calls. */
3536 /* arrays can also be used although they are not lvalues */
3537 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3538 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3539 test_lvalue();
3540 mk_pointer(&vtop->type);
3541 gaddrof();
3542 break;
3543 case '!':
3544 next();
3545 unary();
3546 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3547 CType boolean;
3548 boolean.t = VT_BOOL;
3549 gen_cast(&boolean);
3550 vtop->c.i = !vtop->c.i;
3551 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3552 vtop->c.i = vtop->c.i ^ 1;
3553 else {
3554 save_regs(1);
3555 vseti(VT_JMP, gtst(1, 0));
3557 break;
3558 case '~':
3559 next();
3560 unary();
3561 vpushi(-1);
3562 gen_op('^');
3563 break;
3564 case '+':
3565 next();
3566 /* in order to force cast, we add zero */
3567 unary();
3568 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3569 error("pointer not accepted for unary plus");
3570 vpushi(0);
3571 gen_op('+');
3572 break;
3573 case TOK_SIZEOF:
3574 case TOK_ALIGNOF1:
3575 case TOK_ALIGNOF2:
3576 t = tok;
3577 next();
3578 in_sizeof++;
3579 unary_type(&type); // Perform a in_sizeof = 0;
3580 if ((t == TOK_SIZEOF) && (type.t & VT_VLA)) {
3581 vla_runtime_type_size(&type, &align);
3582 size = 0;
3583 } else {
3584 size = type_size(&type, &align);
3586 if (t == TOK_SIZEOF) {
3587 if (!(type.t & VT_VLA)) {
3588 if (size < 0)
3589 error("sizeof applied to an incomplete type");
3590 vpushi(size);
3592 } else {
3593 vpushi(align);
3595 vtop->type.t |= VT_UNSIGNED;
3596 break;
3598 case TOK_builtin_types_compatible_p:
3600 CType type1, type2;
3601 next();
3602 skip('(');
3603 parse_type(&type1);
3604 skip(',');
3605 parse_type(&type2);
3606 skip(')');
3607 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3608 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3609 vpushi(is_compatible_types(&type1, &type2));
3611 break;
3612 case TOK_builtin_constant_p:
3614 int saved_nocode_wanted, res;
3615 next();
3616 skip('(');
3617 saved_nocode_wanted = nocode_wanted;
3618 nocode_wanted = 1;
3619 gexpr();
3620 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3621 vpop();
3622 nocode_wanted = saved_nocode_wanted;
3623 skip(')');
3624 vpushi(res);
3626 break;
3627 case TOK_builtin_frame_address:
3629 CType type;
3630 next();
3631 skip('(');
3632 if (tok != TOK_CINT) {
3633 error("__builtin_frame_address only takes integers");
3635 if (tokc.i != 0) {
3636 error("TCC only supports __builtin_frame_address(0)");
3638 next();
3639 skip(')');
3640 type.t = VT_VOID;
3641 mk_pointer(&type);
3642 vset(&type, VT_LOCAL, 0);
3644 break;
3645 #ifdef TCC_TARGET_X86_64
3646 case TOK_builtin_va_arg_types:
3648 /* This definition must be synced with stdarg.h */
3649 enum __va_arg_type {
3650 __va_gen_reg, __va_float_reg, __va_stack
3652 CType type;
3653 int bt;
3654 next();
3655 skip('(');
3656 parse_type(&type);
3657 skip(')');
3658 bt = type.t & VT_BTYPE;
3659 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
3660 vpushi(__va_stack);
3661 } else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
3662 vpushi(__va_float_reg);
3663 } else {
3664 vpushi(__va_gen_reg);
3667 break;
3668 #endif
3669 case TOK_INC:
3670 case TOK_DEC:
3671 t = tok;
3672 next();
3673 unary();
3674 inc(0, t);
3675 break;
3676 case '-':
3677 next();
3678 vpushi(0);
3679 unary();
3680 gen_op('-');
3681 break;
3682 case TOK_LAND:
3683 if (!gnu_ext)
3684 goto tok_identifier;
3685 next();
3686 /* allow to take the address of a label */
3687 if (tok < TOK_UIDENT)
3688 expect("label identifier");
3689 s = label_find(tok);
3690 if (!s) {
3691 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3692 } else {
3693 if (s->r == LABEL_DECLARED)
3694 s->r = LABEL_FORWARD;
3696 if (!s->type.t) {
3697 s->type.t = VT_VOID;
3698 mk_pointer(&s->type);
3699 s->type.t |= VT_STATIC;
3701 vset(&s->type, VT_CONST | VT_SYM, 0);
3702 vtop->sym = s;
3703 next();
3704 break;
3706 // special qnan , snan and infinity values
3707 case TOK___NAN__:
3708 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3709 next();
3710 break;
3711 case TOK___SNAN__:
3712 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3713 next();
3714 break;
3715 case TOK___INF__:
3716 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3717 next();
3718 break;
3720 default:
3721 tok_identifier:
3722 t = tok;
3723 next();
3724 if (t < TOK_UIDENT)
3725 expect("identifier");
3726 s = sym_find(t);
3727 if (!s) {
3728 if (tok != '(')
3729 error("'%s' undeclared", get_tok_str(t, NULL));
3730 /* for simple function calls, we tolerate undeclared
3731 external reference to int() function */
3732 if (tcc_state->warn_implicit_function_declaration)
3733 warning("implicit declaration of function '%s'",
3734 get_tok_str(t, NULL));
3735 s = external_global_sym(t, &func_old_type, 0);
3737 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3738 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3739 /* if referencing an inline function, then we generate a
3740 symbol to it if not already done. It will have the
3741 effect to generate code for it at the end of the
3742 compilation unit. Inline function as always
3743 generated in the text section. */
3744 if (!s->c)
3745 put_extern_sym(s, text_section, 0, 0);
3746 r = VT_SYM | VT_CONST;
3747 } else {
3748 r = s->r;
3750 if (s->type.t & VT_VLA)
3751 vpushv(s->s);
3752 else
3753 vset(&s->type, r, s->c);
3754 /* if forward reference, we must point to s */
3755 if (vtop->r & VT_SYM) {
3756 vtop->sym = s;
3757 vtop->c.ul = 0;
3759 break;
3762 /* post operations */
3763 while (1) {
3764 if (tok == TOK_INC || tok == TOK_DEC) {
3765 inc(1, tok);
3766 next();
3767 } else if (tok == '.' || tok == TOK_ARROW) {
3768 int qualifiers;
3769 /* field */
3770 if (tok == TOK_ARROW)
3771 indir();
3772 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3773 test_lvalue();
3774 gaddrof();
3775 next();
3776 /* expect pointer on structure */
3777 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3778 expect("struct or union");
3779 s = vtop->type.ref;
3780 /* find field */
3781 tok |= SYM_FIELD;
3782 while ((s = s->next) != NULL) {
3783 if (s->v == tok)
3784 break;
3786 if (!s)
3787 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3788 /* add field offset to pointer */
3789 vtop->type = char_pointer_type; /* change type to 'char *' */
3790 vpushi(s->c);
3791 gen_op('+');
3792 /* change type to field type, and set to lvalue */
3793 vtop->type = s->type;
3794 vtop->type.t |= qualifiers;
3795 /* an array is never an lvalue */
3796 if (!(vtop->type.t & VT_ARRAY)) {
3797 vtop->r |= lvalue_type(vtop->type.t);
3798 #ifdef CONFIG_TCC_BCHECK
3799 /* if bound checking, the referenced pointer must be checked */
3800 if (tcc_state->do_bounds_check)
3801 vtop->r |= VT_MUSTBOUND;
3802 #endif
3804 next();
3805 } else if (tok == '[') {
3806 next();
3807 gexpr();
3808 gen_op('+');
3809 indir();
3810 skip(']');
3811 } else if (tok == '(') {
3812 SValue ret;
3813 Sym *sa;
3814 int nb_args;
3816 /* function call */
3817 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3818 /* pointer test (no array accepted) */
3819 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3820 vtop->type = *pointed_type(&vtop->type);
3821 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3822 goto error_func;
3823 } else {
3824 error_func:
3825 expect("function pointer");
3827 } else {
3828 vtop->r &= ~VT_LVAL; /* no lvalue */
3830 /* get return type */
3831 s = vtop->type.ref;
3832 next();
3833 sa = s->next; /* first parameter */
3834 nb_args = 0;
3835 ret.r2 = VT_CONST;
3836 /* compute first implicit argument if a structure is returned */
3837 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3838 /* get some space for the returned structure */
3839 size = type_size(&s->type, &align);
3840 loc = (loc - size) & -align;
3841 ret.type = s->type;
3842 ret.r = VT_LOCAL | VT_LVAL;
3843 /* pass it as 'int' to avoid structure arg passing
3844 problems */
3845 vseti(VT_LOCAL, loc);
3846 ret.c = vtop->c;
3847 nb_args++;
3848 } else {
3849 ret.type = s->type;
3850 /* return in register */
3851 if (is_float(ret.type.t)) {
3852 ret.r = reg_fret(ret.type.t);
3853 } else {
3854 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3855 ret.r2 = REG_LRET;
3856 ret.r = REG_IRET;
3858 ret.c.i = 0;
3860 if (tok != ')') {
3861 for(;;) {
3862 expr_eq();
3863 gfunc_param_typed(s, sa);
3864 nb_args++;
3865 if (sa)
3866 sa = sa->next;
3867 if (tok == ')')
3868 break;
3869 skip(',');
3872 if (sa)
3873 error("too few arguments to function");
3874 skip(')');
3875 if (!nocode_wanted) {
3876 gfunc_call(nb_args);
3877 } else {
3878 vtop -= (nb_args + 1);
3880 /* return value */
3881 vsetc(&ret.type, ret.r, &ret.c);
3882 vtop->r2 = ret.r2;
3883 } else {
3884 break;
3889 ST_FUNC void expr_prod(void)
3891 int t;
3893 unary();
3894 while (tok == '*' || tok == '/' || tok == '%') {
3895 t = tok;
3896 next();
3897 unary();
3898 gen_op(t);
3902 ST_FUNC void expr_sum(void)
3904 int t;
3906 expr_prod();
3907 while (tok == '+' || tok == '-') {
3908 t = tok;
3909 next();
3910 expr_prod();
3911 gen_op(t);
3915 static void expr_shift(void)
3917 int t;
3919 expr_sum();
3920 while (tok == TOK_SHL || tok == TOK_SAR) {
3921 t = tok;
3922 next();
3923 expr_sum();
3924 gen_op(t);
3928 static void expr_cmp(void)
3930 int t;
3932 expr_shift();
3933 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3934 tok == TOK_ULT || tok == TOK_UGE) {
3935 t = tok;
3936 next();
3937 expr_shift();
3938 gen_op(t);
3942 static void expr_cmpeq(void)
3944 int t;
3946 expr_cmp();
3947 while (tok == TOK_EQ || tok == TOK_NE) {
3948 t = tok;
3949 next();
3950 expr_cmp();
3951 gen_op(t);
3955 static void expr_and(void)
3957 expr_cmpeq();
3958 while (tok == '&') {
3959 next();
3960 expr_cmpeq();
3961 gen_op('&');
3965 static void expr_xor(void)
3967 expr_and();
3968 while (tok == '^') {
3969 next();
3970 expr_and();
3971 gen_op('^');
3975 static void expr_or(void)
3977 expr_xor();
3978 while (tok == '|') {
3979 next();
3980 expr_xor();
3981 gen_op('|');
3985 /* XXX: fix this mess */
3986 static void expr_land_const(void)
3988 expr_or();
3989 while (tok == TOK_LAND) {
3990 next();
3991 expr_or();
3992 gen_op(TOK_LAND);
3996 /* XXX: fix this mess */
3997 static void expr_lor_const(void)
3999 expr_land_const();
4000 while (tok == TOK_LOR) {
4001 next();
4002 expr_land_const();
4003 gen_op(TOK_LOR);
4007 /* only used if non constant */
4008 static void expr_land(void)
4010 int t;
4012 expr_or();
4013 if (tok == TOK_LAND) {
4014 t = 0;
4015 save_regs(1);
4016 for(;;) {
4017 t = gtst(1, t);
4018 if (tok != TOK_LAND) {
4019 vseti(VT_JMPI, t);
4020 break;
4022 next();
4023 expr_or();
4028 static void expr_lor(void)
4030 int t;
4032 expr_land();
4033 if (tok == TOK_LOR) {
4034 t = 0;
4035 save_regs(1);
4036 for(;;) {
4037 t = gtst(0, t);
4038 if (tok != TOK_LOR) {
4039 vseti(VT_JMP, t);
4040 break;
4042 next();
4043 expr_land();
4048 /* XXX: better constant handling */
4049 static void expr_cond(void)
4051 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4052 SValue sv;
4053 CType type, type1, type2;
4055 if (const_wanted) {
4056 expr_lor_const();
4057 if (tok == '?') {
4058 CType boolean;
4059 int c;
4060 boolean.t = VT_BOOL;
4061 vdup();
4062 gen_cast(&boolean);
4063 c = vtop->c.i;
4064 vpop();
4065 next();
4066 if (tok != ':' || !gnu_ext) {
4067 vpop();
4068 gexpr();
4070 if (!c)
4071 vpop();
4072 skip(':');
4073 expr_cond();
4074 if (c)
4075 vpop();
4077 } else {
4078 expr_lor();
4079 if (tok == '?') {
4080 next();
4081 if (vtop != vstack) {
4082 /* needed to avoid having different registers saved in
4083 each branch */
4084 if (is_float(vtop->type.t)) {
4085 rc = RC_FLOAT;
4086 #ifdef TCC_TARGET_X86_64
4087 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4088 rc = RC_ST0;
4090 #endif
4092 else
4093 rc = RC_INT;
4094 gv(rc);
4095 save_regs(1);
4097 if (tok == ':' && gnu_ext) {
4098 gv_dup();
4099 tt = gtst(1, 0);
4100 } else {
4101 tt = gtst(1, 0);
4102 gexpr();
4104 type1 = vtop->type;
4105 sv = *vtop; /* save value to handle it later */
4106 vtop--; /* no vpop so that FP stack is not flushed */
4107 skip(':');
4108 u = gjmp(0);
4109 gsym(tt);
4110 expr_cond();
4111 type2 = vtop->type;
4113 t1 = type1.t;
4114 bt1 = t1 & VT_BTYPE;
4115 t2 = type2.t;
4116 bt2 = t2 & VT_BTYPE;
4117 /* cast operands to correct type according to ISOC rules */
4118 if (is_float(bt1) || is_float(bt2)) {
4119 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4120 type.t = VT_LDOUBLE;
4121 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4122 type.t = VT_DOUBLE;
4123 } else {
4124 type.t = VT_FLOAT;
4126 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4127 /* cast to biggest op */
4128 type.t = VT_LLONG;
4129 /* convert to unsigned if it does not fit in a long long */
4130 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4131 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4132 type.t |= VT_UNSIGNED;
4133 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4134 /* XXX: test pointer compatibility */
4135 type = type1;
4136 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4137 /* XXX: test function pointer compatibility */
4138 type = type1;
4139 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4140 /* XXX: test structure compatibility */
4141 type = type1;
4142 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4143 /* NOTE: as an extension, we accept void on only one side */
4144 type.t = VT_VOID;
4145 } else {
4146 /* integer operations */
4147 type.t = VT_INT;
4148 /* convert to unsigned if it does not fit in an integer */
4149 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4150 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4151 type.t |= VT_UNSIGNED;
4154 /* now we convert second operand */
4155 gen_cast(&type);
4156 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4157 gaddrof();
4158 rc = RC_INT;
4159 if (is_float(type.t)) {
4160 rc = RC_FLOAT;
4161 #ifdef TCC_TARGET_X86_64
4162 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4163 rc = RC_ST0;
4165 #endif
4166 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4167 /* for long longs, we use fixed registers to avoid having
4168 to handle a complicated move */
4169 rc = RC_IRET;
4172 r2 = gv(rc);
4173 /* this is horrible, but we must also convert first
4174 operand */
4175 tt = gjmp(0);
4176 gsym(u);
4177 /* put again first value and cast it */
4178 *vtop = sv;
4179 gen_cast(&type);
4180 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4181 gaddrof();
4182 r1 = gv(rc);
4183 move_reg(r2, r1);
4184 vtop->r = r2;
4185 gsym(tt);
4190 static void expr_eq(void)
4192 int t;
4194 expr_cond();
4195 if (tok == '=' ||
4196 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4197 tok == TOK_A_XOR || tok == TOK_A_OR ||
4198 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4199 test_lvalue();
4200 t = tok;
4201 next();
4202 if (t == '=') {
4203 expr_eq();
4204 } else {
4205 vdup();
4206 expr_eq();
4207 gen_op(t & 0x7f);
4209 vstore();
4213 ST_FUNC void gexpr(void)
4215 while (1) {
4216 expr_eq();
4217 if (tok != ',')
4218 break;
4219 vpop();
4220 next();
4224 /* parse an expression and return its type without any side effect. */
4225 static void expr_type(CType *type)
4227 int saved_nocode_wanted;
4229 saved_nocode_wanted = nocode_wanted;
4230 nocode_wanted = 1;
4231 gexpr();
4232 *type = vtop->type;
4233 vpop();
4234 nocode_wanted = saved_nocode_wanted;
4237 /* parse a unary expression and return its type without any side
4238 effect. */
4239 static void unary_type(CType *type)
4241 int a;
4243 a = nocode_wanted;
4244 nocode_wanted = 1;
4245 unary();
4246 *type = vtop->type;
4247 vpop();
4248 nocode_wanted = a;
4251 /* parse a constant expression and return value in vtop. */
4252 static void expr_const1(void)
4254 int a;
4255 a = const_wanted;
4256 const_wanted = 1;
4257 expr_cond();
4258 const_wanted = a;
4261 /* parse an integer constant and return its value. */
4262 ST_FUNC int expr_const(void)
4264 int c;
4265 expr_const1();
4266 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4267 expect("constant expression");
4268 c = vtop->c.i;
4269 vpop();
4270 return c;
4273 /* return the label token if current token is a label, otherwise
4274 return zero */
4275 static int is_label(void)
4277 int last_tok;
4279 /* fast test first */
4280 if (tok < TOK_UIDENT)
4281 return 0;
4282 /* no need to save tokc because tok is an identifier */
4283 last_tok = tok;
4284 next();
4285 if (tok == ':') {
4286 next();
4287 return last_tok;
4288 } else {
4289 unget_tok(last_tok);
4290 return 0;
4294 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4295 int case_reg, int is_expr)
4297 int a, b, c, d;
4298 Sym *s;
4300 /* generate line number info */
4301 if (tcc_state->do_debug &&
4302 (last_line_num != file->line_num || last_ind != ind)) {
4303 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4304 last_ind = ind;
4305 last_line_num = file->line_num;
4308 if (is_expr) {
4309 /* default return value is (void) */
4310 vpushi(0);
4311 vtop->type.t = VT_VOID;
4314 if (tok == TOK_IF) {
4315 /* if test */
4316 next();
4317 skip('(');
4318 gexpr();
4319 skip(')');
4320 a = gtst(1, 0);
4321 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4322 c = tok;
4323 if (c == TOK_ELSE) {
4324 next();
4325 d = gjmp(0);
4326 gsym(a);
4327 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4328 gsym(d); /* patch else jmp */
4329 } else
4330 gsym(a);
4331 } else if (tok == TOK_WHILE) {
4332 next();
4333 d = ind;
4334 skip('(');
4335 gexpr();
4336 skip(')');
4337 a = gtst(1, 0);
4338 b = 0;
4339 block(&a, &b, case_sym, def_sym, case_reg, 0);
4340 gjmp_addr(d);
4341 gsym(a);
4342 gsym_addr(b, d);
4343 } else if (tok == '{') {
4344 Sym *llabel;
4346 next();
4347 /* record local declaration stack position */
4348 s = local_stack;
4349 llabel = local_label_stack;
4350 /* handle local labels declarations */
4351 if (tok == TOK_LABEL) {
4352 next();
4353 for(;;) {
4354 if (tok < TOK_UIDENT)
4355 expect("label identifier");
4356 label_push(&local_label_stack, tok, LABEL_DECLARED);
4357 next();
4358 if (tok == ',') {
4359 next();
4360 } else {
4361 skip(';');
4362 break;
4366 while (tok != '}') {
4367 decl(VT_LOCAL);
4368 if (tok != '}') {
4369 if (is_expr)
4370 vpop();
4371 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4374 /* pop locally defined labels */
4375 label_pop(&local_label_stack, llabel);
4376 /* pop locally defined symbols */
4377 if(is_expr) {
4378 /* XXX: this solution makes only valgrind happy...
4379 triggered by gcc.c-torture/execute/20000917-1.c */
4380 Sym *p;
4381 switch(vtop->type.t & VT_BTYPE) {
4382 case VT_PTR:
4383 case VT_STRUCT:
4384 case VT_ENUM:
4385 case VT_FUNC:
4386 for(p=vtop->type.ref;p;p=p->prev)
4387 if(p->prev==s)
4388 error("unsupported expression type");
4391 sym_pop(&local_stack, s);
4392 next();
4393 } else if (tok == TOK_RETURN) {
4394 next();
4395 if (tok != ';') {
4396 gexpr();
4397 gen_assign_cast(&func_vt);
4398 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4399 CType type;
4400 /* if returning structure, must copy it to implicit
4401 first pointer arg location */
4402 #ifdef TCC_ARM_EABI
4403 int align, size;
4404 size = type_size(&func_vt,&align);
4405 if(size <= 4)
4407 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4408 && (align & 3))
4410 int addr;
4411 loc = (loc - size) & -4;
4412 addr = loc;
4413 type = func_vt;
4414 vset(&type, VT_LOCAL | VT_LVAL, addr);
4415 vswap();
4416 vstore();
4417 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4419 vtop->type = int_type;
4420 gv(RC_IRET);
4421 } else {
4422 #endif
4423 type = func_vt;
4424 mk_pointer(&type);
4425 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4426 indir();
4427 vswap();
4428 /* copy structure value to pointer */
4429 vstore();
4430 #ifdef TCC_ARM_EABI
4432 #endif
4433 } else if (is_float(func_vt.t)) {
4434 gv(rc_fret(func_vt.t));
4435 } else {
4436 gv(RC_IRET);
4438 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4440 skip(';');
4441 rsym = gjmp(rsym); /* jmp */
4442 } else if (tok == TOK_BREAK) {
4443 /* compute jump */
4444 if (!bsym)
4445 error("cannot break");
4446 *bsym = gjmp(*bsym);
4447 next();
4448 skip(';');
4449 } else if (tok == TOK_CONTINUE) {
4450 /* compute jump */
4451 if (!csym)
4452 error("cannot continue");
4453 *csym = gjmp(*csym);
4454 next();
4455 skip(';');
4456 } else if (tok == TOK_FOR) {
4457 int e;
4458 next();
4459 skip('(');
4460 s = local_stack;
4461 if (tok != ';') {
4462 /* c99 for-loop init decl? */
4463 if (!decl0(VT_LOCAL, 1)) {
4464 /* no, regular for-loop init expr */
4465 gexpr();
4466 vpop();
4469 skip(';');
4470 d = ind;
4471 c = ind;
4472 a = 0;
4473 b = 0;
4474 if (tok != ';') {
4475 gexpr();
4476 a = gtst(1, 0);
4478 skip(';');
4479 if (tok != ')') {
4480 e = gjmp(0);
4481 c = ind;
4482 gexpr();
4483 vpop();
4484 gjmp_addr(d);
4485 gsym(e);
4487 skip(')');
4488 block(&a, &b, case_sym, def_sym, case_reg, 0);
4489 gjmp_addr(c);
4490 gsym(a);
4491 gsym_addr(b, c);
4492 sym_pop(&local_stack, s);
4493 } else
4494 if (tok == TOK_DO) {
4495 next();
4496 a = 0;
4497 b = 0;
4498 d = ind;
4499 block(&a, &b, case_sym, def_sym, case_reg, 0);
4500 skip(TOK_WHILE);
4501 skip('(');
4502 gsym(b);
4503 gexpr();
4504 c = gtst(0, 0);
4505 gsym_addr(c, d);
4506 skip(')');
4507 gsym(a);
4508 skip(';');
4509 } else
4510 if (tok == TOK_SWITCH) {
4511 next();
4512 skip('(');
4513 gexpr();
4514 /* XXX: other types than integer */
4515 case_reg = gv(RC_INT);
4516 vpop();
4517 skip(')');
4518 a = 0;
4519 b = gjmp(0); /* jump to first case */
4520 c = 0;
4521 block(&a, csym, &b, &c, case_reg, 0);
4522 /* if no default, jmp after switch */
4523 if (c == 0)
4524 c = ind;
4525 /* default label */
4526 gsym_addr(b, c);
4527 /* break label */
4528 gsym(a);
4529 } else
4530 if (tok == TOK_CASE) {
4531 int v1, v2;
4532 if (!case_sym)
4533 expect("switch");
4534 next();
4535 v1 = expr_const();
4536 v2 = v1;
4537 if (gnu_ext && tok == TOK_DOTS) {
4538 next();
4539 v2 = expr_const();
4540 if (v2 < v1)
4541 warning("empty case range");
4543 /* since a case is like a label, we must skip it with a jmp */
4544 b = gjmp(0);
4545 gsym(*case_sym);
4546 vseti(case_reg, 0);
4547 vpushi(v1);
4548 if (v1 == v2) {
4549 gen_op(TOK_EQ);
4550 *case_sym = gtst(1, 0);
4551 } else {
4552 gen_op(TOK_GE);
4553 *case_sym = gtst(1, 0);
4554 vseti(case_reg, 0);
4555 vpushi(v2);
4556 gen_op(TOK_LE);
4557 *case_sym = gtst(1, *case_sym);
4559 gsym(b);
4560 skip(':');
4561 is_expr = 0;
4562 goto block_after_label;
4563 } else
4564 if (tok == TOK_DEFAULT) {
4565 next();
4566 skip(':');
4567 if (!def_sym)
4568 expect("switch");
4569 if (*def_sym)
4570 error("too many 'default'");
4571 *def_sym = ind;
4572 is_expr = 0;
4573 goto block_after_label;
4574 } else
4575 if (tok == TOK_GOTO) {
4576 next();
4577 if (tok == '*' && gnu_ext) {
4578 /* computed goto */
4579 next();
4580 gexpr();
4581 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4582 expect("pointer");
4583 ggoto();
4584 } else if (tok >= TOK_UIDENT) {
4585 s = label_find(tok);
4586 /* put forward definition if needed */
4587 if (!s) {
4588 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4589 } else {
4590 if (s->r == LABEL_DECLARED)
4591 s->r = LABEL_FORWARD;
4593 /* label already defined */
4594 if (s->r & LABEL_FORWARD)
4595 s->jnext = gjmp(s->jnext);
4596 else
4597 gjmp_addr(s->jnext);
4598 next();
4599 } else {
4600 expect("label identifier");
4602 skip(';');
4603 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4604 asm_instr();
4605 } else {
4606 b = is_label();
4607 if (b) {
4608 /* label case */
4609 s = label_find(b);
4610 if (s) {
4611 if (s->r == LABEL_DEFINED)
4612 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4613 gsym(s->jnext);
4614 s->r = LABEL_DEFINED;
4615 } else {
4616 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4618 s->jnext = ind;
4619 /* we accept this, but it is a mistake */
4620 block_after_label:
4621 if (tok == '}') {
4622 warning("deprecated use of label at end of compound statement");
4623 } else {
4624 if (is_expr)
4625 vpop();
4626 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4628 } else {
4629 /* expression case */
4630 if (tok != ';') {
4631 if (is_expr) {
4632 vpop();
4633 gexpr();
4634 } else {
4635 gexpr();
4636 vpop();
4639 skip(';');
4644 /* t is the array or struct type. c is the array or struct
4645 address. cur_index/cur_field is the pointer to the current
4646 value. 'size_only' is true if only size info is needed (only used
4647 in arrays) */
4648 static void decl_designator(CType *type, Section *sec, unsigned long c,
4649 int *cur_index, Sym **cur_field,
4650 int size_only)
4652 Sym *s, *f;
4653 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4654 CType type1;
4656 notfirst = 0;
4657 elem_size = 0;
4658 nb_elems = 1;
4659 if (gnu_ext && (l = is_label()) != 0)
4660 goto struct_field;
4661 while (tok == '[' || tok == '.') {
4662 if (tok == '[') {
4663 if (!(type->t & VT_ARRAY))
4664 expect("array type");
4665 s = type->ref;
4666 next();
4667 index = expr_const();
4668 if (index < 0 || (s->c >= 0 && index >= s->c))
4669 expect("invalid index");
4670 if (tok == TOK_DOTS && gnu_ext) {
4671 next();
4672 index_last = expr_const();
4673 if (index_last < 0 ||
4674 (s->c >= 0 && index_last >= s->c) ||
4675 index_last < index)
4676 expect("invalid index");
4677 } else {
4678 index_last = index;
4680 skip(']');
4681 if (!notfirst)
4682 *cur_index = index_last;
4683 type = pointed_type(type);
4684 elem_size = type_size(type, &align);
4685 c += index * elem_size;
4686 /* NOTE: we only support ranges for last designator */
4687 nb_elems = index_last - index + 1;
4688 if (nb_elems != 1) {
4689 notfirst = 1;
4690 break;
4692 } else {
4693 next();
4694 l = tok;
4695 next();
4696 struct_field:
4697 if ((type->t & VT_BTYPE) != VT_STRUCT)
4698 expect("struct/union type");
4699 s = type->ref;
4700 l |= SYM_FIELD;
4701 f = s->next;
4702 while (f) {
4703 if (f->v == l)
4704 break;
4705 f = f->next;
4707 if (!f)
4708 expect("field");
4709 if (!notfirst)
4710 *cur_field = f;
4711 /* XXX: fix this mess by using explicit storage field */
4712 type1 = f->type;
4713 type1.t |= (type->t & ~VT_TYPE);
4714 type = &type1;
4715 c += f->c;
4717 notfirst = 1;
4719 if (notfirst) {
4720 if (tok == '=') {
4721 next();
4722 } else {
4723 if (!gnu_ext)
4724 expect("=");
4726 } else {
4727 if (type->t & VT_ARRAY) {
4728 index = *cur_index;
4729 type = pointed_type(type);
4730 c += index * type_size(type, &align);
4731 } else {
4732 f = *cur_field;
4733 if (!f)
4734 error("too many field init");
4735 /* XXX: fix this mess by using explicit storage field */
4736 type1 = f->type;
4737 type1.t |= (type->t & ~VT_TYPE);
4738 type = &type1;
4739 c += f->c;
4742 decl_initializer(type, sec, c, 0, size_only);
4744 /* XXX: make it more general */
4745 if (!size_only && nb_elems > 1) {
4746 unsigned long c_end;
4747 uint8_t *src, *dst;
4748 int i;
4750 if (!sec)
4751 error("range init not supported yet for dynamic storage");
4752 c_end = c + nb_elems * elem_size;
4753 if (c_end > sec->data_allocated)
4754 section_realloc(sec, c_end);
4755 src = sec->data + c;
4756 dst = src;
4757 for(i = 1; i < nb_elems; i++) {
4758 dst += elem_size;
4759 memcpy(dst, src, elem_size);
4764 #define EXPR_VAL 0
4765 #define EXPR_CONST 1
4766 #define EXPR_ANY 2
4768 /* store a value or an expression directly in global data or in local array */
4769 static void init_putv(CType *type, Section *sec, unsigned long c,
4770 int v, int expr_type)
4772 int saved_global_expr, bt, bit_pos, bit_size;
4773 void *ptr;
4774 unsigned long long bit_mask;
4775 CType dtype;
4777 switch(expr_type) {
4778 case EXPR_VAL:
4779 vpushi(v);
4780 break;
4781 case EXPR_CONST:
4782 /* compound literals must be allocated globally in this case */
4783 saved_global_expr = global_expr;
4784 global_expr = 1;
4785 expr_const1();
4786 global_expr = saved_global_expr;
4787 /* NOTE: symbols are accepted */
4788 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4789 error("initializer element is not constant");
4790 break;
4791 case EXPR_ANY:
4792 expr_eq();
4793 break;
4796 dtype = *type;
4797 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4799 if (sec) {
4800 /* XXX: not portable */
4801 /* XXX: generate error if incorrect relocation */
4802 gen_assign_cast(&dtype);
4803 bt = type->t & VT_BTYPE;
4804 /* we'll write at most 12 bytes */
4805 if (c + 12 > sec->data_allocated) {
4806 section_realloc(sec, c + 12);
4808 ptr = sec->data + c;
4809 /* XXX: make code faster ? */
4810 if (!(type->t & VT_BITFIELD)) {
4811 bit_pos = 0;
4812 bit_size = 32;
4813 bit_mask = -1LL;
4814 } else {
4815 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4816 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4817 bit_mask = (1LL << bit_size) - 1;
4819 if ((vtop->r & VT_SYM) &&
4820 (bt == VT_BYTE ||
4821 bt == VT_SHORT ||
4822 bt == VT_DOUBLE ||
4823 bt == VT_LDOUBLE ||
4824 bt == VT_LLONG ||
4825 (bt == VT_INT && bit_size != 32)))
4826 error("initializer element is not computable at load time");
4827 switch(bt) {
4828 case VT_BOOL:
4829 vtop->c.i = (vtop->c.i != 0);
4830 case VT_BYTE:
4831 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4832 break;
4833 case VT_SHORT:
4834 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4835 break;
4836 case VT_DOUBLE:
4837 *(double *)ptr = vtop->c.d;
4838 break;
4839 case VT_LDOUBLE:
4840 *(long double *)ptr = vtop->c.ld;
4841 break;
4842 case VT_LLONG:
4843 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4844 break;
4845 default:
4846 if (vtop->r & VT_SYM) {
4847 greloc(sec, vtop->sym, c, R_DATA_PTR);
4849 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4850 break;
4852 vtop--;
4853 } else {
4854 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4855 vswap();
4856 vstore();
4857 vpop();
4861 /* put zeros for variable based init */
4862 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4864 if (sec) {
4865 /* nothing to do because globals are already set to zero */
4866 } else {
4867 vpush_global_sym(&func_old_type, TOK_memset);
4868 vseti(VT_LOCAL, c);
4869 vpushi(0);
4870 vpushi(size);
4871 gfunc_call(3);
4875 /* 't' contains the type and storage info. 'c' is the offset of the
4876 object in section 'sec'. If 'sec' is NULL, it means stack based
4877 allocation. 'first' is true if array '{' must be read (multi
4878 dimension implicit array init handling). 'size_only' is true if
4879 size only evaluation is wanted (only for arrays). */
4880 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4881 int first, int size_only)
4883 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
4884 int size1, align1, expr_type;
4885 Sym *s, *f;
4886 CType *t1;
4888 if (type->t & VT_VLA) {
4889 int a;
4890 CValue retcval;
4892 vpush_global_sym(&func_old_type, TOK_alloca);
4893 vla_runtime_type_size(type, &a);
4894 gfunc_call(1);
4896 /* return value */
4897 retcval.i = 0;
4898 vsetc(type, REG_IRET, &retcval);
4899 } else if (type->t & VT_ARRAY) {
4900 s = type->ref;
4901 n = s->c;
4902 array_length = 0;
4903 t1 = pointed_type(type);
4904 size1 = type_size(t1, &align1);
4906 no_oblock = 1;
4907 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4908 tok == '{') {
4909 if (tok != '{')
4910 error("character array initializer must be a literal,"
4911 " optionally enclosed in braces");
4912 skip('{');
4913 no_oblock = 0;
4916 /* only parse strings here if correct type (otherwise: handle
4917 them as ((w)char *) expressions */
4918 if ((tok == TOK_LSTR &&
4919 #ifdef TCC_TARGET_PE
4920 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4921 #else
4922 (t1->t & VT_BTYPE) == VT_INT
4923 #endif
4924 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4925 while (tok == TOK_STR || tok == TOK_LSTR) {
4926 int cstr_len, ch;
4927 CString *cstr;
4929 cstr = tokc.cstr;
4930 /* compute maximum number of chars wanted */
4931 if (tok == TOK_STR)
4932 cstr_len = cstr->size;
4933 else
4934 cstr_len = cstr->size / sizeof(nwchar_t);
4935 cstr_len--;
4936 nb = cstr_len;
4937 if (n >= 0 && nb > (n - array_length))
4938 nb = n - array_length;
4939 if (!size_only) {
4940 if (cstr_len > nb)
4941 warning("initializer-string for array is too long");
4942 /* in order to go faster for common case (char
4943 string in global variable, we handle it
4944 specifically */
4945 if (sec && tok == TOK_STR && size1 == 1) {
4946 memcpy(sec->data + c + array_length, cstr->data, nb);
4947 } else {
4948 for(i=0;i<nb;i++) {
4949 if (tok == TOK_STR)
4950 ch = ((unsigned char *)cstr->data)[i];
4951 else
4952 ch = ((nwchar_t *)cstr->data)[i];
4953 init_putv(t1, sec, c + (array_length + i) * size1,
4954 ch, EXPR_VAL);
4958 array_length += nb;
4959 next();
4961 /* only add trailing zero if enough storage (no
4962 warning in this case since it is standard) */
4963 if (n < 0 || array_length < n) {
4964 if (!size_only) {
4965 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4967 array_length++;
4969 } else {
4970 index = 0;
4971 while (tok != '}') {
4972 decl_designator(type, sec, c, &index, NULL, size_only);
4973 if (n >= 0 && index >= n)
4974 error("index too large");
4975 /* must put zero in holes (note that doing it that way
4976 ensures that it even works with designators) */
4977 if (!size_only && array_length < index) {
4978 init_putz(t1, sec, c + array_length * size1,
4979 (index - array_length) * size1);
4981 index++;
4982 if (index > array_length)
4983 array_length = index;
4984 /* special test for multi dimensional arrays (may not
4985 be strictly correct if designators are used at the
4986 same time) */
4987 if (index >= n && no_oblock)
4988 break;
4989 if (tok == '}')
4990 break;
4991 skip(',');
4994 if (!no_oblock)
4995 skip('}');
4996 /* put zeros at the end */
4997 if (!size_only && n >= 0 && array_length < n) {
4998 init_putz(t1, sec, c + array_length * size1,
4999 (n - array_length) * size1);
5001 /* patch type size if needed */
5002 if (n < 0)
5003 s->c = array_length;
5004 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5005 (sec || !first || tok == '{')) {
5006 int par_count;
5008 /* NOTE: the previous test is a specific case for automatic
5009 struct/union init */
5010 /* XXX: union needs only one init */
5012 /* XXX: this test is incorrect for local initializers
5013 beginning with ( without {. It would be much more difficult
5014 to do it correctly (ideally, the expression parser should
5015 be used in all cases) */
5016 par_count = 0;
5017 if (tok == '(') {
5018 AttributeDef ad1;
5019 CType type1;
5020 next();
5021 while (tok == '(') {
5022 par_count++;
5023 next();
5025 if (!parse_btype(&type1, &ad1))
5026 expect("cast");
5027 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5028 #if 0
5029 if (!is_assignable_types(type, &type1))
5030 error("invalid type for cast");
5031 #endif
5032 skip(')');
5034 no_oblock = 1;
5035 if (first || tok == '{') {
5036 skip('{');
5037 no_oblock = 0;
5039 s = type->ref;
5040 f = s->next;
5041 array_length = 0;
5042 index = 0;
5043 n = s->c;
5044 while (tok != '}') {
5045 decl_designator(type, sec, c, NULL, &f, size_only);
5046 index = f->c;
5047 if (!size_only && array_length < index) {
5048 init_putz(type, sec, c + array_length,
5049 index - array_length);
5051 index = index + type_size(&f->type, &align1);
5052 if (index > array_length)
5053 array_length = index;
5055 /* gr: skip fields from same union - ugly. */
5056 while (f->next) {
5057 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5058 /* test for same offset */
5059 if (f->next->c != f->c)
5060 break;
5061 /* if yes, test for bitfield shift */
5062 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5063 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5064 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5065 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5066 if (bit_pos_1 != bit_pos_2)
5067 break;
5069 f = f->next;
5072 f = f->next;
5073 if (no_oblock && f == NULL)
5074 break;
5075 if (tok == '}')
5076 break;
5077 skip(',');
5079 /* put zeros at the end */
5080 if (!size_only && array_length < n) {
5081 init_putz(type, sec, c + array_length,
5082 n - array_length);
5084 if (!no_oblock)
5085 skip('}');
5086 while (par_count) {
5087 skip(')');
5088 par_count--;
5090 } else if (tok == '{') {
5091 next();
5092 decl_initializer(type, sec, c, first, size_only);
5093 skip('}');
5094 } else if (size_only) {
5095 /* just skip expression */
5096 parlevel = parlevel1 = 0;
5097 while ((parlevel > 0 || parlevel1 > 0 ||
5098 (tok != '}' && tok != ',')) && tok != -1) {
5099 if (tok == '(')
5100 parlevel++;
5101 else if (tok == ')')
5102 parlevel--;
5103 else if (tok == '{')
5104 parlevel1++;
5105 else if (tok == '}')
5106 parlevel1--;
5107 next();
5109 } else {
5110 /* currently, we always use constant expression for globals
5111 (may change for scripting case) */
5112 expr_type = EXPR_CONST;
5113 if (!sec)
5114 expr_type = EXPR_ANY;
5115 init_putv(type, sec, c, 0, expr_type);
5119 /* parse an initializer for type 't' if 'has_init' is non zero, and
5120 allocate space in local or global data space ('r' is either
5121 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5122 variable 'v' with an associated name represented by 'asm_label' of
5123 scope 'scope' is declared before initializers are parsed. If 'v' is
5124 zero, then a reference to the new object is put in the value stack.
5125 If 'has_init' is 2, a special parsing is done to handle string
5126 constants. */
5127 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5128 int has_init, int v, char *asm_label,
5129 int scope)
5131 int size, align, addr, data_offset;
5132 int level;
5133 ParseState saved_parse_state = {0};
5134 TokenString init_str;
5135 Section *sec;
5136 Sym *vla = NULL;
5137 Sym *flexible_array;
5139 flexible_array = NULL;
5140 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5141 Sym *field;
5142 field = type->ref;
5143 while (field && field->next)
5144 field = field->next;
5145 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5146 flexible_array = field;
5149 size = type_size(type, &align);
5150 /* If unknown size, we must evaluate it before
5151 evaluating initializers because
5152 initializers can generate global data too
5153 (e.g. string pointers or ISOC99 compound
5154 literals). It also simplifies local
5155 initializers handling */
5156 tok_str_new(&init_str);
5157 if (size < 0 || flexible_array) {
5158 if (!has_init)
5159 error("unknown type size");
5160 /* get all init string */
5161 if (has_init == 2) {
5162 /* only get strings */
5163 while (tok == TOK_STR || tok == TOK_LSTR) {
5164 tok_str_add_tok(&init_str);
5165 next();
5167 } else {
5168 level = 0;
5169 while (level > 0 || (tok != ',' && tok != ';')) {
5170 if (tok < 0)
5171 error("unexpected end of file in initializer");
5172 tok_str_add_tok(&init_str);
5173 if (tok == '{')
5174 level++;
5175 else if (tok == '}') {
5176 level--;
5177 if (level <= 0) {
5178 next();
5179 break;
5182 next();
5185 tok_str_add(&init_str, -1);
5186 tok_str_add(&init_str, 0);
5188 /* compute size */
5189 save_parse_state(&saved_parse_state);
5191 macro_ptr = init_str.str;
5192 next();
5193 decl_initializer(type, NULL, 0, 1, 1);
5194 /* prepare second initializer parsing */
5195 macro_ptr = init_str.str;
5196 next();
5198 /* if still unknown size, error */
5199 size = type_size(type, &align);
5200 if (size < 0)
5201 error("unknown type size");
5203 if (flexible_array)
5204 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5205 /* take into account specified alignment if bigger */
5206 if (ad->aligned) {
5207 if (ad->aligned > align)
5208 align = ad->aligned;
5209 } else if (ad->packed) {
5210 align = 1;
5212 if ((r & VT_VALMASK) == VT_LOCAL) {
5213 sec = NULL;
5214 #ifdef CONFIG_TCC_BCHECK
5215 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5216 if (type->t & VT_VLA)
5217 warning("Array bound check don't work for VLA");
5218 else
5219 loc--;
5221 #endif
5222 loc = (loc - size) & -align;
5223 addr = loc;
5224 #ifdef CONFIG_TCC_BCHECK
5225 /* handles bounds */
5226 /* XXX: currently, since we do only one pass, we cannot track
5227 '&' operators, so we add only arrays */
5228 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY) &&
5229 !(type->t & VT_VLA)) {
5230 unsigned long *bounds_ptr;
5231 /* add padding between regions */
5232 loc--;
5233 /* then add local bound info */
5234 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5235 bounds_ptr[0] = addr;
5236 bounds_ptr[1] = size;
5238 #endif
5239 if (v) {
5240 /* local variable */
5241 vla = sym_push(v, type, r, addr);
5242 } else {
5243 /* push local reference */
5244 vset(type, r, addr);
5246 } else {
5247 Sym *sym;
5249 sym = NULL;
5250 if (v && scope == VT_CONST) {
5251 /* see if the symbol was already defined */
5252 sym = sym_find(v);
5253 if (sym) {
5254 if (!is_compatible_types(&sym->type, type))
5255 error("incompatible types for redefinition of '%s'",
5256 get_tok_str(v, NULL));
5257 if (sym->type.t & VT_EXTERN) {
5258 /* if the variable is extern, it was not allocated */
5259 sym->type.t &= ~VT_EXTERN;
5260 /* set array size if it was ommited in extern
5261 declaration */
5262 if ((sym->type.t & VT_ARRAY) &&
5263 sym->type.ref->c < 0 &&
5264 type->ref->c >= 0)
5265 sym->type.ref->c = type->ref->c;
5266 } else {
5267 /* we accept several definitions of the same
5268 global variable. this is tricky, because we
5269 must play with the SHN_COMMON type of the symbol */
5270 /* XXX: should check if the variable was already
5271 initialized. It is incorrect to initialized it
5272 twice */
5273 /* no init data, we won't add more to the symbol */
5274 if (!has_init)
5275 goto no_alloc;
5280 /* allocate symbol in corresponding section */
5281 sec = ad->section;
5282 if (!sec) {
5283 if (has_init)
5284 sec = data_section;
5285 else if (tcc_state->nocommon)
5286 sec = bss_section;
5288 if (sec) {
5289 data_offset = sec->data_offset;
5290 data_offset = (data_offset + align - 1) & -align;
5291 addr = data_offset;
5292 /* very important to increment global pointer at this time
5293 because initializers themselves can create new initializers */
5294 data_offset += size;
5295 #ifdef CONFIG_TCC_BCHECK
5296 /* add padding if bound check */
5297 if (tcc_state->do_bounds_check)
5298 data_offset++;
5299 #endif
5300 sec->data_offset = data_offset;
5301 /* allocate section space to put the data */
5302 if (sec->sh_type != SHT_NOBITS &&
5303 data_offset > sec->data_allocated)
5304 section_realloc(sec, data_offset);
5305 /* align section if needed */
5306 if (align > sec->sh_addralign)
5307 sec->sh_addralign = align;
5308 } else {
5309 addr = 0; /* avoid warning */
5312 if (v) {
5313 if (scope != VT_CONST || !sym) {
5314 sym = sym_push(v, type, r | VT_SYM, 0);
5315 sym->asm_label = asm_label;
5317 /* update symbol definition */
5318 if (sec) {
5319 put_extern_sym(sym, sec, addr, size);
5320 } else {
5321 ElfW(Sym) *esym;
5322 /* put a common area */
5323 put_extern_sym(sym, NULL, align, size);
5324 /* XXX: find a nicer way */
5325 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5326 esym->st_shndx = SHN_COMMON;
5328 } else {
5329 CValue cval;
5331 /* push global reference */
5332 sym = get_sym_ref(type, sec, addr, size);
5333 cval.ul = 0;
5334 vsetc(type, VT_CONST | VT_SYM, &cval);
5335 vtop->sym = sym;
5337 /* patch symbol weakness */
5338 if (type->t & VT_WEAK)
5339 weaken_symbol(sym);
5340 #ifdef CONFIG_TCC_BCHECK
5341 /* handles bounds now because the symbol must be defined
5342 before for the relocation */
5343 if (tcc_state->do_bounds_check) {
5344 unsigned long *bounds_ptr;
5346 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5347 /* then add global bound info */
5348 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5349 bounds_ptr[0] = 0; /* relocated */
5350 bounds_ptr[1] = size;
5352 #endif
5354 if (has_init || (type->t & VT_VLA)) {
5355 decl_initializer(type, sec, addr, 1, 0);
5356 if (type->t & VT_VLA)
5357 vla->s = vtop;
5358 /* restore parse state if needed */
5359 if (init_str.str) {
5360 tok_str_free(init_str.str);
5361 restore_parse_state(&saved_parse_state);
5363 /* patch flexible array member size back to -1, */
5364 /* for possible subsequent similar declarations */
5365 if (flexible_array)
5366 flexible_array->type.ref->c = -1;
5368 no_alloc: ;
5371 static void put_func_debug(Sym *sym)
5373 char buf[512];
5375 /* stabs info */
5376 /* XXX: we put here a dummy type */
5377 snprintf(buf, sizeof(buf), "%s:%c1",
5378 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5379 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5380 cur_text_section, sym->c);
5381 /* //gr gdb wants a line at the function */
5382 put_stabn(N_SLINE, 0, file->line_num, 0);
5383 last_ind = 0;
5384 last_line_num = 0;
5387 /* parse an old style function declaration list */
5388 /* XXX: check multiple parameter */
5389 static void func_decl_list(Sym *func_sym)
5391 AttributeDef ad;
5392 int v;
5393 Sym *s;
5394 CType btype, type;
5396 /* parse each declaration */
5397 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5398 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5399 if (!parse_btype(&btype, &ad))
5400 expect("declaration list");
5401 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5402 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5403 tok == ';') {
5404 /* we accept no variable after */
5405 } else {
5406 for(;;) {
5407 type = btype;
5408 type_decl(&type, &ad, &v, TYPE_DIRECT);
5409 /* find parameter in function parameter list */
5410 s = func_sym->next;
5411 while (s != NULL) {
5412 if ((s->v & ~SYM_FIELD) == v)
5413 goto found;
5414 s = s->next;
5416 error("declaration for parameter '%s' but no such parameter",
5417 get_tok_str(v, NULL));
5418 found:
5419 /* check that no storage specifier except 'register' was given */
5420 if (type.t & VT_STORAGE)
5421 error("storage class specified for '%s'", get_tok_str(v, NULL));
5422 convert_parameter_type(&type);
5423 /* we can add the type (NOTE: it could be local to the function) */
5424 s->type = type;
5425 /* accept other parameters */
5426 if (tok == ',')
5427 next();
5428 else
5429 break;
5432 skip(';');
5436 /* parse a function defined by symbol 'sym' and generate its code in
5437 'cur_text_section' */
5438 static void gen_function(Sym *sym)
5440 int saved_nocode_wanted = nocode_wanted;
5441 nocode_wanted = 0;
5442 ind = cur_text_section->data_offset;
5443 /* NOTE: we patch the symbol size later */
5444 put_extern_sym(sym, cur_text_section, ind, 0);
5445 funcname = get_tok_str(sym->v, NULL);
5446 func_ind = ind;
5447 /* put debug symbol */
5448 if (tcc_state->do_debug)
5449 put_func_debug(sym);
5450 /* push a dummy symbol to enable local sym storage */
5451 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5452 gfunc_prolog(&sym->type);
5453 rsym = 0;
5454 block(NULL, NULL, NULL, NULL, 0, 0);
5455 gsym(rsym);
5456 gfunc_epilog();
5457 cur_text_section->data_offset = ind;
5458 label_pop(&global_label_stack, NULL);
5459 sym_pop(&local_stack, NULL); /* reset local stack */
5460 /* end of function */
5461 /* patch symbol size */
5462 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5463 ind - func_ind;
5464 /* patch symbol weakness (this definition overrules any prototype) */
5465 if (sym->type.t & VT_WEAK)
5466 weaken_symbol(sym);
5467 if (tcc_state->do_debug) {
5468 put_stabn(N_FUN, 0, 0, ind - func_ind);
5470 /* It's better to crash than to generate wrong code */
5471 cur_text_section = NULL;
5472 funcname = ""; /* for safety */
5473 func_vt.t = VT_VOID; /* for safety */
5474 ind = 0; /* for safety */
5475 nocode_wanted = saved_nocode_wanted;
5478 ST_FUNC void gen_inline_functions(void)
5480 Sym *sym;
5481 int *str, inline_generated, i;
5482 struct InlineFunc *fn;
5484 /* iterate while inline function are referenced */
5485 for(;;) {
5486 inline_generated = 0;
5487 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5488 fn = tcc_state->inline_fns[i];
5489 sym = fn->sym;
5490 if (sym && sym->c) {
5491 /* the function was used: generate its code and
5492 convert it to a normal function */
5493 str = fn->token_str;
5494 fn->sym = NULL;
5495 if (file)
5496 strcpy(file->filename, fn->filename);
5497 sym->r = VT_SYM | VT_CONST;
5498 sym->type.t &= ~VT_INLINE;
5500 macro_ptr = str;
5501 next();
5502 cur_text_section = text_section;
5503 gen_function(sym);
5504 macro_ptr = NULL; /* fail safe */
5506 inline_generated = 1;
5509 if (!inline_generated)
5510 break;
5512 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5513 fn = tcc_state->inline_fns[i];
5514 str = fn->token_str;
5515 tok_str_free(str);
5517 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5520 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5521 static int decl0(int l, int is_for_loop_init)
5523 int v, has_init, r;
5524 CType type, btype;
5525 Sym *sym;
5526 AttributeDef ad;
5528 while (1) {
5529 if (!parse_btype(&btype, &ad)) {
5530 if (is_for_loop_init)
5531 return 0;
5532 /* skip redundant ';' */
5533 /* XXX: find more elegant solution */
5534 if (tok == ';') {
5535 next();
5536 continue;
5538 if (l == VT_CONST &&
5539 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5540 /* global asm block */
5541 asm_global_instr();
5542 continue;
5544 /* special test for old K&R protos without explicit int
5545 type. Only accepted when defining global data */
5546 if (l == VT_LOCAL || tok < TOK_DEFINE)
5547 break;
5548 btype.t = VT_INT;
5550 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5551 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5552 tok == ';') {
5553 /* we accept no variable after */
5554 next();
5555 continue;
5557 while (1) { /* iterate thru each declaration */
5558 char *asm_label; // associated asm label
5559 type = btype;
5560 type_decl(&type, &ad, &v, TYPE_DIRECT);
5561 #if 0
5563 char buf[500];
5564 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5565 printf("type = '%s'\n", buf);
5567 #endif
5568 if ((type.t & VT_BTYPE) == VT_FUNC) {
5569 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5570 error("function without file scope cannot be static");
5572 /* if old style function prototype, we accept a
5573 declaration list */
5574 sym = type.ref;
5575 if (sym->c == FUNC_OLD)
5576 func_decl_list(sym);
5579 asm_label = NULL;
5580 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5581 CString astr;
5583 asm_label_instr(&astr);
5584 asm_label = tcc_strdup(astr.data);
5585 cstr_free(&astr);
5587 /* parse one last attribute list, after asm label */
5588 parse_attribute(&ad);
5591 if (ad.weak)
5592 type.t |= VT_WEAK;
5593 #ifdef TCC_TARGET_PE
5594 if (ad.func_import)
5595 type.t |= VT_IMPORT;
5596 if (ad.func_export)
5597 type.t |= VT_EXPORT;
5598 #endif
5599 if (tok == '{') {
5600 if (l == VT_LOCAL)
5601 error("cannot use local functions");
5602 if ((type.t & VT_BTYPE) != VT_FUNC)
5603 expect("function definition");
5605 /* reject abstract declarators in function definition */
5606 sym = type.ref;
5607 while ((sym = sym->next) != NULL)
5608 if (!(sym->v & ~SYM_FIELD))
5609 expect("identifier");
5611 /* XXX: cannot do better now: convert extern line to static inline */
5612 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5613 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5615 sym = sym_find(v);
5616 if (sym) {
5617 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5618 goto func_error1;
5620 r = sym->type.ref->r;
5621 /* use func_call from prototype if not defined */
5622 if (FUNC_CALL(r) != FUNC_CDECL
5623 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5624 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5626 /* use export from prototype */
5627 if (FUNC_EXPORT(r))
5628 FUNC_EXPORT(type.ref->r) = 1;
5630 /* use static from prototype */
5631 if (sym->type.t & VT_STATIC)
5632 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5634 if (!is_compatible_types(&sym->type, &type)) {
5635 func_error1:
5636 error("incompatible types for redefinition of '%s'",
5637 get_tok_str(v, NULL));
5639 /* if symbol is already defined, then put complete type */
5640 sym->type = type;
5641 } else {
5642 /* put function symbol */
5643 sym = global_identifier_push(v, type.t, 0);
5644 sym->type.ref = type.ref;
5647 /* static inline functions are just recorded as a kind
5648 of macro. Their code will be emitted at the end of
5649 the compilation unit only if they are used */
5650 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5651 (VT_INLINE | VT_STATIC)) {
5652 TokenString func_str;
5653 int block_level;
5654 struct InlineFunc *fn;
5655 const char *filename;
5657 tok_str_new(&func_str);
5659 block_level = 0;
5660 for(;;) {
5661 int t;
5662 if (tok == TOK_EOF)
5663 error("unexpected end of file");
5664 tok_str_add_tok(&func_str);
5665 t = tok;
5666 next();
5667 if (t == '{') {
5668 block_level++;
5669 } else if (t == '}') {
5670 block_level--;
5671 if (block_level == 0)
5672 break;
5675 tok_str_add(&func_str, -1);
5676 tok_str_add(&func_str, 0);
5677 filename = file ? file->filename : "";
5678 fn = tcc_malloc(sizeof *fn + strlen(filename));
5679 strcpy(fn->filename, filename);
5680 fn->sym = sym;
5681 fn->token_str = func_str.str;
5682 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5684 } else {
5685 /* compute text section */
5686 cur_text_section = ad.section;
5687 if (!cur_text_section)
5688 cur_text_section = text_section;
5689 sym->r = VT_SYM | VT_CONST;
5690 gen_function(sym);
5692 break;
5693 } else {
5694 if (btype.t & VT_TYPEDEF) {
5695 /* save typedefed type */
5696 /* XXX: test storage specifiers ? */
5697 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5698 sym->type.t |= VT_TYPEDEF;
5699 } else {
5700 r = 0;
5701 if ((type.t & VT_BTYPE) == VT_FUNC) {
5702 /* external function definition */
5703 /* specific case for func_call attribute */
5704 type.ref->r = INT_ATTR(&ad);
5705 } else if (!(type.t & VT_ARRAY)) {
5706 /* not lvalue if array */
5707 r |= lvalue_type(type.t);
5709 has_init = (tok == '=');
5710 if (has_init && (type.t & VT_VLA))
5711 error("Variable length array cannot be initialized");
5712 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5713 ((type.t & VT_ARRAY) && (!(type.t & VT_VLA)) &&
5714 (type.t & VT_STATIC) && !has_init && l == VT_CONST &&
5715 type.ref->c < 0)) {
5716 /* external variable or function */
5717 /* NOTE: as GCC, uninitialized global static
5718 arrays of null size are considered as
5719 extern */
5720 sym = external_sym(v, &type, r, asm_label);
5722 if (type.t & VT_WEAK)
5723 weaken_symbol(sym);
5725 if (ad.alias_target) {
5726 Section tsec;
5727 Elf32_Sym *esym;
5728 Sym *alias_target;
5730 alias_target = sym_find(ad.alias_target);
5731 if (!alias_target || !alias_target->c)
5732 error("unsupported forward __alias__ attribute");
5733 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5734 tsec.sh_num = esym->st_shndx;
5735 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5737 } else {
5738 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5739 if (type.t & VT_STATIC)
5740 r |= VT_CONST;
5741 else
5742 r |= l;
5743 if (has_init)
5744 next();
5745 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5748 if (tok != ',') {
5749 if (is_for_loop_init)
5750 return 1;
5751 skip(';');
5752 break;
5754 next();
5758 return 0;
5761 ST_FUNC void decl(int l)
5763 decl0(l, 0);