Add support for thread-local storage variables
[tinycc.git] / tccgen.c
blobbfe461f5e675187ad706e646e10dc2e40a10c6b9
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 *tdata_section, *tbss_section; /* thread-local storage sections */
35 ST_DATA Section *cur_text_section; /* current section where function code is generated */
36 #ifdef CONFIG_TCC_ASM
37 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
38 #endif
39 #ifdef CONFIG_TCC_BCHECK
40 /* bound check related sections */
41 ST_DATA Section *bounds_section; /* contains global data bound description */
42 ST_DATA Section *lbounds_section; /* contains local data bound description */
43 #endif
44 /* symbol sections */
45 ST_DATA Section *symtab_section, *strtab_section;
46 /* debug sections */
47 ST_DATA Section *stab_section, *stabstr_section;
48 ST_DATA Sym *sym_free_first;
49 ST_DATA void **sym_pools;
50 ST_DATA int nb_sym_pools;
52 ST_DATA Sym *global_stack;
53 ST_DATA Sym *local_stack;
54 ST_DATA Sym *scope_stack_bottom;
55 ST_DATA Sym *define_stack;
56 ST_DATA Sym *global_label_stack;
57 ST_DATA Sym *local_label_stack;
59 ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
60 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
61 ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
62 ST_DATA int vla_flags; /* VLA_* flags */
64 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
66 ST_DATA int const_wanted; /* true if constant wanted */
67 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
68 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
69 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
70 ST_DATA int func_vc;
71 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
72 ST_DATA char *funcname;
74 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType *type);
78 static inline CType *pointed_type(CType *type);
79 static int is_compatible_types(CType *type1, CType *type2);
80 static int parse_btype(CType *type, AttributeDef *ad);
81 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
82 static void parse_expr_type(CType *type);
83 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
84 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
85 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
86 static int decl0(int l, int is_for_loop_init);
87 static void expr_eq(void);
88 static void unary_type(CType *type);
89 static void vla_runtime_type_size(CType *type, int *a);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType *type1, CType *type2);
92 static void expr_type(CType *type);
94 ST_INLN int is_float(int t)
96 int bt;
97 bt = t & VT_BTYPE;
98 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC int ieee_finite(double d)
106 int *p = (int *)&d;
107 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
110 ST_FUNC void test_lvalue(void)
112 if (!(vtop->r & VT_LVAL))
113 expect("lvalue");
116 /* ------------------------------------------------------------------------- */
117 /* symbol allocator */
118 static Sym *__sym_malloc(void)
120 Sym *sym_pool, *sym, *last_sym;
121 int i;
123 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
124 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
126 last_sym = sym_free_first;
127 sym = sym_pool;
128 for(i = 0; i < SYM_POOL_NB; i++) {
129 sym->next = last_sym;
130 last_sym = sym;
131 sym++;
133 sym_free_first = last_sym;
134 return last_sym;
137 static inline Sym *sym_malloc(void)
139 Sym *sym;
140 sym = sym_free_first;
141 if (!sym)
142 sym = __sym_malloc();
143 sym_free_first = sym->next;
144 return sym;
147 ST_INLN void sym_free(Sym *sym)
149 sym->next = sym_free_first;
150 tcc_free(sym->asm_label);
151 sym_free_first = sym;
154 /* push, without hashing */
155 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
157 Sym *s;
158 if (ps == &local_stack) {
159 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
160 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
161 tcc_error("incompatible types for redefinition of '%s'",
162 get_tok_str(v, NULL));
164 s = *ps;
165 s = sym_malloc();
166 s->asm_label = NULL;
167 s->v = v;
168 s->type.t = t;
169 s->type.ref = NULL;
170 #ifdef _WIN64
171 s->d = NULL;
172 #endif
173 s->c = c;
174 s->next = NULL;
175 /* add in stack */
176 s->prev = *ps;
177 *ps = s;
178 return s;
181 /* find a symbol and return its associated structure. 's' is the top
182 of the symbol stack */
183 ST_FUNC Sym *sym_find2(Sym *s, int v)
185 while (s) {
186 if (s->v == v)
187 return s;
188 s = s->prev;
190 return NULL;
193 /* structure lookup */
194 ST_INLN Sym *struct_find(int v)
196 v -= TOK_IDENT;
197 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
198 return NULL;
199 return table_ident[v]->sym_struct;
202 /* find an identifier */
203 ST_INLN Sym *sym_find(int v)
205 v -= TOK_IDENT;
206 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
207 return NULL;
208 return table_ident[v]->sym_identifier;
211 /* push a given symbol on the symbol stack */
212 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
214 Sym *s, **ps;
215 TokenSym *ts;
217 if (local_stack)
218 ps = &local_stack;
219 else
220 ps = &global_stack;
221 s = sym_push2(ps, v, type->t, c);
222 s->type.ref = type->ref;
223 s->r = r;
224 /* don't record fields or anonymous symbols */
225 /* XXX: simplify */
226 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
227 /* record symbol in token array */
228 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
229 if (v & SYM_STRUCT)
230 ps = &ts->sym_struct;
231 else
232 ps = &ts->sym_identifier;
233 s->prev_tok = *ps;
234 *ps = s;
236 return s;
239 /* push a global identifier */
240 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
242 Sym *s, **ps;
243 s = sym_push2(&global_stack, v, t, c);
244 /* don't record anonymous symbol */
245 if (v < SYM_FIRST_ANOM) {
246 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
247 /* modify the top most local identifier, so that
248 sym_identifier will point to 's' when popped */
249 while (*ps != NULL)
250 ps = &(*ps)->prev_tok;
251 s->prev_tok = NULL;
252 *ps = s;
254 return s;
257 /* pop symbols until top reaches 'b' */
258 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
260 Sym *s, *ss, **ps;
261 TokenSym *ts;
262 int v;
264 s = *ptop;
265 while(s != b) {
266 ss = s->prev;
267 v = s->v;
268 /* remove symbol in token array */
269 /* XXX: simplify */
270 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
271 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
272 if (v & SYM_STRUCT)
273 ps = &ts->sym_struct;
274 else
275 ps = &ts->sym_identifier;
276 *ps = s->prev_tok;
278 sym_free(s);
279 s = ss;
281 *ptop = b;
284 static void weaken_symbol(Sym *sym)
286 sym->type.t |= VT_WEAK;
287 if (sym->c > 0) {
288 int esym_type;
289 ElfW(Sym) *esym;
291 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
292 esym_type = ELFW(ST_TYPE)(esym->st_info);
293 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
297 /* ------------------------------------------------------------------------- */
299 ST_FUNC void swap(int *p, int *q)
301 int t;
302 t = *p;
303 *p = *q;
304 *q = t;
307 static void vsetc(CType *type, int r, CValue *vc)
309 int v;
311 if (vtop >= vstack + (VSTACK_SIZE - 1))
312 tcc_error("memory full");
313 /* cannot let cpu flags if other instruction are generated. Also
314 avoid leaving VT_JMP anywhere except on the top of the stack
315 because it would complicate the code generator. */
316 if (vtop >= vstack) {
317 v = vtop->r & VT_VALMASK;
318 if (v == VT_CMP || (v & ~1) == VT_JMP)
319 gv(RC_INT);
321 vtop++;
322 vtop->type = *type;
323 vtop->r = r;
324 vtop->r2 = VT_CONST;
325 vtop->c = *vc;
328 /* push constant of type "type" with useless value */
329 void vpush(CType *type)
331 CValue cval;
332 vsetc(type, VT_CONST, &cval);
335 /* push integer constant */
336 ST_FUNC void vpushi(int v)
338 CValue cval;
339 cval.i = v;
340 vsetc(&int_type, VT_CONST, &cval);
343 /* push a pointer sized constant */
344 static void vpushs(long long v)
346 CValue cval;
347 if (PTR_SIZE == 4)
348 cval.i = (int)v;
349 else
350 cval.ull = v;
351 vsetc(&size_type, VT_CONST, &cval);
354 /* push arbitrary 64bit constant */
355 void vpush64(int ty, unsigned long long v)
357 CValue cval;
358 CType ctype;
359 ctype.t = ty;
360 ctype.ref = NULL;
361 cval.ull = v;
362 vsetc(&ctype, VT_CONST, &cval);
365 /* push long long constant */
366 static inline void vpushll(long long v)
368 vpush64(VT_LLONG, v);
371 /* Return a static symbol pointing to a section */
372 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
374 int v;
375 Sym *sym;
377 v = anon_sym++;
378 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
379 sym->type.ref = type->ref;
380 sym->r = VT_CONST | VT_SYM;
381 put_extern_sym(sym, sec, offset, size);
382 return sym;
385 /* push a reference to a section offset by adding a dummy symbol */
386 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
388 CValue cval;
390 cval.ul = 0;
391 vsetc(type, VT_CONST | VT_SYM, &cval);
392 vtop->sym = get_sym_ref(type, sec, offset, size);
395 /* define a new external reference to a symbol 'v' of type 'u' */
396 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
398 Sym *s;
400 s = sym_find(v);
401 if (!s) {
402 /* push forward reference */
403 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
404 s->type.ref = type->ref;
405 s->r = r | VT_CONST | VT_SYM;
407 return s;
410 /* define a new external reference to a symbol 'v' with alternate asm
411 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
412 is no alternate name (most cases) */
413 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
415 Sym *s;
417 s = sym_find(v);
418 if (!s) {
419 /* push forward reference */
420 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
421 s->asm_label = asm_label;
422 s->type.t |= VT_EXTERN;
423 } else if (s->type.ref == func_old_type.ref) {
424 s->type.ref = type->ref;
425 s->r = r | VT_CONST | VT_SYM;
426 s->type.t |= VT_EXTERN;
427 } else if (!is_compatible_types(&s->type, type)) {
428 tcc_error("incompatible types for redefinition of '%s'",
429 get_tok_str(v, NULL));
431 return s;
434 /* push a reference to global symbol v */
435 ST_FUNC void vpush_global_sym(CType *type, int v)
437 Sym *sym;
438 CValue cval;
440 sym = external_global_sym(v, type, 0);
441 cval.ul = 0;
442 vsetc(type, VT_CONST | VT_SYM, &cval);
443 vtop->sym = sym;
446 ST_FUNC void vset(CType *type, int r, int v)
448 CValue cval;
450 cval.i = v;
451 vsetc(type, r, &cval);
454 static void vseti(int r, int v)
456 CType type;
457 type.t = VT_INT;
458 type.ref = 0;
459 vset(&type, r, v);
462 ST_FUNC void vswap(void)
464 SValue tmp;
465 /* cannot let cpu flags if other instruction are generated. Also
466 avoid leaving VT_JMP anywhere except on the top of the stack
467 because it would complicate the code generator. */
468 if (vtop >= vstack) {
469 int v = vtop->r & VT_VALMASK;
470 if (v == VT_CMP || (v & ~1) == VT_JMP)
471 gv(RC_INT);
473 tmp = vtop[0];
474 vtop[0] = vtop[-1];
475 vtop[-1] = tmp;
477 /* XXX: +2% overall speed possible with optimized memswap
479 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
483 ST_FUNC void vpushv(SValue *v)
485 if (vtop >= vstack + (VSTACK_SIZE - 1))
486 tcc_error("memory full");
487 vtop++;
488 *vtop = *v;
491 static void vdup(void)
493 vpushv(vtop);
496 /* save r to the memory stack, and mark it as being free */
497 ST_FUNC void save_reg(int r)
499 int l, saved, size, align;
500 SValue *p, sv;
501 CType *type;
503 /* modify all stack values */
504 saved = 0;
505 l = 0;
506 for(p=vstack;p<=vtop;p++) {
507 if ((p->r & VT_VALMASK) == r ||
508 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
509 /* must save value on stack if not already done */
510 if (!saved) {
511 /* NOTE: must reload 'r' because r might be equal to r2 */
512 r = p->r & VT_VALMASK;
513 /* store register in the stack */
514 type = &p->type;
515 if ((p->r & VT_LVAL) ||
516 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
517 #ifdef TCC_TARGET_X86_64
518 type = &char_pointer_type;
519 #else
520 type = &int_type;
521 #endif
522 size = type_size(type, &align);
523 loc = (loc - size) & -align;
524 sv.type.t = type->t;
525 sv.r = VT_LOCAL | VT_LVAL;
526 sv.c.ul = loc;
527 store(r, &sv);
528 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
529 /* x86 specific: need to pop fp register ST0 if saved */
530 if (r == TREG_ST0) {
531 o(0xd8dd); /* fstp %st(0) */
533 #endif
534 #ifndef TCC_TARGET_X86_64
535 /* special long long case */
536 if ((type->t & VT_BTYPE) == VT_LLONG) {
537 sv.c.ul += 4;
538 store(p->r2, &sv);
540 #endif
541 l = loc;
542 saved = 1;
544 /* mark that stack entry as being saved on the stack */
545 if (p->r & VT_LVAL) {
546 /* also clear the bounded flag because the
547 relocation address of the function was stored in
548 p->c.ul */
549 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
550 } else {
551 p->r = lvalue_type(p->type.t) | VT_LOCAL;
553 p->r2 = VT_CONST;
554 p->c.ul = l;
559 #ifdef TCC_TARGET_ARM
560 /* find a register of class 'rc2' with at most one reference on stack.
561 * If none, call get_reg(rc) */
562 ST_FUNC int get_reg_ex(int rc, int rc2)
564 int r;
565 SValue *p;
567 for(r=0;r<NB_REGS;r++) {
568 if (reg_classes[r] & rc2) {
569 int n;
570 n=0;
571 for(p = vstack; p <= vtop; p++) {
572 if ((p->r & VT_VALMASK) == r ||
573 (p->r2 & VT_VALMASK) == r)
574 n++;
576 if (n <= 1)
577 return r;
580 return get_reg(rc);
582 #endif
584 /* find a free register of class 'rc'. If none, save one register */
585 ST_FUNC int get_reg(int rc)
587 int r;
588 SValue *p;
590 /* find a free register */
591 for(r=0;r<NB_REGS;r++) {
592 if (reg_classes[r] & rc) {
593 for(p=vstack;p<=vtop;p++) {
594 if ((p->r & VT_VALMASK) == r ||
595 (p->r2 & VT_VALMASK) == r)
596 goto notfound;
598 return r;
600 notfound: ;
603 /* no register left : free the first one on the stack (VERY
604 IMPORTANT to start from the bottom to ensure that we don't
605 spill registers used in gen_opi()) */
606 for(p=vstack;p<=vtop;p++) {
607 /* look at second register (if long long) */
608 r = p->r2 & VT_VALMASK;
609 if (r < VT_CONST && (reg_classes[r] & rc))
610 goto save_found;
611 r = p->r & VT_VALMASK;
612 if (r < VT_CONST && (reg_classes[r] & rc)) {
613 save_found:
614 save_reg(r);
615 return r;
618 /* Should never comes here */
619 return -1;
622 /* save registers up to (vtop - n) stack entry */
623 ST_FUNC void save_regs(int n)
625 int r;
626 SValue *p, *p1;
627 p1 = vtop - n;
628 for(p = vstack;p <= p1; p++) {
629 r = p->r & VT_VALMASK;
630 if (r < VT_CONST) {
631 save_reg(r);
636 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
637 if needed */
638 static void move_reg(int r, int s, int t)
640 SValue sv;
642 if (r != s) {
643 save_reg(r);
644 sv.type.t = t;
645 sv.type.ref = NULL;
646 sv.r = s;
647 sv.c.ul = 0;
648 load(r, &sv);
652 /* get address of vtop (vtop MUST BE an lvalue) */
653 static void gaddrof(void)
655 if (vtop->r & VT_REF)
656 gv(RC_INT);
657 vtop->r &= ~VT_LVAL;
658 /* tricky: if saved lvalue, then we can go back to lvalue */
659 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
660 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
665 #ifdef CONFIG_TCC_BCHECK
666 /* generate lvalue bound code */
667 static void gbound(void)
669 int lval_type;
670 CType type1;
672 vtop->r &= ~VT_MUSTBOUND;
673 /* if lvalue, then use checking code before dereferencing */
674 if (vtop->r & VT_LVAL) {
675 /* if not VT_BOUNDED value, then make one */
676 if (!(vtop->r & VT_BOUNDED)) {
677 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
678 /* must save type because we must set it to int to get pointer */
679 type1 = vtop->type;
680 vtop->type.t = VT_INT;
681 gaddrof();
682 vpushi(0);
683 gen_bounded_ptr_add();
684 vtop->r |= lval_type;
685 vtop->type = type1;
687 /* then check for dereferencing */
688 gen_bounded_ptr_deref();
691 #endif
693 /* store vtop a register belonging to class 'rc'. lvalues are
694 converted to values. Cannot be used if cannot be converted to
695 register value (such as structures). */
696 ST_FUNC int gv(int rc)
698 int r, bit_pos, bit_size, size, align, i;
699 int rc2;
701 /* NOTE: get_reg can modify vstack[] */
702 if (vtop->type.t & VT_BITFIELD) {
703 CType type;
704 int bits = 32;
705 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
706 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
707 /* remove bit field info to avoid loops */
708 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
709 /* cast to int to propagate signedness in following ops */
710 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
711 type.t = VT_LLONG;
712 bits = 64;
713 } else
714 type.t = VT_INT;
715 if((vtop->type.t & VT_UNSIGNED) ||
716 (vtop->type.t & VT_BTYPE) == VT_BOOL)
717 type.t |= VT_UNSIGNED;
718 gen_cast(&type);
719 /* generate shifts */
720 vpushi(bits - (bit_pos + bit_size));
721 gen_op(TOK_SHL);
722 vpushi(bits - bit_size);
723 /* NOTE: transformed to SHR if unsigned */
724 gen_op(TOK_SAR);
725 r = gv(rc);
726 } else {
727 if (is_float(vtop->type.t) &&
728 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
729 Sym *sym;
730 int *ptr;
731 unsigned long offset;
732 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
733 CValue check;
734 #endif
736 /* XXX: unify with initializers handling ? */
737 /* CPUs usually cannot use float constants, so we store them
738 generically in data segment */
739 size = type_size(&vtop->type, &align);
740 offset = (data_section->data_offset + align - 1) & -align;
741 data_section->data_offset = offset;
742 /* XXX: not portable yet */
743 #if defined(__i386__) || defined(__x86_64__)
744 /* Zero pad x87 tenbyte long doubles */
745 if (size == LDOUBLE_SIZE) {
746 vtop->c.tab[2] &= 0xffff;
747 #if LDOUBLE_SIZE == 16
748 vtop->c.tab[3] = 0;
749 #endif
751 #endif
752 ptr = section_ptr_add(data_section, size);
753 size = size >> 2;
754 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
755 check.d = 1;
756 if(check.tab[0])
757 for(i=0;i<size;i++)
758 ptr[i] = vtop->c.tab[size-1-i];
759 else
760 #endif
761 for(i=0;i<size;i++)
762 ptr[i] = vtop->c.tab[i];
763 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
764 vtop->r |= VT_LVAL | VT_SYM;
765 vtop->sym = sym;
766 vtop->c.ul = 0;
768 #ifdef CONFIG_TCC_BCHECK
769 if (vtop->r & VT_MUSTBOUND)
770 gbound();
771 #endif
773 r = vtop->r & VT_VALMASK;
774 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
775 if (rc == RC_IRET)
776 rc2 = RC_LRET;
777 #ifdef TCC_TARGET_X86_64
778 else if (rc == RC_FRET)
779 rc2 = RC_QRET;
780 #endif
782 /* need to reload if:
783 - constant
784 - lvalue (need to dereference pointer)
785 - already a register, but not in the right class */
786 if (r >= VT_CONST
787 || (vtop->r & VT_LVAL)
788 || !(reg_classes[r] & rc)
789 #ifdef TCC_TARGET_X86_64
790 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
791 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
792 #else
793 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
794 #endif
797 r = get_reg(rc);
798 #ifdef TCC_TARGET_X86_64
799 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
800 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
801 #else
802 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
803 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
804 unsigned long long ll;
805 #endif
806 int r2, original_type;
807 original_type = vtop->type.t;
808 /* two register type load : expand to two words
809 temporarily */
810 #ifndef TCC_TARGET_X86_64
811 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
812 /* load constant */
813 ll = vtop->c.ull;
814 vtop->c.ui = ll; /* first word */
815 load(r, vtop);
816 vtop->r = r; /* save register value */
817 vpushi(ll >> 32); /* second word */
818 } else
819 #endif
820 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
821 (vtop->r & VT_LVAL)) {
822 /* We do not want to modifier the long long
823 pointer here, so the safest (and less
824 efficient) is to save all the other registers
825 in the stack. XXX: totally inefficient. */
826 save_regs(1);
827 /* load from memory */
828 vtop->type.t = load_type;
829 load(r, vtop);
830 vdup();
831 vtop[-1].r = r; /* save register value */
832 /* increment pointer to get second word */
833 vtop->type.t = addr_type;
834 gaddrof();
835 vpushi(load_size);
836 gen_op('+');
837 vtop->r |= VT_LVAL;
838 vtop->type.t = load_type;
839 } else {
840 /* move registers */
841 load(r, vtop);
842 vdup();
843 vtop[-1].r = r; /* save register value */
844 vtop->r = vtop[-1].r2;
846 /* Allocate second register. Here we rely on the fact that
847 get_reg() tries first to free r2 of an SValue. */
848 r2 = get_reg(rc2);
849 load(r2, vtop);
850 vpop();
851 /* write second register */
852 vtop->r2 = r2;
853 vtop->type.t = original_type;
854 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
855 int t1, t;
856 /* lvalue of scalar type : need to use lvalue type
857 because of possible cast */
858 t = vtop->type.t;
859 t1 = t;
860 /* compute memory access type */
861 if (vtop->r & VT_REF)
862 #ifdef TCC_TARGET_X86_64
863 t = VT_PTR;
864 #else
865 t = VT_INT;
866 #endif
867 else if (vtop->r & VT_LVAL_BYTE)
868 t = VT_BYTE;
869 else if (vtop->r & VT_LVAL_SHORT)
870 t = VT_SHORT;
871 if (vtop->r & VT_LVAL_UNSIGNED)
872 t |= VT_UNSIGNED;
873 vtop->type.t = t;
874 load(r, vtop);
875 /* restore wanted type */
876 vtop->type.t = t1;
877 } else {
878 /* one register type load */
879 load(r, vtop);
882 vtop->r = r;
883 #ifdef TCC_TARGET_C67
884 /* uses register pairs for doubles */
885 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
886 vtop->r2 = r+1;
887 #endif
889 return r;
892 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
893 ST_FUNC void gv2(int rc1, int rc2)
895 int v;
897 /* generate more generic register first. But VT_JMP or VT_CMP
898 values must be generated first in all cases to avoid possible
899 reload errors */
900 v = vtop[0].r & VT_VALMASK;
901 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
902 vswap();
903 gv(rc1);
904 vswap();
905 gv(rc2);
906 /* test if reload is needed for first register */
907 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
908 vswap();
909 gv(rc1);
910 vswap();
912 } else {
913 gv(rc2);
914 vswap();
915 gv(rc1);
916 vswap();
917 /* test if reload is needed for first register */
918 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
919 gv(rc2);
924 /* wrapper around RC_FRET to return a register by type */
925 static int rc_fret(int t)
927 #ifdef TCC_TARGET_X86_64
928 if (t == VT_LDOUBLE) {
929 return RC_ST0;
931 #endif
932 return RC_FRET;
935 /* wrapper around REG_FRET to return a register by type */
936 static int reg_fret(int t)
938 #ifdef TCC_TARGET_X86_64
939 if (t == VT_LDOUBLE) {
940 return TREG_ST0;
942 #endif
943 return REG_FRET;
946 /* expand long long on stack in two int registers */
947 static void lexpand(void)
949 int u;
951 u = vtop->type.t & VT_UNSIGNED;
952 gv(RC_INT);
953 vdup();
954 vtop[0].r = vtop[-1].r2;
955 vtop[0].r2 = VT_CONST;
956 vtop[-1].r2 = VT_CONST;
957 vtop[0].type.t = VT_INT | u;
958 vtop[-1].type.t = VT_INT | u;
961 #ifdef TCC_TARGET_ARM
962 /* expand long long on stack */
963 ST_FUNC void lexpand_nr(void)
965 int u,v;
967 u = vtop->type.t & VT_UNSIGNED;
968 vdup();
969 vtop->r2 = VT_CONST;
970 vtop->type.t = VT_INT | u;
971 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
972 if (v == VT_CONST) {
973 vtop[-1].c.ui = vtop->c.ull;
974 vtop->c.ui = vtop->c.ull >> 32;
975 vtop->r = VT_CONST;
976 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
977 vtop->c.ui += 4;
978 vtop->r = vtop[-1].r;
979 } else if (v > VT_CONST) {
980 vtop--;
981 lexpand();
982 } else
983 vtop->r = vtop[-1].r2;
984 vtop[-1].r2 = VT_CONST;
985 vtop[-1].type.t = VT_INT | u;
987 #endif
989 /* build a long long from two ints */
990 static void lbuild(int t)
992 gv2(RC_INT, RC_INT);
993 vtop[-1].r2 = vtop[0].r;
994 vtop[-1].type.t = t;
995 vpop();
998 /* rotate n first stack elements to the bottom
999 I1 ... In -> I2 ... In I1 [top is right]
1001 ST_FUNC void vrotb(int n)
1003 int i;
1004 SValue tmp;
1006 tmp = vtop[-n + 1];
1007 for(i=-n+1;i!=0;i++)
1008 vtop[i] = vtop[i+1];
1009 vtop[0] = tmp;
1012 /* rotate the n elements before entry e towards the top
1013 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1015 ST_FUNC void vrote(SValue *e, int n)
1017 int i;
1018 SValue tmp;
1020 tmp = *e;
1021 for(i = 0;i < n - 1; i++)
1022 e[-i] = e[-i - 1];
1023 e[-n + 1] = tmp;
1026 /* rotate n first stack elements to the top
1027 I1 ... In -> In I1 ... I(n-1) [top is right]
1029 ST_FUNC void vrott(int n)
1031 vrote(vtop, n);
1034 /* pop stack value */
1035 ST_FUNC void vpop(void)
1037 int v;
1038 v = vtop->r & VT_VALMASK;
1039 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1040 /* for x86, we need to pop the FP stack */
1041 if (v == TREG_ST0 && !nocode_wanted) {
1042 o(0xd8dd); /* fstp %st(0) */
1043 } else
1044 #endif
1045 if (v == VT_JMP || v == VT_JMPI) {
1046 /* need to put correct jump if && or || without test */
1047 gsym(vtop->c.ul);
1049 vtop--;
1052 /* convert stack entry to register and duplicate its value in another
1053 register */
1054 static void gv_dup(void)
1056 int rc, t, r, r1;
1057 SValue sv;
1059 t = vtop->type.t;
1060 if ((t & VT_BTYPE) == VT_LLONG) {
1061 lexpand();
1062 gv_dup();
1063 vswap();
1064 vrotb(3);
1065 gv_dup();
1066 vrotb(4);
1067 /* stack: H L L1 H1 */
1068 lbuild(t);
1069 vrotb(3);
1070 vrotb(3);
1071 vswap();
1072 lbuild(t);
1073 vswap();
1074 } else {
1075 /* duplicate value */
1076 rc = RC_INT;
1077 sv.type.t = VT_INT;
1078 if (is_float(t)) {
1079 rc = RC_FLOAT;
1080 #ifdef TCC_TARGET_X86_64
1081 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1082 rc = RC_ST0;
1084 #endif
1085 sv.type.t = t;
1087 r = gv(rc);
1088 r1 = get_reg(rc);
1089 sv.r = r;
1090 sv.c.ul = 0;
1091 load(r1, &sv); /* move r to r1 */
1092 vdup();
1093 /* duplicates value */
1094 if (r != r1)
1095 vtop->r = r1;
1099 #ifndef TCC_TARGET_X86_64
1100 /* generate CPU independent (unsigned) long long operations */
1101 static void gen_opl(int op)
1103 int t, a, b, op1, c, i;
1104 int func;
1105 unsigned short reg_iret = REG_IRET;
1106 unsigned short reg_lret = REG_LRET;
1107 SValue tmp;
1109 switch(op) {
1110 case '/':
1111 case TOK_PDIV:
1112 func = TOK___divdi3;
1113 goto gen_func;
1114 case TOK_UDIV:
1115 func = TOK___udivdi3;
1116 goto gen_func;
1117 case '%':
1118 func = TOK___moddi3;
1119 goto gen_mod_func;
1120 case TOK_UMOD:
1121 func = TOK___umoddi3;
1122 gen_mod_func:
1123 #ifdef TCC_ARM_EABI
1124 reg_iret = TREG_R2;
1125 reg_lret = TREG_R3;
1126 #endif
1127 gen_func:
1128 /* call generic long long function */
1129 vpush_global_sym(&func_old_type, func);
1130 vrott(3);
1131 gfunc_call(2);
1132 vpushi(0);
1133 vtop->r = reg_iret;
1134 vtop->r2 = reg_lret;
1135 break;
1136 case '^':
1137 case '&':
1138 case '|':
1139 case '*':
1140 case '+':
1141 case '-':
1142 t = vtop->type.t;
1143 vswap();
1144 lexpand();
1145 vrotb(3);
1146 lexpand();
1147 /* stack: L1 H1 L2 H2 */
1148 tmp = vtop[0];
1149 vtop[0] = vtop[-3];
1150 vtop[-3] = tmp;
1151 tmp = vtop[-2];
1152 vtop[-2] = vtop[-3];
1153 vtop[-3] = tmp;
1154 vswap();
1155 /* stack: H1 H2 L1 L2 */
1156 if (op == '*') {
1157 vpushv(vtop - 1);
1158 vpushv(vtop - 1);
1159 gen_op(TOK_UMULL);
1160 lexpand();
1161 /* stack: H1 H2 L1 L2 ML MH */
1162 for(i=0;i<4;i++)
1163 vrotb(6);
1164 /* stack: ML MH H1 H2 L1 L2 */
1165 tmp = vtop[0];
1166 vtop[0] = vtop[-2];
1167 vtop[-2] = tmp;
1168 /* stack: ML MH H1 L2 H2 L1 */
1169 gen_op('*');
1170 vrotb(3);
1171 vrotb(3);
1172 gen_op('*');
1173 /* stack: ML MH M1 M2 */
1174 gen_op('+');
1175 gen_op('+');
1176 } else if (op == '+' || op == '-') {
1177 /* XXX: add non carry method too (for MIPS or alpha) */
1178 if (op == '+')
1179 op1 = TOK_ADDC1;
1180 else
1181 op1 = TOK_SUBC1;
1182 gen_op(op1);
1183 /* stack: H1 H2 (L1 op L2) */
1184 vrotb(3);
1185 vrotb(3);
1186 gen_op(op1 + 1); /* TOK_xxxC2 */
1187 } else {
1188 gen_op(op);
1189 /* stack: H1 H2 (L1 op L2) */
1190 vrotb(3);
1191 vrotb(3);
1192 /* stack: (L1 op L2) H1 H2 */
1193 gen_op(op);
1194 /* stack: (L1 op L2) (H1 op H2) */
1196 /* stack: L H */
1197 lbuild(t);
1198 break;
1199 case TOK_SAR:
1200 case TOK_SHR:
1201 case TOK_SHL:
1202 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1203 t = vtop[-1].type.t;
1204 vswap();
1205 lexpand();
1206 vrotb(3);
1207 /* stack: L H shift */
1208 c = (int)vtop->c.i;
1209 /* constant: simpler */
1210 /* NOTE: all comments are for SHL. the other cases are
1211 done by swaping words */
1212 vpop();
1213 if (op != TOK_SHL)
1214 vswap();
1215 if (c >= 32) {
1216 /* stack: L H */
1217 vpop();
1218 if (c > 32) {
1219 vpushi(c - 32);
1220 gen_op(op);
1222 if (op != TOK_SAR) {
1223 vpushi(0);
1224 } else {
1225 gv_dup();
1226 vpushi(31);
1227 gen_op(TOK_SAR);
1229 vswap();
1230 } else {
1231 vswap();
1232 gv_dup();
1233 /* stack: H L L */
1234 vpushi(c);
1235 gen_op(op);
1236 vswap();
1237 vpushi(32 - c);
1238 if (op == TOK_SHL)
1239 gen_op(TOK_SHR);
1240 else
1241 gen_op(TOK_SHL);
1242 vrotb(3);
1243 /* stack: L L H */
1244 vpushi(c);
1245 if (op == TOK_SHL)
1246 gen_op(TOK_SHL);
1247 else
1248 gen_op(TOK_SHR);
1249 gen_op('|');
1251 if (op != TOK_SHL)
1252 vswap();
1253 lbuild(t);
1254 } else {
1255 /* XXX: should provide a faster fallback on x86 ? */
1256 switch(op) {
1257 case TOK_SAR:
1258 func = TOK___ashrdi3;
1259 goto gen_func;
1260 case TOK_SHR:
1261 func = TOK___lshrdi3;
1262 goto gen_func;
1263 case TOK_SHL:
1264 func = TOK___ashldi3;
1265 goto gen_func;
1268 break;
1269 default:
1270 /* compare operations */
1271 t = vtop->type.t;
1272 vswap();
1273 lexpand();
1274 vrotb(3);
1275 lexpand();
1276 /* stack: L1 H1 L2 H2 */
1277 tmp = vtop[-1];
1278 vtop[-1] = vtop[-2];
1279 vtop[-2] = tmp;
1280 /* stack: L1 L2 H1 H2 */
1281 /* compare high */
1282 op1 = op;
1283 /* when values are equal, we need to compare low words. since
1284 the jump is inverted, we invert the test too. */
1285 if (op1 == TOK_LT)
1286 op1 = TOK_LE;
1287 else if (op1 == TOK_GT)
1288 op1 = TOK_GE;
1289 else if (op1 == TOK_ULT)
1290 op1 = TOK_ULE;
1291 else if (op1 == TOK_UGT)
1292 op1 = TOK_UGE;
1293 a = 0;
1294 b = 0;
1295 gen_op(op1);
1296 if (op1 != TOK_NE) {
1297 a = gtst(1, 0);
1299 if (op != TOK_EQ) {
1300 /* generate non equal test */
1301 /* XXX: NOT PORTABLE yet */
1302 if (a == 0) {
1303 b = gtst(0, 0);
1304 } else {
1305 #if defined(TCC_TARGET_I386)
1306 b = psym(0x850f, 0);
1307 #elif defined(TCC_TARGET_ARM)
1308 b = ind;
1309 o(0x1A000000 | encbranch(ind, 0, 1));
1310 #elif defined(TCC_TARGET_C67)
1311 tcc_error("not implemented");
1312 #else
1313 #error not supported
1314 #endif
1317 /* compare low. Always unsigned */
1318 op1 = op;
1319 if (op1 == TOK_LT)
1320 op1 = TOK_ULT;
1321 else if (op1 == TOK_LE)
1322 op1 = TOK_ULE;
1323 else if (op1 == TOK_GT)
1324 op1 = TOK_UGT;
1325 else if (op1 == TOK_GE)
1326 op1 = TOK_UGE;
1327 gen_op(op1);
1328 a = gtst(1, a);
1329 gsym(b);
1330 vseti(VT_JMPI, a);
1331 break;
1334 #endif
1336 /* handle integer constant optimizations and various machine
1337 independent opt */
1338 static void gen_opic(int op)
1340 int c1, c2, t1, t2, n;
1341 SValue *v1, *v2;
1342 long long l1, l2;
1343 typedef unsigned long long U;
1345 v1 = vtop - 1;
1346 v2 = vtop;
1347 t1 = v1->type.t & VT_BTYPE;
1348 t2 = v2->type.t & VT_BTYPE;
1350 if (t1 == VT_LLONG)
1351 l1 = v1->c.ll;
1352 else if (v1->type.t & VT_UNSIGNED)
1353 l1 = v1->c.ui;
1354 else
1355 l1 = v1->c.i;
1357 if (t2 == VT_LLONG)
1358 l2 = v2->c.ll;
1359 else if (v2->type.t & VT_UNSIGNED)
1360 l2 = v2->c.ui;
1361 else
1362 l2 = v2->c.i;
1364 /* currently, we cannot do computations with forward symbols */
1365 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1366 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1367 if (c1 && c2) {
1368 switch(op) {
1369 case '+': l1 += l2; break;
1370 case '-': l1 -= l2; break;
1371 case '&': l1 &= l2; break;
1372 case '^': l1 ^= l2; break;
1373 case '|': l1 |= l2; break;
1374 case '*': l1 *= l2; break;
1376 case TOK_PDIV:
1377 case '/':
1378 case '%':
1379 case TOK_UDIV:
1380 case TOK_UMOD:
1381 /* if division by zero, generate explicit division */
1382 if (l2 == 0) {
1383 if (const_wanted)
1384 tcc_error("division by zero in constant");
1385 goto general_case;
1387 switch(op) {
1388 default: l1 /= l2; break;
1389 case '%': l1 %= l2; break;
1390 case TOK_UDIV: l1 = (U)l1 / l2; break;
1391 case TOK_UMOD: l1 = (U)l1 % l2; break;
1393 break;
1394 case TOK_SHL: l1 <<= l2; break;
1395 case TOK_SHR: l1 = (U)l1 >> l2; break;
1396 case TOK_SAR: l1 >>= l2; break;
1397 /* tests */
1398 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1399 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1400 case TOK_EQ: l1 = l1 == l2; break;
1401 case TOK_NE: l1 = l1 != l2; break;
1402 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1403 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1404 case TOK_LT: l1 = l1 < l2; break;
1405 case TOK_GE: l1 = l1 >= l2; break;
1406 case TOK_LE: l1 = l1 <= l2; break;
1407 case TOK_GT: l1 = l1 > l2; break;
1408 /* logical */
1409 case TOK_LAND: l1 = l1 && l2; break;
1410 case TOK_LOR: l1 = l1 || l2; break;
1411 default:
1412 goto general_case;
1414 v1->c.ll = l1;
1415 vtop--;
1416 } else {
1417 /* if commutative ops, put c2 as constant */
1418 if (c1 && (op == '+' || op == '&' || op == '^' ||
1419 op == '|' || op == '*')) {
1420 vswap();
1421 c2 = c1; //c = c1, c1 = c2, c2 = c;
1422 l2 = l1; //l = l1, l1 = l2, l2 = l;
1424 /* Filter out NOP operations like x*1, x-0, x&-1... */
1425 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1426 op == TOK_PDIV) &&
1427 l2 == 1) ||
1428 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1429 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1430 l2 == 0) ||
1431 (op == '&' &&
1432 l2 == -1))) {
1433 /* nothing to do */
1434 vtop--;
1435 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1436 /* try to use shifts instead of muls or divs */
1437 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1438 n = -1;
1439 while (l2) {
1440 l2 >>= 1;
1441 n++;
1443 vtop->c.ll = n;
1444 if (op == '*')
1445 op = TOK_SHL;
1446 else if (op == TOK_PDIV)
1447 op = TOK_SAR;
1448 else
1449 op = TOK_SHR;
1451 goto general_case;
1452 } else if (c2 && (op == '+' || op == '-') &&
1453 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1454 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1455 /* symbol + constant case */
1456 if (op == '-')
1457 l2 = -l2;
1458 vtop--;
1459 vtop->c.ll += l2;
1460 } else {
1461 general_case:
1462 if (!nocode_wanted) {
1463 /* call low level op generator */
1464 if (t1 == VT_LLONG || t2 == VT_LLONG)
1465 gen_opl(op);
1466 else
1467 gen_opi(op);
1468 } else {
1469 vtop--;
1475 /* generate a floating point operation with constant propagation */
1476 static void gen_opif(int op)
1478 int c1, c2;
1479 SValue *v1, *v2;
1480 long double f1, f2;
1482 v1 = vtop - 1;
1483 v2 = vtop;
1484 /* currently, we cannot do computations with forward symbols */
1485 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1486 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1487 if (c1 && c2) {
1488 if (v1->type.t == VT_FLOAT) {
1489 f1 = v1->c.f;
1490 f2 = v2->c.f;
1491 } else if (v1->type.t == VT_DOUBLE) {
1492 f1 = v1->c.d;
1493 f2 = v2->c.d;
1494 } else {
1495 f1 = v1->c.ld;
1496 f2 = v2->c.ld;
1499 /* NOTE: we only do constant propagation if finite number (not
1500 NaN or infinity) (ANSI spec) */
1501 if (!ieee_finite(f1) || !ieee_finite(f2))
1502 goto general_case;
1504 switch(op) {
1505 case '+': f1 += f2; break;
1506 case '-': f1 -= f2; break;
1507 case '*': f1 *= f2; break;
1508 case '/':
1509 if (f2 == 0.0) {
1510 if (const_wanted)
1511 tcc_error("division by zero in constant");
1512 goto general_case;
1514 f1 /= f2;
1515 break;
1516 /* XXX: also handles tests ? */
1517 default:
1518 goto general_case;
1520 /* XXX: overflow test ? */
1521 if (v1->type.t == VT_FLOAT) {
1522 v1->c.f = f1;
1523 } else if (v1->type.t == VT_DOUBLE) {
1524 v1->c.d = f1;
1525 } else {
1526 v1->c.ld = f1;
1528 vtop--;
1529 } else {
1530 general_case:
1531 if (!nocode_wanted) {
1532 gen_opf(op);
1533 } else {
1534 vtop--;
1539 static int pointed_size(CType *type)
1541 int align;
1542 return type_size(pointed_type(type), &align);
1545 static void vla_runtime_pointed_size(CType *type)
1547 int align;
1548 vla_runtime_type_size(pointed_type(type), &align);
1551 static inline int is_null_pointer(SValue *p)
1553 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1554 return 0;
1555 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1556 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1557 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0);
1560 static inline int is_integer_btype(int bt)
1562 return (bt == VT_BYTE || bt == VT_SHORT ||
1563 bt == VT_INT || bt == VT_LLONG);
1566 /* check types for comparison or substraction of pointers */
1567 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1569 CType *type1, *type2, tmp_type1, tmp_type2;
1570 int bt1, bt2;
1572 /* null pointers are accepted for all comparisons as gcc */
1573 if (is_null_pointer(p1) || is_null_pointer(p2))
1574 return;
1575 type1 = &p1->type;
1576 type2 = &p2->type;
1577 bt1 = type1->t & VT_BTYPE;
1578 bt2 = type2->t & VT_BTYPE;
1579 /* accept comparison between pointer and integer with a warning */
1580 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1581 if (op != TOK_LOR && op != TOK_LAND )
1582 tcc_warning("comparison between pointer and integer");
1583 return;
1586 /* both must be pointers or implicit function pointers */
1587 if (bt1 == VT_PTR) {
1588 type1 = pointed_type(type1);
1589 } else if (bt1 != VT_FUNC)
1590 goto invalid_operands;
1592 if (bt2 == VT_PTR) {
1593 type2 = pointed_type(type2);
1594 } else if (bt2 != VT_FUNC) {
1595 invalid_operands:
1596 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1598 if ((type1->t & VT_BTYPE) == VT_VOID ||
1599 (type2->t & VT_BTYPE) == VT_VOID)
1600 return;
1601 tmp_type1 = *type1;
1602 tmp_type2 = *type2;
1603 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1604 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1605 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1606 /* gcc-like error if '-' is used */
1607 if (op == '-')
1608 goto invalid_operands;
1609 else
1610 tcc_warning("comparison of distinct pointer types lacks a cast");
1614 /* generic gen_op: handles types problems */
1615 ST_FUNC void gen_op(int op)
1617 int u, t1, t2, bt1, bt2, t;
1618 CType type1;
1620 t1 = vtop[-1].type.t;
1621 t2 = vtop[0].type.t;
1622 bt1 = t1 & VT_BTYPE;
1623 bt2 = t2 & VT_BTYPE;
1625 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1626 /* at least one operand is a pointer */
1627 /* relationnal op: must be both pointers */
1628 if (op >= TOK_ULT && op <= TOK_LOR) {
1629 check_comparison_pointer_types(vtop - 1, vtop, op);
1630 /* pointers are handled are unsigned */
1631 #ifdef TCC_TARGET_X86_64
1632 t = VT_LLONG | VT_UNSIGNED;
1633 #else
1634 t = VT_INT | VT_UNSIGNED;
1635 #endif
1636 goto std_op;
1638 /* if both pointers, then it must be the '-' op */
1639 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1640 if (op != '-')
1641 tcc_error("cannot use pointers here");
1642 check_comparison_pointer_types(vtop - 1, vtop, op);
1643 /* XXX: check that types are compatible */
1644 if (vtop[-1].type.t & VT_VLA) {
1645 vla_runtime_pointed_size(&vtop[-1].type);
1646 } else {
1647 vpushi(pointed_size(&vtop[-1].type));
1649 vrott(3);
1650 gen_opic(op);
1651 /* set to integer type */
1652 #ifdef TCC_TARGET_X86_64
1653 vtop->type.t = VT_LLONG;
1654 #else
1655 vtop->type.t = VT_INT;
1656 #endif
1657 vswap();
1658 gen_op(TOK_PDIV);
1659 } else {
1660 /* exactly one pointer : must be '+' or '-'. */
1661 if (op != '-' && op != '+')
1662 tcc_error("cannot use pointers here");
1663 /* Put pointer as first operand */
1664 if (bt2 == VT_PTR) {
1665 vswap();
1666 swap(&t1, &t2);
1668 type1 = vtop[-1].type;
1669 type1.t &= ~VT_ARRAY;
1670 if (vtop[-1].type.t & VT_VLA)
1671 vla_runtime_pointed_size(&vtop[-1].type);
1672 else {
1673 u = pointed_size(&vtop[-1].type);
1674 if (u < 0)
1675 tcc_error("unknown array element size");
1676 #ifdef TCC_TARGET_X86_64
1677 vpushll(u);
1678 #else
1679 /* XXX: cast to int ? (long long case) */
1680 vpushi(u);
1681 #endif
1683 gen_op('*');
1684 #ifdef CONFIG_TCC_BCHECK
1685 /* if evaluating constant expression, no code should be
1686 generated, so no bound check */
1687 if (tcc_state->do_bounds_check && !const_wanted) {
1688 /* if bounded pointers, we generate a special code to
1689 test bounds */
1690 if (op == '-') {
1691 vpushi(0);
1692 vswap();
1693 gen_op('-');
1695 gen_bounded_ptr_add();
1696 } else
1697 #endif
1699 gen_opic(op);
1701 /* put again type if gen_opic() swaped operands */
1702 vtop->type = type1;
1704 } else if (is_float(bt1) || is_float(bt2)) {
1705 /* compute bigger type and do implicit casts */
1706 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1707 t = VT_LDOUBLE;
1708 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1709 t = VT_DOUBLE;
1710 } else {
1711 t = VT_FLOAT;
1713 /* floats can only be used for a few operations */
1714 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1715 (op < TOK_ULT || op > TOK_GT))
1716 tcc_error("invalid operands for binary operation");
1717 goto std_op;
1718 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1719 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1720 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1721 t |= VT_UNSIGNED;
1722 goto std_op;
1723 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1724 /* cast to biggest op */
1725 t = VT_LLONG;
1726 /* convert to unsigned if it does not fit in a long long */
1727 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1728 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1729 t |= VT_UNSIGNED;
1730 goto std_op;
1731 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1732 tcc_error("comparison of struct");
1733 } else {
1734 /* integer operations */
1735 t = VT_INT;
1736 /* convert to unsigned if it does not fit in an integer */
1737 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1738 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1739 t |= VT_UNSIGNED;
1740 std_op:
1741 /* XXX: currently, some unsigned operations are explicit, so
1742 we modify them here */
1743 if (t & VT_UNSIGNED) {
1744 if (op == TOK_SAR)
1745 op = TOK_SHR;
1746 else if (op == '/')
1747 op = TOK_UDIV;
1748 else if (op == '%')
1749 op = TOK_UMOD;
1750 else if (op == TOK_LT)
1751 op = TOK_ULT;
1752 else if (op == TOK_GT)
1753 op = TOK_UGT;
1754 else if (op == TOK_LE)
1755 op = TOK_ULE;
1756 else if (op == TOK_GE)
1757 op = TOK_UGE;
1759 vswap();
1760 type1.t = t;
1761 gen_cast(&type1);
1762 vswap();
1763 /* special case for shifts and long long: we keep the shift as
1764 an integer */
1765 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1766 type1.t = VT_INT;
1767 gen_cast(&type1);
1768 if (is_float(t))
1769 gen_opif(op);
1770 else
1771 gen_opic(op);
1772 if (op >= TOK_ULT && op <= TOK_GT) {
1773 /* relationnal op: the result is an int */
1774 vtop->type.t = VT_INT;
1775 } else {
1776 vtop->type.t = t;
1781 #ifndef TCC_TARGET_ARM
1782 /* generic itof for unsigned long long case */
1783 static void gen_cvt_itof1(int t)
1785 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1786 (VT_LLONG | VT_UNSIGNED)) {
1788 if (t == VT_FLOAT)
1789 vpush_global_sym(&func_old_type, TOK___floatundisf);
1790 #if LDOUBLE_SIZE != 8
1791 else if (t == VT_LDOUBLE)
1792 vpush_global_sym(&func_old_type, TOK___floatundixf);
1793 #endif
1794 else
1795 vpush_global_sym(&func_old_type, TOK___floatundidf);
1796 vrott(2);
1797 gfunc_call(1);
1798 vpushi(0);
1799 vtop->r = reg_fret(t);
1800 } else {
1801 gen_cvt_itof(t);
1804 #endif
1806 /* generic ftoi for unsigned long long case */
1807 static void gen_cvt_ftoi1(int t)
1809 int st;
1811 if (t == (VT_LLONG | VT_UNSIGNED)) {
1812 /* not handled natively */
1813 st = vtop->type.t & VT_BTYPE;
1814 if (st == VT_FLOAT)
1815 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1816 #if LDOUBLE_SIZE != 8
1817 else if (st == VT_LDOUBLE)
1818 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1819 #endif
1820 else
1821 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1822 vrott(2);
1823 gfunc_call(1);
1824 vpushi(0);
1825 vtop->r = REG_IRET;
1826 vtop->r2 = REG_LRET;
1827 } else {
1828 gen_cvt_ftoi(t);
1832 /* force char or short cast */
1833 static void force_charshort_cast(int t)
1835 int bits, dbt;
1836 dbt = t & VT_BTYPE;
1837 /* XXX: add optimization if lvalue : just change type and offset */
1838 if (dbt == VT_BYTE)
1839 bits = 8;
1840 else
1841 bits = 16;
1842 if (t & VT_UNSIGNED) {
1843 vpushi((1 << bits) - 1);
1844 gen_op('&');
1845 } else {
1846 bits = 32 - bits;
1847 vpushi(bits);
1848 gen_op(TOK_SHL);
1849 /* result must be signed or the SAR is converted to an SHL
1850 This was not the case when "t" was a signed short
1851 and the last value on the stack was an unsigned int */
1852 vtop->type.t &= ~VT_UNSIGNED;
1853 vpushi(bits);
1854 gen_op(TOK_SAR);
1858 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1859 static void gen_cast(CType *type)
1861 int sbt, dbt, sf, df, c, p;
1863 /* special delayed cast for char/short */
1864 /* XXX: in some cases (multiple cascaded casts), it may still
1865 be incorrect */
1866 if (vtop->r & VT_MUSTCAST) {
1867 vtop->r &= ~VT_MUSTCAST;
1868 force_charshort_cast(vtop->type.t);
1871 /* bitfields first get cast to ints */
1872 if (vtop->type.t & VT_BITFIELD) {
1873 gv(RC_INT);
1876 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1877 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1879 if (sbt != dbt) {
1880 sf = is_float(sbt);
1881 df = is_float(dbt);
1882 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1883 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1884 if (c) {
1885 /* constant case: we can do it now */
1886 /* XXX: in ISOC, cannot do it if error in convert */
1887 if (sbt == VT_FLOAT)
1888 vtop->c.ld = vtop->c.f;
1889 else if (sbt == VT_DOUBLE)
1890 vtop->c.ld = vtop->c.d;
1892 if (df) {
1893 if ((sbt & VT_BTYPE) == VT_LLONG) {
1894 if (sbt & VT_UNSIGNED)
1895 vtop->c.ld = vtop->c.ull;
1896 else
1897 vtop->c.ld = vtop->c.ll;
1898 } else if(!sf) {
1899 if (sbt & VT_UNSIGNED)
1900 vtop->c.ld = vtop->c.ui;
1901 else
1902 vtop->c.ld = vtop->c.i;
1905 if (dbt == VT_FLOAT)
1906 vtop->c.f = (float)vtop->c.ld;
1907 else if (dbt == VT_DOUBLE)
1908 vtop->c.d = (double)vtop->c.ld;
1909 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1910 vtop->c.ull = (unsigned long long)vtop->c.ld;
1911 } else if (sf && dbt == VT_BOOL) {
1912 vtop->c.i = (vtop->c.ld != 0);
1913 } else {
1914 if(sf)
1915 vtop->c.ll = (long long)vtop->c.ld;
1916 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1917 vtop->c.ll = vtop->c.ull;
1918 else if (sbt & VT_UNSIGNED)
1919 vtop->c.ll = vtop->c.ui;
1920 #ifdef TCC_TARGET_X86_64
1921 else if (sbt == VT_PTR)
1923 #endif
1924 else if (sbt != VT_LLONG)
1925 vtop->c.ll = vtop->c.i;
1927 if (dbt == (VT_LLONG|VT_UNSIGNED))
1928 vtop->c.ull = vtop->c.ll;
1929 else if (dbt == VT_BOOL)
1930 vtop->c.i = (vtop->c.ll != 0);
1931 else if (dbt != VT_LLONG) {
1932 int s = 0;
1933 if ((dbt & VT_BTYPE) == VT_BYTE)
1934 s = 24;
1935 else if ((dbt & VT_BTYPE) == VT_SHORT)
1936 s = 16;
1938 if(dbt & VT_UNSIGNED)
1939 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1940 else
1941 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1944 } else if (p && dbt == VT_BOOL) {
1945 vtop->r = VT_CONST;
1946 vtop->c.i = 1;
1947 } else if (!nocode_wanted) {
1948 /* non constant case: generate code */
1949 if (sf && df) {
1950 /* convert from fp to fp */
1951 gen_cvt_ftof(dbt);
1952 } else if (df) {
1953 /* convert int to fp */
1954 gen_cvt_itof1(dbt);
1955 } else if (sf) {
1956 /* convert fp to int */
1957 if (dbt == VT_BOOL) {
1958 vpushi(0);
1959 gen_op(TOK_NE);
1960 } else {
1961 /* we handle char/short/etc... with generic code */
1962 if (dbt != (VT_INT | VT_UNSIGNED) &&
1963 dbt != (VT_LLONG | VT_UNSIGNED) &&
1964 dbt != VT_LLONG)
1965 dbt = VT_INT;
1966 gen_cvt_ftoi1(dbt);
1967 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1968 /* additional cast for char/short... */
1969 vtop->type.t = dbt;
1970 gen_cast(type);
1973 #ifndef TCC_TARGET_X86_64
1974 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1975 if ((sbt & VT_BTYPE) != VT_LLONG) {
1976 /* scalar to long long */
1977 /* machine independent conversion */
1978 gv(RC_INT);
1979 /* generate high word */
1980 if (sbt == (VT_INT | VT_UNSIGNED)) {
1981 vpushi(0);
1982 gv(RC_INT);
1983 } else {
1984 if (sbt == VT_PTR) {
1985 /* cast from pointer to int before we apply
1986 shift operation, which pointers don't support*/
1987 gen_cast(&int_type);
1989 gv_dup();
1990 vpushi(31);
1991 gen_op(TOK_SAR);
1993 /* patch second register */
1994 vtop[-1].r2 = vtop->r;
1995 vpop();
1997 #else
1998 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1999 (dbt & VT_BTYPE) == VT_PTR ||
2000 (dbt & VT_BTYPE) == VT_FUNC) {
2001 if ((sbt & VT_BTYPE) != VT_LLONG &&
2002 (sbt & VT_BTYPE) != VT_PTR &&
2003 (sbt & VT_BTYPE) != VT_FUNC) {
2004 /* need to convert from 32bit to 64bit */
2005 int r = gv(RC_INT);
2006 if (sbt != (VT_INT | VT_UNSIGNED)) {
2007 /* x86_64 specific: movslq */
2008 o(0x6348);
2009 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2012 #endif
2013 } else if (dbt == VT_BOOL) {
2014 /* scalar to bool */
2015 vpushi(0);
2016 gen_op(TOK_NE);
2017 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2018 (dbt & VT_BTYPE) == VT_SHORT) {
2019 if (sbt == VT_PTR) {
2020 vtop->type.t = VT_INT;
2021 tcc_warning("nonportable conversion from pointer to char/short");
2023 force_charshort_cast(dbt);
2024 } else if ((dbt & VT_BTYPE) == VT_INT) {
2025 /* scalar to int */
2026 if (sbt == VT_LLONG) {
2027 /* from long long: just take low order word */
2028 lexpand();
2029 vpop();
2031 /* if lvalue and single word type, nothing to do because
2032 the lvalue already contains the real type size (see
2033 VT_LVAL_xxx constants) */
2036 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2037 /* if we are casting between pointer types,
2038 we must update the VT_LVAL_xxx size */
2039 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2040 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2042 vtop->type = *type;
2045 /* return type size as known at compile time. Put alignment at 'a' */
2046 ST_FUNC int type_size(CType *type, int *a)
2048 Sym *s;
2049 int bt;
2051 bt = type->t & VT_BTYPE;
2052 if (bt == VT_STRUCT) {
2053 /* struct/union */
2054 s = type->ref;
2055 *a = s->r;
2056 return s->c;
2057 } else if (bt == VT_PTR) {
2058 if (type->t & VT_ARRAY) {
2059 int ts;
2061 s = type->ref;
2062 ts = type_size(&s->type, a);
2064 if (ts < 0 && s->c < 0)
2065 ts = -ts;
2067 return ts * s->c;
2068 } else {
2069 *a = PTR_SIZE;
2070 return PTR_SIZE;
2072 } else if (bt == VT_LDOUBLE) {
2073 *a = LDOUBLE_ALIGN;
2074 return LDOUBLE_SIZE;
2075 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2076 #ifdef TCC_TARGET_I386
2077 #ifdef TCC_TARGET_PE
2078 *a = 8;
2079 #else
2080 *a = 4;
2081 #endif
2082 #elif defined(TCC_TARGET_ARM)
2083 #ifdef TCC_ARM_EABI
2084 *a = 8;
2085 #else
2086 *a = 4;
2087 #endif
2088 #else
2089 *a = 8;
2090 #endif
2091 return 8;
2092 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2093 *a = 4;
2094 return 4;
2095 } else if (bt == VT_SHORT) {
2096 *a = 2;
2097 return 2;
2098 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2099 *a = 8;
2100 return 16;
2101 } else {
2102 /* char, void, function, _Bool */
2103 *a = 1;
2104 return 1;
2108 /* push type size as known at runtime time on top of value stack. Put
2109 alignment at 'a' */
2110 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2112 if (type->t & VT_VLA) {
2113 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2114 } else {
2115 vpushi(type_size(type, a));
2119 static void vla_sp_save(void) {
2120 if (!(vla_flags & VLA_SP_LOC_SET)) {
2121 *vla_sp_loc = (loc -= PTR_SIZE);
2122 vla_flags |= VLA_SP_LOC_SET;
2124 if (!(vla_flags & VLA_SP_SAVED)) {
2125 gen_vla_sp_save(*vla_sp_loc);
2126 vla_flags |= VLA_SP_SAVED;
2130 /* return the pointed type of t */
2131 static inline CType *pointed_type(CType *type)
2133 return &type->ref->type;
2136 /* modify type so that its it is a pointer to type. */
2137 ST_FUNC void mk_pointer(CType *type)
2139 Sym *s;
2140 s = sym_push(SYM_FIELD, type, 0, -1);
2141 type->t = VT_PTR | (type->t & ~VT_TYPE);
2142 type->ref = s;
2145 /* compare function types. OLD functions match any new functions */
2146 static int is_compatible_func(CType *type1, CType *type2)
2148 Sym *s1, *s2;
2150 s1 = type1->ref;
2151 s2 = type2->ref;
2152 if (!is_compatible_types(&s1->type, &s2->type))
2153 return 0;
2154 /* check func_call */
2155 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2156 return 0;
2157 /* XXX: not complete */
2158 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2159 return 1;
2160 if (s1->c != s2->c)
2161 return 0;
2162 while (s1 != NULL) {
2163 if (s2 == NULL)
2164 return 0;
2165 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2166 return 0;
2167 s1 = s1->next;
2168 s2 = s2->next;
2170 if (s2)
2171 return 0;
2172 return 1;
2175 /* return true if type1 and type2 are the same. If unqualified is
2176 true, qualifiers on the types are ignored.
2178 - enums are not checked as gcc __builtin_types_compatible_p ()
2180 static int compare_types(CType *type1, CType *type2, int unqualified)
2182 int bt1, t1, t2;
2184 t1 = type1->t & VT_TYPE;
2185 t2 = type2->t & VT_TYPE;
2186 if (unqualified) {
2187 /* strip qualifiers before comparing */
2188 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2189 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2191 /* XXX: bitfields ? */
2192 if (t1 != t2)
2193 return 0;
2194 /* test more complicated cases */
2195 bt1 = t1 & VT_BTYPE;
2196 if (bt1 == VT_PTR) {
2197 type1 = pointed_type(type1);
2198 type2 = pointed_type(type2);
2199 return is_compatible_types(type1, type2);
2200 } else if (bt1 == VT_STRUCT) {
2201 return (type1->ref == type2->ref);
2202 } else if (bt1 == VT_FUNC) {
2203 return is_compatible_func(type1, type2);
2204 } else {
2205 return 1;
2209 /* return true if type1 and type2 are exactly the same (including
2210 qualifiers).
2212 static int is_compatible_types(CType *type1, CType *type2)
2214 return compare_types(type1,type2,0);
2217 /* return true if type1 and type2 are the same (ignoring qualifiers).
2219 static int is_compatible_parameter_types(CType *type1, CType *type2)
2221 return compare_types(type1,type2,1);
2224 /* print a type. If 'varstr' is not NULL, then the variable is also
2225 printed in the type */
2226 /* XXX: union */
2227 /* XXX: add array and function pointers */
2228 static void type_to_str(char *buf, int buf_size,
2229 CType *type, const char *varstr)
2231 int bt, v, t;
2232 Sym *s, *sa;
2233 char buf1[256];
2234 const char *tstr;
2236 t = type->t & VT_TYPE;
2237 bt = t & VT_BTYPE;
2238 buf[0] = '\0';
2239 if (t & VT_CONSTANT)
2240 pstrcat(buf, buf_size, "const ");
2241 if (t & VT_VOLATILE)
2242 pstrcat(buf, buf_size, "volatile ");
2243 if (t & VT_UNSIGNED)
2244 pstrcat(buf, buf_size, "unsigned ");
2245 switch(bt) {
2246 case VT_VOID:
2247 tstr = "void";
2248 goto add_tstr;
2249 case VT_BOOL:
2250 tstr = "_Bool";
2251 goto add_tstr;
2252 case VT_BYTE:
2253 tstr = "char";
2254 goto add_tstr;
2255 case VT_SHORT:
2256 tstr = "short";
2257 goto add_tstr;
2258 case VT_INT:
2259 tstr = "int";
2260 goto add_tstr;
2261 case VT_LONG:
2262 tstr = "long";
2263 goto add_tstr;
2264 case VT_LLONG:
2265 tstr = "long long";
2266 goto add_tstr;
2267 case VT_FLOAT:
2268 tstr = "float";
2269 goto add_tstr;
2270 case VT_DOUBLE:
2271 tstr = "double";
2272 goto add_tstr;
2273 case VT_LDOUBLE:
2274 tstr = "long double";
2275 add_tstr:
2276 pstrcat(buf, buf_size, tstr);
2277 break;
2278 case VT_ENUM:
2279 case VT_STRUCT:
2280 if (bt == VT_STRUCT)
2281 tstr = "struct ";
2282 else
2283 tstr = "enum ";
2284 pstrcat(buf, buf_size, tstr);
2285 v = type->ref->v & ~SYM_STRUCT;
2286 if (v >= SYM_FIRST_ANOM)
2287 pstrcat(buf, buf_size, "<anonymous>");
2288 else
2289 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2290 break;
2291 case VT_FUNC:
2292 s = type->ref;
2293 type_to_str(buf, buf_size, &s->type, varstr);
2294 pstrcat(buf, buf_size, "(");
2295 sa = s->next;
2296 while (sa != NULL) {
2297 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2298 pstrcat(buf, buf_size, buf1);
2299 sa = sa->next;
2300 if (sa)
2301 pstrcat(buf, buf_size, ", ");
2303 pstrcat(buf, buf_size, ")");
2304 goto no_var;
2305 case VT_PTR:
2306 s = type->ref;
2307 pstrcpy(buf1, sizeof(buf1), "*");
2308 if (varstr)
2309 pstrcat(buf1, sizeof(buf1), varstr);
2310 type_to_str(buf, buf_size, &s->type, buf1);
2311 goto no_var;
2313 if (varstr) {
2314 pstrcat(buf, buf_size, " ");
2315 pstrcat(buf, buf_size, varstr);
2317 no_var: ;
2320 /* verify type compatibility to store vtop in 'dt' type, and generate
2321 casts if needed. */
2322 static void gen_assign_cast(CType *dt)
2324 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2325 char buf1[256], buf2[256];
2326 int dbt, sbt;
2328 st = &vtop->type; /* source type */
2329 dbt = dt->t & VT_BTYPE;
2330 sbt = st->t & VT_BTYPE;
2331 if (sbt == VT_VOID)
2332 tcc_error("Cannot assign void value");
2333 if (dt->t & VT_CONSTANT)
2334 tcc_warning("assignment of read-only location");
2335 switch(dbt) {
2336 case VT_PTR:
2337 /* special cases for pointers */
2338 /* '0' can also be a pointer */
2339 if (is_null_pointer(vtop))
2340 goto type_ok;
2341 /* accept implicit pointer to integer cast with warning */
2342 if (is_integer_btype(sbt)) {
2343 tcc_warning("assignment makes pointer from integer without a cast");
2344 goto type_ok;
2346 type1 = pointed_type(dt);
2347 /* a function is implicitely a function pointer */
2348 if (sbt == VT_FUNC) {
2349 if ((type1->t & VT_BTYPE) != VT_VOID &&
2350 !is_compatible_types(pointed_type(dt), st))
2351 tcc_warning("assignment from incompatible pointer type");
2352 goto type_ok;
2354 if (sbt != VT_PTR)
2355 goto error;
2356 type2 = pointed_type(st);
2357 if ((type1->t & VT_BTYPE) == VT_VOID ||
2358 (type2->t & VT_BTYPE) == VT_VOID) {
2359 /* void * can match anything */
2360 } else {
2361 /* exact type match, except for unsigned */
2362 tmp_type1 = *type1;
2363 tmp_type2 = *type2;
2364 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2365 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2366 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2367 tcc_warning("assignment from incompatible pointer type");
2369 /* check const and volatile */
2370 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2371 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2372 tcc_warning("assignment discards qualifiers from pointer target type");
2373 break;
2374 case VT_BYTE:
2375 case VT_SHORT:
2376 case VT_INT:
2377 case VT_LLONG:
2378 if (sbt == VT_PTR || sbt == VT_FUNC) {
2379 tcc_warning("assignment makes integer from pointer without a cast");
2381 /* XXX: more tests */
2382 break;
2383 case VT_STRUCT:
2384 tmp_type1 = *dt;
2385 tmp_type2 = *st;
2386 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2387 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2388 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2389 error:
2390 type_to_str(buf1, sizeof(buf1), st, NULL);
2391 type_to_str(buf2, sizeof(buf2), dt, NULL);
2392 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2394 break;
2396 type_ok:
2397 gen_cast(dt);
2400 /* store vtop in lvalue pushed on stack */
2401 ST_FUNC void vstore(void)
2403 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2405 ft = vtop[-1].type.t;
2406 sbt = vtop->type.t & VT_BTYPE;
2407 dbt = ft & VT_BTYPE;
2408 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2409 (sbt == VT_INT && dbt == VT_SHORT))
2410 && !(vtop->type.t & VT_BITFIELD)) {
2411 /* optimize char/short casts */
2412 delayed_cast = VT_MUSTCAST;
2413 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2414 /* XXX: factorize */
2415 if (ft & VT_CONSTANT)
2416 tcc_warning("assignment of read-only location");
2417 } else {
2418 delayed_cast = 0;
2419 if (!(ft & VT_BITFIELD))
2420 gen_assign_cast(&vtop[-1].type);
2423 if (sbt == VT_STRUCT) {
2424 /* if structure, only generate pointer */
2425 /* structure assignment : generate memcpy */
2426 /* XXX: optimize if small size */
2427 if (!nocode_wanted) {
2428 size = type_size(&vtop->type, &align);
2430 /* destination */
2431 vswap();
2432 vtop->type.t = VT_PTR;
2433 gaddrof();
2435 /* address of memcpy() */
2436 #ifdef TCC_ARM_EABI
2437 if(!(align & 7))
2438 vpush_global_sym(&func_old_type, TOK_memcpy8);
2439 else if(!(align & 3))
2440 vpush_global_sym(&func_old_type, TOK_memcpy4);
2441 else
2442 #endif
2443 vpush_global_sym(&func_old_type, TOK_memcpy);
2445 vswap();
2446 /* source */
2447 vpushv(vtop - 2);
2448 vtop->type.t = VT_PTR;
2449 gaddrof();
2450 /* type size */
2451 vpushi(size);
2452 gfunc_call(3);
2453 } else {
2454 vswap();
2455 vpop();
2457 /* leave source on stack */
2458 } else if (ft & VT_BITFIELD) {
2459 /* bitfield store handling */
2460 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2461 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2462 /* remove bit field info to avoid loops */
2463 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2465 /* duplicate source into other register */
2466 gv_dup();
2467 vswap();
2468 vrott(3);
2470 if((ft & VT_BTYPE) == VT_BOOL) {
2471 gen_cast(&vtop[-1].type);
2472 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2475 /* duplicate destination */
2476 vdup();
2477 vtop[-1] = vtop[-2];
2479 /* mask and shift source */
2480 if((ft & VT_BTYPE) != VT_BOOL) {
2481 if((ft & VT_BTYPE) == VT_LLONG) {
2482 vpushll((1ULL << bit_size) - 1ULL);
2483 } else {
2484 vpushi((1 << bit_size) - 1);
2486 gen_op('&');
2488 vpushi(bit_pos);
2489 gen_op(TOK_SHL);
2490 /* load destination, mask and or with source */
2491 vswap();
2492 if((ft & VT_BTYPE) == VT_LLONG) {
2493 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2494 } else {
2495 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2497 gen_op('&');
2498 gen_op('|');
2499 /* store result */
2500 vstore();
2502 /* pop off shifted source from "duplicate source..." above */
2503 vpop();
2505 } else {
2506 #ifdef CONFIG_TCC_BCHECK
2507 /* bound check case */
2508 if (vtop[-1].r & VT_MUSTBOUND) {
2509 vswap();
2510 gbound();
2511 vswap();
2513 #endif
2514 if (!nocode_wanted) {
2515 rc = RC_INT;
2516 if (is_float(ft)) {
2517 rc = RC_FLOAT;
2518 #ifdef TCC_TARGET_X86_64
2519 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2520 rc = RC_ST0;
2521 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2522 rc = RC_FRET;
2524 #endif
2526 r = gv(rc); /* generate value */
2527 /* if lvalue was saved on stack, must read it */
2528 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2529 SValue sv;
2530 t = get_reg(RC_INT);
2531 #ifdef TCC_TARGET_X86_64
2532 sv.type.t = VT_PTR;
2533 #else
2534 sv.type.t = VT_INT;
2535 #endif
2536 sv.r = VT_LOCAL | VT_LVAL;
2537 sv.c.ul = vtop[-1].c.ul;
2538 load(t, &sv);
2539 vtop[-1].r = t | VT_LVAL;
2541 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2542 #ifdef TCC_TARGET_X86_64
2543 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2544 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2545 #else
2546 if ((ft & VT_BTYPE) == VT_LLONG) {
2547 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2548 #endif
2549 vtop[-1].type.t = load_type;
2550 store(r, vtop - 1);
2551 vswap();
2552 /* convert to int to increment easily */
2553 vtop->type.t = addr_type;
2554 gaddrof();
2555 vpushi(load_size);
2556 gen_op('+');
2557 vtop->r |= VT_LVAL;
2558 vswap();
2559 vtop[-1].type.t = load_type;
2560 /* XXX: it works because r2 is spilled last ! */
2561 store(vtop->r2, vtop - 1);
2562 } else {
2563 store(r, vtop - 1);
2566 vswap();
2567 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2568 vtop->r |= delayed_cast;
2572 /* post defines POST/PRE add. c is the token ++ or -- */
2573 ST_FUNC void inc(int post, int c)
2575 test_lvalue();
2576 vdup(); /* save lvalue */
2577 if (post) {
2578 gv_dup(); /* duplicate value */
2579 vrotb(3);
2580 vrotb(3);
2582 /* add constant */
2583 vpushi(c - TOK_MID);
2584 gen_op('+');
2585 vstore(); /* store value */
2586 if (post)
2587 vpop(); /* if post op, return saved value */
2590 /* Parse GNUC __attribute__ extension. Currently, the following
2591 extensions are recognized:
2592 - aligned(n) : set data/function alignment.
2593 - packed : force data alignment to 1
2594 - section(x) : generate data/code in this section.
2595 - unused : currently ignored, but may be used someday.
2596 - regparm(n) : pass function parameters in registers (i386 only)
2598 static void parse_attribute(AttributeDef *ad)
2600 int t, n;
2602 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2603 next();
2604 skip('(');
2605 skip('(');
2606 while (tok != ')') {
2607 if (tok < TOK_IDENT)
2608 expect("attribute name");
2609 t = tok;
2610 next();
2611 switch(t) {
2612 case TOK_SECTION1:
2613 case TOK_SECTION2:
2614 skip('(');
2615 if (tok != TOK_STR)
2616 expect("section name");
2617 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2618 next();
2619 skip(')');
2620 break;
2621 case TOK_ALIAS1:
2622 case TOK_ALIAS2:
2623 skip('(');
2624 if (tok != TOK_STR)
2625 expect("alias(\"target\")");
2626 ad->alias_target = /* save string as token, for later */
2627 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2628 next();
2629 skip(')');
2630 break;
2631 case TOK_ALIGNED1:
2632 case TOK_ALIGNED2:
2633 if (tok == '(') {
2634 next();
2635 n = expr_const();
2636 if (n <= 0 || (n & (n - 1)) != 0)
2637 tcc_error("alignment must be a positive power of two");
2638 skip(')');
2639 } else {
2640 n = MAX_ALIGN;
2642 ad->aligned = n;
2643 break;
2644 case TOK_PACKED1:
2645 case TOK_PACKED2:
2646 ad->packed = 1;
2647 break;
2648 case TOK_WEAK1:
2649 case TOK_WEAK2:
2650 ad->weak = 1;
2651 break;
2652 case TOK_UNUSED1:
2653 case TOK_UNUSED2:
2654 /* currently, no need to handle it because tcc does not
2655 track unused objects */
2656 break;
2657 case TOK_NORETURN1:
2658 case TOK_NORETURN2:
2659 /* currently, no need to handle it because tcc does not
2660 track unused objects */
2661 break;
2662 case TOK_CDECL1:
2663 case TOK_CDECL2:
2664 case TOK_CDECL3:
2665 ad->func_call = FUNC_CDECL;
2666 break;
2667 case TOK_STDCALL1:
2668 case TOK_STDCALL2:
2669 case TOK_STDCALL3:
2670 ad->func_call = FUNC_STDCALL;
2671 break;
2672 #ifdef TCC_TARGET_I386
2673 case TOK_REGPARM1:
2674 case TOK_REGPARM2:
2675 skip('(');
2676 n = expr_const();
2677 if (n > 3)
2678 n = 3;
2679 else if (n < 0)
2680 n = 0;
2681 if (n > 0)
2682 ad->func_call = FUNC_FASTCALL1 + n - 1;
2683 skip(')');
2684 break;
2685 case TOK_FASTCALL1:
2686 case TOK_FASTCALL2:
2687 case TOK_FASTCALL3:
2688 ad->func_call = FUNC_FASTCALLW;
2689 break;
2690 #endif
2691 case TOK_MODE:
2692 skip('(');
2693 switch(tok) {
2694 case TOK_MODE_DI:
2695 ad->mode = VT_LLONG + 1;
2696 break;
2697 case TOK_MODE_HI:
2698 ad->mode = VT_SHORT + 1;
2699 break;
2700 case TOK_MODE_SI:
2701 ad->mode = VT_INT + 1;
2702 break;
2703 default:
2704 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2705 break;
2707 next();
2708 skip(')');
2709 break;
2710 case TOK_DLLEXPORT:
2711 ad->func_export = 1;
2712 break;
2713 case TOK_DLLIMPORT:
2714 ad->func_import = 1;
2715 break;
2716 default:
2717 if (tcc_state->warn_unsupported)
2718 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2719 /* skip parameters */
2720 if (tok == '(') {
2721 int parenthesis = 0;
2722 do {
2723 if (tok == '(')
2724 parenthesis++;
2725 else if (tok == ')')
2726 parenthesis--;
2727 next();
2728 } while (parenthesis && tok != -1);
2730 break;
2732 if (tok != ',')
2733 break;
2734 next();
2736 skip(')');
2737 skip(')');
2741 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2742 static void struct_decl(CType *type, int u, int tdef)
2744 int a, v, size, align, maxalign, c, offset, flexible;
2745 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2746 Sym *s, *ss, *ass, **ps;
2747 AttributeDef ad;
2748 CType type1, btype;
2750 a = tok; /* save decl type */
2751 next();
2752 if (tok != '{') {
2753 v = tok;
2754 next();
2755 /* struct already defined ? return it */
2756 if (v < TOK_IDENT)
2757 expect("struct/union/enum name");
2758 s = struct_find(v);
2759 if (s) {
2760 if (s->type.t != a)
2761 tcc_error("invalid type");
2762 goto do_decl;
2763 } else if (tok >= TOK_IDENT && !tdef)
2764 tcc_error("unknown struct/union/enum");
2765 } else {
2766 v = anon_sym++;
2768 type1.t = a;
2769 type1.ref = NULL;
2770 /* we put an undefined size for struct/union */
2771 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2772 s->r = 0; /* default alignment is zero as gcc */
2773 /* put struct/union/enum name in type */
2774 do_decl:
2775 type->t = u;
2776 type->ref = s;
2778 if (tok == '{') {
2779 next();
2780 if (s->c != -1)
2781 tcc_error("struct/union/enum already defined");
2782 /* cannot be empty */
2783 c = 0;
2784 /* non empty enums are not allowed */
2785 if (a == TOK_ENUM) {
2786 for(;;) {
2787 v = tok;
2788 if (v < TOK_UIDENT)
2789 expect("identifier");
2790 ss = sym_find(v);
2791 if (ss)
2792 tcc_error("redefinition of enumerator '%s'",
2793 get_tok_str(v, NULL));
2794 next();
2795 if (tok == '=') {
2796 next();
2797 c = expr_const();
2799 /* enum symbols have static storage */
2800 ss = sym_push(v, &int_type, VT_CONST, c);
2801 ss->type.t |= VT_STATIC;
2802 if (tok != ',')
2803 break;
2804 next();
2805 c++;
2806 /* NOTE: we accept a trailing comma */
2807 if (tok == '}')
2808 break;
2810 s->c = type_size(&int_type, &align);
2811 skip('}');
2812 } else {
2813 maxalign = 1;
2814 ps = &s->next;
2815 prevbt = VT_INT;
2816 bit_pos = 0;
2817 offset = 0;
2818 flexible = 0;
2819 while (tok != '}') {
2820 parse_btype(&btype, &ad);
2821 while (1) {
2822 if (flexible)
2823 tcc_error("flexible array member '%s' not at the end of struct",
2824 get_tok_str(v, NULL));
2825 bit_size = -1;
2826 v = 0;
2827 type1 = btype;
2828 if (tok != ':') {
2829 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2830 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2831 expect("identifier");
2832 if (type_size(&type1, &align) < 0) {
2833 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2834 flexible = 1;
2835 else
2836 tcc_error("field '%s' has incomplete type",
2837 get_tok_str(v, NULL));
2839 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2840 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2841 tcc_error("invalid type for '%s'",
2842 get_tok_str(v, NULL));
2844 if (tok == ':') {
2845 next();
2846 bit_size = expr_const();
2847 /* XXX: handle v = 0 case for messages */
2848 if (bit_size < 0)
2849 tcc_error("negative width in bit-field '%s'",
2850 get_tok_str(v, NULL));
2851 if (v && bit_size == 0)
2852 tcc_error("zero width for bit-field '%s'",
2853 get_tok_str(v, NULL));
2855 size = type_size(&type1, &align);
2856 if (ad.aligned) {
2857 if (align < ad.aligned)
2858 align = ad.aligned;
2859 } else if (ad.packed) {
2860 align = 1;
2861 } else if (*tcc_state->pack_stack_ptr) {
2862 if (align > *tcc_state->pack_stack_ptr)
2863 align = *tcc_state->pack_stack_ptr;
2865 lbit_pos = 0;
2866 if (bit_size >= 0) {
2867 bt = type1.t & VT_BTYPE;
2868 if (bt != VT_INT &&
2869 bt != VT_BYTE &&
2870 bt != VT_SHORT &&
2871 bt != VT_BOOL &&
2872 bt != VT_ENUM &&
2873 bt != VT_LLONG)
2874 tcc_error("bitfields must have scalar type");
2875 bsize = size * 8;
2876 if (bit_size > bsize) {
2877 tcc_error("width of '%s' exceeds its type",
2878 get_tok_str(v, NULL));
2879 } else if (bit_size == bsize) {
2880 /* no need for bit fields */
2881 bit_pos = 0;
2882 } else if (bit_size == 0) {
2883 /* XXX: what to do if only padding in a
2884 structure ? */
2885 /* zero size: means to pad */
2886 bit_pos = 0;
2887 } else {
2888 /* we do not have enough room ?
2889 did the type change?
2890 is it a union? */
2891 if ((bit_pos + bit_size) > bsize ||
2892 bt != prevbt || a == TOK_UNION)
2893 bit_pos = 0;
2894 lbit_pos = bit_pos;
2895 /* XXX: handle LSB first */
2896 type1.t |= VT_BITFIELD |
2897 (bit_pos << VT_STRUCT_SHIFT) |
2898 (bit_size << (VT_STRUCT_SHIFT + 6));
2899 bit_pos += bit_size;
2901 prevbt = bt;
2902 } else {
2903 bit_pos = 0;
2905 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2906 /* add new memory data only if starting
2907 bit field */
2908 if (lbit_pos == 0) {
2909 if (a == TOK_STRUCT) {
2910 c = (c + align - 1) & -align;
2911 offset = c;
2912 if (size > 0)
2913 c += size;
2914 } else {
2915 offset = 0;
2916 if (size > c)
2917 c = size;
2919 if (align > maxalign)
2920 maxalign = align;
2922 #if 0
2923 printf("add field %s offset=%d",
2924 get_tok_str(v, NULL), offset);
2925 if (type1.t & VT_BITFIELD) {
2926 printf(" pos=%d size=%d",
2927 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2928 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2930 printf("\n");
2931 #endif
2933 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2934 ass = type1.ref;
2935 while ((ass = ass->next) != NULL) {
2936 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2937 *ps = ss;
2938 ps = &ss->next;
2940 } else if (v) {
2941 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2942 *ps = ss;
2943 ps = &ss->next;
2945 if (tok == ';' || tok == TOK_EOF)
2946 break;
2947 skip(',');
2949 skip(';');
2951 skip('}');
2952 /* store size and alignment */
2953 s->c = (c + maxalign - 1) & -maxalign;
2954 s->r = maxalign;
2959 /* return 0 if no type declaration. otherwise, return the basic type
2960 and skip it.
2962 static int parse_btype(CType *type, AttributeDef *ad)
2964 int t, u, type_found, typespec_found, typedef_found;
2965 Sym *s;
2966 CType type1;
2968 memset(ad, 0, sizeof(AttributeDef));
2969 type_found = 0;
2970 typespec_found = 0;
2971 typedef_found = 0;
2972 t = 0;
2973 while(1) {
2974 switch(tok) {
2975 case TOK_EXTENSION:
2976 /* currently, we really ignore extension */
2977 next();
2978 continue;
2980 /* basic types */
2981 case TOK_CHAR:
2982 u = VT_BYTE;
2983 basic_type:
2984 next();
2985 basic_type1:
2986 if ((t & VT_BTYPE) != 0)
2987 tcc_error("too many basic types");
2988 t |= u;
2989 typespec_found = 1;
2990 break;
2991 case TOK_VOID:
2992 u = VT_VOID;
2993 goto basic_type;
2994 case TOK_SHORT:
2995 u = VT_SHORT;
2996 goto basic_type;
2997 case TOK_INT:
2998 next();
2999 typespec_found = 1;
3000 break;
3001 case TOK_LONG:
3002 next();
3003 if ((t & VT_BTYPE) == VT_DOUBLE) {
3004 #ifndef TCC_TARGET_PE
3005 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3006 #endif
3007 } else if ((t & VT_BTYPE) == VT_LONG) {
3008 t = (t & ~VT_BTYPE) | VT_LLONG;
3009 } else {
3010 u = VT_LONG;
3011 goto basic_type1;
3013 break;
3014 case TOK_BOOL:
3015 u = VT_BOOL;
3016 goto basic_type;
3017 case TOK_FLOAT:
3018 u = VT_FLOAT;
3019 goto basic_type;
3020 case TOK_DOUBLE:
3021 next();
3022 if ((t & VT_BTYPE) == VT_LONG) {
3023 #ifdef TCC_TARGET_PE
3024 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3025 #else
3026 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3027 #endif
3028 } else {
3029 u = VT_DOUBLE;
3030 goto basic_type1;
3032 break;
3033 case TOK_ENUM:
3034 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3035 basic_type2:
3036 u = type1.t;
3037 type->ref = type1.ref;
3038 goto basic_type1;
3039 case TOK_STRUCT:
3040 case TOK_UNION:
3041 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3042 goto basic_type2;
3044 /* type modifiers */
3045 case TOK_CONST1:
3046 case TOK_CONST2:
3047 case TOK_CONST3:
3048 t |= VT_CONSTANT;
3049 next();
3050 break;
3051 case TOK_VOLATILE1:
3052 case TOK_VOLATILE2:
3053 case TOK_VOLATILE3:
3054 t |= VT_VOLATILE;
3055 next();
3056 break;
3057 case TOK_SIGNED1:
3058 case TOK_SIGNED2:
3059 case TOK_SIGNED3:
3060 typespec_found = 1;
3061 t |= VT_SIGNED;
3062 next();
3063 break;
3064 case TOK_REGISTER:
3065 case TOK_AUTO:
3066 case TOK_RESTRICT1:
3067 case TOK_RESTRICT2:
3068 case TOK_RESTRICT3:
3069 next();
3070 break;
3071 case TOK_UNSIGNED:
3072 t |= VT_UNSIGNED;
3073 next();
3074 typespec_found = 1;
3075 break;
3077 /* storage */
3078 case TOK_EXTERN:
3079 t |= VT_EXTERN;
3080 next();
3081 break;
3082 case TOK_STATIC:
3083 t |= VT_STATIC;
3084 next();
3085 break;
3086 case TOK_TYPEDEF:
3087 t |= VT_TYPEDEF;
3088 next();
3089 break;
3090 case TOK_INLINE1:
3091 case TOK_INLINE2:
3092 case TOK_INLINE3:
3093 t |= VT_INLINE;
3094 next();
3095 break;
3096 case TOK_THREAD:
3097 t |= VT_TLS;
3098 next();
3099 break;
3101 /* GNUC attribute */
3102 case TOK_ATTRIBUTE1:
3103 case TOK_ATTRIBUTE2:
3104 parse_attribute(ad);
3105 if (ad->mode) {
3106 u = ad->mode -1;
3107 t = (t & ~VT_BTYPE) | u;
3109 break;
3110 /* GNUC typeof */
3111 case TOK_TYPEOF1:
3112 case TOK_TYPEOF2:
3113 case TOK_TYPEOF3:
3114 next();
3115 parse_expr_type(&type1);
3116 /* remove all storage modifiers except typedef */
3117 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3118 goto basic_type2;
3119 default:
3120 if (typespec_found || typedef_found)
3121 goto the_end;
3122 s = sym_find(tok);
3123 if (!s || !(s->type.t & VT_TYPEDEF))
3124 goto the_end;
3125 typedef_found = 1;
3126 t |= (s->type.t & ~VT_TYPEDEF);
3127 type->ref = s->type.ref;
3128 if (s->r) {
3129 /* get attributes from typedef */
3130 if (0 == ad->aligned)
3131 ad->aligned = FUNC_ALIGN(s->r);
3132 if (0 == ad->func_call)
3133 ad->func_call = FUNC_CALL(s->r);
3134 ad->packed |= FUNC_PACKED(s->r);
3136 next();
3137 typespec_found = 1;
3138 break;
3140 type_found = 1;
3142 the_end:
3143 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3144 tcc_error("signed and unsigned modifier");
3145 if (tcc_state->char_is_unsigned) {
3146 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3147 t |= VT_UNSIGNED;
3149 t &= ~VT_SIGNED;
3151 /* long is never used as type */
3152 if ((t & VT_BTYPE) == VT_LONG)
3153 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3154 t = (t & ~VT_BTYPE) | VT_INT;
3155 #else
3156 t = (t & ~VT_BTYPE) | VT_LLONG;
3157 #endif
3158 type->t = t;
3159 return type_found;
3162 /* convert a function parameter type (array to pointer and function to
3163 function pointer) */
3164 static inline void convert_parameter_type(CType *pt)
3166 /* remove const and volatile qualifiers (XXX: const could be used
3167 to indicate a const function parameter */
3168 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3169 /* array must be transformed to pointer according to ANSI C */
3170 pt->t &= ~VT_ARRAY;
3171 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3172 mk_pointer(pt);
3176 ST_FUNC void parse_asm_str(CString *astr)
3178 skip('(');
3179 /* read the string */
3180 if (tok != TOK_STR)
3181 expect("string constant");
3182 cstr_new(astr);
3183 while (tok == TOK_STR) {
3184 /* XXX: add \0 handling too ? */
3185 cstr_cat(astr, tokc.cstr->data);
3186 next();
3188 cstr_ccat(astr, '\0');
3191 /* Parse an asm label and return the label
3192 * Don't forget to free the CString in the caller! */
3193 static void asm_label_instr(CString *astr)
3195 next();
3196 parse_asm_str(astr);
3197 skip(')');
3198 #ifdef ASM_DEBUG
3199 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3200 #endif
3203 static void post_type(CType *type, AttributeDef *ad)
3205 int n, l, t1, arg_size, align;
3206 Sym **plast, *s, *first;
3207 AttributeDef ad1;
3208 CType pt;
3210 if (tok == '(') {
3211 /* function declaration */
3212 next();
3213 l = 0;
3214 first = NULL;
3215 plast = &first;
3216 arg_size = 0;
3217 if (tok != ')') {
3218 for(;;) {
3219 /* read param name and compute offset */
3220 if (l != FUNC_OLD) {
3221 if (!parse_btype(&pt, &ad1)) {
3222 if (l) {
3223 tcc_error("invalid type");
3224 } else {
3225 l = FUNC_OLD;
3226 goto old_proto;
3229 l = FUNC_NEW;
3230 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3231 break;
3232 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3233 if ((pt.t & VT_BTYPE) == VT_VOID)
3234 tcc_error("parameter declared as void");
3235 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3236 } else {
3237 old_proto:
3238 n = tok;
3239 if (n < TOK_UIDENT)
3240 expect("identifier");
3241 pt.t = VT_INT;
3242 next();
3244 convert_parameter_type(&pt);
3245 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3246 *plast = s;
3247 plast = &s->next;
3248 if (tok == ')')
3249 break;
3250 skip(',');
3251 if (l == FUNC_NEW && tok == TOK_DOTS) {
3252 l = FUNC_ELLIPSIS;
3253 next();
3254 break;
3258 /* if no parameters, then old type prototype */
3259 if (l == 0)
3260 l = FUNC_OLD;
3261 skip(')');
3262 /* NOTE: const is ignored in returned type as it has a special
3263 meaning in gcc / C++ */
3264 type->t &= ~VT_CONSTANT;
3265 /* some ancient pre-K&R C allows a function to return an array
3266 and the array brackets to be put after the arguments, such
3267 that "int c()[]" means something like "int[] c()" */
3268 if (tok == '[') {
3269 next();
3270 skip(']'); /* only handle simple "[]" */
3271 type->t |= VT_PTR;
3273 /* we push a anonymous symbol which will contain the function prototype */
3274 ad->func_args = arg_size;
3275 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3276 s->next = first;
3277 type->t = VT_FUNC;
3278 type->ref = s;
3279 } else if (tok == '[') {
3280 /* array definition */
3281 next();
3282 if (tok == TOK_RESTRICT1)
3283 next();
3284 n = -1;
3285 t1 = 0;
3286 if (tok != ']') {
3287 if (!local_stack || nocode_wanted)
3288 vpushi(expr_const());
3289 else gexpr();
3290 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3291 n = vtop->c.i;
3292 if (n < 0)
3293 tcc_error("invalid array size");
3294 } else {
3295 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3296 tcc_error("size of variable length array should be an integer");
3297 t1 = VT_VLA;
3300 skip(']');
3301 /* parse next post type */
3302 post_type(type, ad);
3303 if (type->t == VT_FUNC)
3304 tcc_error("declaration of an array of functions");
3305 t1 |= type->t & VT_VLA;
3307 if (t1 & VT_VLA) {
3308 loc -= type_size(&int_type, &align);
3309 loc &= -align;
3310 n = loc;
3312 vla_runtime_type_size(type, &align);
3313 gen_op('*');
3314 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3315 vswap();
3316 vstore();
3318 if (n != -1)
3319 vpop();
3321 /* we push an anonymous symbol which will contain the array
3322 element type */
3323 s = sym_push(SYM_FIELD, type, 0, n);
3324 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3325 type->ref = s;
3329 /* Parse a type declaration (except basic type), and return the type
3330 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3331 expected. 'type' should contain the basic type. 'ad' is the
3332 attribute definition of the basic type. It can be modified by
3333 type_decl().
3335 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3337 Sym *s;
3338 CType type1, *type2;
3339 int qualifiers, storage;
3341 while (tok == '*') {
3342 qualifiers = 0;
3343 redo:
3344 next();
3345 switch(tok) {
3346 case TOK_CONST1:
3347 case TOK_CONST2:
3348 case TOK_CONST3:
3349 qualifiers |= VT_CONSTANT;
3350 goto redo;
3351 case TOK_VOLATILE1:
3352 case TOK_VOLATILE2:
3353 case TOK_VOLATILE3:
3354 qualifiers |= VT_VOLATILE;
3355 goto redo;
3356 case TOK_RESTRICT1:
3357 case TOK_RESTRICT2:
3358 case TOK_RESTRICT3:
3359 goto redo;
3361 mk_pointer(type);
3362 type->t |= qualifiers;
3365 /* XXX: clarify attribute handling */
3366 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3367 parse_attribute(ad);
3369 /* recursive type */
3370 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3371 type1.t = 0; /* XXX: same as int */
3372 if (tok == '(') {
3373 next();
3374 /* XXX: this is not correct to modify 'ad' at this point, but
3375 the syntax is not clear */
3376 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3377 parse_attribute(ad);
3378 type_decl(&type1, ad, v, td);
3379 skip(')');
3380 } else {
3381 /* type identifier */
3382 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3383 *v = tok;
3384 next();
3385 } else {
3386 if (!(td & TYPE_ABSTRACT))
3387 expect("identifier");
3388 *v = 0;
3391 storage = type->t & VT_STORAGE;
3392 type->t &= ~VT_STORAGE;
3393 if (storage & VT_STATIC) {
3394 int saved_nocode_wanted = nocode_wanted;
3395 nocode_wanted = 1;
3396 post_type(type, ad);
3397 nocode_wanted = saved_nocode_wanted;
3398 } else
3399 post_type(type, ad);
3400 type->t |= storage;
3401 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3402 parse_attribute(ad);
3404 if (!type1.t)
3405 return;
3406 /* append type at the end of type1 */
3407 type2 = &type1;
3408 for(;;) {
3409 s = type2->ref;
3410 type2 = &s->type;
3411 if (!type2->t) {
3412 *type2 = *type;
3413 break;
3416 *type = type1;
3419 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3420 ST_FUNC int lvalue_type(int t)
3422 int bt, r;
3423 r = VT_LVAL;
3424 bt = t & VT_BTYPE;
3425 if (bt == VT_BYTE || bt == VT_BOOL)
3426 r |= VT_LVAL_BYTE;
3427 else if (bt == VT_SHORT)
3428 r |= VT_LVAL_SHORT;
3429 else
3430 return r;
3431 if (t & VT_UNSIGNED)
3432 r |= VT_LVAL_UNSIGNED;
3433 return r;
3436 /* indirection with full error checking and bound check */
3437 ST_FUNC void indir(void)
3439 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3440 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3441 return;
3442 expect("pointer");
3444 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3445 gv(RC_INT);
3446 vtop->type = *pointed_type(&vtop->type);
3447 /* Arrays and functions are never lvalues */
3448 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3449 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3450 vtop->r |= lvalue_type(vtop->type.t);
3451 /* if bound checking, the referenced pointer must be checked */
3452 #ifdef CONFIG_TCC_BCHECK
3453 if (tcc_state->do_bounds_check)
3454 vtop->r |= VT_MUSTBOUND;
3455 #endif
3459 /* pass a parameter to a function and do type checking and casting */
3460 static void gfunc_param_typed(Sym *func, Sym *arg)
3462 int func_type;
3463 CType type;
3465 func_type = func->c;
3466 if (func_type == FUNC_OLD ||
3467 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3468 /* default casting : only need to convert float to double */
3469 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3470 type.t = VT_DOUBLE;
3471 gen_cast(&type);
3473 } else if (arg == NULL) {
3474 tcc_error("too many arguments to function");
3475 } else {
3476 type = arg->type;
3477 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3478 gen_assign_cast(&type);
3482 /* parse an expression of the form '(type)' or '(expr)' and return its
3483 type */
3484 static void parse_expr_type(CType *type)
3486 int n;
3487 AttributeDef ad;
3489 skip('(');
3490 if (parse_btype(type, &ad)) {
3491 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3492 } else {
3493 expr_type(type);
3495 skip(')');
3498 static void parse_type(CType *type)
3500 AttributeDef ad;
3501 int n;
3503 if (!parse_btype(type, &ad)) {
3504 expect("type");
3506 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3509 static void vpush_tokc(int t)
3511 CType type;
3512 type.t = t;
3513 type.ref = 0;
3514 vsetc(&type, VT_CONST, &tokc);
3517 ST_FUNC void unary(void)
3519 int n, t, align, size, r, sizeof_caller;
3520 CType type;
3521 Sym *s;
3522 AttributeDef ad;
3523 static int in_sizeof = 0;
3525 sizeof_caller = in_sizeof;
3526 in_sizeof = 0;
3527 /* XXX: GCC 2.95.3 does not generate a table although it should be
3528 better here */
3529 tok_next:
3530 switch(tok) {
3531 case TOK_EXTENSION:
3532 next();
3533 goto tok_next;
3534 case TOK_CINT:
3535 case TOK_CCHAR:
3536 case TOK_LCHAR:
3537 vpushi(tokc.i);
3538 next();
3539 break;
3540 case TOK_CUINT:
3541 vpush_tokc(VT_INT | VT_UNSIGNED);
3542 next();
3543 break;
3544 case TOK_CLLONG:
3545 vpush_tokc(VT_LLONG);
3546 next();
3547 break;
3548 case TOK_CULLONG:
3549 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3550 next();
3551 break;
3552 case TOK_CFLOAT:
3553 vpush_tokc(VT_FLOAT);
3554 next();
3555 break;
3556 case TOK_CDOUBLE:
3557 vpush_tokc(VT_DOUBLE);
3558 next();
3559 break;
3560 case TOK_CLDOUBLE:
3561 vpush_tokc(VT_LDOUBLE);
3562 next();
3563 break;
3564 case TOK___FUNCTION__:
3565 if (!gnu_ext)
3566 goto tok_identifier;
3567 /* fall thru */
3568 case TOK___FUNC__:
3570 void *ptr;
3571 int len;
3572 /* special function name identifier */
3573 len = strlen(funcname) + 1;
3574 /* generate char[len] type */
3575 type.t = VT_BYTE;
3576 mk_pointer(&type);
3577 type.t |= VT_ARRAY;
3578 type.ref->c = len;
3579 vpush_ref(&type, data_section, data_section->data_offset, len);
3580 ptr = section_ptr_add(data_section, len);
3581 memcpy(ptr, funcname, len);
3582 next();
3584 break;
3585 case TOK_LSTR:
3586 #ifdef TCC_TARGET_PE
3587 t = VT_SHORT | VT_UNSIGNED;
3588 #else
3589 t = VT_INT;
3590 #endif
3591 goto str_init;
3592 case TOK_STR:
3593 /* string parsing */
3594 t = VT_BYTE;
3595 str_init:
3596 if (tcc_state->warn_write_strings)
3597 t |= VT_CONSTANT;
3598 type.t = t;
3599 mk_pointer(&type);
3600 type.t |= VT_ARRAY;
3601 memset(&ad, 0, sizeof(AttributeDef));
3602 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3603 break;
3604 case '(':
3605 next();
3606 /* cast ? */
3607 if (parse_btype(&type, &ad)) {
3608 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3609 skip(')');
3610 /* check ISOC99 compound literal */
3611 if (tok == '{') {
3612 /* data is allocated locally by default */
3613 if (global_expr)
3614 r = VT_CONST;
3615 else
3616 r = VT_LOCAL;
3617 /* all except arrays are lvalues */
3618 if (!(type.t & VT_ARRAY))
3619 r |= lvalue_type(type.t);
3620 memset(&ad, 0, sizeof(AttributeDef));
3621 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3622 } else {
3623 if (sizeof_caller) {
3624 vpush(&type);
3625 return;
3627 unary();
3628 gen_cast(&type);
3630 } else if (tok == '{') {
3631 /* save all registers */
3632 save_regs(0);
3633 /* statement expression : we do not accept break/continue
3634 inside as GCC does */
3635 block(NULL, NULL, NULL, NULL, 0, 1);
3636 skip(')');
3637 } else {
3638 gexpr();
3639 skip(')');
3641 break;
3642 case '*':
3643 next();
3644 unary();
3645 indir();
3646 break;
3647 case '&':
3648 next();
3649 unary();
3650 /* functions names must be treated as function pointers,
3651 except for unary '&' and sizeof. Since we consider that
3652 functions are not lvalues, we only have to handle it
3653 there and in function calls. */
3654 /* arrays can also be used although they are not lvalues */
3655 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3656 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3657 test_lvalue();
3658 mk_pointer(&vtop->type);
3659 gaddrof();
3660 break;
3661 case '!':
3662 next();
3663 unary();
3664 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3665 CType boolean;
3666 boolean.t = VT_BOOL;
3667 gen_cast(&boolean);
3668 vtop->c.i = !vtop->c.i;
3669 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3670 vtop->c.i = vtop->c.i ^ 1;
3671 else {
3672 save_regs(1);
3673 vseti(VT_JMP, gtst(1, 0));
3675 break;
3676 case '~':
3677 next();
3678 unary();
3679 vpushi(-1);
3680 gen_op('^');
3681 break;
3682 case '+':
3683 next();
3684 /* in order to force cast, we add zero */
3685 unary();
3686 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3687 tcc_error("pointer not accepted for unary plus");
3688 vpushi(0);
3689 gen_op('+');
3690 break;
3691 case TOK_SIZEOF:
3692 case TOK_ALIGNOF1:
3693 case TOK_ALIGNOF2:
3694 t = tok;
3695 next();
3696 in_sizeof++;
3697 unary_type(&type); // Perform a in_sizeof = 0;
3698 size = type_size(&type, &align);
3699 if (t == TOK_SIZEOF) {
3700 if (!(type.t & VT_VLA)) {
3701 if (size < 0)
3702 tcc_error("sizeof applied to an incomplete type");
3703 vpushs(size);
3704 } else {
3705 vla_runtime_type_size(&type, &align);
3707 } else {
3708 vpushs(align);
3710 vtop->type.t |= VT_UNSIGNED;
3711 break;
3713 case TOK_builtin_types_compatible_p:
3715 CType type1, type2;
3716 next();
3717 skip('(');
3718 parse_type(&type1);
3719 skip(',');
3720 parse_type(&type2);
3721 skip(')');
3722 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3723 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3724 vpushi(is_compatible_types(&type1, &type2));
3726 break;
3727 case TOK_builtin_constant_p:
3729 int saved_nocode_wanted, res;
3730 next();
3731 skip('(');
3732 saved_nocode_wanted = nocode_wanted;
3733 nocode_wanted = 1;
3734 gexpr();
3735 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3736 vpop();
3737 nocode_wanted = saved_nocode_wanted;
3738 skip(')');
3739 vpushi(res);
3741 break;
3742 case TOK_builtin_frame_address:
3744 int level;
3745 CType type;
3746 next();
3747 skip('(');
3748 if (tok != TOK_CINT || tokc.i < 0) {
3749 tcc_error("__builtin_frame_address only takes positive integers");
3751 level = tokc.i;
3752 next();
3753 skip(')');
3754 type.t = VT_VOID;
3755 mk_pointer(&type);
3756 vset(&type, VT_LOCAL, 0); /* local frame */
3757 while (level--) {
3758 mk_pointer(&vtop->type);
3759 indir(); /* -> parent frame */
3762 break;
3763 #ifdef TCC_TARGET_X86_64
3764 #ifdef TCC_TARGET_PE
3765 case TOK_builtin_va_start:
3767 next();
3768 skip('(');
3769 expr_eq();
3770 skip(',');
3771 expr_eq();
3772 skip(')');
3773 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3774 tcc_error("__builtin_va_start expects a local variable");
3775 vtop->r &= ~(VT_LVAL | VT_REF);
3776 vtop->type = char_pointer_type;
3777 vstore();
3779 break;
3780 #else
3781 case TOK_builtin_va_arg_types:
3783 CType type;
3784 next();
3785 skip('(');
3786 parse_type(&type);
3787 skip(')');
3788 vpushi(classify_x86_64_va_arg(&type));
3790 break;
3791 #endif
3792 #endif
3793 case TOK_INC:
3794 case TOK_DEC:
3795 t = tok;
3796 next();
3797 unary();
3798 inc(0, t);
3799 break;
3800 case '-':
3801 next();
3802 vpushi(0);
3803 unary();
3804 gen_op('-');
3805 break;
3806 case TOK_LAND:
3807 if (!gnu_ext)
3808 goto tok_identifier;
3809 next();
3810 /* allow to take the address of a label */
3811 if (tok < TOK_UIDENT)
3812 expect("label identifier");
3813 s = label_find(tok);
3814 if (!s) {
3815 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3816 } else {
3817 if (s->r == LABEL_DECLARED)
3818 s->r = LABEL_FORWARD;
3820 if (!s->type.t) {
3821 s->type.t = VT_VOID;
3822 mk_pointer(&s->type);
3823 s->type.t |= VT_STATIC;
3825 vset(&s->type, VT_CONST | VT_SYM, 0);
3826 vtop->sym = s;
3827 next();
3828 break;
3830 // special qnan , snan and infinity values
3831 case TOK___NAN__:
3832 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3833 next();
3834 break;
3835 case TOK___SNAN__:
3836 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3837 next();
3838 break;
3839 case TOK___INF__:
3840 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3841 next();
3842 break;
3844 default:
3845 tok_identifier:
3846 t = tok;
3847 next();
3848 if (t < TOK_UIDENT)
3849 expect("identifier");
3850 s = sym_find(t);
3851 if (!s) {
3852 if (tok != '(')
3853 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3854 /* for simple function calls, we tolerate undeclared
3855 external reference to int() function */
3856 if (tcc_state->warn_implicit_function_declaration)
3857 tcc_warning("implicit declaration of function '%s'",
3858 get_tok_str(t, NULL));
3859 s = external_global_sym(t, &func_old_type, 0);
3861 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3862 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3863 /* if referencing an inline function, then we generate a
3864 symbol to it if not already done. It will have the
3865 effect to generate code for it at the end of the
3866 compilation unit. Inline function as always
3867 generated in the text section. */
3868 if (!s->c)
3869 put_extern_sym(s, text_section, 0, 0);
3870 r = VT_SYM | VT_CONST;
3871 } else {
3872 r = s->r;
3874 vset(&s->type, r, s->c);
3875 /* if forward reference, we must point to s */
3876 if (vtop->r & VT_SYM) {
3877 vtop->sym = s;
3878 vtop->c.ul = 0;
3880 break;
3883 /* post operations */
3884 while (1) {
3885 if (tok == TOK_INC || tok == TOK_DEC) {
3886 inc(1, tok);
3887 next();
3888 } else if (tok == '.' || tok == TOK_ARROW) {
3889 int qualifiers;
3890 /* field */
3891 if (tok == TOK_ARROW)
3892 indir();
3893 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3894 test_lvalue();
3895 gaddrof();
3896 next();
3897 /* expect pointer on structure */
3898 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3899 expect("struct or union");
3900 s = vtop->type.ref;
3901 /* find field */
3902 tok |= SYM_FIELD;
3903 while ((s = s->next) != NULL) {
3904 if (s->v == tok)
3905 break;
3907 if (!s)
3908 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3909 /* add field offset to pointer */
3910 vtop->type = char_pointer_type; /* change type to 'char *' */
3911 vpushi(s->c);
3912 gen_op('+');
3913 /* change type to field type, and set to lvalue */
3914 vtop->type = s->type;
3915 vtop->type.t |= qualifiers;
3916 /* an array is never an lvalue */
3917 if (!(vtop->type.t & VT_ARRAY)) {
3918 vtop->r |= lvalue_type(vtop->type.t);
3919 #ifdef CONFIG_TCC_BCHECK
3920 /* if bound checking, the referenced pointer must be checked */
3921 if (tcc_state->do_bounds_check)
3922 vtop->r |= VT_MUSTBOUND;
3923 #endif
3925 next();
3926 } else if (tok == '[') {
3927 next();
3928 gexpr();
3929 gen_op('+');
3930 indir();
3931 skip(']');
3932 } else if (tok == '(') {
3933 SValue ret;
3934 Sym *sa;
3935 int nb_args, sret;
3937 /* function call */
3938 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3939 /* pointer test (no array accepted) */
3940 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3941 vtop->type = *pointed_type(&vtop->type);
3942 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3943 goto error_func;
3944 } else {
3945 error_func:
3946 expect("function pointer");
3948 } else {
3949 vtop->r &= ~VT_LVAL; /* no lvalue */
3951 /* get return type */
3952 s = vtop->type.ref;
3953 next();
3954 sa = s->next; /* first parameter */
3955 nb_args = 0;
3956 ret.r2 = VT_CONST;
3957 /* compute first implicit argument if a structure is returned */
3958 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3959 int ret_align;
3960 sret = gfunc_sret(&s->type, &ret.type, &ret_align);
3961 if (sret) {
3962 /* get some space for the returned structure */
3963 size = type_size(&s->type, &align);
3964 loc = (loc - size) & -align;
3965 ret.type = s->type;
3966 ret.r = VT_LOCAL | VT_LVAL;
3967 /* pass it as 'int' to avoid structure arg passing
3968 problems */
3969 vseti(VT_LOCAL, loc);
3970 ret.c = vtop->c;
3971 nb_args++;
3973 } else {
3974 sret = 0;
3975 ret.type = s->type;
3978 if (!sret) {
3979 /* return in register */
3980 if (is_float(ret.type.t)) {
3981 ret.r = reg_fret(ret.type.t);
3982 #ifdef TCC_TARGET_X86_64
3983 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
3984 ret.r2 = REG_QRET;
3985 #endif
3986 } else {
3987 #ifdef TCC_TARGET_X86_64
3988 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
3989 #else
3990 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3991 #endif
3992 ret.r2 = REG_LRET;
3993 ret.r = REG_IRET;
3995 ret.c.i = 0;
3997 if (tok != ')') {
3998 for(;;) {
3999 expr_eq();
4000 gfunc_param_typed(s, sa);
4001 nb_args++;
4002 if (sa)
4003 sa = sa->next;
4004 if (tok == ')')
4005 break;
4006 skip(',');
4009 if (sa)
4010 tcc_error("too few arguments to function");
4011 skip(')');
4012 if (!nocode_wanted) {
4013 gfunc_call(nb_args);
4014 } else {
4015 vtop -= (nb_args + 1);
4017 /* return value */
4018 vsetc(&ret.type, ret.r, &ret.c);
4019 vtop->r2 = ret.r2;
4020 /* handle packed struct return */
4021 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && !sret) {
4022 int addr;
4023 size = type_size(&s->type, &align);
4024 loc = (loc - size) & -align;
4025 addr = loc;
4026 vset(&ret.type, VT_LOCAL | VT_LVAL, addr);
4027 vswap();
4028 vstore();
4029 vtop--;
4030 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4032 } else {
4033 break;
4038 ST_FUNC void expr_prod(void)
4040 int t;
4042 unary();
4043 while (tok == '*' || tok == '/' || tok == '%') {
4044 t = tok;
4045 next();
4046 unary();
4047 gen_op(t);
4051 ST_FUNC void expr_sum(void)
4053 int t;
4055 expr_prod();
4056 while (tok == '+' || tok == '-') {
4057 t = tok;
4058 next();
4059 expr_prod();
4060 gen_op(t);
4064 static void expr_shift(void)
4066 int t;
4068 expr_sum();
4069 while (tok == TOK_SHL || tok == TOK_SAR) {
4070 t = tok;
4071 next();
4072 expr_sum();
4073 gen_op(t);
4077 static void expr_cmp(void)
4079 int t;
4081 expr_shift();
4082 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4083 tok == TOK_ULT || tok == TOK_UGE) {
4084 t = tok;
4085 next();
4086 expr_shift();
4087 gen_op(t);
4091 static void expr_cmpeq(void)
4093 int t;
4095 expr_cmp();
4096 while (tok == TOK_EQ || tok == TOK_NE) {
4097 t = tok;
4098 next();
4099 expr_cmp();
4100 gen_op(t);
4104 static void expr_and(void)
4106 expr_cmpeq();
4107 while (tok == '&') {
4108 next();
4109 expr_cmpeq();
4110 gen_op('&');
4114 static void expr_xor(void)
4116 expr_and();
4117 while (tok == '^') {
4118 next();
4119 expr_and();
4120 gen_op('^');
4124 static void expr_or(void)
4126 expr_xor();
4127 while (tok == '|') {
4128 next();
4129 expr_xor();
4130 gen_op('|');
4134 /* XXX: fix this mess */
4135 static void expr_land_const(void)
4137 expr_or();
4138 while (tok == TOK_LAND) {
4139 next();
4140 expr_or();
4141 gen_op(TOK_LAND);
4145 /* XXX: fix this mess */
4146 static void expr_lor_const(void)
4148 expr_land_const();
4149 while (tok == TOK_LOR) {
4150 next();
4151 expr_land_const();
4152 gen_op(TOK_LOR);
4156 /* only used if non constant */
4157 static void expr_land(void)
4159 int t;
4161 expr_or();
4162 if (tok == TOK_LAND) {
4163 t = 0;
4164 save_regs(1);
4165 for(;;) {
4166 t = gtst(1, t);
4167 if (tok != TOK_LAND) {
4168 vseti(VT_JMPI, t);
4169 break;
4171 next();
4172 expr_or();
4177 static void expr_lor(void)
4179 int t;
4181 expr_land();
4182 if (tok == TOK_LOR) {
4183 t = 0;
4184 save_regs(1);
4185 for(;;) {
4186 t = gtst(0, t);
4187 if (tok != TOK_LOR) {
4188 vseti(VT_JMP, t);
4189 break;
4191 next();
4192 expr_land();
4197 /* XXX: better constant handling */
4198 static void expr_cond(void)
4200 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4201 SValue sv;
4202 CType type, type1, type2;
4204 if (const_wanted) {
4205 expr_lor_const();
4206 if (tok == '?') {
4207 CType boolean;
4208 int c;
4209 boolean.t = VT_BOOL;
4210 vdup();
4211 gen_cast(&boolean);
4212 c = vtop->c.i;
4213 vpop();
4214 next();
4215 if (tok != ':' || !gnu_ext) {
4216 vpop();
4217 gexpr();
4219 if (!c)
4220 vpop();
4221 skip(':');
4222 expr_cond();
4223 if (c)
4224 vpop();
4226 } else {
4227 expr_lor();
4228 if (tok == '?') {
4229 next();
4230 if (vtop != vstack) {
4231 /* needed to avoid having different registers saved in
4232 each branch */
4233 if (is_float(vtop->type.t)) {
4234 rc = RC_FLOAT;
4235 #ifdef TCC_TARGET_X86_64
4236 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4237 rc = RC_ST0;
4239 #endif
4241 else
4242 rc = RC_INT;
4243 gv(rc);
4244 save_regs(1);
4246 if (tok == ':' && gnu_ext) {
4247 gv_dup();
4248 tt = gtst(1, 0);
4249 } else {
4250 tt = gtst(1, 0);
4251 gexpr();
4253 type1 = vtop->type;
4254 sv = *vtop; /* save value to handle it later */
4255 vtop--; /* no vpop so that FP stack is not flushed */
4256 skip(':');
4257 u = gjmp(0);
4258 gsym(tt);
4259 expr_cond();
4260 type2 = vtop->type;
4262 t1 = type1.t;
4263 bt1 = t1 & VT_BTYPE;
4264 t2 = type2.t;
4265 bt2 = t2 & VT_BTYPE;
4266 /* cast operands to correct type according to ISOC rules */
4267 if (is_float(bt1) || is_float(bt2)) {
4268 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4269 type.t = VT_LDOUBLE;
4270 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4271 type.t = VT_DOUBLE;
4272 } else {
4273 type.t = VT_FLOAT;
4275 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4276 /* cast to biggest op */
4277 type.t = VT_LLONG;
4278 /* convert to unsigned if it does not fit in a long long */
4279 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4280 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4281 type.t |= VT_UNSIGNED;
4282 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4283 /* If one is a null ptr constant the result type
4284 is the other. */
4285 if (is_null_pointer (vtop))
4286 type = type1;
4287 else if (is_null_pointer (&sv))
4288 type = type2;
4289 /* XXX: test pointer compatibility, C99 has more elaborate
4290 rules here. */
4291 else
4292 type = type1;
4293 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4294 /* XXX: test function pointer compatibility */
4295 type = bt1 == VT_FUNC ? type1 : type2;
4296 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4297 /* XXX: test structure compatibility */
4298 type = bt1 == VT_STRUCT ? type1 : type2;
4299 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4300 /* NOTE: as an extension, we accept void on only one side */
4301 type.t = VT_VOID;
4302 } else {
4303 /* integer operations */
4304 type.t = VT_INT;
4305 /* convert to unsigned if it does not fit in an integer */
4306 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4307 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4308 type.t |= VT_UNSIGNED;
4311 /* now we convert second operand */
4312 gen_cast(&type);
4313 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4314 gaddrof();
4315 rc = RC_INT;
4316 if (is_float(type.t)) {
4317 rc = RC_FLOAT;
4318 #ifdef TCC_TARGET_X86_64
4319 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4320 rc = RC_ST0;
4322 #endif
4323 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4324 /* for long longs, we use fixed registers to avoid having
4325 to handle a complicated move */
4326 rc = RC_IRET;
4329 r2 = gv(rc);
4330 /* this is horrible, but we must also convert first
4331 operand */
4332 tt = gjmp(0);
4333 gsym(u);
4334 /* put again first value and cast it */
4335 *vtop = sv;
4336 gen_cast(&type);
4337 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4338 gaddrof();
4339 r1 = gv(rc);
4340 move_reg(r2, r1, type.t);
4341 vtop->r = r2;
4342 gsym(tt);
4347 static void expr_eq(void)
4349 int t;
4351 expr_cond();
4352 if (tok == '=' ||
4353 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4354 tok == TOK_A_XOR || tok == TOK_A_OR ||
4355 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4356 test_lvalue();
4357 t = tok;
4358 next();
4359 if (t == '=') {
4360 expr_eq();
4361 } else {
4362 vdup();
4363 expr_eq();
4364 gen_op(t & 0x7f);
4366 vstore();
4370 ST_FUNC void gexpr(void)
4372 while (1) {
4373 expr_eq();
4374 if (tok != ',')
4375 break;
4376 vpop();
4377 next();
4381 /* parse an expression and return its type without any side effect. */
4382 static void expr_type(CType *type)
4384 int saved_nocode_wanted;
4386 saved_nocode_wanted = nocode_wanted;
4387 nocode_wanted = 1;
4388 gexpr();
4389 *type = vtop->type;
4390 vpop();
4391 nocode_wanted = saved_nocode_wanted;
4394 /* parse a unary expression and return its type without any side
4395 effect. */
4396 static void unary_type(CType *type)
4398 int a;
4400 a = nocode_wanted;
4401 nocode_wanted = 1;
4402 unary();
4403 *type = vtop->type;
4404 vpop();
4405 nocode_wanted = a;
4408 /* parse a constant expression and return value in vtop. */
4409 static void expr_const1(void)
4411 int a;
4412 a = const_wanted;
4413 const_wanted = 1;
4414 expr_cond();
4415 const_wanted = a;
4418 /* parse an integer constant and return its value. */
4419 ST_FUNC int expr_const(void)
4421 int c;
4422 expr_const1();
4423 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4424 expect("constant expression");
4425 c = vtop->c.i;
4426 vpop();
4427 return c;
4430 /* return the label token if current token is a label, otherwise
4431 return zero */
4432 static int is_label(void)
4434 int last_tok;
4436 /* fast test first */
4437 if (tok < TOK_UIDENT)
4438 return 0;
4439 /* no need to save tokc because tok is an identifier */
4440 last_tok = tok;
4441 next();
4442 if (tok == ':') {
4443 next();
4444 return last_tok;
4445 } else {
4446 unget_tok(last_tok);
4447 return 0;
4451 static void label_or_decl(int l)
4453 int last_tok;
4455 /* fast test first */
4456 if (tok >= TOK_UIDENT)
4458 /* no need to save tokc because tok is an identifier */
4459 last_tok = tok;
4460 next();
4461 if (tok == ':') {
4462 unget_tok(last_tok);
4463 return;
4465 unget_tok(last_tok);
4467 decl(l);
4470 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4471 int case_reg, int is_expr)
4473 int a, b, c, d;
4474 Sym *s, *frame_bottom;
4476 /* generate line number info */
4477 if (tcc_state->do_debug &&
4478 (last_line_num != file->line_num || last_ind != ind)) {
4479 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4480 last_ind = ind;
4481 last_line_num = file->line_num;
4484 if (is_expr) {
4485 /* default return value is (void) */
4486 vpushi(0);
4487 vtop->type.t = VT_VOID;
4490 if (tok == TOK_IF) {
4491 /* if test */
4492 next();
4493 skip('(');
4494 gexpr();
4495 skip(')');
4496 a = gtst(1, 0);
4497 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4498 c = tok;
4499 if (c == TOK_ELSE) {
4500 next();
4501 d = gjmp(0);
4502 gsym(a);
4503 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4504 gsym(d); /* patch else jmp */
4505 } else
4506 gsym(a);
4507 } else if (tok == TOK_WHILE) {
4508 next();
4509 d = ind;
4510 skip('(');
4511 gexpr();
4512 skip(')');
4513 a = gtst(1, 0);
4514 b = 0;
4515 block(&a, &b, case_sym, def_sym, case_reg, 0);
4516 gjmp_addr(d);
4517 gsym(a);
4518 gsym_addr(b, d);
4519 } else if (tok == '{') {
4520 Sym *llabel;
4521 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4523 next();
4524 /* record local declaration stack position */
4525 s = local_stack;
4526 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4527 frame_bottom->next = scope_stack_bottom;
4528 scope_stack_bottom = frame_bottom;
4529 llabel = local_label_stack;
4531 /* save VLA state */
4532 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4533 if (saved_vla_sp_loc != &vla_sp_root_loc)
4534 vla_sp_loc = &block_vla_sp_loc;
4536 saved_vla_flags = vla_flags;
4537 vla_flags |= VLA_NEED_NEW_FRAME;
4539 /* handle local labels declarations */
4540 if (tok == TOK_LABEL) {
4541 next();
4542 for(;;) {
4543 if (tok < TOK_UIDENT)
4544 expect("label identifier");
4545 label_push(&local_label_stack, tok, LABEL_DECLARED);
4546 next();
4547 if (tok == ',') {
4548 next();
4549 } else {
4550 skip(';');
4551 break;
4555 while (tok != '}') {
4556 label_or_decl(VT_LOCAL);
4557 if (tok != '}') {
4558 if (is_expr)
4559 vpop();
4560 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4563 /* pop locally defined labels */
4564 label_pop(&local_label_stack, llabel);
4565 if(is_expr) {
4566 /* XXX: this solution makes only valgrind happy...
4567 triggered by gcc.c-torture/execute/20000917-1.c */
4568 Sym *p;
4569 switch(vtop->type.t & VT_BTYPE) {
4570 case VT_PTR:
4571 case VT_STRUCT:
4572 case VT_ENUM:
4573 case VT_FUNC:
4574 for(p=vtop->type.ref;p;p=p->prev)
4575 if(p->prev==s)
4576 tcc_error("unsupported expression type");
4579 /* pop locally defined symbols */
4580 scope_stack_bottom = scope_stack_bottom->next;
4581 sym_pop(&local_stack, s);
4583 /* Pop VLA frames and restore stack pointer if required */
4584 if (saved_vla_sp_loc != &vla_sp_root_loc)
4585 *saved_vla_sp_loc = block_vla_sp_loc;
4586 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4587 vla_sp_loc = saved_vla_sp_loc;
4588 gen_vla_sp_restore(*vla_sp_loc);
4590 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4592 next();
4593 } else if (tok == TOK_RETURN) {
4594 next();
4595 if (tok != ';') {
4596 gexpr();
4597 gen_assign_cast(&func_vt);
4598 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4599 CType type, ret_type;
4600 int ret_align;
4601 if (gfunc_sret(&func_vt, &ret_type, &ret_align)) {
4602 /* if returning structure, must copy it to implicit
4603 first pointer arg location */
4604 type = func_vt;
4605 mk_pointer(&type);
4606 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4607 indir();
4608 vswap();
4609 /* copy structure value to pointer */
4610 vstore();
4611 } else {
4612 /* returning structure packed into registers */
4613 int size, addr, align;
4614 size = type_size(&func_vt,&align);
4615 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4616 && (align & (ret_align-1))) {
4617 loc = (loc - size) & -align;
4618 addr = loc;
4619 type = func_vt;
4620 vset(&type, VT_LOCAL | VT_LVAL, addr);
4621 vswap();
4622 vstore();
4623 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4625 vtop->type = ret_type;
4626 if (is_float(ret_type.t))
4627 gv(rc_fret(ret_type.t));
4628 else
4629 gv(RC_IRET);
4631 } else if (is_float(func_vt.t)) {
4632 gv(rc_fret(func_vt.t));
4633 } else {
4634 gv(RC_IRET);
4636 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4638 skip(';');
4639 rsym = gjmp(rsym); /* jmp */
4640 } else if (tok == TOK_BREAK) {
4641 /* compute jump */
4642 if (!bsym)
4643 tcc_error("cannot break");
4644 *bsym = gjmp(*bsym);
4645 next();
4646 skip(';');
4647 } else if (tok == TOK_CONTINUE) {
4648 /* compute jump */
4649 if (!csym)
4650 tcc_error("cannot continue");
4651 *csym = gjmp(*csym);
4652 next();
4653 skip(';');
4654 } else if (tok == TOK_FOR) {
4655 int e;
4656 next();
4657 skip('(');
4658 s = local_stack;
4659 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4660 frame_bottom->next = scope_stack_bottom;
4661 scope_stack_bottom = frame_bottom;
4662 if (tok != ';') {
4663 /* c99 for-loop init decl? */
4664 if (!decl0(VT_LOCAL, 1)) {
4665 /* no, regular for-loop init expr */
4666 gexpr();
4667 vpop();
4670 skip(';');
4671 d = ind;
4672 c = ind;
4673 a = 0;
4674 b = 0;
4675 if (tok != ';') {
4676 gexpr();
4677 a = gtst(1, 0);
4679 skip(';');
4680 if (tok != ')') {
4681 e = gjmp(0);
4682 c = ind;
4683 gexpr();
4684 vpop();
4685 gjmp_addr(d);
4686 gsym(e);
4688 skip(')');
4689 block(&a, &b, case_sym, def_sym, case_reg, 0);
4690 gjmp_addr(c);
4691 gsym(a);
4692 gsym_addr(b, c);
4693 scope_stack_bottom = scope_stack_bottom->next;
4694 sym_pop(&local_stack, s);
4695 } else
4696 if (tok == TOK_DO) {
4697 next();
4698 a = 0;
4699 b = 0;
4700 d = ind;
4701 block(&a, &b, case_sym, def_sym, case_reg, 0);
4702 skip(TOK_WHILE);
4703 skip('(');
4704 gsym(b);
4705 gexpr();
4706 c = gtst(0, 0);
4707 gsym_addr(c, d);
4708 skip(')');
4709 gsym(a);
4710 skip(';');
4711 } else
4712 if (tok == TOK_SWITCH) {
4713 next();
4714 skip('(');
4715 gexpr();
4716 /* XXX: other types than integer */
4717 case_reg = gv(RC_INT);
4718 vpop();
4719 skip(')');
4720 a = 0;
4721 b = gjmp(0); /* jump to first case */
4722 c = 0;
4723 block(&a, csym, &b, &c, case_reg, 0);
4724 /* if no default, jmp after switch */
4725 if (c == 0)
4726 c = ind;
4727 /* default label */
4728 gsym_addr(b, c);
4729 /* break label */
4730 gsym(a);
4731 } else
4732 if (tok == TOK_CASE) {
4733 int v1, v2;
4734 if (!case_sym)
4735 expect("switch");
4736 next();
4737 v1 = expr_const();
4738 v2 = v1;
4739 if (gnu_ext && tok == TOK_DOTS) {
4740 next();
4741 v2 = expr_const();
4742 if (v2 < v1)
4743 tcc_warning("empty case range");
4745 /* since a case is like a label, we must skip it with a jmp */
4746 b = gjmp(0);
4747 gsym(*case_sym);
4748 vseti(case_reg, 0);
4749 vpushi(v1);
4750 if (v1 == v2) {
4751 gen_op(TOK_EQ);
4752 *case_sym = gtst(1, 0);
4753 } else {
4754 gen_op(TOK_GE);
4755 *case_sym = gtst(1, 0);
4756 vseti(case_reg, 0);
4757 vpushi(v2);
4758 gen_op(TOK_LE);
4759 *case_sym = gtst(1, *case_sym);
4761 gsym(b);
4762 skip(':');
4763 is_expr = 0;
4764 goto block_after_label;
4765 } else
4766 if (tok == TOK_DEFAULT) {
4767 next();
4768 skip(':');
4769 if (!def_sym)
4770 expect("switch");
4771 if (*def_sym)
4772 tcc_error("too many 'default'");
4773 *def_sym = ind;
4774 is_expr = 0;
4775 goto block_after_label;
4776 } else
4777 if (tok == TOK_GOTO) {
4778 next();
4779 if (tok == '*' && gnu_ext) {
4780 /* computed goto */
4781 next();
4782 gexpr();
4783 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4784 expect("pointer");
4785 ggoto();
4786 } else if (tok >= TOK_UIDENT) {
4787 s = label_find(tok);
4788 /* put forward definition if needed */
4789 if (!s) {
4790 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4791 } else {
4792 if (s->r == LABEL_DECLARED)
4793 s->r = LABEL_FORWARD;
4795 /* label already defined */
4796 if (vla_flags & VLA_IN_SCOPE) {
4797 /* If VLAs are in use, save the current stack pointer and
4798 reset the stack pointer to what it was at function entry
4799 (label will restore stack pointer in inner scopes) */
4800 vla_sp_save();
4801 gen_vla_sp_restore(vla_sp_root_loc);
4803 if (s->r & LABEL_FORWARD)
4804 s->jnext = gjmp(s->jnext);
4805 else
4806 gjmp_addr(s->jnext);
4807 next();
4808 } else {
4809 expect("label identifier");
4811 skip(';');
4812 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4813 asm_instr();
4814 } else {
4815 b = is_label();
4816 if (b) {
4817 /* label case */
4818 if (vla_flags & VLA_IN_SCOPE) {
4819 /* save/restore stack pointer across label
4820 this is a no-op when combined with the load immediately
4821 after the label unless we arrive via goto */
4822 vla_sp_save();
4824 s = label_find(b);
4825 if (s) {
4826 if (s->r == LABEL_DEFINED)
4827 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4828 gsym(s->jnext);
4829 s->r = LABEL_DEFINED;
4830 } else {
4831 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4833 s->jnext = ind;
4834 if (vla_flags & VLA_IN_SCOPE) {
4835 gen_vla_sp_restore(*vla_sp_loc);
4836 vla_flags |= VLA_NEED_NEW_FRAME;
4838 /* we accept this, but it is a mistake */
4839 block_after_label:
4840 if (tok == '}') {
4841 tcc_warning("deprecated use of label at end of compound statement");
4842 } else {
4843 if (is_expr)
4844 vpop();
4845 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4847 } else {
4848 /* expression case */
4849 if (tok != ';') {
4850 if (is_expr) {
4851 vpop();
4852 gexpr();
4853 } else {
4854 gexpr();
4855 vpop();
4858 skip(';');
4863 /* t is the array or struct type. c is the array or struct
4864 address. cur_index/cur_field is the pointer to the current
4865 value. 'size_only' is true if only size info is needed (only used
4866 in arrays) */
4867 static void decl_designator(CType *type, Section *sec, unsigned long c,
4868 int *cur_index, Sym **cur_field,
4869 int size_only)
4871 Sym *s, *f;
4872 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4873 CType type1;
4875 notfirst = 0;
4876 elem_size = 0;
4877 nb_elems = 1;
4878 if (gnu_ext && (l = is_label()) != 0)
4879 goto struct_field;
4880 while (tok == '[' || tok == '.') {
4881 if (tok == '[') {
4882 if (!(type->t & VT_ARRAY))
4883 expect("array type");
4884 s = type->ref;
4885 next();
4886 index = expr_const();
4887 if (index < 0 || (s->c >= 0 && index >= s->c))
4888 expect("invalid index");
4889 if (tok == TOK_DOTS && gnu_ext) {
4890 next();
4891 index_last = expr_const();
4892 if (index_last < 0 ||
4893 (s->c >= 0 && index_last >= s->c) ||
4894 index_last < index)
4895 expect("invalid index");
4896 } else {
4897 index_last = index;
4899 skip(']');
4900 if (!notfirst)
4901 *cur_index = index_last;
4902 type = pointed_type(type);
4903 elem_size = type_size(type, &align);
4904 c += index * elem_size;
4905 /* NOTE: we only support ranges for last designator */
4906 nb_elems = index_last - index + 1;
4907 if (nb_elems != 1) {
4908 notfirst = 1;
4909 break;
4911 } else {
4912 next();
4913 l = tok;
4914 next();
4915 struct_field:
4916 if ((type->t & VT_BTYPE) != VT_STRUCT)
4917 expect("struct/union type");
4918 s = type->ref;
4919 l |= SYM_FIELD;
4920 f = s->next;
4921 while (f) {
4922 if (f->v == l)
4923 break;
4924 f = f->next;
4926 if (!f)
4927 expect("field");
4928 if (!notfirst)
4929 *cur_field = f;
4930 /* XXX: fix this mess by using explicit storage field */
4931 type1 = f->type;
4932 type1.t |= (type->t & ~VT_TYPE);
4933 type = &type1;
4934 c += f->c;
4936 notfirst = 1;
4938 if (notfirst) {
4939 if (tok == '=') {
4940 next();
4941 } else {
4942 if (!gnu_ext)
4943 expect("=");
4945 } else {
4946 if (type->t & VT_ARRAY) {
4947 index = *cur_index;
4948 type = pointed_type(type);
4949 c += index * type_size(type, &align);
4950 } else {
4951 f = *cur_field;
4952 if (!f)
4953 tcc_error("too many field init");
4954 /* XXX: fix this mess by using explicit storage field */
4955 type1 = f->type;
4956 type1.t |= (type->t & ~VT_TYPE);
4957 type = &type1;
4958 c += f->c;
4961 decl_initializer(type, sec, c, 0, size_only);
4963 /* XXX: make it more general */
4964 if (!size_only && nb_elems > 1) {
4965 unsigned long c_end;
4966 uint8_t *src, *dst;
4967 int i;
4969 if (!sec)
4970 tcc_error("range init not supported yet for dynamic storage");
4971 c_end = c + nb_elems * elem_size;
4972 if (c_end > sec->data_allocated)
4973 section_realloc(sec, c_end);
4974 src = sec->data + c;
4975 dst = src;
4976 for(i = 1; i < nb_elems; i++) {
4977 dst += elem_size;
4978 memcpy(dst, src, elem_size);
4983 #define EXPR_VAL 0
4984 #define EXPR_CONST 1
4985 #define EXPR_ANY 2
4987 /* store a value or an expression directly in global data or in local array */
4988 static void init_putv(CType *type, Section *sec, unsigned long c,
4989 int v, int expr_type)
4991 int saved_global_expr, bt, bit_pos, bit_size;
4992 void *ptr;
4993 unsigned long long bit_mask;
4994 CType dtype;
4996 switch(expr_type) {
4997 case EXPR_VAL:
4998 vpushi(v);
4999 break;
5000 case EXPR_CONST:
5001 /* compound literals must be allocated globally in this case */
5002 saved_global_expr = global_expr;
5003 global_expr = 1;
5004 expr_const1();
5005 global_expr = saved_global_expr;
5006 /* NOTE: symbols are accepted */
5007 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5008 tcc_error("initializer element is not constant");
5009 break;
5010 case EXPR_ANY:
5011 expr_eq();
5012 break;
5015 dtype = *type;
5016 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5018 if (sec) {
5019 /* XXX: not portable */
5020 /* XXX: generate error if incorrect relocation */
5021 gen_assign_cast(&dtype);
5022 bt = type->t & VT_BTYPE;
5023 /* we'll write at most 12 bytes */
5024 if (c + 12 > sec->data_allocated) {
5025 section_realloc(sec, c + 12);
5027 ptr = sec->data + c;
5028 /* XXX: make code faster ? */
5029 if (!(type->t & VT_BITFIELD)) {
5030 bit_pos = 0;
5031 bit_size = 32;
5032 bit_mask = -1LL;
5033 } else {
5034 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5035 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5036 bit_mask = (1LL << bit_size) - 1;
5038 if ((vtop->r & VT_SYM) &&
5039 (bt == VT_BYTE ||
5040 bt == VT_SHORT ||
5041 bt == VT_DOUBLE ||
5042 bt == VT_LDOUBLE ||
5043 bt == VT_LLONG ||
5044 (bt == VT_INT && bit_size != 32)))
5045 tcc_error("initializer element is not computable at load time");
5046 switch(bt) {
5047 case VT_BOOL:
5048 vtop->c.i = (vtop->c.i != 0);
5049 case VT_BYTE:
5050 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5051 break;
5052 case VT_SHORT:
5053 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5054 break;
5055 case VT_DOUBLE:
5056 *(double *)ptr = vtop->c.d;
5057 break;
5058 case VT_LDOUBLE:
5059 *(long double *)ptr = vtop->c.ld;
5060 break;
5061 case VT_LLONG:
5062 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5063 break;
5064 default:
5065 if (vtop->r & VT_SYM) {
5066 greloc(sec, vtop->sym, c, R_DATA_PTR);
5068 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5069 break;
5071 vtop--;
5072 } else {
5073 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5074 vswap();
5075 vstore();
5076 vpop();
5080 /* put zeros for variable based init */
5081 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5083 if (sec) {
5084 /* nothing to do because globals are already set to zero */
5085 } else {
5086 vpush_global_sym(&func_old_type, TOK_memset);
5087 vseti(VT_LOCAL, c);
5088 vpushi(0);
5089 vpushs(size);
5090 gfunc_call(3);
5094 /* 't' contains the type and storage info. 'c' is the offset of the
5095 object in section 'sec'. If 'sec' is NULL, it means stack based
5096 allocation. 'first' is true if array '{' must be read (multi
5097 dimension implicit array init handling). 'size_only' is true if
5098 size only evaluation is wanted (only for arrays). */
5099 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5100 int first, int size_only)
5102 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5103 int size1, align1, expr_type;
5104 Sym *s, *f;
5105 CType *t1;
5107 if (type->t & VT_VLA) {
5108 int a;
5110 /* save current stack pointer */
5111 if (vla_flags & VLA_NEED_NEW_FRAME) {
5112 vla_sp_save();
5113 vla_flags = VLA_IN_SCOPE;
5114 vla_sp_loc = &vla_sp_loc_tmp;
5117 vla_runtime_type_size(type, &a);
5118 gen_vla_alloc(type, a);
5119 vset(type, VT_LOCAL|VT_LVAL, c);
5120 vswap();
5121 vstore();
5122 vpop();
5123 } else if (type->t & VT_ARRAY) {
5124 s = type->ref;
5125 n = s->c;
5126 array_length = 0;
5127 t1 = pointed_type(type);
5128 size1 = type_size(t1, &align1);
5130 no_oblock = 1;
5131 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5132 tok == '{') {
5133 if (tok != '{')
5134 tcc_error("character array initializer must be a literal,"
5135 " optionally enclosed in braces");
5136 skip('{');
5137 no_oblock = 0;
5140 /* only parse strings here if correct type (otherwise: handle
5141 them as ((w)char *) expressions */
5142 if ((tok == TOK_LSTR &&
5143 #ifdef TCC_TARGET_PE
5144 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5145 #else
5146 (t1->t & VT_BTYPE) == VT_INT
5147 #endif
5148 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5149 while (tok == TOK_STR || tok == TOK_LSTR) {
5150 int cstr_len, ch;
5151 CString *cstr;
5153 cstr = tokc.cstr;
5154 /* compute maximum number of chars wanted */
5155 if (tok == TOK_STR)
5156 cstr_len = cstr->size;
5157 else
5158 cstr_len = cstr->size / sizeof(nwchar_t);
5159 cstr_len--;
5160 nb = cstr_len;
5161 if (n >= 0 && nb > (n - array_length))
5162 nb = n - array_length;
5163 if (!size_only) {
5164 if (cstr_len > nb)
5165 tcc_warning("initializer-string for array is too long");
5166 /* in order to go faster for common case (char
5167 string in global variable, we handle it
5168 specifically */
5169 if (sec && tok == TOK_STR && size1 == 1) {
5170 memcpy(sec->data + c + array_length, cstr->data, nb);
5171 } else {
5172 for(i=0;i<nb;i++) {
5173 if (tok == TOK_STR)
5174 ch = ((unsigned char *)cstr->data)[i];
5175 else
5176 ch = ((nwchar_t *)cstr->data)[i];
5177 init_putv(t1, sec, c + (array_length + i) * size1,
5178 ch, EXPR_VAL);
5182 array_length += nb;
5183 next();
5185 /* only add trailing zero if enough storage (no
5186 warning in this case since it is standard) */
5187 if (n < 0 || array_length < n) {
5188 if (!size_only) {
5189 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5191 array_length++;
5193 } else {
5194 index = 0;
5195 while (tok != '}') {
5196 decl_designator(type, sec, c, &index, NULL, size_only);
5197 if (n >= 0 && index >= n)
5198 tcc_error("index too large");
5199 /* must put zero in holes (note that doing it that way
5200 ensures that it even works with designators) */
5201 if (!size_only && array_length < index) {
5202 init_putz(t1, sec, c + array_length * size1,
5203 (index - array_length) * size1);
5205 index++;
5206 if (index > array_length)
5207 array_length = index;
5208 /* special test for multi dimensional arrays (may not
5209 be strictly correct if designators are used at the
5210 same time) */
5211 if (index >= n && no_oblock)
5212 break;
5213 if (tok == '}')
5214 break;
5215 skip(',');
5218 if (!no_oblock)
5219 skip('}');
5220 /* put zeros at the end */
5221 if (!size_only && n >= 0 && array_length < n) {
5222 init_putz(t1, sec, c + array_length * size1,
5223 (n - array_length) * size1);
5225 /* patch type size if needed */
5226 if (n < 0)
5227 s->c = array_length;
5228 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5229 (sec || !first || tok == '{')) {
5230 int par_count;
5232 /* NOTE: the previous test is a specific case for automatic
5233 struct/union init */
5234 /* XXX: union needs only one init */
5236 /* XXX: this test is incorrect for local initializers
5237 beginning with ( without {. It would be much more difficult
5238 to do it correctly (ideally, the expression parser should
5239 be used in all cases) */
5240 par_count = 0;
5241 if (tok == '(') {
5242 AttributeDef ad1;
5243 CType type1;
5244 next();
5245 while (tok == '(') {
5246 par_count++;
5247 next();
5249 if (!parse_btype(&type1, &ad1))
5250 expect("cast");
5251 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5252 #if 0
5253 if (!is_assignable_types(type, &type1))
5254 tcc_error("invalid type for cast");
5255 #endif
5256 skip(')');
5258 no_oblock = 1;
5259 if (first || tok == '{') {
5260 skip('{');
5261 no_oblock = 0;
5263 s = type->ref;
5264 f = s->next;
5265 array_length = 0;
5266 index = 0;
5267 n = s->c;
5268 while (tok != '}') {
5269 decl_designator(type, sec, c, NULL, &f, size_only);
5270 index = f->c;
5271 if (!size_only && array_length < index) {
5272 init_putz(type, sec, c + array_length,
5273 index - array_length);
5275 index = index + type_size(&f->type, &align1);
5276 if (index > array_length)
5277 array_length = index;
5279 /* gr: skip fields from same union - ugly. */
5280 while (f->next) {
5281 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5282 /* test for same offset */
5283 if (f->next->c != f->c)
5284 break;
5285 /* if yes, test for bitfield shift */
5286 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5287 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5288 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5289 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5290 if (bit_pos_1 != bit_pos_2)
5291 break;
5293 f = f->next;
5296 f = f->next;
5297 if (no_oblock && f == NULL)
5298 break;
5299 if (tok == '}')
5300 break;
5301 skip(',');
5303 /* put zeros at the end */
5304 if (!size_only && array_length < n) {
5305 init_putz(type, sec, c + array_length,
5306 n - array_length);
5308 if (!no_oblock)
5309 skip('}');
5310 while (par_count) {
5311 skip(')');
5312 par_count--;
5314 } else if (tok == '{') {
5315 next();
5316 decl_initializer(type, sec, c, first, size_only);
5317 skip('}');
5318 } else if (size_only) {
5319 /* just skip expression */
5320 parlevel = parlevel1 = 0;
5321 while ((parlevel > 0 || parlevel1 > 0 ||
5322 (tok != '}' && tok != ',')) && tok != -1) {
5323 if (tok == '(')
5324 parlevel++;
5325 else if (tok == ')')
5326 parlevel--;
5327 else if (tok == '{')
5328 parlevel1++;
5329 else if (tok == '}')
5330 parlevel1--;
5331 next();
5333 } else {
5334 /* currently, we always use constant expression for globals
5335 (may change for scripting case) */
5336 expr_type = EXPR_CONST;
5337 if (!sec)
5338 expr_type = EXPR_ANY;
5339 init_putv(type, sec, c, 0, expr_type);
5343 /* parse an initializer for type 't' if 'has_init' is non zero, and
5344 allocate space in local or global data space ('r' is either
5345 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5346 variable 'v' with an associated name represented by 'asm_label' of
5347 scope 'scope' is declared before initializers are parsed. If 'v' is
5348 zero, then a reference to the new object is put in the value stack.
5349 If 'has_init' is 2, a special parsing is done to handle string
5350 constants. */
5351 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5352 int has_init, int v, char *asm_label,
5353 int scope)
5355 int size, align, addr, data_offset;
5356 int level;
5357 ParseState saved_parse_state = {0};
5358 TokenString init_str;
5359 Section *sec;
5360 Sym *flexible_array;
5362 flexible_array = NULL;
5363 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5364 Sym *field = type->ref->next;
5365 if (field) {
5366 while (field->next)
5367 field = field->next;
5368 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5369 flexible_array = field;
5373 size = type_size(type, &align);
5374 /* If unknown size, we must evaluate it before
5375 evaluating initializers because
5376 initializers can generate global data too
5377 (e.g. string pointers or ISOC99 compound
5378 literals). It also simplifies local
5379 initializers handling */
5380 tok_str_new(&init_str);
5381 if (size < 0 || (flexible_array && has_init)) {
5382 if (!has_init)
5383 tcc_error("unknown type size");
5384 /* get all init string */
5385 if (has_init == 2) {
5386 /* only get strings */
5387 while (tok == TOK_STR || tok == TOK_LSTR) {
5388 tok_str_add_tok(&init_str);
5389 next();
5391 } else {
5392 level = 0;
5393 while (level > 0 || (tok != ',' && tok != ';')) {
5394 if (tok < 0)
5395 tcc_error("unexpected end of file in initializer");
5396 tok_str_add_tok(&init_str);
5397 if (tok == '{')
5398 level++;
5399 else if (tok == '}') {
5400 level--;
5401 if (level <= 0) {
5402 next();
5403 break;
5406 next();
5409 tok_str_add(&init_str, -1);
5410 tok_str_add(&init_str, 0);
5412 /* compute size */
5413 save_parse_state(&saved_parse_state);
5415 macro_ptr = init_str.str;
5416 next();
5417 decl_initializer(type, NULL, 0, 1, 1);
5418 /* prepare second initializer parsing */
5419 macro_ptr = init_str.str;
5420 next();
5422 /* if still unknown size, error */
5423 size = type_size(type, &align);
5424 if (size < 0)
5425 tcc_error("unknown type size");
5427 if (flexible_array)
5428 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5429 /* take into account specified alignment if bigger */
5430 if (ad->aligned) {
5431 if (ad->aligned > align)
5432 align = ad->aligned;
5433 } else if (ad->packed) {
5434 align = 1;
5436 if ((r & VT_VALMASK) == VT_LOCAL) {
5437 sec = NULL;
5438 #ifdef CONFIG_TCC_BCHECK
5439 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5440 loc--;
5442 #endif
5443 loc = (loc - size) & -align;
5444 addr = loc;
5445 #ifdef CONFIG_TCC_BCHECK
5446 /* handles bounds */
5447 /* XXX: currently, since we do only one pass, we cannot track
5448 '&' operators, so we add only arrays */
5449 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5450 unsigned long *bounds_ptr;
5451 /* add padding between regions */
5452 loc--;
5453 /* then add local bound info */
5454 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5455 bounds_ptr[0] = addr;
5456 bounds_ptr[1] = size;
5458 #endif
5459 if (v) {
5460 /* local variable */
5461 sym_push(v, type, r, addr);
5462 } else {
5463 /* push local reference */
5464 vset(type, r, addr);
5466 } else {
5467 Sym *sym;
5469 sym = NULL;
5470 if (v && scope == VT_CONST) {
5471 /* see if the symbol was already defined */
5472 sym = sym_find(v);
5473 if (sym) {
5474 if (!is_compatible_types(&sym->type, type))
5475 tcc_error("incompatible types for redefinition of '%s'",
5476 get_tok_str(v, NULL));
5477 if (sym->type.t & VT_EXTERN) {
5478 /* if the variable is extern, it was not allocated */
5479 sym->type.t &= ~VT_EXTERN;
5480 /* set array size if it was ommited in extern
5481 declaration */
5482 if ((sym->type.t & VT_ARRAY) &&
5483 sym->type.ref->c < 0 &&
5484 type->ref->c >= 0)
5485 sym->type.ref->c = type->ref->c;
5486 } else {
5487 /* we accept several definitions of the same
5488 global variable. this is tricky, because we
5489 must play with the SHN_COMMON type of the symbol */
5490 /* XXX: should check if the variable was already
5491 initialized. It is incorrect to initialized it
5492 twice */
5493 /* no init data, we won't add more to the symbol */
5494 if (!has_init)
5495 goto no_alloc;
5500 /* allocate symbol in corresponding section */
5501 sec = ad->section;
5502 if (!sec) {
5503 if (has_init) {
5504 if (type->t & VT_TLS) {
5505 if (!tdata_section)
5506 tdata_section = new_section(tcc_state, ".tdata",
5507 SHT_PROGBITS,
5508 SHF_ALLOC | SHF_WRITE | SHF_TLS);
5509 sec = tdata_section;
5510 } else
5511 sec = data_section;
5513 else if (tcc_state->nocommon) {
5514 if (type->t & VT_TLS) {
5515 if (!tbss_section)
5516 tbss_section = new_section(tcc_state, ".tbss",
5517 SHT_NOBITS,
5518 SHF_ALLOC | SHF_WRITE | SHF_TLS);
5519 sec = tbss_section;
5520 } else
5521 sec = bss_section;
5524 if (sec) {
5525 data_offset = sec->data_offset;
5526 data_offset = (data_offset + align - 1) & -align;
5527 addr = data_offset;
5528 /* very important to increment global pointer at this time
5529 because initializers themselves can create new initializers */
5530 data_offset += size;
5531 #ifdef CONFIG_TCC_BCHECK
5532 /* add padding if bound check */
5533 if (tcc_state->do_bounds_check)
5534 data_offset++;
5535 #endif
5536 sec->data_offset = data_offset;
5537 /* allocate section space to put the data */
5538 if (sec->sh_type != SHT_NOBITS &&
5539 data_offset > sec->data_allocated)
5540 section_realloc(sec, data_offset);
5541 /* align section if needed */
5542 if (align > sec->sh_addralign)
5543 sec->sh_addralign = align;
5544 } else {
5545 addr = 0; /* avoid warning */
5548 if (v) {
5549 if (scope != VT_CONST || !sym) {
5550 sym = sym_push(v, type, r | VT_SYM, 0);
5551 sym->asm_label = asm_label;
5553 /* update symbol definition */
5554 if (sec) {
5555 put_extern_sym(sym, sec, addr, size);
5556 } else {
5557 ElfW(Sym) *esym;
5558 /* put a common area */
5559 put_extern_sym(sym, NULL, align, size);
5560 /* XXX: find a nicer way */
5561 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5562 esym->st_shndx = SHN_COMMON;
5564 } else {
5565 CValue cval;
5567 /* push global reference */
5568 sym = get_sym_ref(type, sec, addr, size);
5569 cval.ul = 0;
5570 vsetc(type, VT_CONST | VT_SYM, &cval);
5571 vtop->sym = sym;
5573 /* patch symbol weakness */
5574 if (type->t & VT_WEAK)
5575 weaken_symbol(sym);
5576 #ifdef CONFIG_TCC_BCHECK
5577 /* handles bounds now because the symbol must be defined
5578 before for the relocation */
5579 if (tcc_state->do_bounds_check) {
5580 unsigned long *bounds_ptr;
5582 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5583 /* then add global bound info */
5584 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5585 bounds_ptr[0] = 0; /* relocated */
5586 bounds_ptr[1] = size;
5588 #endif
5590 if (has_init || (type->t & VT_VLA)) {
5591 decl_initializer(type, sec, addr, 1, 0);
5592 /* restore parse state if needed */
5593 if (init_str.str) {
5594 tok_str_free(init_str.str);
5595 restore_parse_state(&saved_parse_state);
5597 /* patch flexible array member size back to -1, */
5598 /* for possible subsequent similar declarations */
5599 if (flexible_array)
5600 flexible_array->type.ref->c = -1;
5602 no_alloc: ;
5605 static void put_func_debug(Sym *sym)
5607 char buf[512];
5609 /* stabs info */
5610 /* XXX: we put here a dummy type */
5611 snprintf(buf, sizeof(buf), "%s:%c1",
5612 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5613 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5614 cur_text_section, sym->c);
5615 /* //gr gdb wants a line at the function */
5616 put_stabn(N_SLINE, 0, file->line_num, 0);
5617 last_ind = 0;
5618 last_line_num = 0;
5621 /* parse an old style function declaration list */
5622 /* XXX: check multiple parameter */
5623 static void func_decl_list(Sym *func_sym)
5625 AttributeDef ad;
5626 int v;
5627 Sym *s;
5628 CType btype, type;
5630 /* parse each declaration */
5631 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5632 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5633 if (!parse_btype(&btype, &ad))
5634 expect("declaration list");
5635 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5636 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5637 tok == ';') {
5638 /* we accept no variable after */
5639 } else {
5640 for(;;) {
5641 type = btype;
5642 type_decl(&type, &ad, &v, TYPE_DIRECT);
5643 /* find parameter in function parameter list */
5644 s = func_sym->next;
5645 while (s != NULL) {
5646 if ((s->v & ~SYM_FIELD) == v)
5647 goto found;
5648 s = s->next;
5650 tcc_error("declaration for parameter '%s' but no such parameter",
5651 get_tok_str(v, NULL));
5652 found:
5653 /* check that no storage specifier except 'register' was given */
5654 if (type.t & VT_STORAGE)
5655 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5656 convert_parameter_type(&type);
5657 /* we can add the type (NOTE: it could be local to the function) */
5658 s->type = type;
5659 /* accept other parameters */
5660 if (tok == ',')
5661 next();
5662 else
5663 break;
5666 skip(';');
5670 /* parse a function defined by symbol 'sym' and generate its code in
5671 'cur_text_section' */
5672 static void gen_function(Sym *sym)
5674 int saved_nocode_wanted = nocode_wanted;
5675 nocode_wanted = 0;
5676 ind = cur_text_section->data_offset;
5677 /* NOTE: we patch the symbol size later */
5678 put_extern_sym(sym, cur_text_section, ind, 0);
5679 funcname = get_tok_str(sym->v, NULL);
5680 func_ind = ind;
5681 /* Initialize VLA state */
5682 vla_sp_loc = &vla_sp_root_loc;
5683 vla_flags = VLA_NEED_NEW_FRAME;
5684 /* put debug symbol */
5685 if (tcc_state->do_debug)
5686 put_func_debug(sym);
5687 /* push a dummy symbol to enable local sym storage */
5688 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5689 gfunc_prolog(&sym->type);
5690 rsym = 0;
5691 block(NULL, NULL, NULL, NULL, 0, 0);
5692 gsym(rsym);
5693 gfunc_epilog();
5694 cur_text_section->data_offset = ind;
5695 label_pop(&global_label_stack, NULL);
5696 /* reset local stack */
5697 scope_stack_bottom = NULL;
5698 sym_pop(&local_stack, NULL);
5699 /* end of function */
5700 /* patch symbol size */
5701 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5702 ind - func_ind;
5703 /* patch symbol weakness (this definition overrules any prototype) */
5704 if (sym->type.t & VT_WEAK)
5705 weaken_symbol(sym);
5706 if (tcc_state->do_debug) {
5707 put_stabn(N_FUN, 0, 0, ind - func_ind);
5709 /* It's better to crash than to generate wrong code */
5710 cur_text_section = NULL;
5711 funcname = ""; /* for safety */
5712 func_vt.t = VT_VOID; /* for safety */
5713 ind = 0; /* for safety */
5714 nocode_wanted = saved_nocode_wanted;
5717 ST_FUNC void gen_inline_functions(void)
5719 Sym *sym;
5720 int *str, inline_generated, i;
5721 struct InlineFunc *fn;
5723 /* iterate while inline function are referenced */
5724 for(;;) {
5725 inline_generated = 0;
5726 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5727 fn = tcc_state->inline_fns[i];
5728 sym = fn->sym;
5729 if (sym && sym->c) {
5730 /* the function was used: generate its code and
5731 convert it to a normal function */
5732 str = fn->token_str;
5733 fn->sym = NULL;
5734 if (file)
5735 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5736 sym->r = VT_SYM | VT_CONST;
5737 sym->type.t &= ~VT_INLINE;
5739 macro_ptr = str;
5740 next();
5741 cur_text_section = text_section;
5742 gen_function(sym);
5743 macro_ptr = NULL; /* fail safe */
5745 inline_generated = 1;
5748 if (!inline_generated)
5749 break;
5751 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5752 fn = tcc_state->inline_fns[i];
5753 str = fn->token_str;
5754 tok_str_free(str);
5756 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5759 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5760 static int decl0(int l, int is_for_loop_init)
5762 int v, has_init, r;
5763 CType type, btype;
5764 Sym *sym;
5765 AttributeDef ad;
5767 while (1) {
5768 if (!parse_btype(&btype, &ad)) {
5769 if (is_for_loop_init)
5770 return 0;
5771 /* skip redundant ';' */
5772 /* XXX: find more elegant solution */
5773 if (tok == ';') {
5774 next();
5775 continue;
5777 if (l == VT_CONST &&
5778 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5779 /* global asm block */
5780 asm_global_instr();
5781 continue;
5783 /* special test for old K&R protos without explicit int
5784 type. Only accepted when defining global data */
5785 if (l == VT_LOCAL || tok < TOK_DEFINE)
5786 break;
5787 btype.t = VT_INT;
5789 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5790 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5791 tok == ';') {
5792 /* we accept no variable after */
5793 next();
5794 continue;
5796 while (1) { /* iterate thru each declaration */
5797 char *asm_label; // associated asm label
5798 type = btype;
5799 type_decl(&type, &ad, &v, TYPE_DIRECT);
5800 #if 0
5802 char buf[500];
5803 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5804 printf("type = '%s'\n", buf);
5806 #endif
5807 if ((type.t & VT_BTYPE) == VT_FUNC) {
5808 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5809 tcc_error("function without file scope cannot be static");
5811 /* if old style function prototype, we accept a
5812 declaration list */
5813 sym = type.ref;
5814 if (sym->c == FUNC_OLD)
5815 func_decl_list(sym);
5818 asm_label = NULL;
5819 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5820 CString astr;
5822 asm_label_instr(&astr);
5823 asm_label = tcc_strdup(astr.data);
5824 cstr_free(&astr);
5826 /* parse one last attribute list, after asm label */
5827 parse_attribute(&ad);
5830 if (ad.weak)
5831 type.t |= VT_WEAK;
5832 #ifdef TCC_TARGET_PE
5833 if (ad.func_import)
5834 type.t |= VT_IMPORT;
5835 if (ad.func_export)
5836 type.t |= VT_EXPORT;
5837 #endif
5838 if (tok == '{') {
5839 if (l == VT_LOCAL)
5840 tcc_error("cannot use local functions");
5841 if ((type.t & VT_BTYPE) != VT_FUNC)
5842 expect("function definition");
5844 /* reject abstract declarators in function definition */
5845 sym = type.ref;
5846 while ((sym = sym->next) != NULL)
5847 if (!(sym->v & ~SYM_FIELD))
5848 expect("identifier");
5850 /* XXX: cannot do better now: convert extern line to static inline */
5851 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5852 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5854 sym = sym_find(v);
5855 if (sym) {
5856 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5857 goto func_error1;
5859 r = sym->type.ref->r;
5861 if (!FUNC_PROTO(r))
5862 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5864 /* use func_call from prototype if not defined */
5865 if (FUNC_CALL(r) != FUNC_CDECL
5866 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5867 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5869 /* use export from prototype */
5870 if (FUNC_EXPORT(r))
5871 FUNC_EXPORT(type.ref->r) = 1;
5873 /* use static from prototype */
5874 if (sym->type.t & VT_STATIC)
5875 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5877 if (!is_compatible_types(&sym->type, &type)) {
5878 func_error1:
5879 tcc_error("incompatible types for redefinition of '%s'",
5880 get_tok_str(v, NULL));
5882 FUNC_PROTO(type.ref->r) = 0;
5883 /* if symbol is already defined, then put complete type */
5884 sym->type = type;
5885 } else {
5886 /* put function symbol */
5887 sym = global_identifier_push(v, type.t, 0);
5888 sym->type.ref = type.ref;
5891 /* static inline functions are just recorded as a kind
5892 of macro. Their code will be emitted at the end of
5893 the compilation unit only if they are used */
5894 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5895 (VT_INLINE | VT_STATIC)) {
5896 TokenString func_str;
5897 int block_level;
5898 struct InlineFunc *fn;
5899 const char *filename;
5901 tok_str_new(&func_str);
5903 block_level = 0;
5904 for(;;) {
5905 int t;
5906 if (tok == TOK_EOF)
5907 tcc_error("unexpected end of file");
5908 tok_str_add_tok(&func_str);
5909 t = tok;
5910 next();
5911 if (t == '{') {
5912 block_level++;
5913 } else if (t == '}') {
5914 block_level--;
5915 if (block_level == 0)
5916 break;
5919 tok_str_add(&func_str, -1);
5920 tok_str_add(&func_str, 0);
5921 filename = file ? file->filename : "";
5922 fn = tcc_malloc(sizeof *fn + strlen(filename));
5923 strcpy(fn->filename, filename);
5924 fn->sym = sym;
5925 fn->token_str = func_str.str;
5926 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5928 } else {
5929 /* compute text section */
5930 cur_text_section = ad.section;
5931 if (!cur_text_section)
5932 cur_text_section = text_section;
5933 sym->r = VT_SYM | VT_CONST;
5934 gen_function(sym);
5936 break;
5937 } else {
5938 if (btype.t & VT_TYPEDEF) {
5939 /* save typedefed type */
5940 /* XXX: test storage specifiers ? */
5941 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5942 sym->type.t |= VT_TYPEDEF;
5943 } else {
5944 r = 0;
5945 if ((type.t & VT_BTYPE) == VT_FUNC) {
5946 /* external function definition */
5947 /* specific case for func_call attribute */
5948 ad.func_proto = 1;
5949 type.ref->r = INT_ATTR(&ad);
5950 } else if (!(type.t & VT_ARRAY)) {
5951 /* not lvalue if array */
5952 r |= lvalue_type(type.t);
5954 has_init = (tok == '=');
5955 if (has_init && (type.t & VT_VLA))
5956 tcc_error("Variable length array cannot be initialized");
5957 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5958 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5959 !has_init && l == VT_CONST && type.ref->c < 0)) {
5960 /* external variable or function */
5961 /* NOTE: as GCC, uninitialized global static
5962 arrays of null size are considered as
5963 extern */
5964 sym = external_sym(v, &type, r, asm_label);
5966 if (type.t & VT_WEAK)
5967 weaken_symbol(sym);
5969 if (ad.alias_target) {
5970 Section tsec;
5971 Elf32_Sym *esym;
5972 Sym *alias_target;
5974 alias_target = sym_find(ad.alias_target);
5975 if (!alias_target || !alias_target->c)
5976 tcc_error("unsupported forward __alias__ attribute");
5977 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5978 tsec.sh_num = esym->st_shndx;
5979 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5981 } else {
5982 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5983 if (type.t & VT_STATIC)
5984 r |= VT_CONST;
5985 else
5986 r |= l;
5987 if (has_init)
5988 next();
5989 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5992 if (tok != ',') {
5993 if (is_for_loop_init)
5994 return 1;
5995 skip(';');
5996 break;
5998 next();
6000 ad.aligned = 0;
6003 return 0;
6006 ST_FUNC void decl(int l)
6008 decl0(l, 0);