Allow local redefinition of enumerator
[tinycc.git] / tccgen.c
blob84188ad350a1dc6e36e84e469f3ffd8dbfc25b52
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 memset(&cval, 0, sizeof(CValue));
333 vsetc(type, VT_CONST, &cval);
336 /* push integer constant */
337 ST_FUNC void vpushi(int v)
339 CValue cval;
340 memset(&cval, 0, sizeof(CValue));
341 cval.i = v;
342 vsetc(&int_type, VT_CONST, &cval);
345 /* push a pointer sized constant */
346 static void vpushs(long long v)
348 CValue cval;
349 memset(&cval, 0, sizeof(CValue));
350 if (PTR_SIZE == 4)
351 cval.i = (int)v;
352 else
353 cval.ull = v;
354 vsetc(&size_type, VT_CONST, &cval);
357 /* push arbitrary 64bit constant */
358 void vpush64(int ty, unsigned long long v)
360 CType ctype;
361 CValue cval;
362 memset(&cval, 0, sizeof(CValue));
363 ctype.t = ty;
364 ctype.ref = NULL;
365 cval.ull = v;
366 vsetc(&ctype, VT_CONST, &cval);
369 /* push long long constant */
370 static inline void vpushll(long long v)
372 vpush64(VT_LLONG, v);
375 /* push a symbol value of TYPE */
376 static inline void vpushsym(CType *type, Sym *sym)
378 CValue cval;
379 memset(&cval, 0, sizeof(CValue));
381 cval.ull = 0;
382 vsetc(type, VT_CONST | VT_SYM, &cval);
383 vtop->sym = sym;
386 /* Return a static symbol pointing to a section */
387 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
389 int v;
390 Sym *sym;
392 v = anon_sym++;
393 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
394 sym->type.ref = type->ref;
395 sym->r = VT_CONST | VT_SYM;
396 put_extern_sym(sym, sec, offset, size);
397 return sym;
400 /* push a reference to a section offset by adding a dummy symbol */
401 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
403 vpushsym(type, get_sym_ref(type, sec, offset, size));
406 /* define a new external reference to a symbol 'v' of type 'u' */
407 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
409 Sym *s;
411 s = sym_find(v);
412 if (!s) {
413 /* push forward reference */
414 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
415 s->type.ref = type->ref;
416 s->r = r | VT_CONST | VT_SYM;
418 return s;
421 /* define a new external reference to a symbol 'v' with alternate asm
422 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
423 is no alternate name (most cases) */
424 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
426 Sym *s;
428 s = sym_find(v);
429 if (!s) {
430 /* push forward reference */
431 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
432 s->asm_label = asm_label;
433 s->type.t |= VT_EXTERN;
434 } else if (s->type.ref == func_old_type.ref) {
435 s->type.ref = type->ref;
436 s->r = r | VT_CONST | VT_SYM;
437 s->type.t |= VT_EXTERN;
438 } else if (!is_compatible_types(&s->type, type)) {
439 tcc_error("incompatible types for redefinition of '%s'",
440 get_tok_str(v, NULL));
442 return s;
445 /* push a reference to global symbol v */
446 ST_FUNC void vpush_global_sym(CType *type, int v)
448 vpushsym(type, external_global_sym(v, type, 0));
451 ST_FUNC void vset(CType *type, int r, int v)
453 CValue cval;
454 memset(&cval, 0, sizeof(CValue));
456 cval.i = v;
457 vsetc(type, r, &cval);
460 static void vseti(int r, int v)
462 CType type;
463 type.t = VT_INT;
464 type.ref = 0;
465 vset(&type, r, v);
468 ST_FUNC void vswap(void)
470 SValue tmp;
471 /* cannot let cpu flags if other instruction are generated. Also
472 avoid leaving VT_JMP anywhere except on the top of the stack
473 because it would complicate the code generator. */
474 if (vtop >= vstack) {
475 int v = vtop->r & VT_VALMASK;
476 if (v == VT_CMP || (v & ~1) == VT_JMP)
477 gv(RC_INT);
479 tmp = vtop[0];
480 vtop[0] = vtop[-1];
481 vtop[-1] = tmp;
483 /* XXX: +2% overall speed possible with optimized memswap
485 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
489 ST_FUNC void vpushv(SValue *v)
491 if (vtop >= vstack + (VSTACK_SIZE - 1))
492 tcc_error("memory full (vstack)");
493 vtop++;
494 *vtop = *v;
497 static void vdup(void)
499 vpushv(vtop);
502 /* save r to the memory stack, and mark it as being free */
503 ST_FUNC void save_reg(int r)
505 int l, saved, size, align;
506 SValue *p, sv;
507 CType *type;
509 /* modify all stack values */
510 saved = 0;
511 l = 0;
512 for(p=vstack;p<=vtop;p++) {
513 if ((p->r & VT_VALMASK) == r ||
514 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
515 /* must save value on stack if not already done */
516 if (!saved) {
517 /* NOTE: must reload 'r' because r might be equal to r2 */
518 r = p->r & VT_VALMASK;
519 /* store register in the stack */
520 type = &p->type;
521 if ((p->r & VT_LVAL) ||
522 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
523 #ifdef TCC_TARGET_X86_64
524 type = &char_pointer_type;
525 #else
526 type = &int_type;
527 #endif
528 size = type_size(type, &align);
529 loc = (loc - size) & -align;
530 sv.type.t = type->t;
531 sv.r = VT_LOCAL | VT_LVAL;
532 sv.c.ul = loc;
533 store(r, &sv);
534 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
535 /* x86 specific: need to pop fp register ST0 if saved */
536 if (r == TREG_ST0) {
537 o(0xd8dd); /* fstp %st(0) */
539 #endif
540 #ifndef TCC_TARGET_X86_64
541 /* special long long case */
542 if ((type->t & VT_BTYPE) == VT_LLONG) {
543 sv.c.ul += 4;
544 store(p->r2, &sv);
546 #endif
547 l = loc;
548 saved = 1;
550 /* mark that stack entry as being saved on the stack */
551 if (p->r & VT_LVAL) {
552 /* also clear the bounded flag because the
553 relocation address of the function was stored in
554 p->c.ul */
555 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
556 } else {
557 p->r = lvalue_type(p->type.t) | VT_LOCAL;
559 p->r2 = VT_CONST;
560 p->c.ul = l;
565 #ifdef TCC_TARGET_ARM
566 /* find a register of class 'rc2' with at most one reference on stack.
567 * If none, call get_reg(rc) */
568 ST_FUNC int get_reg_ex(int rc, int rc2)
570 int r;
571 SValue *p;
573 for(r=0;r<NB_REGS;r++) {
574 if (reg_classes[r] & rc2) {
575 int n;
576 n=0;
577 for(p = vstack; p <= vtop; p++) {
578 if ((p->r & VT_VALMASK) == r ||
579 (p->r2 & VT_VALMASK) == r)
580 n++;
582 if (n <= 1)
583 return r;
586 return get_reg(rc);
588 #endif
590 /* find a free register of class 'rc'. If none, save one register */
591 ST_FUNC int get_reg(int rc)
593 int r;
594 SValue *p;
596 /* find a free register */
597 for(r=0;r<NB_REGS;r++) {
598 if (reg_classes[r] & rc) {
599 for(p=vstack;p<=vtop;p++) {
600 if ((p->r & VT_VALMASK) == r ||
601 (p->r2 & VT_VALMASK) == r)
602 goto notfound;
604 return r;
606 notfound: ;
609 /* no register left : free the first one on the stack (VERY
610 IMPORTANT to start from the bottom to ensure that we don't
611 spill registers used in gen_opi()) */
612 for(p=vstack;p<=vtop;p++) {
613 /* look at second register (if long long) */
614 r = p->r2 & VT_VALMASK;
615 if (r < VT_CONST && (reg_classes[r] & rc))
616 goto save_found;
617 r = p->r & VT_VALMASK;
618 if (r < VT_CONST && (reg_classes[r] & rc)) {
619 save_found:
620 save_reg(r);
621 return r;
624 /* Should never comes here */
625 return -1;
628 /* save registers up to (vtop - n) stack entry */
629 ST_FUNC void save_regs(int n)
631 int r;
632 SValue *p, *p1;
633 p1 = vtop - n;
634 for(p = vstack;p <= p1; p++) {
635 r = p->r & VT_VALMASK;
636 if (r < VT_CONST) {
637 save_reg(r);
642 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
643 if needed */
644 static void move_reg(int r, int s, int t)
646 SValue sv;
648 if (r != s) {
649 save_reg(r);
650 sv.type.t = t;
651 sv.type.ref = NULL;
652 sv.r = s;
653 sv.c.ul = 0;
654 load(r, &sv);
658 /* get address of vtop (vtop MUST BE an lvalue) */
659 static void gaddrof(void)
661 if (vtop->r & VT_REF)
662 gv(RC_INT);
663 vtop->r &= ~VT_LVAL;
664 /* tricky: if saved lvalue, then we can go back to lvalue */
665 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
666 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
671 #ifdef CONFIG_TCC_BCHECK
672 /* generate lvalue bound code */
673 static void gbound(void)
675 int lval_type;
676 CType type1;
678 vtop->r &= ~VT_MUSTBOUND;
679 /* if lvalue, then use checking code before dereferencing */
680 if (vtop->r & VT_LVAL) {
681 /* if not VT_BOUNDED value, then make one */
682 if (!(vtop->r & VT_BOUNDED)) {
683 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
684 /* must save type because we must set it to int to get pointer */
685 type1 = vtop->type;
686 vtop->type.t = VT_INT;
687 gaddrof();
688 vpushi(0);
689 gen_bounded_ptr_add();
690 vtop->r |= lval_type;
691 vtop->type = type1;
693 /* then check for dereferencing */
694 gen_bounded_ptr_deref();
697 #endif
699 /* store vtop a register belonging to class 'rc'. lvalues are
700 converted to values. Cannot be used if cannot be converted to
701 register value (such as structures). */
702 ST_FUNC int gv(int rc)
704 int r, bit_pos, bit_size, size, align, i;
705 int rc2;
707 /* NOTE: get_reg can modify vstack[] */
708 if (vtop->type.t & VT_BITFIELD) {
709 CType type;
710 int bits = 32;
711 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
712 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
713 /* remove bit field info to avoid loops */
714 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
715 /* cast to int to propagate signedness in following ops */
716 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
717 type.t = VT_LLONG;
718 bits = 64;
719 } else
720 type.t = VT_INT;
721 if((vtop->type.t & VT_UNSIGNED) ||
722 (vtop->type.t & VT_BTYPE) == VT_BOOL)
723 type.t |= VT_UNSIGNED;
724 gen_cast(&type);
725 /* generate shifts */
726 vpushi(bits - (bit_pos + bit_size));
727 gen_op(TOK_SHL);
728 vpushi(bits - bit_size);
729 /* NOTE: transformed to SHR if unsigned */
730 gen_op(TOK_SAR);
731 r = gv(rc);
732 } else {
733 if (is_float(vtop->type.t) &&
734 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
735 Sym *sym;
736 int *ptr;
737 unsigned long offset;
738 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
739 CValue check;
740 memset(&check, 0, sizeof(CValue));
741 #endif
743 /* XXX: unify with initializers handling ? */
744 /* CPUs usually cannot use float constants, so we store them
745 generically in data segment */
746 size = type_size(&vtop->type, &align);
747 offset = (data_section->data_offset + align - 1) & -align;
748 data_section->data_offset = offset;
749 /* XXX: not portable yet */
750 #if defined(__i386__) || defined(__x86_64__)
751 /* Zero pad x87 tenbyte long doubles */
752 if (size == LDOUBLE_SIZE) {
753 vtop->c.tab[2] &= 0xffff;
754 #if LDOUBLE_SIZE == 16
755 vtop->c.tab[3] = 0;
756 #endif
758 #endif
759 ptr = section_ptr_add(data_section, size);
760 size = size >> 2;
761 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
762 check.d = 1;
763 if(check.tab[0])
764 for(i=0;i<size;i++)
765 ptr[i] = vtop->c.tab[size-1-i];
766 else
767 #endif
768 for(i=0;i<size;i++)
769 ptr[i] = vtop->c.tab[i];
770 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
771 vtop->r |= VT_LVAL | VT_SYM;
772 vtop->sym = sym;
773 vtop->c.ull = 0;
775 #ifdef CONFIG_TCC_BCHECK
776 if (vtop->r & VT_MUSTBOUND)
777 gbound();
778 #endif
780 r = vtop->r & VT_VALMASK;
781 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
782 if (rc == RC_IRET)
783 rc2 = RC_LRET;
784 #ifdef TCC_TARGET_X86_64
785 else if (rc == RC_FRET)
786 rc2 = RC_QRET;
787 #endif
789 /* need to reload if:
790 - constant
791 - lvalue (need to dereference pointer)
792 - already a register, but not in the right class */
793 if (r >= VT_CONST
794 || (vtop->r & VT_LVAL)
795 || !(reg_classes[r] & rc)
796 #ifdef TCC_TARGET_X86_64
797 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
798 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
799 #else
800 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
801 #endif
804 r = get_reg(rc);
805 #ifdef TCC_TARGET_X86_64
806 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
807 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
808 #else
809 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
810 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
811 unsigned long long ll;
812 #endif
813 int r2, original_type;
814 original_type = vtop->type.t;
815 /* two register type load : expand to two words
816 temporarily */
817 #ifndef TCC_TARGET_X86_64
818 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
819 /* load constant */
820 ll = vtop->c.ull;
821 vtop->c.ui = ll; /* first word */
822 load(r, vtop);
823 vtop->r = r; /* save register value */
824 vpushi(ll >> 32); /* second word */
825 } else
826 #endif
827 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
828 (vtop->r & VT_LVAL)) {
829 /* We do not want to modifier the long long
830 pointer here, so the safest (and less
831 efficient) is to save all the other registers
832 in the stack. XXX: totally inefficient. */
833 save_regs(1);
834 /* load from memory */
835 vtop->type.t = load_type;
836 load(r, vtop);
837 vdup();
838 vtop[-1].r = r; /* save register value */
839 /* increment pointer to get second word */
840 vtop->type.t = addr_type;
841 gaddrof();
842 vpushi(load_size);
843 gen_op('+');
844 vtop->r |= VT_LVAL;
845 vtop->type.t = load_type;
846 } else {
847 /* move registers */
848 load(r, vtop);
849 vdup();
850 vtop[-1].r = r; /* save register value */
851 vtop->r = vtop[-1].r2;
853 /* Allocate second register. Here we rely on the fact that
854 get_reg() tries first to free r2 of an SValue. */
855 r2 = get_reg(rc2);
856 load(r2, vtop);
857 vpop();
858 /* write second register */
859 vtop->r2 = r2;
860 vtop->type.t = original_type;
861 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
862 int t1, t;
863 /* lvalue of scalar type : need to use lvalue type
864 because of possible cast */
865 t = vtop->type.t;
866 t1 = t;
867 /* compute memory access type */
868 if (vtop->r & VT_REF)
869 #ifdef TCC_TARGET_X86_64
870 t = VT_PTR;
871 #else
872 t = VT_INT;
873 #endif
874 else if (vtop->r & VT_LVAL_BYTE)
875 t = VT_BYTE;
876 else if (vtop->r & VT_LVAL_SHORT)
877 t = VT_SHORT;
878 if (vtop->r & VT_LVAL_UNSIGNED)
879 t |= VT_UNSIGNED;
880 vtop->type.t = t;
881 load(r, vtop);
882 /* restore wanted type */
883 vtop->type.t = t1;
884 } else {
885 /* one register type load */
886 load(r, vtop);
889 vtop->r = r;
890 #ifdef TCC_TARGET_C67
891 /* uses register pairs for doubles */
892 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
893 vtop->r2 = r+1;
894 #endif
896 return r;
899 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
900 ST_FUNC void gv2(int rc1, int rc2)
902 int v;
904 /* generate more generic register first. But VT_JMP or VT_CMP
905 values must be generated first in all cases to avoid possible
906 reload errors */
907 v = vtop[0].r & VT_VALMASK;
908 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
909 vswap();
910 gv(rc1);
911 vswap();
912 gv(rc2);
913 /* test if reload is needed for first register */
914 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
915 vswap();
916 gv(rc1);
917 vswap();
919 } else {
920 gv(rc2);
921 vswap();
922 gv(rc1);
923 vswap();
924 /* test if reload is needed for first register */
925 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
926 gv(rc2);
931 /* wrapper around RC_FRET to return a register by type */
932 static int rc_fret(int t)
934 #ifdef TCC_TARGET_X86_64
935 if (t == VT_LDOUBLE) {
936 return RC_ST0;
938 #endif
939 return RC_FRET;
942 /* wrapper around REG_FRET to return a register by type */
943 static int reg_fret(int t)
945 #ifdef TCC_TARGET_X86_64
946 if (t == VT_LDOUBLE) {
947 return TREG_ST0;
949 #endif
950 return REG_FRET;
953 /* expand long long on stack in two int registers */
954 static void lexpand(void)
956 int u;
958 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
959 gv(RC_INT);
960 vdup();
961 vtop[0].r = vtop[-1].r2;
962 vtop[0].r2 = VT_CONST;
963 vtop[-1].r2 = VT_CONST;
964 vtop[0].type.t = VT_INT | u;
965 vtop[-1].type.t = VT_INT | u;
968 #ifdef TCC_TARGET_ARM
969 /* expand long long on stack */
970 ST_FUNC void lexpand_nr(void)
972 int u,v;
974 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
975 vdup();
976 vtop->r2 = VT_CONST;
977 vtop->type.t = VT_INT | u;
978 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
979 if (v == VT_CONST) {
980 vtop[-1].c.ui = vtop->c.ull;
981 vtop->c.ui = vtop->c.ull >> 32;
982 vtop->r = VT_CONST;
983 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
984 vtop->c.ui += 4;
985 vtop->r = vtop[-1].r;
986 } else if (v > VT_CONST) {
987 vtop--;
988 lexpand();
989 } else
990 vtop->r = vtop[-1].r2;
991 vtop[-1].r2 = VT_CONST;
992 vtop[-1].type.t = VT_INT | u;
994 #endif
996 /* build a long long from two ints */
997 static void lbuild(int t)
999 gv2(RC_INT, RC_INT);
1000 vtop[-1].r2 = vtop[0].r;
1001 vtop[-1].type.t = t;
1002 vpop();
1005 /* rotate n first stack elements to the bottom
1006 I1 ... In -> I2 ... In I1 [top is right]
1008 ST_FUNC void vrotb(int n)
1010 int i;
1011 SValue tmp;
1013 tmp = vtop[-n + 1];
1014 for(i=-n+1;i!=0;i++)
1015 vtop[i] = vtop[i+1];
1016 vtop[0] = tmp;
1019 /* rotate the n elements before entry e towards the top
1020 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1022 ST_FUNC void vrote(SValue *e, int n)
1024 int i;
1025 SValue tmp;
1027 tmp = *e;
1028 for(i = 0;i < n - 1; i++)
1029 e[-i] = e[-i - 1];
1030 e[-n + 1] = tmp;
1033 /* rotate n first stack elements to the top
1034 I1 ... In -> In I1 ... I(n-1) [top is right]
1036 ST_FUNC void vrott(int n)
1038 vrote(vtop, n);
1041 /* pop stack value */
1042 ST_FUNC void vpop(void)
1044 int v;
1045 v = vtop->r & VT_VALMASK;
1046 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1047 /* for x86, we need to pop the FP stack */
1048 if (v == TREG_ST0 && !nocode_wanted) {
1049 o(0xd8dd); /* fstp %st(0) */
1050 } else
1051 #endif
1052 if (v == VT_JMP || v == VT_JMPI) {
1053 /* need to put correct jump if && or || without test */
1054 gsym(vtop->c.ul);
1056 vtop--;
1059 /* convert stack entry to register and duplicate its value in another
1060 register */
1061 static void gv_dup(void)
1063 int rc, t, r, r1;
1064 SValue sv;
1066 t = vtop->type.t;
1067 if ((t & VT_BTYPE) == VT_LLONG) {
1068 lexpand();
1069 gv_dup();
1070 vswap();
1071 vrotb(3);
1072 gv_dup();
1073 vrotb(4);
1074 /* stack: H L L1 H1 */
1075 lbuild(t);
1076 vrotb(3);
1077 vrotb(3);
1078 vswap();
1079 lbuild(t);
1080 vswap();
1081 } else {
1082 /* duplicate value */
1083 rc = RC_INT;
1084 sv.type.t = VT_INT;
1085 if (is_float(t)) {
1086 rc = RC_FLOAT;
1087 #ifdef TCC_TARGET_X86_64
1088 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1089 rc = RC_ST0;
1091 #endif
1092 sv.type.t = t;
1094 r = gv(rc);
1095 r1 = get_reg(rc);
1096 sv.r = r;
1097 sv.c.ul = 0;
1098 load(r1, &sv); /* move r to r1 */
1099 vdup();
1100 /* duplicates value */
1101 if (r != r1)
1102 vtop->r = r1;
1106 /* Generate value test
1108 * Generate a test for any value (jump, comparison and integers) */
1109 int gvtst(int inv, int t)
1111 int v = vtop->r & VT_VALMASK;
1112 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1113 vpushi(0);
1114 gen_op(TOK_NE);
1116 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1117 /* constant jmp optimization */
1118 if ((vtop->c.i != 0) != inv)
1119 t = gjmp(t);
1120 vtop--;
1121 return t;
1123 return gtst(inv, t);
1126 #ifndef TCC_TARGET_X86_64
1127 /* generate CPU independent (unsigned) long long operations */
1128 static void gen_opl(int op)
1130 int t, a, b, op1, c, i;
1131 int func;
1132 unsigned short reg_iret = REG_IRET;
1133 unsigned short reg_lret = REG_LRET;
1134 SValue tmp;
1136 switch(op) {
1137 case '/':
1138 case TOK_PDIV:
1139 func = TOK___divdi3;
1140 goto gen_func;
1141 case TOK_UDIV:
1142 func = TOK___udivdi3;
1143 goto gen_func;
1144 case '%':
1145 func = TOK___moddi3;
1146 goto gen_mod_func;
1147 case TOK_UMOD:
1148 func = TOK___umoddi3;
1149 gen_mod_func:
1150 #ifdef TCC_ARM_EABI
1151 reg_iret = TREG_R2;
1152 reg_lret = TREG_R3;
1153 #endif
1154 gen_func:
1155 /* call generic long long function */
1156 vpush_global_sym(&func_old_type, func);
1157 vrott(3);
1158 gfunc_call(2);
1159 vpushi(0);
1160 vtop->r = reg_iret;
1161 vtop->r2 = reg_lret;
1162 break;
1163 case '^':
1164 case '&':
1165 case '|':
1166 case '*':
1167 case '+':
1168 case '-':
1169 t = vtop->type.t;
1170 vswap();
1171 lexpand();
1172 vrotb(3);
1173 lexpand();
1174 /* stack: L1 H1 L2 H2 */
1175 tmp = vtop[0];
1176 vtop[0] = vtop[-3];
1177 vtop[-3] = tmp;
1178 tmp = vtop[-2];
1179 vtop[-2] = vtop[-3];
1180 vtop[-3] = tmp;
1181 vswap();
1182 /* stack: H1 H2 L1 L2 */
1183 if (op == '*') {
1184 vpushv(vtop - 1);
1185 vpushv(vtop - 1);
1186 gen_op(TOK_UMULL);
1187 lexpand();
1188 /* stack: H1 H2 L1 L2 ML MH */
1189 for(i=0;i<4;i++)
1190 vrotb(6);
1191 /* stack: ML MH H1 H2 L1 L2 */
1192 tmp = vtop[0];
1193 vtop[0] = vtop[-2];
1194 vtop[-2] = tmp;
1195 /* stack: ML MH H1 L2 H2 L1 */
1196 gen_op('*');
1197 vrotb(3);
1198 vrotb(3);
1199 gen_op('*');
1200 /* stack: ML MH M1 M2 */
1201 gen_op('+');
1202 gen_op('+');
1203 } else if (op == '+' || op == '-') {
1204 /* XXX: add non carry method too (for MIPS or alpha) */
1205 if (op == '+')
1206 op1 = TOK_ADDC1;
1207 else
1208 op1 = TOK_SUBC1;
1209 gen_op(op1);
1210 /* stack: H1 H2 (L1 op L2) */
1211 vrotb(3);
1212 vrotb(3);
1213 gen_op(op1 + 1); /* TOK_xxxC2 */
1214 } else {
1215 gen_op(op);
1216 /* stack: H1 H2 (L1 op L2) */
1217 vrotb(3);
1218 vrotb(3);
1219 /* stack: (L1 op L2) H1 H2 */
1220 gen_op(op);
1221 /* stack: (L1 op L2) (H1 op H2) */
1223 /* stack: L H */
1224 lbuild(t);
1225 break;
1226 case TOK_SAR:
1227 case TOK_SHR:
1228 case TOK_SHL:
1229 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1230 t = vtop[-1].type.t;
1231 vswap();
1232 lexpand();
1233 vrotb(3);
1234 /* stack: L H shift */
1235 c = (int)vtop->c.i;
1236 /* constant: simpler */
1237 /* NOTE: all comments are for SHL. the other cases are
1238 done by swaping words */
1239 vpop();
1240 if (op != TOK_SHL)
1241 vswap();
1242 if (c >= 32) {
1243 /* stack: L H */
1244 vpop();
1245 if (c > 32) {
1246 vpushi(c - 32);
1247 gen_op(op);
1249 if (op != TOK_SAR) {
1250 vpushi(0);
1251 } else {
1252 gv_dup();
1253 vpushi(31);
1254 gen_op(TOK_SAR);
1256 vswap();
1257 } else {
1258 vswap();
1259 gv_dup();
1260 /* stack: H L L */
1261 vpushi(c);
1262 gen_op(op);
1263 vswap();
1264 vpushi(32 - c);
1265 if (op == TOK_SHL)
1266 gen_op(TOK_SHR);
1267 else
1268 gen_op(TOK_SHL);
1269 vrotb(3);
1270 /* stack: L L H */
1271 vpushi(c);
1272 if (op == TOK_SHL)
1273 gen_op(TOK_SHL);
1274 else
1275 gen_op(TOK_SHR);
1276 gen_op('|');
1278 if (op != TOK_SHL)
1279 vswap();
1280 lbuild(t);
1281 } else {
1282 /* XXX: should provide a faster fallback on x86 ? */
1283 switch(op) {
1284 case TOK_SAR:
1285 func = TOK___ashrdi3;
1286 goto gen_func;
1287 case TOK_SHR:
1288 func = TOK___lshrdi3;
1289 goto gen_func;
1290 case TOK_SHL:
1291 func = TOK___ashldi3;
1292 goto gen_func;
1295 break;
1296 default:
1297 /* compare operations */
1298 t = vtop->type.t;
1299 vswap();
1300 lexpand();
1301 vrotb(3);
1302 lexpand();
1303 /* stack: L1 H1 L2 H2 */
1304 tmp = vtop[-1];
1305 vtop[-1] = vtop[-2];
1306 vtop[-2] = tmp;
1307 /* stack: L1 L2 H1 H2 */
1308 /* compare high */
1309 op1 = op;
1310 /* when values are equal, we need to compare low words. since
1311 the jump is inverted, we invert the test too. */
1312 if (op1 == TOK_LT)
1313 op1 = TOK_LE;
1314 else if (op1 == TOK_GT)
1315 op1 = TOK_GE;
1316 else if (op1 == TOK_ULT)
1317 op1 = TOK_ULE;
1318 else if (op1 == TOK_UGT)
1319 op1 = TOK_UGE;
1320 a = 0;
1321 b = 0;
1322 gen_op(op1);
1323 if (op1 != TOK_NE) {
1324 a = gvtst(1, 0);
1326 if (op != TOK_EQ) {
1327 /* generate non equal test */
1328 /* XXX: NOT PORTABLE yet */
1329 if (a == 0) {
1330 b = gvtst(0, 0);
1331 } else {
1332 #if defined(TCC_TARGET_I386)
1333 b = psym(0x850f, 0);
1334 #elif defined(TCC_TARGET_ARM)
1335 b = ind;
1336 o(0x1A000000 | encbranch(ind, 0, 1));
1337 #elif defined(TCC_TARGET_C67)
1338 tcc_error("not implemented");
1339 #else
1340 #error not supported
1341 #endif
1344 /* compare low. Always unsigned */
1345 op1 = op;
1346 if (op1 == TOK_LT)
1347 op1 = TOK_ULT;
1348 else if (op1 == TOK_LE)
1349 op1 = TOK_ULE;
1350 else if (op1 == TOK_GT)
1351 op1 = TOK_UGT;
1352 else if (op1 == TOK_GE)
1353 op1 = TOK_UGE;
1354 gen_op(op1);
1355 a = gvtst(1, a);
1356 gsym(b);
1357 vseti(VT_JMPI, a);
1358 break;
1361 #endif
1363 /* handle integer constant optimizations and various machine
1364 independent opt */
1365 static void gen_opic(int op)
1367 int c1, c2, t1, t2, n;
1368 SValue *v1, *v2;
1369 long long l1, l2;
1370 typedef unsigned long long U;
1372 v1 = vtop - 1;
1373 v2 = vtop;
1374 t1 = v1->type.t & VT_BTYPE;
1375 t2 = v2->type.t & VT_BTYPE;
1377 if (t1 == VT_LLONG)
1378 l1 = v1->c.ll;
1379 else if (v1->type.t & VT_UNSIGNED)
1380 l1 = v1->c.ui;
1381 else
1382 l1 = v1->c.i;
1384 if (t2 == VT_LLONG)
1385 l2 = v2->c.ll;
1386 else if (v2->type.t & VT_UNSIGNED)
1387 l2 = v2->c.ui;
1388 else
1389 l2 = v2->c.i;
1391 /* currently, we cannot do computations with forward symbols */
1392 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1393 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1394 if (c1 && c2) {
1395 switch(op) {
1396 case '+': l1 += l2; break;
1397 case '-': l1 -= l2; break;
1398 case '&': l1 &= l2; break;
1399 case '^': l1 ^= l2; break;
1400 case '|': l1 |= l2; break;
1401 case '*': l1 *= l2; break;
1403 case TOK_PDIV:
1404 case '/':
1405 case '%':
1406 case TOK_UDIV:
1407 case TOK_UMOD:
1408 /* if division by zero, generate explicit division */
1409 if (l2 == 0) {
1410 if (const_wanted)
1411 tcc_error("division by zero in constant");
1412 goto general_case;
1414 switch(op) {
1415 default: l1 /= l2; break;
1416 case '%': l1 %= l2; break;
1417 case TOK_UDIV: l1 = (U)l1 / l2; break;
1418 case TOK_UMOD: l1 = (U)l1 % l2; break;
1420 break;
1421 case TOK_SHL: l1 <<= l2; break;
1422 case TOK_SHR: l1 = (U)l1 >> l2; break;
1423 case TOK_SAR: l1 >>= l2; break;
1424 /* tests */
1425 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1426 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1427 case TOK_EQ: l1 = l1 == l2; break;
1428 case TOK_NE: l1 = l1 != l2; break;
1429 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1430 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1431 case TOK_LT: l1 = l1 < l2; break;
1432 case TOK_GE: l1 = l1 >= l2; break;
1433 case TOK_LE: l1 = l1 <= l2; break;
1434 case TOK_GT: l1 = l1 > l2; break;
1435 /* logical */
1436 case TOK_LAND: l1 = l1 && l2; break;
1437 case TOK_LOR: l1 = l1 || l2; break;
1438 default:
1439 goto general_case;
1441 v1->c.ll = l1;
1442 vtop--;
1443 } else {
1444 /* if commutative ops, put c2 as constant */
1445 if (c1 && (op == '+' || op == '&' || op == '^' ||
1446 op == '|' || op == '*')) {
1447 vswap();
1448 c2 = c1; //c = c1, c1 = c2, c2 = c;
1449 l2 = l1; //l = l1, l1 = l2, l2 = l;
1451 /* Filter out NOP operations like x*1, x-0, x&-1... */
1452 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1453 op == TOK_PDIV) &&
1454 l2 == 1) ||
1455 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1456 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1457 l2 == 0) ||
1458 (op == '&' &&
1459 l2 == -1))) {
1460 /* nothing to do */
1461 vtop--;
1462 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1463 /* try to use shifts instead of muls or divs */
1464 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1465 n = -1;
1466 while (l2) {
1467 l2 >>= 1;
1468 n++;
1470 vtop->c.ll = n;
1471 if (op == '*')
1472 op = TOK_SHL;
1473 else if (op == TOK_PDIV)
1474 op = TOK_SAR;
1475 else
1476 op = TOK_SHR;
1478 goto general_case;
1479 } else if (c2 && (op == '+' || op == '-') &&
1480 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1481 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1482 /* symbol + constant case */
1483 if (op == '-')
1484 l2 = -l2;
1485 vtop--;
1486 vtop->c.ll += l2;
1487 } else {
1488 general_case:
1489 if (!nocode_wanted) {
1490 /* call low level op generator */
1491 if (t1 == VT_LLONG || t2 == VT_LLONG)
1492 gen_opl(op);
1493 else
1494 gen_opi(op);
1495 } else {
1496 vtop--;
1502 /* generate a floating point operation with constant propagation */
1503 static void gen_opif(int op)
1505 int c1, c2;
1506 SValue *v1, *v2;
1507 long double f1, f2;
1509 v1 = vtop - 1;
1510 v2 = vtop;
1511 /* currently, we cannot do computations with forward symbols */
1512 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1513 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1514 if (c1 && c2) {
1515 if (v1->type.t == VT_FLOAT) {
1516 f1 = v1->c.f;
1517 f2 = v2->c.f;
1518 } else if (v1->type.t == VT_DOUBLE) {
1519 f1 = v1->c.d;
1520 f2 = v2->c.d;
1521 } else {
1522 f1 = v1->c.ld;
1523 f2 = v2->c.ld;
1526 /* NOTE: we only do constant propagation if finite number (not
1527 NaN or infinity) (ANSI spec) */
1528 if (!ieee_finite(f1) || !ieee_finite(f2))
1529 goto general_case;
1531 switch(op) {
1532 case '+': f1 += f2; break;
1533 case '-': f1 -= f2; break;
1534 case '*': f1 *= f2; break;
1535 case '/':
1536 if (f2 == 0.0) {
1537 if (const_wanted)
1538 tcc_error("division by zero in constant");
1539 goto general_case;
1541 f1 /= f2;
1542 break;
1543 /* XXX: also handles tests ? */
1544 default:
1545 goto general_case;
1547 /* XXX: overflow test ? */
1548 if (v1->type.t == VT_FLOAT) {
1549 v1->c.f = f1;
1550 } else if (v1->type.t == VT_DOUBLE) {
1551 v1->c.d = f1;
1552 } else {
1553 v1->c.ld = f1;
1555 vtop--;
1556 } else {
1557 general_case:
1558 if (!nocode_wanted) {
1559 gen_opf(op);
1560 } else {
1561 vtop--;
1566 static int pointed_size(CType *type)
1568 int align;
1569 return type_size(pointed_type(type), &align);
1572 static void vla_runtime_pointed_size(CType *type)
1574 int align;
1575 vla_runtime_type_size(pointed_type(type), &align);
1578 static inline int is_null_pointer(SValue *p)
1580 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1581 return 0;
1582 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1583 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1584 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0);
1587 static inline int is_integer_btype(int bt)
1589 return (bt == VT_BYTE || bt == VT_SHORT ||
1590 bt == VT_INT || bt == VT_LLONG);
1593 /* check types for comparison or substraction of pointers */
1594 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1596 CType *type1, *type2, tmp_type1, tmp_type2;
1597 int bt1, bt2;
1599 /* null pointers are accepted for all comparisons as gcc */
1600 if (is_null_pointer(p1) || is_null_pointer(p2))
1601 return;
1602 type1 = &p1->type;
1603 type2 = &p2->type;
1604 bt1 = type1->t & VT_BTYPE;
1605 bt2 = type2->t & VT_BTYPE;
1606 /* accept comparison between pointer and integer with a warning */
1607 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1608 if (op != TOK_LOR && op != TOK_LAND )
1609 tcc_warning("comparison between pointer and integer");
1610 return;
1613 /* both must be pointers or implicit function pointers */
1614 if (bt1 == VT_PTR) {
1615 type1 = pointed_type(type1);
1616 } else if (bt1 != VT_FUNC)
1617 goto invalid_operands;
1619 if (bt2 == VT_PTR) {
1620 type2 = pointed_type(type2);
1621 } else if (bt2 != VT_FUNC) {
1622 invalid_operands:
1623 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1625 if ((type1->t & VT_BTYPE) == VT_VOID ||
1626 (type2->t & VT_BTYPE) == VT_VOID)
1627 return;
1628 tmp_type1 = *type1;
1629 tmp_type2 = *type2;
1630 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1631 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1632 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1633 /* gcc-like error if '-' is used */
1634 if (op == '-')
1635 goto invalid_operands;
1636 else
1637 tcc_warning("comparison of distinct pointer types lacks a cast");
1641 /* generic gen_op: handles types problems */
1642 ST_FUNC void gen_op(int op)
1644 int u, t1, t2, bt1, bt2, t;
1645 CType type1;
1647 t1 = vtop[-1].type.t;
1648 t2 = vtop[0].type.t;
1649 bt1 = t1 & VT_BTYPE;
1650 bt2 = t2 & VT_BTYPE;
1652 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1653 /* at least one operand is a pointer */
1654 /* relationnal op: must be both pointers */
1655 if (op >= TOK_ULT && op <= TOK_LOR) {
1656 check_comparison_pointer_types(vtop - 1, vtop, op);
1657 /* pointers are handled are unsigned */
1658 #ifdef TCC_TARGET_X86_64
1659 t = VT_LLONG | VT_UNSIGNED;
1660 #else
1661 t = VT_INT | VT_UNSIGNED;
1662 #endif
1663 goto std_op;
1665 /* if both pointers, then it must be the '-' op */
1666 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1667 if (op != '-')
1668 tcc_error("cannot use pointers here");
1669 check_comparison_pointer_types(vtop - 1, vtop, op);
1670 /* XXX: check that types are compatible */
1671 if (vtop[-1].type.t & VT_VLA) {
1672 vla_runtime_pointed_size(&vtop[-1].type);
1673 } else {
1674 vpushi(pointed_size(&vtop[-1].type));
1676 vrott(3);
1677 gen_opic(op);
1678 /* set to integer type */
1679 #ifdef TCC_TARGET_X86_64
1680 vtop->type.t = VT_LLONG;
1681 #else
1682 vtop->type.t = VT_INT;
1683 #endif
1684 vswap();
1685 gen_op(TOK_PDIV);
1686 } else {
1687 /* exactly one pointer : must be '+' or '-'. */
1688 if (op != '-' && op != '+')
1689 tcc_error("cannot use pointers here");
1690 /* Put pointer as first operand */
1691 if (bt2 == VT_PTR) {
1692 vswap();
1693 swap(&t1, &t2);
1695 type1 = vtop[-1].type;
1696 type1.t &= ~VT_ARRAY;
1697 if (vtop[-1].type.t & VT_VLA)
1698 vla_runtime_pointed_size(&vtop[-1].type);
1699 else {
1700 u = pointed_size(&vtop[-1].type);
1701 if (u < 0)
1702 tcc_error("unknown array element size");
1703 #ifdef TCC_TARGET_X86_64
1704 vpushll(u);
1705 #else
1706 /* XXX: cast to int ? (long long case) */
1707 vpushi(u);
1708 #endif
1710 gen_op('*');
1711 #ifdef CONFIG_TCC_BCHECK
1712 /* if evaluating constant expression, no code should be
1713 generated, so no bound check */
1714 if (tcc_state->do_bounds_check && !const_wanted) {
1715 /* if bounded pointers, we generate a special code to
1716 test bounds */
1717 if (op == '-') {
1718 vpushi(0);
1719 vswap();
1720 gen_op('-');
1722 gen_bounded_ptr_add();
1723 } else
1724 #endif
1726 gen_opic(op);
1728 /* put again type if gen_opic() swaped operands */
1729 vtop->type = type1;
1731 } else if (is_float(bt1) || is_float(bt2)) {
1732 /* compute bigger type and do implicit casts */
1733 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1734 t = VT_LDOUBLE;
1735 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1736 t = VT_DOUBLE;
1737 } else {
1738 t = VT_FLOAT;
1740 /* floats can only be used for a few operations */
1741 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1742 (op < TOK_ULT || op > TOK_GT))
1743 tcc_error("invalid operands for binary operation");
1744 goto std_op;
1745 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1746 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1747 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1748 t |= VT_UNSIGNED;
1749 goto std_op;
1750 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1751 /* cast to biggest op */
1752 t = VT_LLONG;
1753 /* convert to unsigned if it does not fit in a long long */
1754 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1755 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1756 t |= VT_UNSIGNED;
1757 goto std_op;
1758 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1759 tcc_error("comparison of struct");
1760 } else {
1761 /* integer operations */
1762 t = VT_INT;
1763 /* convert to unsigned if it does not fit in an integer */
1764 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1765 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1766 t |= VT_UNSIGNED;
1767 std_op:
1768 /* XXX: currently, some unsigned operations are explicit, so
1769 we modify them here */
1770 if (t & VT_UNSIGNED) {
1771 if (op == TOK_SAR)
1772 op = TOK_SHR;
1773 else if (op == '/')
1774 op = TOK_UDIV;
1775 else if (op == '%')
1776 op = TOK_UMOD;
1777 else if (op == TOK_LT)
1778 op = TOK_ULT;
1779 else if (op == TOK_GT)
1780 op = TOK_UGT;
1781 else if (op == TOK_LE)
1782 op = TOK_ULE;
1783 else if (op == TOK_GE)
1784 op = TOK_UGE;
1786 vswap();
1787 type1.t = t;
1788 gen_cast(&type1);
1789 vswap();
1790 /* special case for shifts and long long: we keep the shift as
1791 an integer */
1792 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1793 type1.t = VT_INT;
1794 gen_cast(&type1);
1795 if (is_float(t))
1796 gen_opif(op);
1797 else
1798 gen_opic(op);
1799 if (op >= TOK_ULT && op <= TOK_GT) {
1800 /* relationnal op: the result is an int */
1801 vtop->type.t = VT_INT;
1802 } else {
1803 vtop->type.t = t;
1808 #ifndef TCC_TARGET_ARM
1809 /* generic itof for unsigned long long case */
1810 static void gen_cvt_itof1(int t)
1812 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1813 (VT_LLONG | VT_UNSIGNED)) {
1815 if (t == VT_FLOAT)
1816 vpush_global_sym(&func_old_type, TOK___floatundisf);
1817 #if LDOUBLE_SIZE != 8
1818 else if (t == VT_LDOUBLE)
1819 vpush_global_sym(&func_old_type, TOK___floatundixf);
1820 #endif
1821 else
1822 vpush_global_sym(&func_old_type, TOK___floatundidf);
1823 vrott(2);
1824 gfunc_call(1);
1825 vpushi(0);
1826 vtop->r = reg_fret(t);
1827 } else {
1828 gen_cvt_itof(t);
1831 #endif
1833 /* generic ftoi for unsigned long long case */
1834 static void gen_cvt_ftoi1(int t)
1836 int st;
1838 if (t == (VT_LLONG | VT_UNSIGNED)) {
1839 /* not handled natively */
1840 st = vtop->type.t & VT_BTYPE;
1841 if (st == VT_FLOAT)
1842 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1843 #if LDOUBLE_SIZE != 8
1844 else if (st == VT_LDOUBLE)
1845 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1846 #endif
1847 else
1848 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1849 vrott(2);
1850 gfunc_call(1);
1851 vpushi(0);
1852 vtop->r = REG_IRET;
1853 vtop->r2 = REG_LRET;
1854 } else {
1855 gen_cvt_ftoi(t);
1859 /* force char or short cast */
1860 static void force_charshort_cast(int t)
1862 int bits, dbt;
1863 dbt = t & VT_BTYPE;
1864 /* XXX: add optimization if lvalue : just change type and offset */
1865 if (dbt == VT_BYTE)
1866 bits = 8;
1867 else
1868 bits = 16;
1869 if (t & VT_UNSIGNED) {
1870 vpushi((1 << bits) - 1);
1871 gen_op('&');
1872 } else {
1873 bits = 32 - bits;
1874 vpushi(bits);
1875 gen_op(TOK_SHL);
1876 /* result must be signed or the SAR is converted to an SHL
1877 This was not the case when "t" was a signed short
1878 and the last value on the stack was an unsigned int */
1879 vtop->type.t &= ~VT_UNSIGNED;
1880 vpushi(bits);
1881 gen_op(TOK_SAR);
1885 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1886 static void gen_cast(CType *type)
1888 int sbt, dbt, sf, df, c, p;
1890 /* special delayed cast for char/short */
1891 /* XXX: in some cases (multiple cascaded casts), it may still
1892 be incorrect */
1893 if (vtop->r & VT_MUSTCAST) {
1894 vtop->r &= ~VT_MUSTCAST;
1895 force_charshort_cast(vtop->type.t);
1898 /* bitfields first get cast to ints */
1899 if (vtop->type.t & VT_BITFIELD) {
1900 gv(RC_INT);
1903 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1904 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1906 if (sbt != dbt) {
1907 sf = is_float(sbt);
1908 df = is_float(dbt);
1909 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1910 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1911 if (c) {
1912 /* constant case: we can do it now */
1913 /* XXX: in ISOC, cannot do it if error in convert */
1914 if (sbt == VT_FLOAT)
1915 vtop->c.ld = vtop->c.f;
1916 else if (sbt == VT_DOUBLE)
1917 vtop->c.ld = vtop->c.d;
1919 if (df) {
1920 if ((sbt & VT_BTYPE) == VT_LLONG) {
1921 if (sbt & VT_UNSIGNED)
1922 vtop->c.ld = vtop->c.ull;
1923 else
1924 vtop->c.ld = vtop->c.ll;
1925 } else if(!sf) {
1926 if (sbt & VT_UNSIGNED)
1927 vtop->c.ld = vtop->c.ui;
1928 else
1929 vtop->c.ld = vtop->c.i;
1932 if (dbt == VT_FLOAT)
1933 vtop->c.f = (float)vtop->c.ld;
1934 else if (dbt == VT_DOUBLE)
1935 vtop->c.d = (double)vtop->c.ld;
1936 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1937 vtop->c.ull = (unsigned long long)vtop->c.ld;
1938 } else if (sf && dbt == VT_BOOL) {
1939 vtop->c.i = (vtop->c.ld != 0);
1940 } else {
1941 if(sf)
1942 vtop->c.ll = (long long)vtop->c.ld;
1943 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1944 vtop->c.ll = vtop->c.ull;
1945 else if (sbt & VT_UNSIGNED)
1946 vtop->c.ll = vtop->c.ui;
1947 #ifdef TCC_TARGET_X86_64
1948 else if (sbt == VT_PTR)
1950 #endif
1951 else if (sbt != VT_LLONG)
1952 vtop->c.ll = vtop->c.i;
1954 if (dbt == (VT_LLONG|VT_UNSIGNED))
1955 vtop->c.ull = vtop->c.ll;
1956 else if (dbt == VT_BOOL)
1957 vtop->c.i = (vtop->c.ll != 0);
1958 #ifdef TCC_TARGET_X86_64
1959 else if (dbt == VT_PTR)
1961 #endif
1962 else if (dbt != VT_LLONG) {
1963 int s = 0;
1964 if ((dbt & VT_BTYPE) == VT_BYTE)
1965 s = 24;
1966 else if ((dbt & VT_BTYPE) == VT_SHORT)
1967 s = 16;
1968 if(dbt & VT_UNSIGNED)
1969 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1970 else
1971 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1974 } else if (p && dbt == VT_BOOL) {
1975 vtop->r = VT_CONST;
1976 vtop->c.i = 1;
1977 } else if (!nocode_wanted) {
1978 /* non constant case: generate code */
1979 if (sf && df) {
1980 /* convert from fp to fp */
1981 gen_cvt_ftof(dbt);
1982 } else if (df) {
1983 /* convert int to fp */
1984 gen_cvt_itof1(dbt);
1985 } else if (sf) {
1986 /* convert fp to int */
1987 if (dbt == VT_BOOL) {
1988 vpushi(0);
1989 gen_op(TOK_NE);
1990 } else {
1991 /* we handle char/short/etc... with generic code */
1992 if (dbt != (VT_INT | VT_UNSIGNED) &&
1993 dbt != (VT_LLONG | VT_UNSIGNED) &&
1994 dbt != VT_LLONG)
1995 dbt = VT_INT;
1996 gen_cvt_ftoi1(dbt);
1997 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1998 /* additional cast for char/short... */
1999 vtop->type.t = dbt;
2000 gen_cast(type);
2003 #ifndef TCC_TARGET_X86_64
2004 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2005 if ((sbt & VT_BTYPE) != VT_LLONG) {
2006 /* scalar to long long */
2007 /* machine independent conversion */
2008 gv(RC_INT);
2009 /* generate high word */
2010 if (sbt == (VT_INT | VT_UNSIGNED)) {
2011 vpushi(0);
2012 gv(RC_INT);
2013 } else {
2014 if (sbt == VT_PTR) {
2015 /* cast from pointer to int before we apply
2016 shift operation, which pointers don't support*/
2017 gen_cast(&int_type);
2019 gv_dup();
2020 vpushi(31);
2021 gen_op(TOK_SAR);
2023 /* patch second register */
2024 vtop[-1].r2 = vtop->r;
2025 vpop();
2027 #else
2028 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2029 (dbt & VT_BTYPE) == VT_PTR ||
2030 (dbt & VT_BTYPE) == VT_FUNC) {
2031 if ((sbt & VT_BTYPE) != VT_LLONG &&
2032 (sbt & VT_BTYPE) != VT_PTR &&
2033 (sbt & VT_BTYPE) != VT_FUNC) {
2034 /* need to convert from 32bit to 64bit */
2035 int r = gv(RC_INT);
2036 if (sbt != (VT_INT | VT_UNSIGNED)) {
2037 /* x86_64 specific: movslq */
2038 o(0x6348);
2039 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2042 #endif
2043 } else if (dbt == VT_BOOL) {
2044 /* scalar to bool */
2045 vpushi(0);
2046 gen_op(TOK_NE);
2047 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2048 (dbt & VT_BTYPE) == VT_SHORT) {
2049 if (sbt == VT_PTR) {
2050 vtop->type.t = VT_INT;
2051 tcc_warning("nonportable conversion from pointer to char/short");
2053 force_charshort_cast(dbt);
2054 } else if ((dbt & VT_BTYPE) == VT_INT) {
2055 /* scalar to int */
2056 if (sbt == VT_LLONG) {
2057 /* from long long: just take low order word */
2058 lexpand();
2059 vpop();
2061 /* if lvalue and single word type, nothing to do because
2062 the lvalue already contains the real type size (see
2063 VT_LVAL_xxx constants) */
2066 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2067 /* if we are casting between pointer types,
2068 we must update the VT_LVAL_xxx size */
2069 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2070 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2072 vtop->type = *type;
2075 /* return type size as known at compile time. Put alignment at 'a' */
2076 ST_FUNC int type_size(CType *type, int *a)
2078 Sym *s;
2079 int bt;
2081 bt = type->t & VT_BTYPE;
2082 if (bt == VT_STRUCT) {
2083 /* struct/union */
2084 s = type->ref;
2085 *a = s->r;
2086 return s->c;
2087 } else if (bt == VT_PTR) {
2088 if (type->t & VT_ARRAY) {
2089 int ts;
2091 s = type->ref;
2092 ts = type_size(&s->type, a);
2094 if (ts < 0 && s->c < 0)
2095 ts = -ts;
2097 return ts * s->c;
2098 } else {
2099 *a = PTR_SIZE;
2100 return PTR_SIZE;
2102 } else if (bt == VT_LDOUBLE) {
2103 *a = LDOUBLE_ALIGN;
2104 return LDOUBLE_SIZE;
2105 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2106 #ifdef TCC_TARGET_I386
2107 #ifdef TCC_TARGET_PE
2108 *a = 8;
2109 #else
2110 *a = 4;
2111 #endif
2112 #elif defined(TCC_TARGET_ARM)
2113 #ifdef TCC_ARM_EABI
2114 *a = 8;
2115 #else
2116 *a = 4;
2117 #endif
2118 #else
2119 *a = 8;
2120 #endif
2121 return 8;
2122 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2123 *a = 4;
2124 return 4;
2125 } else if (bt == VT_SHORT) {
2126 *a = 2;
2127 return 2;
2128 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2129 *a = 8;
2130 return 16;
2131 } else {
2132 /* char, void, function, _Bool */
2133 *a = 1;
2134 return 1;
2138 /* push type size as known at runtime time on top of value stack. Put
2139 alignment at 'a' */
2140 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2142 if (type->t & VT_VLA) {
2143 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2144 } else {
2145 vpushi(type_size(type, a));
2149 static void vla_sp_save(void) {
2150 if (!(vla_flags & VLA_SP_LOC_SET)) {
2151 *vla_sp_loc = (loc -= PTR_SIZE);
2152 vla_flags |= VLA_SP_LOC_SET;
2154 if (!(vla_flags & VLA_SP_SAVED)) {
2155 gen_vla_sp_save(*vla_sp_loc);
2156 vla_flags |= VLA_SP_SAVED;
2160 /* return the pointed type of t */
2161 static inline CType *pointed_type(CType *type)
2163 return &type->ref->type;
2166 /* modify type so that its it is a pointer to type. */
2167 ST_FUNC void mk_pointer(CType *type)
2169 Sym *s;
2170 s = sym_push(SYM_FIELD, type, 0, -1);
2171 type->t = VT_PTR | (type->t & ~VT_TYPE);
2172 type->ref = s;
2175 /* compare function types. OLD functions match any new functions */
2176 static int is_compatible_func(CType *type1, CType *type2)
2178 Sym *s1, *s2;
2180 s1 = type1->ref;
2181 s2 = type2->ref;
2182 if (!is_compatible_types(&s1->type, &s2->type))
2183 return 0;
2184 /* check func_call */
2185 if (s1->a.func_call != s2->a.func_call)
2186 return 0;
2187 /* XXX: not complete */
2188 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2189 return 1;
2190 if (s1->c != s2->c)
2191 return 0;
2192 while (s1 != NULL) {
2193 if (s2 == NULL)
2194 return 0;
2195 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2196 return 0;
2197 s1 = s1->next;
2198 s2 = s2->next;
2200 if (s2)
2201 return 0;
2202 return 1;
2205 /* return true if type1 and type2 are the same. If unqualified is
2206 true, qualifiers on the types are ignored.
2208 - enums are not checked as gcc __builtin_types_compatible_p ()
2210 static int compare_types(CType *type1, CType *type2, int unqualified)
2212 int bt1, t1, t2;
2214 t1 = type1->t & VT_TYPE;
2215 t2 = type2->t & VT_TYPE;
2216 if (unqualified) {
2217 /* strip qualifiers before comparing */
2218 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2219 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2221 /* Default Vs explicit signedness only matters for char */
2222 if ((t1 & VT_BTYPE) != VT_BYTE) {
2223 t1 &= ~VT_DEFSIGN;
2224 t2 &= ~VT_DEFSIGN;
2226 /* XXX: bitfields ? */
2227 if (t1 != t2)
2228 return 0;
2229 /* test more complicated cases */
2230 bt1 = t1 & VT_BTYPE;
2231 if (bt1 == VT_PTR) {
2232 type1 = pointed_type(type1);
2233 type2 = pointed_type(type2);
2234 return is_compatible_types(type1, type2);
2235 } else if (bt1 == VT_STRUCT) {
2236 return (type1->ref == type2->ref);
2237 } else if (bt1 == VT_FUNC) {
2238 return is_compatible_func(type1, type2);
2239 } else {
2240 return 1;
2244 /* return true if type1 and type2 are exactly the same (including
2245 qualifiers).
2247 static int is_compatible_types(CType *type1, CType *type2)
2249 return compare_types(type1,type2,0);
2252 /* return true if type1 and type2 are the same (ignoring qualifiers).
2254 static int is_compatible_parameter_types(CType *type1, CType *type2)
2256 return compare_types(type1,type2,1);
2259 /* print a type. If 'varstr' is not NULL, then the variable is also
2260 printed in the type */
2261 /* XXX: union */
2262 /* XXX: add array and function pointers */
2263 static void type_to_str(char *buf, int buf_size,
2264 CType *type, const char *varstr)
2266 int bt, v, t;
2267 Sym *s, *sa;
2268 char buf1[256];
2269 const char *tstr;
2271 t = type->t & VT_TYPE;
2272 bt = t & VT_BTYPE;
2273 buf[0] = '\0';
2274 if (t & VT_CONSTANT)
2275 pstrcat(buf, buf_size, "const ");
2276 if (t & VT_VOLATILE)
2277 pstrcat(buf, buf_size, "volatile ");
2278 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2279 pstrcat(buf, buf_size, "unsigned ");
2280 else if (t & VT_DEFSIGN)
2281 pstrcat(buf, buf_size, "signed ");
2282 switch(bt) {
2283 case VT_VOID:
2284 tstr = "void";
2285 goto add_tstr;
2286 case VT_BOOL:
2287 tstr = "_Bool";
2288 goto add_tstr;
2289 case VT_BYTE:
2290 tstr = "char";
2291 goto add_tstr;
2292 case VT_SHORT:
2293 tstr = "short";
2294 goto add_tstr;
2295 case VT_INT:
2296 tstr = "int";
2297 goto add_tstr;
2298 case VT_LONG:
2299 tstr = "long";
2300 goto add_tstr;
2301 case VT_LLONG:
2302 tstr = "long long";
2303 goto add_tstr;
2304 case VT_FLOAT:
2305 tstr = "float";
2306 goto add_tstr;
2307 case VT_DOUBLE:
2308 tstr = "double";
2309 goto add_tstr;
2310 case VT_LDOUBLE:
2311 tstr = "long double";
2312 add_tstr:
2313 pstrcat(buf, buf_size, tstr);
2314 break;
2315 case VT_ENUM:
2316 case VT_STRUCT:
2317 if (bt == VT_STRUCT)
2318 tstr = "struct ";
2319 else
2320 tstr = "enum ";
2321 pstrcat(buf, buf_size, tstr);
2322 v = type->ref->v & ~SYM_STRUCT;
2323 if (v >= SYM_FIRST_ANOM)
2324 pstrcat(buf, buf_size, "<anonymous>");
2325 else
2326 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2327 break;
2328 case VT_FUNC:
2329 s = type->ref;
2330 type_to_str(buf, buf_size, &s->type, varstr);
2331 pstrcat(buf, buf_size, "(");
2332 sa = s->next;
2333 while (sa != NULL) {
2334 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2335 pstrcat(buf, buf_size, buf1);
2336 sa = sa->next;
2337 if (sa)
2338 pstrcat(buf, buf_size, ", ");
2340 pstrcat(buf, buf_size, ")");
2341 goto no_var;
2342 case VT_PTR:
2343 s = type->ref;
2344 pstrcpy(buf1, sizeof(buf1), "*");
2345 if (varstr)
2346 pstrcat(buf1, sizeof(buf1), varstr);
2347 type_to_str(buf, buf_size, &s->type, buf1);
2348 goto no_var;
2350 if (varstr) {
2351 pstrcat(buf, buf_size, " ");
2352 pstrcat(buf, buf_size, varstr);
2354 no_var: ;
2357 /* verify type compatibility to store vtop in 'dt' type, and generate
2358 casts if needed. */
2359 static void gen_assign_cast(CType *dt)
2361 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2362 char buf1[256], buf2[256];
2363 int dbt, sbt;
2365 st = &vtop->type; /* source type */
2366 dbt = dt->t & VT_BTYPE;
2367 sbt = st->t & VT_BTYPE;
2368 if (sbt == VT_VOID || dbt == VT_VOID)
2369 tcc_error("cannot cast from/to void");
2370 if (dt->t & VT_CONSTANT)
2371 tcc_warning("assignment of read-only location");
2372 switch(dbt) {
2373 case VT_PTR:
2374 /* special cases for pointers */
2375 /* '0' can also be a pointer */
2376 if (is_null_pointer(vtop))
2377 goto type_ok;
2378 /* accept implicit pointer to integer cast with warning */
2379 if (is_integer_btype(sbt)) {
2380 tcc_warning("assignment makes pointer from integer without a cast");
2381 goto type_ok;
2383 type1 = pointed_type(dt);
2384 /* a function is implicitely a function pointer */
2385 if (sbt == VT_FUNC) {
2386 if ((type1->t & VT_BTYPE) != VT_VOID &&
2387 !is_compatible_types(pointed_type(dt), st))
2388 tcc_warning("assignment from incompatible pointer type");
2389 goto type_ok;
2391 if (sbt != VT_PTR)
2392 goto error;
2393 type2 = pointed_type(st);
2394 if ((type1->t & VT_BTYPE) == VT_VOID ||
2395 (type2->t & VT_BTYPE) == VT_VOID) {
2396 /* void * can match anything */
2397 } else {
2398 /* exact type match, except for unsigned */
2399 tmp_type1 = *type1;
2400 tmp_type2 = *type2;
2401 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2402 VT_VOLATILE);
2403 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2404 VT_VOLATILE);
2405 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2406 tcc_warning("assignment from incompatible pointer type");
2408 /* check const and volatile */
2409 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2410 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2411 tcc_warning("assignment discards qualifiers from pointer target type");
2412 break;
2413 case VT_BYTE:
2414 case VT_SHORT:
2415 case VT_INT:
2416 case VT_LLONG:
2417 if (sbt == VT_PTR || sbt == VT_FUNC) {
2418 tcc_warning("assignment makes integer from pointer without a cast");
2420 /* XXX: more tests */
2421 break;
2422 case VT_STRUCT:
2423 tmp_type1 = *dt;
2424 tmp_type2 = *st;
2425 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2426 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2427 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2428 error:
2429 type_to_str(buf1, sizeof(buf1), st, NULL);
2430 type_to_str(buf2, sizeof(buf2), dt, NULL);
2431 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2433 break;
2435 type_ok:
2436 gen_cast(dt);
2439 /* store vtop in lvalue pushed on stack */
2440 ST_FUNC void vstore(void)
2442 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2444 ft = vtop[-1].type.t;
2445 sbt = vtop->type.t & VT_BTYPE;
2446 dbt = ft & VT_BTYPE;
2447 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2448 (sbt == VT_INT && dbt == VT_SHORT))
2449 && !(vtop->type.t & VT_BITFIELD)) {
2450 /* optimize char/short casts */
2451 delayed_cast = VT_MUSTCAST;
2452 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2453 /* XXX: factorize */
2454 if (ft & VT_CONSTANT)
2455 tcc_warning("assignment of read-only location");
2456 } else {
2457 delayed_cast = 0;
2458 if (!(ft & VT_BITFIELD))
2459 gen_assign_cast(&vtop[-1].type);
2462 if (sbt == VT_STRUCT) {
2463 /* if structure, only generate pointer */
2464 /* structure assignment : generate memcpy */
2465 /* XXX: optimize if small size */
2466 if (!nocode_wanted) {
2467 size = type_size(&vtop->type, &align);
2469 /* destination */
2470 vswap();
2471 vtop->type.t = VT_PTR;
2472 gaddrof();
2474 /* address of memcpy() */
2475 #ifdef TCC_ARM_EABI
2476 if(!(align & 7))
2477 vpush_global_sym(&func_old_type, TOK_memcpy8);
2478 else if(!(align & 3))
2479 vpush_global_sym(&func_old_type, TOK_memcpy4);
2480 else
2481 #endif
2482 vpush_global_sym(&func_old_type, TOK_memcpy);
2484 vswap();
2485 /* source */
2486 vpushv(vtop - 2);
2487 vtop->type.t = VT_PTR;
2488 gaddrof();
2489 /* type size */
2490 vpushi(size);
2491 gfunc_call(3);
2492 } else {
2493 vswap();
2494 vpop();
2496 /* leave source on stack */
2497 } else if (ft & VT_BITFIELD) {
2498 /* bitfield store handling */
2499 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2500 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2501 /* remove bit field info to avoid loops */
2502 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2504 /* duplicate source into other register */
2505 gv_dup();
2506 vswap();
2507 vrott(3);
2509 if((ft & VT_BTYPE) == VT_BOOL) {
2510 gen_cast(&vtop[-1].type);
2511 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2514 /* duplicate destination */
2515 vdup();
2516 vtop[-1] = vtop[-2];
2518 /* mask and shift source */
2519 if((ft & VT_BTYPE) != VT_BOOL) {
2520 if((ft & VT_BTYPE) == VT_LLONG) {
2521 vpushll((1ULL << bit_size) - 1ULL);
2522 } else {
2523 vpushi((1 << bit_size) - 1);
2525 gen_op('&');
2527 vpushi(bit_pos);
2528 gen_op(TOK_SHL);
2529 /* load destination, mask and or with source */
2530 vswap();
2531 if((ft & VT_BTYPE) == VT_LLONG) {
2532 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2533 } else {
2534 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2536 gen_op('&');
2537 gen_op('|');
2538 /* store result */
2539 vstore();
2541 /* pop off shifted source from "duplicate source..." above */
2542 vpop();
2544 } else {
2545 #ifdef CONFIG_TCC_BCHECK
2546 /* bound check case */
2547 if (vtop[-1].r & VT_MUSTBOUND) {
2548 vswap();
2549 gbound();
2550 vswap();
2552 #endif
2553 if (!nocode_wanted) {
2554 rc = RC_INT;
2555 if (is_float(ft)) {
2556 rc = RC_FLOAT;
2557 #ifdef TCC_TARGET_X86_64
2558 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2559 rc = RC_ST0;
2560 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2561 rc = RC_FRET;
2563 #endif
2565 r = gv(rc); /* generate value */
2566 /* if lvalue was saved on stack, must read it */
2567 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2568 SValue sv;
2569 t = get_reg(RC_INT);
2570 #ifdef TCC_TARGET_X86_64
2571 sv.type.t = VT_PTR;
2572 #else
2573 sv.type.t = VT_INT;
2574 #endif
2575 sv.r = VT_LOCAL | VT_LVAL;
2576 sv.c.ul = vtop[-1].c.ul;
2577 load(t, &sv);
2578 vtop[-1].r = t | VT_LVAL;
2580 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2581 #ifdef TCC_TARGET_X86_64
2582 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2583 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2584 #else
2585 if ((ft & VT_BTYPE) == VT_LLONG) {
2586 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2587 #endif
2588 vtop[-1].type.t = load_type;
2589 store(r, vtop - 1);
2590 vswap();
2591 /* convert to int to increment easily */
2592 vtop->type.t = addr_type;
2593 gaddrof();
2594 vpushi(load_size);
2595 gen_op('+');
2596 vtop->r |= VT_LVAL;
2597 vswap();
2598 vtop[-1].type.t = load_type;
2599 /* XXX: it works because r2 is spilled last ! */
2600 store(vtop->r2, vtop - 1);
2601 } else {
2602 store(r, vtop - 1);
2605 vswap();
2606 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2607 vtop->r |= delayed_cast;
2611 /* post defines POST/PRE add. c is the token ++ or -- */
2612 ST_FUNC void inc(int post, int c)
2614 test_lvalue();
2615 vdup(); /* save lvalue */
2616 if (post) {
2617 gv_dup(); /* duplicate value */
2618 vrotb(3);
2619 vrotb(3);
2621 /* add constant */
2622 vpushi(c - TOK_MID);
2623 gen_op('+');
2624 vstore(); /* store value */
2625 if (post)
2626 vpop(); /* if post op, return saved value */
2629 /* Parse GNUC __attribute__ extension. Currently, the following
2630 extensions are recognized:
2631 - aligned(n) : set data/function alignment.
2632 - packed : force data alignment to 1
2633 - section(x) : generate data/code in this section.
2634 - unused : currently ignored, but may be used someday.
2635 - regparm(n) : pass function parameters in registers (i386 only)
2637 static void parse_attribute(AttributeDef *ad)
2639 int t, n;
2641 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2642 next();
2643 skip('(');
2644 skip('(');
2645 while (tok != ')') {
2646 if (tok < TOK_IDENT)
2647 expect("attribute name");
2648 t = tok;
2649 next();
2650 switch(t) {
2651 case TOK_SECTION1:
2652 case TOK_SECTION2:
2653 skip('(');
2654 if (tok != TOK_STR)
2655 expect("section name");
2656 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2657 next();
2658 skip(')');
2659 break;
2660 case TOK_ALIAS1:
2661 case TOK_ALIAS2:
2662 skip('(');
2663 if (tok != TOK_STR)
2664 expect("alias(\"target\")");
2665 ad->alias_target = /* save string as token, for later */
2666 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2667 next();
2668 skip(')');
2669 break;
2670 case TOK_ALIGNED1:
2671 case TOK_ALIGNED2:
2672 if (tok == '(') {
2673 next();
2674 n = expr_const();
2675 if (n <= 0 || (n & (n - 1)) != 0)
2676 tcc_error("alignment must be a positive power of two");
2677 skip(')');
2678 } else {
2679 n = MAX_ALIGN;
2681 ad->a.aligned = n;
2682 break;
2683 case TOK_PACKED1:
2684 case TOK_PACKED2:
2685 ad->a.packed = 1;
2686 break;
2687 case TOK_WEAK1:
2688 case TOK_WEAK2:
2689 ad->a.weak = 1;
2690 break;
2691 case TOK_UNUSED1:
2692 case TOK_UNUSED2:
2693 /* currently, no need to handle it because tcc does not
2694 track unused objects */
2695 break;
2696 case TOK_NORETURN1:
2697 case TOK_NORETURN2:
2698 /* currently, no need to handle it because tcc does not
2699 track unused objects */
2700 break;
2701 case TOK_CDECL1:
2702 case TOK_CDECL2:
2703 case TOK_CDECL3:
2704 ad->a.func_call = FUNC_CDECL;
2705 break;
2706 case TOK_STDCALL1:
2707 case TOK_STDCALL2:
2708 case TOK_STDCALL3:
2709 ad->a.func_call = FUNC_STDCALL;
2710 break;
2711 #ifdef TCC_TARGET_I386
2712 case TOK_REGPARM1:
2713 case TOK_REGPARM2:
2714 skip('(');
2715 n = expr_const();
2716 if (n > 3)
2717 n = 3;
2718 else if (n < 0)
2719 n = 0;
2720 if (n > 0)
2721 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2722 skip(')');
2723 break;
2724 case TOK_FASTCALL1:
2725 case TOK_FASTCALL2:
2726 case TOK_FASTCALL3:
2727 ad->a.func_call = FUNC_FASTCALLW;
2728 break;
2729 #endif
2730 case TOK_MODE:
2731 skip('(');
2732 switch(tok) {
2733 case TOK_MODE_DI:
2734 ad->a.mode = VT_LLONG + 1;
2735 break;
2736 case TOK_MODE_HI:
2737 ad->a.mode = VT_SHORT + 1;
2738 break;
2739 case TOK_MODE_SI:
2740 ad->a.mode = VT_INT + 1;
2741 break;
2742 default:
2743 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2744 break;
2746 next();
2747 skip(')');
2748 break;
2749 case TOK_DLLEXPORT:
2750 ad->a.func_export = 1;
2751 break;
2752 case TOK_DLLIMPORT:
2753 ad->a.func_import = 1;
2754 break;
2755 default:
2756 if (tcc_state->warn_unsupported)
2757 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2758 /* skip parameters */
2759 if (tok == '(') {
2760 int parenthesis = 0;
2761 do {
2762 if (tok == '(')
2763 parenthesis++;
2764 else if (tok == ')')
2765 parenthesis--;
2766 next();
2767 } while (parenthesis && tok != -1);
2769 break;
2771 if (tok != ',')
2772 break;
2773 next();
2775 skip(')');
2776 skip(')');
2780 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2781 static void struct_decl(CType *type, int u, int tdef)
2783 int a, v, size, align, maxalign, c, offset, flexible;
2784 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2785 Sym *s, *ss, *ass, **ps;
2786 AttributeDef ad;
2787 CType type1, btype;
2789 a = tok; /* save decl type */
2790 next();
2791 if (tok != '{') {
2792 v = tok;
2793 next();
2794 /* struct already defined ? return it */
2795 if (v < TOK_IDENT)
2796 expect("struct/union/enum name");
2797 s = struct_find(v);
2798 if (s) {
2799 if (s->type.t != a)
2800 tcc_error("invalid type");
2801 goto do_decl;
2802 } else if (tok >= TOK_IDENT && !tdef)
2803 tcc_error("unknown struct/union/enum");
2804 } else {
2805 v = anon_sym++;
2807 type1.t = a;
2808 type1.ref = NULL;
2809 /* we put an undefined size for struct/union */
2810 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2811 s->r = 0; /* default alignment is zero as gcc */
2812 /* put struct/union/enum name in type */
2813 do_decl:
2814 type->t = u;
2815 type->ref = s;
2817 if (tok == '{') {
2818 next();
2819 if (s->c != -1)
2820 tcc_error("struct/union/enum already defined");
2821 /* cannot be empty */
2822 c = 0;
2823 /* non empty enums are not allowed */
2824 if (a == TOK_ENUM) {
2825 for(;;) {
2826 v = tok;
2827 if (v < TOK_UIDENT)
2828 expect("identifier");
2829 ss = sym_find(v);
2830 if (ss && !local_stack)
2831 tcc_error("redefinition of enumerator '%s'",
2832 get_tok_str(v, NULL));
2833 next();
2834 if (tok == '=') {
2835 next();
2836 c = expr_const();
2838 /* enum symbols have static storage */
2839 ss = sym_push(v, &int_type, VT_CONST, c);
2840 ss->type.t |= VT_STATIC;
2841 if (tok != ',')
2842 break;
2843 next();
2844 c++;
2845 /* NOTE: we accept a trailing comma */
2846 if (tok == '}')
2847 break;
2849 s->c = type_size(&int_type, &align);
2850 skip('}');
2851 } else {
2852 maxalign = 1;
2853 ps = &s->next;
2854 prevbt = VT_INT;
2855 bit_pos = 0;
2856 offset = 0;
2857 flexible = 0;
2858 while (tok != '}') {
2859 parse_btype(&btype, &ad);
2860 while (1) {
2861 if (flexible)
2862 tcc_error("flexible array member '%s' not at the end of struct",
2863 get_tok_str(v, NULL));
2864 bit_size = -1;
2865 v = 0;
2866 type1 = btype;
2867 if (tok != ':') {
2868 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2869 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2870 expect("identifier");
2871 if (type_size(&type1, &align) < 0) {
2872 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2873 flexible = 1;
2874 else
2875 tcc_error("field '%s' has incomplete type",
2876 get_tok_str(v, NULL));
2878 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2879 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2880 tcc_error("invalid type for '%s'",
2881 get_tok_str(v, NULL));
2883 if (tok == ':') {
2884 next();
2885 bit_size = expr_const();
2886 /* XXX: handle v = 0 case for messages */
2887 if (bit_size < 0)
2888 tcc_error("negative width in bit-field '%s'",
2889 get_tok_str(v, NULL));
2890 if (v && bit_size == 0)
2891 tcc_error("zero width for bit-field '%s'",
2892 get_tok_str(v, NULL));
2894 size = type_size(&type1, &align);
2895 if (ad.a.aligned) {
2896 if (align < ad.a.aligned)
2897 align = ad.a.aligned;
2898 } else if (ad.a.packed) {
2899 align = 1;
2900 } else if (*tcc_state->pack_stack_ptr) {
2901 if (align > *tcc_state->pack_stack_ptr)
2902 align = *tcc_state->pack_stack_ptr;
2904 lbit_pos = 0;
2905 if (bit_size >= 0) {
2906 bt = type1.t & VT_BTYPE;
2907 if (bt != VT_INT &&
2908 bt != VT_BYTE &&
2909 bt != VT_SHORT &&
2910 bt != VT_BOOL &&
2911 bt != VT_ENUM &&
2912 bt != VT_LLONG)
2913 tcc_error("bitfields must have scalar type");
2914 bsize = size * 8;
2915 if (bit_size > bsize) {
2916 tcc_error("width of '%s' exceeds its type",
2917 get_tok_str(v, NULL));
2918 } else if (bit_size == bsize) {
2919 /* no need for bit fields */
2920 bit_pos = 0;
2921 } else if (bit_size == 0) {
2922 /* XXX: what to do if only padding in a
2923 structure ? */
2924 /* zero size: means to pad */
2925 bit_pos = 0;
2926 } else {
2927 /* we do not have enough room ?
2928 did the type change?
2929 is it a union? */
2930 if ((bit_pos + bit_size) > bsize ||
2931 bt != prevbt || a == TOK_UNION)
2932 bit_pos = 0;
2933 lbit_pos = bit_pos;
2934 /* XXX: handle LSB first */
2935 type1.t |= VT_BITFIELD |
2936 (bit_pos << VT_STRUCT_SHIFT) |
2937 (bit_size << (VT_STRUCT_SHIFT + 6));
2938 bit_pos += bit_size;
2940 prevbt = bt;
2941 } else {
2942 bit_pos = 0;
2944 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2945 /* add new memory data only if starting
2946 bit field */
2947 if (lbit_pos == 0) {
2948 if (a == TOK_STRUCT) {
2949 c = (c + align - 1) & -align;
2950 offset = c;
2951 if (size > 0)
2952 c += size;
2953 } else {
2954 offset = 0;
2955 if (size > c)
2956 c = size;
2958 if (align > maxalign)
2959 maxalign = align;
2961 #if 0
2962 printf("add field %s offset=%d",
2963 get_tok_str(v, NULL), offset);
2964 if (type1.t & VT_BITFIELD) {
2965 printf(" pos=%d size=%d",
2966 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2967 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2969 printf("\n");
2970 #endif
2972 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2973 ass = type1.ref;
2974 while ((ass = ass->next) != NULL) {
2975 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2976 *ps = ss;
2977 ps = &ss->next;
2979 } else if (v) {
2980 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2981 *ps = ss;
2982 ps = &ss->next;
2984 if (tok == ';' || tok == TOK_EOF)
2985 break;
2986 skip(',');
2988 skip(';');
2990 skip('}');
2991 /* store size and alignment */
2992 s->c = (c + maxalign - 1) & -maxalign;
2993 s->r = maxalign;
2998 /* return 1 if basic type is a type size (short, long, long long) */
2999 int is_btype_size (int bt)
3001 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
3004 /* return 0 if no type declaration. otherwise, return the basic type
3005 and skip it.
3007 static int parse_btype(CType *type, AttributeDef *ad)
3009 int t, u, bt_size, complete, type_found, typespec_found;
3010 Sym *s;
3011 CType type1;
3013 memset(ad, 0, sizeof(AttributeDef));
3014 complete = 0;
3015 type_found = 0;
3016 typespec_found = 0;
3017 t = 0;
3018 while(1) {
3019 switch(tok) {
3020 case TOK_EXTENSION:
3021 /* currently, we really ignore extension */
3022 next();
3023 continue;
3025 /* basic types */
3026 case TOK_CHAR:
3027 u = VT_BYTE;
3028 basic_type:
3029 next();
3030 basic_type1:
3031 if (complete)
3032 tcc_error("too many basic types");
3033 t |= u;
3034 bt_size = is_btype_size (u & VT_BTYPE);
3035 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3036 complete = 1;
3037 typespec_found = 1;
3038 break;
3039 case TOK_VOID:
3040 u = VT_VOID;
3041 goto basic_type;
3042 case TOK_SHORT:
3043 u = VT_SHORT;
3044 goto basic_type;
3045 case TOK_INT:
3046 u = VT_INT;
3047 goto basic_type;
3048 case TOK_LONG:
3049 next();
3050 if ((t & VT_BTYPE) == VT_DOUBLE) {
3051 #ifndef TCC_TARGET_PE
3052 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3053 #endif
3054 } else if ((t & VT_BTYPE) == VT_LONG) {
3055 t = (t & ~VT_BTYPE) | VT_LLONG;
3056 } else {
3057 u = VT_LONG;
3058 goto basic_type1;
3060 break;
3061 case TOK_BOOL:
3062 u = VT_BOOL;
3063 goto basic_type;
3064 case TOK_FLOAT:
3065 u = VT_FLOAT;
3066 goto basic_type;
3067 case TOK_DOUBLE:
3068 next();
3069 if ((t & VT_BTYPE) == VT_LONG) {
3070 #ifdef TCC_TARGET_PE
3071 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3072 #else
3073 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3074 #endif
3075 } else {
3076 u = VT_DOUBLE;
3077 goto basic_type1;
3079 break;
3080 case TOK_ENUM:
3081 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3082 basic_type2:
3083 u = type1.t;
3084 type->ref = type1.ref;
3085 goto basic_type1;
3086 case TOK_STRUCT:
3087 case TOK_UNION:
3088 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3089 goto basic_type2;
3091 /* type modifiers */
3092 case TOK_CONST1:
3093 case TOK_CONST2:
3094 case TOK_CONST3:
3095 t |= VT_CONSTANT;
3096 next();
3097 break;
3098 case TOK_VOLATILE1:
3099 case TOK_VOLATILE2:
3100 case TOK_VOLATILE3:
3101 t |= VT_VOLATILE;
3102 next();
3103 break;
3104 case TOK_SIGNED1:
3105 case TOK_SIGNED2:
3106 case TOK_SIGNED3:
3107 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3108 tcc_error("signed and unsigned modifier");
3109 typespec_found = 1;
3110 t |= VT_DEFSIGN;
3111 next();
3112 break;
3113 case TOK_REGISTER:
3114 case TOK_AUTO:
3115 case TOK_RESTRICT1:
3116 case TOK_RESTRICT2:
3117 case TOK_RESTRICT3:
3118 next();
3119 break;
3120 case TOK_UNSIGNED:
3121 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3122 tcc_error("signed and unsigned modifier");
3123 t |= VT_DEFSIGN | VT_UNSIGNED;
3124 next();
3125 typespec_found = 1;
3126 break;
3128 /* storage */
3129 case TOK_EXTERN:
3130 t |= VT_EXTERN;
3131 next();
3132 break;
3133 case TOK_STATIC:
3134 t |= VT_STATIC;
3135 next();
3136 break;
3137 case TOK_TYPEDEF:
3138 t |= VT_TYPEDEF;
3139 next();
3140 break;
3141 case TOK_INLINE1:
3142 case TOK_INLINE2:
3143 case TOK_INLINE3:
3144 t |= VT_INLINE;
3145 next();
3146 break;
3148 /* GNUC attribute */
3149 case TOK_ATTRIBUTE1:
3150 case TOK_ATTRIBUTE2:
3151 parse_attribute(ad);
3152 if (ad->a.mode) {
3153 u = ad->a.mode -1;
3154 t = (t & ~VT_BTYPE) | u;
3156 break;
3157 /* GNUC typeof */
3158 case TOK_TYPEOF1:
3159 case TOK_TYPEOF2:
3160 case TOK_TYPEOF3:
3161 next();
3162 parse_expr_type(&type1);
3163 /* remove all storage modifiers except typedef */
3164 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3165 goto basic_type2;
3166 default:
3167 if (typespec_found)
3168 goto the_end;
3169 s = sym_find(tok);
3170 if (!s || !(s->type.t & VT_TYPEDEF))
3171 goto the_end;
3172 t |= (s->type.t & ~VT_TYPEDEF);
3173 type->ref = s->type.ref;
3174 if (s->r) {
3175 /* get attributes from typedef */
3176 if (0 == ad->a.aligned)
3177 ad->a.aligned = s->a.aligned;
3178 if (0 == ad->a.func_call)
3179 ad->a.func_call = s->a.func_call;
3180 ad->a.packed |= s->a.packed;
3182 next();
3183 typespec_found = 1;
3184 break;
3186 type_found = 1;
3188 the_end:
3189 if (tcc_state->char_is_unsigned) {
3190 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3191 t |= VT_UNSIGNED;
3194 /* long is never used as type */
3195 if ((t & VT_BTYPE) == VT_LONG)
3196 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3197 t = (t & ~VT_BTYPE) | VT_INT;
3198 #else
3199 t = (t & ~VT_BTYPE) | VT_LLONG;
3200 #endif
3201 type->t = t;
3202 return type_found;
3205 /* convert a function parameter type (array to pointer and function to
3206 function pointer) */
3207 static inline void convert_parameter_type(CType *pt)
3209 /* remove const and volatile qualifiers (XXX: const could be used
3210 to indicate a const function parameter */
3211 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3212 /* array must be transformed to pointer according to ANSI C */
3213 pt->t &= ~VT_ARRAY;
3214 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3215 mk_pointer(pt);
3219 ST_FUNC void parse_asm_str(CString *astr)
3221 skip('(');
3222 /* read the string */
3223 if (tok != TOK_STR)
3224 expect("string constant");
3225 cstr_new(astr);
3226 while (tok == TOK_STR) {
3227 /* XXX: add \0 handling too ? */
3228 cstr_cat(astr, tokc.cstr->data);
3229 next();
3231 cstr_ccat(astr, '\0');
3234 /* Parse an asm label and return the label
3235 * Don't forget to free the CString in the caller! */
3236 static void asm_label_instr(CString *astr)
3238 next();
3239 parse_asm_str(astr);
3240 skip(')');
3241 #ifdef ASM_DEBUG
3242 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3243 #endif
3246 static void post_type(CType *type, AttributeDef *ad)
3248 int n, l, t1, arg_size, align;
3249 Sym **plast, *s, *first;
3250 AttributeDef ad1;
3251 CType pt;
3253 if (tok == '(') {
3254 /* function declaration */
3255 next();
3256 l = 0;
3257 first = NULL;
3258 plast = &first;
3259 arg_size = 0;
3260 if (tok != ')') {
3261 for(;;) {
3262 /* read param name and compute offset */
3263 if (l != FUNC_OLD) {
3264 if (!parse_btype(&pt, &ad1)) {
3265 if (l) {
3266 tcc_error("invalid type");
3267 } else {
3268 l = FUNC_OLD;
3269 goto old_proto;
3272 l = FUNC_NEW;
3273 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3274 break;
3275 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3276 if ((pt.t & VT_BTYPE) == VT_VOID)
3277 tcc_error("parameter declared as void");
3278 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3279 } else {
3280 old_proto:
3281 n = tok;
3282 if (n < TOK_UIDENT)
3283 expect("identifier");
3284 pt.t = VT_INT;
3285 next();
3287 convert_parameter_type(&pt);
3288 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3289 *plast = s;
3290 plast = &s->next;
3291 if (tok == ')')
3292 break;
3293 skip(',');
3294 if (l == FUNC_NEW && tok == TOK_DOTS) {
3295 l = FUNC_ELLIPSIS;
3296 next();
3297 break;
3301 /* if no parameters, then old type prototype */
3302 if (l == 0)
3303 l = FUNC_OLD;
3304 skip(')');
3305 /* NOTE: const is ignored in returned type as it has a special
3306 meaning in gcc / C++ */
3307 type->t &= ~VT_CONSTANT;
3308 /* some ancient pre-K&R C allows a function to return an array
3309 and the array brackets to be put after the arguments, such
3310 that "int c()[]" means something like "int[] c()" */
3311 if (tok == '[') {
3312 next();
3313 skip(']'); /* only handle simple "[]" */
3314 type->t |= VT_PTR;
3316 /* we push a anonymous symbol which will contain the function prototype */
3317 ad->a.func_args = arg_size;
3318 s = sym_push(SYM_FIELD, type, 0, l);
3319 s->a = ad->a;
3320 s->next = first;
3321 type->t = VT_FUNC;
3322 type->ref = s;
3323 } else if (tok == '[') {
3324 /* array definition */
3325 next();
3326 if (tok == TOK_RESTRICT1)
3327 next();
3328 n = -1;
3329 t1 = 0;
3330 if (tok != ']') {
3331 if (!local_stack || nocode_wanted)
3332 vpushi(expr_const());
3333 else gexpr();
3334 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3335 n = vtop->c.i;
3336 if (n < 0)
3337 tcc_error("invalid array size");
3338 } else {
3339 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3340 tcc_error("size of variable length array should be an integer");
3341 t1 = VT_VLA;
3344 skip(']');
3345 /* parse next post type */
3346 post_type(type, ad);
3347 if (type->t == VT_FUNC)
3348 tcc_error("declaration of an array of functions");
3349 t1 |= type->t & VT_VLA;
3351 if (t1 & VT_VLA) {
3352 loc -= type_size(&int_type, &align);
3353 loc &= -align;
3354 n = loc;
3356 vla_runtime_type_size(type, &align);
3357 gen_op('*');
3358 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3359 vswap();
3360 vstore();
3362 if (n != -1)
3363 vpop();
3365 /* we push an anonymous symbol which will contain the array
3366 element type */
3367 s = sym_push(SYM_FIELD, type, 0, n);
3368 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3369 type->ref = s;
3373 /* Parse a type declaration (except basic type), and return the type
3374 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3375 expected. 'type' should contain the basic type. 'ad' is the
3376 attribute definition of the basic type. It can be modified by
3377 type_decl().
3379 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3381 Sym *s;
3382 CType type1, *type2;
3383 int qualifiers, storage;
3385 while (tok == '*') {
3386 qualifiers = 0;
3387 redo:
3388 next();
3389 switch(tok) {
3390 case TOK_CONST1:
3391 case TOK_CONST2:
3392 case TOK_CONST3:
3393 qualifiers |= VT_CONSTANT;
3394 goto redo;
3395 case TOK_VOLATILE1:
3396 case TOK_VOLATILE2:
3397 case TOK_VOLATILE3:
3398 qualifiers |= VT_VOLATILE;
3399 goto redo;
3400 case TOK_RESTRICT1:
3401 case TOK_RESTRICT2:
3402 case TOK_RESTRICT3:
3403 goto redo;
3405 mk_pointer(type);
3406 type->t |= qualifiers;
3409 /* XXX: clarify attribute handling */
3410 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3411 parse_attribute(ad);
3413 /* recursive type */
3414 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3415 type1.t = 0; /* XXX: same as int */
3416 if (tok == '(') {
3417 next();
3418 /* XXX: this is not correct to modify 'ad' at this point, but
3419 the syntax is not clear */
3420 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3421 parse_attribute(ad);
3422 type_decl(&type1, ad, v, td);
3423 skip(')');
3424 } else {
3425 /* type identifier */
3426 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3427 *v = tok;
3428 next();
3429 } else {
3430 if (!(td & TYPE_ABSTRACT))
3431 expect("identifier");
3432 *v = 0;
3435 storage = type->t & VT_STORAGE;
3436 type->t &= ~VT_STORAGE;
3437 if (storage & VT_STATIC) {
3438 int saved_nocode_wanted = nocode_wanted;
3439 nocode_wanted = 1;
3440 post_type(type, ad);
3441 nocode_wanted = saved_nocode_wanted;
3442 } else
3443 post_type(type, ad);
3444 type->t |= storage;
3445 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3446 parse_attribute(ad);
3448 if (!type1.t)
3449 return;
3450 /* append type at the end of type1 */
3451 type2 = &type1;
3452 for(;;) {
3453 s = type2->ref;
3454 type2 = &s->type;
3455 if (!type2->t) {
3456 *type2 = *type;
3457 break;
3460 *type = type1;
3463 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3464 ST_FUNC int lvalue_type(int t)
3466 int bt, r;
3467 r = VT_LVAL;
3468 bt = t & VT_BTYPE;
3469 if (bt == VT_BYTE || bt == VT_BOOL)
3470 r |= VT_LVAL_BYTE;
3471 else if (bt == VT_SHORT)
3472 r |= VT_LVAL_SHORT;
3473 else
3474 return r;
3475 if (t & VT_UNSIGNED)
3476 r |= VT_LVAL_UNSIGNED;
3477 return r;
3480 /* indirection with full error checking and bound check */
3481 ST_FUNC void indir(void)
3483 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3484 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3485 return;
3486 expect("pointer");
3488 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3489 gv(RC_INT);
3490 vtop->type = *pointed_type(&vtop->type);
3491 /* Arrays and functions are never lvalues */
3492 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3493 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3494 vtop->r |= lvalue_type(vtop->type.t);
3495 /* if bound checking, the referenced pointer must be checked */
3496 #ifdef CONFIG_TCC_BCHECK
3497 if (tcc_state->do_bounds_check)
3498 vtop->r |= VT_MUSTBOUND;
3499 #endif
3503 /* pass a parameter to a function and do type checking and casting */
3504 static void gfunc_param_typed(Sym *func, Sym *arg)
3506 int func_type;
3507 CType type;
3509 func_type = func->c;
3510 if (func_type == FUNC_OLD ||
3511 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3512 /* default casting : only need to convert float to double */
3513 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3514 type.t = VT_DOUBLE;
3515 gen_cast(&type);
3516 } else if (vtop->type.t & VT_BITFIELD) {
3517 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3518 gen_cast(&type);
3520 } else if (arg == NULL) {
3521 tcc_error("too many arguments to function");
3522 } else {
3523 type = arg->type;
3524 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3525 gen_assign_cast(&type);
3529 /* parse an expression of the form '(type)' or '(expr)' and return its
3530 type */
3531 static void parse_expr_type(CType *type)
3533 int n;
3534 AttributeDef ad;
3536 skip('(');
3537 if (parse_btype(type, &ad)) {
3538 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3539 } else {
3540 expr_type(type);
3542 skip(')');
3545 static void parse_type(CType *type)
3547 AttributeDef ad;
3548 int n;
3550 if (!parse_btype(type, &ad)) {
3551 expect("type");
3553 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3556 static void vpush_tokc(int t)
3558 CType type;
3559 type.t = t;
3560 type.ref = 0;
3561 vsetc(&type, VT_CONST, &tokc);
3564 ST_FUNC void unary(void)
3566 int n, t, align, size, r, sizeof_caller;
3567 CType type;
3568 Sym *s;
3569 AttributeDef ad;
3570 static int in_sizeof = 0;
3572 sizeof_caller = in_sizeof;
3573 in_sizeof = 0;
3574 /* XXX: GCC 2.95.3 does not generate a table although it should be
3575 better here */
3576 tok_next:
3577 switch(tok) {
3578 case TOK_EXTENSION:
3579 next();
3580 goto tok_next;
3581 case TOK_CINT:
3582 case TOK_CCHAR:
3583 case TOK_LCHAR:
3584 vpushi(tokc.i);
3585 next();
3586 break;
3587 case TOK_CUINT:
3588 vpush_tokc(VT_INT | VT_UNSIGNED);
3589 next();
3590 break;
3591 case TOK_CLLONG:
3592 vpush_tokc(VT_LLONG);
3593 next();
3594 break;
3595 case TOK_CULLONG:
3596 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3597 next();
3598 break;
3599 case TOK_CFLOAT:
3600 vpush_tokc(VT_FLOAT);
3601 next();
3602 break;
3603 case TOK_CDOUBLE:
3604 vpush_tokc(VT_DOUBLE);
3605 next();
3606 break;
3607 case TOK_CLDOUBLE:
3608 vpush_tokc(VT_LDOUBLE);
3609 next();
3610 break;
3611 case TOK___FUNCTION__:
3612 if (!gnu_ext)
3613 goto tok_identifier;
3614 /* fall thru */
3615 case TOK___FUNC__:
3617 void *ptr;
3618 int len;
3619 /* special function name identifier */
3620 len = strlen(funcname) + 1;
3621 /* generate char[len] type */
3622 type.t = VT_BYTE;
3623 mk_pointer(&type);
3624 type.t |= VT_ARRAY;
3625 type.ref->c = len;
3626 vpush_ref(&type, data_section, data_section->data_offset, len);
3627 ptr = section_ptr_add(data_section, len);
3628 memcpy(ptr, funcname, len);
3629 next();
3631 break;
3632 case TOK_LSTR:
3633 #ifdef TCC_TARGET_PE
3634 t = VT_SHORT | VT_UNSIGNED;
3635 #else
3636 t = VT_INT;
3637 #endif
3638 goto str_init;
3639 case TOK_STR:
3640 /* string parsing */
3641 t = VT_BYTE;
3642 str_init:
3643 if (tcc_state->warn_write_strings)
3644 t |= VT_CONSTANT;
3645 type.t = t;
3646 mk_pointer(&type);
3647 type.t |= VT_ARRAY;
3648 memset(&ad, 0, sizeof(AttributeDef));
3649 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3650 break;
3651 case '(':
3652 next();
3653 /* cast ? */
3654 if (parse_btype(&type, &ad)) {
3655 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3656 skip(')');
3657 /* check ISOC99 compound literal */
3658 if (tok == '{') {
3659 /* data is allocated locally by default */
3660 if (global_expr)
3661 r = VT_CONST;
3662 else
3663 r = VT_LOCAL;
3664 /* all except arrays are lvalues */
3665 if (!(type.t & VT_ARRAY))
3666 r |= lvalue_type(type.t);
3667 memset(&ad, 0, sizeof(AttributeDef));
3668 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3669 } else {
3670 if (sizeof_caller) {
3671 vpush(&type);
3672 return;
3674 unary();
3675 gen_cast(&type);
3677 } else if (tok == '{') {
3678 /* save all registers */
3679 save_regs(0);
3680 /* statement expression : we do not accept break/continue
3681 inside as GCC does */
3682 block(NULL, NULL, NULL, NULL, 0, 1);
3683 skip(')');
3684 } else {
3685 gexpr();
3686 skip(')');
3688 break;
3689 case '*':
3690 next();
3691 unary();
3692 indir();
3693 break;
3694 case '&':
3695 next();
3696 unary();
3697 /* functions names must be treated as function pointers,
3698 except for unary '&' and sizeof. Since we consider that
3699 functions are not lvalues, we only have to handle it
3700 there and in function calls. */
3701 /* arrays can also be used although they are not lvalues */
3702 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3703 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3704 test_lvalue();
3705 mk_pointer(&vtop->type);
3706 gaddrof();
3707 break;
3708 case '!':
3709 next();
3710 unary();
3711 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3712 CType boolean;
3713 boolean.t = VT_BOOL;
3714 gen_cast(&boolean);
3715 vtop->c.i = !vtop->c.i;
3716 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3717 vtop->c.i = vtop->c.i ^ 1;
3718 else {
3719 save_regs(1);
3720 vseti(VT_JMP, gvtst(1, 0));
3722 break;
3723 case '~':
3724 next();
3725 unary();
3726 vpushi(-1);
3727 gen_op('^');
3728 break;
3729 case '+':
3730 next();
3731 unary();
3732 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3733 tcc_error("pointer not accepted for unary plus");
3734 /* In order to force cast, we add zero, except for floating point
3735 where we really need an noop (otherwise -0.0 will be transformed
3736 into +0.0). */
3737 if (!is_float(vtop->type.t)) {
3738 vpushi(0);
3739 gen_op('+');
3741 break;
3742 case TOK_SIZEOF:
3743 case TOK_ALIGNOF1:
3744 case TOK_ALIGNOF2:
3745 t = tok;
3746 next();
3747 in_sizeof++;
3748 unary_type(&type); // Perform a in_sizeof = 0;
3749 size = type_size(&type, &align);
3750 if (t == TOK_SIZEOF) {
3751 if (!(type.t & VT_VLA)) {
3752 if (size < 0)
3753 tcc_error("sizeof applied to an incomplete type");
3754 vpushs(size);
3755 } else {
3756 vla_runtime_type_size(&type, &align);
3758 } else {
3759 vpushs(align);
3761 vtop->type.t |= VT_UNSIGNED;
3762 break;
3764 case TOK_builtin_types_compatible_p:
3766 CType type1, type2;
3767 next();
3768 skip('(');
3769 parse_type(&type1);
3770 skip(',');
3771 parse_type(&type2);
3772 skip(')');
3773 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3774 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3775 vpushi(is_compatible_types(&type1, &type2));
3777 break;
3778 case TOK_builtin_constant_p:
3780 int saved_nocode_wanted, res;
3781 next();
3782 skip('(');
3783 saved_nocode_wanted = nocode_wanted;
3784 nocode_wanted = 1;
3785 gexpr();
3786 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3787 vpop();
3788 nocode_wanted = saved_nocode_wanted;
3789 skip(')');
3790 vpushi(res);
3792 break;
3793 case TOK_builtin_frame_address:
3795 int level;
3796 CType type;
3797 next();
3798 skip('(');
3799 if (tok != TOK_CINT || tokc.i < 0) {
3800 tcc_error("__builtin_frame_address only takes positive integers");
3802 level = tokc.i;
3803 next();
3804 skip(')');
3805 type.t = VT_VOID;
3806 mk_pointer(&type);
3807 vset(&type, VT_LOCAL, 0); /* local frame */
3808 while (level--) {
3809 mk_pointer(&vtop->type);
3810 indir(); /* -> parent frame */
3813 break;
3814 #ifdef TCC_TARGET_X86_64
3815 #ifdef TCC_TARGET_PE
3816 case TOK_builtin_va_start:
3818 next();
3819 skip('(');
3820 expr_eq();
3821 skip(',');
3822 expr_eq();
3823 skip(')');
3824 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3825 tcc_error("__builtin_va_start expects a local variable");
3826 vtop->r &= ~(VT_LVAL | VT_REF);
3827 vtop->type = char_pointer_type;
3828 vstore();
3830 break;
3831 #else
3832 case TOK_builtin_va_arg_types:
3834 CType type;
3835 next();
3836 skip('(');
3837 parse_type(&type);
3838 skip(')');
3839 vpushi(classify_x86_64_va_arg(&type));
3841 break;
3842 #endif
3843 #endif
3844 case TOK_INC:
3845 case TOK_DEC:
3846 t = tok;
3847 next();
3848 unary();
3849 inc(0, t);
3850 break;
3851 case '-':
3852 next();
3853 unary();
3854 t = vtop->type.t & VT_BTYPE;
3855 if (is_float(t)) {
3856 /* In IEEE negate(x) isn't subtract(0,x), but rather
3857 subtract(-0, x). */
3858 vpush(&vtop->type);
3859 if (t == VT_FLOAT)
3860 vtop->c.f = -0.0f;
3861 else if (t == VT_DOUBLE)
3862 vtop->c.d = -0.0;
3863 else
3864 vtop->c.ld = -0.0;
3865 } else
3866 vpushi(0);
3867 vswap();
3868 gen_op('-');
3869 break;
3870 case TOK_LAND:
3871 if (!gnu_ext)
3872 goto tok_identifier;
3873 next();
3874 /* allow to take the address of a label */
3875 if (tok < TOK_UIDENT)
3876 expect("label identifier");
3877 s = label_find(tok);
3878 if (!s) {
3879 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3880 } else {
3881 if (s->r == LABEL_DECLARED)
3882 s->r = LABEL_FORWARD;
3884 if (!s->type.t) {
3885 s->type.t = VT_VOID;
3886 mk_pointer(&s->type);
3887 s->type.t |= VT_STATIC;
3889 vset(&s->type, VT_CONST | VT_SYM, 0);
3890 vtop->sym = s;
3891 next();
3892 break;
3894 // special qnan , snan and infinity values
3895 case TOK___NAN__:
3896 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3897 next();
3898 break;
3899 case TOK___SNAN__:
3900 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3901 next();
3902 break;
3903 case TOK___INF__:
3904 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3905 next();
3906 break;
3908 default:
3909 tok_identifier:
3910 t = tok;
3911 next();
3912 if (t < TOK_UIDENT)
3913 expect("identifier");
3914 s = sym_find(t);
3915 if (!s) {
3916 if (tok != '(')
3917 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3918 /* for simple function calls, we tolerate undeclared
3919 external reference to int() function */
3920 if (tcc_state->warn_implicit_function_declaration)
3921 tcc_warning("implicit declaration of function '%s'",
3922 get_tok_str(t, NULL));
3923 s = external_global_sym(t, &func_old_type, 0);
3925 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3926 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3927 /* if referencing an inline function, then we generate a
3928 symbol to it if not already done. It will have the
3929 effect to generate code for it at the end of the
3930 compilation unit. Inline function as always
3931 generated in the text section. */
3932 if (!s->c)
3933 put_extern_sym(s, text_section, 0, 0);
3934 r = VT_SYM | VT_CONST;
3935 } else {
3936 r = s->r;
3938 vset(&s->type, r, s->c);
3939 /* if forward reference, we must point to s */
3940 if (vtop->r & VT_SYM) {
3941 vtop->sym = s;
3942 vtop->c.ull = 0;
3944 break;
3947 /* post operations */
3948 while (1) {
3949 if (tok == TOK_INC || tok == TOK_DEC) {
3950 inc(1, tok);
3951 next();
3952 } else if (tok == '.' || tok == TOK_ARROW) {
3953 int qualifiers;
3954 /* field */
3955 if (tok == TOK_ARROW)
3956 indir();
3957 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3958 test_lvalue();
3959 gaddrof();
3960 next();
3961 /* expect pointer on structure */
3962 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3963 expect("struct or union");
3964 s = vtop->type.ref;
3965 /* find field */
3966 tok |= SYM_FIELD;
3967 while ((s = s->next) != NULL) {
3968 if (s->v == tok)
3969 break;
3971 if (!s)
3972 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3973 /* add field offset to pointer */
3974 vtop->type = char_pointer_type; /* change type to 'char *' */
3975 vpushi(s->c);
3976 gen_op('+');
3977 /* change type to field type, and set to lvalue */
3978 vtop->type = s->type;
3979 vtop->type.t |= qualifiers;
3980 /* an array is never an lvalue */
3981 if (!(vtop->type.t & VT_ARRAY)) {
3982 vtop->r |= lvalue_type(vtop->type.t);
3983 #ifdef CONFIG_TCC_BCHECK
3984 /* if bound checking, the referenced pointer must be checked */
3985 if (tcc_state->do_bounds_check)
3986 vtop->r |= VT_MUSTBOUND;
3987 #endif
3989 next();
3990 } else if (tok == '[') {
3991 next();
3992 gexpr();
3993 gen_op('+');
3994 indir();
3995 skip(']');
3996 } else if (tok == '(') {
3997 SValue ret;
3998 Sym *sa;
3999 int nb_args, ret_nregs, ret_align, variadic;
4001 /* function call */
4002 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
4003 /* pointer test (no array accepted) */
4004 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4005 vtop->type = *pointed_type(&vtop->type);
4006 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4007 goto error_func;
4008 } else {
4009 error_func:
4010 expect("function pointer");
4012 } else {
4013 vtop->r &= ~VT_LVAL; /* no lvalue */
4015 /* get return type */
4016 s = vtop->type.ref;
4017 next();
4018 sa = s->next; /* first parameter */
4019 nb_args = 0;
4020 ret.r2 = VT_CONST;
4021 /* compute first implicit argument if a structure is returned */
4022 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4023 variadic = (s->c == FUNC_ELLIPSIS);
4024 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4025 &ret_align);
4026 if (!ret_nregs) {
4027 /* get some space for the returned structure */
4028 size = type_size(&s->type, &align);
4029 loc = (loc - size) & -align;
4030 ret.type = s->type;
4031 ret.r = VT_LOCAL | VT_LVAL;
4032 /* pass it as 'int' to avoid structure arg passing
4033 problems */
4034 vseti(VT_LOCAL, loc);
4035 ret.c = vtop->c;
4036 nb_args++;
4038 } else {
4039 ret_nregs = 1;
4040 ret.type = s->type;
4043 if (ret_nregs) {
4044 /* return in register */
4045 if (is_float(ret.type.t)) {
4046 ret.r = reg_fret(ret.type.t);
4047 #ifdef TCC_TARGET_X86_64
4048 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4049 ret.r2 = REG_QRET;
4050 #endif
4051 } else {
4052 #ifdef TCC_TARGET_X86_64
4053 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4054 #else
4055 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4056 #endif
4057 ret.r2 = REG_LRET;
4058 ret.r = REG_IRET;
4060 ret.c.i = 0;
4062 if (tok != ')') {
4063 for(;;) {
4064 expr_eq();
4065 gfunc_param_typed(s, sa);
4066 nb_args++;
4067 if (sa)
4068 sa = sa->next;
4069 if (tok == ')')
4070 break;
4071 skip(',');
4074 if (sa)
4075 tcc_error("too few arguments to function");
4076 skip(')');
4077 if (!nocode_wanted) {
4078 gfunc_call(nb_args);
4079 } else {
4080 vtop -= (nb_args + 1);
4083 /* return value */
4084 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4085 vsetc(&ret.type, r, &ret.c);
4086 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4089 /* handle packed struct return */
4090 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4091 int addr, offset;
4093 size = type_size(&s->type, &align);
4094 loc = (loc - size) & -align;
4095 addr = loc;
4096 offset = 0;
4097 for (;;) {
4098 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4099 vswap();
4100 vstore();
4101 vtop--;
4102 if (--ret_nregs == 0)
4103 break;
4104 /* XXX: compatible with arm only: ret_align == register_size */
4105 offset += ret_align;
4107 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4109 } else {
4110 break;
4115 ST_FUNC void expr_prod(void)
4117 int t;
4119 unary();
4120 while (tok == '*' || tok == '/' || tok == '%') {
4121 t = tok;
4122 next();
4123 unary();
4124 gen_op(t);
4128 ST_FUNC void expr_sum(void)
4130 int t;
4132 expr_prod();
4133 while (tok == '+' || tok == '-') {
4134 t = tok;
4135 next();
4136 expr_prod();
4137 gen_op(t);
4141 static void expr_shift(void)
4143 int t;
4145 expr_sum();
4146 while (tok == TOK_SHL || tok == TOK_SAR) {
4147 t = tok;
4148 next();
4149 expr_sum();
4150 gen_op(t);
4154 static void expr_cmp(void)
4156 int t;
4158 expr_shift();
4159 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4160 tok == TOK_ULT || tok == TOK_UGE) {
4161 t = tok;
4162 next();
4163 expr_shift();
4164 gen_op(t);
4168 static void expr_cmpeq(void)
4170 int t;
4172 expr_cmp();
4173 while (tok == TOK_EQ || tok == TOK_NE) {
4174 t = tok;
4175 next();
4176 expr_cmp();
4177 gen_op(t);
4181 static void expr_and(void)
4183 expr_cmpeq();
4184 while (tok == '&') {
4185 next();
4186 expr_cmpeq();
4187 gen_op('&');
4191 static void expr_xor(void)
4193 expr_and();
4194 while (tok == '^') {
4195 next();
4196 expr_and();
4197 gen_op('^');
4201 static void expr_or(void)
4203 expr_xor();
4204 while (tok == '|') {
4205 next();
4206 expr_xor();
4207 gen_op('|');
4211 /* XXX: fix this mess */
4212 static void expr_land_const(void)
4214 expr_or();
4215 while (tok == TOK_LAND) {
4216 next();
4217 expr_or();
4218 gen_op(TOK_LAND);
4222 /* XXX: fix this mess */
4223 static void expr_lor_const(void)
4225 expr_land_const();
4226 while (tok == TOK_LOR) {
4227 next();
4228 expr_land_const();
4229 gen_op(TOK_LOR);
4233 /* only used if non constant */
4234 static void expr_land(void)
4236 int t;
4238 expr_or();
4239 if (tok == TOK_LAND) {
4240 t = 0;
4241 save_regs(1);
4242 for(;;) {
4243 t = gvtst(1, t);
4244 if (tok != TOK_LAND) {
4245 vseti(VT_JMPI, t);
4246 break;
4248 next();
4249 expr_or();
4254 static void expr_lor(void)
4256 int t;
4258 expr_land();
4259 if (tok == TOK_LOR) {
4260 t = 0;
4261 save_regs(1);
4262 for(;;) {
4263 t = gvtst(0, t);
4264 if (tok != TOK_LOR) {
4265 vseti(VT_JMP, t);
4266 break;
4268 next();
4269 expr_land();
4274 /* XXX: better constant handling */
4275 static void expr_cond(void)
4277 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4278 SValue sv;
4279 CType type, type1, type2;
4281 if (const_wanted) {
4282 expr_lor_const();
4283 if (tok == '?') {
4284 CType boolean;
4285 int c;
4286 boolean.t = VT_BOOL;
4287 vdup();
4288 gen_cast(&boolean);
4289 c = vtop->c.i;
4290 vpop();
4291 next();
4292 if (tok != ':' || !gnu_ext) {
4293 vpop();
4294 gexpr();
4296 if (!c)
4297 vpop();
4298 skip(':');
4299 expr_cond();
4300 if (c)
4301 vpop();
4303 } else {
4304 expr_lor();
4305 if (tok == '?') {
4306 next();
4307 if (vtop != vstack) {
4308 /* needed to avoid having different registers saved in
4309 each branch */
4310 if (is_float(vtop->type.t)) {
4311 rc = RC_FLOAT;
4312 #ifdef TCC_TARGET_X86_64
4313 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4314 rc = RC_ST0;
4316 #endif
4318 else
4319 rc = RC_INT;
4320 gv(rc);
4321 save_regs(1);
4323 if (tok == ':' && gnu_ext) {
4324 gv_dup();
4325 tt = gvtst(1, 0);
4326 } else {
4327 tt = gvtst(1, 0);
4328 gexpr();
4330 type1 = vtop->type;
4331 sv = *vtop; /* save value to handle it later */
4332 vtop--; /* no vpop so that FP stack is not flushed */
4333 skip(':');
4334 u = gjmp(0);
4335 gsym(tt);
4336 expr_cond();
4337 type2 = vtop->type;
4339 t1 = type1.t;
4340 bt1 = t1 & VT_BTYPE;
4341 t2 = type2.t;
4342 bt2 = t2 & VT_BTYPE;
4343 /* cast operands to correct type according to ISOC rules */
4344 if (is_float(bt1) || is_float(bt2)) {
4345 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4346 type.t = VT_LDOUBLE;
4347 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4348 type.t = VT_DOUBLE;
4349 } else {
4350 type.t = VT_FLOAT;
4352 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4353 /* cast to biggest op */
4354 type.t = VT_LLONG;
4355 /* convert to unsigned if it does not fit in a long long */
4356 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4357 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4358 type.t |= VT_UNSIGNED;
4359 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4360 /* If one is a null ptr constant the result type
4361 is the other. */
4362 if (is_null_pointer (vtop))
4363 type = type1;
4364 else if (is_null_pointer (&sv))
4365 type = type2;
4366 /* XXX: test pointer compatibility, C99 has more elaborate
4367 rules here. */
4368 else
4369 type = type1;
4370 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4371 /* XXX: test function pointer compatibility */
4372 type = bt1 == VT_FUNC ? type1 : type2;
4373 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4374 /* XXX: test structure compatibility */
4375 type = bt1 == VT_STRUCT ? type1 : type2;
4376 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4377 /* NOTE: as an extension, we accept void on only one side */
4378 type.t = VT_VOID;
4379 } else {
4380 /* integer operations */
4381 type.t = VT_INT;
4382 /* convert to unsigned if it does not fit in an integer */
4383 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4384 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4385 type.t |= VT_UNSIGNED;
4388 /* now we convert second operand */
4389 gen_cast(&type);
4390 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4391 gaddrof();
4392 rc = RC_INT;
4393 if (is_float(type.t)) {
4394 rc = RC_FLOAT;
4395 #ifdef TCC_TARGET_X86_64
4396 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4397 rc = RC_ST0;
4399 #endif
4400 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4401 /* for long longs, we use fixed registers to avoid having
4402 to handle a complicated move */
4403 rc = RC_IRET;
4406 r2 = gv(rc);
4407 /* this is horrible, but we must also convert first
4408 operand */
4409 tt = gjmp(0);
4410 gsym(u);
4411 /* put again first value and cast it */
4412 *vtop = sv;
4413 gen_cast(&type);
4414 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4415 gaddrof();
4416 r1 = gv(rc);
4417 move_reg(r2, r1, type.t);
4418 vtop->r = r2;
4419 gsym(tt);
4424 static void expr_eq(void)
4426 int t;
4428 expr_cond();
4429 if (tok == '=' ||
4430 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4431 tok == TOK_A_XOR || tok == TOK_A_OR ||
4432 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4433 test_lvalue();
4434 t = tok;
4435 next();
4436 if (t == '=') {
4437 expr_eq();
4438 } else {
4439 vdup();
4440 expr_eq();
4441 gen_op(t & 0x7f);
4443 vstore();
4447 ST_FUNC void gexpr(void)
4449 while (1) {
4450 expr_eq();
4451 if (tok != ',')
4452 break;
4453 vpop();
4454 next();
4458 /* parse an expression and return its type without any side effect. */
4459 static void expr_type(CType *type)
4461 int saved_nocode_wanted;
4463 saved_nocode_wanted = nocode_wanted;
4464 nocode_wanted = 1;
4465 gexpr();
4466 *type = vtop->type;
4467 vpop();
4468 nocode_wanted = saved_nocode_wanted;
4471 /* parse a unary expression and return its type without any side
4472 effect. */
4473 static void unary_type(CType *type)
4475 int a;
4477 a = nocode_wanted;
4478 nocode_wanted = 1;
4479 unary();
4480 *type = vtop->type;
4481 vpop();
4482 nocode_wanted = a;
4485 /* parse a constant expression and return value in vtop. */
4486 static void expr_const1(void)
4488 int a;
4489 a = const_wanted;
4490 const_wanted = 1;
4491 expr_cond();
4492 const_wanted = a;
4495 /* parse an integer constant and return its value. */
4496 ST_FUNC int expr_const(void)
4498 int c;
4499 expr_const1();
4500 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4501 expect("constant expression");
4502 c = vtop->c.i;
4503 vpop();
4504 return c;
4507 /* return the label token if current token is a label, otherwise
4508 return zero */
4509 static int is_label(void)
4511 int last_tok;
4513 /* fast test first */
4514 if (tok < TOK_UIDENT)
4515 return 0;
4516 /* no need to save tokc because tok is an identifier */
4517 last_tok = tok;
4518 next();
4519 if (tok == ':') {
4520 next();
4521 return last_tok;
4522 } else {
4523 unget_tok(last_tok);
4524 return 0;
4528 static void label_or_decl(int l)
4530 int last_tok;
4532 /* fast test first */
4533 if (tok >= TOK_UIDENT)
4535 /* no need to save tokc because tok is an identifier */
4536 last_tok = tok;
4537 next();
4538 if (tok == ':') {
4539 unget_tok(last_tok);
4540 return;
4542 unget_tok(last_tok);
4544 decl(l);
4547 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4548 int case_reg, int is_expr)
4550 int a, b, c, d;
4551 Sym *s, *frame_bottom;
4553 /* generate line number info */
4554 if (tcc_state->do_debug &&
4555 (last_line_num != file->line_num || last_ind != ind)) {
4556 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4557 last_ind = ind;
4558 last_line_num = file->line_num;
4561 if (is_expr) {
4562 /* default return value is (void) */
4563 vpushi(0);
4564 vtop->type.t = VT_VOID;
4567 if (tok == TOK_IF) {
4568 /* if test */
4569 next();
4570 skip('(');
4571 gexpr();
4572 skip(')');
4573 a = gvtst(1, 0);
4574 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4575 c = tok;
4576 if (c == TOK_ELSE) {
4577 next();
4578 d = gjmp(0);
4579 gsym(a);
4580 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4581 gsym(d); /* patch else jmp */
4582 } else
4583 gsym(a);
4584 } else if (tok == TOK_WHILE) {
4585 next();
4586 d = ind;
4587 skip('(');
4588 gexpr();
4589 skip(')');
4590 a = gvtst(1, 0);
4591 b = 0;
4592 block(&a, &b, case_sym, def_sym, case_reg, 0);
4593 gjmp_addr(d);
4594 gsym(a);
4595 gsym_addr(b, d);
4596 } else if (tok == '{') {
4597 Sym *llabel;
4598 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4600 next();
4601 /* record local declaration stack position */
4602 s = local_stack;
4603 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4604 frame_bottom->next = scope_stack_bottom;
4605 scope_stack_bottom = frame_bottom;
4606 llabel = local_label_stack;
4608 /* save VLA state */
4609 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4610 if (saved_vla_sp_loc != &vla_sp_root_loc)
4611 vla_sp_loc = &block_vla_sp_loc;
4613 saved_vla_flags = vla_flags;
4614 vla_flags |= VLA_NEED_NEW_FRAME;
4616 /* handle local labels declarations */
4617 if (tok == TOK_LABEL) {
4618 next();
4619 for(;;) {
4620 if (tok < TOK_UIDENT)
4621 expect("label identifier");
4622 label_push(&local_label_stack, tok, LABEL_DECLARED);
4623 next();
4624 if (tok == ',') {
4625 next();
4626 } else {
4627 skip(';');
4628 break;
4632 while (tok != '}') {
4633 label_or_decl(VT_LOCAL);
4634 if (tok != '}') {
4635 if (is_expr)
4636 vpop();
4637 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4640 /* pop locally defined labels */
4641 label_pop(&local_label_stack, llabel);
4642 if(is_expr) {
4643 /* XXX: this solution makes only valgrind happy...
4644 triggered by gcc.c-torture/execute/20000917-1.c */
4645 Sym *p;
4646 switch(vtop->type.t & VT_BTYPE) {
4647 case VT_PTR:
4648 case VT_STRUCT:
4649 case VT_ENUM:
4650 case VT_FUNC:
4651 for(p=vtop->type.ref;p;p=p->prev)
4652 if(p->prev==s)
4653 tcc_error("unsupported expression type");
4656 /* pop locally defined symbols */
4657 scope_stack_bottom = scope_stack_bottom->next;
4658 sym_pop(&local_stack, s);
4660 /* Pop VLA frames and restore stack pointer if required */
4661 if (saved_vla_sp_loc != &vla_sp_root_loc)
4662 *saved_vla_sp_loc = block_vla_sp_loc;
4663 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4664 vla_sp_loc = saved_vla_sp_loc;
4665 gen_vla_sp_restore(*vla_sp_loc);
4667 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4669 next();
4670 } else if (tok == TOK_RETURN) {
4671 next();
4672 if (tok != ';') {
4673 gexpr();
4674 gen_assign_cast(&func_vt);
4675 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4676 CType type, ret_type;
4677 int ret_align, ret_nregs;
4678 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4679 &ret_align);
4680 if (0 == ret_nregs) {
4681 /* if returning structure, must copy it to implicit
4682 first pointer arg location */
4683 type = func_vt;
4684 mk_pointer(&type);
4685 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4686 indir();
4687 vswap();
4688 /* copy structure value to pointer */
4689 vstore();
4690 } else {
4691 /* returning structure packed into registers */
4692 int r, size, addr, align;
4693 size = type_size(&func_vt,&align);
4694 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4695 && (align & (ret_align-1))) {
4696 loc = (loc - size) & -align;
4697 addr = loc;
4698 type = func_vt;
4699 vset(&type, VT_LOCAL | VT_LVAL, addr);
4700 vswap();
4701 vstore();
4702 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4704 vtop->type = ret_type;
4705 if (is_float(ret_type.t))
4706 r = rc_fret(ret_type.t);
4707 else
4708 r = RC_IRET;
4710 for (;;) {
4711 gv(r);
4712 if (--ret_nregs == 0)
4713 break;
4714 /* We assume that when a structure is returned in multiple
4715 registers, their classes are consecutive values of the
4716 suite s(n) = 2^n */
4717 r <<= 1;
4718 /* XXX: compatible with arm only: ret_align == register_size */
4719 vtop->c.i += ret_align;
4720 vtop->r = VT_LOCAL | VT_LVAL;
4723 } else if (is_float(func_vt.t)) {
4724 gv(rc_fret(func_vt.t));
4725 } else {
4726 gv(RC_IRET);
4728 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4730 skip(';');
4731 rsym = gjmp(rsym); /* jmp */
4732 } else if (tok == TOK_BREAK) {
4733 /* compute jump */
4734 if (!bsym)
4735 tcc_error("cannot break");
4736 *bsym = gjmp(*bsym);
4737 next();
4738 skip(';');
4739 } else if (tok == TOK_CONTINUE) {
4740 /* compute jump */
4741 if (!csym)
4742 tcc_error("cannot continue");
4743 *csym = gjmp(*csym);
4744 next();
4745 skip(';');
4746 } else if (tok == TOK_FOR) {
4747 int e;
4748 next();
4749 skip('(');
4750 s = local_stack;
4751 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4752 frame_bottom->next = scope_stack_bottom;
4753 scope_stack_bottom = frame_bottom;
4754 if (tok != ';') {
4755 /* c99 for-loop init decl? */
4756 if (!decl0(VT_LOCAL, 1)) {
4757 /* no, regular for-loop init expr */
4758 gexpr();
4759 vpop();
4762 skip(';');
4763 d = ind;
4764 c = ind;
4765 a = 0;
4766 b = 0;
4767 if (tok != ';') {
4768 gexpr();
4769 a = gvtst(1, 0);
4771 skip(';');
4772 if (tok != ')') {
4773 e = gjmp(0);
4774 c = ind;
4775 gexpr();
4776 vpop();
4777 gjmp_addr(d);
4778 gsym(e);
4780 skip(')');
4781 block(&a, &b, case_sym, def_sym, case_reg, 0);
4782 gjmp_addr(c);
4783 gsym(a);
4784 gsym_addr(b, c);
4785 scope_stack_bottom = scope_stack_bottom->next;
4786 sym_pop(&local_stack, s);
4787 } else
4788 if (tok == TOK_DO) {
4789 next();
4790 a = 0;
4791 b = 0;
4792 d = ind;
4793 block(&a, &b, case_sym, def_sym, case_reg, 0);
4794 skip(TOK_WHILE);
4795 skip('(');
4796 gsym(b);
4797 gexpr();
4798 c = gvtst(0, 0);
4799 gsym_addr(c, d);
4800 skip(')');
4801 gsym(a);
4802 skip(';');
4803 } else
4804 if (tok == TOK_SWITCH) {
4805 next();
4806 skip('(');
4807 gexpr();
4808 /* XXX: other types than integer */
4809 case_reg = gv(RC_INT);
4810 vpop();
4811 skip(')');
4812 a = 0;
4813 b = gjmp(0); /* jump to first case */
4814 c = 0;
4815 block(&a, csym, &b, &c, case_reg, 0);
4816 /* if no default, jmp after switch */
4817 if (c == 0)
4818 c = ind;
4819 /* default label */
4820 gsym_addr(b, c);
4821 /* break label */
4822 gsym(a);
4823 } else
4824 if (tok == TOK_CASE) {
4825 int v1, v2;
4826 if (!case_sym)
4827 expect("switch");
4828 next();
4829 v1 = expr_const();
4830 v2 = v1;
4831 if (gnu_ext && tok == TOK_DOTS) {
4832 next();
4833 v2 = expr_const();
4834 if (v2 < v1)
4835 tcc_warning("empty case range");
4837 /* since a case is like a label, we must skip it with a jmp */
4838 b = gjmp(0);
4839 gsym(*case_sym);
4840 vseti(case_reg, 0);
4841 vpushi(v1);
4842 if (v1 == v2) {
4843 gen_op(TOK_EQ);
4844 *case_sym = gtst(1, 0);
4845 } else {
4846 gen_op(TOK_GE);
4847 *case_sym = gtst(1, 0);
4848 vseti(case_reg, 0);
4849 vpushi(v2);
4850 gen_op(TOK_LE);
4851 *case_sym = gtst(1, *case_sym);
4853 gsym(b);
4854 skip(':');
4855 is_expr = 0;
4856 goto block_after_label;
4857 } else
4858 if (tok == TOK_DEFAULT) {
4859 next();
4860 skip(':');
4861 if (!def_sym)
4862 expect("switch");
4863 if (*def_sym)
4864 tcc_error("too many 'default'");
4865 *def_sym = ind;
4866 is_expr = 0;
4867 goto block_after_label;
4868 } else
4869 if (tok == TOK_GOTO) {
4870 next();
4871 if (tok == '*' && gnu_ext) {
4872 /* computed goto */
4873 next();
4874 gexpr();
4875 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4876 expect("pointer");
4877 ggoto();
4878 } else if (tok >= TOK_UIDENT) {
4879 s = label_find(tok);
4880 /* put forward definition if needed */
4881 if (!s) {
4882 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4883 } else {
4884 if (s->r == LABEL_DECLARED)
4885 s->r = LABEL_FORWARD;
4887 /* label already defined */
4888 if (vla_flags & VLA_IN_SCOPE) {
4889 /* If VLAs are in use, save the current stack pointer and
4890 reset the stack pointer to what it was at function entry
4891 (label will restore stack pointer in inner scopes) */
4892 vla_sp_save();
4893 gen_vla_sp_restore(vla_sp_root_loc);
4895 if (s->r & LABEL_FORWARD)
4896 s->jnext = gjmp(s->jnext);
4897 else
4898 gjmp_addr(s->jnext);
4899 next();
4900 } else {
4901 expect("label identifier");
4903 skip(';');
4904 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4905 asm_instr();
4906 } else {
4907 b = is_label();
4908 if (b) {
4909 /* label case */
4910 if (vla_flags & VLA_IN_SCOPE) {
4911 /* save/restore stack pointer across label
4912 this is a no-op when combined with the load immediately
4913 after the label unless we arrive via goto */
4914 vla_sp_save();
4916 s = label_find(b);
4917 if (s) {
4918 if (s->r == LABEL_DEFINED)
4919 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4920 gsym(s->jnext);
4921 s->r = LABEL_DEFINED;
4922 } else {
4923 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4925 s->jnext = ind;
4926 if (vla_flags & VLA_IN_SCOPE) {
4927 gen_vla_sp_restore(*vla_sp_loc);
4928 vla_flags |= VLA_NEED_NEW_FRAME;
4930 /* we accept this, but it is a mistake */
4931 block_after_label:
4932 if (tok == '}') {
4933 tcc_warning("deprecated use of label at end of compound statement");
4934 } else {
4935 if (is_expr)
4936 vpop();
4937 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4939 } else {
4940 /* expression case */
4941 if (tok != ';') {
4942 if (is_expr) {
4943 vpop();
4944 gexpr();
4945 } else {
4946 gexpr();
4947 vpop();
4950 skip(';');
4955 /* t is the array or struct type. c is the array or struct
4956 address. cur_index/cur_field is the pointer to the current
4957 value. 'size_only' is true if only size info is needed (only used
4958 in arrays) */
4959 static void decl_designator(CType *type, Section *sec, unsigned long c,
4960 int *cur_index, Sym **cur_field,
4961 int size_only)
4963 Sym *s, *f;
4964 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4965 CType type1;
4967 notfirst = 0;
4968 elem_size = 0;
4969 nb_elems = 1;
4970 if (gnu_ext && (l = is_label()) != 0)
4971 goto struct_field;
4972 while (tok == '[' || tok == '.') {
4973 if (tok == '[') {
4974 if (!(type->t & VT_ARRAY))
4975 expect("array type");
4976 s = type->ref;
4977 next();
4978 index = expr_const();
4979 if (index < 0 || (s->c >= 0 && index >= s->c))
4980 expect("invalid index");
4981 if (tok == TOK_DOTS && gnu_ext) {
4982 next();
4983 index_last = expr_const();
4984 if (index_last < 0 ||
4985 (s->c >= 0 && index_last >= s->c) ||
4986 index_last < index)
4987 expect("invalid index");
4988 } else {
4989 index_last = index;
4991 skip(']');
4992 if (!notfirst)
4993 *cur_index = index_last;
4994 type = pointed_type(type);
4995 elem_size = type_size(type, &align);
4996 c += index * elem_size;
4997 /* NOTE: we only support ranges for last designator */
4998 nb_elems = index_last - index + 1;
4999 if (nb_elems != 1) {
5000 notfirst = 1;
5001 break;
5003 } else {
5004 next();
5005 l = tok;
5006 next();
5007 struct_field:
5008 if ((type->t & VT_BTYPE) != VT_STRUCT)
5009 expect("struct/union type");
5010 s = type->ref;
5011 l |= SYM_FIELD;
5012 f = s->next;
5013 while (f) {
5014 if (f->v == l)
5015 break;
5016 f = f->next;
5018 if (!f)
5019 expect("field");
5020 if (!notfirst)
5021 *cur_field = f;
5022 /* XXX: fix this mess by using explicit storage field */
5023 type1 = f->type;
5024 type1.t |= (type->t & ~VT_TYPE);
5025 type = &type1;
5026 c += f->c;
5028 notfirst = 1;
5030 if (notfirst) {
5031 if (tok == '=') {
5032 next();
5033 } else {
5034 if (!gnu_ext)
5035 expect("=");
5037 } else {
5038 if (type->t & VT_ARRAY) {
5039 index = *cur_index;
5040 type = pointed_type(type);
5041 c += index * type_size(type, &align);
5042 } else {
5043 f = *cur_field;
5044 if (!f)
5045 tcc_error("too many field init");
5046 /* XXX: fix this mess by using explicit storage field */
5047 type1 = f->type;
5048 type1.t |= (type->t & ~VT_TYPE);
5049 type = &type1;
5050 c += f->c;
5053 decl_initializer(type, sec, c, 0, size_only);
5055 /* XXX: make it more general */
5056 if (!size_only && nb_elems > 1) {
5057 unsigned long c_end;
5058 uint8_t *src, *dst;
5059 int i;
5061 if (!sec)
5062 tcc_error("range init not supported yet for dynamic storage");
5063 c_end = c + nb_elems * elem_size;
5064 if (c_end > sec->data_allocated)
5065 section_realloc(sec, c_end);
5066 src = sec->data + c;
5067 dst = src;
5068 for(i = 1; i < nb_elems; i++) {
5069 dst += elem_size;
5070 memcpy(dst, src, elem_size);
5075 #define EXPR_VAL 0
5076 #define EXPR_CONST 1
5077 #define EXPR_ANY 2
5079 /* store a value or an expression directly in global data or in local array */
5080 static void init_putv(CType *type, Section *sec, unsigned long c,
5081 int v, int expr_type)
5083 int saved_global_expr, bt, bit_pos, bit_size;
5084 void *ptr;
5085 unsigned long long bit_mask;
5086 CType dtype;
5088 switch(expr_type) {
5089 case EXPR_VAL:
5090 vpushi(v);
5091 break;
5092 case EXPR_CONST:
5093 /* compound literals must be allocated globally in this case */
5094 saved_global_expr = global_expr;
5095 global_expr = 1;
5096 expr_const1();
5097 global_expr = saved_global_expr;
5098 /* NOTE: symbols are accepted */
5099 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5100 tcc_error("initializer element is not constant");
5101 break;
5102 case EXPR_ANY:
5103 expr_eq();
5104 break;
5107 dtype = *type;
5108 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5110 if (sec) {
5111 /* XXX: not portable */
5112 /* XXX: generate error if incorrect relocation */
5113 gen_assign_cast(&dtype);
5114 bt = type->t & VT_BTYPE;
5115 /* we'll write at most 12 bytes */
5116 if (c + 12 > sec->data_allocated) {
5117 section_realloc(sec, c + 12);
5119 ptr = sec->data + c;
5120 /* XXX: make code faster ? */
5121 if (!(type->t & VT_BITFIELD)) {
5122 bit_pos = 0;
5123 bit_size = 32;
5124 bit_mask = -1LL;
5125 } else {
5126 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5127 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5128 bit_mask = (1LL << bit_size) - 1;
5130 if ((vtop->r & VT_SYM) &&
5131 (bt == VT_BYTE ||
5132 bt == VT_SHORT ||
5133 bt == VT_DOUBLE ||
5134 bt == VT_LDOUBLE ||
5135 bt == VT_LLONG ||
5136 (bt == VT_INT && bit_size != 32)))
5137 tcc_error("initializer element is not computable at load time");
5138 switch(bt) {
5139 case VT_BOOL:
5140 vtop->c.i = (vtop->c.i != 0);
5141 case VT_BYTE:
5142 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5143 break;
5144 case VT_SHORT:
5145 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5146 break;
5147 case VT_DOUBLE:
5148 *(double *)ptr = vtop->c.d;
5149 break;
5150 case VT_LDOUBLE:
5151 *(long double *)ptr = vtop->c.ld;
5152 break;
5153 case VT_LLONG:
5154 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5155 break;
5156 case VT_PTR:
5157 if (vtop->r & VT_SYM) {
5158 greloc(sec, vtop->sym, c, R_DATA_PTR);
5160 *(addr_t *)ptr |= (vtop->c.ull & bit_mask) << bit_pos;
5161 break;
5162 default:
5163 if (vtop->r & VT_SYM) {
5164 greloc(sec, vtop->sym, c, R_DATA_PTR);
5166 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5167 break;
5169 vtop--;
5170 } else {
5171 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5172 vswap();
5173 vstore();
5174 vpop();
5178 /* put zeros for variable based init */
5179 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5181 if (sec) {
5182 /* nothing to do because globals are already set to zero */
5183 } else {
5184 vpush_global_sym(&func_old_type, TOK_memset);
5185 vseti(VT_LOCAL, c);
5186 #ifdef TCC_TARGET_ARM
5187 vpushs(size);
5188 vpushi(0);
5189 #else
5190 vpushi(0);
5191 vpushs(size);
5192 #endif
5193 gfunc_call(3);
5197 /* 't' contains the type and storage info. 'c' is the offset of the
5198 object in section 'sec'. If 'sec' is NULL, it means stack based
5199 allocation. 'first' is true if array '{' must be read (multi
5200 dimension implicit array init handling). 'size_only' is true if
5201 size only evaluation is wanted (only for arrays). */
5202 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5203 int first, int size_only)
5205 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5206 int size1, align1, expr_type;
5207 Sym *s, *f;
5208 CType *t1;
5210 if (type->t & VT_VLA) {
5211 int a;
5213 /* save current stack pointer */
5214 if (vla_flags & VLA_NEED_NEW_FRAME) {
5215 vla_sp_save();
5216 vla_flags = VLA_IN_SCOPE;
5217 vla_sp_loc = &vla_sp_loc_tmp;
5220 vla_runtime_type_size(type, &a);
5221 gen_vla_alloc(type, a);
5222 vset(type, VT_LOCAL|VT_LVAL, c);
5223 vswap();
5224 vstore();
5225 vpop();
5226 } else if (type->t & VT_ARRAY) {
5227 s = type->ref;
5228 n = s->c;
5229 array_length = 0;
5230 t1 = pointed_type(type);
5231 size1 = type_size(t1, &align1);
5233 no_oblock = 1;
5234 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5235 tok == '{') {
5236 if (tok != '{')
5237 tcc_error("character array initializer must be a literal,"
5238 " optionally enclosed in braces");
5239 skip('{');
5240 no_oblock = 0;
5243 /* only parse strings here if correct type (otherwise: handle
5244 them as ((w)char *) expressions */
5245 if ((tok == TOK_LSTR &&
5246 #ifdef TCC_TARGET_PE
5247 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5248 #else
5249 (t1->t & VT_BTYPE) == VT_INT
5250 #endif
5251 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5252 while (tok == TOK_STR || tok == TOK_LSTR) {
5253 int cstr_len, ch;
5254 CString *cstr;
5256 cstr = tokc.cstr;
5257 /* compute maximum number of chars wanted */
5258 if (tok == TOK_STR)
5259 cstr_len = cstr->size;
5260 else
5261 cstr_len = cstr->size / sizeof(nwchar_t);
5262 cstr_len--;
5263 nb = cstr_len;
5264 if (n >= 0 && nb > (n - array_length))
5265 nb = n - array_length;
5266 if (!size_only) {
5267 if (cstr_len > nb)
5268 tcc_warning("initializer-string for array is too long");
5269 /* in order to go faster for common case (char
5270 string in global variable, we handle it
5271 specifically */
5272 if (sec && tok == TOK_STR && size1 == 1) {
5273 memcpy(sec->data + c + array_length, cstr->data, nb);
5274 } else {
5275 for(i=0;i<nb;i++) {
5276 if (tok == TOK_STR)
5277 ch = ((unsigned char *)cstr->data)[i];
5278 else
5279 ch = ((nwchar_t *)cstr->data)[i];
5280 init_putv(t1, sec, c + (array_length + i) * size1,
5281 ch, EXPR_VAL);
5285 array_length += nb;
5286 next();
5288 /* only add trailing zero if enough storage (no
5289 warning in this case since it is standard) */
5290 if (n < 0 || array_length < n) {
5291 if (!size_only) {
5292 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5294 array_length++;
5296 } else {
5297 index = 0;
5298 while (tok != '}') {
5299 decl_designator(type, sec, c, &index, NULL, size_only);
5300 if (n >= 0 && index >= n)
5301 tcc_error("index too large");
5302 /* must put zero in holes (note that doing it that way
5303 ensures that it even works with designators) */
5304 if (!size_only && array_length < index) {
5305 init_putz(t1, sec, c + array_length * size1,
5306 (index - array_length) * size1);
5308 index++;
5309 if (index > array_length)
5310 array_length = index;
5311 /* special test for multi dimensional arrays (may not
5312 be strictly correct if designators are used at the
5313 same time) */
5314 if (index >= n && no_oblock)
5315 break;
5316 if (tok == '}')
5317 break;
5318 skip(',');
5321 if (!no_oblock)
5322 skip('}');
5323 /* put zeros at the end */
5324 if (!size_only && n >= 0 && array_length < n) {
5325 init_putz(t1, sec, c + array_length * size1,
5326 (n - array_length) * size1);
5328 /* patch type size if needed */
5329 if (n < 0)
5330 s->c = array_length;
5331 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5332 (sec || !first || tok == '{')) {
5333 int par_count;
5335 /* NOTE: the previous test is a specific case for automatic
5336 struct/union init */
5337 /* XXX: union needs only one init */
5339 /* XXX: this test is incorrect for local initializers
5340 beginning with ( without {. It would be much more difficult
5341 to do it correctly (ideally, the expression parser should
5342 be used in all cases) */
5343 par_count = 0;
5344 if (tok == '(') {
5345 AttributeDef ad1;
5346 CType type1;
5347 next();
5348 while (tok == '(') {
5349 par_count++;
5350 next();
5352 if (!parse_btype(&type1, &ad1))
5353 expect("cast");
5354 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5355 #if 0
5356 if (!is_assignable_types(type, &type1))
5357 tcc_error("invalid type for cast");
5358 #endif
5359 skip(')');
5361 no_oblock = 1;
5362 if (first || tok == '{') {
5363 skip('{');
5364 no_oblock = 0;
5366 s = type->ref;
5367 f = s->next;
5368 array_length = 0;
5369 index = 0;
5370 n = s->c;
5371 while (tok != '}') {
5372 decl_designator(type, sec, c, NULL, &f, size_only);
5373 index = f->c;
5374 if (!size_only && array_length < index) {
5375 init_putz(type, sec, c + array_length,
5376 index - array_length);
5378 index = index + type_size(&f->type, &align1);
5379 if (index > array_length)
5380 array_length = index;
5382 /* gr: skip fields from same union - ugly. */
5383 while (f->next) {
5384 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5385 /* test for same offset */
5386 if (f->next->c != f->c)
5387 break;
5388 /* if yes, test for bitfield shift */
5389 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5390 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5391 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5392 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5393 if (bit_pos_1 != bit_pos_2)
5394 break;
5396 f = f->next;
5399 f = f->next;
5400 if (no_oblock && f == NULL)
5401 break;
5402 if (tok == '}')
5403 break;
5404 skip(',');
5406 /* put zeros at the end */
5407 if (!size_only && array_length < n) {
5408 init_putz(type, sec, c + array_length,
5409 n - array_length);
5411 if (!no_oblock)
5412 skip('}');
5413 while (par_count) {
5414 skip(')');
5415 par_count--;
5417 } else if (tok == '{') {
5418 next();
5419 decl_initializer(type, sec, c, first, size_only);
5420 skip('}');
5421 } else if (size_only) {
5422 /* just skip expression */
5423 parlevel = parlevel1 = 0;
5424 while ((parlevel > 0 || parlevel1 > 0 ||
5425 (tok != '}' && tok != ',')) && tok != -1) {
5426 if (tok == '(')
5427 parlevel++;
5428 else if (tok == ')')
5429 parlevel--;
5430 else if (tok == '{')
5431 parlevel1++;
5432 else if (tok == '}')
5433 parlevel1--;
5434 next();
5436 } else {
5437 /* currently, we always use constant expression for globals
5438 (may change for scripting case) */
5439 expr_type = EXPR_CONST;
5440 if (!sec)
5441 expr_type = EXPR_ANY;
5442 init_putv(type, sec, c, 0, expr_type);
5446 /* parse an initializer for type 't' if 'has_init' is non zero, and
5447 allocate space in local or global data space ('r' is either
5448 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5449 variable 'v' with an associated name represented by 'asm_label' of
5450 scope 'scope' is declared before initializers are parsed. If 'v' is
5451 zero, then a reference to the new object is put in the value stack.
5452 If 'has_init' is 2, a special parsing is done to handle string
5453 constants. */
5454 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5455 int has_init, int v, char *asm_label,
5456 int scope)
5458 int size, align, addr, data_offset;
5459 int level;
5460 ParseState saved_parse_state = {0};
5461 TokenString init_str;
5462 Section *sec;
5463 Sym *flexible_array;
5465 flexible_array = NULL;
5466 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5467 Sym *field = type->ref->next;
5468 if (field) {
5469 while (field->next)
5470 field = field->next;
5471 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5472 flexible_array = field;
5476 size = type_size(type, &align);
5477 /* If unknown size, we must evaluate it before
5478 evaluating initializers because
5479 initializers can generate global data too
5480 (e.g. string pointers or ISOC99 compound
5481 literals). It also simplifies local
5482 initializers handling */
5483 tok_str_new(&init_str);
5484 if (size < 0 || (flexible_array && has_init)) {
5485 if (!has_init)
5486 tcc_error("unknown type size");
5487 /* get all init string */
5488 if (has_init == 2) {
5489 /* only get strings */
5490 while (tok == TOK_STR || tok == TOK_LSTR) {
5491 tok_str_add_tok(&init_str);
5492 next();
5494 } else {
5495 level = 0;
5496 while (level > 0 || (tok != ',' && tok != ';')) {
5497 if (tok < 0)
5498 tcc_error("unexpected end of file in initializer");
5499 tok_str_add_tok(&init_str);
5500 if (tok == '{')
5501 level++;
5502 else if (tok == '}') {
5503 level--;
5504 if (level <= 0) {
5505 next();
5506 break;
5509 next();
5512 tok_str_add(&init_str, -1);
5513 tok_str_add(&init_str, 0);
5515 /* compute size */
5516 save_parse_state(&saved_parse_state);
5518 macro_ptr = init_str.str;
5519 next();
5520 decl_initializer(type, NULL, 0, 1, 1);
5521 /* prepare second initializer parsing */
5522 macro_ptr = init_str.str;
5523 next();
5525 /* if still unknown size, error */
5526 size = type_size(type, &align);
5527 if (size < 0)
5528 tcc_error("unknown type size");
5530 if (flexible_array)
5531 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5532 /* take into account specified alignment if bigger */
5533 if (ad->a.aligned) {
5534 if (ad->a.aligned > align)
5535 align = ad->a.aligned;
5536 } else if (ad->a.packed) {
5537 align = 1;
5539 if ((r & VT_VALMASK) == VT_LOCAL) {
5540 sec = NULL;
5541 #ifdef CONFIG_TCC_BCHECK
5542 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5543 loc--;
5545 #endif
5546 loc = (loc - size) & -align;
5547 addr = loc;
5548 #ifdef CONFIG_TCC_BCHECK
5549 /* handles bounds */
5550 /* XXX: currently, since we do only one pass, we cannot track
5551 '&' operators, so we add only arrays */
5552 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5553 unsigned long *bounds_ptr;
5554 /* add padding between regions */
5555 loc--;
5556 /* then add local bound info */
5557 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5558 bounds_ptr[0] = addr;
5559 bounds_ptr[1] = size;
5561 #endif
5562 if (v) {
5563 /* local variable */
5564 sym_push(v, type, r, addr);
5565 } else {
5566 /* push local reference */
5567 vset(type, r, addr);
5569 } else {
5570 Sym *sym;
5572 sym = NULL;
5573 if (v && scope == VT_CONST) {
5574 /* see if the symbol was already defined */
5575 sym = sym_find(v);
5576 if (sym) {
5577 if (!is_compatible_types(&sym->type, type))
5578 tcc_error("incompatible types for redefinition of '%s'",
5579 get_tok_str(v, NULL));
5580 if (sym->type.t & VT_EXTERN) {
5581 /* if the variable is extern, it was not allocated */
5582 sym->type.t &= ~VT_EXTERN;
5583 /* set array size if it was ommited in extern
5584 declaration */
5585 if ((sym->type.t & VT_ARRAY) &&
5586 sym->type.ref->c < 0 &&
5587 type->ref->c >= 0)
5588 sym->type.ref->c = type->ref->c;
5589 } else {
5590 /* we accept several definitions of the same
5591 global variable. this is tricky, because we
5592 must play with the SHN_COMMON type of the symbol */
5593 /* XXX: should check if the variable was already
5594 initialized. It is incorrect to initialized it
5595 twice */
5596 /* no init data, we won't add more to the symbol */
5597 if (!has_init)
5598 goto no_alloc;
5603 /* allocate symbol in corresponding section */
5604 sec = ad->section;
5605 if (!sec) {
5606 if (has_init)
5607 sec = data_section;
5608 else if (tcc_state->nocommon)
5609 sec = bss_section;
5611 if (sec) {
5612 data_offset = sec->data_offset;
5613 data_offset = (data_offset + align - 1) & -align;
5614 addr = data_offset;
5615 /* very important to increment global pointer at this time
5616 because initializers themselves can create new initializers */
5617 data_offset += size;
5618 #ifdef CONFIG_TCC_BCHECK
5619 /* add padding if bound check */
5620 if (tcc_state->do_bounds_check)
5621 data_offset++;
5622 #endif
5623 sec->data_offset = data_offset;
5624 /* allocate section space to put the data */
5625 if (sec->sh_type != SHT_NOBITS &&
5626 data_offset > sec->data_allocated)
5627 section_realloc(sec, data_offset);
5628 /* align section if needed */
5629 if (align > sec->sh_addralign)
5630 sec->sh_addralign = align;
5631 } else {
5632 addr = 0; /* avoid warning */
5635 if (v) {
5636 if (scope != VT_CONST || !sym) {
5637 sym = sym_push(v, type, r | VT_SYM, 0);
5638 sym->asm_label = asm_label;
5640 /* update symbol definition */
5641 if (sec) {
5642 put_extern_sym(sym, sec, addr, size);
5643 } else {
5644 ElfW(Sym) *esym;
5645 /* put a common area */
5646 put_extern_sym(sym, NULL, align, size);
5647 /* XXX: find a nicer way */
5648 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5649 esym->st_shndx = SHN_COMMON;
5651 } else {
5652 /* push global reference */
5653 sym = get_sym_ref(type, sec, addr, size);
5654 vpushsym(type, sym);
5656 /* patch symbol weakness */
5657 if (type->t & VT_WEAK)
5658 weaken_symbol(sym);
5659 #ifdef CONFIG_TCC_BCHECK
5660 /* handles bounds now because the symbol must be defined
5661 before for the relocation */
5662 if (tcc_state->do_bounds_check) {
5663 unsigned long *bounds_ptr;
5665 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5666 /* then add global bound info */
5667 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5668 bounds_ptr[0] = 0; /* relocated */
5669 bounds_ptr[1] = size;
5671 #endif
5673 if (has_init || (type->t & VT_VLA)) {
5674 decl_initializer(type, sec, addr, 1, 0);
5675 /* restore parse state if needed */
5676 if (init_str.str) {
5677 tok_str_free(init_str.str);
5678 restore_parse_state(&saved_parse_state);
5680 /* patch flexible array member size back to -1, */
5681 /* for possible subsequent similar declarations */
5682 if (flexible_array)
5683 flexible_array->type.ref->c = -1;
5685 no_alloc: ;
5688 static void put_func_debug(Sym *sym)
5690 char buf[512];
5692 /* stabs info */
5693 /* XXX: we put here a dummy type */
5694 snprintf(buf, sizeof(buf), "%s:%c1",
5695 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5696 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5697 cur_text_section, sym->c);
5698 /* //gr gdb wants a line at the function */
5699 put_stabn(N_SLINE, 0, file->line_num, 0);
5700 last_ind = 0;
5701 last_line_num = 0;
5704 /* parse an old style function declaration list */
5705 /* XXX: check multiple parameter */
5706 static void func_decl_list(Sym *func_sym)
5708 AttributeDef ad;
5709 int v;
5710 Sym *s;
5711 CType btype, type;
5713 /* parse each declaration */
5714 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5715 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5716 if (!parse_btype(&btype, &ad))
5717 expect("declaration list");
5718 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5719 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5720 tok == ';') {
5721 /* we accept no variable after */
5722 } else {
5723 for(;;) {
5724 type = btype;
5725 type_decl(&type, &ad, &v, TYPE_DIRECT);
5726 /* find parameter in function parameter list */
5727 s = func_sym->next;
5728 while (s != NULL) {
5729 if ((s->v & ~SYM_FIELD) == v)
5730 goto found;
5731 s = s->next;
5733 tcc_error("declaration for parameter '%s' but no such parameter",
5734 get_tok_str(v, NULL));
5735 found:
5736 /* check that no storage specifier except 'register' was given */
5737 if (type.t & VT_STORAGE)
5738 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5739 convert_parameter_type(&type);
5740 /* we can add the type (NOTE: it could be local to the function) */
5741 s->type = type;
5742 /* accept other parameters */
5743 if (tok == ',')
5744 next();
5745 else
5746 break;
5749 skip(';');
5753 /* parse a function defined by symbol 'sym' and generate its code in
5754 'cur_text_section' */
5755 static void gen_function(Sym *sym)
5757 int saved_nocode_wanted = nocode_wanted;
5758 nocode_wanted = 0;
5759 ind = cur_text_section->data_offset;
5760 /* NOTE: we patch the symbol size later */
5761 put_extern_sym(sym, cur_text_section, ind, 0);
5762 funcname = get_tok_str(sym->v, NULL);
5763 func_ind = ind;
5764 /* Initialize VLA state */
5765 vla_sp_loc = &vla_sp_root_loc;
5766 vla_flags = VLA_NEED_NEW_FRAME;
5767 /* put debug symbol */
5768 if (tcc_state->do_debug)
5769 put_func_debug(sym);
5770 /* push a dummy symbol to enable local sym storage */
5771 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5772 gfunc_prolog(&sym->type);
5773 #ifdef CONFIG_TCC_BCHECK
5774 if (tcc_state->do_bounds_check
5775 && !strcmp(get_tok_str(sym->v, NULL), "main")) {
5776 int i;
5778 sym = local_stack;
5779 for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) {
5780 if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD)
5781 break;
5782 vpush_global_sym(&func_old_type, TOK___bound_main_arg);
5783 vset(&sym->type, sym->r, sym->c);
5784 gfunc_call(1);
5787 #endif
5788 rsym = 0;
5789 block(NULL, NULL, NULL, NULL, 0, 0);
5790 gsym(rsym);
5791 gfunc_epilog();
5792 cur_text_section->data_offset = ind;
5793 label_pop(&global_label_stack, NULL);
5794 /* reset local stack */
5795 scope_stack_bottom = NULL;
5796 sym_pop(&local_stack, NULL);
5797 /* end of function */
5798 /* patch symbol size */
5799 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5800 ind - func_ind;
5801 /* patch symbol weakness (this definition overrules any prototype) */
5802 if (sym->type.t & VT_WEAK)
5803 weaken_symbol(sym);
5804 if (tcc_state->do_debug) {
5805 put_stabn(N_FUN, 0, 0, ind - func_ind);
5807 /* It's better to crash than to generate wrong code */
5808 cur_text_section = NULL;
5809 funcname = ""; /* for safety */
5810 func_vt.t = VT_VOID; /* for safety */
5811 func_var = 0; /* for safety */
5812 ind = 0; /* for safety */
5813 nocode_wanted = saved_nocode_wanted;
5816 ST_FUNC void gen_inline_functions(void)
5818 Sym *sym;
5819 int *str, inline_generated, i;
5820 struct InlineFunc *fn;
5822 /* iterate while inline function are referenced */
5823 for(;;) {
5824 inline_generated = 0;
5825 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5826 fn = tcc_state->inline_fns[i];
5827 sym = fn->sym;
5828 if (sym && sym->c) {
5829 /* the function was used: generate its code and
5830 convert it to a normal function */
5831 str = fn->token_str;
5832 fn->sym = NULL;
5833 if (file)
5834 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5835 sym->r = VT_SYM | VT_CONST;
5836 sym->type.t &= ~VT_INLINE;
5838 macro_ptr = str;
5839 next();
5840 cur_text_section = text_section;
5841 gen_function(sym);
5842 macro_ptr = NULL; /* fail safe */
5844 inline_generated = 1;
5847 if (!inline_generated)
5848 break;
5850 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5851 fn = tcc_state->inline_fns[i];
5852 str = fn->token_str;
5853 tok_str_free(str);
5855 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5858 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5859 static int decl0(int l, int is_for_loop_init)
5861 int v, has_init, r;
5862 CType type, btype;
5863 Sym *sym;
5864 AttributeDef ad;
5866 while (1) {
5867 if (!parse_btype(&btype, &ad)) {
5868 if (is_for_loop_init)
5869 return 0;
5870 /* skip redundant ';' */
5871 /* XXX: find more elegant solution */
5872 if (tok == ';') {
5873 next();
5874 continue;
5876 if (l == VT_CONST &&
5877 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5878 /* global asm block */
5879 asm_global_instr();
5880 continue;
5882 /* special test for old K&R protos without explicit int
5883 type. Only accepted when defining global data */
5884 if (l == VT_LOCAL || tok < TOK_DEFINE)
5885 break;
5886 btype.t = VT_INT;
5888 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5889 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5890 tok == ';') {
5891 /* we accept no variable after */
5892 next();
5893 continue;
5895 while (1) { /* iterate thru each declaration */
5896 char *asm_label; // associated asm label
5897 type = btype;
5898 type_decl(&type, &ad, &v, TYPE_DIRECT);
5899 #if 0
5901 char buf[500];
5902 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5903 printf("type = '%s'\n", buf);
5905 #endif
5906 if ((type.t & VT_BTYPE) == VT_FUNC) {
5907 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5908 tcc_error("function without file scope cannot be static");
5910 /* if old style function prototype, we accept a
5911 declaration list */
5912 sym = type.ref;
5913 if (sym->c == FUNC_OLD)
5914 func_decl_list(sym);
5917 asm_label = NULL;
5918 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5919 CString astr;
5921 asm_label_instr(&astr);
5922 asm_label = tcc_strdup(astr.data);
5923 cstr_free(&astr);
5925 /* parse one last attribute list, after asm label */
5926 parse_attribute(&ad);
5929 if (ad.a.weak)
5930 type.t |= VT_WEAK;
5931 #ifdef TCC_TARGET_PE
5932 if (ad.a.func_import)
5933 type.t |= VT_IMPORT;
5934 if (ad.a.func_export)
5935 type.t |= VT_EXPORT;
5936 #endif
5937 if (tok == '{') {
5938 if (l == VT_LOCAL)
5939 tcc_error("cannot use local functions");
5940 if ((type.t & VT_BTYPE) != VT_FUNC)
5941 expect("function definition");
5943 /* reject abstract declarators in function definition */
5944 sym = type.ref;
5945 while ((sym = sym->next) != NULL)
5946 if (!(sym->v & ~SYM_FIELD))
5947 expect("identifier");
5949 /* XXX: cannot do better now: convert extern line to static inline */
5950 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5951 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5953 sym = sym_find(v);
5954 if (sym) {
5955 Sym *ref;
5956 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5957 goto func_error1;
5959 ref = sym->type.ref;
5960 if (0 == ref->a.func_proto)
5961 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5963 /* use func_call from prototype if not defined */
5964 if (ref->a.func_call != FUNC_CDECL
5965 && type.ref->a.func_call == FUNC_CDECL)
5966 type.ref->a.func_call = ref->a.func_call;
5968 /* use export from prototype */
5969 if (ref->a.func_export)
5970 type.ref->a.func_export = 1;
5972 /* use static from prototype */
5973 if (sym->type.t & VT_STATIC)
5974 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5976 if (!is_compatible_types(&sym->type, &type)) {
5977 func_error1:
5978 tcc_error("incompatible types for redefinition of '%s'",
5979 get_tok_str(v, NULL));
5981 type.ref->a.func_proto = 0;
5982 /* if symbol is already defined, then put complete type */
5983 sym->type = type;
5984 } else {
5985 /* put function symbol */
5986 sym = global_identifier_push(v, type.t, 0);
5987 sym->type.ref = type.ref;
5990 /* static inline functions are just recorded as a kind
5991 of macro. Their code will be emitted at the end of
5992 the compilation unit only if they are used */
5993 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5994 (VT_INLINE | VT_STATIC)) {
5995 TokenString func_str;
5996 int block_level;
5997 struct InlineFunc *fn;
5998 const char *filename;
6000 tok_str_new(&func_str);
6002 block_level = 0;
6003 for(;;) {
6004 int t;
6005 if (tok == TOK_EOF)
6006 tcc_error("unexpected end of file");
6007 tok_str_add_tok(&func_str);
6008 t = tok;
6009 next();
6010 if (t == '{') {
6011 block_level++;
6012 } else if (t == '}') {
6013 block_level--;
6014 if (block_level == 0)
6015 break;
6018 tok_str_add(&func_str, -1);
6019 tok_str_add(&func_str, 0);
6020 filename = file ? file->filename : "";
6021 fn = tcc_malloc(sizeof *fn + strlen(filename));
6022 strcpy(fn->filename, filename);
6023 fn->sym = sym;
6024 fn->token_str = func_str.str;
6025 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
6027 } else {
6028 /* compute text section */
6029 cur_text_section = ad.section;
6030 if (!cur_text_section)
6031 cur_text_section = text_section;
6032 sym->r = VT_SYM | VT_CONST;
6033 gen_function(sym);
6035 break;
6036 } else {
6037 if (btype.t & VT_TYPEDEF) {
6038 /* save typedefed type */
6039 /* XXX: test storage specifiers ? */
6040 sym = sym_push(v, &type, 0, 0);
6041 sym->a = ad.a;
6042 sym->type.t |= VT_TYPEDEF;
6043 } else {
6044 r = 0;
6045 if ((type.t & VT_BTYPE) == VT_FUNC) {
6046 /* external function definition */
6047 /* specific case for func_call attribute */
6048 ad.a.func_proto = 1;
6049 type.ref->a = ad.a;
6050 } else if (!(type.t & VT_ARRAY)) {
6051 /* not lvalue if array */
6052 r |= lvalue_type(type.t);
6054 has_init = (tok == '=');
6055 if (has_init && (type.t & VT_VLA))
6056 tcc_error("Variable length array cannot be initialized");
6057 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6058 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6059 !has_init && l == VT_CONST && type.ref->c < 0)) {
6060 /* external variable or function */
6061 /* NOTE: as GCC, uninitialized global static
6062 arrays of null size are considered as
6063 extern */
6064 sym = external_sym(v, &type, r, asm_label);
6066 if (type.t & VT_WEAK)
6067 weaken_symbol(sym);
6069 if (ad.alias_target) {
6070 Section tsec;
6071 Elf32_Sym *esym;
6072 Sym *alias_target;
6074 alias_target = sym_find(ad.alias_target);
6075 if (!alias_target || !alias_target->c)
6076 tcc_error("unsupported forward __alias__ attribute");
6077 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6078 tsec.sh_num = esym->st_shndx;
6079 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6081 } else {
6082 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6083 if (type.t & VT_STATIC)
6084 r |= VT_CONST;
6085 else
6086 r |= l;
6087 if (has_init)
6088 next();
6089 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6092 if (tok != ',') {
6093 if (is_for_loop_init)
6094 return 1;
6095 skip(';');
6096 break;
6098 next();
6100 ad.a.aligned = 0;
6103 return 0;
6106 ST_FUNC void decl(int l)
6108 decl0(l, 0);