Update elf.h
[tinycc.git] / tccgen.c
blob7a675cc0dcd02df036bf269e98ab4ed6a64a9ccf
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
31 ST_DATA int rsym, anon_sym, ind, loc;
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *scope_stack_bottom;
54 ST_DATA Sym *define_stack;
55 ST_DATA Sym *global_label_stack;
56 ST_DATA Sym *local_label_stack;
58 ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA int vla_flags; /* VLA_* flags */
63 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
65 ST_DATA int const_wanted; /* true if constant wanted */
66 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
67 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
69 ST_DATA int func_var; /* true if current function is variadic (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[4];
107 memcpy(p, &d, sizeof(double));
108 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
111 ST_FUNC void test_lvalue(void)
113 if (!(vtop->r & VT_LVAL))
114 expect("lvalue");
117 /* ------------------------------------------------------------------------- */
118 /* symbol allocator */
119 static Sym *__sym_malloc(void)
121 Sym *sym_pool, *sym, *last_sym;
122 int i;
124 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
125 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
127 last_sym = sym_free_first;
128 sym = sym_pool;
129 for(i = 0; i < SYM_POOL_NB; i++) {
130 sym->next = last_sym;
131 last_sym = sym;
132 sym++;
134 sym_free_first = last_sym;
135 return last_sym;
138 static inline Sym *sym_malloc(void)
140 Sym *sym;
141 sym = sym_free_first;
142 if (!sym)
143 sym = __sym_malloc();
144 sym_free_first = sym->next;
145 return sym;
148 ST_INLN void sym_free(Sym *sym)
150 sym->next = sym_free_first;
151 tcc_free(sym->asm_label);
152 sym_free_first = sym;
155 /* push, without hashing */
156 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
158 Sym *s;
159 if (ps == &local_stack) {
160 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
161 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
162 tcc_error("incompatible types for redefinition of '%s'",
163 get_tok_str(v, NULL));
165 s = *ps;
166 s = sym_malloc();
167 s->asm_label = NULL;
168 s->v = v;
169 s->type.t = t;
170 s->type.ref = NULL;
171 #ifdef _WIN64
172 s->d = NULL;
173 #endif
174 s->c = c;
175 s->next = NULL;
176 /* add in stack */
177 s->prev = *ps;
178 *ps = s;
179 return s;
182 /* find a symbol and return its associated structure. 's' is the top
183 of the symbol stack */
184 ST_FUNC Sym *sym_find2(Sym *s, int v)
186 while (s) {
187 if (s->v == v)
188 return s;
189 s = s->prev;
191 return NULL;
194 /* structure lookup */
195 ST_INLN Sym *struct_find(int v)
197 v -= TOK_IDENT;
198 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
199 return NULL;
200 return table_ident[v]->sym_struct;
203 /* find an identifier */
204 ST_INLN Sym *sym_find(int v)
206 v -= TOK_IDENT;
207 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
208 return NULL;
209 return table_ident[v]->sym_identifier;
212 /* push a given symbol on the symbol stack */
213 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
215 Sym *s, **ps;
216 TokenSym *ts;
218 if (local_stack)
219 ps = &local_stack;
220 else
221 ps = &global_stack;
222 s = sym_push2(ps, v, type->t, c);
223 s->type.ref = type->ref;
224 s->r = r;
225 /* don't record fields or anonymous symbols */
226 /* XXX: simplify */
227 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
228 /* record symbol in token array */
229 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
230 if (v & SYM_STRUCT)
231 ps = &ts->sym_struct;
232 else
233 ps = &ts->sym_identifier;
234 s->prev_tok = *ps;
235 *ps = s;
237 return s;
240 /* push a global identifier */
241 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
243 Sym *s, **ps;
244 s = sym_push2(&global_stack, v, t, c);
245 /* don't record anonymous symbol */
246 if (v < SYM_FIRST_ANOM) {
247 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
248 /* modify the top most local identifier, so that
249 sym_identifier will point to 's' when popped */
250 while (*ps != NULL)
251 ps = &(*ps)->prev_tok;
252 s->prev_tok = NULL;
253 *ps = s;
255 return s;
258 /* pop symbols until top reaches 'b' */
259 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
261 Sym *s, *ss, **ps;
262 TokenSym *ts;
263 int v;
265 s = *ptop;
266 while(s != b) {
267 ss = s->prev;
268 v = s->v;
269 /* remove symbol in token array */
270 /* XXX: simplify */
271 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
272 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
273 if (v & SYM_STRUCT)
274 ps = &ts->sym_struct;
275 else
276 ps = &ts->sym_identifier;
277 *ps = s->prev_tok;
279 sym_free(s);
280 s = ss;
282 *ptop = b;
285 static void weaken_symbol(Sym *sym)
287 sym->type.t |= VT_WEAK;
288 if (sym->c > 0) {
289 int esym_type;
290 ElfW(Sym) *esym;
292 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
293 esym_type = ELFW(ST_TYPE)(esym->st_info);
294 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
298 /* ------------------------------------------------------------------------- */
300 ST_FUNC void swap(int *p, int *q)
302 int t;
303 t = *p;
304 *p = *q;
305 *q = t;
308 static void vsetc(CType *type, int r, CValue *vc)
310 int v;
312 if (vtop >= vstack + (VSTACK_SIZE - 1))
313 tcc_error("memory full (vstack)");
314 /* cannot let cpu flags if other instruction are generated. Also
315 avoid leaving VT_JMP anywhere except on the top of the stack
316 because it would complicate the code generator. */
317 if (vtop >= vstack) {
318 v = vtop->r & VT_VALMASK;
319 if (v == VT_CMP || (v & ~1) == VT_JMP)
320 gv(RC_INT);
322 vtop++;
323 vtop->type = *type;
324 vtop->r = r;
325 vtop->r2 = VT_CONST;
326 vtop->c = *vc;
329 /* push constant of type "type" with useless value */
330 void vpush(CType *type)
332 CValue cval;
333 vsetc(type, VT_CONST, &cval);
336 /* push integer constant */
337 ST_FUNC void vpushi(int v)
339 CValue cval;
340 cval.i = v;
341 vsetc(&int_type, VT_CONST, &cval);
344 /* push a pointer sized constant */
345 static void vpushs(long long v)
347 CValue cval;
348 if (PTR_SIZE == 4)
349 cval.i = (int)v;
350 else
351 cval.ull = v;
352 vsetc(&size_type, VT_CONST, &cval);
355 /* push arbitrary 64bit constant */
356 void vpush64(int ty, unsigned long long v)
358 CValue cval;
359 CType ctype;
360 ctype.t = ty;
361 ctype.ref = NULL;
362 cval.ull = v;
363 vsetc(&ctype, VT_CONST, &cval);
366 /* push long long constant */
367 static inline void vpushll(long long v)
369 vpush64(VT_LLONG, v);
372 /* Return a static symbol pointing to a section */
373 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
375 int v;
376 Sym *sym;
378 v = anon_sym++;
379 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
380 sym->type.ref = type->ref;
381 sym->r = VT_CONST | VT_SYM;
382 put_extern_sym(sym, sec, offset, size);
383 return sym;
386 /* push a reference to a section offset by adding a dummy symbol */
387 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
389 CValue cval;
391 cval.ul = 0;
392 vsetc(type, VT_CONST | VT_SYM, &cval);
393 vtop->sym = get_sym_ref(type, sec, offset, size);
396 /* define a new external reference to a symbol 'v' of type 'u' */
397 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
399 Sym *s;
401 s = sym_find(v);
402 if (!s) {
403 /* push forward reference */
404 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
405 s->type.ref = type->ref;
406 s->r = r | VT_CONST | VT_SYM;
408 return s;
411 /* define a new external reference to a symbol 'v' with alternate asm
412 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
413 is no alternate name (most cases) */
414 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
416 Sym *s;
418 s = sym_find(v);
419 if (!s) {
420 /* push forward reference */
421 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
422 s->asm_label = asm_label;
423 s->type.t |= VT_EXTERN;
424 } else if (s->type.ref == func_old_type.ref) {
425 s->type.ref = type->ref;
426 s->r = r | VT_CONST | VT_SYM;
427 s->type.t |= VT_EXTERN;
428 } else if (!is_compatible_types(&s->type, type)) {
429 tcc_error("incompatible types for redefinition of '%s'",
430 get_tok_str(v, NULL));
432 return s;
435 /* push a reference to global symbol v */
436 ST_FUNC void vpush_global_sym(CType *type, int v)
438 Sym *sym;
439 CValue cval;
441 sym = external_global_sym(v, type, 0);
442 cval.ul = 0;
443 vsetc(type, VT_CONST | VT_SYM, &cval);
444 vtop->sym = sym;
447 ST_FUNC void vset(CType *type, int r, int v)
449 CValue cval;
451 cval.i = v;
452 vsetc(type, r, &cval);
455 static void vseti(int r, int v)
457 CType type;
458 type.t = VT_INT;
459 type.ref = 0;
460 vset(&type, r, v);
463 ST_FUNC void vswap(void)
465 SValue tmp;
466 /* cannot let cpu flags if other instruction are generated. Also
467 avoid leaving VT_JMP anywhere except on the top of the stack
468 because it would complicate the code generator. */
469 if (vtop >= vstack) {
470 int v = vtop->r & VT_VALMASK;
471 if (v == VT_CMP || (v & ~1) == VT_JMP)
472 gv(RC_INT);
474 tmp = vtop[0];
475 vtop[0] = vtop[-1];
476 vtop[-1] = tmp;
478 /* XXX: +2% overall speed possible with optimized memswap
480 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
484 ST_FUNC void vpushv(SValue *v)
486 if (vtop >= vstack + (VSTACK_SIZE - 1))
487 tcc_error("memory full (vstack)");
488 vtop++;
489 *vtop = *v;
492 static void vdup(void)
494 vpushv(vtop);
497 /* save r to the memory stack, and mark it as being free */
498 ST_FUNC void save_reg(int r)
500 int l, saved, size, align;
501 SValue *p, sv;
502 CType *type;
504 /* modify all stack values */
505 saved = 0;
506 l = 0;
507 for(p=vstack;p<=vtop;p++) {
508 if ((p->r & VT_VALMASK) == r ||
509 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
510 /* must save value on stack if not already done */
511 if (!saved) {
512 /* NOTE: must reload 'r' because r might be equal to r2 */
513 r = p->r & VT_VALMASK;
514 /* store register in the stack */
515 type = &p->type;
516 if ((p->r & VT_LVAL) ||
517 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
518 #ifdef TCC_TARGET_X86_64
519 type = &char_pointer_type;
520 #else
521 type = &int_type;
522 #endif
523 size = type_size(type, &align);
524 loc = (loc - size) & -align;
525 sv.type.t = type->t;
526 sv.r = VT_LOCAL | VT_LVAL;
527 sv.c.ul = loc;
528 store(r, &sv);
529 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
530 /* x86 specific: need to pop fp register ST0 if saved */
531 if (r == TREG_ST0) {
532 o(0xd8dd); /* fstp %st(0) */
534 #endif
535 #ifndef TCC_TARGET_X86_64
536 /* special long long case */
537 if ((type->t & VT_BTYPE) == VT_LLONG) {
538 sv.c.ul += 4;
539 store(p->r2, &sv);
541 #endif
542 l = loc;
543 saved = 1;
545 /* mark that stack entry as being saved on the stack */
546 if (p->r & VT_LVAL) {
547 /* also clear the bounded flag because the
548 relocation address of the function was stored in
549 p->c.ul */
550 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
551 } else {
552 p->r = lvalue_type(p->type.t) | VT_LOCAL;
554 p->r2 = VT_CONST;
555 p->c.ul = l;
560 #ifdef TCC_TARGET_ARM
561 /* find a register of class 'rc2' with at most one reference on stack.
562 * If none, call get_reg(rc) */
563 ST_FUNC int get_reg_ex(int rc, int rc2)
565 int r;
566 SValue *p;
568 for(r=0;r<NB_REGS;r++) {
569 if (reg_classes[r] & rc2) {
570 int n;
571 n=0;
572 for(p = vstack; p <= vtop; p++) {
573 if ((p->r & VT_VALMASK) == r ||
574 (p->r2 & VT_VALMASK) == r)
575 n++;
577 if (n <= 1)
578 return r;
581 return get_reg(rc);
583 #endif
585 /* find a free register of class 'rc'. If none, save one register */
586 ST_FUNC int get_reg(int rc)
588 int r;
589 SValue *p;
591 /* find a free register */
592 for(r=0;r<NB_REGS;r++) {
593 if (reg_classes[r] & rc) {
594 for(p=vstack;p<=vtop;p++) {
595 if ((p->r & VT_VALMASK) == r ||
596 (p->r2 & VT_VALMASK) == r)
597 goto notfound;
599 return r;
601 notfound: ;
604 /* no register left : free the first one on the stack (VERY
605 IMPORTANT to start from the bottom to ensure that we don't
606 spill registers used in gen_opi()) */
607 for(p=vstack;p<=vtop;p++) {
608 /* look at second register (if long long) */
609 r = p->r2 & VT_VALMASK;
610 if (r < VT_CONST && (reg_classes[r] & rc))
611 goto save_found;
612 r = p->r & VT_VALMASK;
613 if (r < VT_CONST && (reg_classes[r] & rc)) {
614 save_found:
615 save_reg(r);
616 return r;
619 /* Should never comes here */
620 return -1;
623 /* save registers up to (vtop - n) stack entry */
624 ST_FUNC void save_regs(int n)
626 int r;
627 SValue *p, *p1;
628 p1 = vtop - n;
629 for(p = vstack;p <= p1; p++) {
630 r = p->r & VT_VALMASK;
631 if (r < VT_CONST) {
632 save_reg(r);
637 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
638 if needed */
639 static void move_reg(int r, int s, int t)
641 SValue sv;
643 if (r != s) {
644 save_reg(r);
645 sv.type.t = t;
646 sv.type.ref = NULL;
647 sv.r = s;
648 sv.c.ul = 0;
649 load(r, &sv);
653 /* get address of vtop (vtop MUST BE an lvalue) */
654 static void gaddrof(void)
656 if (vtop->r & VT_REF)
657 gv(RC_INT);
658 vtop->r &= ~VT_LVAL;
659 /* tricky: if saved lvalue, then we can go back to lvalue */
660 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
661 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
666 #ifdef CONFIG_TCC_BCHECK
667 /* generate lvalue bound code */
668 static void gbound(void)
670 int lval_type;
671 CType type1;
673 vtop->r &= ~VT_MUSTBOUND;
674 /* if lvalue, then use checking code before dereferencing */
675 if (vtop->r & VT_LVAL) {
676 /* if not VT_BOUNDED value, then make one */
677 if (!(vtop->r & VT_BOUNDED)) {
678 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
679 /* must save type because we must set it to int to get pointer */
680 type1 = vtop->type;
681 vtop->type.t = VT_INT;
682 gaddrof();
683 vpushi(0);
684 gen_bounded_ptr_add();
685 vtop->r |= lval_type;
686 vtop->type = type1;
688 /* then check for dereferencing */
689 gen_bounded_ptr_deref();
692 #endif
694 /* store vtop a register belonging to class 'rc'. lvalues are
695 converted to values. Cannot be used if cannot be converted to
696 register value (such as structures). */
697 ST_FUNC int gv(int rc)
699 int r, bit_pos, bit_size, size, align, i;
700 int rc2;
702 /* NOTE: get_reg can modify vstack[] */
703 if (vtop->type.t & VT_BITFIELD) {
704 CType type;
705 int bits = 32;
706 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
707 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
708 /* remove bit field info to avoid loops */
709 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
710 /* cast to int to propagate signedness in following ops */
711 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
712 type.t = VT_LLONG;
713 bits = 64;
714 } else
715 type.t = VT_INT;
716 if((vtop->type.t & VT_UNSIGNED) ||
717 (vtop->type.t & VT_BTYPE) == VT_BOOL)
718 type.t |= VT_UNSIGNED;
719 gen_cast(&type);
720 /* generate shifts */
721 vpushi(bits - (bit_pos + bit_size));
722 gen_op(TOK_SHL);
723 vpushi(bits - bit_size);
724 /* NOTE: transformed to SHR if unsigned */
725 gen_op(TOK_SAR);
726 r = gv(rc);
727 } else {
728 if (is_float(vtop->type.t) &&
729 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
730 Sym *sym;
731 int *ptr;
732 unsigned long offset;
733 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
734 CValue check;
735 #endif
737 /* XXX: unify with initializers handling ? */
738 /* CPUs usually cannot use float constants, so we store them
739 generically in data segment */
740 size = type_size(&vtop->type, &align);
741 offset = (data_section->data_offset + align - 1) & -align;
742 data_section->data_offset = offset;
743 /* XXX: not portable yet */
744 #if defined(__i386__) || defined(__x86_64__)
745 /* Zero pad x87 tenbyte long doubles */
746 if (size == LDOUBLE_SIZE) {
747 vtop->c.tab[2] &= 0xffff;
748 #if LDOUBLE_SIZE == 16
749 vtop->c.tab[3] = 0;
750 #endif
752 #endif
753 ptr = section_ptr_add(data_section, size);
754 size = size >> 2;
755 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
756 check.d = 1;
757 if(check.tab[0])
758 for(i=0;i<size;i++)
759 ptr[i] = vtop->c.tab[size-1-i];
760 else
761 #endif
762 for(i=0;i<size;i++)
763 ptr[i] = vtop->c.tab[i];
764 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
765 vtop->r |= VT_LVAL | VT_SYM;
766 vtop->sym = sym;
767 vtop->c.ul = 0;
769 #ifdef CONFIG_TCC_BCHECK
770 if (vtop->r & VT_MUSTBOUND)
771 gbound();
772 #endif
774 r = vtop->r & VT_VALMASK;
775 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
776 if (rc == RC_IRET)
777 rc2 = RC_LRET;
778 #ifdef TCC_TARGET_X86_64
779 else if (rc == RC_FRET)
780 rc2 = RC_QRET;
781 #endif
783 /* need to reload if:
784 - constant
785 - lvalue (need to dereference pointer)
786 - already a register, but not in the right class */
787 if (r >= VT_CONST
788 || (vtop->r & VT_LVAL)
789 || !(reg_classes[r] & rc)
790 #ifdef TCC_TARGET_X86_64
791 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
792 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
793 #else
794 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
795 #endif
798 r = get_reg(rc);
799 #ifdef TCC_TARGET_X86_64
800 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
801 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
802 #else
803 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
804 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
805 unsigned long long ll;
806 #endif
807 int r2, original_type;
808 original_type = vtop->type.t;
809 /* two register type load : expand to two words
810 temporarily */
811 #ifndef TCC_TARGET_X86_64
812 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
813 /* load constant */
814 ll = vtop->c.ull;
815 vtop->c.ui = ll; /* first word */
816 load(r, vtop);
817 vtop->r = r; /* save register value */
818 vpushi(ll >> 32); /* second word */
819 } else
820 #endif
821 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
822 (vtop->r & VT_LVAL)) {
823 /* We do not want to modifier the long long
824 pointer here, so the safest (and less
825 efficient) is to save all the other registers
826 in the stack. XXX: totally inefficient. */
827 save_regs(1);
828 /* load from memory */
829 vtop->type.t = load_type;
830 load(r, vtop);
831 vdup();
832 vtop[-1].r = r; /* save register value */
833 /* increment pointer to get second word */
834 vtop->type.t = addr_type;
835 gaddrof();
836 vpushi(load_size);
837 gen_op('+');
838 vtop->r |= VT_LVAL;
839 vtop->type.t = load_type;
840 } else {
841 /* move registers */
842 load(r, vtop);
843 vdup();
844 vtop[-1].r = r; /* save register value */
845 vtop->r = vtop[-1].r2;
847 /* Allocate second register. Here we rely on the fact that
848 get_reg() tries first to free r2 of an SValue. */
849 r2 = get_reg(rc2);
850 load(r2, vtop);
851 vpop();
852 /* write second register */
853 vtop->r2 = r2;
854 vtop->type.t = original_type;
855 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
856 int t1, t;
857 /* lvalue of scalar type : need to use lvalue type
858 because of possible cast */
859 t = vtop->type.t;
860 t1 = t;
861 /* compute memory access type */
862 if (vtop->r & VT_REF)
863 #ifdef TCC_TARGET_X86_64
864 t = VT_PTR;
865 #else
866 t = VT_INT;
867 #endif
868 else if (vtop->r & VT_LVAL_BYTE)
869 t = VT_BYTE;
870 else if (vtop->r & VT_LVAL_SHORT)
871 t = VT_SHORT;
872 if (vtop->r & VT_LVAL_UNSIGNED)
873 t |= VT_UNSIGNED;
874 vtop->type.t = t;
875 load(r, vtop);
876 /* restore wanted type */
877 vtop->type.t = t1;
878 } else {
879 /* one register type load */
880 load(r, vtop);
883 vtop->r = r;
884 #ifdef TCC_TARGET_C67
885 /* uses register pairs for doubles */
886 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
887 vtop->r2 = r+1;
888 #endif
890 return r;
893 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
894 ST_FUNC void gv2(int rc1, int rc2)
896 int v;
898 /* generate more generic register first. But VT_JMP or VT_CMP
899 values must be generated first in all cases to avoid possible
900 reload errors */
901 v = vtop[0].r & VT_VALMASK;
902 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
903 vswap();
904 gv(rc1);
905 vswap();
906 gv(rc2);
907 /* test if reload is needed for first register */
908 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
909 vswap();
910 gv(rc1);
911 vswap();
913 } else {
914 gv(rc2);
915 vswap();
916 gv(rc1);
917 vswap();
918 /* test if reload is needed for first register */
919 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
920 gv(rc2);
925 /* wrapper around RC_FRET to return a register by type */
926 static int rc_fret(int t)
928 #ifdef TCC_TARGET_X86_64
929 if (t == VT_LDOUBLE) {
930 return RC_ST0;
932 #endif
933 return RC_FRET;
936 /* wrapper around REG_FRET to return a register by type */
937 static int reg_fret(int t)
939 #ifdef TCC_TARGET_X86_64
940 if (t == VT_LDOUBLE) {
941 return TREG_ST0;
943 #endif
944 return REG_FRET;
947 /* expand long long on stack in two int registers */
948 static void lexpand(void)
950 int u;
952 u = vtop->type.t & VT_UNSIGNED;
953 gv(RC_INT);
954 vdup();
955 vtop[0].r = vtop[-1].r2;
956 vtop[0].r2 = VT_CONST;
957 vtop[-1].r2 = VT_CONST;
958 vtop[0].type.t = VT_INT | u;
959 vtop[-1].type.t = VT_INT | u;
962 #ifdef TCC_TARGET_ARM
963 /* expand long long on stack */
964 ST_FUNC void lexpand_nr(void)
966 int u,v;
968 u = vtop->type.t & VT_UNSIGNED;
969 vdup();
970 vtop->r2 = VT_CONST;
971 vtop->type.t = VT_INT | u;
972 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
973 if (v == VT_CONST) {
974 vtop[-1].c.ui = vtop->c.ull;
975 vtop->c.ui = vtop->c.ull >> 32;
976 vtop->r = VT_CONST;
977 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
978 vtop->c.ui += 4;
979 vtop->r = vtop[-1].r;
980 } else if (v > VT_CONST) {
981 vtop--;
982 lexpand();
983 } else
984 vtop->r = vtop[-1].r2;
985 vtop[-1].r2 = VT_CONST;
986 vtop[-1].type.t = VT_INT | u;
988 #endif
990 /* build a long long from two ints */
991 static void lbuild(int t)
993 gv2(RC_INT, RC_INT);
994 vtop[-1].r2 = vtop[0].r;
995 vtop[-1].type.t = t;
996 vpop();
999 /* rotate n first stack elements to the bottom
1000 I1 ... In -> I2 ... In I1 [top is right]
1002 ST_FUNC void vrotb(int n)
1004 int i;
1005 SValue tmp;
1007 tmp = vtop[-n + 1];
1008 for(i=-n+1;i!=0;i++)
1009 vtop[i] = vtop[i+1];
1010 vtop[0] = tmp;
1013 /* rotate the n elements before entry e towards the top
1014 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1016 ST_FUNC void vrote(SValue *e, int n)
1018 int i;
1019 SValue tmp;
1021 tmp = *e;
1022 for(i = 0;i < n - 1; i++)
1023 e[-i] = e[-i - 1];
1024 e[-n + 1] = tmp;
1027 /* rotate n first stack elements to the top
1028 I1 ... In -> In I1 ... I(n-1) [top is right]
1030 ST_FUNC void vrott(int n)
1032 vrote(vtop, n);
1035 /* pop stack value */
1036 ST_FUNC void vpop(void)
1038 int v;
1039 v = vtop->r & VT_VALMASK;
1040 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1041 /* for x86, we need to pop the FP stack */
1042 if (v == TREG_ST0 && !nocode_wanted) {
1043 o(0xd8dd); /* fstp %st(0) */
1044 } else
1045 #endif
1046 if (v == VT_JMP || v == VT_JMPI) {
1047 /* need to put correct jump if && or || without test */
1048 gsym(vtop->c.ul);
1050 vtop--;
1053 /* convert stack entry to register and duplicate its value in another
1054 register */
1055 static void gv_dup(void)
1057 int rc, t, r, r1;
1058 SValue sv;
1060 t = vtop->type.t;
1061 if ((t & VT_BTYPE) == VT_LLONG) {
1062 lexpand();
1063 gv_dup();
1064 vswap();
1065 vrotb(3);
1066 gv_dup();
1067 vrotb(4);
1068 /* stack: H L L1 H1 */
1069 lbuild(t);
1070 vrotb(3);
1071 vrotb(3);
1072 vswap();
1073 lbuild(t);
1074 vswap();
1075 } else {
1076 /* duplicate value */
1077 rc = RC_INT;
1078 sv.type.t = VT_INT;
1079 if (is_float(t)) {
1080 rc = RC_FLOAT;
1081 #ifdef TCC_TARGET_X86_64
1082 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1083 rc = RC_ST0;
1085 #endif
1086 sv.type.t = t;
1088 r = gv(rc);
1089 r1 = get_reg(rc);
1090 sv.r = r;
1091 sv.c.ul = 0;
1092 load(r1, &sv); /* move r to r1 */
1093 vdup();
1094 /* duplicates value */
1095 if (r != r1)
1096 vtop->r = r1;
1100 /* Generate value test
1102 * Generate a test for any value (jump, comparison and integers) */
1103 int gvtst(int inv, int t)
1105 int v = vtop->r & VT_VALMASK;
1106 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1107 vpushi(0);
1108 gen_op(TOK_NE);
1110 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1111 /* constant jmp optimization */
1112 if ((vtop->c.i != 0) != inv)
1113 t = gjmp(t);
1114 vtop--;
1115 return t;
1117 return gtst(inv, t);
1120 #ifndef TCC_TARGET_X86_64
1121 /* generate CPU independent (unsigned) long long operations */
1122 static void gen_opl(int op)
1124 int t, a, b, op1, c, i;
1125 int func;
1126 unsigned short reg_iret = REG_IRET;
1127 unsigned short reg_lret = REG_LRET;
1128 SValue tmp;
1130 switch(op) {
1131 case '/':
1132 case TOK_PDIV:
1133 func = TOK___divdi3;
1134 goto gen_func;
1135 case TOK_UDIV:
1136 func = TOK___udivdi3;
1137 goto gen_func;
1138 case '%':
1139 func = TOK___moddi3;
1140 goto gen_mod_func;
1141 case TOK_UMOD:
1142 func = TOK___umoddi3;
1143 gen_mod_func:
1144 #ifdef TCC_ARM_EABI
1145 reg_iret = TREG_R2;
1146 reg_lret = TREG_R3;
1147 #endif
1148 gen_func:
1149 /* call generic long long function */
1150 vpush_global_sym(&func_old_type, func);
1151 vrott(3);
1152 gfunc_call(2);
1153 vpushi(0);
1154 vtop->r = reg_iret;
1155 vtop->r2 = reg_lret;
1156 break;
1157 case '^':
1158 case '&':
1159 case '|':
1160 case '*':
1161 case '+':
1162 case '-':
1163 t = vtop->type.t;
1164 vswap();
1165 lexpand();
1166 vrotb(3);
1167 lexpand();
1168 /* stack: L1 H1 L2 H2 */
1169 tmp = vtop[0];
1170 vtop[0] = vtop[-3];
1171 vtop[-3] = tmp;
1172 tmp = vtop[-2];
1173 vtop[-2] = vtop[-3];
1174 vtop[-3] = tmp;
1175 vswap();
1176 /* stack: H1 H2 L1 L2 */
1177 if (op == '*') {
1178 vpushv(vtop - 1);
1179 vpushv(vtop - 1);
1180 gen_op(TOK_UMULL);
1181 lexpand();
1182 /* stack: H1 H2 L1 L2 ML MH */
1183 for(i=0;i<4;i++)
1184 vrotb(6);
1185 /* stack: ML MH H1 H2 L1 L2 */
1186 tmp = vtop[0];
1187 vtop[0] = vtop[-2];
1188 vtop[-2] = tmp;
1189 /* stack: ML MH H1 L2 H2 L1 */
1190 gen_op('*');
1191 vrotb(3);
1192 vrotb(3);
1193 gen_op('*');
1194 /* stack: ML MH M1 M2 */
1195 gen_op('+');
1196 gen_op('+');
1197 } else if (op == '+' || op == '-') {
1198 /* XXX: add non carry method too (for MIPS or alpha) */
1199 if (op == '+')
1200 op1 = TOK_ADDC1;
1201 else
1202 op1 = TOK_SUBC1;
1203 gen_op(op1);
1204 /* stack: H1 H2 (L1 op L2) */
1205 vrotb(3);
1206 vrotb(3);
1207 gen_op(op1 + 1); /* TOK_xxxC2 */
1208 } else {
1209 gen_op(op);
1210 /* stack: H1 H2 (L1 op L2) */
1211 vrotb(3);
1212 vrotb(3);
1213 /* stack: (L1 op L2) H1 H2 */
1214 gen_op(op);
1215 /* stack: (L1 op L2) (H1 op H2) */
1217 /* stack: L H */
1218 lbuild(t);
1219 break;
1220 case TOK_SAR:
1221 case TOK_SHR:
1222 case TOK_SHL:
1223 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1224 t = vtop[-1].type.t;
1225 vswap();
1226 lexpand();
1227 vrotb(3);
1228 /* stack: L H shift */
1229 c = (int)vtop->c.i;
1230 /* constant: simpler */
1231 /* NOTE: all comments are for SHL. the other cases are
1232 done by swaping words */
1233 vpop();
1234 if (op != TOK_SHL)
1235 vswap();
1236 if (c >= 32) {
1237 /* stack: L H */
1238 vpop();
1239 if (c > 32) {
1240 vpushi(c - 32);
1241 gen_op(op);
1243 if (op != TOK_SAR) {
1244 vpushi(0);
1245 } else {
1246 gv_dup();
1247 vpushi(31);
1248 gen_op(TOK_SAR);
1250 vswap();
1251 } else {
1252 vswap();
1253 gv_dup();
1254 /* stack: H L L */
1255 vpushi(c);
1256 gen_op(op);
1257 vswap();
1258 vpushi(32 - c);
1259 if (op == TOK_SHL)
1260 gen_op(TOK_SHR);
1261 else
1262 gen_op(TOK_SHL);
1263 vrotb(3);
1264 /* stack: L L H */
1265 vpushi(c);
1266 if (op == TOK_SHL)
1267 gen_op(TOK_SHL);
1268 else
1269 gen_op(TOK_SHR);
1270 gen_op('|');
1272 if (op != TOK_SHL)
1273 vswap();
1274 lbuild(t);
1275 } else {
1276 /* XXX: should provide a faster fallback on x86 ? */
1277 switch(op) {
1278 case TOK_SAR:
1279 func = TOK___ashrdi3;
1280 goto gen_func;
1281 case TOK_SHR:
1282 func = TOK___lshrdi3;
1283 goto gen_func;
1284 case TOK_SHL:
1285 func = TOK___ashldi3;
1286 goto gen_func;
1289 break;
1290 default:
1291 /* compare operations */
1292 t = vtop->type.t;
1293 vswap();
1294 lexpand();
1295 vrotb(3);
1296 lexpand();
1297 /* stack: L1 H1 L2 H2 */
1298 tmp = vtop[-1];
1299 vtop[-1] = vtop[-2];
1300 vtop[-2] = tmp;
1301 /* stack: L1 L2 H1 H2 */
1302 /* compare high */
1303 op1 = op;
1304 /* when values are equal, we need to compare low words. since
1305 the jump is inverted, we invert the test too. */
1306 if (op1 == TOK_LT)
1307 op1 = TOK_LE;
1308 else if (op1 == TOK_GT)
1309 op1 = TOK_GE;
1310 else if (op1 == TOK_ULT)
1311 op1 = TOK_ULE;
1312 else if (op1 == TOK_UGT)
1313 op1 = TOK_UGE;
1314 a = 0;
1315 b = 0;
1316 gen_op(op1);
1317 if (op1 != TOK_NE) {
1318 a = gvtst(1, 0);
1320 if (op != TOK_EQ) {
1321 /* generate non equal test */
1322 /* XXX: NOT PORTABLE yet */
1323 if (a == 0) {
1324 b = gvtst(0, 0);
1325 } else {
1326 #if defined(TCC_TARGET_I386)
1327 b = psym(0x850f, 0);
1328 #elif defined(TCC_TARGET_ARM)
1329 b = ind;
1330 o(0x1A000000 | encbranch(ind, 0, 1));
1331 #elif defined(TCC_TARGET_C67)
1332 tcc_error("not implemented");
1333 #else
1334 #error not supported
1335 #endif
1338 /* compare low. Always unsigned */
1339 op1 = op;
1340 if (op1 == TOK_LT)
1341 op1 = TOK_ULT;
1342 else if (op1 == TOK_LE)
1343 op1 = TOK_ULE;
1344 else if (op1 == TOK_GT)
1345 op1 = TOK_UGT;
1346 else if (op1 == TOK_GE)
1347 op1 = TOK_UGE;
1348 gen_op(op1);
1349 a = gvtst(1, a);
1350 gsym(b);
1351 vseti(VT_JMPI, a);
1352 break;
1355 #endif
1357 /* handle integer constant optimizations and various machine
1358 independent opt */
1359 static void gen_opic(int op)
1361 int c1, c2, t1, t2, n;
1362 SValue *v1, *v2;
1363 long long l1, l2;
1364 typedef unsigned long long U;
1366 v1 = vtop - 1;
1367 v2 = vtop;
1368 t1 = v1->type.t & VT_BTYPE;
1369 t2 = v2->type.t & VT_BTYPE;
1371 if (t1 == VT_LLONG)
1372 l1 = v1->c.ll;
1373 else if (v1->type.t & VT_UNSIGNED)
1374 l1 = v1->c.ui;
1375 else
1376 l1 = v1->c.i;
1378 if (t2 == VT_LLONG)
1379 l2 = v2->c.ll;
1380 else if (v2->type.t & VT_UNSIGNED)
1381 l2 = v2->c.ui;
1382 else
1383 l2 = v2->c.i;
1385 /* currently, we cannot do computations with forward symbols */
1386 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1387 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1388 if (c1 && c2) {
1389 switch(op) {
1390 case '+': l1 += l2; break;
1391 case '-': l1 -= l2; break;
1392 case '&': l1 &= l2; break;
1393 case '^': l1 ^= l2; break;
1394 case '|': l1 |= l2; break;
1395 case '*': l1 *= l2; break;
1397 case TOK_PDIV:
1398 case '/':
1399 case '%':
1400 case TOK_UDIV:
1401 case TOK_UMOD:
1402 /* if division by zero, generate explicit division */
1403 if (l2 == 0) {
1404 if (const_wanted)
1405 tcc_error("division by zero in constant");
1406 goto general_case;
1408 switch(op) {
1409 default: l1 /= l2; break;
1410 case '%': l1 %= l2; break;
1411 case TOK_UDIV: l1 = (U)l1 / l2; break;
1412 case TOK_UMOD: l1 = (U)l1 % l2; break;
1414 break;
1415 case TOK_SHL: l1 <<= l2; break;
1416 case TOK_SHR: l1 = (U)l1 >> l2; break;
1417 case TOK_SAR: l1 >>= l2; break;
1418 /* tests */
1419 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1420 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1421 case TOK_EQ: l1 = l1 == l2; break;
1422 case TOK_NE: l1 = l1 != l2; break;
1423 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1424 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1425 case TOK_LT: l1 = l1 < l2; break;
1426 case TOK_GE: l1 = l1 >= l2; break;
1427 case TOK_LE: l1 = l1 <= l2; break;
1428 case TOK_GT: l1 = l1 > l2; break;
1429 /* logical */
1430 case TOK_LAND: l1 = l1 && l2; break;
1431 case TOK_LOR: l1 = l1 || l2; break;
1432 default:
1433 goto general_case;
1435 v1->c.ll = l1;
1436 vtop--;
1437 } else {
1438 /* if commutative ops, put c2 as constant */
1439 if (c1 && (op == '+' || op == '&' || op == '^' ||
1440 op == '|' || op == '*')) {
1441 vswap();
1442 c2 = c1; //c = c1, c1 = c2, c2 = c;
1443 l2 = l1; //l = l1, l1 = l2, l2 = l;
1445 /* Filter out NOP operations like x*1, x-0, x&-1... */
1446 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1447 op == TOK_PDIV) &&
1448 l2 == 1) ||
1449 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1450 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1451 l2 == 0) ||
1452 (op == '&' &&
1453 l2 == -1))) {
1454 /* nothing to do */
1455 vtop--;
1456 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1457 /* try to use shifts instead of muls or divs */
1458 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1459 n = -1;
1460 while (l2) {
1461 l2 >>= 1;
1462 n++;
1464 vtop->c.ll = n;
1465 if (op == '*')
1466 op = TOK_SHL;
1467 else if (op == TOK_PDIV)
1468 op = TOK_SAR;
1469 else
1470 op = TOK_SHR;
1472 goto general_case;
1473 } else if (c2 && (op == '+' || op == '-') &&
1474 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1475 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1476 /* symbol + constant case */
1477 if (op == '-')
1478 l2 = -l2;
1479 vtop--;
1480 vtop->c.ll += l2;
1481 } else {
1482 general_case:
1483 if (!nocode_wanted) {
1484 /* call low level op generator */
1485 if (t1 == VT_LLONG || t2 == VT_LLONG)
1486 gen_opl(op);
1487 else
1488 gen_opi(op);
1489 } else {
1490 vtop--;
1496 /* generate a floating point operation with constant propagation */
1497 static void gen_opif(int op)
1499 int c1, c2;
1500 SValue *v1, *v2;
1501 long double f1, f2;
1503 v1 = vtop - 1;
1504 v2 = vtop;
1505 /* currently, we cannot do computations with forward symbols */
1506 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1507 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1508 if (c1 && c2) {
1509 if (v1->type.t == VT_FLOAT) {
1510 f1 = v1->c.f;
1511 f2 = v2->c.f;
1512 } else if (v1->type.t == VT_DOUBLE) {
1513 f1 = v1->c.d;
1514 f2 = v2->c.d;
1515 } else {
1516 f1 = v1->c.ld;
1517 f2 = v2->c.ld;
1520 /* NOTE: we only do constant propagation if finite number (not
1521 NaN or infinity) (ANSI spec) */
1522 if (!ieee_finite(f1) || !ieee_finite(f2))
1523 goto general_case;
1525 switch(op) {
1526 case '+': f1 += f2; break;
1527 case '-': f1 -= f2; break;
1528 case '*': f1 *= f2; break;
1529 case '/':
1530 if (f2 == 0.0) {
1531 if (const_wanted)
1532 tcc_error("division by zero in constant");
1533 goto general_case;
1535 f1 /= f2;
1536 break;
1537 /* XXX: also handles tests ? */
1538 default:
1539 goto general_case;
1541 /* XXX: overflow test ? */
1542 if (v1->type.t == VT_FLOAT) {
1543 v1->c.f = f1;
1544 } else if (v1->type.t == VT_DOUBLE) {
1545 v1->c.d = f1;
1546 } else {
1547 v1->c.ld = f1;
1549 vtop--;
1550 } else {
1551 general_case:
1552 if (!nocode_wanted) {
1553 gen_opf(op);
1554 } else {
1555 vtop--;
1560 static int pointed_size(CType *type)
1562 int align;
1563 return type_size(pointed_type(type), &align);
1566 static void vla_runtime_pointed_size(CType *type)
1568 int align;
1569 vla_runtime_type_size(pointed_type(type), &align);
1572 static inline int is_null_pointer(SValue *p)
1574 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1575 return 0;
1576 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1577 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1578 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0);
1581 static inline int is_integer_btype(int bt)
1583 return (bt == VT_BYTE || bt == VT_SHORT ||
1584 bt == VT_INT || bt == VT_LLONG);
1587 /* check types for comparison or substraction of pointers */
1588 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1590 CType *type1, *type2, tmp_type1, tmp_type2;
1591 int bt1, bt2;
1593 /* null pointers are accepted for all comparisons as gcc */
1594 if (is_null_pointer(p1) || is_null_pointer(p2))
1595 return;
1596 type1 = &p1->type;
1597 type2 = &p2->type;
1598 bt1 = type1->t & VT_BTYPE;
1599 bt2 = type2->t & VT_BTYPE;
1600 /* accept comparison between pointer and integer with a warning */
1601 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1602 if (op != TOK_LOR && op != TOK_LAND )
1603 tcc_warning("comparison between pointer and integer");
1604 return;
1607 /* both must be pointers or implicit function pointers */
1608 if (bt1 == VT_PTR) {
1609 type1 = pointed_type(type1);
1610 } else if (bt1 != VT_FUNC)
1611 goto invalid_operands;
1613 if (bt2 == VT_PTR) {
1614 type2 = pointed_type(type2);
1615 } else if (bt2 != VT_FUNC) {
1616 invalid_operands:
1617 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1619 if ((type1->t & VT_BTYPE) == VT_VOID ||
1620 (type2->t & VT_BTYPE) == VT_VOID)
1621 return;
1622 tmp_type1 = *type1;
1623 tmp_type2 = *type2;
1624 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1625 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1626 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1627 /* gcc-like error if '-' is used */
1628 if (op == '-')
1629 goto invalid_operands;
1630 else
1631 tcc_warning("comparison of distinct pointer types lacks a cast");
1635 /* generic gen_op: handles types problems */
1636 ST_FUNC void gen_op(int op)
1638 int u, t1, t2, bt1, bt2, t;
1639 CType type1;
1641 t1 = vtop[-1].type.t;
1642 t2 = vtop[0].type.t;
1643 bt1 = t1 & VT_BTYPE;
1644 bt2 = t2 & VT_BTYPE;
1646 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1647 /* at least one operand is a pointer */
1648 /* relationnal op: must be both pointers */
1649 if (op >= TOK_ULT && op <= TOK_LOR) {
1650 check_comparison_pointer_types(vtop - 1, vtop, op);
1651 /* pointers are handled are unsigned */
1652 #ifdef TCC_TARGET_X86_64
1653 t = VT_LLONG | VT_UNSIGNED;
1654 #else
1655 t = VT_INT | VT_UNSIGNED;
1656 #endif
1657 goto std_op;
1659 /* if both pointers, then it must be the '-' op */
1660 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1661 if (op != '-')
1662 tcc_error("cannot use pointers here");
1663 check_comparison_pointer_types(vtop - 1, vtop, op);
1664 /* XXX: check that types are compatible */
1665 if (vtop[-1].type.t & VT_VLA) {
1666 vla_runtime_pointed_size(&vtop[-1].type);
1667 } else {
1668 vpushi(pointed_size(&vtop[-1].type));
1670 vrott(3);
1671 gen_opic(op);
1672 /* set to integer type */
1673 #ifdef TCC_TARGET_X86_64
1674 vtop->type.t = VT_LLONG;
1675 #else
1676 vtop->type.t = VT_INT;
1677 #endif
1678 vswap();
1679 gen_op(TOK_PDIV);
1680 } else {
1681 /* exactly one pointer : must be '+' or '-'. */
1682 if (op != '-' && op != '+')
1683 tcc_error("cannot use pointers here");
1684 /* Put pointer as first operand */
1685 if (bt2 == VT_PTR) {
1686 vswap();
1687 swap(&t1, &t2);
1689 type1 = vtop[-1].type;
1690 type1.t &= ~VT_ARRAY;
1691 if (vtop[-1].type.t & VT_VLA)
1692 vla_runtime_pointed_size(&vtop[-1].type);
1693 else {
1694 u = pointed_size(&vtop[-1].type);
1695 if (u < 0)
1696 tcc_error("unknown array element size");
1697 #ifdef TCC_TARGET_X86_64
1698 vpushll(u);
1699 #else
1700 /* XXX: cast to int ? (long long case) */
1701 vpushi(u);
1702 #endif
1704 gen_op('*');
1705 #ifdef CONFIG_TCC_BCHECK
1706 /* if evaluating constant expression, no code should be
1707 generated, so no bound check */
1708 if (tcc_state->do_bounds_check && !const_wanted) {
1709 /* if bounded pointers, we generate a special code to
1710 test bounds */
1711 if (op == '-') {
1712 vpushi(0);
1713 vswap();
1714 gen_op('-');
1716 gen_bounded_ptr_add();
1717 } else
1718 #endif
1720 gen_opic(op);
1722 /* put again type if gen_opic() swaped operands */
1723 vtop->type = type1;
1725 } else if (is_float(bt1) || is_float(bt2)) {
1726 /* compute bigger type and do implicit casts */
1727 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1728 t = VT_LDOUBLE;
1729 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1730 t = VT_DOUBLE;
1731 } else {
1732 t = VT_FLOAT;
1734 /* floats can only be used for a few operations */
1735 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1736 (op < TOK_ULT || op > TOK_GT))
1737 tcc_error("invalid operands for binary operation");
1738 goto std_op;
1739 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1740 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1741 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1742 t |= VT_UNSIGNED;
1743 goto std_op;
1744 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1745 /* cast to biggest op */
1746 t = VT_LLONG;
1747 /* convert to unsigned if it does not fit in a long long */
1748 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1749 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1750 t |= VT_UNSIGNED;
1751 goto std_op;
1752 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1753 tcc_error("comparison of struct");
1754 } else {
1755 /* integer operations */
1756 t = VT_INT;
1757 /* convert to unsigned if it does not fit in an integer */
1758 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1759 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1760 t |= VT_UNSIGNED;
1761 std_op:
1762 /* XXX: currently, some unsigned operations are explicit, so
1763 we modify them here */
1764 if (t & VT_UNSIGNED) {
1765 if (op == TOK_SAR)
1766 op = TOK_SHR;
1767 else if (op == '/')
1768 op = TOK_UDIV;
1769 else if (op == '%')
1770 op = TOK_UMOD;
1771 else if (op == TOK_LT)
1772 op = TOK_ULT;
1773 else if (op == TOK_GT)
1774 op = TOK_UGT;
1775 else if (op == TOK_LE)
1776 op = TOK_ULE;
1777 else if (op == TOK_GE)
1778 op = TOK_UGE;
1780 vswap();
1781 type1.t = t;
1782 gen_cast(&type1);
1783 vswap();
1784 /* special case for shifts and long long: we keep the shift as
1785 an integer */
1786 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1787 type1.t = VT_INT;
1788 gen_cast(&type1);
1789 if (is_float(t))
1790 gen_opif(op);
1791 else
1792 gen_opic(op);
1793 if (op >= TOK_ULT && op <= TOK_GT) {
1794 /* relationnal op: the result is an int */
1795 vtop->type.t = VT_INT;
1796 } else {
1797 vtop->type.t = t;
1802 #ifndef TCC_TARGET_ARM
1803 /* generic itof for unsigned long long case */
1804 static void gen_cvt_itof1(int t)
1806 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1807 (VT_LLONG | VT_UNSIGNED)) {
1809 if (t == VT_FLOAT)
1810 vpush_global_sym(&func_old_type, TOK___floatundisf);
1811 #if LDOUBLE_SIZE != 8
1812 else if (t == VT_LDOUBLE)
1813 vpush_global_sym(&func_old_type, TOK___floatundixf);
1814 #endif
1815 else
1816 vpush_global_sym(&func_old_type, TOK___floatundidf);
1817 vrott(2);
1818 gfunc_call(1);
1819 vpushi(0);
1820 vtop->r = reg_fret(t);
1821 } else {
1822 gen_cvt_itof(t);
1825 #endif
1827 /* generic ftoi for unsigned long long case */
1828 static void gen_cvt_ftoi1(int t)
1830 int st;
1832 if (t == (VT_LLONG | VT_UNSIGNED)) {
1833 /* not handled natively */
1834 st = vtop->type.t & VT_BTYPE;
1835 if (st == VT_FLOAT)
1836 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1837 #if LDOUBLE_SIZE != 8
1838 else if (st == VT_LDOUBLE)
1839 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1840 #endif
1841 else
1842 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1843 vrott(2);
1844 gfunc_call(1);
1845 vpushi(0);
1846 vtop->r = REG_IRET;
1847 vtop->r2 = REG_LRET;
1848 } else {
1849 gen_cvt_ftoi(t);
1853 /* force char or short cast */
1854 static void force_charshort_cast(int t)
1856 int bits, dbt;
1857 dbt = t & VT_BTYPE;
1858 /* XXX: add optimization if lvalue : just change type and offset */
1859 if (dbt == VT_BYTE)
1860 bits = 8;
1861 else
1862 bits = 16;
1863 if (t & VT_UNSIGNED) {
1864 vpushi((1 << bits) - 1);
1865 gen_op('&');
1866 } else {
1867 bits = 32 - bits;
1868 vpushi(bits);
1869 gen_op(TOK_SHL);
1870 /* result must be signed or the SAR is converted to an SHL
1871 This was not the case when "t" was a signed short
1872 and the last value on the stack was an unsigned int */
1873 vtop->type.t &= ~VT_UNSIGNED;
1874 vpushi(bits);
1875 gen_op(TOK_SAR);
1879 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1880 static void gen_cast(CType *type)
1882 int sbt, dbt, sf, df, c, p;
1884 /* special delayed cast for char/short */
1885 /* XXX: in some cases (multiple cascaded casts), it may still
1886 be incorrect */
1887 if (vtop->r & VT_MUSTCAST) {
1888 vtop->r &= ~VT_MUSTCAST;
1889 force_charshort_cast(vtop->type.t);
1892 /* bitfields first get cast to ints */
1893 if (vtop->type.t & VT_BITFIELD) {
1894 gv(RC_INT);
1897 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1898 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1900 if (sbt != dbt) {
1901 sf = is_float(sbt);
1902 df = is_float(dbt);
1903 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1904 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1905 if (c) {
1906 /* constant case: we can do it now */
1907 /* XXX: in ISOC, cannot do it if error in convert */
1908 if (sbt == VT_FLOAT)
1909 vtop->c.ld = vtop->c.f;
1910 else if (sbt == VT_DOUBLE)
1911 vtop->c.ld = vtop->c.d;
1913 if (df) {
1914 if ((sbt & VT_BTYPE) == VT_LLONG) {
1915 if (sbt & VT_UNSIGNED)
1916 vtop->c.ld = vtop->c.ull;
1917 else
1918 vtop->c.ld = vtop->c.ll;
1919 } else if(!sf) {
1920 if (sbt & VT_UNSIGNED)
1921 vtop->c.ld = vtop->c.ui;
1922 else
1923 vtop->c.ld = vtop->c.i;
1926 if (dbt == VT_FLOAT)
1927 vtop->c.f = (float)vtop->c.ld;
1928 else if (dbt == VT_DOUBLE)
1929 vtop->c.d = (double)vtop->c.ld;
1930 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1931 vtop->c.ull = (unsigned long long)vtop->c.ld;
1932 } else if (sf && dbt == VT_BOOL) {
1933 vtop->c.i = (vtop->c.ld != 0);
1934 } else {
1935 if(sf)
1936 vtop->c.ll = (long long)vtop->c.ld;
1937 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1938 vtop->c.ll = vtop->c.ull;
1939 else if (sbt & VT_UNSIGNED)
1940 vtop->c.ll = vtop->c.ui;
1941 #ifdef TCC_TARGET_X86_64
1942 else if (sbt == VT_PTR)
1944 #endif
1945 else if (sbt != VT_LLONG)
1946 vtop->c.ll = vtop->c.i;
1948 if (dbt == (VT_LLONG|VT_UNSIGNED))
1949 vtop->c.ull = vtop->c.ll;
1950 else if (dbt == VT_BOOL)
1951 vtop->c.i = (vtop->c.ll != 0);
1952 else if (dbt != VT_LLONG) {
1953 int s = 0;
1954 if ((dbt & VT_BTYPE) == VT_BYTE)
1955 s = 24;
1956 else if ((dbt & VT_BTYPE) == VT_SHORT)
1957 s = 16;
1959 if(dbt & VT_UNSIGNED)
1960 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1961 else
1962 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1965 } else if (p && dbt == VT_BOOL) {
1966 vtop->r = VT_CONST;
1967 vtop->c.i = 1;
1968 } else if (!nocode_wanted) {
1969 /* non constant case: generate code */
1970 if (sf && df) {
1971 /* convert from fp to fp */
1972 gen_cvt_ftof(dbt);
1973 } else if (df) {
1974 /* convert int to fp */
1975 gen_cvt_itof1(dbt);
1976 } else if (sf) {
1977 /* convert fp to int */
1978 if (dbt == VT_BOOL) {
1979 vpushi(0);
1980 gen_op(TOK_NE);
1981 } else {
1982 /* we handle char/short/etc... with generic code */
1983 if (dbt != (VT_INT | VT_UNSIGNED) &&
1984 dbt != (VT_LLONG | VT_UNSIGNED) &&
1985 dbt != VT_LLONG)
1986 dbt = VT_INT;
1987 gen_cvt_ftoi1(dbt);
1988 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1989 /* additional cast for char/short... */
1990 vtop->type.t = dbt;
1991 gen_cast(type);
1994 #ifndef TCC_TARGET_X86_64
1995 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1996 if ((sbt & VT_BTYPE) != VT_LLONG) {
1997 /* scalar to long long */
1998 /* machine independent conversion */
1999 gv(RC_INT);
2000 /* generate high word */
2001 if (sbt == (VT_INT | VT_UNSIGNED)) {
2002 vpushi(0);
2003 gv(RC_INT);
2004 } else {
2005 if (sbt == VT_PTR) {
2006 /* cast from pointer to int before we apply
2007 shift operation, which pointers don't support*/
2008 gen_cast(&int_type);
2010 gv_dup();
2011 vpushi(31);
2012 gen_op(TOK_SAR);
2014 /* patch second register */
2015 vtop[-1].r2 = vtop->r;
2016 vpop();
2018 #else
2019 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2020 (dbt & VT_BTYPE) == VT_PTR ||
2021 (dbt & VT_BTYPE) == VT_FUNC) {
2022 if ((sbt & VT_BTYPE) != VT_LLONG &&
2023 (sbt & VT_BTYPE) != VT_PTR &&
2024 (sbt & VT_BTYPE) != VT_FUNC) {
2025 /* need to convert from 32bit to 64bit */
2026 int r = gv(RC_INT);
2027 if (sbt != (VT_INT | VT_UNSIGNED)) {
2028 /* x86_64 specific: movslq */
2029 o(0x6348);
2030 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2033 #endif
2034 } else if (dbt == VT_BOOL) {
2035 /* scalar to bool */
2036 vpushi(0);
2037 gen_op(TOK_NE);
2038 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2039 (dbt & VT_BTYPE) == VT_SHORT) {
2040 if (sbt == VT_PTR) {
2041 vtop->type.t = VT_INT;
2042 tcc_warning("nonportable conversion from pointer to char/short");
2044 force_charshort_cast(dbt);
2045 } else if ((dbt & VT_BTYPE) == VT_INT) {
2046 /* scalar to int */
2047 if (sbt == VT_LLONG) {
2048 /* from long long: just take low order word */
2049 lexpand();
2050 vpop();
2052 /* if lvalue and single word type, nothing to do because
2053 the lvalue already contains the real type size (see
2054 VT_LVAL_xxx constants) */
2057 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2058 /* if we are casting between pointer types,
2059 we must update the VT_LVAL_xxx size */
2060 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2061 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2063 vtop->type = *type;
2066 /* return type size as known at compile time. Put alignment at 'a' */
2067 ST_FUNC int type_size(CType *type, int *a)
2069 Sym *s;
2070 int bt;
2072 bt = type->t & VT_BTYPE;
2073 if (bt == VT_STRUCT) {
2074 /* struct/union */
2075 s = type->ref;
2076 *a = s->r;
2077 return s->c;
2078 } else if (bt == VT_PTR) {
2079 if (type->t & VT_ARRAY) {
2080 int ts;
2082 s = type->ref;
2083 ts = type_size(&s->type, a);
2085 if (ts < 0 && s->c < 0)
2086 ts = -ts;
2088 return ts * s->c;
2089 } else {
2090 *a = PTR_SIZE;
2091 return PTR_SIZE;
2093 } else if (bt == VT_LDOUBLE) {
2094 *a = LDOUBLE_ALIGN;
2095 return LDOUBLE_SIZE;
2096 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2097 #ifdef TCC_TARGET_I386
2098 #ifdef TCC_TARGET_PE
2099 *a = 8;
2100 #else
2101 *a = 4;
2102 #endif
2103 #elif defined(TCC_TARGET_ARM)
2104 #ifdef TCC_ARM_EABI
2105 *a = 8;
2106 #else
2107 *a = 4;
2108 #endif
2109 #else
2110 *a = 8;
2111 #endif
2112 return 8;
2113 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2114 *a = 4;
2115 return 4;
2116 } else if (bt == VT_SHORT) {
2117 *a = 2;
2118 return 2;
2119 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2120 *a = 8;
2121 return 16;
2122 } else {
2123 /* char, void, function, _Bool */
2124 *a = 1;
2125 return 1;
2129 /* push type size as known at runtime time on top of value stack. Put
2130 alignment at 'a' */
2131 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2133 if (type->t & VT_VLA) {
2134 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2135 } else {
2136 vpushi(type_size(type, a));
2140 static void vla_sp_save(void) {
2141 if (!(vla_flags & VLA_SP_LOC_SET)) {
2142 *vla_sp_loc = (loc -= PTR_SIZE);
2143 vla_flags |= VLA_SP_LOC_SET;
2145 if (!(vla_flags & VLA_SP_SAVED)) {
2146 gen_vla_sp_save(*vla_sp_loc);
2147 vla_flags |= VLA_SP_SAVED;
2151 /* return the pointed type of t */
2152 static inline CType *pointed_type(CType *type)
2154 return &type->ref->type;
2157 /* modify type so that its it is a pointer to type. */
2158 ST_FUNC void mk_pointer(CType *type)
2160 Sym *s;
2161 s = sym_push(SYM_FIELD, type, 0, -1);
2162 type->t = VT_PTR | (type->t & ~VT_TYPE);
2163 type->ref = s;
2166 /* compare function types. OLD functions match any new functions */
2167 static int is_compatible_func(CType *type1, CType *type2)
2169 Sym *s1, *s2;
2171 s1 = type1->ref;
2172 s2 = type2->ref;
2173 if (!is_compatible_types(&s1->type, &s2->type))
2174 return 0;
2175 /* check func_call */
2176 if (s1->a.func_call != s2->a.func_call)
2177 return 0;
2178 /* XXX: not complete */
2179 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2180 return 1;
2181 if (s1->c != s2->c)
2182 return 0;
2183 while (s1 != NULL) {
2184 if (s2 == NULL)
2185 return 0;
2186 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2187 return 0;
2188 s1 = s1->next;
2189 s2 = s2->next;
2191 if (s2)
2192 return 0;
2193 return 1;
2196 /* return true if type1 and type2 are the same. If unqualified is
2197 true, qualifiers on the types are ignored.
2199 - enums are not checked as gcc __builtin_types_compatible_p ()
2201 static int compare_types(CType *type1, CType *type2, int unqualified)
2203 int bt1, t1, t2;
2205 t1 = type1->t & VT_TYPE;
2206 t2 = type2->t & VT_TYPE;
2207 if (unqualified) {
2208 /* strip qualifiers before comparing */
2209 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2210 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2212 /* XXX: bitfields ? */
2213 if (t1 != t2)
2214 return 0;
2215 /* test more complicated cases */
2216 bt1 = t1 & VT_BTYPE;
2217 if (bt1 == VT_PTR) {
2218 type1 = pointed_type(type1);
2219 type2 = pointed_type(type2);
2220 return is_compatible_types(type1, type2);
2221 } else if (bt1 == VT_STRUCT) {
2222 return (type1->ref == type2->ref);
2223 } else if (bt1 == VT_FUNC) {
2224 return is_compatible_func(type1, type2);
2225 } else {
2226 return 1;
2230 /* return true if type1 and type2 are exactly the same (including
2231 qualifiers).
2233 static int is_compatible_types(CType *type1, CType *type2)
2235 return compare_types(type1,type2,0);
2238 /* return true if type1 and type2 are the same (ignoring qualifiers).
2240 static int is_compatible_parameter_types(CType *type1, CType *type2)
2242 return compare_types(type1,type2,1);
2245 /* print a type. If 'varstr' is not NULL, then the variable is also
2246 printed in the type */
2247 /* XXX: union */
2248 /* XXX: add array and function pointers */
2249 static void type_to_str(char *buf, int buf_size,
2250 CType *type, const char *varstr)
2252 int bt, v, t;
2253 Sym *s, *sa;
2254 char buf1[256];
2255 const char *tstr;
2257 t = type->t & VT_TYPE;
2258 bt = t & VT_BTYPE;
2259 buf[0] = '\0';
2260 if (t & VT_CONSTANT)
2261 pstrcat(buf, buf_size, "const ");
2262 if (t & VT_VOLATILE)
2263 pstrcat(buf, buf_size, "volatile ");
2264 if (t & VT_UNSIGNED)
2265 pstrcat(buf, buf_size, "unsigned ");
2266 switch(bt) {
2267 case VT_VOID:
2268 tstr = "void";
2269 goto add_tstr;
2270 case VT_BOOL:
2271 tstr = "_Bool";
2272 goto add_tstr;
2273 case VT_BYTE:
2274 tstr = "char";
2275 goto add_tstr;
2276 case VT_SHORT:
2277 tstr = "short";
2278 goto add_tstr;
2279 case VT_INT:
2280 tstr = "int";
2281 goto add_tstr;
2282 case VT_LONG:
2283 tstr = "long";
2284 goto add_tstr;
2285 case VT_LLONG:
2286 tstr = "long long";
2287 goto add_tstr;
2288 case VT_FLOAT:
2289 tstr = "float";
2290 goto add_tstr;
2291 case VT_DOUBLE:
2292 tstr = "double";
2293 goto add_tstr;
2294 case VT_LDOUBLE:
2295 tstr = "long double";
2296 add_tstr:
2297 pstrcat(buf, buf_size, tstr);
2298 break;
2299 case VT_ENUM:
2300 case VT_STRUCT:
2301 if (bt == VT_STRUCT)
2302 tstr = "struct ";
2303 else
2304 tstr = "enum ";
2305 pstrcat(buf, buf_size, tstr);
2306 v = type->ref->v & ~SYM_STRUCT;
2307 if (v >= SYM_FIRST_ANOM)
2308 pstrcat(buf, buf_size, "<anonymous>");
2309 else
2310 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2311 break;
2312 case VT_FUNC:
2313 s = type->ref;
2314 type_to_str(buf, buf_size, &s->type, varstr);
2315 pstrcat(buf, buf_size, "(");
2316 sa = s->next;
2317 while (sa != NULL) {
2318 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2319 pstrcat(buf, buf_size, buf1);
2320 sa = sa->next;
2321 if (sa)
2322 pstrcat(buf, buf_size, ", ");
2324 pstrcat(buf, buf_size, ")");
2325 goto no_var;
2326 case VT_PTR:
2327 s = type->ref;
2328 pstrcpy(buf1, sizeof(buf1), "*");
2329 if (varstr)
2330 pstrcat(buf1, sizeof(buf1), varstr);
2331 type_to_str(buf, buf_size, &s->type, buf1);
2332 goto no_var;
2334 if (varstr) {
2335 pstrcat(buf, buf_size, " ");
2336 pstrcat(buf, buf_size, varstr);
2338 no_var: ;
2341 /* verify type compatibility to store vtop in 'dt' type, and generate
2342 casts if needed. */
2343 static void gen_assign_cast(CType *dt)
2345 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2346 char buf1[256], buf2[256];
2347 int dbt, sbt;
2349 st = &vtop->type; /* source type */
2350 dbt = dt->t & VT_BTYPE;
2351 sbt = st->t & VT_BTYPE;
2352 if (sbt == VT_VOID || dbt == VT_VOID)
2353 tcc_error("cannot cast from/to void");
2354 if (dt->t & VT_CONSTANT)
2355 tcc_warning("assignment of read-only location");
2356 switch(dbt) {
2357 case VT_PTR:
2358 /* special cases for pointers */
2359 /* '0' can also be a pointer */
2360 if (is_null_pointer(vtop))
2361 goto type_ok;
2362 /* accept implicit pointer to integer cast with warning */
2363 if (is_integer_btype(sbt)) {
2364 tcc_warning("assignment makes pointer from integer without a cast");
2365 goto type_ok;
2367 type1 = pointed_type(dt);
2368 /* a function is implicitely a function pointer */
2369 if (sbt == VT_FUNC) {
2370 if ((type1->t & VT_BTYPE) != VT_VOID &&
2371 !is_compatible_types(pointed_type(dt), st))
2372 tcc_warning("assignment from incompatible pointer type");
2373 goto type_ok;
2375 if (sbt != VT_PTR)
2376 goto error;
2377 type2 = pointed_type(st);
2378 if ((type1->t & VT_BTYPE) == VT_VOID ||
2379 (type2->t & VT_BTYPE) == VT_VOID) {
2380 /* void * can match anything */
2381 } else {
2382 /* exact type match, except for unsigned */
2383 tmp_type1 = *type1;
2384 tmp_type2 = *type2;
2385 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2386 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2387 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2388 tcc_warning("assignment from incompatible pointer type");
2390 /* check const and volatile */
2391 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2392 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2393 tcc_warning("assignment discards qualifiers from pointer target type");
2394 break;
2395 case VT_BYTE:
2396 case VT_SHORT:
2397 case VT_INT:
2398 case VT_LLONG:
2399 if (sbt == VT_PTR || sbt == VT_FUNC) {
2400 tcc_warning("assignment makes integer from pointer without a cast");
2402 /* XXX: more tests */
2403 break;
2404 case VT_STRUCT:
2405 tmp_type1 = *dt;
2406 tmp_type2 = *st;
2407 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2408 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2409 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2410 error:
2411 type_to_str(buf1, sizeof(buf1), st, NULL);
2412 type_to_str(buf2, sizeof(buf2), dt, NULL);
2413 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2415 break;
2417 type_ok:
2418 gen_cast(dt);
2421 /* store vtop in lvalue pushed on stack */
2422 ST_FUNC void vstore(void)
2424 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2426 ft = vtop[-1].type.t;
2427 sbt = vtop->type.t & VT_BTYPE;
2428 dbt = ft & VT_BTYPE;
2429 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2430 (sbt == VT_INT && dbt == VT_SHORT))
2431 && !(vtop->type.t & VT_BITFIELD)) {
2432 /* optimize char/short casts */
2433 delayed_cast = VT_MUSTCAST;
2434 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2435 /* XXX: factorize */
2436 if (ft & VT_CONSTANT)
2437 tcc_warning("assignment of read-only location");
2438 } else {
2439 delayed_cast = 0;
2440 if (!(ft & VT_BITFIELD))
2441 gen_assign_cast(&vtop[-1].type);
2444 if (sbt == VT_STRUCT) {
2445 /* if structure, only generate pointer */
2446 /* structure assignment : generate memcpy */
2447 /* XXX: optimize if small size */
2448 if (!nocode_wanted) {
2449 size = type_size(&vtop->type, &align);
2451 /* destination */
2452 vswap();
2453 vtop->type.t = VT_PTR;
2454 gaddrof();
2456 /* address of memcpy() */
2457 #ifdef TCC_ARM_EABI
2458 if(!(align & 7))
2459 vpush_global_sym(&func_old_type, TOK_memcpy8);
2460 else if(!(align & 3))
2461 vpush_global_sym(&func_old_type, TOK_memcpy4);
2462 else
2463 #endif
2464 vpush_global_sym(&func_old_type, TOK_memcpy);
2466 vswap();
2467 /* source */
2468 vpushv(vtop - 2);
2469 vtop->type.t = VT_PTR;
2470 gaddrof();
2471 /* type size */
2472 vpushi(size);
2473 gfunc_call(3);
2474 } else {
2475 vswap();
2476 vpop();
2478 /* leave source on stack */
2479 } else if (ft & VT_BITFIELD) {
2480 /* bitfield store handling */
2481 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2482 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2483 /* remove bit field info to avoid loops */
2484 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2486 /* duplicate source into other register */
2487 gv_dup();
2488 vswap();
2489 vrott(3);
2491 if((ft & VT_BTYPE) == VT_BOOL) {
2492 gen_cast(&vtop[-1].type);
2493 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2496 /* duplicate destination */
2497 vdup();
2498 vtop[-1] = vtop[-2];
2500 /* mask and shift source */
2501 if((ft & VT_BTYPE) != VT_BOOL) {
2502 if((ft & VT_BTYPE) == VT_LLONG) {
2503 vpushll((1ULL << bit_size) - 1ULL);
2504 } else {
2505 vpushi((1 << bit_size) - 1);
2507 gen_op('&');
2509 vpushi(bit_pos);
2510 gen_op(TOK_SHL);
2511 /* load destination, mask and or with source */
2512 vswap();
2513 if((ft & VT_BTYPE) == VT_LLONG) {
2514 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2515 } else {
2516 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2518 gen_op('&');
2519 gen_op('|');
2520 /* store result */
2521 vstore();
2523 /* pop off shifted source from "duplicate source..." above */
2524 vpop();
2526 } else {
2527 #ifdef CONFIG_TCC_BCHECK
2528 /* bound check case */
2529 if (vtop[-1].r & VT_MUSTBOUND) {
2530 vswap();
2531 gbound();
2532 vswap();
2534 #endif
2535 if (!nocode_wanted) {
2536 rc = RC_INT;
2537 if (is_float(ft)) {
2538 rc = RC_FLOAT;
2539 #ifdef TCC_TARGET_X86_64
2540 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2541 rc = RC_ST0;
2542 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2543 rc = RC_FRET;
2545 #endif
2547 r = gv(rc); /* generate value */
2548 /* if lvalue was saved on stack, must read it */
2549 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2550 SValue sv;
2551 t = get_reg(RC_INT);
2552 #ifdef TCC_TARGET_X86_64
2553 sv.type.t = VT_PTR;
2554 #else
2555 sv.type.t = VT_INT;
2556 #endif
2557 sv.r = VT_LOCAL | VT_LVAL;
2558 sv.c.ul = vtop[-1].c.ul;
2559 load(t, &sv);
2560 vtop[-1].r = t | VT_LVAL;
2562 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2563 #ifdef TCC_TARGET_X86_64
2564 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2565 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2566 #else
2567 if ((ft & VT_BTYPE) == VT_LLONG) {
2568 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2569 #endif
2570 vtop[-1].type.t = load_type;
2571 store(r, vtop - 1);
2572 vswap();
2573 /* convert to int to increment easily */
2574 vtop->type.t = addr_type;
2575 gaddrof();
2576 vpushi(load_size);
2577 gen_op('+');
2578 vtop->r |= VT_LVAL;
2579 vswap();
2580 vtop[-1].type.t = load_type;
2581 /* XXX: it works because r2 is spilled last ! */
2582 store(vtop->r2, vtop - 1);
2583 } else {
2584 store(r, vtop - 1);
2587 vswap();
2588 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2589 vtop->r |= delayed_cast;
2593 /* post defines POST/PRE add. c is the token ++ or -- */
2594 ST_FUNC void inc(int post, int c)
2596 test_lvalue();
2597 vdup(); /* save lvalue */
2598 if (post) {
2599 gv_dup(); /* duplicate value */
2600 vrotb(3);
2601 vrotb(3);
2603 /* add constant */
2604 vpushi(c - TOK_MID);
2605 gen_op('+');
2606 vstore(); /* store value */
2607 if (post)
2608 vpop(); /* if post op, return saved value */
2611 /* Parse GNUC __attribute__ extension. Currently, the following
2612 extensions are recognized:
2613 - aligned(n) : set data/function alignment.
2614 - packed : force data alignment to 1
2615 - section(x) : generate data/code in this section.
2616 - unused : currently ignored, but may be used someday.
2617 - regparm(n) : pass function parameters in registers (i386 only)
2619 static void parse_attribute(AttributeDef *ad)
2621 int t, n;
2623 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2624 next();
2625 skip('(');
2626 skip('(');
2627 while (tok != ')') {
2628 if (tok < TOK_IDENT)
2629 expect("attribute name");
2630 t = tok;
2631 next();
2632 switch(t) {
2633 case TOK_SECTION1:
2634 case TOK_SECTION2:
2635 skip('(');
2636 if (tok != TOK_STR)
2637 expect("section name");
2638 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2639 next();
2640 skip(')');
2641 break;
2642 case TOK_ALIAS1:
2643 case TOK_ALIAS2:
2644 skip('(');
2645 if (tok != TOK_STR)
2646 expect("alias(\"target\")");
2647 ad->alias_target = /* save string as token, for later */
2648 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2649 next();
2650 skip(')');
2651 break;
2652 case TOK_ALIGNED1:
2653 case TOK_ALIGNED2:
2654 if (tok == '(') {
2655 next();
2656 n = expr_const();
2657 if (n <= 0 || (n & (n - 1)) != 0)
2658 tcc_error("alignment must be a positive power of two");
2659 skip(')');
2660 } else {
2661 n = MAX_ALIGN;
2663 ad->a.aligned = n;
2664 break;
2665 case TOK_PACKED1:
2666 case TOK_PACKED2:
2667 ad->a.packed = 1;
2668 break;
2669 case TOK_WEAK1:
2670 case TOK_WEAK2:
2671 ad->a.weak = 1;
2672 break;
2673 case TOK_UNUSED1:
2674 case TOK_UNUSED2:
2675 /* currently, no need to handle it because tcc does not
2676 track unused objects */
2677 break;
2678 case TOK_NORETURN1:
2679 case TOK_NORETURN2:
2680 /* currently, no need to handle it because tcc does not
2681 track unused objects */
2682 break;
2683 case TOK_CDECL1:
2684 case TOK_CDECL2:
2685 case TOK_CDECL3:
2686 ad->a.func_call = FUNC_CDECL;
2687 break;
2688 case TOK_STDCALL1:
2689 case TOK_STDCALL2:
2690 case TOK_STDCALL3:
2691 ad->a.func_call = FUNC_STDCALL;
2692 break;
2693 #ifdef TCC_TARGET_I386
2694 case TOK_REGPARM1:
2695 case TOK_REGPARM2:
2696 skip('(');
2697 n = expr_const();
2698 if (n > 3)
2699 n = 3;
2700 else if (n < 0)
2701 n = 0;
2702 if (n > 0)
2703 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2704 skip(')');
2705 break;
2706 case TOK_FASTCALL1:
2707 case TOK_FASTCALL2:
2708 case TOK_FASTCALL3:
2709 ad->a.func_call = FUNC_FASTCALLW;
2710 break;
2711 #endif
2712 case TOK_MODE:
2713 skip('(');
2714 switch(tok) {
2715 case TOK_MODE_DI:
2716 ad->a.mode = VT_LLONG + 1;
2717 break;
2718 case TOK_MODE_HI:
2719 ad->a.mode = VT_SHORT + 1;
2720 break;
2721 case TOK_MODE_SI:
2722 ad->a.mode = VT_INT + 1;
2723 break;
2724 default:
2725 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2726 break;
2728 next();
2729 skip(')');
2730 break;
2731 case TOK_DLLEXPORT:
2732 ad->a.func_export = 1;
2733 break;
2734 case TOK_DLLIMPORT:
2735 ad->a.func_import = 1;
2736 break;
2737 default:
2738 if (tcc_state->warn_unsupported)
2739 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2740 /* skip parameters */
2741 if (tok == '(') {
2742 int parenthesis = 0;
2743 do {
2744 if (tok == '(')
2745 parenthesis++;
2746 else if (tok == ')')
2747 parenthesis--;
2748 next();
2749 } while (parenthesis && tok != -1);
2751 break;
2753 if (tok != ',')
2754 break;
2755 next();
2757 skip(')');
2758 skip(')');
2762 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2763 static void struct_decl(CType *type, int u, int tdef)
2765 int a, v, size, align, maxalign, c, offset, flexible;
2766 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2767 Sym *s, *ss, *ass, **ps;
2768 AttributeDef ad;
2769 CType type1, btype;
2771 a = tok; /* save decl type */
2772 next();
2773 if (tok != '{') {
2774 v = tok;
2775 next();
2776 /* struct already defined ? return it */
2777 if (v < TOK_IDENT)
2778 expect("struct/union/enum name");
2779 s = struct_find(v);
2780 if (s) {
2781 if (s->type.t != a)
2782 tcc_error("invalid type");
2783 goto do_decl;
2784 } else if (tok >= TOK_IDENT && !tdef)
2785 tcc_error("unknown struct/union/enum");
2786 } else {
2787 v = anon_sym++;
2789 type1.t = a;
2790 type1.ref = NULL;
2791 /* we put an undefined size for struct/union */
2792 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2793 s->r = 0; /* default alignment is zero as gcc */
2794 /* put struct/union/enum name in type */
2795 do_decl:
2796 type->t = u;
2797 type->ref = s;
2799 if (tok == '{') {
2800 next();
2801 if (s->c != -1)
2802 tcc_error("struct/union/enum already defined");
2803 /* cannot be empty */
2804 c = 0;
2805 /* non empty enums are not allowed */
2806 if (a == TOK_ENUM) {
2807 for(;;) {
2808 v = tok;
2809 if (v < TOK_UIDENT)
2810 expect("identifier");
2811 ss = sym_find(v);
2812 if (ss)
2813 tcc_error("redefinition of enumerator '%s'",
2814 get_tok_str(v, NULL));
2815 next();
2816 if (tok == '=') {
2817 next();
2818 c = expr_const();
2820 /* enum symbols have static storage */
2821 ss = sym_push(v, &int_type, VT_CONST, c);
2822 ss->type.t |= VT_STATIC;
2823 if (tok != ',')
2824 break;
2825 next();
2826 c++;
2827 /* NOTE: we accept a trailing comma */
2828 if (tok == '}')
2829 break;
2831 s->c = type_size(&int_type, &align);
2832 skip('}');
2833 } else {
2834 maxalign = 1;
2835 ps = &s->next;
2836 prevbt = VT_INT;
2837 bit_pos = 0;
2838 offset = 0;
2839 flexible = 0;
2840 while (tok != '}') {
2841 parse_btype(&btype, &ad);
2842 while (1) {
2843 if (flexible)
2844 tcc_error("flexible array member '%s' not at the end of struct",
2845 get_tok_str(v, NULL));
2846 bit_size = -1;
2847 v = 0;
2848 type1 = btype;
2849 if (tok != ':') {
2850 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2851 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2852 expect("identifier");
2853 if (type_size(&type1, &align) < 0) {
2854 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2855 flexible = 1;
2856 else
2857 tcc_error("field '%s' has incomplete type",
2858 get_tok_str(v, NULL));
2860 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2861 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2862 tcc_error("invalid type for '%s'",
2863 get_tok_str(v, NULL));
2865 if (tok == ':') {
2866 next();
2867 bit_size = expr_const();
2868 /* XXX: handle v = 0 case for messages */
2869 if (bit_size < 0)
2870 tcc_error("negative width in bit-field '%s'",
2871 get_tok_str(v, NULL));
2872 if (v && bit_size == 0)
2873 tcc_error("zero width for bit-field '%s'",
2874 get_tok_str(v, NULL));
2876 size = type_size(&type1, &align);
2877 if (ad.a.aligned) {
2878 if (align < ad.a.aligned)
2879 align = ad.a.aligned;
2880 } else if (ad.a.packed) {
2881 align = 1;
2882 } else if (*tcc_state->pack_stack_ptr) {
2883 if (align > *tcc_state->pack_stack_ptr)
2884 align = *tcc_state->pack_stack_ptr;
2886 lbit_pos = 0;
2887 if (bit_size >= 0) {
2888 bt = type1.t & VT_BTYPE;
2889 if (bt != VT_INT &&
2890 bt != VT_BYTE &&
2891 bt != VT_SHORT &&
2892 bt != VT_BOOL &&
2893 bt != VT_ENUM &&
2894 bt != VT_LLONG)
2895 tcc_error("bitfields must have scalar type");
2896 bsize = size * 8;
2897 if (bit_size > bsize) {
2898 tcc_error("width of '%s' exceeds its type",
2899 get_tok_str(v, NULL));
2900 } else if (bit_size == bsize) {
2901 /* no need for bit fields */
2902 bit_pos = 0;
2903 } else if (bit_size == 0) {
2904 /* XXX: what to do if only padding in a
2905 structure ? */
2906 /* zero size: means to pad */
2907 bit_pos = 0;
2908 } else {
2909 /* we do not have enough room ?
2910 did the type change?
2911 is it a union? */
2912 if ((bit_pos + bit_size) > bsize ||
2913 bt != prevbt || a == TOK_UNION)
2914 bit_pos = 0;
2915 lbit_pos = bit_pos;
2916 /* XXX: handle LSB first */
2917 type1.t |= VT_BITFIELD |
2918 (bit_pos << VT_STRUCT_SHIFT) |
2919 (bit_size << (VT_STRUCT_SHIFT + 6));
2920 bit_pos += bit_size;
2922 prevbt = bt;
2923 } else {
2924 bit_pos = 0;
2926 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2927 /* add new memory data only if starting
2928 bit field */
2929 if (lbit_pos == 0) {
2930 if (a == TOK_STRUCT) {
2931 c = (c + align - 1) & -align;
2932 offset = c;
2933 if (size > 0)
2934 c += size;
2935 } else {
2936 offset = 0;
2937 if (size > c)
2938 c = size;
2940 if (align > maxalign)
2941 maxalign = align;
2943 #if 0
2944 printf("add field %s offset=%d",
2945 get_tok_str(v, NULL), offset);
2946 if (type1.t & VT_BITFIELD) {
2947 printf(" pos=%d size=%d",
2948 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2949 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2951 printf("\n");
2952 #endif
2954 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2955 ass = type1.ref;
2956 while ((ass = ass->next) != NULL) {
2957 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2958 *ps = ss;
2959 ps = &ss->next;
2961 } else if (v) {
2962 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2963 *ps = ss;
2964 ps = &ss->next;
2966 if (tok == ';' || tok == TOK_EOF)
2967 break;
2968 skip(',');
2970 skip(';');
2972 skip('}');
2973 /* store size and alignment */
2974 s->c = (c + maxalign - 1) & -maxalign;
2975 s->r = maxalign;
2980 /* return 0 if no type declaration. otherwise, return the basic type
2981 and skip it.
2983 static int parse_btype(CType *type, AttributeDef *ad)
2985 int t, u, type_found, typespec_found, typedef_found;
2986 Sym *s;
2987 CType type1;
2989 memset(ad, 0, sizeof(AttributeDef));
2990 type_found = 0;
2991 typespec_found = 0;
2992 typedef_found = 0;
2993 t = 0;
2994 while(1) {
2995 switch(tok) {
2996 case TOK_EXTENSION:
2997 /* currently, we really ignore extension */
2998 next();
2999 continue;
3001 /* basic types */
3002 case TOK_CHAR:
3003 u = VT_BYTE;
3004 basic_type:
3005 next();
3006 basic_type1:
3007 if ((t & VT_BTYPE) != 0)
3008 tcc_error("too many basic types");
3009 t |= u;
3010 typespec_found = 1;
3011 break;
3012 case TOK_VOID:
3013 u = VT_VOID;
3014 goto basic_type;
3015 case TOK_SHORT:
3016 u = VT_SHORT;
3017 goto basic_type;
3018 case TOK_INT:
3019 next();
3020 typespec_found = 1;
3021 break;
3022 case TOK_LONG:
3023 next();
3024 if ((t & VT_BTYPE) == VT_DOUBLE) {
3025 #ifndef TCC_TARGET_PE
3026 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3027 #endif
3028 } else if ((t & VT_BTYPE) == VT_LONG) {
3029 t = (t & ~VT_BTYPE) | VT_LLONG;
3030 } else {
3031 u = VT_LONG;
3032 goto basic_type1;
3034 break;
3035 case TOK_BOOL:
3036 u = VT_BOOL;
3037 goto basic_type;
3038 case TOK_FLOAT:
3039 u = VT_FLOAT;
3040 goto basic_type;
3041 case TOK_DOUBLE:
3042 next();
3043 if ((t & VT_BTYPE) == VT_LONG) {
3044 #ifdef TCC_TARGET_PE
3045 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3046 #else
3047 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3048 #endif
3049 } else {
3050 u = VT_DOUBLE;
3051 goto basic_type1;
3053 break;
3054 case TOK_ENUM:
3055 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3056 basic_type2:
3057 u = type1.t;
3058 type->ref = type1.ref;
3059 goto basic_type1;
3060 case TOK_STRUCT:
3061 case TOK_UNION:
3062 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3063 goto basic_type2;
3065 /* type modifiers */
3066 case TOK_CONST1:
3067 case TOK_CONST2:
3068 case TOK_CONST3:
3069 t |= VT_CONSTANT;
3070 next();
3071 break;
3072 case TOK_VOLATILE1:
3073 case TOK_VOLATILE2:
3074 case TOK_VOLATILE3:
3075 t |= VT_VOLATILE;
3076 next();
3077 break;
3078 case TOK_SIGNED1:
3079 case TOK_SIGNED2:
3080 case TOK_SIGNED3:
3081 typespec_found = 1;
3082 t |= VT_SIGNED;
3083 next();
3084 break;
3085 case TOK_REGISTER:
3086 case TOK_AUTO:
3087 case TOK_RESTRICT1:
3088 case TOK_RESTRICT2:
3089 case TOK_RESTRICT3:
3090 next();
3091 break;
3092 case TOK_UNSIGNED:
3093 t |= VT_UNSIGNED;
3094 next();
3095 typespec_found = 1;
3096 break;
3098 /* storage */
3099 case TOK_EXTERN:
3100 t |= VT_EXTERN;
3101 next();
3102 break;
3103 case TOK_STATIC:
3104 t |= VT_STATIC;
3105 next();
3106 break;
3107 case TOK_TYPEDEF:
3108 t |= VT_TYPEDEF;
3109 next();
3110 break;
3111 case TOK_INLINE1:
3112 case TOK_INLINE2:
3113 case TOK_INLINE3:
3114 t |= VT_INLINE;
3115 next();
3116 break;
3118 /* GNUC attribute */
3119 case TOK_ATTRIBUTE1:
3120 case TOK_ATTRIBUTE2:
3121 parse_attribute(ad);
3122 if (ad->a.mode) {
3123 u = ad->a.mode -1;
3124 t = (t & ~VT_BTYPE) | u;
3126 break;
3127 /* GNUC typeof */
3128 case TOK_TYPEOF1:
3129 case TOK_TYPEOF2:
3130 case TOK_TYPEOF3:
3131 next();
3132 parse_expr_type(&type1);
3133 /* remove all storage modifiers except typedef */
3134 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3135 goto basic_type2;
3136 default:
3137 if (typespec_found || typedef_found)
3138 goto the_end;
3139 s = sym_find(tok);
3140 if (!s || !(s->type.t & VT_TYPEDEF))
3141 goto the_end;
3142 typedef_found = 1;
3143 t |= (s->type.t & ~VT_TYPEDEF);
3144 type->ref = s->type.ref;
3145 if (s->r) {
3146 /* get attributes from typedef */
3147 if (0 == ad->a.aligned)
3148 ad->a.aligned = s->a.aligned;
3149 if (0 == ad->a.func_call)
3150 ad->a.func_call = s->a.func_call;
3151 ad->a.packed |= s->a.packed;
3153 next();
3154 typespec_found = 1;
3155 break;
3157 type_found = 1;
3159 the_end:
3160 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3161 tcc_error("signed and unsigned modifier");
3162 if (tcc_state->char_is_unsigned) {
3163 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3164 t |= VT_UNSIGNED;
3166 t &= ~VT_SIGNED;
3168 /* long is never used as type */
3169 if ((t & VT_BTYPE) == VT_LONG)
3170 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3171 t = (t & ~VT_BTYPE) | VT_INT;
3172 #else
3173 t = (t & ~VT_BTYPE) | VT_LLONG;
3174 #endif
3175 type->t = t;
3176 return type_found;
3179 /* convert a function parameter type (array to pointer and function to
3180 function pointer) */
3181 static inline void convert_parameter_type(CType *pt)
3183 /* remove const and volatile qualifiers (XXX: const could be used
3184 to indicate a const function parameter */
3185 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3186 /* array must be transformed to pointer according to ANSI C */
3187 pt->t &= ~VT_ARRAY;
3188 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3189 mk_pointer(pt);
3193 ST_FUNC void parse_asm_str(CString *astr)
3195 skip('(');
3196 /* read the string */
3197 if (tok != TOK_STR)
3198 expect("string constant");
3199 cstr_new(astr);
3200 while (tok == TOK_STR) {
3201 /* XXX: add \0 handling too ? */
3202 cstr_cat(astr, tokc.cstr->data);
3203 next();
3205 cstr_ccat(astr, '\0');
3208 /* Parse an asm label and return the label
3209 * Don't forget to free the CString in the caller! */
3210 static void asm_label_instr(CString *astr)
3212 next();
3213 parse_asm_str(astr);
3214 skip(')');
3215 #ifdef ASM_DEBUG
3216 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3217 #endif
3220 static void post_type(CType *type, AttributeDef *ad)
3222 int n, l, t1, arg_size, align;
3223 Sym **plast, *s, *first;
3224 AttributeDef ad1;
3225 CType pt;
3227 if (tok == '(') {
3228 /* function declaration */
3229 next();
3230 l = 0;
3231 first = NULL;
3232 plast = &first;
3233 arg_size = 0;
3234 if (tok != ')') {
3235 for(;;) {
3236 /* read param name and compute offset */
3237 if (l != FUNC_OLD) {
3238 if (!parse_btype(&pt, &ad1)) {
3239 if (l) {
3240 tcc_error("invalid type");
3241 } else {
3242 l = FUNC_OLD;
3243 goto old_proto;
3246 l = FUNC_NEW;
3247 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3248 break;
3249 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3250 if ((pt.t & VT_BTYPE) == VT_VOID)
3251 tcc_error("parameter declared as void");
3252 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3253 } else {
3254 old_proto:
3255 n = tok;
3256 if (n < TOK_UIDENT)
3257 expect("identifier");
3258 pt.t = VT_INT;
3259 next();
3261 convert_parameter_type(&pt);
3262 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3263 *plast = s;
3264 plast = &s->next;
3265 if (tok == ')')
3266 break;
3267 skip(',');
3268 if (l == FUNC_NEW && tok == TOK_DOTS) {
3269 l = FUNC_ELLIPSIS;
3270 next();
3271 break;
3275 /* if no parameters, then old type prototype */
3276 if (l == 0)
3277 l = FUNC_OLD;
3278 skip(')');
3279 /* NOTE: const is ignored in returned type as it has a special
3280 meaning in gcc / C++ */
3281 type->t &= ~VT_CONSTANT;
3282 /* some ancient pre-K&R C allows a function to return an array
3283 and the array brackets to be put after the arguments, such
3284 that "int c()[]" means something like "int[] c()" */
3285 if (tok == '[') {
3286 next();
3287 skip(']'); /* only handle simple "[]" */
3288 type->t |= VT_PTR;
3290 /* we push a anonymous symbol which will contain the function prototype */
3291 ad->a.func_args = arg_size;
3292 s = sym_push(SYM_FIELD, type, 0, l);
3293 s->a = ad->a;
3294 s->next = first;
3295 type->t = VT_FUNC;
3296 type->ref = s;
3297 } else if (tok == '[') {
3298 /* array definition */
3299 next();
3300 if (tok == TOK_RESTRICT1)
3301 next();
3302 n = -1;
3303 t1 = 0;
3304 if (tok != ']') {
3305 if (!local_stack || nocode_wanted)
3306 vpushi(expr_const());
3307 else gexpr();
3308 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3309 n = vtop->c.i;
3310 if (n < 0)
3311 tcc_error("invalid array size");
3312 } else {
3313 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3314 tcc_error("size of variable length array should be an integer");
3315 t1 = VT_VLA;
3318 skip(']');
3319 /* parse next post type */
3320 post_type(type, ad);
3321 if (type->t == VT_FUNC)
3322 tcc_error("declaration of an array of functions");
3323 t1 |= type->t & VT_VLA;
3325 if (t1 & VT_VLA) {
3326 loc -= type_size(&int_type, &align);
3327 loc &= -align;
3328 n = loc;
3330 vla_runtime_type_size(type, &align);
3331 gen_op('*');
3332 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3333 vswap();
3334 vstore();
3336 if (n != -1)
3337 vpop();
3339 /* we push an anonymous symbol which will contain the array
3340 element type */
3341 s = sym_push(SYM_FIELD, type, 0, n);
3342 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3343 type->ref = s;
3347 /* Parse a type declaration (except basic type), and return the type
3348 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3349 expected. 'type' should contain the basic type. 'ad' is the
3350 attribute definition of the basic type. It can be modified by
3351 type_decl().
3353 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3355 Sym *s;
3356 CType type1, *type2;
3357 int qualifiers, storage;
3359 while (tok == '*') {
3360 qualifiers = 0;
3361 redo:
3362 next();
3363 switch(tok) {
3364 case TOK_CONST1:
3365 case TOK_CONST2:
3366 case TOK_CONST3:
3367 qualifiers |= VT_CONSTANT;
3368 goto redo;
3369 case TOK_VOLATILE1:
3370 case TOK_VOLATILE2:
3371 case TOK_VOLATILE3:
3372 qualifiers |= VT_VOLATILE;
3373 goto redo;
3374 case TOK_RESTRICT1:
3375 case TOK_RESTRICT2:
3376 case TOK_RESTRICT3:
3377 goto redo;
3379 mk_pointer(type);
3380 type->t |= qualifiers;
3383 /* XXX: clarify attribute handling */
3384 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3385 parse_attribute(ad);
3387 /* recursive type */
3388 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3389 type1.t = 0; /* XXX: same as int */
3390 if (tok == '(') {
3391 next();
3392 /* XXX: this is not correct to modify 'ad' at this point, but
3393 the syntax is not clear */
3394 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3395 parse_attribute(ad);
3396 type_decl(&type1, ad, v, td);
3397 skip(')');
3398 } else {
3399 /* type identifier */
3400 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3401 *v = tok;
3402 next();
3403 } else {
3404 if (!(td & TYPE_ABSTRACT))
3405 expect("identifier");
3406 *v = 0;
3409 storage = type->t & VT_STORAGE;
3410 type->t &= ~VT_STORAGE;
3411 if (storage & VT_STATIC) {
3412 int saved_nocode_wanted = nocode_wanted;
3413 nocode_wanted = 1;
3414 post_type(type, ad);
3415 nocode_wanted = saved_nocode_wanted;
3416 } else
3417 post_type(type, ad);
3418 type->t |= storage;
3419 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3420 parse_attribute(ad);
3422 if (!type1.t)
3423 return;
3424 /* append type at the end of type1 */
3425 type2 = &type1;
3426 for(;;) {
3427 s = type2->ref;
3428 type2 = &s->type;
3429 if (!type2->t) {
3430 *type2 = *type;
3431 break;
3434 *type = type1;
3437 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3438 ST_FUNC int lvalue_type(int t)
3440 int bt, r;
3441 r = VT_LVAL;
3442 bt = t & VT_BTYPE;
3443 if (bt == VT_BYTE || bt == VT_BOOL)
3444 r |= VT_LVAL_BYTE;
3445 else if (bt == VT_SHORT)
3446 r |= VT_LVAL_SHORT;
3447 else
3448 return r;
3449 if (t & VT_UNSIGNED)
3450 r |= VT_LVAL_UNSIGNED;
3451 return r;
3454 /* indirection with full error checking and bound check */
3455 ST_FUNC void indir(void)
3457 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3458 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3459 return;
3460 expect("pointer");
3462 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3463 gv(RC_INT);
3464 vtop->type = *pointed_type(&vtop->type);
3465 /* Arrays and functions are never lvalues */
3466 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3467 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3468 vtop->r |= lvalue_type(vtop->type.t);
3469 /* if bound checking, the referenced pointer must be checked */
3470 #ifdef CONFIG_TCC_BCHECK
3471 if (tcc_state->do_bounds_check)
3472 vtop->r |= VT_MUSTBOUND;
3473 #endif
3477 /* pass a parameter to a function and do type checking and casting */
3478 static void gfunc_param_typed(Sym *func, Sym *arg)
3480 int func_type;
3481 CType type;
3483 func_type = func->c;
3484 if (func_type == FUNC_OLD ||
3485 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3486 /* default casting : only need to convert float to double */
3487 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3488 type.t = VT_DOUBLE;
3489 gen_cast(&type);
3491 } else if (arg == NULL) {
3492 tcc_error("too many arguments to function");
3493 } else {
3494 type = arg->type;
3495 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3496 gen_assign_cast(&type);
3500 /* parse an expression of the form '(type)' or '(expr)' and return its
3501 type */
3502 static void parse_expr_type(CType *type)
3504 int n;
3505 AttributeDef ad;
3507 skip('(');
3508 if (parse_btype(type, &ad)) {
3509 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3510 } else {
3511 expr_type(type);
3513 skip(')');
3516 static void parse_type(CType *type)
3518 AttributeDef ad;
3519 int n;
3521 if (!parse_btype(type, &ad)) {
3522 expect("type");
3524 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3527 static void vpush_tokc(int t)
3529 CType type;
3530 type.t = t;
3531 type.ref = 0;
3532 vsetc(&type, VT_CONST, &tokc);
3535 ST_FUNC void unary(void)
3537 int n, t, align, size, r, sizeof_caller;
3538 CType type;
3539 Sym *s;
3540 AttributeDef ad;
3541 static int in_sizeof = 0;
3543 sizeof_caller = in_sizeof;
3544 in_sizeof = 0;
3545 /* XXX: GCC 2.95.3 does not generate a table although it should be
3546 better here */
3547 tok_next:
3548 switch(tok) {
3549 case TOK_EXTENSION:
3550 next();
3551 goto tok_next;
3552 case TOK_CINT:
3553 case TOK_CCHAR:
3554 case TOK_LCHAR:
3555 vpushi(tokc.i);
3556 next();
3557 break;
3558 case TOK_CUINT:
3559 vpush_tokc(VT_INT | VT_UNSIGNED);
3560 next();
3561 break;
3562 case TOK_CLLONG:
3563 vpush_tokc(VT_LLONG);
3564 next();
3565 break;
3566 case TOK_CULLONG:
3567 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3568 next();
3569 break;
3570 case TOK_CFLOAT:
3571 vpush_tokc(VT_FLOAT);
3572 next();
3573 break;
3574 case TOK_CDOUBLE:
3575 vpush_tokc(VT_DOUBLE);
3576 next();
3577 break;
3578 case TOK_CLDOUBLE:
3579 vpush_tokc(VT_LDOUBLE);
3580 next();
3581 break;
3582 case TOK___FUNCTION__:
3583 if (!gnu_ext)
3584 goto tok_identifier;
3585 /* fall thru */
3586 case TOK___FUNC__:
3588 void *ptr;
3589 int len;
3590 /* special function name identifier */
3591 len = strlen(funcname) + 1;
3592 /* generate char[len] type */
3593 type.t = VT_BYTE;
3594 mk_pointer(&type);
3595 type.t |= VT_ARRAY;
3596 type.ref->c = len;
3597 vpush_ref(&type, data_section, data_section->data_offset, len);
3598 ptr = section_ptr_add(data_section, len);
3599 memcpy(ptr, funcname, len);
3600 next();
3602 break;
3603 case TOK_LSTR:
3604 #ifdef TCC_TARGET_PE
3605 t = VT_SHORT | VT_UNSIGNED;
3606 #else
3607 t = VT_INT;
3608 #endif
3609 goto str_init;
3610 case TOK_STR:
3611 /* string parsing */
3612 t = VT_BYTE;
3613 str_init:
3614 if (tcc_state->warn_write_strings)
3615 t |= VT_CONSTANT;
3616 type.t = t;
3617 mk_pointer(&type);
3618 type.t |= VT_ARRAY;
3619 memset(&ad, 0, sizeof(AttributeDef));
3620 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3621 break;
3622 case '(':
3623 next();
3624 /* cast ? */
3625 if (parse_btype(&type, &ad)) {
3626 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3627 skip(')');
3628 /* check ISOC99 compound literal */
3629 if (tok == '{') {
3630 /* data is allocated locally by default */
3631 if (global_expr)
3632 r = VT_CONST;
3633 else
3634 r = VT_LOCAL;
3635 /* all except arrays are lvalues */
3636 if (!(type.t & VT_ARRAY))
3637 r |= lvalue_type(type.t);
3638 memset(&ad, 0, sizeof(AttributeDef));
3639 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3640 } else {
3641 if (sizeof_caller) {
3642 vpush(&type);
3643 return;
3645 unary();
3646 gen_cast(&type);
3648 } else if (tok == '{') {
3649 /* save all registers */
3650 save_regs(0);
3651 /* statement expression : we do not accept break/continue
3652 inside as GCC does */
3653 block(NULL, NULL, NULL, NULL, 0, 1);
3654 skip(')');
3655 } else {
3656 gexpr();
3657 skip(')');
3659 break;
3660 case '*':
3661 next();
3662 unary();
3663 indir();
3664 break;
3665 case '&':
3666 next();
3667 unary();
3668 /* functions names must be treated as function pointers,
3669 except for unary '&' and sizeof. Since we consider that
3670 functions are not lvalues, we only have to handle it
3671 there and in function calls. */
3672 /* arrays can also be used although they are not lvalues */
3673 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3674 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3675 test_lvalue();
3676 mk_pointer(&vtop->type);
3677 gaddrof();
3678 break;
3679 case '!':
3680 next();
3681 unary();
3682 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3683 CType boolean;
3684 boolean.t = VT_BOOL;
3685 gen_cast(&boolean);
3686 vtop->c.i = !vtop->c.i;
3687 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3688 vtop->c.i = vtop->c.i ^ 1;
3689 else {
3690 save_regs(1);
3691 vseti(VT_JMP, gvtst(1, 0));
3693 break;
3694 case '~':
3695 next();
3696 unary();
3697 vpushi(-1);
3698 gen_op('^');
3699 break;
3700 case '+':
3701 next();
3702 /* in order to force cast, we add zero */
3703 unary();
3704 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3705 tcc_error("pointer not accepted for unary plus");
3706 vpushi(0);
3707 gen_op('+');
3708 break;
3709 case TOK_SIZEOF:
3710 case TOK_ALIGNOF1:
3711 case TOK_ALIGNOF2:
3712 t = tok;
3713 next();
3714 in_sizeof++;
3715 unary_type(&type); // Perform a in_sizeof = 0;
3716 size = type_size(&type, &align);
3717 if (t == TOK_SIZEOF) {
3718 if (!(type.t & VT_VLA)) {
3719 if (size < 0)
3720 tcc_error("sizeof applied to an incomplete type");
3721 vpushs(size);
3722 } else {
3723 vla_runtime_type_size(&type, &align);
3725 } else {
3726 vpushs(align);
3728 vtop->type.t |= VT_UNSIGNED;
3729 break;
3731 case TOK_builtin_types_compatible_p:
3733 CType type1, type2;
3734 next();
3735 skip('(');
3736 parse_type(&type1);
3737 skip(',');
3738 parse_type(&type2);
3739 skip(')');
3740 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3741 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3742 vpushi(is_compatible_types(&type1, &type2));
3744 break;
3745 case TOK_builtin_constant_p:
3747 int saved_nocode_wanted, res;
3748 next();
3749 skip('(');
3750 saved_nocode_wanted = nocode_wanted;
3751 nocode_wanted = 1;
3752 gexpr();
3753 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3754 vpop();
3755 nocode_wanted = saved_nocode_wanted;
3756 skip(')');
3757 vpushi(res);
3759 break;
3760 case TOK_builtin_frame_address:
3762 int level;
3763 CType type;
3764 next();
3765 skip('(');
3766 if (tok != TOK_CINT || tokc.i < 0) {
3767 tcc_error("__builtin_frame_address only takes positive integers");
3769 level = tokc.i;
3770 next();
3771 skip(')');
3772 type.t = VT_VOID;
3773 mk_pointer(&type);
3774 vset(&type, VT_LOCAL, 0); /* local frame */
3775 while (level--) {
3776 mk_pointer(&vtop->type);
3777 indir(); /* -> parent frame */
3780 break;
3781 #ifdef TCC_TARGET_X86_64
3782 #ifdef TCC_TARGET_PE
3783 case TOK_builtin_va_start:
3785 next();
3786 skip('(');
3787 expr_eq();
3788 skip(',');
3789 expr_eq();
3790 skip(')');
3791 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3792 tcc_error("__builtin_va_start expects a local variable");
3793 vtop->r &= ~(VT_LVAL | VT_REF);
3794 vtop->type = char_pointer_type;
3795 vstore();
3797 break;
3798 #else
3799 case TOK_builtin_va_arg_types:
3801 CType type;
3802 next();
3803 skip('(');
3804 parse_type(&type);
3805 skip(')');
3806 vpushi(classify_x86_64_va_arg(&type));
3808 break;
3809 #endif
3810 #endif
3811 case TOK_INC:
3812 case TOK_DEC:
3813 t = tok;
3814 next();
3815 unary();
3816 inc(0, t);
3817 break;
3818 case '-':
3819 next();
3820 unary();
3821 t = vtop->type.t & VT_BTYPE;
3822 /* handle (-)0.0 */
3823 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST &&
3824 is_float(t)) {
3825 if (t == VT_FLOAT)
3826 vtop->c.f = -vtop->c.f;
3827 else if (t == VT_DOUBLE)
3828 vtop->c.d = -vtop->c.d;
3829 else
3830 vtop->c.ld = -vtop->c.ld;
3831 } else {
3832 vpushi(0);
3833 vswap();
3834 gen_op('-');
3836 break;
3837 case TOK_LAND:
3838 if (!gnu_ext)
3839 goto tok_identifier;
3840 next();
3841 /* allow to take the address of a label */
3842 if (tok < TOK_UIDENT)
3843 expect("label identifier");
3844 s = label_find(tok);
3845 if (!s) {
3846 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3847 } else {
3848 if (s->r == LABEL_DECLARED)
3849 s->r = LABEL_FORWARD;
3851 if (!s->type.t) {
3852 s->type.t = VT_VOID;
3853 mk_pointer(&s->type);
3854 s->type.t |= VT_STATIC;
3856 vset(&s->type, VT_CONST | VT_SYM, 0);
3857 vtop->sym = s;
3858 next();
3859 break;
3861 // special qnan , snan and infinity values
3862 case TOK___NAN__:
3863 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3864 next();
3865 break;
3866 case TOK___SNAN__:
3867 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3868 next();
3869 break;
3870 case TOK___INF__:
3871 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3872 next();
3873 break;
3875 default:
3876 tok_identifier:
3877 t = tok;
3878 next();
3879 if (t < TOK_UIDENT)
3880 expect("identifier");
3881 s = sym_find(t);
3882 if (!s) {
3883 if (tok != '(')
3884 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3885 /* for simple function calls, we tolerate undeclared
3886 external reference to int() function */
3887 if (tcc_state->warn_implicit_function_declaration)
3888 tcc_warning("implicit declaration of function '%s'",
3889 get_tok_str(t, NULL));
3890 s = external_global_sym(t, &func_old_type, 0);
3892 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3893 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3894 /* if referencing an inline function, then we generate a
3895 symbol to it if not already done. It will have the
3896 effect to generate code for it at the end of the
3897 compilation unit. Inline function as always
3898 generated in the text section. */
3899 if (!s->c)
3900 put_extern_sym(s, text_section, 0, 0);
3901 r = VT_SYM | VT_CONST;
3902 } else {
3903 r = s->r;
3905 vset(&s->type, r, s->c);
3906 /* if forward reference, we must point to s */
3907 if (vtop->r & VT_SYM) {
3908 vtop->sym = s;
3909 vtop->c.ul = 0;
3911 break;
3914 /* post operations */
3915 while (1) {
3916 if (tok == TOK_INC || tok == TOK_DEC) {
3917 inc(1, tok);
3918 next();
3919 } else if (tok == '.' || tok == TOK_ARROW) {
3920 int qualifiers;
3921 /* field */
3922 if (tok == TOK_ARROW)
3923 indir();
3924 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3925 test_lvalue();
3926 gaddrof();
3927 next();
3928 /* expect pointer on structure */
3929 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3930 expect("struct or union");
3931 s = vtop->type.ref;
3932 /* find field */
3933 tok |= SYM_FIELD;
3934 while ((s = s->next) != NULL) {
3935 if (s->v == tok)
3936 break;
3938 if (!s)
3939 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3940 /* add field offset to pointer */
3941 vtop->type = char_pointer_type; /* change type to 'char *' */
3942 vpushi(s->c);
3943 gen_op('+');
3944 /* change type to field type, and set to lvalue */
3945 vtop->type = s->type;
3946 vtop->type.t |= qualifiers;
3947 /* an array is never an lvalue */
3948 if (!(vtop->type.t & VT_ARRAY)) {
3949 vtop->r |= lvalue_type(vtop->type.t);
3950 #ifdef CONFIG_TCC_BCHECK
3951 /* if bound checking, the referenced pointer must be checked */
3952 if (tcc_state->do_bounds_check)
3953 vtop->r |= VT_MUSTBOUND;
3954 #endif
3956 next();
3957 } else if (tok == '[') {
3958 next();
3959 gexpr();
3960 gen_op('+');
3961 indir();
3962 skip(']');
3963 } else if (tok == '(') {
3964 SValue ret;
3965 Sym *sa;
3966 int nb_args, ret_nregs, ret_align, variadic;
3968 /* function call */
3969 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3970 /* pointer test (no array accepted) */
3971 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3972 vtop->type = *pointed_type(&vtop->type);
3973 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3974 goto error_func;
3975 } else {
3976 error_func:
3977 expect("function pointer");
3979 } else {
3980 vtop->r &= ~VT_LVAL; /* no lvalue */
3982 /* get return type */
3983 s = vtop->type.ref;
3984 next();
3985 sa = s->next; /* first parameter */
3986 nb_args = 0;
3987 ret.r2 = VT_CONST;
3988 /* compute first implicit argument if a structure is returned */
3989 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3990 variadic = (s->c == FUNC_ELLIPSIS);
3991 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
3992 &ret_align);
3993 if (!ret_nregs) {
3994 /* get some space for the returned structure */
3995 size = type_size(&s->type, &align);
3996 loc = (loc - size) & -align;
3997 ret.type = s->type;
3998 ret.r = VT_LOCAL | VT_LVAL;
3999 /* pass it as 'int' to avoid structure arg passing
4000 problems */
4001 vseti(VT_LOCAL, loc);
4002 ret.c = vtop->c;
4003 nb_args++;
4005 } else {
4006 ret_nregs = 1;
4007 ret.type = s->type;
4010 if (ret_nregs) {
4011 /* return in register */
4012 if (is_float(ret.type.t)) {
4013 ret.r = reg_fret(ret.type.t);
4014 #ifdef TCC_TARGET_X86_64
4015 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4016 ret.r2 = REG_QRET;
4017 #endif
4018 } else {
4019 #ifdef TCC_TARGET_X86_64
4020 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4021 #else
4022 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4023 #endif
4024 ret.r2 = REG_LRET;
4025 ret.r = REG_IRET;
4027 ret.c.i = 0;
4029 if (tok != ')') {
4030 for(;;) {
4031 expr_eq();
4032 gfunc_param_typed(s, sa);
4033 nb_args++;
4034 if (sa)
4035 sa = sa->next;
4036 if (tok == ')')
4037 break;
4038 skip(',');
4041 if (sa)
4042 tcc_error("too few arguments to function");
4043 skip(')');
4044 if (!nocode_wanted) {
4045 gfunc_call(nb_args);
4046 } else {
4047 vtop -= (nb_args + 1);
4050 /* return value */
4051 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4052 vsetc(&ret.type, r, &ret.c);
4053 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4056 /* handle packed struct return */
4057 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4058 int addr, offset;
4060 size = type_size(&s->type, &align);
4061 loc = (loc - size) & -align;
4062 addr = loc;
4063 offset = 0;
4064 for (;;) {
4065 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4066 vswap();
4067 vstore();
4068 vtop--;
4069 if (--ret_nregs == 0)
4070 break;
4071 /* XXX: compatible with arm only: ret_align == register_size */
4072 offset += ret_align;
4074 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4076 } else {
4077 break;
4082 ST_FUNC void expr_prod(void)
4084 int t;
4086 unary();
4087 while (tok == '*' || tok == '/' || tok == '%') {
4088 t = tok;
4089 next();
4090 unary();
4091 gen_op(t);
4095 ST_FUNC void expr_sum(void)
4097 int t;
4099 expr_prod();
4100 while (tok == '+' || tok == '-') {
4101 t = tok;
4102 next();
4103 expr_prod();
4104 gen_op(t);
4108 static void expr_shift(void)
4110 int t;
4112 expr_sum();
4113 while (tok == TOK_SHL || tok == TOK_SAR) {
4114 t = tok;
4115 next();
4116 expr_sum();
4117 gen_op(t);
4121 static void expr_cmp(void)
4123 int t;
4125 expr_shift();
4126 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4127 tok == TOK_ULT || tok == TOK_UGE) {
4128 t = tok;
4129 next();
4130 expr_shift();
4131 gen_op(t);
4135 static void expr_cmpeq(void)
4137 int t;
4139 expr_cmp();
4140 while (tok == TOK_EQ || tok == TOK_NE) {
4141 t = tok;
4142 next();
4143 expr_cmp();
4144 gen_op(t);
4148 static void expr_and(void)
4150 expr_cmpeq();
4151 while (tok == '&') {
4152 next();
4153 expr_cmpeq();
4154 gen_op('&');
4158 static void expr_xor(void)
4160 expr_and();
4161 while (tok == '^') {
4162 next();
4163 expr_and();
4164 gen_op('^');
4168 static void expr_or(void)
4170 expr_xor();
4171 while (tok == '|') {
4172 next();
4173 expr_xor();
4174 gen_op('|');
4178 /* XXX: fix this mess */
4179 static void expr_land_const(void)
4181 expr_or();
4182 while (tok == TOK_LAND) {
4183 next();
4184 expr_or();
4185 gen_op(TOK_LAND);
4189 /* XXX: fix this mess */
4190 static void expr_lor_const(void)
4192 expr_land_const();
4193 while (tok == TOK_LOR) {
4194 next();
4195 expr_land_const();
4196 gen_op(TOK_LOR);
4200 /* only used if non constant */
4201 static void expr_land(void)
4203 int t;
4205 expr_or();
4206 if (tok == TOK_LAND) {
4207 t = 0;
4208 save_regs(1);
4209 for(;;) {
4210 t = gvtst(1, t);
4211 if (tok != TOK_LAND) {
4212 vseti(VT_JMPI, t);
4213 break;
4215 next();
4216 expr_or();
4221 static void expr_lor(void)
4223 int t;
4225 expr_land();
4226 if (tok == TOK_LOR) {
4227 t = 0;
4228 save_regs(1);
4229 for(;;) {
4230 t = gvtst(0, t);
4231 if (tok != TOK_LOR) {
4232 vseti(VT_JMP, t);
4233 break;
4235 next();
4236 expr_land();
4241 /* XXX: better constant handling */
4242 static void expr_cond(void)
4244 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4245 SValue sv;
4246 CType type, type1, type2;
4248 if (const_wanted) {
4249 expr_lor_const();
4250 if (tok == '?') {
4251 CType boolean;
4252 int c;
4253 boolean.t = VT_BOOL;
4254 vdup();
4255 gen_cast(&boolean);
4256 c = vtop->c.i;
4257 vpop();
4258 next();
4259 if (tok != ':' || !gnu_ext) {
4260 vpop();
4261 gexpr();
4263 if (!c)
4264 vpop();
4265 skip(':');
4266 expr_cond();
4267 if (c)
4268 vpop();
4270 } else {
4271 expr_lor();
4272 if (tok == '?') {
4273 next();
4274 if (vtop != vstack) {
4275 /* needed to avoid having different registers saved in
4276 each branch */
4277 if (is_float(vtop->type.t)) {
4278 rc = RC_FLOAT;
4279 #ifdef TCC_TARGET_X86_64
4280 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4281 rc = RC_ST0;
4283 #endif
4285 else
4286 rc = RC_INT;
4287 gv(rc);
4288 save_regs(1);
4290 if (tok == ':' && gnu_ext) {
4291 gv_dup();
4292 tt = gvtst(1, 0);
4293 } else {
4294 tt = gvtst(1, 0);
4295 gexpr();
4297 type1 = vtop->type;
4298 sv = *vtop; /* save value to handle it later */
4299 vtop--; /* no vpop so that FP stack is not flushed */
4300 skip(':');
4301 u = gjmp(0);
4302 gsym(tt);
4303 expr_cond();
4304 type2 = vtop->type;
4306 t1 = type1.t;
4307 bt1 = t1 & VT_BTYPE;
4308 t2 = type2.t;
4309 bt2 = t2 & VT_BTYPE;
4310 /* cast operands to correct type according to ISOC rules */
4311 if (is_float(bt1) || is_float(bt2)) {
4312 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4313 type.t = VT_LDOUBLE;
4314 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4315 type.t = VT_DOUBLE;
4316 } else {
4317 type.t = VT_FLOAT;
4319 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4320 /* cast to biggest op */
4321 type.t = VT_LLONG;
4322 /* convert to unsigned if it does not fit in a long long */
4323 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4324 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4325 type.t |= VT_UNSIGNED;
4326 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4327 /* If one is a null ptr constant the result type
4328 is the other. */
4329 if (is_null_pointer (vtop))
4330 type = type1;
4331 else if (is_null_pointer (&sv))
4332 type = type2;
4333 /* XXX: test pointer compatibility, C99 has more elaborate
4334 rules here. */
4335 else
4336 type = type1;
4337 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4338 /* XXX: test function pointer compatibility */
4339 type = bt1 == VT_FUNC ? type1 : type2;
4340 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4341 /* XXX: test structure compatibility */
4342 type = bt1 == VT_STRUCT ? type1 : type2;
4343 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4344 /* NOTE: as an extension, we accept void on only one side */
4345 type.t = VT_VOID;
4346 } else {
4347 /* integer operations */
4348 type.t = VT_INT;
4349 /* convert to unsigned if it does not fit in an integer */
4350 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4351 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4352 type.t |= VT_UNSIGNED;
4355 /* now we convert second operand */
4356 gen_cast(&type);
4357 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4358 gaddrof();
4359 rc = RC_INT;
4360 if (is_float(type.t)) {
4361 rc = RC_FLOAT;
4362 #ifdef TCC_TARGET_X86_64
4363 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4364 rc = RC_ST0;
4366 #endif
4367 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4368 /* for long longs, we use fixed registers to avoid having
4369 to handle a complicated move */
4370 rc = RC_IRET;
4373 r2 = gv(rc);
4374 /* this is horrible, but we must also convert first
4375 operand */
4376 tt = gjmp(0);
4377 gsym(u);
4378 /* put again first value and cast it */
4379 *vtop = sv;
4380 gen_cast(&type);
4381 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4382 gaddrof();
4383 r1 = gv(rc);
4384 move_reg(r2, r1, type.t);
4385 vtop->r = r2;
4386 gsym(tt);
4391 static void expr_eq(void)
4393 int t;
4395 expr_cond();
4396 if (tok == '=' ||
4397 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4398 tok == TOK_A_XOR || tok == TOK_A_OR ||
4399 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4400 test_lvalue();
4401 t = tok;
4402 next();
4403 if (t == '=') {
4404 expr_eq();
4405 } else {
4406 vdup();
4407 expr_eq();
4408 gen_op(t & 0x7f);
4410 vstore();
4414 ST_FUNC void gexpr(void)
4416 while (1) {
4417 expr_eq();
4418 if (tok != ',')
4419 break;
4420 vpop();
4421 next();
4425 /* parse an expression and return its type without any side effect. */
4426 static void expr_type(CType *type)
4428 int saved_nocode_wanted;
4430 saved_nocode_wanted = nocode_wanted;
4431 nocode_wanted = 1;
4432 gexpr();
4433 *type = vtop->type;
4434 vpop();
4435 nocode_wanted = saved_nocode_wanted;
4438 /* parse a unary expression and return its type without any side
4439 effect. */
4440 static void unary_type(CType *type)
4442 int a;
4444 a = nocode_wanted;
4445 nocode_wanted = 1;
4446 unary();
4447 *type = vtop->type;
4448 vpop();
4449 nocode_wanted = a;
4452 /* parse a constant expression and return value in vtop. */
4453 static void expr_const1(void)
4455 int a;
4456 a = const_wanted;
4457 const_wanted = 1;
4458 expr_cond();
4459 const_wanted = a;
4462 /* parse an integer constant and return its value. */
4463 ST_FUNC int expr_const(void)
4465 int c;
4466 expr_const1();
4467 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4468 expect("constant expression");
4469 c = vtop->c.i;
4470 vpop();
4471 return c;
4474 /* return the label token if current token is a label, otherwise
4475 return zero */
4476 static int is_label(void)
4478 int last_tok;
4480 /* fast test first */
4481 if (tok < TOK_UIDENT)
4482 return 0;
4483 /* no need to save tokc because tok is an identifier */
4484 last_tok = tok;
4485 next();
4486 if (tok == ':') {
4487 next();
4488 return last_tok;
4489 } else {
4490 unget_tok(last_tok);
4491 return 0;
4495 static void label_or_decl(int l)
4497 int last_tok;
4499 /* fast test first */
4500 if (tok >= TOK_UIDENT)
4502 /* no need to save tokc because tok is an identifier */
4503 last_tok = tok;
4504 next();
4505 if (tok == ':') {
4506 unget_tok(last_tok);
4507 return;
4509 unget_tok(last_tok);
4511 decl(l);
4514 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4515 int case_reg, int is_expr)
4517 int a, b, c, d;
4518 Sym *s, *frame_bottom;
4520 /* generate line number info */
4521 if (tcc_state->do_debug &&
4522 (last_line_num != file->line_num || last_ind != ind)) {
4523 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4524 last_ind = ind;
4525 last_line_num = file->line_num;
4528 if (is_expr) {
4529 /* default return value is (void) */
4530 vpushi(0);
4531 vtop->type.t = VT_VOID;
4534 if (tok == TOK_IF) {
4535 /* if test */
4536 next();
4537 skip('(');
4538 gexpr();
4539 skip(')');
4540 a = gvtst(1, 0);
4541 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4542 c = tok;
4543 if (c == TOK_ELSE) {
4544 next();
4545 d = gjmp(0);
4546 gsym(a);
4547 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4548 gsym(d); /* patch else jmp */
4549 } else
4550 gsym(a);
4551 } else if (tok == TOK_WHILE) {
4552 next();
4553 d = ind;
4554 skip('(');
4555 gexpr();
4556 skip(')');
4557 a = gvtst(1, 0);
4558 b = 0;
4559 block(&a, &b, case_sym, def_sym, case_reg, 0);
4560 gjmp_addr(d);
4561 gsym(a);
4562 gsym_addr(b, d);
4563 } else if (tok == '{') {
4564 Sym *llabel;
4565 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4567 next();
4568 /* record local declaration stack position */
4569 s = local_stack;
4570 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4571 frame_bottom->next = scope_stack_bottom;
4572 scope_stack_bottom = frame_bottom;
4573 llabel = local_label_stack;
4575 /* save VLA state */
4576 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4577 if (saved_vla_sp_loc != &vla_sp_root_loc)
4578 vla_sp_loc = &block_vla_sp_loc;
4580 saved_vla_flags = vla_flags;
4581 vla_flags |= VLA_NEED_NEW_FRAME;
4583 /* handle local labels declarations */
4584 if (tok == TOK_LABEL) {
4585 next();
4586 for(;;) {
4587 if (tok < TOK_UIDENT)
4588 expect("label identifier");
4589 label_push(&local_label_stack, tok, LABEL_DECLARED);
4590 next();
4591 if (tok == ',') {
4592 next();
4593 } else {
4594 skip(';');
4595 break;
4599 while (tok != '}') {
4600 label_or_decl(VT_LOCAL);
4601 if (tok != '}') {
4602 if (is_expr)
4603 vpop();
4604 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4607 /* pop locally defined labels */
4608 label_pop(&local_label_stack, llabel);
4609 if(is_expr) {
4610 /* XXX: this solution makes only valgrind happy...
4611 triggered by gcc.c-torture/execute/20000917-1.c */
4612 Sym *p;
4613 switch(vtop->type.t & VT_BTYPE) {
4614 case VT_PTR:
4615 case VT_STRUCT:
4616 case VT_ENUM:
4617 case VT_FUNC:
4618 for(p=vtop->type.ref;p;p=p->prev)
4619 if(p->prev==s)
4620 tcc_error("unsupported expression type");
4623 /* pop locally defined symbols */
4624 scope_stack_bottom = scope_stack_bottom->next;
4625 sym_pop(&local_stack, s);
4627 /* Pop VLA frames and restore stack pointer if required */
4628 if (saved_vla_sp_loc != &vla_sp_root_loc)
4629 *saved_vla_sp_loc = block_vla_sp_loc;
4630 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4631 vla_sp_loc = saved_vla_sp_loc;
4632 gen_vla_sp_restore(*vla_sp_loc);
4634 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4636 next();
4637 } else if (tok == TOK_RETURN) {
4638 next();
4639 if (tok != ';') {
4640 gexpr();
4641 gen_assign_cast(&func_vt);
4642 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4643 CType type, ret_type;
4644 int ret_align, ret_nregs;
4645 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4646 &ret_align);
4647 if (0 == ret_nregs) {
4648 /* if returning structure, must copy it to implicit
4649 first pointer arg location */
4650 type = func_vt;
4651 mk_pointer(&type);
4652 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4653 indir();
4654 vswap();
4655 /* copy structure value to pointer */
4656 vstore();
4657 } else {
4658 /* returning structure packed into registers */
4659 int r, size, addr, align;
4660 size = type_size(&func_vt,&align);
4661 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4662 && (align & (ret_align-1))) {
4663 loc = (loc - size) & -align;
4664 addr = loc;
4665 type = func_vt;
4666 vset(&type, VT_LOCAL | VT_LVAL, addr);
4667 vswap();
4668 vstore();
4669 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4671 vtop->type = ret_type;
4672 if (is_float(ret_type.t))
4673 r = rc_fret(ret_type.t);
4674 else
4675 r = RC_IRET;
4677 for (;;) {
4678 gv(r);
4679 if (--ret_nregs == 0)
4680 break;
4681 /* We assume that when a structure is returned in multiple
4682 registers, their classes are consecutive values of the
4683 suite s(n) = 2^n */
4684 r <<= 1;
4685 /* XXX: compatible with arm only: ret_align == register_size */
4686 vtop->c.i += ret_align;
4687 vtop->r = VT_LOCAL | VT_LVAL;
4690 } else if (is_float(func_vt.t)) {
4691 gv(rc_fret(func_vt.t));
4692 } else {
4693 gv(RC_IRET);
4695 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4697 skip(';');
4698 rsym = gjmp(rsym); /* jmp */
4699 } else if (tok == TOK_BREAK) {
4700 /* compute jump */
4701 if (!bsym)
4702 tcc_error("cannot break");
4703 *bsym = gjmp(*bsym);
4704 next();
4705 skip(';');
4706 } else if (tok == TOK_CONTINUE) {
4707 /* compute jump */
4708 if (!csym)
4709 tcc_error("cannot continue");
4710 *csym = gjmp(*csym);
4711 next();
4712 skip(';');
4713 } else if (tok == TOK_FOR) {
4714 int e;
4715 next();
4716 skip('(');
4717 s = local_stack;
4718 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4719 frame_bottom->next = scope_stack_bottom;
4720 scope_stack_bottom = frame_bottom;
4721 if (tok != ';') {
4722 /* c99 for-loop init decl? */
4723 if (!decl0(VT_LOCAL, 1)) {
4724 /* no, regular for-loop init expr */
4725 gexpr();
4726 vpop();
4729 skip(';');
4730 d = ind;
4731 c = ind;
4732 a = 0;
4733 b = 0;
4734 if (tok != ';') {
4735 gexpr();
4736 a = gvtst(1, 0);
4738 skip(';');
4739 if (tok != ')') {
4740 e = gjmp(0);
4741 c = ind;
4742 gexpr();
4743 vpop();
4744 gjmp_addr(d);
4745 gsym(e);
4747 skip(')');
4748 block(&a, &b, case_sym, def_sym, case_reg, 0);
4749 gjmp_addr(c);
4750 gsym(a);
4751 gsym_addr(b, c);
4752 scope_stack_bottom = scope_stack_bottom->next;
4753 sym_pop(&local_stack, s);
4754 } else
4755 if (tok == TOK_DO) {
4756 next();
4757 a = 0;
4758 b = 0;
4759 d = ind;
4760 block(&a, &b, case_sym, def_sym, case_reg, 0);
4761 skip(TOK_WHILE);
4762 skip('(');
4763 gsym(b);
4764 gexpr();
4765 c = gvtst(0, 0);
4766 gsym_addr(c, d);
4767 skip(')');
4768 gsym(a);
4769 skip(';');
4770 } else
4771 if (tok == TOK_SWITCH) {
4772 next();
4773 skip('(');
4774 gexpr();
4775 /* XXX: other types than integer */
4776 case_reg = gv(RC_INT);
4777 vpop();
4778 skip(')');
4779 a = 0;
4780 b = gjmp(0); /* jump to first case */
4781 c = 0;
4782 block(&a, csym, &b, &c, case_reg, 0);
4783 /* if no default, jmp after switch */
4784 if (c == 0)
4785 c = ind;
4786 /* default label */
4787 gsym_addr(b, c);
4788 /* break label */
4789 gsym(a);
4790 } else
4791 if (tok == TOK_CASE) {
4792 int v1, v2;
4793 if (!case_sym)
4794 expect("switch");
4795 next();
4796 v1 = expr_const();
4797 v2 = v1;
4798 if (gnu_ext && tok == TOK_DOTS) {
4799 next();
4800 v2 = expr_const();
4801 if (v2 < v1)
4802 tcc_warning("empty case range");
4804 /* since a case is like a label, we must skip it with a jmp */
4805 b = gjmp(0);
4806 gsym(*case_sym);
4807 vseti(case_reg, 0);
4808 vpushi(v1);
4809 if (v1 == v2) {
4810 gen_op(TOK_EQ);
4811 *case_sym = gtst(1, 0);
4812 } else {
4813 gen_op(TOK_GE);
4814 *case_sym = gtst(1, 0);
4815 vseti(case_reg, 0);
4816 vpushi(v2);
4817 gen_op(TOK_LE);
4818 *case_sym = gtst(1, *case_sym);
4820 gsym(b);
4821 skip(':');
4822 is_expr = 0;
4823 goto block_after_label;
4824 } else
4825 if (tok == TOK_DEFAULT) {
4826 next();
4827 skip(':');
4828 if (!def_sym)
4829 expect("switch");
4830 if (*def_sym)
4831 tcc_error("too many 'default'");
4832 *def_sym = ind;
4833 is_expr = 0;
4834 goto block_after_label;
4835 } else
4836 if (tok == TOK_GOTO) {
4837 next();
4838 if (tok == '*' && gnu_ext) {
4839 /* computed goto */
4840 next();
4841 gexpr();
4842 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4843 expect("pointer");
4844 ggoto();
4845 } else if (tok >= TOK_UIDENT) {
4846 s = label_find(tok);
4847 /* put forward definition if needed */
4848 if (!s) {
4849 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4850 } else {
4851 if (s->r == LABEL_DECLARED)
4852 s->r = LABEL_FORWARD;
4854 /* label already defined */
4855 if (vla_flags & VLA_IN_SCOPE) {
4856 /* If VLAs are in use, save the current stack pointer and
4857 reset the stack pointer to what it was at function entry
4858 (label will restore stack pointer in inner scopes) */
4859 vla_sp_save();
4860 gen_vla_sp_restore(vla_sp_root_loc);
4862 if (s->r & LABEL_FORWARD)
4863 s->jnext = gjmp(s->jnext);
4864 else
4865 gjmp_addr(s->jnext);
4866 next();
4867 } else {
4868 expect("label identifier");
4870 skip(';');
4871 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4872 asm_instr();
4873 } else {
4874 b = is_label();
4875 if (b) {
4876 /* label case */
4877 if (vla_flags & VLA_IN_SCOPE) {
4878 /* save/restore stack pointer across label
4879 this is a no-op when combined with the load immediately
4880 after the label unless we arrive via goto */
4881 vla_sp_save();
4883 s = label_find(b);
4884 if (s) {
4885 if (s->r == LABEL_DEFINED)
4886 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4887 gsym(s->jnext);
4888 s->r = LABEL_DEFINED;
4889 } else {
4890 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4892 s->jnext = ind;
4893 if (vla_flags & VLA_IN_SCOPE) {
4894 gen_vla_sp_restore(*vla_sp_loc);
4895 vla_flags |= VLA_NEED_NEW_FRAME;
4897 /* we accept this, but it is a mistake */
4898 block_after_label:
4899 if (tok == '}') {
4900 tcc_warning("deprecated use of label at end of compound statement");
4901 } else {
4902 if (is_expr)
4903 vpop();
4904 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4906 } else {
4907 /* expression case */
4908 if (tok != ';') {
4909 if (is_expr) {
4910 vpop();
4911 gexpr();
4912 } else {
4913 gexpr();
4914 vpop();
4917 skip(';');
4922 /* t is the array or struct type. c is the array or struct
4923 address. cur_index/cur_field is the pointer to the current
4924 value. 'size_only' is true if only size info is needed (only used
4925 in arrays) */
4926 static void decl_designator(CType *type, Section *sec, unsigned long c,
4927 int *cur_index, Sym **cur_field,
4928 int size_only)
4930 Sym *s, *f;
4931 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4932 CType type1;
4934 notfirst = 0;
4935 elem_size = 0;
4936 nb_elems = 1;
4937 if (gnu_ext && (l = is_label()) != 0)
4938 goto struct_field;
4939 while (tok == '[' || tok == '.') {
4940 if (tok == '[') {
4941 if (!(type->t & VT_ARRAY))
4942 expect("array type");
4943 s = type->ref;
4944 next();
4945 index = expr_const();
4946 if (index < 0 || (s->c >= 0 && index >= s->c))
4947 expect("invalid index");
4948 if (tok == TOK_DOTS && gnu_ext) {
4949 next();
4950 index_last = expr_const();
4951 if (index_last < 0 ||
4952 (s->c >= 0 && index_last >= s->c) ||
4953 index_last < index)
4954 expect("invalid index");
4955 } else {
4956 index_last = index;
4958 skip(']');
4959 if (!notfirst)
4960 *cur_index = index_last;
4961 type = pointed_type(type);
4962 elem_size = type_size(type, &align);
4963 c += index * elem_size;
4964 /* NOTE: we only support ranges for last designator */
4965 nb_elems = index_last - index + 1;
4966 if (nb_elems != 1) {
4967 notfirst = 1;
4968 break;
4970 } else {
4971 next();
4972 l = tok;
4973 next();
4974 struct_field:
4975 if ((type->t & VT_BTYPE) != VT_STRUCT)
4976 expect("struct/union type");
4977 s = type->ref;
4978 l |= SYM_FIELD;
4979 f = s->next;
4980 while (f) {
4981 if (f->v == l)
4982 break;
4983 f = f->next;
4985 if (!f)
4986 expect("field");
4987 if (!notfirst)
4988 *cur_field = f;
4989 /* XXX: fix this mess by using explicit storage field */
4990 type1 = f->type;
4991 type1.t |= (type->t & ~VT_TYPE);
4992 type = &type1;
4993 c += f->c;
4995 notfirst = 1;
4997 if (notfirst) {
4998 if (tok == '=') {
4999 next();
5000 } else {
5001 if (!gnu_ext)
5002 expect("=");
5004 } else {
5005 if (type->t & VT_ARRAY) {
5006 index = *cur_index;
5007 type = pointed_type(type);
5008 c += index * type_size(type, &align);
5009 } else {
5010 f = *cur_field;
5011 if (!f)
5012 tcc_error("too many field init");
5013 /* XXX: fix this mess by using explicit storage field */
5014 type1 = f->type;
5015 type1.t |= (type->t & ~VT_TYPE);
5016 type = &type1;
5017 c += f->c;
5020 decl_initializer(type, sec, c, 0, size_only);
5022 /* XXX: make it more general */
5023 if (!size_only && nb_elems > 1) {
5024 unsigned long c_end;
5025 uint8_t *src, *dst;
5026 int i;
5028 if (!sec)
5029 tcc_error("range init not supported yet for dynamic storage");
5030 c_end = c + nb_elems * elem_size;
5031 if (c_end > sec->data_allocated)
5032 section_realloc(sec, c_end);
5033 src = sec->data + c;
5034 dst = src;
5035 for(i = 1; i < nb_elems; i++) {
5036 dst += elem_size;
5037 memcpy(dst, src, elem_size);
5042 #define EXPR_VAL 0
5043 #define EXPR_CONST 1
5044 #define EXPR_ANY 2
5046 /* store a value or an expression directly in global data or in local array */
5047 static void init_putv(CType *type, Section *sec, unsigned long c,
5048 int v, int expr_type)
5050 int saved_global_expr, bt, bit_pos, bit_size;
5051 void *ptr;
5052 unsigned long long bit_mask;
5053 CType dtype;
5055 switch(expr_type) {
5056 case EXPR_VAL:
5057 vpushi(v);
5058 break;
5059 case EXPR_CONST:
5060 /* compound literals must be allocated globally in this case */
5061 saved_global_expr = global_expr;
5062 global_expr = 1;
5063 expr_const1();
5064 global_expr = saved_global_expr;
5065 /* NOTE: symbols are accepted */
5066 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5067 tcc_error("initializer element is not constant");
5068 break;
5069 case EXPR_ANY:
5070 expr_eq();
5071 break;
5074 dtype = *type;
5075 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5077 if (sec) {
5078 /* XXX: not portable */
5079 /* XXX: generate error if incorrect relocation */
5080 gen_assign_cast(&dtype);
5081 bt = type->t & VT_BTYPE;
5082 /* we'll write at most 12 bytes */
5083 if (c + 12 > sec->data_allocated) {
5084 section_realloc(sec, c + 12);
5086 ptr = sec->data + c;
5087 /* XXX: make code faster ? */
5088 if (!(type->t & VT_BITFIELD)) {
5089 bit_pos = 0;
5090 bit_size = 32;
5091 bit_mask = -1LL;
5092 } else {
5093 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5094 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5095 bit_mask = (1LL << bit_size) - 1;
5097 if ((vtop->r & VT_SYM) &&
5098 (bt == VT_BYTE ||
5099 bt == VT_SHORT ||
5100 bt == VT_DOUBLE ||
5101 bt == VT_LDOUBLE ||
5102 bt == VT_LLONG ||
5103 (bt == VT_INT && bit_size != 32)))
5104 tcc_error("initializer element is not computable at load time");
5105 switch(bt) {
5106 case VT_BOOL:
5107 vtop->c.i = (vtop->c.i != 0);
5108 case VT_BYTE:
5109 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5110 break;
5111 case VT_SHORT:
5112 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5113 break;
5114 case VT_DOUBLE:
5115 *(double *)ptr = vtop->c.d;
5116 break;
5117 case VT_LDOUBLE:
5118 *(long double *)ptr = vtop->c.ld;
5119 break;
5120 case VT_LLONG:
5121 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5122 break;
5123 default:
5124 if (vtop->r & VT_SYM) {
5125 greloc(sec, vtop->sym, c, R_DATA_PTR);
5127 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5128 break;
5130 vtop--;
5131 } else {
5132 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5133 vswap();
5134 vstore();
5135 vpop();
5139 /* put zeros for variable based init */
5140 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5142 if (sec) {
5143 /* nothing to do because globals are already set to zero */
5144 } else {
5145 vpush_global_sym(&func_old_type, TOK_memset);
5146 vseti(VT_LOCAL, c);
5147 vpushi(0);
5148 vpushs(size);
5149 gfunc_call(3);
5153 /* 't' contains the type and storage info. 'c' is the offset of the
5154 object in section 'sec'. If 'sec' is NULL, it means stack based
5155 allocation. 'first' is true if array '{' must be read (multi
5156 dimension implicit array init handling). 'size_only' is true if
5157 size only evaluation is wanted (only for arrays). */
5158 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5159 int first, int size_only)
5161 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5162 int size1, align1, expr_type;
5163 Sym *s, *f;
5164 CType *t1;
5166 if (type->t & VT_VLA) {
5167 int a;
5169 /* save current stack pointer */
5170 if (vla_flags & VLA_NEED_NEW_FRAME) {
5171 vla_sp_save();
5172 vla_flags = VLA_IN_SCOPE;
5173 vla_sp_loc = &vla_sp_loc_tmp;
5176 vla_runtime_type_size(type, &a);
5177 gen_vla_alloc(type, a);
5178 vset(type, VT_LOCAL|VT_LVAL, c);
5179 vswap();
5180 vstore();
5181 vpop();
5182 } else if (type->t & VT_ARRAY) {
5183 s = type->ref;
5184 n = s->c;
5185 array_length = 0;
5186 t1 = pointed_type(type);
5187 size1 = type_size(t1, &align1);
5189 no_oblock = 1;
5190 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5191 tok == '{') {
5192 if (tok != '{')
5193 tcc_error("character array initializer must be a literal,"
5194 " optionally enclosed in braces");
5195 skip('{');
5196 no_oblock = 0;
5199 /* only parse strings here if correct type (otherwise: handle
5200 them as ((w)char *) expressions */
5201 if ((tok == TOK_LSTR &&
5202 #ifdef TCC_TARGET_PE
5203 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5204 #else
5205 (t1->t & VT_BTYPE) == VT_INT
5206 #endif
5207 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5208 while (tok == TOK_STR || tok == TOK_LSTR) {
5209 int cstr_len, ch;
5210 CString *cstr;
5212 cstr = tokc.cstr;
5213 /* compute maximum number of chars wanted */
5214 if (tok == TOK_STR)
5215 cstr_len = cstr->size;
5216 else
5217 cstr_len = cstr->size / sizeof(nwchar_t);
5218 cstr_len--;
5219 nb = cstr_len;
5220 if (n >= 0 && nb > (n - array_length))
5221 nb = n - array_length;
5222 if (!size_only) {
5223 if (cstr_len > nb)
5224 tcc_warning("initializer-string for array is too long");
5225 /* in order to go faster for common case (char
5226 string in global variable, we handle it
5227 specifically */
5228 if (sec && tok == TOK_STR && size1 == 1) {
5229 memcpy(sec->data + c + array_length, cstr->data, nb);
5230 } else {
5231 for(i=0;i<nb;i++) {
5232 if (tok == TOK_STR)
5233 ch = ((unsigned char *)cstr->data)[i];
5234 else
5235 ch = ((nwchar_t *)cstr->data)[i];
5236 init_putv(t1, sec, c + (array_length + i) * size1,
5237 ch, EXPR_VAL);
5241 array_length += nb;
5242 next();
5244 /* only add trailing zero if enough storage (no
5245 warning in this case since it is standard) */
5246 if (n < 0 || array_length < n) {
5247 if (!size_only) {
5248 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5250 array_length++;
5252 } else {
5253 index = 0;
5254 while (tok != '}') {
5255 decl_designator(type, sec, c, &index, NULL, size_only);
5256 if (n >= 0 && index >= n)
5257 tcc_error("index too large");
5258 /* must put zero in holes (note that doing it that way
5259 ensures that it even works with designators) */
5260 if (!size_only && array_length < index) {
5261 init_putz(t1, sec, c + array_length * size1,
5262 (index - array_length) * size1);
5264 index++;
5265 if (index > array_length)
5266 array_length = index;
5267 /* special test for multi dimensional arrays (may not
5268 be strictly correct if designators are used at the
5269 same time) */
5270 if (index >= n && no_oblock)
5271 break;
5272 if (tok == '}')
5273 break;
5274 skip(',');
5277 if (!no_oblock)
5278 skip('}');
5279 /* put zeros at the end */
5280 if (!size_only && n >= 0 && array_length < n) {
5281 init_putz(t1, sec, c + array_length * size1,
5282 (n - array_length) * size1);
5284 /* patch type size if needed */
5285 if (n < 0)
5286 s->c = array_length;
5287 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5288 (sec || !first || tok == '{')) {
5289 int par_count;
5291 /* NOTE: the previous test is a specific case for automatic
5292 struct/union init */
5293 /* XXX: union needs only one init */
5295 /* XXX: this test is incorrect for local initializers
5296 beginning with ( without {. It would be much more difficult
5297 to do it correctly (ideally, the expression parser should
5298 be used in all cases) */
5299 par_count = 0;
5300 if (tok == '(') {
5301 AttributeDef ad1;
5302 CType type1;
5303 next();
5304 while (tok == '(') {
5305 par_count++;
5306 next();
5308 if (!parse_btype(&type1, &ad1))
5309 expect("cast");
5310 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5311 #if 0
5312 if (!is_assignable_types(type, &type1))
5313 tcc_error("invalid type for cast");
5314 #endif
5315 skip(')');
5317 no_oblock = 1;
5318 if (first || tok == '{') {
5319 skip('{');
5320 no_oblock = 0;
5322 s = type->ref;
5323 f = s->next;
5324 array_length = 0;
5325 index = 0;
5326 n = s->c;
5327 while (tok != '}') {
5328 decl_designator(type, sec, c, NULL, &f, size_only);
5329 index = f->c;
5330 if (!size_only && array_length < index) {
5331 init_putz(type, sec, c + array_length,
5332 index - array_length);
5334 index = index + type_size(&f->type, &align1);
5335 if (index > array_length)
5336 array_length = index;
5338 /* gr: skip fields from same union - ugly. */
5339 while (f->next) {
5340 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5341 /* test for same offset */
5342 if (f->next->c != f->c)
5343 break;
5344 /* if yes, test for bitfield shift */
5345 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5346 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5347 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5348 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5349 if (bit_pos_1 != bit_pos_2)
5350 break;
5352 f = f->next;
5355 f = f->next;
5356 if (no_oblock && f == NULL)
5357 break;
5358 if (tok == '}')
5359 break;
5360 skip(',');
5362 /* put zeros at the end */
5363 if (!size_only && array_length < n) {
5364 init_putz(type, sec, c + array_length,
5365 n - array_length);
5367 if (!no_oblock)
5368 skip('}');
5369 while (par_count) {
5370 skip(')');
5371 par_count--;
5373 } else if (tok == '{') {
5374 next();
5375 decl_initializer(type, sec, c, first, size_only);
5376 skip('}');
5377 } else if (size_only) {
5378 /* just skip expression */
5379 parlevel = parlevel1 = 0;
5380 while ((parlevel > 0 || parlevel1 > 0 ||
5381 (tok != '}' && tok != ',')) && tok != -1) {
5382 if (tok == '(')
5383 parlevel++;
5384 else if (tok == ')')
5385 parlevel--;
5386 else if (tok == '{')
5387 parlevel1++;
5388 else if (tok == '}')
5389 parlevel1--;
5390 next();
5392 } else {
5393 /* currently, we always use constant expression for globals
5394 (may change for scripting case) */
5395 expr_type = EXPR_CONST;
5396 if (!sec)
5397 expr_type = EXPR_ANY;
5398 init_putv(type, sec, c, 0, expr_type);
5402 /* parse an initializer for type 't' if 'has_init' is non zero, and
5403 allocate space in local or global data space ('r' is either
5404 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5405 variable 'v' with an associated name represented by 'asm_label' of
5406 scope 'scope' is declared before initializers are parsed. If 'v' is
5407 zero, then a reference to the new object is put in the value stack.
5408 If 'has_init' is 2, a special parsing is done to handle string
5409 constants. */
5410 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5411 int has_init, int v, char *asm_label,
5412 int scope)
5414 int size, align, addr, data_offset;
5415 int level;
5416 ParseState saved_parse_state = {0};
5417 TokenString init_str;
5418 Section *sec;
5419 Sym *flexible_array;
5421 flexible_array = NULL;
5422 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5423 Sym *field = type->ref->next;
5424 if (field) {
5425 while (field->next)
5426 field = field->next;
5427 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5428 flexible_array = field;
5432 size = type_size(type, &align);
5433 /* If unknown size, we must evaluate it before
5434 evaluating initializers because
5435 initializers can generate global data too
5436 (e.g. string pointers or ISOC99 compound
5437 literals). It also simplifies local
5438 initializers handling */
5439 tok_str_new(&init_str);
5440 if (size < 0 || (flexible_array && has_init)) {
5441 if (!has_init)
5442 tcc_error("unknown type size");
5443 /* get all init string */
5444 if (has_init == 2) {
5445 /* only get strings */
5446 while (tok == TOK_STR || tok == TOK_LSTR) {
5447 tok_str_add_tok(&init_str);
5448 next();
5450 } else {
5451 level = 0;
5452 while (level > 0 || (tok != ',' && tok != ';')) {
5453 if (tok < 0)
5454 tcc_error("unexpected end of file in initializer");
5455 tok_str_add_tok(&init_str);
5456 if (tok == '{')
5457 level++;
5458 else if (tok == '}') {
5459 level--;
5460 if (level <= 0) {
5461 next();
5462 break;
5465 next();
5468 tok_str_add(&init_str, -1);
5469 tok_str_add(&init_str, 0);
5471 /* compute size */
5472 save_parse_state(&saved_parse_state);
5474 macro_ptr = init_str.str;
5475 next();
5476 decl_initializer(type, NULL, 0, 1, 1);
5477 /* prepare second initializer parsing */
5478 macro_ptr = init_str.str;
5479 next();
5481 /* if still unknown size, error */
5482 size = type_size(type, &align);
5483 if (size < 0)
5484 tcc_error("unknown type size");
5486 if (flexible_array)
5487 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5488 /* take into account specified alignment if bigger */
5489 if (ad->a.aligned) {
5490 if (ad->a.aligned > align)
5491 align = ad->a.aligned;
5492 } else if (ad->a.packed) {
5493 align = 1;
5495 if ((r & VT_VALMASK) == VT_LOCAL) {
5496 sec = NULL;
5497 #ifdef CONFIG_TCC_BCHECK
5498 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5499 loc--;
5501 #endif
5502 loc = (loc - size) & -align;
5503 addr = loc;
5504 #ifdef CONFIG_TCC_BCHECK
5505 /* handles bounds */
5506 /* XXX: currently, since we do only one pass, we cannot track
5507 '&' operators, so we add only arrays */
5508 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5509 unsigned long *bounds_ptr;
5510 /* add padding between regions */
5511 loc--;
5512 /* then add local bound info */
5513 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5514 bounds_ptr[0] = addr;
5515 bounds_ptr[1] = size;
5517 #endif
5518 if (v) {
5519 /* local variable */
5520 sym_push(v, type, r, addr);
5521 } else {
5522 /* push local reference */
5523 vset(type, r, addr);
5525 } else {
5526 Sym *sym;
5528 sym = NULL;
5529 if (v && scope == VT_CONST) {
5530 /* see if the symbol was already defined */
5531 sym = sym_find(v);
5532 if (sym) {
5533 if (!is_compatible_types(&sym->type, type))
5534 tcc_error("incompatible types for redefinition of '%s'",
5535 get_tok_str(v, NULL));
5536 if (sym->type.t & VT_EXTERN) {
5537 /* if the variable is extern, it was not allocated */
5538 sym->type.t &= ~VT_EXTERN;
5539 /* set array size if it was ommited in extern
5540 declaration */
5541 if ((sym->type.t & VT_ARRAY) &&
5542 sym->type.ref->c < 0 &&
5543 type->ref->c >= 0)
5544 sym->type.ref->c = type->ref->c;
5545 } else {
5546 /* we accept several definitions of the same
5547 global variable. this is tricky, because we
5548 must play with the SHN_COMMON type of the symbol */
5549 /* XXX: should check if the variable was already
5550 initialized. It is incorrect to initialized it
5551 twice */
5552 /* no init data, we won't add more to the symbol */
5553 if (!has_init)
5554 goto no_alloc;
5559 /* allocate symbol in corresponding section */
5560 sec = ad->section;
5561 if (!sec) {
5562 if (has_init)
5563 sec = data_section;
5564 else if (tcc_state->nocommon)
5565 sec = bss_section;
5567 if (sec) {
5568 data_offset = sec->data_offset;
5569 data_offset = (data_offset + align - 1) & -align;
5570 addr = data_offset;
5571 /* very important to increment global pointer at this time
5572 because initializers themselves can create new initializers */
5573 data_offset += size;
5574 #ifdef CONFIG_TCC_BCHECK
5575 /* add padding if bound check */
5576 if (tcc_state->do_bounds_check)
5577 data_offset++;
5578 #endif
5579 sec->data_offset = data_offset;
5580 /* allocate section space to put the data */
5581 if (sec->sh_type != SHT_NOBITS &&
5582 data_offset > sec->data_allocated)
5583 section_realloc(sec, data_offset);
5584 /* align section if needed */
5585 if (align > sec->sh_addralign)
5586 sec->sh_addralign = align;
5587 } else {
5588 addr = 0; /* avoid warning */
5591 if (v) {
5592 if (scope != VT_CONST || !sym) {
5593 sym = sym_push(v, type, r | VT_SYM, 0);
5594 sym->asm_label = asm_label;
5596 /* update symbol definition */
5597 if (sec) {
5598 put_extern_sym(sym, sec, addr, size);
5599 } else {
5600 ElfW(Sym) *esym;
5601 /* put a common area */
5602 put_extern_sym(sym, NULL, align, size);
5603 /* XXX: find a nicer way */
5604 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5605 esym->st_shndx = SHN_COMMON;
5607 } else {
5608 CValue cval;
5610 /* push global reference */
5611 sym = get_sym_ref(type, sec, addr, size);
5612 cval.ul = 0;
5613 vsetc(type, VT_CONST | VT_SYM, &cval);
5614 vtop->sym = sym;
5616 /* patch symbol weakness */
5617 if (type->t & VT_WEAK)
5618 weaken_symbol(sym);
5619 #ifdef CONFIG_TCC_BCHECK
5620 /* handles bounds now because the symbol must be defined
5621 before for the relocation */
5622 if (tcc_state->do_bounds_check) {
5623 unsigned long *bounds_ptr;
5625 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5626 /* then add global bound info */
5627 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5628 bounds_ptr[0] = 0; /* relocated */
5629 bounds_ptr[1] = size;
5631 #endif
5633 if (has_init || (type->t & VT_VLA)) {
5634 decl_initializer(type, sec, addr, 1, 0);
5635 /* restore parse state if needed */
5636 if (init_str.str) {
5637 tok_str_free(init_str.str);
5638 restore_parse_state(&saved_parse_state);
5640 /* patch flexible array member size back to -1, */
5641 /* for possible subsequent similar declarations */
5642 if (flexible_array)
5643 flexible_array->type.ref->c = -1;
5645 no_alloc: ;
5648 static void put_func_debug(Sym *sym)
5650 char buf[512];
5652 /* stabs info */
5653 /* XXX: we put here a dummy type */
5654 snprintf(buf, sizeof(buf), "%s:%c1",
5655 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5656 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5657 cur_text_section, sym->c);
5658 /* //gr gdb wants a line at the function */
5659 put_stabn(N_SLINE, 0, file->line_num, 0);
5660 last_ind = 0;
5661 last_line_num = 0;
5664 /* parse an old style function declaration list */
5665 /* XXX: check multiple parameter */
5666 static void func_decl_list(Sym *func_sym)
5668 AttributeDef ad;
5669 int v;
5670 Sym *s;
5671 CType btype, type;
5673 /* parse each declaration */
5674 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5675 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5676 if (!parse_btype(&btype, &ad))
5677 expect("declaration list");
5678 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5679 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5680 tok == ';') {
5681 /* we accept no variable after */
5682 } else {
5683 for(;;) {
5684 type = btype;
5685 type_decl(&type, &ad, &v, TYPE_DIRECT);
5686 /* find parameter in function parameter list */
5687 s = func_sym->next;
5688 while (s != NULL) {
5689 if ((s->v & ~SYM_FIELD) == v)
5690 goto found;
5691 s = s->next;
5693 tcc_error("declaration for parameter '%s' but no such parameter",
5694 get_tok_str(v, NULL));
5695 found:
5696 /* check that no storage specifier except 'register' was given */
5697 if (type.t & VT_STORAGE)
5698 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5699 convert_parameter_type(&type);
5700 /* we can add the type (NOTE: it could be local to the function) */
5701 s->type = type;
5702 /* accept other parameters */
5703 if (tok == ',')
5704 next();
5705 else
5706 break;
5709 skip(';');
5713 /* parse a function defined by symbol 'sym' and generate its code in
5714 'cur_text_section' */
5715 static void gen_function(Sym *sym)
5717 int saved_nocode_wanted = nocode_wanted;
5718 nocode_wanted = 0;
5719 ind = cur_text_section->data_offset;
5720 /* NOTE: we patch the symbol size later */
5721 put_extern_sym(sym, cur_text_section, ind, 0);
5722 funcname = get_tok_str(sym->v, NULL);
5723 func_ind = ind;
5724 /* Initialize VLA state */
5725 vla_sp_loc = &vla_sp_root_loc;
5726 vla_flags = VLA_NEED_NEW_FRAME;
5727 /* put debug symbol */
5728 if (tcc_state->do_debug)
5729 put_func_debug(sym);
5730 /* push a dummy symbol to enable local sym storage */
5731 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5732 gfunc_prolog(&sym->type);
5733 rsym = 0;
5734 block(NULL, NULL, NULL, NULL, 0, 0);
5735 gsym(rsym);
5736 gfunc_epilog();
5737 cur_text_section->data_offset = ind;
5738 label_pop(&global_label_stack, NULL);
5739 /* reset local stack */
5740 scope_stack_bottom = NULL;
5741 sym_pop(&local_stack, NULL);
5742 /* end of function */
5743 /* patch symbol size */
5744 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5745 ind - func_ind;
5746 /* patch symbol weakness (this definition overrules any prototype) */
5747 if (sym->type.t & VT_WEAK)
5748 weaken_symbol(sym);
5749 if (tcc_state->do_debug) {
5750 put_stabn(N_FUN, 0, 0, ind - func_ind);
5752 /* It's better to crash than to generate wrong code */
5753 cur_text_section = NULL;
5754 funcname = ""; /* for safety */
5755 func_vt.t = VT_VOID; /* for safety */
5756 func_var = 0; /* for safety */
5757 ind = 0; /* for safety */
5758 nocode_wanted = saved_nocode_wanted;
5761 ST_FUNC void gen_inline_functions(void)
5763 Sym *sym;
5764 int *str, inline_generated, i;
5765 struct InlineFunc *fn;
5767 /* iterate while inline function are referenced */
5768 for(;;) {
5769 inline_generated = 0;
5770 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5771 fn = tcc_state->inline_fns[i];
5772 sym = fn->sym;
5773 if (sym && sym->c) {
5774 /* the function was used: generate its code and
5775 convert it to a normal function */
5776 str = fn->token_str;
5777 fn->sym = NULL;
5778 if (file)
5779 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5780 sym->r = VT_SYM | VT_CONST;
5781 sym->type.t &= ~VT_INLINE;
5783 macro_ptr = str;
5784 next();
5785 cur_text_section = text_section;
5786 gen_function(sym);
5787 macro_ptr = NULL; /* fail safe */
5789 inline_generated = 1;
5792 if (!inline_generated)
5793 break;
5795 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5796 fn = tcc_state->inline_fns[i];
5797 str = fn->token_str;
5798 tok_str_free(str);
5800 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5803 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5804 static int decl0(int l, int is_for_loop_init)
5806 int v, has_init, r;
5807 CType type, btype;
5808 Sym *sym;
5809 AttributeDef ad;
5811 while (1) {
5812 if (!parse_btype(&btype, &ad)) {
5813 if (is_for_loop_init)
5814 return 0;
5815 /* skip redundant ';' */
5816 /* XXX: find more elegant solution */
5817 if (tok == ';') {
5818 next();
5819 continue;
5821 if (l == VT_CONST &&
5822 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5823 /* global asm block */
5824 asm_global_instr();
5825 continue;
5827 /* special test for old K&R protos without explicit int
5828 type. Only accepted when defining global data */
5829 if (l == VT_LOCAL || tok < TOK_DEFINE)
5830 break;
5831 btype.t = VT_INT;
5833 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5834 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5835 tok == ';') {
5836 /* we accept no variable after */
5837 next();
5838 continue;
5840 while (1) { /* iterate thru each declaration */
5841 char *asm_label; // associated asm label
5842 type = btype;
5843 type_decl(&type, &ad, &v, TYPE_DIRECT);
5844 #if 0
5846 char buf[500];
5847 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5848 printf("type = '%s'\n", buf);
5850 #endif
5851 if ((type.t & VT_BTYPE) == VT_FUNC) {
5852 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5853 tcc_error("function without file scope cannot be static");
5855 /* if old style function prototype, we accept a
5856 declaration list */
5857 sym = type.ref;
5858 if (sym->c == FUNC_OLD)
5859 func_decl_list(sym);
5862 asm_label = NULL;
5863 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5864 CString astr;
5866 asm_label_instr(&astr);
5867 asm_label = tcc_strdup(astr.data);
5868 cstr_free(&astr);
5870 /* parse one last attribute list, after asm label */
5871 parse_attribute(&ad);
5874 if (ad.a.weak)
5875 type.t |= VT_WEAK;
5876 #ifdef TCC_TARGET_PE
5877 if (ad.a.func_import)
5878 type.t |= VT_IMPORT;
5879 if (ad.a.func_export)
5880 type.t |= VT_EXPORT;
5881 #endif
5882 if (tok == '{') {
5883 if (l == VT_LOCAL)
5884 tcc_error("cannot use local functions");
5885 if ((type.t & VT_BTYPE) != VT_FUNC)
5886 expect("function definition");
5888 /* reject abstract declarators in function definition */
5889 sym = type.ref;
5890 while ((sym = sym->next) != NULL)
5891 if (!(sym->v & ~SYM_FIELD))
5892 expect("identifier");
5894 /* XXX: cannot do better now: convert extern line to static inline */
5895 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5896 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5898 sym = sym_find(v);
5899 if (sym) {
5900 Sym *ref;
5901 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5902 goto func_error1;
5904 ref = sym->type.ref;
5905 if (0 == ref->a.func_proto)
5906 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5908 /* use func_call from prototype if not defined */
5909 if (ref->a.func_call != FUNC_CDECL
5910 && type.ref->a.func_call == FUNC_CDECL)
5911 type.ref->a.func_call = ref->a.func_call;
5913 /* use export from prototype */
5914 if (ref->a.func_export)
5915 type.ref->a.func_export = 1;
5917 /* use static from prototype */
5918 if (sym->type.t & VT_STATIC)
5919 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5921 if (!is_compatible_types(&sym->type, &type)) {
5922 func_error1:
5923 tcc_error("incompatible types for redefinition of '%s'",
5924 get_tok_str(v, NULL));
5926 type.ref->a.func_proto = 0;
5927 /* if symbol is already defined, then put complete type */
5928 sym->type = type;
5929 } else {
5930 /* put function symbol */
5931 sym = global_identifier_push(v, type.t, 0);
5932 sym->type.ref = type.ref;
5935 /* static inline functions are just recorded as a kind
5936 of macro. Their code will be emitted at the end of
5937 the compilation unit only if they are used */
5938 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5939 (VT_INLINE | VT_STATIC)) {
5940 TokenString func_str;
5941 int block_level;
5942 struct InlineFunc *fn;
5943 const char *filename;
5945 tok_str_new(&func_str);
5947 block_level = 0;
5948 for(;;) {
5949 int t;
5950 if (tok == TOK_EOF)
5951 tcc_error("unexpected end of file");
5952 tok_str_add_tok(&func_str);
5953 t = tok;
5954 next();
5955 if (t == '{') {
5956 block_level++;
5957 } else if (t == '}') {
5958 block_level--;
5959 if (block_level == 0)
5960 break;
5963 tok_str_add(&func_str, -1);
5964 tok_str_add(&func_str, 0);
5965 filename = file ? file->filename : "";
5966 fn = tcc_malloc(sizeof *fn + strlen(filename));
5967 strcpy(fn->filename, filename);
5968 fn->sym = sym;
5969 fn->token_str = func_str.str;
5970 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5972 } else {
5973 /* compute text section */
5974 cur_text_section = ad.section;
5975 if (!cur_text_section)
5976 cur_text_section = text_section;
5977 sym->r = VT_SYM | VT_CONST;
5978 gen_function(sym);
5980 break;
5981 } else {
5982 if (btype.t & VT_TYPEDEF) {
5983 /* save typedefed type */
5984 /* XXX: test storage specifiers ? */
5985 sym = sym_push(v, &type, 0, 0);
5986 sym->a = ad.a;
5987 sym->type.t |= VT_TYPEDEF;
5988 } else {
5989 r = 0;
5990 if ((type.t & VT_BTYPE) == VT_FUNC) {
5991 /* external function definition */
5992 /* specific case for func_call attribute */
5993 ad.a.func_proto = 1;
5994 type.ref->a = ad.a;
5995 } else if (!(type.t & VT_ARRAY)) {
5996 /* not lvalue if array */
5997 r |= lvalue_type(type.t);
5999 has_init = (tok == '=');
6000 if (has_init && (type.t & VT_VLA))
6001 tcc_error("Variable length array cannot be initialized");
6002 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6003 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6004 !has_init && l == VT_CONST && type.ref->c < 0)) {
6005 /* external variable or function */
6006 /* NOTE: as GCC, uninitialized global static
6007 arrays of null size are considered as
6008 extern */
6009 sym = external_sym(v, &type, r, asm_label);
6011 if (type.t & VT_WEAK)
6012 weaken_symbol(sym);
6014 if (ad.alias_target) {
6015 Section tsec;
6016 Elf32_Sym *esym;
6017 Sym *alias_target;
6019 alias_target = sym_find(ad.alias_target);
6020 if (!alias_target || !alias_target->c)
6021 tcc_error("unsupported forward __alias__ attribute");
6022 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6023 tsec.sh_num = esym->st_shndx;
6024 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6026 } else {
6027 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6028 if (type.t & VT_STATIC)
6029 r |= VT_CONST;
6030 else
6031 r |= l;
6032 if (has_init)
6033 next();
6034 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6037 if (tok != ',') {
6038 if (is_for_loop_init)
6039 return 1;
6040 skip(';');
6041 break;
6043 next();
6045 ad.a.aligned = 0;
6048 return 0;
6051 ST_FUNC void decl(int l)
6053 decl0(l, 0);