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