Fix test for macro nesting
[tinycc.git] / tccgen.c
blob5705db306c2f3f1bbc2c6ed0748df91c49c41e92
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 else if (s->v == -1)
189 return NULL;
190 s = s->prev;
192 return NULL;
195 /* structure lookup */
196 ST_INLN Sym *struct_find(int v)
198 v -= TOK_IDENT;
199 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
200 return NULL;
201 return table_ident[v]->sym_struct;
204 /* find an identifier */
205 ST_INLN Sym *sym_find(int v)
207 v -= TOK_IDENT;
208 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
209 return NULL;
210 return table_ident[v]->sym_identifier;
213 /* push a given symbol on the symbol stack */
214 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
216 Sym *s, **ps;
217 TokenSym *ts;
219 if (local_stack)
220 ps = &local_stack;
221 else
222 ps = &global_stack;
223 s = sym_push2(ps, v, type->t, c);
224 s->type.ref = type->ref;
225 s->r = r;
226 /* don't record fields or anonymous symbols */
227 /* XXX: simplify */
228 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
229 /* record symbol in token array */
230 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
231 if (v & SYM_STRUCT)
232 ps = &ts->sym_struct;
233 else
234 ps = &ts->sym_identifier;
235 s->prev_tok = *ps;
236 *ps = s;
238 return s;
241 /* push a global identifier */
242 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
244 Sym *s, **ps;
245 s = sym_push2(&global_stack, v, t, c);
246 /* don't record anonymous symbol */
247 if (v < SYM_FIRST_ANOM) {
248 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
249 /* modify the top most local identifier, so that
250 sym_identifier will point to 's' when popped */
251 while (*ps != NULL)
252 ps = &(*ps)->prev_tok;
253 s->prev_tok = NULL;
254 *ps = s;
256 return s;
259 /* pop symbols until top reaches 'b' */
260 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
262 Sym *s, *ss, **ps;
263 TokenSym *ts;
264 int v;
266 s = *ptop;
267 while(s != b) {
268 ss = s->prev;
269 v = s->v;
270 /* remove symbol in token array */
271 /* XXX: simplify */
272 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
273 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
274 if (v & SYM_STRUCT)
275 ps = &ts->sym_struct;
276 else
277 ps = &ts->sym_identifier;
278 *ps = s->prev_tok;
280 sym_free(s);
281 s = ss;
283 *ptop = b;
286 static void weaken_symbol(Sym *sym)
288 sym->type.t |= VT_WEAK;
289 if (sym->c > 0) {
290 int esym_type;
291 ElfW(Sym) *esym;
293 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
294 esym_type = ELFW(ST_TYPE)(esym->st_info);
295 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
299 /* ------------------------------------------------------------------------- */
301 ST_FUNC void swap(int *p, int *q)
303 int t;
304 t = *p;
305 *p = *q;
306 *q = t;
309 static void vsetc(CType *type, int r, CValue *vc)
311 int v;
313 if (vtop >= vstack + (VSTACK_SIZE - 1))
314 tcc_error("memory full (vstack)");
315 /* cannot let cpu flags if other instruction are generated. Also
316 avoid leaving VT_JMP anywhere except on the top of the stack
317 because it would complicate the code generator. */
318 if (vtop >= vstack) {
319 v = vtop->r & VT_VALMASK;
320 if (v == VT_CMP || (v & ~1) == VT_JMP)
321 gv(RC_INT);
323 vtop++;
324 vtop->type = *type;
325 vtop->r = r;
326 vtop->r2 = VT_CONST;
327 vtop->c = *vc;
330 /* push constant of type "type" with useless value */
331 void vpush(CType *type)
333 CValue cval;
334 vsetc(type, VT_CONST, &cval);
337 /* push integer constant */
338 ST_FUNC void vpushi(int v)
340 CValue cval;
341 cval.i = v;
342 vsetc(&int_type, VT_CONST, &cval);
345 /* push a pointer sized constant */
346 static void vpushs(addr_t v)
348 CValue cval;
349 cval.ptr_offset = v;
350 vsetc(&size_type, VT_CONST, &cval);
353 /* push arbitrary 64bit constant */
354 void vpush64(int ty, unsigned long long v)
356 CValue cval;
357 CType ctype;
358 ctype.t = ty;
359 ctype.ref = NULL;
360 cval.ull = v;
361 vsetc(&ctype, VT_CONST, &cval);
364 /* push long long constant */
365 static inline void vpushll(long long v)
367 vpush64(VT_LLONG, v);
370 /* push a symbol value of TYPE */
371 static inline void vpushsym(CType *type, Sym *sym)
373 CValue cval;
374 cval.ptr_offset = 0;
375 vsetc(type, VT_CONST | VT_SYM, &cval);
376 vtop->sym = sym;
379 /* Return a static symbol pointing to a section */
380 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
382 int v;
383 Sym *sym;
385 v = anon_sym++;
386 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
387 sym->type.ref = type->ref;
388 sym->r = VT_CONST | VT_SYM;
389 put_extern_sym(sym, sec, offset, size);
390 return sym;
393 /* push a reference to a section offset by adding a dummy symbol */
394 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
396 vpushsym(type, get_sym_ref(type, sec, offset, size));
399 /* define a new external reference to a symbol 'v' of type 'u' */
400 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
402 Sym *s;
404 s = sym_find(v);
405 if (!s) {
406 /* push forward reference */
407 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
408 s->type.ref = type->ref;
409 s->r = r | VT_CONST | VT_SYM;
411 return s;
414 /* define a new external reference to a symbol 'v' with alternate asm
415 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
416 is no alternate name (most cases) */
417 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
419 Sym *s;
421 s = sym_find(v);
422 if (!s) {
423 /* push forward reference */
424 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
425 s->asm_label = asm_label;
426 s->type.t |= VT_EXTERN;
427 } else if (s->type.ref == func_old_type.ref) {
428 s->type.ref = type->ref;
429 s->r = r | VT_CONST | VT_SYM;
430 s->type.t |= VT_EXTERN;
431 } else if (!is_compatible_types(&s->type, type)) {
432 tcc_error("incompatible types for redefinition of '%s'",
433 get_tok_str(v, NULL));
435 return s;
438 /* push a reference to global symbol v */
439 ST_FUNC void vpush_global_sym(CType *type, int v)
441 vpushsym(type, external_global_sym(v, type, 0));
444 ST_FUNC void vset(CType *type, int r, int v)
446 CValue cval;
448 cval.i = v;
449 vsetc(type, r, &cval);
452 static void vseti(int r, int v)
454 CType type;
455 type.t = VT_INT;
456 type.ref = 0;
457 vset(&type, r, v);
460 ST_FUNC void vswap(void)
462 SValue tmp;
463 /* cannot let cpu flags if other instruction are generated. Also
464 avoid leaving VT_JMP anywhere except on the top of the stack
465 because it would complicate the code generator. */
466 if (vtop >= vstack) {
467 int v = vtop->r & VT_VALMASK;
468 if (v == VT_CMP || (v & ~1) == VT_JMP)
469 gv(RC_INT);
471 tmp = vtop[0];
472 vtop[0] = vtop[-1];
473 vtop[-1] = tmp;
475 /* XXX: +2% overall speed possible with optimized memswap
477 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
481 ST_FUNC void vpushv(SValue *v)
483 if (vtop >= vstack + (VSTACK_SIZE - 1))
484 tcc_error("memory full (vstack)");
485 vtop++;
486 *vtop = *v;
489 static void vdup(void)
491 vpushv(vtop);
494 /* save r to the memory stack, and mark it as being free */
495 ST_FUNC void save_reg(int r)
497 int l, saved, size, align;
498 SValue *p, sv;
499 CType *type;
501 /* modify all stack values */
502 saved = 0;
503 l = 0;
504 for(p=vstack;p<=vtop;p++) {
505 if ((p->r & VT_VALMASK) == r ||
506 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
507 /* must save value on stack if not already done */
508 if (!saved) {
509 /* NOTE: must reload 'r' because r might be equal to r2 */
510 r = p->r & VT_VALMASK;
511 /* store register in the stack */
512 type = &p->type;
513 if ((p->r & VT_LVAL) ||
514 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
515 #ifdef TCC_TARGET_X86_64
516 type = &char_pointer_type;
517 #else
518 type = &int_type;
519 #endif
520 size = type_size(type, &align);
521 loc = (loc - size) & -align;
522 sv.type.t = type->t;
523 sv.r = VT_LOCAL | VT_LVAL;
524 sv.c.ul = loc;
525 store(r, &sv);
526 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
527 /* x86 specific: need to pop fp register ST0 if saved */
528 if (r == TREG_ST0) {
529 o(0xd8dd); /* fstp %st(0) */
531 #endif
532 #ifndef TCC_TARGET_X86_64
533 /* special long long case */
534 if ((type->t & VT_BTYPE) == VT_LLONG) {
535 sv.c.ul += 4;
536 store(p->r2, &sv);
538 #endif
539 l = loc;
540 saved = 1;
542 /* mark that stack entry as being saved on the stack */
543 if (p->r & VT_LVAL) {
544 /* also clear the bounded flag because the
545 relocation address of the function was stored in
546 p->c.ul */
547 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
548 } else {
549 p->r = lvalue_type(p->type.t) | VT_LOCAL;
551 p->r2 = VT_CONST;
552 p->c.ul = l;
557 #ifdef TCC_TARGET_ARM
558 /* find a register of class 'rc2' with at most one reference on stack.
559 * If none, call get_reg(rc) */
560 ST_FUNC int get_reg_ex(int rc, int rc2)
562 int r;
563 SValue *p;
565 for(r=0;r<NB_REGS;r++) {
566 if (reg_classes[r] & rc2) {
567 int n;
568 n=0;
569 for(p = vstack; p <= vtop; p++) {
570 if ((p->r & VT_VALMASK) == r ||
571 (p->r2 & VT_VALMASK) == r)
572 n++;
574 if (n <= 1)
575 return r;
578 return get_reg(rc);
580 #endif
582 /* find a free register of class 'rc'. If none, save one register */
583 ST_FUNC int get_reg(int rc)
585 int r;
586 SValue *p;
588 /* find a free register */
589 for(r=0;r<NB_REGS;r++) {
590 if (reg_classes[r] & rc) {
591 for(p=vstack;p<=vtop;p++) {
592 if ((p->r & VT_VALMASK) == r ||
593 (p->r2 & VT_VALMASK) == r)
594 goto notfound;
596 return r;
598 notfound: ;
601 /* no register left : free the first one on the stack (VERY
602 IMPORTANT to start from the bottom to ensure that we don't
603 spill registers used in gen_opi()) */
604 for(p=vstack;p<=vtop;p++) {
605 /* look at second register (if long long) */
606 r = p->r2 & VT_VALMASK;
607 if (r < VT_CONST && (reg_classes[r] & rc))
608 goto save_found;
609 r = p->r & VT_VALMASK;
610 if (r < VT_CONST && (reg_classes[r] & rc)) {
611 save_found:
612 save_reg(r);
613 return r;
616 /* Should never comes here */
617 return -1;
620 /* save registers up to (vtop - n) stack entry */
621 ST_FUNC void save_regs(int n)
623 int r;
624 SValue *p, *p1;
625 p1 = vtop - n;
626 for(p = vstack;p <= p1; p++) {
627 r = p->r & VT_VALMASK;
628 if (r < VT_CONST) {
629 save_reg(r);
634 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
635 if needed */
636 static void move_reg(int r, int s, int t)
638 SValue sv;
640 if (r != s) {
641 save_reg(r);
642 sv.type.t = t;
643 sv.type.ref = NULL;
644 sv.r = s;
645 sv.c.ul = 0;
646 load(r, &sv);
650 /* get address of vtop (vtop MUST BE an lvalue) */
651 static void gaddrof(void)
653 if (vtop->r & VT_REF)
654 gv(RC_INT);
655 vtop->r &= ~VT_LVAL;
656 /* tricky: if saved lvalue, then we can go back to lvalue */
657 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
658 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
663 #ifdef CONFIG_TCC_BCHECK
664 /* generate lvalue bound code */
665 static void gbound(void)
667 int lval_type;
668 CType type1;
670 vtop->r &= ~VT_MUSTBOUND;
671 /* if lvalue, then use checking code before dereferencing */
672 if (vtop->r & VT_LVAL) {
673 /* if not VT_BOUNDED value, then make one */
674 if (!(vtop->r & VT_BOUNDED)) {
675 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
676 /* must save type because we must set it to int to get pointer */
677 type1 = vtop->type;
678 vtop->type.t = VT_INT;
679 gaddrof();
680 vpushi(0);
681 gen_bounded_ptr_add();
682 vtop->r |= lval_type;
683 vtop->type = type1;
685 /* then check for dereferencing */
686 gen_bounded_ptr_deref();
689 #endif
691 /* store vtop a register belonging to class 'rc'. lvalues are
692 converted to values. Cannot be used if cannot be converted to
693 register value (such as structures). */
694 ST_FUNC int gv(int rc)
696 int r, bit_pos, bit_size, size, align, i;
697 int rc2;
699 /* NOTE: get_reg can modify vstack[] */
700 if (vtop->type.t & VT_BITFIELD) {
701 CType type;
702 int bits = 32;
703 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
704 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
705 /* remove bit field info to avoid loops */
706 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
707 /* cast to int to propagate signedness in following ops */
708 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
709 type.t = VT_LLONG;
710 bits = 64;
711 } else
712 type.t = VT_INT;
713 if((vtop->type.t & VT_UNSIGNED) ||
714 (vtop->type.t & VT_BTYPE) == VT_BOOL)
715 type.t |= VT_UNSIGNED;
716 gen_cast(&type);
717 /* generate shifts */
718 vpushi(bits - (bit_pos + bit_size));
719 gen_op(TOK_SHL);
720 vpushi(bits - bit_size);
721 /* NOTE: transformed to SHR if unsigned */
722 gen_op(TOK_SAR);
723 r = gv(rc);
724 } else {
725 if (is_float(vtop->type.t) &&
726 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
727 Sym *sym;
728 int *ptr;
729 unsigned long offset;
730 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
731 CValue check;
732 #endif
734 /* XXX: unify with initializers handling ? */
735 /* CPUs usually cannot use float constants, so we store them
736 generically in data segment */
737 size = type_size(&vtop->type, &align);
738 offset = (data_section->data_offset + align - 1) & -align;
739 data_section->data_offset = offset;
740 /* XXX: not portable yet */
741 #if defined(__i386__) || defined(__x86_64__)
742 /* Zero pad x87 tenbyte long doubles */
743 if (size == LDOUBLE_SIZE) {
744 vtop->c.tab[2] &= 0xffff;
745 #if LDOUBLE_SIZE == 16
746 vtop->c.tab[3] = 0;
747 #endif
749 #endif
750 ptr = section_ptr_add(data_section, size);
751 size = size >> 2;
752 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
753 check.d = 1;
754 if(check.tab[0])
755 for(i=0;i<size;i++)
756 ptr[i] = vtop->c.tab[size-1-i];
757 else
758 #endif
759 for(i=0;i<size;i++)
760 ptr[i] = vtop->c.tab[i];
761 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
762 vtop->r |= VT_LVAL | VT_SYM;
763 vtop->sym = sym;
764 vtop->c.ptr_offset = 0;
766 #ifdef CONFIG_TCC_BCHECK
767 if (vtop->r & VT_MUSTBOUND)
768 gbound();
769 #endif
771 r = vtop->r & VT_VALMASK;
772 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
773 if (rc == RC_IRET)
774 rc2 = RC_LRET;
775 #ifdef TCC_TARGET_X86_64
776 else if (rc == RC_FRET)
777 rc2 = RC_QRET;
778 #endif
780 /* need to reload if:
781 - constant
782 - lvalue (need to dereference pointer)
783 - already a register, but not in the right class */
784 if (r >= VT_CONST
785 || (vtop->r & VT_LVAL)
786 || !(reg_classes[r] & rc)
787 #ifdef TCC_TARGET_X86_64
788 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
789 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
790 #else
791 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
792 #endif
795 r = get_reg(rc);
796 #ifdef TCC_TARGET_X86_64
797 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
798 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
799 #else
800 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
801 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
802 unsigned long long ll;
803 #endif
804 int r2, original_type;
805 original_type = vtop->type.t;
806 /* two register type load : expand to two words
807 temporarily */
808 #ifndef TCC_TARGET_X86_64
809 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
810 /* load constant */
811 ll = vtop->c.ull;
812 vtop->c.ui = ll; /* first word */
813 load(r, vtop);
814 vtop->r = r; /* save register value */
815 vpushi(ll >> 32); /* second word */
816 } else
817 #endif
818 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
819 (vtop->r & VT_LVAL)) {
820 /* We do not want to modifier the long long
821 pointer here, so the safest (and less
822 efficient) is to save all the other registers
823 in the stack. XXX: totally inefficient. */
824 save_regs(1);
825 /* load from memory */
826 vtop->type.t = load_type;
827 load(r, vtop);
828 vdup();
829 vtop[-1].r = r; /* save register value */
830 /* increment pointer to get second word */
831 vtop->type.t = addr_type;
832 gaddrof();
833 vpushi(load_size);
834 gen_op('+');
835 vtop->r |= VT_LVAL;
836 vtop->type.t = load_type;
837 } else {
838 /* move registers */
839 load(r, vtop);
840 vdup();
841 vtop[-1].r = r; /* save register value */
842 vtop->r = vtop[-1].r2;
844 /* Allocate second register. Here we rely on the fact that
845 get_reg() tries first to free r2 of an SValue. */
846 r2 = get_reg(rc2);
847 load(r2, vtop);
848 vpop();
849 /* write second register */
850 vtop->r2 = r2;
851 vtop->type.t = original_type;
852 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
853 int t1, t;
854 /* lvalue of scalar type : need to use lvalue type
855 because of possible cast */
856 t = vtop->type.t;
857 t1 = t;
858 /* compute memory access type */
859 if (vtop->r & VT_REF)
860 #ifdef TCC_TARGET_X86_64
861 t = VT_PTR;
862 #else
863 t = VT_INT;
864 #endif
865 else if (vtop->r & VT_LVAL_BYTE)
866 t = VT_BYTE;
867 else if (vtop->r & VT_LVAL_SHORT)
868 t = VT_SHORT;
869 if (vtop->r & VT_LVAL_UNSIGNED)
870 t |= VT_UNSIGNED;
871 vtop->type.t = t;
872 load(r, vtop);
873 /* restore wanted type */
874 vtop->type.t = t1;
875 } else {
876 /* one register type load */
877 load(r, vtop);
880 vtop->r = r;
881 #ifdef TCC_TARGET_C67
882 /* uses register pairs for doubles */
883 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
884 vtop->r2 = r+1;
885 #endif
887 return r;
890 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
891 ST_FUNC void gv2(int rc1, int rc2)
893 int v;
895 /* generate more generic register first. But VT_JMP or VT_CMP
896 values must be generated first in all cases to avoid possible
897 reload errors */
898 v = vtop[0].r & VT_VALMASK;
899 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
900 vswap();
901 gv(rc1);
902 vswap();
903 gv(rc2);
904 /* test if reload is needed for first register */
905 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
906 vswap();
907 gv(rc1);
908 vswap();
910 } else {
911 gv(rc2);
912 vswap();
913 gv(rc1);
914 vswap();
915 /* test if reload is needed for first register */
916 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
917 gv(rc2);
922 /* wrapper around RC_FRET to return a register by type */
923 static int rc_fret(int t)
925 #ifdef TCC_TARGET_X86_64
926 if (t == VT_LDOUBLE) {
927 return RC_ST0;
929 #endif
930 return RC_FRET;
933 /* wrapper around REG_FRET to return a register by type */
934 static int reg_fret(int t)
936 #ifdef TCC_TARGET_X86_64
937 if (t == VT_LDOUBLE) {
938 return TREG_ST0;
940 #endif
941 return REG_FRET;
944 /* expand long long on stack in two int registers */
945 static void lexpand(void)
947 int u;
949 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
950 gv(RC_INT);
951 vdup();
952 vtop[0].r = vtop[-1].r2;
953 vtop[0].r2 = VT_CONST;
954 vtop[-1].r2 = VT_CONST;
955 vtop[0].type.t = VT_INT | u;
956 vtop[-1].type.t = VT_INT | u;
959 #ifdef TCC_TARGET_ARM
960 /* expand long long on stack */
961 ST_FUNC void lexpand_nr(void)
963 int u,v;
965 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
966 vdup();
967 vtop->r2 = VT_CONST;
968 vtop->type.t = VT_INT | u;
969 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
970 if (v == VT_CONST) {
971 vtop[-1].c.ui = vtop->c.ull;
972 vtop->c.ui = vtop->c.ull >> 32;
973 vtop->r = VT_CONST;
974 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
975 vtop->c.ui += 4;
976 vtop->r = vtop[-1].r;
977 } else if (v > VT_CONST) {
978 vtop--;
979 lexpand();
980 } else
981 vtop->r = vtop[-1].r2;
982 vtop[-1].r2 = VT_CONST;
983 vtop[-1].type.t = VT_INT | u;
985 #endif
987 /* build a long long from two ints */
988 static void lbuild(int t)
990 gv2(RC_INT, RC_INT);
991 vtop[-1].r2 = vtop[0].r;
992 vtop[-1].type.t = t;
993 vpop();
996 /* rotate n first stack elements to the bottom
997 I1 ... In -> I2 ... In I1 [top is right]
999 ST_FUNC void vrotb(int n)
1001 int i;
1002 SValue tmp;
1004 tmp = vtop[-n + 1];
1005 for(i=-n+1;i!=0;i++)
1006 vtop[i] = vtop[i+1];
1007 vtop[0] = tmp;
1010 /* rotate the n elements before entry e towards the top
1011 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1013 ST_FUNC void vrote(SValue *e, int n)
1015 int i;
1016 SValue tmp;
1018 tmp = *e;
1019 for(i = 0;i < n - 1; i++)
1020 e[-i] = e[-i - 1];
1021 e[-n + 1] = tmp;
1024 /* rotate n first stack elements to the top
1025 I1 ... In -> In I1 ... I(n-1) [top is right]
1027 ST_FUNC void vrott(int n)
1029 vrote(vtop, n);
1032 /* pop stack value */
1033 ST_FUNC void vpop(void)
1035 int v;
1036 v = vtop->r & VT_VALMASK;
1037 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1038 /* for x86, we need to pop the FP stack */
1039 if (v == TREG_ST0 && !nocode_wanted) {
1040 o(0xd8dd); /* fstp %st(0) */
1041 } else
1042 #endif
1043 if (v == VT_JMP || v == VT_JMPI) {
1044 /* need to put correct jump if && or || without test */
1045 gsym(vtop->c.ul);
1047 vtop--;
1050 /* convert stack entry to register and duplicate its value in another
1051 register */
1052 static void gv_dup(void)
1054 int rc, t, r, r1;
1055 SValue sv;
1057 t = vtop->type.t;
1058 if ((t & VT_BTYPE) == VT_LLONG) {
1059 lexpand();
1060 gv_dup();
1061 vswap();
1062 vrotb(3);
1063 gv_dup();
1064 vrotb(4);
1065 /* stack: H L L1 H1 */
1066 lbuild(t);
1067 vrotb(3);
1068 vrotb(3);
1069 vswap();
1070 lbuild(t);
1071 vswap();
1072 } else {
1073 /* duplicate value */
1074 rc = RC_INT;
1075 sv.type.t = VT_INT;
1076 if (is_float(t)) {
1077 rc = RC_FLOAT;
1078 #ifdef TCC_TARGET_X86_64
1079 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1080 rc = RC_ST0;
1082 #endif
1083 sv.type.t = t;
1085 r = gv(rc);
1086 r1 = get_reg(rc);
1087 sv.r = r;
1088 sv.c.ul = 0;
1089 load(r1, &sv); /* move r to r1 */
1090 vdup();
1091 /* duplicates value */
1092 if (r != r1)
1093 vtop->r = r1;
1097 /* Generate value test
1099 * Generate a test for any value (jump, comparison and integers) */
1100 int gvtst(int inv, int t)
1102 int v = vtop->r & VT_VALMASK;
1103 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1104 vpushi(0);
1105 gen_op(TOK_NE);
1107 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1108 /* constant jmp optimization */
1109 if ((vtop->c.i != 0) != inv)
1110 t = gjmp(t);
1111 vtop--;
1112 return t;
1114 return gtst(inv, t);
1117 #ifndef TCC_TARGET_X86_64
1118 /* generate CPU independent (unsigned) long long operations */
1119 static void gen_opl(int op)
1121 int t, a, b, op1, c, i;
1122 int func;
1123 unsigned short reg_iret = REG_IRET;
1124 unsigned short reg_lret = REG_LRET;
1125 SValue tmp;
1127 switch(op) {
1128 case '/':
1129 case TOK_PDIV:
1130 func = TOK___divdi3;
1131 goto gen_func;
1132 case TOK_UDIV:
1133 func = TOK___udivdi3;
1134 goto gen_func;
1135 case '%':
1136 func = TOK___moddi3;
1137 goto gen_mod_func;
1138 case TOK_UMOD:
1139 func = TOK___umoddi3;
1140 gen_mod_func:
1141 #ifdef TCC_ARM_EABI
1142 reg_iret = TREG_R2;
1143 reg_lret = TREG_R3;
1144 #endif
1145 gen_func:
1146 /* call generic long long function */
1147 vpush_global_sym(&func_old_type, func);
1148 vrott(3);
1149 gfunc_call(2);
1150 vpushi(0);
1151 vtop->r = reg_iret;
1152 vtop->r2 = reg_lret;
1153 break;
1154 case '^':
1155 case '&':
1156 case '|':
1157 case '*':
1158 case '+':
1159 case '-':
1160 t = vtop->type.t;
1161 vswap();
1162 lexpand();
1163 vrotb(3);
1164 lexpand();
1165 /* stack: L1 H1 L2 H2 */
1166 tmp = vtop[0];
1167 vtop[0] = vtop[-3];
1168 vtop[-3] = tmp;
1169 tmp = vtop[-2];
1170 vtop[-2] = vtop[-3];
1171 vtop[-3] = tmp;
1172 vswap();
1173 /* stack: H1 H2 L1 L2 */
1174 if (op == '*') {
1175 vpushv(vtop - 1);
1176 vpushv(vtop - 1);
1177 gen_op(TOK_UMULL);
1178 lexpand();
1179 /* stack: H1 H2 L1 L2 ML MH */
1180 for(i=0;i<4;i++)
1181 vrotb(6);
1182 /* stack: ML MH H1 H2 L1 L2 */
1183 tmp = vtop[0];
1184 vtop[0] = vtop[-2];
1185 vtop[-2] = tmp;
1186 /* stack: ML MH H1 L2 H2 L1 */
1187 gen_op('*');
1188 vrotb(3);
1189 vrotb(3);
1190 gen_op('*');
1191 /* stack: ML MH M1 M2 */
1192 gen_op('+');
1193 gen_op('+');
1194 } else if (op == '+' || op == '-') {
1195 /* XXX: add non carry method too (for MIPS or alpha) */
1196 if (op == '+')
1197 op1 = TOK_ADDC1;
1198 else
1199 op1 = TOK_SUBC1;
1200 gen_op(op1);
1201 /* stack: H1 H2 (L1 op L2) */
1202 vrotb(3);
1203 vrotb(3);
1204 gen_op(op1 + 1); /* TOK_xxxC2 */
1205 } else {
1206 gen_op(op);
1207 /* stack: H1 H2 (L1 op L2) */
1208 vrotb(3);
1209 vrotb(3);
1210 /* stack: (L1 op L2) H1 H2 */
1211 gen_op(op);
1212 /* stack: (L1 op L2) (H1 op H2) */
1214 /* stack: L H */
1215 lbuild(t);
1216 break;
1217 case TOK_SAR:
1218 case TOK_SHR:
1219 case TOK_SHL:
1220 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1221 t = vtop[-1].type.t;
1222 vswap();
1223 lexpand();
1224 vrotb(3);
1225 /* stack: L H shift */
1226 c = (int)vtop->c.i;
1227 /* constant: simpler */
1228 /* NOTE: all comments are for SHL. the other cases are
1229 done by swaping words */
1230 vpop();
1231 if (op != TOK_SHL)
1232 vswap();
1233 if (c >= 32) {
1234 /* stack: L H */
1235 vpop();
1236 if (c > 32) {
1237 vpushi(c - 32);
1238 gen_op(op);
1240 if (op != TOK_SAR) {
1241 vpushi(0);
1242 } else {
1243 gv_dup();
1244 vpushi(31);
1245 gen_op(TOK_SAR);
1247 vswap();
1248 } else {
1249 vswap();
1250 gv_dup();
1251 /* stack: H L L */
1252 vpushi(c);
1253 gen_op(op);
1254 vswap();
1255 vpushi(32 - c);
1256 if (op == TOK_SHL)
1257 gen_op(TOK_SHR);
1258 else
1259 gen_op(TOK_SHL);
1260 vrotb(3);
1261 /* stack: L L H */
1262 vpushi(c);
1263 if (op == TOK_SHL)
1264 gen_op(TOK_SHL);
1265 else
1266 gen_op(TOK_SHR);
1267 gen_op('|');
1269 if (op != TOK_SHL)
1270 vswap();
1271 lbuild(t);
1272 } else {
1273 /* XXX: should provide a faster fallback on x86 ? */
1274 switch(op) {
1275 case TOK_SAR:
1276 func = TOK___ashrdi3;
1277 goto gen_func;
1278 case TOK_SHR:
1279 func = TOK___lshrdi3;
1280 goto gen_func;
1281 case TOK_SHL:
1282 func = TOK___ashldi3;
1283 goto gen_func;
1286 break;
1287 default:
1288 /* compare operations */
1289 t = vtop->type.t;
1290 vswap();
1291 lexpand();
1292 vrotb(3);
1293 lexpand();
1294 /* stack: L1 H1 L2 H2 */
1295 tmp = vtop[-1];
1296 vtop[-1] = vtop[-2];
1297 vtop[-2] = tmp;
1298 /* stack: L1 L2 H1 H2 */
1299 /* compare high */
1300 op1 = op;
1301 /* when values are equal, we need to compare low words. since
1302 the jump is inverted, we invert the test too. */
1303 if (op1 == TOK_LT)
1304 op1 = TOK_LE;
1305 else if (op1 == TOK_GT)
1306 op1 = TOK_GE;
1307 else if (op1 == TOK_ULT)
1308 op1 = TOK_ULE;
1309 else if (op1 == TOK_UGT)
1310 op1 = TOK_UGE;
1311 a = 0;
1312 b = 0;
1313 gen_op(op1);
1314 if (op1 != TOK_NE) {
1315 a = gvtst(1, 0);
1317 if (op != TOK_EQ) {
1318 /* generate non equal test */
1319 /* XXX: NOT PORTABLE yet */
1320 if (a == 0) {
1321 b = gvtst(0, 0);
1322 } else {
1323 #if defined(TCC_TARGET_I386)
1324 b = psym(0x850f, 0);
1325 #elif defined(TCC_TARGET_ARM)
1326 b = ind;
1327 o(0x1A000000 | encbranch(ind, 0, 1));
1328 #elif defined(TCC_TARGET_C67)
1329 tcc_error("not implemented");
1330 #else
1331 #error not supported
1332 #endif
1335 /* compare low. Always unsigned */
1336 op1 = op;
1337 if (op1 == TOK_LT)
1338 op1 = TOK_ULT;
1339 else if (op1 == TOK_LE)
1340 op1 = TOK_ULE;
1341 else if (op1 == TOK_GT)
1342 op1 = TOK_UGT;
1343 else if (op1 == TOK_GE)
1344 op1 = TOK_UGE;
1345 gen_op(op1);
1346 a = gvtst(1, a);
1347 gsym(b);
1348 vseti(VT_JMPI, a);
1349 break;
1352 #endif
1354 /* handle integer constant optimizations and various machine
1355 independent opt */
1356 static void gen_opic(int op)
1358 int c1, c2, t1, t2, n;
1359 SValue *v1, *v2;
1360 long long l1, l2;
1361 typedef unsigned long long U;
1363 v1 = vtop - 1;
1364 v2 = vtop;
1365 t1 = v1->type.t & VT_BTYPE;
1366 t2 = v2->type.t & VT_BTYPE;
1368 if (t1 == VT_LLONG)
1369 l1 = v1->c.ll;
1370 else if (v1->type.t & VT_UNSIGNED)
1371 l1 = v1->c.ui;
1372 else
1373 l1 = v1->c.i;
1375 if (t2 == VT_LLONG)
1376 l2 = v2->c.ll;
1377 else if (v2->type.t & VT_UNSIGNED)
1378 l2 = v2->c.ui;
1379 else
1380 l2 = v2->c.i;
1382 /* currently, we cannot do computations with forward symbols */
1383 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1384 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1385 if (c1 && c2) {
1386 switch(op) {
1387 case '+': l1 += l2; break;
1388 case '-': l1 -= l2; break;
1389 case '&': l1 &= l2; break;
1390 case '^': l1 ^= l2; break;
1391 case '|': l1 |= l2; break;
1392 case '*': l1 *= l2; break;
1394 case TOK_PDIV:
1395 case '/':
1396 case '%':
1397 case TOK_UDIV:
1398 case TOK_UMOD:
1399 /* if division by zero, generate explicit division */
1400 if (l2 == 0) {
1401 if (const_wanted)
1402 tcc_error("division by zero in constant");
1403 goto general_case;
1405 switch(op) {
1406 default: l1 /= l2; break;
1407 case '%': l1 %= l2; break;
1408 case TOK_UDIV: l1 = (U)l1 / l2; break;
1409 case TOK_UMOD: l1 = (U)l1 % l2; break;
1411 break;
1412 case TOK_SHL: l1 <<= l2; break;
1413 case TOK_SHR: l1 = (U)l1 >> l2; break;
1414 case TOK_SAR: l1 >>= l2; break;
1415 /* tests */
1416 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1417 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1418 case TOK_EQ: l1 = l1 == l2; break;
1419 case TOK_NE: l1 = l1 != l2; break;
1420 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1421 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1422 case TOK_LT: l1 = l1 < l2; break;
1423 case TOK_GE: l1 = l1 >= l2; break;
1424 case TOK_LE: l1 = l1 <= l2; break;
1425 case TOK_GT: l1 = l1 > l2; break;
1426 /* logical */
1427 case TOK_LAND: l1 = l1 && l2; break;
1428 case TOK_LOR: l1 = l1 || l2; break;
1429 default:
1430 goto general_case;
1432 v1->c.ll = l1;
1433 vtop--;
1434 } else {
1435 /* if commutative ops, put c2 as constant */
1436 if (c1 && (op == '+' || op == '&' || op == '^' ||
1437 op == '|' || op == '*')) {
1438 vswap();
1439 c2 = c1; //c = c1, c1 = c2, c2 = c;
1440 l2 = l1; //l = l1, l1 = l2, l2 = l;
1442 /* Filter out NOP operations like x*1, x-0, x&-1... */
1443 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1444 op == TOK_PDIV) &&
1445 l2 == 1) ||
1446 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1447 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1448 l2 == 0) ||
1449 (op == '&' &&
1450 l2 == -1))) {
1451 /* nothing to do */
1452 vtop--;
1453 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1454 /* try to use shifts instead of muls or divs */
1455 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1456 n = -1;
1457 while (l2) {
1458 l2 >>= 1;
1459 n++;
1461 vtop->c.ll = n;
1462 if (op == '*')
1463 op = TOK_SHL;
1464 else if (op == TOK_PDIV)
1465 op = TOK_SAR;
1466 else
1467 op = TOK_SHR;
1469 goto general_case;
1470 } else if (c2 && (op == '+' || op == '-') &&
1471 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1472 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1473 /* symbol + constant case */
1474 if (op == '-')
1475 l2 = -l2;
1476 vtop--;
1477 vtop->c.ll += l2;
1478 } else {
1479 general_case:
1480 if (!nocode_wanted) {
1481 /* call low level op generator */
1482 if (t1 == VT_LLONG || t2 == VT_LLONG)
1483 gen_opl(op);
1484 else
1485 gen_opi(op);
1486 } else {
1487 vtop--;
1493 /* generate a floating point operation with constant propagation */
1494 static void gen_opif(int op)
1496 int c1, c2;
1497 SValue *v1, *v2;
1498 long double f1, f2;
1500 v1 = vtop - 1;
1501 v2 = vtop;
1502 /* currently, we cannot do computations with forward symbols */
1503 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1504 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1505 if (c1 && c2) {
1506 if (v1->type.t == VT_FLOAT) {
1507 f1 = v1->c.f;
1508 f2 = v2->c.f;
1509 } else if (v1->type.t == VT_DOUBLE) {
1510 f1 = v1->c.d;
1511 f2 = v2->c.d;
1512 } else {
1513 f1 = v1->c.ld;
1514 f2 = v2->c.ld;
1517 /* NOTE: we only do constant propagation if finite number (not
1518 NaN or infinity) (ANSI spec) */
1519 if (!ieee_finite(f1) || !ieee_finite(f2))
1520 goto general_case;
1522 switch(op) {
1523 case '+': f1 += f2; break;
1524 case '-': f1 -= f2; break;
1525 case '*': f1 *= f2; break;
1526 case '/':
1527 if (f2 == 0.0) {
1528 if (const_wanted)
1529 tcc_error("division by zero in constant");
1530 goto general_case;
1532 f1 /= f2;
1533 break;
1534 /* XXX: also handles tests ? */
1535 default:
1536 goto general_case;
1538 /* XXX: overflow test ? */
1539 if (v1->type.t == VT_FLOAT) {
1540 v1->c.f = f1;
1541 } else if (v1->type.t == VT_DOUBLE) {
1542 v1->c.d = f1;
1543 } else {
1544 v1->c.ld = f1;
1546 vtop--;
1547 } else {
1548 general_case:
1549 if (!nocode_wanted) {
1550 gen_opf(op);
1551 } else {
1552 vtop--;
1557 static int pointed_size(CType *type)
1559 int align;
1560 return type_size(pointed_type(type), &align);
1563 static void vla_runtime_pointed_size(CType *type)
1565 int align;
1566 vla_runtime_type_size(pointed_type(type), &align);
1569 static inline int is_null_pointer(SValue *p)
1571 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1572 return 0;
1573 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1574 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1575 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0);
1578 static inline int is_integer_btype(int bt)
1580 return (bt == VT_BYTE || bt == VT_SHORT ||
1581 bt == VT_INT || bt == VT_LLONG);
1584 /* check types for comparison or subtraction of pointers */
1585 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1587 CType *type1, *type2, tmp_type1, tmp_type2;
1588 int bt1, bt2;
1590 /* null pointers are accepted for all comparisons as gcc */
1591 if (is_null_pointer(p1) || is_null_pointer(p2))
1592 return;
1593 type1 = &p1->type;
1594 type2 = &p2->type;
1595 bt1 = type1->t & VT_BTYPE;
1596 bt2 = type2->t & VT_BTYPE;
1597 /* accept comparison between pointer and integer with a warning */
1598 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1599 if (op != TOK_LOR && op != TOK_LAND )
1600 tcc_warning("comparison between pointer and integer");
1601 return;
1604 /* both must be pointers or implicit function pointers */
1605 if (bt1 == VT_PTR) {
1606 type1 = pointed_type(type1);
1607 } else if (bt1 != VT_FUNC)
1608 goto invalid_operands;
1610 if (bt2 == VT_PTR) {
1611 type2 = pointed_type(type2);
1612 } else if (bt2 != VT_FUNC) {
1613 invalid_operands:
1614 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1616 if ((type1->t & VT_BTYPE) == VT_VOID ||
1617 (type2->t & VT_BTYPE) == VT_VOID)
1618 return;
1619 tmp_type1 = *type1;
1620 tmp_type2 = *type2;
1621 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1622 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1623 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1624 /* gcc-like error if '-' is used */
1625 if (op == '-')
1626 goto invalid_operands;
1627 else
1628 tcc_warning("comparison of distinct pointer types lacks a cast");
1632 /* generic gen_op: handles types problems */
1633 ST_FUNC void gen_op(int op)
1635 int u, t1, t2, bt1, bt2, t;
1636 CType type1;
1638 t1 = vtop[-1].type.t;
1639 t2 = vtop[0].type.t;
1640 bt1 = t1 & VT_BTYPE;
1641 bt2 = t2 & VT_BTYPE;
1643 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1644 /* at least one operand is a pointer */
1645 /* relationnal op: must be both pointers */
1646 if (op >= TOK_ULT && op <= TOK_LOR) {
1647 check_comparison_pointer_types(vtop - 1, vtop, op);
1648 /* pointers are handled are unsigned */
1649 #ifdef TCC_TARGET_X86_64
1650 t = VT_LLONG | VT_UNSIGNED;
1651 #else
1652 t = VT_INT | VT_UNSIGNED;
1653 #endif
1654 goto std_op;
1656 /* if both pointers, then it must be the '-' op */
1657 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1658 if (op != '-')
1659 tcc_error("cannot use pointers here");
1660 check_comparison_pointer_types(vtop - 1, vtop, op);
1661 /* XXX: check that types are compatible */
1662 if (vtop[-1].type.t & VT_VLA) {
1663 vla_runtime_pointed_size(&vtop[-1].type);
1664 } else {
1665 vpushi(pointed_size(&vtop[-1].type));
1667 vrott(3);
1668 gen_opic(op);
1669 /* set to integer type */
1670 #ifdef TCC_TARGET_X86_64
1671 vtop->type.t = VT_LLONG;
1672 #else
1673 vtop->type.t = VT_INT;
1674 #endif
1675 vswap();
1676 gen_op(TOK_PDIV);
1677 } else {
1678 /* exactly one pointer : must be '+' or '-'. */
1679 if (op != '-' && op != '+')
1680 tcc_error("cannot use pointers here");
1681 /* Put pointer as first operand */
1682 if (bt2 == VT_PTR) {
1683 vswap();
1684 swap(&t1, &t2);
1686 type1 = vtop[-1].type;
1687 type1.t &= ~VT_ARRAY;
1688 if (vtop[-1].type.t & VT_VLA)
1689 vla_runtime_pointed_size(&vtop[-1].type);
1690 else {
1691 u = pointed_size(&vtop[-1].type);
1692 if (u < 0)
1693 tcc_error("unknown array element size");
1694 #ifdef TCC_TARGET_X86_64
1695 vpushll(u);
1696 #else
1697 /* XXX: cast to int ? (long long case) */
1698 vpushi(u);
1699 #endif
1701 gen_op('*');
1702 #ifdef CONFIG_TCC_BCHECK
1703 /* if evaluating constant expression, no code should be
1704 generated, so no bound check */
1705 if (tcc_state->do_bounds_check && !const_wanted) {
1706 /* if bounded pointers, we generate a special code to
1707 test bounds */
1708 if (op == '-') {
1709 vpushi(0);
1710 vswap();
1711 gen_op('-');
1713 gen_bounded_ptr_add();
1714 } else
1715 #endif
1717 gen_opic(op);
1719 /* put again type if gen_opic() swaped operands */
1720 vtop->type = type1;
1722 } else if (is_float(bt1) || is_float(bt2)) {
1723 /* compute bigger type and do implicit casts */
1724 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1725 t = VT_LDOUBLE;
1726 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1727 t = VT_DOUBLE;
1728 } else {
1729 t = VT_FLOAT;
1731 /* floats can only be used for a few operations */
1732 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1733 (op < TOK_ULT || op > TOK_GT))
1734 tcc_error("invalid operands for binary operation");
1735 goto std_op;
1736 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1737 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1738 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1739 t |= VT_UNSIGNED;
1740 goto std_op;
1741 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1742 /* cast to biggest op */
1743 t = VT_LLONG;
1744 /* convert to unsigned if it does not fit in a long long */
1745 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1746 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1747 t |= VT_UNSIGNED;
1748 goto std_op;
1749 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1750 tcc_error("comparison of struct");
1751 } else {
1752 /* integer operations */
1753 t = VT_INT;
1754 /* convert to unsigned if it does not fit in an integer */
1755 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1756 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1757 t |= VT_UNSIGNED;
1758 std_op:
1759 /* XXX: currently, some unsigned operations are explicit, so
1760 we modify them here */
1761 if (t & VT_UNSIGNED) {
1762 if (op == TOK_SAR)
1763 op = TOK_SHR;
1764 else if (op == '/')
1765 op = TOK_UDIV;
1766 else if (op == '%')
1767 op = TOK_UMOD;
1768 else if (op == TOK_LT)
1769 op = TOK_ULT;
1770 else if (op == TOK_GT)
1771 op = TOK_UGT;
1772 else if (op == TOK_LE)
1773 op = TOK_ULE;
1774 else if (op == TOK_GE)
1775 op = TOK_UGE;
1777 vswap();
1778 type1.t = t;
1779 gen_cast(&type1);
1780 vswap();
1781 /* special case for shifts and long long: we keep the shift as
1782 an integer */
1783 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1784 type1.t = VT_INT;
1785 gen_cast(&type1);
1786 if (is_float(t))
1787 gen_opif(op);
1788 else
1789 gen_opic(op);
1790 if (op >= TOK_ULT && op <= TOK_GT) {
1791 /* relationnal op: the result is an int */
1792 vtop->type.t = VT_INT;
1793 } else {
1794 vtop->type.t = t;
1799 #ifndef TCC_TARGET_ARM
1800 /* generic itof for unsigned long long case */
1801 static void gen_cvt_itof1(int t)
1803 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1804 (VT_LLONG | VT_UNSIGNED)) {
1806 if (t == VT_FLOAT)
1807 vpush_global_sym(&func_old_type, TOK___floatundisf);
1808 #if LDOUBLE_SIZE != 8
1809 else if (t == VT_LDOUBLE)
1810 vpush_global_sym(&func_old_type, TOK___floatundixf);
1811 #endif
1812 else
1813 vpush_global_sym(&func_old_type, TOK___floatundidf);
1814 vrott(2);
1815 gfunc_call(1);
1816 vpushi(0);
1817 vtop->r = reg_fret(t);
1818 } else {
1819 gen_cvt_itof(t);
1822 #endif
1824 /* generic ftoi for unsigned long long case */
1825 static void gen_cvt_ftoi1(int t)
1827 int st;
1829 if (t == (VT_LLONG | VT_UNSIGNED)) {
1830 /* not handled natively */
1831 st = vtop->type.t & VT_BTYPE;
1832 if (st == VT_FLOAT)
1833 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1834 #if LDOUBLE_SIZE != 8
1835 else if (st == VT_LDOUBLE)
1836 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1837 #endif
1838 else
1839 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1840 vrott(2);
1841 gfunc_call(1);
1842 vpushi(0);
1843 vtop->r = REG_IRET;
1844 vtop->r2 = REG_LRET;
1845 } else {
1846 gen_cvt_ftoi(t);
1850 /* force char or short cast */
1851 static void force_charshort_cast(int t)
1853 int bits, dbt;
1854 dbt = t & VT_BTYPE;
1855 /* XXX: add optimization if lvalue : just change type and offset */
1856 if (dbt == VT_BYTE)
1857 bits = 8;
1858 else
1859 bits = 16;
1860 if (t & VT_UNSIGNED) {
1861 vpushi((1 << bits) - 1);
1862 gen_op('&');
1863 } else {
1864 bits = 32 - bits;
1865 vpushi(bits);
1866 gen_op(TOK_SHL);
1867 /* result must be signed or the SAR is converted to an SHL
1868 This was not the case when "t" was a signed short
1869 and the last value on the stack was an unsigned int */
1870 vtop->type.t &= ~VT_UNSIGNED;
1871 vpushi(bits);
1872 gen_op(TOK_SAR);
1876 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1877 static void gen_cast(CType *type)
1879 int sbt, dbt, sf, df, c, p;
1881 /* special delayed cast for char/short */
1882 /* XXX: in some cases (multiple cascaded casts), it may still
1883 be incorrect */
1884 if (vtop->r & VT_MUSTCAST) {
1885 vtop->r &= ~VT_MUSTCAST;
1886 force_charshort_cast(vtop->type.t);
1889 /* bitfields first get cast to ints */
1890 if (vtop->type.t & VT_BITFIELD) {
1891 gv(RC_INT);
1894 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1895 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1897 if (sbt != dbt) {
1898 sf = is_float(sbt);
1899 df = is_float(dbt);
1900 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1901 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1902 if (c) {
1903 /* constant case: we can do it now */
1904 /* XXX: in ISOC, cannot do it if error in convert */
1905 if (sbt == VT_FLOAT)
1906 vtop->c.ld = vtop->c.f;
1907 else if (sbt == VT_DOUBLE)
1908 vtop->c.ld = vtop->c.d;
1910 if (df) {
1911 if ((sbt & VT_BTYPE) == VT_LLONG) {
1912 if (sbt & VT_UNSIGNED)
1913 vtop->c.ld = vtop->c.ull;
1914 else
1915 vtop->c.ld = vtop->c.ll;
1916 } else if(!sf) {
1917 if (sbt & VT_UNSIGNED)
1918 vtop->c.ld = vtop->c.ui;
1919 else
1920 vtop->c.ld = vtop->c.i;
1923 if (dbt == VT_FLOAT)
1924 vtop->c.f = (float)vtop->c.ld;
1925 else if (dbt == VT_DOUBLE)
1926 vtop->c.d = (double)vtop->c.ld;
1927 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1928 vtop->c.ull = (unsigned long long)vtop->c.ld;
1929 } else if (sf && dbt == VT_BOOL) {
1930 vtop->c.i = (vtop->c.ld != 0);
1931 } else {
1932 if(sf)
1933 vtop->c.ll = (long long)vtop->c.ld;
1934 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1935 vtop->c.ll = vtop->c.ull;
1936 else if (sbt & VT_UNSIGNED)
1937 vtop->c.ll = vtop->c.ui;
1938 #ifdef TCC_TARGET_X86_64
1939 else if (sbt == VT_PTR)
1941 #endif
1942 else if (sbt != VT_LLONG)
1943 vtop->c.ll = vtop->c.i;
1945 if (dbt == (VT_LLONG|VT_UNSIGNED))
1946 vtop->c.ull = vtop->c.ll;
1947 else if (dbt == VT_BOOL)
1948 vtop->c.i = (vtop->c.ll != 0);
1949 #ifdef TCC_TARGET_X86_64
1950 else if (dbt == VT_PTR)
1952 #endif
1953 else if (dbt != VT_LLONG) {
1954 int s = 0;
1955 if ((dbt & VT_BTYPE) == VT_BYTE)
1956 s = 24;
1957 else if ((dbt & VT_BTYPE) == VT_SHORT)
1958 s = 16;
1959 if(dbt & VT_UNSIGNED)
1960 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1961 else
1962 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1965 } else if (p && dbt == VT_BOOL) {
1966 vtop->r = VT_CONST;
1967 vtop->c.i = 1;
1968 } else if (!nocode_wanted) {
1969 /* non constant case: generate code */
1970 if (sf && df) {
1971 /* convert from fp to fp */
1972 gen_cvt_ftof(dbt);
1973 } else if (df) {
1974 /* convert int to fp */
1975 gen_cvt_itof1(dbt);
1976 } else if (sf) {
1977 /* convert fp to int */
1978 if (dbt == VT_BOOL) {
1979 vpushi(0);
1980 gen_op(TOK_NE);
1981 } else {
1982 /* we handle char/short/etc... with generic code */
1983 if (dbt != (VT_INT | VT_UNSIGNED) &&
1984 dbt != (VT_LLONG | VT_UNSIGNED) &&
1985 dbt != VT_LLONG)
1986 dbt = VT_INT;
1987 gen_cvt_ftoi1(dbt);
1988 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1989 /* additional cast for char/short... */
1990 vtop->type.t = dbt;
1991 gen_cast(type);
1994 #ifndef TCC_TARGET_X86_64
1995 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1996 if ((sbt & VT_BTYPE) != VT_LLONG) {
1997 /* scalar to long long */
1998 /* machine independent conversion */
1999 gv(RC_INT);
2000 /* generate high word */
2001 if (sbt == (VT_INT | VT_UNSIGNED)) {
2002 vpushi(0);
2003 gv(RC_INT);
2004 } else {
2005 if (sbt == VT_PTR) {
2006 /* cast from pointer to int before we apply
2007 shift operation, which pointers don't support*/
2008 gen_cast(&int_type);
2010 gv_dup();
2011 vpushi(31);
2012 gen_op(TOK_SAR);
2014 /* patch second register */
2015 vtop[-1].r2 = vtop->r;
2016 vpop();
2018 #else
2019 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2020 (dbt & VT_BTYPE) == VT_PTR ||
2021 (dbt & VT_BTYPE) == VT_FUNC) {
2022 if ((sbt & VT_BTYPE) != VT_LLONG &&
2023 (sbt & VT_BTYPE) != VT_PTR &&
2024 (sbt & VT_BTYPE) != VT_FUNC) {
2025 /* need to convert from 32bit to 64bit */
2026 int r = gv(RC_INT);
2027 if (sbt != (VT_INT | VT_UNSIGNED)) {
2028 /* x86_64 specific: movslq */
2029 o(0x6348);
2030 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2033 #endif
2034 } else if (dbt == VT_BOOL) {
2035 /* scalar to bool */
2036 vpushi(0);
2037 gen_op(TOK_NE);
2038 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2039 (dbt & VT_BTYPE) == VT_SHORT) {
2040 if (sbt == VT_PTR) {
2041 vtop->type.t = VT_INT;
2042 tcc_warning("nonportable conversion from pointer to char/short");
2044 force_charshort_cast(dbt);
2045 } else if ((dbt & VT_BTYPE) == VT_INT) {
2046 /* scalar to int */
2047 if (sbt == VT_LLONG) {
2048 /* from long long: just take low order word */
2049 lexpand();
2050 vpop();
2052 /* if lvalue and single word type, nothing to do because
2053 the lvalue already contains the real type size (see
2054 VT_LVAL_xxx constants) */
2057 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2058 /* if we are casting between pointer types,
2059 we must update the VT_LVAL_xxx size */
2060 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2061 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2063 vtop->type = *type;
2066 /* return type size as known at compile time. Put alignment at 'a' */
2067 ST_FUNC int type_size(CType *type, int *a)
2069 Sym *s;
2070 int bt;
2072 bt = type->t & VT_BTYPE;
2073 if (bt == VT_STRUCT) {
2074 /* struct/union */
2075 s = type->ref;
2076 *a = s->r;
2077 return s->c;
2078 } else if (bt == VT_PTR) {
2079 if (type->t & VT_ARRAY) {
2080 int ts;
2082 s = type->ref;
2083 ts = type_size(&s->type, a);
2085 if (ts < 0 && s->c < 0)
2086 ts = -ts;
2088 return ts * s->c;
2089 } else {
2090 *a = PTR_SIZE;
2091 return PTR_SIZE;
2093 } else if (bt == VT_LDOUBLE) {
2094 *a = LDOUBLE_ALIGN;
2095 return LDOUBLE_SIZE;
2096 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2097 #ifdef TCC_TARGET_I386
2098 #ifdef TCC_TARGET_PE
2099 *a = 8;
2100 #else
2101 *a = 4;
2102 #endif
2103 #elif defined(TCC_TARGET_ARM)
2104 #ifdef TCC_ARM_EABI
2105 *a = 8;
2106 #else
2107 *a = 4;
2108 #endif
2109 #else
2110 *a = 8;
2111 #endif
2112 return 8;
2113 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2114 *a = 4;
2115 return 4;
2116 } else if (bt == VT_SHORT) {
2117 *a = 2;
2118 return 2;
2119 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2120 *a = 8;
2121 return 16;
2122 } else {
2123 /* char, void, function, _Bool */
2124 *a = 1;
2125 return 1;
2129 /* push type size as known at runtime time on top of value stack. Put
2130 alignment at 'a' */
2131 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2133 if (type->t & VT_VLA) {
2134 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2135 } else {
2136 vpushi(type_size(type, a));
2140 static void vla_sp_save(void) {
2141 if (!(vla_flags & VLA_SP_LOC_SET)) {
2142 *vla_sp_loc = (loc -= PTR_SIZE);
2143 vla_flags |= VLA_SP_LOC_SET;
2145 if (!(vla_flags & VLA_SP_SAVED)) {
2146 gen_vla_sp_save(*vla_sp_loc);
2147 vla_flags |= VLA_SP_SAVED;
2151 /* return the pointed type of t */
2152 static inline CType *pointed_type(CType *type)
2154 return &type->ref->type;
2157 /* modify type so that its it is a pointer to type. */
2158 ST_FUNC void mk_pointer(CType *type)
2160 Sym *s;
2161 s = sym_push(SYM_FIELD, type, 0, -1);
2162 type->t = VT_PTR | (type->t & ~VT_TYPE);
2163 type->ref = s;
2166 /* compare function types. OLD functions match any new functions */
2167 static int is_compatible_func(CType *type1, CType *type2)
2169 Sym *s1, *s2;
2171 s1 = type1->ref;
2172 s2 = type2->ref;
2173 if (!is_compatible_types(&s1->type, &s2->type))
2174 return 0;
2175 /* check func_call */
2176 if (s1->a.func_call != s2->a.func_call)
2177 return 0;
2178 /* XXX: not complete */
2179 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2180 return 1;
2181 if (s1->c != s2->c)
2182 return 0;
2183 while (s1 != NULL) {
2184 if (s2 == NULL)
2185 return 0;
2186 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2187 return 0;
2188 s1 = s1->next;
2189 s2 = s2->next;
2191 if (s2)
2192 return 0;
2193 return 1;
2196 /* return true if type1 and type2 are the same. If unqualified is
2197 true, qualifiers on the types are ignored.
2199 - enums are not checked as gcc __builtin_types_compatible_p ()
2201 static int compare_types(CType *type1, CType *type2, int unqualified)
2203 int bt1, t1, t2;
2205 t1 = type1->t & VT_TYPE;
2206 t2 = type2->t & VT_TYPE;
2207 if (unqualified) {
2208 /* strip qualifiers before comparing */
2209 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2210 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2212 /* Default Vs explicit signedness only matters for char */
2213 if ((t1 & VT_BTYPE) != VT_BYTE) {
2214 t1 &= ~VT_DEFSIGN;
2215 t2 &= ~VT_DEFSIGN;
2217 /* XXX: bitfields ? */
2218 if (t1 != t2)
2219 return 0;
2220 /* test more complicated cases */
2221 bt1 = t1 & VT_BTYPE;
2222 if (bt1 == VT_PTR) {
2223 type1 = pointed_type(type1);
2224 type2 = pointed_type(type2);
2225 return is_compatible_types(type1, type2);
2226 } else if (bt1 == VT_STRUCT) {
2227 return (type1->ref == type2->ref);
2228 } else if (bt1 == VT_FUNC) {
2229 return is_compatible_func(type1, type2);
2230 } else {
2231 return 1;
2235 /* return true if type1 and type2 are exactly the same (including
2236 qualifiers).
2238 static int is_compatible_types(CType *type1, CType *type2)
2240 return compare_types(type1,type2,0);
2243 /* return true if type1 and type2 are the same (ignoring qualifiers).
2245 static int is_compatible_parameter_types(CType *type1, CType *type2)
2247 return compare_types(type1,type2,1);
2250 /* print a type. If 'varstr' is not NULL, then the variable is also
2251 printed in the type */
2252 /* XXX: union */
2253 /* XXX: add array and function pointers */
2254 static void type_to_str(char *buf, int buf_size,
2255 CType *type, const char *varstr)
2257 int bt, v, t;
2258 Sym *s, *sa;
2259 char buf1[256];
2260 const char *tstr;
2262 t = type->t & VT_TYPE;
2263 bt = t & VT_BTYPE;
2264 buf[0] = '\0';
2265 if (t & VT_CONSTANT)
2266 pstrcat(buf, buf_size, "const ");
2267 if (t & VT_VOLATILE)
2268 pstrcat(buf, buf_size, "volatile ");
2269 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2270 pstrcat(buf, buf_size, "unsigned ");
2271 else if (t & VT_DEFSIGN)
2272 pstrcat(buf, buf_size, "signed ");
2273 switch(bt) {
2274 case VT_VOID:
2275 tstr = "void";
2276 goto add_tstr;
2277 case VT_BOOL:
2278 tstr = "_Bool";
2279 goto add_tstr;
2280 case VT_BYTE:
2281 tstr = "char";
2282 goto add_tstr;
2283 case VT_SHORT:
2284 tstr = "short";
2285 goto add_tstr;
2286 case VT_INT:
2287 tstr = "int";
2288 goto add_tstr;
2289 case VT_LONG:
2290 tstr = "long";
2291 goto add_tstr;
2292 case VT_LLONG:
2293 tstr = "long long";
2294 goto add_tstr;
2295 case VT_FLOAT:
2296 tstr = "float";
2297 goto add_tstr;
2298 case VT_DOUBLE:
2299 tstr = "double";
2300 goto add_tstr;
2301 case VT_LDOUBLE:
2302 tstr = "long double";
2303 add_tstr:
2304 pstrcat(buf, buf_size, tstr);
2305 break;
2306 case VT_ENUM:
2307 case VT_STRUCT:
2308 if (bt == VT_STRUCT)
2309 tstr = "struct ";
2310 else
2311 tstr = "enum ";
2312 pstrcat(buf, buf_size, tstr);
2313 v = type->ref->v & ~SYM_STRUCT;
2314 if (v >= SYM_FIRST_ANOM)
2315 pstrcat(buf, buf_size, "<anonymous>");
2316 else
2317 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2318 break;
2319 case VT_FUNC:
2320 s = type->ref;
2321 type_to_str(buf, buf_size, &s->type, varstr);
2322 pstrcat(buf, buf_size, "(");
2323 sa = s->next;
2324 while (sa != NULL) {
2325 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2326 pstrcat(buf, buf_size, buf1);
2327 sa = sa->next;
2328 if (sa)
2329 pstrcat(buf, buf_size, ", ");
2331 pstrcat(buf, buf_size, ")");
2332 goto no_var;
2333 case VT_PTR:
2334 s = type->ref;
2335 pstrcpy(buf1, sizeof(buf1), "*");
2336 if (varstr)
2337 pstrcat(buf1, sizeof(buf1), varstr);
2338 type_to_str(buf, buf_size, &s->type, buf1);
2339 goto no_var;
2341 if (varstr) {
2342 pstrcat(buf, buf_size, " ");
2343 pstrcat(buf, buf_size, varstr);
2345 no_var: ;
2348 /* verify type compatibility to store vtop in 'dt' type, and generate
2349 casts if needed. */
2350 static void gen_assign_cast(CType *dt)
2352 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2353 char buf1[256], buf2[256];
2354 int dbt, sbt;
2356 st = &vtop->type; /* source type */
2357 dbt = dt->t & VT_BTYPE;
2358 sbt = st->t & VT_BTYPE;
2359 if (sbt == VT_VOID || dbt == VT_VOID)
2360 tcc_error("cannot cast from/to void");
2361 if (dt->t & VT_CONSTANT)
2362 tcc_warning("assignment of read-only location");
2363 switch(dbt) {
2364 case VT_PTR:
2365 /* special cases for pointers */
2366 /* '0' can also be a pointer */
2367 if (is_null_pointer(vtop))
2368 goto type_ok;
2369 /* accept implicit pointer to integer cast with warning */
2370 if (is_integer_btype(sbt)) {
2371 tcc_warning("assignment makes pointer from integer without a cast");
2372 goto type_ok;
2374 type1 = pointed_type(dt);
2375 /* a function is implicitely a function pointer */
2376 if (sbt == VT_FUNC) {
2377 if ((type1->t & VT_BTYPE) != VT_VOID &&
2378 !is_compatible_types(pointed_type(dt), st))
2379 tcc_warning("assignment from incompatible pointer type");
2380 goto type_ok;
2382 if (sbt != VT_PTR)
2383 goto error;
2384 type2 = pointed_type(st);
2385 if ((type1->t & VT_BTYPE) == VT_VOID ||
2386 (type2->t & VT_BTYPE) == VT_VOID) {
2387 /* void * can match anything */
2388 } else {
2389 /* exact type match, except for unsigned */
2390 tmp_type1 = *type1;
2391 tmp_type2 = *type2;
2392 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2393 VT_VOLATILE);
2394 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2395 VT_VOLATILE);
2396 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2397 tcc_warning("assignment from incompatible pointer type");
2399 /* check const and volatile */
2400 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2401 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2402 tcc_warning("assignment discards qualifiers from pointer target type");
2403 break;
2404 case VT_BYTE:
2405 case VT_SHORT:
2406 case VT_INT:
2407 case VT_LLONG:
2408 if (sbt == VT_PTR || sbt == VT_FUNC) {
2409 tcc_warning("assignment makes integer from pointer without a cast");
2411 /* XXX: more tests */
2412 break;
2413 case VT_STRUCT:
2414 tmp_type1 = *dt;
2415 tmp_type2 = *st;
2416 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2417 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2418 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2419 error:
2420 type_to_str(buf1, sizeof(buf1), st, NULL);
2421 type_to_str(buf2, sizeof(buf2), dt, NULL);
2422 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2424 break;
2426 type_ok:
2427 gen_cast(dt);
2430 /* store vtop in lvalue pushed on stack */
2431 ST_FUNC void vstore(void)
2433 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2435 ft = vtop[-1].type.t;
2436 sbt = vtop->type.t & VT_BTYPE;
2437 dbt = ft & VT_BTYPE;
2438 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2439 (sbt == VT_INT && dbt == VT_SHORT))
2440 && !(vtop->type.t & VT_BITFIELD)) {
2441 /* optimize char/short casts */
2442 delayed_cast = VT_MUSTCAST;
2443 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2444 /* XXX: factorize */
2445 if (ft & VT_CONSTANT)
2446 tcc_warning("assignment of read-only location");
2447 } else {
2448 delayed_cast = 0;
2449 if (!(ft & VT_BITFIELD))
2450 gen_assign_cast(&vtop[-1].type);
2453 if (sbt == VT_STRUCT) {
2454 /* if structure, only generate pointer */
2455 /* structure assignment : generate memcpy */
2456 /* XXX: optimize if small size */
2457 if (!nocode_wanted) {
2458 size = type_size(&vtop->type, &align);
2460 /* destination */
2461 vswap();
2462 vtop->type.t = VT_PTR;
2463 gaddrof();
2465 /* address of memcpy() */
2466 #ifdef TCC_ARM_EABI
2467 if(!(align & 7))
2468 vpush_global_sym(&func_old_type, TOK_memcpy8);
2469 else if(!(align & 3))
2470 vpush_global_sym(&func_old_type, TOK_memcpy4);
2471 else
2472 #endif
2473 vpush_global_sym(&func_old_type, TOK_memcpy);
2475 vswap();
2476 /* source */
2477 vpushv(vtop - 2);
2478 vtop->type.t = VT_PTR;
2479 gaddrof();
2480 /* type size */
2481 vpushi(size);
2482 gfunc_call(3);
2483 } else {
2484 vswap();
2485 vpop();
2487 /* leave source on stack */
2488 } else if (ft & VT_BITFIELD) {
2489 /* bitfield store handling */
2490 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2491 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2492 /* remove bit field info to avoid loops */
2493 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2495 /* duplicate source into other register */
2496 gv_dup();
2497 vswap();
2498 vrott(3);
2500 if((ft & VT_BTYPE) == VT_BOOL) {
2501 gen_cast(&vtop[-1].type);
2502 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2505 /* duplicate destination */
2506 vdup();
2507 vtop[-1] = vtop[-2];
2509 /* mask and shift source */
2510 if((ft & VT_BTYPE) != VT_BOOL) {
2511 if((ft & VT_BTYPE) == VT_LLONG) {
2512 vpushll((1ULL << bit_size) - 1ULL);
2513 } else {
2514 vpushi((1 << bit_size) - 1);
2516 gen_op('&');
2518 vpushi(bit_pos);
2519 gen_op(TOK_SHL);
2520 /* load destination, mask and or with source */
2521 vswap();
2522 if((ft & VT_BTYPE) == VT_LLONG) {
2523 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2524 } else {
2525 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2527 gen_op('&');
2528 gen_op('|');
2529 /* store result */
2530 vstore();
2532 /* pop off shifted source from "duplicate source..." above */
2533 vpop();
2535 } else {
2536 #ifdef CONFIG_TCC_BCHECK
2537 /* bound check case */
2538 if (vtop[-1].r & VT_MUSTBOUND) {
2539 vswap();
2540 gbound();
2541 vswap();
2543 #endif
2544 if (!nocode_wanted) {
2545 rc = RC_INT;
2546 if (is_float(ft)) {
2547 rc = RC_FLOAT;
2548 #ifdef TCC_TARGET_X86_64
2549 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2550 rc = RC_ST0;
2551 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2552 rc = RC_FRET;
2554 #endif
2556 r = gv(rc); /* generate value */
2557 /* if lvalue was saved on stack, must read it */
2558 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2559 SValue sv;
2560 t = get_reg(RC_INT);
2561 #ifdef TCC_TARGET_X86_64
2562 sv.type.t = VT_PTR;
2563 #else
2564 sv.type.t = VT_INT;
2565 #endif
2566 sv.r = VT_LOCAL | VT_LVAL;
2567 sv.c.ul = vtop[-1].c.ul;
2568 load(t, &sv);
2569 vtop[-1].r = t | VT_LVAL;
2571 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2572 #ifdef TCC_TARGET_X86_64
2573 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2574 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2575 #else
2576 if ((ft & VT_BTYPE) == VT_LLONG) {
2577 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2578 #endif
2579 vtop[-1].type.t = load_type;
2580 store(r, vtop - 1);
2581 vswap();
2582 /* convert to int to increment easily */
2583 vtop->type.t = addr_type;
2584 gaddrof();
2585 vpushi(load_size);
2586 gen_op('+');
2587 vtop->r |= VT_LVAL;
2588 vswap();
2589 vtop[-1].type.t = load_type;
2590 /* XXX: it works because r2 is spilled last ! */
2591 store(vtop->r2, vtop - 1);
2592 } else {
2593 store(r, vtop - 1);
2596 vswap();
2597 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2598 vtop->r |= delayed_cast;
2602 /* post defines POST/PRE add. c is the token ++ or -- */
2603 ST_FUNC void inc(int post, int c)
2605 test_lvalue();
2606 vdup(); /* save lvalue */
2607 if (post) {
2608 gv_dup(); /* duplicate value */
2609 vrotb(3);
2610 vrotb(3);
2612 /* add constant */
2613 vpushi(c - TOK_MID);
2614 gen_op('+');
2615 vstore(); /* store value */
2616 if (post)
2617 vpop(); /* if post op, return saved value */
2620 /* Parse GNUC __attribute__ extension. Currently, the following
2621 extensions are recognized:
2622 - aligned(n) : set data/function alignment.
2623 - packed : force data alignment to 1
2624 - section(x) : generate data/code in this section.
2625 - unused : currently ignored, but may be used someday.
2626 - regparm(n) : pass function parameters in registers (i386 only)
2628 static void parse_attribute(AttributeDef *ad)
2630 int t, n;
2632 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2633 next();
2634 skip('(');
2635 skip('(');
2636 while (tok != ')') {
2637 if (tok < TOK_IDENT)
2638 expect("attribute name");
2639 t = tok;
2640 next();
2641 switch(t) {
2642 case TOK_SECTION1:
2643 case TOK_SECTION2:
2644 skip('(');
2645 if (tok != TOK_STR)
2646 expect("section name");
2647 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2648 next();
2649 skip(')');
2650 break;
2651 case TOK_ALIAS1:
2652 case TOK_ALIAS2:
2653 skip('(');
2654 if (tok != TOK_STR)
2655 expect("alias(\"target\")");
2656 ad->alias_target = /* save string as token, for later */
2657 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2658 next();
2659 skip(')');
2660 break;
2661 case TOK_ALIGNED1:
2662 case TOK_ALIGNED2:
2663 if (tok == '(') {
2664 next();
2665 n = expr_const();
2666 if (n <= 0 || (n & (n - 1)) != 0)
2667 tcc_error("alignment must be a positive power of two");
2668 skip(')');
2669 } else {
2670 n = MAX_ALIGN;
2672 ad->a.aligned = n;
2673 break;
2674 case TOK_PACKED1:
2675 case TOK_PACKED2:
2676 ad->a.packed = 1;
2677 break;
2678 case TOK_WEAK1:
2679 case TOK_WEAK2:
2680 ad->a.weak = 1;
2681 break;
2682 case TOK_UNUSED1:
2683 case TOK_UNUSED2:
2684 /* currently, no need to handle it because tcc does not
2685 track unused objects */
2686 break;
2687 case TOK_NORETURN1:
2688 case TOK_NORETURN2:
2689 /* currently, no need to handle it because tcc does not
2690 track unused objects */
2691 break;
2692 case TOK_CDECL1:
2693 case TOK_CDECL2:
2694 case TOK_CDECL3:
2695 ad->a.func_call = FUNC_CDECL;
2696 break;
2697 case TOK_STDCALL1:
2698 case TOK_STDCALL2:
2699 case TOK_STDCALL3:
2700 ad->a.func_call = FUNC_STDCALL;
2701 break;
2702 #ifdef TCC_TARGET_I386
2703 case TOK_REGPARM1:
2704 case TOK_REGPARM2:
2705 skip('(');
2706 n = expr_const();
2707 if (n > 3)
2708 n = 3;
2709 else if (n < 0)
2710 n = 0;
2711 if (n > 0)
2712 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2713 skip(')');
2714 break;
2715 case TOK_FASTCALL1:
2716 case TOK_FASTCALL2:
2717 case TOK_FASTCALL3:
2718 ad->a.func_call = FUNC_FASTCALLW;
2719 break;
2720 #endif
2721 case TOK_MODE:
2722 skip('(');
2723 switch(tok) {
2724 case TOK_MODE_DI:
2725 ad->a.mode = VT_LLONG + 1;
2726 break;
2727 case TOK_MODE_HI:
2728 ad->a.mode = VT_SHORT + 1;
2729 break;
2730 case TOK_MODE_SI:
2731 ad->a.mode = VT_INT + 1;
2732 break;
2733 default:
2734 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2735 break;
2737 next();
2738 skip(')');
2739 break;
2740 case TOK_DLLEXPORT:
2741 ad->a.func_export = 1;
2742 break;
2743 case TOK_DLLIMPORT:
2744 ad->a.func_import = 1;
2745 break;
2746 default:
2747 if (tcc_state->warn_unsupported)
2748 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2749 /* skip parameters */
2750 if (tok == '(') {
2751 int parenthesis = 0;
2752 do {
2753 if (tok == '(')
2754 parenthesis++;
2755 else if (tok == ')')
2756 parenthesis--;
2757 next();
2758 } while (parenthesis && tok != -1);
2760 break;
2762 if (tok != ',')
2763 break;
2764 next();
2766 skip(')');
2767 skip(')');
2771 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2772 static void struct_decl(CType *type, int u, int tdef)
2774 int a, v, size, align, maxalign, c, offset, flexible;
2775 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2776 Sym *s, *ss, *ass, **ps;
2777 AttributeDef ad;
2778 CType type1, btype;
2780 a = tok; /* save decl type */
2781 next();
2782 if (tok != '{') {
2783 v = tok;
2784 next();
2785 /* struct already defined ? return it */
2786 if (v < TOK_IDENT)
2787 expect("struct/union/enum name");
2788 s = struct_find(v);
2789 if (s) {
2790 if (s->type.t != a)
2791 tcc_error("invalid type");
2792 goto do_decl;
2793 } else if (tok >= TOK_IDENT && !tdef)
2794 tcc_error("unknown struct/union/enum");
2795 } else {
2796 v = anon_sym++;
2798 type1.t = a;
2799 type1.ref = NULL;
2800 /* we put an undefined size for struct/union */
2801 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2802 s->r = 0; /* default alignment is zero as gcc */
2803 /* put struct/union/enum name in type */
2804 do_decl:
2805 type->t = u;
2806 type->ref = s;
2808 if (tok == '{') {
2809 next();
2810 if (s->c != -1)
2811 tcc_error("struct/union/enum already defined");
2812 /* cannot be empty */
2813 c = 0;
2814 /* non empty enums are not allowed */
2815 if (a == TOK_ENUM) {
2816 for(;;) {
2817 v = tok;
2818 if (v < TOK_UIDENT)
2819 expect("identifier");
2820 ss = sym_find(v);
2821 if (ss && !local_stack)
2822 tcc_error("redefinition of enumerator '%s'",
2823 get_tok_str(v, NULL));
2824 next();
2825 if (tok == '=') {
2826 next();
2827 c = expr_const();
2829 /* enum symbols have static storage */
2830 ss = sym_push(v, &int_type, VT_CONST, c);
2831 ss->type.t |= VT_STATIC;
2832 if (tok != ',')
2833 break;
2834 next();
2835 c++;
2836 /* NOTE: we accept a trailing comma */
2837 if (tok == '}')
2838 break;
2840 s->c = type_size(&int_type, &align);
2841 skip('}');
2842 } else {
2843 maxalign = 1;
2844 ps = &s->next;
2845 prevbt = VT_INT;
2846 bit_pos = 0;
2847 offset = 0;
2848 flexible = 0;
2849 while (tok != '}') {
2850 parse_btype(&btype, &ad);
2851 while (1) {
2852 if (flexible)
2853 tcc_error("flexible array member '%s' not at the end of struct",
2854 get_tok_str(v, NULL));
2855 bit_size = -1;
2856 v = 0;
2857 type1 = btype;
2858 if (tok != ':') {
2859 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2860 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2861 expect("identifier");
2862 if (type_size(&type1, &align) < 0) {
2863 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2864 flexible = 1;
2865 else
2866 tcc_error("field '%s' has incomplete type",
2867 get_tok_str(v, NULL));
2869 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2870 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2871 tcc_error("invalid type for '%s'",
2872 get_tok_str(v, NULL));
2874 if (tok == ':') {
2875 next();
2876 bit_size = expr_const();
2877 /* XXX: handle v = 0 case for messages */
2878 if (bit_size < 0)
2879 tcc_error("negative width in bit-field '%s'",
2880 get_tok_str(v, NULL));
2881 if (v && bit_size == 0)
2882 tcc_error("zero width for bit-field '%s'",
2883 get_tok_str(v, NULL));
2885 size = type_size(&type1, &align);
2886 if (ad.a.aligned) {
2887 if (align < ad.a.aligned)
2888 align = ad.a.aligned;
2889 } else if (ad.a.packed) {
2890 align = 1;
2891 } else if (*tcc_state->pack_stack_ptr) {
2892 if (align > *tcc_state->pack_stack_ptr)
2893 align = *tcc_state->pack_stack_ptr;
2895 lbit_pos = 0;
2896 if (bit_size >= 0) {
2897 bt = type1.t & VT_BTYPE;
2898 if (bt != VT_INT &&
2899 bt != VT_BYTE &&
2900 bt != VT_SHORT &&
2901 bt != VT_BOOL &&
2902 bt != VT_ENUM &&
2903 bt != VT_LLONG)
2904 tcc_error("bitfields must have scalar type");
2905 bsize = size * 8;
2906 if (bit_size > bsize) {
2907 tcc_error("width of '%s' exceeds its type",
2908 get_tok_str(v, NULL));
2909 } else if (bit_size == bsize) {
2910 /* no need for bit fields */
2911 bit_pos = 0;
2912 } else if (bit_size == 0) {
2913 /* XXX: what to do if only padding in a
2914 structure ? */
2915 /* zero size: means to pad */
2916 bit_pos = 0;
2917 } else {
2918 /* we do not have enough room ?
2919 did the type change?
2920 is it a union? */
2921 if ((bit_pos + bit_size) > bsize ||
2922 bt != prevbt || a == TOK_UNION)
2923 bit_pos = 0;
2924 lbit_pos = bit_pos;
2925 /* XXX: handle LSB first */
2926 type1.t |= VT_BITFIELD |
2927 (bit_pos << VT_STRUCT_SHIFT) |
2928 (bit_size << (VT_STRUCT_SHIFT + 6));
2929 bit_pos += bit_size;
2931 prevbt = bt;
2932 } else {
2933 bit_pos = 0;
2935 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2936 /* add new memory data only if starting
2937 bit field */
2938 if (lbit_pos == 0) {
2939 if (a == TOK_STRUCT) {
2940 c = (c + align - 1) & -align;
2941 offset = c;
2942 if (size > 0)
2943 c += size;
2944 } else {
2945 offset = 0;
2946 if (size > c)
2947 c = size;
2949 if (align > maxalign)
2950 maxalign = align;
2952 #if 0
2953 printf("add field %s offset=%d",
2954 get_tok_str(v, NULL), offset);
2955 if (type1.t & VT_BITFIELD) {
2956 printf(" pos=%d size=%d",
2957 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2958 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2960 printf("\n");
2961 #endif
2963 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2964 ass = type1.ref;
2965 while ((ass = ass->next) != NULL) {
2966 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2967 *ps = ss;
2968 ps = &ss->next;
2970 } else if (v) {
2971 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2972 *ps = ss;
2973 ps = &ss->next;
2975 if (tok == ';' || tok == TOK_EOF)
2976 break;
2977 skip(',');
2979 skip(';');
2981 skip('}');
2982 /* store size and alignment */
2983 s->c = (c + maxalign - 1) & -maxalign;
2984 s->r = maxalign;
2989 /* return 1 if basic type is a type size (short, long, long long) */
2990 int is_btype_size (int bt)
2992 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
2995 /* return 0 if no type declaration. otherwise, return the basic type
2996 and skip it.
2998 static int parse_btype(CType *type, AttributeDef *ad)
3000 int t, u, bt_size, complete, type_found, typespec_found;
3001 Sym *s;
3002 CType type1;
3004 memset(ad, 0, sizeof(AttributeDef));
3005 complete = 0;
3006 type_found = 0;
3007 typespec_found = 0;
3008 t = 0;
3009 while(1) {
3010 switch(tok) {
3011 case TOK_EXTENSION:
3012 /* currently, we really ignore extension */
3013 next();
3014 continue;
3016 /* basic types */
3017 case TOK_CHAR:
3018 u = VT_BYTE;
3019 basic_type:
3020 next();
3021 basic_type1:
3022 if (complete)
3023 tcc_error("too many basic types");
3024 t |= u;
3025 bt_size = is_btype_size (u & VT_BTYPE);
3026 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3027 complete = 1;
3028 typespec_found = 1;
3029 break;
3030 case TOK_VOID:
3031 u = VT_VOID;
3032 goto basic_type;
3033 case TOK_SHORT:
3034 u = VT_SHORT;
3035 goto basic_type;
3036 case TOK_INT:
3037 u = VT_INT;
3038 goto basic_type;
3039 case TOK_LONG:
3040 next();
3041 if ((t & VT_BTYPE) == VT_DOUBLE) {
3042 #ifndef TCC_TARGET_PE
3043 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3044 #endif
3045 } else if ((t & VT_BTYPE) == VT_LONG) {
3046 t = (t & ~VT_BTYPE) | VT_LLONG;
3047 } else {
3048 u = VT_LONG;
3049 goto basic_type1;
3051 break;
3052 case TOK_BOOL:
3053 u = VT_BOOL;
3054 goto basic_type;
3055 case TOK_FLOAT:
3056 u = VT_FLOAT;
3057 goto basic_type;
3058 case TOK_DOUBLE:
3059 next();
3060 if ((t & VT_BTYPE) == VT_LONG) {
3061 #ifdef TCC_TARGET_PE
3062 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3063 #else
3064 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3065 #endif
3066 } else {
3067 u = VT_DOUBLE;
3068 goto basic_type1;
3070 break;
3071 case TOK_ENUM:
3072 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3073 basic_type2:
3074 u = type1.t;
3075 type->ref = type1.ref;
3076 goto basic_type1;
3077 case TOK_STRUCT:
3078 case TOK_UNION:
3079 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3080 goto basic_type2;
3082 /* type modifiers */
3083 case TOK_CONST1:
3084 case TOK_CONST2:
3085 case TOK_CONST3:
3086 t |= VT_CONSTANT;
3087 next();
3088 break;
3089 case TOK_VOLATILE1:
3090 case TOK_VOLATILE2:
3091 case TOK_VOLATILE3:
3092 t |= VT_VOLATILE;
3093 next();
3094 break;
3095 case TOK_SIGNED1:
3096 case TOK_SIGNED2:
3097 case TOK_SIGNED3:
3098 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3099 tcc_error("signed and unsigned modifier");
3100 typespec_found = 1;
3101 t |= VT_DEFSIGN;
3102 next();
3103 break;
3104 case TOK_REGISTER:
3105 case TOK_AUTO:
3106 case TOK_RESTRICT1:
3107 case TOK_RESTRICT2:
3108 case TOK_RESTRICT3:
3109 next();
3110 break;
3111 case TOK_UNSIGNED:
3112 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3113 tcc_error("signed and unsigned modifier");
3114 t |= VT_DEFSIGN | VT_UNSIGNED;
3115 next();
3116 typespec_found = 1;
3117 break;
3119 /* storage */
3120 case TOK_EXTERN:
3121 t |= VT_EXTERN;
3122 next();
3123 break;
3124 case TOK_STATIC:
3125 t |= VT_STATIC;
3126 next();
3127 break;
3128 case TOK_TYPEDEF:
3129 t |= VT_TYPEDEF;
3130 next();
3131 break;
3132 case TOK_INLINE1:
3133 case TOK_INLINE2:
3134 case TOK_INLINE3:
3135 t |= VT_INLINE;
3136 next();
3137 break;
3139 /* GNUC attribute */
3140 case TOK_ATTRIBUTE1:
3141 case TOK_ATTRIBUTE2:
3142 parse_attribute(ad);
3143 if (ad->a.mode) {
3144 u = ad->a.mode -1;
3145 t = (t & ~VT_BTYPE) | u;
3147 break;
3148 /* GNUC typeof */
3149 case TOK_TYPEOF1:
3150 case TOK_TYPEOF2:
3151 case TOK_TYPEOF3:
3152 next();
3153 parse_expr_type(&type1);
3154 /* remove all storage modifiers except typedef */
3155 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3156 goto basic_type2;
3157 default:
3158 if (typespec_found)
3159 goto the_end;
3160 s = sym_find(tok);
3161 if (!s || !(s->type.t & VT_TYPEDEF))
3162 goto the_end;
3163 t |= (s->type.t & ~VT_TYPEDEF);
3164 type->ref = s->type.ref;
3165 if (s->r) {
3166 /* get attributes from typedef */
3167 if (0 == ad->a.aligned)
3168 ad->a.aligned = s->a.aligned;
3169 if (0 == ad->a.func_call)
3170 ad->a.func_call = s->a.func_call;
3171 ad->a.packed |= s->a.packed;
3173 next();
3174 typespec_found = 1;
3175 break;
3177 type_found = 1;
3179 the_end:
3180 if (tcc_state->char_is_unsigned) {
3181 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3182 t |= VT_UNSIGNED;
3185 /* long is never used as type */
3186 if ((t & VT_BTYPE) == VT_LONG)
3187 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3188 t = (t & ~VT_BTYPE) | VT_INT;
3189 #else
3190 t = (t & ~VT_BTYPE) | VT_LLONG;
3191 #endif
3192 type->t = t;
3193 return type_found;
3196 /* convert a function parameter type (array to pointer and function to
3197 function pointer) */
3198 static inline void convert_parameter_type(CType *pt)
3200 /* remove const and volatile qualifiers (XXX: const could be used
3201 to indicate a const function parameter */
3202 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3203 /* array must be transformed to pointer according to ANSI C */
3204 pt->t &= ~VT_ARRAY;
3205 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3206 mk_pointer(pt);
3210 ST_FUNC void parse_asm_str(CString *astr)
3212 skip('(');
3213 /* read the string */
3214 if (tok != TOK_STR)
3215 expect("string constant");
3216 cstr_new(astr);
3217 while (tok == TOK_STR) {
3218 /* XXX: add \0 handling too ? */
3219 cstr_cat(astr, tokc.cstr->data);
3220 next();
3222 cstr_ccat(astr, '\0');
3225 /* Parse an asm label and return the label
3226 * Don't forget to free the CString in the caller! */
3227 static void asm_label_instr(CString *astr)
3229 next();
3230 parse_asm_str(astr);
3231 skip(')');
3232 #ifdef ASM_DEBUG
3233 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3234 #endif
3237 static void post_type(CType *type, AttributeDef *ad)
3239 int n, l, t1, arg_size, align;
3240 Sym **plast, *s, *first;
3241 AttributeDef ad1;
3242 CType pt;
3244 if (tok == '(') {
3245 /* function declaration */
3246 next();
3247 l = 0;
3248 first = NULL;
3249 plast = &first;
3250 arg_size = 0;
3251 if (tok != ')') {
3252 for(;;) {
3253 /* read param name and compute offset */
3254 if (l != FUNC_OLD) {
3255 if (!parse_btype(&pt, &ad1)) {
3256 if (l) {
3257 tcc_error("invalid type");
3258 } else {
3259 l = FUNC_OLD;
3260 goto old_proto;
3263 l = FUNC_NEW;
3264 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3265 break;
3266 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3267 if ((pt.t & VT_BTYPE) == VT_VOID)
3268 tcc_error("parameter declared as void");
3269 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3270 } else {
3271 old_proto:
3272 n = tok;
3273 if (n < TOK_UIDENT)
3274 expect("identifier");
3275 pt.t = VT_INT;
3276 next();
3278 convert_parameter_type(&pt);
3279 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3280 *plast = s;
3281 plast = &s->next;
3282 if (tok == ')')
3283 break;
3284 skip(',');
3285 if (l == FUNC_NEW && tok == TOK_DOTS) {
3286 l = FUNC_ELLIPSIS;
3287 next();
3288 break;
3292 /* if no parameters, then old type prototype */
3293 if (l == 0)
3294 l = FUNC_OLD;
3295 skip(')');
3296 /* NOTE: const is ignored in returned type as it has a special
3297 meaning in gcc / C++ */
3298 type->t &= ~VT_CONSTANT;
3299 /* some ancient pre-K&R C allows a function to return an array
3300 and the array brackets to be put after the arguments, such
3301 that "int c()[]" means something like "int[] c()" */
3302 if (tok == '[') {
3303 next();
3304 skip(']'); /* only handle simple "[]" */
3305 type->t |= VT_PTR;
3307 /* we push a anonymous symbol which will contain the function prototype */
3308 ad->a.func_args = arg_size;
3309 s = sym_push(SYM_FIELD, type, 0, l);
3310 s->a = ad->a;
3311 s->next = first;
3312 type->t = VT_FUNC;
3313 type->ref = s;
3314 } else if (tok == '[') {
3315 /* array definition */
3316 next();
3317 if (tok == TOK_RESTRICT1)
3318 next();
3319 n = -1;
3320 t1 = 0;
3321 if (tok != ']') {
3322 if (!local_stack || nocode_wanted)
3323 vpushi(expr_const());
3324 else gexpr();
3325 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3326 n = vtop->c.i;
3327 if (n < 0)
3328 tcc_error("invalid array size");
3329 } else {
3330 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3331 tcc_error("size of variable length array should be an integer");
3332 t1 = VT_VLA;
3335 skip(']');
3336 /* parse next post type */
3337 post_type(type, ad);
3338 if (type->t == VT_FUNC)
3339 tcc_error("declaration of an array of functions");
3340 t1 |= type->t & VT_VLA;
3342 if (t1 & VT_VLA) {
3343 loc -= type_size(&int_type, &align);
3344 loc &= -align;
3345 n = loc;
3347 vla_runtime_type_size(type, &align);
3348 gen_op('*');
3349 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3350 vswap();
3351 vstore();
3353 if (n != -1)
3354 vpop();
3356 /* we push an anonymous symbol which will contain the array
3357 element type */
3358 s = sym_push(SYM_FIELD, type, 0, n);
3359 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3360 type->ref = s;
3364 /* Parse a type declaration (except basic type), and return the type
3365 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3366 expected. 'type' should contain the basic type. 'ad' is the
3367 attribute definition of the basic type. It can be modified by
3368 type_decl().
3370 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3372 Sym *s;
3373 CType type1, *type2;
3374 int qualifiers, storage;
3376 while (tok == '*') {
3377 qualifiers = 0;
3378 redo:
3379 next();
3380 switch(tok) {
3381 case TOK_CONST1:
3382 case TOK_CONST2:
3383 case TOK_CONST3:
3384 qualifiers |= VT_CONSTANT;
3385 goto redo;
3386 case TOK_VOLATILE1:
3387 case TOK_VOLATILE2:
3388 case TOK_VOLATILE3:
3389 qualifiers |= VT_VOLATILE;
3390 goto redo;
3391 case TOK_RESTRICT1:
3392 case TOK_RESTRICT2:
3393 case TOK_RESTRICT3:
3394 goto redo;
3396 mk_pointer(type);
3397 type->t |= qualifiers;
3400 /* XXX: clarify attribute handling */
3401 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3402 parse_attribute(ad);
3404 /* recursive type */
3405 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3406 type1.t = 0; /* XXX: same as int */
3407 if (tok == '(') {
3408 next();
3409 /* XXX: this is not correct to modify 'ad' at this point, but
3410 the syntax is not clear */
3411 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3412 parse_attribute(ad);
3413 type_decl(&type1, ad, v, td);
3414 skip(')');
3415 } else {
3416 /* type identifier */
3417 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3418 *v = tok;
3419 next();
3420 } else {
3421 if (!(td & TYPE_ABSTRACT))
3422 expect("identifier");
3423 *v = 0;
3426 storage = type->t & VT_STORAGE;
3427 type->t &= ~VT_STORAGE;
3428 if (storage & VT_STATIC) {
3429 int saved_nocode_wanted = nocode_wanted;
3430 nocode_wanted = 1;
3431 post_type(type, ad);
3432 nocode_wanted = saved_nocode_wanted;
3433 } else
3434 post_type(type, ad);
3435 type->t |= storage;
3436 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3437 parse_attribute(ad);
3439 if (!type1.t)
3440 return;
3441 /* append type at the end of type1 */
3442 type2 = &type1;
3443 for(;;) {
3444 s = type2->ref;
3445 type2 = &s->type;
3446 if (!type2->t) {
3447 *type2 = *type;
3448 break;
3451 *type = type1;
3454 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3455 ST_FUNC int lvalue_type(int t)
3457 int bt, r;
3458 r = VT_LVAL;
3459 bt = t & VT_BTYPE;
3460 if (bt == VT_BYTE || bt == VT_BOOL)
3461 r |= VT_LVAL_BYTE;
3462 else if (bt == VT_SHORT)
3463 r |= VT_LVAL_SHORT;
3464 else
3465 return r;
3466 if (t & VT_UNSIGNED)
3467 r |= VT_LVAL_UNSIGNED;
3468 return r;
3471 /* indirection with full error checking and bound check */
3472 ST_FUNC void indir(void)
3474 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3475 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3476 return;
3477 expect("pointer");
3479 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3480 gv(RC_INT);
3481 vtop->type = *pointed_type(&vtop->type);
3482 /* Arrays and functions are never lvalues */
3483 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3484 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3485 vtop->r |= lvalue_type(vtop->type.t);
3486 /* if bound checking, the referenced pointer must be checked */
3487 #ifdef CONFIG_TCC_BCHECK
3488 if (tcc_state->do_bounds_check)
3489 vtop->r |= VT_MUSTBOUND;
3490 #endif
3494 /* pass a parameter to a function and do type checking and casting */
3495 static void gfunc_param_typed(Sym *func, Sym *arg)
3497 int func_type;
3498 CType type;
3500 func_type = func->c;
3501 if (func_type == FUNC_OLD ||
3502 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3503 /* default casting : only need to convert float to double */
3504 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3505 type.t = VT_DOUBLE;
3506 gen_cast(&type);
3507 } else if (vtop->type.t & VT_BITFIELD) {
3508 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3509 gen_cast(&type);
3511 } else if (arg == NULL) {
3512 tcc_error("too many arguments to function");
3513 } else {
3514 type = arg->type;
3515 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3516 gen_assign_cast(&type);
3520 /* parse an expression of the form '(type)' or '(expr)' and return its
3521 type */
3522 static void parse_expr_type(CType *type)
3524 int n;
3525 AttributeDef ad;
3527 skip('(');
3528 if (parse_btype(type, &ad)) {
3529 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3530 } else {
3531 expr_type(type);
3533 skip(')');
3536 static void parse_type(CType *type)
3538 AttributeDef ad;
3539 int n;
3541 if (!parse_btype(type, &ad)) {
3542 expect("type");
3544 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3547 static void vpush_tokc(int t)
3549 CType type;
3550 type.t = t;
3551 type.ref = 0;
3552 vsetc(&type, VT_CONST, &tokc);
3555 ST_FUNC void unary(void)
3557 int n, t, align, size, r, sizeof_caller;
3558 CType type;
3559 Sym *s;
3560 AttributeDef ad;
3561 static int in_sizeof = 0;
3563 sizeof_caller = in_sizeof;
3564 in_sizeof = 0;
3565 /* XXX: GCC 2.95.3 does not generate a table although it should be
3566 better here */
3567 tok_next:
3568 switch(tok) {
3569 case TOK_EXTENSION:
3570 next();
3571 goto tok_next;
3572 case TOK_CINT:
3573 case TOK_CCHAR:
3574 case TOK_LCHAR:
3575 vpushi(tokc.i);
3576 next();
3577 break;
3578 case TOK_CUINT:
3579 vpush_tokc(VT_INT | VT_UNSIGNED);
3580 next();
3581 break;
3582 case TOK_CLLONG:
3583 vpush_tokc(VT_LLONG);
3584 next();
3585 break;
3586 case TOK_CULLONG:
3587 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3588 next();
3589 break;
3590 case TOK_CFLOAT:
3591 vpush_tokc(VT_FLOAT);
3592 next();
3593 break;
3594 case TOK_CDOUBLE:
3595 vpush_tokc(VT_DOUBLE);
3596 next();
3597 break;
3598 case TOK_CLDOUBLE:
3599 vpush_tokc(VT_LDOUBLE);
3600 next();
3601 break;
3602 case TOK___FUNCTION__:
3603 if (!gnu_ext)
3604 goto tok_identifier;
3605 /* fall thru */
3606 case TOK___FUNC__:
3608 void *ptr;
3609 int len;
3610 /* special function name identifier */
3611 len = strlen(funcname) + 1;
3612 /* generate char[len] type */
3613 type.t = VT_BYTE;
3614 mk_pointer(&type);
3615 type.t |= VT_ARRAY;
3616 type.ref->c = len;
3617 vpush_ref(&type, data_section, data_section->data_offset, len);
3618 ptr = section_ptr_add(data_section, len);
3619 memcpy(ptr, funcname, len);
3620 next();
3622 break;
3623 case TOK_LSTR:
3624 #ifdef TCC_TARGET_PE
3625 t = VT_SHORT | VT_UNSIGNED;
3626 #else
3627 t = VT_INT;
3628 #endif
3629 goto str_init;
3630 case TOK_STR:
3631 /* string parsing */
3632 t = VT_BYTE;
3633 str_init:
3634 if (tcc_state->warn_write_strings)
3635 t |= VT_CONSTANT;
3636 type.t = t;
3637 mk_pointer(&type);
3638 type.t |= VT_ARRAY;
3639 memset(&ad, 0, sizeof(AttributeDef));
3640 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3641 break;
3642 case '(':
3643 next();
3644 /* cast ? */
3645 if (parse_btype(&type, &ad)) {
3646 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3647 skip(')');
3648 /* check ISOC99 compound literal */
3649 if (tok == '{') {
3650 /* data is allocated locally by default */
3651 if (global_expr)
3652 r = VT_CONST;
3653 else
3654 r = VT_LOCAL;
3655 /* all except arrays are lvalues */
3656 if (!(type.t & VT_ARRAY))
3657 r |= lvalue_type(type.t);
3658 memset(&ad, 0, sizeof(AttributeDef));
3659 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3660 } else {
3661 if (sizeof_caller) {
3662 vpush(&type);
3663 return;
3665 unary();
3666 gen_cast(&type);
3668 } else if (tok == '{') {
3669 /* save all registers */
3670 save_regs(0);
3671 /* statement expression : we do not accept break/continue
3672 inside as GCC does */
3673 block(NULL, NULL, NULL, NULL, 0, 1);
3674 skip(')');
3675 } else {
3676 gexpr();
3677 skip(')');
3679 break;
3680 case '*':
3681 next();
3682 unary();
3683 indir();
3684 break;
3685 case '&':
3686 next();
3687 unary();
3688 /* functions names must be treated as function pointers,
3689 except for unary '&' and sizeof. Since we consider that
3690 functions are not lvalues, we only have to handle it
3691 there and in function calls. */
3692 /* arrays can also be used although they are not lvalues */
3693 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3694 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3695 test_lvalue();
3696 mk_pointer(&vtop->type);
3697 gaddrof();
3698 break;
3699 case '!':
3700 next();
3701 unary();
3702 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3703 CType boolean;
3704 boolean.t = VT_BOOL;
3705 gen_cast(&boolean);
3706 vtop->c.i = !vtop->c.i;
3707 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3708 vtop->c.i = vtop->c.i ^ 1;
3709 else {
3710 save_regs(1);
3711 vseti(VT_JMP, gvtst(1, 0));
3713 break;
3714 case '~':
3715 next();
3716 unary();
3717 vpushi(-1);
3718 gen_op('^');
3719 break;
3720 case '+':
3721 next();
3722 unary();
3723 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3724 tcc_error("pointer not accepted for unary plus");
3725 /* In order to force cast, we add zero, except for floating point
3726 where we really need an noop (otherwise -0.0 will be transformed
3727 into +0.0). */
3728 if (!is_float(vtop->type.t)) {
3729 vpushi(0);
3730 gen_op('+');
3732 break;
3733 case TOK_SIZEOF:
3734 case TOK_ALIGNOF1:
3735 case TOK_ALIGNOF2:
3736 t = tok;
3737 next();
3738 in_sizeof++;
3739 unary_type(&type); // Perform a in_sizeof = 0;
3740 size = type_size(&type, &align);
3741 if (t == TOK_SIZEOF) {
3742 if (!(type.t & VT_VLA)) {
3743 if (size < 0)
3744 tcc_error("sizeof applied to an incomplete type");
3745 vpushs(size);
3746 } else {
3747 vla_runtime_type_size(&type, &align);
3749 } else {
3750 vpushs(align);
3752 vtop->type.t |= VT_UNSIGNED;
3753 break;
3755 case TOK_builtin_types_compatible_p:
3757 CType type1, type2;
3758 next();
3759 skip('(');
3760 parse_type(&type1);
3761 skip(',');
3762 parse_type(&type2);
3763 skip(')');
3764 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3765 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3766 vpushi(is_compatible_types(&type1, &type2));
3768 break;
3769 case TOK_builtin_constant_p:
3771 int saved_nocode_wanted, res;
3772 next();
3773 skip('(');
3774 saved_nocode_wanted = nocode_wanted;
3775 nocode_wanted = 1;
3776 gexpr();
3777 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3778 vpop();
3779 nocode_wanted = saved_nocode_wanted;
3780 skip(')');
3781 vpushi(res);
3783 break;
3784 case TOK_builtin_frame_address:
3786 int level;
3787 CType type;
3788 next();
3789 skip('(');
3790 if (tok != TOK_CINT || tokc.i < 0) {
3791 tcc_error("__builtin_frame_address only takes positive integers");
3793 level = tokc.i;
3794 next();
3795 skip(')');
3796 type.t = VT_VOID;
3797 mk_pointer(&type);
3798 vset(&type, VT_LOCAL, 0); /* local frame */
3799 while (level--) {
3800 mk_pointer(&vtop->type);
3801 indir(); /* -> parent frame */
3804 break;
3805 #ifdef TCC_TARGET_X86_64
3806 #ifdef TCC_TARGET_PE
3807 case TOK_builtin_va_start:
3809 next();
3810 skip('(');
3811 expr_eq();
3812 skip(',');
3813 expr_eq();
3814 skip(')');
3815 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3816 tcc_error("__builtin_va_start expects a local variable");
3817 vtop->r &= ~(VT_LVAL | VT_REF);
3818 vtop->type = char_pointer_type;
3819 vstore();
3821 break;
3822 #else
3823 case TOK_builtin_va_arg_types:
3825 CType type;
3826 next();
3827 skip('(');
3828 parse_type(&type);
3829 skip(')');
3830 vpushi(classify_x86_64_va_arg(&type));
3832 break;
3833 #endif
3834 #endif
3835 case TOK_INC:
3836 case TOK_DEC:
3837 t = tok;
3838 next();
3839 unary();
3840 inc(0, t);
3841 break;
3842 case '-':
3843 next();
3844 unary();
3845 t = vtop->type.t & VT_BTYPE;
3846 if (is_float(t)) {
3847 /* In IEEE negate(x) isn't subtract(0,x), but rather
3848 subtract(-0, x). */
3849 vpush(&vtop->type);
3850 if (t == VT_FLOAT)
3851 vtop->c.f = -0.0f;
3852 else if (t == VT_DOUBLE)
3853 vtop->c.d = -0.0;
3854 else
3855 vtop->c.ld = -0.0;
3856 } else
3857 vpushi(0);
3858 vswap();
3859 gen_op('-');
3860 break;
3861 case TOK_LAND:
3862 if (!gnu_ext)
3863 goto tok_identifier;
3864 next();
3865 /* allow to take the address of a label */
3866 if (tok < TOK_UIDENT)
3867 expect("label identifier");
3868 s = label_find(tok);
3869 if (!s) {
3870 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3871 } else {
3872 if (s->r == LABEL_DECLARED)
3873 s->r = LABEL_FORWARD;
3875 if (!s->type.t) {
3876 s->type.t = VT_VOID;
3877 mk_pointer(&s->type);
3878 s->type.t |= VT_STATIC;
3880 vpushsym(&s->type, s);
3881 next();
3882 break;
3884 // special qnan , snan and infinity values
3885 case TOK___NAN__:
3886 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3887 next();
3888 break;
3889 case TOK___SNAN__:
3890 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3891 next();
3892 break;
3893 case TOK___INF__:
3894 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3895 next();
3896 break;
3898 default:
3899 tok_identifier:
3900 t = tok;
3901 next();
3902 if (t < TOK_UIDENT)
3903 expect("identifier");
3904 s = sym_find(t);
3905 if (!s) {
3906 const char *name = get_tok_str(t, NULL);
3907 if (tok != '(')
3908 tcc_error("'%s' undeclared", name);
3909 /* for simple function calls, we tolerate undeclared
3910 external reference to int() function */
3911 if (tcc_state->warn_implicit_function_declaration
3912 #ifdef TCC_TARGET_PE
3913 /* people must be warned about using undeclared WINAPI functions
3914 (which usually start with uppercase letter) */
3915 || (name[0] >= 'A' && name[0] <= 'Z')
3916 #endif
3918 tcc_warning("implicit declaration of function '%s'", name);
3919 s = external_global_sym(t, &func_old_type, 0);
3921 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3922 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3923 /* if referencing an inline function, then we generate a
3924 symbol to it if not already done. It will have the
3925 effect to generate code for it at the end of the
3926 compilation unit. Inline function as always
3927 generated in the text section. */
3928 if (!s->c)
3929 put_extern_sym(s, text_section, 0, 0);
3930 r = VT_SYM | VT_CONST;
3931 } else {
3932 r = s->r;
3934 vset(&s->type, r, s->c);
3935 /* if forward reference, we must point to s */
3936 if (vtop->r & VT_SYM) {
3937 vtop->sym = s;
3938 vtop->c.ptr_offset = 0;
3940 break;
3943 /* post operations */
3944 while (1) {
3945 if (tok == TOK_INC || tok == TOK_DEC) {
3946 inc(1, tok);
3947 next();
3948 } else if (tok == '.' || tok == TOK_ARROW) {
3949 int qualifiers;
3950 /* field */
3951 if (tok == TOK_ARROW)
3952 indir();
3953 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3954 test_lvalue();
3955 gaddrof();
3956 next();
3957 /* expect pointer on structure */
3958 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3959 expect("struct or union");
3960 s = vtop->type.ref;
3961 /* find field */
3962 tok |= SYM_FIELD;
3963 while ((s = s->next) != NULL) {
3964 if (s->v == tok)
3965 break;
3967 if (!s)
3968 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3969 /* add field offset to pointer */
3970 vtop->type = char_pointer_type; /* change type to 'char *' */
3971 vpushi(s->c);
3972 gen_op('+');
3973 /* change type to field type, and set to lvalue */
3974 vtop->type = s->type;
3975 vtop->type.t |= qualifiers;
3976 /* an array is never an lvalue */
3977 if (!(vtop->type.t & VT_ARRAY)) {
3978 vtop->r |= lvalue_type(vtop->type.t);
3979 #ifdef CONFIG_TCC_BCHECK
3980 /* if bound checking, the referenced pointer must be checked */
3981 if (tcc_state->do_bounds_check)
3982 vtop->r |= VT_MUSTBOUND;
3983 #endif
3985 next();
3986 } else if (tok == '[') {
3987 next();
3988 gexpr();
3989 gen_op('+');
3990 indir();
3991 skip(']');
3992 } else if (tok == '(') {
3993 SValue ret;
3994 Sym *sa;
3995 int nb_args, ret_nregs, ret_align, variadic;
3997 /* function call */
3998 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3999 /* pointer test (no array accepted) */
4000 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4001 vtop->type = *pointed_type(&vtop->type);
4002 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4003 goto error_func;
4004 } else {
4005 error_func:
4006 expect("function pointer");
4008 } else {
4009 vtop->r &= ~VT_LVAL; /* no lvalue */
4011 /* get return type */
4012 s = vtop->type.ref;
4013 next();
4014 sa = s->next; /* first parameter */
4015 nb_args = 0;
4016 ret.r2 = VT_CONST;
4017 /* compute first implicit argument if a structure is returned */
4018 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4019 variadic = (s->c == FUNC_ELLIPSIS);
4020 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4021 &ret_align);
4022 if (!ret_nregs) {
4023 /* get some space for the returned structure */
4024 size = type_size(&s->type, &align);
4025 loc = (loc - size) & -align;
4026 ret.type = s->type;
4027 ret.r = VT_LOCAL | VT_LVAL;
4028 /* pass it as 'int' to avoid structure arg passing
4029 problems */
4030 vseti(VT_LOCAL, loc);
4031 ret.c = vtop->c;
4032 nb_args++;
4034 } else {
4035 ret_nregs = 1;
4036 ret.type = s->type;
4039 if (ret_nregs) {
4040 /* return in register */
4041 if (is_float(ret.type.t)) {
4042 ret.r = reg_fret(ret.type.t);
4043 #ifdef TCC_TARGET_X86_64
4044 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4045 ret.r2 = REG_QRET;
4046 #endif
4047 } else {
4048 #ifdef TCC_TARGET_X86_64
4049 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4050 #else
4051 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4052 #endif
4053 ret.r2 = REG_LRET;
4054 ret.r = REG_IRET;
4056 ret.c.i = 0;
4058 if (tok != ')') {
4059 for(;;) {
4060 expr_eq();
4061 gfunc_param_typed(s, sa);
4062 nb_args++;
4063 if (sa)
4064 sa = sa->next;
4065 if (tok == ')')
4066 break;
4067 skip(',');
4070 if (sa)
4071 tcc_error("too few arguments to function");
4072 skip(')');
4073 if (!nocode_wanted) {
4074 gfunc_call(nb_args);
4075 } else {
4076 vtop -= (nb_args + 1);
4079 /* return value */
4080 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4081 vsetc(&ret.type, r, &ret.c);
4082 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4085 /* handle packed struct return */
4086 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4087 int addr, offset;
4089 size = type_size(&s->type, &align);
4090 loc = (loc - size) & -align;
4091 addr = loc;
4092 offset = 0;
4093 for (;;) {
4094 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4095 vswap();
4096 vstore();
4097 vtop--;
4098 if (--ret_nregs == 0)
4099 break;
4100 /* XXX: compatible with arm only: ret_align == register_size */
4101 offset += ret_align;
4103 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4105 } else {
4106 break;
4111 ST_FUNC void expr_prod(void)
4113 int t;
4115 unary();
4116 while (tok == '*' || tok == '/' || tok == '%') {
4117 t = tok;
4118 next();
4119 unary();
4120 gen_op(t);
4124 ST_FUNC void expr_sum(void)
4126 int t;
4128 expr_prod();
4129 while (tok == '+' || tok == '-') {
4130 t = tok;
4131 next();
4132 expr_prod();
4133 gen_op(t);
4137 static void expr_shift(void)
4139 int t;
4141 expr_sum();
4142 while (tok == TOK_SHL || tok == TOK_SAR) {
4143 t = tok;
4144 next();
4145 expr_sum();
4146 gen_op(t);
4150 static void expr_cmp(void)
4152 int t;
4154 expr_shift();
4155 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4156 tok == TOK_ULT || tok == TOK_UGE) {
4157 t = tok;
4158 next();
4159 expr_shift();
4160 gen_op(t);
4164 static void expr_cmpeq(void)
4166 int t;
4168 expr_cmp();
4169 while (tok == TOK_EQ || tok == TOK_NE) {
4170 t = tok;
4171 next();
4172 expr_cmp();
4173 gen_op(t);
4177 static void expr_and(void)
4179 expr_cmpeq();
4180 while (tok == '&') {
4181 next();
4182 expr_cmpeq();
4183 gen_op('&');
4187 static void expr_xor(void)
4189 expr_and();
4190 while (tok == '^') {
4191 next();
4192 expr_and();
4193 gen_op('^');
4197 static void expr_or(void)
4199 expr_xor();
4200 while (tok == '|') {
4201 next();
4202 expr_xor();
4203 gen_op('|');
4207 /* XXX: fix this mess */
4208 static void expr_land_const(void)
4210 expr_or();
4211 while (tok == TOK_LAND) {
4212 next();
4213 expr_or();
4214 gen_op(TOK_LAND);
4218 /* XXX: fix this mess */
4219 static void expr_lor_const(void)
4221 expr_land_const();
4222 while (tok == TOK_LOR) {
4223 next();
4224 expr_land_const();
4225 gen_op(TOK_LOR);
4229 /* only used if non constant */
4230 static void expr_land(void)
4232 int t;
4234 expr_or();
4235 if (tok == TOK_LAND) {
4236 t = 0;
4237 save_regs(1);
4238 for(;;) {
4239 t = gvtst(1, t);
4240 if (tok != TOK_LAND) {
4241 vseti(VT_JMPI, t);
4242 break;
4244 next();
4245 expr_or();
4250 static void expr_lor(void)
4252 int t;
4254 expr_land();
4255 if (tok == TOK_LOR) {
4256 t = 0;
4257 save_regs(1);
4258 for(;;) {
4259 t = gvtst(0, t);
4260 if (tok != TOK_LOR) {
4261 vseti(VT_JMP, t);
4262 break;
4264 next();
4265 expr_land();
4270 /* XXX: better constant handling */
4271 static void expr_cond(void)
4273 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4274 SValue sv;
4275 CType type, type1, type2;
4277 if (const_wanted) {
4278 expr_lor_const();
4279 if (tok == '?') {
4280 CType boolean;
4281 int c;
4282 boolean.t = VT_BOOL;
4283 vdup();
4284 gen_cast(&boolean);
4285 c = vtop->c.i;
4286 vpop();
4287 next();
4288 if (tok != ':' || !gnu_ext) {
4289 vpop();
4290 gexpr();
4292 if (!c)
4293 vpop();
4294 skip(':');
4295 expr_cond();
4296 if (c)
4297 vpop();
4299 } else {
4300 expr_lor();
4301 if (tok == '?') {
4302 next();
4303 if (vtop != vstack) {
4304 /* needed to avoid having different registers saved in
4305 each branch */
4306 if (is_float(vtop->type.t)) {
4307 rc = RC_FLOAT;
4308 #ifdef TCC_TARGET_X86_64
4309 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4310 rc = RC_ST0;
4312 #endif
4314 else
4315 rc = RC_INT;
4316 gv(rc);
4317 save_regs(1);
4319 if (tok == ':' && gnu_ext) {
4320 gv_dup();
4321 tt = gvtst(1, 0);
4322 } else {
4323 tt = gvtst(1, 0);
4324 gexpr();
4326 type1 = vtop->type;
4327 sv = *vtop; /* save value to handle it later */
4328 vtop--; /* no vpop so that FP stack is not flushed */
4329 skip(':');
4330 u = gjmp(0);
4331 gsym(tt);
4332 expr_cond();
4333 type2 = vtop->type;
4335 t1 = type1.t;
4336 bt1 = t1 & VT_BTYPE;
4337 t2 = type2.t;
4338 bt2 = t2 & VT_BTYPE;
4339 /* cast operands to correct type according to ISOC rules */
4340 if (is_float(bt1) || is_float(bt2)) {
4341 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4342 type.t = VT_LDOUBLE;
4343 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4344 type.t = VT_DOUBLE;
4345 } else {
4346 type.t = VT_FLOAT;
4348 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4349 /* cast to biggest op */
4350 type.t = VT_LLONG;
4351 /* convert to unsigned if it does not fit in a long long */
4352 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4353 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4354 type.t |= VT_UNSIGNED;
4355 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4356 /* If one is a null ptr constant the result type
4357 is the other. */
4358 if (is_null_pointer (vtop))
4359 type = type1;
4360 else if (is_null_pointer (&sv))
4361 type = type2;
4362 /* XXX: test pointer compatibility, C99 has more elaborate
4363 rules here. */
4364 else
4365 type = type1;
4366 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4367 /* XXX: test function pointer compatibility */
4368 type = bt1 == VT_FUNC ? type1 : type2;
4369 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4370 /* XXX: test structure compatibility */
4371 type = bt1 == VT_STRUCT ? type1 : type2;
4372 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4373 /* NOTE: as an extension, we accept void on only one side */
4374 type.t = VT_VOID;
4375 } else {
4376 /* integer operations */
4377 type.t = VT_INT;
4378 /* convert to unsigned if it does not fit in an integer */
4379 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4380 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4381 type.t |= VT_UNSIGNED;
4384 /* now we convert second operand */
4385 gen_cast(&type);
4386 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4387 gaddrof();
4388 rc = RC_INT;
4389 if (is_float(type.t)) {
4390 rc = RC_FLOAT;
4391 #ifdef TCC_TARGET_X86_64
4392 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4393 rc = RC_ST0;
4395 #endif
4396 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4397 /* for long longs, we use fixed registers to avoid having
4398 to handle a complicated move */
4399 rc = RC_IRET;
4402 r2 = gv(rc);
4403 /* this is horrible, but we must also convert first
4404 operand */
4405 tt = gjmp(0);
4406 gsym(u);
4407 /* put again first value and cast it */
4408 *vtop = sv;
4409 gen_cast(&type);
4410 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4411 gaddrof();
4412 r1 = gv(rc);
4413 move_reg(r2, r1, type.t);
4414 vtop->r = r2;
4415 gsym(tt);
4420 static void expr_eq(void)
4422 int t;
4424 expr_cond();
4425 if (tok == '=' ||
4426 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4427 tok == TOK_A_XOR || tok == TOK_A_OR ||
4428 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4429 test_lvalue();
4430 t = tok;
4431 next();
4432 if (t == '=') {
4433 expr_eq();
4434 } else {
4435 vdup();
4436 expr_eq();
4437 gen_op(t & 0x7f);
4439 vstore();
4443 ST_FUNC void gexpr(void)
4445 while (1) {
4446 expr_eq();
4447 if (tok != ',')
4448 break;
4449 vpop();
4450 next();
4454 /* parse an expression and return its type without any side effect. */
4455 static void expr_type(CType *type)
4457 int saved_nocode_wanted;
4459 saved_nocode_wanted = nocode_wanted;
4460 nocode_wanted = 1;
4461 gexpr();
4462 *type = vtop->type;
4463 vpop();
4464 nocode_wanted = saved_nocode_wanted;
4467 /* parse a unary expression and return its type without any side
4468 effect. */
4469 static void unary_type(CType *type)
4471 int a;
4473 a = nocode_wanted;
4474 nocode_wanted = 1;
4475 unary();
4476 *type = vtop->type;
4477 vpop();
4478 nocode_wanted = a;
4481 /* parse a constant expression and return value in vtop. */
4482 static void expr_const1(void)
4484 int a;
4485 a = const_wanted;
4486 const_wanted = 1;
4487 expr_cond();
4488 const_wanted = a;
4491 /* parse an integer constant and return its value. */
4492 ST_FUNC int expr_const(void)
4494 int c;
4495 expr_const1();
4496 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4497 expect("constant expression");
4498 c = vtop->c.i;
4499 vpop();
4500 return c;
4503 /* return the label token if current token is a label, otherwise
4504 return zero */
4505 static int is_label(void)
4507 int last_tok;
4509 /* fast test first */
4510 if (tok < TOK_UIDENT)
4511 return 0;
4512 /* no need to save tokc because tok is an identifier */
4513 last_tok = tok;
4514 next();
4515 if (tok == ':') {
4516 next();
4517 return last_tok;
4518 } else {
4519 unget_tok(last_tok);
4520 return 0;
4524 static void label_or_decl(int l)
4526 int last_tok;
4528 /* fast test first */
4529 if (tok >= TOK_UIDENT)
4531 /* no need to save tokc because tok is an identifier */
4532 last_tok = tok;
4533 next();
4534 if (tok == ':') {
4535 unget_tok(last_tok);
4536 return;
4538 unget_tok(last_tok);
4540 decl(l);
4543 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4544 int case_reg, int is_expr)
4546 int a, b, c, d;
4547 Sym *s, *frame_bottom;
4549 /* generate line number info */
4550 if (tcc_state->do_debug &&
4551 (last_line_num != file->line_num || last_ind != ind)) {
4552 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4553 last_ind = ind;
4554 last_line_num = file->line_num;
4557 if (is_expr) {
4558 /* default return value is (void) */
4559 vpushi(0);
4560 vtop->type.t = VT_VOID;
4563 if (tok == TOK_IF) {
4564 /* if test */
4565 next();
4566 skip('(');
4567 gexpr();
4568 skip(')');
4569 a = gvtst(1, 0);
4570 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4571 c = tok;
4572 if (c == TOK_ELSE) {
4573 next();
4574 d = gjmp(0);
4575 gsym(a);
4576 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4577 gsym(d); /* patch else jmp */
4578 } else
4579 gsym(a);
4580 } else if (tok == TOK_WHILE) {
4581 next();
4582 d = ind;
4583 skip('(');
4584 gexpr();
4585 skip(')');
4586 a = gvtst(1, 0);
4587 b = 0;
4588 block(&a, &b, case_sym, def_sym, case_reg, 0);
4589 gjmp_addr(d);
4590 gsym(a);
4591 gsym_addr(b, d);
4592 } else if (tok == '{') {
4593 Sym *llabel;
4594 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4596 next();
4597 /* record local declaration stack position */
4598 s = local_stack;
4599 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4600 frame_bottom->next = scope_stack_bottom;
4601 scope_stack_bottom = frame_bottom;
4602 llabel = local_label_stack;
4604 /* save VLA state */
4605 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4606 if (saved_vla_sp_loc != &vla_sp_root_loc)
4607 vla_sp_loc = &block_vla_sp_loc;
4609 saved_vla_flags = vla_flags;
4610 vla_flags |= VLA_NEED_NEW_FRAME;
4612 /* handle local labels declarations */
4613 if (tok == TOK_LABEL) {
4614 next();
4615 for(;;) {
4616 if (tok < TOK_UIDENT)
4617 expect("label identifier");
4618 label_push(&local_label_stack, tok, LABEL_DECLARED);
4619 next();
4620 if (tok == ',') {
4621 next();
4622 } else {
4623 skip(';');
4624 break;
4628 while (tok != '}') {
4629 label_or_decl(VT_LOCAL);
4630 if (tok != '}') {
4631 if (is_expr)
4632 vpop();
4633 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4636 /* pop locally defined labels */
4637 label_pop(&local_label_stack, llabel);
4638 if(is_expr) {
4639 /* XXX: this solution makes only valgrind happy...
4640 triggered by gcc.c-torture/execute/20000917-1.c */
4641 Sym *p;
4642 switch(vtop->type.t & VT_BTYPE) {
4643 case VT_PTR:
4644 case VT_STRUCT:
4645 case VT_ENUM:
4646 case VT_FUNC:
4647 for(p=vtop->type.ref;p;p=p->prev)
4648 if(p->prev==s)
4649 tcc_error("unsupported expression type");
4652 /* pop locally defined symbols */
4653 scope_stack_bottom = scope_stack_bottom->next;
4654 sym_pop(&local_stack, s);
4656 /* Pop VLA frames and restore stack pointer if required */
4657 if (saved_vla_sp_loc != &vla_sp_root_loc)
4658 *saved_vla_sp_loc = block_vla_sp_loc;
4659 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4660 vla_sp_loc = saved_vla_sp_loc;
4661 gen_vla_sp_restore(*vla_sp_loc);
4663 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4665 next();
4666 } else if (tok == TOK_RETURN) {
4667 next();
4668 if (tok != ';') {
4669 gexpr();
4670 gen_assign_cast(&func_vt);
4671 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4672 CType type, ret_type;
4673 int ret_align, ret_nregs;
4674 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4675 &ret_align);
4676 if (0 == ret_nregs) {
4677 /* if returning structure, must copy it to implicit
4678 first pointer arg location */
4679 type = func_vt;
4680 mk_pointer(&type);
4681 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4682 indir();
4683 vswap();
4684 /* copy structure value to pointer */
4685 vstore();
4686 } else {
4687 /* returning structure packed into registers */
4688 int r, size, addr, align;
4689 size = type_size(&func_vt,&align);
4690 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4691 && (align & (ret_align-1))) {
4692 loc = (loc - size) & -align;
4693 addr = loc;
4694 type = func_vt;
4695 vset(&type, VT_LOCAL | VT_LVAL, addr);
4696 vswap();
4697 vstore();
4698 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4700 vtop->type = ret_type;
4701 if (is_float(ret_type.t))
4702 r = rc_fret(ret_type.t);
4703 else
4704 r = RC_IRET;
4706 for (;;) {
4707 gv(r);
4708 if (--ret_nregs == 0)
4709 break;
4710 /* We assume that when a structure is returned in multiple
4711 registers, their classes are consecutive values of the
4712 suite s(n) = 2^n */
4713 r <<= 1;
4714 /* XXX: compatible with arm only: ret_align == register_size */
4715 vtop->c.i += ret_align;
4716 vtop->r = VT_LOCAL | VT_LVAL;
4719 } else if (is_float(func_vt.t)) {
4720 gv(rc_fret(func_vt.t));
4721 } else {
4722 gv(RC_IRET);
4724 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4726 skip(';');
4727 rsym = gjmp(rsym); /* jmp */
4728 } else if (tok == TOK_BREAK) {
4729 /* compute jump */
4730 if (!bsym)
4731 tcc_error("cannot break");
4732 *bsym = gjmp(*bsym);
4733 next();
4734 skip(';');
4735 } else if (tok == TOK_CONTINUE) {
4736 /* compute jump */
4737 if (!csym)
4738 tcc_error("cannot continue");
4739 *csym = gjmp(*csym);
4740 next();
4741 skip(';');
4742 } else if (tok == TOK_FOR) {
4743 int e;
4744 next();
4745 skip('(');
4746 s = local_stack;
4747 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4748 frame_bottom->next = scope_stack_bottom;
4749 scope_stack_bottom = frame_bottom;
4750 if (tok != ';') {
4751 /* c99 for-loop init decl? */
4752 if (!decl0(VT_LOCAL, 1)) {
4753 /* no, regular for-loop init expr */
4754 gexpr();
4755 vpop();
4758 skip(';');
4759 d = ind;
4760 c = ind;
4761 a = 0;
4762 b = 0;
4763 if (tok != ';') {
4764 gexpr();
4765 a = gvtst(1, 0);
4767 skip(';');
4768 if (tok != ')') {
4769 e = gjmp(0);
4770 c = ind;
4771 gexpr();
4772 vpop();
4773 gjmp_addr(d);
4774 gsym(e);
4776 skip(')');
4777 block(&a, &b, case_sym, def_sym, case_reg, 0);
4778 gjmp_addr(c);
4779 gsym(a);
4780 gsym_addr(b, c);
4781 scope_stack_bottom = scope_stack_bottom->next;
4782 sym_pop(&local_stack, s);
4783 } else
4784 if (tok == TOK_DO) {
4785 next();
4786 a = 0;
4787 b = 0;
4788 d = ind;
4789 block(&a, &b, case_sym, def_sym, case_reg, 0);
4790 skip(TOK_WHILE);
4791 skip('(');
4792 gsym(b);
4793 gexpr();
4794 c = gvtst(0, 0);
4795 gsym_addr(c, d);
4796 skip(')');
4797 gsym(a);
4798 skip(';');
4799 } else
4800 if (tok == TOK_SWITCH) {
4801 next();
4802 skip('(');
4803 gexpr();
4804 /* XXX: other types than integer */
4805 case_reg = gv(RC_INT);
4806 vpop();
4807 skip(')');
4808 a = 0;
4809 b = gjmp(0); /* jump to first case */
4810 c = 0;
4811 block(&a, csym, &b, &c, case_reg, 0);
4812 /* if no default, jmp after switch */
4813 if (c == 0)
4814 c = ind;
4815 /* default label */
4816 gsym_addr(b, c);
4817 /* break label */
4818 gsym(a);
4819 } else
4820 if (tok == TOK_CASE) {
4821 int v1, v2;
4822 if (!case_sym)
4823 expect("switch");
4824 next();
4825 v1 = expr_const();
4826 v2 = v1;
4827 if (gnu_ext && tok == TOK_DOTS) {
4828 next();
4829 v2 = expr_const();
4830 if (v2 < v1)
4831 tcc_warning("empty case range");
4833 /* since a case is like a label, we must skip it with a jmp */
4834 b = gjmp(0);
4835 gsym(*case_sym);
4836 vseti(case_reg, 0);
4837 vpushi(v1);
4838 if (v1 == v2) {
4839 gen_op(TOK_EQ);
4840 *case_sym = gtst(1, 0);
4841 } else {
4842 gen_op(TOK_GE);
4843 *case_sym = gtst(1, 0);
4844 vseti(case_reg, 0);
4845 vpushi(v2);
4846 gen_op(TOK_LE);
4847 *case_sym = gtst(1, *case_sym);
4849 gsym(b);
4850 skip(':');
4851 is_expr = 0;
4852 goto block_after_label;
4853 } else
4854 if (tok == TOK_DEFAULT) {
4855 next();
4856 skip(':');
4857 if (!def_sym)
4858 expect("switch");
4859 if (*def_sym)
4860 tcc_error("too many 'default'");
4861 *def_sym = ind;
4862 is_expr = 0;
4863 goto block_after_label;
4864 } else
4865 if (tok == TOK_GOTO) {
4866 next();
4867 if (tok == '*' && gnu_ext) {
4868 /* computed goto */
4869 next();
4870 gexpr();
4871 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4872 expect("pointer");
4873 ggoto();
4874 } else if (tok >= TOK_UIDENT) {
4875 s = label_find(tok);
4876 /* put forward definition if needed */
4877 if (!s) {
4878 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4879 } else {
4880 if (s->r == LABEL_DECLARED)
4881 s->r = LABEL_FORWARD;
4883 /* label already defined */
4884 if (vla_flags & VLA_IN_SCOPE) {
4885 /* If VLAs are in use, save the current stack pointer and
4886 reset the stack pointer to what it was at function entry
4887 (label will restore stack pointer in inner scopes) */
4888 vla_sp_save();
4889 gen_vla_sp_restore(vla_sp_root_loc);
4891 if (s->r & LABEL_FORWARD)
4892 s->jnext = gjmp(s->jnext);
4893 else
4894 gjmp_addr(s->jnext);
4895 next();
4896 } else {
4897 expect("label identifier");
4899 skip(';');
4900 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4901 asm_instr();
4902 } else {
4903 b = is_label();
4904 if (b) {
4905 /* label case */
4906 if (vla_flags & VLA_IN_SCOPE) {
4907 /* save/restore stack pointer across label
4908 this is a no-op when combined with the load immediately
4909 after the label unless we arrive via goto */
4910 vla_sp_save();
4912 s = label_find(b);
4913 if (s) {
4914 if (s->r == LABEL_DEFINED)
4915 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4916 gsym(s->jnext);
4917 s->r = LABEL_DEFINED;
4918 } else {
4919 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4921 s->jnext = ind;
4922 if (vla_flags & VLA_IN_SCOPE) {
4923 gen_vla_sp_restore(*vla_sp_loc);
4924 vla_flags |= VLA_NEED_NEW_FRAME;
4926 /* we accept this, but it is a mistake */
4927 block_after_label:
4928 if (tok == '}') {
4929 tcc_warning("deprecated use of label at end of compound statement");
4930 } else {
4931 if (is_expr)
4932 vpop();
4933 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4935 } else {
4936 /* expression case */
4937 if (tok != ';') {
4938 if (is_expr) {
4939 vpop();
4940 gexpr();
4941 } else {
4942 gexpr();
4943 vpop();
4946 skip(';');
4951 /* t is the array or struct type. c is the array or struct
4952 address. cur_index/cur_field is the pointer to the current
4953 value. 'size_only' is true if only size info is needed (only used
4954 in arrays) */
4955 static void decl_designator(CType *type, Section *sec, unsigned long c,
4956 int *cur_index, Sym **cur_field,
4957 int size_only)
4959 Sym *s, *f;
4960 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4961 CType type1;
4963 notfirst = 0;
4964 elem_size = 0;
4965 nb_elems = 1;
4966 if (gnu_ext && (l = is_label()) != 0)
4967 goto struct_field;
4968 while (tok == '[' || tok == '.') {
4969 if (tok == '[') {
4970 if (!(type->t & VT_ARRAY))
4971 expect("array type");
4972 s = type->ref;
4973 next();
4974 index = expr_const();
4975 if (index < 0 || (s->c >= 0 && index >= s->c))
4976 expect("invalid index");
4977 if (tok == TOK_DOTS && gnu_ext) {
4978 next();
4979 index_last = expr_const();
4980 if (index_last < 0 ||
4981 (s->c >= 0 && index_last >= s->c) ||
4982 index_last < index)
4983 expect("invalid index");
4984 } else {
4985 index_last = index;
4987 skip(']');
4988 if (!notfirst)
4989 *cur_index = index_last;
4990 type = pointed_type(type);
4991 elem_size = type_size(type, &align);
4992 c += index * elem_size;
4993 /* NOTE: we only support ranges for last designator */
4994 nb_elems = index_last - index + 1;
4995 if (nb_elems != 1) {
4996 notfirst = 1;
4997 break;
4999 } else {
5000 next();
5001 l = tok;
5002 next();
5003 struct_field:
5004 if ((type->t & VT_BTYPE) != VT_STRUCT)
5005 expect("struct/union type");
5006 s = type->ref;
5007 l |= SYM_FIELD;
5008 f = s->next;
5009 while (f) {
5010 if (f->v == l)
5011 break;
5012 f = f->next;
5014 if (!f)
5015 expect("field");
5016 if (!notfirst)
5017 *cur_field = f;
5018 /* XXX: fix this mess by using explicit storage field */
5019 type1 = f->type;
5020 type1.t |= (type->t & ~VT_TYPE);
5021 type = &type1;
5022 c += f->c;
5024 notfirst = 1;
5026 if (notfirst) {
5027 if (tok == '=') {
5028 next();
5029 } else {
5030 if (!gnu_ext)
5031 expect("=");
5033 } else {
5034 if (type->t & VT_ARRAY) {
5035 index = *cur_index;
5036 type = pointed_type(type);
5037 c += index * type_size(type, &align);
5038 } else {
5039 f = *cur_field;
5040 if (!f)
5041 tcc_error("too many field init");
5042 /* XXX: fix this mess by using explicit storage field */
5043 type1 = f->type;
5044 type1.t |= (type->t & ~VT_TYPE);
5045 type = &type1;
5046 c += f->c;
5049 decl_initializer(type, sec, c, 0, size_only);
5051 /* XXX: make it more general */
5052 if (!size_only && nb_elems > 1) {
5053 unsigned long c_end;
5054 uint8_t *src, *dst;
5055 int i;
5057 if (!sec)
5058 tcc_error("range init not supported yet for dynamic storage");
5059 c_end = c + nb_elems * elem_size;
5060 if (c_end > sec->data_allocated)
5061 section_realloc(sec, c_end);
5062 src = sec->data + c;
5063 dst = src;
5064 for(i = 1; i < nb_elems; i++) {
5065 dst += elem_size;
5066 memcpy(dst, src, elem_size);
5071 #define EXPR_VAL 0
5072 #define EXPR_CONST 1
5073 #define EXPR_ANY 2
5075 /* store a value or an expression directly in global data or in local array */
5076 static void init_putv(CType *type, Section *sec, unsigned long c,
5077 int v, int expr_type)
5079 int saved_global_expr, bt, bit_pos, bit_size;
5080 void *ptr;
5081 unsigned long long bit_mask;
5082 CType dtype;
5084 switch(expr_type) {
5085 case EXPR_VAL:
5086 vpushi(v);
5087 break;
5088 case EXPR_CONST:
5089 /* compound literals must be allocated globally in this case */
5090 saved_global_expr = global_expr;
5091 global_expr = 1;
5092 expr_const1();
5093 global_expr = saved_global_expr;
5094 /* NOTE: symbols are accepted */
5095 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5096 tcc_error("initializer element is not constant");
5097 break;
5098 case EXPR_ANY:
5099 expr_eq();
5100 break;
5103 dtype = *type;
5104 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5106 if (sec) {
5107 /* XXX: not portable */
5108 /* XXX: generate error if incorrect relocation */
5109 gen_assign_cast(&dtype);
5110 bt = type->t & VT_BTYPE;
5111 /* we'll write at most 12 bytes */
5112 if (c + 12 > sec->data_allocated) {
5113 section_realloc(sec, c + 12);
5115 ptr = sec->data + c;
5116 /* XXX: make code faster ? */
5117 if (!(type->t & VT_BITFIELD)) {
5118 bit_pos = 0;
5119 bit_size = 32;
5120 bit_mask = -1LL;
5121 } else {
5122 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5123 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5124 bit_mask = (1LL << bit_size) - 1;
5126 if ((vtop->r & VT_SYM) &&
5127 (bt == VT_BYTE ||
5128 bt == VT_SHORT ||
5129 bt == VT_DOUBLE ||
5130 bt == VT_LDOUBLE ||
5131 bt == VT_LLONG ||
5132 (bt == VT_INT && bit_size != 32)))
5133 tcc_error("initializer element is not computable at load time");
5134 switch(bt) {
5135 case VT_BOOL:
5136 vtop->c.i = (vtop->c.i != 0);
5137 case VT_BYTE:
5138 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5139 break;
5140 case VT_SHORT:
5141 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5142 break;
5143 case VT_DOUBLE:
5144 *(double *)ptr = vtop->c.d;
5145 break;
5146 case VT_LDOUBLE:
5147 *(long double *)ptr = vtop->c.ld;
5148 break;
5149 case VT_LLONG:
5150 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5151 break;
5152 case VT_PTR:
5153 if (vtop->r & VT_SYM) {
5154 greloc(sec, vtop->sym, c, R_DATA_PTR);
5156 *(addr_t *)ptr |= (vtop->c.ptr_offset & bit_mask) << bit_pos;
5157 break;
5158 default:
5159 if (vtop->r & VT_SYM) {
5160 greloc(sec, vtop->sym, c, R_DATA_PTR);
5162 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5163 break;
5165 vtop--;
5166 } else {
5167 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5168 vswap();
5169 vstore();
5170 vpop();
5174 /* put zeros for variable based init */
5175 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5177 if (sec) {
5178 /* nothing to do because globals are already set to zero */
5179 } else {
5180 vpush_global_sym(&func_old_type, TOK_memset);
5181 vseti(VT_LOCAL, c);
5182 #ifdef TCC_TARGET_ARM
5183 vpushs(size);
5184 vpushi(0);
5185 #else
5186 vpushi(0);
5187 vpushs(size);
5188 #endif
5189 gfunc_call(3);
5193 /* 't' contains the type and storage info. 'c' is the offset of the
5194 object in section 'sec'. If 'sec' is NULL, it means stack based
5195 allocation. 'first' is true if array '{' must be read (multi
5196 dimension implicit array init handling). 'size_only' is true if
5197 size only evaluation is wanted (only for arrays). */
5198 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5199 int first, int size_only)
5201 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5202 int size1, align1, expr_type;
5203 Sym *s, *f;
5204 CType *t1;
5206 if (type->t & VT_VLA) {
5207 int a;
5209 /* save current stack pointer */
5210 if (vla_flags & VLA_NEED_NEW_FRAME) {
5211 vla_sp_save();
5212 vla_flags = VLA_IN_SCOPE;
5213 vla_sp_loc = &vla_sp_loc_tmp;
5216 vla_runtime_type_size(type, &a);
5217 gen_vla_alloc(type, a);
5218 vset(type, VT_LOCAL|VT_LVAL, c);
5219 vswap();
5220 vstore();
5221 vpop();
5222 } else if (type->t & VT_ARRAY) {
5223 s = type->ref;
5224 n = s->c;
5225 array_length = 0;
5226 t1 = pointed_type(type);
5227 size1 = type_size(t1, &align1);
5229 no_oblock = 1;
5230 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5231 tok == '{') {
5232 if (tok != '{')
5233 tcc_error("character array initializer must be a literal,"
5234 " optionally enclosed in braces");
5235 skip('{');
5236 no_oblock = 0;
5239 /* only parse strings here if correct type (otherwise: handle
5240 them as ((w)char *) expressions */
5241 if ((tok == TOK_LSTR &&
5242 #ifdef TCC_TARGET_PE
5243 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5244 #else
5245 (t1->t & VT_BTYPE) == VT_INT
5246 #endif
5247 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5248 while (tok == TOK_STR || tok == TOK_LSTR) {
5249 int cstr_len, ch;
5250 CString *cstr;
5252 cstr = tokc.cstr;
5253 /* compute maximum number of chars wanted */
5254 if (tok == TOK_STR)
5255 cstr_len = cstr->size;
5256 else
5257 cstr_len = cstr->size / sizeof(nwchar_t);
5258 cstr_len--;
5259 nb = cstr_len;
5260 if (n >= 0 && nb > (n - array_length))
5261 nb = n - array_length;
5262 if (!size_only) {
5263 if (cstr_len > nb)
5264 tcc_warning("initializer-string for array is too long");
5265 /* in order to go faster for common case (char
5266 string in global variable, we handle it
5267 specifically */
5268 if (sec && tok == TOK_STR && size1 == 1) {
5269 memcpy(sec->data + c + array_length, cstr->data, nb);
5270 } else {
5271 for(i=0;i<nb;i++) {
5272 if (tok == TOK_STR)
5273 ch = ((unsigned char *)cstr->data)[i];
5274 else
5275 ch = ((nwchar_t *)cstr->data)[i];
5276 init_putv(t1, sec, c + (array_length + i) * size1,
5277 ch, EXPR_VAL);
5281 array_length += nb;
5282 next();
5284 /* only add trailing zero if enough storage (no
5285 warning in this case since it is standard) */
5286 if (n < 0 || array_length < n) {
5287 if (!size_only) {
5288 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5290 array_length++;
5292 } else {
5293 index = 0;
5294 while (tok != '}') {
5295 decl_designator(type, sec, c, &index, NULL, size_only);
5296 if (n >= 0 && index >= n)
5297 tcc_error("index too large");
5298 /* must put zero in holes (note that doing it that way
5299 ensures that it even works with designators) */
5300 if (!size_only && array_length < index) {
5301 init_putz(t1, sec, c + array_length * size1,
5302 (index - array_length) * size1);
5304 index++;
5305 if (index > array_length)
5306 array_length = index;
5307 /* special test for multi dimensional arrays (may not
5308 be strictly correct if designators are used at the
5309 same time) */
5310 if (index >= n && no_oblock)
5311 break;
5312 if (tok == '}')
5313 break;
5314 skip(',');
5317 if (!no_oblock)
5318 skip('}');
5319 /* put zeros at the end */
5320 if (!size_only && n >= 0 && array_length < n) {
5321 init_putz(t1, sec, c + array_length * size1,
5322 (n - array_length) * size1);
5324 /* patch type size if needed */
5325 if (n < 0)
5326 s->c = array_length;
5327 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5328 (sec || !first || tok == '{')) {
5329 int par_count;
5331 /* NOTE: the previous test is a specific case for automatic
5332 struct/union init */
5333 /* XXX: union needs only one init */
5335 /* XXX: this test is incorrect for local initializers
5336 beginning with ( without {. It would be much more difficult
5337 to do it correctly (ideally, the expression parser should
5338 be used in all cases) */
5339 par_count = 0;
5340 if (tok == '(') {
5341 AttributeDef ad1;
5342 CType type1;
5343 next();
5344 while (tok == '(') {
5345 par_count++;
5346 next();
5348 if (!parse_btype(&type1, &ad1))
5349 expect("cast");
5350 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5351 #if 0
5352 if (!is_assignable_types(type, &type1))
5353 tcc_error("invalid type for cast");
5354 #endif
5355 skip(')');
5357 no_oblock = 1;
5358 if (first || tok == '{') {
5359 skip('{');
5360 no_oblock = 0;
5362 s = type->ref;
5363 f = s->next;
5364 array_length = 0;
5365 index = 0;
5366 n = s->c;
5367 while (tok != '}') {
5368 decl_designator(type, sec, c, NULL, &f, size_only);
5369 index = f->c;
5370 if (!size_only && array_length < index) {
5371 init_putz(type, sec, c + array_length,
5372 index - array_length);
5374 index = index + type_size(&f->type, &align1);
5375 if (index > array_length)
5376 array_length = index;
5378 /* gr: skip fields from same union - ugly. */
5379 while (f->next) {
5380 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5381 /* test for same offset */
5382 if (f->next->c != f->c)
5383 break;
5384 /* if yes, test for bitfield shift */
5385 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5386 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5387 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5388 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5389 if (bit_pos_1 != bit_pos_2)
5390 break;
5392 f = f->next;
5395 f = f->next;
5396 if (no_oblock && f == NULL)
5397 break;
5398 if (tok == '}')
5399 break;
5400 skip(',');
5402 /* put zeros at the end */
5403 if (!size_only && array_length < n) {
5404 init_putz(type, sec, c + array_length,
5405 n - array_length);
5407 if (!no_oblock)
5408 skip('}');
5409 while (par_count) {
5410 skip(')');
5411 par_count--;
5413 } else if (tok == '{') {
5414 next();
5415 decl_initializer(type, sec, c, first, size_only);
5416 skip('}');
5417 } else if (size_only) {
5418 /* just skip expression */
5419 parlevel = parlevel1 = 0;
5420 while ((parlevel > 0 || parlevel1 > 0 ||
5421 (tok != '}' && tok != ',')) && tok != -1) {
5422 if (tok == '(')
5423 parlevel++;
5424 else if (tok == ')')
5425 parlevel--;
5426 else if (tok == '{')
5427 parlevel1++;
5428 else if (tok == '}')
5429 parlevel1--;
5430 next();
5432 } else {
5433 /* currently, we always use constant expression for globals
5434 (may change for scripting case) */
5435 expr_type = EXPR_CONST;
5436 if (!sec)
5437 expr_type = EXPR_ANY;
5438 init_putv(type, sec, c, 0, expr_type);
5442 /* parse an initializer for type 't' if 'has_init' is non zero, and
5443 allocate space in local or global data space ('r' is either
5444 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5445 variable 'v' with an associated name represented by 'asm_label' of
5446 scope 'scope' is declared before initializers are parsed. If 'v' is
5447 zero, then a reference to the new object is put in the value stack.
5448 If 'has_init' is 2, a special parsing is done to handle string
5449 constants. */
5450 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5451 int has_init, int v, char *asm_label,
5452 int scope)
5454 int size, align, addr, data_offset;
5455 int level;
5456 ParseState saved_parse_state = {0};
5457 TokenString init_str;
5458 Section *sec;
5459 Sym *flexible_array;
5461 flexible_array = NULL;
5462 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5463 Sym *field = type->ref->next;
5464 if (field) {
5465 while (field->next)
5466 field = field->next;
5467 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5468 flexible_array = field;
5472 size = type_size(type, &align);
5473 /* If unknown size, we must evaluate it before
5474 evaluating initializers because
5475 initializers can generate global data too
5476 (e.g. string pointers or ISOC99 compound
5477 literals). It also simplifies local
5478 initializers handling */
5479 tok_str_new(&init_str);
5480 if (size < 0 || (flexible_array && has_init)) {
5481 if (!has_init)
5482 tcc_error("unknown type size");
5483 /* get all init string */
5484 if (has_init == 2) {
5485 /* only get strings */
5486 while (tok == TOK_STR || tok == TOK_LSTR) {
5487 tok_str_add_tok(&init_str);
5488 next();
5490 } else {
5491 level = 0;
5492 while (level > 0 || (tok != ',' && tok != ';')) {
5493 if (tok < 0)
5494 tcc_error("unexpected end of file in initializer");
5495 tok_str_add_tok(&init_str);
5496 if (tok == '{')
5497 level++;
5498 else if (tok == '}') {
5499 level--;
5500 if (level <= 0) {
5501 next();
5502 break;
5505 next();
5508 tok_str_add(&init_str, -1);
5509 tok_str_add(&init_str, 0);
5511 /* compute size */
5512 save_parse_state(&saved_parse_state);
5514 macro_ptr = init_str.str;
5515 next();
5516 decl_initializer(type, NULL, 0, 1, 1);
5517 /* prepare second initializer parsing */
5518 macro_ptr = init_str.str;
5519 next();
5521 /* if still unknown size, error */
5522 size = type_size(type, &align);
5523 if (size < 0)
5524 tcc_error("unknown type size");
5526 if (flexible_array)
5527 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5528 /* take into account specified alignment if bigger */
5529 if (ad->a.aligned) {
5530 if (ad->a.aligned > align)
5531 align = ad->a.aligned;
5532 } else if (ad->a.packed) {
5533 align = 1;
5535 if ((r & VT_VALMASK) == VT_LOCAL) {
5536 sec = NULL;
5537 #ifdef CONFIG_TCC_BCHECK
5538 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5539 loc--;
5541 #endif
5542 loc = (loc - size) & -align;
5543 addr = loc;
5544 #ifdef CONFIG_TCC_BCHECK
5545 /* handles bounds */
5546 /* XXX: currently, since we do only one pass, we cannot track
5547 '&' operators, so we add only arrays */
5548 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5549 unsigned long *bounds_ptr;
5550 /* add padding between regions */
5551 loc--;
5552 /* then add local bound info */
5553 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5554 bounds_ptr[0] = addr;
5555 bounds_ptr[1] = size;
5557 #endif
5558 if (v) {
5559 /* local variable */
5560 sym_push(v, type, r, addr);
5561 } else {
5562 /* push local reference */
5563 vset(type, r, addr);
5565 } else {
5566 Sym *sym;
5568 sym = NULL;
5569 if (v && scope == VT_CONST) {
5570 /* see if the symbol was already defined */
5571 sym = sym_find(v);
5572 if (sym) {
5573 if (!is_compatible_types(&sym->type, type))
5574 tcc_error("incompatible types for redefinition of '%s'",
5575 get_tok_str(v, NULL));
5576 if (sym->type.t & VT_EXTERN) {
5577 /* if the variable is extern, it was not allocated */
5578 sym->type.t &= ~VT_EXTERN;
5579 /* set array size if it was omitted in extern
5580 declaration */
5581 if ((sym->type.t & VT_ARRAY) &&
5582 sym->type.ref->c < 0 &&
5583 type->ref->c >= 0)
5584 sym->type.ref->c = type->ref->c;
5585 } else {
5586 /* we accept several definitions of the same
5587 global variable. this is tricky, because we
5588 must play with the SHN_COMMON type of the symbol */
5589 /* XXX: should check if the variable was already
5590 initialized. It is incorrect to initialized it
5591 twice */
5592 /* no init data, we won't add more to the symbol */
5593 if (!has_init)
5594 goto no_alloc;
5599 /* allocate symbol in corresponding section */
5600 sec = ad->section;
5601 if (!sec) {
5602 if (has_init)
5603 sec = data_section;
5604 else if (tcc_state->nocommon)
5605 sec = bss_section;
5607 if (sec) {
5608 data_offset = sec->data_offset;
5609 data_offset = (data_offset + align - 1) & -align;
5610 addr = data_offset;
5611 /* very important to increment global pointer at this time
5612 because initializers themselves can create new initializers */
5613 data_offset += size;
5614 #ifdef CONFIG_TCC_BCHECK
5615 /* add padding if bound check */
5616 if (tcc_state->do_bounds_check)
5617 data_offset++;
5618 #endif
5619 sec->data_offset = data_offset;
5620 /* allocate section space to put the data */
5621 if (sec->sh_type != SHT_NOBITS &&
5622 data_offset > sec->data_allocated)
5623 section_realloc(sec, data_offset);
5624 /* align section if needed */
5625 if (align > sec->sh_addralign)
5626 sec->sh_addralign = align;
5627 } else {
5628 addr = 0; /* avoid warning */
5631 if (v) {
5632 if (scope != VT_CONST || !sym) {
5633 sym = sym_push(v, type, r | VT_SYM, 0);
5634 sym->asm_label = asm_label;
5636 /* update symbol definition */
5637 if (sec) {
5638 put_extern_sym(sym, sec, addr, size);
5639 } else {
5640 ElfW(Sym) *esym;
5641 /* put a common area */
5642 put_extern_sym(sym, NULL, align, size);
5643 /* XXX: find a nicer way */
5644 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5645 esym->st_shndx = SHN_COMMON;
5647 } else {
5648 /* push global reference */
5649 sym = get_sym_ref(type, sec, addr, size);
5650 vpushsym(type, sym);
5652 /* patch symbol weakness */
5653 if (type->t & VT_WEAK)
5654 weaken_symbol(sym);
5655 #ifdef CONFIG_TCC_BCHECK
5656 /* handles bounds now because the symbol must be defined
5657 before for the relocation */
5658 if (tcc_state->do_bounds_check) {
5659 unsigned long *bounds_ptr;
5661 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5662 /* then add global bound info */
5663 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5664 bounds_ptr[0] = 0; /* relocated */
5665 bounds_ptr[1] = size;
5667 #endif
5669 if (has_init || (type->t & VT_VLA)) {
5670 decl_initializer(type, sec, addr, 1, 0);
5671 /* restore parse state if needed */
5672 if (init_str.str) {
5673 tok_str_free(init_str.str);
5674 restore_parse_state(&saved_parse_state);
5676 /* patch flexible array member size back to -1, */
5677 /* for possible subsequent similar declarations */
5678 if (flexible_array)
5679 flexible_array->type.ref->c = -1;
5681 no_alloc: ;
5684 static void put_func_debug(Sym *sym)
5686 char buf[512];
5688 /* stabs info */
5689 /* XXX: we put here a dummy type */
5690 snprintf(buf, sizeof(buf), "%s:%c1",
5691 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5692 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5693 cur_text_section, sym->c);
5694 /* //gr gdb wants a line at the function */
5695 put_stabn(N_SLINE, 0, file->line_num, 0);
5696 last_ind = 0;
5697 last_line_num = 0;
5700 /* parse an old style function declaration list */
5701 /* XXX: check multiple parameter */
5702 static void func_decl_list(Sym *func_sym)
5704 AttributeDef ad;
5705 int v;
5706 Sym *s;
5707 CType btype, type;
5709 /* parse each declaration */
5710 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5711 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5712 if (!parse_btype(&btype, &ad))
5713 expect("declaration list");
5714 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5715 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5716 tok == ';') {
5717 /* we accept no variable after */
5718 } else {
5719 for(;;) {
5720 type = btype;
5721 type_decl(&type, &ad, &v, TYPE_DIRECT);
5722 /* find parameter in function parameter list */
5723 s = func_sym->next;
5724 while (s != NULL) {
5725 if ((s->v & ~SYM_FIELD) == v)
5726 goto found;
5727 s = s->next;
5729 tcc_error("declaration for parameter '%s' but no such parameter",
5730 get_tok_str(v, NULL));
5731 found:
5732 /* check that no storage specifier except 'register' was given */
5733 if (type.t & VT_STORAGE)
5734 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5735 convert_parameter_type(&type);
5736 /* we can add the type (NOTE: it could be local to the function) */
5737 s->type = type;
5738 /* accept other parameters */
5739 if (tok == ',')
5740 next();
5741 else
5742 break;
5745 skip(';');
5749 /* parse a function defined by symbol 'sym' and generate its code in
5750 'cur_text_section' */
5751 static void gen_function(Sym *sym)
5753 int saved_nocode_wanted = nocode_wanted;
5754 nocode_wanted = 0;
5755 ind = cur_text_section->data_offset;
5756 /* NOTE: we patch the symbol size later */
5757 put_extern_sym(sym, cur_text_section, ind, 0);
5758 funcname = get_tok_str(sym->v, NULL);
5759 func_ind = ind;
5760 /* Initialize VLA state */
5761 vla_sp_loc = &vla_sp_root_loc;
5762 vla_flags = VLA_NEED_NEW_FRAME;
5763 /* put debug symbol */
5764 if (tcc_state->do_debug)
5765 put_func_debug(sym);
5766 /* push a dummy symbol to enable local sym storage */
5767 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5768 gfunc_prolog(&sym->type);
5769 #ifdef CONFIG_TCC_BCHECK
5770 if (tcc_state->do_bounds_check
5771 && !strcmp(get_tok_str(sym->v, NULL), "main")) {
5772 int i;
5774 sym = local_stack;
5775 for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) {
5776 if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD)
5777 break;
5778 vpush_global_sym(&func_old_type, TOK___bound_main_arg);
5779 vset(&sym->type, sym->r, sym->c);
5780 gfunc_call(1);
5783 #endif
5784 rsym = 0;
5785 block(NULL, NULL, NULL, NULL, 0, 0);
5786 gsym(rsym);
5787 gfunc_epilog();
5788 cur_text_section->data_offset = ind;
5789 label_pop(&global_label_stack, NULL);
5790 /* reset local stack */
5791 scope_stack_bottom = NULL;
5792 sym_pop(&local_stack, NULL);
5793 /* end of function */
5794 /* patch symbol size */
5795 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5796 ind - func_ind;
5797 /* patch symbol weakness (this definition overrules any prototype) */
5798 if (sym->type.t & VT_WEAK)
5799 weaken_symbol(sym);
5800 if (tcc_state->do_debug) {
5801 put_stabn(N_FUN, 0, 0, ind - func_ind);
5803 /* It's better to crash than to generate wrong code */
5804 cur_text_section = NULL;
5805 funcname = ""; /* for safety */
5806 func_vt.t = VT_VOID; /* for safety */
5807 func_var = 0; /* for safety */
5808 ind = 0; /* for safety */
5809 nocode_wanted = saved_nocode_wanted;
5812 ST_FUNC void gen_inline_functions(void)
5814 Sym *sym;
5815 int *str, inline_generated, i;
5816 struct InlineFunc *fn;
5818 /* iterate while inline function are referenced */
5819 for(;;) {
5820 inline_generated = 0;
5821 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5822 fn = tcc_state->inline_fns[i];
5823 sym = fn->sym;
5824 if (sym && sym->c) {
5825 /* the function was used: generate its code and
5826 convert it to a normal function */
5827 str = fn->token_str;
5828 fn->sym = NULL;
5829 if (file)
5830 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5831 sym->r = VT_SYM | VT_CONST;
5832 sym->type.t &= ~VT_INLINE;
5834 macro_ptr = str;
5835 next();
5836 cur_text_section = text_section;
5837 gen_function(sym);
5838 macro_ptr = NULL; /* fail safe */
5840 inline_generated = 1;
5843 if (!inline_generated)
5844 break;
5846 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5847 fn = tcc_state->inline_fns[i];
5848 str = fn->token_str;
5849 tok_str_free(str);
5851 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5854 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5855 static int decl0(int l, int is_for_loop_init)
5857 int v, has_init, r;
5858 CType type, btype;
5859 Sym *sym;
5860 AttributeDef ad;
5862 while (1) {
5863 if (!parse_btype(&btype, &ad)) {
5864 if (is_for_loop_init)
5865 return 0;
5866 /* skip redundant ';' */
5867 /* XXX: find more elegant solution */
5868 if (tok == ';') {
5869 next();
5870 continue;
5872 if (l == VT_CONST &&
5873 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5874 /* global asm block */
5875 asm_global_instr();
5876 continue;
5878 /* special test for old K&R protos without explicit int
5879 type. Only accepted when defining global data */
5880 if (l == VT_LOCAL || tok < TOK_DEFINE)
5881 break;
5882 btype.t = VT_INT;
5884 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5885 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5886 tok == ';') {
5887 /* we accept no variable after */
5888 next();
5889 continue;
5891 while (1) { /* iterate thru each declaration */
5892 char *asm_label; // associated asm label
5893 type = btype;
5894 type_decl(&type, &ad, &v, TYPE_DIRECT);
5895 #if 0
5897 char buf[500];
5898 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5899 printf("type = '%s'\n", buf);
5901 #endif
5902 if ((type.t & VT_BTYPE) == VT_FUNC) {
5903 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5904 tcc_error("function without file scope cannot be static");
5906 /* if old style function prototype, we accept a
5907 declaration list */
5908 sym = type.ref;
5909 if (sym->c == FUNC_OLD)
5910 func_decl_list(sym);
5913 asm_label = NULL;
5914 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5915 CString astr;
5917 asm_label_instr(&astr);
5918 asm_label = tcc_strdup(astr.data);
5919 cstr_free(&astr);
5921 /* parse one last attribute list, after asm label */
5922 parse_attribute(&ad);
5925 if (ad.a.weak)
5926 type.t |= VT_WEAK;
5927 #ifdef TCC_TARGET_PE
5928 if (ad.a.func_import)
5929 type.t |= VT_IMPORT;
5930 if (ad.a.func_export)
5931 type.t |= VT_EXPORT;
5932 #endif
5933 if (tok == '{') {
5934 if (l == VT_LOCAL)
5935 tcc_error("cannot use local functions");
5936 if ((type.t & VT_BTYPE) != VT_FUNC)
5937 expect("function definition");
5939 /* reject abstract declarators in function definition */
5940 sym = type.ref;
5941 while ((sym = sym->next) != NULL)
5942 if (!(sym->v & ~SYM_FIELD))
5943 expect("identifier");
5945 /* XXX: cannot do better now: convert extern line to static inline */
5946 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5947 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5949 sym = sym_find(v);
5950 if (sym) {
5951 Sym *ref;
5952 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5953 goto func_error1;
5955 ref = sym->type.ref;
5956 if (0 == ref->a.func_proto)
5957 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5959 /* use func_call from prototype if not defined */
5960 if (ref->a.func_call != FUNC_CDECL
5961 && type.ref->a.func_call == FUNC_CDECL)
5962 type.ref->a.func_call = ref->a.func_call;
5964 /* use export from prototype */
5965 if (ref->a.func_export)
5966 type.ref->a.func_export = 1;
5968 /* use static from prototype */
5969 if (sym->type.t & VT_STATIC)
5970 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5972 if (!is_compatible_types(&sym->type, &type)) {
5973 func_error1:
5974 tcc_error("incompatible types for redefinition of '%s'",
5975 get_tok_str(v, NULL));
5977 type.ref->a.func_proto = 0;
5978 /* if symbol is already defined, then put complete type */
5979 sym->type = type;
5980 } else {
5981 /* put function symbol */
5982 sym = global_identifier_push(v, type.t, 0);
5983 sym->type.ref = type.ref;
5986 /* static inline functions are just recorded as a kind
5987 of macro. Their code will be emitted at the end of
5988 the compilation unit only if they are used */
5989 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5990 (VT_INLINE | VT_STATIC)) {
5991 TokenString func_str;
5992 int block_level;
5993 struct InlineFunc *fn;
5994 const char *filename;
5996 tok_str_new(&func_str);
5998 block_level = 0;
5999 for(;;) {
6000 int t;
6001 if (tok == TOK_EOF)
6002 tcc_error("unexpected end of file");
6003 tok_str_add_tok(&func_str);
6004 t = tok;
6005 next();
6006 if (t == '{') {
6007 block_level++;
6008 } else if (t == '}') {
6009 block_level--;
6010 if (block_level == 0)
6011 break;
6014 tok_str_add(&func_str, -1);
6015 tok_str_add(&func_str, 0);
6016 filename = file ? file->filename : "";
6017 fn = tcc_malloc(sizeof *fn + strlen(filename));
6018 strcpy(fn->filename, filename);
6019 fn->sym = sym;
6020 fn->token_str = func_str.str;
6021 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
6023 } else {
6024 /* compute text section */
6025 cur_text_section = ad.section;
6026 if (!cur_text_section)
6027 cur_text_section = text_section;
6028 sym->r = VT_SYM | VT_CONST;
6029 gen_function(sym);
6031 break;
6032 } else {
6033 if (btype.t & VT_TYPEDEF) {
6034 /* save typedefed type */
6035 /* XXX: test storage specifiers ? */
6036 sym = sym_push(v, &type, 0, 0);
6037 sym->a = ad.a;
6038 sym->type.t |= VT_TYPEDEF;
6039 } else {
6040 r = 0;
6041 if ((type.t & VT_BTYPE) == VT_FUNC) {
6042 /* external function definition */
6043 /* specific case for func_call attribute */
6044 ad.a.func_proto = 1;
6045 type.ref->a = ad.a;
6046 } else if (!(type.t & VT_ARRAY)) {
6047 /* not lvalue if array */
6048 r |= lvalue_type(type.t);
6050 has_init = (tok == '=');
6051 if (has_init && (type.t & VT_VLA))
6052 tcc_error("Variable length array cannot be initialized");
6053 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6054 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6055 !has_init && l == VT_CONST && type.ref->c < 0)) {
6056 /* external variable or function */
6057 /* NOTE: as GCC, uninitialized global static
6058 arrays of null size are considered as
6059 extern */
6060 sym = external_sym(v, &type, r, asm_label);
6062 if (type.t & VT_WEAK)
6063 weaken_symbol(sym);
6065 if (ad.alias_target) {
6066 Section tsec;
6067 Elf32_Sym *esym;
6068 Sym *alias_target;
6070 alias_target = sym_find(ad.alias_target);
6071 if (!alias_target || !alias_target->c)
6072 tcc_error("unsupported forward __alias__ attribute");
6073 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6074 tsec.sh_num = esym->st_shndx;
6075 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6077 } else {
6078 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6079 if (type.t & VT_STATIC)
6080 r |= VT_CONST;
6081 else
6082 r |= l;
6083 if (has_init)
6084 next();
6085 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6088 if (tok != ',') {
6089 if (is_for_loop_init)
6090 return 1;
6091 skip(';');
6092 break;
6094 next();
6096 ad.a.aligned = 0;
6099 return 0;
6102 ST_FUNC void decl(int l)
6104 decl0(l, 0);