Fixes for PE x86_64 for fail in code
[tinycc.git] / tccgen.c
blob99cab530a3a385cfc0ee13b4634fea367aed407d
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 = {0};
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;
1958 #ifdef TCC_TARGET_X86_64
1959 if (!(dbt & (VT_PTR|VT_LLONG|VT_FUNC|VT_STRUCT)))
1960 #endif
1961 if(dbt & VT_UNSIGNED)
1962 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1963 else
1964 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1967 } else if (p && dbt == VT_BOOL) {
1968 vtop->r = VT_CONST;
1969 vtop->c.i = 1;
1970 } else if (!nocode_wanted) {
1971 /* non constant case: generate code */
1972 if (sf && df) {
1973 /* convert from fp to fp */
1974 gen_cvt_ftof(dbt);
1975 } else if (df) {
1976 /* convert int to fp */
1977 gen_cvt_itof1(dbt);
1978 } else if (sf) {
1979 /* convert fp to int */
1980 if (dbt == VT_BOOL) {
1981 vpushi(0);
1982 gen_op(TOK_NE);
1983 } else {
1984 /* we handle char/short/etc... with generic code */
1985 if (dbt != (VT_INT | VT_UNSIGNED) &&
1986 dbt != (VT_LLONG | VT_UNSIGNED) &&
1987 dbt != VT_LLONG)
1988 dbt = VT_INT;
1989 gen_cvt_ftoi1(dbt);
1990 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1991 /* additional cast for char/short... */
1992 vtop->type.t = dbt;
1993 gen_cast(type);
1996 #ifndef TCC_TARGET_X86_64
1997 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1998 if ((sbt & VT_BTYPE) != VT_LLONG) {
1999 /* scalar to long long */
2000 /* machine independent conversion */
2001 gv(RC_INT);
2002 /* generate high word */
2003 if (sbt == (VT_INT | VT_UNSIGNED)) {
2004 vpushi(0);
2005 gv(RC_INT);
2006 } else {
2007 if (sbt == VT_PTR) {
2008 /* cast from pointer to int before we apply
2009 shift operation, which pointers don't support*/
2010 gen_cast(&int_type);
2012 gv_dup();
2013 vpushi(31);
2014 gen_op(TOK_SAR);
2016 /* patch second register */
2017 vtop[-1].r2 = vtop->r;
2018 vpop();
2020 #else
2021 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2022 (dbt & VT_BTYPE) == VT_PTR ||
2023 (dbt & VT_BTYPE) == VT_FUNC) {
2024 if ((sbt & VT_BTYPE) != VT_LLONG &&
2025 (sbt & VT_BTYPE) != VT_PTR &&
2026 (sbt & VT_BTYPE) != VT_FUNC) {
2027 /* need to convert from 32bit to 64bit */
2028 int r = gv(RC_INT);
2029 if (sbt != (VT_INT | VT_UNSIGNED)) {
2030 /* x86_64 specific: movslq */
2031 o(0x6348);
2032 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2035 #endif
2036 } else if (dbt == VT_BOOL) {
2037 /* scalar to bool */
2038 vpushi(0);
2039 gen_op(TOK_NE);
2040 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2041 (dbt & VT_BTYPE) == VT_SHORT) {
2042 if (sbt == VT_PTR) {
2043 vtop->type.t = VT_INT;
2044 tcc_warning("nonportable conversion from pointer to char/short");
2046 force_charshort_cast(dbt);
2047 } else if ((dbt & VT_BTYPE) == VT_INT) {
2048 /* scalar to int */
2049 if (sbt == VT_LLONG) {
2050 /* from long long: just take low order word */
2051 lexpand();
2052 vpop();
2054 /* if lvalue and single word type, nothing to do because
2055 the lvalue already contains the real type size (see
2056 VT_LVAL_xxx constants) */
2059 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2060 /* if we are casting between pointer types,
2061 we must update the VT_LVAL_xxx size */
2062 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2063 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2065 vtop->type = *type;
2068 /* return type size as known at compile time. Put alignment at 'a' */
2069 ST_FUNC int type_size(CType *type, int *a)
2071 Sym *s;
2072 int bt;
2074 bt = type->t & VT_BTYPE;
2075 if (bt == VT_STRUCT) {
2076 /* struct/union */
2077 s = type->ref;
2078 *a = s->r;
2079 return s->c;
2080 } else if (bt == VT_PTR) {
2081 if (type->t & VT_ARRAY) {
2082 int ts;
2084 s = type->ref;
2085 ts = type_size(&s->type, a);
2087 if (ts < 0 && s->c < 0)
2088 ts = -ts;
2090 return ts * s->c;
2091 } else {
2092 *a = PTR_SIZE;
2093 return PTR_SIZE;
2095 } else if (bt == VT_LDOUBLE) {
2096 *a = LDOUBLE_ALIGN;
2097 return LDOUBLE_SIZE;
2098 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2099 #ifdef TCC_TARGET_I386
2100 #ifdef TCC_TARGET_PE
2101 *a = 8;
2102 #else
2103 *a = 4;
2104 #endif
2105 #elif defined(TCC_TARGET_ARM)
2106 #ifdef TCC_ARM_EABI
2107 *a = 8;
2108 #else
2109 *a = 4;
2110 #endif
2111 #else
2112 *a = 8;
2113 #endif
2114 return 8;
2115 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2116 *a = 4;
2117 return 4;
2118 } else if (bt == VT_SHORT) {
2119 *a = 2;
2120 return 2;
2121 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2122 *a = 8;
2123 return 16;
2124 } else {
2125 /* char, void, function, _Bool */
2126 *a = 1;
2127 return 1;
2131 /* push type size as known at runtime time on top of value stack. Put
2132 alignment at 'a' */
2133 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2135 if (type->t & VT_VLA) {
2136 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2137 } else {
2138 vpushi(type_size(type, a));
2142 static void vla_sp_save(void) {
2143 if (!(vla_flags & VLA_SP_LOC_SET)) {
2144 *vla_sp_loc = (loc -= PTR_SIZE);
2145 vla_flags |= VLA_SP_LOC_SET;
2147 if (!(vla_flags & VLA_SP_SAVED)) {
2148 gen_vla_sp_save(*vla_sp_loc);
2149 vla_flags |= VLA_SP_SAVED;
2153 /* return the pointed type of t */
2154 static inline CType *pointed_type(CType *type)
2156 return &type->ref->type;
2159 /* modify type so that its it is a pointer to type. */
2160 ST_FUNC void mk_pointer(CType *type)
2162 Sym *s;
2163 s = sym_push(SYM_FIELD, type, 0, -1);
2164 type->t = VT_PTR | (type->t & ~VT_TYPE);
2165 type->ref = s;
2168 /* compare function types. OLD functions match any new functions */
2169 static int is_compatible_func(CType *type1, CType *type2)
2171 Sym *s1, *s2;
2173 s1 = type1->ref;
2174 s2 = type2->ref;
2175 if (!is_compatible_types(&s1->type, &s2->type))
2176 return 0;
2177 /* check func_call */
2178 if (s1->a.func_call != s2->a.func_call)
2179 return 0;
2180 /* XXX: not complete */
2181 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2182 return 1;
2183 if (s1->c != s2->c)
2184 return 0;
2185 while (s1 != NULL) {
2186 if (s2 == NULL)
2187 return 0;
2188 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2189 return 0;
2190 s1 = s1->next;
2191 s2 = s2->next;
2193 if (s2)
2194 return 0;
2195 return 1;
2198 /* return true if type1 and type2 are the same. If unqualified is
2199 true, qualifiers on the types are ignored.
2201 - enums are not checked as gcc __builtin_types_compatible_p ()
2203 static int compare_types(CType *type1, CType *type2, int unqualified)
2205 int bt1, t1, t2;
2207 t1 = type1->t & VT_TYPE;
2208 t2 = type2->t & VT_TYPE;
2209 if (unqualified) {
2210 /* strip qualifiers before comparing */
2211 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2212 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2214 /* XXX: bitfields ? */
2215 if (t1 != t2)
2216 return 0;
2217 /* test more complicated cases */
2218 bt1 = t1 & VT_BTYPE;
2219 if (bt1 == VT_PTR) {
2220 type1 = pointed_type(type1);
2221 type2 = pointed_type(type2);
2222 return is_compatible_types(type1, type2);
2223 } else if (bt1 == VT_STRUCT) {
2224 return (type1->ref == type2->ref);
2225 } else if (bt1 == VT_FUNC) {
2226 return is_compatible_func(type1, type2);
2227 } else {
2228 return 1;
2232 /* return true if type1 and type2 are exactly the same (including
2233 qualifiers).
2235 static int is_compatible_types(CType *type1, CType *type2)
2237 return compare_types(type1,type2,0);
2240 /* return true if type1 and type2 are the same (ignoring qualifiers).
2242 static int is_compatible_parameter_types(CType *type1, CType *type2)
2244 return compare_types(type1,type2,1);
2247 /* print a type. If 'varstr' is not NULL, then the variable is also
2248 printed in the type */
2249 /* XXX: union */
2250 /* XXX: add array and function pointers */
2251 static void type_to_str(char *buf, int buf_size,
2252 CType *type, const char *varstr)
2254 int bt, v, t;
2255 Sym *s, *sa;
2256 char buf1[256];
2257 const char *tstr;
2259 t = type->t & VT_TYPE;
2260 bt = t & VT_BTYPE;
2261 buf[0] = '\0';
2262 if (t & VT_CONSTANT)
2263 pstrcat(buf, buf_size, "const ");
2264 if (t & VT_VOLATILE)
2265 pstrcat(buf, buf_size, "volatile ");
2266 if (t & VT_UNSIGNED)
2267 pstrcat(buf, buf_size, "unsigned ");
2268 switch(bt) {
2269 case VT_VOID:
2270 tstr = "void";
2271 goto add_tstr;
2272 case VT_BOOL:
2273 tstr = "_Bool";
2274 goto add_tstr;
2275 case VT_BYTE:
2276 tstr = "char";
2277 goto add_tstr;
2278 case VT_SHORT:
2279 tstr = "short";
2280 goto add_tstr;
2281 case VT_INT:
2282 tstr = "int";
2283 goto add_tstr;
2284 case VT_LONG:
2285 tstr = "long";
2286 goto add_tstr;
2287 case VT_LLONG:
2288 tstr = "long long";
2289 goto add_tstr;
2290 case VT_FLOAT:
2291 tstr = "float";
2292 goto add_tstr;
2293 case VT_DOUBLE:
2294 tstr = "double";
2295 goto add_tstr;
2296 case VT_LDOUBLE:
2297 tstr = "long double";
2298 add_tstr:
2299 pstrcat(buf, buf_size, tstr);
2300 break;
2301 case VT_ENUM:
2302 case VT_STRUCT:
2303 if (bt == VT_STRUCT)
2304 tstr = "struct ";
2305 else
2306 tstr = "enum ";
2307 pstrcat(buf, buf_size, tstr);
2308 v = type->ref->v & ~SYM_STRUCT;
2309 if (v >= SYM_FIRST_ANOM)
2310 pstrcat(buf, buf_size, "<anonymous>");
2311 else
2312 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2313 break;
2314 case VT_FUNC:
2315 s = type->ref;
2316 type_to_str(buf, buf_size, &s->type, varstr);
2317 pstrcat(buf, buf_size, "(");
2318 sa = s->next;
2319 while (sa != NULL) {
2320 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2321 pstrcat(buf, buf_size, buf1);
2322 sa = sa->next;
2323 if (sa)
2324 pstrcat(buf, buf_size, ", ");
2326 pstrcat(buf, buf_size, ")");
2327 goto no_var;
2328 case VT_PTR:
2329 s = type->ref;
2330 pstrcpy(buf1, sizeof(buf1), "*");
2331 if (varstr)
2332 pstrcat(buf1, sizeof(buf1), varstr);
2333 type_to_str(buf, buf_size, &s->type, buf1);
2334 goto no_var;
2336 if (varstr) {
2337 pstrcat(buf, buf_size, " ");
2338 pstrcat(buf, buf_size, varstr);
2340 no_var: ;
2343 /* verify type compatibility to store vtop in 'dt' type, and generate
2344 casts if needed. */
2345 static void gen_assign_cast(CType *dt)
2347 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2348 char buf1[256], buf2[256];
2349 int dbt, sbt;
2351 st = &vtop->type; /* source type */
2352 dbt = dt->t & VT_BTYPE;
2353 sbt = st->t & VT_BTYPE;
2354 if (sbt == VT_VOID || dbt == VT_VOID)
2355 tcc_error("cannot cast from/to void");
2356 if (dt->t & VT_CONSTANT)
2357 tcc_warning("assignment of read-only location");
2358 switch(dbt) {
2359 case VT_PTR:
2360 /* special cases for pointers */
2361 /* '0' can also be a pointer */
2362 if (is_null_pointer(vtop))
2363 goto type_ok;
2364 /* accept implicit pointer to integer cast with warning */
2365 if (is_integer_btype(sbt)) {
2366 tcc_warning("assignment makes pointer from integer without a cast");
2367 goto type_ok;
2369 type1 = pointed_type(dt);
2370 /* a function is implicitely a function pointer */
2371 if (sbt == VT_FUNC) {
2372 if ((type1->t & VT_BTYPE) != VT_VOID &&
2373 !is_compatible_types(pointed_type(dt), st))
2374 tcc_warning("assignment from incompatible pointer type");
2375 goto type_ok;
2377 if (sbt != VT_PTR)
2378 goto error;
2379 type2 = pointed_type(st);
2380 if ((type1->t & VT_BTYPE) == VT_VOID ||
2381 (type2->t & VT_BTYPE) == VT_VOID) {
2382 /* void * can match anything */
2383 } else {
2384 /* exact type match, except for unsigned */
2385 tmp_type1 = *type1;
2386 tmp_type2 = *type2;
2387 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2388 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2389 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2390 tcc_warning("assignment from incompatible pointer type");
2392 /* check const and volatile */
2393 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2394 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2395 tcc_warning("assignment discards qualifiers from pointer target type");
2396 break;
2397 case VT_BYTE:
2398 case VT_SHORT:
2399 case VT_INT:
2400 case VT_LLONG:
2401 if (sbt == VT_PTR || sbt == VT_FUNC) {
2402 tcc_warning("assignment makes integer from pointer without a cast");
2404 /* XXX: more tests */
2405 break;
2406 case VT_STRUCT:
2407 tmp_type1 = *dt;
2408 tmp_type2 = *st;
2409 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2410 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2411 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2412 error:
2413 type_to_str(buf1, sizeof(buf1), st, NULL);
2414 type_to_str(buf2, sizeof(buf2), dt, NULL);
2415 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2417 break;
2419 type_ok:
2420 gen_cast(dt);
2423 /* store vtop in lvalue pushed on stack */
2424 ST_FUNC void vstore(void)
2426 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2428 ft = vtop[-1].type.t;
2429 sbt = vtop->type.t & VT_BTYPE;
2430 dbt = ft & VT_BTYPE;
2431 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2432 (sbt == VT_INT && dbt == VT_SHORT))
2433 && !(vtop->type.t & VT_BITFIELD)) {
2434 /* optimize char/short casts */
2435 delayed_cast = VT_MUSTCAST;
2436 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2437 /* XXX: factorize */
2438 if (ft & VT_CONSTANT)
2439 tcc_warning("assignment of read-only location");
2440 } else {
2441 delayed_cast = 0;
2442 if (!(ft & VT_BITFIELD))
2443 gen_assign_cast(&vtop[-1].type);
2446 if (sbt == VT_STRUCT) {
2447 /* if structure, only generate pointer */
2448 /* structure assignment : generate memcpy */
2449 /* XXX: optimize if small size */
2450 if (!nocode_wanted) {
2451 size = type_size(&vtop->type, &align);
2453 /* destination */
2454 vswap();
2455 vtop->type.t = VT_PTR;
2456 gaddrof();
2458 /* address of memcpy() */
2459 #ifdef TCC_ARM_EABI
2460 if(!(align & 7))
2461 vpush_global_sym(&func_old_type, TOK_memcpy8);
2462 else if(!(align & 3))
2463 vpush_global_sym(&func_old_type, TOK_memcpy4);
2464 else
2465 #endif
2466 vpush_global_sym(&func_old_type, TOK_memcpy);
2468 vswap();
2469 /* source */
2470 vpushv(vtop - 2);
2471 vtop->type.t = VT_PTR;
2472 gaddrof();
2473 /* type size */
2474 vpushi(size);
2475 gfunc_call(3);
2476 } else {
2477 vswap();
2478 vpop();
2480 /* leave source on stack */
2481 } else if (ft & VT_BITFIELD) {
2482 /* bitfield store handling */
2483 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2484 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2485 /* remove bit field info to avoid loops */
2486 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2488 /* duplicate source into other register */
2489 gv_dup();
2490 vswap();
2491 vrott(3);
2493 if((ft & VT_BTYPE) == VT_BOOL) {
2494 gen_cast(&vtop[-1].type);
2495 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2498 /* duplicate destination */
2499 vdup();
2500 vtop[-1] = vtop[-2];
2502 /* mask and shift source */
2503 if((ft & VT_BTYPE) != VT_BOOL) {
2504 if((ft & VT_BTYPE) == VT_LLONG) {
2505 vpushll((1ULL << bit_size) - 1ULL);
2506 } else {
2507 vpushi((1 << bit_size) - 1);
2509 gen_op('&');
2511 vpushi(bit_pos);
2512 gen_op(TOK_SHL);
2513 /* load destination, mask and or with source */
2514 vswap();
2515 if((ft & VT_BTYPE) == VT_LLONG) {
2516 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2517 } else {
2518 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2520 gen_op('&');
2521 gen_op('|');
2522 /* store result */
2523 vstore();
2525 /* pop off shifted source from "duplicate source..." above */
2526 vpop();
2528 } else {
2529 #ifdef CONFIG_TCC_BCHECK
2530 /* bound check case */
2531 if (vtop[-1].r & VT_MUSTBOUND) {
2532 vswap();
2533 gbound();
2534 vswap();
2536 #endif
2537 if (!nocode_wanted) {
2538 rc = RC_INT;
2539 if (is_float(ft)) {
2540 rc = RC_FLOAT;
2541 #ifdef TCC_TARGET_X86_64
2542 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2543 rc = RC_ST0;
2544 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2545 rc = RC_FRET;
2547 #endif
2549 r = gv(rc); /* generate value */
2550 /* if lvalue was saved on stack, must read it */
2551 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2552 SValue sv;
2553 t = get_reg(RC_INT);
2554 #ifdef TCC_TARGET_X86_64
2555 sv.type.t = VT_PTR;
2556 #else
2557 sv.type.t = VT_INT;
2558 #endif
2559 sv.r = VT_LOCAL | VT_LVAL;
2560 sv.c.ul = vtop[-1].c.ul;
2561 load(t, &sv);
2562 vtop[-1].r = t | VT_LVAL;
2564 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2565 #ifdef TCC_TARGET_X86_64
2566 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2567 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2568 #else
2569 if ((ft & VT_BTYPE) == VT_LLONG) {
2570 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2571 #endif
2572 vtop[-1].type.t = load_type;
2573 store(r, vtop - 1);
2574 vswap();
2575 /* convert to int to increment easily */
2576 vtop->type.t = addr_type;
2577 gaddrof();
2578 vpushi(load_size);
2579 gen_op('+');
2580 vtop->r |= VT_LVAL;
2581 vswap();
2582 vtop[-1].type.t = load_type;
2583 /* XXX: it works because r2 is spilled last ! */
2584 store(vtop->r2, vtop - 1);
2585 } else {
2586 store(r, vtop - 1);
2589 vswap();
2590 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2591 vtop->r |= delayed_cast;
2595 /* post defines POST/PRE add. c is the token ++ or -- */
2596 ST_FUNC void inc(int post, int c)
2598 test_lvalue();
2599 vdup(); /* save lvalue */
2600 if (post) {
2601 gv_dup(); /* duplicate value */
2602 vrotb(3);
2603 vrotb(3);
2605 /* add constant */
2606 vpushi(c - TOK_MID);
2607 gen_op('+');
2608 vstore(); /* store value */
2609 if (post)
2610 vpop(); /* if post op, return saved value */
2613 /* Parse GNUC __attribute__ extension. Currently, the following
2614 extensions are recognized:
2615 - aligned(n) : set data/function alignment.
2616 - packed : force data alignment to 1
2617 - section(x) : generate data/code in this section.
2618 - unused : currently ignored, but may be used someday.
2619 - regparm(n) : pass function parameters in registers (i386 only)
2621 static void parse_attribute(AttributeDef *ad)
2623 int t, n;
2625 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2626 next();
2627 skip('(');
2628 skip('(');
2629 while (tok != ')') {
2630 if (tok < TOK_IDENT)
2631 expect("attribute name");
2632 t = tok;
2633 next();
2634 switch(t) {
2635 case TOK_SECTION1:
2636 case TOK_SECTION2:
2637 skip('(');
2638 if (tok != TOK_STR)
2639 expect("section name");
2640 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2641 next();
2642 skip(')');
2643 break;
2644 case TOK_ALIAS1:
2645 case TOK_ALIAS2:
2646 skip('(');
2647 if (tok != TOK_STR)
2648 expect("alias(\"target\")");
2649 ad->alias_target = /* save string as token, for later */
2650 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2651 next();
2652 skip(')');
2653 break;
2654 case TOK_ALIGNED1:
2655 case TOK_ALIGNED2:
2656 if (tok == '(') {
2657 next();
2658 n = expr_const();
2659 if (n <= 0 || (n & (n - 1)) != 0)
2660 tcc_error("alignment must be a positive power of two");
2661 skip(')');
2662 } else {
2663 n = MAX_ALIGN;
2665 ad->a.aligned = n;
2666 break;
2667 case TOK_PACKED1:
2668 case TOK_PACKED2:
2669 ad->a.packed = 1;
2670 break;
2671 case TOK_WEAK1:
2672 case TOK_WEAK2:
2673 ad->a.weak = 1;
2674 break;
2675 case TOK_UNUSED1:
2676 case TOK_UNUSED2:
2677 /* currently, no need to handle it because tcc does not
2678 track unused objects */
2679 break;
2680 case TOK_NORETURN1:
2681 case TOK_NORETURN2:
2682 /* currently, no need to handle it because tcc does not
2683 track unused objects */
2684 break;
2685 case TOK_CDECL1:
2686 case TOK_CDECL2:
2687 case TOK_CDECL3:
2688 ad->a.func_call = FUNC_CDECL;
2689 break;
2690 case TOK_STDCALL1:
2691 case TOK_STDCALL2:
2692 case TOK_STDCALL3:
2693 ad->a.func_call = FUNC_STDCALL;
2694 break;
2695 #ifdef TCC_TARGET_I386
2696 case TOK_REGPARM1:
2697 case TOK_REGPARM2:
2698 skip('(');
2699 n = expr_const();
2700 if (n > 3)
2701 n = 3;
2702 else if (n < 0)
2703 n = 0;
2704 if (n > 0)
2705 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2706 skip(')');
2707 break;
2708 case TOK_FASTCALL1:
2709 case TOK_FASTCALL2:
2710 case TOK_FASTCALL3:
2711 ad->a.func_call = FUNC_FASTCALLW;
2712 break;
2713 #endif
2714 case TOK_MODE:
2715 skip('(');
2716 switch(tok) {
2717 case TOK_MODE_DI:
2718 ad->a.mode = VT_LLONG + 1;
2719 break;
2720 case TOK_MODE_HI:
2721 ad->a.mode = VT_SHORT + 1;
2722 break;
2723 case TOK_MODE_SI:
2724 ad->a.mode = VT_INT + 1;
2725 break;
2726 default:
2727 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2728 break;
2730 next();
2731 skip(')');
2732 break;
2733 case TOK_DLLEXPORT:
2734 ad->a.func_export = 1;
2735 break;
2736 case TOK_DLLIMPORT:
2737 ad->a.func_import = 1;
2738 break;
2739 default:
2740 if (tcc_state->warn_unsupported)
2741 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2742 /* skip parameters */
2743 if (tok == '(') {
2744 int parenthesis = 0;
2745 do {
2746 if (tok == '(')
2747 parenthesis++;
2748 else if (tok == ')')
2749 parenthesis--;
2750 next();
2751 } while (parenthesis && tok != -1);
2753 break;
2755 if (tok != ',')
2756 break;
2757 next();
2759 skip(')');
2760 skip(')');
2764 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2765 static void struct_decl(CType *type, int u, int tdef)
2767 int a, v, size, align, maxalign, c, offset, flexible;
2768 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2769 Sym *s, *ss, *ass, **ps;
2770 AttributeDef ad;
2771 CType type1, btype;
2773 a = tok; /* save decl type */
2774 next();
2775 if (tok != '{') {
2776 v = tok;
2777 next();
2778 /* struct already defined ? return it */
2779 if (v < TOK_IDENT)
2780 expect("struct/union/enum name");
2781 s = struct_find(v);
2782 if (s) {
2783 if (s->type.t != a)
2784 tcc_error("invalid type");
2785 goto do_decl;
2786 } else if (tok >= TOK_IDENT && !tdef)
2787 tcc_error("unknown struct/union/enum");
2788 } else {
2789 v = anon_sym++;
2791 type1.t = a;
2792 type1.ref = NULL;
2793 /* we put an undefined size for struct/union */
2794 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2795 s->r = 0; /* default alignment is zero as gcc */
2796 /* put struct/union/enum name in type */
2797 do_decl:
2798 type->t = u;
2799 type->ref = s;
2801 if (tok == '{') {
2802 next();
2803 if (s->c != -1)
2804 tcc_error("struct/union/enum already defined");
2805 /* cannot be empty */
2806 c = 0;
2807 /* non empty enums are not allowed */
2808 if (a == TOK_ENUM) {
2809 for(;;) {
2810 v = tok;
2811 if (v < TOK_UIDENT)
2812 expect("identifier");
2813 ss = sym_find(v);
2814 if (ss)
2815 tcc_error("redefinition of enumerator '%s'",
2816 get_tok_str(v, NULL));
2817 next();
2818 if (tok == '=') {
2819 next();
2820 c = expr_const();
2822 /* enum symbols have static storage */
2823 ss = sym_push(v, &int_type, VT_CONST, c);
2824 ss->type.t |= VT_STATIC;
2825 if (tok != ',')
2826 break;
2827 next();
2828 c++;
2829 /* NOTE: we accept a trailing comma */
2830 if (tok == '}')
2831 break;
2833 s->c = type_size(&int_type, &align);
2834 skip('}');
2835 } else {
2836 maxalign = 1;
2837 ps = &s->next;
2838 prevbt = VT_INT;
2839 bit_pos = 0;
2840 offset = 0;
2841 flexible = 0;
2842 while (tok != '}') {
2843 parse_btype(&btype, &ad);
2844 while (1) {
2845 if (flexible)
2846 tcc_error("flexible array member '%s' not at the end of struct",
2847 get_tok_str(v, NULL));
2848 bit_size = -1;
2849 v = 0;
2850 type1 = btype;
2851 if (tok != ':') {
2852 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2853 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2854 expect("identifier");
2855 if (type_size(&type1, &align) < 0) {
2856 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2857 flexible = 1;
2858 else
2859 tcc_error("field '%s' has incomplete type",
2860 get_tok_str(v, NULL));
2862 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2863 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2864 tcc_error("invalid type for '%s'",
2865 get_tok_str(v, NULL));
2867 if (tok == ':') {
2868 next();
2869 bit_size = expr_const();
2870 /* XXX: handle v = 0 case for messages */
2871 if (bit_size < 0)
2872 tcc_error("negative width in bit-field '%s'",
2873 get_tok_str(v, NULL));
2874 if (v && bit_size == 0)
2875 tcc_error("zero width for bit-field '%s'",
2876 get_tok_str(v, NULL));
2878 size = type_size(&type1, &align);
2879 if (ad.a.aligned) {
2880 if (align < ad.a.aligned)
2881 align = ad.a.aligned;
2882 } else if (ad.a.packed) {
2883 align = 1;
2884 } else if (*tcc_state->pack_stack_ptr) {
2885 if (align > *tcc_state->pack_stack_ptr)
2886 align = *tcc_state->pack_stack_ptr;
2888 lbit_pos = 0;
2889 if (bit_size >= 0) {
2890 bt = type1.t & VT_BTYPE;
2891 if (bt != VT_INT &&
2892 bt != VT_BYTE &&
2893 bt != VT_SHORT &&
2894 bt != VT_BOOL &&
2895 bt != VT_ENUM &&
2896 bt != VT_LLONG)
2897 tcc_error("bitfields must have scalar type");
2898 bsize = size * 8;
2899 if (bit_size > bsize) {
2900 tcc_error("width of '%s' exceeds its type",
2901 get_tok_str(v, NULL));
2902 } else if (bit_size == bsize) {
2903 /* no need for bit fields */
2904 bit_pos = 0;
2905 } else if (bit_size == 0) {
2906 /* XXX: what to do if only padding in a
2907 structure ? */
2908 /* zero size: means to pad */
2909 bit_pos = 0;
2910 } else {
2911 /* we do not have enough room ?
2912 did the type change?
2913 is it a union? */
2914 if ((bit_pos + bit_size) > bsize ||
2915 bt != prevbt || a == TOK_UNION)
2916 bit_pos = 0;
2917 lbit_pos = bit_pos;
2918 /* XXX: handle LSB first */
2919 type1.t |= VT_BITFIELD |
2920 (bit_pos << VT_STRUCT_SHIFT) |
2921 (bit_size << (VT_STRUCT_SHIFT + 6));
2922 bit_pos += bit_size;
2924 prevbt = bt;
2925 } else {
2926 bit_pos = 0;
2928 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2929 /* add new memory data only if starting
2930 bit field */
2931 if (lbit_pos == 0) {
2932 if (a == TOK_STRUCT) {
2933 c = (c + align - 1) & -align;
2934 offset = c;
2935 if (size > 0)
2936 c += size;
2937 } else {
2938 offset = 0;
2939 if (size > c)
2940 c = size;
2942 if (align > maxalign)
2943 maxalign = align;
2945 #if 0
2946 printf("add field %s offset=%d",
2947 get_tok_str(v, NULL), offset);
2948 if (type1.t & VT_BITFIELD) {
2949 printf(" pos=%d size=%d",
2950 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2951 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2953 printf("\n");
2954 #endif
2956 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2957 ass = type1.ref;
2958 while ((ass = ass->next) != NULL) {
2959 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2960 *ps = ss;
2961 ps = &ss->next;
2963 } else if (v) {
2964 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2965 *ps = ss;
2966 ps = &ss->next;
2968 if (tok == ';' || tok == TOK_EOF)
2969 break;
2970 skip(',');
2972 skip(';');
2974 skip('}');
2975 /* store size and alignment */
2976 s->c = (c + maxalign - 1) & -maxalign;
2977 s->r = maxalign;
2982 /* return 0 if no type declaration. otherwise, return the basic type
2983 and skip it.
2985 static int parse_btype(CType *type, AttributeDef *ad)
2987 int t, u, type_found, typespec_found, typedef_found;
2988 Sym *s;
2989 CType type1;
2991 memset(ad, 0, sizeof(AttributeDef));
2992 type_found = 0;
2993 typespec_found = 0;
2994 typedef_found = 0;
2995 t = 0;
2996 while(1) {
2997 switch(tok) {
2998 case TOK_EXTENSION:
2999 /* currently, we really ignore extension */
3000 next();
3001 continue;
3003 /* basic types */
3004 case TOK_CHAR:
3005 u = VT_BYTE;
3006 basic_type:
3007 next();
3008 basic_type1:
3009 if ((t & VT_BTYPE) != 0)
3010 tcc_error("too many basic types");
3011 t |= u;
3012 typespec_found = 1;
3013 break;
3014 case TOK_VOID:
3015 u = VT_VOID;
3016 goto basic_type;
3017 case TOK_SHORT:
3018 u = VT_SHORT;
3019 goto basic_type;
3020 case TOK_INT:
3021 next();
3022 typespec_found = 1;
3023 break;
3024 case TOK_LONG:
3025 next();
3026 if ((t & VT_BTYPE) == VT_DOUBLE) {
3027 #ifndef TCC_TARGET_PE
3028 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3029 #endif
3030 } else if ((t & VT_BTYPE) == VT_LONG) {
3031 t = (t & ~VT_BTYPE) | VT_LLONG;
3032 } else {
3033 u = VT_LONG;
3034 goto basic_type1;
3036 break;
3037 case TOK_BOOL:
3038 u = VT_BOOL;
3039 goto basic_type;
3040 case TOK_FLOAT:
3041 u = VT_FLOAT;
3042 goto basic_type;
3043 case TOK_DOUBLE:
3044 next();
3045 if ((t & VT_BTYPE) == VT_LONG) {
3046 #ifdef TCC_TARGET_PE
3047 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3048 #else
3049 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3050 #endif
3051 } else {
3052 u = VT_DOUBLE;
3053 goto basic_type1;
3055 break;
3056 case TOK_ENUM:
3057 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3058 basic_type2:
3059 u = type1.t;
3060 type->ref = type1.ref;
3061 goto basic_type1;
3062 case TOK_STRUCT:
3063 case TOK_UNION:
3064 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3065 goto basic_type2;
3067 /* type modifiers */
3068 case TOK_CONST1:
3069 case TOK_CONST2:
3070 case TOK_CONST3:
3071 t |= VT_CONSTANT;
3072 next();
3073 break;
3074 case TOK_VOLATILE1:
3075 case TOK_VOLATILE2:
3076 case TOK_VOLATILE3:
3077 t |= VT_VOLATILE;
3078 next();
3079 break;
3080 case TOK_SIGNED1:
3081 case TOK_SIGNED2:
3082 case TOK_SIGNED3:
3083 typespec_found = 1;
3084 t |= VT_SIGNED;
3085 next();
3086 break;
3087 case TOK_REGISTER:
3088 case TOK_AUTO:
3089 case TOK_RESTRICT1:
3090 case TOK_RESTRICT2:
3091 case TOK_RESTRICT3:
3092 next();
3093 break;
3094 case TOK_UNSIGNED:
3095 t |= VT_UNSIGNED;
3096 next();
3097 typespec_found = 1;
3098 break;
3100 /* storage */
3101 case TOK_EXTERN:
3102 t |= VT_EXTERN;
3103 next();
3104 break;
3105 case TOK_STATIC:
3106 t |= VT_STATIC;
3107 next();
3108 break;
3109 case TOK_TYPEDEF:
3110 t |= VT_TYPEDEF;
3111 next();
3112 break;
3113 case TOK_INLINE1:
3114 case TOK_INLINE2:
3115 case TOK_INLINE3:
3116 t |= VT_INLINE;
3117 next();
3118 break;
3120 /* GNUC attribute */
3121 case TOK_ATTRIBUTE1:
3122 case TOK_ATTRIBUTE2:
3123 parse_attribute(ad);
3124 if (ad->a.mode) {
3125 u = ad->a.mode -1;
3126 t = (t & ~VT_BTYPE) | u;
3128 break;
3129 /* GNUC typeof */
3130 case TOK_TYPEOF1:
3131 case TOK_TYPEOF2:
3132 case TOK_TYPEOF3:
3133 next();
3134 parse_expr_type(&type1);
3135 /* remove all storage modifiers except typedef */
3136 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3137 goto basic_type2;
3138 default:
3139 if (typespec_found || typedef_found)
3140 goto the_end;
3141 s = sym_find(tok);
3142 if (!s || !(s->type.t & VT_TYPEDEF))
3143 goto the_end;
3144 typedef_found = 1;
3145 t |= (s->type.t & ~VT_TYPEDEF);
3146 type->ref = s->type.ref;
3147 if (s->r) {
3148 /* get attributes from typedef */
3149 if (0 == ad->a.aligned)
3150 ad->a.aligned = s->a.aligned;
3151 if (0 == ad->a.func_call)
3152 ad->a.func_call = s->a.func_call;
3153 ad->a.packed |= s->a.packed;
3155 next();
3156 typespec_found = 1;
3157 break;
3159 type_found = 1;
3161 the_end:
3162 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3163 tcc_error("signed and unsigned modifier");
3164 if (tcc_state->char_is_unsigned) {
3165 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3166 t |= VT_UNSIGNED;
3168 t &= ~VT_SIGNED;
3170 /* long is never used as type */
3171 if ((t & VT_BTYPE) == VT_LONG)
3172 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3173 t = (t & ~VT_BTYPE) | VT_INT;
3174 #else
3175 t = (t & ~VT_BTYPE) | VT_LLONG;
3176 #endif
3177 type->t = t;
3178 return type_found;
3181 /* convert a function parameter type (array to pointer and function to
3182 function pointer) */
3183 static inline void convert_parameter_type(CType *pt)
3185 /* remove const and volatile qualifiers (XXX: const could be used
3186 to indicate a const function parameter */
3187 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3188 /* array must be transformed to pointer according to ANSI C */
3189 pt->t &= ~VT_ARRAY;
3190 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3191 mk_pointer(pt);
3195 ST_FUNC void parse_asm_str(CString *astr)
3197 skip('(');
3198 /* read the string */
3199 if (tok != TOK_STR)
3200 expect("string constant");
3201 cstr_new(astr);
3202 while (tok == TOK_STR) {
3203 /* XXX: add \0 handling too ? */
3204 cstr_cat(astr, tokc.cstr->data);
3205 next();
3207 cstr_ccat(astr, '\0');
3210 /* Parse an asm label and return the label
3211 * Don't forget to free the CString in the caller! */
3212 static void asm_label_instr(CString *astr)
3214 next();
3215 parse_asm_str(astr);
3216 skip(')');
3217 #ifdef ASM_DEBUG
3218 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3219 #endif
3222 static void post_type(CType *type, AttributeDef *ad)
3224 int n, l, t1, arg_size, align;
3225 Sym **plast, *s, *first;
3226 AttributeDef ad1;
3227 CType pt;
3229 if (tok == '(') {
3230 /* function declaration */
3231 next();
3232 l = 0;
3233 first = NULL;
3234 plast = &first;
3235 arg_size = 0;
3236 if (tok != ')') {
3237 for(;;) {
3238 /* read param name and compute offset */
3239 if (l != FUNC_OLD) {
3240 if (!parse_btype(&pt, &ad1)) {
3241 if (l) {
3242 tcc_error("invalid type");
3243 } else {
3244 l = FUNC_OLD;
3245 goto old_proto;
3248 l = FUNC_NEW;
3249 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3250 break;
3251 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3252 if ((pt.t & VT_BTYPE) == VT_VOID)
3253 tcc_error("parameter declared as void");
3254 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3255 } else {
3256 old_proto:
3257 n = tok;
3258 if (n < TOK_UIDENT)
3259 expect("identifier");
3260 pt.t = VT_INT;
3261 next();
3263 convert_parameter_type(&pt);
3264 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3265 *plast = s;
3266 plast = &s->next;
3267 if (tok == ')')
3268 break;
3269 skip(',');
3270 if (l == FUNC_NEW && tok == TOK_DOTS) {
3271 l = FUNC_ELLIPSIS;
3272 next();
3273 break;
3277 /* if no parameters, then old type prototype */
3278 if (l == 0)
3279 l = FUNC_OLD;
3280 skip(')');
3281 /* NOTE: const is ignored in returned type as it has a special
3282 meaning in gcc / C++ */
3283 type->t &= ~VT_CONSTANT;
3284 /* some ancient pre-K&R C allows a function to return an array
3285 and the array brackets to be put after the arguments, such
3286 that "int c()[]" means something like "int[] c()" */
3287 if (tok == '[') {
3288 next();
3289 skip(']'); /* only handle simple "[]" */
3290 type->t |= VT_PTR;
3292 /* we push a anonymous symbol which will contain the function prototype */
3293 ad->a.func_args = arg_size;
3294 s = sym_push(SYM_FIELD, type, 0, l);
3295 s->a = ad->a;
3296 s->next = first;
3297 type->t = VT_FUNC;
3298 type->ref = s;
3299 } else if (tok == '[') {
3300 /* array definition */
3301 next();
3302 if (tok == TOK_RESTRICT1)
3303 next();
3304 n = -1;
3305 t1 = 0;
3306 if (tok != ']') {
3307 if (!local_stack || nocode_wanted)
3308 vpushi(expr_const());
3309 else gexpr();
3310 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3311 n = vtop->c.i;
3312 if (n < 0)
3313 tcc_error("invalid array size");
3314 } else {
3315 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3316 tcc_error("size of variable length array should be an integer");
3317 t1 = VT_VLA;
3320 skip(']');
3321 /* parse next post type */
3322 post_type(type, ad);
3323 if (type->t == VT_FUNC)
3324 tcc_error("declaration of an array of functions");
3325 t1 |= type->t & VT_VLA;
3327 if (t1 & VT_VLA) {
3328 loc -= type_size(&int_type, &align);
3329 loc &= -align;
3330 n = loc;
3332 vla_runtime_type_size(type, &align);
3333 gen_op('*');
3334 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3335 vswap();
3336 vstore();
3338 if (n != -1)
3339 vpop();
3341 /* we push an anonymous symbol which will contain the array
3342 element type */
3343 s = sym_push(SYM_FIELD, type, 0, n);
3344 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3345 type->ref = s;
3349 /* Parse a type declaration (except basic type), and return the type
3350 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3351 expected. 'type' should contain the basic type. 'ad' is the
3352 attribute definition of the basic type. It can be modified by
3353 type_decl().
3355 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3357 Sym *s;
3358 CType type1, *type2;
3359 int qualifiers, storage;
3361 while (tok == '*') {
3362 qualifiers = 0;
3363 redo:
3364 next();
3365 switch(tok) {
3366 case TOK_CONST1:
3367 case TOK_CONST2:
3368 case TOK_CONST3:
3369 qualifiers |= VT_CONSTANT;
3370 goto redo;
3371 case TOK_VOLATILE1:
3372 case TOK_VOLATILE2:
3373 case TOK_VOLATILE3:
3374 qualifiers |= VT_VOLATILE;
3375 goto redo;
3376 case TOK_RESTRICT1:
3377 case TOK_RESTRICT2:
3378 case TOK_RESTRICT3:
3379 goto redo;
3381 mk_pointer(type);
3382 type->t |= qualifiers;
3385 /* XXX: clarify attribute handling */
3386 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3387 parse_attribute(ad);
3389 /* recursive type */
3390 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3391 type1.t = 0; /* XXX: same as int */
3392 if (tok == '(') {
3393 next();
3394 /* XXX: this is not correct to modify 'ad' at this point, but
3395 the syntax is not clear */
3396 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3397 parse_attribute(ad);
3398 type_decl(&type1, ad, v, td);
3399 skip(')');
3400 } else {
3401 /* type identifier */
3402 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3403 *v = tok;
3404 next();
3405 } else {
3406 if (!(td & TYPE_ABSTRACT))
3407 expect("identifier");
3408 *v = 0;
3411 storage = type->t & VT_STORAGE;
3412 type->t &= ~VT_STORAGE;
3413 if (storage & VT_STATIC) {
3414 int saved_nocode_wanted = nocode_wanted;
3415 nocode_wanted = 1;
3416 post_type(type, ad);
3417 nocode_wanted = saved_nocode_wanted;
3418 } else
3419 post_type(type, ad);
3420 type->t |= storage;
3421 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3422 parse_attribute(ad);
3424 if (!type1.t)
3425 return;
3426 /* append type at the end of type1 */
3427 type2 = &type1;
3428 for(;;) {
3429 s = type2->ref;
3430 type2 = &s->type;
3431 if (!type2->t) {
3432 *type2 = *type;
3433 break;
3436 *type = type1;
3439 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3440 ST_FUNC int lvalue_type(int t)
3442 int bt, r;
3443 r = VT_LVAL;
3444 bt = t & VT_BTYPE;
3445 if (bt == VT_BYTE || bt == VT_BOOL)
3446 r |= VT_LVAL_BYTE;
3447 else if (bt == VT_SHORT)
3448 r |= VT_LVAL_SHORT;
3449 else
3450 return r;
3451 if (t & VT_UNSIGNED)
3452 r |= VT_LVAL_UNSIGNED;
3453 return r;
3456 /* indirection with full error checking and bound check */
3457 ST_FUNC void indir(void)
3459 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3460 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3461 return;
3462 expect("pointer");
3464 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3465 gv(RC_INT);
3466 vtop->type = *pointed_type(&vtop->type);
3467 /* Arrays and functions are never lvalues */
3468 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3469 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3470 vtop->r |= lvalue_type(vtop->type.t);
3471 /* if bound checking, the referenced pointer must be checked */
3472 #ifdef CONFIG_TCC_BCHECK
3473 if (tcc_state->do_bounds_check)
3474 vtop->r |= VT_MUSTBOUND;
3475 #endif
3479 /* pass a parameter to a function and do type checking and casting */
3480 static void gfunc_param_typed(Sym *func, Sym *arg)
3482 int func_type;
3483 CType type;
3485 func_type = func->c;
3486 if (func_type == FUNC_OLD ||
3487 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3488 /* default casting : only need to convert float to double */
3489 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3490 type.t = VT_DOUBLE;
3491 gen_cast(&type);
3493 } else if (arg == NULL) {
3494 tcc_error("too many arguments to function");
3495 } else {
3496 type = arg->type;
3497 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3498 gen_assign_cast(&type);
3502 /* parse an expression of the form '(type)' or '(expr)' and return its
3503 type */
3504 static void parse_expr_type(CType *type)
3506 int n;
3507 AttributeDef ad;
3509 skip('(');
3510 if (parse_btype(type, &ad)) {
3511 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3512 } else {
3513 expr_type(type);
3515 skip(')');
3518 static void parse_type(CType *type)
3520 AttributeDef ad;
3521 int n;
3523 if (!parse_btype(type, &ad)) {
3524 expect("type");
3526 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3529 static void vpush_tokc(int t)
3531 CType type;
3532 type.t = t;
3533 type.ref = 0;
3534 vsetc(&type, VT_CONST, &tokc);
3537 ST_FUNC void unary(void)
3539 int n, t, align, size, r, sizeof_caller;
3540 CType type;
3541 Sym *s;
3542 AttributeDef ad;
3543 static int in_sizeof = 0;
3545 sizeof_caller = in_sizeof;
3546 in_sizeof = 0;
3547 /* XXX: GCC 2.95.3 does not generate a table although it should be
3548 better here */
3549 tok_next:
3550 switch(tok) {
3551 case TOK_EXTENSION:
3552 next();
3553 goto tok_next;
3554 case TOK_CINT:
3555 case TOK_CCHAR:
3556 case TOK_LCHAR:
3557 vpushi(tokc.i);
3558 next();
3559 break;
3560 case TOK_CUINT:
3561 vpush_tokc(VT_INT | VT_UNSIGNED);
3562 next();
3563 break;
3564 case TOK_CLLONG:
3565 vpush_tokc(VT_LLONG);
3566 next();
3567 break;
3568 case TOK_CULLONG:
3569 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3570 next();
3571 break;
3572 case TOK_CFLOAT:
3573 vpush_tokc(VT_FLOAT);
3574 next();
3575 break;
3576 case TOK_CDOUBLE:
3577 vpush_tokc(VT_DOUBLE);
3578 next();
3579 break;
3580 case TOK_CLDOUBLE:
3581 vpush_tokc(VT_LDOUBLE);
3582 next();
3583 break;
3584 case TOK___FUNCTION__:
3585 if (!gnu_ext)
3586 goto tok_identifier;
3587 /* fall thru */
3588 case TOK___FUNC__:
3590 void *ptr;
3591 int len;
3592 /* special function name identifier */
3593 len = strlen(funcname) + 1;
3594 /* generate char[len] type */
3595 type.t = VT_BYTE;
3596 mk_pointer(&type);
3597 type.t |= VT_ARRAY;
3598 type.ref->c = len;
3599 vpush_ref(&type, data_section, data_section->data_offset, len);
3600 ptr = section_ptr_add(data_section, len);
3601 memcpy(ptr, funcname, len);
3602 next();
3604 break;
3605 case TOK_LSTR:
3606 #ifdef TCC_TARGET_PE
3607 t = VT_SHORT | VT_UNSIGNED;
3608 #else
3609 t = VT_INT;
3610 #endif
3611 goto str_init;
3612 case TOK_STR:
3613 /* string parsing */
3614 t = VT_BYTE;
3615 str_init:
3616 if (tcc_state->warn_write_strings)
3617 t |= VT_CONSTANT;
3618 type.t = t;
3619 mk_pointer(&type);
3620 type.t |= VT_ARRAY;
3621 memset(&ad, 0, sizeof(AttributeDef));
3622 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3623 break;
3624 case '(':
3625 next();
3626 /* cast ? */
3627 if (parse_btype(&type, &ad)) {
3628 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3629 skip(')');
3630 /* check ISOC99 compound literal */
3631 if (tok == '{') {
3632 /* data is allocated locally by default */
3633 if (global_expr)
3634 r = VT_CONST;
3635 else
3636 r = VT_LOCAL;
3637 /* all except arrays are lvalues */
3638 if (!(type.t & VT_ARRAY))
3639 r |= lvalue_type(type.t);
3640 memset(&ad, 0, sizeof(AttributeDef));
3641 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3642 } else {
3643 if (sizeof_caller) {
3644 vpush(&type);
3645 return;
3647 unary();
3648 gen_cast(&type);
3650 } else if (tok == '{') {
3651 /* save all registers */
3652 save_regs(0);
3653 /* statement expression : we do not accept break/continue
3654 inside as GCC does */
3655 block(NULL, NULL, NULL, NULL, 0, 1);
3656 skip(')');
3657 } else {
3658 gexpr();
3659 skip(')');
3661 break;
3662 case '*':
3663 next();
3664 unary();
3665 indir();
3666 break;
3667 case '&':
3668 next();
3669 unary();
3670 /* functions names must be treated as function pointers,
3671 except for unary '&' and sizeof. Since we consider that
3672 functions are not lvalues, we only have to handle it
3673 there and in function calls. */
3674 /* arrays can also be used although they are not lvalues */
3675 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3676 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3677 test_lvalue();
3678 mk_pointer(&vtop->type);
3679 gaddrof();
3680 break;
3681 case '!':
3682 next();
3683 unary();
3684 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3685 CType boolean;
3686 boolean.t = VT_BOOL;
3687 gen_cast(&boolean);
3688 vtop->c.i = !vtop->c.i;
3689 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3690 vtop->c.i = vtop->c.i ^ 1;
3691 else {
3692 save_regs(1);
3693 vseti(VT_JMP, gvtst(1, 0));
3695 break;
3696 case '~':
3697 next();
3698 unary();
3699 vpushi(-1);
3700 gen_op('^');
3701 break;
3702 case '+':
3703 next();
3704 /* in order to force cast, we add zero */
3705 unary();
3706 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3707 tcc_error("pointer not accepted for unary plus");
3708 vpushi(0);
3709 gen_op('+');
3710 break;
3711 case TOK_SIZEOF:
3712 case TOK_ALIGNOF1:
3713 case TOK_ALIGNOF2:
3714 t = tok;
3715 next();
3716 in_sizeof++;
3717 unary_type(&type); // Perform a in_sizeof = 0;
3718 size = type_size(&type, &align);
3719 if (t == TOK_SIZEOF) {
3720 if (!(type.t & VT_VLA)) {
3721 if (size < 0)
3722 tcc_error("sizeof applied to an incomplete type");
3723 vpushs(size);
3724 } else {
3725 vla_runtime_type_size(&type, &align);
3727 } else {
3728 vpushs(align);
3730 vtop->type.t |= VT_UNSIGNED;
3731 break;
3733 case TOK_builtin_types_compatible_p:
3735 CType type1, type2;
3736 next();
3737 skip('(');
3738 parse_type(&type1);
3739 skip(',');
3740 parse_type(&type2);
3741 skip(')');
3742 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3743 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3744 vpushi(is_compatible_types(&type1, &type2));
3746 break;
3747 case TOK_builtin_constant_p:
3749 int saved_nocode_wanted, res;
3750 next();
3751 skip('(');
3752 saved_nocode_wanted = nocode_wanted;
3753 nocode_wanted = 1;
3754 gexpr();
3755 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3756 vpop();
3757 nocode_wanted = saved_nocode_wanted;
3758 skip(')');
3759 vpushi(res);
3761 break;
3762 case TOK_builtin_frame_address:
3764 int level;
3765 CType type;
3766 next();
3767 skip('(');
3768 if (tok != TOK_CINT || tokc.i < 0) {
3769 tcc_error("__builtin_frame_address only takes positive integers");
3771 level = tokc.i;
3772 next();
3773 skip(')');
3774 type.t = VT_VOID;
3775 mk_pointer(&type);
3776 vset(&type, VT_LOCAL, 0); /* local frame */
3777 while (level--) {
3778 mk_pointer(&vtop->type);
3779 indir(); /* -> parent frame */
3782 break;
3783 #ifdef TCC_TARGET_X86_64
3784 #ifdef TCC_TARGET_PE
3785 case TOK_builtin_va_start:
3787 next();
3788 skip('(');
3789 expr_eq();
3790 skip(',');
3791 expr_eq();
3792 skip(')');
3793 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3794 tcc_error("__builtin_va_start expects a local variable");
3795 vtop->r &= ~(VT_LVAL | VT_REF);
3796 vtop->type = char_pointer_type;
3797 vstore();
3799 break;
3800 #else
3801 case TOK_builtin_va_arg_types:
3803 CType type;
3804 next();
3805 skip('(');
3806 parse_type(&type);
3807 skip(')');
3808 vpushi(classify_x86_64_va_arg(&type));
3810 break;
3811 #endif
3812 #endif
3813 case TOK_INC:
3814 case TOK_DEC:
3815 t = tok;
3816 next();
3817 unary();
3818 inc(0, t);
3819 break;
3820 case '-':
3821 next();
3822 unary();
3823 t = vtop->type.t & VT_BTYPE;
3824 /* handle (-)0.0 */
3825 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST &&
3826 is_float(t)) {
3827 if (t == VT_FLOAT)
3828 vtop->c.f = -vtop->c.f;
3829 else if (t == VT_DOUBLE)
3830 vtop->c.d = -vtop->c.d;
3831 else
3832 vtop->c.ld = -vtop->c.ld;
3833 } else {
3834 vpushi(0);
3835 vswap();
3836 gen_op('-');
3838 break;
3839 case TOK_LAND:
3840 if (!gnu_ext)
3841 goto tok_identifier;
3842 next();
3843 /* allow to take the address of a label */
3844 if (tok < TOK_UIDENT)
3845 expect("label identifier");
3846 s = label_find(tok);
3847 if (!s) {
3848 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3849 } else {
3850 if (s->r == LABEL_DECLARED)
3851 s->r = LABEL_FORWARD;
3853 if (!s->type.t) {
3854 s->type.t = VT_VOID;
3855 mk_pointer(&s->type);
3856 s->type.t |= VT_STATIC;
3858 vset(&s->type, VT_CONST | VT_SYM, 0);
3859 vtop->sym = s;
3860 next();
3861 break;
3863 // special qnan , snan and infinity values
3864 case TOK___NAN__:
3865 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3866 next();
3867 break;
3868 case TOK___SNAN__:
3869 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3870 next();
3871 break;
3872 case TOK___INF__:
3873 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3874 next();
3875 break;
3877 default:
3878 tok_identifier:
3879 t = tok;
3880 next();
3881 if (t < TOK_UIDENT)
3882 expect("identifier");
3883 s = sym_find(t);
3884 if (!s) {
3885 if (tok != '(')
3886 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3887 /* for simple function calls, we tolerate undeclared
3888 external reference to int() function */
3889 if (tcc_state->warn_implicit_function_declaration)
3890 tcc_warning("implicit declaration of function '%s'",
3891 get_tok_str(t, NULL));
3892 s = external_global_sym(t, &func_old_type, 0);
3894 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3895 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3896 /* if referencing an inline function, then we generate a
3897 symbol to it if not already done. It will have the
3898 effect to generate code for it at the end of the
3899 compilation unit. Inline function as always
3900 generated in the text section. */
3901 if (!s->c)
3902 put_extern_sym(s, text_section, 0, 0);
3903 r = VT_SYM | VT_CONST;
3904 } else {
3905 r = s->r;
3907 vset(&s->type, r, s->c);
3908 /* if forward reference, we must point to s */
3909 if (vtop->r & VT_SYM) {
3910 vtop->sym = s;
3911 #ifdef TCC_TARGET_X86_64
3912 s1->vtop->c.ull = 0;
3913 #else
3914 s1->vtop->c.ul = 0;
3915 #endif
3917 break;
3920 /* post operations */
3921 while (1) {
3922 if (tok == TOK_INC || tok == TOK_DEC) {
3923 inc(1, tok);
3924 next();
3925 } else if (tok == '.' || tok == TOK_ARROW) {
3926 int qualifiers;
3927 /* field */
3928 if (tok == TOK_ARROW)
3929 indir();
3930 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3931 test_lvalue();
3932 gaddrof();
3933 next();
3934 /* expect pointer on structure */
3935 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3936 expect("struct or union");
3937 s = vtop->type.ref;
3938 /* find field */
3939 tok |= SYM_FIELD;
3940 while ((s = s->next) != NULL) {
3941 if (s->v == tok)
3942 break;
3944 if (!s)
3945 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3946 /* add field offset to pointer */
3947 vtop->type = char_pointer_type; /* change type to 'char *' */
3948 vpushi(s->c);
3949 gen_op('+');
3950 /* change type to field type, and set to lvalue */
3951 vtop->type = s->type;
3952 vtop->type.t |= qualifiers;
3953 /* an array is never an lvalue */
3954 if (!(vtop->type.t & VT_ARRAY)) {
3955 vtop->r |= lvalue_type(vtop->type.t);
3956 #ifdef CONFIG_TCC_BCHECK
3957 /* if bound checking, the referenced pointer must be checked */
3958 if (tcc_state->do_bounds_check)
3959 vtop->r |= VT_MUSTBOUND;
3960 #endif
3962 next();
3963 } else if (tok == '[') {
3964 next();
3965 gexpr();
3966 gen_op('+');
3967 indir();
3968 skip(']');
3969 } else if (tok == '(') {
3970 SValue ret;
3971 Sym *sa;
3972 int nb_args, ret_nregs, ret_align, variadic;
3974 /* function call */
3975 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3976 /* pointer test (no array accepted) */
3977 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3978 vtop->type = *pointed_type(&vtop->type);
3979 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3980 goto error_func;
3981 } else {
3982 error_func:
3983 expect("function pointer");
3985 } else {
3986 vtop->r &= ~VT_LVAL; /* no lvalue */
3988 /* get return type */
3989 s = vtop->type.ref;
3990 next();
3991 sa = s->next; /* first parameter */
3992 nb_args = 0;
3993 ret.r2 = VT_CONST;
3994 /* compute first implicit argument if a structure is returned */
3995 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3996 variadic = (s->c == FUNC_ELLIPSIS);
3997 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
3998 &ret_align);
3999 if (!ret_nregs) {
4000 /* get some space for the returned structure */
4001 size = type_size(&s->type, &align);
4002 loc = (loc - size) & -align;
4003 ret.type = s->type;
4004 ret.r = VT_LOCAL | VT_LVAL;
4005 /* pass it as 'int' to avoid structure arg passing
4006 problems */
4007 vseti(VT_LOCAL, loc);
4008 ret.c = vtop->c;
4009 nb_args++;
4011 } else {
4012 ret_nregs = 1;
4013 ret.type = s->type;
4016 if (ret_nregs) {
4017 /* return in register */
4018 if (is_float(ret.type.t)) {
4019 ret.r = reg_fret(ret.type.t);
4020 #ifdef TCC_TARGET_X86_64
4021 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4022 ret.r2 = REG_QRET;
4023 #endif
4024 } else {
4025 #ifdef TCC_TARGET_X86_64
4026 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4027 #else
4028 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4029 #endif
4030 ret.r2 = REG_LRET;
4031 ret.r = REG_IRET;
4033 ret.c.i = 0;
4035 if (tok != ')') {
4036 for(;;) {
4037 expr_eq();
4038 gfunc_param_typed(s, sa);
4039 nb_args++;
4040 if (sa)
4041 sa = sa->next;
4042 if (tok == ')')
4043 break;
4044 skip(',');
4047 if (sa)
4048 tcc_error("too few arguments to function");
4049 skip(')');
4050 if (!nocode_wanted) {
4051 gfunc_call(nb_args);
4052 } else {
4053 vtop -= (nb_args + 1);
4056 /* return value */
4057 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4058 vsetc(&ret.type, r, &ret.c);
4059 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4062 /* handle packed struct return */
4063 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4064 int addr, offset;
4066 size = type_size(&s->type, &align);
4067 loc = (loc - size) & -align;
4068 addr = loc;
4069 offset = 0;
4070 for (;;) {
4071 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4072 vswap();
4073 vstore();
4074 vtop--;
4075 if (--ret_nregs == 0)
4076 break;
4077 /* XXX: compatible with arm only: ret_align == register_size */
4078 offset += ret_align;
4080 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4082 } else {
4083 break;
4088 ST_FUNC void expr_prod(void)
4090 int t;
4092 unary();
4093 while (tok == '*' || tok == '/' || tok == '%') {
4094 t = tok;
4095 next();
4096 unary();
4097 gen_op(t);
4101 ST_FUNC void expr_sum(void)
4103 int t;
4105 expr_prod();
4106 while (tok == '+' || tok == '-') {
4107 t = tok;
4108 next();
4109 expr_prod();
4110 gen_op(t);
4114 static void expr_shift(void)
4116 int t;
4118 expr_sum();
4119 while (tok == TOK_SHL || tok == TOK_SAR) {
4120 t = tok;
4121 next();
4122 expr_sum();
4123 gen_op(t);
4127 static void expr_cmp(void)
4129 int t;
4131 expr_shift();
4132 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4133 tok == TOK_ULT || tok == TOK_UGE) {
4134 t = tok;
4135 next();
4136 expr_shift();
4137 gen_op(t);
4141 static void expr_cmpeq(void)
4143 int t;
4145 expr_cmp();
4146 while (tok == TOK_EQ || tok == TOK_NE) {
4147 t = tok;
4148 next();
4149 expr_cmp();
4150 gen_op(t);
4154 static void expr_and(void)
4156 expr_cmpeq();
4157 while (tok == '&') {
4158 next();
4159 expr_cmpeq();
4160 gen_op('&');
4164 static void expr_xor(void)
4166 expr_and();
4167 while (tok == '^') {
4168 next();
4169 expr_and();
4170 gen_op('^');
4174 static void expr_or(void)
4176 expr_xor();
4177 while (tok == '|') {
4178 next();
4179 expr_xor();
4180 gen_op('|');
4184 /* XXX: fix this mess */
4185 static void expr_land_const(void)
4187 expr_or();
4188 while (tok == TOK_LAND) {
4189 next();
4190 expr_or();
4191 gen_op(TOK_LAND);
4195 /* XXX: fix this mess */
4196 static void expr_lor_const(void)
4198 expr_land_const();
4199 while (tok == TOK_LOR) {
4200 next();
4201 expr_land_const();
4202 gen_op(TOK_LOR);
4206 /* only used if non constant */
4207 static void expr_land(void)
4209 int t;
4211 expr_or();
4212 if (tok == TOK_LAND) {
4213 t = 0;
4214 save_regs(1);
4215 for(;;) {
4216 t = gvtst(1, t);
4217 if (tok != TOK_LAND) {
4218 vseti(VT_JMPI, t);
4219 break;
4221 next();
4222 expr_or();
4227 static void expr_lor(void)
4229 int t;
4231 expr_land();
4232 if (tok == TOK_LOR) {
4233 t = 0;
4234 save_regs(1);
4235 for(;;) {
4236 t = gvtst(0, t);
4237 if (tok != TOK_LOR) {
4238 vseti(VT_JMP, t);
4239 break;
4241 next();
4242 expr_land();
4247 /* XXX: better constant handling */
4248 static void expr_cond(void)
4250 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4251 SValue sv;
4252 CType type, type1, type2;
4254 if (const_wanted) {
4255 expr_lor_const();
4256 if (tok == '?') {
4257 CType boolean;
4258 int c;
4259 boolean.t = VT_BOOL;
4260 vdup();
4261 gen_cast(&boolean);
4262 c = vtop->c.i;
4263 vpop();
4264 next();
4265 if (tok != ':' || !gnu_ext) {
4266 vpop();
4267 gexpr();
4269 if (!c)
4270 vpop();
4271 skip(':');
4272 expr_cond();
4273 if (c)
4274 vpop();
4276 } else {
4277 expr_lor();
4278 if (tok == '?') {
4279 next();
4280 if (vtop != vstack) {
4281 /* needed to avoid having different registers saved in
4282 each branch */
4283 if (is_float(vtop->type.t)) {
4284 rc = RC_FLOAT;
4285 #ifdef TCC_TARGET_X86_64
4286 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4287 rc = RC_ST0;
4289 #endif
4291 else
4292 rc = RC_INT;
4293 gv(rc);
4294 save_regs(1);
4296 if (tok == ':' && gnu_ext) {
4297 gv_dup();
4298 tt = gvtst(1, 0);
4299 } else {
4300 tt = gvtst(1, 0);
4301 gexpr();
4303 type1 = vtop->type;
4304 sv = *vtop; /* save value to handle it later */
4305 vtop--; /* no vpop so that FP stack is not flushed */
4306 skip(':');
4307 u = gjmp(0);
4308 gsym(tt);
4309 expr_cond();
4310 type2 = vtop->type;
4312 t1 = type1.t;
4313 bt1 = t1 & VT_BTYPE;
4314 t2 = type2.t;
4315 bt2 = t2 & VT_BTYPE;
4316 /* cast operands to correct type according to ISOC rules */
4317 if (is_float(bt1) || is_float(bt2)) {
4318 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4319 type.t = VT_LDOUBLE;
4320 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4321 type.t = VT_DOUBLE;
4322 } else {
4323 type.t = VT_FLOAT;
4325 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4326 /* cast to biggest op */
4327 type.t = VT_LLONG;
4328 /* convert to unsigned if it does not fit in a long long */
4329 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4330 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4331 type.t |= VT_UNSIGNED;
4332 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4333 /* If one is a null ptr constant the result type
4334 is the other. */
4335 if (is_null_pointer (vtop))
4336 type = type1;
4337 else if (is_null_pointer (&sv))
4338 type = type2;
4339 /* XXX: test pointer compatibility, C99 has more elaborate
4340 rules here. */
4341 else
4342 type = type1;
4343 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4344 /* XXX: test function pointer compatibility */
4345 type = bt1 == VT_FUNC ? type1 : type2;
4346 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4347 /* XXX: test structure compatibility */
4348 type = bt1 == VT_STRUCT ? type1 : type2;
4349 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4350 /* NOTE: as an extension, we accept void on only one side */
4351 type.t = VT_VOID;
4352 } else {
4353 /* integer operations */
4354 type.t = VT_INT;
4355 /* convert to unsigned if it does not fit in an integer */
4356 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4357 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4358 type.t |= VT_UNSIGNED;
4361 /* now we convert second operand */
4362 gen_cast(&type);
4363 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4364 gaddrof();
4365 rc = RC_INT;
4366 if (is_float(type.t)) {
4367 rc = RC_FLOAT;
4368 #ifdef TCC_TARGET_X86_64
4369 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4370 rc = RC_ST0;
4372 #endif
4373 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4374 /* for long longs, we use fixed registers to avoid having
4375 to handle a complicated move */
4376 rc = RC_IRET;
4379 r2 = gv(rc);
4380 /* this is horrible, but we must also convert first
4381 operand */
4382 tt = gjmp(0);
4383 gsym(u);
4384 /* put again first value and cast it */
4385 *vtop = sv;
4386 gen_cast(&type);
4387 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4388 gaddrof();
4389 r1 = gv(rc);
4390 move_reg(r2, r1, type.t);
4391 vtop->r = r2;
4392 gsym(tt);
4397 static void expr_eq(void)
4399 int t;
4401 expr_cond();
4402 if (tok == '=' ||
4403 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4404 tok == TOK_A_XOR || tok == TOK_A_OR ||
4405 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4406 test_lvalue();
4407 t = tok;
4408 next();
4409 if (t == '=') {
4410 expr_eq();
4411 } else {
4412 vdup();
4413 expr_eq();
4414 gen_op(t & 0x7f);
4416 vstore();
4420 ST_FUNC void gexpr(void)
4422 while (1) {
4423 expr_eq();
4424 if (tok != ',')
4425 break;
4426 vpop();
4427 next();
4431 /* parse an expression and return its type without any side effect. */
4432 static void expr_type(CType *type)
4434 int saved_nocode_wanted;
4436 saved_nocode_wanted = nocode_wanted;
4437 nocode_wanted = 1;
4438 gexpr();
4439 *type = vtop->type;
4440 vpop();
4441 nocode_wanted = saved_nocode_wanted;
4444 /* parse a unary expression and return its type without any side
4445 effect. */
4446 static void unary_type(CType *type)
4448 int a;
4450 a = nocode_wanted;
4451 nocode_wanted = 1;
4452 unary();
4453 *type = vtop->type;
4454 vpop();
4455 nocode_wanted = a;
4458 /* parse a constant expression and return value in vtop. */
4459 static void expr_const1(void)
4461 int a;
4462 a = const_wanted;
4463 const_wanted = 1;
4464 expr_cond();
4465 const_wanted = a;
4468 /* parse an integer constant and return its value. */
4469 ST_FUNC int expr_const(void)
4471 int c;
4472 expr_const1();
4473 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4474 expect("constant expression");
4475 c = vtop->c.i;
4476 vpop();
4477 return c;
4480 /* return the label token if current token is a label, otherwise
4481 return zero */
4482 static int is_label(void)
4484 int last_tok;
4486 /* fast test first */
4487 if (tok < TOK_UIDENT)
4488 return 0;
4489 /* no need to save tokc because tok is an identifier */
4490 last_tok = tok;
4491 next();
4492 if (tok == ':') {
4493 next();
4494 return last_tok;
4495 } else {
4496 unget_tok(last_tok);
4497 return 0;
4501 static void label_or_decl(int l)
4503 int last_tok;
4505 /* fast test first */
4506 if (tok >= TOK_UIDENT)
4508 /* no need to save tokc because tok is an identifier */
4509 last_tok = tok;
4510 next();
4511 if (tok == ':') {
4512 unget_tok(last_tok);
4513 return;
4515 unget_tok(last_tok);
4517 decl(l);
4520 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4521 int case_reg, int is_expr)
4523 int a, b, c, d;
4524 Sym *s, *frame_bottom;
4526 /* generate line number info */
4527 if (tcc_state->do_debug &&
4528 (last_line_num != file->line_num || last_ind != ind)) {
4529 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4530 last_ind = ind;
4531 last_line_num = file->line_num;
4534 if (is_expr) {
4535 /* default return value is (void) */
4536 vpushi(0);
4537 vtop->type.t = VT_VOID;
4540 if (tok == TOK_IF) {
4541 /* if test */
4542 next();
4543 skip('(');
4544 gexpr();
4545 skip(')');
4546 a = gvtst(1, 0);
4547 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4548 c = tok;
4549 if (c == TOK_ELSE) {
4550 next();
4551 d = gjmp(0);
4552 gsym(a);
4553 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4554 gsym(d); /* patch else jmp */
4555 } else
4556 gsym(a);
4557 } else if (tok == TOK_WHILE) {
4558 next();
4559 d = ind;
4560 skip('(');
4561 gexpr();
4562 skip(')');
4563 a = gvtst(1, 0);
4564 b = 0;
4565 block(&a, &b, case_sym, def_sym, case_reg, 0);
4566 gjmp_addr(d);
4567 gsym(a);
4568 gsym_addr(b, d);
4569 } else if (tok == '{') {
4570 Sym *llabel;
4571 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4573 next();
4574 /* record local declaration stack position */
4575 s = local_stack;
4576 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4577 frame_bottom->next = scope_stack_bottom;
4578 scope_stack_bottom = frame_bottom;
4579 llabel = local_label_stack;
4581 /* save VLA state */
4582 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4583 if (saved_vla_sp_loc != &vla_sp_root_loc)
4584 vla_sp_loc = &block_vla_sp_loc;
4586 saved_vla_flags = vla_flags;
4587 vla_flags |= VLA_NEED_NEW_FRAME;
4589 /* handle local labels declarations */
4590 if (tok == TOK_LABEL) {
4591 next();
4592 for(;;) {
4593 if (tok < TOK_UIDENT)
4594 expect("label identifier");
4595 label_push(&local_label_stack, tok, LABEL_DECLARED);
4596 next();
4597 if (tok == ',') {
4598 next();
4599 } else {
4600 skip(';');
4601 break;
4605 while (tok != '}') {
4606 label_or_decl(VT_LOCAL);
4607 if (tok != '}') {
4608 if (is_expr)
4609 vpop();
4610 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4613 /* pop locally defined labels */
4614 label_pop(&local_label_stack, llabel);
4615 if(is_expr) {
4616 /* XXX: this solution makes only valgrind happy...
4617 triggered by gcc.c-torture/execute/20000917-1.c */
4618 Sym *p;
4619 switch(vtop->type.t & VT_BTYPE) {
4620 case VT_PTR:
4621 case VT_STRUCT:
4622 case VT_ENUM:
4623 case VT_FUNC:
4624 for(p=vtop->type.ref;p;p=p->prev)
4625 if(p->prev==s)
4626 tcc_error("unsupported expression type");
4629 /* pop locally defined symbols */
4630 scope_stack_bottom = scope_stack_bottom->next;
4631 sym_pop(&local_stack, s);
4633 /* Pop VLA frames and restore stack pointer if required */
4634 if (saved_vla_sp_loc != &vla_sp_root_loc)
4635 *saved_vla_sp_loc = block_vla_sp_loc;
4636 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4637 vla_sp_loc = saved_vla_sp_loc;
4638 gen_vla_sp_restore(*vla_sp_loc);
4640 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4642 next();
4643 } else if (tok == TOK_RETURN) {
4644 next();
4645 if (tok != ';') {
4646 gexpr();
4647 gen_assign_cast(&func_vt);
4648 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4649 CType type, ret_type;
4650 int ret_align, ret_nregs;
4651 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4652 &ret_align);
4653 if (0 == ret_nregs) {
4654 /* if returning structure, must copy it to implicit
4655 first pointer arg location */
4656 type = func_vt;
4657 mk_pointer(&type);
4658 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4659 indir();
4660 vswap();
4661 /* copy structure value to pointer */
4662 vstore();
4663 } else {
4664 /* returning structure packed into registers */
4665 int r, size, addr, align;
4666 size = type_size(&func_vt,&align);
4667 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4668 && (align & (ret_align-1))) {
4669 loc = (loc - size) & -align;
4670 addr = loc;
4671 type = func_vt;
4672 vset(&type, VT_LOCAL | VT_LVAL, addr);
4673 vswap();
4674 vstore();
4675 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4677 vtop->type = ret_type;
4678 if (is_float(ret_type.t))
4679 r = rc_fret(ret_type.t);
4680 else
4681 r = RC_IRET;
4683 for (;;) {
4684 gv(r);
4685 if (--ret_nregs == 0)
4686 break;
4687 /* We assume that when a structure is returned in multiple
4688 registers, their classes are consecutive values of the
4689 suite s(n) = 2^n */
4690 r <<= 1;
4691 /* XXX: compatible with arm only: ret_align == register_size */
4692 vtop->c.i += ret_align;
4693 vtop->r = VT_LOCAL | VT_LVAL;
4696 } else if (is_float(func_vt.t)) {
4697 gv(rc_fret(func_vt.t));
4698 } else {
4699 gv(RC_IRET);
4701 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4703 skip(';');
4704 rsym = gjmp(rsym); /* jmp */
4705 } else if (tok == TOK_BREAK) {
4706 /* compute jump */
4707 if (!bsym)
4708 tcc_error("cannot break");
4709 *bsym = gjmp(*bsym);
4710 next();
4711 skip(';');
4712 } else if (tok == TOK_CONTINUE) {
4713 /* compute jump */
4714 if (!csym)
4715 tcc_error("cannot continue");
4716 *csym = gjmp(*csym);
4717 next();
4718 skip(';');
4719 } else if (tok == TOK_FOR) {
4720 int e;
4721 next();
4722 skip('(');
4723 s = local_stack;
4724 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4725 frame_bottom->next = scope_stack_bottom;
4726 scope_stack_bottom = frame_bottom;
4727 if (tok != ';') {
4728 /* c99 for-loop init decl? */
4729 if (!decl0(VT_LOCAL, 1)) {
4730 /* no, regular for-loop init expr */
4731 gexpr();
4732 vpop();
4735 skip(';');
4736 d = ind;
4737 c = ind;
4738 a = 0;
4739 b = 0;
4740 if (tok != ';') {
4741 gexpr();
4742 a = gvtst(1, 0);
4744 skip(';');
4745 if (tok != ')') {
4746 e = gjmp(0);
4747 c = ind;
4748 gexpr();
4749 vpop();
4750 gjmp_addr(d);
4751 gsym(e);
4753 skip(')');
4754 block(&a, &b, case_sym, def_sym, case_reg, 0);
4755 gjmp_addr(c);
4756 gsym(a);
4757 gsym_addr(b, c);
4758 scope_stack_bottom = scope_stack_bottom->next;
4759 sym_pop(&local_stack, s);
4760 } else
4761 if (tok == TOK_DO) {
4762 next();
4763 a = 0;
4764 b = 0;
4765 d = ind;
4766 block(&a, &b, case_sym, def_sym, case_reg, 0);
4767 skip(TOK_WHILE);
4768 skip('(');
4769 gsym(b);
4770 gexpr();
4771 c = gvtst(0, 0);
4772 gsym_addr(c, d);
4773 skip(')');
4774 gsym(a);
4775 skip(';');
4776 } else
4777 if (tok == TOK_SWITCH) {
4778 next();
4779 skip('(');
4780 gexpr();
4781 /* XXX: other types than integer */
4782 case_reg = gv(RC_INT);
4783 vpop();
4784 skip(')');
4785 a = 0;
4786 b = gjmp(0); /* jump to first case */
4787 c = 0;
4788 block(&a, csym, &b, &c, case_reg, 0);
4789 /* if no default, jmp after switch */
4790 if (c == 0)
4791 c = ind;
4792 /* default label */
4793 gsym_addr(b, c);
4794 /* break label */
4795 gsym(a);
4796 } else
4797 if (tok == TOK_CASE) {
4798 int v1, v2;
4799 if (!case_sym)
4800 expect("switch");
4801 next();
4802 v1 = expr_const();
4803 v2 = v1;
4804 if (gnu_ext && tok == TOK_DOTS) {
4805 next();
4806 v2 = expr_const();
4807 if (v2 < v1)
4808 tcc_warning("empty case range");
4810 /* since a case is like a label, we must skip it with a jmp */
4811 b = gjmp(0);
4812 gsym(*case_sym);
4813 vseti(case_reg, 0);
4814 vpushi(v1);
4815 if (v1 == v2) {
4816 gen_op(TOK_EQ);
4817 *case_sym = gtst(1, 0);
4818 } else {
4819 gen_op(TOK_GE);
4820 *case_sym = gtst(1, 0);
4821 vseti(case_reg, 0);
4822 vpushi(v2);
4823 gen_op(TOK_LE);
4824 *case_sym = gtst(1, *case_sym);
4826 gsym(b);
4827 skip(':');
4828 is_expr = 0;
4829 goto block_after_label;
4830 } else
4831 if (tok == TOK_DEFAULT) {
4832 next();
4833 skip(':');
4834 if (!def_sym)
4835 expect("switch");
4836 if (*def_sym)
4837 tcc_error("too many 'default'");
4838 *def_sym = ind;
4839 is_expr = 0;
4840 goto block_after_label;
4841 } else
4842 if (tok == TOK_GOTO) {
4843 next();
4844 if (tok == '*' && gnu_ext) {
4845 /* computed goto */
4846 next();
4847 gexpr();
4848 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4849 expect("pointer");
4850 ggoto();
4851 } else if (tok >= TOK_UIDENT) {
4852 s = label_find(tok);
4853 /* put forward definition if needed */
4854 if (!s) {
4855 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4856 } else {
4857 if (s->r == LABEL_DECLARED)
4858 s->r = LABEL_FORWARD;
4860 /* label already defined */
4861 if (vla_flags & VLA_IN_SCOPE) {
4862 /* If VLAs are in use, save the current stack pointer and
4863 reset the stack pointer to what it was at function entry
4864 (label will restore stack pointer in inner scopes) */
4865 vla_sp_save();
4866 gen_vla_sp_restore(vla_sp_root_loc);
4868 if (s->r & LABEL_FORWARD)
4869 s->jnext = gjmp(s->jnext);
4870 else
4871 gjmp_addr(s->jnext);
4872 next();
4873 } else {
4874 expect("label identifier");
4876 skip(';');
4877 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4878 asm_instr();
4879 } else {
4880 b = is_label();
4881 if (b) {
4882 /* label case */
4883 if (vla_flags & VLA_IN_SCOPE) {
4884 /* save/restore stack pointer across label
4885 this is a no-op when combined with the load immediately
4886 after the label unless we arrive via goto */
4887 vla_sp_save();
4889 s = label_find(b);
4890 if (s) {
4891 if (s->r == LABEL_DEFINED)
4892 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4893 gsym(s->jnext);
4894 s->r = LABEL_DEFINED;
4895 } else {
4896 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4898 s->jnext = ind;
4899 if (vla_flags & VLA_IN_SCOPE) {
4900 gen_vla_sp_restore(*vla_sp_loc);
4901 vla_flags |= VLA_NEED_NEW_FRAME;
4903 /* we accept this, but it is a mistake */
4904 block_after_label:
4905 if (tok == '}') {
4906 tcc_warning("deprecated use of label at end of compound statement");
4907 } else {
4908 if (is_expr)
4909 vpop();
4910 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4912 } else {
4913 /* expression case */
4914 if (tok != ';') {
4915 if (is_expr) {
4916 vpop();
4917 gexpr();
4918 } else {
4919 gexpr();
4920 vpop();
4923 skip(';');
4928 /* t is the array or struct type. c is the array or struct
4929 address. cur_index/cur_field is the pointer to the current
4930 value. 'size_only' is true if only size info is needed (only used
4931 in arrays) */
4932 static void decl_designator(CType *type, Section *sec, unsigned long c,
4933 int *cur_index, Sym **cur_field,
4934 int size_only)
4936 Sym *s, *f;
4937 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4938 CType type1;
4940 notfirst = 0;
4941 elem_size = 0;
4942 nb_elems = 1;
4943 if (gnu_ext && (l = is_label()) != 0)
4944 goto struct_field;
4945 while (tok == '[' || tok == '.') {
4946 if (tok == '[') {
4947 if (!(type->t & VT_ARRAY))
4948 expect("array type");
4949 s = type->ref;
4950 next();
4951 index = expr_const();
4952 if (index < 0 || (s->c >= 0 && index >= s->c))
4953 expect("invalid index");
4954 if (tok == TOK_DOTS && gnu_ext) {
4955 next();
4956 index_last = expr_const();
4957 if (index_last < 0 ||
4958 (s->c >= 0 && index_last >= s->c) ||
4959 index_last < index)
4960 expect("invalid index");
4961 } else {
4962 index_last = index;
4964 skip(']');
4965 if (!notfirst)
4966 *cur_index = index_last;
4967 type = pointed_type(type);
4968 elem_size = type_size(type, &align);
4969 c += index * elem_size;
4970 /* NOTE: we only support ranges for last designator */
4971 nb_elems = index_last - index + 1;
4972 if (nb_elems != 1) {
4973 notfirst = 1;
4974 break;
4976 } else {
4977 next();
4978 l = tok;
4979 next();
4980 struct_field:
4981 if ((type->t & VT_BTYPE) != VT_STRUCT)
4982 expect("struct/union type");
4983 s = type->ref;
4984 l |= SYM_FIELD;
4985 f = s->next;
4986 while (f) {
4987 if (f->v == l)
4988 break;
4989 f = f->next;
4991 if (!f)
4992 expect("field");
4993 if (!notfirst)
4994 *cur_field = f;
4995 /* XXX: fix this mess by using explicit storage field */
4996 type1 = f->type;
4997 type1.t |= (type->t & ~VT_TYPE);
4998 type = &type1;
4999 c += f->c;
5001 notfirst = 1;
5003 if (notfirst) {
5004 if (tok == '=') {
5005 next();
5006 } else {
5007 if (!gnu_ext)
5008 expect("=");
5010 } else {
5011 if (type->t & VT_ARRAY) {
5012 index = *cur_index;
5013 type = pointed_type(type);
5014 c += index * type_size(type, &align);
5015 } else {
5016 f = *cur_field;
5017 if (!f)
5018 tcc_error("too many field init");
5019 /* XXX: fix this mess by using explicit storage field */
5020 type1 = f->type;
5021 type1.t |= (type->t & ~VT_TYPE);
5022 type = &type1;
5023 c += f->c;
5026 decl_initializer(type, sec, c, 0, size_only);
5028 /* XXX: make it more general */
5029 if (!size_only && nb_elems > 1) {
5030 unsigned long c_end;
5031 uint8_t *src, *dst;
5032 int i;
5034 if (!sec)
5035 tcc_error("range init not supported yet for dynamic storage");
5036 c_end = c + nb_elems * elem_size;
5037 if (c_end > sec->data_allocated)
5038 section_realloc(sec, c_end);
5039 src = sec->data + c;
5040 dst = src;
5041 for(i = 1; i < nb_elems; i++) {
5042 dst += elem_size;
5043 memcpy(dst, src, elem_size);
5048 #define EXPR_VAL 0
5049 #define EXPR_CONST 1
5050 #define EXPR_ANY 2
5052 /* store a value or an expression directly in global data or in local array */
5053 static void init_putv(CType *type, Section *sec, unsigned long c,
5054 int v, int expr_type)
5056 int saved_global_expr, bt, bit_pos, bit_size;
5057 void *ptr;
5058 unsigned long long bit_mask;
5059 CType dtype;
5061 switch(expr_type) {
5062 case EXPR_VAL:
5063 vpushi(v);
5064 break;
5065 case EXPR_CONST:
5066 /* compound literals must be allocated globally in this case */
5067 saved_global_expr = global_expr;
5068 global_expr = 1;
5069 expr_const1();
5070 global_expr = saved_global_expr;
5071 /* NOTE: symbols are accepted */
5072 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5073 tcc_error("initializer element is not constant");
5074 break;
5075 case EXPR_ANY:
5076 expr_eq();
5077 break;
5080 dtype = *type;
5081 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5083 if (sec) {
5084 /* XXX: not portable */
5085 /* XXX: generate error if incorrect relocation */
5086 gen_assign_cast(&dtype);
5087 bt = type->t & VT_BTYPE;
5088 /* we'll write at most 12 bytes */
5089 if (c + 12 > sec->data_allocated) {
5090 section_realloc(sec, c + 12);
5092 ptr = sec->data + c;
5093 /* XXX: make code faster ? */
5094 if (!(type->t & VT_BITFIELD)) {
5095 bit_pos = 0;
5096 bit_size = 32;
5097 bit_mask = -1LL;
5098 } else {
5099 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5100 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5101 bit_mask = (1LL << bit_size) - 1;
5103 if ((vtop->r & VT_SYM) &&
5104 (bt == VT_BYTE ||
5105 bt == VT_SHORT ||
5106 bt == VT_DOUBLE ||
5107 bt == VT_LDOUBLE ||
5108 bt == VT_LLONG ||
5109 (bt == VT_INT && bit_size != 32)))
5110 tcc_error("initializer element is not computable at load time");
5111 switch(bt) {
5112 case VT_BOOL:
5113 vtop->c.i = (vtop->c.i != 0);
5114 case VT_BYTE:
5115 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5116 break;
5117 case VT_SHORT:
5118 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5119 break;
5120 case VT_DOUBLE:
5121 *(double *)ptr = vtop->c.d;
5122 break;
5123 case VT_LDOUBLE:
5124 *(long double *)ptr = vtop->c.ld;
5125 break;
5126 case VT_LLONG:
5127 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5128 break;
5129 case VT_PTR:
5130 if (s1->vtop->r & VT_SYM) {
5131 greloc(s1, sec, s1->vtop->sym, c, R_DATA_PTR);
5133 *(addr_t *)ptr |= (s1->vtop->c.ull & bit_mask) << bit_pos;
5134 break;
5135 default:
5136 if (vtop->r & VT_SYM) {
5137 greloc(sec, vtop->sym, c, R_DATA_PTR);
5139 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5140 break;
5142 vtop--;
5143 } else {
5144 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5145 vswap();
5146 vstore();
5147 vpop();
5151 /* put zeros for variable based init */
5152 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5154 if (sec) {
5155 /* nothing to do because globals are already set to zero */
5156 } else {
5157 vpush_global_sym(&func_old_type, TOK_memset);
5158 vseti(VT_LOCAL, c);
5159 vpushi(0);
5160 vpushs(size);
5161 gfunc_call(3);
5165 /* 't' contains the type and storage info. 'c' is the offset of the
5166 object in section 'sec'. If 'sec' is NULL, it means stack based
5167 allocation. 'first' is true if array '{' must be read (multi
5168 dimension implicit array init handling). 'size_only' is true if
5169 size only evaluation is wanted (only for arrays). */
5170 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5171 int first, int size_only)
5173 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5174 int size1, align1, expr_type;
5175 Sym *s, *f;
5176 CType *t1;
5178 if (type->t & VT_VLA) {
5179 int a;
5181 /* save current stack pointer */
5182 if (vla_flags & VLA_NEED_NEW_FRAME) {
5183 vla_sp_save();
5184 vla_flags = VLA_IN_SCOPE;
5185 vla_sp_loc = &vla_sp_loc_tmp;
5188 vla_runtime_type_size(type, &a);
5189 gen_vla_alloc(type, a);
5190 vset(type, VT_LOCAL|VT_LVAL, c);
5191 vswap();
5192 vstore();
5193 vpop();
5194 } else if (type->t & VT_ARRAY) {
5195 s = type->ref;
5196 n = s->c;
5197 array_length = 0;
5198 t1 = pointed_type(type);
5199 size1 = type_size(t1, &align1);
5201 no_oblock = 1;
5202 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5203 tok == '{') {
5204 if (tok != '{')
5205 tcc_error("character array initializer must be a literal,"
5206 " optionally enclosed in braces");
5207 skip('{');
5208 no_oblock = 0;
5211 /* only parse strings here if correct type (otherwise: handle
5212 them as ((w)char *) expressions */
5213 if ((tok == TOK_LSTR &&
5214 #ifdef TCC_TARGET_PE
5215 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5216 #else
5217 (t1->t & VT_BTYPE) == VT_INT
5218 #endif
5219 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5220 while (tok == TOK_STR || tok == TOK_LSTR) {
5221 int cstr_len, ch;
5222 CString *cstr;
5224 cstr = tokc.cstr;
5225 /* compute maximum number of chars wanted */
5226 if (tok == TOK_STR)
5227 cstr_len = cstr->size;
5228 else
5229 cstr_len = cstr->size / sizeof(nwchar_t);
5230 cstr_len--;
5231 nb = cstr_len;
5232 if (n >= 0 && nb > (n - array_length))
5233 nb = n - array_length;
5234 if (!size_only) {
5235 if (cstr_len > nb)
5236 tcc_warning("initializer-string for array is too long");
5237 /* in order to go faster for common case (char
5238 string in global variable, we handle it
5239 specifically */
5240 if (sec && tok == TOK_STR && size1 == 1) {
5241 memcpy(sec->data + c + array_length, cstr->data, nb);
5242 } else {
5243 for(i=0;i<nb;i++) {
5244 if (tok == TOK_STR)
5245 ch = ((unsigned char *)cstr->data)[i];
5246 else
5247 ch = ((nwchar_t *)cstr->data)[i];
5248 init_putv(t1, sec, c + (array_length + i) * size1,
5249 ch, EXPR_VAL);
5253 array_length += nb;
5254 next();
5256 /* only add trailing zero if enough storage (no
5257 warning in this case since it is standard) */
5258 if (n < 0 || array_length < n) {
5259 if (!size_only) {
5260 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5262 array_length++;
5264 } else {
5265 index = 0;
5266 while (tok != '}') {
5267 decl_designator(type, sec, c, &index, NULL, size_only);
5268 if (n >= 0 && index >= n)
5269 tcc_error("index too large");
5270 /* must put zero in holes (note that doing it that way
5271 ensures that it even works with designators) */
5272 if (!size_only && array_length < index) {
5273 init_putz(t1, sec, c + array_length * size1,
5274 (index - array_length) * size1);
5276 index++;
5277 if (index > array_length)
5278 array_length = index;
5279 /* special test for multi dimensional arrays (may not
5280 be strictly correct if designators are used at the
5281 same time) */
5282 if (index >= n && no_oblock)
5283 break;
5284 if (tok == '}')
5285 break;
5286 skip(',');
5289 if (!no_oblock)
5290 skip('}');
5291 /* put zeros at the end */
5292 if (!size_only && n >= 0 && array_length < n) {
5293 init_putz(t1, sec, c + array_length * size1,
5294 (n - array_length) * size1);
5296 /* patch type size if needed */
5297 if (n < 0)
5298 s->c = array_length;
5299 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5300 (sec || !first || tok == '{')) {
5301 int par_count;
5303 /* NOTE: the previous test is a specific case for automatic
5304 struct/union init */
5305 /* XXX: union needs only one init */
5307 /* XXX: this test is incorrect for local initializers
5308 beginning with ( without {. It would be much more difficult
5309 to do it correctly (ideally, the expression parser should
5310 be used in all cases) */
5311 par_count = 0;
5312 if (tok == '(') {
5313 AttributeDef ad1;
5314 CType type1;
5315 next();
5316 while (tok == '(') {
5317 par_count++;
5318 next();
5320 if (!parse_btype(&type1, &ad1))
5321 expect("cast");
5322 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5323 #if 0
5324 if (!is_assignable_types(type, &type1))
5325 tcc_error("invalid type for cast");
5326 #endif
5327 skip(')');
5329 no_oblock = 1;
5330 if (first || tok == '{') {
5331 skip('{');
5332 no_oblock = 0;
5334 s = type->ref;
5335 f = s->next;
5336 array_length = 0;
5337 index = 0;
5338 n = s->c;
5339 while (tok != '}') {
5340 decl_designator(type, sec, c, NULL, &f, size_only);
5341 index = f->c;
5342 if (!size_only && array_length < index) {
5343 init_putz(type, sec, c + array_length,
5344 index - array_length);
5346 index = index + type_size(&f->type, &align1);
5347 if (index > array_length)
5348 array_length = index;
5350 /* gr: skip fields from same union - ugly. */
5351 while (f->next) {
5352 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5353 /* test for same offset */
5354 if (f->next->c != f->c)
5355 break;
5356 /* if yes, test for bitfield shift */
5357 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5358 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5359 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5360 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5361 if (bit_pos_1 != bit_pos_2)
5362 break;
5364 f = f->next;
5367 f = f->next;
5368 if (no_oblock && f == NULL)
5369 break;
5370 if (tok == '}')
5371 break;
5372 skip(',');
5374 /* put zeros at the end */
5375 if (!size_only && array_length < n) {
5376 init_putz(type, sec, c + array_length,
5377 n - array_length);
5379 if (!no_oblock)
5380 skip('}');
5381 while (par_count) {
5382 skip(')');
5383 par_count--;
5385 } else if (tok == '{') {
5386 next();
5387 decl_initializer(type, sec, c, first, size_only);
5388 skip('}');
5389 } else if (size_only) {
5390 /* just skip expression */
5391 parlevel = parlevel1 = 0;
5392 while ((parlevel > 0 || parlevel1 > 0 ||
5393 (tok != '}' && tok != ',')) && tok != -1) {
5394 if (tok == '(')
5395 parlevel++;
5396 else if (tok == ')')
5397 parlevel--;
5398 else if (tok == '{')
5399 parlevel1++;
5400 else if (tok == '}')
5401 parlevel1--;
5402 next();
5404 } else {
5405 /* currently, we always use constant expression for globals
5406 (may change for scripting case) */
5407 expr_type = EXPR_CONST;
5408 if (!sec)
5409 expr_type = EXPR_ANY;
5410 init_putv(type, sec, c, 0, expr_type);
5414 /* parse an initializer for type 't' if 'has_init' is non zero, and
5415 allocate space in local or global data space ('r' is either
5416 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5417 variable 'v' with an associated name represented by 'asm_label' of
5418 scope 'scope' is declared before initializers are parsed. If 'v' is
5419 zero, then a reference to the new object is put in the value stack.
5420 If 'has_init' is 2, a special parsing is done to handle string
5421 constants. */
5422 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5423 int has_init, int v, char *asm_label,
5424 int scope)
5426 int size, align, addr, data_offset;
5427 int level;
5428 ParseState saved_parse_state = {0};
5429 TokenString init_str;
5430 Section *sec;
5431 Sym *flexible_array;
5433 flexible_array = NULL;
5434 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5435 Sym *field = type->ref->next;
5436 if (field) {
5437 while (field->next)
5438 field = field->next;
5439 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5440 flexible_array = field;
5444 size = type_size(type, &align);
5445 /* If unknown size, we must evaluate it before
5446 evaluating initializers because
5447 initializers can generate global data too
5448 (e.g. string pointers or ISOC99 compound
5449 literals). It also simplifies local
5450 initializers handling */
5451 tok_str_new(&init_str);
5452 if (size < 0 || (flexible_array && has_init)) {
5453 if (!has_init)
5454 tcc_error("unknown type size");
5455 /* get all init string */
5456 if (has_init == 2) {
5457 /* only get strings */
5458 while (tok == TOK_STR || tok == TOK_LSTR) {
5459 tok_str_add_tok(&init_str);
5460 next();
5462 } else {
5463 level = 0;
5464 while (level > 0 || (tok != ',' && tok != ';')) {
5465 if (tok < 0)
5466 tcc_error("unexpected end of file in initializer");
5467 tok_str_add_tok(&init_str);
5468 if (tok == '{')
5469 level++;
5470 else if (tok == '}') {
5471 level--;
5472 if (level <= 0) {
5473 next();
5474 break;
5477 next();
5480 tok_str_add(&init_str, -1);
5481 tok_str_add(&init_str, 0);
5483 /* compute size */
5484 save_parse_state(&saved_parse_state);
5486 macro_ptr = init_str.str;
5487 next();
5488 decl_initializer(type, NULL, 0, 1, 1);
5489 /* prepare second initializer parsing */
5490 macro_ptr = init_str.str;
5491 next();
5493 /* if still unknown size, error */
5494 size = type_size(type, &align);
5495 if (size < 0)
5496 tcc_error("unknown type size");
5498 if (flexible_array)
5499 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5500 /* take into account specified alignment if bigger */
5501 if (ad->a.aligned) {
5502 if (ad->a.aligned > align)
5503 align = ad->a.aligned;
5504 } else if (ad->a.packed) {
5505 align = 1;
5507 if ((r & VT_VALMASK) == VT_LOCAL) {
5508 sec = NULL;
5509 #ifdef CONFIG_TCC_BCHECK
5510 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5511 loc--;
5513 #endif
5514 loc = (loc - size) & -align;
5515 addr = loc;
5516 #ifdef CONFIG_TCC_BCHECK
5517 /* handles bounds */
5518 /* XXX: currently, since we do only one pass, we cannot track
5519 '&' operators, so we add only arrays */
5520 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5521 unsigned long *bounds_ptr;
5522 /* add padding between regions */
5523 loc--;
5524 /* then add local bound info */
5525 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5526 bounds_ptr[0] = addr;
5527 bounds_ptr[1] = size;
5529 #endif
5530 if (v) {
5531 /* local variable */
5532 sym_push(v, type, r, addr);
5533 } else {
5534 /* push local reference */
5535 vset(type, r, addr);
5537 } else {
5538 Sym *sym;
5540 sym = NULL;
5541 if (v && scope == VT_CONST) {
5542 /* see if the symbol was already defined */
5543 sym = sym_find(v);
5544 if (sym) {
5545 if (!is_compatible_types(&sym->type, type))
5546 tcc_error("incompatible types for redefinition of '%s'",
5547 get_tok_str(v, NULL));
5548 if (sym->type.t & VT_EXTERN) {
5549 /* if the variable is extern, it was not allocated */
5550 sym->type.t &= ~VT_EXTERN;
5551 /* set array size if it was ommited in extern
5552 declaration */
5553 if ((sym->type.t & VT_ARRAY) &&
5554 sym->type.ref->c < 0 &&
5555 type->ref->c >= 0)
5556 sym->type.ref->c = type->ref->c;
5557 } else {
5558 /* we accept several definitions of the same
5559 global variable. this is tricky, because we
5560 must play with the SHN_COMMON type of the symbol */
5561 /* XXX: should check if the variable was already
5562 initialized. It is incorrect to initialized it
5563 twice */
5564 /* no init data, we won't add more to the symbol */
5565 if (!has_init)
5566 goto no_alloc;
5571 /* allocate symbol in corresponding section */
5572 sec = ad->section;
5573 if (!sec) {
5574 if (has_init)
5575 sec = data_section;
5576 else if (tcc_state->nocommon)
5577 sec = bss_section;
5579 if (sec) {
5580 data_offset = sec->data_offset;
5581 data_offset = (data_offset + align - 1) & -align;
5582 addr = data_offset;
5583 /* very important to increment global pointer at this time
5584 because initializers themselves can create new initializers */
5585 data_offset += size;
5586 #ifdef CONFIG_TCC_BCHECK
5587 /* add padding if bound check */
5588 if (tcc_state->do_bounds_check)
5589 data_offset++;
5590 #endif
5591 sec->data_offset = data_offset;
5592 /* allocate section space to put the data */
5593 if (sec->sh_type != SHT_NOBITS &&
5594 data_offset > sec->data_allocated)
5595 section_realloc(sec, data_offset);
5596 /* align section if needed */
5597 if (align > sec->sh_addralign)
5598 sec->sh_addralign = align;
5599 } else {
5600 addr = 0; /* avoid warning */
5603 if (v) {
5604 if (scope != VT_CONST || !sym) {
5605 sym = sym_push(v, type, r | VT_SYM, 0);
5606 sym->asm_label = asm_label;
5608 /* update symbol definition */
5609 if (sec) {
5610 put_extern_sym(sym, sec, addr, size);
5611 } else {
5612 ElfW(Sym) *esym;
5613 /* put a common area */
5614 put_extern_sym(sym, NULL, align, size);
5615 /* XXX: find a nicer way */
5616 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5617 esym->st_shndx = SHN_COMMON;
5619 } else {
5620 CValue cval;
5622 /* push global reference */
5623 sym = get_sym_ref(type, sec, addr, size);
5624 cval.ul = 0;
5625 vsetc(type, VT_CONST | VT_SYM, &cval);
5626 vtop->sym = sym;
5628 /* patch symbol weakness */
5629 if (type->t & VT_WEAK)
5630 weaken_symbol(sym);
5631 #ifdef CONFIG_TCC_BCHECK
5632 /* handles bounds now because the symbol must be defined
5633 before for the relocation */
5634 if (tcc_state->do_bounds_check) {
5635 unsigned long *bounds_ptr;
5637 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5638 /* then add global bound info */
5639 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5640 bounds_ptr[0] = 0; /* relocated */
5641 bounds_ptr[1] = size;
5643 #endif
5645 if (has_init || (type->t & VT_VLA)) {
5646 decl_initializer(type, sec, addr, 1, 0);
5647 /* restore parse state if needed */
5648 if (init_str.str) {
5649 tok_str_free(init_str.str);
5650 restore_parse_state(&saved_parse_state);
5652 /* patch flexible array member size back to -1, */
5653 /* for possible subsequent similar declarations */
5654 if (flexible_array)
5655 flexible_array->type.ref->c = -1;
5657 no_alloc: ;
5660 static void put_func_debug(Sym *sym)
5662 char buf[512];
5664 /* stabs info */
5665 /* XXX: we put here a dummy type */
5666 snprintf(buf, sizeof(buf), "%s:%c1",
5667 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5668 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5669 cur_text_section, sym->c);
5670 /* //gr gdb wants a line at the function */
5671 put_stabn(N_SLINE, 0, file->line_num, 0);
5672 last_ind = 0;
5673 last_line_num = 0;
5676 /* parse an old style function declaration list */
5677 /* XXX: check multiple parameter */
5678 static void func_decl_list(Sym *func_sym)
5680 AttributeDef ad;
5681 int v;
5682 Sym *s;
5683 CType btype, type;
5685 /* parse each declaration */
5686 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5687 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5688 if (!parse_btype(&btype, &ad))
5689 expect("declaration list");
5690 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5691 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5692 tok == ';') {
5693 /* we accept no variable after */
5694 } else {
5695 for(;;) {
5696 type = btype;
5697 type_decl(&type, &ad, &v, TYPE_DIRECT);
5698 /* find parameter in function parameter list */
5699 s = func_sym->next;
5700 while (s != NULL) {
5701 if ((s->v & ~SYM_FIELD) == v)
5702 goto found;
5703 s = s->next;
5705 tcc_error("declaration for parameter '%s' but no such parameter",
5706 get_tok_str(v, NULL));
5707 found:
5708 /* check that no storage specifier except 'register' was given */
5709 if (type.t & VT_STORAGE)
5710 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5711 convert_parameter_type(&type);
5712 /* we can add the type (NOTE: it could be local to the function) */
5713 s->type = type;
5714 /* accept other parameters */
5715 if (tok == ',')
5716 next();
5717 else
5718 break;
5721 skip(';');
5725 /* parse a function defined by symbol 'sym' and generate its code in
5726 'cur_text_section' */
5727 static void gen_function(Sym *sym)
5729 int saved_nocode_wanted = nocode_wanted;
5730 nocode_wanted = 0;
5731 ind = cur_text_section->data_offset;
5732 /* NOTE: we patch the symbol size later */
5733 put_extern_sym(sym, cur_text_section, ind, 0);
5734 funcname = get_tok_str(sym->v, NULL);
5735 func_ind = ind;
5736 /* Initialize VLA state */
5737 vla_sp_loc = &vla_sp_root_loc;
5738 vla_flags = VLA_NEED_NEW_FRAME;
5739 /* put debug symbol */
5740 if (tcc_state->do_debug)
5741 put_func_debug(sym);
5742 /* push a dummy symbol to enable local sym storage */
5743 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5744 gfunc_prolog(&sym->type);
5745 rsym = 0;
5746 block(NULL, NULL, NULL, NULL, 0, 0);
5747 gsym(rsym);
5748 gfunc_epilog();
5749 cur_text_section->data_offset = ind;
5750 label_pop(&global_label_stack, NULL);
5751 /* reset local stack */
5752 scope_stack_bottom = NULL;
5753 sym_pop(&local_stack, NULL);
5754 /* end of function */
5755 /* patch symbol size */
5756 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5757 ind - func_ind;
5758 /* patch symbol weakness (this definition overrules any prototype) */
5759 if (sym->type.t & VT_WEAK)
5760 weaken_symbol(sym);
5761 if (tcc_state->do_debug) {
5762 put_stabn(N_FUN, 0, 0, ind - func_ind);
5764 /* It's better to crash than to generate wrong code */
5765 cur_text_section = NULL;
5766 funcname = ""; /* for safety */
5767 func_vt.t = VT_VOID; /* for safety */
5768 func_var = 0; /* for safety */
5769 ind = 0; /* for safety */
5770 nocode_wanted = saved_nocode_wanted;
5773 ST_FUNC void gen_inline_functions(void)
5775 Sym *sym;
5776 int *str, inline_generated, i;
5777 struct InlineFunc *fn;
5779 /* iterate while inline function are referenced */
5780 for(;;) {
5781 inline_generated = 0;
5782 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5783 fn = tcc_state->inline_fns[i];
5784 sym = fn->sym;
5785 if (sym && sym->c) {
5786 /* the function was used: generate its code and
5787 convert it to a normal function */
5788 str = fn->token_str;
5789 fn->sym = NULL;
5790 if (file)
5791 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5792 sym->r = VT_SYM | VT_CONST;
5793 sym->type.t &= ~VT_INLINE;
5795 macro_ptr = str;
5796 next();
5797 cur_text_section = text_section;
5798 gen_function(sym);
5799 macro_ptr = NULL; /* fail safe */
5801 inline_generated = 1;
5804 if (!inline_generated)
5805 break;
5807 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5808 fn = tcc_state->inline_fns[i];
5809 str = fn->token_str;
5810 tok_str_free(str);
5812 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5815 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5816 static int decl0(int l, int is_for_loop_init)
5818 int v, has_init, r;
5819 CType type, btype;
5820 Sym *sym;
5821 AttributeDef ad;
5823 while (1) {
5824 if (!parse_btype(&btype, &ad)) {
5825 if (is_for_loop_init)
5826 return 0;
5827 /* skip redundant ';' */
5828 /* XXX: find more elegant solution */
5829 if (tok == ';') {
5830 next();
5831 continue;
5833 if (l == VT_CONST &&
5834 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5835 /* global asm block */
5836 asm_global_instr();
5837 continue;
5839 /* special test for old K&R protos without explicit int
5840 type. Only accepted when defining global data */
5841 if (l == VT_LOCAL || tok < TOK_DEFINE)
5842 break;
5843 btype.t = VT_INT;
5845 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5846 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5847 tok == ';') {
5848 /* we accept no variable after */
5849 next();
5850 continue;
5852 while (1) { /* iterate thru each declaration */
5853 char *asm_label; // associated asm label
5854 type = btype;
5855 type_decl(&type, &ad, &v, TYPE_DIRECT);
5856 #if 0
5858 char buf[500];
5859 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5860 printf("type = '%s'\n", buf);
5862 #endif
5863 if ((type.t & VT_BTYPE) == VT_FUNC) {
5864 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5865 tcc_error("function without file scope cannot be static");
5867 /* if old style function prototype, we accept a
5868 declaration list */
5869 sym = type.ref;
5870 if (sym->c == FUNC_OLD)
5871 func_decl_list(sym);
5874 asm_label = NULL;
5875 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5876 CString astr;
5878 asm_label_instr(&astr);
5879 asm_label = tcc_strdup(astr.data);
5880 cstr_free(&astr);
5882 /* parse one last attribute list, after asm label */
5883 parse_attribute(&ad);
5886 if (ad.a.weak)
5887 type.t |= VT_WEAK;
5888 #ifdef TCC_TARGET_PE
5889 if (ad.a.func_import)
5890 type.t |= VT_IMPORT;
5891 if (ad.a.func_export)
5892 type.t |= VT_EXPORT;
5893 #endif
5894 if (tok == '{') {
5895 if (l == VT_LOCAL)
5896 tcc_error("cannot use local functions");
5897 if ((type.t & VT_BTYPE) != VT_FUNC)
5898 expect("function definition");
5900 /* reject abstract declarators in function definition */
5901 sym = type.ref;
5902 while ((sym = sym->next) != NULL)
5903 if (!(sym->v & ~SYM_FIELD))
5904 expect("identifier");
5906 /* XXX: cannot do better now: convert extern line to static inline */
5907 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5908 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5910 sym = sym_find(v);
5911 if (sym) {
5912 Sym *ref;
5913 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5914 goto func_error1;
5916 ref = sym->type.ref;
5917 if (0 == ref->a.func_proto)
5918 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5920 /* use func_call from prototype if not defined */
5921 if (ref->a.func_call != FUNC_CDECL
5922 && type.ref->a.func_call == FUNC_CDECL)
5923 type.ref->a.func_call = ref->a.func_call;
5925 /* use export from prototype */
5926 if (ref->a.func_export)
5927 type.ref->a.func_export = 1;
5929 /* use static from prototype */
5930 if (sym->type.t & VT_STATIC)
5931 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5933 if (!is_compatible_types(&sym->type, &type)) {
5934 func_error1:
5935 tcc_error("incompatible types for redefinition of '%s'",
5936 get_tok_str(v, NULL));
5938 type.ref->a.func_proto = 0;
5939 /* if symbol is already defined, then put complete type */
5940 sym->type = type;
5941 } else {
5942 /* put function symbol */
5943 sym = global_identifier_push(v, type.t, 0);
5944 sym->type.ref = type.ref;
5947 /* static inline functions are just recorded as a kind
5948 of macro. Their code will be emitted at the end of
5949 the compilation unit only if they are used */
5950 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5951 (VT_INLINE | VT_STATIC)) {
5952 TokenString func_str;
5953 int block_level;
5954 struct InlineFunc *fn;
5955 const char *filename;
5957 tok_str_new(&func_str);
5959 block_level = 0;
5960 for(;;) {
5961 int t;
5962 if (tok == TOK_EOF)
5963 tcc_error("unexpected end of file");
5964 tok_str_add_tok(&func_str);
5965 t = tok;
5966 next();
5967 if (t == '{') {
5968 block_level++;
5969 } else if (t == '}') {
5970 block_level--;
5971 if (block_level == 0)
5972 break;
5975 tok_str_add(&func_str, -1);
5976 tok_str_add(&func_str, 0);
5977 filename = file ? file->filename : "";
5978 fn = tcc_malloc(sizeof *fn + strlen(filename));
5979 strcpy(fn->filename, filename);
5980 fn->sym = sym;
5981 fn->token_str = func_str.str;
5982 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5984 } else {
5985 /* compute text section */
5986 cur_text_section = ad.section;
5987 if (!cur_text_section)
5988 cur_text_section = text_section;
5989 sym->r = VT_SYM | VT_CONST;
5990 gen_function(sym);
5992 break;
5993 } else {
5994 if (btype.t & VT_TYPEDEF) {
5995 /* save typedefed type */
5996 /* XXX: test storage specifiers ? */
5997 sym = sym_push(v, &type, 0, 0);
5998 sym->a = ad.a;
5999 sym->type.t |= VT_TYPEDEF;
6000 } else {
6001 r = 0;
6002 if ((type.t & VT_BTYPE) == VT_FUNC) {
6003 /* external function definition */
6004 /* specific case for func_call attribute */
6005 ad.a.func_proto = 1;
6006 type.ref->a = ad.a;
6007 } else if (!(type.t & VT_ARRAY)) {
6008 /* not lvalue if array */
6009 r |= lvalue_type(type.t);
6011 has_init = (tok == '=');
6012 if (has_init && (type.t & VT_VLA))
6013 tcc_error("Variable length array cannot be initialized");
6014 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6015 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6016 !has_init && l == VT_CONST && type.ref->c < 0)) {
6017 /* external variable or function */
6018 /* NOTE: as GCC, uninitialized global static
6019 arrays of null size are considered as
6020 extern */
6021 sym = external_sym(v, &type, r, asm_label);
6023 if (type.t & VT_WEAK)
6024 weaken_symbol(sym);
6026 if (ad.alias_target) {
6027 Section tsec;
6028 Elf32_Sym *esym;
6029 Sym *alias_target;
6031 alias_target = sym_find(ad.alias_target);
6032 if (!alias_target || !alias_target->c)
6033 tcc_error("unsupported forward __alias__ attribute");
6034 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6035 tsec.sh_num = esym->st_shndx;
6036 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6038 } else {
6039 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6040 if (type.t & VT_STATIC)
6041 r |= VT_CONST;
6042 else
6043 r |= l;
6044 if (has_init)
6045 next();
6046 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6049 if (tok != ',') {
6050 if (is_for_loop_init)
6051 return 1;
6052 skip(';');
6053 break;
6055 next();
6057 ad.a.aligned = 0;
6060 return 0;
6063 ST_FUNC void decl(int l)
6065 decl0(l, 0);