Make parse_btype only accept one basic type
[tinycc.git] / tccgen.c
blob9c12c92a5d764a95c6a482939afc71eceecbf1b0
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
31 ST_DATA int rsym, anon_sym, ind, loc;
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *scope_stack_bottom;
54 ST_DATA Sym *define_stack;
55 ST_DATA Sym *global_label_stack;
56 ST_DATA Sym *local_label_stack;
58 ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA int vla_flags; /* VLA_* flags */
63 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
65 ST_DATA int const_wanted; /* true if constant wanted */
66 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
67 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
69 ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
70 ST_DATA int func_vc;
71 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
72 ST_DATA char *funcname;
74 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType *type);
78 static inline CType *pointed_type(CType *type);
79 static int is_compatible_types(CType *type1, CType *type2);
80 static int parse_btype(CType *type, AttributeDef *ad);
81 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
82 static void parse_expr_type(CType *type);
83 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
84 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
85 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
86 static int decl0(int l, int is_for_loop_init);
87 static void expr_eq(void);
88 static void unary_type(CType *type);
89 static void vla_runtime_type_size(CType *type, int *a);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType *type1, CType *type2);
92 static void expr_type(CType *type);
94 ST_INLN int is_float(int t)
96 int bt;
97 bt = t & VT_BTYPE;
98 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC int ieee_finite(double d)
106 int p[4];
107 memcpy(p, &d, sizeof(double));
108 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
111 ST_FUNC void test_lvalue(void)
113 if (!(vtop->r & VT_LVAL))
114 expect("lvalue");
117 /* ------------------------------------------------------------------------- */
118 /* symbol allocator */
119 static Sym *__sym_malloc(void)
121 Sym *sym_pool, *sym, *last_sym;
122 int i;
124 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
125 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
127 last_sym = sym_free_first;
128 sym = sym_pool;
129 for(i = 0; i < SYM_POOL_NB; i++) {
130 sym->next = last_sym;
131 last_sym = sym;
132 sym++;
134 sym_free_first = last_sym;
135 return last_sym;
138 static inline Sym *sym_malloc(void)
140 Sym *sym;
141 sym = sym_free_first;
142 if (!sym)
143 sym = __sym_malloc();
144 sym_free_first = sym->next;
145 return sym;
148 ST_INLN void sym_free(Sym *sym)
150 sym->next = sym_free_first;
151 tcc_free(sym->asm_label);
152 sym_free_first = sym;
155 /* push, without hashing */
156 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
158 Sym *s;
159 if (ps == &local_stack) {
160 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
161 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
162 tcc_error("incompatible types for redefinition of '%s'",
163 get_tok_str(v, NULL));
165 s = 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 (vstack)");
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 /* push a symbol value of TYPE */
372 static inline void vpushsym(CType *type, Sym *sym)
374 CValue cval;
376 cval.ull = 0;
377 vsetc(type, VT_CONST | VT_SYM, &cval);
378 vtop->sym = sym;
381 /* Return a static symbol pointing to a section */
382 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
384 int v;
385 Sym *sym;
387 v = anon_sym++;
388 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
389 sym->type.ref = type->ref;
390 sym->r = VT_CONST | VT_SYM;
391 put_extern_sym(sym, sec, offset, size);
392 return sym;
395 /* push a reference to a section offset by adding a dummy symbol */
396 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
398 vpushsym(type, get_sym_ref(type, sec, offset, size));
401 /* define a new external reference to a symbol 'v' of type 'u' */
402 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
404 Sym *s;
406 s = sym_find(v);
407 if (!s) {
408 /* push forward reference */
409 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
410 s->type.ref = type->ref;
411 s->r = r | VT_CONST | VT_SYM;
413 return s;
416 /* define a new external reference to a symbol 'v' with alternate asm
417 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
418 is no alternate name (most cases) */
419 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
421 Sym *s;
423 s = sym_find(v);
424 if (!s) {
425 /* push forward reference */
426 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
427 s->asm_label = asm_label;
428 s->type.t |= VT_EXTERN;
429 } else if (s->type.ref == func_old_type.ref) {
430 s->type.ref = type->ref;
431 s->r = r | VT_CONST | VT_SYM;
432 s->type.t |= VT_EXTERN;
433 } else if (!is_compatible_types(&s->type, type)) {
434 tcc_error("incompatible types for redefinition of '%s'",
435 get_tok_str(v, NULL));
437 return s;
440 /* push a reference to global symbol v */
441 ST_FUNC void vpush_global_sym(CType *type, int v)
443 vpushsym(type, external_global_sym(v, type, 0));
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 (vstack)");
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.ull = 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_DEFSIGN | 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_DEFSIGN | 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_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1624 tmp_type2.t &= ~(VT_DEFSIGN | 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 #ifdef TCC_TARGET_X86_64
1952 else if (dbt == VT_PTR)
1954 #endif
1955 else if (dbt != VT_LLONG) {
1956 int s = 0;
1957 if ((dbt & VT_BTYPE) == VT_BYTE)
1958 s = 24;
1959 else if ((dbt & VT_BTYPE) == VT_SHORT)
1960 s = 16;
1961 if(dbt & VT_UNSIGNED)
1962 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1963 else
1964 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1967 } else if (p && dbt == VT_BOOL) {
1968 vtop->r = VT_CONST;
1969 vtop->c.i = 1;
1970 } else if (!nocode_wanted) {
1971 /* non constant case: generate code */
1972 if (sf && df) {
1973 /* convert from fp to fp */
1974 gen_cvt_ftof(dbt);
1975 } else if (df) {
1976 /* convert int to fp */
1977 gen_cvt_itof1(dbt);
1978 } else if (sf) {
1979 /* convert fp to int */
1980 if (dbt == VT_BOOL) {
1981 vpushi(0);
1982 gen_op(TOK_NE);
1983 } else {
1984 /* we handle char/short/etc... with generic code */
1985 if (dbt != (VT_INT | VT_UNSIGNED) &&
1986 dbt != (VT_LLONG | VT_UNSIGNED) &&
1987 dbt != VT_LLONG)
1988 dbt = VT_INT;
1989 gen_cvt_ftoi1(dbt);
1990 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1991 /* additional cast for char/short... */
1992 vtop->type.t = dbt;
1993 gen_cast(type);
1996 #ifndef TCC_TARGET_X86_64
1997 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1998 if ((sbt & VT_BTYPE) != VT_LLONG) {
1999 /* scalar to long long */
2000 /* machine independent conversion */
2001 gv(RC_INT);
2002 /* generate high word */
2003 if (sbt == (VT_INT | VT_UNSIGNED)) {
2004 vpushi(0);
2005 gv(RC_INT);
2006 } else {
2007 if (sbt == VT_PTR) {
2008 /* cast from pointer to int before we apply
2009 shift operation, which pointers don't support*/
2010 gen_cast(&int_type);
2012 gv_dup();
2013 vpushi(31);
2014 gen_op(TOK_SAR);
2016 /* patch second register */
2017 vtop[-1].r2 = vtop->r;
2018 vpop();
2020 #else
2021 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2022 (dbt & VT_BTYPE) == VT_PTR ||
2023 (dbt & VT_BTYPE) == VT_FUNC) {
2024 if ((sbt & VT_BTYPE) != VT_LLONG &&
2025 (sbt & VT_BTYPE) != VT_PTR &&
2026 (sbt & VT_BTYPE) != VT_FUNC) {
2027 /* need to convert from 32bit to 64bit */
2028 int r = gv(RC_INT);
2029 if (sbt != (VT_INT | VT_UNSIGNED)) {
2030 /* x86_64 specific: movslq */
2031 o(0x6348);
2032 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2035 #endif
2036 } else if (dbt == VT_BOOL) {
2037 /* scalar to bool */
2038 vpushi(0);
2039 gen_op(TOK_NE);
2040 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2041 (dbt & VT_BTYPE) == VT_SHORT) {
2042 if (sbt == VT_PTR) {
2043 vtop->type.t = VT_INT;
2044 tcc_warning("nonportable conversion from pointer to char/short");
2046 force_charshort_cast(dbt);
2047 } else if ((dbt & VT_BTYPE) == VT_INT) {
2048 /* scalar to int */
2049 if (sbt == VT_LLONG) {
2050 /* from long long: just take low order word */
2051 lexpand();
2052 vpop();
2054 /* if lvalue and single word type, nothing to do because
2055 the lvalue already contains the real type size (see
2056 VT_LVAL_xxx constants) */
2059 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2060 /* if we are casting between pointer types,
2061 we must update the VT_LVAL_xxx size */
2062 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2063 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2065 vtop->type = *type;
2068 /* return type size as known at compile time. Put alignment at 'a' */
2069 ST_FUNC int type_size(CType *type, int *a)
2071 Sym *s;
2072 int bt;
2074 bt = type->t & VT_BTYPE;
2075 if (bt == VT_STRUCT) {
2076 /* struct/union */
2077 s = type->ref;
2078 *a = s->r;
2079 return s->c;
2080 } else if (bt == VT_PTR) {
2081 if (type->t & VT_ARRAY) {
2082 int ts;
2084 s = type->ref;
2085 ts = type_size(&s->type, a);
2087 if (ts < 0 && s->c < 0)
2088 ts = -ts;
2090 return ts * s->c;
2091 } else {
2092 *a = PTR_SIZE;
2093 return PTR_SIZE;
2095 } else if (bt == VT_LDOUBLE) {
2096 *a = LDOUBLE_ALIGN;
2097 return LDOUBLE_SIZE;
2098 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2099 #ifdef TCC_TARGET_I386
2100 #ifdef TCC_TARGET_PE
2101 *a = 8;
2102 #else
2103 *a = 4;
2104 #endif
2105 #elif defined(TCC_TARGET_ARM)
2106 #ifdef TCC_ARM_EABI
2107 *a = 8;
2108 #else
2109 *a = 4;
2110 #endif
2111 #else
2112 *a = 8;
2113 #endif
2114 return 8;
2115 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2116 *a = 4;
2117 return 4;
2118 } else if (bt == VT_SHORT) {
2119 *a = 2;
2120 return 2;
2121 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2122 *a = 8;
2123 return 16;
2124 } else {
2125 /* char, void, function, _Bool */
2126 *a = 1;
2127 return 1;
2131 /* push type size as known at runtime time on top of value stack. Put
2132 alignment at 'a' */
2133 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2135 if (type->t & VT_VLA) {
2136 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2137 } else {
2138 vpushi(type_size(type, a));
2142 static void vla_sp_save(void) {
2143 if (!(vla_flags & VLA_SP_LOC_SET)) {
2144 *vla_sp_loc = (loc -= PTR_SIZE);
2145 vla_flags |= VLA_SP_LOC_SET;
2147 if (!(vla_flags & VLA_SP_SAVED)) {
2148 gen_vla_sp_save(*vla_sp_loc);
2149 vla_flags |= VLA_SP_SAVED;
2153 /* return the pointed type of t */
2154 static inline CType *pointed_type(CType *type)
2156 return &type->ref->type;
2159 /* modify type so that its it is a pointer to type. */
2160 ST_FUNC void mk_pointer(CType *type)
2162 Sym *s;
2163 s = sym_push(SYM_FIELD, type, 0, -1);
2164 type->t = VT_PTR | (type->t & ~VT_TYPE);
2165 type->ref = s;
2168 /* compare function types. OLD functions match any new functions */
2169 static int is_compatible_func(CType *type1, CType *type2)
2171 Sym *s1, *s2;
2173 s1 = type1->ref;
2174 s2 = type2->ref;
2175 if (!is_compatible_types(&s1->type, &s2->type))
2176 return 0;
2177 /* check func_call */
2178 if (s1->a.func_call != s2->a.func_call)
2179 return 0;
2180 /* XXX: not complete */
2181 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2182 return 1;
2183 if (s1->c != s2->c)
2184 return 0;
2185 while (s1 != NULL) {
2186 if (s2 == NULL)
2187 return 0;
2188 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2189 return 0;
2190 s1 = s1->next;
2191 s2 = s2->next;
2193 if (s2)
2194 return 0;
2195 return 1;
2198 /* return true if type1 and type2 are the same. If unqualified is
2199 true, qualifiers on the types are ignored.
2201 - enums are not checked as gcc __builtin_types_compatible_p ()
2203 static int compare_types(CType *type1, CType *type2, int unqualified)
2205 int bt1, t1, t2;
2207 t1 = type1->t & VT_TYPE;
2208 t2 = type2->t & VT_TYPE;
2209 if (unqualified) {
2210 /* strip qualifiers before comparing */
2211 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2212 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2214 /* Default Vs explicit signedness only matters for char */
2215 if ((t1 & VT_BTYPE) != VT_BYTE) {
2216 t1 &= ~VT_DEFSIGN;
2217 t2 &= ~VT_DEFSIGN;
2219 /* XXX: bitfields ? */
2220 if (t1 != t2)
2221 return 0;
2222 /* test more complicated cases */
2223 bt1 = t1 & VT_BTYPE;
2224 if (bt1 == VT_PTR) {
2225 type1 = pointed_type(type1);
2226 type2 = pointed_type(type2);
2227 return is_compatible_types(type1, type2);
2228 } else if (bt1 == VT_STRUCT) {
2229 return (type1->ref == type2->ref);
2230 } else if (bt1 == VT_FUNC) {
2231 return is_compatible_func(type1, type2);
2232 } else {
2233 return 1;
2237 /* return true if type1 and type2 are exactly the same (including
2238 qualifiers).
2240 static int is_compatible_types(CType *type1, CType *type2)
2242 return compare_types(type1,type2,0);
2245 /* return true if type1 and type2 are the same (ignoring qualifiers).
2247 static int is_compatible_parameter_types(CType *type1, CType *type2)
2249 return compare_types(type1,type2,1);
2252 /* print a type. If 'varstr' is not NULL, then the variable is also
2253 printed in the type */
2254 /* XXX: union */
2255 /* XXX: add array and function pointers */
2256 static void type_to_str(char *buf, int buf_size,
2257 CType *type, const char *varstr)
2259 int bt, v, t;
2260 Sym *s, *sa;
2261 char buf1[256];
2262 const char *tstr;
2264 t = type->t & VT_TYPE;
2265 bt = t & VT_BTYPE;
2266 buf[0] = '\0';
2267 if (t & VT_CONSTANT)
2268 pstrcat(buf, buf_size, "const ");
2269 if (t & VT_VOLATILE)
2270 pstrcat(buf, buf_size, "volatile ");
2271 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2272 pstrcat(buf, buf_size, "unsigned ");
2273 else if (t & VT_DEFSIGN)
2274 pstrcat(buf, buf_size, "signed ");
2275 switch(bt) {
2276 case VT_VOID:
2277 tstr = "void";
2278 goto add_tstr;
2279 case VT_BOOL:
2280 tstr = "_Bool";
2281 goto add_tstr;
2282 case VT_BYTE:
2283 tstr = "char";
2284 goto add_tstr;
2285 case VT_SHORT:
2286 tstr = "short";
2287 goto add_tstr;
2288 case VT_INT:
2289 tstr = "int";
2290 goto add_tstr;
2291 case VT_LONG:
2292 tstr = "long";
2293 goto add_tstr;
2294 case VT_LLONG:
2295 tstr = "long long";
2296 goto add_tstr;
2297 case VT_FLOAT:
2298 tstr = "float";
2299 goto add_tstr;
2300 case VT_DOUBLE:
2301 tstr = "double";
2302 goto add_tstr;
2303 case VT_LDOUBLE:
2304 tstr = "long double";
2305 add_tstr:
2306 pstrcat(buf, buf_size, tstr);
2307 break;
2308 case VT_ENUM:
2309 case VT_STRUCT:
2310 if (bt == VT_STRUCT)
2311 tstr = "struct ";
2312 else
2313 tstr = "enum ";
2314 pstrcat(buf, buf_size, tstr);
2315 v = type->ref->v & ~SYM_STRUCT;
2316 if (v >= SYM_FIRST_ANOM)
2317 pstrcat(buf, buf_size, "<anonymous>");
2318 else
2319 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2320 break;
2321 case VT_FUNC:
2322 s = type->ref;
2323 type_to_str(buf, buf_size, &s->type, varstr);
2324 pstrcat(buf, buf_size, "(");
2325 sa = s->next;
2326 while (sa != NULL) {
2327 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2328 pstrcat(buf, buf_size, buf1);
2329 sa = sa->next;
2330 if (sa)
2331 pstrcat(buf, buf_size, ", ");
2333 pstrcat(buf, buf_size, ")");
2334 goto no_var;
2335 case VT_PTR:
2336 s = type->ref;
2337 pstrcpy(buf1, sizeof(buf1), "*");
2338 if (varstr)
2339 pstrcat(buf1, sizeof(buf1), varstr);
2340 type_to_str(buf, buf_size, &s->type, buf1);
2341 goto no_var;
2343 if (varstr) {
2344 pstrcat(buf, buf_size, " ");
2345 pstrcat(buf, buf_size, varstr);
2347 no_var: ;
2350 /* verify type compatibility to store vtop in 'dt' type, and generate
2351 casts if needed. */
2352 static void gen_assign_cast(CType *dt)
2354 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2355 char buf1[256], buf2[256];
2356 int dbt, sbt;
2358 st = &vtop->type; /* source type */
2359 dbt = dt->t & VT_BTYPE;
2360 sbt = st->t & VT_BTYPE;
2361 if (sbt == VT_VOID || dbt == VT_VOID)
2362 tcc_error("cannot cast from/to void");
2363 if (dt->t & VT_CONSTANT)
2364 tcc_warning("assignment of read-only location");
2365 switch(dbt) {
2366 case VT_PTR:
2367 /* special cases for pointers */
2368 /* '0' can also be a pointer */
2369 if (is_null_pointer(vtop))
2370 goto type_ok;
2371 /* accept implicit pointer to integer cast with warning */
2372 if (is_integer_btype(sbt)) {
2373 tcc_warning("assignment makes pointer from integer without a cast");
2374 goto type_ok;
2376 type1 = pointed_type(dt);
2377 /* a function is implicitely a function pointer */
2378 if (sbt == VT_FUNC) {
2379 if ((type1->t & VT_BTYPE) != VT_VOID &&
2380 !is_compatible_types(pointed_type(dt), st))
2381 tcc_warning("assignment from incompatible pointer type");
2382 goto type_ok;
2384 if (sbt != VT_PTR)
2385 goto error;
2386 type2 = pointed_type(st);
2387 if ((type1->t & VT_BTYPE) == VT_VOID ||
2388 (type2->t & VT_BTYPE) == VT_VOID) {
2389 /* void * can match anything */
2390 } else {
2391 /* exact type match, except for unsigned */
2392 tmp_type1 = *type1;
2393 tmp_type2 = *type2;
2394 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2395 VT_VOLATILE);
2396 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2397 VT_VOLATILE);
2398 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2399 tcc_warning("assignment from incompatible pointer type");
2401 /* check const and volatile */
2402 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2403 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2404 tcc_warning("assignment discards qualifiers from pointer target type");
2405 break;
2406 case VT_BYTE:
2407 case VT_SHORT:
2408 case VT_INT:
2409 case VT_LLONG:
2410 if (sbt == VT_PTR || sbt == VT_FUNC) {
2411 tcc_warning("assignment makes integer from pointer without a cast");
2413 /* XXX: more tests */
2414 break;
2415 case VT_STRUCT:
2416 tmp_type1 = *dt;
2417 tmp_type2 = *st;
2418 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2419 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2420 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2421 error:
2422 type_to_str(buf1, sizeof(buf1), st, NULL);
2423 type_to_str(buf2, sizeof(buf2), dt, NULL);
2424 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2426 break;
2428 type_ok:
2429 gen_cast(dt);
2432 /* store vtop in lvalue pushed on stack */
2433 ST_FUNC void vstore(void)
2435 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2437 ft = vtop[-1].type.t;
2438 sbt = vtop->type.t & VT_BTYPE;
2439 dbt = ft & VT_BTYPE;
2440 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2441 (sbt == VT_INT && dbt == VT_SHORT))
2442 && !(vtop->type.t & VT_BITFIELD)) {
2443 /* optimize char/short casts */
2444 delayed_cast = VT_MUSTCAST;
2445 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2446 /* XXX: factorize */
2447 if (ft & VT_CONSTANT)
2448 tcc_warning("assignment of read-only location");
2449 } else {
2450 delayed_cast = 0;
2451 if (!(ft & VT_BITFIELD))
2452 gen_assign_cast(&vtop[-1].type);
2455 if (sbt == VT_STRUCT) {
2456 /* if structure, only generate pointer */
2457 /* structure assignment : generate memcpy */
2458 /* XXX: optimize if small size */
2459 if (!nocode_wanted) {
2460 size = type_size(&vtop->type, &align);
2462 /* destination */
2463 vswap();
2464 vtop->type.t = VT_PTR;
2465 gaddrof();
2467 /* address of memcpy() */
2468 #ifdef TCC_ARM_EABI
2469 if(!(align & 7))
2470 vpush_global_sym(&func_old_type, TOK_memcpy8);
2471 else if(!(align & 3))
2472 vpush_global_sym(&func_old_type, TOK_memcpy4);
2473 else
2474 #endif
2475 vpush_global_sym(&func_old_type, TOK_memcpy);
2477 vswap();
2478 /* source */
2479 vpushv(vtop - 2);
2480 vtop->type.t = VT_PTR;
2481 gaddrof();
2482 /* type size */
2483 vpushi(size);
2484 gfunc_call(3);
2485 } else {
2486 vswap();
2487 vpop();
2489 /* leave source on stack */
2490 } else if (ft & VT_BITFIELD) {
2491 /* bitfield store handling */
2492 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2493 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2494 /* remove bit field info to avoid loops */
2495 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2497 /* duplicate source into other register */
2498 gv_dup();
2499 vswap();
2500 vrott(3);
2502 if((ft & VT_BTYPE) == VT_BOOL) {
2503 gen_cast(&vtop[-1].type);
2504 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2507 /* duplicate destination */
2508 vdup();
2509 vtop[-1] = vtop[-2];
2511 /* mask and shift source */
2512 if((ft & VT_BTYPE) != VT_BOOL) {
2513 if((ft & VT_BTYPE) == VT_LLONG) {
2514 vpushll((1ULL << bit_size) - 1ULL);
2515 } else {
2516 vpushi((1 << bit_size) - 1);
2518 gen_op('&');
2520 vpushi(bit_pos);
2521 gen_op(TOK_SHL);
2522 /* load destination, mask and or with source */
2523 vswap();
2524 if((ft & VT_BTYPE) == VT_LLONG) {
2525 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2526 } else {
2527 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2529 gen_op('&');
2530 gen_op('|');
2531 /* store result */
2532 vstore();
2534 /* pop off shifted source from "duplicate source..." above */
2535 vpop();
2537 } else {
2538 #ifdef CONFIG_TCC_BCHECK
2539 /* bound check case */
2540 if (vtop[-1].r & VT_MUSTBOUND) {
2541 vswap();
2542 gbound();
2543 vswap();
2545 #endif
2546 if (!nocode_wanted) {
2547 rc = RC_INT;
2548 if (is_float(ft)) {
2549 rc = RC_FLOAT;
2550 #ifdef TCC_TARGET_X86_64
2551 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2552 rc = RC_ST0;
2553 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2554 rc = RC_FRET;
2556 #endif
2558 r = gv(rc); /* generate value */
2559 /* if lvalue was saved on stack, must read it */
2560 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2561 SValue sv;
2562 t = get_reg(RC_INT);
2563 #ifdef TCC_TARGET_X86_64
2564 sv.type.t = VT_PTR;
2565 #else
2566 sv.type.t = VT_INT;
2567 #endif
2568 sv.r = VT_LOCAL | VT_LVAL;
2569 sv.c.ul = vtop[-1].c.ul;
2570 load(t, &sv);
2571 vtop[-1].r = t | VT_LVAL;
2573 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2574 #ifdef TCC_TARGET_X86_64
2575 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2576 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2577 #else
2578 if ((ft & VT_BTYPE) == VT_LLONG) {
2579 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2580 #endif
2581 vtop[-1].type.t = load_type;
2582 store(r, vtop - 1);
2583 vswap();
2584 /* convert to int to increment easily */
2585 vtop->type.t = addr_type;
2586 gaddrof();
2587 vpushi(load_size);
2588 gen_op('+');
2589 vtop->r |= VT_LVAL;
2590 vswap();
2591 vtop[-1].type.t = load_type;
2592 /* XXX: it works because r2 is spilled last ! */
2593 store(vtop->r2, vtop - 1);
2594 } else {
2595 store(r, vtop - 1);
2598 vswap();
2599 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2600 vtop->r |= delayed_cast;
2604 /* post defines POST/PRE add. c is the token ++ or -- */
2605 ST_FUNC void inc(int post, int c)
2607 test_lvalue();
2608 vdup(); /* save lvalue */
2609 if (post) {
2610 gv_dup(); /* duplicate value */
2611 vrotb(3);
2612 vrotb(3);
2614 /* add constant */
2615 vpushi(c - TOK_MID);
2616 gen_op('+');
2617 vstore(); /* store value */
2618 if (post)
2619 vpop(); /* if post op, return saved value */
2622 /* Parse GNUC __attribute__ extension. Currently, the following
2623 extensions are recognized:
2624 - aligned(n) : set data/function alignment.
2625 - packed : force data alignment to 1
2626 - section(x) : generate data/code in this section.
2627 - unused : currently ignored, but may be used someday.
2628 - regparm(n) : pass function parameters in registers (i386 only)
2630 static void parse_attribute(AttributeDef *ad)
2632 int t, n;
2634 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2635 next();
2636 skip('(');
2637 skip('(');
2638 while (tok != ')') {
2639 if (tok < TOK_IDENT)
2640 expect("attribute name");
2641 t = tok;
2642 next();
2643 switch(t) {
2644 case TOK_SECTION1:
2645 case TOK_SECTION2:
2646 skip('(');
2647 if (tok != TOK_STR)
2648 expect("section name");
2649 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2650 next();
2651 skip(')');
2652 break;
2653 case TOK_ALIAS1:
2654 case TOK_ALIAS2:
2655 skip('(');
2656 if (tok != TOK_STR)
2657 expect("alias(\"target\")");
2658 ad->alias_target = /* save string as token, for later */
2659 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2660 next();
2661 skip(')');
2662 break;
2663 case TOK_ALIGNED1:
2664 case TOK_ALIGNED2:
2665 if (tok == '(') {
2666 next();
2667 n = expr_const();
2668 if (n <= 0 || (n & (n - 1)) != 0)
2669 tcc_error("alignment must be a positive power of two");
2670 skip(')');
2671 } else {
2672 n = MAX_ALIGN;
2674 ad->a.aligned = n;
2675 break;
2676 case TOK_PACKED1:
2677 case TOK_PACKED2:
2678 ad->a.packed = 1;
2679 break;
2680 case TOK_WEAK1:
2681 case TOK_WEAK2:
2682 ad->a.weak = 1;
2683 break;
2684 case TOK_UNUSED1:
2685 case TOK_UNUSED2:
2686 /* currently, no need to handle it because tcc does not
2687 track unused objects */
2688 break;
2689 case TOK_NORETURN1:
2690 case TOK_NORETURN2:
2691 /* currently, no need to handle it because tcc does not
2692 track unused objects */
2693 break;
2694 case TOK_CDECL1:
2695 case TOK_CDECL2:
2696 case TOK_CDECL3:
2697 ad->a.func_call = FUNC_CDECL;
2698 break;
2699 case TOK_STDCALL1:
2700 case TOK_STDCALL2:
2701 case TOK_STDCALL3:
2702 ad->a.func_call = FUNC_STDCALL;
2703 break;
2704 #ifdef TCC_TARGET_I386
2705 case TOK_REGPARM1:
2706 case TOK_REGPARM2:
2707 skip('(');
2708 n = expr_const();
2709 if (n > 3)
2710 n = 3;
2711 else if (n < 0)
2712 n = 0;
2713 if (n > 0)
2714 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2715 skip(')');
2716 break;
2717 case TOK_FASTCALL1:
2718 case TOK_FASTCALL2:
2719 case TOK_FASTCALL3:
2720 ad->a.func_call = FUNC_FASTCALLW;
2721 break;
2722 #endif
2723 case TOK_MODE:
2724 skip('(');
2725 switch(tok) {
2726 case TOK_MODE_DI:
2727 ad->a.mode = VT_LLONG + 1;
2728 break;
2729 case TOK_MODE_HI:
2730 ad->a.mode = VT_SHORT + 1;
2731 break;
2732 case TOK_MODE_SI:
2733 ad->a.mode = VT_INT + 1;
2734 break;
2735 default:
2736 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2737 break;
2739 next();
2740 skip(')');
2741 break;
2742 case TOK_DLLEXPORT:
2743 ad->a.func_export = 1;
2744 break;
2745 case TOK_DLLIMPORT:
2746 ad->a.func_import = 1;
2747 break;
2748 default:
2749 if (tcc_state->warn_unsupported)
2750 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2751 /* skip parameters */
2752 if (tok == '(') {
2753 int parenthesis = 0;
2754 do {
2755 if (tok == '(')
2756 parenthesis++;
2757 else if (tok == ')')
2758 parenthesis--;
2759 next();
2760 } while (parenthesis && tok != -1);
2762 break;
2764 if (tok != ',')
2765 break;
2766 next();
2768 skip(')');
2769 skip(')');
2773 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2774 static void struct_decl(CType *type, int u, int tdef)
2776 int a, v, size, align, maxalign, c, offset, flexible;
2777 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2778 Sym *s, *ss, *ass, **ps;
2779 AttributeDef ad;
2780 CType type1, btype;
2782 a = tok; /* save decl type */
2783 next();
2784 if (tok != '{') {
2785 v = tok;
2786 next();
2787 /* struct already defined ? return it */
2788 if (v < TOK_IDENT)
2789 expect("struct/union/enum name");
2790 s = struct_find(v);
2791 if (s) {
2792 if (s->type.t != a)
2793 tcc_error("invalid type");
2794 goto do_decl;
2795 } else if (tok >= TOK_IDENT && !tdef)
2796 tcc_error("unknown struct/union/enum");
2797 } else {
2798 v = anon_sym++;
2800 type1.t = a;
2801 type1.ref = NULL;
2802 /* we put an undefined size for struct/union */
2803 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2804 s->r = 0; /* default alignment is zero as gcc */
2805 /* put struct/union/enum name in type */
2806 do_decl:
2807 type->t = u;
2808 type->ref = s;
2810 if (tok == '{') {
2811 next();
2812 if (s->c != -1)
2813 tcc_error("struct/union/enum already defined");
2814 /* cannot be empty */
2815 c = 0;
2816 /* non empty enums are not allowed */
2817 if (a == TOK_ENUM) {
2818 for(;;) {
2819 v = tok;
2820 if (v < TOK_UIDENT)
2821 expect("identifier");
2822 ss = sym_find(v);
2823 if (ss)
2824 tcc_error("redefinition of enumerator '%s'",
2825 get_tok_str(v, NULL));
2826 next();
2827 if (tok == '=') {
2828 next();
2829 c = expr_const();
2831 /* enum symbols have static storage */
2832 ss = sym_push(v, &int_type, VT_CONST, c);
2833 ss->type.t |= VT_STATIC;
2834 if (tok != ',')
2835 break;
2836 next();
2837 c++;
2838 /* NOTE: we accept a trailing comma */
2839 if (tok == '}')
2840 break;
2842 s->c = type_size(&int_type, &align);
2843 skip('}');
2844 } else {
2845 maxalign = 1;
2846 ps = &s->next;
2847 prevbt = VT_INT;
2848 bit_pos = 0;
2849 offset = 0;
2850 flexible = 0;
2851 while (tok != '}') {
2852 parse_btype(&btype, &ad);
2853 while (1) {
2854 if (flexible)
2855 tcc_error("flexible array member '%s' not at the end of struct",
2856 get_tok_str(v, NULL));
2857 bit_size = -1;
2858 v = 0;
2859 type1 = btype;
2860 if (tok != ':') {
2861 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2862 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2863 expect("identifier");
2864 if (type_size(&type1, &align) < 0) {
2865 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2866 flexible = 1;
2867 else
2868 tcc_error("field '%s' has incomplete type",
2869 get_tok_str(v, NULL));
2871 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2872 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2873 tcc_error("invalid type for '%s'",
2874 get_tok_str(v, NULL));
2876 if (tok == ':') {
2877 next();
2878 bit_size = expr_const();
2879 /* XXX: handle v = 0 case for messages */
2880 if (bit_size < 0)
2881 tcc_error("negative width in bit-field '%s'",
2882 get_tok_str(v, NULL));
2883 if (v && bit_size == 0)
2884 tcc_error("zero width for bit-field '%s'",
2885 get_tok_str(v, NULL));
2887 size = type_size(&type1, &align);
2888 if (ad.a.aligned) {
2889 if (align < ad.a.aligned)
2890 align = ad.a.aligned;
2891 } else if (ad.a.packed) {
2892 align = 1;
2893 } else if (*tcc_state->pack_stack_ptr) {
2894 if (align > *tcc_state->pack_stack_ptr)
2895 align = *tcc_state->pack_stack_ptr;
2897 lbit_pos = 0;
2898 if (bit_size >= 0) {
2899 bt = type1.t & VT_BTYPE;
2900 if (bt != VT_INT &&
2901 bt != VT_BYTE &&
2902 bt != VT_SHORT &&
2903 bt != VT_BOOL &&
2904 bt != VT_ENUM &&
2905 bt != VT_LLONG)
2906 tcc_error("bitfields must have scalar type");
2907 bsize = size * 8;
2908 if (bit_size > bsize) {
2909 tcc_error("width of '%s' exceeds its type",
2910 get_tok_str(v, NULL));
2911 } else if (bit_size == bsize) {
2912 /* no need for bit fields */
2913 bit_pos = 0;
2914 } else if (bit_size == 0) {
2915 /* XXX: what to do if only padding in a
2916 structure ? */
2917 /* zero size: means to pad */
2918 bit_pos = 0;
2919 } else {
2920 /* we do not have enough room ?
2921 did the type change?
2922 is it a union? */
2923 if ((bit_pos + bit_size) > bsize ||
2924 bt != prevbt || a == TOK_UNION)
2925 bit_pos = 0;
2926 lbit_pos = bit_pos;
2927 /* XXX: handle LSB first */
2928 type1.t |= VT_BITFIELD |
2929 (bit_pos << VT_STRUCT_SHIFT) |
2930 (bit_size << (VT_STRUCT_SHIFT + 6));
2931 bit_pos += bit_size;
2933 prevbt = bt;
2934 } else {
2935 bit_pos = 0;
2937 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2938 /* add new memory data only if starting
2939 bit field */
2940 if (lbit_pos == 0) {
2941 if (a == TOK_STRUCT) {
2942 c = (c + align - 1) & -align;
2943 offset = c;
2944 if (size > 0)
2945 c += size;
2946 } else {
2947 offset = 0;
2948 if (size > c)
2949 c = size;
2951 if (align > maxalign)
2952 maxalign = align;
2954 #if 0
2955 printf("add field %s offset=%d",
2956 get_tok_str(v, NULL), offset);
2957 if (type1.t & VT_BITFIELD) {
2958 printf(" pos=%d size=%d",
2959 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2960 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2962 printf("\n");
2963 #endif
2965 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2966 ass = type1.ref;
2967 while ((ass = ass->next) != NULL) {
2968 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2969 *ps = ss;
2970 ps = &ss->next;
2972 } else if (v) {
2973 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2974 *ps = ss;
2975 ps = &ss->next;
2977 if (tok == ';' || tok == TOK_EOF)
2978 break;
2979 skip(',');
2981 skip(';');
2983 skip('}');
2984 /* store size and alignment */
2985 s->c = (c + maxalign - 1) & -maxalign;
2986 s->r = maxalign;
2991 /* return 1 if basic type is a type size (short, long, long long) */
2992 int is_btype_size (int bt)
2994 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
2997 /* return 0 if no type declaration. otherwise, return the basic type
2998 and skip it.
3000 static int parse_btype(CType *type, AttributeDef *ad)
3002 int t, u, bt_size, complete, type_found, typespec_found;
3003 Sym *s;
3004 CType type1;
3006 memset(ad, 0, sizeof(AttributeDef));
3007 complete = 0;
3008 type_found = 0;
3009 typespec_found = 0;
3010 t = 0;
3011 while(1) {
3012 switch(tok) {
3013 case TOK_EXTENSION:
3014 /* currently, we really ignore extension */
3015 next();
3016 continue;
3018 /* basic types */
3019 case TOK_CHAR:
3020 u = VT_BYTE;
3021 basic_type:
3022 next();
3023 basic_type1:
3024 if (complete)
3025 tcc_error("too many basic types");
3026 t |= u;
3027 bt_size = is_btype_size (u & VT_BTYPE);
3028 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3029 complete = 1;
3030 typespec_found = 1;
3031 break;
3032 case TOK_VOID:
3033 u = VT_VOID;
3034 goto basic_type;
3035 case TOK_SHORT:
3036 u = VT_SHORT;
3037 goto basic_type;
3038 case TOK_INT:
3039 u = VT_INT;
3040 goto basic_type;
3041 case TOK_LONG:
3042 next();
3043 if ((t & VT_BTYPE) == VT_DOUBLE) {
3044 #ifndef TCC_TARGET_PE
3045 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3046 #endif
3047 } else if ((t & VT_BTYPE) == VT_LONG) {
3048 t = (t & ~VT_BTYPE) | VT_LLONG;
3049 } else {
3050 u = VT_LONG;
3051 goto basic_type1;
3053 break;
3054 case TOK_BOOL:
3055 u = VT_BOOL;
3056 goto basic_type;
3057 case TOK_FLOAT:
3058 u = VT_FLOAT;
3059 goto basic_type;
3060 case TOK_DOUBLE:
3061 next();
3062 if ((t & VT_BTYPE) == VT_LONG) {
3063 #ifdef TCC_TARGET_PE
3064 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3065 #else
3066 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3067 #endif
3068 } else {
3069 u = VT_DOUBLE;
3070 goto basic_type1;
3072 break;
3073 case TOK_ENUM:
3074 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3075 basic_type2:
3076 u = type1.t;
3077 type->ref = type1.ref;
3078 goto basic_type1;
3079 case TOK_STRUCT:
3080 case TOK_UNION:
3081 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3082 goto basic_type2;
3084 /* type modifiers */
3085 case TOK_CONST1:
3086 case TOK_CONST2:
3087 case TOK_CONST3:
3088 t |= VT_CONSTANT;
3089 next();
3090 break;
3091 case TOK_VOLATILE1:
3092 case TOK_VOLATILE2:
3093 case TOK_VOLATILE3:
3094 t |= VT_VOLATILE;
3095 next();
3096 break;
3097 case TOK_SIGNED1:
3098 case TOK_SIGNED2:
3099 case TOK_SIGNED3:
3100 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3101 tcc_error("signed and unsigned modifier");
3102 typespec_found = 1;
3103 t |= VT_DEFSIGN;
3104 next();
3105 break;
3106 case TOK_REGISTER:
3107 case TOK_AUTO:
3108 case TOK_RESTRICT1:
3109 case TOK_RESTRICT2:
3110 case TOK_RESTRICT3:
3111 next();
3112 break;
3113 case TOK_UNSIGNED:
3114 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3115 tcc_error("signed and unsigned modifier");
3116 t |= VT_DEFSIGN | VT_UNSIGNED;
3117 next();
3118 typespec_found = 1;
3119 break;
3121 /* storage */
3122 case TOK_EXTERN:
3123 t |= VT_EXTERN;
3124 next();
3125 break;
3126 case TOK_STATIC:
3127 t |= VT_STATIC;
3128 next();
3129 break;
3130 case TOK_TYPEDEF:
3131 t |= VT_TYPEDEF;
3132 next();
3133 break;
3134 case TOK_INLINE1:
3135 case TOK_INLINE2:
3136 case TOK_INLINE3:
3137 t |= VT_INLINE;
3138 next();
3139 break;
3141 /* GNUC attribute */
3142 case TOK_ATTRIBUTE1:
3143 case TOK_ATTRIBUTE2:
3144 parse_attribute(ad);
3145 if (ad->a.mode) {
3146 u = ad->a.mode -1;
3147 t = (t & ~VT_BTYPE) | u;
3149 break;
3150 /* GNUC typeof */
3151 case TOK_TYPEOF1:
3152 case TOK_TYPEOF2:
3153 case TOK_TYPEOF3:
3154 next();
3155 parse_expr_type(&type1);
3156 /* remove all storage modifiers except typedef */
3157 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3158 goto basic_type2;
3159 default:
3160 if (typespec_found)
3161 goto the_end;
3162 s = sym_find(tok);
3163 if (!s || !(s->type.t & VT_TYPEDEF))
3164 goto the_end;
3165 t |= (s->type.t & ~VT_TYPEDEF);
3166 type->ref = s->type.ref;
3167 if (s->r) {
3168 /* get attributes from typedef */
3169 if (0 == ad->a.aligned)
3170 ad->a.aligned = s->a.aligned;
3171 if (0 == ad->a.func_call)
3172 ad->a.func_call = s->a.func_call;
3173 ad->a.packed |= s->a.packed;
3175 next();
3176 typespec_found = 1;
3177 break;
3179 type_found = 1;
3181 the_end:
3182 if (tcc_state->char_is_unsigned) {
3183 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3184 t |= VT_UNSIGNED;
3187 /* long is never used as type */
3188 if ((t & VT_BTYPE) == VT_LONG)
3189 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3190 t = (t & ~VT_BTYPE) | VT_INT;
3191 #else
3192 t = (t & ~VT_BTYPE) | VT_LLONG;
3193 #endif
3194 type->t = t;
3195 return type_found;
3198 /* convert a function parameter type (array to pointer and function to
3199 function pointer) */
3200 static inline void convert_parameter_type(CType *pt)
3202 /* remove const and volatile qualifiers (XXX: const could be used
3203 to indicate a const function parameter */
3204 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3205 /* array must be transformed to pointer according to ANSI C */
3206 pt->t &= ~VT_ARRAY;
3207 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3208 mk_pointer(pt);
3212 ST_FUNC void parse_asm_str(CString *astr)
3214 skip('(');
3215 /* read the string */
3216 if (tok != TOK_STR)
3217 expect("string constant");
3218 cstr_new(astr);
3219 while (tok == TOK_STR) {
3220 /* XXX: add \0 handling too ? */
3221 cstr_cat(astr, tokc.cstr->data);
3222 next();
3224 cstr_ccat(astr, '\0');
3227 /* Parse an asm label and return the label
3228 * Don't forget to free the CString in the caller! */
3229 static void asm_label_instr(CString *astr)
3231 next();
3232 parse_asm_str(astr);
3233 skip(')');
3234 #ifdef ASM_DEBUG
3235 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3236 #endif
3239 static void post_type(CType *type, AttributeDef *ad)
3241 int n, l, t1, arg_size, align;
3242 Sym **plast, *s, *first;
3243 AttributeDef ad1;
3244 CType pt;
3246 if (tok == '(') {
3247 /* function declaration */
3248 next();
3249 l = 0;
3250 first = NULL;
3251 plast = &first;
3252 arg_size = 0;
3253 if (tok != ')') {
3254 for(;;) {
3255 /* read param name and compute offset */
3256 if (l != FUNC_OLD) {
3257 if (!parse_btype(&pt, &ad1)) {
3258 if (l) {
3259 tcc_error("invalid type");
3260 } else {
3261 l = FUNC_OLD;
3262 goto old_proto;
3265 l = FUNC_NEW;
3266 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3267 break;
3268 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3269 if ((pt.t & VT_BTYPE) == VT_VOID)
3270 tcc_error("parameter declared as void");
3271 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3272 } else {
3273 old_proto:
3274 n = tok;
3275 if (n < TOK_UIDENT)
3276 expect("identifier");
3277 pt.t = VT_INT;
3278 next();
3280 convert_parameter_type(&pt);
3281 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3282 *plast = s;
3283 plast = &s->next;
3284 if (tok == ')')
3285 break;
3286 skip(',');
3287 if (l == FUNC_NEW && tok == TOK_DOTS) {
3288 l = FUNC_ELLIPSIS;
3289 next();
3290 break;
3294 /* if no parameters, then old type prototype */
3295 if (l == 0)
3296 l = FUNC_OLD;
3297 skip(')');
3298 /* NOTE: const is ignored in returned type as it has a special
3299 meaning in gcc / C++ */
3300 type->t &= ~VT_CONSTANT;
3301 /* some ancient pre-K&R C allows a function to return an array
3302 and the array brackets to be put after the arguments, such
3303 that "int c()[]" means something like "int[] c()" */
3304 if (tok == '[') {
3305 next();
3306 skip(']'); /* only handle simple "[]" */
3307 type->t |= VT_PTR;
3309 /* we push a anonymous symbol which will contain the function prototype */
3310 ad->a.func_args = arg_size;
3311 s = sym_push(SYM_FIELD, type, 0, l);
3312 s->a = ad->a;
3313 s->next = first;
3314 type->t = VT_FUNC;
3315 type->ref = s;
3316 } else if (tok == '[') {
3317 /* array definition */
3318 next();
3319 if (tok == TOK_RESTRICT1)
3320 next();
3321 n = -1;
3322 t1 = 0;
3323 if (tok != ']') {
3324 if (!local_stack || nocode_wanted)
3325 vpushi(expr_const());
3326 else gexpr();
3327 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3328 n = vtop->c.i;
3329 if (n < 0)
3330 tcc_error("invalid array size");
3331 } else {
3332 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3333 tcc_error("size of variable length array should be an integer");
3334 t1 = VT_VLA;
3337 skip(']');
3338 /* parse next post type */
3339 post_type(type, ad);
3340 if (type->t == VT_FUNC)
3341 tcc_error("declaration of an array of functions");
3342 t1 |= type->t & VT_VLA;
3344 if (t1 & VT_VLA) {
3345 loc -= type_size(&int_type, &align);
3346 loc &= -align;
3347 n = loc;
3349 vla_runtime_type_size(type, &align);
3350 gen_op('*');
3351 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3352 vswap();
3353 vstore();
3355 if (n != -1)
3356 vpop();
3358 /* we push an anonymous symbol which will contain the array
3359 element type */
3360 s = sym_push(SYM_FIELD, type, 0, n);
3361 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3362 type->ref = s;
3366 /* Parse a type declaration (except basic type), and return the type
3367 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3368 expected. 'type' should contain the basic type. 'ad' is the
3369 attribute definition of the basic type. It can be modified by
3370 type_decl().
3372 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3374 Sym *s;
3375 CType type1, *type2;
3376 int qualifiers, storage;
3378 while (tok == '*') {
3379 qualifiers = 0;
3380 redo:
3381 next();
3382 switch(tok) {
3383 case TOK_CONST1:
3384 case TOK_CONST2:
3385 case TOK_CONST3:
3386 qualifiers |= VT_CONSTANT;
3387 goto redo;
3388 case TOK_VOLATILE1:
3389 case TOK_VOLATILE2:
3390 case TOK_VOLATILE3:
3391 qualifiers |= VT_VOLATILE;
3392 goto redo;
3393 case TOK_RESTRICT1:
3394 case TOK_RESTRICT2:
3395 case TOK_RESTRICT3:
3396 goto redo;
3398 mk_pointer(type);
3399 type->t |= qualifiers;
3402 /* XXX: clarify attribute handling */
3403 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3404 parse_attribute(ad);
3406 /* recursive type */
3407 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3408 type1.t = 0; /* XXX: same as int */
3409 if (tok == '(') {
3410 next();
3411 /* XXX: this is not correct to modify 'ad' at this point, but
3412 the syntax is not clear */
3413 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3414 parse_attribute(ad);
3415 type_decl(&type1, ad, v, td);
3416 skip(')');
3417 } else {
3418 /* type identifier */
3419 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3420 *v = tok;
3421 next();
3422 } else {
3423 if (!(td & TYPE_ABSTRACT))
3424 expect("identifier");
3425 *v = 0;
3428 storage = type->t & VT_STORAGE;
3429 type->t &= ~VT_STORAGE;
3430 if (storage & VT_STATIC) {
3431 int saved_nocode_wanted = nocode_wanted;
3432 nocode_wanted = 1;
3433 post_type(type, ad);
3434 nocode_wanted = saved_nocode_wanted;
3435 } else
3436 post_type(type, ad);
3437 type->t |= storage;
3438 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3439 parse_attribute(ad);
3441 if (!type1.t)
3442 return;
3443 /* append type at the end of type1 */
3444 type2 = &type1;
3445 for(;;) {
3446 s = type2->ref;
3447 type2 = &s->type;
3448 if (!type2->t) {
3449 *type2 = *type;
3450 break;
3453 *type = type1;
3456 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3457 ST_FUNC int lvalue_type(int t)
3459 int bt, r;
3460 r = VT_LVAL;
3461 bt = t & VT_BTYPE;
3462 if (bt == VT_BYTE || bt == VT_BOOL)
3463 r |= VT_LVAL_BYTE;
3464 else if (bt == VT_SHORT)
3465 r |= VT_LVAL_SHORT;
3466 else
3467 return r;
3468 if (t & VT_UNSIGNED)
3469 r |= VT_LVAL_UNSIGNED;
3470 return r;
3473 /* indirection with full error checking and bound check */
3474 ST_FUNC void indir(void)
3476 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3477 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3478 return;
3479 expect("pointer");
3481 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3482 gv(RC_INT);
3483 vtop->type = *pointed_type(&vtop->type);
3484 /* Arrays and functions are never lvalues */
3485 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3486 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3487 vtop->r |= lvalue_type(vtop->type.t);
3488 /* if bound checking, the referenced pointer must be checked */
3489 #ifdef CONFIG_TCC_BCHECK
3490 if (tcc_state->do_bounds_check)
3491 vtop->r |= VT_MUSTBOUND;
3492 #endif
3496 /* pass a parameter to a function and do type checking and casting */
3497 static void gfunc_param_typed(Sym *func, Sym *arg)
3499 int func_type;
3500 CType type;
3502 func_type = func->c;
3503 if (func_type == FUNC_OLD ||
3504 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3505 /* default casting : only need to convert float to double */
3506 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3507 type.t = VT_DOUBLE;
3508 gen_cast(&type);
3509 } else if (vtop->type.t & VT_BITFIELD) {
3510 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3511 gen_cast(&type);
3513 } else if (arg == NULL) {
3514 tcc_error("too many arguments to function");
3515 } else {
3516 type = arg->type;
3517 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3518 gen_assign_cast(&type);
3522 /* parse an expression of the form '(type)' or '(expr)' and return its
3523 type */
3524 static void parse_expr_type(CType *type)
3526 int n;
3527 AttributeDef ad;
3529 skip('(');
3530 if (parse_btype(type, &ad)) {
3531 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3532 } else {
3533 expr_type(type);
3535 skip(')');
3538 static void parse_type(CType *type)
3540 AttributeDef ad;
3541 int n;
3543 if (!parse_btype(type, &ad)) {
3544 expect("type");
3546 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3549 static void vpush_tokc(int t)
3551 CType type;
3552 type.t = t;
3553 type.ref = 0;
3554 vsetc(&type, VT_CONST, &tokc);
3557 ST_FUNC void unary(void)
3559 int n, t, align, size, r, sizeof_caller;
3560 CType type;
3561 Sym *s;
3562 AttributeDef ad;
3563 static int in_sizeof = 0;
3565 sizeof_caller = in_sizeof;
3566 in_sizeof = 0;
3567 /* XXX: GCC 2.95.3 does not generate a table although it should be
3568 better here */
3569 tok_next:
3570 switch(tok) {
3571 case TOK_EXTENSION:
3572 next();
3573 goto tok_next;
3574 case TOK_CINT:
3575 case TOK_CCHAR:
3576 case TOK_LCHAR:
3577 vpushi(tokc.i);
3578 next();
3579 break;
3580 case TOK_CUINT:
3581 vpush_tokc(VT_INT | VT_UNSIGNED);
3582 next();
3583 break;
3584 case TOK_CLLONG:
3585 vpush_tokc(VT_LLONG);
3586 next();
3587 break;
3588 case TOK_CULLONG:
3589 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3590 next();
3591 break;
3592 case TOK_CFLOAT:
3593 vpush_tokc(VT_FLOAT);
3594 next();
3595 break;
3596 case TOK_CDOUBLE:
3597 vpush_tokc(VT_DOUBLE);
3598 next();
3599 break;
3600 case TOK_CLDOUBLE:
3601 vpush_tokc(VT_LDOUBLE);
3602 next();
3603 break;
3604 case TOK___FUNCTION__:
3605 if (!gnu_ext)
3606 goto tok_identifier;
3607 /* fall thru */
3608 case TOK___FUNC__:
3610 void *ptr;
3611 int len;
3612 /* special function name identifier */
3613 len = strlen(funcname) + 1;
3614 /* generate char[len] type */
3615 type.t = VT_BYTE;
3616 mk_pointer(&type);
3617 type.t |= VT_ARRAY;
3618 type.ref->c = len;
3619 vpush_ref(&type, data_section, data_section->data_offset, len);
3620 ptr = section_ptr_add(data_section, len);
3621 memcpy(ptr, funcname, len);
3622 next();
3624 break;
3625 case TOK_LSTR:
3626 #ifdef TCC_TARGET_PE
3627 t = VT_SHORT | VT_UNSIGNED;
3628 #else
3629 t = VT_INT;
3630 #endif
3631 goto str_init;
3632 case TOK_STR:
3633 /* string parsing */
3634 t = VT_BYTE;
3635 str_init:
3636 if (tcc_state->warn_write_strings)
3637 t |= VT_CONSTANT;
3638 type.t = t;
3639 mk_pointer(&type);
3640 type.t |= VT_ARRAY;
3641 memset(&ad, 0, sizeof(AttributeDef));
3642 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3643 break;
3644 case '(':
3645 next();
3646 /* cast ? */
3647 if (parse_btype(&type, &ad)) {
3648 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3649 skip(')');
3650 /* check ISOC99 compound literal */
3651 if (tok == '{') {
3652 /* data is allocated locally by default */
3653 if (global_expr)
3654 r = VT_CONST;
3655 else
3656 r = VT_LOCAL;
3657 /* all except arrays are lvalues */
3658 if (!(type.t & VT_ARRAY))
3659 r |= lvalue_type(type.t);
3660 memset(&ad, 0, sizeof(AttributeDef));
3661 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3662 } else {
3663 if (sizeof_caller) {
3664 vpush(&type);
3665 return;
3667 unary();
3668 gen_cast(&type);
3670 } else if (tok == '{') {
3671 /* save all registers */
3672 save_regs(0);
3673 /* statement expression : we do not accept break/continue
3674 inside as GCC does */
3675 block(NULL, NULL, NULL, NULL, 0, 1);
3676 skip(')');
3677 } else {
3678 gexpr();
3679 skip(')');
3681 break;
3682 case '*':
3683 next();
3684 unary();
3685 indir();
3686 break;
3687 case '&':
3688 next();
3689 unary();
3690 /* functions names must be treated as function pointers,
3691 except for unary '&' and sizeof. Since we consider that
3692 functions are not lvalues, we only have to handle it
3693 there and in function calls. */
3694 /* arrays can also be used although they are not lvalues */
3695 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3696 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3697 test_lvalue();
3698 mk_pointer(&vtop->type);
3699 gaddrof();
3700 break;
3701 case '!':
3702 next();
3703 unary();
3704 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3705 CType boolean;
3706 boolean.t = VT_BOOL;
3707 gen_cast(&boolean);
3708 vtop->c.i = !vtop->c.i;
3709 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3710 vtop->c.i = vtop->c.i ^ 1;
3711 else {
3712 save_regs(1);
3713 vseti(VT_JMP, gvtst(1, 0));
3715 break;
3716 case '~':
3717 next();
3718 unary();
3719 vpushi(-1);
3720 gen_op('^');
3721 break;
3722 case '+':
3723 next();
3724 unary();
3725 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3726 tcc_error("pointer not accepted for unary plus");
3727 /* In order to force cast, we add zero, except for floating point
3728 where we really need an noop (otherwise -0.0 will be transformed
3729 into +0.0). */
3730 if (!is_float(vtop->type.t)) {
3731 vpushi(0);
3732 gen_op('+');
3734 break;
3735 case TOK_SIZEOF:
3736 case TOK_ALIGNOF1:
3737 case TOK_ALIGNOF2:
3738 t = tok;
3739 next();
3740 in_sizeof++;
3741 unary_type(&type); // Perform a in_sizeof = 0;
3742 size = type_size(&type, &align);
3743 if (t == TOK_SIZEOF) {
3744 if (!(type.t & VT_VLA)) {
3745 if (size < 0)
3746 tcc_error("sizeof applied to an incomplete type");
3747 vpushs(size);
3748 } else {
3749 vla_runtime_type_size(&type, &align);
3751 } else {
3752 vpushs(align);
3754 vtop->type.t |= VT_UNSIGNED;
3755 break;
3757 case TOK_builtin_types_compatible_p:
3759 CType type1, type2;
3760 next();
3761 skip('(');
3762 parse_type(&type1);
3763 skip(',');
3764 parse_type(&type2);
3765 skip(')');
3766 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3767 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3768 vpushi(is_compatible_types(&type1, &type2));
3770 break;
3771 case TOK_builtin_constant_p:
3773 int saved_nocode_wanted, res;
3774 next();
3775 skip('(');
3776 saved_nocode_wanted = nocode_wanted;
3777 nocode_wanted = 1;
3778 gexpr();
3779 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3780 vpop();
3781 nocode_wanted = saved_nocode_wanted;
3782 skip(')');
3783 vpushi(res);
3785 break;
3786 case TOK_builtin_frame_address:
3788 int level;
3789 CType type;
3790 next();
3791 skip('(');
3792 if (tok != TOK_CINT || tokc.i < 0) {
3793 tcc_error("__builtin_frame_address only takes positive integers");
3795 level = tokc.i;
3796 next();
3797 skip(')');
3798 type.t = VT_VOID;
3799 mk_pointer(&type);
3800 vset(&type, VT_LOCAL, 0); /* local frame */
3801 while (level--) {
3802 mk_pointer(&vtop->type);
3803 indir(); /* -> parent frame */
3806 break;
3807 #ifdef TCC_TARGET_X86_64
3808 #ifdef TCC_TARGET_PE
3809 case TOK_builtin_va_start:
3811 next();
3812 skip('(');
3813 expr_eq();
3814 skip(',');
3815 expr_eq();
3816 skip(')');
3817 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3818 tcc_error("__builtin_va_start expects a local variable");
3819 vtop->r &= ~(VT_LVAL | VT_REF);
3820 vtop->type = char_pointer_type;
3821 vstore();
3823 break;
3824 #else
3825 case TOK_builtin_va_arg_types:
3827 CType type;
3828 next();
3829 skip('(');
3830 parse_type(&type);
3831 skip(')');
3832 vpushi(classify_x86_64_va_arg(&type));
3834 break;
3835 #endif
3836 #endif
3837 case TOK_INC:
3838 case TOK_DEC:
3839 t = tok;
3840 next();
3841 unary();
3842 inc(0, t);
3843 break;
3844 case '-':
3845 next();
3846 unary();
3847 t = vtop->type.t & VT_BTYPE;
3848 if (is_float(t)) {
3849 /* In IEEE negate(x) isn't subtract(0,x), but rather
3850 subtract(-0, x). */
3851 vpush(&vtop->type);
3852 if (t == VT_FLOAT)
3853 vtop->c.f = -0.0f;
3854 else if (t == VT_DOUBLE)
3855 vtop->c.d = -0.0;
3856 else
3857 vtop->c.ld = -0.0;
3858 } else
3859 vpushi(0);
3860 vswap();
3861 gen_op('-');
3862 break;
3863 case TOK_LAND:
3864 if (!gnu_ext)
3865 goto tok_identifier;
3866 next();
3867 /* allow to take the address of a label */
3868 if (tok < TOK_UIDENT)
3869 expect("label identifier");
3870 s = label_find(tok);
3871 if (!s) {
3872 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3873 } else {
3874 if (s->r == LABEL_DECLARED)
3875 s->r = LABEL_FORWARD;
3877 if (!s->type.t) {
3878 s->type.t = VT_VOID;
3879 mk_pointer(&s->type);
3880 s->type.t |= VT_STATIC;
3882 vset(&s->type, VT_CONST | VT_SYM, 0);
3883 vtop->sym = s;
3884 next();
3885 break;
3887 // special qnan , snan and infinity values
3888 case TOK___NAN__:
3889 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3890 next();
3891 break;
3892 case TOK___SNAN__:
3893 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3894 next();
3895 break;
3896 case TOK___INF__:
3897 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3898 next();
3899 break;
3901 default:
3902 tok_identifier:
3903 t = tok;
3904 next();
3905 if (t < TOK_UIDENT)
3906 expect("identifier");
3907 s = sym_find(t);
3908 if (!s) {
3909 if (tok != '(')
3910 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3911 /* for simple function calls, we tolerate undeclared
3912 external reference to int() function */
3913 if (tcc_state->warn_implicit_function_declaration)
3914 tcc_warning("implicit declaration of function '%s'",
3915 get_tok_str(t, NULL));
3916 s = external_global_sym(t, &func_old_type, 0);
3918 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3919 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3920 /* if referencing an inline function, then we generate a
3921 symbol to it if not already done. It will have the
3922 effect to generate code for it at the end of the
3923 compilation unit. Inline function as always
3924 generated in the text section. */
3925 if (!s->c)
3926 put_extern_sym(s, text_section, 0, 0);
3927 r = VT_SYM | VT_CONST;
3928 } else {
3929 r = s->r;
3931 vset(&s->type, r, s->c);
3932 /* if forward reference, we must point to s */
3933 if (vtop->r & VT_SYM) {
3934 vtop->sym = s;
3935 vtop->c.ull = 0;
3937 break;
3940 /* post operations */
3941 while (1) {
3942 if (tok == TOK_INC || tok == TOK_DEC) {
3943 inc(1, tok);
3944 next();
3945 } else if (tok == '.' || tok == TOK_ARROW) {
3946 int qualifiers;
3947 /* field */
3948 if (tok == TOK_ARROW)
3949 indir();
3950 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3951 test_lvalue();
3952 gaddrof();
3953 next();
3954 /* expect pointer on structure */
3955 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3956 expect("struct or union");
3957 s = vtop->type.ref;
3958 /* find field */
3959 tok |= SYM_FIELD;
3960 while ((s = s->next) != NULL) {
3961 if (s->v == tok)
3962 break;
3964 if (!s)
3965 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3966 /* add field offset to pointer */
3967 vtop->type = char_pointer_type; /* change type to 'char *' */
3968 vpushi(s->c);
3969 gen_op('+');
3970 /* change type to field type, and set to lvalue */
3971 vtop->type = s->type;
3972 vtop->type.t |= qualifiers;
3973 /* an array is never an lvalue */
3974 if (!(vtop->type.t & VT_ARRAY)) {
3975 vtop->r |= lvalue_type(vtop->type.t);
3976 #ifdef CONFIG_TCC_BCHECK
3977 /* if bound checking, the referenced pointer must be checked */
3978 if (tcc_state->do_bounds_check)
3979 vtop->r |= VT_MUSTBOUND;
3980 #endif
3982 next();
3983 } else if (tok == '[') {
3984 next();
3985 gexpr();
3986 gen_op('+');
3987 indir();
3988 skip(']');
3989 } else if (tok == '(') {
3990 SValue ret;
3991 Sym *sa;
3992 int nb_args, ret_nregs, ret_align, variadic;
3994 /* function call */
3995 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3996 /* pointer test (no array accepted) */
3997 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3998 vtop->type = *pointed_type(&vtop->type);
3999 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4000 goto error_func;
4001 } else {
4002 error_func:
4003 expect("function pointer");
4005 } else {
4006 vtop->r &= ~VT_LVAL; /* no lvalue */
4008 /* get return type */
4009 s = vtop->type.ref;
4010 next();
4011 sa = s->next; /* first parameter */
4012 nb_args = 0;
4013 ret.r2 = VT_CONST;
4014 /* compute first implicit argument if a structure is returned */
4015 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4016 variadic = (s->c == FUNC_ELLIPSIS);
4017 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4018 &ret_align);
4019 if (!ret_nregs) {
4020 /* get some space for the returned structure */
4021 size = type_size(&s->type, &align);
4022 loc = (loc - size) & -align;
4023 ret.type = s->type;
4024 ret.r = VT_LOCAL | VT_LVAL;
4025 /* pass it as 'int' to avoid structure arg passing
4026 problems */
4027 vseti(VT_LOCAL, loc);
4028 ret.c = vtop->c;
4029 nb_args++;
4031 } else {
4032 ret_nregs = 1;
4033 ret.type = s->type;
4036 if (ret_nregs) {
4037 /* return in register */
4038 if (is_float(ret.type.t)) {
4039 ret.r = reg_fret(ret.type.t);
4040 #ifdef TCC_TARGET_X86_64
4041 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4042 ret.r2 = REG_QRET;
4043 #endif
4044 } else {
4045 #ifdef TCC_TARGET_X86_64
4046 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4047 #else
4048 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4049 #endif
4050 ret.r2 = REG_LRET;
4051 ret.r = REG_IRET;
4053 ret.c.i = 0;
4055 if (tok != ')') {
4056 for(;;) {
4057 expr_eq();
4058 gfunc_param_typed(s, sa);
4059 nb_args++;
4060 if (sa)
4061 sa = sa->next;
4062 if (tok == ')')
4063 break;
4064 skip(',');
4067 if (sa)
4068 tcc_error("too few arguments to function");
4069 skip(')');
4070 if (!nocode_wanted) {
4071 gfunc_call(nb_args);
4072 } else {
4073 vtop -= (nb_args + 1);
4076 /* return value */
4077 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4078 vsetc(&ret.type, r, &ret.c);
4079 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4082 /* handle packed struct return */
4083 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4084 int addr, offset;
4086 size = type_size(&s->type, &align);
4087 loc = (loc - size) & -align;
4088 addr = loc;
4089 offset = 0;
4090 for (;;) {
4091 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4092 vswap();
4093 vstore();
4094 vtop--;
4095 if (--ret_nregs == 0)
4096 break;
4097 /* XXX: compatible with arm only: ret_align == register_size */
4098 offset += ret_align;
4100 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4102 } else {
4103 break;
4108 ST_FUNC void expr_prod(void)
4110 int t;
4112 unary();
4113 while (tok == '*' || tok == '/' || tok == '%') {
4114 t = tok;
4115 next();
4116 unary();
4117 gen_op(t);
4121 ST_FUNC void expr_sum(void)
4123 int t;
4125 expr_prod();
4126 while (tok == '+' || tok == '-') {
4127 t = tok;
4128 next();
4129 expr_prod();
4130 gen_op(t);
4134 static void expr_shift(void)
4136 int t;
4138 expr_sum();
4139 while (tok == TOK_SHL || tok == TOK_SAR) {
4140 t = tok;
4141 next();
4142 expr_sum();
4143 gen_op(t);
4147 static void expr_cmp(void)
4149 int t;
4151 expr_shift();
4152 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4153 tok == TOK_ULT || tok == TOK_UGE) {
4154 t = tok;
4155 next();
4156 expr_shift();
4157 gen_op(t);
4161 static void expr_cmpeq(void)
4163 int t;
4165 expr_cmp();
4166 while (tok == TOK_EQ || tok == TOK_NE) {
4167 t = tok;
4168 next();
4169 expr_cmp();
4170 gen_op(t);
4174 static void expr_and(void)
4176 expr_cmpeq();
4177 while (tok == '&') {
4178 next();
4179 expr_cmpeq();
4180 gen_op('&');
4184 static void expr_xor(void)
4186 expr_and();
4187 while (tok == '^') {
4188 next();
4189 expr_and();
4190 gen_op('^');
4194 static void expr_or(void)
4196 expr_xor();
4197 while (tok == '|') {
4198 next();
4199 expr_xor();
4200 gen_op('|');
4204 /* XXX: fix this mess */
4205 static void expr_land_const(void)
4207 expr_or();
4208 while (tok == TOK_LAND) {
4209 next();
4210 expr_or();
4211 gen_op(TOK_LAND);
4215 /* XXX: fix this mess */
4216 static void expr_lor_const(void)
4218 expr_land_const();
4219 while (tok == TOK_LOR) {
4220 next();
4221 expr_land_const();
4222 gen_op(TOK_LOR);
4226 /* only used if non constant */
4227 static void expr_land(void)
4229 int t;
4231 expr_or();
4232 if (tok == TOK_LAND) {
4233 t = 0;
4234 save_regs(1);
4235 for(;;) {
4236 t = gvtst(1, t);
4237 if (tok != TOK_LAND) {
4238 vseti(VT_JMPI, t);
4239 break;
4241 next();
4242 expr_or();
4247 static void expr_lor(void)
4249 int t;
4251 expr_land();
4252 if (tok == TOK_LOR) {
4253 t = 0;
4254 save_regs(1);
4255 for(;;) {
4256 t = gvtst(0, t);
4257 if (tok != TOK_LOR) {
4258 vseti(VT_JMP, t);
4259 break;
4261 next();
4262 expr_land();
4267 /* XXX: better constant handling */
4268 static void expr_cond(void)
4270 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4271 SValue sv;
4272 CType type, type1, type2;
4274 if (const_wanted) {
4275 expr_lor_const();
4276 if (tok == '?') {
4277 CType boolean;
4278 int c;
4279 boolean.t = VT_BOOL;
4280 vdup();
4281 gen_cast(&boolean);
4282 c = vtop->c.i;
4283 vpop();
4284 next();
4285 if (tok != ':' || !gnu_ext) {
4286 vpop();
4287 gexpr();
4289 if (!c)
4290 vpop();
4291 skip(':');
4292 expr_cond();
4293 if (c)
4294 vpop();
4296 } else {
4297 expr_lor();
4298 if (tok == '?') {
4299 next();
4300 if (vtop != vstack) {
4301 /* needed to avoid having different registers saved in
4302 each branch */
4303 if (is_float(vtop->type.t)) {
4304 rc = RC_FLOAT;
4305 #ifdef TCC_TARGET_X86_64
4306 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4307 rc = RC_ST0;
4309 #endif
4311 else
4312 rc = RC_INT;
4313 gv(rc);
4314 save_regs(1);
4316 if (tok == ':' && gnu_ext) {
4317 gv_dup();
4318 tt = gvtst(1, 0);
4319 } else {
4320 tt = gvtst(1, 0);
4321 gexpr();
4323 type1 = vtop->type;
4324 sv = *vtop; /* save value to handle it later */
4325 vtop--; /* no vpop so that FP stack is not flushed */
4326 skip(':');
4327 u = gjmp(0);
4328 gsym(tt);
4329 expr_cond();
4330 type2 = vtop->type;
4332 t1 = type1.t;
4333 bt1 = t1 & VT_BTYPE;
4334 t2 = type2.t;
4335 bt2 = t2 & VT_BTYPE;
4336 /* cast operands to correct type according to ISOC rules */
4337 if (is_float(bt1) || is_float(bt2)) {
4338 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4339 type.t = VT_LDOUBLE;
4340 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4341 type.t = VT_DOUBLE;
4342 } else {
4343 type.t = VT_FLOAT;
4345 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4346 /* cast to biggest op */
4347 type.t = VT_LLONG;
4348 /* convert to unsigned if it does not fit in a long long */
4349 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4350 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4351 type.t |= VT_UNSIGNED;
4352 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4353 /* If one is a null ptr constant the result type
4354 is the other. */
4355 if (is_null_pointer (vtop))
4356 type = type1;
4357 else if (is_null_pointer (&sv))
4358 type = type2;
4359 /* XXX: test pointer compatibility, C99 has more elaborate
4360 rules here. */
4361 else
4362 type = type1;
4363 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4364 /* XXX: test function pointer compatibility */
4365 type = bt1 == VT_FUNC ? type1 : type2;
4366 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4367 /* XXX: test structure compatibility */
4368 type = bt1 == VT_STRUCT ? type1 : type2;
4369 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4370 /* NOTE: as an extension, we accept void on only one side */
4371 type.t = VT_VOID;
4372 } else {
4373 /* integer operations */
4374 type.t = VT_INT;
4375 /* convert to unsigned if it does not fit in an integer */
4376 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4377 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4378 type.t |= VT_UNSIGNED;
4381 /* now we convert second operand */
4382 gen_cast(&type);
4383 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4384 gaddrof();
4385 rc = RC_INT;
4386 if (is_float(type.t)) {
4387 rc = RC_FLOAT;
4388 #ifdef TCC_TARGET_X86_64
4389 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4390 rc = RC_ST0;
4392 #endif
4393 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4394 /* for long longs, we use fixed registers to avoid having
4395 to handle a complicated move */
4396 rc = RC_IRET;
4399 r2 = gv(rc);
4400 /* this is horrible, but we must also convert first
4401 operand */
4402 tt = gjmp(0);
4403 gsym(u);
4404 /* put again first value and cast it */
4405 *vtop = sv;
4406 gen_cast(&type);
4407 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4408 gaddrof();
4409 r1 = gv(rc);
4410 move_reg(r2, r1, type.t);
4411 vtop->r = r2;
4412 gsym(tt);
4417 static void expr_eq(void)
4419 int t;
4421 expr_cond();
4422 if (tok == '=' ||
4423 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4424 tok == TOK_A_XOR || tok == TOK_A_OR ||
4425 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4426 test_lvalue();
4427 t = tok;
4428 next();
4429 if (t == '=') {
4430 expr_eq();
4431 } else {
4432 vdup();
4433 expr_eq();
4434 gen_op(t & 0x7f);
4436 vstore();
4440 ST_FUNC void gexpr(void)
4442 while (1) {
4443 expr_eq();
4444 if (tok != ',')
4445 break;
4446 vpop();
4447 next();
4451 /* parse an expression and return its type without any side effect. */
4452 static void expr_type(CType *type)
4454 int saved_nocode_wanted;
4456 saved_nocode_wanted = nocode_wanted;
4457 nocode_wanted = 1;
4458 gexpr();
4459 *type = vtop->type;
4460 vpop();
4461 nocode_wanted = saved_nocode_wanted;
4464 /* parse a unary expression and return its type without any side
4465 effect. */
4466 static void unary_type(CType *type)
4468 int a;
4470 a = nocode_wanted;
4471 nocode_wanted = 1;
4472 unary();
4473 *type = vtop->type;
4474 vpop();
4475 nocode_wanted = a;
4478 /* parse a constant expression and return value in vtop. */
4479 static void expr_const1(void)
4481 int a;
4482 a = const_wanted;
4483 const_wanted = 1;
4484 expr_cond();
4485 const_wanted = a;
4488 /* parse an integer constant and return its value. */
4489 ST_FUNC int expr_const(void)
4491 int c;
4492 expr_const1();
4493 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4494 expect("constant expression");
4495 c = vtop->c.i;
4496 vpop();
4497 return c;
4500 /* return the label token if current token is a label, otherwise
4501 return zero */
4502 static int is_label(void)
4504 int last_tok;
4506 /* fast test first */
4507 if (tok < TOK_UIDENT)
4508 return 0;
4509 /* no need to save tokc because tok is an identifier */
4510 last_tok = tok;
4511 next();
4512 if (tok == ':') {
4513 next();
4514 return last_tok;
4515 } else {
4516 unget_tok(last_tok);
4517 return 0;
4521 static void label_or_decl(int l)
4523 int last_tok;
4525 /* fast test first */
4526 if (tok >= TOK_UIDENT)
4528 /* no need to save tokc because tok is an identifier */
4529 last_tok = tok;
4530 next();
4531 if (tok == ':') {
4532 unget_tok(last_tok);
4533 return;
4535 unget_tok(last_tok);
4537 decl(l);
4540 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4541 int case_reg, int is_expr)
4543 int a, b, c, d;
4544 Sym *s, *frame_bottom;
4546 /* generate line number info */
4547 if (tcc_state->do_debug &&
4548 (last_line_num != file->line_num || last_ind != ind)) {
4549 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4550 last_ind = ind;
4551 last_line_num = file->line_num;
4554 if (is_expr) {
4555 /* default return value is (void) */
4556 vpushi(0);
4557 vtop->type.t = VT_VOID;
4560 if (tok == TOK_IF) {
4561 /* if test */
4562 next();
4563 skip('(');
4564 gexpr();
4565 skip(')');
4566 a = gvtst(1, 0);
4567 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4568 c = tok;
4569 if (c == TOK_ELSE) {
4570 next();
4571 d = gjmp(0);
4572 gsym(a);
4573 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4574 gsym(d); /* patch else jmp */
4575 } else
4576 gsym(a);
4577 } else if (tok == TOK_WHILE) {
4578 next();
4579 d = ind;
4580 skip('(');
4581 gexpr();
4582 skip(')');
4583 a = gvtst(1, 0);
4584 b = 0;
4585 block(&a, &b, case_sym, def_sym, case_reg, 0);
4586 gjmp_addr(d);
4587 gsym(a);
4588 gsym_addr(b, d);
4589 } else if (tok == '{') {
4590 Sym *llabel;
4591 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4593 next();
4594 /* record local declaration stack position */
4595 s = local_stack;
4596 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4597 frame_bottom->next = scope_stack_bottom;
4598 scope_stack_bottom = frame_bottom;
4599 llabel = local_label_stack;
4601 /* save VLA state */
4602 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4603 if (saved_vla_sp_loc != &vla_sp_root_loc)
4604 vla_sp_loc = &block_vla_sp_loc;
4606 saved_vla_flags = vla_flags;
4607 vla_flags |= VLA_NEED_NEW_FRAME;
4609 /* handle local labels declarations */
4610 if (tok == TOK_LABEL) {
4611 next();
4612 for(;;) {
4613 if (tok < TOK_UIDENT)
4614 expect("label identifier");
4615 label_push(&local_label_stack, tok, LABEL_DECLARED);
4616 next();
4617 if (tok == ',') {
4618 next();
4619 } else {
4620 skip(';');
4621 break;
4625 while (tok != '}') {
4626 label_or_decl(VT_LOCAL);
4627 if (tok != '}') {
4628 if (is_expr)
4629 vpop();
4630 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4633 /* pop locally defined labels */
4634 label_pop(&local_label_stack, llabel);
4635 if(is_expr) {
4636 /* XXX: this solution makes only valgrind happy...
4637 triggered by gcc.c-torture/execute/20000917-1.c */
4638 Sym *p;
4639 switch(vtop->type.t & VT_BTYPE) {
4640 case VT_PTR:
4641 case VT_STRUCT:
4642 case VT_ENUM:
4643 case VT_FUNC:
4644 for(p=vtop->type.ref;p;p=p->prev)
4645 if(p->prev==s)
4646 tcc_error("unsupported expression type");
4649 /* pop locally defined symbols */
4650 scope_stack_bottom = scope_stack_bottom->next;
4651 sym_pop(&local_stack, s);
4653 /* Pop VLA frames and restore stack pointer if required */
4654 if (saved_vla_sp_loc != &vla_sp_root_loc)
4655 *saved_vla_sp_loc = block_vla_sp_loc;
4656 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4657 vla_sp_loc = saved_vla_sp_loc;
4658 gen_vla_sp_restore(*vla_sp_loc);
4660 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4662 next();
4663 } else if (tok == TOK_RETURN) {
4664 next();
4665 if (tok != ';') {
4666 gexpr();
4667 gen_assign_cast(&func_vt);
4668 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4669 CType type, ret_type;
4670 int ret_align, ret_nregs;
4671 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4672 &ret_align);
4673 if (0 == ret_nregs) {
4674 /* if returning structure, must copy it to implicit
4675 first pointer arg location */
4676 type = func_vt;
4677 mk_pointer(&type);
4678 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4679 indir();
4680 vswap();
4681 /* copy structure value to pointer */
4682 vstore();
4683 } else {
4684 /* returning structure packed into registers */
4685 int r, size, addr, align;
4686 size = type_size(&func_vt,&align);
4687 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4688 && (align & (ret_align-1))) {
4689 loc = (loc - size) & -align;
4690 addr = loc;
4691 type = func_vt;
4692 vset(&type, VT_LOCAL | VT_LVAL, addr);
4693 vswap();
4694 vstore();
4695 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4697 vtop->type = ret_type;
4698 if (is_float(ret_type.t))
4699 r = rc_fret(ret_type.t);
4700 else
4701 r = RC_IRET;
4703 for (;;) {
4704 gv(r);
4705 if (--ret_nregs == 0)
4706 break;
4707 /* We assume that when a structure is returned in multiple
4708 registers, their classes are consecutive values of the
4709 suite s(n) = 2^n */
4710 r <<= 1;
4711 /* XXX: compatible with arm only: ret_align == register_size */
4712 vtop->c.i += ret_align;
4713 vtop->r = VT_LOCAL | VT_LVAL;
4716 } else if (is_float(func_vt.t)) {
4717 gv(rc_fret(func_vt.t));
4718 } else {
4719 gv(RC_IRET);
4721 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4723 skip(';');
4724 rsym = gjmp(rsym); /* jmp */
4725 } else if (tok == TOK_BREAK) {
4726 /* compute jump */
4727 if (!bsym)
4728 tcc_error("cannot break");
4729 *bsym = gjmp(*bsym);
4730 next();
4731 skip(';');
4732 } else if (tok == TOK_CONTINUE) {
4733 /* compute jump */
4734 if (!csym)
4735 tcc_error("cannot continue");
4736 *csym = gjmp(*csym);
4737 next();
4738 skip(';');
4739 } else if (tok == TOK_FOR) {
4740 int e;
4741 next();
4742 skip('(');
4743 s = local_stack;
4744 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4745 frame_bottom->next = scope_stack_bottom;
4746 scope_stack_bottom = frame_bottom;
4747 if (tok != ';') {
4748 /* c99 for-loop init decl? */
4749 if (!decl0(VT_LOCAL, 1)) {
4750 /* no, regular for-loop init expr */
4751 gexpr();
4752 vpop();
4755 skip(';');
4756 d = ind;
4757 c = ind;
4758 a = 0;
4759 b = 0;
4760 if (tok != ';') {
4761 gexpr();
4762 a = gvtst(1, 0);
4764 skip(';');
4765 if (tok != ')') {
4766 e = gjmp(0);
4767 c = ind;
4768 gexpr();
4769 vpop();
4770 gjmp_addr(d);
4771 gsym(e);
4773 skip(')');
4774 block(&a, &b, case_sym, def_sym, case_reg, 0);
4775 gjmp_addr(c);
4776 gsym(a);
4777 gsym_addr(b, c);
4778 scope_stack_bottom = scope_stack_bottom->next;
4779 sym_pop(&local_stack, s);
4780 } else
4781 if (tok == TOK_DO) {
4782 next();
4783 a = 0;
4784 b = 0;
4785 d = ind;
4786 block(&a, &b, case_sym, def_sym, case_reg, 0);
4787 skip(TOK_WHILE);
4788 skip('(');
4789 gsym(b);
4790 gexpr();
4791 c = gvtst(0, 0);
4792 gsym_addr(c, d);
4793 skip(')');
4794 gsym(a);
4795 skip(';');
4796 } else
4797 if (tok == TOK_SWITCH) {
4798 next();
4799 skip('(');
4800 gexpr();
4801 /* XXX: other types than integer */
4802 case_reg = gv(RC_INT);
4803 vpop();
4804 skip(')');
4805 a = 0;
4806 b = gjmp(0); /* jump to first case */
4807 c = 0;
4808 block(&a, csym, &b, &c, case_reg, 0);
4809 /* if no default, jmp after switch */
4810 if (c == 0)
4811 c = ind;
4812 /* default label */
4813 gsym_addr(b, c);
4814 /* break label */
4815 gsym(a);
4816 } else
4817 if (tok == TOK_CASE) {
4818 int v1, v2;
4819 if (!case_sym)
4820 expect("switch");
4821 next();
4822 v1 = expr_const();
4823 v2 = v1;
4824 if (gnu_ext && tok == TOK_DOTS) {
4825 next();
4826 v2 = expr_const();
4827 if (v2 < v1)
4828 tcc_warning("empty case range");
4830 /* since a case is like a label, we must skip it with a jmp */
4831 b = gjmp(0);
4832 gsym(*case_sym);
4833 vseti(case_reg, 0);
4834 vpushi(v1);
4835 if (v1 == v2) {
4836 gen_op(TOK_EQ);
4837 *case_sym = gtst(1, 0);
4838 } else {
4839 gen_op(TOK_GE);
4840 *case_sym = gtst(1, 0);
4841 vseti(case_reg, 0);
4842 vpushi(v2);
4843 gen_op(TOK_LE);
4844 *case_sym = gtst(1, *case_sym);
4846 gsym(b);
4847 skip(':');
4848 is_expr = 0;
4849 goto block_after_label;
4850 } else
4851 if (tok == TOK_DEFAULT) {
4852 next();
4853 skip(':');
4854 if (!def_sym)
4855 expect("switch");
4856 if (*def_sym)
4857 tcc_error("too many 'default'");
4858 *def_sym = ind;
4859 is_expr = 0;
4860 goto block_after_label;
4861 } else
4862 if (tok == TOK_GOTO) {
4863 next();
4864 if (tok == '*' && gnu_ext) {
4865 /* computed goto */
4866 next();
4867 gexpr();
4868 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4869 expect("pointer");
4870 ggoto();
4871 } else if (tok >= TOK_UIDENT) {
4872 s = label_find(tok);
4873 /* put forward definition if needed */
4874 if (!s) {
4875 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4876 } else {
4877 if (s->r == LABEL_DECLARED)
4878 s->r = LABEL_FORWARD;
4880 /* label already defined */
4881 if (vla_flags & VLA_IN_SCOPE) {
4882 /* If VLAs are in use, save the current stack pointer and
4883 reset the stack pointer to what it was at function entry
4884 (label will restore stack pointer in inner scopes) */
4885 vla_sp_save();
4886 gen_vla_sp_restore(vla_sp_root_loc);
4888 if (s->r & LABEL_FORWARD)
4889 s->jnext = gjmp(s->jnext);
4890 else
4891 gjmp_addr(s->jnext);
4892 next();
4893 } else {
4894 expect("label identifier");
4896 skip(';');
4897 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4898 asm_instr();
4899 } else {
4900 b = is_label();
4901 if (b) {
4902 /* label case */
4903 if (vla_flags & VLA_IN_SCOPE) {
4904 /* save/restore stack pointer across label
4905 this is a no-op when combined with the load immediately
4906 after the label unless we arrive via goto */
4907 vla_sp_save();
4909 s = label_find(b);
4910 if (s) {
4911 if (s->r == LABEL_DEFINED)
4912 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4913 gsym(s->jnext);
4914 s->r = LABEL_DEFINED;
4915 } else {
4916 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4918 s->jnext = ind;
4919 if (vla_flags & VLA_IN_SCOPE) {
4920 gen_vla_sp_restore(*vla_sp_loc);
4921 vla_flags |= VLA_NEED_NEW_FRAME;
4923 /* we accept this, but it is a mistake */
4924 block_after_label:
4925 if (tok == '}') {
4926 tcc_warning("deprecated use of label at end of compound statement");
4927 } else {
4928 if (is_expr)
4929 vpop();
4930 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4932 } else {
4933 /* expression case */
4934 if (tok != ';') {
4935 if (is_expr) {
4936 vpop();
4937 gexpr();
4938 } else {
4939 gexpr();
4940 vpop();
4943 skip(';');
4948 /* t is the array or struct type. c is the array or struct
4949 address. cur_index/cur_field is the pointer to the current
4950 value. 'size_only' is true if only size info is needed (only used
4951 in arrays) */
4952 static void decl_designator(CType *type, Section *sec, unsigned long c,
4953 int *cur_index, Sym **cur_field,
4954 int size_only)
4956 Sym *s, *f;
4957 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4958 CType type1;
4960 notfirst = 0;
4961 elem_size = 0;
4962 nb_elems = 1;
4963 if (gnu_ext && (l = is_label()) != 0)
4964 goto struct_field;
4965 while (tok == '[' || tok == '.') {
4966 if (tok == '[') {
4967 if (!(type->t & VT_ARRAY))
4968 expect("array type");
4969 s = type->ref;
4970 next();
4971 index = expr_const();
4972 if (index < 0 || (s->c >= 0 && index >= s->c))
4973 expect("invalid index");
4974 if (tok == TOK_DOTS && gnu_ext) {
4975 next();
4976 index_last = expr_const();
4977 if (index_last < 0 ||
4978 (s->c >= 0 && index_last >= s->c) ||
4979 index_last < index)
4980 expect("invalid index");
4981 } else {
4982 index_last = index;
4984 skip(']');
4985 if (!notfirst)
4986 *cur_index = index_last;
4987 type = pointed_type(type);
4988 elem_size = type_size(type, &align);
4989 c += index * elem_size;
4990 /* NOTE: we only support ranges for last designator */
4991 nb_elems = index_last - index + 1;
4992 if (nb_elems != 1) {
4993 notfirst = 1;
4994 break;
4996 } else {
4997 next();
4998 l = tok;
4999 next();
5000 struct_field:
5001 if ((type->t & VT_BTYPE) != VT_STRUCT)
5002 expect("struct/union type");
5003 s = type->ref;
5004 l |= SYM_FIELD;
5005 f = s->next;
5006 while (f) {
5007 if (f->v == l)
5008 break;
5009 f = f->next;
5011 if (!f)
5012 expect("field");
5013 if (!notfirst)
5014 *cur_field = f;
5015 /* XXX: fix this mess by using explicit storage field */
5016 type1 = f->type;
5017 type1.t |= (type->t & ~VT_TYPE);
5018 type = &type1;
5019 c += f->c;
5021 notfirst = 1;
5023 if (notfirst) {
5024 if (tok == '=') {
5025 next();
5026 } else {
5027 if (!gnu_ext)
5028 expect("=");
5030 } else {
5031 if (type->t & VT_ARRAY) {
5032 index = *cur_index;
5033 type = pointed_type(type);
5034 c += index * type_size(type, &align);
5035 } else {
5036 f = *cur_field;
5037 if (!f)
5038 tcc_error("too many field init");
5039 /* XXX: fix this mess by using explicit storage field */
5040 type1 = f->type;
5041 type1.t |= (type->t & ~VT_TYPE);
5042 type = &type1;
5043 c += f->c;
5046 decl_initializer(type, sec, c, 0, size_only);
5048 /* XXX: make it more general */
5049 if (!size_only && nb_elems > 1) {
5050 unsigned long c_end;
5051 uint8_t *src, *dst;
5052 int i;
5054 if (!sec)
5055 tcc_error("range init not supported yet for dynamic storage");
5056 c_end = c + nb_elems * elem_size;
5057 if (c_end > sec->data_allocated)
5058 section_realloc(sec, c_end);
5059 src = sec->data + c;
5060 dst = src;
5061 for(i = 1; i < nb_elems; i++) {
5062 dst += elem_size;
5063 memcpy(dst, src, elem_size);
5068 #define EXPR_VAL 0
5069 #define EXPR_CONST 1
5070 #define EXPR_ANY 2
5072 /* store a value or an expression directly in global data or in local array */
5073 static void init_putv(CType *type, Section *sec, unsigned long c,
5074 int v, int expr_type)
5076 int saved_global_expr, bt, bit_pos, bit_size;
5077 void *ptr;
5078 unsigned long long bit_mask;
5079 CType dtype;
5081 switch(expr_type) {
5082 case EXPR_VAL:
5083 vpushi(v);
5084 break;
5085 case EXPR_CONST:
5086 /* compound literals must be allocated globally in this case */
5087 saved_global_expr = global_expr;
5088 global_expr = 1;
5089 expr_const1();
5090 global_expr = saved_global_expr;
5091 /* NOTE: symbols are accepted */
5092 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5093 tcc_error("initializer element is not constant");
5094 break;
5095 case EXPR_ANY:
5096 expr_eq();
5097 break;
5100 dtype = *type;
5101 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5103 if (sec) {
5104 /* XXX: not portable */
5105 /* XXX: generate error if incorrect relocation */
5106 gen_assign_cast(&dtype);
5107 bt = type->t & VT_BTYPE;
5108 /* we'll write at most 12 bytes */
5109 if (c + 12 > sec->data_allocated) {
5110 section_realloc(sec, c + 12);
5112 ptr = sec->data + c;
5113 /* XXX: make code faster ? */
5114 if (!(type->t & VT_BITFIELD)) {
5115 bit_pos = 0;
5116 bit_size = 32;
5117 bit_mask = -1LL;
5118 } else {
5119 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5120 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5121 bit_mask = (1LL << bit_size) - 1;
5123 if ((vtop->r & VT_SYM) &&
5124 (bt == VT_BYTE ||
5125 bt == VT_SHORT ||
5126 bt == VT_DOUBLE ||
5127 bt == VT_LDOUBLE ||
5128 bt == VT_LLONG ||
5129 (bt == VT_INT && bit_size != 32)))
5130 tcc_error("initializer element is not computable at load time");
5131 switch(bt) {
5132 case VT_BOOL:
5133 vtop->c.i = (vtop->c.i != 0);
5134 case VT_BYTE:
5135 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5136 break;
5137 case VT_SHORT:
5138 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5139 break;
5140 case VT_DOUBLE:
5141 *(double *)ptr = vtop->c.d;
5142 break;
5143 case VT_LDOUBLE:
5144 *(long double *)ptr = vtop->c.ld;
5145 break;
5146 case VT_LLONG:
5147 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5148 break;
5149 case VT_PTR:
5150 if (vtop->r & VT_SYM) {
5151 greloc(sec, vtop->sym, c, R_DATA_PTR);
5153 *(addr_t *)ptr |= (vtop->c.ull & bit_mask) << bit_pos;
5154 break;
5155 default:
5156 if (vtop->r & VT_SYM) {
5157 greloc(sec, vtop->sym, c, R_DATA_PTR);
5159 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5160 break;
5162 vtop--;
5163 } else {
5164 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5165 vswap();
5166 vstore();
5167 vpop();
5171 /* put zeros for variable based init */
5172 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5174 if (sec) {
5175 /* nothing to do because globals are already set to zero */
5176 } else {
5177 vpush_global_sym(&func_old_type, TOK_memset);
5178 vseti(VT_LOCAL, c);
5179 #ifdef TCC_TARGET_ARM
5180 vpushs(size);
5181 vpushi(0);
5182 #else
5183 vpushi(0);
5184 vpushs(size);
5185 #endif
5186 gfunc_call(3);
5190 /* 't' contains the type and storage info. 'c' is the offset of the
5191 object in section 'sec'. If 'sec' is NULL, it means stack based
5192 allocation. 'first' is true if array '{' must be read (multi
5193 dimension implicit array init handling). 'size_only' is true if
5194 size only evaluation is wanted (only for arrays). */
5195 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5196 int first, int size_only)
5198 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5199 int size1, align1, expr_type;
5200 Sym *s, *f;
5201 CType *t1;
5203 if (type->t & VT_VLA) {
5204 int a;
5206 /* save current stack pointer */
5207 if (vla_flags & VLA_NEED_NEW_FRAME) {
5208 vla_sp_save();
5209 vla_flags = VLA_IN_SCOPE;
5210 vla_sp_loc = &vla_sp_loc_tmp;
5213 vla_runtime_type_size(type, &a);
5214 gen_vla_alloc(type, a);
5215 vset(type, VT_LOCAL|VT_LVAL, c);
5216 vswap();
5217 vstore();
5218 vpop();
5219 } else if (type->t & VT_ARRAY) {
5220 s = type->ref;
5221 n = s->c;
5222 array_length = 0;
5223 t1 = pointed_type(type);
5224 size1 = type_size(t1, &align1);
5226 no_oblock = 1;
5227 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5228 tok == '{') {
5229 if (tok != '{')
5230 tcc_error("character array initializer must be a literal,"
5231 " optionally enclosed in braces");
5232 skip('{');
5233 no_oblock = 0;
5236 /* only parse strings here if correct type (otherwise: handle
5237 them as ((w)char *) expressions */
5238 if ((tok == TOK_LSTR &&
5239 #ifdef TCC_TARGET_PE
5240 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5241 #else
5242 (t1->t & VT_BTYPE) == VT_INT
5243 #endif
5244 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5245 while (tok == TOK_STR || tok == TOK_LSTR) {
5246 int cstr_len, ch;
5247 CString *cstr;
5249 cstr = tokc.cstr;
5250 /* compute maximum number of chars wanted */
5251 if (tok == TOK_STR)
5252 cstr_len = cstr->size;
5253 else
5254 cstr_len = cstr->size / sizeof(nwchar_t);
5255 cstr_len--;
5256 nb = cstr_len;
5257 if (n >= 0 && nb > (n - array_length))
5258 nb = n - array_length;
5259 if (!size_only) {
5260 if (cstr_len > nb)
5261 tcc_warning("initializer-string for array is too long");
5262 /* in order to go faster for common case (char
5263 string in global variable, we handle it
5264 specifically */
5265 if (sec && tok == TOK_STR && size1 == 1) {
5266 memcpy(sec->data + c + array_length, cstr->data, nb);
5267 } else {
5268 for(i=0;i<nb;i++) {
5269 if (tok == TOK_STR)
5270 ch = ((unsigned char *)cstr->data)[i];
5271 else
5272 ch = ((nwchar_t *)cstr->data)[i];
5273 init_putv(t1, sec, c + (array_length + i) * size1,
5274 ch, EXPR_VAL);
5278 array_length += nb;
5279 next();
5281 /* only add trailing zero if enough storage (no
5282 warning in this case since it is standard) */
5283 if (n < 0 || array_length < n) {
5284 if (!size_only) {
5285 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5287 array_length++;
5289 } else {
5290 index = 0;
5291 while (tok != '}') {
5292 decl_designator(type, sec, c, &index, NULL, size_only);
5293 if (n >= 0 && index >= n)
5294 tcc_error("index too large");
5295 /* must put zero in holes (note that doing it that way
5296 ensures that it even works with designators) */
5297 if (!size_only && array_length < index) {
5298 init_putz(t1, sec, c + array_length * size1,
5299 (index - array_length) * size1);
5301 index++;
5302 if (index > array_length)
5303 array_length = index;
5304 /* special test for multi dimensional arrays (may not
5305 be strictly correct if designators are used at the
5306 same time) */
5307 if (index >= n && no_oblock)
5308 break;
5309 if (tok == '}')
5310 break;
5311 skip(',');
5314 if (!no_oblock)
5315 skip('}');
5316 /* put zeros at the end */
5317 if (!size_only && n >= 0 && array_length < n) {
5318 init_putz(t1, sec, c + array_length * size1,
5319 (n - array_length) * size1);
5321 /* patch type size if needed */
5322 if (n < 0)
5323 s->c = array_length;
5324 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5325 (sec || !first || tok == '{')) {
5326 int par_count;
5328 /* NOTE: the previous test is a specific case for automatic
5329 struct/union init */
5330 /* XXX: union needs only one init */
5332 /* XXX: this test is incorrect for local initializers
5333 beginning with ( without {. It would be much more difficult
5334 to do it correctly (ideally, the expression parser should
5335 be used in all cases) */
5336 par_count = 0;
5337 if (tok == '(') {
5338 AttributeDef ad1;
5339 CType type1;
5340 next();
5341 while (tok == '(') {
5342 par_count++;
5343 next();
5345 if (!parse_btype(&type1, &ad1))
5346 expect("cast");
5347 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5348 #if 0
5349 if (!is_assignable_types(type, &type1))
5350 tcc_error("invalid type for cast");
5351 #endif
5352 skip(')');
5354 no_oblock = 1;
5355 if (first || tok == '{') {
5356 skip('{');
5357 no_oblock = 0;
5359 s = type->ref;
5360 f = s->next;
5361 array_length = 0;
5362 index = 0;
5363 n = s->c;
5364 while (tok != '}') {
5365 decl_designator(type, sec, c, NULL, &f, size_only);
5366 index = f->c;
5367 if (!size_only && array_length < index) {
5368 init_putz(type, sec, c + array_length,
5369 index - array_length);
5371 index = index + type_size(&f->type, &align1);
5372 if (index > array_length)
5373 array_length = index;
5375 /* gr: skip fields from same union - ugly. */
5376 while (f->next) {
5377 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5378 /* test for same offset */
5379 if (f->next->c != f->c)
5380 break;
5381 /* if yes, test for bitfield shift */
5382 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5383 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5384 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5385 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5386 if (bit_pos_1 != bit_pos_2)
5387 break;
5389 f = f->next;
5392 f = f->next;
5393 if (no_oblock && f == NULL)
5394 break;
5395 if (tok == '}')
5396 break;
5397 skip(',');
5399 /* put zeros at the end */
5400 if (!size_only && array_length < n) {
5401 init_putz(type, sec, c + array_length,
5402 n - array_length);
5404 if (!no_oblock)
5405 skip('}');
5406 while (par_count) {
5407 skip(')');
5408 par_count--;
5410 } else if (tok == '{') {
5411 next();
5412 decl_initializer(type, sec, c, first, size_only);
5413 skip('}');
5414 } else if (size_only) {
5415 /* just skip expression */
5416 parlevel = parlevel1 = 0;
5417 while ((parlevel > 0 || parlevel1 > 0 ||
5418 (tok != '}' && tok != ',')) && tok != -1) {
5419 if (tok == '(')
5420 parlevel++;
5421 else if (tok == ')')
5422 parlevel--;
5423 else if (tok == '{')
5424 parlevel1++;
5425 else if (tok == '}')
5426 parlevel1--;
5427 next();
5429 } else {
5430 /* currently, we always use constant expression for globals
5431 (may change for scripting case) */
5432 expr_type = EXPR_CONST;
5433 if (!sec)
5434 expr_type = EXPR_ANY;
5435 init_putv(type, sec, c, 0, expr_type);
5439 /* parse an initializer for type 't' if 'has_init' is non zero, and
5440 allocate space in local or global data space ('r' is either
5441 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5442 variable 'v' with an associated name represented by 'asm_label' of
5443 scope 'scope' is declared before initializers are parsed. If 'v' is
5444 zero, then a reference to the new object is put in the value stack.
5445 If 'has_init' is 2, a special parsing is done to handle string
5446 constants. */
5447 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5448 int has_init, int v, char *asm_label,
5449 int scope)
5451 int size, align, addr, data_offset;
5452 int level;
5453 ParseState saved_parse_state = {0};
5454 TokenString init_str;
5455 Section *sec;
5456 Sym *flexible_array;
5458 flexible_array = NULL;
5459 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5460 Sym *field = type->ref->next;
5461 if (field) {
5462 while (field->next)
5463 field = field->next;
5464 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5465 flexible_array = field;
5469 size = type_size(type, &align);
5470 /* If unknown size, we must evaluate it before
5471 evaluating initializers because
5472 initializers can generate global data too
5473 (e.g. string pointers or ISOC99 compound
5474 literals). It also simplifies local
5475 initializers handling */
5476 tok_str_new(&init_str);
5477 if (size < 0 || (flexible_array && has_init)) {
5478 if (!has_init)
5479 tcc_error("unknown type size");
5480 /* get all init string */
5481 if (has_init == 2) {
5482 /* only get strings */
5483 while (tok == TOK_STR || tok == TOK_LSTR) {
5484 tok_str_add_tok(&init_str);
5485 next();
5487 } else {
5488 level = 0;
5489 while (level > 0 || (tok != ',' && tok != ';')) {
5490 if (tok < 0)
5491 tcc_error("unexpected end of file in initializer");
5492 tok_str_add_tok(&init_str);
5493 if (tok == '{')
5494 level++;
5495 else if (tok == '}') {
5496 level--;
5497 if (level <= 0) {
5498 next();
5499 break;
5502 next();
5505 tok_str_add(&init_str, -1);
5506 tok_str_add(&init_str, 0);
5508 /* compute size */
5509 save_parse_state(&saved_parse_state);
5511 macro_ptr = init_str.str;
5512 next();
5513 decl_initializer(type, NULL, 0, 1, 1);
5514 /* prepare second initializer parsing */
5515 macro_ptr = init_str.str;
5516 next();
5518 /* if still unknown size, error */
5519 size = type_size(type, &align);
5520 if (size < 0)
5521 tcc_error("unknown type size");
5523 if (flexible_array)
5524 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5525 /* take into account specified alignment if bigger */
5526 if (ad->a.aligned) {
5527 if (ad->a.aligned > align)
5528 align = ad->a.aligned;
5529 } else if (ad->a.packed) {
5530 align = 1;
5532 if ((r & VT_VALMASK) == VT_LOCAL) {
5533 sec = NULL;
5534 #ifdef CONFIG_TCC_BCHECK
5535 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5536 loc--;
5538 #endif
5539 loc = (loc - size) & -align;
5540 addr = loc;
5541 #ifdef CONFIG_TCC_BCHECK
5542 /* handles bounds */
5543 /* XXX: currently, since we do only one pass, we cannot track
5544 '&' operators, so we add only arrays */
5545 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5546 unsigned long *bounds_ptr;
5547 /* add padding between regions */
5548 loc--;
5549 /* then add local bound info */
5550 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5551 bounds_ptr[0] = addr;
5552 bounds_ptr[1] = size;
5554 #endif
5555 if (v) {
5556 /* local variable */
5557 sym_push(v, type, r, addr);
5558 } else {
5559 /* push local reference */
5560 vset(type, r, addr);
5562 } else {
5563 Sym *sym;
5565 sym = NULL;
5566 if (v && scope == VT_CONST) {
5567 /* see if the symbol was already defined */
5568 sym = sym_find(v);
5569 if (sym) {
5570 if (!is_compatible_types(&sym->type, type))
5571 tcc_error("incompatible types for redefinition of '%s'",
5572 get_tok_str(v, NULL));
5573 if (sym->type.t & VT_EXTERN) {
5574 /* if the variable is extern, it was not allocated */
5575 sym->type.t &= ~VT_EXTERN;
5576 /* set array size if it was ommited in extern
5577 declaration */
5578 if ((sym->type.t & VT_ARRAY) &&
5579 sym->type.ref->c < 0 &&
5580 type->ref->c >= 0)
5581 sym->type.ref->c = type->ref->c;
5582 } else {
5583 /* we accept several definitions of the same
5584 global variable. this is tricky, because we
5585 must play with the SHN_COMMON type of the symbol */
5586 /* XXX: should check if the variable was already
5587 initialized. It is incorrect to initialized it
5588 twice */
5589 /* no init data, we won't add more to the symbol */
5590 if (!has_init)
5591 goto no_alloc;
5596 /* allocate symbol in corresponding section */
5597 sec = ad->section;
5598 if (!sec) {
5599 if (has_init)
5600 sec = data_section;
5601 else if (tcc_state->nocommon)
5602 sec = bss_section;
5604 if (sec) {
5605 data_offset = sec->data_offset;
5606 data_offset = (data_offset + align - 1) & -align;
5607 addr = data_offset;
5608 /* very important to increment global pointer at this time
5609 because initializers themselves can create new initializers */
5610 data_offset += size;
5611 #ifdef CONFIG_TCC_BCHECK
5612 /* add padding if bound check */
5613 if (tcc_state->do_bounds_check)
5614 data_offset++;
5615 #endif
5616 sec->data_offset = data_offset;
5617 /* allocate section space to put the data */
5618 if (sec->sh_type != SHT_NOBITS &&
5619 data_offset > sec->data_allocated)
5620 section_realloc(sec, data_offset);
5621 /* align section if needed */
5622 if (align > sec->sh_addralign)
5623 sec->sh_addralign = align;
5624 } else {
5625 addr = 0; /* avoid warning */
5628 if (v) {
5629 if (scope != VT_CONST || !sym) {
5630 sym = sym_push(v, type, r | VT_SYM, 0);
5631 sym->asm_label = asm_label;
5633 /* update symbol definition */
5634 if (sec) {
5635 put_extern_sym(sym, sec, addr, size);
5636 } else {
5637 ElfW(Sym) *esym;
5638 /* put a common area */
5639 put_extern_sym(sym, NULL, align, size);
5640 /* XXX: find a nicer way */
5641 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5642 esym->st_shndx = SHN_COMMON;
5644 } else {
5645 /* push global reference */
5646 sym = get_sym_ref(type, sec, addr, size);
5647 vpushsym(type, sym);
5649 /* patch symbol weakness */
5650 if (type->t & VT_WEAK)
5651 weaken_symbol(sym);
5652 #ifdef CONFIG_TCC_BCHECK
5653 /* handles bounds now because the symbol must be defined
5654 before for the relocation */
5655 if (tcc_state->do_bounds_check) {
5656 unsigned long *bounds_ptr;
5658 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5659 /* then add global bound info */
5660 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5661 bounds_ptr[0] = 0; /* relocated */
5662 bounds_ptr[1] = size;
5664 #endif
5666 if (has_init || (type->t & VT_VLA)) {
5667 decl_initializer(type, sec, addr, 1, 0);
5668 /* restore parse state if needed */
5669 if (init_str.str) {
5670 tok_str_free(init_str.str);
5671 restore_parse_state(&saved_parse_state);
5673 /* patch flexible array member size back to -1, */
5674 /* for possible subsequent similar declarations */
5675 if (flexible_array)
5676 flexible_array->type.ref->c = -1;
5678 no_alloc: ;
5681 static void put_func_debug(Sym *sym)
5683 char buf[512];
5685 /* stabs info */
5686 /* XXX: we put here a dummy type */
5687 snprintf(buf, sizeof(buf), "%s:%c1",
5688 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5689 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5690 cur_text_section, sym->c);
5691 /* //gr gdb wants a line at the function */
5692 put_stabn(N_SLINE, 0, file->line_num, 0);
5693 last_ind = 0;
5694 last_line_num = 0;
5697 /* parse an old style function declaration list */
5698 /* XXX: check multiple parameter */
5699 static void func_decl_list(Sym *func_sym)
5701 AttributeDef ad;
5702 int v;
5703 Sym *s;
5704 CType btype, type;
5706 /* parse each declaration */
5707 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5708 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5709 if (!parse_btype(&btype, &ad))
5710 expect("declaration list");
5711 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5712 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5713 tok == ';') {
5714 /* we accept no variable after */
5715 } else {
5716 for(;;) {
5717 type = btype;
5718 type_decl(&type, &ad, &v, TYPE_DIRECT);
5719 /* find parameter in function parameter list */
5720 s = func_sym->next;
5721 while (s != NULL) {
5722 if ((s->v & ~SYM_FIELD) == v)
5723 goto found;
5724 s = s->next;
5726 tcc_error("declaration for parameter '%s' but no such parameter",
5727 get_tok_str(v, NULL));
5728 found:
5729 /* check that no storage specifier except 'register' was given */
5730 if (type.t & VT_STORAGE)
5731 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5732 convert_parameter_type(&type);
5733 /* we can add the type (NOTE: it could be local to the function) */
5734 s->type = type;
5735 /* accept other parameters */
5736 if (tok == ',')
5737 next();
5738 else
5739 break;
5742 skip(';');
5746 /* parse a function defined by symbol 'sym' and generate its code in
5747 'cur_text_section' */
5748 static void gen_function(Sym *sym)
5750 int saved_nocode_wanted = nocode_wanted;
5751 nocode_wanted = 0;
5752 ind = cur_text_section->data_offset;
5753 /* NOTE: we patch the symbol size later */
5754 put_extern_sym(sym, cur_text_section, ind, 0);
5755 funcname = get_tok_str(sym->v, NULL);
5756 func_ind = ind;
5757 /* Initialize VLA state */
5758 vla_sp_loc = &vla_sp_root_loc;
5759 vla_flags = VLA_NEED_NEW_FRAME;
5760 /* put debug symbol */
5761 if (tcc_state->do_debug)
5762 put_func_debug(sym);
5763 /* push a dummy symbol to enable local sym storage */
5764 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5765 gfunc_prolog(&sym->type);
5766 rsym = 0;
5767 block(NULL, NULL, NULL, NULL, 0, 0);
5768 gsym(rsym);
5769 gfunc_epilog();
5770 cur_text_section->data_offset = ind;
5771 label_pop(&global_label_stack, NULL);
5772 /* reset local stack */
5773 scope_stack_bottom = NULL;
5774 sym_pop(&local_stack, NULL);
5775 /* end of function */
5776 /* patch symbol size */
5777 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5778 ind - func_ind;
5779 /* patch symbol weakness (this definition overrules any prototype) */
5780 if (sym->type.t & VT_WEAK)
5781 weaken_symbol(sym);
5782 if (tcc_state->do_debug) {
5783 put_stabn(N_FUN, 0, 0, ind - func_ind);
5785 /* It's better to crash than to generate wrong code */
5786 cur_text_section = NULL;
5787 funcname = ""; /* for safety */
5788 func_vt.t = VT_VOID; /* for safety */
5789 func_var = 0; /* for safety */
5790 ind = 0; /* for safety */
5791 nocode_wanted = saved_nocode_wanted;
5794 ST_FUNC void gen_inline_functions(void)
5796 Sym *sym;
5797 int *str, inline_generated, i;
5798 struct InlineFunc *fn;
5800 /* iterate while inline function are referenced */
5801 for(;;) {
5802 inline_generated = 0;
5803 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5804 fn = tcc_state->inline_fns[i];
5805 sym = fn->sym;
5806 if (sym && sym->c) {
5807 /* the function was used: generate its code and
5808 convert it to a normal function */
5809 str = fn->token_str;
5810 fn->sym = NULL;
5811 if (file)
5812 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5813 sym->r = VT_SYM | VT_CONST;
5814 sym->type.t &= ~VT_INLINE;
5816 macro_ptr = str;
5817 next();
5818 cur_text_section = text_section;
5819 gen_function(sym);
5820 macro_ptr = NULL; /* fail safe */
5822 inline_generated = 1;
5825 if (!inline_generated)
5826 break;
5828 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5829 fn = tcc_state->inline_fns[i];
5830 str = fn->token_str;
5831 tok_str_free(str);
5833 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5836 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5837 static int decl0(int l, int is_for_loop_init)
5839 int v, has_init, r;
5840 CType type, btype;
5841 Sym *sym;
5842 AttributeDef ad;
5844 while (1) {
5845 if (!parse_btype(&btype, &ad)) {
5846 if (is_for_loop_init)
5847 return 0;
5848 /* skip redundant ';' */
5849 /* XXX: find more elegant solution */
5850 if (tok == ';') {
5851 next();
5852 continue;
5854 if (l == VT_CONST &&
5855 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5856 /* global asm block */
5857 asm_global_instr();
5858 continue;
5860 /* special test for old K&R protos without explicit int
5861 type. Only accepted when defining global data */
5862 if (l == VT_LOCAL || tok < TOK_DEFINE)
5863 break;
5864 btype.t = VT_INT;
5866 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5867 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5868 tok == ';') {
5869 /* we accept no variable after */
5870 next();
5871 continue;
5873 while (1) { /* iterate thru each declaration */
5874 char *asm_label; // associated asm label
5875 type = btype;
5876 type_decl(&type, &ad, &v, TYPE_DIRECT);
5877 #if 0
5879 char buf[500];
5880 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5881 printf("type = '%s'\n", buf);
5883 #endif
5884 if ((type.t & VT_BTYPE) == VT_FUNC) {
5885 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5886 tcc_error("function without file scope cannot be static");
5888 /* if old style function prototype, we accept a
5889 declaration list */
5890 sym = type.ref;
5891 if (sym->c == FUNC_OLD)
5892 func_decl_list(sym);
5895 asm_label = NULL;
5896 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5897 CString astr;
5899 asm_label_instr(&astr);
5900 asm_label = tcc_strdup(astr.data);
5901 cstr_free(&astr);
5903 /* parse one last attribute list, after asm label */
5904 parse_attribute(&ad);
5907 if (ad.a.weak)
5908 type.t |= VT_WEAK;
5909 #ifdef TCC_TARGET_PE
5910 if (ad.a.func_import)
5911 type.t |= VT_IMPORT;
5912 if (ad.a.func_export)
5913 type.t |= VT_EXPORT;
5914 #endif
5915 if (tok == '{') {
5916 if (l == VT_LOCAL)
5917 tcc_error("cannot use local functions");
5918 if ((type.t & VT_BTYPE) != VT_FUNC)
5919 expect("function definition");
5921 /* reject abstract declarators in function definition */
5922 sym = type.ref;
5923 while ((sym = sym->next) != NULL)
5924 if (!(sym->v & ~SYM_FIELD))
5925 expect("identifier");
5927 /* XXX: cannot do better now: convert extern line to static inline */
5928 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5929 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5931 sym = sym_find(v);
5932 if (sym) {
5933 Sym *ref;
5934 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5935 goto func_error1;
5937 ref = sym->type.ref;
5938 if (0 == ref->a.func_proto)
5939 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5941 /* use func_call from prototype if not defined */
5942 if (ref->a.func_call != FUNC_CDECL
5943 && type.ref->a.func_call == FUNC_CDECL)
5944 type.ref->a.func_call = ref->a.func_call;
5946 /* use export from prototype */
5947 if (ref->a.func_export)
5948 type.ref->a.func_export = 1;
5950 /* use static from prototype */
5951 if (sym->type.t & VT_STATIC)
5952 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5954 if (!is_compatible_types(&sym->type, &type)) {
5955 func_error1:
5956 tcc_error("incompatible types for redefinition of '%s'",
5957 get_tok_str(v, NULL));
5959 type.ref->a.func_proto = 0;
5960 /* if symbol is already defined, then put complete type */
5961 sym->type = type;
5962 } else {
5963 /* put function symbol */
5964 sym = global_identifier_push(v, type.t, 0);
5965 sym->type.ref = type.ref;
5968 /* static inline functions are just recorded as a kind
5969 of macro. Their code will be emitted at the end of
5970 the compilation unit only if they are used */
5971 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5972 (VT_INLINE | VT_STATIC)) {
5973 TokenString func_str;
5974 int block_level;
5975 struct InlineFunc *fn;
5976 const char *filename;
5978 tok_str_new(&func_str);
5980 block_level = 0;
5981 for(;;) {
5982 int t;
5983 if (tok == TOK_EOF)
5984 tcc_error("unexpected end of file");
5985 tok_str_add_tok(&func_str);
5986 t = tok;
5987 next();
5988 if (t == '{') {
5989 block_level++;
5990 } else if (t == '}') {
5991 block_level--;
5992 if (block_level == 0)
5993 break;
5996 tok_str_add(&func_str, -1);
5997 tok_str_add(&func_str, 0);
5998 filename = file ? file->filename : "";
5999 fn = tcc_malloc(sizeof *fn + strlen(filename));
6000 strcpy(fn->filename, filename);
6001 fn->sym = sym;
6002 fn->token_str = func_str.str;
6003 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
6005 } else {
6006 /* compute text section */
6007 cur_text_section = ad.section;
6008 if (!cur_text_section)
6009 cur_text_section = text_section;
6010 sym->r = VT_SYM | VT_CONST;
6011 gen_function(sym);
6013 break;
6014 } else {
6015 if (btype.t & VT_TYPEDEF) {
6016 /* save typedefed type */
6017 /* XXX: test storage specifiers ? */
6018 sym = sym_push(v, &type, 0, 0);
6019 sym->a = ad.a;
6020 sym->type.t |= VT_TYPEDEF;
6021 } else {
6022 r = 0;
6023 if ((type.t & VT_BTYPE) == VT_FUNC) {
6024 /* external function definition */
6025 /* specific case for func_call attribute */
6026 ad.a.func_proto = 1;
6027 type.ref->a = ad.a;
6028 } else if (!(type.t & VT_ARRAY)) {
6029 /* not lvalue if array */
6030 r |= lvalue_type(type.t);
6032 has_init = (tok == '=');
6033 if (has_init && (type.t & VT_VLA))
6034 tcc_error("Variable length array cannot be initialized");
6035 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6036 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6037 !has_init && l == VT_CONST && type.ref->c < 0)) {
6038 /* external variable or function */
6039 /* NOTE: as GCC, uninitialized global static
6040 arrays of null size are considered as
6041 extern */
6042 sym = external_sym(v, &type, r, asm_label);
6044 if (type.t & VT_WEAK)
6045 weaken_symbol(sym);
6047 if (ad.alias_target) {
6048 Section tsec;
6049 Elf32_Sym *esym;
6050 Sym *alias_target;
6052 alias_target = sym_find(ad.alias_target);
6053 if (!alias_target || !alias_target->c)
6054 tcc_error("unsupported forward __alias__ attribute");
6055 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6056 tsec.sh_num = esym->st_shndx;
6057 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6059 } else {
6060 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6061 if (type.t & VT_STATIC)
6062 r |= VT_CONST;
6063 else
6064 r |= l;
6065 if (has_init)
6066 next();
6067 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6070 if (tok != ',') {
6071 if (is_for_loop_init)
6072 return 1;
6073 skip(';');
6074 break;
6076 next();
6078 ad.a.aligned = 0;
6081 return 0;
6084 ST_FUNC void decl(int l)
6086 decl0(l, 0);