Fix negation of 0.0 and -0.0
[tinycc.git] / tccgen.c
blobe8f7f82ae37d33610eb2f545479f04e2f353d097
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
31 ST_DATA int rsym, anon_sym, ind, loc;
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *scope_stack_bottom;
54 ST_DATA Sym *define_stack;
55 ST_DATA Sym *global_label_stack;
56 ST_DATA Sym *local_label_stack;
58 ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA int vla_flags; /* VLA_* flags */
63 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
65 ST_DATA int const_wanted; /* true if constant wanted */
66 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
67 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
69 ST_DATA int func_vc;
70 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
71 ST_DATA char *funcname;
73 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
75 /* ------------------------------------------------------------------------- */
76 static void gen_cast(CType *type);
77 static inline CType *pointed_type(CType *type);
78 static int is_compatible_types(CType *type1, CType *type2);
79 static int parse_btype(CType *type, AttributeDef *ad);
80 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
81 static void parse_expr_type(CType *type);
82 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
83 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
84 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
85 static int decl0(int l, int is_for_loop_init);
86 static void expr_eq(void);
87 static void unary_type(CType *type);
88 static void vla_runtime_type_size(CType *type, int *a);
89 static void vla_sp_save(void);
90 static int is_compatible_parameter_types(CType *type1, CType *type2);
91 static void expr_type(CType *type);
93 ST_INLN int is_float(int t)
95 int bt;
96 bt = t & VT_BTYPE;
97 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
100 /* we use our own 'finite' function to avoid potential problems with
101 non standard math libs */
102 /* XXX: endianness dependent */
103 ST_FUNC int ieee_finite(double d)
105 int *p = (int *)&d;
106 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
109 ST_FUNC void test_lvalue(void)
111 if (!(vtop->r & VT_LVAL))
112 expect("lvalue");
115 /* ------------------------------------------------------------------------- */
116 /* symbol allocator */
117 static Sym *__sym_malloc(void)
119 Sym *sym_pool, *sym, *last_sym;
120 int i;
122 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
123 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
125 last_sym = sym_free_first;
126 sym = sym_pool;
127 for(i = 0; i < SYM_POOL_NB; i++) {
128 sym->next = last_sym;
129 last_sym = sym;
130 sym++;
132 sym_free_first = last_sym;
133 return last_sym;
136 static inline Sym *sym_malloc(void)
138 Sym *sym;
139 sym = sym_free_first;
140 if (!sym)
141 sym = __sym_malloc();
142 sym_free_first = sym->next;
143 return sym;
146 ST_INLN void sym_free(Sym *sym)
148 sym->next = sym_free_first;
149 tcc_free(sym->asm_label);
150 sym_free_first = sym;
153 /* push, without hashing */
154 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
156 Sym *s;
157 if (ps == &local_stack) {
158 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
159 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
160 tcc_error("incompatible types for redefinition of '%s'",
161 get_tok_str(v, NULL));
163 s = *ps;
164 s = sym_malloc();
165 s->asm_label = NULL;
166 s->v = v;
167 s->type.t = t;
168 s->type.ref = NULL;
169 #ifdef _WIN64
170 s->d = NULL;
171 #endif
172 s->c = c;
173 s->next = NULL;
174 /* add in stack */
175 s->prev = *ps;
176 *ps = s;
177 return s;
180 /* find a symbol and return its associated structure. 's' is the top
181 of the symbol stack */
182 ST_FUNC Sym *sym_find2(Sym *s, int v)
184 while (s) {
185 if (s->v == v)
186 return s;
187 s = s->prev;
189 return NULL;
192 /* structure lookup */
193 ST_INLN Sym *struct_find(int v)
195 v -= TOK_IDENT;
196 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
197 return NULL;
198 return table_ident[v]->sym_struct;
201 /* find an identifier */
202 ST_INLN Sym *sym_find(int v)
204 v -= TOK_IDENT;
205 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
206 return NULL;
207 return table_ident[v]->sym_identifier;
210 /* push a given symbol on the symbol stack */
211 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
213 Sym *s, **ps;
214 TokenSym *ts;
216 if (local_stack)
217 ps = &local_stack;
218 else
219 ps = &global_stack;
220 s = sym_push2(ps, v, type->t, c);
221 s->type.ref = type->ref;
222 s->r = r;
223 /* don't record fields or anonymous symbols */
224 /* XXX: simplify */
225 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
226 /* record symbol in token array */
227 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
228 if (v & SYM_STRUCT)
229 ps = &ts->sym_struct;
230 else
231 ps = &ts->sym_identifier;
232 s->prev_tok = *ps;
233 *ps = s;
235 return s;
238 /* push a global identifier */
239 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
241 Sym *s, **ps;
242 s = sym_push2(&global_stack, v, t, c);
243 /* don't record anonymous symbol */
244 if (v < SYM_FIRST_ANOM) {
245 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
246 /* modify the top most local identifier, so that
247 sym_identifier will point to 's' when popped */
248 while (*ps != NULL)
249 ps = &(*ps)->prev_tok;
250 s->prev_tok = NULL;
251 *ps = s;
253 return s;
256 /* pop symbols until top reaches 'b' */
257 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
259 Sym *s, *ss, **ps;
260 TokenSym *ts;
261 int v;
263 s = *ptop;
264 while(s != b) {
265 ss = s->prev;
266 v = s->v;
267 /* remove symbol in token array */
268 /* XXX: simplify */
269 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
270 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
271 if (v & SYM_STRUCT)
272 ps = &ts->sym_struct;
273 else
274 ps = &ts->sym_identifier;
275 *ps = s->prev_tok;
277 sym_free(s);
278 s = ss;
280 *ptop = b;
283 static void weaken_symbol(Sym *sym)
285 sym->type.t |= VT_WEAK;
286 if (sym->c > 0) {
287 int esym_type;
288 ElfW(Sym) *esym;
290 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
291 esym_type = ELFW(ST_TYPE)(esym->st_info);
292 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
296 /* ------------------------------------------------------------------------- */
298 ST_FUNC void swap(int *p, int *q)
300 int t;
301 t = *p;
302 *p = *q;
303 *q = t;
306 static void vsetc(CType *type, int r, CValue *vc)
308 int v;
310 if (vtop >= vstack + (VSTACK_SIZE - 1))
311 tcc_error("memory full");
312 /* cannot let cpu flags if other instruction are generated. Also
313 avoid leaving VT_JMP anywhere except on the top of the stack
314 because it would complicate the code generator. */
315 if (vtop >= vstack) {
316 v = vtop->r & VT_VALMASK;
317 if (v == VT_CMP || (v & ~1) == VT_JMP)
318 gv(RC_INT);
320 vtop++;
321 vtop->type = *type;
322 vtop->r = r;
323 vtop->r2 = VT_CONST;
324 vtop->c = *vc;
327 /* push constant of type "type" with useless value */
328 void vpush(CType *type)
330 CValue cval;
331 vsetc(type, VT_CONST, &cval);
334 /* push integer constant */
335 ST_FUNC void vpushi(int v)
337 CValue cval;
338 cval.i = v;
339 vsetc(&int_type, VT_CONST, &cval);
342 /* push a pointer sized constant */
343 static void vpushs(long long v)
345 CValue cval;
346 if (PTR_SIZE == 4)
347 cval.i = (int)v;
348 else
349 cval.ull = v;
350 vsetc(&size_type, VT_CONST, &cval);
353 /* push arbitrary 64bit constant */
354 void vpush64(int ty, unsigned long long v)
356 CValue cval;
357 CType ctype;
358 ctype.t = ty;
359 ctype.ref = NULL;
360 cval.ull = v;
361 vsetc(&ctype, VT_CONST, &cval);
364 /* push long long constant */
365 static inline void vpushll(long long v)
367 vpush64(VT_LLONG, v);
370 /* Return a static symbol pointing to a section */
371 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
373 int v;
374 Sym *sym;
376 v = anon_sym++;
377 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
378 sym->type.ref = type->ref;
379 sym->r = VT_CONST | VT_SYM;
380 put_extern_sym(sym, sec, offset, size);
381 return sym;
384 /* push a reference to a section offset by adding a dummy symbol */
385 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
387 CValue cval;
389 cval.ul = 0;
390 vsetc(type, VT_CONST | VT_SYM, &cval);
391 vtop->sym = get_sym_ref(type, sec, offset, size);
394 /* define a new external reference to a symbol 'v' of type 'u' */
395 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
397 Sym *s;
399 s = sym_find(v);
400 if (!s) {
401 /* push forward reference */
402 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
403 s->type.ref = type->ref;
404 s->r = r | VT_CONST | VT_SYM;
406 return s;
409 /* define a new external reference to a symbol 'v' with alternate asm
410 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
411 is no alternate name (most cases) */
412 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
414 Sym *s;
416 s = sym_find(v);
417 if (!s) {
418 /* push forward reference */
419 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
420 s->asm_label = asm_label;
421 s->type.t |= VT_EXTERN;
422 } else if (s->type.ref == func_old_type.ref) {
423 s->type.ref = type->ref;
424 s->r = r | VT_CONST | VT_SYM;
425 s->type.t |= VT_EXTERN;
426 } else if (!is_compatible_types(&s->type, type)) {
427 tcc_error("incompatible types for redefinition of '%s'",
428 get_tok_str(v, NULL));
430 return s;
433 /* push a reference to global symbol v */
434 ST_FUNC void vpush_global_sym(CType *type, int v)
436 Sym *sym;
437 CValue cval;
439 sym = external_global_sym(v, type, 0);
440 cval.ul = 0;
441 vsetc(type, VT_CONST | VT_SYM, &cval);
442 vtop->sym = sym;
445 ST_FUNC void vset(CType *type, int r, int v)
447 CValue cval;
449 cval.i = v;
450 vsetc(type, r, &cval);
453 static void vseti(int r, int v)
455 CType type;
456 type.t = VT_INT;
457 type.ref = 0;
458 vset(&type, r, v);
461 ST_FUNC void vswap(void)
463 SValue tmp;
464 /* cannot let cpu flags if other instruction are generated. Also
465 avoid leaving VT_JMP anywhere except on the top of the stack
466 because it would complicate the code generator. */
467 if (vtop >= vstack) {
468 int v = vtop->r & VT_VALMASK;
469 if (v == VT_CMP || (v & ~1) == VT_JMP)
470 gv(RC_INT);
472 tmp = vtop[0];
473 vtop[0] = vtop[-1];
474 vtop[-1] = tmp;
476 /* XXX: +2% overall speed possible with optimized memswap
478 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
482 ST_FUNC void vpushv(SValue *v)
484 if (vtop >= vstack + (VSTACK_SIZE - 1))
485 tcc_error("memory full");
486 vtop++;
487 *vtop = *v;
490 static void vdup(void)
492 vpushv(vtop);
495 /* save r to the memory stack, and mark it as being free */
496 ST_FUNC void save_reg(int r)
498 int l, saved, size, align;
499 SValue *p, sv;
500 CType *type;
502 /* modify all stack values */
503 saved = 0;
504 l = 0;
505 for(p=vstack;p<=vtop;p++) {
506 if ((p->r & VT_VALMASK) == r ||
507 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
508 /* must save value on stack if not already done */
509 if (!saved) {
510 /* NOTE: must reload 'r' because r might be equal to r2 */
511 r = p->r & VT_VALMASK;
512 /* store register in the stack */
513 type = &p->type;
514 if ((p->r & VT_LVAL) ||
515 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
516 #ifdef TCC_TARGET_X86_64
517 type = &char_pointer_type;
518 #else
519 type = &int_type;
520 #endif
521 size = type_size(type, &align);
522 loc = (loc - size) & -align;
523 sv.type.t = type->t;
524 sv.r = VT_LOCAL | VT_LVAL;
525 sv.c.ul = loc;
526 store(r, &sv);
527 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
528 /* x86 specific: need to pop fp register ST0 if saved */
529 if (r == TREG_ST0) {
530 o(0xd8dd); /* fstp %st(0) */
532 #endif
533 #ifndef TCC_TARGET_X86_64
534 /* special long long case */
535 if ((type->t & VT_BTYPE) == VT_LLONG) {
536 sv.c.ul += 4;
537 store(p->r2, &sv);
539 #endif
540 l = loc;
541 saved = 1;
543 /* mark that stack entry as being saved on the stack */
544 if (p->r & VT_LVAL) {
545 /* also clear the bounded flag because the
546 relocation address of the function was stored in
547 p->c.ul */
548 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
549 } else {
550 p->r = lvalue_type(p->type.t) | VT_LOCAL;
552 p->r2 = VT_CONST;
553 p->c.ul = l;
558 #ifdef TCC_TARGET_ARM
559 /* find a register of class 'rc2' with at most one reference on stack.
560 * If none, call get_reg(rc) */
561 ST_FUNC int get_reg_ex(int rc, int rc2)
563 int r;
564 SValue *p;
566 for(r=0;r<NB_REGS;r++) {
567 if (reg_classes[r] & rc2) {
568 int n;
569 n=0;
570 for(p = vstack; p <= vtop; p++) {
571 if ((p->r & VT_VALMASK) == r ||
572 (p->r2 & VT_VALMASK) == r)
573 n++;
575 if (n <= 1)
576 return r;
579 return get_reg(rc);
581 #endif
583 /* find a free register of class 'rc'. If none, save one register */
584 ST_FUNC int get_reg(int rc)
586 int r;
587 SValue *p;
589 /* find a free register */
590 for(r=0;r<NB_REGS;r++) {
591 if (reg_classes[r] & rc) {
592 for(p=vstack;p<=vtop;p++) {
593 if ((p->r & VT_VALMASK) == r ||
594 (p->r2 & VT_VALMASK) == r)
595 goto notfound;
597 return r;
599 notfound: ;
602 /* no register left : free the first one on the stack (VERY
603 IMPORTANT to start from the bottom to ensure that we don't
604 spill registers used in gen_opi()) */
605 for(p=vstack;p<=vtop;p++) {
606 /* look at second register (if long long) */
607 r = p->r2 & VT_VALMASK;
608 if (r < VT_CONST && (reg_classes[r] & rc))
609 goto save_found;
610 r = p->r & VT_VALMASK;
611 if (r < VT_CONST && (reg_classes[r] & rc)) {
612 save_found:
613 save_reg(r);
614 return r;
617 /* Should never comes here */
618 return -1;
621 /* save registers up to (vtop - n) stack entry */
622 ST_FUNC void save_regs(int n)
624 int r;
625 SValue *p, *p1;
626 p1 = vtop - n;
627 for(p = vstack;p <= p1; p++) {
628 r = p->r & VT_VALMASK;
629 if (r < VT_CONST) {
630 save_reg(r);
635 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
636 if needed */
637 static void move_reg(int r, int s, int t)
639 SValue sv;
641 if (r != s) {
642 save_reg(r);
643 sv.type.t = t;
644 sv.type.ref = NULL;
645 sv.r = s;
646 sv.c.ul = 0;
647 load(r, &sv);
651 /* get address of vtop (vtop MUST BE an lvalue) */
652 static void gaddrof(void)
654 if (vtop->r & VT_REF)
655 gv(RC_INT);
656 vtop->r &= ~VT_LVAL;
657 /* tricky: if saved lvalue, then we can go back to lvalue */
658 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
659 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
664 #ifdef CONFIG_TCC_BCHECK
665 /* generate lvalue bound code */
666 static void gbound(void)
668 int lval_type;
669 CType type1;
671 vtop->r &= ~VT_MUSTBOUND;
672 /* if lvalue, then use checking code before dereferencing */
673 if (vtop->r & VT_LVAL) {
674 /* if not VT_BOUNDED value, then make one */
675 if (!(vtop->r & VT_BOUNDED)) {
676 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
677 /* must save type because we must set it to int to get pointer */
678 type1 = vtop->type;
679 vtop->type.t = VT_INT;
680 gaddrof();
681 vpushi(0);
682 gen_bounded_ptr_add();
683 vtop->r |= lval_type;
684 vtop->type = type1;
686 /* then check for dereferencing */
687 gen_bounded_ptr_deref();
690 #endif
692 /* store vtop a register belonging to class 'rc'. lvalues are
693 converted to values. Cannot be used if cannot be converted to
694 register value (such as structures). */
695 ST_FUNC int gv(int rc)
697 int r, bit_pos, bit_size, size, align, i;
698 int rc2;
700 /* NOTE: get_reg can modify vstack[] */
701 if (vtop->type.t & VT_BITFIELD) {
702 CType type;
703 int bits = 32;
704 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
705 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
706 /* remove bit field info to avoid loops */
707 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
708 /* cast to int to propagate signedness in following ops */
709 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
710 type.t = VT_LLONG;
711 bits = 64;
712 } else
713 type.t = VT_INT;
714 if((vtop->type.t & VT_UNSIGNED) ||
715 (vtop->type.t & VT_BTYPE) == VT_BOOL)
716 type.t |= VT_UNSIGNED;
717 gen_cast(&type);
718 /* generate shifts */
719 vpushi(bits - (bit_pos + bit_size));
720 gen_op(TOK_SHL);
721 vpushi(bits - bit_size);
722 /* NOTE: transformed to SHR if unsigned */
723 gen_op(TOK_SAR);
724 r = gv(rc);
725 } else {
726 if (is_float(vtop->type.t) &&
727 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
728 Sym *sym;
729 int *ptr;
730 unsigned long offset;
731 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
732 CValue check;
733 #endif
735 /* XXX: unify with initializers handling ? */
736 /* CPUs usually cannot use float constants, so we store them
737 generically in data segment */
738 size = type_size(&vtop->type, &align);
739 offset = (data_section->data_offset + align - 1) & -align;
740 data_section->data_offset = offset;
741 /* XXX: not portable yet */
742 #if defined(__i386__) || defined(__x86_64__)
743 /* Zero pad x87 tenbyte long doubles */
744 if (size == LDOUBLE_SIZE) {
745 vtop->c.tab[2] &= 0xffff;
746 #if LDOUBLE_SIZE == 16
747 vtop->c.tab[3] = 0;
748 #endif
750 #endif
751 ptr = section_ptr_add(data_section, size);
752 size = size >> 2;
753 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
754 check.d = 1;
755 if(check.tab[0])
756 for(i=0;i<size;i++)
757 ptr[i] = vtop->c.tab[size-1-i];
758 else
759 #endif
760 for(i=0;i<size;i++)
761 ptr[i] = vtop->c.tab[i];
762 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
763 vtop->r |= VT_LVAL | VT_SYM;
764 vtop->sym = sym;
765 vtop->c.ul = 0;
767 #ifdef CONFIG_TCC_BCHECK
768 if (vtop->r & VT_MUSTBOUND)
769 gbound();
770 #endif
772 r = vtop->r & VT_VALMASK;
773 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
774 if (rc == RC_IRET)
775 rc2 = RC_LRET;
776 #ifdef TCC_TARGET_X86_64
777 else if (rc == RC_FRET)
778 rc2 = RC_QRET;
779 #endif
781 /* need to reload if:
782 - constant
783 - lvalue (need to dereference pointer)
784 - already a register, but not in the right class */
785 if (r >= VT_CONST
786 || (vtop->r & VT_LVAL)
787 || !(reg_classes[r] & rc)
788 #ifdef TCC_TARGET_X86_64
789 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
790 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
791 #else
792 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
793 #endif
796 r = get_reg(rc);
797 #ifdef TCC_TARGET_X86_64
798 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
799 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
800 #else
801 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
802 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
803 unsigned long long ll;
804 #endif
805 int r2, original_type;
806 original_type = vtop->type.t;
807 /* two register type load : expand to two words
808 temporarily */
809 #ifndef TCC_TARGET_X86_64
810 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
811 /* load constant */
812 ll = vtop->c.ull;
813 vtop->c.ui = ll; /* first word */
814 load(r, vtop);
815 vtop->r = r; /* save register value */
816 vpushi(ll >> 32); /* second word */
817 } else
818 #endif
819 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
820 (vtop->r & VT_LVAL)) {
821 /* We do not want to modifier the long long
822 pointer here, so the safest (and less
823 efficient) is to save all the other registers
824 in the stack. XXX: totally inefficient. */
825 save_regs(1);
826 /* load from memory */
827 vtop->type.t = load_type;
828 load(r, vtop);
829 vdup();
830 vtop[-1].r = r; /* save register value */
831 /* increment pointer to get second word */
832 vtop->type.t = addr_type;
833 gaddrof();
834 vpushi(load_size);
835 gen_op('+');
836 vtop->r |= VT_LVAL;
837 vtop->type.t = load_type;
838 } else {
839 /* move registers */
840 load(r, vtop);
841 vdup();
842 vtop[-1].r = r; /* save register value */
843 vtop->r = vtop[-1].r2;
845 /* Allocate second register. Here we rely on the fact that
846 get_reg() tries first to free r2 of an SValue. */
847 r2 = get_reg(rc2);
848 load(r2, vtop);
849 vpop();
850 /* write second register */
851 vtop->r2 = r2;
852 vtop->type.t = original_type;
853 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
854 int t1, t;
855 /* lvalue of scalar type : need to use lvalue type
856 because of possible cast */
857 t = vtop->type.t;
858 t1 = t;
859 /* compute memory access type */
860 if (vtop->r & VT_REF)
861 #ifdef TCC_TARGET_X86_64
862 t = VT_PTR;
863 #else
864 t = VT_INT;
865 #endif
866 else if (vtop->r & VT_LVAL_BYTE)
867 t = VT_BYTE;
868 else if (vtop->r & VT_LVAL_SHORT)
869 t = VT_SHORT;
870 if (vtop->r & VT_LVAL_UNSIGNED)
871 t |= VT_UNSIGNED;
872 vtop->type.t = t;
873 load(r, vtop);
874 /* restore wanted type */
875 vtop->type.t = t1;
876 } else {
877 /* one register type load */
878 load(r, vtop);
881 vtop->r = r;
882 #ifdef TCC_TARGET_C67
883 /* uses register pairs for doubles */
884 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
885 vtop->r2 = r+1;
886 #endif
888 return r;
891 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
892 ST_FUNC void gv2(int rc1, int rc2)
894 int v;
896 /* generate more generic register first. But VT_JMP or VT_CMP
897 values must be generated first in all cases to avoid possible
898 reload errors */
899 v = vtop[0].r & VT_VALMASK;
900 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
901 vswap();
902 gv(rc1);
903 vswap();
904 gv(rc2);
905 /* test if reload is needed for first register */
906 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
907 vswap();
908 gv(rc1);
909 vswap();
911 } else {
912 gv(rc2);
913 vswap();
914 gv(rc1);
915 vswap();
916 /* test if reload is needed for first register */
917 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
918 gv(rc2);
923 /* wrapper around RC_FRET to return a register by type */
924 static int rc_fret(int t)
926 #ifdef TCC_TARGET_X86_64
927 if (t == VT_LDOUBLE) {
928 return RC_ST0;
930 #endif
931 return RC_FRET;
934 /* wrapper around REG_FRET to return a register by type */
935 static int reg_fret(int t)
937 #ifdef TCC_TARGET_X86_64
938 if (t == VT_LDOUBLE) {
939 return TREG_ST0;
941 #endif
942 return REG_FRET;
945 /* expand long long on stack in two int registers */
946 static void lexpand(void)
948 int u;
950 u = vtop->type.t & VT_UNSIGNED;
951 gv(RC_INT);
952 vdup();
953 vtop[0].r = vtop[-1].r2;
954 vtop[0].r2 = VT_CONST;
955 vtop[-1].r2 = VT_CONST;
956 vtop[0].type.t = VT_INT | u;
957 vtop[-1].type.t = VT_INT | u;
960 #ifdef TCC_TARGET_ARM
961 /* expand long long on stack */
962 ST_FUNC void lexpand_nr(void)
964 int u,v;
966 u = vtop->type.t & VT_UNSIGNED;
967 vdup();
968 vtop->r2 = VT_CONST;
969 vtop->type.t = VT_INT | u;
970 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
971 if (v == VT_CONST) {
972 vtop[-1].c.ui = vtop->c.ull;
973 vtop->c.ui = vtop->c.ull >> 32;
974 vtop->r = VT_CONST;
975 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
976 vtop->c.ui += 4;
977 vtop->r = vtop[-1].r;
978 } else if (v > VT_CONST) {
979 vtop--;
980 lexpand();
981 } else
982 vtop->r = vtop[-1].r2;
983 vtop[-1].r2 = VT_CONST;
984 vtop[-1].type.t = VT_INT | u;
986 #endif
988 /* build a long long from two ints */
989 static void lbuild(int t)
991 gv2(RC_INT, RC_INT);
992 vtop[-1].r2 = vtop[0].r;
993 vtop[-1].type.t = t;
994 vpop();
997 /* rotate n first stack elements to the bottom
998 I1 ... In -> I2 ... In I1 [top is right]
1000 ST_FUNC void vrotb(int n)
1002 int i;
1003 SValue tmp;
1005 tmp = vtop[-n + 1];
1006 for(i=-n+1;i!=0;i++)
1007 vtop[i] = vtop[i+1];
1008 vtop[0] = tmp;
1011 /* rotate the n elements before entry e towards the top
1012 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1014 ST_FUNC void vrote(SValue *e, int n)
1016 int i;
1017 SValue tmp;
1019 tmp = *e;
1020 for(i = 0;i < n - 1; i++)
1021 e[-i] = e[-i - 1];
1022 e[-n + 1] = tmp;
1025 /* rotate n first stack elements to the top
1026 I1 ... In -> In I1 ... I(n-1) [top is right]
1028 ST_FUNC void vrott(int n)
1030 vrote(vtop, n);
1033 /* pop stack value */
1034 ST_FUNC void vpop(void)
1036 int v;
1037 v = vtop->r & VT_VALMASK;
1038 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1039 /* for x86, we need to pop the FP stack */
1040 if (v == TREG_ST0 && !nocode_wanted) {
1041 o(0xd8dd); /* fstp %st(0) */
1042 } else
1043 #endif
1044 if (v == VT_JMP || v == VT_JMPI) {
1045 /* need to put correct jump if && or || without test */
1046 gsym(vtop->c.ul);
1048 vtop--;
1051 /* convert stack entry to register and duplicate its value in another
1052 register */
1053 static void gv_dup(void)
1055 int rc, t, r, r1;
1056 SValue sv;
1058 t = vtop->type.t;
1059 if ((t & VT_BTYPE) == VT_LLONG) {
1060 lexpand();
1061 gv_dup();
1062 vswap();
1063 vrotb(3);
1064 gv_dup();
1065 vrotb(4);
1066 /* stack: H L L1 H1 */
1067 lbuild(t);
1068 vrotb(3);
1069 vrotb(3);
1070 vswap();
1071 lbuild(t);
1072 vswap();
1073 } else {
1074 /* duplicate value */
1075 rc = RC_INT;
1076 sv.type.t = VT_INT;
1077 if (is_float(t)) {
1078 rc = RC_FLOAT;
1079 #ifdef TCC_TARGET_X86_64
1080 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1081 rc = RC_ST0;
1083 #endif
1084 sv.type.t = t;
1086 r = gv(rc);
1087 r1 = get_reg(rc);
1088 sv.r = r;
1089 sv.c.ul = 0;
1090 load(r1, &sv); /* move r to r1 */
1091 vdup();
1092 /* duplicates value */
1093 if (r != r1)
1094 vtop->r = r1;
1098 #ifndef TCC_TARGET_X86_64
1099 /* generate CPU independent (unsigned) long long operations */
1100 static void gen_opl(int op)
1102 int t, a, b, op1, c, i;
1103 int func;
1104 unsigned short reg_iret = REG_IRET;
1105 unsigned short reg_lret = REG_LRET;
1106 SValue tmp;
1108 switch(op) {
1109 case '/':
1110 case TOK_PDIV:
1111 func = TOK___divdi3;
1112 goto gen_func;
1113 case TOK_UDIV:
1114 func = TOK___udivdi3;
1115 goto gen_func;
1116 case '%':
1117 func = TOK___moddi3;
1118 goto gen_mod_func;
1119 case TOK_UMOD:
1120 func = TOK___umoddi3;
1121 gen_mod_func:
1122 #ifdef TCC_ARM_EABI
1123 reg_iret = TREG_R2;
1124 reg_lret = TREG_R3;
1125 #endif
1126 gen_func:
1127 /* call generic long long function */
1128 vpush_global_sym(&func_old_type, func);
1129 vrott(3);
1130 gfunc_call(2);
1131 vpushi(0);
1132 vtop->r = reg_iret;
1133 vtop->r2 = reg_lret;
1134 break;
1135 case '^':
1136 case '&':
1137 case '|':
1138 case '*':
1139 case '+':
1140 case '-':
1141 t = vtop->type.t;
1142 vswap();
1143 lexpand();
1144 vrotb(3);
1145 lexpand();
1146 /* stack: L1 H1 L2 H2 */
1147 tmp = vtop[0];
1148 vtop[0] = vtop[-3];
1149 vtop[-3] = tmp;
1150 tmp = vtop[-2];
1151 vtop[-2] = vtop[-3];
1152 vtop[-3] = tmp;
1153 vswap();
1154 /* stack: H1 H2 L1 L2 */
1155 if (op == '*') {
1156 vpushv(vtop - 1);
1157 vpushv(vtop - 1);
1158 gen_op(TOK_UMULL);
1159 lexpand();
1160 /* stack: H1 H2 L1 L2 ML MH */
1161 for(i=0;i<4;i++)
1162 vrotb(6);
1163 /* stack: ML MH H1 H2 L1 L2 */
1164 tmp = vtop[0];
1165 vtop[0] = vtop[-2];
1166 vtop[-2] = tmp;
1167 /* stack: ML MH H1 L2 H2 L1 */
1168 gen_op('*');
1169 vrotb(3);
1170 vrotb(3);
1171 gen_op('*');
1172 /* stack: ML MH M1 M2 */
1173 gen_op('+');
1174 gen_op('+');
1175 } else if (op == '+' || op == '-') {
1176 /* XXX: add non carry method too (for MIPS or alpha) */
1177 if (op == '+')
1178 op1 = TOK_ADDC1;
1179 else
1180 op1 = TOK_SUBC1;
1181 gen_op(op1);
1182 /* stack: H1 H2 (L1 op L2) */
1183 vrotb(3);
1184 vrotb(3);
1185 gen_op(op1 + 1); /* TOK_xxxC2 */
1186 } else {
1187 gen_op(op);
1188 /* stack: H1 H2 (L1 op L2) */
1189 vrotb(3);
1190 vrotb(3);
1191 /* stack: (L1 op L2) H1 H2 */
1192 gen_op(op);
1193 /* stack: (L1 op L2) (H1 op H2) */
1195 /* stack: L H */
1196 lbuild(t);
1197 break;
1198 case TOK_SAR:
1199 case TOK_SHR:
1200 case TOK_SHL:
1201 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1202 t = vtop[-1].type.t;
1203 vswap();
1204 lexpand();
1205 vrotb(3);
1206 /* stack: L H shift */
1207 c = (int)vtop->c.i;
1208 /* constant: simpler */
1209 /* NOTE: all comments are for SHL. the other cases are
1210 done by swaping words */
1211 vpop();
1212 if (op != TOK_SHL)
1213 vswap();
1214 if (c >= 32) {
1215 /* stack: L H */
1216 vpop();
1217 if (c > 32) {
1218 vpushi(c - 32);
1219 gen_op(op);
1221 if (op != TOK_SAR) {
1222 vpushi(0);
1223 } else {
1224 gv_dup();
1225 vpushi(31);
1226 gen_op(TOK_SAR);
1228 vswap();
1229 } else {
1230 vswap();
1231 gv_dup();
1232 /* stack: H L L */
1233 vpushi(c);
1234 gen_op(op);
1235 vswap();
1236 vpushi(32 - c);
1237 if (op == TOK_SHL)
1238 gen_op(TOK_SHR);
1239 else
1240 gen_op(TOK_SHL);
1241 vrotb(3);
1242 /* stack: L L H */
1243 vpushi(c);
1244 if (op == TOK_SHL)
1245 gen_op(TOK_SHL);
1246 else
1247 gen_op(TOK_SHR);
1248 gen_op('|');
1250 if (op != TOK_SHL)
1251 vswap();
1252 lbuild(t);
1253 } else {
1254 /* XXX: should provide a faster fallback on x86 ? */
1255 switch(op) {
1256 case TOK_SAR:
1257 func = TOK___ashrdi3;
1258 goto gen_func;
1259 case TOK_SHR:
1260 func = TOK___lshrdi3;
1261 goto gen_func;
1262 case TOK_SHL:
1263 func = TOK___ashldi3;
1264 goto gen_func;
1267 break;
1268 default:
1269 /* compare operations */
1270 t = vtop->type.t;
1271 vswap();
1272 lexpand();
1273 vrotb(3);
1274 lexpand();
1275 /* stack: L1 H1 L2 H2 */
1276 tmp = vtop[-1];
1277 vtop[-1] = vtop[-2];
1278 vtop[-2] = tmp;
1279 /* stack: L1 L2 H1 H2 */
1280 /* compare high */
1281 op1 = op;
1282 /* when values are equal, we need to compare low words. since
1283 the jump is inverted, we invert the test too. */
1284 if (op1 == TOK_LT)
1285 op1 = TOK_LE;
1286 else if (op1 == TOK_GT)
1287 op1 = TOK_GE;
1288 else if (op1 == TOK_ULT)
1289 op1 = TOK_ULE;
1290 else if (op1 == TOK_UGT)
1291 op1 = TOK_UGE;
1292 a = 0;
1293 b = 0;
1294 gen_op(op1);
1295 if (op1 != TOK_NE) {
1296 a = gtst(1, 0);
1298 if (op != TOK_EQ) {
1299 /* generate non equal test */
1300 /* XXX: NOT PORTABLE yet */
1301 if (a == 0) {
1302 b = gtst(0, 0);
1303 } else {
1304 #if defined(TCC_TARGET_I386)
1305 b = psym(0x850f, 0);
1306 #elif defined(TCC_TARGET_ARM)
1307 b = ind;
1308 o(0x1A000000 | encbranch(ind, 0, 1));
1309 #elif defined(TCC_TARGET_C67)
1310 tcc_error("not implemented");
1311 #else
1312 #error not supported
1313 #endif
1316 /* compare low. Always unsigned */
1317 op1 = op;
1318 if (op1 == TOK_LT)
1319 op1 = TOK_ULT;
1320 else if (op1 == TOK_LE)
1321 op1 = TOK_ULE;
1322 else if (op1 == TOK_GT)
1323 op1 = TOK_UGT;
1324 else if (op1 == TOK_GE)
1325 op1 = TOK_UGE;
1326 gen_op(op1);
1327 a = gtst(1, a);
1328 gsym(b);
1329 vseti(VT_JMPI, a);
1330 break;
1333 #endif
1335 /* handle integer constant optimizations and various machine
1336 independent opt */
1337 static void gen_opic(int op)
1339 int c1, c2, t1, t2, n;
1340 SValue *v1, *v2;
1341 long long l1, l2;
1342 typedef unsigned long long U;
1344 v1 = vtop - 1;
1345 v2 = vtop;
1346 t1 = v1->type.t & VT_BTYPE;
1347 t2 = v2->type.t & VT_BTYPE;
1349 if (t1 == VT_LLONG)
1350 l1 = v1->c.ll;
1351 else if (v1->type.t & VT_UNSIGNED)
1352 l1 = v1->c.ui;
1353 else
1354 l1 = v1->c.i;
1356 if (t2 == VT_LLONG)
1357 l2 = v2->c.ll;
1358 else if (v2->type.t & VT_UNSIGNED)
1359 l2 = v2->c.ui;
1360 else
1361 l2 = v2->c.i;
1363 /* currently, we cannot do computations with forward symbols */
1364 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1365 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1366 if (c1 && c2) {
1367 switch(op) {
1368 case '+': l1 += l2; break;
1369 case '-': l1 -= l2; break;
1370 case '&': l1 &= l2; break;
1371 case '^': l1 ^= l2; break;
1372 case '|': l1 |= l2; break;
1373 case '*': l1 *= l2; break;
1375 case TOK_PDIV:
1376 case '/':
1377 case '%':
1378 case TOK_UDIV:
1379 case TOK_UMOD:
1380 /* if division by zero, generate explicit division */
1381 if (l2 == 0) {
1382 if (const_wanted)
1383 tcc_error("division by zero in constant");
1384 goto general_case;
1386 switch(op) {
1387 default: l1 /= l2; break;
1388 case '%': l1 %= l2; break;
1389 case TOK_UDIV: l1 = (U)l1 / l2; break;
1390 case TOK_UMOD: l1 = (U)l1 % l2; break;
1392 break;
1393 case TOK_SHL: l1 <<= l2; break;
1394 case TOK_SHR: l1 = (U)l1 >> l2; break;
1395 case TOK_SAR: l1 >>= l2; break;
1396 /* tests */
1397 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1398 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1399 case TOK_EQ: l1 = l1 == l2; break;
1400 case TOK_NE: l1 = l1 != l2; break;
1401 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1402 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1403 case TOK_LT: l1 = l1 < l2; break;
1404 case TOK_GE: l1 = l1 >= l2; break;
1405 case TOK_LE: l1 = l1 <= l2; break;
1406 case TOK_GT: l1 = l1 > l2; break;
1407 /* logical */
1408 case TOK_LAND: l1 = l1 && l2; break;
1409 case TOK_LOR: l1 = l1 || l2; break;
1410 default:
1411 goto general_case;
1413 v1->c.ll = l1;
1414 vtop--;
1415 } else {
1416 /* if commutative ops, put c2 as constant */
1417 if (c1 && (op == '+' || op == '&' || op == '^' ||
1418 op == '|' || op == '*')) {
1419 vswap();
1420 c2 = c1; //c = c1, c1 = c2, c2 = c;
1421 l2 = l1; //l = l1, l1 = l2, l2 = l;
1423 /* Filter out NOP operations like x*1, x-0, x&-1... */
1424 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1425 op == TOK_PDIV) &&
1426 l2 == 1) ||
1427 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1428 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1429 l2 == 0) ||
1430 (op == '&' &&
1431 l2 == -1))) {
1432 /* nothing to do */
1433 vtop--;
1434 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1435 /* try to use shifts instead of muls or divs */
1436 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1437 n = -1;
1438 while (l2) {
1439 l2 >>= 1;
1440 n++;
1442 vtop->c.ll = n;
1443 if (op == '*')
1444 op = TOK_SHL;
1445 else if (op == TOK_PDIV)
1446 op = TOK_SAR;
1447 else
1448 op = TOK_SHR;
1450 goto general_case;
1451 } else if (c2 && (op == '+' || op == '-') &&
1452 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1453 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1454 /* symbol + constant case */
1455 if (op == '-')
1456 l2 = -l2;
1457 vtop--;
1458 vtop->c.ll += l2;
1459 } else {
1460 general_case:
1461 if (!nocode_wanted) {
1462 /* call low level op generator */
1463 if (t1 == VT_LLONG || t2 == VT_LLONG)
1464 gen_opl(op);
1465 else
1466 gen_opi(op);
1467 } else {
1468 vtop--;
1474 /* generate a floating point operation with constant propagation */
1475 static void gen_opif(int op)
1477 int c1, c2;
1478 SValue *v1, *v2;
1479 long double f1, f2;
1481 v1 = vtop - 1;
1482 v2 = vtop;
1483 /* currently, we cannot do computations with forward symbols */
1484 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1485 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1486 if (c1 && c2) {
1487 if (v1->type.t == VT_FLOAT) {
1488 f1 = v1->c.f;
1489 f2 = v2->c.f;
1490 } else if (v1->type.t == VT_DOUBLE) {
1491 f1 = v1->c.d;
1492 f2 = v2->c.d;
1493 } else {
1494 f1 = v1->c.ld;
1495 f2 = v2->c.ld;
1498 /* NOTE: we only do constant propagation if finite number (not
1499 NaN or infinity) (ANSI spec) */
1500 if (!ieee_finite(f1) || !ieee_finite(f2))
1501 goto general_case;
1503 switch(op) {
1504 case '+': f1 += f2; break;
1505 case '-': f1 -= f2; break;
1506 case '*': f1 *= f2; break;
1507 case '/':
1508 if (f2 == 0.0) {
1509 if (const_wanted)
1510 tcc_error("division by zero in constant");
1511 goto general_case;
1513 f1 /= f2;
1514 break;
1515 /* XXX: also handles tests ? */
1516 default:
1517 goto general_case;
1519 /* XXX: overflow test ? */
1520 if (v1->type.t == VT_FLOAT) {
1521 v1->c.f = f1;
1522 } else if (v1->type.t == VT_DOUBLE) {
1523 v1->c.d = f1;
1524 } else {
1525 v1->c.ld = f1;
1527 vtop--;
1528 } else {
1529 general_case:
1530 if (!nocode_wanted) {
1531 gen_opf(op);
1532 } else {
1533 vtop--;
1538 static int pointed_size(CType *type)
1540 int align;
1541 return type_size(pointed_type(type), &align);
1544 static void vla_runtime_pointed_size(CType *type)
1546 int align;
1547 vla_runtime_type_size(pointed_type(type), &align);
1550 static inline int is_null_pointer(SValue *p)
1552 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1553 return 0;
1554 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1555 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1556 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0);
1559 static inline int is_integer_btype(int bt)
1561 return (bt == VT_BYTE || bt == VT_SHORT ||
1562 bt == VT_INT || bt == VT_LLONG);
1565 /* check types for comparison or substraction of pointers */
1566 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1568 CType *type1, *type2, tmp_type1, tmp_type2;
1569 int bt1, bt2;
1571 /* null pointers are accepted for all comparisons as gcc */
1572 if (is_null_pointer(p1) || is_null_pointer(p2))
1573 return;
1574 type1 = &p1->type;
1575 type2 = &p2->type;
1576 bt1 = type1->t & VT_BTYPE;
1577 bt2 = type2->t & VT_BTYPE;
1578 /* accept comparison between pointer and integer with a warning */
1579 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1580 if (op != TOK_LOR && op != TOK_LAND )
1581 tcc_warning("comparison between pointer and integer");
1582 return;
1585 /* both must be pointers or implicit function pointers */
1586 if (bt1 == VT_PTR) {
1587 type1 = pointed_type(type1);
1588 } else if (bt1 != VT_FUNC)
1589 goto invalid_operands;
1591 if (bt2 == VT_PTR) {
1592 type2 = pointed_type(type2);
1593 } else if (bt2 != VT_FUNC) {
1594 invalid_operands:
1595 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1597 if ((type1->t & VT_BTYPE) == VT_VOID ||
1598 (type2->t & VT_BTYPE) == VT_VOID)
1599 return;
1600 tmp_type1 = *type1;
1601 tmp_type2 = *type2;
1602 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1603 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1604 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1605 /* gcc-like error if '-' is used */
1606 if (op == '-')
1607 goto invalid_operands;
1608 else
1609 tcc_warning("comparison of distinct pointer types lacks a cast");
1613 /* generic gen_op: handles types problems */
1614 ST_FUNC void gen_op(int op)
1616 int u, t1, t2, bt1, bt2, t;
1617 CType type1;
1619 t1 = vtop[-1].type.t;
1620 t2 = vtop[0].type.t;
1621 bt1 = t1 & VT_BTYPE;
1622 bt2 = t2 & VT_BTYPE;
1624 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1625 /* at least one operand is a pointer */
1626 /* relationnal op: must be both pointers */
1627 if (op >= TOK_ULT && op <= TOK_LOR) {
1628 check_comparison_pointer_types(vtop - 1, vtop, op);
1629 /* pointers are handled are unsigned */
1630 #ifdef TCC_TARGET_X86_64
1631 t = VT_LLONG | VT_UNSIGNED;
1632 #else
1633 t = VT_INT | VT_UNSIGNED;
1634 #endif
1635 goto std_op;
1637 /* if both pointers, then it must be the '-' op */
1638 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1639 if (op != '-')
1640 tcc_error("cannot use pointers here");
1641 check_comparison_pointer_types(vtop - 1, vtop, op);
1642 /* XXX: check that types are compatible */
1643 if (vtop[-1].type.t & VT_VLA) {
1644 vla_runtime_pointed_size(&vtop[-1].type);
1645 } else {
1646 vpushi(pointed_size(&vtop[-1].type));
1648 vrott(3);
1649 gen_opic(op);
1650 /* set to integer type */
1651 #ifdef TCC_TARGET_X86_64
1652 vtop->type.t = VT_LLONG;
1653 #else
1654 vtop->type.t = VT_INT;
1655 #endif
1656 vswap();
1657 gen_op(TOK_PDIV);
1658 } else {
1659 /* exactly one pointer : must be '+' or '-'. */
1660 if (op != '-' && op != '+')
1661 tcc_error("cannot use pointers here");
1662 /* Put pointer as first operand */
1663 if (bt2 == VT_PTR) {
1664 vswap();
1665 swap(&t1, &t2);
1667 type1 = vtop[-1].type;
1668 type1.t &= ~VT_ARRAY;
1669 if (vtop[-1].type.t & VT_VLA)
1670 vla_runtime_pointed_size(&vtop[-1].type);
1671 else {
1672 u = pointed_size(&vtop[-1].type);
1673 if (u < 0)
1674 tcc_error("unknown array element size");
1675 #ifdef TCC_TARGET_X86_64
1676 vpushll(u);
1677 #else
1678 /* XXX: cast to int ? (long long case) */
1679 vpushi(u);
1680 #endif
1682 gen_op('*');
1683 #ifdef CONFIG_TCC_BCHECK
1684 /* if evaluating constant expression, no code should be
1685 generated, so no bound check */
1686 if (tcc_state->do_bounds_check && !const_wanted) {
1687 /* if bounded pointers, we generate a special code to
1688 test bounds */
1689 if (op == '-') {
1690 vpushi(0);
1691 vswap();
1692 gen_op('-');
1694 gen_bounded_ptr_add();
1695 } else
1696 #endif
1698 gen_opic(op);
1700 /* put again type if gen_opic() swaped operands */
1701 vtop->type = type1;
1703 } else if (is_float(bt1) || is_float(bt2)) {
1704 /* compute bigger type and do implicit casts */
1705 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1706 t = VT_LDOUBLE;
1707 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1708 t = VT_DOUBLE;
1709 } else {
1710 t = VT_FLOAT;
1712 /* floats can only be used for a few operations */
1713 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1714 (op < TOK_ULT || op > TOK_GT))
1715 tcc_error("invalid operands for binary operation");
1716 goto std_op;
1717 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1718 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1719 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1720 t |= VT_UNSIGNED;
1721 goto std_op;
1722 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1723 /* cast to biggest op */
1724 t = VT_LLONG;
1725 /* convert to unsigned if it does not fit in a long long */
1726 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1727 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1728 t |= VT_UNSIGNED;
1729 goto std_op;
1730 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1731 tcc_error("comparison of struct");
1732 } else {
1733 /* integer operations */
1734 t = VT_INT;
1735 /* convert to unsigned if it does not fit in an integer */
1736 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1737 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1738 t |= VT_UNSIGNED;
1739 std_op:
1740 /* XXX: currently, some unsigned operations are explicit, so
1741 we modify them here */
1742 if (t & VT_UNSIGNED) {
1743 if (op == TOK_SAR)
1744 op = TOK_SHR;
1745 else if (op == '/')
1746 op = TOK_UDIV;
1747 else if (op == '%')
1748 op = TOK_UMOD;
1749 else if (op == TOK_LT)
1750 op = TOK_ULT;
1751 else if (op == TOK_GT)
1752 op = TOK_UGT;
1753 else if (op == TOK_LE)
1754 op = TOK_ULE;
1755 else if (op == TOK_GE)
1756 op = TOK_UGE;
1758 vswap();
1759 type1.t = t;
1760 gen_cast(&type1);
1761 vswap();
1762 /* special case for shifts and long long: we keep the shift as
1763 an integer */
1764 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1765 type1.t = VT_INT;
1766 gen_cast(&type1);
1767 if (is_float(t))
1768 gen_opif(op);
1769 else
1770 gen_opic(op);
1771 if (op >= TOK_ULT && op <= TOK_GT) {
1772 /* relationnal op: the result is an int */
1773 vtop->type.t = VT_INT;
1774 } else {
1775 vtop->type.t = t;
1780 #ifndef TCC_TARGET_ARM
1781 /* generic itof for unsigned long long case */
1782 static void gen_cvt_itof1(int t)
1784 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1785 (VT_LLONG | VT_UNSIGNED)) {
1787 if (t == VT_FLOAT)
1788 vpush_global_sym(&func_old_type, TOK___floatundisf);
1789 #if LDOUBLE_SIZE != 8
1790 else if (t == VT_LDOUBLE)
1791 vpush_global_sym(&func_old_type, TOK___floatundixf);
1792 #endif
1793 else
1794 vpush_global_sym(&func_old_type, TOK___floatundidf);
1795 vrott(2);
1796 gfunc_call(1);
1797 vpushi(0);
1798 vtop->r = reg_fret(t);
1799 } else {
1800 gen_cvt_itof(t);
1803 #endif
1805 /* generic ftoi for unsigned long long case */
1806 static void gen_cvt_ftoi1(int t)
1808 int st;
1810 if (t == (VT_LLONG | VT_UNSIGNED)) {
1811 /* not handled natively */
1812 st = vtop->type.t & VT_BTYPE;
1813 if (st == VT_FLOAT)
1814 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1815 #if LDOUBLE_SIZE != 8
1816 else if (st == VT_LDOUBLE)
1817 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1818 #endif
1819 else
1820 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1821 vrott(2);
1822 gfunc_call(1);
1823 vpushi(0);
1824 vtop->r = REG_IRET;
1825 vtop->r2 = REG_LRET;
1826 } else {
1827 gen_cvt_ftoi(t);
1831 /* force char or short cast */
1832 static void force_charshort_cast(int t)
1834 int bits, dbt;
1835 dbt = t & VT_BTYPE;
1836 /* XXX: add optimization if lvalue : just change type and offset */
1837 if (dbt == VT_BYTE)
1838 bits = 8;
1839 else
1840 bits = 16;
1841 if (t & VT_UNSIGNED) {
1842 vpushi((1 << bits) - 1);
1843 gen_op('&');
1844 } else {
1845 bits = 32 - bits;
1846 vpushi(bits);
1847 gen_op(TOK_SHL);
1848 /* result must be signed or the SAR is converted to an SHL
1849 This was not the case when "t" was a signed short
1850 and the last value on the stack was an unsigned int */
1851 vtop->type.t &= ~VT_UNSIGNED;
1852 vpushi(bits);
1853 gen_op(TOK_SAR);
1857 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1858 static void gen_cast(CType *type)
1860 int sbt, dbt, sf, df, c, p;
1862 /* special delayed cast for char/short */
1863 /* XXX: in some cases (multiple cascaded casts), it may still
1864 be incorrect */
1865 if (vtop->r & VT_MUSTCAST) {
1866 vtop->r &= ~VT_MUSTCAST;
1867 force_charshort_cast(vtop->type.t);
1870 /* bitfields first get cast to ints */
1871 if (vtop->type.t & VT_BITFIELD) {
1872 gv(RC_INT);
1875 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1876 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1878 if (sbt != dbt) {
1879 sf = is_float(sbt);
1880 df = is_float(dbt);
1881 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1882 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1883 if (c) {
1884 /* constant case: we can do it now */
1885 /* XXX: in ISOC, cannot do it if error in convert */
1886 if (sbt == VT_FLOAT)
1887 vtop->c.ld = vtop->c.f;
1888 else if (sbt == VT_DOUBLE)
1889 vtop->c.ld = vtop->c.d;
1891 if (df) {
1892 if ((sbt & VT_BTYPE) == VT_LLONG) {
1893 if (sbt & VT_UNSIGNED)
1894 vtop->c.ld = vtop->c.ull;
1895 else
1896 vtop->c.ld = vtop->c.ll;
1897 } else if(!sf) {
1898 if (sbt & VT_UNSIGNED)
1899 vtop->c.ld = vtop->c.ui;
1900 else
1901 vtop->c.ld = vtop->c.i;
1904 if (dbt == VT_FLOAT)
1905 vtop->c.f = (float)vtop->c.ld;
1906 else if (dbt == VT_DOUBLE)
1907 vtop->c.d = (double)vtop->c.ld;
1908 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1909 vtop->c.ull = (unsigned long long)vtop->c.ld;
1910 } else if (sf && dbt == VT_BOOL) {
1911 vtop->c.i = (vtop->c.ld != 0);
1912 } else {
1913 if(sf)
1914 vtop->c.ll = (long long)vtop->c.ld;
1915 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1916 vtop->c.ll = vtop->c.ull;
1917 else if (sbt & VT_UNSIGNED)
1918 vtop->c.ll = vtop->c.ui;
1919 #ifdef TCC_TARGET_X86_64
1920 else if (sbt == VT_PTR)
1922 #endif
1923 else if (sbt != VT_LLONG)
1924 vtop->c.ll = vtop->c.i;
1926 if (dbt == (VT_LLONG|VT_UNSIGNED))
1927 vtop->c.ull = vtop->c.ll;
1928 else if (dbt == VT_BOOL)
1929 vtop->c.i = (vtop->c.ll != 0);
1930 else if (dbt != VT_LLONG) {
1931 int s = 0;
1932 if ((dbt & VT_BTYPE) == VT_BYTE)
1933 s = 24;
1934 else if ((dbt & VT_BTYPE) == VT_SHORT)
1935 s = 16;
1937 if(dbt & VT_UNSIGNED)
1938 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1939 else
1940 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1943 } else if (p && dbt == VT_BOOL) {
1944 vtop->r = VT_CONST;
1945 vtop->c.i = 1;
1946 } else if (!nocode_wanted) {
1947 /* non constant case: generate code */
1948 if (sf && df) {
1949 /* convert from fp to fp */
1950 gen_cvt_ftof(dbt);
1951 } else if (df) {
1952 /* convert int to fp */
1953 gen_cvt_itof1(dbt);
1954 } else if (sf) {
1955 /* convert fp to int */
1956 if (dbt == VT_BOOL) {
1957 vpushi(0);
1958 gen_op(TOK_NE);
1959 } else {
1960 /* we handle char/short/etc... with generic code */
1961 if (dbt != (VT_INT | VT_UNSIGNED) &&
1962 dbt != (VT_LLONG | VT_UNSIGNED) &&
1963 dbt != VT_LLONG)
1964 dbt = VT_INT;
1965 gen_cvt_ftoi1(dbt);
1966 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1967 /* additional cast for char/short... */
1968 vtop->type.t = dbt;
1969 gen_cast(type);
1972 #ifndef TCC_TARGET_X86_64
1973 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1974 if ((sbt & VT_BTYPE) != VT_LLONG) {
1975 /* scalar to long long */
1976 /* machine independent conversion */
1977 gv(RC_INT);
1978 /* generate high word */
1979 if (sbt == (VT_INT | VT_UNSIGNED)) {
1980 vpushi(0);
1981 gv(RC_INT);
1982 } else {
1983 if (sbt == VT_PTR) {
1984 /* cast from pointer to int before we apply
1985 shift operation, which pointers don't support*/
1986 gen_cast(&int_type);
1988 gv_dup();
1989 vpushi(31);
1990 gen_op(TOK_SAR);
1992 /* patch second register */
1993 vtop[-1].r2 = vtop->r;
1994 vpop();
1996 #else
1997 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1998 (dbt & VT_BTYPE) == VT_PTR ||
1999 (dbt & VT_BTYPE) == VT_FUNC) {
2000 if ((sbt & VT_BTYPE) != VT_LLONG &&
2001 (sbt & VT_BTYPE) != VT_PTR &&
2002 (sbt & VT_BTYPE) != VT_FUNC) {
2003 /* need to convert from 32bit to 64bit */
2004 int r = gv(RC_INT);
2005 if (sbt != (VT_INT | VT_UNSIGNED)) {
2006 /* x86_64 specific: movslq */
2007 o(0x6348);
2008 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2011 #endif
2012 } else if (dbt == VT_BOOL) {
2013 /* scalar to bool */
2014 vpushi(0);
2015 gen_op(TOK_NE);
2016 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2017 (dbt & VT_BTYPE) == VT_SHORT) {
2018 if (sbt == VT_PTR) {
2019 vtop->type.t = VT_INT;
2020 tcc_warning("nonportable conversion from pointer to char/short");
2022 force_charshort_cast(dbt);
2023 } else if ((dbt & VT_BTYPE) == VT_INT) {
2024 /* scalar to int */
2025 if (sbt == VT_LLONG) {
2026 /* from long long: just take low order word */
2027 lexpand();
2028 vpop();
2030 /* if lvalue and single word type, nothing to do because
2031 the lvalue already contains the real type size (see
2032 VT_LVAL_xxx constants) */
2035 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2036 /* if we are casting between pointer types,
2037 we must update the VT_LVAL_xxx size */
2038 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2039 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2041 vtop->type = *type;
2044 /* return type size as known at compile time. Put alignment at 'a' */
2045 ST_FUNC int type_size(CType *type, int *a)
2047 Sym *s;
2048 int bt;
2050 bt = type->t & VT_BTYPE;
2051 if (bt == VT_STRUCT) {
2052 /* struct/union */
2053 s = type->ref;
2054 *a = s->r;
2055 return s->c;
2056 } else if (bt == VT_PTR) {
2057 if (type->t & VT_ARRAY) {
2058 int ts;
2060 s = type->ref;
2061 ts = type_size(&s->type, a);
2063 if (ts < 0 && s->c < 0)
2064 ts = -ts;
2066 return ts * s->c;
2067 } else {
2068 *a = PTR_SIZE;
2069 return PTR_SIZE;
2071 } else if (bt == VT_LDOUBLE) {
2072 *a = LDOUBLE_ALIGN;
2073 return LDOUBLE_SIZE;
2074 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2075 #ifdef TCC_TARGET_I386
2076 #ifdef TCC_TARGET_PE
2077 *a = 8;
2078 #else
2079 *a = 4;
2080 #endif
2081 #elif defined(TCC_TARGET_ARM)
2082 #ifdef TCC_ARM_EABI
2083 *a = 8;
2084 #else
2085 *a = 4;
2086 #endif
2087 #else
2088 *a = 8;
2089 #endif
2090 return 8;
2091 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2092 *a = 4;
2093 return 4;
2094 } else if (bt == VT_SHORT) {
2095 *a = 2;
2096 return 2;
2097 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2098 *a = 8;
2099 return 16;
2100 } else {
2101 /* char, void, function, _Bool */
2102 *a = 1;
2103 return 1;
2107 /* push type size as known at runtime time on top of value stack. Put
2108 alignment at 'a' */
2109 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2111 if (type->t & VT_VLA) {
2112 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2113 } else {
2114 vpushi(type_size(type, a));
2118 static void vla_sp_save(void) {
2119 if (!(vla_flags & VLA_SP_LOC_SET)) {
2120 *vla_sp_loc = (loc -= PTR_SIZE);
2121 vla_flags |= VLA_SP_LOC_SET;
2123 if (!(vla_flags & VLA_SP_SAVED)) {
2124 gen_vla_sp_save(*vla_sp_loc);
2125 vla_flags |= VLA_SP_SAVED;
2129 /* return the pointed type of t */
2130 static inline CType *pointed_type(CType *type)
2132 return &type->ref->type;
2135 /* modify type so that its it is a pointer to type. */
2136 ST_FUNC void mk_pointer(CType *type)
2138 Sym *s;
2139 s = sym_push(SYM_FIELD, type, 0, -1);
2140 type->t = VT_PTR | (type->t & ~VT_TYPE);
2141 type->ref = s;
2144 /* compare function types. OLD functions match any new functions */
2145 static int is_compatible_func(CType *type1, CType *type2)
2147 Sym *s1, *s2;
2149 s1 = type1->ref;
2150 s2 = type2->ref;
2151 if (!is_compatible_types(&s1->type, &s2->type))
2152 return 0;
2153 /* check func_call */
2154 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2155 return 0;
2156 /* XXX: not complete */
2157 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2158 return 1;
2159 if (s1->c != s2->c)
2160 return 0;
2161 while (s1 != NULL) {
2162 if (s2 == NULL)
2163 return 0;
2164 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2165 return 0;
2166 s1 = s1->next;
2167 s2 = s2->next;
2169 if (s2)
2170 return 0;
2171 return 1;
2174 /* return true if type1 and type2 are the same. If unqualified is
2175 true, qualifiers on the types are ignored.
2177 - enums are not checked as gcc __builtin_types_compatible_p ()
2179 static int compare_types(CType *type1, CType *type2, int unqualified)
2181 int bt1, t1, t2;
2183 t1 = type1->t & VT_TYPE;
2184 t2 = type2->t & VT_TYPE;
2185 if (unqualified) {
2186 /* strip qualifiers before comparing */
2187 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2188 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2190 /* XXX: bitfields ? */
2191 if (t1 != t2)
2192 return 0;
2193 /* test more complicated cases */
2194 bt1 = t1 & VT_BTYPE;
2195 if (bt1 == VT_PTR) {
2196 type1 = pointed_type(type1);
2197 type2 = pointed_type(type2);
2198 return is_compatible_types(type1, type2);
2199 } else if (bt1 == VT_STRUCT) {
2200 return (type1->ref == type2->ref);
2201 } else if (bt1 == VT_FUNC) {
2202 return is_compatible_func(type1, type2);
2203 } else {
2204 return 1;
2208 /* return true if type1 and type2 are exactly the same (including
2209 qualifiers).
2211 static int is_compatible_types(CType *type1, CType *type2)
2213 return compare_types(type1,type2,0);
2216 /* return true if type1 and type2 are the same (ignoring qualifiers).
2218 static int is_compatible_parameter_types(CType *type1, CType *type2)
2220 return compare_types(type1,type2,1);
2223 /* print a type. If 'varstr' is not NULL, then the variable is also
2224 printed in the type */
2225 /* XXX: union */
2226 /* XXX: add array and function pointers */
2227 static void type_to_str(char *buf, int buf_size,
2228 CType *type, const char *varstr)
2230 int bt, v, t;
2231 Sym *s, *sa;
2232 char buf1[256];
2233 const char *tstr;
2235 t = type->t & VT_TYPE;
2236 bt = t & VT_BTYPE;
2237 buf[0] = '\0';
2238 if (t & VT_CONSTANT)
2239 pstrcat(buf, buf_size, "const ");
2240 if (t & VT_VOLATILE)
2241 pstrcat(buf, buf_size, "volatile ");
2242 if (t & VT_UNSIGNED)
2243 pstrcat(buf, buf_size, "unsigned ");
2244 switch(bt) {
2245 case VT_VOID:
2246 tstr = "void";
2247 goto add_tstr;
2248 case VT_BOOL:
2249 tstr = "_Bool";
2250 goto add_tstr;
2251 case VT_BYTE:
2252 tstr = "char";
2253 goto add_tstr;
2254 case VT_SHORT:
2255 tstr = "short";
2256 goto add_tstr;
2257 case VT_INT:
2258 tstr = "int";
2259 goto add_tstr;
2260 case VT_LONG:
2261 tstr = "long";
2262 goto add_tstr;
2263 case VT_LLONG:
2264 tstr = "long long";
2265 goto add_tstr;
2266 case VT_FLOAT:
2267 tstr = "float";
2268 goto add_tstr;
2269 case VT_DOUBLE:
2270 tstr = "double";
2271 goto add_tstr;
2272 case VT_LDOUBLE:
2273 tstr = "long double";
2274 add_tstr:
2275 pstrcat(buf, buf_size, tstr);
2276 break;
2277 case VT_ENUM:
2278 case VT_STRUCT:
2279 if (bt == VT_STRUCT)
2280 tstr = "struct ";
2281 else
2282 tstr = "enum ";
2283 pstrcat(buf, buf_size, tstr);
2284 v = type->ref->v & ~SYM_STRUCT;
2285 if (v >= SYM_FIRST_ANOM)
2286 pstrcat(buf, buf_size, "<anonymous>");
2287 else
2288 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2289 break;
2290 case VT_FUNC:
2291 s = type->ref;
2292 type_to_str(buf, buf_size, &s->type, varstr);
2293 pstrcat(buf, buf_size, "(");
2294 sa = s->next;
2295 while (sa != NULL) {
2296 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2297 pstrcat(buf, buf_size, buf1);
2298 sa = sa->next;
2299 if (sa)
2300 pstrcat(buf, buf_size, ", ");
2302 pstrcat(buf, buf_size, ")");
2303 goto no_var;
2304 case VT_PTR:
2305 s = type->ref;
2306 pstrcpy(buf1, sizeof(buf1), "*");
2307 if (varstr)
2308 pstrcat(buf1, sizeof(buf1), varstr);
2309 type_to_str(buf, buf_size, &s->type, buf1);
2310 goto no_var;
2312 if (varstr) {
2313 pstrcat(buf, buf_size, " ");
2314 pstrcat(buf, buf_size, varstr);
2316 no_var: ;
2319 /* verify type compatibility to store vtop in 'dt' type, and generate
2320 casts if needed. */
2321 static void gen_assign_cast(CType *dt)
2323 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2324 char buf1[256], buf2[256];
2325 int dbt, sbt;
2327 st = &vtop->type; /* source type */
2328 dbt = dt->t & VT_BTYPE;
2329 sbt = st->t & VT_BTYPE;
2330 if (sbt == VT_VOID)
2331 tcc_error("Cannot assign void value");
2332 if (dt->t & VT_CONSTANT)
2333 tcc_warning("assignment of read-only location");
2334 switch(dbt) {
2335 case VT_PTR:
2336 /* special cases for pointers */
2337 /* '0' can also be a pointer */
2338 if (is_null_pointer(vtop))
2339 goto type_ok;
2340 /* accept implicit pointer to integer cast with warning */
2341 if (is_integer_btype(sbt)) {
2342 tcc_warning("assignment makes pointer from integer without a cast");
2343 goto type_ok;
2345 type1 = pointed_type(dt);
2346 /* a function is implicitely a function pointer */
2347 if (sbt == VT_FUNC) {
2348 if ((type1->t & VT_BTYPE) != VT_VOID &&
2349 !is_compatible_types(pointed_type(dt), st))
2350 tcc_warning("assignment from incompatible pointer type");
2351 goto type_ok;
2353 if (sbt != VT_PTR)
2354 goto error;
2355 type2 = pointed_type(st);
2356 if ((type1->t & VT_BTYPE) == VT_VOID ||
2357 (type2->t & VT_BTYPE) == VT_VOID) {
2358 /* void * can match anything */
2359 } else {
2360 /* exact type match, except for unsigned */
2361 tmp_type1 = *type1;
2362 tmp_type2 = *type2;
2363 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2364 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2365 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2366 tcc_warning("assignment from incompatible pointer type");
2368 /* check const and volatile */
2369 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2370 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2371 tcc_warning("assignment discards qualifiers from pointer target type");
2372 break;
2373 case VT_BYTE:
2374 case VT_SHORT:
2375 case VT_INT:
2376 case VT_LLONG:
2377 if (sbt == VT_PTR || sbt == VT_FUNC) {
2378 tcc_warning("assignment makes integer from pointer without a cast");
2380 /* XXX: more tests */
2381 break;
2382 case VT_STRUCT:
2383 tmp_type1 = *dt;
2384 tmp_type2 = *st;
2385 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2386 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2387 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2388 error:
2389 type_to_str(buf1, sizeof(buf1), st, NULL);
2390 type_to_str(buf2, sizeof(buf2), dt, NULL);
2391 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2393 break;
2395 type_ok:
2396 gen_cast(dt);
2399 /* store vtop in lvalue pushed on stack */
2400 ST_FUNC void vstore(void)
2402 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2404 ft = vtop[-1].type.t;
2405 sbt = vtop->type.t & VT_BTYPE;
2406 dbt = ft & VT_BTYPE;
2407 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2408 (sbt == VT_INT && dbt == VT_SHORT))
2409 && !(vtop->type.t & VT_BITFIELD)) {
2410 /* optimize char/short casts */
2411 delayed_cast = VT_MUSTCAST;
2412 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2413 /* XXX: factorize */
2414 if (ft & VT_CONSTANT)
2415 tcc_warning("assignment of read-only location");
2416 } else {
2417 delayed_cast = 0;
2418 if (!(ft & VT_BITFIELD))
2419 gen_assign_cast(&vtop[-1].type);
2422 if (sbt == VT_STRUCT) {
2423 /* if structure, only generate pointer */
2424 /* structure assignment : generate memcpy */
2425 /* XXX: optimize if small size */
2426 if (!nocode_wanted) {
2427 size = type_size(&vtop->type, &align);
2429 /* destination */
2430 vswap();
2431 vtop->type.t = VT_PTR;
2432 gaddrof();
2434 /* address of memcpy() */
2435 #ifdef TCC_ARM_EABI
2436 if(!(align & 7))
2437 vpush_global_sym(&func_old_type, TOK_memcpy8);
2438 else if(!(align & 3))
2439 vpush_global_sym(&func_old_type, TOK_memcpy4);
2440 else
2441 #endif
2442 vpush_global_sym(&func_old_type, TOK_memcpy);
2444 vswap();
2445 /* source */
2446 vpushv(vtop - 2);
2447 vtop->type.t = VT_PTR;
2448 gaddrof();
2449 /* type size */
2450 vpushi(size);
2451 gfunc_call(3);
2452 } else {
2453 vswap();
2454 vpop();
2456 /* leave source on stack */
2457 } else if (ft & VT_BITFIELD) {
2458 /* bitfield store handling */
2459 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2460 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2461 /* remove bit field info to avoid loops */
2462 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2464 /* duplicate source into other register */
2465 gv_dup();
2466 vswap();
2467 vrott(3);
2469 if((ft & VT_BTYPE) == VT_BOOL) {
2470 gen_cast(&vtop[-1].type);
2471 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2474 /* duplicate destination */
2475 vdup();
2476 vtop[-1] = vtop[-2];
2478 /* mask and shift source */
2479 if((ft & VT_BTYPE) != VT_BOOL) {
2480 if((ft & VT_BTYPE) == VT_LLONG) {
2481 vpushll((1ULL << bit_size) - 1ULL);
2482 } else {
2483 vpushi((1 << bit_size) - 1);
2485 gen_op('&');
2487 vpushi(bit_pos);
2488 gen_op(TOK_SHL);
2489 /* load destination, mask and or with source */
2490 vswap();
2491 if((ft & VT_BTYPE) == VT_LLONG) {
2492 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2493 } else {
2494 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2496 gen_op('&');
2497 gen_op('|');
2498 /* store result */
2499 vstore();
2501 /* pop off shifted source from "duplicate source..." above */
2502 vpop();
2504 } else {
2505 #ifdef CONFIG_TCC_BCHECK
2506 /* bound check case */
2507 if (vtop[-1].r & VT_MUSTBOUND) {
2508 vswap();
2509 gbound();
2510 vswap();
2512 #endif
2513 if (!nocode_wanted) {
2514 rc = RC_INT;
2515 if (is_float(ft)) {
2516 rc = RC_FLOAT;
2517 #ifdef TCC_TARGET_X86_64
2518 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2519 rc = RC_ST0;
2520 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2521 rc = RC_FRET;
2523 #endif
2525 r = gv(rc); /* generate value */
2526 /* if lvalue was saved on stack, must read it */
2527 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2528 SValue sv;
2529 t = get_reg(RC_INT);
2530 #ifdef TCC_TARGET_X86_64
2531 sv.type.t = VT_PTR;
2532 #else
2533 sv.type.t = VT_INT;
2534 #endif
2535 sv.r = VT_LOCAL | VT_LVAL;
2536 sv.c.ul = vtop[-1].c.ul;
2537 load(t, &sv);
2538 vtop[-1].r = t | VT_LVAL;
2540 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2541 #ifdef TCC_TARGET_X86_64
2542 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2543 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2544 #else
2545 if ((ft & VT_BTYPE) == VT_LLONG) {
2546 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2547 #endif
2548 vtop[-1].type.t = load_type;
2549 store(r, vtop - 1);
2550 vswap();
2551 /* convert to int to increment easily */
2552 vtop->type.t = addr_type;
2553 gaddrof();
2554 vpushi(load_size);
2555 gen_op('+');
2556 vtop->r |= VT_LVAL;
2557 vswap();
2558 vtop[-1].type.t = load_type;
2559 /* XXX: it works because r2 is spilled last ! */
2560 store(vtop->r2, vtop - 1);
2561 } else {
2562 store(r, vtop - 1);
2565 vswap();
2566 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2567 vtop->r |= delayed_cast;
2571 /* post defines POST/PRE add. c is the token ++ or -- */
2572 ST_FUNC void inc(int post, int c)
2574 test_lvalue();
2575 vdup(); /* save lvalue */
2576 if (post) {
2577 gv_dup(); /* duplicate value */
2578 vrotb(3);
2579 vrotb(3);
2581 /* add constant */
2582 vpushi(c - TOK_MID);
2583 gen_op('+');
2584 vstore(); /* store value */
2585 if (post)
2586 vpop(); /* if post op, return saved value */
2589 /* Parse GNUC __attribute__ extension. Currently, the following
2590 extensions are recognized:
2591 - aligned(n) : set data/function alignment.
2592 - packed : force data alignment to 1
2593 - section(x) : generate data/code in this section.
2594 - unused : currently ignored, but may be used someday.
2595 - regparm(n) : pass function parameters in registers (i386 only)
2597 static void parse_attribute(AttributeDef *ad)
2599 int t, n;
2601 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2602 next();
2603 skip('(');
2604 skip('(');
2605 while (tok != ')') {
2606 if (tok < TOK_IDENT)
2607 expect("attribute name");
2608 t = tok;
2609 next();
2610 switch(t) {
2611 case TOK_SECTION1:
2612 case TOK_SECTION2:
2613 skip('(');
2614 if (tok != TOK_STR)
2615 expect("section name");
2616 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2617 next();
2618 skip(')');
2619 break;
2620 case TOK_ALIAS1:
2621 case TOK_ALIAS2:
2622 skip('(');
2623 if (tok != TOK_STR)
2624 expect("alias(\"target\")");
2625 ad->alias_target = /* save string as token, for later */
2626 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2627 next();
2628 skip(')');
2629 break;
2630 case TOK_ALIGNED1:
2631 case TOK_ALIGNED2:
2632 if (tok == '(') {
2633 next();
2634 n = expr_const();
2635 if (n <= 0 || (n & (n - 1)) != 0)
2636 tcc_error("alignment must be a positive power of two");
2637 skip(')');
2638 } else {
2639 n = MAX_ALIGN;
2641 ad->aligned = n;
2642 break;
2643 case TOK_PACKED1:
2644 case TOK_PACKED2:
2645 ad->packed = 1;
2646 break;
2647 case TOK_WEAK1:
2648 case TOK_WEAK2:
2649 ad->weak = 1;
2650 break;
2651 case TOK_UNUSED1:
2652 case TOK_UNUSED2:
2653 /* currently, no need to handle it because tcc does not
2654 track unused objects */
2655 break;
2656 case TOK_NORETURN1:
2657 case TOK_NORETURN2:
2658 /* currently, no need to handle it because tcc does not
2659 track unused objects */
2660 break;
2661 case TOK_CDECL1:
2662 case TOK_CDECL2:
2663 case TOK_CDECL3:
2664 ad->func_call = FUNC_CDECL;
2665 break;
2666 case TOK_STDCALL1:
2667 case TOK_STDCALL2:
2668 case TOK_STDCALL3:
2669 ad->func_call = FUNC_STDCALL;
2670 break;
2671 #ifdef TCC_TARGET_I386
2672 case TOK_REGPARM1:
2673 case TOK_REGPARM2:
2674 skip('(');
2675 n = expr_const();
2676 if (n > 3)
2677 n = 3;
2678 else if (n < 0)
2679 n = 0;
2680 if (n > 0)
2681 ad->func_call = FUNC_FASTCALL1 + n - 1;
2682 skip(')');
2683 break;
2684 case TOK_FASTCALL1:
2685 case TOK_FASTCALL2:
2686 case TOK_FASTCALL3:
2687 ad->func_call = FUNC_FASTCALLW;
2688 break;
2689 #endif
2690 case TOK_MODE:
2691 skip('(');
2692 switch(tok) {
2693 case TOK_MODE_DI:
2694 ad->mode = VT_LLONG + 1;
2695 break;
2696 case TOK_MODE_HI:
2697 ad->mode = VT_SHORT + 1;
2698 break;
2699 case TOK_MODE_SI:
2700 ad->mode = VT_INT + 1;
2701 break;
2702 default:
2703 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2704 break;
2706 next();
2707 skip(')');
2708 break;
2709 case TOK_DLLEXPORT:
2710 ad->func_export = 1;
2711 break;
2712 case TOK_DLLIMPORT:
2713 ad->func_import = 1;
2714 break;
2715 default:
2716 if (tcc_state->warn_unsupported)
2717 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2718 /* skip parameters */
2719 if (tok == '(') {
2720 int parenthesis = 0;
2721 do {
2722 if (tok == '(')
2723 parenthesis++;
2724 else if (tok == ')')
2725 parenthesis--;
2726 next();
2727 } while (parenthesis && tok != -1);
2729 break;
2731 if (tok != ',')
2732 break;
2733 next();
2735 skip(')');
2736 skip(')');
2740 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2741 static void struct_decl(CType *type, int u, int tdef)
2743 int a, v, size, align, maxalign, c, offset, flexible;
2744 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2745 Sym *s, *ss, *ass, **ps;
2746 AttributeDef ad;
2747 CType type1, btype;
2749 a = tok; /* save decl type */
2750 next();
2751 if (tok != '{') {
2752 v = tok;
2753 next();
2754 /* struct already defined ? return it */
2755 if (v < TOK_IDENT)
2756 expect("struct/union/enum name");
2757 s = struct_find(v);
2758 if (s) {
2759 if (s->type.t != a)
2760 tcc_error("invalid type");
2761 goto do_decl;
2762 } else if (tok >= TOK_IDENT && !tdef)
2763 tcc_error("unknown struct/union/enum");
2764 } else {
2765 v = anon_sym++;
2767 type1.t = a;
2768 type1.ref = NULL;
2769 /* we put an undefined size for struct/union */
2770 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2771 s->r = 0; /* default alignment is zero as gcc */
2772 /* put struct/union/enum name in type */
2773 do_decl:
2774 type->t = u;
2775 type->ref = s;
2777 if (tok == '{') {
2778 next();
2779 if (s->c != -1)
2780 tcc_error("struct/union/enum already defined");
2781 /* cannot be empty */
2782 c = 0;
2783 /* non empty enums are not allowed */
2784 if (a == TOK_ENUM) {
2785 for(;;) {
2786 v = tok;
2787 if (v < TOK_UIDENT)
2788 expect("identifier");
2789 ss = sym_find(v);
2790 if (ss)
2791 tcc_error("redefinition of enumerator '%s'",
2792 get_tok_str(v, NULL));
2793 next();
2794 if (tok == '=') {
2795 next();
2796 c = expr_const();
2798 /* enum symbols have static storage */
2799 ss = sym_push(v, &int_type, VT_CONST, c);
2800 ss->type.t |= VT_STATIC;
2801 if (tok != ',')
2802 break;
2803 next();
2804 c++;
2805 /* NOTE: we accept a trailing comma */
2806 if (tok == '}')
2807 break;
2809 s->c = type_size(&int_type, &align);
2810 skip('}');
2811 } else {
2812 maxalign = 1;
2813 ps = &s->next;
2814 prevbt = VT_INT;
2815 bit_pos = 0;
2816 offset = 0;
2817 flexible = 0;
2818 while (tok != '}') {
2819 parse_btype(&btype, &ad);
2820 while (1) {
2821 if (flexible)
2822 tcc_error("flexible array member '%s' not at the end of struct",
2823 get_tok_str(v, NULL));
2824 bit_size = -1;
2825 v = 0;
2826 type1 = btype;
2827 if (tok != ':') {
2828 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2829 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2830 expect("identifier");
2831 if (type_size(&type1, &align) < 0) {
2832 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2833 flexible = 1;
2834 else
2835 tcc_error("field '%s' has incomplete type",
2836 get_tok_str(v, NULL));
2838 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2839 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2840 tcc_error("invalid type for '%s'",
2841 get_tok_str(v, NULL));
2843 if (tok == ':') {
2844 next();
2845 bit_size = expr_const();
2846 /* XXX: handle v = 0 case for messages */
2847 if (bit_size < 0)
2848 tcc_error("negative width in bit-field '%s'",
2849 get_tok_str(v, NULL));
2850 if (v && bit_size == 0)
2851 tcc_error("zero width for bit-field '%s'",
2852 get_tok_str(v, NULL));
2854 size = type_size(&type1, &align);
2855 if (ad.aligned) {
2856 if (align < ad.aligned)
2857 align = ad.aligned;
2858 } else if (ad.packed) {
2859 align = 1;
2860 } else if (*tcc_state->pack_stack_ptr) {
2861 if (align > *tcc_state->pack_stack_ptr)
2862 align = *tcc_state->pack_stack_ptr;
2864 lbit_pos = 0;
2865 if (bit_size >= 0) {
2866 bt = type1.t & VT_BTYPE;
2867 if (bt != VT_INT &&
2868 bt != VT_BYTE &&
2869 bt != VT_SHORT &&
2870 bt != VT_BOOL &&
2871 bt != VT_ENUM &&
2872 bt != VT_LLONG)
2873 tcc_error("bitfields must have scalar type");
2874 bsize = size * 8;
2875 if (bit_size > bsize) {
2876 tcc_error("width of '%s' exceeds its type",
2877 get_tok_str(v, NULL));
2878 } else if (bit_size == bsize) {
2879 /* no need for bit fields */
2880 bit_pos = 0;
2881 } else if (bit_size == 0) {
2882 /* XXX: what to do if only padding in a
2883 structure ? */
2884 /* zero size: means to pad */
2885 bit_pos = 0;
2886 } else {
2887 /* we do not have enough room ?
2888 did the type change?
2889 is it a union? */
2890 if ((bit_pos + bit_size) > bsize ||
2891 bt != prevbt || a == TOK_UNION)
2892 bit_pos = 0;
2893 lbit_pos = bit_pos;
2894 /* XXX: handle LSB first */
2895 type1.t |= VT_BITFIELD |
2896 (bit_pos << VT_STRUCT_SHIFT) |
2897 (bit_size << (VT_STRUCT_SHIFT + 6));
2898 bit_pos += bit_size;
2900 prevbt = bt;
2901 } else {
2902 bit_pos = 0;
2904 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2905 /* add new memory data only if starting
2906 bit field */
2907 if (lbit_pos == 0) {
2908 if (a == TOK_STRUCT) {
2909 c = (c + align - 1) & -align;
2910 offset = c;
2911 if (size > 0)
2912 c += size;
2913 } else {
2914 offset = 0;
2915 if (size > c)
2916 c = size;
2918 if (align > maxalign)
2919 maxalign = align;
2921 #if 0
2922 printf("add field %s offset=%d",
2923 get_tok_str(v, NULL), offset);
2924 if (type1.t & VT_BITFIELD) {
2925 printf(" pos=%d size=%d",
2926 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2927 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2929 printf("\n");
2930 #endif
2932 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2933 ass = type1.ref;
2934 while ((ass = ass->next) != NULL) {
2935 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2936 *ps = ss;
2937 ps = &ss->next;
2939 } else if (v) {
2940 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2941 *ps = ss;
2942 ps = &ss->next;
2944 if (tok == ';' || tok == TOK_EOF)
2945 break;
2946 skip(',');
2948 skip(';');
2950 skip('}');
2951 /* store size and alignment */
2952 s->c = (c + maxalign - 1) & -maxalign;
2953 s->r = maxalign;
2958 /* return 0 if no type declaration. otherwise, return the basic type
2959 and skip it.
2961 static int parse_btype(CType *type, AttributeDef *ad)
2963 int t, u, type_found, typespec_found, typedef_found;
2964 Sym *s;
2965 CType type1;
2967 memset(ad, 0, sizeof(AttributeDef));
2968 type_found = 0;
2969 typespec_found = 0;
2970 typedef_found = 0;
2971 t = 0;
2972 while(1) {
2973 switch(tok) {
2974 case TOK_EXTENSION:
2975 /* currently, we really ignore extension */
2976 next();
2977 continue;
2979 /* basic types */
2980 case TOK_CHAR:
2981 u = VT_BYTE;
2982 basic_type:
2983 next();
2984 basic_type1:
2985 if ((t & VT_BTYPE) != 0)
2986 tcc_error("too many basic types");
2987 t |= u;
2988 typespec_found = 1;
2989 break;
2990 case TOK_VOID:
2991 u = VT_VOID;
2992 goto basic_type;
2993 case TOK_SHORT:
2994 u = VT_SHORT;
2995 goto basic_type;
2996 case TOK_INT:
2997 next();
2998 typespec_found = 1;
2999 break;
3000 case TOK_LONG:
3001 next();
3002 if ((t & VT_BTYPE) == VT_DOUBLE) {
3003 #ifndef TCC_TARGET_PE
3004 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3005 #endif
3006 } else if ((t & VT_BTYPE) == VT_LONG) {
3007 t = (t & ~VT_BTYPE) | VT_LLONG;
3008 } else {
3009 u = VT_LONG;
3010 goto basic_type1;
3012 break;
3013 case TOK_BOOL:
3014 u = VT_BOOL;
3015 goto basic_type;
3016 case TOK_FLOAT:
3017 u = VT_FLOAT;
3018 goto basic_type;
3019 case TOK_DOUBLE:
3020 next();
3021 if ((t & VT_BTYPE) == VT_LONG) {
3022 #ifdef TCC_TARGET_PE
3023 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3024 #else
3025 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3026 #endif
3027 } else {
3028 u = VT_DOUBLE;
3029 goto basic_type1;
3031 break;
3032 case TOK_ENUM:
3033 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3034 basic_type2:
3035 u = type1.t;
3036 type->ref = type1.ref;
3037 goto basic_type1;
3038 case TOK_STRUCT:
3039 case TOK_UNION:
3040 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3041 goto basic_type2;
3043 /* type modifiers */
3044 case TOK_CONST1:
3045 case TOK_CONST2:
3046 case TOK_CONST3:
3047 t |= VT_CONSTANT;
3048 next();
3049 break;
3050 case TOK_VOLATILE1:
3051 case TOK_VOLATILE2:
3052 case TOK_VOLATILE3:
3053 t |= VT_VOLATILE;
3054 next();
3055 break;
3056 case TOK_SIGNED1:
3057 case TOK_SIGNED2:
3058 case TOK_SIGNED3:
3059 typespec_found = 1;
3060 t |= VT_SIGNED;
3061 next();
3062 break;
3063 case TOK_REGISTER:
3064 case TOK_AUTO:
3065 case TOK_RESTRICT1:
3066 case TOK_RESTRICT2:
3067 case TOK_RESTRICT3:
3068 next();
3069 break;
3070 case TOK_UNSIGNED:
3071 t |= VT_UNSIGNED;
3072 next();
3073 typespec_found = 1;
3074 break;
3076 /* storage */
3077 case TOK_EXTERN:
3078 t |= VT_EXTERN;
3079 next();
3080 break;
3081 case TOK_STATIC:
3082 t |= VT_STATIC;
3083 next();
3084 break;
3085 case TOK_TYPEDEF:
3086 t |= VT_TYPEDEF;
3087 next();
3088 break;
3089 case TOK_INLINE1:
3090 case TOK_INLINE2:
3091 case TOK_INLINE3:
3092 t |= VT_INLINE;
3093 next();
3094 break;
3096 /* GNUC attribute */
3097 case TOK_ATTRIBUTE1:
3098 case TOK_ATTRIBUTE2:
3099 parse_attribute(ad);
3100 if (ad->mode) {
3101 u = ad->mode -1;
3102 t = (t & ~VT_BTYPE) | u;
3104 break;
3105 /* GNUC typeof */
3106 case TOK_TYPEOF1:
3107 case TOK_TYPEOF2:
3108 case TOK_TYPEOF3:
3109 next();
3110 parse_expr_type(&type1);
3111 /* remove all storage modifiers except typedef */
3112 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3113 goto basic_type2;
3114 default:
3115 if (typespec_found || typedef_found)
3116 goto the_end;
3117 s = sym_find(tok);
3118 if (!s || !(s->type.t & VT_TYPEDEF))
3119 goto the_end;
3120 typedef_found = 1;
3121 t |= (s->type.t & ~VT_TYPEDEF);
3122 type->ref = s->type.ref;
3123 if (s->r) {
3124 /* get attributes from typedef */
3125 if (0 == ad->aligned)
3126 ad->aligned = FUNC_ALIGN(s->r);
3127 if (0 == ad->func_call)
3128 ad->func_call = FUNC_CALL(s->r);
3129 ad->packed |= FUNC_PACKED(s->r);
3131 next();
3132 typespec_found = 1;
3133 break;
3135 type_found = 1;
3137 the_end:
3138 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3139 tcc_error("signed and unsigned modifier");
3140 if (tcc_state->char_is_unsigned) {
3141 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3142 t |= VT_UNSIGNED;
3144 t &= ~VT_SIGNED;
3146 /* long is never used as type */
3147 if ((t & VT_BTYPE) == VT_LONG)
3148 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3149 t = (t & ~VT_BTYPE) | VT_INT;
3150 #else
3151 t = (t & ~VT_BTYPE) | VT_LLONG;
3152 #endif
3153 type->t = t;
3154 return type_found;
3157 /* convert a function parameter type (array to pointer and function to
3158 function pointer) */
3159 static inline void convert_parameter_type(CType *pt)
3161 /* remove const and volatile qualifiers (XXX: const could be used
3162 to indicate a const function parameter */
3163 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3164 /* array must be transformed to pointer according to ANSI C */
3165 pt->t &= ~VT_ARRAY;
3166 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3167 mk_pointer(pt);
3171 ST_FUNC void parse_asm_str(CString *astr)
3173 skip('(');
3174 /* read the string */
3175 if (tok != TOK_STR)
3176 expect("string constant");
3177 cstr_new(astr);
3178 while (tok == TOK_STR) {
3179 /* XXX: add \0 handling too ? */
3180 cstr_cat(astr, tokc.cstr->data);
3181 next();
3183 cstr_ccat(astr, '\0');
3186 /* Parse an asm label and return the label
3187 * Don't forget to free the CString in the caller! */
3188 static void asm_label_instr(CString *astr)
3190 next();
3191 parse_asm_str(astr);
3192 skip(')');
3193 #ifdef ASM_DEBUG
3194 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3195 #endif
3198 static void post_type(CType *type, AttributeDef *ad)
3200 int n, l, t1, arg_size, align;
3201 Sym **plast, *s, *first;
3202 AttributeDef ad1;
3203 CType pt;
3205 if (tok == '(') {
3206 /* function declaration */
3207 next();
3208 l = 0;
3209 first = NULL;
3210 plast = &first;
3211 arg_size = 0;
3212 if (tok != ')') {
3213 for(;;) {
3214 /* read param name and compute offset */
3215 if (l != FUNC_OLD) {
3216 if (!parse_btype(&pt, &ad1)) {
3217 if (l) {
3218 tcc_error("invalid type");
3219 } else {
3220 l = FUNC_OLD;
3221 goto old_proto;
3224 l = FUNC_NEW;
3225 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3226 break;
3227 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3228 if ((pt.t & VT_BTYPE) == VT_VOID)
3229 tcc_error("parameter declared as void");
3230 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3231 } else {
3232 old_proto:
3233 n = tok;
3234 if (n < TOK_UIDENT)
3235 expect("identifier");
3236 pt.t = VT_INT;
3237 next();
3239 convert_parameter_type(&pt);
3240 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3241 *plast = s;
3242 plast = &s->next;
3243 if (tok == ')')
3244 break;
3245 skip(',');
3246 if (l == FUNC_NEW && tok == TOK_DOTS) {
3247 l = FUNC_ELLIPSIS;
3248 next();
3249 break;
3253 /* if no parameters, then old type prototype */
3254 if (l == 0)
3255 l = FUNC_OLD;
3256 skip(')');
3257 /* NOTE: const is ignored in returned type as it has a special
3258 meaning in gcc / C++ */
3259 type->t &= ~VT_CONSTANT;
3260 /* some ancient pre-K&R C allows a function to return an array
3261 and the array brackets to be put after the arguments, such
3262 that "int c()[]" means something like "int[] c()" */
3263 if (tok == '[') {
3264 next();
3265 skip(']'); /* only handle simple "[]" */
3266 type->t |= VT_PTR;
3268 /* we push a anonymous symbol which will contain the function prototype */
3269 ad->func_args = arg_size;
3270 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3271 s->next = first;
3272 type->t = VT_FUNC;
3273 type->ref = s;
3274 } else if (tok == '[') {
3275 /* array definition */
3276 next();
3277 if (tok == TOK_RESTRICT1)
3278 next();
3279 n = -1;
3280 t1 = 0;
3281 if (tok != ']') {
3282 if (!local_stack || nocode_wanted)
3283 vpushi(expr_const());
3284 else gexpr();
3285 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3286 n = vtop->c.i;
3287 if (n < 0)
3288 tcc_error("invalid array size");
3289 } else {
3290 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3291 tcc_error("size of variable length array should be an integer");
3292 t1 = VT_VLA;
3295 skip(']');
3296 /* parse next post type */
3297 post_type(type, ad);
3298 if (type->t == VT_FUNC)
3299 tcc_error("declaration of an array of functions");
3300 t1 |= type->t & VT_VLA;
3302 if (t1 & VT_VLA) {
3303 loc -= type_size(&int_type, &align);
3304 loc &= -align;
3305 n = loc;
3307 vla_runtime_type_size(type, &align);
3308 gen_op('*');
3309 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3310 vswap();
3311 vstore();
3313 if (n != -1)
3314 vpop();
3316 /* we push an anonymous symbol which will contain the array
3317 element type */
3318 s = sym_push(SYM_FIELD, type, 0, n);
3319 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3320 type->ref = s;
3324 /* Parse a type declaration (except basic type), and return the type
3325 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3326 expected. 'type' should contain the basic type. 'ad' is the
3327 attribute definition of the basic type. It can be modified by
3328 type_decl().
3330 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3332 Sym *s;
3333 CType type1, *type2;
3334 int qualifiers, storage;
3336 while (tok == '*') {
3337 qualifiers = 0;
3338 redo:
3339 next();
3340 switch(tok) {
3341 case TOK_CONST1:
3342 case TOK_CONST2:
3343 case TOK_CONST3:
3344 qualifiers |= VT_CONSTANT;
3345 goto redo;
3346 case TOK_VOLATILE1:
3347 case TOK_VOLATILE2:
3348 case TOK_VOLATILE3:
3349 qualifiers |= VT_VOLATILE;
3350 goto redo;
3351 case TOK_RESTRICT1:
3352 case TOK_RESTRICT2:
3353 case TOK_RESTRICT3:
3354 goto redo;
3356 mk_pointer(type);
3357 type->t |= qualifiers;
3360 /* XXX: clarify attribute handling */
3361 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3362 parse_attribute(ad);
3364 /* recursive type */
3365 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3366 type1.t = 0; /* XXX: same as int */
3367 if (tok == '(') {
3368 next();
3369 /* XXX: this is not correct to modify 'ad' at this point, but
3370 the syntax is not clear */
3371 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3372 parse_attribute(ad);
3373 type_decl(&type1, ad, v, td);
3374 skip(')');
3375 } else {
3376 /* type identifier */
3377 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3378 *v = tok;
3379 next();
3380 } else {
3381 if (!(td & TYPE_ABSTRACT))
3382 expect("identifier");
3383 *v = 0;
3386 storage = type->t & VT_STORAGE;
3387 type->t &= ~VT_STORAGE;
3388 if (storage & VT_STATIC) {
3389 int saved_nocode_wanted = nocode_wanted;
3390 nocode_wanted = 1;
3391 post_type(type, ad);
3392 nocode_wanted = saved_nocode_wanted;
3393 } else
3394 post_type(type, ad);
3395 type->t |= storage;
3396 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3397 parse_attribute(ad);
3399 if (!type1.t)
3400 return;
3401 /* append type at the end of type1 */
3402 type2 = &type1;
3403 for(;;) {
3404 s = type2->ref;
3405 type2 = &s->type;
3406 if (!type2->t) {
3407 *type2 = *type;
3408 break;
3411 *type = type1;
3414 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3415 ST_FUNC int lvalue_type(int t)
3417 int bt, r;
3418 r = VT_LVAL;
3419 bt = t & VT_BTYPE;
3420 if (bt == VT_BYTE || bt == VT_BOOL)
3421 r |= VT_LVAL_BYTE;
3422 else if (bt == VT_SHORT)
3423 r |= VT_LVAL_SHORT;
3424 else
3425 return r;
3426 if (t & VT_UNSIGNED)
3427 r |= VT_LVAL_UNSIGNED;
3428 return r;
3431 /* indirection with full error checking and bound check */
3432 ST_FUNC void indir(void)
3434 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3435 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3436 return;
3437 expect("pointer");
3439 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3440 gv(RC_INT);
3441 vtop->type = *pointed_type(&vtop->type);
3442 /* Arrays and functions are never lvalues */
3443 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3444 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3445 vtop->r |= lvalue_type(vtop->type.t);
3446 /* if bound checking, the referenced pointer must be checked */
3447 #ifdef CONFIG_TCC_BCHECK
3448 if (tcc_state->do_bounds_check)
3449 vtop->r |= VT_MUSTBOUND;
3450 #endif
3454 /* pass a parameter to a function and do type checking and casting */
3455 static void gfunc_param_typed(Sym *func, Sym *arg)
3457 int func_type;
3458 CType type;
3460 func_type = func->c;
3461 if (func_type == FUNC_OLD ||
3462 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3463 /* default casting : only need to convert float to double */
3464 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3465 type.t = VT_DOUBLE;
3466 gen_cast(&type);
3468 } else if (arg == NULL) {
3469 tcc_error("too many arguments to function");
3470 } else {
3471 type = arg->type;
3472 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3473 gen_assign_cast(&type);
3477 /* parse an expression of the form '(type)' or '(expr)' and return its
3478 type */
3479 static void parse_expr_type(CType *type)
3481 int n;
3482 AttributeDef ad;
3484 skip('(');
3485 if (parse_btype(type, &ad)) {
3486 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3487 } else {
3488 expr_type(type);
3490 skip(')');
3493 static void parse_type(CType *type)
3495 AttributeDef ad;
3496 int n;
3498 if (!parse_btype(type, &ad)) {
3499 expect("type");
3501 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3504 static void vpush_tokc(int t)
3506 CType type;
3507 type.t = t;
3508 type.ref = 0;
3509 vsetc(&type, VT_CONST, &tokc);
3512 ST_FUNC void unary(void)
3514 int n, t, align, size, r, sizeof_caller;
3515 CType type;
3516 Sym *s;
3517 AttributeDef ad;
3518 static int in_sizeof = 0;
3520 sizeof_caller = in_sizeof;
3521 in_sizeof = 0;
3522 /* XXX: GCC 2.95.3 does not generate a table although it should be
3523 better here */
3524 tok_next:
3525 switch(tok) {
3526 case TOK_EXTENSION:
3527 next();
3528 goto tok_next;
3529 case TOK_CINT:
3530 case TOK_CCHAR:
3531 case TOK_LCHAR:
3532 vpushi(tokc.i);
3533 next();
3534 break;
3535 case TOK_CUINT:
3536 vpush_tokc(VT_INT | VT_UNSIGNED);
3537 next();
3538 break;
3539 case TOK_CLLONG:
3540 vpush_tokc(VT_LLONG);
3541 next();
3542 break;
3543 case TOK_CULLONG:
3544 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3545 next();
3546 break;
3547 case TOK_CFLOAT:
3548 vpush_tokc(VT_FLOAT);
3549 next();
3550 break;
3551 case TOK_CDOUBLE:
3552 vpush_tokc(VT_DOUBLE);
3553 next();
3554 break;
3555 case TOK_CLDOUBLE:
3556 vpush_tokc(VT_LDOUBLE);
3557 next();
3558 break;
3559 case TOK___FUNCTION__:
3560 if (!gnu_ext)
3561 goto tok_identifier;
3562 /* fall thru */
3563 case TOK___FUNC__:
3565 void *ptr;
3566 int len;
3567 /* special function name identifier */
3568 len = strlen(funcname) + 1;
3569 /* generate char[len] type */
3570 type.t = VT_BYTE;
3571 mk_pointer(&type);
3572 type.t |= VT_ARRAY;
3573 type.ref->c = len;
3574 vpush_ref(&type, data_section, data_section->data_offset, len);
3575 ptr = section_ptr_add(data_section, len);
3576 memcpy(ptr, funcname, len);
3577 next();
3579 break;
3580 case TOK_LSTR:
3581 #ifdef TCC_TARGET_PE
3582 t = VT_SHORT | VT_UNSIGNED;
3583 #else
3584 t = VT_INT;
3585 #endif
3586 goto str_init;
3587 case TOK_STR:
3588 /* string parsing */
3589 t = VT_BYTE;
3590 str_init:
3591 if (tcc_state->warn_write_strings)
3592 t |= VT_CONSTANT;
3593 type.t = t;
3594 mk_pointer(&type);
3595 type.t |= VT_ARRAY;
3596 memset(&ad, 0, sizeof(AttributeDef));
3597 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3598 break;
3599 case '(':
3600 next();
3601 /* cast ? */
3602 if (parse_btype(&type, &ad)) {
3603 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3604 skip(')');
3605 /* check ISOC99 compound literal */
3606 if (tok == '{') {
3607 /* data is allocated locally by default */
3608 if (global_expr)
3609 r = VT_CONST;
3610 else
3611 r = VT_LOCAL;
3612 /* all except arrays are lvalues */
3613 if (!(type.t & VT_ARRAY))
3614 r |= lvalue_type(type.t);
3615 memset(&ad, 0, sizeof(AttributeDef));
3616 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3617 } else {
3618 if (sizeof_caller) {
3619 vpush(&type);
3620 return;
3622 unary();
3623 gen_cast(&type);
3625 } else if (tok == '{') {
3626 /* save all registers */
3627 save_regs(0);
3628 /* statement expression : we do not accept break/continue
3629 inside as GCC does */
3630 block(NULL, NULL, NULL, NULL, 0, 1);
3631 skip(')');
3632 } else {
3633 gexpr();
3634 skip(')');
3636 break;
3637 case '*':
3638 next();
3639 unary();
3640 indir();
3641 break;
3642 case '&':
3643 next();
3644 unary();
3645 /* functions names must be treated as function pointers,
3646 except for unary '&' and sizeof. Since we consider that
3647 functions are not lvalues, we only have to handle it
3648 there and in function calls. */
3649 /* arrays can also be used although they are not lvalues */
3650 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3651 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3652 test_lvalue();
3653 mk_pointer(&vtop->type);
3654 gaddrof();
3655 break;
3656 case '!':
3657 next();
3658 unary();
3659 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3660 CType boolean;
3661 boolean.t = VT_BOOL;
3662 gen_cast(&boolean);
3663 vtop->c.i = !vtop->c.i;
3664 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3665 vtop->c.i = vtop->c.i ^ 1;
3666 else {
3667 save_regs(1);
3668 vseti(VT_JMP, gtst(1, 0));
3670 break;
3671 case '~':
3672 next();
3673 unary();
3674 vpushi(-1);
3675 gen_op('^');
3676 break;
3677 case '+':
3678 next();
3679 /* in order to force cast, we add zero */
3680 unary();
3681 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3682 tcc_error("pointer not accepted for unary plus");
3683 vpushi(0);
3684 gen_op('+');
3685 break;
3686 case TOK_SIZEOF:
3687 case TOK_ALIGNOF1:
3688 case TOK_ALIGNOF2:
3689 t = tok;
3690 next();
3691 in_sizeof++;
3692 unary_type(&type); // Perform a in_sizeof = 0;
3693 size = type_size(&type, &align);
3694 if (t == TOK_SIZEOF) {
3695 if (!(type.t & VT_VLA)) {
3696 if (size < 0)
3697 tcc_error("sizeof applied to an incomplete type");
3698 vpushs(size);
3699 } else {
3700 vla_runtime_type_size(&type, &align);
3702 } else {
3703 vpushs(align);
3705 vtop->type.t |= VT_UNSIGNED;
3706 break;
3708 case TOK_builtin_types_compatible_p:
3710 CType type1, type2;
3711 next();
3712 skip('(');
3713 parse_type(&type1);
3714 skip(',');
3715 parse_type(&type2);
3716 skip(')');
3717 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3718 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3719 vpushi(is_compatible_types(&type1, &type2));
3721 break;
3722 case TOK_builtin_constant_p:
3724 int saved_nocode_wanted, res;
3725 next();
3726 skip('(');
3727 saved_nocode_wanted = nocode_wanted;
3728 nocode_wanted = 1;
3729 gexpr();
3730 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3731 vpop();
3732 nocode_wanted = saved_nocode_wanted;
3733 skip(')');
3734 vpushi(res);
3736 break;
3737 case TOK_builtin_frame_address:
3739 int level;
3740 CType type;
3741 next();
3742 skip('(');
3743 if (tok != TOK_CINT || tokc.i < 0) {
3744 tcc_error("__builtin_frame_address only takes positive integers");
3746 level = tokc.i;
3747 next();
3748 skip(')');
3749 type.t = VT_VOID;
3750 mk_pointer(&type);
3751 vset(&type, VT_LOCAL, 0); /* local frame */
3752 while (level--) {
3753 mk_pointer(&vtop->type);
3754 indir(); /* -> parent frame */
3757 break;
3758 #ifdef TCC_TARGET_X86_64
3759 #ifdef TCC_TARGET_PE
3760 case TOK_builtin_va_start:
3762 next();
3763 skip('(');
3764 expr_eq();
3765 skip(',');
3766 expr_eq();
3767 skip(')');
3768 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3769 tcc_error("__builtin_va_start expects a local variable");
3770 vtop->r &= ~(VT_LVAL | VT_REF);
3771 vtop->type = char_pointer_type;
3772 vstore();
3774 break;
3775 #else
3776 case TOK_builtin_va_arg_types:
3778 CType type;
3779 next();
3780 skip('(');
3781 parse_type(&type);
3782 skip(')');
3783 vpushi(classify_x86_64_va_arg(&type));
3785 break;
3786 #endif
3787 #endif
3788 case TOK_INC:
3789 case TOK_DEC:
3790 t = tok;
3791 next();
3792 unary();
3793 inc(0, t);
3794 break;
3795 case '-':
3796 next();
3797 unary();
3798 t = vtop->type.t & VT_BTYPE;
3799 /* handle (-)0.0 */
3800 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST &&
3801 is_float(t)) {
3802 if (t == VT_FLOAT)
3803 vtop->c.f = -vtop->c.f;
3804 else if (t == VT_DOUBLE)
3805 vtop->c.d = -vtop->c.d;
3806 else
3807 vtop->c.ld = -vtop->c.ld;
3808 } else {
3809 vpushi(0);
3810 vswap();
3811 gen_op('-');
3813 break;
3814 case TOK_LAND:
3815 if (!gnu_ext)
3816 goto tok_identifier;
3817 next();
3818 /* allow to take the address of a label */
3819 if (tok < TOK_UIDENT)
3820 expect("label identifier");
3821 s = label_find(tok);
3822 if (!s) {
3823 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3824 } else {
3825 if (s->r == LABEL_DECLARED)
3826 s->r = LABEL_FORWARD;
3828 if (!s->type.t) {
3829 s->type.t = VT_VOID;
3830 mk_pointer(&s->type);
3831 s->type.t |= VT_STATIC;
3833 vset(&s->type, VT_CONST | VT_SYM, 0);
3834 vtop->sym = s;
3835 next();
3836 break;
3838 // special qnan , snan and infinity values
3839 case TOK___NAN__:
3840 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3841 next();
3842 break;
3843 case TOK___SNAN__:
3844 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3845 next();
3846 break;
3847 case TOK___INF__:
3848 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3849 next();
3850 break;
3852 default:
3853 tok_identifier:
3854 t = tok;
3855 next();
3856 if (t < TOK_UIDENT)
3857 expect("identifier");
3858 s = sym_find(t);
3859 if (!s) {
3860 if (tok != '(')
3861 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3862 /* for simple function calls, we tolerate undeclared
3863 external reference to int() function */
3864 if (tcc_state->warn_implicit_function_declaration)
3865 tcc_warning("implicit declaration of function '%s'",
3866 get_tok_str(t, NULL));
3867 s = external_global_sym(t, &func_old_type, 0);
3869 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3870 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3871 /* if referencing an inline function, then we generate a
3872 symbol to it if not already done. It will have the
3873 effect to generate code for it at the end of the
3874 compilation unit. Inline function as always
3875 generated in the text section. */
3876 if (!s->c)
3877 put_extern_sym(s, text_section, 0, 0);
3878 r = VT_SYM | VT_CONST;
3879 } else {
3880 r = s->r;
3882 vset(&s->type, r, s->c);
3883 /* if forward reference, we must point to s */
3884 if (vtop->r & VT_SYM) {
3885 vtop->sym = s;
3886 vtop->c.ul = 0;
3888 break;
3891 /* post operations */
3892 while (1) {
3893 if (tok == TOK_INC || tok == TOK_DEC) {
3894 inc(1, tok);
3895 next();
3896 } else if (tok == '.' || tok == TOK_ARROW) {
3897 int qualifiers;
3898 /* field */
3899 if (tok == TOK_ARROW)
3900 indir();
3901 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3902 test_lvalue();
3903 gaddrof();
3904 next();
3905 /* expect pointer on structure */
3906 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3907 expect("struct or union");
3908 s = vtop->type.ref;
3909 /* find field */
3910 tok |= SYM_FIELD;
3911 while ((s = s->next) != NULL) {
3912 if (s->v == tok)
3913 break;
3915 if (!s)
3916 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3917 /* add field offset to pointer */
3918 vtop->type = char_pointer_type; /* change type to 'char *' */
3919 vpushi(s->c);
3920 gen_op('+');
3921 /* change type to field type, and set to lvalue */
3922 vtop->type = s->type;
3923 vtop->type.t |= qualifiers;
3924 /* an array is never an lvalue */
3925 if (!(vtop->type.t & VT_ARRAY)) {
3926 vtop->r |= lvalue_type(vtop->type.t);
3927 #ifdef CONFIG_TCC_BCHECK
3928 /* if bound checking, the referenced pointer must be checked */
3929 if (tcc_state->do_bounds_check)
3930 vtop->r |= VT_MUSTBOUND;
3931 #endif
3933 next();
3934 } else if (tok == '[') {
3935 next();
3936 gexpr();
3937 gen_op('+');
3938 indir();
3939 skip(']');
3940 } else if (tok == '(') {
3941 SValue ret;
3942 Sym *sa;
3943 int nb_args, ret_nregs, ret_align;
3945 /* function call */
3946 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3947 /* pointer test (no array accepted) */
3948 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3949 vtop->type = *pointed_type(&vtop->type);
3950 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3951 goto error_func;
3952 } else {
3953 error_func:
3954 expect("function pointer");
3956 } else {
3957 vtop->r &= ~VT_LVAL; /* no lvalue */
3959 /* get return type */
3960 s = vtop->type.ref;
3961 next();
3962 sa = s->next; /* first parameter */
3963 nb_args = 0;
3964 ret.r2 = VT_CONST;
3965 /* compute first implicit argument if a structure is returned */
3966 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3967 ret_nregs = gfunc_sret(&s->type, &ret.type, &ret_align);
3968 if (!ret_nregs) {
3969 /* get some space for the returned structure */
3970 size = type_size(&s->type, &align);
3971 loc = (loc - size) & -align;
3972 ret.type = s->type;
3973 ret.r = VT_LOCAL | VT_LVAL;
3974 /* pass it as 'int' to avoid structure arg passing
3975 problems */
3976 vseti(VT_LOCAL, loc);
3977 ret.c = vtop->c;
3978 nb_args++;
3980 } else {
3981 ret_nregs = 1;
3982 ret.type = s->type;
3985 if (ret_nregs) {
3986 /* return in register */
3987 if (is_float(ret.type.t)) {
3988 ret.r = reg_fret(ret.type.t);
3989 #ifdef TCC_TARGET_X86_64
3990 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
3991 ret.r2 = REG_QRET;
3992 #endif
3993 } else {
3994 #ifdef TCC_TARGET_X86_64
3995 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
3996 #else
3997 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3998 #endif
3999 ret.r2 = REG_LRET;
4000 ret.r = REG_IRET;
4002 ret.c.i = 0;
4004 if (tok != ')') {
4005 for(;;) {
4006 expr_eq();
4007 gfunc_param_typed(s, sa);
4008 nb_args++;
4009 if (sa)
4010 sa = sa->next;
4011 if (tok == ')')
4012 break;
4013 skip(',');
4016 if (sa)
4017 tcc_error("too few arguments to function");
4018 skip(')');
4019 if (!nocode_wanted) {
4020 gfunc_call(nb_args);
4021 } else {
4022 vtop -= (nb_args + 1);
4025 /* return value */
4026 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4027 vsetc(&ret.type, r, &ret.c);
4028 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4031 /* handle packed struct return */
4032 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4033 int addr, offset;
4035 size = type_size(&s->type, &align);
4036 loc = (loc - size) & -align;
4037 addr = loc;
4038 offset = 0;
4039 for (;;) {
4040 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4041 vswap();
4042 vstore();
4043 vtop--;
4044 if (--ret_nregs == 0)
4045 break;
4046 /* XXX: compatible with arm only: ret_align == register_size */
4047 offset += ret_align;
4049 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4051 } else {
4052 break;
4057 ST_FUNC void expr_prod(void)
4059 int t;
4061 unary();
4062 while (tok == '*' || tok == '/' || tok == '%') {
4063 t = tok;
4064 next();
4065 unary();
4066 gen_op(t);
4070 ST_FUNC void expr_sum(void)
4072 int t;
4074 expr_prod();
4075 while (tok == '+' || tok == '-') {
4076 t = tok;
4077 next();
4078 expr_prod();
4079 gen_op(t);
4083 static void expr_shift(void)
4085 int t;
4087 expr_sum();
4088 while (tok == TOK_SHL || tok == TOK_SAR) {
4089 t = tok;
4090 next();
4091 expr_sum();
4092 gen_op(t);
4096 static void expr_cmp(void)
4098 int t;
4100 expr_shift();
4101 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4102 tok == TOK_ULT || tok == TOK_UGE) {
4103 t = tok;
4104 next();
4105 expr_shift();
4106 gen_op(t);
4110 static void expr_cmpeq(void)
4112 int t;
4114 expr_cmp();
4115 while (tok == TOK_EQ || tok == TOK_NE) {
4116 t = tok;
4117 next();
4118 expr_cmp();
4119 gen_op(t);
4123 static void expr_and(void)
4125 expr_cmpeq();
4126 while (tok == '&') {
4127 next();
4128 expr_cmpeq();
4129 gen_op('&');
4133 static void expr_xor(void)
4135 expr_and();
4136 while (tok == '^') {
4137 next();
4138 expr_and();
4139 gen_op('^');
4143 static void expr_or(void)
4145 expr_xor();
4146 while (tok == '|') {
4147 next();
4148 expr_xor();
4149 gen_op('|');
4153 /* XXX: fix this mess */
4154 static void expr_land_const(void)
4156 expr_or();
4157 while (tok == TOK_LAND) {
4158 next();
4159 expr_or();
4160 gen_op(TOK_LAND);
4164 /* XXX: fix this mess */
4165 static void expr_lor_const(void)
4167 expr_land_const();
4168 while (tok == TOK_LOR) {
4169 next();
4170 expr_land_const();
4171 gen_op(TOK_LOR);
4175 /* only used if non constant */
4176 static void expr_land(void)
4178 int t;
4180 expr_or();
4181 if (tok == TOK_LAND) {
4182 t = 0;
4183 save_regs(1);
4184 for(;;) {
4185 t = gtst(1, t);
4186 if (tok != TOK_LAND) {
4187 vseti(VT_JMPI, t);
4188 break;
4190 next();
4191 expr_or();
4196 static void expr_lor(void)
4198 int t;
4200 expr_land();
4201 if (tok == TOK_LOR) {
4202 t = 0;
4203 save_regs(1);
4204 for(;;) {
4205 t = gtst(0, t);
4206 if (tok != TOK_LOR) {
4207 vseti(VT_JMP, t);
4208 break;
4210 next();
4211 expr_land();
4216 /* XXX: better constant handling */
4217 static void expr_cond(void)
4219 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4220 SValue sv;
4221 CType type, type1, type2;
4223 if (const_wanted) {
4224 expr_lor_const();
4225 if (tok == '?') {
4226 CType boolean;
4227 int c;
4228 boolean.t = VT_BOOL;
4229 vdup();
4230 gen_cast(&boolean);
4231 c = vtop->c.i;
4232 vpop();
4233 next();
4234 if (tok != ':' || !gnu_ext) {
4235 vpop();
4236 gexpr();
4238 if (!c)
4239 vpop();
4240 skip(':');
4241 expr_cond();
4242 if (c)
4243 vpop();
4245 } else {
4246 expr_lor();
4247 if (tok == '?') {
4248 next();
4249 if (vtop != vstack) {
4250 /* needed to avoid having different registers saved in
4251 each branch */
4252 if (is_float(vtop->type.t)) {
4253 rc = RC_FLOAT;
4254 #ifdef TCC_TARGET_X86_64
4255 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4256 rc = RC_ST0;
4258 #endif
4260 else
4261 rc = RC_INT;
4262 gv(rc);
4263 save_regs(1);
4265 if (tok == ':' && gnu_ext) {
4266 gv_dup();
4267 tt = gtst(1, 0);
4268 } else {
4269 tt = gtst(1, 0);
4270 gexpr();
4272 type1 = vtop->type;
4273 sv = *vtop; /* save value to handle it later */
4274 vtop--; /* no vpop so that FP stack is not flushed */
4275 skip(':');
4276 u = gjmp(0);
4277 gsym(tt);
4278 expr_cond();
4279 type2 = vtop->type;
4281 t1 = type1.t;
4282 bt1 = t1 & VT_BTYPE;
4283 t2 = type2.t;
4284 bt2 = t2 & VT_BTYPE;
4285 /* cast operands to correct type according to ISOC rules */
4286 if (is_float(bt1) || is_float(bt2)) {
4287 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4288 type.t = VT_LDOUBLE;
4289 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4290 type.t = VT_DOUBLE;
4291 } else {
4292 type.t = VT_FLOAT;
4294 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4295 /* cast to biggest op */
4296 type.t = VT_LLONG;
4297 /* convert to unsigned if it does not fit in a long long */
4298 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4299 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4300 type.t |= VT_UNSIGNED;
4301 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4302 /* If one is a null ptr constant the result type
4303 is the other. */
4304 if (is_null_pointer (vtop))
4305 type = type1;
4306 else if (is_null_pointer (&sv))
4307 type = type2;
4308 /* XXX: test pointer compatibility, C99 has more elaborate
4309 rules here. */
4310 else
4311 type = type1;
4312 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4313 /* XXX: test function pointer compatibility */
4314 type = bt1 == VT_FUNC ? type1 : type2;
4315 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4316 /* XXX: test structure compatibility */
4317 type = bt1 == VT_STRUCT ? type1 : type2;
4318 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4319 /* NOTE: as an extension, we accept void on only one side */
4320 type.t = VT_VOID;
4321 } else {
4322 /* integer operations */
4323 type.t = VT_INT;
4324 /* convert to unsigned if it does not fit in an integer */
4325 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4326 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4327 type.t |= VT_UNSIGNED;
4330 /* now we convert second operand */
4331 gen_cast(&type);
4332 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4333 gaddrof();
4334 rc = RC_INT;
4335 if (is_float(type.t)) {
4336 rc = RC_FLOAT;
4337 #ifdef TCC_TARGET_X86_64
4338 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4339 rc = RC_ST0;
4341 #endif
4342 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4343 /* for long longs, we use fixed registers to avoid having
4344 to handle a complicated move */
4345 rc = RC_IRET;
4348 r2 = gv(rc);
4349 /* this is horrible, but we must also convert first
4350 operand */
4351 tt = gjmp(0);
4352 gsym(u);
4353 /* put again first value and cast it */
4354 *vtop = sv;
4355 gen_cast(&type);
4356 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4357 gaddrof();
4358 r1 = gv(rc);
4359 move_reg(r2, r1, type.t);
4360 vtop->r = r2;
4361 gsym(tt);
4366 static void expr_eq(void)
4368 int t;
4370 expr_cond();
4371 if (tok == '=' ||
4372 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4373 tok == TOK_A_XOR || tok == TOK_A_OR ||
4374 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4375 test_lvalue();
4376 t = tok;
4377 next();
4378 if (t == '=') {
4379 expr_eq();
4380 } else {
4381 vdup();
4382 expr_eq();
4383 gen_op(t & 0x7f);
4385 vstore();
4389 ST_FUNC void gexpr(void)
4391 while (1) {
4392 expr_eq();
4393 if (tok != ',')
4394 break;
4395 vpop();
4396 next();
4400 /* parse an expression and return its type without any side effect. */
4401 static void expr_type(CType *type)
4403 int saved_nocode_wanted;
4405 saved_nocode_wanted = nocode_wanted;
4406 nocode_wanted = 1;
4407 gexpr();
4408 *type = vtop->type;
4409 vpop();
4410 nocode_wanted = saved_nocode_wanted;
4413 /* parse a unary expression and return its type without any side
4414 effect. */
4415 static void unary_type(CType *type)
4417 int a;
4419 a = nocode_wanted;
4420 nocode_wanted = 1;
4421 unary();
4422 *type = vtop->type;
4423 vpop();
4424 nocode_wanted = a;
4427 /* parse a constant expression and return value in vtop. */
4428 static void expr_const1(void)
4430 int a;
4431 a = const_wanted;
4432 const_wanted = 1;
4433 expr_cond();
4434 const_wanted = a;
4437 /* parse an integer constant and return its value. */
4438 ST_FUNC int expr_const(void)
4440 int c;
4441 expr_const1();
4442 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4443 expect("constant expression");
4444 c = vtop->c.i;
4445 vpop();
4446 return c;
4449 /* return the label token if current token is a label, otherwise
4450 return zero */
4451 static int is_label(void)
4453 int last_tok;
4455 /* fast test first */
4456 if (tok < TOK_UIDENT)
4457 return 0;
4458 /* no need to save tokc because tok is an identifier */
4459 last_tok = tok;
4460 next();
4461 if (tok == ':') {
4462 next();
4463 return last_tok;
4464 } else {
4465 unget_tok(last_tok);
4466 return 0;
4470 static void label_or_decl(int l)
4472 int last_tok;
4474 /* fast test first */
4475 if (tok >= TOK_UIDENT)
4477 /* no need to save tokc because tok is an identifier */
4478 last_tok = tok;
4479 next();
4480 if (tok == ':') {
4481 unget_tok(last_tok);
4482 return;
4484 unget_tok(last_tok);
4486 decl(l);
4489 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4490 int case_reg, int is_expr)
4492 int a, b, c, d;
4493 Sym *s, *frame_bottom;
4495 /* generate line number info */
4496 if (tcc_state->do_debug &&
4497 (last_line_num != file->line_num || last_ind != ind)) {
4498 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4499 last_ind = ind;
4500 last_line_num = file->line_num;
4503 if (is_expr) {
4504 /* default return value is (void) */
4505 vpushi(0);
4506 vtop->type.t = VT_VOID;
4509 if (tok == TOK_IF) {
4510 /* if test */
4511 next();
4512 skip('(');
4513 gexpr();
4514 skip(')');
4515 a = gtst(1, 0);
4516 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4517 c = tok;
4518 if (c == TOK_ELSE) {
4519 next();
4520 d = gjmp(0);
4521 gsym(a);
4522 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4523 gsym(d); /* patch else jmp */
4524 } else
4525 gsym(a);
4526 } else if (tok == TOK_WHILE) {
4527 next();
4528 d = ind;
4529 skip('(');
4530 gexpr();
4531 skip(')');
4532 a = gtst(1, 0);
4533 b = 0;
4534 block(&a, &b, case_sym, def_sym, case_reg, 0);
4535 gjmp_addr(d);
4536 gsym(a);
4537 gsym_addr(b, d);
4538 } else if (tok == '{') {
4539 Sym *llabel;
4540 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4542 next();
4543 /* record local declaration stack position */
4544 s = local_stack;
4545 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4546 frame_bottom->next = scope_stack_bottom;
4547 scope_stack_bottom = frame_bottom;
4548 llabel = local_label_stack;
4550 /* save VLA state */
4551 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4552 if (saved_vla_sp_loc != &vla_sp_root_loc)
4553 vla_sp_loc = &block_vla_sp_loc;
4555 saved_vla_flags = vla_flags;
4556 vla_flags |= VLA_NEED_NEW_FRAME;
4558 /* handle local labels declarations */
4559 if (tok == TOK_LABEL) {
4560 next();
4561 for(;;) {
4562 if (tok < TOK_UIDENT)
4563 expect("label identifier");
4564 label_push(&local_label_stack, tok, LABEL_DECLARED);
4565 next();
4566 if (tok == ',') {
4567 next();
4568 } else {
4569 skip(';');
4570 break;
4574 while (tok != '}') {
4575 label_or_decl(VT_LOCAL);
4576 if (tok != '}') {
4577 if (is_expr)
4578 vpop();
4579 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4582 /* pop locally defined labels */
4583 label_pop(&local_label_stack, llabel);
4584 if(is_expr) {
4585 /* XXX: this solution makes only valgrind happy...
4586 triggered by gcc.c-torture/execute/20000917-1.c */
4587 Sym *p;
4588 switch(vtop->type.t & VT_BTYPE) {
4589 case VT_PTR:
4590 case VT_STRUCT:
4591 case VT_ENUM:
4592 case VT_FUNC:
4593 for(p=vtop->type.ref;p;p=p->prev)
4594 if(p->prev==s)
4595 tcc_error("unsupported expression type");
4598 /* pop locally defined symbols */
4599 scope_stack_bottom = scope_stack_bottom->next;
4600 sym_pop(&local_stack, s);
4602 /* Pop VLA frames and restore stack pointer if required */
4603 if (saved_vla_sp_loc != &vla_sp_root_loc)
4604 *saved_vla_sp_loc = block_vla_sp_loc;
4605 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4606 vla_sp_loc = saved_vla_sp_loc;
4607 gen_vla_sp_restore(*vla_sp_loc);
4609 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4611 next();
4612 } else if (tok == TOK_RETURN) {
4613 next();
4614 if (tok != ';') {
4615 gexpr();
4616 gen_assign_cast(&func_vt);
4617 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4618 CType type, ret_type;
4619 int ret_align, ret_nregs;
4620 ret_nregs = gfunc_sret(&func_vt, &ret_type, &ret_align);
4621 if (0 == ret_nregs) {
4622 /* if returning structure, must copy it to implicit
4623 first pointer arg location */
4624 type = func_vt;
4625 mk_pointer(&type);
4626 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4627 indir();
4628 vswap();
4629 /* copy structure value to pointer */
4630 vstore();
4631 } else {
4632 /* returning structure packed into registers */
4633 int r, size, addr, align;
4634 size = type_size(&func_vt,&align);
4635 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4636 && (align & (ret_align-1))) {
4637 loc = (loc - size) & -align;
4638 addr = loc;
4639 type = func_vt;
4640 vset(&type, VT_LOCAL | VT_LVAL, addr);
4641 vswap();
4642 vstore();
4643 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4645 vtop->type = ret_type;
4646 if (is_float(ret_type.t))
4647 r = rc_fret(ret_type.t);
4648 else
4649 r = RC_IRET;
4651 for (;;) {
4652 gv(r);
4653 if (--ret_nregs == 0)
4654 break;
4655 /* We assume that when a structure is returned in multiple
4656 registers, their classes are consecutive values of the
4657 suite s(n) = 2^n */
4658 r <<= 1;
4659 /* XXX: compatible with arm only: ret_align == register_size */
4660 vtop->c.i += ret_align;
4661 vtop->r = VT_LOCAL | VT_LVAL;
4664 } else if (is_float(func_vt.t)) {
4665 gv(rc_fret(func_vt.t));
4666 } else {
4667 gv(RC_IRET);
4669 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4671 skip(';');
4672 rsym = gjmp(rsym); /* jmp */
4673 } else if (tok == TOK_BREAK) {
4674 /* compute jump */
4675 if (!bsym)
4676 tcc_error("cannot break");
4677 *bsym = gjmp(*bsym);
4678 next();
4679 skip(';');
4680 } else if (tok == TOK_CONTINUE) {
4681 /* compute jump */
4682 if (!csym)
4683 tcc_error("cannot continue");
4684 *csym = gjmp(*csym);
4685 next();
4686 skip(';');
4687 } else if (tok == TOK_FOR) {
4688 int e;
4689 next();
4690 skip('(');
4691 s = local_stack;
4692 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4693 frame_bottom->next = scope_stack_bottom;
4694 scope_stack_bottom = frame_bottom;
4695 if (tok != ';') {
4696 /* c99 for-loop init decl? */
4697 if (!decl0(VT_LOCAL, 1)) {
4698 /* no, regular for-loop init expr */
4699 gexpr();
4700 vpop();
4703 skip(';');
4704 d = ind;
4705 c = ind;
4706 a = 0;
4707 b = 0;
4708 if (tok != ';') {
4709 gexpr();
4710 a = gtst(1, 0);
4712 skip(';');
4713 if (tok != ')') {
4714 e = gjmp(0);
4715 c = ind;
4716 gexpr();
4717 vpop();
4718 gjmp_addr(d);
4719 gsym(e);
4721 skip(')');
4722 block(&a, &b, case_sym, def_sym, case_reg, 0);
4723 gjmp_addr(c);
4724 gsym(a);
4725 gsym_addr(b, c);
4726 scope_stack_bottom = scope_stack_bottom->next;
4727 sym_pop(&local_stack, s);
4728 } else
4729 if (tok == TOK_DO) {
4730 next();
4731 a = 0;
4732 b = 0;
4733 d = ind;
4734 block(&a, &b, case_sym, def_sym, case_reg, 0);
4735 skip(TOK_WHILE);
4736 skip('(');
4737 gsym(b);
4738 gexpr();
4739 c = gtst(0, 0);
4740 gsym_addr(c, d);
4741 skip(')');
4742 gsym(a);
4743 skip(';');
4744 } else
4745 if (tok == TOK_SWITCH) {
4746 next();
4747 skip('(');
4748 gexpr();
4749 /* XXX: other types than integer */
4750 case_reg = gv(RC_INT);
4751 vpop();
4752 skip(')');
4753 a = 0;
4754 b = gjmp(0); /* jump to first case */
4755 c = 0;
4756 block(&a, csym, &b, &c, case_reg, 0);
4757 /* if no default, jmp after switch */
4758 if (c == 0)
4759 c = ind;
4760 /* default label */
4761 gsym_addr(b, c);
4762 /* break label */
4763 gsym(a);
4764 } else
4765 if (tok == TOK_CASE) {
4766 int v1, v2;
4767 if (!case_sym)
4768 expect("switch");
4769 next();
4770 v1 = expr_const();
4771 v2 = v1;
4772 if (gnu_ext && tok == TOK_DOTS) {
4773 next();
4774 v2 = expr_const();
4775 if (v2 < v1)
4776 tcc_warning("empty case range");
4778 /* since a case is like a label, we must skip it with a jmp */
4779 b = gjmp(0);
4780 gsym(*case_sym);
4781 vseti(case_reg, 0);
4782 vpushi(v1);
4783 if (v1 == v2) {
4784 gen_op(TOK_EQ);
4785 *case_sym = gtst(1, 0);
4786 } else {
4787 gen_op(TOK_GE);
4788 *case_sym = gtst(1, 0);
4789 vseti(case_reg, 0);
4790 vpushi(v2);
4791 gen_op(TOK_LE);
4792 *case_sym = gtst(1, *case_sym);
4794 gsym(b);
4795 skip(':');
4796 is_expr = 0;
4797 goto block_after_label;
4798 } else
4799 if (tok == TOK_DEFAULT) {
4800 next();
4801 skip(':');
4802 if (!def_sym)
4803 expect("switch");
4804 if (*def_sym)
4805 tcc_error("too many 'default'");
4806 *def_sym = ind;
4807 is_expr = 0;
4808 goto block_after_label;
4809 } else
4810 if (tok == TOK_GOTO) {
4811 next();
4812 if (tok == '*' && gnu_ext) {
4813 /* computed goto */
4814 next();
4815 gexpr();
4816 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4817 expect("pointer");
4818 ggoto();
4819 } else if (tok >= TOK_UIDENT) {
4820 s = label_find(tok);
4821 /* put forward definition if needed */
4822 if (!s) {
4823 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4824 } else {
4825 if (s->r == LABEL_DECLARED)
4826 s->r = LABEL_FORWARD;
4828 /* label already defined */
4829 if (vla_flags & VLA_IN_SCOPE) {
4830 /* If VLAs are in use, save the current stack pointer and
4831 reset the stack pointer to what it was at function entry
4832 (label will restore stack pointer in inner scopes) */
4833 vla_sp_save();
4834 gen_vla_sp_restore(vla_sp_root_loc);
4836 if (s->r & LABEL_FORWARD)
4837 s->jnext = gjmp(s->jnext);
4838 else
4839 gjmp_addr(s->jnext);
4840 next();
4841 } else {
4842 expect("label identifier");
4844 skip(';');
4845 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4846 asm_instr();
4847 } else {
4848 b = is_label();
4849 if (b) {
4850 /* label case */
4851 if (vla_flags & VLA_IN_SCOPE) {
4852 /* save/restore stack pointer across label
4853 this is a no-op when combined with the load immediately
4854 after the label unless we arrive via goto */
4855 vla_sp_save();
4857 s = label_find(b);
4858 if (s) {
4859 if (s->r == LABEL_DEFINED)
4860 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4861 gsym(s->jnext);
4862 s->r = LABEL_DEFINED;
4863 } else {
4864 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4866 s->jnext = ind;
4867 if (vla_flags & VLA_IN_SCOPE) {
4868 gen_vla_sp_restore(*vla_sp_loc);
4869 vla_flags |= VLA_NEED_NEW_FRAME;
4871 /* we accept this, but it is a mistake */
4872 block_after_label:
4873 if (tok == '}') {
4874 tcc_warning("deprecated use of label at end of compound statement");
4875 } else {
4876 if (is_expr)
4877 vpop();
4878 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4880 } else {
4881 /* expression case */
4882 if (tok != ';') {
4883 if (is_expr) {
4884 vpop();
4885 gexpr();
4886 } else {
4887 gexpr();
4888 vpop();
4891 skip(';');
4896 /* t is the array or struct type. c is the array or struct
4897 address. cur_index/cur_field is the pointer to the current
4898 value. 'size_only' is true if only size info is needed (only used
4899 in arrays) */
4900 static void decl_designator(CType *type, Section *sec, unsigned long c,
4901 int *cur_index, Sym **cur_field,
4902 int size_only)
4904 Sym *s, *f;
4905 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4906 CType type1;
4908 notfirst = 0;
4909 elem_size = 0;
4910 nb_elems = 1;
4911 if (gnu_ext && (l = is_label()) != 0)
4912 goto struct_field;
4913 while (tok == '[' || tok == '.') {
4914 if (tok == '[') {
4915 if (!(type->t & VT_ARRAY))
4916 expect("array type");
4917 s = type->ref;
4918 next();
4919 index = expr_const();
4920 if (index < 0 || (s->c >= 0 && index >= s->c))
4921 expect("invalid index");
4922 if (tok == TOK_DOTS && gnu_ext) {
4923 next();
4924 index_last = expr_const();
4925 if (index_last < 0 ||
4926 (s->c >= 0 && index_last >= s->c) ||
4927 index_last < index)
4928 expect("invalid index");
4929 } else {
4930 index_last = index;
4932 skip(']');
4933 if (!notfirst)
4934 *cur_index = index_last;
4935 type = pointed_type(type);
4936 elem_size = type_size(type, &align);
4937 c += index * elem_size;
4938 /* NOTE: we only support ranges for last designator */
4939 nb_elems = index_last - index + 1;
4940 if (nb_elems != 1) {
4941 notfirst = 1;
4942 break;
4944 } else {
4945 next();
4946 l = tok;
4947 next();
4948 struct_field:
4949 if ((type->t & VT_BTYPE) != VT_STRUCT)
4950 expect("struct/union type");
4951 s = type->ref;
4952 l |= SYM_FIELD;
4953 f = s->next;
4954 while (f) {
4955 if (f->v == l)
4956 break;
4957 f = f->next;
4959 if (!f)
4960 expect("field");
4961 if (!notfirst)
4962 *cur_field = f;
4963 /* XXX: fix this mess by using explicit storage field */
4964 type1 = f->type;
4965 type1.t |= (type->t & ~VT_TYPE);
4966 type = &type1;
4967 c += f->c;
4969 notfirst = 1;
4971 if (notfirst) {
4972 if (tok == '=') {
4973 next();
4974 } else {
4975 if (!gnu_ext)
4976 expect("=");
4978 } else {
4979 if (type->t & VT_ARRAY) {
4980 index = *cur_index;
4981 type = pointed_type(type);
4982 c += index * type_size(type, &align);
4983 } else {
4984 f = *cur_field;
4985 if (!f)
4986 tcc_error("too many field init");
4987 /* XXX: fix this mess by using explicit storage field */
4988 type1 = f->type;
4989 type1.t |= (type->t & ~VT_TYPE);
4990 type = &type1;
4991 c += f->c;
4994 decl_initializer(type, sec, c, 0, size_only);
4996 /* XXX: make it more general */
4997 if (!size_only && nb_elems > 1) {
4998 unsigned long c_end;
4999 uint8_t *src, *dst;
5000 int i;
5002 if (!sec)
5003 tcc_error("range init not supported yet for dynamic storage");
5004 c_end = c + nb_elems * elem_size;
5005 if (c_end > sec->data_allocated)
5006 section_realloc(sec, c_end);
5007 src = sec->data + c;
5008 dst = src;
5009 for(i = 1; i < nb_elems; i++) {
5010 dst += elem_size;
5011 memcpy(dst, src, elem_size);
5016 #define EXPR_VAL 0
5017 #define EXPR_CONST 1
5018 #define EXPR_ANY 2
5020 /* store a value or an expression directly in global data or in local array */
5021 static void init_putv(CType *type, Section *sec, unsigned long c,
5022 int v, int expr_type)
5024 int saved_global_expr, bt, bit_pos, bit_size;
5025 void *ptr;
5026 unsigned long long bit_mask;
5027 CType dtype;
5029 switch(expr_type) {
5030 case EXPR_VAL:
5031 vpushi(v);
5032 break;
5033 case EXPR_CONST:
5034 /* compound literals must be allocated globally in this case */
5035 saved_global_expr = global_expr;
5036 global_expr = 1;
5037 expr_const1();
5038 global_expr = saved_global_expr;
5039 /* NOTE: symbols are accepted */
5040 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5041 tcc_error("initializer element is not constant");
5042 break;
5043 case EXPR_ANY:
5044 expr_eq();
5045 break;
5048 dtype = *type;
5049 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5051 if (sec) {
5052 /* XXX: not portable */
5053 /* XXX: generate error if incorrect relocation */
5054 gen_assign_cast(&dtype);
5055 bt = type->t & VT_BTYPE;
5056 /* we'll write at most 12 bytes */
5057 if (c + 12 > sec->data_allocated) {
5058 section_realloc(sec, c + 12);
5060 ptr = sec->data + c;
5061 /* XXX: make code faster ? */
5062 if (!(type->t & VT_BITFIELD)) {
5063 bit_pos = 0;
5064 bit_size = 32;
5065 bit_mask = -1LL;
5066 } else {
5067 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5068 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5069 bit_mask = (1LL << bit_size) - 1;
5071 if ((vtop->r & VT_SYM) &&
5072 (bt == VT_BYTE ||
5073 bt == VT_SHORT ||
5074 bt == VT_DOUBLE ||
5075 bt == VT_LDOUBLE ||
5076 bt == VT_LLONG ||
5077 (bt == VT_INT && bit_size != 32)))
5078 tcc_error("initializer element is not computable at load time");
5079 switch(bt) {
5080 case VT_BOOL:
5081 vtop->c.i = (vtop->c.i != 0);
5082 case VT_BYTE:
5083 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5084 break;
5085 case VT_SHORT:
5086 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5087 break;
5088 case VT_DOUBLE:
5089 *(double *)ptr = vtop->c.d;
5090 break;
5091 case VT_LDOUBLE:
5092 *(long double *)ptr = vtop->c.ld;
5093 break;
5094 case VT_LLONG:
5095 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5096 break;
5097 default:
5098 if (vtop->r & VT_SYM) {
5099 greloc(sec, vtop->sym, c, R_DATA_PTR);
5101 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5102 break;
5104 vtop--;
5105 } else {
5106 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5107 vswap();
5108 vstore();
5109 vpop();
5113 /* put zeros for variable based init */
5114 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5116 if (sec) {
5117 /* nothing to do because globals are already set to zero */
5118 } else {
5119 vpush_global_sym(&func_old_type, TOK_memset);
5120 vseti(VT_LOCAL, c);
5121 vpushi(0);
5122 vpushs(size);
5123 gfunc_call(3);
5127 /* 't' contains the type and storage info. 'c' is the offset of the
5128 object in section 'sec'. If 'sec' is NULL, it means stack based
5129 allocation. 'first' is true if array '{' must be read (multi
5130 dimension implicit array init handling). 'size_only' is true if
5131 size only evaluation is wanted (only for arrays). */
5132 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5133 int first, int size_only)
5135 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5136 int size1, align1, expr_type;
5137 Sym *s, *f;
5138 CType *t1;
5140 if (type->t & VT_VLA) {
5141 int a;
5143 /* save current stack pointer */
5144 if (vla_flags & VLA_NEED_NEW_FRAME) {
5145 vla_sp_save();
5146 vla_flags = VLA_IN_SCOPE;
5147 vla_sp_loc = &vla_sp_loc_tmp;
5150 vla_runtime_type_size(type, &a);
5151 gen_vla_alloc(type, a);
5152 vset(type, VT_LOCAL|VT_LVAL, c);
5153 vswap();
5154 vstore();
5155 vpop();
5156 } else if (type->t & VT_ARRAY) {
5157 s = type->ref;
5158 n = s->c;
5159 array_length = 0;
5160 t1 = pointed_type(type);
5161 size1 = type_size(t1, &align1);
5163 no_oblock = 1;
5164 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5165 tok == '{') {
5166 if (tok != '{')
5167 tcc_error("character array initializer must be a literal,"
5168 " optionally enclosed in braces");
5169 skip('{');
5170 no_oblock = 0;
5173 /* only parse strings here if correct type (otherwise: handle
5174 them as ((w)char *) expressions */
5175 if ((tok == TOK_LSTR &&
5176 #ifdef TCC_TARGET_PE
5177 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5178 #else
5179 (t1->t & VT_BTYPE) == VT_INT
5180 #endif
5181 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5182 while (tok == TOK_STR || tok == TOK_LSTR) {
5183 int cstr_len, ch;
5184 CString *cstr;
5186 cstr = tokc.cstr;
5187 /* compute maximum number of chars wanted */
5188 if (tok == TOK_STR)
5189 cstr_len = cstr->size;
5190 else
5191 cstr_len = cstr->size / sizeof(nwchar_t);
5192 cstr_len--;
5193 nb = cstr_len;
5194 if (n >= 0 && nb > (n - array_length))
5195 nb = n - array_length;
5196 if (!size_only) {
5197 if (cstr_len > nb)
5198 tcc_warning("initializer-string for array is too long");
5199 /* in order to go faster for common case (char
5200 string in global variable, we handle it
5201 specifically */
5202 if (sec && tok == TOK_STR && size1 == 1) {
5203 memcpy(sec->data + c + array_length, cstr->data, nb);
5204 } else {
5205 for(i=0;i<nb;i++) {
5206 if (tok == TOK_STR)
5207 ch = ((unsigned char *)cstr->data)[i];
5208 else
5209 ch = ((nwchar_t *)cstr->data)[i];
5210 init_putv(t1, sec, c + (array_length + i) * size1,
5211 ch, EXPR_VAL);
5215 array_length += nb;
5216 next();
5218 /* only add trailing zero if enough storage (no
5219 warning in this case since it is standard) */
5220 if (n < 0 || array_length < n) {
5221 if (!size_only) {
5222 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5224 array_length++;
5226 } else {
5227 index = 0;
5228 while (tok != '}') {
5229 decl_designator(type, sec, c, &index, NULL, size_only);
5230 if (n >= 0 && index >= n)
5231 tcc_error("index too large");
5232 /* must put zero in holes (note that doing it that way
5233 ensures that it even works with designators) */
5234 if (!size_only && array_length < index) {
5235 init_putz(t1, sec, c + array_length * size1,
5236 (index - array_length) * size1);
5238 index++;
5239 if (index > array_length)
5240 array_length = index;
5241 /* special test for multi dimensional arrays (may not
5242 be strictly correct if designators are used at the
5243 same time) */
5244 if (index >= n && no_oblock)
5245 break;
5246 if (tok == '}')
5247 break;
5248 skip(',');
5251 if (!no_oblock)
5252 skip('}');
5253 /* put zeros at the end */
5254 if (!size_only && n >= 0 && array_length < n) {
5255 init_putz(t1, sec, c + array_length * size1,
5256 (n - array_length) * size1);
5258 /* patch type size if needed */
5259 if (n < 0)
5260 s->c = array_length;
5261 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5262 (sec || !first || tok == '{')) {
5263 int par_count;
5265 /* NOTE: the previous test is a specific case for automatic
5266 struct/union init */
5267 /* XXX: union needs only one init */
5269 /* XXX: this test is incorrect for local initializers
5270 beginning with ( without {. It would be much more difficult
5271 to do it correctly (ideally, the expression parser should
5272 be used in all cases) */
5273 par_count = 0;
5274 if (tok == '(') {
5275 AttributeDef ad1;
5276 CType type1;
5277 next();
5278 while (tok == '(') {
5279 par_count++;
5280 next();
5282 if (!parse_btype(&type1, &ad1))
5283 expect("cast");
5284 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5285 #if 0
5286 if (!is_assignable_types(type, &type1))
5287 tcc_error("invalid type for cast");
5288 #endif
5289 skip(')');
5291 no_oblock = 1;
5292 if (first || tok == '{') {
5293 skip('{');
5294 no_oblock = 0;
5296 s = type->ref;
5297 f = s->next;
5298 array_length = 0;
5299 index = 0;
5300 n = s->c;
5301 while (tok != '}') {
5302 decl_designator(type, sec, c, NULL, &f, size_only);
5303 index = f->c;
5304 if (!size_only && array_length < index) {
5305 init_putz(type, sec, c + array_length,
5306 index - array_length);
5308 index = index + type_size(&f->type, &align1);
5309 if (index > array_length)
5310 array_length = index;
5312 /* gr: skip fields from same union - ugly. */
5313 while (f->next) {
5314 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5315 /* test for same offset */
5316 if (f->next->c != f->c)
5317 break;
5318 /* if yes, test for bitfield shift */
5319 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5320 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5321 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5322 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5323 if (bit_pos_1 != bit_pos_2)
5324 break;
5326 f = f->next;
5329 f = f->next;
5330 if (no_oblock && f == NULL)
5331 break;
5332 if (tok == '}')
5333 break;
5334 skip(',');
5336 /* put zeros at the end */
5337 if (!size_only && array_length < n) {
5338 init_putz(type, sec, c + array_length,
5339 n - array_length);
5341 if (!no_oblock)
5342 skip('}');
5343 while (par_count) {
5344 skip(')');
5345 par_count--;
5347 } else if (tok == '{') {
5348 next();
5349 decl_initializer(type, sec, c, first, size_only);
5350 skip('}');
5351 } else if (size_only) {
5352 /* just skip expression */
5353 parlevel = parlevel1 = 0;
5354 while ((parlevel > 0 || parlevel1 > 0 ||
5355 (tok != '}' && tok != ',')) && tok != -1) {
5356 if (tok == '(')
5357 parlevel++;
5358 else if (tok == ')')
5359 parlevel--;
5360 else if (tok == '{')
5361 parlevel1++;
5362 else if (tok == '}')
5363 parlevel1--;
5364 next();
5366 } else {
5367 /* currently, we always use constant expression for globals
5368 (may change for scripting case) */
5369 expr_type = EXPR_CONST;
5370 if (!sec)
5371 expr_type = EXPR_ANY;
5372 init_putv(type, sec, c, 0, expr_type);
5376 /* parse an initializer for type 't' if 'has_init' is non zero, and
5377 allocate space in local or global data space ('r' is either
5378 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5379 variable 'v' with an associated name represented by 'asm_label' of
5380 scope 'scope' is declared before initializers are parsed. If 'v' is
5381 zero, then a reference to the new object is put in the value stack.
5382 If 'has_init' is 2, a special parsing is done to handle string
5383 constants. */
5384 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5385 int has_init, int v, char *asm_label,
5386 int scope)
5388 int size, align, addr, data_offset;
5389 int level;
5390 ParseState saved_parse_state = {0};
5391 TokenString init_str;
5392 Section *sec;
5393 Sym *flexible_array;
5395 flexible_array = NULL;
5396 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5397 Sym *field = type->ref->next;
5398 if (field) {
5399 while (field->next)
5400 field = field->next;
5401 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5402 flexible_array = field;
5406 size = type_size(type, &align);
5407 /* If unknown size, we must evaluate it before
5408 evaluating initializers because
5409 initializers can generate global data too
5410 (e.g. string pointers or ISOC99 compound
5411 literals). It also simplifies local
5412 initializers handling */
5413 tok_str_new(&init_str);
5414 if (size < 0 || (flexible_array && has_init)) {
5415 if (!has_init)
5416 tcc_error("unknown type size");
5417 /* get all init string */
5418 if (has_init == 2) {
5419 /* only get strings */
5420 while (tok == TOK_STR || tok == TOK_LSTR) {
5421 tok_str_add_tok(&init_str);
5422 next();
5424 } else {
5425 level = 0;
5426 while (level > 0 || (tok != ',' && tok != ';')) {
5427 if (tok < 0)
5428 tcc_error("unexpected end of file in initializer");
5429 tok_str_add_tok(&init_str);
5430 if (tok == '{')
5431 level++;
5432 else if (tok == '}') {
5433 level--;
5434 if (level <= 0) {
5435 next();
5436 break;
5439 next();
5442 tok_str_add(&init_str, -1);
5443 tok_str_add(&init_str, 0);
5445 /* compute size */
5446 save_parse_state(&saved_parse_state);
5448 macro_ptr = init_str.str;
5449 next();
5450 decl_initializer(type, NULL, 0, 1, 1);
5451 /* prepare second initializer parsing */
5452 macro_ptr = init_str.str;
5453 next();
5455 /* if still unknown size, error */
5456 size = type_size(type, &align);
5457 if (size < 0)
5458 tcc_error("unknown type size");
5460 if (flexible_array)
5461 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5462 /* take into account specified alignment if bigger */
5463 if (ad->aligned) {
5464 if (ad->aligned > align)
5465 align = ad->aligned;
5466 } else if (ad->packed) {
5467 align = 1;
5469 if ((r & VT_VALMASK) == VT_LOCAL) {
5470 sec = NULL;
5471 #ifdef CONFIG_TCC_BCHECK
5472 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5473 loc--;
5475 #endif
5476 loc = (loc - size) & -align;
5477 addr = loc;
5478 #ifdef CONFIG_TCC_BCHECK
5479 /* handles bounds */
5480 /* XXX: currently, since we do only one pass, we cannot track
5481 '&' operators, so we add only arrays */
5482 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5483 unsigned long *bounds_ptr;
5484 /* add padding between regions */
5485 loc--;
5486 /* then add local bound info */
5487 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5488 bounds_ptr[0] = addr;
5489 bounds_ptr[1] = size;
5491 #endif
5492 if (v) {
5493 /* local variable */
5494 sym_push(v, type, r, addr);
5495 } else {
5496 /* push local reference */
5497 vset(type, r, addr);
5499 } else {
5500 Sym *sym;
5502 sym = NULL;
5503 if (v && scope == VT_CONST) {
5504 /* see if the symbol was already defined */
5505 sym = sym_find(v);
5506 if (sym) {
5507 if (!is_compatible_types(&sym->type, type))
5508 tcc_error("incompatible types for redefinition of '%s'",
5509 get_tok_str(v, NULL));
5510 if (sym->type.t & VT_EXTERN) {
5511 /* if the variable is extern, it was not allocated */
5512 sym->type.t &= ~VT_EXTERN;
5513 /* set array size if it was ommited in extern
5514 declaration */
5515 if ((sym->type.t & VT_ARRAY) &&
5516 sym->type.ref->c < 0 &&
5517 type->ref->c >= 0)
5518 sym->type.ref->c = type->ref->c;
5519 } else {
5520 /* we accept several definitions of the same
5521 global variable. this is tricky, because we
5522 must play with the SHN_COMMON type of the symbol */
5523 /* XXX: should check if the variable was already
5524 initialized. It is incorrect to initialized it
5525 twice */
5526 /* no init data, we won't add more to the symbol */
5527 if (!has_init)
5528 goto no_alloc;
5533 /* allocate symbol in corresponding section */
5534 sec = ad->section;
5535 if (!sec) {
5536 if (has_init)
5537 sec = data_section;
5538 else if (tcc_state->nocommon)
5539 sec = bss_section;
5541 if (sec) {
5542 data_offset = sec->data_offset;
5543 data_offset = (data_offset + align - 1) & -align;
5544 addr = data_offset;
5545 /* very important to increment global pointer at this time
5546 because initializers themselves can create new initializers */
5547 data_offset += size;
5548 #ifdef CONFIG_TCC_BCHECK
5549 /* add padding if bound check */
5550 if (tcc_state->do_bounds_check)
5551 data_offset++;
5552 #endif
5553 sec->data_offset = data_offset;
5554 /* allocate section space to put the data */
5555 if (sec->sh_type != SHT_NOBITS &&
5556 data_offset > sec->data_allocated)
5557 section_realloc(sec, data_offset);
5558 /* align section if needed */
5559 if (align > sec->sh_addralign)
5560 sec->sh_addralign = align;
5561 } else {
5562 addr = 0; /* avoid warning */
5565 if (v) {
5566 if (scope != VT_CONST || !sym) {
5567 sym = sym_push(v, type, r | VT_SYM, 0);
5568 sym->asm_label = asm_label;
5570 /* update symbol definition */
5571 if (sec) {
5572 put_extern_sym(sym, sec, addr, size);
5573 } else {
5574 ElfW(Sym) *esym;
5575 /* put a common area */
5576 put_extern_sym(sym, NULL, align, size);
5577 /* XXX: find a nicer way */
5578 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5579 esym->st_shndx = SHN_COMMON;
5581 } else {
5582 CValue cval;
5584 /* push global reference */
5585 sym = get_sym_ref(type, sec, addr, size);
5586 cval.ul = 0;
5587 vsetc(type, VT_CONST | VT_SYM, &cval);
5588 vtop->sym = sym;
5590 /* patch symbol weakness */
5591 if (type->t & VT_WEAK)
5592 weaken_symbol(sym);
5593 #ifdef CONFIG_TCC_BCHECK
5594 /* handles bounds now because the symbol must be defined
5595 before for the relocation */
5596 if (tcc_state->do_bounds_check) {
5597 unsigned long *bounds_ptr;
5599 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5600 /* then add global bound info */
5601 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5602 bounds_ptr[0] = 0; /* relocated */
5603 bounds_ptr[1] = size;
5605 #endif
5607 if (has_init || (type->t & VT_VLA)) {
5608 decl_initializer(type, sec, addr, 1, 0);
5609 /* restore parse state if needed */
5610 if (init_str.str) {
5611 tok_str_free(init_str.str);
5612 restore_parse_state(&saved_parse_state);
5614 /* patch flexible array member size back to -1, */
5615 /* for possible subsequent similar declarations */
5616 if (flexible_array)
5617 flexible_array->type.ref->c = -1;
5619 no_alloc: ;
5622 static void put_func_debug(Sym *sym)
5624 char buf[512];
5626 /* stabs info */
5627 /* XXX: we put here a dummy type */
5628 snprintf(buf, sizeof(buf), "%s:%c1",
5629 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5630 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5631 cur_text_section, sym->c);
5632 /* //gr gdb wants a line at the function */
5633 put_stabn(N_SLINE, 0, file->line_num, 0);
5634 last_ind = 0;
5635 last_line_num = 0;
5638 /* parse an old style function declaration list */
5639 /* XXX: check multiple parameter */
5640 static void func_decl_list(Sym *func_sym)
5642 AttributeDef ad;
5643 int v;
5644 Sym *s;
5645 CType btype, type;
5647 /* parse each declaration */
5648 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5649 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5650 if (!parse_btype(&btype, &ad))
5651 expect("declaration list");
5652 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5653 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5654 tok == ';') {
5655 /* we accept no variable after */
5656 } else {
5657 for(;;) {
5658 type = btype;
5659 type_decl(&type, &ad, &v, TYPE_DIRECT);
5660 /* find parameter in function parameter list */
5661 s = func_sym->next;
5662 while (s != NULL) {
5663 if ((s->v & ~SYM_FIELD) == v)
5664 goto found;
5665 s = s->next;
5667 tcc_error("declaration for parameter '%s' but no such parameter",
5668 get_tok_str(v, NULL));
5669 found:
5670 /* check that no storage specifier except 'register' was given */
5671 if (type.t & VT_STORAGE)
5672 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5673 convert_parameter_type(&type);
5674 /* we can add the type (NOTE: it could be local to the function) */
5675 s->type = type;
5676 /* accept other parameters */
5677 if (tok == ',')
5678 next();
5679 else
5680 break;
5683 skip(';');
5687 /* parse a function defined by symbol 'sym' and generate its code in
5688 'cur_text_section' */
5689 static void gen_function(Sym *sym)
5691 int saved_nocode_wanted = nocode_wanted;
5692 nocode_wanted = 0;
5693 ind = cur_text_section->data_offset;
5694 /* NOTE: we patch the symbol size later */
5695 put_extern_sym(sym, cur_text_section, ind, 0);
5696 funcname = get_tok_str(sym->v, NULL);
5697 func_ind = ind;
5698 /* Initialize VLA state */
5699 vla_sp_loc = &vla_sp_root_loc;
5700 vla_flags = VLA_NEED_NEW_FRAME;
5701 /* put debug symbol */
5702 if (tcc_state->do_debug)
5703 put_func_debug(sym);
5704 /* push a dummy symbol to enable local sym storage */
5705 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5706 gfunc_prolog(&sym->type);
5707 rsym = 0;
5708 block(NULL, NULL, NULL, NULL, 0, 0);
5709 gsym(rsym);
5710 gfunc_epilog();
5711 cur_text_section->data_offset = ind;
5712 label_pop(&global_label_stack, NULL);
5713 /* reset local stack */
5714 scope_stack_bottom = NULL;
5715 sym_pop(&local_stack, NULL);
5716 /* end of function */
5717 /* patch symbol size */
5718 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5719 ind - func_ind;
5720 /* patch symbol weakness (this definition overrules any prototype) */
5721 if (sym->type.t & VT_WEAK)
5722 weaken_symbol(sym);
5723 if (tcc_state->do_debug) {
5724 put_stabn(N_FUN, 0, 0, ind - func_ind);
5726 /* It's better to crash than to generate wrong code */
5727 cur_text_section = NULL;
5728 funcname = ""; /* for safety */
5729 func_vt.t = VT_VOID; /* for safety */
5730 ind = 0; /* for safety */
5731 nocode_wanted = saved_nocode_wanted;
5734 ST_FUNC void gen_inline_functions(void)
5736 Sym *sym;
5737 int *str, inline_generated, i;
5738 struct InlineFunc *fn;
5740 /* iterate while inline function are referenced */
5741 for(;;) {
5742 inline_generated = 0;
5743 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5744 fn = tcc_state->inline_fns[i];
5745 sym = fn->sym;
5746 if (sym && sym->c) {
5747 /* the function was used: generate its code and
5748 convert it to a normal function */
5749 str = fn->token_str;
5750 fn->sym = NULL;
5751 if (file)
5752 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5753 sym->r = VT_SYM | VT_CONST;
5754 sym->type.t &= ~VT_INLINE;
5756 macro_ptr = str;
5757 next();
5758 cur_text_section = text_section;
5759 gen_function(sym);
5760 macro_ptr = NULL; /* fail safe */
5762 inline_generated = 1;
5765 if (!inline_generated)
5766 break;
5768 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5769 fn = tcc_state->inline_fns[i];
5770 str = fn->token_str;
5771 tok_str_free(str);
5773 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5776 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5777 static int decl0(int l, int is_for_loop_init)
5779 int v, has_init, r;
5780 CType type, btype;
5781 Sym *sym;
5782 AttributeDef ad;
5784 while (1) {
5785 if (!parse_btype(&btype, &ad)) {
5786 if (is_for_loop_init)
5787 return 0;
5788 /* skip redundant ';' */
5789 /* XXX: find more elegant solution */
5790 if (tok == ';') {
5791 next();
5792 continue;
5794 if (l == VT_CONST &&
5795 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5796 /* global asm block */
5797 asm_global_instr();
5798 continue;
5800 /* special test for old K&R protos without explicit int
5801 type. Only accepted when defining global data */
5802 if (l == VT_LOCAL || tok < TOK_DEFINE)
5803 break;
5804 btype.t = VT_INT;
5806 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5807 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5808 tok == ';') {
5809 /* we accept no variable after */
5810 next();
5811 continue;
5813 while (1) { /* iterate thru each declaration */
5814 char *asm_label; // associated asm label
5815 type = btype;
5816 type_decl(&type, &ad, &v, TYPE_DIRECT);
5817 #if 0
5819 char buf[500];
5820 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5821 printf("type = '%s'\n", buf);
5823 #endif
5824 if ((type.t & VT_BTYPE) == VT_FUNC) {
5825 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5826 tcc_error("function without file scope cannot be static");
5828 /* if old style function prototype, we accept a
5829 declaration list */
5830 sym = type.ref;
5831 if (sym->c == FUNC_OLD)
5832 func_decl_list(sym);
5835 asm_label = NULL;
5836 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5837 CString astr;
5839 asm_label_instr(&astr);
5840 asm_label = tcc_strdup(astr.data);
5841 cstr_free(&astr);
5843 /* parse one last attribute list, after asm label */
5844 parse_attribute(&ad);
5847 if (ad.weak)
5848 type.t |= VT_WEAK;
5849 #ifdef TCC_TARGET_PE
5850 if (ad.func_import)
5851 type.t |= VT_IMPORT;
5852 if (ad.func_export)
5853 type.t |= VT_EXPORT;
5854 #endif
5855 if (tok == '{') {
5856 if (l == VT_LOCAL)
5857 tcc_error("cannot use local functions");
5858 if ((type.t & VT_BTYPE) != VT_FUNC)
5859 expect("function definition");
5861 /* reject abstract declarators in function definition */
5862 sym = type.ref;
5863 while ((sym = sym->next) != NULL)
5864 if (!(sym->v & ~SYM_FIELD))
5865 expect("identifier");
5867 /* XXX: cannot do better now: convert extern line to static inline */
5868 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5869 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5871 sym = sym_find(v);
5872 if (sym) {
5873 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5874 goto func_error1;
5876 r = sym->type.ref->r;
5878 if (!FUNC_PROTO(r))
5879 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5881 /* use func_call from prototype if not defined */
5882 if (FUNC_CALL(r) != FUNC_CDECL
5883 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5884 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5886 /* use export from prototype */
5887 if (FUNC_EXPORT(r))
5888 FUNC_EXPORT(type.ref->r) = 1;
5890 /* use static from prototype */
5891 if (sym->type.t & VT_STATIC)
5892 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5894 if (!is_compatible_types(&sym->type, &type)) {
5895 func_error1:
5896 tcc_error("incompatible types for redefinition of '%s'",
5897 get_tok_str(v, NULL));
5899 FUNC_PROTO(type.ref->r) = 0;
5900 /* if symbol is already defined, then put complete type */
5901 sym->type = type;
5902 } else {
5903 /* put function symbol */
5904 sym = global_identifier_push(v, type.t, 0);
5905 sym->type.ref = type.ref;
5908 /* static inline functions are just recorded as a kind
5909 of macro. Their code will be emitted at the end of
5910 the compilation unit only if they are used */
5911 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5912 (VT_INLINE | VT_STATIC)) {
5913 TokenString func_str;
5914 int block_level;
5915 struct InlineFunc *fn;
5916 const char *filename;
5918 tok_str_new(&func_str);
5920 block_level = 0;
5921 for(;;) {
5922 int t;
5923 if (tok == TOK_EOF)
5924 tcc_error("unexpected end of file");
5925 tok_str_add_tok(&func_str);
5926 t = tok;
5927 next();
5928 if (t == '{') {
5929 block_level++;
5930 } else if (t == '}') {
5931 block_level--;
5932 if (block_level == 0)
5933 break;
5936 tok_str_add(&func_str, -1);
5937 tok_str_add(&func_str, 0);
5938 filename = file ? file->filename : "";
5939 fn = tcc_malloc(sizeof *fn + strlen(filename));
5940 strcpy(fn->filename, filename);
5941 fn->sym = sym;
5942 fn->token_str = func_str.str;
5943 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5945 } else {
5946 /* compute text section */
5947 cur_text_section = ad.section;
5948 if (!cur_text_section)
5949 cur_text_section = text_section;
5950 sym->r = VT_SYM | VT_CONST;
5951 gen_function(sym);
5953 break;
5954 } else {
5955 if (btype.t & VT_TYPEDEF) {
5956 /* save typedefed type */
5957 /* XXX: test storage specifiers ? */
5958 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5959 sym->type.t |= VT_TYPEDEF;
5960 } else {
5961 r = 0;
5962 if ((type.t & VT_BTYPE) == VT_FUNC) {
5963 /* external function definition */
5964 /* specific case for func_call attribute */
5965 ad.func_proto = 1;
5966 type.ref->r = INT_ATTR(&ad);
5967 } else if (!(type.t & VT_ARRAY)) {
5968 /* not lvalue if array */
5969 r |= lvalue_type(type.t);
5971 has_init = (tok == '=');
5972 if (has_init && (type.t & VT_VLA))
5973 tcc_error("Variable length array cannot be initialized");
5974 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5975 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5976 !has_init && l == VT_CONST && type.ref->c < 0)) {
5977 /* external variable or function */
5978 /* NOTE: as GCC, uninitialized global static
5979 arrays of null size are considered as
5980 extern */
5981 sym = external_sym(v, &type, r, asm_label);
5983 if (type.t & VT_WEAK)
5984 weaken_symbol(sym);
5986 if (ad.alias_target) {
5987 Section tsec;
5988 Elf32_Sym *esym;
5989 Sym *alias_target;
5991 alias_target = sym_find(ad.alias_target);
5992 if (!alias_target || !alias_target->c)
5993 tcc_error("unsupported forward __alias__ attribute");
5994 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5995 tsec.sh_num = esym->st_shndx;
5996 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5998 } else {
5999 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6000 if (type.t & VT_STATIC)
6001 r |= VT_CONST;
6002 else
6003 r |= l;
6004 if (has_init)
6005 next();
6006 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6009 if (tok != ',') {
6010 if (is_for_loop_init)
6011 return 1;
6012 skip(';');
6013 break;
6015 next();
6017 ad.aligned = 0;
6020 return 0;
6023 ST_FUNC void decl(int l)
6025 decl0(l, 0);