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