Fix parameter passing of long long bitfield
[tinycc.git] / tccgen.c
blob03a446a93aac9cb69b442a39f393a78a2383fc65
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 /* push a symbol value of TYPE */
373 static inline void vpushsym(CType *type, Sym *sym)
375 CValue cval;
377 cval.ull = 0;
378 vsetc(type, VT_CONST | VT_SYM, &cval);
379 vtop->sym = sym;
382 /* Return a static symbol pointing to a section */
383 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
385 int v;
386 Sym *sym;
388 v = anon_sym++;
389 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
390 sym->type.ref = type->ref;
391 sym->r = VT_CONST | VT_SYM;
392 put_extern_sym(sym, sec, offset, size);
393 return sym;
396 /* push a reference to a section offset by adding a dummy symbol */
397 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
399 vpushsym(type, get_sym_ref(type, sec, offset, size));
402 /* define a new external reference to a symbol 'v' of type 'u' */
403 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
405 Sym *s;
407 s = sym_find(v);
408 if (!s) {
409 /* push forward reference */
410 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
411 s->type.ref = type->ref;
412 s->r = r | VT_CONST | VT_SYM;
414 return s;
417 /* define a new external reference to a symbol 'v' with alternate asm
418 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
419 is no alternate name (most cases) */
420 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
422 Sym *s;
424 s = sym_find(v);
425 if (!s) {
426 /* push forward reference */
427 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
428 s->asm_label = asm_label;
429 s->type.t |= VT_EXTERN;
430 } else if (s->type.ref == func_old_type.ref) {
431 s->type.ref = type->ref;
432 s->r = r | VT_CONST | VT_SYM;
433 s->type.t |= VT_EXTERN;
434 } else if (!is_compatible_types(&s->type, type)) {
435 tcc_error("incompatible types for redefinition of '%s'",
436 get_tok_str(v, NULL));
438 return s;
441 /* push a reference to global symbol v */
442 ST_FUNC void vpush_global_sym(CType *type, int v)
444 vpushsym(type, external_global_sym(v, type, 0));
447 ST_FUNC void vset(CType *type, int r, int v)
449 CValue cval;
451 cval.i = v;
452 vsetc(type, r, &cval);
455 static void vseti(int r, int v)
457 CType type;
458 type.t = VT_INT;
459 type.ref = 0;
460 vset(&type, r, v);
463 ST_FUNC void vswap(void)
465 SValue tmp;
466 /* cannot let cpu flags if other instruction are generated. Also
467 avoid leaving VT_JMP anywhere except on the top of the stack
468 because it would complicate the code generator. */
469 if (vtop >= vstack) {
470 int v = vtop->r & VT_VALMASK;
471 if (v == VT_CMP || (v & ~1) == VT_JMP)
472 gv(RC_INT);
474 tmp = vtop[0];
475 vtop[0] = vtop[-1];
476 vtop[-1] = tmp;
478 /* XXX: +2% overall speed possible with optimized memswap
480 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
484 ST_FUNC void vpushv(SValue *v)
486 if (vtop >= vstack + (VSTACK_SIZE - 1))
487 tcc_error("memory full (vstack)");
488 vtop++;
489 *vtop = *v;
492 static void vdup(void)
494 vpushv(vtop);
497 /* save r to the memory stack, and mark it as being free */
498 ST_FUNC void save_reg(int r)
500 int l, saved, size, align;
501 SValue *p, sv;
502 CType *type;
504 /* modify all stack values */
505 saved = 0;
506 l = 0;
507 for(p=vstack;p<=vtop;p++) {
508 if ((p->r & VT_VALMASK) == r ||
509 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
510 /* must save value on stack if not already done */
511 if (!saved) {
512 /* NOTE: must reload 'r' because r might be equal to r2 */
513 r = p->r & VT_VALMASK;
514 /* store register in the stack */
515 type = &p->type;
516 if ((p->r & VT_LVAL) ||
517 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
518 #ifdef TCC_TARGET_X86_64
519 type = &char_pointer_type;
520 #else
521 type = &int_type;
522 #endif
523 size = type_size(type, &align);
524 loc = (loc - size) & -align;
525 sv.type.t = type->t;
526 sv.r = VT_LOCAL | VT_LVAL;
527 sv.c.ul = loc;
528 store(r, &sv);
529 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
530 /* x86 specific: need to pop fp register ST0 if saved */
531 if (r == TREG_ST0) {
532 o(0xd8dd); /* fstp %st(0) */
534 #endif
535 #ifndef TCC_TARGET_X86_64
536 /* special long long case */
537 if ((type->t & VT_BTYPE) == VT_LLONG) {
538 sv.c.ul += 4;
539 store(p->r2, &sv);
541 #endif
542 l = loc;
543 saved = 1;
545 /* mark that stack entry as being saved on the stack */
546 if (p->r & VT_LVAL) {
547 /* also clear the bounded flag because the
548 relocation address of the function was stored in
549 p->c.ul */
550 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
551 } else {
552 p->r = lvalue_type(p->type.t) | VT_LOCAL;
554 p->r2 = VT_CONST;
555 p->c.ul = l;
560 #ifdef TCC_TARGET_ARM
561 /* find a register of class 'rc2' with at most one reference on stack.
562 * If none, call get_reg(rc) */
563 ST_FUNC int get_reg_ex(int rc, int rc2)
565 int r;
566 SValue *p;
568 for(r=0;r<NB_REGS;r++) {
569 if (reg_classes[r] & rc2) {
570 int n;
571 n=0;
572 for(p = vstack; p <= vtop; p++) {
573 if ((p->r & VT_VALMASK) == r ||
574 (p->r2 & VT_VALMASK) == r)
575 n++;
577 if (n <= 1)
578 return r;
581 return get_reg(rc);
583 #endif
585 /* find a free register of class 'rc'. If none, save one register */
586 ST_FUNC int get_reg(int rc)
588 int r;
589 SValue *p;
591 /* find a free register */
592 for(r=0;r<NB_REGS;r++) {
593 if (reg_classes[r] & rc) {
594 for(p=vstack;p<=vtop;p++) {
595 if ((p->r & VT_VALMASK) == r ||
596 (p->r2 & VT_VALMASK) == r)
597 goto notfound;
599 return r;
601 notfound: ;
604 /* no register left : free the first one on the stack (VERY
605 IMPORTANT to start from the bottom to ensure that we don't
606 spill registers used in gen_opi()) */
607 for(p=vstack;p<=vtop;p++) {
608 /* look at second register (if long long) */
609 r = p->r2 & VT_VALMASK;
610 if (r < VT_CONST && (reg_classes[r] & rc))
611 goto save_found;
612 r = p->r & VT_VALMASK;
613 if (r < VT_CONST && (reg_classes[r] & rc)) {
614 save_found:
615 save_reg(r);
616 return r;
619 /* Should never comes here */
620 return -1;
623 /* save registers up to (vtop - n) stack entry */
624 ST_FUNC void save_regs(int n)
626 int r;
627 SValue *p, *p1;
628 p1 = vtop - n;
629 for(p = vstack;p <= p1; p++) {
630 r = p->r & VT_VALMASK;
631 if (r < VT_CONST) {
632 save_reg(r);
637 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
638 if needed */
639 static void move_reg(int r, int s, int t)
641 SValue sv;
643 if (r != s) {
644 save_reg(r);
645 sv.type.t = t;
646 sv.type.ref = NULL;
647 sv.r = s;
648 sv.c.ul = 0;
649 load(r, &sv);
653 /* get address of vtop (vtop MUST BE an lvalue) */
654 static void gaddrof(void)
656 if (vtop->r & VT_REF)
657 gv(RC_INT);
658 vtop->r &= ~VT_LVAL;
659 /* tricky: if saved lvalue, then we can go back to lvalue */
660 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
661 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
666 #ifdef CONFIG_TCC_BCHECK
667 /* generate lvalue bound code */
668 static void gbound(void)
670 int lval_type;
671 CType type1;
673 vtop->r &= ~VT_MUSTBOUND;
674 /* if lvalue, then use checking code before dereferencing */
675 if (vtop->r & VT_LVAL) {
676 /* if not VT_BOUNDED value, then make one */
677 if (!(vtop->r & VT_BOUNDED)) {
678 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
679 /* must save type because we must set it to int to get pointer */
680 type1 = vtop->type;
681 vtop->type.t = VT_INT;
682 gaddrof();
683 vpushi(0);
684 gen_bounded_ptr_add();
685 vtop->r |= lval_type;
686 vtop->type = type1;
688 /* then check for dereferencing */
689 gen_bounded_ptr_deref();
692 #endif
694 /* store vtop a register belonging to class 'rc'. lvalues are
695 converted to values. Cannot be used if cannot be converted to
696 register value (such as structures). */
697 ST_FUNC int gv(int rc)
699 int r, bit_pos, bit_size, size, align, i;
700 int rc2;
702 /* NOTE: get_reg can modify vstack[] */
703 if (vtop->type.t & VT_BITFIELD) {
704 CType type;
705 int bits = 32;
706 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
707 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
708 /* remove bit field info to avoid loops */
709 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
710 /* cast to int to propagate signedness in following ops */
711 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
712 type.t = VT_LLONG;
713 bits = 64;
714 } else
715 type.t = VT_INT;
716 if((vtop->type.t & VT_UNSIGNED) ||
717 (vtop->type.t & VT_BTYPE) == VT_BOOL)
718 type.t |= VT_UNSIGNED;
719 gen_cast(&type);
720 /* generate shifts */
721 vpushi(bits - (bit_pos + bit_size));
722 gen_op(TOK_SHL);
723 vpushi(bits - bit_size);
724 /* NOTE: transformed to SHR if unsigned */
725 gen_op(TOK_SAR);
726 r = gv(rc);
727 } else {
728 if (is_float(vtop->type.t) &&
729 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
730 Sym *sym;
731 int *ptr;
732 unsigned long offset;
733 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
734 CValue check;
735 #endif
737 /* XXX: unify with initializers handling ? */
738 /* CPUs usually cannot use float constants, so we store them
739 generically in data segment */
740 size = type_size(&vtop->type, &align);
741 offset = (data_section->data_offset + align - 1) & -align;
742 data_section->data_offset = offset;
743 /* XXX: not portable yet */
744 #if defined(__i386__) || defined(__x86_64__)
745 /* Zero pad x87 tenbyte long doubles */
746 if (size == LDOUBLE_SIZE) {
747 vtop->c.tab[2] &= 0xffff;
748 #if LDOUBLE_SIZE == 16
749 vtop->c.tab[3] = 0;
750 #endif
752 #endif
753 ptr = section_ptr_add(data_section, size);
754 size = size >> 2;
755 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
756 check.d = 1;
757 if(check.tab[0])
758 for(i=0;i<size;i++)
759 ptr[i] = vtop->c.tab[size-1-i];
760 else
761 #endif
762 for(i=0;i<size;i++)
763 ptr[i] = vtop->c.tab[i];
764 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
765 vtop->r |= VT_LVAL | VT_SYM;
766 vtop->sym = sym;
767 vtop->c.ull = 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 #ifdef TCC_TARGET_X86_64
1953 else if (dbt == VT_PTR)
1955 #endif
1956 else if (dbt != VT_LLONG) {
1957 int s = 0;
1958 if ((dbt & VT_BTYPE) == VT_BYTE)
1959 s = 24;
1960 else if ((dbt & VT_BTYPE) == VT_SHORT)
1961 s = 16;
1962 if(dbt & VT_UNSIGNED)
1963 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1964 else
1965 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1968 } else if (p && dbt == VT_BOOL) {
1969 vtop->r = VT_CONST;
1970 vtop->c.i = 1;
1971 } else if (!nocode_wanted) {
1972 /* non constant case: generate code */
1973 if (sf && df) {
1974 /* convert from fp to fp */
1975 gen_cvt_ftof(dbt);
1976 } else if (df) {
1977 /* convert int to fp */
1978 gen_cvt_itof1(dbt);
1979 } else if (sf) {
1980 /* convert fp to int */
1981 if (dbt == VT_BOOL) {
1982 vpushi(0);
1983 gen_op(TOK_NE);
1984 } else {
1985 /* we handle char/short/etc... with generic code */
1986 if (dbt != (VT_INT | VT_UNSIGNED) &&
1987 dbt != (VT_LLONG | VT_UNSIGNED) &&
1988 dbt != VT_LLONG)
1989 dbt = VT_INT;
1990 gen_cvt_ftoi1(dbt);
1991 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1992 /* additional cast for char/short... */
1993 vtop->type.t = dbt;
1994 gen_cast(type);
1997 #ifndef TCC_TARGET_X86_64
1998 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1999 if ((sbt & VT_BTYPE) != VT_LLONG) {
2000 /* scalar to long long */
2001 /* machine independent conversion */
2002 gv(RC_INT);
2003 /* generate high word */
2004 if (sbt == (VT_INT | VT_UNSIGNED)) {
2005 vpushi(0);
2006 gv(RC_INT);
2007 } else {
2008 if (sbt == VT_PTR) {
2009 /* cast from pointer to int before we apply
2010 shift operation, which pointers don't support*/
2011 gen_cast(&int_type);
2013 gv_dup();
2014 vpushi(31);
2015 gen_op(TOK_SAR);
2017 /* patch second register */
2018 vtop[-1].r2 = vtop->r;
2019 vpop();
2021 #else
2022 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2023 (dbt & VT_BTYPE) == VT_PTR ||
2024 (dbt & VT_BTYPE) == VT_FUNC) {
2025 if ((sbt & VT_BTYPE) != VT_LLONG &&
2026 (sbt & VT_BTYPE) != VT_PTR &&
2027 (sbt & VT_BTYPE) != VT_FUNC) {
2028 /* need to convert from 32bit to 64bit */
2029 int r = gv(RC_INT);
2030 if (sbt != (VT_INT | VT_UNSIGNED)) {
2031 /* x86_64 specific: movslq */
2032 o(0x6348);
2033 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2036 #endif
2037 } else if (dbt == VT_BOOL) {
2038 /* scalar to bool */
2039 vpushi(0);
2040 gen_op(TOK_NE);
2041 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2042 (dbt & VT_BTYPE) == VT_SHORT) {
2043 if (sbt == VT_PTR) {
2044 vtop->type.t = VT_INT;
2045 tcc_warning("nonportable conversion from pointer to char/short");
2047 force_charshort_cast(dbt);
2048 } else if ((dbt & VT_BTYPE) == VT_INT) {
2049 /* scalar to int */
2050 if (sbt == VT_LLONG) {
2051 /* from long long: just take low order word */
2052 lexpand();
2053 vpop();
2055 /* if lvalue and single word type, nothing to do because
2056 the lvalue already contains the real type size (see
2057 VT_LVAL_xxx constants) */
2060 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2061 /* if we are casting between pointer types,
2062 we must update the VT_LVAL_xxx size */
2063 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2064 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2066 vtop->type = *type;
2069 /* return type size as known at compile time. Put alignment at 'a' */
2070 ST_FUNC int type_size(CType *type, int *a)
2072 Sym *s;
2073 int bt;
2075 bt = type->t & VT_BTYPE;
2076 if (bt == VT_STRUCT) {
2077 /* struct/union */
2078 s = type->ref;
2079 *a = s->r;
2080 return s->c;
2081 } else if (bt == VT_PTR) {
2082 if (type->t & VT_ARRAY) {
2083 int ts;
2085 s = type->ref;
2086 ts = type_size(&s->type, a);
2088 if (ts < 0 && s->c < 0)
2089 ts = -ts;
2091 return ts * s->c;
2092 } else {
2093 *a = PTR_SIZE;
2094 return PTR_SIZE;
2096 } else if (bt == VT_LDOUBLE) {
2097 *a = LDOUBLE_ALIGN;
2098 return LDOUBLE_SIZE;
2099 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2100 #ifdef TCC_TARGET_I386
2101 #ifdef TCC_TARGET_PE
2102 *a = 8;
2103 #else
2104 *a = 4;
2105 #endif
2106 #elif defined(TCC_TARGET_ARM)
2107 #ifdef TCC_ARM_EABI
2108 *a = 8;
2109 #else
2110 *a = 4;
2111 #endif
2112 #else
2113 *a = 8;
2114 #endif
2115 return 8;
2116 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2117 *a = 4;
2118 return 4;
2119 } else if (bt == VT_SHORT) {
2120 *a = 2;
2121 return 2;
2122 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2123 *a = 8;
2124 return 16;
2125 } else {
2126 /* char, void, function, _Bool */
2127 *a = 1;
2128 return 1;
2132 /* push type size as known at runtime time on top of value stack. Put
2133 alignment at 'a' */
2134 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2136 if (type->t & VT_VLA) {
2137 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2138 } else {
2139 vpushi(type_size(type, a));
2143 static void vla_sp_save(void) {
2144 if (!(vla_flags & VLA_SP_LOC_SET)) {
2145 *vla_sp_loc = (loc -= PTR_SIZE);
2146 vla_flags |= VLA_SP_LOC_SET;
2148 if (!(vla_flags & VLA_SP_SAVED)) {
2149 gen_vla_sp_save(*vla_sp_loc);
2150 vla_flags |= VLA_SP_SAVED;
2154 /* return the pointed type of t */
2155 static inline CType *pointed_type(CType *type)
2157 return &type->ref->type;
2160 /* modify type so that its it is a pointer to type. */
2161 ST_FUNC void mk_pointer(CType *type)
2163 Sym *s;
2164 s = sym_push(SYM_FIELD, type, 0, -1);
2165 type->t = VT_PTR | (type->t & ~VT_TYPE);
2166 type->ref = s;
2169 /* compare function types. OLD functions match any new functions */
2170 static int is_compatible_func(CType *type1, CType *type2)
2172 Sym *s1, *s2;
2174 s1 = type1->ref;
2175 s2 = type2->ref;
2176 if (!is_compatible_types(&s1->type, &s2->type))
2177 return 0;
2178 /* check func_call */
2179 if (s1->a.func_call != s2->a.func_call)
2180 return 0;
2181 /* XXX: not complete */
2182 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2183 return 1;
2184 if (s1->c != s2->c)
2185 return 0;
2186 while (s1 != NULL) {
2187 if (s2 == NULL)
2188 return 0;
2189 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2190 return 0;
2191 s1 = s1->next;
2192 s2 = s2->next;
2194 if (s2)
2195 return 0;
2196 return 1;
2199 /* return true if type1 and type2 are the same. If unqualified is
2200 true, qualifiers on the types are ignored.
2202 - enums are not checked as gcc __builtin_types_compatible_p ()
2204 static int compare_types(CType *type1, CType *type2, int unqualified)
2206 int bt1, t1, t2;
2208 t1 = type1->t & VT_TYPE;
2209 t2 = type2->t & VT_TYPE;
2210 if (unqualified) {
2211 /* strip qualifiers before comparing */
2212 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2213 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2215 /* XXX: bitfields ? */
2216 if (t1 != t2)
2217 return 0;
2218 /* test more complicated cases */
2219 bt1 = t1 & VT_BTYPE;
2220 if (bt1 == VT_PTR) {
2221 type1 = pointed_type(type1);
2222 type2 = pointed_type(type2);
2223 return is_compatible_types(type1, type2);
2224 } else if (bt1 == VT_STRUCT) {
2225 return (type1->ref == type2->ref);
2226 } else if (bt1 == VT_FUNC) {
2227 return is_compatible_func(type1, type2);
2228 } else {
2229 return 1;
2233 /* return true if type1 and type2 are exactly the same (including
2234 qualifiers).
2236 static int is_compatible_types(CType *type1, CType *type2)
2238 return compare_types(type1,type2,0);
2241 /* return true if type1 and type2 are the same (ignoring qualifiers).
2243 static int is_compatible_parameter_types(CType *type1, CType *type2)
2245 return compare_types(type1,type2,1);
2248 /* print a type. If 'varstr' is not NULL, then the variable is also
2249 printed in the type */
2250 /* XXX: union */
2251 /* XXX: add array and function pointers */
2252 static void type_to_str(char *buf, int buf_size,
2253 CType *type, const char *varstr)
2255 int bt, v, t;
2256 Sym *s, *sa;
2257 char buf1[256];
2258 const char *tstr;
2260 t = type->t & VT_TYPE;
2261 bt = t & VT_BTYPE;
2262 buf[0] = '\0';
2263 if (t & VT_CONSTANT)
2264 pstrcat(buf, buf_size, "const ");
2265 if (t & VT_VOLATILE)
2266 pstrcat(buf, buf_size, "volatile ");
2267 if (t & VT_UNSIGNED)
2268 pstrcat(buf, buf_size, "unsigned ");
2269 switch(bt) {
2270 case VT_VOID:
2271 tstr = "void";
2272 goto add_tstr;
2273 case VT_BOOL:
2274 tstr = "_Bool";
2275 goto add_tstr;
2276 case VT_BYTE:
2277 tstr = "char";
2278 goto add_tstr;
2279 case VT_SHORT:
2280 tstr = "short";
2281 goto add_tstr;
2282 case VT_INT:
2283 tstr = "int";
2284 goto add_tstr;
2285 case VT_LONG:
2286 tstr = "long";
2287 goto add_tstr;
2288 case VT_LLONG:
2289 tstr = "long long";
2290 goto add_tstr;
2291 case VT_FLOAT:
2292 tstr = "float";
2293 goto add_tstr;
2294 case VT_DOUBLE:
2295 tstr = "double";
2296 goto add_tstr;
2297 case VT_LDOUBLE:
2298 tstr = "long double";
2299 add_tstr:
2300 pstrcat(buf, buf_size, tstr);
2301 break;
2302 case VT_ENUM:
2303 case VT_STRUCT:
2304 if (bt == VT_STRUCT)
2305 tstr = "struct ";
2306 else
2307 tstr = "enum ";
2308 pstrcat(buf, buf_size, tstr);
2309 v = type->ref->v & ~SYM_STRUCT;
2310 if (v >= SYM_FIRST_ANOM)
2311 pstrcat(buf, buf_size, "<anonymous>");
2312 else
2313 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2314 break;
2315 case VT_FUNC:
2316 s = type->ref;
2317 type_to_str(buf, buf_size, &s->type, varstr);
2318 pstrcat(buf, buf_size, "(");
2319 sa = s->next;
2320 while (sa != NULL) {
2321 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2322 pstrcat(buf, buf_size, buf1);
2323 sa = sa->next;
2324 if (sa)
2325 pstrcat(buf, buf_size, ", ");
2327 pstrcat(buf, buf_size, ")");
2328 goto no_var;
2329 case VT_PTR:
2330 s = type->ref;
2331 pstrcpy(buf1, sizeof(buf1), "*");
2332 if (varstr)
2333 pstrcat(buf1, sizeof(buf1), varstr);
2334 type_to_str(buf, buf_size, &s->type, buf1);
2335 goto no_var;
2337 if (varstr) {
2338 pstrcat(buf, buf_size, " ");
2339 pstrcat(buf, buf_size, varstr);
2341 no_var: ;
2344 /* verify type compatibility to store vtop in 'dt' type, and generate
2345 casts if needed. */
2346 static void gen_assign_cast(CType *dt)
2348 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2349 char buf1[256], buf2[256];
2350 int dbt, sbt;
2352 st = &vtop->type; /* source type */
2353 dbt = dt->t & VT_BTYPE;
2354 sbt = st->t & VT_BTYPE;
2355 if (sbt == VT_VOID || dbt == VT_VOID)
2356 tcc_error("cannot cast from/to void");
2357 if (dt->t & VT_CONSTANT)
2358 tcc_warning("assignment of read-only location");
2359 switch(dbt) {
2360 case VT_PTR:
2361 /* special cases for pointers */
2362 /* '0' can also be a pointer */
2363 if (is_null_pointer(vtop))
2364 goto type_ok;
2365 /* accept implicit pointer to integer cast with warning */
2366 if (is_integer_btype(sbt)) {
2367 tcc_warning("assignment makes pointer from integer without a cast");
2368 goto type_ok;
2370 type1 = pointed_type(dt);
2371 /* a function is implicitely a function pointer */
2372 if (sbt == VT_FUNC) {
2373 if ((type1->t & VT_BTYPE) != VT_VOID &&
2374 !is_compatible_types(pointed_type(dt), st))
2375 tcc_warning("assignment from incompatible pointer type");
2376 goto type_ok;
2378 if (sbt != VT_PTR)
2379 goto error;
2380 type2 = pointed_type(st);
2381 if ((type1->t & VT_BTYPE) == VT_VOID ||
2382 (type2->t & VT_BTYPE) == VT_VOID) {
2383 /* void * can match anything */
2384 } else {
2385 /* exact type match, except for unsigned */
2386 tmp_type1 = *type1;
2387 tmp_type2 = *type2;
2388 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2389 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2390 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2391 tcc_warning("assignment from incompatible pointer type");
2393 /* check const and volatile */
2394 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2395 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2396 tcc_warning("assignment discards qualifiers from pointer target type");
2397 break;
2398 case VT_BYTE:
2399 case VT_SHORT:
2400 case VT_INT:
2401 case VT_LLONG:
2402 if (sbt == VT_PTR || sbt == VT_FUNC) {
2403 tcc_warning("assignment makes integer from pointer without a cast");
2405 /* XXX: more tests */
2406 break;
2407 case VT_STRUCT:
2408 tmp_type1 = *dt;
2409 tmp_type2 = *st;
2410 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2411 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2412 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2413 error:
2414 type_to_str(buf1, sizeof(buf1), st, NULL);
2415 type_to_str(buf2, sizeof(buf2), dt, NULL);
2416 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2418 break;
2420 type_ok:
2421 gen_cast(dt);
2424 /* store vtop in lvalue pushed on stack */
2425 ST_FUNC void vstore(void)
2427 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2429 ft = vtop[-1].type.t;
2430 sbt = vtop->type.t & VT_BTYPE;
2431 dbt = ft & VT_BTYPE;
2432 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2433 (sbt == VT_INT && dbt == VT_SHORT))
2434 && !(vtop->type.t & VT_BITFIELD)) {
2435 /* optimize char/short casts */
2436 delayed_cast = VT_MUSTCAST;
2437 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2438 /* XXX: factorize */
2439 if (ft & VT_CONSTANT)
2440 tcc_warning("assignment of read-only location");
2441 } else {
2442 delayed_cast = 0;
2443 if (!(ft & VT_BITFIELD))
2444 gen_assign_cast(&vtop[-1].type);
2447 if (sbt == VT_STRUCT) {
2448 /* if structure, only generate pointer */
2449 /* structure assignment : generate memcpy */
2450 /* XXX: optimize if small size */
2451 if (!nocode_wanted) {
2452 size = type_size(&vtop->type, &align);
2454 /* destination */
2455 vswap();
2456 vtop->type.t = VT_PTR;
2457 gaddrof();
2459 /* address of memcpy() */
2460 #ifdef TCC_ARM_EABI
2461 if(!(align & 7))
2462 vpush_global_sym(&func_old_type, TOK_memcpy8);
2463 else if(!(align & 3))
2464 vpush_global_sym(&func_old_type, TOK_memcpy4);
2465 else
2466 #endif
2467 vpush_global_sym(&func_old_type, TOK_memcpy);
2469 vswap();
2470 /* source */
2471 vpushv(vtop - 2);
2472 vtop->type.t = VT_PTR;
2473 gaddrof();
2474 /* type size */
2475 vpushi(size);
2476 gfunc_call(3);
2477 } else {
2478 vswap();
2479 vpop();
2481 /* leave source on stack */
2482 } else if (ft & VT_BITFIELD) {
2483 /* bitfield store handling */
2484 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2485 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2486 /* remove bit field info to avoid loops */
2487 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2489 /* duplicate source into other register */
2490 gv_dup();
2491 vswap();
2492 vrott(3);
2494 if((ft & VT_BTYPE) == VT_BOOL) {
2495 gen_cast(&vtop[-1].type);
2496 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2499 /* duplicate destination */
2500 vdup();
2501 vtop[-1] = vtop[-2];
2503 /* mask and shift source */
2504 if((ft & VT_BTYPE) != VT_BOOL) {
2505 if((ft & VT_BTYPE) == VT_LLONG) {
2506 vpushll((1ULL << bit_size) - 1ULL);
2507 } else {
2508 vpushi((1 << bit_size) - 1);
2510 gen_op('&');
2512 vpushi(bit_pos);
2513 gen_op(TOK_SHL);
2514 /* load destination, mask and or with source */
2515 vswap();
2516 if((ft & VT_BTYPE) == VT_LLONG) {
2517 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2518 } else {
2519 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2521 gen_op('&');
2522 gen_op('|');
2523 /* store result */
2524 vstore();
2526 /* pop off shifted source from "duplicate source..." above */
2527 vpop();
2529 } else {
2530 #ifdef CONFIG_TCC_BCHECK
2531 /* bound check case */
2532 if (vtop[-1].r & VT_MUSTBOUND) {
2533 vswap();
2534 gbound();
2535 vswap();
2537 #endif
2538 if (!nocode_wanted) {
2539 rc = RC_INT;
2540 if (is_float(ft)) {
2541 rc = RC_FLOAT;
2542 #ifdef TCC_TARGET_X86_64
2543 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2544 rc = RC_ST0;
2545 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2546 rc = RC_FRET;
2548 #endif
2550 r = gv(rc); /* generate value */
2551 /* if lvalue was saved on stack, must read it */
2552 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2553 SValue sv;
2554 t = get_reg(RC_INT);
2555 #ifdef TCC_TARGET_X86_64
2556 sv.type.t = VT_PTR;
2557 #else
2558 sv.type.t = VT_INT;
2559 #endif
2560 sv.r = VT_LOCAL | VT_LVAL;
2561 sv.c.ul = vtop[-1].c.ul;
2562 load(t, &sv);
2563 vtop[-1].r = t | VT_LVAL;
2565 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2566 #ifdef TCC_TARGET_X86_64
2567 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2568 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2569 #else
2570 if ((ft & VT_BTYPE) == VT_LLONG) {
2571 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2572 #endif
2573 vtop[-1].type.t = load_type;
2574 store(r, vtop - 1);
2575 vswap();
2576 /* convert to int to increment easily */
2577 vtop->type.t = addr_type;
2578 gaddrof();
2579 vpushi(load_size);
2580 gen_op('+');
2581 vtop->r |= VT_LVAL;
2582 vswap();
2583 vtop[-1].type.t = load_type;
2584 /* XXX: it works because r2 is spilled last ! */
2585 store(vtop->r2, vtop - 1);
2586 } else {
2587 store(r, vtop - 1);
2590 vswap();
2591 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2592 vtop->r |= delayed_cast;
2596 /* post defines POST/PRE add. c is the token ++ or -- */
2597 ST_FUNC void inc(int post, int c)
2599 test_lvalue();
2600 vdup(); /* save lvalue */
2601 if (post) {
2602 gv_dup(); /* duplicate value */
2603 vrotb(3);
2604 vrotb(3);
2606 /* add constant */
2607 vpushi(c - TOK_MID);
2608 gen_op('+');
2609 vstore(); /* store value */
2610 if (post)
2611 vpop(); /* if post op, return saved value */
2614 /* Parse GNUC __attribute__ extension. Currently, the following
2615 extensions are recognized:
2616 - aligned(n) : set data/function alignment.
2617 - packed : force data alignment to 1
2618 - section(x) : generate data/code in this section.
2619 - unused : currently ignored, but may be used someday.
2620 - regparm(n) : pass function parameters in registers (i386 only)
2622 static void parse_attribute(AttributeDef *ad)
2624 int t, n;
2626 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2627 next();
2628 skip('(');
2629 skip('(');
2630 while (tok != ')') {
2631 if (tok < TOK_IDENT)
2632 expect("attribute name");
2633 t = tok;
2634 next();
2635 switch(t) {
2636 case TOK_SECTION1:
2637 case TOK_SECTION2:
2638 skip('(');
2639 if (tok != TOK_STR)
2640 expect("section name");
2641 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2642 next();
2643 skip(')');
2644 break;
2645 case TOK_ALIAS1:
2646 case TOK_ALIAS2:
2647 skip('(');
2648 if (tok != TOK_STR)
2649 expect("alias(\"target\")");
2650 ad->alias_target = /* save string as token, for later */
2651 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2652 next();
2653 skip(')');
2654 break;
2655 case TOK_ALIGNED1:
2656 case TOK_ALIGNED2:
2657 if (tok == '(') {
2658 next();
2659 n = expr_const();
2660 if (n <= 0 || (n & (n - 1)) != 0)
2661 tcc_error("alignment must be a positive power of two");
2662 skip(')');
2663 } else {
2664 n = MAX_ALIGN;
2666 ad->a.aligned = n;
2667 break;
2668 case TOK_PACKED1:
2669 case TOK_PACKED2:
2670 ad->a.packed = 1;
2671 break;
2672 case TOK_WEAK1:
2673 case TOK_WEAK2:
2674 ad->a.weak = 1;
2675 break;
2676 case TOK_UNUSED1:
2677 case TOK_UNUSED2:
2678 /* currently, no need to handle it because tcc does not
2679 track unused objects */
2680 break;
2681 case TOK_NORETURN1:
2682 case TOK_NORETURN2:
2683 /* currently, no need to handle it because tcc does not
2684 track unused objects */
2685 break;
2686 case TOK_CDECL1:
2687 case TOK_CDECL2:
2688 case TOK_CDECL3:
2689 ad->a.func_call = FUNC_CDECL;
2690 break;
2691 case TOK_STDCALL1:
2692 case TOK_STDCALL2:
2693 case TOK_STDCALL3:
2694 ad->a.func_call = FUNC_STDCALL;
2695 break;
2696 #ifdef TCC_TARGET_I386
2697 case TOK_REGPARM1:
2698 case TOK_REGPARM2:
2699 skip('(');
2700 n = expr_const();
2701 if (n > 3)
2702 n = 3;
2703 else if (n < 0)
2704 n = 0;
2705 if (n > 0)
2706 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2707 skip(')');
2708 break;
2709 case TOK_FASTCALL1:
2710 case TOK_FASTCALL2:
2711 case TOK_FASTCALL3:
2712 ad->a.func_call = FUNC_FASTCALLW;
2713 break;
2714 #endif
2715 case TOK_MODE:
2716 skip('(');
2717 switch(tok) {
2718 case TOK_MODE_DI:
2719 ad->a.mode = VT_LLONG + 1;
2720 break;
2721 case TOK_MODE_HI:
2722 ad->a.mode = VT_SHORT + 1;
2723 break;
2724 case TOK_MODE_SI:
2725 ad->a.mode = VT_INT + 1;
2726 break;
2727 default:
2728 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2729 break;
2731 next();
2732 skip(')');
2733 break;
2734 case TOK_DLLEXPORT:
2735 ad->a.func_export = 1;
2736 break;
2737 case TOK_DLLIMPORT:
2738 ad->a.func_import = 1;
2739 break;
2740 default:
2741 if (tcc_state->warn_unsupported)
2742 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2743 /* skip parameters */
2744 if (tok == '(') {
2745 int parenthesis = 0;
2746 do {
2747 if (tok == '(')
2748 parenthesis++;
2749 else if (tok == ')')
2750 parenthesis--;
2751 next();
2752 } while (parenthesis && tok != -1);
2754 break;
2756 if (tok != ',')
2757 break;
2758 next();
2760 skip(')');
2761 skip(')');
2765 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2766 static void struct_decl(CType *type, int u, int tdef)
2768 int a, v, size, align, maxalign, c, offset, flexible;
2769 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2770 Sym *s, *ss, *ass, **ps;
2771 AttributeDef ad;
2772 CType type1, btype;
2774 a = tok; /* save decl type */
2775 next();
2776 if (tok != '{') {
2777 v = tok;
2778 next();
2779 /* struct already defined ? return it */
2780 if (v < TOK_IDENT)
2781 expect("struct/union/enum name");
2782 s = struct_find(v);
2783 if (s) {
2784 if (s->type.t != a)
2785 tcc_error("invalid type");
2786 goto do_decl;
2787 } else if (tok >= TOK_IDENT && !tdef)
2788 tcc_error("unknown struct/union/enum");
2789 } else {
2790 v = anon_sym++;
2792 type1.t = a;
2793 type1.ref = NULL;
2794 /* we put an undefined size for struct/union */
2795 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2796 s->r = 0; /* default alignment is zero as gcc */
2797 /* put struct/union/enum name in type */
2798 do_decl:
2799 type->t = u;
2800 type->ref = s;
2802 if (tok == '{') {
2803 next();
2804 if (s->c != -1)
2805 tcc_error("struct/union/enum already defined");
2806 /* cannot be empty */
2807 c = 0;
2808 /* non empty enums are not allowed */
2809 if (a == TOK_ENUM) {
2810 for(;;) {
2811 v = tok;
2812 if (v < TOK_UIDENT)
2813 expect("identifier");
2814 ss = sym_find(v);
2815 if (ss)
2816 tcc_error("redefinition of enumerator '%s'",
2817 get_tok_str(v, NULL));
2818 next();
2819 if (tok == '=') {
2820 next();
2821 c = expr_const();
2823 /* enum symbols have static storage */
2824 ss = sym_push(v, &int_type, VT_CONST, c);
2825 ss->type.t |= VT_STATIC;
2826 if (tok != ',')
2827 break;
2828 next();
2829 c++;
2830 /* NOTE: we accept a trailing comma */
2831 if (tok == '}')
2832 break;
2834 s->c = type_size(&int_type, &align);
2835 skip('}');
2836 } else {
2837 maxalign = 1;
2838 ps = &s->next;
2839 prevbt = VT_INT;
2840 bit_pos = 0;
2841 offset = 0;
2842 flexible = 0;
2843 while (tok != '}') {
2844 parse_btype(&btype, &ad);
2845 while (1) {
2846 if (flexible)
2847 tcc_error("flexible array member '%s' not at the end of struct",
2848 get_tok_str(v, NULL));
2849 bit_size = -1;
2850 v = 0;
2851 type1 = btype;
2852 if (tok != ':') {
2853 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2854 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2855 expect("identifier");
2856 if (type_size(&type1, &align) < 0) {
2857 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2858 flexible = 1;
2859 else
2860 tcc_error("field '%s' has incomplete type",
2861 get_tok_str(v, NULL));
2863 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2864 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2865 tcc_error("invalid type for '%s'",
2866 get_tok_str(v, NULL));
2868 if (tok == ':') {
2869 next();
2870 bit_size = expr_const();
2871 /* XXX: handle v = 0 case for messages */
2872 if (bit_size < 0)
2873 tcc_error("negative width in bit-field '%s'",
2874 get_tok_str(v, NULL));
2875 if (v && bit_size == 0)
2876 tcc_error("zero width for bit-field '%s'",
2877 get_tok_str(v, NULL));
2879 size = type_size(&type1, &align);
2880 if (ad.a.aligned) {
2881 if (align < ad.a.aligned)
2882 align = ad.a.aligned;
2883 } else if (ad.a.packed) {
2884 align = 1;
2885 } else if (*tcc_state->pack_stack_ptr) {
2886 if (align > *tcc_state->pack_stack_ptr)
2887 align = *tcc_state->pack_stack_ptr;
2889 lbit_pos = 0;
2890 if (bit_size >= 0) {
2891 bt = type1.t & VT_BTYPE;
2892 if (bt != VT_INT &&
2893 bt != VT_BYTE &&
2894 bt != VT_SHORT &&
2895 bt != VT_BOOL &&
2896 bt != VT_ENUM &&
2897 bt != VT_LLONG)
2898 tcc_error("bitfields must have scalar type");
2899 bsize = size * 8;
2900 if (bit_size > bsize) {
2901 tcc_error("width of '%s' exceeds its type",
2902 get_tok_str(v, NULL));
2903 } else if (bit_size == bsize) {
2904 /* no need for bit fields */
2905 bit_pos = 0;
2906 } else if (bit_size == 0) {
2907 /* XXX: what to do if only padding in a
2908 structure ? */
2909 /* zero size: means to pad */
2910 bit_pos = 0;
2911 } else {
2912 /* we do not have enough room ?
2913 did the type change?
2914 is it a union? */
2915 if ((bit_pos + bit_size) > bsize ||
2916 bt != prevbt || a == TOK_UNION)
2917 bit_pos = 0;
2918 lbit_pos = bit_pos;
2919 /* XXX: handle LSB first */
2920 type1.t |= VT_BITFIELD |
2921 (bit_pos << VT_STRUCT_SHIFT) |
2922 (bit_size << (VT_STRUCT_SHIFT + 6));
2923 bit_pos += bit_size;
2925 prevbt = bt;
2926 } else {
2927 bit_pos = 0;
2929 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2930 /* add new memory data only if starting
2931 bit field */
2932 if (lbit_pos == 0) {
2933 if (a == TOK_STRUCT) {
2934 c = (c + align - 1) & -align;
2935 offset = c;
2936 if (size > 0)
2937 c += size;
2938 } else {
2939 offset = 0;
2940 if (size > c)
2941 c = size;
2943 if (align > maxalign)
2944 maxalign = align;
2946 #if 0
2947 printf("add field %s offset=%d",
2948 get_tok_str(v, NULL), offset);
2949 if (type1.t & VT_BITFIELD) {
2950 printf(" pos=%d size=%d",
2951 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2952 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2954 printf("\n");
2955 #endif
2957 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2958 ass = type1.ref;
2959 while ((ass = ass->next) != NULL) {
2960 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2961 *ps = ss;
2962 ps = &ss->next;
2964 } else if (v) {
2965 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2966 *ps = ss;
2967 ps = &ss->next;
2969 if (tok == ';' || tok == TOK_EOF)
2970 break;
2971 skip(',');
2973 skip(';');
2975 skip('}');
2976 /* store size and alignment */
2977 s->c = (c + maxalign - 1) & -maxalign;
2978 s->r = maxalign;
2983 /* return 0 if no type declaration. otherwise, return the basic type
2984 and skip it.
2986 static int parse_btype(CType *type, AttributeDef *ad)
2988 int t, u, type_found, typespec_found, typedef_found;
2989 Sym *s;
2990 CType type1;
2992 memset(ad, 0, sizeof(AttributeDef));
2993 type_found = 0;
2994 typespec_found = 0;
2995 typedef_found = 0;
2996 t = 0;
2997 while(1) {
2998 switch(tok) {
2999 case TOK_EXTENSION:
3000 /* currently, we really ignore extension */
3001 next();
3002 continue;
3004 /* basic types */
3005 case TOK_CHAR:
3006 u = VT_BYTE;
3007 basic_type:
3008 next();
3009 basic_type1:
3010 if ((t & VT_BTYPE) != 0)
3011 tcc_error("too many basic types");
3012 t |= u;
3013 typespec_found = 1;
3014 break;
3015 case TOK_VOID:
3016 u = VT_VOID;
3017 goto basic_type;
3018 case TOK_SHORT:
3019 u = VT_SHORT;
3020 goto basic_type;
3021 case TOK_INT:
3022 next();
3023 typespec_found = 1;
3024 break;
3025 case TOK_LONG:
3026 next();
3027 if ((t & VT_BTYPE) == VT_DOUBLE) {
3028 #ifndef TCC_TARGET_PE
3029 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3030 #endif
3031 } else if ((t & VT_BTYPE) == VT_LONG) {
3032 t = (t & ~VT_BTYPE) | VT_LLONG;
3033 } else {
3034 u = VT_LONG;
3035 goto basic_type1;
3037 break;
3038 case TOK_BOOL:
3039 u = VT_BOOL;
3040 goto basic_type;
3041 case TOK_FLOAT:
3042 u = VT_FLOAT;
3043 goto basic_type;
3044 case TOK_DOUBLE:
3045 next();
3046 if ((t & VT_BTYPE) == VT_LONG) {
3047 #ifdef TCC_TARGET_PE
3048 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3049 #else
3050 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3051 #endif
3052 } else {
3053 u = VT_DOUBLE;
3054 goto basic_type1;
3056 break;
3057 case TOK_ENUM:
3058 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3059 basic_type2:
3060 u = type1.t;
3061 type->ref = type1.ref;
3062 goto basic_type1;
3063 case TOK_STRUCT:
3064 case TOK_UNION:
3065 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3066 goto basic_type2;
3068 /* type modifiers */
3069 case TOK_CONST1:
3070 case TOK_CONST2:
3071 case TOK_CONST3:
3072 t |= VT_CONSTANT;
3073 next();
3074 break;
3075 case TOK_VOLATILE1:
3076 case TOK_VOLATILE2:
3077 case TOK_VOLATILE3:
3078 t |= VT_VOLATILE;
3079 next();
3080 break;
3081 case TOK_SIGNED1:
3082 case TOK_SIGNED2:
3083 case TOK_SIGNED3:
3084 typespec_found = 1;
3085 t |= VT_SIGNED;
3086 next();
3087 break;
3088 case TOK_REGISTER:
3089 case TOK_AUTO:
3090 case TOK_RESTRICT1:
3091 case TOK_RESTRICT2:
3092 case TOK_RESTRICT3:
3093 next();
3094 break;
3095 case TOK_UNSIGNED:
3096 t |= VT_UNSIGNED;
3097 next();
3098 typespec_found = 1;
3099 break;
3101 /* storage */
3102 case TOK_EXTERN:
3103 t |= VT_EXTERN;
3104 next();
3105 break;
3106 case TOK_STATIC:
3107 t |= VT_STATIC;
3108 next();
3109 break;
3110 case TOK_TYPEDEF:
3111 t |= VT_TYPEDEF;
3112 next();
3113 break;
3114 case TOK_INLINE1:
3115 case TOK_INLINE2:
3116 case TOK_INLINE3:
3117 t |= VT_INLINE;
3118 next();
3119 break;
3121 /* GNUC attribute */
3122 case TOK_ATTRIBUTE1:
3123 case TOK_ATTRIBUTE2:
3124 parse_attribute(ad);
3125 if (ad->a.mode) {
3126 u = ad->a.mode -1;
3127 t = (t & ~VT_BTYPE) | u;
3129 break;
3130 /* GNUC typeof */
3131 case TOK_TYPEOF1:
3132 case TOK_TYPEOF2:
3133 case TOK_TYPEOF3:
3134 next();
3135 parse_expr_type(&type1);
3136 /* remove all storage modifiers except typedef */
3137 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3138 goto basic_type2;
3139 default:
3140 if (typespec_found || typedef_found)
3141 goto the_end;
3142 s = sym_find(tok);
3143 if (!s || !(s->type.t & VT_TYPEDEF))
3144 goto the_end;
3145 typedef_found = 1;
3146 t |= (s->type.t & ~VT_TYPEDEF);
3147 type->ref = s->type.ref;
3148 if (s->r) {
3149 /* get attributes from typedef */
3150 if (0 == ad->a.aligned)
3151 ad->a.aligned = s->a.aligned;
3152 if (0 == ad->a.func_call)
3153 ad->a.func_call = s->a.func_call;
3154 ad->a.packed |= s->a.packed;
3156 next();
3157 typespec_found = 1;
3158 break;
3160 type_found = 1;
3162 the_end:
3163 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3164 tcc_error("signed and unsigned modifier");
3165 if (tcc_state->char_is_unsigned) {
3166 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3167 t |= VT_UNSIGNED;
3169 t &= ~VT_SIGNED;
3171 /* long is never used as type */
3172 if ((t & VT_BTYPE) == VT_LONG)
3173 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3174 t = (t & ~VT_BTYPE) | VT_INT;
3175 #else
3176 t = (t & ~VT_BTYPE) | VT_LLONG;
3177 #endif
3178 type->t = t;
3179 return type_found;
3182 /* convert a function parameter type (array to pointer and function to
3183 function pointer) */
3184 static inline void convert_parameter_type(CType *pt)
3186 /* remove const and volatile qualifiers (XXX: const could be used
3187 to indicate a const function parameter */
3188 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3189 /* array must be transformed to pointer according to ANSI C */
3190 pt->t &= ~VT_ARRAY;
3191 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3192 mk_pointer(pt);
3196 ST_FUNC void parse_asm_str(CString *astr)
3198 skip('(');
3199 /* read the string */
3200 if (tok != TOK_STR)
3201 expect("string constant");
3202 cstr_new(astr);
3203 while (tok == TOK_STR) {
3204 /* XXX: add \0 handling too ? */
3205 cstr_cat(astr, tokc.cstr->data);
3206 next();
3208 cstr_ccat(astr, '\0');
3211 /* Parse an asm label and return the label
3212 * Don't forget to free the CString in the caller! */
3213 static void asm_label_instr(CString *astr)
3215 next();
3216 parse_asm_str(astr);
3217 skip(')');
3218 #ifdef ASM_DEBUG
3219 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3220 #endif
3223 static void post_type(CType *type, AttributeDef *ad)
3225 int n, l, t1, arg_size, align;
3226 Sym **plast, *s, *first;
3227 AttributeDef ad1;
3228 CType pt;
3230 if (tok == '(') {
3231 /* function declaration */
3232 next();
3233 l = 0;
3234 first = NULL;
3235 plast = &first;
3236 arg_size = 0;
3237 if (tok != ')') {
3238 for(;;) {
3239 /* read param name and compute offset */
3240 if (l != FUNC_OLD) {
3241 if (!parse_btype(&pt, &ad1)) {
3242 if (l) {
3243 tcc_error("invalid type");
3244 } else {
3245 l = FUNC_OLD;
3246 goto old_proto;
3249 l = FUNC_NEW;
3250 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3251 break;
3252 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3253 if ((pt.t & VT_BTYPE) == VT_VOID)
3254 tcc_error("parameter declared as void");
3255 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3256 } else {
3257 old_proto:
3258 n = tok;
3259 if (n < TOK_UIDENT)
3260 expect("identifier");
3261 pt.t = VT_INT;
3262 next();
3264 convert_parameter_type(&pt);
3265 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3266 *plast = s;
3267 plast = &s->next;
3268 if (tok == ')')
3269 break;
3270 skip(',');
3271 if (l == FUNC_NEW && tok == TOK_DOTS) {
3272 l = FUNC_ELLIPSIS;
3273 next();
3274 break;
3278 /* if no parameters, then old type prototype */
3279 if (l == 0)
3280 l = FUNC_OLD;
3281 skip(')');
3282 /* NOTE: const is ignored in returned type as it has a special
3283 meaning in gcc / C++ */
3284 type->t &= ~VT_CONSTANT;
3285 /* some ancient pre-K&R C allows a function to return an array
3286 and the array brackets to be put after the arguments, such
3287 that "int c()[]" means something like "int[] c()" */
3288 if (tok == '[') {
3289 next();
3290 skip(']'); /* only handle simple "[]" */
3291 type->t |= VT_PTR;
3293 /* we push a anonymous symbol which will contain the function prototype */
3294 ad->a.func_args = arg_size;
3295 s = sym_push(SYM_FIELD, type, 0, l);
3296 s->a = ad->a;
3297 s->next = first;
3298 type->t = VT_FUNC;
3299 type->ref = s;
3300 } else if (tok == '[') {
3301 /* array definition */
3302 next();
3303 if (tok == TOK_RESTRICT1)
3304 next();
3305 n = -1;
3306 t1 = 0;
3307 if (tok != ']') {
3308 if (!local_stack || nocode_wanted)
3309 vpushi(expr_const());
3310 else gexpr();
3311 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3312 n = vtop->c.i;
3313 if (n < 0)
3314 tcc_error("invalid array size");
3315 } else {
3316 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3317 tcc_error("size of variable length array should be an integer");
3318 t1 = VT_VLA;
3321 skip(']');
3322 /* parse next post type */
3323 post_type(type, ad);
3324 if (type->t == VT_FUNC)
3325 tcc_error("declaration of an array of functions");
3326 t1 |= type->t & VT_VLA;
3328 if (t1 & VT_VLA) {
3329 loc -= type_size(&int_type, &align);
3330 loc &= -align;
3331 n = loc;
3333 vla_runtime_type_size(type, &align);
3334 gen_op('*');
3335 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3336 vswap();
3337 vstore();
3339 if (n != -1)
3340 vpop();
3342 /* we push an anonymous symbol which will contain the array
3343 element type */
3344 s = sym_push(SYM_FIELD, type, 0, n);
3345 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3346 type->ref = s;
3350 /* Parse a type declaration (except basic type), and return the type
3351 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3352 expected. 'type' should contain the basic type. 'ad' is the
3353 attribute definition of the basic type. It can be modified by
3354 type_decl().
3356 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3358 Sym *s;
3359 CType type1, *type2;
3360 int qualifiers, storage;
3362 while (tok == '*') {
3363 qualifiers = 0;
3364 redo:
3365 next();
3366 switch(tok) {
3367 case TOK_CONST1:
3368 case TOK_CONST2:
3369 case TOK_CONST3:
3370 qualifiers |= VT_CONSTANT;
3371 goto redo;
3372 case TOK_VOLATILE1:
3373 case TOK_VOLATILE2:
3374 case TOK_VOLATILE3:
3375 qualifiers |= VT_VOLATILE;
3376 goto redo;
3377 case TOK_RESTRICT1:
3378 case TOK_RESTRICT2:
3379 case TOK_RESTRICT3:
3380 goto redo;
3382 mk_pointer(type);
3383 type->t |= qualifiers;
3386 /* XXX: clarify attribute handling */
3387 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3388 parse_attribute(ad);
3390 /* recursive type */
3391 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3392 type1.t = 0; /* XXX: same as int */
3393 if (tok == '(') {
3394 next();
3395 /* XXX: this is not correct to modify 'ad' at this point, but
3396 the syntax is not clear */
3397 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3398 parse_attribute(ad);
3399 type_decl(&type1, ad, v, td);
3400 skip(')');
3401 } else {
3402 /* type identifier */
3403 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3404 *v = tok;
3405 next();
3406 } else {
3407 if (!(td & TYPE_ABSTRACT))
3408 expect("identifier");
3409 *v = 0;
3412 storage = type->t & VT_STORAGE;
3413 type->t &= ~VT_STORAGE;
3414 if (storage & VT_STATIC) {
3415 int saved_nocode_wanted = nocode_wanted;
3416 nocode_wanted = 1;
3417 post_type(type, ad);
3418 nocode_wanted = saved_nocode_wanted;
3419 } else
3420 post_type(type, ad);
3421 type->t |= storage;
3422 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3423 parse_attribute(ad);
3425 if (!type1.t)
3426 return;
3427 /* append type at the end of type1 */
3428 type2 = &type1;
3429 for(;;) {
3430 s = type2->ref;
3431 type2 = &s->type;
3432 if (!type2->t) {
3433 *type2 = *type;
3434 break;
3437 *type = type1;
3440 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3441 ST_FUNC int lvalue_type(int t)
3443 int bt, r;
3444 r = VT_LVAL;
3445 bt = t & VT_BTYPE;
3446 if (bt == VT_BYTE || bt == VT_BOOL)
3447 r |= VT_LVAL_BYTE;
3448 else if (bt == VT_SHORT)
3449 r |= VT_LVAL_SHORT;
3450 else
3451 return r;
3452 if (t & VT_UNSIGNED)
3453 r |= VT_LVAL_UNSIGNED;
3454 return r;
3457 /* indirection with full error checking and bound check */
3458 ST_FUNC void indir(void)
3460 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3461 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3462 return;
3463 expect("pointer");
3465 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3466 gv(RC_INT);
3467 vtop->type = *pointed_type(&vtop->type);
3468 /* Arrays and functions are never lvalues */
3469 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3470 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3471 vtop->r |= lvalue_type(vtop->type.t);
3472 /* if bound checking, the referenced pointer must be checked */
3473 #ifdef CONFIG_TCC_BCHECK
3474 if (tcc_state->do_bounds_check)
3475 vtop->r |= VT_MUSTBOUND;
3476 #endif
3480 /* pass a parameter to a function and do type checking and casting */
3481 static void gfunc_param_typed(Sym *func, Sym *arg)
3483 int func_type;
3484 CType type;
3486 func_type = func->c;
3487 if (func_type == FUNC_OLD ||
3488 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3489 /* default casting : only need to convert float to double */
3490 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3491 type.t = VT_DOUBLE;
3492 gen_cast(&type);
3493 } else if (vtop->type.t & VT_BITFIELD) {
3494 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3495 gen_cast(&type);
3497 } else if (arg == NULL) {
3498 tcc_error("too many arguments to function");
3499 } else {
3500 type = arg->type;
3501 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3502 gen_assign_cast(&type);
3506 /* parse an expression of the form '(type)' or '(expr)' and return its
3507 type */
3508 static void parse_expr_type(CType *type)
3510 int n;
3511 AttributeDef ad;
3513 skip('(');
3514 if (parse_btype(type, &ad)) {
3515 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3516 } else {
3517 expr_type(type);
3519 skip(')');
3522 static void parse_type(CType *type)
3524 AttributeDef ad;
3525 int n;
3527 if (!parse_btype(type, &ad)) {
3528 expect("type");
3530 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3533 static void vpush_tokc(int t)
3535 CType type;
3536 type.t = t;
3537 type.ref = 0;
3538 vsetc(&type, VT_CONST, &tokc);
3541 ST_FUNC void unary(void)
3543 int n, t, align, size, r, sizeof_caller;
3544 CType type;
3545 Sym *s;
3546 AttributeDef ad;
3547 static int in_sizeof = 0;
3549 sizeof_caller = in_sizeof;
3550 in_sizeof = 0;
3551 /* XXX: GCC 2.95.3 does not generate a table although it should be
3552 better here */
3553 tok_next:
3554 switch(tok) {
3555 case TOK_EXTENSION:
3556 next();
3557 goto tok_next;
3558 case TOK_CINT:
3559 case TOK_CCHAR:
3560 case TOK_LCHAR:
3561 vpushi(tokc.i);
3562 next();
3563 break;
3564 case TOK_CUINT:
3565 vpush_tokc(VT_INT | VT_UNSIGNED);
3566 next();
3567 break;
3568 case TOK_CLLONG:
3569 vpush_tokc(VT_LLONG);
3570 next();
3571 break;
3572 case TOK_CULLONG:
3573 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3574 next();
3575 break;
3576 case TOK_CFLOAT:
3577 vpush_tokc(VT_FLOAT);
3578 next();
3579 break;
3580 case TOK_CDOUBLE:
3581 vpush_tokc(VT_DOUBLE);
3582 next();
3583 break;
3584 case TOK_CLDOUBLE:
3585 vpush_tokc(VT_LDOUBLE);
3586 next();
3587 break;
3588 case TOK___FUNCTION__:
3589 if (!gnu_ext)
3590 goto tok_identifier;
3591 /* fall thru */
3592 case TOK___FUNC__:
3594 void *ptr;
3595 int len;
3596 /* special function name identifier */
3597 len = strlen(funcname) + 1;
3598 /* generate char[len] type */
3599 type.t = VT_BYTE;
3600 mk_pointer(&type);
3601 type.t |= VT_ARRAY;
3602 type.ref->c = len;
3603 vpush_ref(&type, data_section, data_section->data_offset, len);
3604 ptr = section_ptr_add(data_section, len);
3605 memcpy(ptr, funcname, len);
3606 next();
3608 break;
3609 case TOK_LSTR:
3610 #ifdef TCC_TARGET_PE
3611 t = VT_SHORT | VT_UNSIGNED;
3612 #else
3613 t = VT_INT;
3614 #endif
3615 goto str_init;
3616 case TOK_STR:
3617 /* string parsing */
3618 t = VT_BYTE;
3619 str_init:
3620 if (tcc_state->warn_write_strings)
3621 t |= VT_CONSTANT;
3622 type.t = t;
3623 mk_pointer(&type);
3624 type.t |= VT_ARRAY;
3625 memset(&ad, 0, sizeof(AttributeDef));
3626 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3627 break;
3628 case '(':
3629 next();
3630 /* cast ? */
3631 if (parse_btype(&type, &ad)) {
3632 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3633 skip(')');
3634 /* check ISOC99 compound literal */
3635 if (tok == '{') {
3636 /* data is allocated locally by default */
3637 if (global_expr)
3638 r = VT_CONST;
3639 else
3640 r = VT_LOCAL;
3641 /* all except arrays are lvalues */
3642 if (!(type.t & VT_ARRAY))
3643 r |= lvalue_type(type.t);
3644 memset(&ad, 0, sizeof(AttributeDef));
3645 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3646 } else {
3647 if (sizeof_caller) {
3648 vpush(&type);
3649 return;
3651 unary();
3652 gen_cast(&type);
3654 } else if (tok == '{') {
3655 /* save all registers */
3656 save_regs(0);
3657 /* statement expression : we do not accept break/continue
3658 inside as GCC does */
3659 block(NULL, NULL, NULL, NULL, 0, 1);
3660 skip(')');
3661 } else {
3662 gexpr();
3663 skip(')');
3665 break;
3666 case '*':
3667 next();
3668 unary();
3669 indir();
3670 break;
3671 case '&':
3672 next();
3673 unary();
3674 /* functions names must be treated as function pointers,
3675 except for unary '&' and sizeof. Since we consider that
3676 functions are not lvalues, we only have to handle it
3677 there and in function calls. */
3678 /* arrays can also be used although they are not lvalues */
3679 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3680 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3681 test_lvalue();
3682 mk_pointer(&vtop->type);
3683 gaddrof();
3684 break;
3685 case '!':
3686 next();
3687 unary();
3688 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3689 CType boolean;
3690 boolean.t = VT_BOOL;
3691 gen_cast(&boolean);
3692 vtop->c.i = !vtop->c.i;
3693 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3694 vtop->c.i = vtop->c.i ^ 1;
3695 else {
3696 save_regs(1);
3697 vseti(VT_JMP, gvtst(1, 0));
3699 break;
3700 case '~':
3701 next();
3702 unary();
3703 vpushi(-1);
3704 gen_op('^');
3705 break;
3706 case '+':
3707 next();
3708 unary();
3709 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3710 tcc_error("pointer not accepted for unary plus");
3711 /* In order to force cast, we add zero, except for floating point
3712 where we really need an noop (otherwise -0.0 will be transformed
3713 into +0.0). */
3714 if (!is_float(vtop->type.t)) {
3715 vpushi(0);
3716 gen_op('+');
3718 break;
3719 case TOK_SIZEOF:
3720 case TOK_ALIGNOF1:
3721 case TOK_ALIGNOF2:
3722 t = tok;
3723 next();
3724 in_sizeof++;
3725 unary_type(&type); // Perform a in_sizeof = 0;
3726 size = type_size(&type, &align);
3727 if (t == TOK_SIZEOF) {
3728 if (!(type.t & VT_VLA)) {
3729 if (size < 0)
3730 tcc_error("sizeof applied to an incomplete type");
3731 vpushs(size);
3732 } else {
3733 vla_runtime_type_size(&type, &align);
3735 } else {
3736 vpushs(align);
3738 vtop->type.t |= VT_UNSIGNED;
3739 break;
3741 case TOK_builtin_types_compatible_p:
3743 CType type1, type2;
3744 next();
3745 skip('(');
3746 parse_type(&type1);
3747 skip(',');
3748 parse_type(&type2);
3749 skip(')');
3750 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3751 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3752 vpushi(is_compatible_types(&type1, &type2));
3754 break;
3755 case TOK_builtin_constant_p:
3757 int saved_nocode_wanted, res;
3758 next();
3759 skip('(');
3760 saved_nocode_wanted = nocode_wanted;
3761 nocode_wanted = 1;
3762 gexpr();
3763 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3764 vpop();
3765 nocode_wanted = saved_nocode_wanted;
3766 skip(')');
3767 vpushi(res);
3769 break;
3770 case TOK_builtin_frame_address:
3772 int level;
3773 CType type;
3774 next();
3775 skip('(');
3776 if (tok != TOK_CINT || tokc.i < 0) {
3777 tcc_error("__builtin_frame_address only takes positive integers");
3779 level = tokc.i;
3780 next();
3781 skip(')');
3782 type.t = VT_VOID;
3783 mk_pointer(&type);
3784 vset(&type, VT_LOCAL, 0); /* local frame */
3785 while (level--) {
3786 mk_pointer(&vtop->type);
3787 indir(); /* -> parent frame */
3790 break;
3791 #ifdef TCC_TARGET_X86_64
3792 #ifdef TCC_TARGET_PE
3793 case TOK_builtin_va_start:
3795 next();
3796 skip('(');
3797 expr_eq();
3798 skip(',');
3799 expr_eq();
3800 skip(')');
3801 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3802 tcc_error("__builtin_va_start expects a local variable");
3803 vtop->r &= ~(VT_LVAL | VT_REF);
3804 vtop->type = char_pointer_type;
3805 vstore();
3807 break;
3808 #else
3809 case TOK_builtin_va_arg_types:
3811 CType type;
3812 next();
3813 skip('(');
3814 parse_type(&type);
3815 skip(')');
3816 vpushi(classify_x86_64_va_arg(&type));
3818 break;
3819 #endif
3820 #endif
3821 case TOK_INC:
3822 case TOK_DEC:
3823 t = tok;
3824 next();
3825 unary();
3826 inc(0, t);
3827 break;
3828 case '-':
3829 next();
3830 unary();
3831 t = vtop->type.t & VT_BTYPE;
3832 if (is_float(t)) {
3833 /* In IEEE negate(x) isn't subtract(0,x), but rather
3834 subtract(-0, x). */
3835 vpush(&vtop->type);
3836 if (t == VT_FLOAT)
3837 vtop->c.f = -0.0f;
3838 else if (t == VT_DOUBLE)
3839 vtop->c.d = -0.0;
3840 else
3841 vtop->c.ld = -0.0;
3842 } else
3843 vpushi(0);
3844 vswap();
3845 gen_op('-');
3846 break;
3847 case TOK_LAND:
3848 if (!gnu_ext)
3849 goto tok_identifier;
3850 next();
3851 /* allow to take the address of a label */
3852 if (tok < TOK_UIDENT)
3853 expect("label identifier");
3854 s = label_find(tok);
3855 if (!s) {
3856 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3857 } else {
3858 if (s->r == LABEL_DECLARED)
3859 s->r = LABEL_FORWARD;
3861 if (!s->type.t) {
3862 s->type.t = VT_VOID;
3863 mk_pointer(&s->type);
3864 s->type.t |= VT_STATIC;
3866 vset(&s->type, VT_CONST | VT_SYM, 0);
3867 vtop->sym = s;
3868 next();
3869 break;
3871 // special qnan , snan and infinity values
3872 case TOK___NAN__:
3873 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3874 next();
3875 break;
3876 case TOK___SNAN__:
3877 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3878 next();
3879 break;
3880 case TOK___INF__:
3881 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3882 next();
3883 break;
3885 default:
3886 tok_identifier:
3887 t = tok;
3888 next();
3889 if (t < TOK_UIDENT)
3890 expect("identifier");
3891 s = sym_find(t);
3892 if (!s) {
3893 if (tok != '(')
3894 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3895 /* for simple function calls, we tolerate undeclared
3896 external reference to int() function */
3897 if (tcc_state->warn_implicit_function_declaration)
3898 tcc_warning("implicit declaration of function '%s'",
3899 get_tok_str(t, NULL));
3900 s = external_global_sym(t, &func_old_type, 0);
3902 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3903 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3904 /* if referencing an inline function, then we generate a
3905 symbol to it if not already done. It will have the
3906 effect to generate code for it at the end of the
3907 compilation unit. Inline function as always
3908 generated in the text section. */
3909 if (!s->c)
3910 put_extern_sym(s, text_section, 0, 0);
3911 r = VT_SYM | VT_CONST;
3912 } else {
3913 r = s->r;
3915 vset(&s->type, r, s->c);
3916 /* if forward reference, we must point to s */
3917 if (vtop->r & VT_SYM) {
3918 vtop->sym = s;
3919 vtop->c.ull = 0;
3921 break;
3924 /* post operations */
3925 while (1) {
3926 if (tok == TOK_INC || tok == TOK_DEC) {
3927 inc(1, tok);
3928 next();
3929 } else if (tok == '.' || tok == TOK_ARROW) {
3930 int qualifiers;
3931 /* field */
3932 if (tok == TOK_ARROW)
3933 indir();
3934 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3935 test_lvalue();
3936 gaddrof();
3937 next();
3938 /* expect pointer on structure */
3939 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3940 expect("struct or union");
3941 s = vtop->type.ref;
3942 /* find field */
3943 tok |= SYM_FIELD;
3944 while ((s = s->next) != NULL) {
3945 if (s->v == tok)
3946 break;
3948 if (!s)
3949 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3950 /* add field offset to pointer */
3951 vtop->type = char_pointer_type; /* change type to 'char *' */
3952 vpushi(s->c);
3953 gen_op('+');
3954 /* change type to field type, and set to lvalue */
3955 vtop->type = s->type;
3956 vtop->type.t |= qualifiers;
3957 /* an array is never an lvalue */
3958 if (!(vtop->type.t & VT_ARRAY)) {
3959 vtop->r |= lvalue_type(vtop->type.t);
3960 #ifdef CONFIG_TCC_BCHECK
3961 /* if bound checking, the referenced pointer must be checked */
3962 if (tcc_state->do_bounds_check)
3963 vtop->r |= VT_MUSTBOUND;
3964 #endif
3966 next();
3967 } else if (tok == '[') {
3968 next();
3969 gexpr();
3970 gen_op('+');
3971 indir();
3972 skip(']');
3973 } else if (tok == '(') {
3974 SValue ret;
3975 Sym *sa;
3976 int nb_args, ret_nregs, ret_align, variadic;
3978 /* function call */
3979 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3980 /* pointer test (no array accepted) */
3981 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3982 vtop->type = *pointed_type(&vtop->type);
3983 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3984 goto error_func;
3985 } else {
3986 error_func:
3987 expect("function pointer");
3989 } else {
3990 vtop->r &= ~VT_LVAL; /* no lvalue */
3992 /* get return type */
3993 s = vtop->type.ref;
3994 next();
3995 sa = s->next; /* first parameter */
3996 nb_args = 0;
3997 ret.r2 = VT_CONST;
3998 /* compute first implicit argument if a structure is returned */
3999 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4000 variadic = (s->c == FUNC_ELLIPSIS);
4001 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4002 &ret_align);
4003 if (!ret_nregs) {
4004 /* get some space for the returned structure */
4005 size = type_size(&s->type, &align);
4006 loc = (loc - size) & -align;
4007 ret.type = s->type;
4008 ret.r = VT_LOCAL | VT_LVAL;
4009 /* pass it as 'int' to avoid structure arg passing
4010 problems */
4011 vseti(VT_LOCAL, loc);
4012 ret.c = vtop->c;
4013 nb_args++;
4015 } else {
4016 ret_nregs = 1;
4017 ret.type = s->type;
4020 if (ret_nregs) {
4021 /* return in register */
4022 if (is_float(ret.type.t)) {
4023 ret.r = reg_fret(ret.type.t);
4024 #ifdef TCC_TARGET_X86_64
4025 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4026 ret.r2 = REG_QRET;
4027 #endif
4028 } else {
4029 #ifdef TCC_TARGET_X86_64
4030 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4031 #else
4032 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4033 #endif
4034 ret.r2 = REG_LRET;
4035 ret.r = REG_IRET;
4037 ret.c.i = 0;
4039 if (tok != ')') {
4040 for(;;) {
4041 expr_eq();
4042 gfunc_param_typed(s, sa);
4043 nb_args++;
4044 if (sa)
4045 sa = sa->next;
4046 if (tok == ')')
4047 break;
4048 skip(',');
4051 if (sa)
4052 tcc_error("too few arguments to function");
4053 skip(')');
4054 if (!nocode_wanted) {
4055 gfunc_call(nb_args);
4056 } else {
4057 vtop -= (nb_args + 1);
4060 /* return value */
4061 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4062 vsetc(&ret.type, r, &ret.c);
4063 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4066 /* handle packed struct return */
4067 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4068 int addr, offset;
4070 size = type_size(&s->type, &align);
4071 loc = (loc - size) & -align;
4072 addr = loc;
4073 offset = 0;
4074 for (;;) {
4075 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4076 vswap();
4077 vstore();
4078 vtop--;
4079 if (--ret_nregs == 0)
4080 break;
4081 /* XXX: compatible with arm only: ret_align == register_size */
4082 offset += ret_align;
4084 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4086 } else {
4087 break;
4092 ST_FUNC void expr_prod(void)
4094 int t;
4096 unary();
4097 while (tok == '*' || tok == '/' || tok == '%') {
4098 t = tok;
4099 next();
4100 unary();
4101 gen_op(t);
4105 ST_FUNC void expr_sum(void)
4107 int t;
4109 expr_prod();
4110 while (tok == '+' || tok == '-') {
4111 t = tok;
4112 next();
4113 expr_prod();
4114 gen_op(t);
4118 static void expr_shift(void)
4120 int t;
4122 expr_sum();
4123 while (tok == TOK_SHL || tok == TOK_SAR) {
4124 t = tok;
4125 next();
4126 expr_sum();
4127 gen_op(t);
4131 static void expr_cmp(void)
4133 int t;
4135 expr_shift();
4136 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4137 tok == TOK_ULT || tok == TOK_UGE) {
4138 t = tok;
4139 next();
4140 expr_shift();
4141 gen_op(t);
4145 static void expr_cmpeq(void)
4147 int t;
4149 expr_cmp();
4150 while (tok == TOK_EQ || tok == TOK_NE) {
4151 t = tok;
4152 next();
4153 expr_cmp();
4154 gen_op(t);
4158 static void expr_and(void)
4160 expr_cmpeq();
4161 while (tok == '&') {
4162 next();
4163 expr_cmpeq();
4164 gen_op('&');
4168 static void expr_xor(void)
4170 expr_and();
4171 while (tok == '^') {
4172 next();
4173 expr_and();
4174 gen_op('^');
4178 static void expr_or(void)
4180 expr_xor();
4181 while (tok == '|') {
4182 next();
4183 expr_xor();
4184 gen_op('|');
4188 /* XXX: fix this mess */
4189 static void expr_land_const(void)
4191 expr_or();
4192 while (tok == TOK_LAND) {
4193 next();
4194 expr_or();
4195 gen_op(TOK_LAND);
4199 /* XXX: fix this mess */
4200 static void expr_lor_const(void)
4202 expr_land_const();
4203 while (tok == TOK_LOR) {
4204 next();
4205 expr_land_const();
4206 gen_op(TOK_LOR);
4210 /* only used if non constant */
4211 static void expr_land(void)
4213 int t;
4215 expr_or();
4216 if (tok == TOK_LAND) {
4217 t = 0;
4218 save_regs(1);
4219 for(;;) {
4220 t = gvtst(1, t);
4221 if (tok != TOK_LAND) {
4222 vseti(VT_JMPI, t);
4223 break;
4225 next();
4226 expr_or();
4231 static void expr_lor(void)
4233 int t;
4235 expr_land();
4236 if (tok == TOK_LOR) {
4237 t = 0;
4238 save_regs(1);
4239 for(;;) {
4240 t = gvtst(0, t);
4241 if (tok != TOK_LOR) {
4242 vseti(VT_JMP, t);
4243 break;
4245 next();
4246 expr_land();
4251 /* XXX: better constant handling */
4252 static void expr_cond(void)
4254 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4255 SValue sv;
4256 CType type, type1, type2;
4258 if (const_wanted) {
4259 expr_lor_const();
4260 if (tok == '?') {
4261 CType boolean;
4262 int c;
4263 boolean.t = VT_BOOL;
4264 vdup();
4265 gen_cast(&boolean);
4266 c = vtop->c.i;
4267 vpop();
4268 next();
4269 if (tok != ':' || !gnu_ext) {
4270 vpop();
4271 gexpr();
4273 if (!c)
4274 vpop();
4275 skip(':');
4276 expr_cond();
4277 if (c)
4278 vpop();
4280 } else {
4281 expr_lor();
4282 if (tok == '?') {
4283 next();
4284 if (vtop != vstack) {
4285 /* needed to avoid having different registers saved in
4286 each branch */
4287 if (is_float(vtop->type.t)) {
4288 rc = RC_FLOAT;
4289 #ifdef TCC_TARGET_X86_64
4290 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4291 rc = RC_ST0;
4293 #endif
4295 else
4296 rc = RC_INT;
4297 gv(rc);
4298 save_regs(1);
4300 if (tok == ':' && gnu_ext) {
4301 gv_dup();
4302 tt = gvtst(1, 0);
4303 } else {
4304 tt = gvtst(1, 0);
4305 gexpr();
4307 type1 = vtop->type;
4308 sv = *vtop; /* save value to handle it later */
4309 vtop--; /* no vpop so that FP stack is not flushed */
4310 skip(':');
4311 u = gjmp(0);
4312 gsym(tt);
4313 expr_cond();
4314 type2 = vtop->type;
4316 t1 = type1.t;
4317 bt1 = t1 & VT_BTYPE;
4318 t2 = type2.t;
4319 bt2 = t2 & VT_BTYPE;
4320 /* cast operands to correct type according to ISOC rules */
4321 if (is_float(bt1) || is_float(bt2)) {
4322 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4323 type.t = VT_LDOUBLE;
4324 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4325 type.t = VT_DOUBLE;
4326 } else {
4327 type.t = VT_FLOAT;
4329 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4330 /* cast to biggest op */
4331 type.t = VT_LLONG;
4332 /* convert to unsigned if it does not fit in a long long */
4333 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4334 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4335 type.t |= VT_UNSIGNED;
4336 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4337 /* If one is a null ptr constant the result type
4338 is the other. */
4339 if (is_null_pointer (vtop))
4340 type = type1;
4341 else if (is_null_pointer (&sv))
4342 type = type2;
4343 /* XXX: test pointer compatibility, C99 has more elaborate
4344 rules here. */
4345 else
4346 type = type1;
4347 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4348 /* XXX: test function pointer compatibility */
4349 type = bt1 == VT_FUNC ? type1 : type2;
4350 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4351 /* XXX: test structure compatibility */
4352 type = bt1 == VT_STRUCT ? type1 : type2;
4353 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4354 /* NOTE: as an extension, we accept void on only one side */
4355 type.t = VT_VOID;
4356 } else {
4357 /* integer operations */
4358 type.t = VT_INT;
4359 /* convert to unsigned if it does not fit in an integer */
4360 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4361 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4362 type.t |= VT_UNSIGNED;
4365 /* now we convert second operand */
4366 gen_cast(&type);
4367 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4368 gaddrof();
4369 rc = RC_INT;
4370 if (is_float(type.t)) {
4371 rc = RC_FLOAT;
4372 #ifdef TCC_TARGET_X86_64
4373 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4374 rc = RC_ST0;
4376 #endif
4377 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4378 /* for long longs, we use fixed registers to avoid having
4379 to handle a complicated move */
4380 rc = RC_IRET;
4383 r2 = gv(rc);
4384 /* this is horrible, but we must also convert first
4385 operand */
4386 tt = gjmp(0);
4387 gsym(u);
4388 /* put again first value and cast it */
4389 *vtop = sv;
4390 gen_cast(&type);
4391 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4392 gaddrof();
4393 r1 = gv(rc);
4394 move_reg(r2, r1, type.t);
4395 vtop->r = r2;
4396 gsym(tt);
4401 static void expr_eq(void)
4403 int t;
4405 expr_cond();
4406 if (tok == '=' ||
4407 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4408 tok == TOK_A_XOR || tok == TOK_A_OR ||
4409 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4410 test_lvalue();
4411 t = tok;
4412 next();
4413 if (t == '=') {
4414 expr_eq();
4415 } else {
4416 vdup();
4417 expr_eq();
4418 gen_op(t & 0x7f);
4420 vstore();
4424 ST_FUNC void gexpr(void)
4426 while (1) {
4427 expr_eq();
4428 if (tok != ',')
4429 break;
4430 vpop();
4431 next();
4435 /* parse an expression and return its type without any side effect. */
4436 static void expr_type(CType *type)
4438 int saved_nocode_wanted;
4440 saved_nocode_wanted = nocode_wanted;
4441 nocode_wanted = 1;
4442 gexpr();
4443 *type = vtop->type;
4444 vpop();
4445 nocode_wanted = saved_nocode_wanted;
4448 /* parse a unary expression and return its type without any side
4449 effect. */
4450 static void unary_type(CType *type)
4452 int a;
4454 a = nocode_wanted;
4455 nocode_wanted = 1;
4456 unary();
4457 *type = vtop->type;
4458 vpop();
4459 nocode_wanted = a;
4462 /* parse a constant expression and return value in vtop. */
4463 static void expr_const1(void)
4465 int a;
4466 a = const_wanted;
4467 const_wanted = 1;
4468 expr_cond();
4469 const_wanted = a;
4472 /* parse an integer constant and return its value. */
4473 ST_FUNC int expr_const(void)
4475 int c;
4476 expr_const1();
4477 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4478 expect("constant expression");
4479 c = vtop->c.i;
4480 vpop();
4481 return c;
4484 /* return the label token if current token is a label, otherwise
4485 return zero */
4486 static int is_label(void)
4488 int last_tok;
4490 /* fast test first */
4491 if (tok < TOK_UIDENT)
4492 return 0;
4493 /* no need to save tokc because tok is an identifier */
4494 last_tok = tok;
4495 next();
4496 if (tok == ':') {
4497 next();
4498 return last_tok;
4499 } else {
4500 unget_tok(last_tok);
4501 return 0;
4505 static void label_or_decl(int l)
4507 int last_tok;
4509 /* fast test first */
4510 if (tok >= TOK_UIDENT)
4512 /* no need to save tokc because tok is an identifier */
4513 last_tok = tok;
4514 next();
4515 if (tok == ':') {
4516 unget_tok(last_tok);
4517 return;
4519 unget_tok(last_tok);
4521 decl(l);
4524 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4525 int case_reg, int is_expr)
4527 int a, b, c, d;
4528 Sym *s, *frame_bottom;
4530 /* generate line number info */
4531 if (tcc_state->do_debug &&
4532 (last_line_num != file->line_num || last_ind != ind)) {
4533 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4534 last_ind = ind;
4535 last_line_num = file->line_num;
4538 if (is_expr) {
4539 /* default return value is (void) */
4540 vpushi(0);
4541 vtop->type.t = VT_VOID;
4544 if (tok == TOK_IF) {
4545 /* if test */
4546 next();
4547 skip('(');
4548 gexpr();
4549 skip(')');
4550 a = gvtst(1, 0);
4551 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4552 c = tok;
4553 if (c == TOK_ELSE) {
4554 next();
4555 d = gjmp(0);
4556 gsym(a);
4557 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4558 gsym(d); /* patch else jmp */
4559 } else
4560 gsym(a);
4561 } else if (tok == TOK_WHILE) {
4562 next();
4563 d = ind;
4564 skip('(');
4565 gexpr();
4566 skip(')');
4567 a = gvtst(1, 0);
4568 b = 0;
4569 block(&a, &b, case_sym, def_sym, case_reg, 0);
4570 gjmp_addr(d);
4571 gsym(a);
4572 gsym_addr(b, d);
4573 } else if (tok == '{') {
4574 Sym *llabel;
4575 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4577 next();
4578 /* record local declaration stack position */
4579 s = local_stack;
4580 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4581 frame_bottom->next = scope_stack_bottom;
4582 scope_stack_bottom = frame_bottom;
4583 llabel = local_label_stack;
4585 /* save VLA state */
4586 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4587 if (saved_vla_sp_loc != &vla_sp_root_loc)
4588 vla_sp_loc = &block_vla_sp_loc;
4590 saved_vla_flags = vla_flags;
4591 vla_flags |= VLA_NEED_NEW_FRAME;
4593 /* handle local labels declarations */
4594 if (tok == TOK_LABEL) {
4595 next();
4596 for(;;) {
4597 if (tok < TOK_UIDENT)
4598 expect("label identifier");
4599 label_push(&local_label_stack, tok, LABEL_DECLARED);
4600 next();
4601 if (tok == ',') {
4602 next();
4603 } else {
4604 skip(';');
4605 break;
4609 while (tok != '}') {
4610 label_or_decl(VT_LOCAL);
4611 if (tok != '}') {
4612 if (is_expr)
4613 vpop();
4614 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4617 /* pop locally defined labels */
4618 label_pop(&local_label_stack, llabel);
4619 if(is_expr) {
4620 /* XXX: this solution makes only valgrind happy...
4621 triggered by gcc.c-torture/execute/20000917-1.c */
4622 Sym *p;
4623 switch(vtop->type.t & VT_BTYPE) {
4624 case VT_PTR:
4625 case VT_STRUCT:
4626 case VT_ENUM:
4627 case VT_FUNC:
4628 for(p=vtop->type.ref;p;p=p->prev)
4629 if(p->prev==s)
4630 tcc_error("unsupported expression type");
4633 /* pop locally defined symbols */
4634 scope_stack_bottom = scope_stack_bottom->next;
4635 sym_pop(&local_stack, s);
4637 /* Pop VLA frames and restore stack pointer if required */
4638 if (saved_vla_sp_loc != &vla_sp_root_loc)
4639 *saved_vla_sp_loc = block_vla_sp_loc;
4640 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4641 vla_sp_loc = saved_vla_sp_loc;
4642 gen_vla_sp_restore(*vla_sp_loc);
4644 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4646 next();
4647 } else if (tok == TOK_RETURN) {
4648 next();
4649 if (tok != ';') {
4650 gexpr();
4651 gen_assign_cast(&func_vt);
4652 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4653 CType type, ret_type;
4654 int ret_align, ret_nregs;
4655 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4656 &ret_align);
4657 if (0 == ret_nregs) {
4658 /* if returning structure, must copy it to implicit
4659 first pointer arg location */
4660 type = func_vt;
4661 mk_pointer(&type);
4662 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4663 indir();
4664 vswap();
4665 /* copy structure value to pointer */
4666 vstore();
4667 } else {
4668 /* returning structure packed into registers */
4669 int r, size, addr, align;
4670 size = type_size(&func_vt,&align);
4671 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4672 && (align & (ret_align-1))) {
4673 loc = (loc - size) & -align;
4674 addr = loc;
4675 type = func_vt;
4676 vset(&type, VT_LOCAL | VT_LVAL, addr);
4677 vswap();
4678 vstore();
4679 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4681 vtop->type = ret_type;
4682 if (is_float(ret_type.t))
4683 r = rc_fret(ret_type.t);
4684 else
4685 r = RC_IRET;
4687 for (;;) {
4688 gv(r);
4689 if (--ret_nregs == 0)
4690 break;
4691 /* We assume that when a structure is returned in multiple
4692 registers, their classes are consecutive values of the
4693 suite s(n) = 2^n */
4694 r <<= 1;
4695 /* XXX: compatible with arm only: ret_align == register_size */
4696 vtop->c.i += ret_align;
4697 vtop->r = VT_LOCAL | VT_LVAL;
4700 } else if (is_float(func_vt.t)) {
4701 gv(rc_fret(func_vt.t));
4702 } else {
4703 gv(RC_IRET);
4705 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4707 skip(';');
4708 rsym = gjmp(rsym); /* jmp */
4709 } else if (tok == TOK_BREAK) {
4710 /* compute jump */
4711 if (!bsym)
4712 tcc_error("cannot break");
4713 *bsym = gjmp(*bsym);
4714 next();
4715 skip(';');
4716 } else if (tok == TOK_CONTINUE) {
4717 /* compute jump */
4718 if (!csym)
4719 tcc_error("cannot continue");
4720 *csym = gjmp(*csym);
4721 next();
4722 skip(';');
4723 } else if (tok == TOK_FOR) {
4724 int e;
4725 next();
4726 skip('(');
4727 s = local_stack;
4728 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4729 frame_bottom->next = scope_stack_bottom;
4730 scope_stack_bottom = frame_bottom;
4731 if (tok != ';') {
4732 /* c99 for-loop init decl? */
4733 if (!decl0(VT_LOCAL, 1)) {
4734 /* no, regular for-loop init expr */
4735 gexpr();
4736 vpop();
4739 skip(';');
4740 d = ind;
4741 c = ind;
4742 a = 0;
4743 b = 0;
4744 if (tok != ';') {
4745 gexpr();
4746 a = gvtst(1, 0);
4748 skip(';');
4749 if (tok != ')') {
4750 e = gjmp(0);
4751 c = ind;
4752 gexpr();
4753 vpop();
4754 gjmp_addr(d);
4755 gsym(e);
4757 skip(')');
4758 block(&a, &b, case_sym, def_sym, case_reg, 0);
4759 gjmp_addr(c);
4760 gsym(a);
4761 gsym_addr(b, c);
4762 scope_stack_bottom = scope_stack_bottom->next;
4763 sym_pop(&local_stack, s);
4764 } else
4765 if (tok == TOK_DO) {
4766 next();
4767 a = 0;
4768 b = 0;
4769 d = ind;
4770 block(&a, &b, case_sym, def_sym, case_reg, 0);
4771 skip(TOK_WHILE);
4772 skip('(');
4773 gsym(b);
4774 gexpr();
4775 c = gvtst(0, 0);
4776 gsym_addr(c, d);
4777 skip(')');
4778 gsym(a);
4779 skip(';');
4780 } else
4781 if (tok == TOK_SWITCH) {
4782 next();
4783 skip('(');
4784 gexpr();
4785 /* XXX: other types than integer */
4786 case_reg = gv(RC_INT);
4787 vpop();
4788 skip(')');
4789 a = 0;
4790 b = gjmp(0); /* jump to first case */
4791 c = 0;
4792 block(&a, csym, &b, &c, case_reg, 0);
4793 /* if no default, jmp after switch */
4794 if (c == 0)
4795 c = ind;
4796 /* default label */
4797 gsym_addr(b, c);
4798 /* break label */
4799 gsym(a);
4800 } else
4801 if (tok == TOK_CASE) {
4802 int v1, v2;
4803 if (!case_sym)
4804 expect("switch");
4805 next();
4806 v1 = expr_const();
4807 v2 = v1;
4808 if (gnu_ext && tok == TOK_DOTS) {
4809 next();
4810 v2 = expr_const();
4811 if (v2 < v1)
4812 tcc_warning("empty case range");
4814 /* since a case is like a label, we must skip it with a jmp */
4815 b = gjmp(0);
4816 gsym(*case_sym);
4817 vseti(case_reg, 0);
4818 vpushi(v1);
4819 if (v1 == v2) {
4820 gen_op(TOK_EQ);
4821 *case_sym = gtst(1, 0);
4822 } else {
4823 gen_op(TOK_GE);
4824 *case_sym = gtst(1, 0);
4825 vseti(case_reg, 0);
4826 vpushi(v2);
4827 gen_op(TOK_LE);
4828 *case_sym = gtst(1, *case_sym);
4830 gsym(b);
4831 skip(':');
4832 is_expr = 0;
4833 goto block_after_label;
4834 } else
4835 if (tok == TOK_DEFAULT) {
4836 next();
4837 skip(':');
4838 if (!def_sym)
4839 expect("switch");
4840 if (*def_sym)
4841 tcc_error("too many 'default'");
4842 *def_sym = ind;
4843 is_expr = 0;
4844 goto block_after_label;
4845 } else
4846 if (tok == TOK_GOTO) {
4847 next();
4848 if (tok == '*' && gnu_ext) {
4849 /* computed goto */
4850 next();
4851 gexpr();
4852 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4853 expect("pointer");
4854 ggoto();
4855 } else if (tok >= TOK_UIDENT) {
4856 s = label_find(tok);
4857 /* put forward definition if needed */
4858 if (!s) {
4859 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4860 } else {
4861 if (s->r == LABEL_DECLARED)
4862 s->r = LABEL_FORWARD;
4864 /* label already defined */
4865 if (vla_flags & VLA_IN_SCOPE) {
4866 /* If VLAs are in use, save the current stack pointer and
4867 reset the stack pointer to what it was at function entry
4868 (label will restore stack pointer in inner scopes) */
4869 vla_sp_save();
4870 gen_vla_sp_restore(vla_sp_root_loc);
4872 if (s->r & LABEL_FORWARD)
4873 s->jnext = gjmp(s->jnext);
4874 else
4875 gjmp_addr(s->jnext);
4876 next();
4877 } else {
4878 expect("label identifier");
4880 skip(';');
4881 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4882 asm_instr();
4883 } else {
4884 b = is_label();
4885 if (b) {
4886 /* label case */
4887 if (vla_flags & VLA_IN_SCOPE) {
4888 /* save/restore stack pointer across label
4889 this is a no-op when combined with the load immediately
4890 after the label unless we arrive via goto */
4891 vla_sp_save();
4893 s = label_find(b);
4894 if (s) {
4895 if (s->r == LABEL_DEFINED)
4896 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4897 gsym(s->jnext);
4898 s->r = LABEL_DEFINED;
4899 } else {
4900 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4902 s->jnext = ind;
4903 if (vla_flags & VLA_IN_SCOPE) {
4904 gen_vla_sp_restore(*vla_sp_loc);
4905 vla_flags |= VLA_NEED_NEW_FRAME;
4907 /* we accept this, but it is a mistake */
4908 block_after_label:
4909 if (tok == '}') {
4910 tcc_warning("deprecated use of label at end of compound statement");
4911 } else {
4912 if (is_expr)
4913 vpop();
4914 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4916 } else {
4917 /* expression case */
4918 if (tok != ';') {
4919 if (is_expr) {
4920 vpop();
4921 gexpr();
4922 } else {
4923 gexpr();
4924 vpop();
4927 skip(';');
4932 /* t is the array or struct type. c is the array or struct
4933 address. cur_index/cur_field is the pointer to the current
4934 value. 'size_only' is true if only size info is needed (only used
4935 in arrays) */
4936 static void decl_designator(CType *type, Section *sec, unsigned long c,
4937 int *cur_index, Sym **cur_field,
4938 int size_only)
4940 Sym *s, *f;
4941 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4942 CType type1;
4944 notfirst = 0;
4945 elem_size = 0;
4946 nb_elems = 1;
4947 if (gnu_ext && (l = is_label()) != 0)
4948 goto struct_field;
4949 while (tok == '[' || tok == '.') {
4950 if (tok == '[') {
4951 if (!(type->t & VT_ARRAY))
4952 expect("array type");
4953 s = type->ref;
4954 next();
4955 index = expr_const();
4956 if (index < 0 || (s->c >= 0 && index >= s->c))
4957 expect("invalid index");
4958 if (tok == TOK_DOTS && gnu_ext) {
4959 next();
4960 index_last = expr_const();
4961 if (index_last < 0 ||
4962 (s->c >= 0 && index_last >= s->c) ||
4963 index_last < index)
4964 expect("invalid index");
4965 } else {
4966 index_last = index;
4968 skip(']');
4969 if (!notfirst)
4970 *cur_index = index_last;
4971 type = pointed_type(type);
4972 elem_size = type_size(type, &align);
4973 c += index * elem_size;
4974 /* NOTE: we only support ranges for last designator */
4975 nb_elems = index_last - index + 1;
4976 if (nb_elems != 1) {
4977 notfirst = 1;
4978 break;
4980 } else {
4981 next();
4982 l = tok;
4983 next();
4984 struct_field:
4985 if ((type->t & VT_BTYPE) != VT_STRUCT)
4986 expect("struct/union type");
4987 s = type->ref;
4988 l |= SYM_FIELD;
4989 f = s->next;
4990 while (f) {
4991 if (f->v == l)
4992 break;
4993 f = f->next;
4995 if (!f)
4996 expect("field");
4997 if (!notfirst)
4998 *cur_field = f;
4999 /* XXX: fix this mess by using explicit storage field */
5000 type1 = f->type;
5001 type1.t |= (type->t & ~VT_TYPE);
5002 type = &type1;
5003 c += f->c;
5005 notfirst = 1;
5007 if (notfirst) {
5008 if (tok == '=') {
5009 next();
5010 } else {
5011 if (!gnu_ext)
5012 expect("=");
5014 } else {
5015 if (type->t & VT_ARRAY) {
5016 index = *cur_index;
5017 type = pointed_type(type);
5018 c += index * type_size(type, &align);
5019 } else {
5020 f = *cur_field;
5021 if (!f)
5022 tcc_error("too many field init");
5023 /* XXX: fix this mess by using explicit storage field */
5024 type1 = f->type;
5025 type1.t |= (type->t & ~VT_TYPE);
5026 type = &type1;
5027 c += f->c;
5030 decl_initializer(type, sec, c, 0, size_only);
5032 /* XXX: make it more general */
5033 if (!size_only && nb_elems > 1) {
5034 unsigned long c_end;
5035 uint8_t *src, *dst;
5036 int i;
5038 if (!sec)
5039 tcc_error("range init not supported yet for dynamic storage");
5040 c_end = c + nb_elems * elem_size;
5041 if (c_end > sec->data_allocated)
5042 section_realloc(sec, c_end);
5043 src = sec->data + c;
5044 dst = src;
5045 for(i = 1; i < nb_elems; i++) {
5046 dst += elem_size;
5047 memcpy(dst, src, elem_size);
5052 #define EXPR_VAL 0
5053 #define EXPR_CONST 1
5054 #define EXPR_ANY 2
5056 /* store a value or an expression directly in global data or in local array */
5057 static void init_putv(CType *type, Section *sec, unsigned long c,
5058 int v, int expr_type)
5060 int saved_global_expr, bt, bit_pos, bit_size;
5061 void *ptr;
5062 unsigned long long bit_mask;
5063 CType dtype;
5065 switch(expr_type) {
5066 case EXPR_VAL:
5067 vpushi(v);
5068 break;
5069 case EXPR_CONST:
5070 /* compound literals must be allocated globally in this case */
5071 saved_global_expr = global_expr;
5072 global_expr = 1;
5073 expr_const1();
5074 global_expr = saved_global_expr;
5075 /* NOTE: symbols are accepted */
5076 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5077 tcc_error("initializer element is not constant");
5078 break;
5079 case EXPR_ANY:
5080 expr_eq();
5081 break;
5084 dtype = *type;
5085 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5087 if (sec) {
5088 /* XXX: not portable */
5089 /* XXX: generate error if incorrect relocation */
5090 gen_assign_cast(&dtype);
5091 bt = type->t & VT_BTYPE;
5092 /* we'll write at most 12 bytes */
5093 if (c + 12 > sec->data_allocated) {
5094 section_realloc(sec, c + 12);
5096 ptr = sec->data + c;
5097 /* XXX: make code faster ? */
5098 if (!(type->t & VT_BITFIELD)) {
5099 bit_pos = 0;
5100 bit_size = 32;
5101 bit_mask = -1LL;
5102 } else {
5103 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5104 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5105 bit_mask = (1LL << bit_size) - 1;
5107 if ((vtop->r & VT_SYM) &&
5108 (bt == VT_BYTE ||
5109 bt == VT_SHORT ||
5110 bt == VT_DOUBLE ||
5111 bt == VT_LDOUBLE ||
5112 bt == VT_LLONG ||
5113 (bt == VT_INT && bit_size != 32)))
5114 tcc_error("initializer element is not computable at load time");
5115 switch(bt) {
5116 case VT_BOOL:
5117 vtop->c.i = (vtop->c.i != 0);
5118 case VT_BYTE:
5119 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5120 break;
5121 case VT_SHORT:
5122 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5123 break;
5124 case VT_DOUBLE:
5125 *(double *)ptr = vtop->c.d;
5126 break;
5127 case VT_LDOUBLE:
5128 *(long double *)ptr = vtop->c.ld;
5129 break;
5130 case VT_LLONG:
5131 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5132 break;
5133 case VT_PTR:
5134 if (vtop->r & VT_SYM) {
5135 greloc(sec, vtop->sym, c, R_DATA_PTR);
5137 *(addr_t *)ptr |= (vtop->c.ull & bit_mask) << bit_pos;
5138 break;
5139 default:
5140 if (vtop->r & VT_SYM) {
5141 greloc(sec, vtop->sym, c, R_DATA_PTR);
5143 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5144 break;
5146 vtop--;
5147 } else {
5148 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5149 vswap();
5150 vstore();
5151 vpop();
5155 /* put zeros for variable based init */
5156 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5158 if (sec) {
5159 /* nothing to do because globals are already set to zero */
5160 } else {
5161 vpush_global_sym(&func_old_type, TOK_memset);
5162 vseti(VT_LOCAL, c);
5163 #ifdef TCC_TARGET_ARM
5164 vpushs(size);
5165 vpushi(0);
5166 #else
5167 vpushi(0);
5168 vpushs(size);
5169 #endif
5170 gfunc_call(3);
5174 /* 't' contains the type and storage info. 'c' is the offset of the
5175 object in section 'sec'. If 'sec' is NULL, it means stack based
5176 allocation. 'first' is true if array '{' must be read (multi
5177 dimension implicit array init handling). 'size_only' is true if
5178 size only evaluation is wanted (only for arrays). */
5179 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5180 int first, int size_only)
5182 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5183 int size1, align1, expr_type;
5184 Sym *s, *f;
5185 CType *t1;
5187 if (type->t & VT_VLA) {
5188 int a;
5190 /* save current stack pointer */
5191 if (vla_flags & VLA_NEED_NEW_FRAME) {
5192 vla_sp_save();
5193 vla_flags = VLA_IN_SCOPE;
5194 vla_sp_loc = &vla_sp_loc_tmp;
5197 vla_runtime_type_size(type, &a);
5198 gen_vla_alloc(type, a);
5199 vset(type, VT_LOCAL|VT_LVAL, c);
5200 vswap();
5201 vstore();
5202 vpop();
5203 } else if (type->t & VT_ARRAY) {
5204 s = type->ref;
5205 n = s->c;
5206 array_length = 0;
5207 t1 = pointed_type(type);
5208 size1 = type_size(t1, &align1);
5210 no_oblock = 1;
5211 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5212 tok == '{') {
5213 if (tok != '{')
5214 tcc_error("character array initializer must be a literal,"
5215 " optionally enclosed in braces");
5216 skip('{');
5217 no_oblock = 0;
5220 /* only parse strings here if correct type (otherwise: handle
5221 them as ((w)char *) expressions */
5222 if ((tok == TOK_LSTR &&
5223 #ifdef TCC_TARGET_PE
5224 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5225 #else
5226 (t1->t & VT_BTYPE) == VT_INT
5227 #endif
5228 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5229 while (tok == TOK_STR || tok == TOK_LSTR) {
5230 int cstr_len, ch;
5231 CString *cstr;
5233 cstr = tokc.cstr;
5234 /* compute maximum number of chars wanted */
5235 if (tok == TOK_STR)
5236 cstr_len = cstr->size;
5237 else
5238 cstr_len = cstr->size / sizeof(nwchar_t);
5239 cstr_len--;
5240 nb = cstr_len;
5241 if (n >= 0 && nb > (n - array_length))
5242 nb = n - array_length;
5243 if (!size_only) {
5244 if (cstr_len > nb)
5245 tcc_warning("initializer-string for array is too long");
5246 /* in order to go faster for common case (char
5247 string in global variable, we handle it
5248 specifically */
5249 if (sec && tok == TOK_STR && size1 == 1) {
5250 memcpy(sec->data + c + array_length, cstr->data, nb);
5251 } else {
5252 for(i=0;i<nb;i++) {
5253 if (tok == TOK_STR)
5254 ch = ((unsigned char *)cstr->data)[i];
5255 else
5256 ch = ((nwchar_t *)cstr->data)[i];
5257 init_putv(t1, sec, c + (array_length + i) * size1,
5258 ch, EXPR_VAL);
5262 array_length += nb;
5263 next();
5265 /* only add trailing zero if enough storage (no
5266 warning in this case since it is standard) */
5267 if (n < 0 || array_length < n) {
5268 if (!size_only) {
5269 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5271 array_length++;
5273 } else {
5274 index = 0;
5275 while (tok != '}') {
5276 decl_designator(type, sec, c, &index, NULL, size_only);
5277 if (n >= 0 && index >= n)
5278 tcc_error("index too large");
5279 /* must put zero in holes (note that doing it that way
5280 ensures that it even works with designators) */
5281 if (!size_only && array_length < index) {
5282 init_putz(t1, sec, c + array_length * size1,
5283 (index - array_length) * size1);
5285 index++;
5286 if (index > array_length)
5287 array_length = index;
5288 /* special test for multi dimensional arrays (may not
5289 be strictly correct if designators are used at the
5290 same time) */
5291 if (index >= n && no_oblock)
5292 break;
5293 if (tok == '}')
5294 break;
5295 skip(',');
5298 if (!no_oblock)
5299 skip('}');
5300 /* put zeros at the end */
5301 if (!size_only && n >= 0 && array_length < n) {
5302 init_putz(t1, sec, c + array_length * size1,
5303 (n - array_length) * size1);
5305 /* patch type size if needed */
5306 if (n < 0)
5307 s->c = array_length;
5308 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5309 (sec || !first || tok == '{')) {
5310 int par_count;
5312 /* NOTE: the previous test is a specific case for automatic
5313 struct/union init */
5314 /* XXX: union needs only one init */
5316 /* XXX: this test is incorrect for local initializers
5317 beginning with ( without {. It would be much more difficult
5318 to do it correctly (ideally, the expression parser should
5319 be used in all cases) */
5320 par_count = 0;
5321 if (tok == '(') {
5322 AttributeDef ad1;
5323 CType type1;
5324 next();
5325 while (tok == '(') {
5326 par_count++;
5327 next();
5329 if (!parse_btype(&type1, &ad1))
5330 expect("cast");
5331 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5332 #if 0
5333 if (!is_assignable_types(type, &type1))
5334 tcc_error("invalid type for cast");
5335 #endif
5336 skip(')');
5338 no_oblock = 1;
5339 if (first || tok == '{') {
5340 skip('{');
5341 no_oblock = 0;
5343 s = type->ref;
5344 f = s->next;
5345 array_length = 0;
5346 index = 0;
5347 n = s->c;
5348 while (tok != '}') {
5349 decl_designator(type, sec, c, NULL, &f, size_only);
5350 index = f->c;
5351 if (!size_only && array_length < index) {
5352 init_putz(type, sec, c + array_length,
5353 index - array_length);
5355 index = index + type_size(&f->type, &align1);
5356 if (index > array_length)
5357 array_length = index;
5359 /* gr: skip fields from same union - ugly. */
5360 while (f->next) {
5361 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5362 /* test for same offset */
5363 if (f->next->c != f->c)
5364 break;
5365 /* if yes, test for bitfield shift */
5366 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5367 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5368 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5369 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5370 if (bit_pos_1 != bit_pos_2)
5371 break;
5373 f = f->next;
5376 f = f->next;
5377 if (no_oblock && f == NULL)
5378 break;
5379 if (tok == '}')
5380 break;
5381 skip(',');
5383 /* put zeros at the end */
5384 if (!size_only && array_length < n) {
5385 init_putz(type, sec, c + array_length,
5386 n - array_length);
5388 if (!no_oblock)
5389 skip('}');
5390 while (par_count) {
5391 skip(')');
5392 par_count--;
5394 } else if (tok == '{') {
5395 next();
5396 decl_initializer(type, sec, c, first, size_only);
5397 skip('}');
5398 } else if (size_only) {
5399 /* just skip expression */
5400 parlevel = parlevel1 = 0;
5401 while ((parlevel > 0 || parlevel1 > 0 ||
5402 (tok != '}' && tok != ',')) && tok != -1) {
5403 if (tok == '(')
5404 parlevel++;
5405 else if (tok == ')')
5406 parlevel--;
5407 else if (tok == '{')
5408 parlevel1++;
5409 else if (tok == '}')
5410 parlevel1--;
5411 next();
5413 } else {
5414 /* currently, we always use constant expression for globals
5415 (may change for scripting case) */
5416 expr_type = EXPR_CONST;
5417 if (!sec)
5418 expr_type = EXPR_ANY;
5419 init_putv(type, sec, c, 0, expr_type);
5423 /* parse an initializer for type 't' if 'has_init' is non zero, and
5424 allocate space in local or global data space ('r' is either
5425 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5426 variable 'v' with an associated name represented by 'asm_label' of
5427 scope 'scope' is declared before initializers are parsed. If 'v' is
5428 zero, then a reference to the new object is put in the value stack.
5429 If 'has_init' is 2, a special parsing is done to handle string
5430 constants. */
5431 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5432 int has_init, int v, char *asm_label,
5433 int scope)
5435 int size, align, addr, data_offset;
5436 int level;
5437 ParseState saved_parse_state = {0};
5438 TokenString init_str;
5439 Section *sec;
5440 Sym *flexible_array;
5442 flexible_array = NULL;
5443 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5444 Sym *field = type->ref->next;
5445 if (field) {
5446 while (field->next)
5447 field = field->next;
5448 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5449 flexible_array = field;
5453 size = type_size(type, &align);
5454 /* If unknown size, we must evaluate it before
5455 evaluating initializers because
5456 initializers can generate global data too
5457 (e.g. string pointers or ISOC99 compound
5458 literals). It also simplifies local
5459 initializers handling */
5460 tok_str_new(&init_str);
5461 if (size < 0 || (flexible_array && has_init)) {
5462 if (!has_init)
5463 tcc_error("unknown type size");
5464 /* get all init string */
5465 if (has_init == 2) {
5466 /* only get strings */
5467 while (tok == TOK_STR || tok == TOK_LSTR) {
5468 tok_str_add_tok(&init_str);
5469 next();
5471 } else {
5472 level = 0;
5473 while (level > 0 || (tok != ',' && tok != ';')) {
5474 if (tok < 0)
5475 tcc_error("unexpected end of file in initializer");
5476 tok_str_add_tok(&init_str);
5477 if (tok == '{')
5478 level++;
5479 else if (tok == '}') {
5480 level--;
5481 if (level <= 0) {
5482 next();
5483 break;
5486 next();
5489 tok_str_add(&init_str, -1);
5490 tok_str_add(&init_str, 0);
5492 /* compute size */
5493 save_parse_state(&saved_parse_state);
5495 macro_ptr = init_str.str;
5496 next();
5497 decl_initializer(type, NULL, 0, 1, 1);
5498 /* prepare second initializer parsing */
5499 macro_ptr = init_str.str;
5500 next();
5502 /* if still unknown size, error */
5503 size = type_size(type, &align);
5504 if (size < 0)
5505 tcc_error("unknown type size");
5507 if (flexible_array)
5508 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5509 /* take into account specified alignment if bigger */
5510 if (ad->a.aligned) {
5511 if (ad->a.aligned > align)
5512 align = ad->a.aligned;
5513 } else if (ad->a.packed) {
5514 align = 1;
5516 if ((r & VT_VALMASK) == VT_LOCAL) {
5517 sec = NULL;
5518 #ifdef CONFIG_TCC_BCHECK
5519 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5520 loc--;
5522 #endif
5523 loc = (loc - size) & -align;
5524 addr = loc;
5525 #ifdef CONFIG_TCC_BCHECK
5526 /* handles bounds */
5527 /* XXX: currently, since we do only one pass, we cannot track
5528 '&' operators, so we add only arrays */
5529 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5530 unsigned long *bounds_ptr;
5531 /* add padding between regions */
5532 loc--;
5533 /* then add local bound info */
5534 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5535 bounds_ptr[0] = addr;
5536 bounds_ptr[1] = size;
5538 #endif
5539 if (v) {
5540 /* local variable */
5541 sym_push(v, type, r, addr);
5542 } else {
5543 /* push local reference */
5544 vset(type, r, addr);
5546 } else {
5547 Sym *sym;
5549 sym = NULL;
5550 if (v && scope == VT_CONST) {
5551 /* see if the symbol was already defined */
5552 sym = sym_find(v);
5553 if (sym) {
5554 if (!is_compatible_types(&sym->type, type))
5555 tcc_error("incompatible types for redefinition of '%s'",
5556 get_tok_str(v, NULL));
5557 if (sym->type.t & VT_EXTERN) {
5558 /* if the variable is extern, it was not allocated */
5559 sym->type.t &= ~VT_EXTERN;
5560 /* set array size if it was ommited in extern
5561 declaration */
5562 if ((sym->type.t & VT_ARRAY) &&
5563 sym->type.ref->c < 0 &&
5564 type->ref->c >= 0)
5565 sym->type.ref->c = type->ref->c;
5566 } else {
5567 /* we accept several definitions of the same
5568 global variable. this is tricky, because we
5569 must play with the SHN_COMMON type of the symbol */
5570 /* XXX: should check if the variable was already
5571 initialized. It is incorrect to initialized it
5572 twice */
5573 /* no init data, we won't add more to the symbol */
5574 if (!has_init)
5575 goto no_alloc;
5580 /* allocate symbol in corresponding section */
5581 sec = ad->section;
5582 if (!sec) {
5583 if (has_init)
5584 sec = data_section;
5585 else if (tcc_state->nocommon)
5586 sec = bss_section;
5588 if (sec) {
5589 data_offset = sec->data_offset;
5590 data_offset = (data_offset + align - 1) & -align;
5591 addr = data_offset;
5592 /* very important to increment global pointer at this time
5593 because initializers themselves can create new initializers */
5594 data_offset += size;
5595 #ifdef CONFIG_TCC_BCHECK
5596 /* add padding if bound check */
5597 if (tcc_state->do_bounds_check)
5598 data_offset++;
5599 #endif
5600 sec->data_offset = data_offset;
5601 /* allocate section space to put the data */
5602 if (sec->sh_type != SHT_NOBITS &&
5603 data_offset > sec->data_allocated)
5604 section_realloc(sec, data_offset);
5605 /* align section if needed */
5606 if (align > sec->sh_addralign)
5607 sec->sh_addralign = align;
5608 } else {
5609 addr = 0; /* avoid warning */
5612 if (v) {
5613 if (scope != VT_CONST || !sym) {
5614 sym = sym_push(v, type, r | VT_SYM, 0);
5615 sym->asm_label = asm_label;
5617 /* update symbol definition */
5618 if (sec) {
5619 put_extern_sym(sym, sec, addr, size);
5620 } else {
5621 ElfW(Sym) *esym;
5622 /* put a common area */
5623 put_extern_sym(sym, NULL, align, size);
5624 /* XXX: find a nicer way */
5625 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5626 esym->st_shndx = SHN_COMMON;
5628 } else {
5629 /* push global reference */
5630 sym = get_sym_ref(type, sec, addr, size);
5631 vpushsym(type, sym);
5633 /* patch symbol weakness */
5634 if (type->t & VT_WEAK)
5635 weaken_symbol(sym);
5636 #ifdef CONFIG_TCC_BCHECK
5637 /* handles bounds now because the symbol must be defined
5638 before for the relocation */
5639 if (tcc_state->do_bounds_check) {
5640 unsigned long *bounds_ptr;
5642 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5643 /* then add global bound info */
5644 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5645 bounds_ptr[0] = 0; /* relocated */
5646 bounds_ptr[1] = size;
5648 #endif
5650 if (has_init || (type->t & VT_VLA)) {
5651 decl_initializer(type, sec, addr, 1, 0);
5652 /* restore parse state if needed */
5653 if (init_str.str) {
5654 tok_str_free(init_str.str);
5655 restore_parse_state(&saved_parse_state);
5657 /* patch flexible array member size back to -1, */
5658 /* for possible subsequent similar declarations */
5659 if (flexible_array)
5660 flexible_array->type.ref->c = -1;
5662 no_alloc: ;
5665 static void put_func_debug(Sym *sym)
5667 char buf[512];
5669 /* stabs info */
5670 /* XXX: we put here a dummy type */
5671 snprintf(buf, sizeof(buf), "%s:%c1",
5672 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5673 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5674 cur_text_section, sym->c);
5675 /* //gr gdb wants a line at the function */
5676 put_stabn(N_SLINE, 0, file->line_num, 0);
5677 last_ind = 0;
5678 last_line_num = 0;
5681 /* parse an old style function declaration list */
5682 /* XXX: check multiple parameter */
5683 static void func_decl_list(Sym *func_sym)
5685 AttributeDef ad;
5686 int v;
5687 Sym *s;
5688 CType btype, type;
5690 /* parse each declaration */
5691 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5692 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5693 if (!parse_btype(&btype, &ad))
5694 expect("declaration list");
5695 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5696 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5697 tok == ';') {
5698 /* we accept no variable after */
5699 } else {
5700 for(;;) {
5701 type = btype;
5702 type_decl(&type, &ad, &v, TYPE_DIRECT);
5703 /* find parameter in function parameter list */
5704 s = func_sym->next;
5705 while (s != NULL) {
5706 if ((s->v & ~SYM_FIELD) == v)
5707 goto found;
5708 s = s->next;
5710 tcc_error("declaration for parameter '%s' but no such parameter",
5711 get_tok_str(v, NULL));
5712 found:
5713 /* check that no storage specifier except 'register' was given */
5714 if (type.t & VT_STORAGE)
5715 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5716 convert_parameter_type(&type);
5717 /* we can add the type (NOTE: it could be local to the function) */
5718 s->type = type;
5719 /* accept other parameters */
5720 if (tok == ',')
5721 next();
5722 else
5723 break;
5726 skip(';');
5730 /* parse a function defined by symbol 'sym' and generate its code in
5731 'cur_text_section' */
5732 static void gen_function(Sym *sym)
5734 int saved_nocode_wanted = nocode_wanted;
5735 nocode_wanted = 0;
5736 ind = cur_text_section->data_offset;
5737 /* NOTE: we patch the symbol size later */
5738 put_extern_sym(sym, cur_text_section, ind, 0);
5739 funcname = get_tok_str(sym->v, NULL);
5740 func_ind = ind;
5741 /* Initialize VLA state */
5742 vla_sp_loc = &vla_sp_root_loc;
5743 vla_flags = VLA_NEED_NEW_FRAME;
5744 /* put debug symbol */
5745 if (tcc_state->do_debug)
5746 put_func_debug(sym);
5747 /* push a dummy symbol to enable local sym storage */
5748 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5749 gfunc_prolog(&sym->type);
5750 rsym = 0;
5751 block(NULL, NULL, NULL, NULL, 0, 0);
5752 gsym(rsym);
5753 gfunc_epilog();
5754 cur_text_section->data_offset = ind;
5755 label_pop(&global_label_stack, NULL);
5756 /* reset local stack */
5757 scope_stack_bottom = NULL;
5758 sym_pop(&local_stack, NULL);
5759 /* end of function */
5760 /* patch symbol size */
5761 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5762 ind - func_ind;
5763 /* patch symbol weakness (this definition overrules any prototype) */
5764 if (sym->type.t & VT_WEAK)
5765 weaken_symbol(sym);
5766 if (tcc_state->do_debug) {
5767 put_stabn(N_FUN, 0, 0, ind - func_ind);
5769 /* It's better to crash than to generate wrong code */
5770 cur_text_section = NULL;
5771 funcname = ""; /* for safety */
5772 func_vt.t = VT_VOID; /* for safety */
5773 func_var = 0; /* for safety */
5774 ind = 0; /* for safety */
5775 nocode_wanted = saved_nocode_wanted;
5778 ST_FUNC void gen_inline_functions(void)
5780 Sym *sym;
5781 int *str, inline_generated, i;
5782 struct InlineFunc *fn;
5784 /* iterate while inline function are referenced */
5785 for(;;) {
5786 inline_generated = 0;
5787 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5788 fn = tcc_state->inline_fns[i];
5789 sym = fn->sym;
5790 if (sym && sym->c) {
5791 /* the function was used: generate its code and
5792 convert it to a normal function */
5793 str = fn->token_str;
5794 fn->sym = NULL;
5795 if (file)
5796 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5797 sym->r = VT_SYM | VT_CONST;
5798 sym->type.t &= ~VT_INLINE;
5800 macro_ptr = str;
5801 next();
5802 cur_text_section = text_section;
5803 gen_function(sym);
5804 macro_ptr = NULL; /* fail safe */
5806 inline_generated = 1;
5809 if (!inline_generated)
5810 break;
5812 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5813 fn = tcc_state->inline_fns[i];
5814 str = fn->token_str;
5815 tok_str_free(str);
5817 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5820 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5821 static int decl0(int l, int is_for_loop_init)
5823 int v, has_init, r;
5824 CType type, btype;
5825 Sym *sym;
5826 AttributeDef ad;
5828 while (1) {
5829 if (!parse_btype(&btype, &ad)) {
5830 if (is_for_loop_init)
5831 return 0;
5832 /* skip redundant ';' */
5833 /* XXX: find more elegant solution */
5834 if (tok == ';') {
5835 next();
5836 continue;
5838 if (l == VT_CONST &&
5839 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5840 /* global asm block */
5841 asm_global_instr();
5842 continue;
5844 /* special test for old K&R protos without explicit int
5845 type. Only accepted when defining global data */
5846 if (l == VT_LOCAL || tok < TOK_DEFINE)
5847 break;
5848 btype.t = VT_INT;
5850 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5851 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5852 tok == ';') {
5853 /* we accept no variable after */
5854 next();
5855 continue;
5857 while (1) { /* iterate thru each declaration */
5858 char *asm_label; // associated asm label
5859 type = btype;
5860 type_decl(&type, &ad, &v, TYPE_DIRECT);
5861 #if 0
5863 char buf[500];
5864 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5865 printf("type = '%s'\n", buf);
5867 #endif
5868 if ((type.t & VT_BTYPE) == VT_FUNC) {
5869 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5870 tcc_error("function without file scope cannot be static");
5872 /* if old style function prototype, we accept a
5873 declaration list */
5874 sym = type.ref;
5875 if (sym->c == FUNC_OLD)
5876 func_decl_list(sym);
5879 asm_label = NULL;
5880 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5881 CString astr;
5883 asm_label_instr(&astr);
5884 asm_label = tcc_strdup(astr.data);
5885 cstr_free(&astr);
5887 /* parse one last attribute list, after asm label */
5888 parse_attribute(&ad);
5891 if (ad.a.weak)
5892 type.t |= VT_WEAK;
5893 #ifdef TCC_TARGET_PE
5894 if (ad.a.func_import)
5895 type.t |= VT_IMPORT;
5896 if (ad.a.func_export)
5897 type.t |= VT_EXPORT;
5898 #endif
5899 if (tok == '{') {
5900 if (l == VT_LOCAL)
5901 tcc_error("cannot use local functions");
5902 if ((type.t & VT_BTYPE) != VT_FUNC)
5903 expect("function definition");
5905 /* reject abstract declarators in function definition */
5906 sym = type.ref;
5907 while ((sym = sym->next) != NULL)
5908 if (!(sym->v & ~SYM_FIELD))
5909 expect("identifier");
5911 /* XXX: cannot do better now: convert extern line to static inline */
5912 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5913 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5915 sym = sym_find(v);
5916 if (sym) {
5917 Sym *ref;
5918 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5919 goto func_error1;
5921 ref = sym->type.ref;
5922 if (0 == ref->a.func_proto)
5923 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5925 /* use func_call from prototype if not defined */
5926 if (ref->a.func_call != FUNC_CDECL
5927 && type.ref->a.func_call == FUNC_CDECL)
5928 type.ref->a.func_call = ref->a.func_call;
5930 /* use export from prototype */
5931 if (ref->a.func_export)
5932 type.ref->a.func_export = 1;
5934 /* use static from prototype */
5935 if (sym->type.t & VT_STATIC)
5936 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5938 if (!is_compatible_types(&sym->type, &type)) {
5939 func_error1:
5940 tcc_error("incompatible types for redefinition of '%s'",
5941 get_tok_str(v, NULL));
5943 type.ref->a.func_proto = 0;
5944 /* if symbol is already defined, then put complete type */
5945 sym->type = type;
5946 } else {
5947 /* put function symbol */
5948 sym = global_identifier_push(v, type.t, 0);
5949 sym->type.ref = type.ref;
5952 /* static inline functions are just recorded as a kind
5953 of macro. Their code will be emitted at the end of
5954 the compilation unit only if they are used */
5955 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5956 (VT_INLINE | VT_STATIC)) {
5957 TokenString func_str;
5958 int block_level;
5959 struct InlineFunc *fn;
5960 const char *filename;
5962 tok_str_new(&func_str);
5964 block_level = 0;
5965 for(;;) {
5966 int t;
5967 if (tok == TOK_EOF)
5968 tcc_error("unexpected end of file");
5969 tok_str_add_tok(&func_str);
5970 t = tok;
5971 next();
5972 if (t == '{') {
5973 block_level++;
5974 } else if (t == '}') {
5975 block_level--;
5976 if (block_level == 0)
5977 break;
5980 tok_str_add(&func_str, -1);
5981 tok_str_add(&func_str, 0);
5982 filename = file ? file->filename : "";
5983 fn = tcc_malloc(sizeof *fn + strlen(filename));
5984 strcpy(fn->filename, filename);
5985 fn->sym = sym;
5986 fn->token_str = func_str.str;
5987 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5989 } else {
5990 /* compute text section */
5991 cur_text_section = ad.section;
5992 if (!cur_text_section)
5993 cur_text_section = text_section;
5994 sym->r = VT_SYM | VT_CONST;
5995 gen_function(sym);
5997 break;
5998 } else {
5999 if (btype.t & VT_TYPEDEF) {
6000 /* save typedefed type */
6001 /* XXX: test storage specifiers ? */
6002 sym = sym_push(v, &type, 0, 0);
6003 sym->a = ad.a;
6004 sym->type.t |= VT_TYPEDEF;
6005 } else {
6006 r = 0;
6007 if ((type.t & VT_BTYPE) == VT_FUNC) {
6008 /* external function definition */
6009 /* specific case for func_call attribute */
6010 ad.a.func_proto = 1;
6011 type.ref->a = ad.a;
6012 } else if (!(type.t & VT_ARRAY)) {
6013 /* not lvalue if array */
6014 r |= lvalue_type(type.t);
6016 has_init = (tok == '=');
6017 if (has_init && (type.t & VT_VLA))
6018 tcc_error("Variable length array cannot be initialized");
6019 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6020 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6021 !has_init && l == VT_CONST && type.ref->c < 0)) {
6022 /* external variable or function */
6023 /* NOTE: as GCC, uninitialized global static
6024 arrays of null size are considered as
6025 extern */
6026 sym = external_sym(v, &type, r, asm_label);
6028 if (type.t & VT_WEAK)
6029 weaken_symbol(sym);
6031 if (ad.alias_target) {
6032 Section tsec;
6033 Elf32_Sym *esym;
6034 Sym *alias_target;
6036 alias_target = sym_find(ad.alias_target);
6037 if (!alias_target || !alias_target->c)
6038 tcc_error("unsupported forward __alias__ attribute");
6039 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6040 tsec.sh_num = esym->st_shndx;
6041 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6043 } else {
6044 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6045 if (type.t & VT_STATIC)
6046 r |= VT_CONST;
6047 else
6048 r |= l;
6049 if (has_init)
6050 next();
6051 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6054 if (tok != ',') {
6055 if (is_for_loop_init)
6056 return 1;
6057 skip(';');
6058 break;
6060 next();
6062 ad.a.aligned = 0;
6065 return 0;
6068 ST_FUNC void decl(int l)
6070 decl0(l, 0);