Modify i386-gen.c,
[tinycc.git] / tccgen.c
blob9e6275ee741fd607be3287add4b38afd21057591
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, ex_rc;
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;
73 ST_DATA int pop_stack;
75 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
77 /* ------------------------------------------------------------------------- */
78 static void gen_cast(CType *type);
79 static inline CType *pointed_type(CType *type);
80 static int is_compatible_types(CType *type1, CType *type2);
81 static int parse_btype(CType *type, AttributeDef *ad);
82 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
83 static void parse_expr_type(CType *type);
84 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
85 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
86 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
87 static int decl0(int l, int is_for_loop_init);
88 static void expr_eq(void);
89 static void unary_type(CType *type);
90 static void vla_runtime_type_size(CType *type, int *a);
91 static void vla_sp_save(void);
92 static int is_compatible_parameter_types(CType *type1, CType *type2);
93 static void expr_type(CType *type);
94 ST_FUNC void vpush64(int ty, unsigned long long v);
95 ST_FUNC void vpush(CType *type);
96 ST_FUNC int gtst(int inv, int t);
97 ST_FUNC int is_btype_size(int bt);
99 ST_INLN int is_float(int t)
101 int bt;
102 bt = t & VT_BTYPE;
103 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
106 /* we use our own 'finite' function to avoid potential problems with
107 non standard math libs */
108 /* XXX: endianness dependent */
109 ST_FUNC int ieee_finite(double d)
111 int p[4];
112 memcpy(p, &d, sizeof(double));
113 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
116 ST_FUNC void test_lvalue(void)
118 if (!(vtop->r & VT_LVAL))
119 expect("lvalue");
122 /* ------------------------------------------------------------------------- */
123 /* symbol allocator */
124 static Sym *__sym_malloc(void)
126 Sym *sym_pool, *sym, *last_sym;
127 int i;
129 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
130 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
132 last_sym = sym_free_first;
133 sym = sym_pool;
134 for(i = 0; i < SYM_POOL_NB; i++) {
135 sym->next = last_sym;
136 last_sym = sym;
137 sym++;
139 sym_free_first = last_sym;
140 return last_sym;
143 static inline Sym *sym_malloc(void)
145 Sym *sym;
146 sym = sym_free_first;
147 if (!sym)
148 sym = __sym_malloc();
149 sym_free_first = sym->next;
150 return sym;
153 ST_INLN void sym_free(Sym *sym)
155 sym->next = sym_free_first;
156 tcc_free(sym->asm_label);
157 sym_free_first = sym;
160 /* push, without hashing */
161 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
163 Sym *s;
164 if (ps == &local_stack) {
165 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
166 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
167 tcc_error("incompatible types for redefinition of '%s'",
168 get_tok_str(v, NULL));
170 s = sym_malloc();
171 s->asm_label = NULL;
172 s->v = v;
173 s->type.t = t;
174 s->type.ref = NULL;
175 #ifdef _WIN64
176 s->d = NULL;
177 #endif
178 s->c = c;
179 s->next = NULL;
180 /* add in stack */
181 s->prev = *ps;
182 *ps = s;
183 return s;
186 /* find a symbol and return its associated structure. 's' is the top
187 of the symbol stack */
188 ST_FUNC Sym *sym_find2(Sym *s, int v)
190 while (s) {
191 if (s->v == v)
192 return s;
193 else if (s->v == -1)
194 return NULL;
195 s = s->prev;
197 return NULL;
200 /* structure lookup */
201 ST_INLN Sym *struct_find(int v)
203 v -= TOK_IDENT;
204 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
205 return NULL;
206 return table_ident[v]->sym_struct;
209 /* find an identifier */
210 ST_INLN Sym *sym_find(int v)
212 v -= TOK_IDENT;
213 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
214 return NULL;
215 return table_ident[v]->sym_identifier;
218 /* push a given symbol on the symbol stack */
219 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
221 Sym *s, **ps;
222 TokenSym *ts;
224 if (local_stack)
225 ps = &local_stack;
226 else
227 ps = &global_stack;
228 s = sym_push2(ps, v, type->t, c);
229 s->type.ref = type->ref;
230 s->r = r;
231 /* don't record fields or anonymous symbols */
232 /* XXX: simplify */
233 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
234 /* record symbol in token array */
235 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
236 if (v & SYM_STRUCT)
237 ps = &ts->sym_struct;
238 else
239 ps = &ts->sym_identifier;
240 s->prev_tok = *ps;
241 *ps = s;
243 return s;
246 /* push a global identifier */
247 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
249 Sym *s, **ps;
250 s = sym_push2(&global_stack, v, t, c);
251 /* don't record anonymous symbol */
252 if (v < SYM_FIRST_ANOM) {
253 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
254 /* modify the top most local identifier, so that
255 sym_identifier will point to 's' when popped */
256 while (*ps != NULL)
257 ps = &(*ps)->prev_tok;
258 s->prev_tok = NULL;
259 *ps = s;
261 return s;
264 /* pop symbols until top reaches 'b' */
265 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
267 Sym *s, *ss, **ps;
268 TokenSym *ts;
269 int v;
271 s = *ptop;
272 while(s != b) {
273 ss = s->prev;
274 v = s->v;
275 /* remove symbol in token array */
276 /* XXX: simplify */
277 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
278 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
279 if (v & SYM_STRUCT)
280 ps = &ts->sym_struct;
281 else
282 ps = &ts->sym_identifier;
283 *ps = s->prev_tok;
285 sym_free(s);
286 s = ss;
288 *ptop = b;
291 static void weaken_symbol(Sym *sym)
293 sym->type.t |= VT_WEAK;
294 if (sym->c > 0) {
295 int esym_type;
296 ElfW(Sym) *esym;
298 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
299 esym_type = ELFW(ST_TYPE)(esym->st_info);
300 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
304 static void apply_visibility(Sym *sym, CType *type)
306 int vis = sym->type.t & VT_VIS_MASK;
307 int vis2 = type->t & VT_VIS_MASK;
308 if (vis == (STV_DEFAULT << VT_VIS_SHIFT))
309 vis = vis2;
310 else if (vis2 == (STV_DEFAULT << VT_VIS_SHIFT))
312 else
313 vis = (vis < vis2) ? vis : vis2;
314 sym->type.t &= ~VT_VIS_MASK;
315 sym->type.t |= vis;
317 if (sym->c > 0) {
318 ElfW(Sym) *esym;
320 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
321 vis >>= VT_VIS_SHIFT;
322 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) | vis;
326 /* ------------------------------------------------------------------------- */
328 ST_FUNC void swap(int *p, int *q)
330 int t;
331 t = *p;
332 *p = *q;
333 *q = t;
336 static void vsetc(CType *type, int r, CValue *vc)
338 int v;
340 if (vtop >= vstack + (VSTACK_SIZE - 1))
341 tcc_error("memory full (vstack)");
342 /* cannot let cpu flags if other instruction are generated. Also
343 avoid leaving VT_JMP anywhere except on the top of the stack
344 because it would complicate the code generator. */
345 if (vtop >= vstack) {
346 v = vtop->r & VT_VALMASK;
347 if (v == VT_CMP || (v & ~1) == VT_JMP)
348 gv(RC_INT);
350 vtop++;
351 vtop->type = *type;
352 vtop->r = r;
353 vtop->r2 = VT_CONST;
354 vtop->c = *vc;
357 /* push constant of type "type" with useless value */
358 ST_FUNC void vpush(CType *type)
360 CValue cval;
361 vsetc(type, VT_CONST, &cval);
364 /* push integer constant */
365 ST_FUNC void vpushi(int v)
367 CValue cval;
368 cval.i = v;
369 vsetc(&int_type, VT_CONST, &cval);
372 /* push a pointer sized constant */
373 ST_FUNC void vpushs(addr_t v)
375 CValue cval;
376 cval.ptr_offset = v;
377 vsetc(&size_type, VT_CONST, &cval);
380 /* push arbitrary 64bit constant */
381 ST_FUNC void vpush64(int ty, unsigned long long v)
383 CValue cval;
384 CType ctype;
385 ctype.t = ty;
386 ctype.ref = NULL;
387 cval.ull = v;
388 vsetc(&ctype, VT_CONST, &cval);
391 /* push long long constant */
392 static inline void vpushll(long long v)
394 vpush64(VT_LLONG, v);
397 /* push a symbol value of TYPE */
398 static inline void vpushsym(CType *type, Sym *sym)
400 CValue cval;
401 cval.ptr_offset = 0;
402 vsetc(type, VT_CONST | VT_SYM, &cval);
403 vtop->sym = sym;
406 /* Return a static symbol pointing to a section */
407 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
409 int v;
410 Sym *sym;
412 v = anon_sym++;
413 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
414 sym->type.ref = type->ref;
415 sym->r = VT_CONST | VT_SYM;
416 put_extern_sym(sym, sec, offset, size);
417 return sym;
420 /* push a reference to a section offset by adding a dummy symbol */
421 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
423 vpushsym(type, get_sym_ref(type, sec, offset, size));
426 /* define a new external reference to a symbol 'v' of type 'u' */
427 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
429 Sym *s;
431 s = sym_find(v);
432 if (!s) {
433 /* push forward reference */
434 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
435 s->type.ref = type->ref;
436 s->r = r | VT_CONST | VT_SYM;
438 return s;
441 /* define a new external reference to a symbol 'v' with alternate asm
442 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
443 is no alternate name (most cases) */
444 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
446 Sym *s;
448 s = sym_find(v);
449 if (!s) {
450 /* push forward reference */
451 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
452 s->asm_label = asm_label;
453 s->type.t |= VT_EXTERN;
454 } else if (s->type.ref == func_old_type.ref) {
455 s->type.ref = type->ref;
456 s->r = r | VT_CONST | VT_SYM;
457 s->type.t |= VT_EXTERN;
458 } else if (!is_compatible_types(&s->type, type)) {
459 tcc_error("incompatible types for redefinition of '%s'",
460 get_tok_str(v, NULL));
462 /* Merge some storage attributes. */
463 if (type->t & VT_WEAK)
464 weaken_symbol(s);
466 if (type->t & VT_VIS_MASK)
467 apply_visibility(s, type);
469 return s;
472 /* push a reference to global symbol v */
473 ST_FUNC void vpush_global_sym(CType *type, int v)
475 vpushsym(type, external_global_sym(v, type, 0));
478 ST_FUNC void vset(CType *type, int r, int v)
480 CValue cval;
482 cval.i = v;
483 vsetc(type, r, &cval);
486 static void vseti(int r, int v)
488 CType type;
489 type.t = VT_INT;
490 type.ref = 0;
491 vset(&type, r, v);
494 ST_FUNC void vswap(void)
496 SValue tmp;
497 /* cannot let cpu flags if other instruction are generated. Also
498 avoid leaving VT_JMP anywhere except on the top of the stack
499 because it would complicate the code generator. */
500 if (vtop >= vstack) {
501 int v = vtop->r & VT_VALMASK;
502 if (v == VT_CMP || (v & ~1) == VT_JMP)
503 gv(RC_INT);
505 tmp = vtop[0];
506 vtop[0] = vtop[-1];
507 vtop[-1] = tmp;
509 /* XXX: +2% overall speed possible with optimized memswap
511 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
515 ST_FUNC void vpushv(SValue *v)
517 if (vtop >= vstack + (VSTACK_SIZE - 1))
518 tcc_error("memory full (vstack)");
519 vtop++;
520 *vtop = *v;
523 ST_FUNC void vdup(void)
525 vpushv(vtop);
528 static int align_size(int size)
530 #ifdef TCC_TARGET_X86_64
531 if(size > 4)
532 return 8;
533 else
534 #endif
535 if(size > 2)
536 return 4;
537 else if(size > 1)
538 return 2;
539 else
540 return 1;
543 int loc_stack(int size, int is_sub){
544 int l, align;
545 align = align_size(size);
546 size = (size + align - 1) & - align;
547 if(is_sub){
548 pop_stack -= size;
549 if(pop_stack >= 0)
550 l = loc + pop_stack;
551 else{
552 loc += pop_stack;
553 l = loc &= -align;
554 pop_stack = 0;
556 }else{
557 pop_stack += size;
558 l = loc + pop_stack;
560 return l;
563 /* save r to the memory stack, and mark it as being free */
564 ST_FUNC void save_reg(int r)
566 int l, saved, size, align;
567 SValue *p, sv;
568 CType *type;
570 /* modify all stack values */
571 l = saved = 0;
572 for(p = vstack; p <= vtop; p++) {
573 #ifdef TCC_TARGET_X86_64
574 if ((p->r & VT_VALMASK) == r ||
575 ((((p->type.t & VT_BTYPE) == VT_QLONG) || ((p->type.t & VT_BTYPE) == VT_QFLOAT)) &&
576 ((p->r2 & VT_VALMASK) == r)))
577 #else
578 if ((p->r & VT_VALMASK) == r || ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r))
579 #endif
581 /* must save value on stack if not already done */
582 if (!saved) {
583 /* NOTE: must reload 'r' because r might be equal to r2 */
584 r = p->r & VT_VALMASK;
585 /* store register in the stack */
586 type = &p->type;
587 if((type->t & VT_BTYPE) == VT_STRUCT){
588 int ret_align;
589 SValue ret;
590 gfunc_sret(type, func_var, &ret.type, &ret_align);
591 type = &ret.type;
593 if ((p->r & VT_LVAL) || ((type->t & VT_BTYPE) == VT_FUNC))
594 #ifdef TCC_TARGET_X86_64
595 type = &char_pointer_type;
596 #else
597 type = &int_type;
598 #endif
599 size = type_size(type, &align);
600 l = loc_stack(size, 1);
601 sv.r = VT_LOCAL | VT_LVAL;
602 sv.c.ul = l;
603 #ifdef TCC_TARGET_X86_64
604 if (((type->t & VT_BTYPE) == VT_QLONG) || ((type->t & VT_BTYPE) == VT_QFLOAT))
605 #else
606 if ((type->t & VT_BTYPE) == VT_LLONG)
607 #endif
609 #ifdef TCC_TARGET_X86_64
610 int load_size = 8, load_type = ((type->t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
611 #else
612 int load_size = 4, load_type = VT_INT;
613 #endif
614 sv.type.t = load_type;
615 store(r, &sv);
616 sv.c.ul += load_size;
617 store(p->r2, &sv);
618 }else{
619 sv.type.t = type->t;
620 store(r, &sv);
622 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
623 /* x86 specific: need to pop fp register ST0 if saved */
624 if (r == TREG_ST0) {
625 o(0xd8dd); /* fstp %st(0) */
627 #endif
628 saved = 1;
630 /* mark that stack entry as being saved on the stack */
631 if (p->r & VT_LVAL) {
632 /* also clear the bounded flag because the
633 relocation address of the function was stored in
634 p->c.ul */
635 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
636 } else {
637 p->r = lvalue_type(p->type.t) | VT_LOCAL | VT_TMP;
639 p->r2 = VT_CONST;
640 p->c.ul = l;
645 #ifdef TCC_TARGET_ARM
646 /* find a register of class 'rc2' with at most one reference on stack.
647 * If none, call get_reg(rc) */
648 ST_FUNC int get_reg_ex(int rc, int rc2)
650 int r;
651 SValue *p;
653 for(r=0;r<NB_REGS;r++) {
654 if (reg_classes[r] & rc2) {
655 int n;
656 n=0;
657 for(p = vstack; p <= vtop; p++) {
658 if ((p->r & VT_VALMASK) == r ||
659 (p->r2 & VT_VALMASK) == r)
660 n++;
662 if (n <= 1)
663 return r;
666 return get_reg(rc);
668 #endif
670 static int for_reg(int rc)
672 int r;
673 SValue *p;
674 if(rc){
675 for(r = 0; r < NB_REGS; r++) {
676 if (reg_classes[r] & rc) {
677 for(p = vstack; p <= vtop; p++) {
678 if ((p->r & VT_VALMASK) == r || (p->r2 & VT_VALMASK) == r)
679 goto notfound;
681 goto found;
683 notfound:;
686 r = -1;
687 found:
688 return r;
691 /* find a free register of class 'rc'. If none, save one register */
692 int get_reg(int rc)
694 int r;
695 SValue *p;
697 /* find a free register */
698 r = for_reg(rc);
699 if (r != -1)
700 return r;
701 /* no register left : free the first one on the stack (VERY
702 IMPORTANT to start from the bottom to ensure that we don't
703 spill registers used in gen_opi()) */
704 for(p = vstack; p <= vtop; p++) {
705 /* look at second register (if long long) */
706 if(p->r & VT_TMP)
707 continue;
708 r = p->r2 & VT_VALMASK;
709 if (r < VT_CONST && (reg_classes[r] & rc))
710 goto save_found;
711 r = p->r & VT_VALMASK;
712 if (r < VT_CONST && (reg_classes[r] & rc)) {
713 save_found:
714 save_reg(r);
715 return r;
718 /* Should never comes here */
719 assert(0);
720 return -1;
723 /* save registers up to (vtop - n) stack entry */
724 ST_FUNC void save_regs(int n)
726 int r;
727 SValue *p, *p1;
728 p1 = vtop - n;
729 for(p = vstack;p <= p1; p++) {
730 r = p->r & VT_VALMASK;
731 if (r < VT_CONST) {
732 save_reg(r);
737 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
738 if needed */
739 static void move_reg(int r, int s, int t)
741 SValue sv;
743 if (r != s) {
744 save_reg(r);
745 sv.type.t = t;
746 sv.type.ref = NULL;
747 sv.r = s;
748 sv.c.ul = 0;
749 load(r, &sv);
753 /* get address of vtop (vtop MUST BE an lvalue) */
754 ST_FUNC void gaddrof(void)
756 if (vtop->r & VT_REF)
757 gv(RC_INT);
758 vtop->r &= ~VT_LVAL;
759 /* tricky: if saved lvalue, then we can go back to lvalue */
760 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
761 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL| VT_TMP;
764 #ifdef CONFIG_TCC_BCHECK
765 /* generate lvalue bound code */
766 static void gbound(void)
768 int lval_type;
769 CType type1;
771 vtop->r &= ~VT_MUSTBOUND;
772 /* if lvalue, then use checking code before dereferencing */
773 if (vtop->r & VT_LVAL) {
774 /* if not VT_BOUNDED value, then make one */
775 if (!(vtop->r & VT_BOUNDED)) {
776 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
777 /* must save type because we must set it to int to get pointer */
778 type1 = vtop->type;
779 vtop->type.t = VT_INT;
780 gaddrof();
781 vpushi(0);
782 gen_bounded_ptr_add();
783 vtop->r |= lval_type;
784 vtop->type = type1;
786 /* then check for dereferencing */
787 gen_bounded_ptr_deref();
790 #endif
792 /* store vtop a register belonging to class 'rc'. lvalues are
793 converted to values. Cannot be used if cannot be converted to
794 register value (such as structures). */
795 ST_FUNC int gv(int rc)
797 int r, bit_pos, bit_size, size, align, i, ft, sbt;
798 int rc2;
800 ft = vtop->type.t;
801 sbt = ft & VT_BTYPE;
802 /* NOTE: get_reg can modify vstack[] */
803 if (ft & VT_BITFIELD) {
804 CType type;
805 int bits;
806 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
807 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
808 /* remove bit field info to avoid loops */
809 ft = vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
810 /* cast to int to propagate signedness in following ops */
811 if (sbt == VT_LLONG) {
812 type.t = VT_LLONG;
813 bits = 64;
814 } else{
815 type.t = VT_INT;
816 bits = 32;
818 if((ft & VT_UNSIGNED) || sbt == VT_BOOL)
819 type.t |= VT_UNSIGNED;
820 gen_cast(&type);
821 /* generate shifts */
822 vpushi(bits - (bit_pos + bit_size));
823 gen_op(TOK_SHL);
824 vpushi(bits - bit_size);
825 /* NOTE: transformed to SHR if unsigned */
826 gen_op(TOK_SAR);
827 r = gv(rc);
828 } else {
829 if (is_float(vtop->type.t) &&
830 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
831 Sym *sym;
832 int *ptr;
833 unsigned long offset;
834 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
835 CValue check;
836 #endif
838 /* XXX: unify with initializers handling ? */
839 /* CPUs usually cannot use float constants, so we store them
840 generically in data segment */
841 size = type_size(&vtop->type, &align);
842 offset = (data_section->data_offset + align - 1) & -align;
843 data_section->data_offset = offset;
844 /* XXX: not portable yet */
845 #if defined(__i386__) || defined(__x86_64__)
846 /* Zero pad x87 tenbyte long doubles */
847 if (size == LDOUBLE_SIZE) {
848 vtop->c.tab[2] &= 0xffff;
849 #if LDOUBLE_SIZE == 16
850 vtop->c.tab[3] = 0;
851 #endif
853 #endif
854 ptr = section_ptr_add(data_section, size);
855 size = size >> 2;
856 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
857 check.d = 1;
858 if(check.tab[0])
859 for(i=0;i<size;i++)
860 ptr[i] = vtop->c.tab[size-1-i];
861 else
862 #endif
863 for(i=0;i<size;i++)
864 ptr[i] = vtop->c.tab[i];
865 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
866 vtop->r |= VT_LVAL | VT_SYM;
867 vtop->sym = sym;
868 vtop->c.ptr_offset = 0;
870 #ifdef CONFIG_TCC_BCHECK
871 if (vtop->r & VT_MUSTBOUND)
872 gbound();
873 #endif
875 r = vtop->r & VT_VALMASK;
876 if((rc & ~RC_MASK) && (rc != RC_ST0))
877 rc2 = ex_rc;
878 else
879 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
881 /* need to reload if:
882 - constant
883 - lvalue (need to dereference pointer)
884 - already a register, but not in the right class */
885 if (r >= VT_CONST || (vtop->r & VT_LVAL) || !(reg_classes[r] & rc)
886 #ifdef TCC_TARGET_X86_64
887 || (sbt == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
888 || (sbt == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
889 #else
890 || (sbt == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
891 #endif
892 || vtop->c.i)
894 r = get_reg(rc);
895 #ifdef TCC_TARGET_X86_64
896 if ((sbt == VT_QLONG) || (sbt == VT_QFLOAT))
897 #else
898 if (sbt == VT_LLONG)
899 #endif
901 #ifdef TCC_TARGET_X86_64
902 int load_size = 8, load_type = (sbt == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
903 #else
904 int load_size = 4, load_type = VT_INT;
905 unsigned long long ll;
906 #endif
907 int r2;
908 /* two register type load : expand to two words
909 temporarily */
910 #ifndef TCC_TARGET_X86_64
911 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
912 /* load constant */
913 ll = vtop->c.ull;
914 vtop->c.ui = ll; /* first word */
915 load(r, vtop);
916 vtop->r = r; /* save register value */
917 vpushi(ll >> 32); /* second word */
918 } else
919 #endif
920 /* XXX: test to VT_CONST incorrect ? */
921 if (r >= VT_CONST || (vtop->r & VT_LVAL)) {
922 /* We do not want to modifier the long long
923 pointer here, so the safest (and less
924 efficient) is to save all the other registers
925 in the regs. use VT_TMP XXX: totally inefficient. */
926 /* load from memory */
927 vtop->type.t = load_type;
928 load(r, vtop);
929 vdup();
930 vtop[-1].r = r | VT_TMP; /* lock register value */
931 /* increment pointer to get second word */
932 vtop->type = char_pointer_type;
933 gaddrof();
934 vpushi(load_size);
935 gen_op('+');
936 vtop->r |= VT_LVAL;
937 vtop->type.t = load_type;
938 } else {
939 /* move registers */
940 load(r, vtop);
941 vdup();
942 vtop[-1].r = r | VT_TMP; /* lock register value */
943 vtop->r = vtop[-1].r2;
945 /* Allocate second register. Here we rely on the fact that
946 get_reg() tries first to free r2 of an SValue. */
947 r2 = get_reg(rc2);
948 load(r2, vtop);
949 vtop--;
950 /* write second register */
951 vtop->r2 = r2;
952 vtop->r &= ~VT_TMP;
953 vtop->type.t = ft;
954 } else if ((vtop->r & VT_LVAL) && !is_float(ft)) {
955 int t;
956 /* lvalue of scalar type : need to use lvalue type
957 because of possible cast */
958 t = ft;
959 /* compute memory access type */
960 if (vtop->r & VT_REF)
961 #ifdef TCC_TARGET_X86_64
962 t = VT_PTR;
963 #else
964 t = VT_INT;
965 #endif
966 else if (vtop->r & VT_LVAL_BYTE)
967 t = VT_BYTE;
968 else if (vtop->r & VT_LVAL_SHORT)
969 t = VT_SHORT;
970 if (vtop->r & VT_LVAL_UNSIGNED)
971 t |= VT_UNSIGNED;
972 vtop->type.t = t;
973 load(r, vtop);
974 /* restore wanted type */
975 vtop->type.t = ft;
976 } else {
977 /* one register type load */
978 load(r, vtop);
980 vtop->r = r;
981 vtop->c.ptr_offset = 0;
983 #ifdef TCC_TARGET_C67
984 /* uses register pairs for doubles */
985 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
986 vtop->r2 = r+1;
987 #endif
989 return r;
992 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
993 ST_FUNC void gv2(int rc1, int rc2)
995 /* generate more generic register first. But VT_JMP or VT_CMP
996 values must be generated first in all cases to avoid possible
997 reload errors */
998 if (rc1 <= rc2) {
999 vswap();
1000 gv(rc1);
1001 vswap();
1002 gv(rc2);
1003 /* test if reload is needed for first register */
1004 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
1005 vswap();
1006 gv(rc1);
1007 vswap();
1009 } else {
1010 gv(rc2);
1011 vswap();
1012 gv(rc1);
1013 vswap();
1014 /* test if reload is needed for first register */
1015 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
1016 gv(rc2);
1021 /* wrapper around RC_FRET to return a register by type */
1022 static int rc_fret(int t)
1024 #ifdef TCC_TARGET_X86_64
1025 if (t == VT_LDOUBLE) {
1026 return RC_ST0;
1028 #endif
1029 ex_rc = RC_QRET;
1030 return RC_FRET;
1033 /* wrapper around REG_FRET to return a register by type */
1034 static int reg_fret(int t)
1036 #ifdef TCC_TARGET_X86_64
1037 if (t == VT_LDOUBLE) {
1038 return TREG_ST0;
1040 #endif
1041 return REG_FRET;
1044 /* expand long long on stack in two int registers */
1045 static void lexpand(void)
1047 int u;
1049 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
1050 gv(RC_INT);
1051 vdup();
1052 vtop[0].r = vtop[-1].r2;
1053 vtop[0].r2 = VT_CONST;
1054 vtop[-1].r2 = VT_CONST;
1055 vtop[0].type.t = VT_INT | u;
1056 vtop[-1].type.t = VT_INT | u;
1059 #ifdef TCC_TARGET_ARM
1060 /* expand long long on stack */
1061 ST_FUNC void lexpand_nr(void)
1063 int u,v;
1065 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
1066 vdup();
1067 vtop->r2 = VT_CONST;
1068 vtop->type.t = VT_INT | u;
1069 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
1070 if (v == VT_CONST) {
1071 vtop[-1].c.ui = vtop->c.ull;
1072 vtop->c.ui = vtop->c.ull >> 32;
1073 vtop->r = VT_CONST;
1074 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1075 vtop->c.ui += 4;
1076 vtop->r = vtop[-1].r;
1077 } else if (v > VT_CONST) {
1078 vtop--;
1079 lexpand();
1080 } else
1081 vtop->r = vtop[-1].r2;
1082 vtop[-1].r2 = VT_CONST;
1083 vtop[-1].type.t = VT_INT | u;
1085 #endif
1087 #ifndef TCC_TARGET_X86_64
1088 /* build a long long from two ints */
1089 static void lbuild(int t)
1091 gv2(RC_INT, RC_INT);
1092 vtop[-1].r2 = vtop[0].r;
1093 vtop[-1].type.t = t;
1094 vpop();
1096 #endif
1098 /* rotate n first stack elements to the bottom
1099 I1 ... In -> I2 ... In I1 [top is right]
1101 ST_FUNC void vrotb(int n)
1103 int i;
1104 SValue tmp;
1106 tmp = vtop[-n + 1];
1107 for(i=-n+1;i!=0;i++)
1108 vtop[i] = vtop[i+1];
1109 vtop[0] = tmp;
1112 /* rotate the n elements before entry e towards the top
1113 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1115 ST_FUNC void vrote(SValue *e, int n)
1117 int i;
1118 SValue tmp;
1120 tmp = *e;
1121 for(i = 0;i < n - 1; i++)
1122 e[-i] = e[-i - 1];
1123 e[-n + 1] = tmp;
1126 /* rotate n first stack elements to the top
1127 I1 ... In -> In I1 ... I(n-1) [top is right]
1129 ST_FUNC void vrott(int n)
1131 vrote(vtop, n);
1134 /* pop stack value */
1135 ST_FUNC void vpop(void)
1137 int v, fr;
1138 fr = vtop->r;
1139 v = fr & VT_VALMASK;
1140 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1141 /* for x86, we need to pop the FP stack */
1142 if (v == TREG_ST0 && !nocode_wanted) {
1143 o(0xd8dd); /* fstp %st(0) */
1144 } else
1145 #endif
1146 if (v == VT_JMP || v == VT_JMPI) {
1147 /* need to put correct jump if && or || without test */
1148 gsym(vtop->c.ul);
1150 if(fr & VT_TMP){
1151 int size, align;
1152 SValue ret;
1153 if((vtop->type.t & VT_BTYPE) == VT_FUNC)
1154 size = 8;
1155 else{
1156 gfunc_sret(&vtop->type, func_var, &ret.type, &align);
1157 size = type_size(&ret.type, &align);
1159 loc_stack(size, 0);
1161 vtop--;
1164 /* convert stack entry to register and duplicate its value in another
1165 register */
1166 static void gv_dup(void)
1168 int rc, t, r, r1;
1169 SValue sv;
1170 t = vtop->type.t;
1171 #ifndef TCC_TARGET_X86_64
1172 if ((t & VT_BTYPE) == VT_LLONG) {
1173 lexpand();
1174 gv_dup();
1175 vswap();
1176 vrotb(3);
1177 gv_dup();
1178 vrotb(4);
1179 /* stack: H L L1 H1 */
1180 lbuild(t);
1181 vrott(3);
1182 vswap();
1183 lbuild(t);
1184 vswap();
1185 } else
1186 #endif
1188 /* duplicate value */
1189 if (is_float(t)) {
1190 rc = RC_FLOAT;
1191 #ifdef TCC_TARGET_X86_64
1192 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1193 rc = RC_ST0;
1195 #endif
1196 }else
1197 rc = RC_INT;
1198 sv.type.t = t;
1199 r = gv(rc);
1200 r1 = get_reg(rc);
1201 sv.r = r;
1202 sv.c.ul = 0;
1203 load(r1, &sv); /* move r to r1 */
1204 vdup();
1205 /* duplicates value */
1206 if (r != r1)
1207 vtop->r = r1;
1210 #ifndef TCC_TARGET_X86_64
1211 /* generate CPU independent (unsigned) long long operations */
1212 static void gen_opl(int op)
1214 int t, a, b, op1, c, i;
1215 int func;
1216 unsigned short reg_iret = REG_IRET;
1217 unsigned short reg_lret = REG_LRET;
1218 SValue tmp;
1220 switch(op) {
1221 case '/':
1222 case TOK_PDIV:
1223 func = TOK___divdi3;
1224 goto gen_func;
1225 case TOK_UDIV:
1226 func = TOK___udivdi3;
1227 goto gen_func;
1228 case '%':
1229 func = TOK___moddi3;
1230 goto gen_mod_func;
1231 case TOK_UMOD:
1232 func = TOK___umoddi3;
1233 gen_mod_func:
1234 #ifdef TCC_ARM_EABI
1235 reg_iret = TREG_R2;
1236 reg_lret = TREG_R3;
1237 #endif
1238 gen_func:
1239 /* call generic long long function */
1240 vpush_global_sym(&func_old_type, func);
1241 vrott(3);
1242 gfunc_call(2);
1243 vpushi(0);
1244 vtop->r = reg_iret;
1245 vtop->r2 = reg_lret;
1246 break;
1247 case '^':
1248 case '&':
1249 case '|':
1250 case '*':
1251 case '+':
1252 case '-':
1253 t = vtop->type.t;
1254 vswap();
1255 lexpand();
1256 vrotb(3);
1257 lexpand();
1258 /* stack: L1 H1 L2 H2 */
1259 tmp = vtop[0];
1260 vtop[0] = vtop[-3];
1261 vtop[-3] = tmp;
1262 tmp = vtop[-2];
1263 vtop[-2] = vtop[-3];
1264 vtop[-3] = tmp;
1265 vswap();
1266 /* stack: H1 H2 L1 L2 */
1267 if (op == '*') {
1268 vpushv(vtop - 1);
1269 vpushv(vtop - 1);
1270 gen_op(TOK_UMULL);
1271 lexpand();
1272 /* stack: H1 H2 L1 L2 ML MH */
1273 for(i=0;i<4;i++)
1274 vrotb(6);
1275 /* stack: ML MH H1 H2 L1 L2 */
1276 tmp = vtop[0];
1277 vtop[0] = vtop[-2];
1278 vtop[-2] = tmp;
1279 /* stack: ML MH H1 L2 H2 L1 */
1280 gen_op('*');
1281 vrotb(3);
1282 vrotb(3);
1283 gen_op('*');
1284 /* stack: ML MH M1 M2 */
1285 gen_op('+');
1286 gen_op('+');
1287 } else if (op == '+' || op == '-') {
1288 /* XXX: add non carry method too (for MIPS or alpha) */
1289 if (op == '+')
1290 op1 = TOK_ADDC1;
1291 else
1292 op1 = TOK_SUBC1;
1293 gen_op(op1);
1294 /* stack: H1 H2 (L1 op L2) */
1295 vrotb(3);
1296 vrotb(3);
1297 gen_op(op1 + 1); /* TOK_xxxC2 */
1298 } else {
1299 gen_op(op);
1300 /* stack: H1 H2 (L1 op L2) */
1301 vrotb(3);
1302 vrotb(3);
1303 /* stack: (L1 op L2) H1 H2 */
1304 gen_op(op);
1305 /* stack: (L1 op L2) (H1 op H2) */
1307 /* stack: L H */
1308 lbuild(t);
1309 break;
1310 case TOK_SAR:
1311 case TOK_SHR:
1312 case TOK_SHL:
1313 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1314 t = vtop[-1].type.t;
1315 vswap();
1316 lexpand();
1317 vrotb(3);
1318 /* stack: L H shift */
1319 c = (int)vtop->c.i;
1320 /* constant: simpler */
1321 /* NOTE: all comments are for SHL. the other cases are
1322 done by swaping words */
1323 vpop();
1324 if (op != TOK_SHL)
1325 vswap();
1326 if (c >= 32) {
1327 /* stack: L H */
1328 vpop();
1329 if (c > 32) {
1330 vpushi(c - 32);
1331 gen_op(op);
1333 if (op != TOK_SAR) {
1334 vpushi(0);
1335 } else {
1336 gv_dup();
1337 vpushi(31);
1338 gen_op(TOK_SAR);
1340 vswap();
1341 } else {
1342 vswap();
1343 gv_dup();
1344 /* stack: H L L */
1345 vpushi(c);
1346 gen_op(op);
1347 vswap();
1348 vpushi(32 - c);
1349 if (op == TOK_SHL)
1350 gen_op(TOK_SHR);
1351 else
1352 gen_op(TOK_SHL);
1353 vrotb(3);
1354 /* stack: L L H */
1355 vpushi(c);
1356 if (op == TOK_SHL)
1357 gen_op(TOK_SHL);
1358 else
1359 gen_op(TOK_SHR);
1360 gen_op('|');
1362 if (op != TOK_SHL)
1363 vswap();
1364 lbuild(t);
1365 } else {
1366 /* XXX: should provide a faster fallback on x86 ? */
1367 switch(op) {
1368 case TOK_SAR:
1369 func = TOK___ashrdi3;
1370 goto gen_func;
1371 case TOK_SHR:
1372 func = TOK___lshrdi3;
1373 goto gen_func;
1374 case TOK_SHL:
1375 func = TOK___ashldi3;
1376 goto gen_func;
1379 break;
1380 default:
1381 /* compare operations */
1382 t = vtop->type.t;
1383 vswap();
1384 lexpand();
1385 vrotb(3);
1386 lexpand();
1387 /* stack: L1 H1 L2 H2 */
1388 tmp = vtop[-1];
1389 vtop[-1] = vtop[-2];
1390 vtop[-2] = tmp;
1391 /* stack: L1 L2 H1 H2 */
1392 /* compare high */
1393 op1 = op;
1394 /* when values are equal, we need to compare low words. since
1395 the jump is inverted, we invert the test too. */
1396 if (op1 == TOK_LT)
1397 op1 = TOK_LE;
1398 else if (op1 == TOK_GT)
1399 op1 = TOK_GE;
1400 else if (op1 == TOK_ULT)
1401 op1 = TOK_ULE;
1402 else if (op1 == TOK_UGT)
1403 op1 = TOK_UGE;
1404 a = 0;
1405 b = 0;
1406 gen_op(op1);
1407 if (op1 != TOK_NE) {
1408 a = gtst(1, 0);
1410 if (op != TOK_EQ) {
1411 /* generate non equal test */
1412 /* XXX: NOT PORTABLE yet */
1413 if (a == 0) {
1414 b = gtst(0, 0);
1415 } else {
1416 #if defined(TCC_TARGET_I386)
1417 b = psym(0x850f, 0);
1418 #elif defined(TCC_TARGET_ARM)
1419 b = ind;
1420 o(0x1A000000 | encbranch(ind, 0, 1));
1421 #elif defined(TCC_TARGET_C67)
1422 tcc_error("not implemented");
1423 #else
1424 #error not supported
1425 #endif
1428 /* compare low. Always unsigned */
1429 op1 = op;
1430 if (op1 == TOK_LT)
1431 op1 = TOK_ULT;
1432 else if (op1 == TOK_LE)
1433 op1 = TOK_ULE;
1434 else if (op1 == TOK_GT)
1435 op1 = TOK_UGT;
1436 else if (op1 == TOK_GE)
1437 op1 = TOK_UGE;
1438 gen_op(op1);
1439 a = gtst(1, a);
1440 gsym(b);
1441 vseti(VT_JMPI, a);
1442 break;
1445 #endif
1447 /* handle integer constant optimizations and various machine
1448 independent opt */
1449 static void gen_opic(int op)
1451 int c1, c2, t1, t2, n;
1452 SValue *v1, *v2;
1453 long long l1, l2;
1454 typedef unsigned long long U;
1456 v1 = vtop - 1;
1457 v2 = vtop;
1458 t1 = v1->type.t & VT_BTYPE;
1459 t2 = v2->type.t & VT_BTYPE;
1461 if (t1 == VT_LLONG)
1462 l1 = v1->c.ll;
1463 else if (v1->type.t & VT_UNSIGNED)
1464 l1 = v1->c.ui;
1465 else
1466 l1 = v1->c.i;
1468 if (t2 == VT_LLONG)
1469 l2 = v2->c.ll;
1470 else if (v2->type.t & VT_UNSIGNED)
1471 l2 = v2->c.ui;
1472 else
1473 l2 = v2->c.i;
1475 /* currently, we cannot do computations with forward symbols */
1476 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1477 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1478 if (c1 && c2) {
1479 switch(op) {
1480 case '+': l1 += l2; break;
1481 case '-': l1 -= l2; break;
1482 case '&': l1 &= l2; break;
1483 case '^': l1 ^= l2; break;
1484 case '|': l1 |= l2; break;
1485 case '*': l1 *= l2; break;
1487 case TOK_PDIV:
1488 case '/':
1489 case '%':
1490 case TOK_UDIV:
1491 case TOK_UMOD:
1492 /* if division by zero, generate explicit division */
1493 if (l2 == 0) {
1494 if (const_wanted)
1495 tcc_error("division by zero in constant");
1496 goto general_case;
1498 switch(op) {
1499 default: l1 /= l2; break;
1500 case '%': l1 %= l2; break;
1501 case TOK_UDIV: l1 = (U)l1 / l2; break;
1502 case TOK_UMOD: l1 = (U)l1 % l2; break;
1504 break;
1505 case TOK_SHL: l1 <<= l2; break;
1506 case TOK_SHR: l1 = (U)l1 >> l2; break;
1507 case TOK_SAR: l1 >>= l2; break;
1508 /* tests */
1509 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1510 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1511 case TOK_EQ: l1 = l1 == l2; break;
1512 case TOK_NE: l1 = l1 != l2; break;
1513 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1514 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1515 case TOK_LT: l1 = l1 < l2; break;
1516 case TOK_GE: l1 = l1 >= l2; break;
1517 case TOK_LE: l1 = l1 <= l2; break;
1518 case TOK_GT: l1 = l1 > l2; break;
1519 /* logical */
1520 case TOK_LAND: l1 = l1 && l2; break;
1521 case TOK_LOR: l1 = l1 || l2; break;
1522 default:
1523 goto general_case;
1525 v1->c.ll = l1;
1526 vtop--;
1527 } else {
1528 /* if commutative ops, put c2 as constant */
1529 if (c1 && (op == '+' || op == '&' || op == '^' ||
1530 op == '|' || op == '*')) {
1531 vswap();
1532 c2 = c1; //c = c1, c1 = c2, c2 = c;
1533 l2 = l1; //l = l1, l1 = l2, l2 = l;
1535 /* Filter out NOP operations like x*1, x-0, x&-1... */
1536 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1537 op == TOK_PDIV) &&
1538 l2 == 1) ||
1539 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1540 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1541 l2 == 0) ||
1542 (op == '&' &&
1543 l2 == -1))) {
1544 /* nothing to do */
1545 vtop--;
1546 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1547 /* try to use shifts instead of muls or divs */
1548 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1549 n = -1;
1550 while (l2) {
1551 l2 >>= 1;
1552 n++;
1554 vtop->c.ll = n;
1555 if (op == '*')
1556 op = TOK_SHL;
1557 else if (op == TOK_PDIV)
1558 op = TOK_SAR;
1559 else
1560 op = TOK_SHR;
1562 goto general_case;
1563 } else if (c2 && (op == '+' || op == '-') &&
1564 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1565 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1566 /* symbol + constant case */
1567 if (op == '-')
1568 l2 = -l2;
1569 vtop--;
1570 vtop->c.ll += l2;
1571 } else {
1572 general_case:
1573 if (!nocode_wanted) {
1574 /* call low level op generator */
1575 if (t1 == VT_LLONG || t2 == VT_LLONG)
1576 gen_opl(op);
1577 else
1578 gen_opi(op);
1579 } else {
1580 vtop--;
1586 /* generate a floating point operation with constant propagation */
1587 static void gen_opif(int op)
1589 int c1, c2;
1590 SValue *v1, *v2;
1591 long double f1, f2;
1593 v1 = vtop - 1;
1594 v2 = vtop;
1595 /* currently, we cannot do computations with forward symbols */
1596 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1597 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1598 if (c1 && c2) {
1599 if (v1->type.t == VT_FLOAT) {
1600 f1 = v1->c.f;
1601 f2 = v2->c.f;
1602 } else if (v1->type.t == VT_DOUBLE) {
1603 f1 = v1->c.d;
1604 f2 = v2->c.d;
1605 } else {
1606 f1 = v1->c.ld;
1607 f2 = v2->c.ld;
1610 /* NOTE: we only do constant propagation if finite number (not
1611 NaN or infinity) (ANSI spec) */
1612 if (!ieee_finite(f1) || !ieee_finite(f2))
1613 goto general_case;
1615 switch(op) {
1616 case '+': f1 += f2; break;
1617 case '-': f1 -= f2; break;
1618 case '*': f1 *= f2; break;
1619 case '/':
1620 if (f2 == 0.0) {
1621 if (const_wanted)
1622 tcc_error("division by zero in constant");
1623 goto general_case;
1625 f1 /= f2;
1626 break;
1627 /* XXX: also handles tests ? */
1628 default:
1629 goto general_case;
1631 /* XXX: overflow test ? */
1632 if (v1->type.t == VT_FLOAT) {
1633 v1->c.f = f1;
1634 } else if (v1->type.t == VT_DOUBLE) {
1635 v1->c.d = f1;
1636 } else {
1637 v1->c.ld = f1;
1639 vtop--;
1640 } else {
1641 general_case:
1642 if (!nocode_wanted) {
1643 gen_opf(op);
1644 } else {
1645 vtop--;
1650 static int pointed_size(CType *type)
1652 int align;
1653 return type_size(pointed_type(type), &align);
1656 static void vla_runtime_pointed_size(CType *type)
1658 int align;
1659 vla_runtime_type_size(pointed_type(type), &align);
1662 static inline int is_null_pointer(SValue *p)
1664 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1665 return 0;
1666 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1667 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1668 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0);
1671 static inline int is_integer_btype(int bt)
1673 return (bt == VT_BYTE || bt == VT_SHORT ||
1674 bt == VT_INT || bt == VT_LLONG);
1677 /* check types for comparison or subtraction of pointers */
1678 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1680 CType *type1, *type2, tmp_type1, tmp_type2;
1681 int bt1, bt2;
1683 /* null pointers are accepted for all comparisons as gcc */
1684 if (is_null_pointer(p1) || is_null_pointer(p2))
1685 return;
1686 type1 = &p1->type;
1687 type2 = &p2->type;
1688 bt1 = type1->t & VT_BTYPE;
1689 bt2 = type2->t & VT_BTYPE;
1690 /* accept comparison between pointer and integer with a warning */
1691 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1692 if (op != TOK_LOR && op != TOK_LAND )
1693 tcc_warning("comparison between pointer and integer");
1694 return;
1697 /* both must be pointers or implicit function pointers */
1698 if (bt1 == VT_PTR) {
1699 type1 = pointed_type(type1);
1700 } else if (bt1 != VT_FUNC)
1701 goto invalid_operands;
1703 if (bt2 == VT_PTR) {
1704 type2 = pointed_type(type2);
1705 } else if (bt2 != VT_FUNC) {
1706 invalid_operands:
1707 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1709 if ((type1->t & VT_BTYPE) == VT_VOID ||
1710 (type2->t & VT_BTYPE) == VT_VOID)
1711 return;
1712 tmp_type1 = *type1;
1713 tmp_type2 = *type2;
1714 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1715 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1716 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1717 /* gcc-like error if '-' is used */
1718 if (op == '-')
1719 goto invalid_operands;
1720 else
1721 tcc_warning("comparison of distinct pointer types lacks a cast");
1725 /* generic gen_op: handles types problems */
1726 ST_FUNC void gen_op(int op)
1728 int u, t1, t2, bt1, bt2, t;
1729 CType type1;
1731 t1 = vtop[-1].type.t;
1732 t2 = vtop[0].type.t;
1733 bt1 = t1 & VT_BTYPE;
1734 bt2 = t2 & VT_BTYPE;
1736 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1737 /* at least one operand is a pointer */
1738 /* relationnal op: must be both pointers */
1739 if (op >= TOK_ULT && op <= TOK_LOR) {
1740 check_comparison_pointer_types(vtop - 1, vtop, op);
1741 /* pointers are handled are unsigned */
1742 #ifdef TCC_TARGET_X86_64
1743 t = VT_LLONG | VT_UNSIGNED;
1744 #else
1745 t = VT_INT | VT_UNSIGNED;
1746 #endif
1747 goto std_op;
1749 /* if both pointers, then it must be the '-' op */
1750 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1751 if (op != '-')
1752 tcc_error("cannot use pointers here");
1753 check_comparison_pointer_types(vtop - 1, vtop, op);
1754 /* XXX: check that types are compatible */
1755 if (vtop[-1].type.t & VT_VLA) {
1756 vla_runtime_pointed_size(&vtop[-1].type);
1757 } else {
1758 vpushi(pointed_size(&vtop[-1].type));
1760 vrott(3);
1761 gen_opic(op);
1762 /* set to integer type */
1763 #ifdef TCC_TARGET_X86_64
1764 vtop->type.t = VT_LLONG;
1765 #else
1766 vtop->type.t = VT_INT;
1767 #endif
1768 vswap();
1769 gen_op(TOK_PDIV);
1770 } else {
1771 /* exactly one pointer : must be '+' or '-'. */
1772 if (op != '-' && op != '+')
1773 tcc_error("cannot use pointers here");
1774 /* Put pointer as first operand */
1775 if (bt2 == VT_PTR) {
1776 vswap();
1777 swap(&t1, &t2);
1779 type1 = vtop[-1].type;
1780 type1.t &= ~VT_ARRAY;
1781 if (vtop[-1].type.t & VT_VLA)
1782 vla_runtime_pointed_size(&vtop[-1].type);
1783 else {
1784 u = pointed_size(&vtop[-1].type);
1785 if (u < 0)
1786 tcc_error("unknown array element size");
1787 #ifdef TCC_TARGET_X86_64
1788 vpushll(u);
1789 #else
1790 /* XXX: cast to int ? (long long case) */
1791 vpushi(u);
1792 #endif
1794 gen_op('*');
1795 #ifdef CONFIG_TCC_BCHECK
1796 /* if evaluating constant expression, no code should be
1797 generated, so no bound check */
1798 if (tcc_state->do_bounds_check && !const_wanted) {
1799 /* if bounded pointers, we generate a special code to
1800 test bounds */
1801 if (op == '-') {
1802 vpushi(0);
1803 vswap();
1804 gen_op('-');
1806 gen_bounded_ptr_add();
1807 } else
1808 #endif
1810 gen_opic(op);
1812 /* put again type if gen_opic() swaped operands */
1813 vtop->type = type1;
1815 } else if (is_float(bt1) || is_float(bt2)) {
1816 /* compute bigger type and do implicit casts */
1817 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1818 t = VT_LDOUBLE;
1819 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1820 t = VT_DOUBLE;
1821 } else {
1822 t = VT_FLOAT;
1824 /* floats can only be used for a few operations */
1825 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1826 (op < TOK_ULT || op > TOK_GT))
1827 tcc_error("invalid operands for binary operation");
1828 goto std_op;
1829 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1830 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1831 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1832 t |= VT_UNSIGNED;
1833 goto std_op;
1834 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1835 /* cast to biggest op */
1836 t = VT_LLONG;
1837 /* convert to unsigned if it does not fit in a long long */
1838 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1839 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1840 t |= VT_UNSIGNED;
1841 goto std_op;
1842 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1843 tcc_error("comparison of struct");
1844 } else {
1845 /* integer operations */
1846 t = VT_INT;
1847 /* convert to unsigned if it does not fit in an integer */
1848 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1849 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1850 t |= VT_UNSIGNED;
1851 std_op:
1852 /* XXX: currently, some unsigned operations are explicit, so
1853 we modify them here */
1854 if (t & VT_UNSIGNED) {
1855 if (op == TOK_SAR)
1856 op = TOK_SHR;
1857 else if (op == '/')
1858 op = TOK_UDIV;
1859 else if (op == '%')
1860 op = TOK_UMOD;
1861 else if (op == TOK_LT)
1862 op = TOK_ULT;
1863 else if (op == TOK_GT)
1864 op = TOK_UGT;
1865 else if (op == TOK_LE)
1866 op = TOK_ULE;
1867 else if (op == TOK_GE)
1868 op = TOK_UGE;
1870 vswap();
1871 type1.t = t;
1872 gen_cast(&type1);
1873 vswap();
1874 /* special case for shifts and long long: we keep the shift as
1875 an integer */
1876 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1877 type1.t = VT_INT;
1878 gen_cast(&type1);
1879 if (is_float(t))
1880 gen_opif(op);
1881 else
1882 gen_opic(op);
1883 if (op >= TOK_ULT && op <= TOK_GT) {
1884 /* relationnal op: the result is an int */
1885 vtop->type.t = VT_INT;
1886 } else {
1887 vtop->type.t = t;
1892 #ifndef TCC_TARGET_ARM
1893 /* generic itof for unsigned long long case */
1894 static void gen_cvt_itof1(int t)
1896 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1897 (VT_LLONG | VT_UNSIGNED)) {
1899 if (t == VT_FLOAT)
1900 vpush_global_sym(&func_old_type, TOK___floatundisf);
1901 #if LDOUBLE_SIZE != 8
1902 else if (t == VT_LDOUBLE)
1903 vpush_global_sym(&func_old_type, TOK___floatundixf);
1904 #endif
1905 else
1906 vpush_global_sym(&func_old_type, TOK___floatundidf);
1907 vrott(2);
1908 gfunc_call(1);
1909 vpushi(0);
1910 vtop->r = reg_fret(t);
1911 } else {
1912 gen_cvt_itof(t);
1915 #endif
1917 /* generic ftoi for unsigned long long case */
1918 static void gen_cvt_ftoi1(int t)
1920 int st;
1922 if (t == (VT_LLONG | VT_UNSIGNED)) {
1923 /* not handled natively */
1924 st = vtop->type.t & VT_BTYPE;
1925 if (st == VT_FLOAT)
1926 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1927 #if LDOUBLE_SIZE != 8
1928 else if (st == VT_LDOUBLE)
1929 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1930 #endif
1931 else
1932 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1933 vrott(2);
1934 gfunc_call(1);
1935 vpushi(0);
1936 vtop->r = REG_IRET;
1937 vtop->r2 = REG_LRET;
1938 } else {
1939 gen_cvt_ftoi(t);
1943 /* force char or short cast */
1944 static void force_charshort_cast(int t)
1946 int bits, dbt;
1947 dbt = t & VT_BTYPE;
1948 /* XXX: add optimization if lvalue : just change type and offset */
1949 if (dbt == VT_BYTE)
1950 bits = 8;
1951 else
1952 bits = 16;
1953 if (t & VT_UNSIGNED) {
1954 vpushi((1 << bits) - 1);
1955 gen_op('&');
1956 } else {
1957 bits = 32 - bits;
1958 vpushi(bits);
1959 gen_op(TOK_SHL);
1960 /* result must be signed or the SAR is converted to an SHL
1961 This was not the case when "t" was a signed short
1962 and the last value on the stack was an unsigned int */
1963 vtop->type.t &= ~VT_UNSIGNED;
1964 vpushi(bits);
1965 gen_op(TOK_SAR);
1969 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1970 static void gen_cast(CType *type)
1972 int sbt, dbt, sf, df, c, p;
1974 /* special delayed cast for char/short */
1975 /* XXX: in some cases (multiple cascaded casts), it may still
1976 be incorrect */
1977 if (vtop->r & VT_MUSTCAST) {
1978 vtop->r &= ~VT_MUSTCAST;
1979 force_charshort_cast(vtop->type.t);
1982 /* bitfields first get cast to ints */
1983 if (vtop->type.t & VT_BITFIELD) {
1984 gv(RC_INT);
1987 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1988 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1990 if (sbt != dbt) {
1991 sf = is_float(sbt);
1992 df = is_float(dbt);
1993 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1994 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1995 if (c) {
1996 /* constant case: we can do it now */
1997 /* XXX: in ISOC, cannot do it if error in convert */
1998 if (sbt == VT_FLOAT)
1999 vtop->c.ld = vtop->c.f;
2000 else if (sbt == VT_DOUBLE)
2001 vtop->c.ld = vtop->c.d;
2003 if (df) {
2004 if ((sbt & VT_BTYPE) == VT_LLONG) {
2005 if (sbt & VT_UNSIGNED)
2006 vtop->c.ld = vtop->c.ull;
2007 else
2008 vtop->c.ld = vtop->c.ll;
2009 } else if(!sf) {
2010 if (sbt & VT_UNSIGNED)
2011 vtop->c.ld = vtop->c.ui;
2012 else
2013 vtop->c.ld = vtop->c.i;
2016 if (dbt == VT_FLOAT)
2017 vtop->c.f = (float)vtop->c.ld;
2018 else if (dbt == VT_DOUBLE)
2019 vtop->c.d = (double)vtop->c.ld;
2020 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
2021 vtop->c.ull = (unsigned long long)vtop->c.ld;
2022 } else if (sf && dbt == VT_BOOL) {
2023 vtop->c.i = (vtop->c.ld != 0);
2024 } else {
2025 if(sf)
2026 vtop->c.ll = (long long)vtop->c.ld;
2027 else if (sbt == (VT_LLONG|VT_UNSIGNED))
2028 vtop->c.ll = vtop->c.ull;
2029 else if (sbt & VT_UNSIGNED)
2030 vtop->c.ll = vtop->c.ui;
2031 #ifdef TCC_TARGET_X86_64
2032 else if (sbt == VT_PTR)
2034 #endif
2035 else if (sbt != VT_LLONG)
2036 vtop->c.ll = vtop->c.i;
2038 if (dbt == (VT_LLONG|VT_UNSIGNED))
2039 vtop->c.ull = vtop->c.ll;
2040 else if (dbt == VT_BOOL)
2041 vtop->c.i = (vtop->c.ll != 0);
2042 #ifdef TCC_TARGET_X86_64
2043 else if (dbt == VT_PTR)
2045 #endif
2046 else if (dbt != VT_LLONG) {
2047 int s = 0;
2048 if ((dbt & VT_BTYPE) == VT_BYTE)
2049 s = 24;
2050 else if ((dbt & VT_BTYPE) == VT_SHORT)
2051 s = 16;
2052 if(dbt & VT_UNSIGNED)
2053 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
2054 else
2055 vtop->c.i = ((int)vtop->c.ll << s) >> s;
2058 } else if (p && dbt == VT_BOOL) {
2059 vtop->r = VT_CONST;
2060 vtop->c.i = 1;
2061 } else if (!nocode_wanted) {
2062 /* non constant case: generate code */
2063 if (sf && df) {
2064 /* convert from fp to fp */
2065 gen_cvt_ftof(dbt);
2066 } else if (df) {
2067 /* convert int to fp */
2068 gen_cvt_itof1(dbt);
2069 } else if (sf) {
2070 /* convert fp to int */
2071 if (dbt == VT_BOOL) {
2072 vpushi(0);
2073 gen_op(TOK_NE);
2074 } else {
2075 /* we handle char/short/etc... with generic code */
2076 if (dbt != (VT_INT | VT_UNSIGNED) &&
2077 dbt != (VT_LLONG | VT_UNSIGNED) &&
2078 dbt != VT_LLONG)
2079 dbt = VT_INT;
2080 gen_cvt_ftoi1(dbt);
2081 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
2082 /* additional cast for char/short... */
2083 vtop->type.t = dbt;
2084 gen_cast(type);
2087 #ifndef TCC_TARGET_X86_64
2088 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2089 if ((sbt & VT_BTYPE) != VT_LLONG) {
2090 /* scalar to long long */
2091 /* machine independent conversion */
2092 gv(RC_INT);
2093 /* generate high word */
2094 if (sbt == (VT_INT | VT_UNSIGNED)) {
2095 vpushi(0);
2096 gv(RC_INT);
2097 } else {
2098 if (sbt == VT_PTR) {
2099 /* cast from pointer to int before we apply
2100 shift operation, which pointers don't support*/
2101 gen_cast(&int_type);
2103 gv_dup();
2104 vpushi(31);
2105 gen_op(TOK_SAR);
2107 /* patch second register */
2108 vtop[-1].r2 = vtop->r;
2109 vpop();
2111 #else
2112 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2113 (dbt & VT_BTYPE) == VT_PTR ||
2114 (dbt & VT_BTYPE) == VT_FUNC) {
2115 if ((sbt & VT_BTYPE) != VT_LLONG &&
2116 (sbt & VT_BTYPE) != VT_PTR &&
2117 (sbt & VT_BTYPE) != VT_FUNC) {
2118 /* need to convert from 32bit to 64bit */
2119 int r = gv(RC_INT);
2120 if (sbt != (VT_INT | VT_UNSIGNED)) {
2121 /* x86_64 specific: movslq */
2122 o(0x6348);
2123 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2126 #endif
2127 } else if (dbt == VT_BOOL) {
2128 /* scalar to bool */
2129 vpushi(0);
2130 gen_op(TOK_NE);
2131 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2132 (dbt & VT_BTYPE) == VT_SHORT) {
2133 if (sbt == VT_PTR) {
2134 vtop->type.t = VT_INT;
2135 tcc_warning("nonportable conversion from pointer to char/short");
2137 force_charshort_cast(dbt);
2138 } else if ((dbt & VT_BTYPE) == VT_INT) {
2139 /* scalar to int */
2140 if (sbt == VT_LLONG) {
2141 /* from long long: just take low order word */
2142 lexpand();
2143 vpop();
2145 /* if lvalue and single word type, nothing to do because
2146 the lvalue already contains the real type size (see
2147 VT_LVAL_xxx constants) */
2150 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2151 /* if we are casting between pointer types,
2152 we must update the VT_LVAL_xxx size */
2153 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2154 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2156 vtop->type = *type;
2159 /* return type size as known at compile time. Put alignment at 'a' */
2160 ST_FUNC int type_size(CType *type, int *a)
2162 Sym *s;
2163 int bt;
2164 size_t size;
2166 bt = type->t & VT_BTYPE;
2167 if (bt == VT_STRUCT) {
2168 assert(!(type->t & VT_VLS));
2169 /* struct/union */
2170 s = type->ref;
2171 *a = s->r;
2172 size = s->c;
2173 } else if (bt == VT_PTR) {
2174 if (type->t & VT_ARRAY) {
2175 int ts;
2176 s = type->ref;
2177 ts = type_size(&s->type, a);
2178 if (ts < 0 && s->c < 0)
2179 ts = -ts;
2180 size = (size_t)ts * s->c;
2181 } else {
2182 *a = PTR_SIZE;
2183 size = PTR_SIZE;
2185 } else if (bt == VT_LDOUBLE) {
2186 *a = LDOUBLE_ALIGN;
2187 size = LDOUBLE_SIZE;
2188 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2189 #ifdef TCC_TARGET_I386
2190 #ifdef TCC_TARGET_PE
2191 *a = 8;
2192 #else
2193 *a = 4;
2194 #endif
2195 #elif defined(TCC_TARGET_ARM)
2196 #ifdef TCC_ARM_EABI
2197 *a = 8;
2198 #else
2199 *a = 4;
2200 #endif
2201 #else
2202 *a = 8;
2203 #endif
2204 size = 8;
2205 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2206 *a = 4;
2207 size = 4;
2208 } else if (bt == VT_SHORT) {
2209 *a = 2;
2210 size = 2;
2211 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2212 *a = 8;
2213 size = 16;
2214 } else {
2215 /* char, void, function, _Bool */
2216 *a = 1;
2217 size = 1;
2219 assert(size == (int)size);
2220 return (int)size;
2223 /* push type size as known at runtime time on top of value stack. Put
2224 alignment at 'a' */
2225 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2227 if (type->t & VT_VLA) {
2228 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2229 } else {
2230 vpushi(type_size(type, a));
2234 static void vla_sp_save(void) {
2235 if (!(vla_flags & VLA_SP_LOC_SET)) {
2236 *vla_sp_loc = loc_stack(PTR_SIZE, 1);
2237 vla_flags |= VLA_SP_LOC_SET;
2239 if (!(vla_flags & VLA_SP_SAVED)) {
2240 gen_vla_sp_save(*vla_sp_loc);
2241 vla_flags |= VLA_SP_SAVED;
2245 /* return the pointed type of t */
2246 static inline CType *pointed_type(CType *type)
2248 return &type->ref->type;
2251 /* modify type so that its it is a pointer to type. */
2252 ST_FUNC void mk_pointer(CType *type)
2254 Sym *s;
2255 s = sym_push(SYM_FIELD, type, 0, -1);
2256 type->t = VT_PTR | (type->t & ~VT_TYPE);
2257 type->ref = s;
2260 /* compare function types. OLD functions match any new functions */
2261 static int is_compatible_func(CType *type1, CType *type2)
2263 Sym *s1, *s2;
2265 s1 = type1->ref;
2266 s2 = type2->ref;
2267 if (!is_compatible_types(&s1->type, &s2->type))
2268 return 0;
2269 /* check func_call */
2270 if (s1->a.func_call != s2->a.func_call)
2271 return 0;
2272 /* XXX: not complete */
2273 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2274 return 1;
2275 if (s1->c != s2->c)
2276 return 0;
2277 while (s1 != NULL) {
2278 if (s2 == NULL)
2279 return 0;
2280 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2281 return 0;
2282 s1 = s1->next;
2283 s2 = s2->next;
2285 if (s2)
2286 return 0;
2287 return 1;
2290 /* return true if type1 and type2 are the same. If unqualified is
2291 true, qualifiers on the types are ignored.
2293 - enums are not checked as gcc __builtin_types_compatible_p ()
2295 static int compare_types(CType *type1, CType *type2, int unqualified)
2297 int bt1, t1, t2;
2299 t1 = type1->t & VT_TYPE;
2300 t2 = type2->t & VT_TYPE;
2301 if (unqualified) {
2302 /* strip qualifiers before comparing */
2303 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2304 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2306 /* Default Vs explicit signedness only matters for char */
2307 if ((t1 & VT_BTYPE) != VT_BYTE) {
2308 t1 &= ~VT_DEFSIGN;
2309 t2 &= ~VT_DEFSIGN;
2311 /* XXX: bitfields ? */
2312 if (t1 != t2)
2313 return 0;
2314 /* test more complicated cases */
2315 bt1 = t1 & VT_BTYPE;
2316 if (bt1 == VT_PTR) {
2317 type1 = pointed_type(type1);
2318 type2 = pointed_type(type2);
2319 return is_compatible_types(type1, type2);
2320 } else if (bt1 == VT_STRUCT) {
2321 return (type1->ref == type2->ref);
2322 } else if (bt1 == VT_FUNC) {
2323 return is_compatible_func(type1, type2);
2324 } else {
2325 return 1;
2329 /* return true if type1 and type2 are exactly the same (including
2330 qualifiers).
2332 static int is_compatible_types(CType *type1, CType *type2)
2334 return compare_types(type1,type2,0);
2337 /* return true if type1 and type2 are the same (ignoring qualifiers).
2339 static int is_compatible_parameter_types(CType *type1, CType *type2)
2341 return compare_types(type1,type2,1);
2344 /* print a type. If 'varstr' is not NULL, then the variable is also
2345 printed in the type */
2346 /* XXX: union */
2347 /* XXX: add array and function pointers */
2348 static void type_to_str(char *buf, int buf_size,
2349 CType *type, const char *varstr)
2351 int bt, v, t;
2352 Sym *s, *sa;
2353 char buf1[256];
2354 const char *tstr;
2356 t = type->t & VT_TYPE;
2357 bt = t & VT_BTYPE;
2358 buf[0] = '\0';
2359 if (t & VT_CONSTANT)
2360 pstrcat(buf, buf_size, "const ");
2361 if (t & VT_VOLATILE)
2362 pstrcat(buf, buf_size, "volatile ");
2363 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2364 pstrcat(buf, buf_size, "unsigned ");
2365 else if (t & VT_DEFSIGN)
2366 pstrcat(buf, buf_size, "signed ");
2367 switch(bt) {
2368 case VT_VOID:
2369 tstr = "void";
2370 goto add_tstr;
2371 case VT_BOOL:
2372 tstr = "_Bool";
2373 goto add_tstr;
2374 case VT_BYTE:
2375 tstr = "char";
2376 goto add_tstr;
2377 case VT_SHORT:
2378 tstr = "short";
2379 goto add_tstr;
2380 case VT_INT:
2381 tstr = "int";
2382 goto add_tstr;
2383 case VT_LONG:
2384 tstr = "long";
2385 goto add_tstr;
2386 case VT_LLONG:
2387 tstr = "long long";
2388 goto add_tstr;
2389 case VT_FLOAT:
2390 tstr = "float";
2391 goto add_tstr;
2392 case VT_DOUBLE:
2393 tstr = "double";
2394 goto add_tstr;
2395 case VT_LDOUBLE:
2396 tstr = "long double";
2397 add_tstr:
2398 pstrcat(buf, buf_size, tstr);
2399 break;
2400 case VT_ENUM:
2401 case VT_STRUCT:
2402 if (bt == VT_STRUCT)
2403 tstr = "struct ";
2404 else
2405 tstr = "enum ";
2406 pstrcat(buf, buf_size, tstr);
2407 v = type->ref->v & ~SYM_STRUCT;
2408 if (v >= SYM_FIRST_ANOM)
2409 pstrcat(buf, buf_size, "<anonymous>");
2410 else
2411 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2412 break;
2413 case VT_FUNC:
2414 s = type->ref;
2415 type_to_str(buf, buf_size, &s->type, varstr);
2416 pstrcat(buf, buf_size, "(");
2417 sa = s->next;
2418 while (sa != NULL) {
2419 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2420 pstrcat(buf, buf_size, buf1);
2421 sa = sa->next;
2422 if (sa)
2423 pstrcat(buf, buf_size, ", ");
2425 pstrcat(buf, buf_size, ")");
2426 goto no_var;
2427 case VT_PTR:
2428 s = type->ref;
2429 pstrcpy(buf1, sizeof(buf1), "*");
2430 if (varstr)
2431 pstrcat(buf1, sizeof(buf1), varstr);
2432 type_to_str(buf, buf_size, &s->type, buf1);
2433 goto no_var;
2435 if (varstr) {
2436 pstrcat(buf, buf_size, " ");
2437 pstrcat(buf, buf_size, varstr);
2439 no_var: ;
2442 /* verify type compatibility to store vtop in 'dt' type, and generate
2443 casts if needed. */
2444 static void gen_assign_cast(CType *dt)
2446 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2447 char buf1[256], buf2[256];
2448 int dbt, sbt;
2450 st = &vtop->type; /* source type */
2451 dbt = dt->t & VT_BTYPE;
2452 sbt = st->t & VT_BTYPE;
2453 if (sbt == VT_VOID || dbt == VT_VOID)
2454 tcc_error("cannot cast from/to void");
2455 if (dt->t & VT_CONSTANT)
2456 tcc_warning("assignment of read-only location");
2457 switch(dbt) {
2458 case VT_PTR:
2459 /* special cases for pointers */
2460 /* '0' can also be a pointer */
2461 if (is_null_pointer(vtop))
2462 goto type_ok;
2463 /* accept implicit pointer to integer cast with warning */
2464 if (is_integer_btype(sbt)) {
2465 tcc_warning("assignment makes pointer from integer without a cast");
2466 goto type_ok;
2468 type1 = pointed_type(dt);
2469 /* a function is implicitely a function pointer */
2470 if (sbt == VT_FUNC) {
2471 if ((type1->t & VT_BTYPE) != VT_VOID &&
2472 !is_compatible_types(pointed_type(dt), st))
2473 tcc_warning("assignment from incompatible pointer type");
2474 goto type_ok;
2476 if (sbt != VT_PTR)
2477 goto error;
2478 type2 = pointed_type(st);
2479 if ((type1->t & VT_BTYPE) == VT_VOID ||
2480 (type2->t & VT_BTYPE) == VT_VOID) {
2481 /* void * can match anything */
2482 } else {
2483 /* exact type match, except for unsigned */
2484 tmp_type1 = *type1;
2485 tmp_type2 = *type2;
2486 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2487 VT_VOLATILE);
2488 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2489 VT_VOLATILE);
2490 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2491 tcc_warning("assignment from incompatible pointer type");
2493 /* check const and volatile */
2494 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2495 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2496 tcc_warning("assignment discards qualifiers from pointer target type");
2497 break;
2498 case VT_BYTE:
2499 case VT_SHORT:
2500 case VT_INT:
2501 case VT_LLONG:
2502 if (sbt == VT_PTR || sbt == VT_FUNC) {
2503 tcc_warning("assignment makes integer from pointer without a cast");
2505 if (sbt == VT_STRUCT)
2506 goto error;
2507 /* XXX: more tests */
2508 break;
2509 case VT_STRUCT:
2510 tmp_type1 = *dt;
2511 tmp_type2 = *st;
2512 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2513 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2514 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2515 error:
2516 type_to_str(buf1, sizeof(buf1), st, NULL);
2517 type_to_str(buf2, sizeof(buf2), dt, NULL);
2518 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2520 break;
2522 type_ok:
2523 gen_cast(dt);
2526 static void vstore_im(){
2527 int rc, ft, sbt, dbt, t, r;
2528 ft = vtop[-1].type.t;
2529 sbt = vtop->type.t & VT_BTYPE;
2530 dbt = ft & VT_BTYPE;
2531 if (is_float(ft)) {
2532 rc = RC_FLOAT;
2533 #ifdef TCC_TARGET_X86_64
2534 if (dbt == VT_LDOUBLE) {
2535 rc = RC_ST0;
2537 #endif
2538 }else
2539 rc = RC_INT;
2540 r = gv(rc); /* generate value */
2541 /* if lvalue was saved on stack, must read it */
2542 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2543 SValue sv;
2544 t = get_reg(RC_INT);
2545 #ifdef TCC_TARGET_X86_64
2546 sv.type.t = VT_PTR;
2547 #else
2548 sv.type.t = VT_INT;
2549 #endif
2550 sv.r = VT_LOCAL | VT_LVAL | VT_TMP;
2551 sv.c.ul = vtop[-1].c.ul;
2552 load(t, &sv);
2553 vtop[-1].r = t | VT_LVAL;
2554 vtop[-1].c.ul = 0;
2556 /* two word case handling : store second register at word + 4 */
2557 #ifdef TCC_TARGET_X86_64
2558 if ((dbt == VT_QLONG) || (dbt == VT_QFLOAT))
2559 #else
2560 if (dbt == VT_LLONG)
2561 #endif
2563 #ifdef TCC_TARGET_X86_64
2564 int load_size = 8, load_type = (sbt == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2565 #else
2566 int load_size = 4, load_type = VT_INT;
2567 #endif
2568 vtop[-1].type.t = load_type;
2569 store(r, vtop - 1);
2570 vswap();
2571 /* convert to int to increment easily */
2572 vtop->type = char_pointer_type;
2573 gaddrof();
2574 vpushi(load_size);
2575 gen_op('+');
2576 vtop->r |= VT_LVAL;
2577 vswap();
2578 vtop[-1].type.t = load_type;
2579 /* XXX: it works because r2 is spilled last ! */
2580 store(vtop->r2, vtop - 1);
2581 vtop->type.t = ft;
2582 vtop[-1].type.t = ft;
2583 } else {
2584 store(r, vtop - 1);
2588 /* store vtop in lvalue pushed on stack */
2589 ST_FUNC void vstore(void)
2591 int sbt, dbt, ft, size, align, bit_size, bit_pos, delayed_cast;
2593 ft = vtop[-1].type.t;
2594 sbt = vtop->type.t & VT_BTYPE;
2595 dbt = ft & VT_BTYPE;
2596 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2597 (sbt == VT_INT && dbt == VT_SHORT)) && !(vtop->type.t & VT_BITFIELD)) {
2598 /* optimize char/short casts */
2599 delayed_cast = VT_MUSTCAST;
2600 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2601 /* XXX: factorize */
2602 if (ft & VT_CONSTANT)
2603 tcc_warning("assignment of read-only location");
2604 } else {
2605 delayed_cast = 0;
2606 if (!(ft & VT_BITFIELD))
2607 gen_assign_cast(&vtop[-1].type);
2610 if (sbt == VT_STRUCT) {
2611 /* if structure, only generate pointer */
2612 /* structure assignment : generate memcpy */
2613 /* XXX: optimize if small size */
2614 if (!nocode_wanted) {
2615 SValue ret;
2616 int ret_nregs, ret_align;
2617 ret_nregs = gfunc_sret(&vtop->type, func_var, &ret.type, &ret_align);
2618 if(ret_nregs){
2619 vswap();
2620 vpushv(vtop - 1);
2621 vtop[0].type = ret.type;
2622 vtop[-1].type = ret.type;
2623 vstore_im();
2624 vtop -=2;
2625 }else{
2626 size = type_size(&vtop->type, &align);
2627 #ifndef TCC_TARGET_X86_64
2628 /* destination */
2629 vswap();
2630 vtop->type.t = VT_PTR;
2631 gaddrof();
2633 /* address of memcpy() */
2634 # ifdef TCC_ARM_EABI
2635 if(!(align & 7))
2636 vpush_global_sym(&func_old_type, TOK_memcpy8);
2637 else if(!(align & 3))
2638 vpush_global_sym(&func_old_type, TOK_memcpy4);
2639 else
2640 # endif
2641 vpush_global_sym(&func_old_type, TOK_memcpy);
2643 vswap();
2644 /* source */
2645 vpushv(vtop - 2);
2646 vtop->type.t = VT_PTR;
2647 gaddrof();
2648 /* type size */
2649 vpushs(size);
2650 gfunc_call(3);
2651 #else
2652 /* destination */
2653 vswap();
2654 vtop->type.t = VT_PTR;
2655 gaddrof();
2656 /* source */
2657 vpushv(vtop - 1);
2658 vtop->type.t = VT_PTR;
2659 gaddrof();
2660 /* size */
2661 vpushs(size);
2662 struct_copy(&vtop[-2], &vtop[-1], &vtop[0]);
2663 vtop -=3;
2664 #endif
2666 } else {
2667 vswap();
2668 vpop();
2670 /* leave source on stack */
2671 } else if (ft & VT_BITFIELD) {
2672 /* bitfield store handling */
2673 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2674 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2675 /* remove bit field info to avoid loops */
2676 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2677 /* duplicate source into other register */
2678 if(dbt == VT_BOOL) {
2679 gen_cast(&vtop[-1].type);
2680 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2683 /* duplicate destination */
2684 vdup();
2685 vtop[-1] = vtop[-2];
2687 /* mask and shift source */
2688 if(dbt != VT_BOOL) {
2689 if(dbt == VT_LLONG) {
2690 vpushll((1ULL << bit_size) - 1ULL);
2691 } else {
2692 vpushi((1 << bit_size) - 1);
2694 gen_op('&');
2696 vpushi(bit_pos);
2697 gen_op(TOK_SHL);
2698 /* load destination, mask and or with source */
2699 vswap();
2700 if(dbt == VT_LLONG) {
2701 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2702 } else {
2703 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2705 gen_op('&');
2706 gen_op('|');
2707 /* store result */
2708 vstore();
2709 } else {
2710 #ifdef CONFIG_TCC_BCHECK
2711 /* bound check case */
2712 if (vtop[-1].r & VT_MUSTBOUND) {
2713 vswap();
2714 gbound();
2715 vswap();
2717 #endif
2718 if (!nocode_wanted) {
2719 vstore_im();
2721 vswap();
2722 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2723 vtop->r |= delayed_cast;
2727 /* post defines POST/PRE add. c is the token ++ or -- */
2728 ST_FUNC void inc(int post, int c)
2730 test_lvalue();
2731 vdup(); /* save lvalue */
2732 if (post) {
2733 gv_dup(); /* duplicate value */
2734 vrotb(3);
2735 vrotb(3);
2737 /* add constant */
2738 vpushi(c - TOK_MID);
2739 gen_op('+');
2740 vstore(); /* store value */
2741 if (post)
2742 vpop(); /* if post op, return saved value */
2745 /* Parse GNUC __attribute__ extension. Currently, the following
2746 extensions are recognized:
2747 - aligned(n) : set data/function alignment.
2748 - packed : force data alignment to 1
2749 - section(x) : generate data/code in this section.
2750 - unused : currently ignored, but may be used someday.
2751 - regparm(n) : pass function parameters in registers (i386 only)
2753 static void parse_attribute(AttributeDef *ad)
2755 int t, n;
2757 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2758 next();
2759 skip('(');
2760 skip('(');
2761 while (tok != ')') {
2762 if (tok < TOK_IDENT)
2763 expect("attribute name");
2764 t = tok;
2765 next();
2766 switch(t) {
2767 case TOK_SECTION1:
2768 case TOK_SECTION2:
2769 skip('(');
2770 if (tok != TOK_STR)
2771 expect("section name");
2772 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2773 next();
2774 skip(')');
2775 break;
2776 case TOK_ALIAS1:
2777 case TOK_ALIAS2:
2778 skip('(');
2779 if (tok != TOK_STR)
2780 expect("alias(\"target\")");
2781 ad->alias_target = /* save string as token, for later */
2782 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2783 next();
2784 skip(')');
2785 break;
2786 case TOK_VISIBILITY1:
2787 case TOK_VISIBILITY2:
2788 skip('(');
2789 if (tok != TOK_STR)
2790 expect("visibility(\"default|hidden|internal|protected\")");
2791 if (!strcmp (tokc.cstr->data, "default"))
2792 ad->a.visibility = STV_DEFAULT;
2793 else if (!strcmp (tokc.cstr->data, "hidden"))
2794 ad->a.visibility = STV_HIDDEN;
2795 else if (!strcmp (tokc.cstr->data, "internal"))
2796 ad->a.visibility = STV_INTERNAL;
2797 else if (!strcmp (tokc.cstr->data, "protected"))
2798 ad->a.visibility = STV_PROTECTED;
2799 else
2800 expect("visibility(\"default|hidden|internal|protected\")");
2801 next();
2802 skip(')');
2803 break;
2804 case TOK_ALIGNED1:
2805 case TOK_ALIGNED2:
2806 if (tok == '(') {
2807 next();
2808 n = expr_const();
2809 if (n <= 0 || (n & (n - 1)) != 0)
2810 tcc_error("alignment must be a positive power of two");
2811 skip(')');
2812 } else {
2813 n = MAX_ALIGN;
2815 ad->a.aligned = n;
2816 break;
2817 case TOK_PACKED1:
2818 case TOK_PACKED2:
2819 ad->a.packed = 1;
2820 break;
2821 case TOK_WEAK1:
2822 case TOK_WEAK2:
2823 ad->a.weak = 1;
2824 break;
2825 case TOK_UNUSED1:
2826 case TOK_UNUSED2:
2827 /* currently, no need to handle it because tcc does not
2828 track unused objects */
2829 break;
2830 case TOK_NORETURN1:
2831 case TOK_NORETURN2:
2832 /* currently, no need to handle it because tcc does not
2833 track unused objects */
2834 break;
2835 case TOK_CDECL1:
2836 case TOK_CDECL2:
2837 case TOK_CDECL3:
2838 ad->a.func_call = FUNC_CDECL;
2839 break;
2840 case TOK_STDCALL1:
2841 case TOK_STDCALL2:
2842 case TOK_STDCALL3:
2843 ad->a.func_call = FUNC_STDCALL;
2844 break;
2845 #ifdef TCC_TARGET_I386
2846 case TOK_REGPARM1:
2847 case TOK_REGPARM2:
2848 skip('(');
2849 n = expr_const();
2850 if (n > 3)
2851 n = 3;
2852 else if (n < 0)
2853 n = 0;
2854 if (n > 0)
2855 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2856 skip(')');
2857 break;
2858 case TOK_FASTCALL1:
2859 case TOK_FASTCALL2:
2860 case TOK_FASTCALL3:
2861 ad->a.func_call = FUNC_FASTCALLW;
2862 break;
2863 #endif
2864 case TOK_MODE:
2865 skip('(');
2866 switch(tok) {
2867 case TOK_MODE_DI:
2868 ad->a.mode = VT_LLONG + 1;
2869 break;
2870 case TOK_MODE_HI:
2871 ad->a.mode = VT_SHORT + 1;
2872 break;
2873 case TOK_MODE_SI:
2874 ad->a.mode = VT_INT + 1;
2875 break;
2876 default:
2877 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2878 break;
2880 next();
2881 skip(')');
2882 break;
2883 case TOK_DLLEXPORT:
2884 ad->a.func_export = 1;
2885 break;
2886 case TOK_DLLIMPORT:
2887 ad->a.func_import = 1;
2888 break;
2889 default:
2890 if (tcc_state->warn_unsupported)
2891 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2892 /* skip parameters */
2893 if (tok == '(') {
2894 int parenthesis = 0;
2895 do {
2896 if (tok == '(')
2897 parenthesis++;
2898 else if (tok == ')')
2899 parenthesis--;
2900 next();
2901 } while (parenthesis && tok != -1);
2903 break;
2905 if (tok != ',')
2906 break;
2907 next();
2909 skip(')');
2910 skip(')');
2914 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2915 static void struct_decl(CType *type, int u, int tdef)
2917 int a, v, size, align, maxalign, c, offset, flexible;
2918 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2919 Sym *s, *ss, *ass, **ps;
2920 AttributeDef ad;
2921 CType type1, btype;
2923 a = tok; /* save decl type */
2924 next();
2925 if (tok != '{') {
2926 v = tok;
2927 next();
2928 /* struct already defined ? return it */
2929 if (v < TOK_IDENT)
2930 expect("struct/union/enum name");
2931 s = struct_find(v);
2932 if (s) {
2933 if (s->type.t != a)
2934 tcc_error("invalid type");
2935 goto do_decl;
2936 } else if (tok >= TOK_IDENT && !tdef)
2937 tcc_error("unknown struct/union/enum");
2938 } else {
2939 v = anon_sym++;
2941 type1.t = a;
2942 type1.ref = NULL;
2943 /* we put an undefined size for struct/union */
2944 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2945 s->r = 0; /* default alignment is zero as gcc */
2946 /* put struct/union/enum name in type */
2947 do_decl:
2948 type->t = u;
2949 type->ref = s;
2951 if (tok == '{') {
2952 next();
2953 if (s->c != -1)
2954 tcc_error("struct/union/enum already defined");
2955 /* cannot be empty */
2956 c = 0;
2957 /* non empty enums are not allowed */
2958 if (a == TOK_ENUM) {
2959 for(;;) {
2960 v = tok;
2961 if (v < TOK_UIDENT)
2962 expect("identifier");
2963 ss = sym_find(v);
2964 if (ss && !local_stack)
2965 tcc_error("redefinition of enumerator '%s'",
2966 get_tok_str(v, NULL));
2967 next();
2968 if (tok == '=') {
2969 next();
2970 c = expr_const();
2972 /* enum symbols have static storage */
2973 ss = sym_push(v, &int_type, VT_CONST, c);
2974 ss->type.t |= VT_STATIC;
2975 if (tok != ',')
2976 break;
2977 next();
2978 c++;
2979 /* NOTE: we accept a trailing comma */
2980 if (tok == '}')
2981 break;
2983 s->c = type_size(&int_type, &align);
2984 skip('}');
2985 } else {
2986 maxalign = 1;
2987 ps = &s->next;
2988 prevbt = VT_INT;
2989 bit_pos = 0;
2990 offset = 0;
2991 flexible = 0;
2992 while (tok != '}') {
2993 parse_btype(&btype, &ad);
2994 while (1) {
2995 if (flexible)
2996 tcc_error("flexible array member '%s' not at the end of struct",
2997 get_tok_str(v, NULL));
2998 bit_size = -1;
2999 v = 0;
3000 type1 = btype;
3001 if (tok != ':') {
3002 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
3003 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
3004 expect("identifier");
3005 if (type_size(&type1, &align) < 0) {
3006 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
3007 flexible = 1;
3008 else
3009 tcc_error("field '%s' has incomplete type",
3010 get_tok_str(v, NULL));
3012 if ((type1.t & VT_BTYPE) == VT_FUNC ||
3013 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
3014 tcc_error("invalid type for '%s'",
3015 get_tok_str(v, NULL));
3017 if (tok == ':') {
3018 next();
3019 bit_size = expr_const();
3020 /* XXX: handle v = 0 case for messages */
3021 if (bit_size < 0)
3022 tcc_error("negative width in bit-field '%s'",
3023 get_tok_str(v, NULL));
3024 if (v && bit_size == 0)
3025 tcc_error("zero width for bit-field '%s'",
3026 get_tok_str(v, NULL));
3028 size = type_size(&type1, &align);
3029 if (ad.a.aligned) {
3030 if (align < ad.a.aligned)
3031 align = ad.a.aligned;
3032 } else if (ad.a.packed) {
3033 align = 1;
3034 } else if (*tcc_state->pack_stack_ptr) {
3035 if (align > *tcc_state->pack_stack_ptr)
3036 align = *tcc_state->pack_stack_ptr;
3038 lbit_pos = 0;
3039 if (bit_size >= 0) {
3040 bt = type1.t & VT_BTYPE;
3041 if (bt != VT_INT &&
3042 bt != VT_BYTE &&
3043 bt != VT_SHORT &&
3044 bt != VT_BOOL &&
3045 bt != VT_ENUM &&
3046 bt != VT_LLONG)
3047 tcc_error("bitfields must have scalar type");
3048 bsize = size * 8;
3049 if (bit_size > bsize) {
3050 tcc_error("width of '%s' exceeds its type",
3051 get_tok_str(v, NULL));
3052 } else if (bit_size == bsize) {
3053 /* no need for bit fields */
3054 bit_pos = 0;
3055 } else if (bit_size == 0) {
3056 /* XXX: what to do if only padding in a
3057 structure ? */
3058 /* zero size: means to pad */
3059 bit_pos = 0;
3060 } else {
3061 /* we do not have enough room ?
3062 did the type change?
3063 is it a union? */
3064 if ((bit_pos + bit_size) > bsize ||
3065 bt != prevbt || a == TOK_UNION)
3066 bit_pos = 0;
3067 lbit_pos = bit_pos;
3068 /* XXX: handle LSB first */
3069 type1.t |= VT_BITFIELD |
3070 (bit_pos << VT_STRUCT_SHIFT) |
3071 (bit_size << (VT_STRUCT_SHIFT + 6));
3072 bit_pos += bit_size;
3074 prevbt = bt;
3075 } else {
3076 bit_pos = 0;
3078 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
3079 /* add new memory data only if starting
3080 bit field */
3081 if (lbit_pos == 0) {
3082 if (a == TOK_STRUCT) {
3083 c = (c + align - 1) & -align;
3084 offset = c;
3085 if (size > 0)
3086 c += size;
3087 } else {
3088 offset = 0;
3089 if (size > c)
3090 c = size;
3092 if (align > maxalign)
3093 maxalign = align;
3095 #if 0
3096 printf("add field %s offset=%d",
3097 get_tok_str(v, NULL), offset);
3098 if (type1.t & VT_BITFIELD) {
3099 printf(" pos=%d size=%d",
3100 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
3101 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
3103 printf("\n");
3104 #endif
3106 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
3107 ass = type1.ref;
3108 while ((ass = ass->next) != NULL) {
3109 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
3110 *ps = ss;
3111 ps = &ss->next;
3113 } else if (v) {
3114 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
3115 *ps = ss;
3116 ps = &ss->next;
3118 if (tok == ';' || tok == TOK_EOF)
3119 break;
3120 skip(',');
3122 skip(';');
3124 skip('}');
3125 /* store size and alignment */
3126 s->c = (c + maxalign - 1) & -maxalign;
3127 s->r = maxalign;
3132 /* return 1 if basic type is a type size (short, long, long long) */
3133 ST_FUNC int is_btype_size(int bt)
3135 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
3138 /* return 0 if no type declaration. otherwise, return the basic type
3139 and skip it.
3141 static int parse_btype(CType *type, AttributeDef *ad)
3143 int t, u, bt_size, complete, type_found, typespec_found;
3144 Sym *s;
3145 CType type1;
3147 memset(ad, 0, sizeof(AttributeDef));
3148 complete = 0;
3149 type_found = 0;
3150 typespec_found = 0;
3151 t = 0;
3152 while(1) {
3153 switch(tok) {
3154 case TOK_EXTENSION:
3155 /* currently, we really ignore extension */
3156 next();
3157 continue;
3159 /* basic types */
3160 case TOK_CHAR:
3161 u = VT_BYTE;
3162 basic_type:
3163 next();
3164 basic_type1:
3165 if (complete)
3166 tcc_error("too many basic types");
3167 t |= u;
3168 bt_size = is_btype_size (u & VT_BTYPE);
3169 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3170 complete = 1;
3171 typespec_found = 1;
3172 break;
3173 case TOK_VOID:
3174 u = VT_VOID;
3175 goto basic_type;
3176 case TOK_SHORT:
3177 u = VT_SHORT;
3178 goto basic_type;
3179 case TOK_INT:
3180 u = VT_INT;
3181 goto basic_type;
3182 case TOK_LONG:
3183 next();
3184 if ((t & VT_BTYPE) == VT_DOUBLE) {
3185 #ifndef TCC_TARGET_PE
3186 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3187 #endif
3188 } else if ((t & VT_BTYPE) == VT_LONG) {
3189 t = (t & ~VT_BTYPE) | VT_LLONG;
3190 } else {
3191 u = VT_LONG;
3192 goto basic_type1;
3194 break;
3195 case TOK_BOOL:
3196 u = VT_BOOL;
3197 goto basic_type;
3198 case TOK_FLOAT:
3199 u = VT_FLOAT;
3200 goto basic_type;
3201 case TOK_DOUBLE:
3202 next();
3203 if ((t & VT_BTYPE) == VT_LONG) {
3204 #ifdef TCC_TARGET_PE
3205 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3206 #else
3207 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3208 #endif
3209 } else {
3210 u = VT_DOUBLE;
3211 goto basic_type1;
3213 break;
3214 case TOK_ENUM:
3215 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3216 basic_type2:
3217 u = type1.t;
3218 type->ref = type1.ref;
3219 goto basic_type1;
3220 case TOK_STRUCT:
3221 case TOK_UNION:
3222 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3223 goto basic_type2;
3225 /* type modifiers */
3226 case TOK_CONST1:
3227 case TOK_CONST2:
3228 case TOK_CONST3:
3229 t |= VT_CONSTANT;
3230 next();
3231 break;
3232 case TOK_VOLATILE1:
3233 case TOK_VOLATILE2:
3234 case TOK_VOLATILE3:
3235 t |= VT_VOLATILE;
3236 next();
3237 break;
3238 case TOK_SIGNED1:
3239 case TOK_SIGNED2:
3240 case TOK_SIGNED3:
3241 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3242 tcc_error("signed and unsigned modifier");
3243 typespec_found = 1;
3244 t |= VT_DEFSIGN;
3245 next();
3246 break;
3247 case TOK_REGISTER:
3248 case TOK_AUTO:
3249 case TOK_RESTRICT1:
3250 case TOK_RESTRICT2:
3251 case TOK_RESTRICT3:
3252 next();
3253 break;
3254 case TOK_UNSIGNED:
3255 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3256 tcc_error("signed and unsigned modifier");
3257 t |= VT_DEFSIGN | VT_UNSIGNED;
3258 next();
3259 typespec_found = 1;
3260 break;
3262 /* storage */
3263 case TOK_EXTERN:
3264 t |= VT_EXTERN;
3265 next();
3266 break;
3267 case TOK_STATIC:
3268 t |= VT_STATIC;
3269 next();
3270 break;
3271 case TOK_TYPEDEF:
3272 t |= VT_TYPEDEF;
3273 next();
3274 break;
3275 case TOK_INLINE1:
3276 case TOK_INLINE2:
3277 case TOK_INLINE3:
3278 t |= VT_INLINE;
3279 next();
3280 break;
3282 /* GNUC attribute */
3283 case TOK_ATTRIBUTE1:
3284 case TOK_ATTRIBUTE2:
3285 parse_attribute(ad);
3286 if (ad->a.mode) {
3287 u = ad->a.mode -1;
3288 t = (t & ~VT_BTYPE) | u;
3290 break;
3291 /* GNUC typeof */
3292 case TOK_TYPEOF1:
3293 case TOK_TYPEOF2:
3294 case TOK_TYPEOF3:
3295 next();
3296 parse_expr_type(&type1);
3297 /* remove all storage modifiers except typedef */
3298 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3299 goto basic_type2;
3300 default:
3301 if (typespec_found)
3302 goto the_end;
3303 s = sym_find(tok);
3304 if (!s || !(s->type.t & VT_TYPEDEF))
3305 goto the_end;
3306 t |= (s->type.t & ~VT_TYPEDEF);
3307 type->ref = s->type.ref;
3308 if (s->r) {
3309 /* get attributes from typedef */
3310 if (0 == ad->a.aligned)
3311 ad->a.aligned = s->a.aligned;
3312 if (0 == ad->a.func_call)
3313 ad->a.func_call = s->a.func_call;
3314 ad->a.packed |= s->a.packed;
3316 next();
3317 typespec_found = 1;
3318 break;
3320 type_found = 1;
3322 the_end:
3323 if (tcc_state->char_is_unsigned) {
3324 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3325 t |= VT_UNSIGNED;
3328 /* long is never used as type */
3329 if ((t & VT_BTYPE) == VT_LONG)
3330 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3331 t = (t & ~VT_BTYPE) | VT_INT;
3332 #else
3333 t = (t & ~VT_BTYPE) | VT_LLONG;
3334 #endif
3335 type->t = t;
3336 return type_found;
3339 /* convert a function parameter type (array to pointer and function to
3340 function pointer) */
3341 static inline void convert_parameter_type(CType *pt)
3343 /* remove const and volatile qualifiers (XXX: const could be used
3344 to indicate a const function parameter */
3345 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3346 /* array must be transformed to pointer according to ANSI C */
3347 pt->t &= ~VT_ARRAY;
3348 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3349 mk_pointer(pt);
3353 ST_FUNC void parse_asm_str(CString *astr)
3355 skip('(');
3356 /* read the string */
3357 if (tok != TOK_STR)
3358 expect("string constant");
3359 cstr_new(astr);
3360 while (tok == TOK_STR) {
3361 /* XXX: add \0 handling too ? */
3362 cstr_cat(astr, tokc.cstr->data);
3363 next();
3365 cstr_ccat(astr, '\0');
3368 /* Parse an asm label and return the label
3369 * Don't forget to free the CString in the caller! */
3370 static void asm_label_instr(CString *astr)
3372 next();
3373 parse_asm_str(astr);
3374 skip(')');
3375 #ifdef ASM_DEBUG
3376 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3377 #endif
3380 static void post_type(CType *type, AttributeDef *ad)
3382 int n, l, t1, arg_size, size, align;
3383 Sym **plast, *s, *first;
3384 AttributeDef ad1;
3385 CType pt;
3387 if (tok == '(') {
3388 /* function declaration */
3389 next();
3390 l = 0;
3391 first = NULL;
3392 plast = &first;
3393 arg_size = 0;
3394 if (tok != ')') {
3395 for(;;) {
3396 /* read param name and compute offset */
3397 if (l != FUNC_OLD) {
3398 if (!parse_btype(&pt, &ad1)) {
3399 if (l) {
3400 tcc_error("invalid type");
3401 } else {
3402 l = FUNC_OLD;
3403 goto old_proto;
3406 l = FUNC_NEW;
3407 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3408 break;
3409 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3410 if ((pt.t & VT_BTYPE) == VT_VOID)
3411 tcc_error("parameter declared as void");
3412 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3413 } else {
3414 old_proto:
3415 n = tok;
3416 if (n < TOK_UIDENT)
3417 expect("identifier");
3418 pt.t = VT_INT;
3419 next();
3421 convert_parameter_type(&pt);
3422 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3423 *plast = s;
3424 plast = &s->next;
3425 if (tok == ')')
3426 break;
3427 skip(',');
3428 if (l == FUNC_NEW && tok == TOK_DOTS) {
3429 l = FUNC_ELLIPSIS;
3430 next();
3431 break;
3435 /* if no parameters, then old type prototype */
3436 if (l == 0)
3437 l = FUNC_OLD;
3438 skip(')');
3439 /* NOTE: const is ignored in returned type as it has a special
3440 meaning in gcc / C++ */
3441 type->t &= ~VT_CONSTANT;
3442 /* some ancient pre-K&R C allows a function to return an array
3443 and the array brackets to be put after the arguments, such
3444 that "int c()[]" means something like "int[] c()" */
3445 if (tok == '[') {
3446 next();
3447 skip(']'); /* only handle simple "[]" */
3448 type->t |= VT_PTR;
3450 /* we push a anonymous symbol which will contain the function prototype */
3451 ad->a.func_args = arg_size;
3452 s = sym_push(SYM_FIELD, type, 0, l);
3453 s->a = ad->a;
3454 s->next = first;
3455 type->t = VT_FUNC;
3456 type->ref = s;
3457 } else if (tok == '[') {
3458 /* array definition */
3459 next();
3460 if (tok == TOK_RESTRICT1)
3461 next();
3462 n = -1;
3463 t1 = 0;
3464 if (tok != ']') {
3465 if (!local_stack || nocode_wanted)
3466 vpushi(expr_const());
3467 else gexpr();
3468 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3469 n = vtop->c.i;
3470 if (n < 0)
3471 tcc_error("invalid array size");
3472 } else {
3473 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3474 tcc_error("size of variable length array should be an integer");
3475 t1 = VT_VLA;
3478 skip(']');
3479 /* parse next post type */
3480 post_type(type, ad);
3481 if (type->t == VT_FUNC)
3482 tcc_error("declaration of an array of functions");
3483 t1 |= type->t & VT_VLA;
3485 if (t1 & VT_VLA) {
3486 size = type_size(&int_type, &align);
3487 n = loc_stack(size, 1);
3489 vla_runtime_type_size(type, &align);
3490 gen_op('*');
3491 vset(&int_type, VT_LOCAL|VT_LVAL, n);
3492 vswap();
3493 vstore();
3495 if (n != -1)
3496 vpop();
3498 /* we push an anonymous symbol which will contain the array
3499 element type */
3500 s = sym_push(SYM_FIELD, type, 0, n);
3501 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3502 type->ref = s;
3506 /* Parse a type declaration (except basic type), and return the type
3507 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3508 expected. 'type' should contain the basic type. 'ad' is the
3509 attribute definition of the basic type. It can be modified by
3510 type_decl().
3512 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3514 Sym *s;
3515 CType type1, *type2;
3516 int qualifiers, storage;
3518 while (tok == '*') {
3519 qualifiers = 0;
3520 redo:
3521 next();
3522 switch(tok) {
3523 case TOK_CONST1:
3524 case TOK_CONST2:
3525 case TOK_CONST3:
3526 qualifiers |= VT_CONSTANT;
3527 goto redo;
3528 case TOK_VOLATILE1:
3529 case TOK_VOLATILE2:
3530 case TOK_VOLATILE3:
3531 qualifiers |= VT_VOLATILE;
3532 goto redo;
3533 case TOK_RESTRICT1:
3534 case TOK_RESTRICT2:
3535 case TOK_RESTRICT3:
3536 goto redo;
3538 mk_pointer(type);
3539 type->t |= qualifiers;
3542 /* XXX: clarify attribute handling */
3543 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3544 parse_attribute(ad);
3546 /* recursive type */
3547 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3548 type1.t = 0; /* XXX: same as int */
3549 if (tok == '(') {
3550 next();
3551 /* XXX: this is not correct to modify 'ad' at this point, but
3552 the syntax is not clear */
3553 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3554 parse_attribute(ad);
3555 type_decl(&type1, ad, v, td);
3556 skip(')');
3557 } else {
3558 /* type identifier */
3559 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3560 *v = tok;
3561 next();
3562 } else {
3563 if (!(td & TYPE_ABSTRACT))
3564 expect("identifier");
3565 *v = 0;
3568 storage = type->t & VT_STORAGE;
3569 type->t &= ~VT_STORAGE;
3570 if (storage & VT_STATIC) {
3571 int saved_nocode_wanted = nocode_wanted;
3572 nocode_wanted = 1;
3573 post_type(type, ad);
3574 nocode_wanted = saved_nocode_wanted;
3575 } else
3576 post_type(type, ad);
3577 type->t |= storage;
3578 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3579 parse_attribute(ad);
3581 if (!type1.t)
3582 return;
3583 /* append type at the end of type1 */
3584 type2 = &type1;
3585 for(;;) {
3586 s = type2->ref;
3587 type2 = &s->type;
3588 if (!type2->t) {
3589 *type2 = *type;
3590 break;
3593 *type = type1;
3596 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3597 ST_FUNC int lvalue_type(int t)
3599 int bt, r;
3600 r = VT_LVAL;
3601 bt = t & VT_BTYPE;
3602 if (bt == VT_BYTE || bt == VT_BOOL)
3603 r |= VT_LVAL_BYTE;
3604 else if (bt == VT_SHORT)
3605 r |= VT_LVAL_SHORT;
3606 else
3607 return r;
3608 if (t & VT_UNSIGNED)
3609 r |= VT_LVAL_UNSIGNED;
3610 return r;
3613 /* indirection with full error checking and bound check */
3614 ST_FUNC void indir(void)
3616 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3617 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3618 return;
3619 expect("pointer");
3621 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3622 gv(RC_INT);
3623 vtop->type = *pointed_type(&vtop->type);
3624 /* Arrays and functions are never lvalues */
3625 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3626 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3627 vtop->r |= lvalue_type(vtop->type.t);
3628 /* if bound checking, the referenced pointer must be checked */
3629 #ifdef CONFIG_TCC_BCHECK
3630 if (tcc_state->do_bounds_check)
3631 vtop->r |= VT_MUSTBOUND;
3632 #endif
3636 /* pass a parameter to a function and do type checking and casting */
3637 static void gfunc_param_typed(Sym *func, Sym *arg)
3639 int func_type;
3640 CType type;
3642 func_type = func->c;
3643 if (func_type == FUNC_OLD ||
3644 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3645 /* default casting : only need to convert float to double */
3646 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3647 type.t = VT_DOUBLE;
3648 gen_cast(&type);
3649 } else if (vtop->type.t & VT_BITFIELD) {
3650 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3651 gen_cast(&type);
3653 } else if (arg == NULL) {
3654 tcc_error("too many arguments to function");
3655 } else {
3656 type = arg->type;
3657 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3658 gen_assign_cast(&type);
3662 /* parse an expression of the form '(type)' or '(expr)' and return its
3663 type */
3664 static void parse_expr_type(CType *type)
3666 int n;
3667 AttributeDef ad;
3669 skip('(');
3670 if (parse_btype(type, &ad)) {
3671 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3672 } else {
3673 expr_type(type);
3675 skip(')');
3678 static void parse_type(CType *type)
3680 AttributeDef ad;
3681 int n;
3683 if (!parse_btype(type, &ad)) {
3684 expect("type");
3686 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3689 static void vpush_tokc(int t)
3691 CType type;
3692 type.t = t;
3693 type.ref = 0;
3694 vsetc(&type, VT_CONST, &tokc);
3697 ST_FUNC void unary(void)
3699 int n, t, align, size, r, sizeof_caller;
3700 CType type;
3701 Sym *s;
3702 AttributeDef ad;
3703 static int in_sizeof = 0;
3705 sizeof_caller = in_sizeof;
3706 in_sizeof = 0;
3707 /* XXX: GCC 2.95.3 does not generate a table although it should be
3708 better here */
3709 tok_next:
3710 switch(tok) {
3711 case TOK_EXTENSION:
3712 next();
3713 goto tok_next;
3714 case TOK_CINT:
3715 case TOK_CCHAR:
3716 case TOK_LCHAR:
3717 vpushi(tokc.i);
3718 next();
3719 break;
3720 case TOK_CUINT:
3721 vpush_tokc(VT_INT | VT_UNSIGNED);
3722 next();
3723 break;
3724 case TOK_CLLONG:
3725 vpush_tokc(VT_LLONG);
3726 next();
3727 break;
3728 case TOK_CULLONG:
3729 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3730 next();
3731 break;
3732 case TOK_CFLOAT:
3733 vpush_tokc(VT_FLOAT);
3734 next();
3735 break;
3736 case TOK_CDOUBLE:
3737 vpush_tokc(VT_DOUBLE);
3738 next();
3739 break;
3740 case TOK_CLDOUBLE:
3741 vpush_tokc(VT_LDOUBLE);
3742 next();
3743 break;
3744 case TOK___FUNCTION__:
3745 if (!gnu_ext)
3746 goto tok_identifier;
3747 /* fall thru */
3748 case TOK___FUNC__:
3750 void *ptr;
3751 int len;
3752 /* special function name identifier */
3753 len = strlen(funcname) + 1;
3754 /* generate char[len] type */
3755 type.t = VT_BYTE;
3756 mk_pointer(&type);
3757 type.t |= VT_ARRAY;
3758 type.ref->c = len;
3759 vpush_ref(&type, data_section, data_section->data_offset, len);
3760 ptr = section_ptr_add(data_section, len);
3761 memcpy(ptr, funcname, len);
3762 next();
3764 break;
3765 case TOK_LSTR:
3766 #ifdef TCC_TARGET_PE
3767 t = VT_SHORT | VT_UNSIGNED;
3768 #else
3769 t = VT_INT;
3770 #endif
3771 goto str_init;
3772 case TOK_STR:
3773 /* string parsing */
3774 t = VT_BYTE;
3775 str_init:
3776 if (tcc_state->warn_write_strings)
3777 t |= VT_CONSTANT;
3778 type.t = t;
3779 mk_pointer(&type);
3780 type.t |= VT_ARRAY;
3781 memset(&ad, 0, sizeof(AttributeDef));
3782 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3783 break;
3784 case '(':
3785 next();
3786 /* cast ? */
3787 if (parse_btype(&type, &ad)) {
3788 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3789 skip(')');
3790 /* check ISOC99 compound literal */
3791 if (tok == '{') {
3792 /* data is allocated locally by default */
3793 if (global_expr)
3794 r = VT_CONST;
3795 else
3796 r = VT_LOCAL;
3797 /* all except arrays are lvalues */
3798 if (!(type.t & VT_ARRAY))
3799 r |= lvalue_type(type.t);
3800 memset(&ad, 0, sizeof(AttributeDef));
3801 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3802 } else {
3803 if (sizeof_caller) {
3804 vpush(&type);
3805 return;
3807 unary();
3808 gen_cast(&type);
3810 } else if (tok == '{') {
3811 /* save all registers */
3812 save_regs(0);
3813 /* statement expression : we do not accept break/continue
3814 inside as GCC does */
3815 block(NULL, NULL, NULL, NULL, 0, 1);
3816 skip(')');
3817 } else {
3818 gexpr();
3819 skip(')');
3821 break;
3822 case '*':
3823 next();
3824 unary();
3825 indir();
3826 break;
3827 case '&':
3828 next();
3829 unary();
3830 /* functions names must be treated as function pointers,
3831 except for unary '&' and sizeof. Since we consider that
3832 functions are not lvalues, we only have to handle it
3833 there and in function calls. */
3834 /* arrays can also be used although they are not lvalues */
3835 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3836 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3837 test_lvalue();
3838 mk_pointer(&vtop->type);
3839 gaddrof();
3840 break;
3841 case '!':
3842 next();
3843 unary();
3844 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3845 CType boolean;
3846 boolean.t = VT_BOOL;
3847 gen_cast(&boolean);
3848 vtop->c.i = !vtop->c.i;
3849 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3850 vtop->c.i = vtop->c.i ^ 1;
3851 else {
3852 save_regs(1);
3853 vseti(VT_JMP, gtst(1, 0));
3855 break;
3856 case '~':
3857 next();
3858 unary();
3859 vpushi(-1);
3860 gen_op('^');
3861 break;
3862 case '+':
3863 next();
3864 unary();
3865 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3866 tcc_error("pointer not accepted for unary plus");
3867 /* In order to force cast, we add zero, except for floating point
3868 where we really need an noop (otherwise -0.0 will be transformed
3869 into +0.0). */
3870 if (!is_float(vtop->type.t)) {
3871 vpushi(0);
3872 gen_op('+');
3874 break;
3875 case TOK_SIZEOF:
3876 case TOK_ALIGNOF1:
3877 case TOK_ALIGNOF2:
3878 t = tok;
3879 next();
3880 in_sizeof++;
3881 unary_type(&type); // Perform a in_sizeof = 0;
3882 size = type_size(&type, &align);
3883 if (t == TOK_SIZEOF) {
3884 if (!(type.t & VT_VLA)) {
3885 if (size < 0)
3886 tcc_error("sizeof applied to an incomplete type");
3887 vpushs(size);
3888 } else {
3889 vla_runtime_type_size(&type, &align);
3891 } else {
3892 vpushs(align);
3894 vtop->type.t |= VT_UNSIGNED;
3895 break;
3897 case TOK_builtin_types_compatible_p:
3899 CType type1, type2;
3900 next();
3901 skip('(');
3902 parse_type(&type1);
3903 skip(',');
3904 parse_type(&type2);
3905 skip(')');
3906 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3907 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3908 vpushi(is_compatible_types(&type1, &type2));
3910 break;
3911 case TOK_builtin_constant_p:
3913 int saved_nocode_wanted, res;
3914 next();
3915 skip('(');
3916 saved_nocode_wanted = nocode_wanted;
3917 nocode_wanted = 1;
3918 gexpr();
3919 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3920 vpop();
3921 nocode_wanted = saved_nocode_wanted;
3922 skip(')');
3923 vpushi(res);
3925 break;
3926 case TOK_builtin_frame_address:
3928 int level;
3929 CType type;
3930 next();
3931 skip('(');
3932 if (tok != TOK_CINT || tokc.i < 0) {
3933 tcc_error("__builtin_frame_address only takes positive integers");
3935 level = tokc.i;
3936 next();
3937 skip(')');
3938 type.t = VT_VOID;
3939 mk_pointer(&type);
3940 vset(&type, VT_LOCAL, 0); /* local frame */
3941 while (level--) {
3942 mk_pointer(&vtop->type);
3943 indir(); /* -> parent frame */
3946 break;
3947 #ifdef TCC_TARGET_X86_64
3948 #ifdef TCC_TARGET_PE
3949 case TOK_builtin_va_start:
3951 next();
3952 skip('(');
3953 expr_eq();
3954 skip(',');
3955 expr_eq();
3956 skip(')');
3957 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3958 tcc_error("__builtin_va_start expects a local variable");
3959 vtop->r &= ~(VT_LVAL | VT_REF);
3960 vtop->type = char_pointer_type;
3961 vstore();
3963 break;
3964 #else
3965 case TOK_builtin_va_arg_types:
3967 CType type;
3968 next();
3969 skip('(');
3970 parse_type(&type);
3971 skip(')');
3972 vpushi(classify_x86_64_va_arg(&type));
3974 break;
3975 #endif
3976 #endif
3977 case TOK_INC:
3978 case TOK_DEC:
3979 t = tok;
3980 next();
3981 unary();
3982 inc(0, t);
3983 break;
3984 case '-':
3985 next();
3986 unary();
3987 t = vtop->type.t & VT_BTYPE;
3988 if (is_float(t)) {
3989 /* In IEEE negate(x) isn't subtract(0,x), but rather
3990 subtract(-0, x). */
3991 vpush(&vtop->type);
3992 if (t == VT_FLOAT)
3993 vtop->c.f = -0.0f;
3994 else if (t == VT_DOUBLE)
3995 vtop->c.d = -0.0;
3996 else
3997 vtop->c.ld = -0.0;
3998 } else
3999 vpushi(0);
4000 vswap();
4001 gen_op('-');
4002 break;
4003 case TOK_LAND:
4004 if (!gnu_ext)
4005 goto tok_identifier;
4006 next();
4007 /* allow to take the address of a label */
4008 if (tok < TOK_UIDENT)
4009 expect("label identifier");
4010 s = label_find(tok);
4011 if (!s) {
4012 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4013 } else {
4014 if (s->r == LABEL_DECLARED)
4015 s->r = LABEL_FORWARD;
4017 if (!s->type.t) {
4018 s->type.t = VT_VOID;
4019 mk_pointer(&s->type);
4020 s->type.t |= VT_STATIC;
4022 vpushsym(&s->type, s);
4023 next();
4024 break;
4026 // special qnan , snan and infinity values
4027 case TOK___NAN__:
4028 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
4029 next();
4030 break;
4031 case TOK___SNAN__:
4032 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
4033 next();
4034 break;
4035 case TOK___INF__:
4036 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
4037 next();
4038 break;
4040 default:
4041 tok_identifier:
4042 t = tok;
4043 next();
4044 if (t < TOK_UIDENT)
4045 expect("identifier");
4046 s = sym_find(t);
4047 if (!s) {
4048 const char *name = get_tok_str(t, NULL);
4049 if (tok != '(')
4050 tcc_error("'%s' undeclared", name);
4051 /* for simple function calls, we tolerate undeclared
4052 external reference to int() function */
4053 if (tcc_state->warn_implicit_function_declaration
4054 #ifdef TCC_TARGET_PE
4055 /* people must be warned about using undeclared WINAPI functions
4056 (which usually start with uppercase letter) */
4057 || (name[0] >= 'A' && name[0] <= 'Z')
4058 #endif
4060 tcc_warning("implicit declaration of function '%s'", name);
4061 s = external_global_sym(t, &func_old_type, 0);
4063 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
4064 (VT_STATIC | VT_INLINE | VT_FUNC)) {
4065 /* if referencing an inline function, then we generate a
4066 symbol to it if not already done. It will have the
4067 effect to generate code for it at the end of the
4068 compilation unit. Inline function as always
4069 generated in the text section. */
4070 if (!s->c)
4071 put_extern_sym(s, text_section, 0, 0);
4072 r = VT_SYM | VT_CONST;
4073 } else {
4074 r = s->r;
4076 vset(&s->type, r, s->c);
4077 /* if forward reference, we must point to s */
4078 if (vtop->r & VT_SYM) {
4079 vtop->sym = s;
4080 vtop->c.ptr_offset = 0;
4082 break;
4085 /* post operations */
4086 while (1) {
4087 SValue ret;
4088 int ret_nregs, ret_align;
4089 if (tok == TOK_INC || tok == TOK_DEC) {
4090 inc(1, tok);
4091 next();
4092 } else if (tok == '.' || tok == TOK_ARROW) {
4093 int qualifiers, add, is_lval;
4094 /* field */
4095 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
4096 add = 0;
4097 if (tok == TOK_ARROW)
4098 indir();
4100 type = vtop->type;
4101 is_lval = (vtop->r & (VT_VALMASK | VT_LVAL)) >= VT_CONST;
4102 if(is_lval){
4103 test_lvalue();
4104 gaddrof();
4105 vtop->type = char_pointer_type; /* change type to 'char *' */
4106 }else
4107 gfunc_sret(&vtop->type, func_var, &ret.type, &ret_align);
4109 next();
4110 /* expect pointer on structure */
4111 if ((type.t & VT_BTYPE) != VT_STRUCT)
4112 expect("struct or union");
4113 s = type.ref;
4114 /* find field */
4115 tok |= SYM_FIELD;
4116 while ((s = s->next) != NULL) {
4117 if (s->v == tok)
4118 break;
4120 if (!s)
4121 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
4122 /* add bit */
4123 add += s->c;
4124 /* change type to field type, and set to lvalue */
4125 type = s->type;
4126 next();
4127 }while(tok == '.');
4129 type.t |= qualifiers;
4130 if (is_lval){
4131 p_lval:
4132 vpushi(add);
4133 gen_op('+');
4134 /* an array is never an lvalue */
4135 if (!(type.t & VT_ARRAY)) {
4136 vtop->r |= lvalue_type(type.t);
4137 #ifdef CONFIG_TCC_BCHECK
4138 /* if bound checking, the referenced pointer must be checked */
4139 if (tcc_state->do_bounds_check)
4140 vtop->r |= VT_MUSTBOUND;
4141 #endif
4143 }else{
4144 gfunc_sret(&vtop->type, func_var, &ret.type, &ret_align);
4145 if(is_float(ret.type.t) || (type.t & VT_ARRAY)){
4146 #ifdef TCC_TARGET_X86_64
4147 if((ret.type.t & VT_BTYPE) != VT_LDOUBLE)
4148 #endif
4150 save_reg(vtop->r);
4151 vtop->r &= ~VT_TMP;
4152 gaddrof();
4153 vtop->type = char_pointer_type; /* change type to 'char *' */
4154 goto p_lval;
4156 }else{
4157 #ifdef TCC_TARGET_X86_64
4158 int load_size = 8;
4159 #else
4160 int load_size = 4;
4161 #endif
4162 if(add & load_size){
4163 add -= load_size;
4164 vtop->r = vtop->r2;
4165 vtop->r2 = VT_CONST;
4167 if(add){
4168 vtop->type.t = VT_LLONG;
4169 vpushi(add*8);
4170 gen_op(TOK_SAR);
4174 vtop->type = type;
4175 } else if (tok == '[') {
4176 next();
4177 gexpr();
4178 gen_op('+');
4179 indir();
4180 skip(']');
4181 } else if (tok == '(') {
4182 Sym *sa;
4183 int nb_args, variadic, addr;
4185 /* function call */
4186 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
4187 /* pointer test (no array accepted) */
4188 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4189 vtop->type = *pointed_type(&vtop->type);
4190 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4191 goto error_func;
4192 } else {
4193 error_func:
4194 expect("function pointer");
4196 } else {
4197 vtop->r &= ~VT_LVAL; /* no lvalue */
4199 /* get return type */
4200 s = vtop->type.ref;
4201 sa = s->next; /* first parameter */
4202 nb_args = 0;
4203 ret.r2 = VT_CONST;
4204 /* compute first implicit argument if a structure is returned */
4205 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4206 variadic = (s->c == FUNC_ELLIPSIS);
4207 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4208 &ret_align);
4209 if (!ret_nregs) {
4210 /* get some space for the returned structure */
4211 size = type_size(&s->type, &align);
4212 addr = loc_stack(size, 1);
4213 ret.type = s->type;
4214 ret.r = VT_LOCAL | VT_LVAL;
4215 /* pass it as 'int' to avoid structure arg passing
4216 problems */
4217 vseti(VT_LOCAL, addr);
4218 ret.c = vtop->c;
4219 nb_args++;
4221 } else {
4222 ret_nregs = 1;
4223 ret.type = s->type;
4226 if (ret_nregs) {
4227 /* return in register */
4228 if (is_float(ret.type.t)) {
4229 ret.r = reg_fret(ret.type.t);
4230 #ifdef TCC_TARGET_X86_64
4231 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4232 ret.r2 = REG_QRET;
4233 #endif
4234 } else {
4235 #ifdef TCC_TARGET_X86_64
4236 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4237 #else
4238 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4239 #endif
4240 ret.r2 = REG_LRET;
4241 ret.r = REG_IRET;
4243 ret.c.i = 0;
4245 next();
4246 if (tok != ')') {
4247 for(;;) {
4248 expr_eq();
4249 gfunc_param_typed(s, sa);
4250 nb_args++;
4251 if (sa)
4252 sa = sa->next;
4253 if (tok == ')')
4254 break;
4255 skip(',');
4258 if (sa)
4259 tcc_error("too few arguments to function");
4260 skip(')');
4261 if (!nocode_wanted) {
4262 gfunc_call(nb_args);
4263 } else {
4264 vtop -= (nb_args + 1);
4267 /* return value */
4268 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4269 vsetc(&ret.type, r, &ret.c);
4270 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4273 /* handle packed struct return */
4274 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs)
4275 vtop->type = s->type;
4276 } else {
4277 break;
4282 ST_FUNC void expr_prod(void)
4284 int t;
4286 unary();
4287 while (tok == '*' || tok == '/' || tok == '%') {
4288 t = tok;
4289 next();
4290 unary();
4291 gen_op(t);
4295 ST_FUNC void expr_sum(void)
4297 int t;
4299 expr_prod();
4300 while (tok == '+' || tok == '-') {
4301 t = tok;
4302 next();
4303 expr_prod();
4304 gen_op(t);
4308 static void expr_shift(void)
4310 int t;
4312 expr_sum();
4313 while (tok == TOK_SHL || tok == TOK_SAR) {
4314 t = tok;
4315 next();
4316 expr_sum();
4317 gen_op(t);
4321 static void expr_cmp(void)
4323 int t;
4325 expr_shift();
4326 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4327 tok == TOK_ULT || tok == TOK_UGE) {
4328 t = tok;
4329 next();
4330 expr_shift();
4331 gen_op(t);
4335 static void expr_cmpeq(void)
4337 int t;
4339 expr_cmp();
4340 while (tok == TOK_EQ || tok == TOK_NE) {
4341 t = tok;
4342 next();
4343 expr_cmp();
4344 gen_op(t);
4348 static void expr_and(void)
4350 expr_cmpeq();
4351 while (tok == '&') {
4352 next();
4353 expr_cmpeq();
4354 gen_op('&');
4358 static void expr_xor(void)
4360 expr_and();
4361 while (tok == '^') {
4362 next();
4363 expr_and();
4364 gen_op('^');
4368 static void expr_or(void)
4370 expr_xor();
4371 while (tok == '|') {
4372 next();
4373 expr_xor();
4374 gen_op('|');
4378 /* XXX: fix this mess */
4379 static void expr_land_const(void)
4381 expr_or();
4382 while (tok == TOK_LAND) {
4383 next();
4384 expr_or();
4385 gen_op(TOK_LAND);
4389 /* XXX: fix this mess */
4390 static void expr_lor_const(void)
4392 expr_land_const();
4393 while (tok == TOK_LOR) {
4394 next();
4395 expr_land_const();
4396 gen_op(TOK_LOR);
4400 /* only used if non constant */
4401 static void expr_land(void)
4403 int t;
4405 expr_or();
4406 if (tok == TOK_LAND) {
4407 t = 0;
4408 save_regs(1);
4409 for(;;) {
4410 t = gtst(1, t);
4411 if (tok != TOK_LAND) {
4412 vseti(VT_JMPI, t);
4413 break;
4415 next();
4416 expr_or();
4421 static void expr_lor(void)
4423 int t;
4425 expr_land();
4426 if (tok == TOK_LOR) {
4427 t = 0;
4428 save_regs(1);
4429 for(;;) {
4430 t = gtst(0, t);
4431 if (tok != TOK_LOR) {
4432 vseti(VT_JMP, t);
4433 break;
4435 next();
4436 expr_land();
4441 /* XXX: better constant handling */
4442 static void expr_cond(void)
4444 int tt, u, r, rc, t1, t2, bt1, bt2, ret_nregs, ret_align;
4445 SValue sv, ret;
4446 CType type, type1, type2;
4448 if (const_wanted) {
4449 expr_lor_const();
4450 if (tok == '?') {
4451 CType boolean;
4452 int c;
4453 boolean.t = VT_BOOL;
4454 vdup();
4455 gen_cast(&boolean);
4456 c = vtop->c.i;
4457 vpop();
4458 next();
4459 if (tok != ':' || !gnu_ext) {
4460 vpop();
4461 gexpr();
4463 if (!c)
4464 vpop();
4465 skip(':');
4466 expr_cond();
4467 if (c)
4468 vpop();
4470 } else {
4471 expr_lor();
4472 if (tok == '?') {
4473 next();
4474 if (vtop != vstack) {
4475 /* needed to avoid having different registers saved in
4476 each branch */
4477 if (is_float(vtop->type.t)) {
4478 rc = RC_FLOAT;
4479 #ifdef TCC_TARGET_X86_64
4480 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4481 rc = RC_ST0;
4483 #endif
4485 else
4486 rc = RC_INT;
4487 save_regs(1);
4489 if (tok == ':' && gnu_ext) {
4490 gv_dup();
4491 tt = gtst(1, 0);
4492 } else {
4493 tt = gtst(1, 0);
4494 gexpr();
4496 type1 = vtop->type;
4497 sv = *vtop; /* save value to handle it later */
4498 vtop--; /* no vpop so that FP stack is not flushed */
4499 skip(':');
4500 u = gjmp(0);
4501 gsym(tt);
4502 expr_cond();
4503 type2 = vtop->type;
4505 t1 = type1.t;
4506 bt1 = t1 & VT_BTYPE;
4507 t2 = type2.t;
4508 bt2 = t2 & VT_BTYPE;
4509 /* cast operands to correct type according to ISOC rules */
4510 if (is_float(bt1) || is_float(bt2)) {
4511 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4512 type.t = VT_LDOUBLE;
4513 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4514 type.t = VT_DOUBLE;
4515 } else {
4516 type.t = VT_FLOAT;
4518 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4519 /* cast to biggest op */
4520 type.t = VT_LLONG;
4521 /* convert to unsigned if it does not fit in a long long */
4522 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4523 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4524 type.t |= VT_UNSIGNED;
4525 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4526 /* If one is a null ptr constant the result type is the other. */
4527 if (is_null_pointer (vtop))
4528 type = type1;
4529 else if (is_null_pointer (&sv))
4530 type = type2;
4531 /* XXX: test pointer compatibility, C99 has more elaborate rules here. */
4532 else
4533 type = type1;
4534 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4535 /* XXX: test function pointer compatibility */
4536 type = bt1 == VT_FUNC ? type1 : type2;
4537 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4538 /* XXX: test structure compatibility */
4539 type = bt1 == VT_STRUCT ? type1 : type2;
4540 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4541 /* NOTE: as an extension, we accept void on only one side */
4542 type.t = VT_VOID;
4543 } else {
4544 /* integer operations */
4545 type.t = VT_INT;
4546 /* convert to unsigned if it does not fit in an integer */
4547 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4548 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4549 type.t |= VT_UNSIGNED;
4552 /* now we convert second operand */
4553 gen_cast(&type);
4554 ret_nregs = 0;
4555 if (VT_STRUCT == (type.t & VT_BTYPE)){
4556 ret_nregs = gfunc_sret(&type, func_var, &ret.type, &ret_align);
4557 if(ret_nregs)
4558 vtop->type = ret.type;
4559 else
4560 gaddrof();
4563 if (is_float(vtop->type.t)) {
4564 rc = RC_FLOAT;
4565 #ifdef TCC_TARGET_X86_64
4566 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4567 rc = RC_ST0;
4569 #endif
4570 } else
4571 rc = RC_INT;
4572 r = gv(rc);
4573 rc = reg_classes[r] & ~RC_MASK;
4574 #ifdef TCC_TARGET_X86_64
4575 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT))
4576 #else
4577 if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
4578 #endif
4579 ex_rc = reg_classes[vtop->r2] & ~RC_MASK;
4580 /* this is horrible, but we must also convert first
4581 operand */
4582 tt = gjmp(0);
4583 gsym(u);
4584 /* put again first value and cast it */
4585 *vtop = sv;
4586 gen_cast(&type);
4587 if (VT_STRUCT == (type.t & VT_BTYPE)){
4588 if(ret_nregs)
4589 vtop->type = ret.type;
4590 else
4591 gaddrof();
4593 gv(rc);
4594 gsym(tt);
4596 if (VT_STRUCT == (type.t & VT_BTYPE)){
4597 if(ret_nregs)
4598 vtop->type = type;
4599 else
4600 vtop->r |= VT_LVAL;
4606 static void expr_eq(void)
4608 int t;
4610 expr_cond();
4611 if (tok == '=' ||
4612 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4613 tok == TOK_A_XOR || tok == TOK_A_OR ||
4614 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4615 test_lvalue();
4616 t = tok;
4617 next();
4618 if (t == '=') {
4619 expr_eq();
4620 } else {
4621 vdup();
4622 expr_eq();
4623 gen_op(t & 0x7f);
4625 vstore();
4629 ST_FUNC void gexpr(void)
4631 while (1) {
4632 expr_eq();
4633 if (tok != ',')
4634 break;
4635 vpop();
4636 next();
4640 /* parse an expression and return its type without any side effect. */
4641 static void expr_type(CType *type)
4643 int saved_nocode_wanted;
4645 saved_nocode_wanted = nocode_wanted;
4646 nocode_wanted = 1;
4647 gexpr();
4648 *type = vtop->type;
4649 vpop();
4650 nocode_wanted = saved_nocode_wanted;
4653 /* parse a unary expression and return its type without any side
4654 effect. */
4655 static void unary_type(CType *type)
4657 int a;
4659 a = nocode_wanted;
4660 nocode_wanted = 1;
4661 unary();
4662 *type = vtop->type;
4663 vpop();
4664 nocode_wanted = a;
4667 /* parse a constant expression and return value in vtop. */
4668 static void expr_const1(void)
4670 int a;
4671 a = const_wanted;
4672 const_wanted = 1;
4673 expr_cond();
4674 const_wanted = a;
4677 /* parse an integer constant and return its value. */
4678 ST_FUNC int expr_const(void)
4680 int c;
4681 expr_const1();
4682 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4683 expect("constant expression");
4684 c = vtop->c.i;
4685 vpop();
4686 return c;
4689 /* return the label token if current token is a label, otherwise
4690 return zero */
4691 static int is_label(void)
4693 int last_tok;
4695 /* fast test first */
4696 if (tok < TOK_UIDENT)
4697 return 0;
4698 /* no need to save tokc because tok is an identifier */
4699 last_tok = tok;
4700 next();
4701 if (tok == ':') {
4702 next();
4703 return last_tok;
4704 } else {
4705 unget_tok(last_tok);
4706 return 0;
4710 static void label_or_decl(int l)
4712 int last_tok;
4714 /* fast test first */
4715 if (tok >= TOK_UIDENT)
4717 /* no need to save tokc because tok is an identifier */
4718 last_tok = tok;
4719 next();
4720 if (tok == ':') {
4721 unget_tok(last_tok);
4722 return;
4724 unget_tok(last_tok);
4726 decl(l);
4729 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4730 int case_reg, int is_expr)
4732 int a, b, c, d;
4733 Sym *s, *frame_bottom;
4735 /* generate line number info */
4736 if (tcc_state->do_debug &&
4737 (last_line_num != file->line_num || last_ind != ind)) {
4738 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4739 last_ind = ind;
4740 last_line_num = file->line_num;
4743 if (is_expr) {
4744 /* default return value is (void) */
4745 vpushi(0);
4746 vtop->type.t = VT_VOID;
4749 if (tok == TOK_IF) {
4750 /* if test */
4751 next();
4752 skip('(');
4753 gexpr();
4754 skip(')');
4755 a = gtst(1, 0);
4756 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4757 c = tok;
4758 if (c == TOK_ELSE) {
4759 next();
4760 d = gjmp(0);
4761 gsym(a);
4762 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4763 gsym(d); /* patch else jmp */
4764 } else
4765 gsym(a);
4766 } else if (tok == TOK_WHILE) {
4767 next();
4768 d = ind;
4769 skip('(');
4770 gexpr();
4771 skip(')');
4772 a = gtst(1, 0);
4773 b = 0;
4774 block(&a, &b, case_sym, def_sym, case_reg, 0);
4775 gjmp_addr(d);
4776 gsym(a);
4777 gsym_addr(b, d);
4778 } else if (tok == '{') {
4779 Sym *llabel;
4780 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4782 next();
4783 /* record local declaration stack position */
4784 s = local_stack;
4785 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4786 frame_bottom->next = scope_stack_bottom;
4787 scope_stack_bottom = frame_bottom;
4788 llabel = local_label_stack;
4790 /* save VLA state */
4791 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4792 if (saved_vla_sp_loc != &vla_sp_root_loc)
4793 vla_sp_loc = &block_vla_sp_loc;
4795 saved_vla_flags = vla_flags;
4796 vla_flags |= VLA_NEED_NEW_FRAME;
4798 /* handle local labels declarations */
4799 if (tok == TOK_LABEL) {
4800 next();
4801 for(;;) {
4802 if (tok < TOK_UIDENT)
4803 expect("label identifier");
4804 label_push(&local_label_stack, tok, LABEL_DECLARED);
4805 next();
4806 if (tok == ',') {
4807 next();
4808 } else {
4809 skip(';');
4810 break;
4814 while (tok != '}') {
4815 label_or_decl(VT_LOCAL);
4816 if (tok != '}') {
4817 if (is_expr)
4818 vpop();
4819 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4822 /* pop locally defined labels */
4823 label_pop(&local_label_stack, llabel);
4824 if(is_expr) {
4825 /* XXX: this solution makes only valgrind happy...
4826 triggered by gcc.c-torture/execute/20000917-1.c */
4827 Sym *p;
4828 switch(vtop->type.t & VT_BTYPE) {
4829 case VT_PTR:
4830 case VT_STRUCT:
4831 case VT_ENUM:
4832 case VT_FUNC:
4833 for(p=vtop->type.ref;p;p=p->prev)
4834 if(p->prev==s)
4835 tcc_error("unsupported expression type");
4838 /* pop locally defined symbols */
4839 scope_stack_bottom = scope_stack_bottom->next;
4840 sym_pop(&local_stack, s);
4842 /* Pop VLA frames and restore stack pointer if required */
4843 if (saved_vla_sp_loc != &vla_sp_root_loc)
4844 *saved_vla_sp_loc = block_vla_sp_loc;
4845 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4846 vla_sp_loc = saved_vla_sp_loc;
4847 gen_vla_sp_restore(*vla_sp_loc);
4849 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4851 next();
4852 } else if (tok == TOK_RETURN) {
4853 next();
4854 if (tok != ';') {
4855 gexpr();
4856 gen_assign_cast(&func_vt);
4857 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4858 CType type, ret_type;
4859 int ret_align, ret_nregs;
4860 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4861 &ret_align);
4862 if (0 == ret_nregs) {
4863 /* if returning structure, must copy it to implicit
4864 first pointer arg location */
4865 type = func_vt;
4866 mk_pointer(&type);
4867 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4868 indir();
4869 vswap();
4870 /* copy structure value to pointer */
4871 vstore();
4872 } else {
4873 /* returning structure packed into registers */
4874 int rc;
4875 vtop->type = ret_type;
4876 if (is_float(ret_type.t))
4877 rc = rc_fret(ret_type.t);
4878 else{
4879 rc = RC_IRET;
4880 ex_rc = RC_LRET;
4883 for (;;) {
4884 gv(rc);
4885 if (--ret_nregs == 0)
4886 break;
4887 /* We assume that when a structure is returned in multiple
4888 registers, their classes are consecutive values of the
4889 suite s(n) = 2^n */
4890 rc <<= 1;
4891 /* XXX: compatible with arm only: ret_align == register_size */
4892 vtop->c.i += ret_align;
4893 vtop->r = VT_LOCAL | VT_LVAL;
4896 } else if (is_float(func_vt.t)) {
4897 gv(rc_fret(func_vt.t));
4898 } else {
4899 gv(RC_IRET);
4901 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4903 skip(';');
4904 rsym = gjmp(rsym); /* jmp */
4905 } else if (tok == TOK_BREAK) {
4906 /* compute jump */
4907 if (!bsym)
4908 tcc_error("cannot break");
4909 *bsym = gjmp(*bsym);
4910 next();
4911 skip(';');
4912 } else if (tok == TOK_CONTINUE) {
4913 /* compute jump */
4914 if (!csym)
4915 tcc_error("cannot continue");
4916 *csym = gjmp(*csym);
4917 next();
4918 skip(';');
4919 } else if (tok == TOK_FOR) {
4920 int e;
4921 next();
4922 skip('(');
4923 s = local_stack;
4924 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4925 frame_bottom->next = scope_stack_bottom;
4926 scope_stack_bottom = frame_bottom;
4927 if (tok != ';') {
4928 /* c99 for-loop init decl? */
4929 if (!decl0(VT_LOCAL, 1)) {
4930 /* no, regular for-loop init expr */
4931 gexpr();
4932 vpop();
4935 skip(';');
4936 d = ind;
4937 c = ind;
4938 a = 0;
4939 b = 0;
4940 if (tok != ';') {
4941 gexpr();
4942 a = gtst(1, 0);
4944 skip(';');
4945 if (tok != ')') {
4946 e = gjmp(0);
4947 c = ind;
4948 gexpr();
4949 vpop();
4950 gjmp_addr(d);
4951 gsym(e);
4953 skip(')');
4954 block(&a, &b, case_sym, def_sym, case_reg, 0);
4955 gjmp_addr(c);
4956 gsym(a);
4957 gsym_addr(b, c);
4958 scope_stack_bottom = scope_stack_bottom->next;
4959 sym_pop(&local_stack, s);
4960 } else
4961 if (tok == TOK_DO) {
4962 next();
4963 a = 0;
4964 b = 0;
4965 d = ind;
4966 block(&a, &b, case_sym, def_sym, case_reg, 0);
4967 skip(TOK_WHILE);
4968 skip('(');
4969 gsym(b);
4970 gexpr();
4971 c = gtst(0, 0);
4972 gsym_addr(c, d);
4973 skip(')');
4974 gsym(a);
4975 skip(';');
4976 } else
4977 if (tok == TOK_SWITCH) {
4978 next();
4979 skip('(');
4980 gexpr();
4981 /* XXX: other types than integer */
4982 case_reg = gv(RC_INT);
4983 vpop();
4984 skip(')');
4985 a = 0;
4986 b = gjmp(0); /* jump to first case */
4987 c = 0;
4988 block(&a, csym, &b, &c, case_reg, 0);
4989 /* if no default, jmp after switch */
4990 if (c == 0)
4991 c = ind;
4992 /* default label */
4993 gsym_addr(b, c);
4994 /* break label */
4995 gsym(a);
4996 } else
4997 if (tok == TOK_CASE) {
4998 int v1, v2;
4999 if (!case_sym)
5000 expect("switch");
5001 next();
5002 v1 = expr_const();
5003 v2 = v1;
5004 if (gnu_ext && tok == TOK_DOTS) {
5005 next();
5006 v2 = expr_const();
5007 if (v2 < v1)
5008 tcc_warning("empty case range");
5010 /* since a case is like a label, we must skip it with a jmp */
5011 b = gjmp(0);
5012 gsym(*case_sym);
5013 vseti(case_reg, 0);
5014 vpushi(v1);
5015 if (v1 == v2) {
5016 gen_op(TOK_EQ);
5017 *case_sym = gtst(1, 0);
5018 } else {
5019 gen_op(TOK_GE);
5020 *case_sym = gtst(1, 0);
5021 vseti(case_reg, 0);
5022 vpushi(v2);
5023 gen_op(TOK_LE);
5024 *case_sym = gtst(1, *case_sym);
5026 gsym(b);
5027 skip(':');
5028 is_expr = 0;
5029 goto block_after_label;
5030 } else
5031 if (tok == TOK_DEFAULT) {
5032 next();
5033 skip(':');
5034 if (!def_sym)
5035 expect("switch");
5036 if (*def_sym)
5037 tcc_error("too many 'default'");
5038 *def_sym = ind;
5039 is_expr = 0;
5040 goto block_after_label;
5041 } else
5042 if (tok == TOK_GOTO) {
5043 next();
5044 if (tok == '*' && gnu_ext) {
5045 /* computed goto */
5046 next();
5047 gexpr();
5048 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
5049 expect("pointer");
5050 ggoto();
5051 } else if (tok >= TOK_UIDENT) {
5052 s = label_find(tok);
5053 /* put forward definition if needed */
5054 if (!s) {
5055 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
5056 } else {
5057 if (s->r == LABEL_DECLARED)
5058 s->r = LABEL_FORWARD;
5060 /* label already defined */
5061 if (vla_flags & VLA_IN_SCOPE) {
5062 /* If VLAs are in use, save the current stack pointer and
5063 reset the stack pointer to what it was at function entry
5064 (label will restore stack pointer in inner scopes) */
5065 vla_sp_save();
5066 gen_vla_sp_restore(vla_sp_root_loc);
5068 if (s->r & LABEL_FORWARD)
5069 s->jnext = gjmp(s->jnext);
5070 else
5071 gjmp_addr(s->jnext);
5072 next();
5073 } else {
5074 expect("label identifier");
5076 skip(';');
5077 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
5078 asm_instr();
5079 } else {
5080 b = is_label();
5081 if (b) {
5082 /* label case */
5083 if (vla_flags & VLA_IN_SCOPE) {
5084 /* save/restore stack pointer across label
5085 this is a no-op when combined with the load immediately
5086 after the label unless we arrive via goto */
5087 vla_sp_save();
5089 s = label_find(b);
5090 if (s) {
5091 if (s->r == LABEL_DEFINED)
5092 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
5093 gsym(s->jnext);
5094 s->r = LABEL_DEFINED;
5095 } else {
5096 s = label_push(&global_label_stack, b, LABEL_DEFINED);
5098 s->jnext = ind;
5099 if (vla_flags & VLA_IN_SCOPE) {
5100 gen_vla_sp_restore(*vla_sp_loc);
5101 vla_flags |= VLA_NEED_NEW_FRAME;
5103 /* we accept this, but it is a mistake */
5104 block_after_label:
5105 if (tok == '}') {
5106 tcc_warning("deprecated use of label at end of compound statement");
5107 } else {
5108 if (is_expr)
5109 vpop();
5110 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
5112 } else {
5113 /* expression case */
5114 if (tok != ';') {
5115 if (is_expr) {
5116 vpop();
5117 gexpr();
5118 } else {
5119 gexpr();
5120 vpop();
5123 skip(';');
5128 /* t is the array or struct type. c is the array or struct
5129 address. cur_index/cur_field is the pointer to the current
5130 value. 'size_only' is true if only size info is needed (only used
5131 in arrays) */
5132 static void decl_designator(CType *type, Section *sec, unsigned long c,
5133 int *cur_index, Sym **cur_field,
5134 int size_only)
5136 Sym *s, *f;
5137 int notfirst, index, index_last, align, l, nb_elems, elem_size;
5138 CType type1;
5140 notfirst = 0;
5141 elem_size = 0;
5142 nb_elems = 1;
5143 if (gnu_ext && (l = is_label()) != 0)
5144 goto struct_field;
5145 while (tok == '[' || tok == '.') {
5146 if (tok == '[') {
5147 if (!(type->t & VT_ARRAY))
5148 expect("array type");
5149 s = type->ref;
5150 next();
5151 index = expr_const();
5152 if (index < 0 || (s->c >= 0 && index >= s->c))
5153 expect("invalid index");
5154 if (tok == TOK_DOTS && gnu_ext) {
5155 next();
5156 index_last = expr_const();
5157 if (index_last < 0 ||
5158 (s->c >= 0 && index_last >= s->c) ||
5159 index_last < index)
5160 expect("invalid index");
5161 } else {
5162 index_last = index;
5164 skip(']');
5165 if (!notfirst)
5166 *cur_index = index_last;
5167 type = pointed_type(type);
5168 elem_size = type_size(type, &align);
5169 c += index * elem_size;
5170 /* NOTE: we only support ranges for last designator */
5171 nb_elems = index_last - index + 1;
5172 if (nb_elems != 1) {
5173 notfirst = 1;
5174 break;
5176 } else {
5177 next();
5178 l = tok;
5179 next();
5180 struct_field:
5181 if ((type->t & VT_BTYPE) != VT_STRUCT)
5182 expect("struct/union type");
5183 s = type->ref;
5184 l |= SYM_FIELD;
5185 f = s->next;
5186 while (f) {
5187 if (f->v == l)
5188 break;
5189 f = f->next;
5191 if (!f)
5192 expect("field");
5193 if (!notfirst)
5194 *cur_field = f;
5195 /* XXX: fix this mess by using explicit storage field */
5196 type1 = f->type;
5197 type1.t |= (type->t & ~VT_TYPE);
5198 type = &type1;
5199 c += f->c;
5201 notfirst = 1;
5203 if (notfirst) {
5204 if (tok == '=') {
5205 next();
5206 } else {
5207 if (!gnu_ext)
5208 expect("=");
5210 } else {
5211 if (type->t & VT_ARRAY) {
5212 index = *cur_index;
5213 type = pointed_type(type);
5214 c += index * type_size(type, &align);
5215 } else {
5216 f = *cur_field;
5217 if (!f)
5218 tcc_error("too many field init");
5219 /* XXX: fix this mess by using explicit storage field */
5220 type1 = f->type;
5221 type1.t |= (type->t & ~VT_TYPE);
5222 type = &type1;
5223 c += f->c;
5226 decl_initializer(type, sec, c, 0, size_only);
5228 /* XXX: make it more general */
5229 if (!size_only && nb_elems > 1) {
5230 unsigned long c_end;
5231 uint8_t *src, *dst;
5232 int i;
5234 if (!sec)
5235 tcc_error("range init not supported yet for dynamic storage");
5236 c_end = c + nb_elems * elem_size;
5237 if (c_end > sec->data_allocated)
5238 section_realloc(sec, c_end);
5239 src = sec->data + c;
5240 dst = src;
5241 for(i = 1; i < nb_elems; i++) {
5242 dst += elem_size;
5243 memcpy(dst, src, elem_size);
5248 #define EXPR_VAL 0
5249 #define EXPR_CONST 1
5250 #define EXPR_ANY 2
5252 /* store a value or an expression directly in global data or in local array */
5253 static void init_putv(CType *type, Section *sec, unsigned long c,
5254 int v, int expr_type)
5256 int saved_global_expr, bt, bit_pos, bit_size;
5257 void *ptr;
5258 unsigned long long bit_mask;
5259 CType dtype;
5261 switch(expr_type) {
5262 case EXPR_VAL:
5263 vpushi(v);
5264 break;
5265 case EXPR_CONST:
5266 /* compound literals must be allocated globally in this case */
5267 saved_global_expr = global_expr;
5268 global_expr = 1;
5269 expr_const1();
5270 global_expr = saved_global_expr;
5271 /* NOTE: symbols are accepted */
5272 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5273 tcc_error("initializer element is not constant");
5274 break;
5275 case EXPR_ANY:
5276 expr_eq();
5277 break;
5280 dtype = *type;
5281 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5283 if (sec) {
5284 /* XXX: not portable */
5285 /* XXX: generate error if incorrect relocation */
5286 gen_assign_cast(&dtype);
5287 bt = type->t & VT_BTYPE;
5288 /* we'll write at most 12 bytes */
5289 if (c + 12 > sec->data_allocated) {
5290 section_realloc(sec, c + 12);
5292 ptr = sec->data + c;
5293 /* XXX: make code faster ? */
5294 if (!(type->t & VT_BITFIELD)) {
5295 bit_pos = 0;
5296 bit_size = 32;
5297 bit_mask = -1LL;
5298 } else {
5299 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5300 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5301 bit_mask = (1LL << bit_size) - 1;
5303 if ((vtop->r & VT_SYM) &&
5304 (bt == VT_BYTE ||
5305 bt == VT_SHORT ||
5306 bt == VT_DOUBLE ||
5307 bt == VT_LDOUBLE ||
5308 bt == VT_LLONG ||
5309 (bt == VT_INT && bit_size != 32)))
5310 tcc_error("initializer element is not computable at load time");
5311 switch(bt) {
5312 case VT_BOOL:
5313 vtop->c.i = (vtop->c.i != 0);
5314 case VT_BYTE:
5315 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5316 break;
5317 case VT_SHORT:
5318 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5319 break;
5320 case VT_DOUBLE:
5321 *(double *)ptr = vtop->c.d;
5322 break;
5323 case VT_LDOUBLE:
5324 *(long double *)ptr = vtop->c.ld;
5325 break;
5326 case VT_LLONG:
5327 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5328 break;
5329 case VT_PTR:
5330 if (vtop->r & VT_SYM) {
5331 greloc(sec, vtop->sym, c, R_DATA_PTR);
5333 *(addr_t *)ptr |= (vtop->c.ptr_offset & bit_mask) << bit_pos;
5334 break;
5335 default:
5336 if (vtop->r & VT_SYM) {
5337 greloc(sec, vtop->sym, c, R_DATA_PTR);
5339 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5340 break;
5342 vtop--;
5343 } else {
5344 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5345 vswap();
5346 vstore();
5347 vpop();
5351 /* put zeros for variable based init */
5352 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5354 if (sec) {
5355 /* nothing to do because globals are already set to zero */
5356 } else {
5357 #ifndef TCC_TARGET_X86_64
5358 vpush_global_sym(&func_old_type, TOK_memset);
5359 vseti(VT_LOCAL, c);
5360 # ifdef TCC_TARGET_ARM
5361 vpushs(size);
5362 vpushi(0);
5363 # else
5364 vpushi(0);
5365 vpushs(size);
5366 # endif
5367 gfunc_call(3);
5368 #else
5369 vseti(VT_LOCAL, c);
5370 gen_putz(vtop, size);
5371 vtop--;
5372 #endif
5376 /* 't' contains the type and storage info. 'c' is the offset of the
5377 object in section 'sec'. If 'sec' is NULL, it means stack based
5378 allocation. 'first' is true if array '{' must be read (multi
5379 dimension implicit array init handling). 'size_only' is true if
5380 size only evaluation is wanted (only for arrays). */
5381 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5382 int first, int size_only)
5384 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5385 int size1, align1, expr_type;
5386 Sym *s, *f;
5387 CType *t1;
5389 if (type->t & VT_VLA) {
5390 int a;
5392 /* save current stack pointer */
5393 if (vla_flags & VLA_NEED_NEW_FRAME) {
5394 vla_sp_save();
5395 vla_flags = VLA_IN_SCOPE;
5396 vla_sp_loc = &vla_sp_loc_tmp;
5399 vla_runtime_type_size(type, &a);
5400 gen_vla_alloc(type, a);
5401 vset(type, VT_LOCAL|VT_LVAL, c);
5402 vswap();
5403 vstore();
5404 vpop();
5405 } else if (type->t & VT_ARRAY) {
5406 s = type->ref;
5407 n = s->c;
5408 array_length = 0;
5409 t1 = pointed_type(type);
5410 size1 = type_size(t1, &align1);
5412 no_oblock = 1;
5413 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5414 tok == '{') {
5415 if (tok != '{')
5416 tcc_error("character array initializer must be a literal,"
5417 " optionally enclosed in braces");
5418 skip('{');
5419 no_oblock = 0;
5422 /* only parse strings here if correct type (otherwise: handle
5423 them as ((w)char *) expressions */
5424 if ((tok == TOK_LSTR &&
5425 #ifdef TCC_TARGET_PE
5426 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5427 #else
5428 (t1->t & VT_BTYPE) == VT_INT
5429 #endif
5430 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5431 while (tok == TOK_STR || tok == TOK_LSTR) {
5432 int cstr_len, ch;
5433 CString *cstr;
5435 cstr = tokc.cstr;
5436 /* compute maximum number of chars wanted */
5437 if (tok == TOK_STR)
5438 cstr_len = cstr->size;
5439 else
5440 cstr_len = cstr->size / sizeof(nwchar_t);
5441 cstr_len--;
5442 nb = cstr_len;
5443 if (n >= 0 && nb > (n - array_length))
5444 nb = n - array_length;
5445 if (!size_only) {
5446 if (cstr_len > nb)
5447 tcc_warning("initializer-string for array is too long");
5448 /* in order to go faster for common case (char
5449 string in global variable, we handle it
5450 specifically */
5451 if (sec && tok == TOK_STR && size1 == 1) {
5452 memcpy(sec->data + c + array_length, cstr->data, nb);
5453 } else {
5454 for(i=0;i<nb;i++) {
5455 if (tok == TOK_STR)
5456 ch = ((unsigned char *)cstr->data)[i];
5457 else
5458 ch = ((nwchar_t *)cstr->data)[i];
5459 init_putv(t1, sec, c + (array_length + i) * size1,
5460 ch, EXPR_VAL);
5464 array_length += nb;
5465 next();
5467 /* only add trailing zero if enough storage (no
5468 warning in this case since it is standard) */
5469 if (n < 0 || array_length < n) {
5470 if (!size_only) {
5471 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5473 array_length++;
5475 } else {
5476 index = 0;
5477 while (tok != '}') {
5478 decl_designator(type, sec, c, &index, NULL, size_only);
5479 if (n >= 0 && index >= n)
5480 tcc_error("index too large");
5481 /* must put zero in holes (note that doing it that way
5482 ensures that it even works with designators) */
5483 if (!size_only && array_length < index) {
5484 init_putz(t1, sec, c + array_length * size1,
5485 (index - array_length) * size1);
5487 index++;
5488 if (index > array_length)
5489 array_length = index;
5490 /* special test for multi dimensional arrays (may not
5491 be strictly correct if designators are used at the
5492 same time) */
5493 if (index >= n && no_oblock)
5494 break;
5495 if (tok == '}')
5496 break;
5497 skip(',');
5500 if (!no_oblock)
5501 skip('}');
5502 /* put zeros at the end */
5503 if (!size_only && n >= 0 && array_length < n) {
5504 init_putz(t1, sec, c + array_length * size1,
5505 (n - array_length) * size1);
5507 /* patch type size if needed */
5508 if (n < 0)
5509 s->c = array_length;
5510 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5511 (sec || !first || tok == '{')) {
5512 int par_count;
5514 /* NOTE: the previous test is a specific case for automatic
5515 struct/union init */
5516 /* XXX: union needs only one init */
5518 /* XXX: this test is incorrect for local initializers
5519 beginning with ( without {. It would be much more difficult
5520 to do it correctly (ideally, the expression parser should
5521 be used in all cases) */
5522 par_count = 0;
5523 if (tok == '(') {
5524 AttributeDef ad1;
5525 CType type1;
5526 next();
5527 while (tok == '(') {
5528 par_count++;
5529 next();
5531 if (!parse_btype(&type1, &ad1))
5532 expect("cast");
5533 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5534 #if 0
5535 if (!is_assignable_types(type, &type1))
5536 tcc_error("invalid type for cast");
5537 #endif
5538 skip(')');
5540 no_oblock = 1;
5541 if (first || tok == '{') {
5542 skip('{');
5543 no_oblock = 0;
5545 s = type->ref;
5546 f = s->next;
5547 array_length = 0;
5548 index = 0;
5549 n = s->c;
5550 while (tok != '}') {
5551 decl_designator(type, sec, c, NULL, &f, size_only);
5552 index = f->c;
5553 if (!size_only && array_length < index) {
5554 init_putz(type, sec, c + array_length,
5555 index - array_length);
5557 index = index + type_size(&f->type, &align1);
5558 if (index > array_length)
5559 array_length = index;
5561 /* gr: skip fields from same union - ugly. */
5562 while (f->next) {
5563 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5564 /* test for same offset */
5565 if (f->next->c != f->c)
5566 break;
5567 /* if yes, test for bitfield shift */
5568 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5569 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5570 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5571 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5572 if (bit_pos_1 != bit_pos_2)
5573 break;
5575 f = f->next;
5578 f = f->next;
5579 if (no_oblock && f == NULL)
5580 break;
5581 if (tok == '}')
5582 break;
5583 skip(',');
5585 /* put zeros at the end */
5586 if (!size_only && array_length < n) {
5587 init_putz(type, sec, c + array_length,
5588 n - array_length);
5590 if (!no_oblock)
5591 skip('}');
5592 while (par_count) {
5593 skip(')');
5594 par_count--;
5596 } else if (tok == '{') {
5597 next();
5598 decl_initializer(type, sec, c, first, size_only);
5599 skip('}');
5600 } else if (size_only) {
5601 /* just skip expression */
5602 parlevel = parlevel1 = 0;
5603 while ((parlevel > 0 || parlevel1 > 0 ||
5604 (tok != '}' && tok != ',')) && tok != -1) {
5605 if (tok == '(')
5606 parlevel++;
5607 else if (tok == ')')
5608 parlevel--;
5609 else if (tok == '{')
5610 parlevel1++;
5611 else if (tok == '}')
5612 parlevel1--;
5613 next();
5615 } else {
5616 /* currently, we always use constant expression for globals
5617 (may change for scripting case) */
5618 expr_type = EXPR_CONST;
5619 if (!sec)
5620 expr_type = EXPR_ANY;
5621 init_putv(type, sec, c, 0, expr_type);
5625 /* parse an initializer for type 't' if 'has_init' is non zero, and
5626 allocate space in local or global data space ('r' is either
5627 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5628 variable 'v' with an associated name represented by 'asm_label' of
5629 scope 'scope' is declared before initializers are parsed. If 'v' is
5630 zero, then a reference to the new object is put in the value stack.
5631 If 'has_init' is 2, a special parsing is done to handle string
5632 constants. */
5633 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5634 int has_init, int v, char *asm_label,
5635 int scope)
5637 int size, align, addr, data_offset;
5638 int level;
5639 ParseState saved_parse_state = {0};
5640 TokenString init_str;
5641 Section *sec;
5642 Sym *flexible_array;
5644 flexible_array = NULL;
5645 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5646 Sym *field = type->ref->next;
5647 if (field) {
5648 while (field->next)
5649 field = field->next;
5650 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5651 flexible_array = field;
5655 size = type_size(type, &align);
5656 /* If unknown size, we must evaluate it before
5657 evaluating initializers because
5658 initializers can generate global data too
5659 (e.g. string pointers or ISOC99 compound
5660 literals). It also simplifies local
5661 initializers handling */
5662 tok_str_new(&init_str);
5663 if (size < 0 || (flexible_array && has_init)) {
5664 if (!has_init)
5665 tcc_error("unknown type size");
5666 /* get all init string */
5667 if (has_init == 2) {
5668 /* only get strings */
5669 while (tok == TOK_STR || tok == TOK_LSTR) {
5670 tok_str_add_tok(&init_str);
5671 next();
5673 } else {
5674 level = 0;
5675 while (level > 0 || (tok != ',' && tok != ';')) {
5676 if (tok < 0)
5677 tcc_error("unexpected end of file in initializer");
5678 tok_str_add_tok(&init_str);
5679 if (tok == '{')
5680 level++;
5681 else if (tok == '}') {
5682 level--;
5683 if (level <= 0) {
5684 next();
5685 break;
5688 next();
5691 tok_str_add(&init_str, -1);
5692 tok_str_add(&init_str, 0);
5694 /* compute size */
5695 save_parse_state(&saved_parse_state);
5697 macro_ptr = init_str.str;
5698 next();
5699 decl_initializer(type, NULL, 0, 1, 1);
5700 /* prepare second initializer parsing */
5701 macro_ptr = init_str.str;
5702 next();
5704 /* if still unknown size, error */
5705 size = type_size(type, &align);
5706 if (size < 0)
5707 tcc_error("unknown type size");
5709 if (flexible_array)
5710 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5711 /* take into account specified alignment if bigger */
5712 if (ad->a.aligned) {
5713 if (ad->a.aligned > align)
5714 align = ad->a.aligned;
5715 } else if (ad->a.packed) {
5716 align = 1;
5718 if ((r & VT_VALMASK) == VT_LOCAL) {
5719 sec = NULL;
5720 #ifdef CONFIG_TCC_BCHECK
5721 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5722 loc_stack(1, 1);
5724 #endif
5725 addr = loc_stack(size, 1);
5726 #ifdef CONFIG_TCC_BCHECK
5727 /* handles bounds */
5728 /* XXX: currently, since we do only one pass, we cannot track
5729 '&' operators, so we add only arrays */
5730 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5731 unsigned long *bounds_ptr;
5732 /* add padding between regions */
5733 loc_stack(1, 1);
5734 /* then add local bound info */
5735 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5736 bounds_ptr[0] = addr;
5737 bounds_ptr[1] = size;
5739 #endif
5740 if (v) {
5741 /* local variable */
5742 sym_push(v, type, r, addr);
5743 } else {
5744 /* push local reference */
5745 vset(type, r, addr);
5747 } else {
5748 Sym *sym;
5750 sym = NULL;
5751 if (v && scope == VT_CONST) {
5752 /* see if the symbol was already defined */
5753 sym = sym_find(v);
5754 if (sym) {
5755 if (!is_compatible_types(&sym->type, type))
5756 tcc_error("incompatible types for redefinition of '%s'",
5757 get_tok_str(v, NULL));
5758 if (sym->type.t & VT_EXTERN) {
5759 /* if the variable is extern, it was not allocated */
5760 sym->type.t &= ~VT_EXTERN;
5761 /* set array size if it was omitted in extern
5762 declaration */
5763 if ((sym->type.t & VT_ARRAY) &&
5764 sym->type.ref->c < 0 &&
5765 type->ref->c >= 0)
5766 sym->type.ref->c = type->ref->c;
5767 } else {
5768 /* we accept several definitions of the same
5769 global variable. this is tricky, because we
5770 must play with the SHN_COMMON type of the symbol */
5771 /* XXX: should check if the variable was already
5772 initialized. It is incorrect to initialized it
5773 twice */
5774 /* no init data, we won't add more to the symbol */
5775 if (!has_init)
5776 goto no_alloc;
5781 /* allocate symbol in corresponding section */
5782 sec = ad->section;
5783 if (!sec) {
5784 if (has_init)
5785 sec = data_section;
5786 else if (tcc_state->nocommon)
5787 sec = bss_section;
5789 if (sec) {
5790 data_offset = sec->data_offset;
5791 data_offset = (data_offset + align - 1) & -align;
5792 addr = data_offset;
5793 /* very important to increment global pointer at this time
5794 because initializers themselves can create new initializers */
5795 data_offset += size;
5796 #ifdef CONFIG_TCC_BCHECK
5797 /* add padding if bound check */
5798 if (tcc_state->do_bounds_check)
5799 data_offset++;
5800 #endif
5801 sec->data_offset = data_offset;
5802 /* allocate section space to put the data */
5803 if (sec->sh_type != SHT_NOBITS &&
5804 data_offset > sec->data_allocated)
5805 section_realloc(sec, data_offset);
5806 /* align section if needed */
5807 if (align > sec->sh_addralign)
5808 sec->sh_addralign = align;
5809 } else {
5810 addr = 0; /* avoid warning */
5813 if (v) {
5814 if (scope != VT_CONST || !sym) {
5815 sym = sym_push(v, type, r | VT_SYM, 0);
5816 sym->asm_label = asm_label;
5818 /* update symbol definition */
5819 if (sec) {
5820 put_extern_sym(sym, sec, addr, size);
5821 } else {
5822 ElfW(Sym) *esym;
5823 /* put a common area */
5824 put_extern_sym(sym, NULL, align, size);
5825 /* XXX: find a nicer way */
5826 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5827 esym->st_shndx = SHN_COMMON;
5829 } else {
5830 /* push global reference */
5831 sym = get_sym_ref(type, sec, addr, size);
5832 vpushsym(type, sym);
5834 /* patch symbol weakness */
5835 if (type->t & VT_WEAK)
5836 weaken_symbol(sym);
5837 apply_visibility(sym, type);
5838 #ifdef CONFIG_TCC_BCHECK
5839 /* handles bounds now because the symbol must be defined
5840 before for the relocation */
5841 if (tcc_state->do_bounds_check) {
5842 unsigned long *bounds_ptr;
5844 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5845 /* then add global bound info */
5846 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5847 bounds_ptr[0] = 0; /* relocated */
5848 bounds_ptr[1] = size;
5850 #endif
5852 if (has_init || (type->t & VT_VLA)) {
5853 decl_initializer(type, sec, addr, 1, 0);
5854 /* restore parse state if needed */
5855 if (init_str.str) {
5856 tok_str_free(init_str.str);
5857 restore_parse_state(&saved_parse_state);
5859 /* patch flexible array member size back to -1, */
5860 /* for possible subsequent similar declarations */
5861 if (flexible_array)
5862 flexible_array->type.ref->c = -1;
5864 no_alloc: ;
5867 static void put_func_debug(Sym *sym)
5869 char buf[512];
5871 /* stabs info */
5872 /* XXX: we put here a dummy type */
5873 snprintf(buf, sizeof(buf), "%s:%c1",
5874 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5875 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5876 cur_text_section, sym->c);
5877 /* //gr gdb wants a line at the function */
5878 put_stabn(N_SLINE, 0, file->line_num, 0);
5879 last_ind = 0;
5880 last_line_num = 0;
5883 /* parse an old style function declaration list */
5884 /* XXX: check multiple parameter */
5885 static void func_decl_list(Sym *func_sym)
5887 AttributeDef ad;
5888 int v;
5889 Sym *s;
5890 CType btype, type;
5892 /* parse each declaration */
5893 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5894 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5895 if (!parse_btype(&btype, &ad))
5896 expect("declaration list");
5897 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5898 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5899 tok == ';') {
5900 /* we accept no variable after */
5901 } else {
5902 for(;;) {
5903 type = btype;
5904 type_decl(&type, &ad, &v, TYPE_DIRECT);
5905 /* find parameter in function parameter list */
5906 s = func_sym->next;
5907 while (s != NULL) {
5908 if ((s->v & ~SYM_FIELD) == v)
5909 goto found;
5910 s = s->next;
5912 tcc_error("declaration for parameter '%s' but no such parameter",
5913 get_tok_str(v, NULL));
5914 found:
5915 /* check that no storage specifier except 'register' was given */
5916 if (type.t & VT_STORAGE)
5917 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5918 convert_parameter_type(&type);
5919 /* we can add the type (NOTE: it could be local to the function) */
5920 s->type = type;
5921 /* accept other parameters */
5922 if (tok == ',')
5923 next();
5924 else
5925 break;
5928 skip(';');
5932 /* parse a function defined by symbol 'sym' and generate its code in
5933 'cur_text_section' */
5934 static void gen_function(Sym *sym)
5936 int saved_nocode_wanted = nocode_wanted;
5937 nocode_wanted = 0;
5938 ind = cur_text_section->data_offset;
5939 /* NOTE: we patch the symbol size later */
5940 put_extern_sym(sym, cur_text_section, ind, 0);
5941 funcname = get_tok_str(sym->v, NULL);
5942 func_ind = ind;
5943 /* Initialize VLA state */
5944 vla_sp_loc = &vla_sp_root_loc;
5945 vla_flags = VLA_NEED_NEW_FRAME;
5946 /* put debug symbol */
5947 if (tcc_state->do_debug)
5948 put_func_debug(sym);
5949 /* push a dummy symbol to enable local sym storage */
5950 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5951 gfunc_prolog(&sym->type);
5952 #ifdef CONFIG_TCC_BCHECK
5953 if (tcc_state->do_bounds_check
5954 && !strcmp(get_tok_str(sym->v, NULL), "main")) {
5955 int i;
5957 sym = local_stack;
5958 for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) {
5959 if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD)
5960 break;
5961 vpush_global_sym(&func_old_type, TOK___bound_main_arg);
5962 vset(&sym->type, sym->r, sym->c);
5963 gfunc_call(1);
5966 #endif
5967 rsym = 0;
5968 block(NULL, NULL, NULL, NULL, 0, 0);
5969 gsym(rsym);
5970 gfunc_epilog();
5971 cur_text_section->data_offset = ind;
5972 label_pop(&global_label_stack, NULL);
5973 /* reset local stack */
5974 scope_stack_bottom = NULL;
5975 sym_pop(&local_stack, NULL);
5976 /* end of function */
5977 /* patch symbol size */
5978 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5979 ind - func_ind;
5980 /* patch symbol weakness (this definition overrules any prototype) */
5981 if (sym->type.t & VT_WEAK)
5982 weaken_symbol(sym);
5983 apply_visibility(sym, &sym->type);
5984 if (tcc_state->do_debug) {
5985 put_stabn(N_FUN, 0, 0, ind - func_ind);
5987 /* It's better to crash than to generate wrong code */
5988 cur_text_section = NULL;
5989 funcname = ""; /* for safety */
5990 func_vt.t = VT_VOID; /* for safety */
5991 func_var = 0; /* for safety */
5992 ind = 0; /* for safety */
5993 nocode_wanted = saved_nocode_wanted;
5996 ST_FUNC void gen_inline_functions(void)
5998 Sym *sym;
5999 int *str, inline_generated, i;
6000 struct InlineFunc *fn;
6002 /* iterate while inline function are referenced */
6003 for(;;) {
6004 inline_generated = 0;
6005 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
6006 fn = tcc_state->inline_fns[i];
6007 sym = fn->sym;
6008 if (sym && sym->c) {
6009 /* the function was used: generate its code and
6010 convert it to a normal function */
6011 str = fn->token_str;
6012 fn->sym = NULL;
6013 if (file)
6014 pstrcpy(file->filename, sizeof file->filename, fn->filename);
6015 sym->r = VT_SYM | VT_CONST;
6016 sym->type.t &= ~VT_INLINE;
6018 macro_ptr = str;
6019 next();
6020 cur_text_section = text_section;
6021 gen_function(sym);
6022 macro_ptr = NULL; /* fail safe */
6024 inline_generated = 1;
6027 if (!inline_generated)
6028 break;
6030 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
6031 fn = tcc_state->inline_fns[i];
6032 str = fn->token_str;
6033 tok_str_free(str);
6035 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
6038 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6039 static int decl0(int l, int is_for_loop_init)
6041 int v, has_init, r;
6042 CType type, btype;
6043 Sym *sym;
6044 AttributeDef ad;
6046 while (1) {
6047 if (!parse_btype(&btype, &ad)) {
6048 if (is_for_loop_init)
6049 return 0;
6050 /* skip redundant ';' */
6051 /* XXX: find more elegant solution */
6052 if (tok == ';') {
6053 next();
6054 continue;
6056 if (l == VT_CONST &&
6057 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6058 /* global asm block */
6059 asm_global_instr();
6060 continue;
6062 /* special test for old K&R protos without explicit int
6063 type. Only accepted when defining global data */
6064 if (l == VT_LOCAL || tok < TOK_DEFINE)
6065 break;
6066 btype.t = VT_INT;
6068 if (((btype.t & VT_BTYPE) == VT_ENUM ||
6069 (btype.t & VT_BTYPE) == VT_STRUCT) &&
6070 tok == ';') {
6071 /* we accept no variable after */
6072 next();
6073 continue;
6075 while (1) { /* iterate thru each declaration */
6076 char *asm_label; // associated asm label
6077 type = btype;
6078 type_decl(&type, &ad, &v, TYPE_DIRECT);
6079 #if 0
6081 char buf[500];
6082 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
6083 printf("type = '%s'\n", buf);
6085 #endif
6086 if ((type.t & VT_BTYPE) == VT_FUNC) {
6087 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
6088 tcc_error("function without file scope cannot be static");
6090 /* if old style function prototype, we accept a
6091 declaration list */
6092 sym = type.ref;
6093 if (sym->c == FUNC_OLD)
6094 func_decl_list(sym);
6097 asm_label = NULL;
6098 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6099 CString astr;
6101 asm_label_instr(&astr);
6102 asm_label = tcc_strdup(astr.data);
6103 cstr_free(&astr);
6105 /* parse one last attribute list, after asm label */
6106 parse_attribute(&ad);
6109 if (ad.a.weak)
6110 type.t |= VT_WEAK;
6111 #ifdef TCC_TARGET_PE
6112 if (ad.a.func_import)
6113 type.t |= VT_IMPORT;
6114 if (ad.a.func_export)
6115 type.t |= VT_EXPORT;
6116 #endif
6117 type.t |= ad.a.visibility << VT_VIS_SHIFT;
6119 if (tok == '{') {
6120 if (l == VT_LOCAL)
6121 tcc_error("cannot use local functions");
6122 if ((type.t & VT_BTYPE) != VT_FUNC)
6123 expect("function definition");
6125 /* reject abstract declarators in function definition */
6126 sym = type.ref;
6127 while ((sym = sym->next) != NULL)
6128 if (!(sym->v & ~SYM_FIELD))
6129 expect("identifier");
6131 /* XXX: cannot do better now: convert extern line to static inline */
6132 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
6133 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6135 sym = sym_find(v);
6136 if (sym) {
6137 Sym *ref;
6138 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
6139 goto func_error1;
6141 ref = sym->type.ref;
6142 if (0 == ref->a.func_proto)
6143 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
6145 /* use func_call from prototype if not defined */
6146 if (ref->a.func_call != FUNC_CDECL
6147 && type.ref->a.func_call == FUNC_CDECL)
6148 type.ref->a.func_call = ref->a.func_call;
6150 /* use export from prototype */
6151 if (ref->a.func_export)
6152 type.ref->a.func_export = 1;
6154 /* use static from prototype */
6155 if (sym->type.t & VT_STATIC)
6156 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6158 /* If the definition has no visibility use the
6159 one from prototype. */
6160 if (! (type.t & VT_VIS_MASK))
6161 type.t |= sym->type.t & VT_VIS_MASK;
6163 if (!is_compatible_types(&sym->type, &type)) {
6164 func_error1:
6165 tcc_error("incompatible types for redefinition of '%s'",
6166 get_tok_str(v, NULL));
6168 type.ref->a.func_proto = 0;
6169 /* if symbol is already defined, then put complete type */
6170 sym->type = type;
6171 } else {
6172 /* put function symbol */
6173 sym = global_identifier_push(v, type.t, 0);
6174 sym->type.ref = type.ref;
6177 /* static inline functions are just recorded as a kind
6178 of macro. Their code will be emitted at the end of
6179 the compilation unit only if they are used */
6180 if ((type.t & (VT_INLINE | VT_STATIC)) ==
6181 (VT_INLINE | VT_STATIC)) {
6182 TokenString func_str;
6183 int block_level;
6184 struct InlineFunc *fn;
6185 const char *filename;
6187 tok_str_new(&func_str);
6189 block_level = 0;
6190 for(;;) {
6191 int t;
6192 if (tok == TOK_EOF)
6193 tcc_error("unexpected end of file");
6194 tok_str_add_tok(&func_str);
6195 t = tok;
6196 next();
6197 if (t == '{') {
6198 block_level++;
6199 } else if (t == '}') {
6200 block_level--;
6201 if (block_level == 0)
6202 break;
6205 tok_str_add(&func_str, -1);
6206 tok_str_add(&func_str, 0);
6207 filename = file ? file->filename : "";
6208 fn = tcc_malloc(sizeof *fn + strlen(filename));
6209 strcpy(fn->filename, filename);
6210 fn->sym = sym;
6211 fn->token_str = func_str.str;
6212 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
6214 } else {
6215 /* compute text section */
6216 cur_text_section = ad.section;
6217 if (!cur_text_section)
6218 cur_text_section = text_section;
6219 sym->r = VT_SYM | VT_CONST;
6220 gen_function(sym);
6222 break;
6223 } else {
6224 if (btype.t & VT_TYPEDEF) {
6225 /* save typedefed type */
6226 /* XXX: test storage specifiers ? */
6227 sym = sym_push(v, &type, 0, 0);
6228 sym->a = ad.a;
6229 sym->type.t |= VT_TYPEDEF;
6230 } else {
6231 r = 0;
6232 if ((type.t & VT_BTYPE) == VT_FUNC) {
6233 /* external function definition */
6234 /* specific case for func_call attribute */
6235 ad.a.func_proto = 1;
6236 type.ref->a = ad.a;
6237 } else if (!(type.t & VT_ARRAY)) {
6238 /* not lvalue if array */
6239 r |= lvalue_type(type.t);
6241 has_init = (tok == '=');
6242 if (has_init && (type.t & VT_VLA))
6243 tcc_error("Variable length array cannot be initialized");
6244 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6245 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6246 !has_init && l == VT_CONST && type.ref->c < 0)) {
6247 /* external variable or function */
6248 /* NOTE: as GCC, uninitialized global static
6249 arrays of null size are considered as
6250 extern */
6251 sym = external_sym(v, &type, r, asm_label);
6253 if (ad.alias_target) {
6254 Section tsec;
6255 Elf32_Sym *esym;
6256 Sym *alias_target;
6258 alias_target = sym_find(ad.alias_target);
6259 if (!alias_target || !alias_target->c)
6260 tcc_error("unsupported forward __alias__ attribute");
6261 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6262 tsec.sh_num = esym->st_shndx;
6263 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6265 } else {
6266 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6267 if (type.t & VT_STATIC)
6268 r |= VT_CONST;
6269 else
6270 r |= l;
6271 if (has_init)
6272 next();
6273 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6276 if (tok != ',') {
6277 if (is_for_loop_init)
6278 return 1;
6279 skip(';');
6280 break;
6282 next();
6284 ad.a.aligned = 0;
6287 return 0;
6290 ST_FUNC void decl(int l)
6292 decl0(l, 0);