win32: warn people about using undeclared WINAPI functions
[tinycc.git] / tccgen.c
blobe6e0fe11a9a4e733ed0c52e29ab10ffbf9084265
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
31 ST_DATA int rsym, anon_sym, ind, loc;
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *scope_stack_bottom;
54 ST_DATA Sym *define_stack;
55 ST_DATA Sym *global_label_stack;
56 ST_DATA Sym *local_label_stack;
58 ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA int vla_flags; /* VLA_* flags */
63 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
65 ST_DATA int const_wanted; /* true if constant wanted */
66 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
67 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
69 ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
70 ST_DATA int func_vc;
71 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
72 ST_DATA char *funcname;
74 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType *type);
78 static inline CType *pointed_type(CType *type);
79 static int is_compatible_types(CType *type1, CType *type2);
80 static int parse_btype(CType *type, AttributeDef *ad);
81 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
82 static void parse_expr_type(CType *type);
83 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
84 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
85 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
86 static int decl0(int l, int is_for_loop_init);
87 static void expr_eq(void);
88 static void unary_type(CType *type);
89 static void vla_runtime_type_size(CType *type, int *a);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType *type1, CType *type2);
92 static void expr_type(CType *type);
94 ST_INLN int is_float(int t)
96 int bt;
97 bt = t & VT_BTYPE;
98 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
101 /* we use our own 'finite' function to avoid potential problems with
102 non standard math libs */
103 /* XXX: endianness dependent */
104 ST_FUNC int ieee_finite(double d)
106 int p[4];
107 memcpy(p, &d, sizeof(double));
108 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
111 ST_FUNC void test_lvalue(void)
113 if (!(vtop->r & VT_LVAL))
114 expect("lvalue");
117 /* ------------------------------------------------------------------------- */
118 /* symbol allocator */
119 static Sym *__sym_malloc(void)
121 Sym *sym_pool, *sym, *last_sym;
122 int i;
124 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
125 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
127 last_sym = sym_free_first;
128 sym = sym_pool;
129 for(i = 0; i < SYM_POOL_NB; i++) {
130 sym->next = last_sym;
131 last_sym = sym;
132 sym++;
134 sym_free_first = last_sym;
135 return last_sym;
138 static inline Sym *sym_malloc(void)
140 Sym *sym;
141 sym = sym_free_first;
142 if (!sym)
143 sym = __sym_malloc();
144 sym_free_first = sym->next;
145 return sym;
148 ST_INLN void sym_free(Sym *sym)
150 sym->next = sym_free_first;
151 tcc_free(sym->asm_label);
152 sym_free_first = sym;
155 /* push, without hashing */
156 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
158 Sym *s;
159 if (ps == &local_stack) {
160 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
161 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
162 tcc_error("incompatible types for redefinition of '%s'",
163 get_tok_str(v, NULL));
165 s = sym_malloc();
166 s->asm_label = NULL;
167 s->v = v;
168 s->type.t = t;
169 s->type.ref = NULL;
170 #ifdef _WIN64
171 s->d = NULL;
172 #endif
173 s->c = c;
174 s->next = NULL;
175 /* add in stack */
176 s->prev = *ps;
177 *ps = s;
178 return s;
181 /* find a symbol and return its associated structure. 's' is the top
182 of the symbol stack */
183 ST_FUNC Sym *sym_find2(Sym *s, int v)
185 while (s) {
186 if (s->v == v)
187 return s;
188 s = s->prev;
190 return NULL;
193 /* structure lookup */
194 ST_INLN Sym *struct_find(int v)
196 v -= TOK_IDENT;
197 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
198 return NULL;
199 return table_ident[v]->sym_struct;
202 /* find an identifier */
203 ST_INLN Sym *sym_find(int v)
205 v -= TOK_IDENT;
206 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
207 return NULL;
208 return table_ident[v]->sym_identifier;
211 /* push a given symbol on the symbol stack */
212 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
214 Sym *s, **ps;
215 TokenSym *ts;
217 if (local_stack)
218 ps = &local_stack;
219 else
220 ps = &global_stack;
221 s = sym_push2(ps, v, type->t, c);
222 s->type.ref = type->ref;
223 s->r = r;
224 /* don't record fields or anonymous symbols */
225 /* XXX: simplify */
226 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
227 /* record symbol in token array */
228 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
229 if (v & SYM_STRUCT)
230 ps = &ts->sym_struct;
231 else
232 ps = &ts->sym_identifier;
233 s->prev_tok = *ps;
234 *ps = s;
236 return s;
239 /* push a global identifier */
240 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
242 Sym *s, **ps;
243 s = sym_push2(&global_stack, v, t, c);
244 /* don't record anonymous symbol */
245 if (v < SYM_FIRST_ANOM) {
246 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
247 /* modify the top most local identifier, so that
248 sym_identifier will point to 's' when popped */
249 while (*ps != NULL)
250 ps = &(*ps)->prev_tok;
251 s->prev_tok = NULL;
252 *ps = s;
254 return s;
257 /* pop symbols until top reaches 'b' */
258 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
260 Sym *s, *ss, **ps;
261 TokenSym *ts;
262 int v;
264 s = *ptop;
265 while(s != b) {
266 ss = s->prev;
267 v = s->v;
268 /* remove symbol in token array */
269 /* XXX: simplify */
270 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
271 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
272 if (v & SYM_STRUCT)
273 ps = &ts->sym_struct;
274 else
275 ps = &ts->sym_identifier;
276 *ps = s->prev_tok;
278 sym_free(s);
279 s = ss;
281 *ptop = b;
284 static void weaken_symbol(Sym *sym)
286 sym->type.t |= VT_WEAK;
287 if (sym->c > 0) {
288 int esym_type;
289 ElfW(Sym) *esym;
291 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
292 esym_type = ELFW(ST_TYPE)(esym->st_info);
293 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
297 /* ------------------------------------------------------------------------- */
299 ST_FUNC void swap(int *p, int *q)
301 int t;
302 t = *p;
303 *p = *q;
304 *q = t;
307 static void vsetc(CType *type, int r, CValue *vc)
309 int v;
311 if (vtop >= vstack + (VSTACK_SIZE - 1))
312 tcc_error("memory full (vstack)");
313 /* cannot let cpu flags if other instruction are generated. Also
314 avoid leaving VT_JMP anywhere except on the top of the stack
315 because it would complicate the code generator. */
316 if (vtop >= vstack) {
317 v = vtop->r & VT_VALMASK;
318 if (v == VT_CMP || (v & ~1) == VT_JMP)
319 gv(RC_INT);
321 vtop++;
322 vtop->type = *type;
323 vtop->r = r;
324 vtop->r2 = VT_CONST;
325 vtop->c = *vc;
328 /* push constant of type "type" with useless value */
329 void vpush(CType *type)
331 CValue cval;
332 vsetc(type, VT_CONST, &cval);
335 /* push integer constant */
336 ST_FUNC void vpushi(int v)
338 CValue cval;
339 cval.i = v;
340 vsetc(&int_type, VT_CONST, &cval);
343 /* push a pointer sized constant */
344 static void vpushs(addr_t v)
346 CValue cval;
347 cval.ptr_offset = v;
348 vsetc(&size_type, VT_CONST, &cval);
351 /* push arbitrary 64bit constant */
352 void vpush64(int ty, unsigned long long v)
354 CValue cval;
355 CType ctype;
356 ctype.t = ty;
357 ctype.ref = NULL;
358 cval.ull = v;
359 vsetc(&ctype, VT_CONST, &cval);
362 /* push long long constant */
363 static inline void vpushll(long long v)
365 vpush64(VT_LLONG, v);
368 /* push a symbol value of TYPE */
369 static inline void vpushsym(CType *type, Sym *sym)
371 CValue cval;
372 cval.ptr_offset = 0;
373 vsetc(type, VT_CONST | VT_SYM, &cval);
374 vtop->sym = sym;
377 /* Return a static symbol pointing to a section */
378 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
380 int v;
381 Sym *sym;
383 v = anon_sym++;
384 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
385 sym->type.ref = type->ref;
386 sym->r = VT_CONST | VT_SYM;
387 put_extern_sym(sym, sec, offset, size);
388 return sym;
391 /* push a reference to a section offset by adding a dummy symbol */
392 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
394 vpushsym(type, get_sym_ref(type, sec, offset, size));
397 /* define a new external reference to a symbol 'v' of type 'u' */
398 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
400 Sym *s;
402 s = sym_find(v);
403 if (!s) {
404 /* push forward reference */
405 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
406 s->type.ref = type->ref;
407 s->r = r | VT_CONST | VT_SYM;
409 return s;
412 /* define a new external reference to a symbol 'v' with alternate asm
413 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
414 is no alternate name (most cases) */
415 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
417 Sym *s;
419 s = sym_find(v);
420 if (!s) {
421 /* push forward reference */
422 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
423 s->asm_label = asm_label;
424 s->type.t |= VT_EXTERN;
425 } else if (s->type.ref == func_old_type.ref) {
426 s->type.ref = type->ref;
427 s->r = r | VT_CONST | VT_SYM;
428 s->type.t |= VT_EXTERN;
429 } else if (!is_compatible_types(&s->type, type)) {
430 tcc_error("incompatible types for redefinition of '%s'",
431 get_tok_str(v, NULL));
433 return s;
436 /* push a reference to global symbol v */
437 ST_FUNC void vpush_global_sym(CType *type, int v)
439 vpushsym(type, external_global_sym(v, type, 0));
442 ST_FUNC void vset(CType *type, int r, int v)
444 CValue cval;
446 cval.i = v;
447 vsetc(type, r, &cval);
450 static void vseti(int r, int v)
452 CType type;
453 type.t = VT_INT;
454 type.ref = 0;
455 vset(&type, r, v);
458 ST_FUNC void vswap(void)
460 SValue tmp;
461 /* cannot let cpu flags if other instruction are generated. Also
462 avoid leaving VT_JMP anywhere except on the top of the stack
463 because it would complicate the code generator. */
464 if (vtop >= vstack) {
465 int v = vtop->r & VT_VALMASK;
466 if (v == VT_CMP || (v & ~1) == VT_JMP)
467 gv(RC_INT);
469 tmp = vtop[0];
470 vtop[0] = vtop[-1];
471 vtop[-1] = tmp;
473 /* XXX: +2% overall speed possible with optimized memswap
475 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
479 ST_FUNC void vpushv(SValue *v)
481 if (vtop >= vstack + (VSTACK_SIZE - 1))
482 tcc_error("memory full (vstack)");
483 vtop++;
484 *vtop = *v;
487 static void vdup(void)
489 vpushv(vtop);
492 /* save r to the memory stack, and mark it as being free */
493 ST_FUNC void save_reg(int r)
495 int l, saved, size, align;
496 SValue *p, sv;
497 CType *type;
499 /* modify all stack values */
500 saved = 0;
501 l = 0;
502 for(p=vstack;p<=vtop;p++) {
503 if ((p->r & VT_VALMASK) == r ||
504 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
505 /* must save value on stack if not already done */
506 if (!saved) {
507 /* NOTE: must reload 'r' because r might be equal to r2 */
508 r = p->r & VT_VALMASK;
509 /* store register in the stack */
510 type = &p->type;
511 if ((p->r & VT_LVAL) ||
512 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
513 #ifdef TCC_TARGET_X86_64
514 type = &char_pointer_type;
515 #else
516 type = &int_type;
517 #endif
518 size = type_size(type, &align);
519 loc = (loc - size) & -align;
520 sv.type.t = type->t;
521 sv.r = VT_LOCAL | VT_LVAL;
522 sv.c.ul = loc;
523 store(r, &sv);
524 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
525 /* x86 specific: need to pop fp register ST0 if saved */
526 if (r == TREG_ST0) {
527 o(0xd8dd); /* fstp %st(0) */
529 #endif
530 #ifndef TCC_TARGET_X86_64
531 /* special long long case */
532 if ((type->t & VT_BTYPE) == VT_LLONG) {
533 sv.c.ul += 4;
534 store(p->r2, &sv);
536 #endif
537 l = loc;
538 saved = 1;
540 /* mark that stack entry as being saved on the stack */
541 if (p->r & VT_LVAL) {
542 /* also clear the bounded flag because the
543 relocation address of the function was stored in
544 p->c.ul */
545 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
546 } else {
547 p->r = lvalue_type(p->type.t) | VT_LOCAL;
549 p->r2 = VT_CONST;
550 p->c.ul = l;
555 #ifdef TCC_TARGET_ARM
556 /* find a register of class 'rc2' with at most one reference on stack.
557 * If none, call get_reg(rc) */
558 ST_FUNC int get_reg_ex(int rc, int rc2)
560 int r;
561 SValue *p;
563 for(r=0;r<NB_REGS;r++) {
564 if (reg_classes[r] & rc2) {
565 int n;
566 n=0;
567 for(p = vstack; p <= vtop; p++) {
568 if ((p->r & VT_VALMASK) == r ||
569 (p->r2 & VT_VALMASK) == r)
570 n++;
572 if (n <= 1)
573 return r;
576 return get_reg(rc);
578 #endif
580 /* find a free register of class 'rc'. If none, save one register */
581 ST_FUNC int get_reg(int rc)
583 int r;
584 SValue *p;
586 /* find a free register */
587 for(r=0;r<NB_REGS;r++) {
588 if (reg_classes[r] & rc) {
589 for(p=vstack;p<=vtop;p++) {
590 if ((p->r & VT_VALMASK) == r ||
591 (p->r2 & VT_VALMASK) == r)
592 goto notfound;
594 return r;
596 notfound: ;
599 /* no register left : free the first one on the stack (VERY
600 IMPORTANT to start from the bottom to ensure that we don't
601 spill registers used in gen_opi()) */
602 for(p=vstack;p<=vtop;p++) {
603 /* look at second register (if long long) */
604 r = p->r2 & VT_VALMASK;
605 if (r < VT_CONST && (reg_classes[r] & rc))
606 goto save_found;
607 r = p->r & VT_VALMASK;
608 if (r < VT_CONST && (reg_classes[r] & rc)) {
609 save_found:
610 save_reg(r);
611 return r;
614 /* Should never comes here */
615 return -1;
618 /* save registers up to (vtop - n) stack entry */
619 ST_FUNC void save_regs(int n)
621 int r;
622 SValue *p, *p1;
623 p1 = vtop - n;
624 for(p = vstack;p <= p1; p++) {
625 r = p->r & VT_VALMASK;
626 if (r < VT_CONST) {
627 save_reg(r);
632 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
633 if needed */
634 static void move_reg(int r, int s, int t)
636 SValue sv;
638 if (r != s) {
639 save_reg(r);
640 sv.type.t = t;
641 sv.type.ref = NULL;
642 sv.r = s;
643 sv.c.ul = 0;
644 load(r, &sv);
648 /* get address of vtop (vtop MUST BE an lvalue) */
649 static void gaddrof(void)
651 if (vtop->r & VT_REF)
652 gv(RC_INT);
653 vtop->r &= ~VT_LVAL;
654 /* tricky: if saved lvalue, then we can go back to lvalue */
655 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
656 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
661 #ifdef CONFIG_TCC_BCHECK
662 /* generate lvalue bound code */
663 static void gbound(void)
665 int lval_type;
666 CType type1;
668 vtop->r &= ~VT_MUSTBOUND;
669 /* if lvalue, then use checking code before dereferencing */
670 if (vtop->r & VT_LVAL) {
671 /* if not VT_BOUNDED value, then make one */
672 if (!(vtop->r & VT_BOUNDED)) {
673 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
674 /* must save type because we must set it to int to get pointer */
675 type1 = vtop->type;
676 vtop->type.t = VT_INT;
677 gaddrof();
678 vpushi(0);
679 gen_bounded_ptr_add();
680 vtop->r |= lval_type;
681 vtop->type = type1;
683 /* then check for dereferencing */
684 gen_bounded_ptr_deref();
687 #endif
689 /* store vtop a register belonging to class 'rc'. lvalues are
690 converted to values. Cannot be used if cannot be converted to
691 register value (such as structures). */
692 ST_FUNC int gv(int rc)
694 int r, bit_pos, bit_size, size, align, i;
695 int rc2;
697 /* NOTE: get_reg can modify vstack[] */
698 if (vtop->type.t & VT_BITFIELD) {
699 CType type;
700 int bits = 32;
701 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
702 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
703 /* remove bit field info to avoid loops */
704 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
705 /* cast to int to propagate signedness in following ops */
706 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
707 type.t = VT_LLONG;
708 bits = 64;
709 } else
710 type.t = VT_INT;
711 if((vtop->type.t & VT_UNSIGNED) ||
712 (vtop->type.t & VT_BTYPE) == VT_BOOL)
713 type.t |= VT_UNSIGNED;
714 gen_cast(&type);
715 /* generate shifts */
716 vpushi(bits - (bit_pos + bit_size));
717 gen_op(TOK_SHL);
718 vpushi(bits - bit_size);
719 /* NOTE: transformed to SHR if unsigned */
720 gen_op(TOK_SAR);
721 r = gv(rc);
722 } else {
723 if (is_float(vtop->type.t) &&
724 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
725 Sym *sym;
726 int *ptr;
727 unsigned long offset;
728 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
729 CValue check;
730 #endif
732 /* XXX: unify with initializers handling ? */
733 /* CPUs usually cannot use float constants, so we store them
734 generically in data segment */
735 size = type_size(&vtop->type, &align);
736 offset = (data_section->data_offset + align - 1) & -align;
737 data_section->data_offset = offset;
738 /* XXX: not portable yet */
739 #if defined(__i386__) || defined(__x86_64__)
740 /* Zero pad x87 tenbyte long doubles */
741 if (size == LDOUBLE_SIZE) {
742 vtop->c.tab[2] &= 0xffff;
743 #if LDOUBLE_SIZE == 16
744 vtop->c.tab[3] = 0;
745 #endif
747 #endif
748 ptr = section_ptr_add(data_section, size);
749 size = size >> 2;
750 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
751 check.d = 1;
752 if(check.tab[0])
753 for(i=0;i<size;i++)
754 ptr[i] = vtop->c.tab[size-1-i];
755 else
756 #endif
757 for(i=0;i<size;i++)
758 ptr[i] = vtop->c.tab[i];
759 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
760 vtop->r |= VT_LVAL | VT_SYM;
761 vtop->sym = sym;
762 vtop->c.ptr_offset = 0;
764 #ifdef CONFIG_TCC_BCHECK
765 if (vtop->r & VT_MUSTBOUND)
766 gbound();
767 #endif
769 r = vtop->r & VT_VALMASK;
770 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
771 if (rc == RC_IRET)
772 rc2 = RC_LRET;
773 #ifdef TCC_TARGET_X86_64
774 else if (rc == RC_FRET)
775 rc2 = RC_QRET;
776 #endif
778 /* need to reload if:
779 - constant
780 - lvalue (need to dereference pointer)
781 - already a register, but not in the right class */
782 if (r >= VT_CONST
783 || (vtop->r & VT_LVAL)
784 || !(reg_classes[r] & rc)
785 #ifdef TCC_TARGET_X86_64
786 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
787 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
788 #else
789 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
790 #endif
793 r = get_reg(rc);
794 #ifdef TCC_TARGET_X86_64
795 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
796 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
797 #else
798 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
799 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
800 unsigned long long ll;
801 #endif
802 int r2, original_type;
803 original_type = vtop->type.t;
804 /* two register type load : expand to two words
805 temporarily */
806 #ifndef TCC_TARGET_X86_64
807 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
808 /* load constant */
809 ll = vtop->c.ull;
810 vtop->c.ui = ll; /* first word */
811 load(r, vtop);
812 vtop->r = r; /* save register value */
813 vpushi(ll >> 32); /* second word */
814 } else
815 #endif
816 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
817 (vtop->r & VT_LVAL)) {
818 /* We do not want to modifier the long long
819 pointer here, so the safest (and less
820 efficient) is to save all the other registers
821 in the stack. XXX: totally inefficient. */
822 save_regs(1);
823 /* load from memory */
824 vtop->type.t = load_type;
825 load(r, vtop);
826 vdup();
827 vtop[-1].r = r; /* save register value */
828 /* increment pointer to get second word */
829 vtop->type.t = addr_type;
830 gaddrof();
831 vpushi(load_size);
832 gen_op('+');
833 vtop->r |= VT_LVAL;
834 vtop->type.t = load_type;
835 } else {
836 /* move registers */
837 load(r, vtop);
838 vdup();
839 vtop[-1].r = r; /* save register value */
840 vtop->r = vtop[-1].r2;
842 /* Allocate second register. Here we rely on the fact that
843 get_reg() tries first to free r2 of an SValue. */
844 r2 = get_reg(rc2);
845 load(r2, vtop);
846 vpop();
847 /* write second register */
848 vtop->r2 = r2;
849 vtop->type.t = original_type;
850 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
851 int t1, t;
852 /* lvalue of scalar type : need to use lvalue type
853 because of possible cast */
854 t = vtop->type.t;
855 t1 = t;
856 /* compute memory access type */
857 if (vtop->r & VT_REF)
858 #ifdef TCC_TARGET_X86_64
859 t = VT_PTR;
860 #else
861 t = VT_INT;
862 #endif
863 else if (vtop->r & VT_LVAL_BYTE)
864 t = VT_BYTE;
865 else if (vtop->r & VT_LVAL_SHORT)
866 t = VT_SHORT;
867 if (vtop->r & VT_LVAL_UNSIGNED)
868 t |= VT_UNSIGNED;
869 vtop->type.t = t;
870 load(r, vtop);
871 /* restore wanted type */
872 vtop->type.t = t1;
873 } else {
874 /* one register type load */
875 load(r, vtop);
878 vtop->r = r;
879 #ifdef TCC_TARGET_C67
880 /* uses register pairs for doubles */
881 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
882 vtop->r2 = r+1;
883 #endif
885 return r;
888 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
889 ST_FUNC void gv2(int rc1, int rc2)
891 int v;
893 /* generate more generic register first. But VT_JMP or VT_CMP
894 values must be generated first in all cases to avoid possible
895 reload errors */
896 v = vtop[0].r & VT_VALMASK;
897 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
898 vswap();
899 gv(rc1);
900 vswap();
901 gv(rc2);
902 /* test if reload is needed for first register */
903 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
904 vswap();
905 gv(rc1);
906 vswap();
908 } else {
909 gv(rc2);
910 vswap();
911 gv(rc1);
912 vswap();
913 /* test if reload is needed for first register */
914 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
915 gv(rc2);
920 /* wrapper around RC_FRET to return a register by type */
921 static int rc_fret(int t)
923 #ifdef TCC_TARGET_X86_64
924 if (t == VT_LDOUBLE) {
925 return RC_ST0;
927 #endif
928 return RC_FRET;
931 /* wrapper around REG_FRET to return a register by type */
932 static int reg_fret(int t)
934 #ifdef TCC_TARGET_X86_64
935 if (t == VT_LDOUBLE) {
936 return TREG_ST0;
938 #endif
939 return REG_FRET;
942 /* expand long long on stack in two int registers */
943 static void lexpand(void)
945 int u;
947 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
948 gv(RC_INT);
949 vdup();
950 vtop[0].r = vtop[-1].r2;
951 vtop[0].r2 = VT_CONST;
952 vtop[-1].r2 = VT_CONST;
953 vtop[0].type.t = VT_INT | u;
954 vtop[-1].type.t = VT_INT | u;
957 #ifdef TCC_TARGET_ARM
958 /* expand long long on stack */
959 ST_FUNC void lexpand_nr(void)
961 int u,v;
963 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
964 vdup();
965 vtop->r2 = VT_CONST;
966 vtop->type.t = VT_INT | u;
967 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
968 if (v == VT_CONST) {
969 vtop[-1].c.ui = vtop->c.ull;
970 vtop->c.ui = vtop->c.ull >> 32;
971 vtop->r = VT_CONST;
972 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
973 vtop->c.ui += 4;
974 vtop->r = vtop[-1].r;
975 } else if (v > VT_CONST) {
976 vtop--;
977 lexpand();
978 } else
979 vtop->r = vtop[-1].r2;
980 vtop[-1].r2 = VT_CONST;
981 vtop[-1].type.t = VT_INT | u;
983 #endif
985 /* build a long long from two ints */
986 static void lbuild(int t)
988 gv2(RC_INT, RC_INT);
989 vtop[-1].r2 = vtop[0].r;
990 vtop[-1].type.t = t;
991 vpop();
994 /* rotate n first stack elements to the bottom
995 I1 ... In -> I2 ... In I1 [top is right]
997 ST_FUNC void vrotb(int n)
999 int i;
1000 SValue tmp;
1002 tmp = vtop[-n + 1];
1003 for(i=-n+1;i!=0;i++)
1004 vtop[i] = vtop[i+1];
1005 vtop[0] = tmp;
1008 /* rotate the n elements before entry e towards the top
1009 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1011 ST_FUNC void vrote(SValue *e, int n)
1013 int i;
1014 SValue tmp;
1016 tmp = *e;
1017 for(i = 0;i < n - 1; i++)
1018 e[-i] = e[-i - 1];
1019 e[-n + 1] = tmp;
1022 /* rotate n first stack elements to the top
1023 I1 ... In -> In I1 ... I(n-1) [top is right]
1025 ST_FUNC void vrott(int n)
1027 vrote(vtop, n);
1030 /* pop stack value */
1031 ST_FUNC void vpop(void)
1033 int v;
1034 v = vtop->r & VT_VALMASK;
1035 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1036 /* for x86, we need to pop the FP stack */
1037 if (v == TREG_ST0 && !nocode_wanted) {
1038 o(0xd8dd); /* fstp %st(0) */
1039 } else
1040 #endif
1041 if (v == VT_JMP || v == VT_JMPI) {
1042 /* need to put correct jump if && or || without test */
1043 gsym(vtop->c.ul);
1045 vtop--;
1048 /* convert stack entry to register and duplicate its value in another
1049 register */
1050 static void gv_dup(void)
1052 int rc, t, r, r1;
1053 SValue sv;
1055 t = vtop->type.t;
1056 if ((t & VT_BTYPE) == VT_LLONG) {
1057 lexpand();
1058 gv_dup();
1059 vswap();
1060 vrotb(3);
1061 gv_dup();
1062 vrotb(4);
1063 /* stack: H L L1 H1 */
1064 lbuild(t);
1065 vrotb(3);
1066 vrotb(3);
1067 vswap();
1068 lbuild(t);
1069 vswap();
1070 } else {
1071 /* duplicate value */
1072 rc = RC_INT;
1073 sv.type.t = VT_INT;
1074 if (is_float(t)) {
1075 rc = RC_FLOAT;
1076 #ifdef TCC_TARGET_X86_64
1077 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1078 rc = RC_ST0;
1080 #endif
1081 sv.type.t = t;
1083 r = gv(rc);
1084 r1 = get_reg(rc);
1085 sv.r = r;
1086 sv.c.ul = 0;
1087 load(r1, &sv); /* move r to r1 */
1088 vdup();
1089 /* duplicates value */
1090 if (r != r1)
1091 vtop->r = r1;
1095 /* Generate value test
1097 * Generate a test for any value (jump, comparison and integers) */
1098 int gvtst(int inv, int t)
1100 int v = vtop->r & VT_VALMASK;
1101 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1102 vpushi(0);
1103 gen_op(TOK_NE);
1105 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1106 /* constant jmp optimization */
1107 if ((vtop->c.i != 0) != inv)
1108 t = gjmp(t);
1109 vtop--;
1110 return t;
1112 return gtst(inv, t);
1115 #ifndef TCC_TARGET_X86_64
1116 /* generate CPU independent (unsigned) long long operations */
1117 static void gen_opl(int op)
1119 int t, a, b, op1, c, i;
1120 int func;
1121 unsigned short reg_iret = REG_IRET;
1122 unsigned short reg_lret = REG_LRET;
1123 SValue tmp;
1125 switch(op) {
1126 case '/':
1127 case TOK_PDIV:
1128 func = TOK___divdi3;
1129 goto gen_func;
1130 case TOK_UDIV:
1131 func = TOK___udivdi3;
1132 goto gen_func;
1133 case '%':
1134 func = TOK___moddi3;
1135 goto gen_mod_func;
1136 case TOK_UMOD:
1137 func = TOK___umoddi3;
1138 gen_mod_func:
1139 #ifdef TCC_ARM_EABI
1140 reg_iret = TREG_R2;
1141 reg_lret = TREG_R3;
1142 #endif
1143 gen_func:
1144 /* call generic long long function */
1145 vpush_global_sym(&func_old_type, func);
1146 vrott(3);
1147 gfunc_call(2);
1148 vpushi(0);
1149 vtop->r = reg_iret;
1150 vtop->r2 = reg_lret;
1151 break;
1152 case '^':
1153 case '&':
1154 case '|':
1155 case '*':
1156 case '+':
1157 case '-':
1158 t = vtop->type.t;
1159 vswap();
1160 lexpand();
1161 vrotb(3);
1162 lexpand();
1163 /* stack: L1 H1 L2 H2 */
1164 tmp = vtop[0];
1165 vtop[0] = vtop[-3];
1166 vtop[-3] = tmp;
1167 tmp = vtop[-2];
1168 vtop[-2] = vtop[-3];
1169 vtop[-3] = tmp;
1170 vswap();
1171 /* stack: H1 H2 L1 L2 */
1172 if (op == '*') {
1173 vpushv(vtop - 1);
1174 vpushv(vtop - 1);
1175 gen_op(TOK_UMULL);
1176 lexpand();
1177 /* stack: H1 H2 L1 L2 ML MH */
1178 for(i=0;i<4;i++)
1179 vrotb(6);
1180 /* stack: ML MH H1 H2 L1 L2 */
1181 tmp = vtop[0];
1182 vtop[0] = vtop[-2];
1183 vtop[-2] = tmp;
1184 /* stack: ML MH H1 L2 H2 L1 */
1185 gen_op('*');
1186 vrotb(3);
1187 vrotb(3);
1188 gen_op('*');
1189 /* stack: ML MH M1 M2 */
1190 gen_op('+');
1191 gen_op('+');
1192 } else if (op == '+' || op == '-') {
1193 /* XXX: add non carry method too (for MIPS or alpha) */
1194 if (op == '+')
1195 op1 = TOK_ADDC1;
1196 else
1197 op1 = TOK_SUBC1;
1198 gen_op(op1);
1199 /* stack: H1 H2 (L1 op L2) */
1200 vrotb(3);
1201 vrotb(3);
1202 gen_op(op1 + 1); /* TOK_xxxC2 */
1203 } else {
1204 gen_op(op);
1205 /* stack: H1 H2 (L1 op L2) */
1206 vrotb(3);
1207 vrotb(3);
1208 /* stack: (L1 op L2) H1 H2 */
1209 gen_op(op);
1210 /* stack: (L1 op L2) (H1 op H2) */
1212 /* stack: L H */
1213 lbuild(t);
1214 break;
1215 case TOK_SAR:
1216 case TOK_SHR:
1217 case TOK_SHL:
1218 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1219 t = vtop[-1].type.t;
1220 vswap();
1221 lexpand();
1222 vrotb(3);
1223 /* stack: L H shift */
1224 c = (int)vtop->c.i;
1225 /* constant: simpler */
1226 /* NOTE: all comments are for SHL. the other cases are
1227 done by swaping words */
1228 vpop();
1229 if (op != TOK_SHL)
1230 vswap();
1231 if (c >= 32) {
1232 /* stack: L H */
1233 vpop();
1234 if (c > 32) {
1235 vpushi(c - 32);
1236 gen_op(op);
1238 if (op != TOK_SAR) {
1239 vpushi(0);
1240 } else {
1241 gv_dup();
1242 vpushi(31);
1243 gen_op(TOK_SAR);
1245 vswap();
1246 } else {
1247 vswap();
1248 gv_dup();
1249 /* stack: H L L */
1250 vpushi(c);
1251 gen_op(op);
1252 vswap();
1253 vpushi(32 - c);
1254 if (op == TOK_SHL)
1255 gen_op(TOK_SHR);
1256 else
1257 gen_op(TOK_SHL);
1258 vrotb(3);
1259 /* stack: L L H */
1260 vpushi(c);
1261 if (op == TOK_SHL)
1262 gen_op(TOK_SHL);
1263 else
1264 gen_op(TOK_SHR);
1265 gen_op('|');
1267 if (op != TOK_SHL)
1268 vswap();
1269 lbuild(t);
1270 } else {
1271 /* XXX: should provide a faster fallback on x86 ? */
1272 switch(op) {
1273 case TOK_SAR:
1274 func = TOK___ashrdi3;
1275 goto gen_func;
1276 case TOK_SHR:
1277 func = TOK___lshrdi3;
1278 goto gen_func;
1279 case TOK_SHL:
1280 func = TOK___ashldi3;
1281 goto gen_func;
1284 break;
1285 default:
1286 /* compare operations */
1287 t = vtop->type.t;
1288 vswap();
1289 lexpand();
1290 vrotb(3);
1291 lexpand();
1292 /* stack: L1 H1 L2 H2 */
1293 tmp = vtop[-1];
1294 vtop[-1] = vtop[-2];
1295 vtop[-2] = tmp;
1296 /* stack: L1 L2 H1 H2 */
1297 /* compare high */
1298 op1 = op;
1299 /* when values are equal, we need to compare low words. since
1300 the jump is inverted, we invert the test too. */
1301 if (op1 == TOK_LT)
1302 op1 = TOK_LE;
1303 else if (op1 == TOK_GT)
1304 op1 = TOK_GE;
1305 else if (op1 == TOK_ULT)
1306 op1 = TOK_ULE;
1307 else if (op1 == TOK_UGT)
1308 op1 = TOK_UGE;
1309 a = 0;
1310 b = 0;
1311 gen_op(op1);
1312 if (op1 != TOK_NE) {
1313 a = gvtst(1, 0);
1315 if (op != TOK_EQ) {
1316 /* generate non equal test */
1317 /* XXX: NOT PORTABLE yet */
1318 if (a == 0) {
1319 b = gvtst(0, 0);
1320 } else {
1321 #if defined(TCC_TARGET_I386)
1322 b = psym(0x850f, 0);
1323 #elif defined(TCC_TARGET_ARM)
1324 b = ind;
1325 o(0x1A000000 | encbranch(ind, 0, 1));
1326 #elif defined(TCC_TARGET_C67)
1327 tcc_error("not implemented");
1328 #else
1329 #error not supported
1330 #endif
1333 /* compare low. Always unsigned */
1334 op1 = op;
1335 if (op1 == TOK_LT)
1336 op1 = TOK_ULT;
1337 else if (op1 == TOK_LE)
1338 op1 = TOK_ULE;
1339 else if (op1 == TOK_GT)
1340 op1 = TOK_UGT;
1341 else if (op1 == TOK_GE)
1342 op1 = TOK_UGE;
1343 gen_op(op1);
1344 a = gvtst(1, a);
1345 gsym(b);
1346 vseti(VT_JMPI, a);
1347 break;
1350 #endif
1352 /* handle integer constant optimizations and various machine
1353 independent opt */
1354 static void gen_opic(int op)
1356 int c1, c2, t1, t2, n;
1357 SValue *v1, *v2;
1358 long long l1, l2;
1359 typedef unsigned long long U;
1361 v1 = vtop - 1;
1362 v2 = vtop;
1363 t1 = v1->type.t & VT_BTYPE;
1364 t2 = v2->type.t & VT_BTYPE;
1366 if (t1 == VT_LLONG)
1367 l1 = v1->c.ll;
1368 else if (v1->type.t & VT_UNSIGNED)
1369 l1 = v1->c.ui;
1370 else
1371 l1 = v1->c.i;
1373 if (t2 == VT_LLONG)
1374 l2 = v2->c.ll;
1375 else if (v2->type.t & VT_UNSIGNED)
1376 l2 = v2->c.ui;
1377 else
1378 l2 = v2->c.i;
1380 /* currently, we cannot do computations with forward symbols */
1381 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1382 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1383 if (c1 && c2) {
1384 switch(op) {
1385 case '+': l1 += l2; break;
1386 case '-': l1 -= l2; break;
1387 case '&': l1 &= l2; break;
1388 case '^': l1 ^= l2; break;
1389 case '|': l1 |= l2; break;
1390 case '*': l1 *= l2; break;
1392 case TOK_PDIV:
1393 case '/':
1394 case '%':
1395 case TOK_UDIV:
1396 case TOK_UMOD:
1397 /* if division by zero, generate explicit division */
1398 if (l2 == 0) {
1399 if (const_wanted)
1400 tcc_error("division by zero in constant");
1401 goto general_case;
1403 switch(op) {
1404 default: l1 /= l2; break;
1405 case '%': l1 %= l2; break;
1406 case TOK_UDIV: l1 = (U)l1 / l2; break;
1407 case TOK_UMOD: l1 = (U)l1 % l2; break;
1409 break;
1410 case TOK_SHL: l1 <<= l2; break;
1411 case TOK_SHR: l1 = (U)l1 >> l2; break;
1412 case TOK_SAR: l1 >>= l2; break;
1413 /* tests */
1414 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1415 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1416 case TOK_EQ: l1 = l1 == l2; break;
1417 case TOK_NE: l1 = l1 != l2; break;
1418 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1419 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1420 case TOK_LT: l1 = l1 < l2; break;
1421 case TOK_GE: l1 = l1 >= l2; break;
1422 case TOK_LE: l1 = l1 <= l2; break;
1423 case TOK_GT: l1 = l1 > l2; break;
1424 /* logical */
1425 case TOK_LAND: l1 = l1 && l2; break;
1426 case TOK_LOR: l1 = l1 || l2; break;
1427 default:
1428 goto general_case;
1430 v1->c.ll = l1;
1431 vtop--;
1432 } else {
1433 /* if commutative ops, put c2 as constant */
1434 if (c1 && (op == '+' || op == '&' || op == '^' ||
1435 op == '|' || op == '*')) {
1436 vswap();
1437 c2 = c1; //c = c1, c1 = c2, c2 = c;
1438 l2 = l1; //l = l1, l1 = l2, l2 = l;
1440 /* Filter out NOP operations like x*1, x-0, x&-1... */
1441 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1442 op == TOK_PDIV) &&
1443 l2 == 1) ||
1444 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1445 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1446 l2 == 0) ||
1447 (op == '&' &&
1448 l2 == -1))) {
1449 /* nothing to do */
1450 vtop--;
1451 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1452 /* try to use shifts instead of muls or divs */
1453 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1454 n = -1;
1455 while (l2) {
1456 l2 >>= 1;
1457 n++;
1459 vtop->c.ll = n;
1460 if (op == '*')
1461 op = TOK_SHL;
1462 else if (op == TOK_PDIV)
1463 op = TOK_SAR;
1464 else
1465 op = TOK_SHR;
1467 goto general_case;
1468 } else if (c2 && (op == '+' || op == '-') &&
1469 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1470 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1471 /* symbol + constant case */
1472 if (op == '-')
1473 l2 = -l2;
1474 vtop--;
1475 vtop->c.ll += l2;
1476 } else {
1477 general_case:
1478 if (!nocode_wanted) {
1479 /* call low level op generator */
1480 if (t1 == VT_LLONG || t2 == VT_LLONG)
1481 gen_opl(op);
1482 else
1483 gen_opi(op);
1484 } else {
1485 vtop--;
1491 /* generate a floating point operation with constant propagation */
1492 static void gen_opif(int op)
1494 int c1, c2;
1495 SValue *v1, *v2;
1496 long double f1, f2;
1498 v1 = vtop - 1;
1499 v2 = vtop;
1500 /* currently, we cannot do computations with forward symbols */
1501 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1502 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1503 if (c1 && c2) {
1504 if (v1->type.t == VT_FLOAT) {
1505 f1 = v1->c.f;
1506 f2 = v2->c.f;
1507 } else if (v1->type.t == VT_DOUBLE) {
1508 f1 = v1->c.d;
1509 f2 = v2->c.d;
1510 } else {
1511 f1 = v1->c.ld;
1512 f2 = v2->c.ld;
1515 /* NOTE: we only do constant propagation if finite number (not
1516 NaN or infinity) (ANSI spec) */
1517 if (!ieee_finite(f1) || !ieee_finite(f2))
1518 goto general_case;
1520 switch(op) {
1521 case '+': f1 += f2; break;
1522 case '-': f1 -= f2; break;
1523 case '*': f1 *= f2; break;
1524 case '/':
1525 if (f2 == 0.0) {
1526 if (const_wanted)
1527 tcc_error("division by zero in constant");
1528 goto general_case;
1530 f1 /= f2;
1531 break;
1532 /* XXX: also handles tests ? */
1533 default:
1534 goto general_case;
1536 /* XXX: overflow test ? */
1537 if (v1->type.t == VT_FLOAT) {
1538 v1->c.f = f1;
1539 } else if (v1->type.t == VT_DOUBLE) {
1540 v1->c.d = f1;
1541 } else {
1542 v1->c.ld = f1;
1544 vtop--;
1545 } else {
1546 general_case:
1547 if (!nocode_wanted) {
1548 gen_opf(op);
1549 } else {
1550 vtop--;
1555 static int pointed_size(CType *type)
1557 int align;
1558 return type_size(pointed_type(type), &align);
1561 static void vla_runtime_pointed_size(CType *type)
1563 int align;
1564 vla_runtime_type_size(pointed_type(type), &align);
1567 static inline int is_null_pointer(SValue *p)
1569 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1570 return 0;
1571 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1572 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1573 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0);
1576 static inline int is_integer_btype(int bt)
1578 return (bt == VT_BYTE || bt == VT_SHORT ||
1579 bt == VT_INT || bt == VT_LLONG);
1582 /* check types for comparison or substraction of pointers */
1583 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1585 CType *type1, *type2, tmp_type1, tmp_type2;
1586 int bt1, bt2;
1588 /* null pointers are accepted for all comparisons as gcc */
1589 if (is_null_pointer(p1) || is_null_pointer(p2))
1590 return;
1591 type1 = &p1->type;
1592 type2 = &p2->type;
1593 bt1 = type1->t & VT_BTYPE;
1594 bt2 = type2->t & VT_BTYPE;
1595 /* accept comparison between pointer and integer with a warning */
1596 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1597 if (op != TOK_LOR && op != TOK_LAND )
1598 tcc_warning("comparison between pointer and integer");
1599 return;
1602 /* both must be pointers or implicit function pointers */
1603 if (bt1 == VT_PTR) {
1604 type1 = pointed_type(type1);
1605 } else if (bt1 != VT_FUNC)
1606 goto invalid_operands;
1608 if (bt2 == VT_PTR) {
1609 type2 = pointed_type(type2);
1610 } else if (bt2 != VT_FUNC) {
1611 invalid_operands:
1612 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1614 if ((type1->t & VT_BTYPE) == VT_VOID ||
1615 (type2->t & VT_BTYPE) == VT_VOID)
1616 return;
1617 tmp_type1 = *type1;
1618 tmp_type2 = *type2;
1619 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1620 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1621 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1622 /* gcc-like error if '-' is used */
1623 if (op == '-')
1624 goto invalid_operands;
1625 else
1626 tcc_warning("comparison of distinct pointer types lacks a cast");
1630 /* generic gen_op: handles types problems */
1631 ST_FUNC void gen_op(int op)
1633 int u, t1, t2, bt1, bt2, t;
1634 CType type1;
1636 t1 = vtop[-1].type.t;
1637 t2 = vtop[0].type.t;
1638 bt1 = t1 & VT_BTYPE;
1639 bt2 = t2 & VT_BTYPE;
1641 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1642 /* at least one operand is a pointer */
1643 /* relationnal op: must be both pointers */
1644 if (op >= TOK_ULT && op <= TOK_LOR) {
1645 check_comparison_pointer_types(vtop - 1, vtop, op);
1646 /* pointers are handled are unsigned */
1647 #ifdef TCC_TARGET_X86_64
1648 t = VT_LLONG | VT_UNSIGNED;
1649 #else
1650 t = VT_INT | VT_UNSIGNED;
1651 #endif
1652 goto std_op;
1654 /* if both pointers, then it must be the '-' op */
1655 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1656 if (op != '-')
1657 tcc_error("cannot use pointers here");
1658 check_comparison_pointer_types(vtop - 1, vtop, op);
1659 /* XXX: check that types are compatible */
1660 if (vtop[-1].type.t & VT_VLA) {
1661 vla_runtime_pointed_size(&vtop[-1].type);
1662 } else {
1663 vpushi(pointed_size(&vtop[-1].type));
1665 vrott(3);
1666 gen_opic(op);
1667 /* set to integer type */
1668 #ifdef TCC_TARGET_X86_64
1669 vtop->type.t = VT_LLONG;
1670 #else
1671 vtop->type.t = VT_INT;
1672 #endif
1673 vswap();
1674 gen_op(TOK_PDIV);
1675 } else {
1676 /* exactly one pointer : must be '+' or '-'. */
1677 if (op != '-' && op != '+')
1678 tcc_error("cannot use pointers here");
1679 /* Put pointer as first operand */
1680 if (bt2 == VT_PTR) {
1681 vswap();
1682 swap(&t1, &t2);
1684 type1 = vtop[-1].type;
1685 type1.t &= ~VT_ARRAY;
1686 if (vtop[-1].type.t & VT_VLA)
1687 vla_runtime_pointed_size(&vtop[-1].type);
1688 else {
1689 u = pointed_size(&vtop[-1].type);
1690 if (u < 0)
1691 tcc_error("unknown array element size");
1692 #ifdef TCC_TARGET_X86_64
1693 vpushll(u);
1694 #else
1695 /* XXX: cast to int ? (long long case) */
1696 vpushi(u);
1697 #endif
1699 gen_op('*');
1700 #ifdef CONFIG_TCC_BCHECK
1701 /* if evaluating constant expression, no code should be
1702 generated, so no bound check */
1703 if (tcc_state->do_bounds_check && !const_wanted) {
1704 /* if bounded pointers, we generate a special code to
1705 test bounds */
1706 if (op == '-') {
1707 vpushi(0);
1708 vswap();
1709 gen_op('-');
1711 gen_bounded_ptr_add();
1712 } else
1713 #endif
1715 gen_opic(op);
1717 /* put again type if gen_opic() swaped operands */
1718 vtop->type = type1;
1720 } else if (is_float(bt1) || is_float(bt2)) {
1721 /* compute bigger type and do implicit casts */
1722 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1723 t = VT_LDOUBLE;
1724 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1725 t = VT_DOUBLE;
1726 } else {
1727 t = VT_FLOAT;
1729 /* floats can only be used for a few operations */
1730 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1731 (op < TOK_ULT || op > TOK_GT))
1732 tcc_error("invalid operands for binary operation");
1733 goto std_op;
1734 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1735 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1736 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1737 t |= VT_UNSIGNED;
1738 goto std_op;
1739 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1740 /* cast to biggest op */
1741 t = VT_LLONG;
1742 /* convert to unsigned if it does not fit in a long long */
1743 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1744 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1745 t |= VT_UNSIGNED;
1746 goto std_op;
1747 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1748 tcc_error("comparison of struct");
1749 } else {
1750 /* integer operations */
1751 t = VT_INT;
1752 /* convert to unsigned if it does not fit in an integer */
1753 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1754 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1755 t |= VT_UNSIGNED;
1756 std_op:
1757 /* XXX: currently, some unsigned operations are explicit, so
1758 we modify them here */
1759 if (t & VT_UNSIGNED) {
1760 if (op == TOK_SAR)
1761 op = TOK_SHR;
1762 else if (op == '/')
1763 op = TOK_UDIV;
1764 else if (op == '%')
1765 op = TOK_UMOD;
1766 else if (op == TOK_LT)
1767 op = TOK_ULT;
1768 else if (op == TOK_GT)
1769 op = TOK_UGT;
1770 else if (op == TOK_LE)
1771 op = TOK_ULE;
1772 else if (op == TOK_GE)
1773 op = TOK_UGE;
1775 vswap();
1776 type1.t = t;
1777 gen_cast(&type1);
1778 vswap();
1779 /* special case for shifts and long long: we keep the shift as
1780 an integer */
1781 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1782 type1.t = VT_INT;
1783 gen_cast(&type1);
1784 if (is_float(t))
1785 gen_opif(op);
1786 else
1787 gen_opic(op);
1788 if (op >= TOK_ULT && op <= TOK_GT) {
1789 /* relationnal op: the result is an int */
1790 vtop->type.t = VT_INT;
1791 } else {
1792 vtop->type.t = t;
1797 #ifndef TCC_TARGET_ARM
1798 /* generic itof for unsigned long long case */
1799 static void gen_cvt_itof1(int t)
1801 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1802 (VT_LLONG | VT_UNSIGNED)) {
1804 if (t == VT_FLOAT)
1805 vpush_global_sym(&func_old_type, TOK___floatundisf);
1806 #if LDOUBLE_SIZE != 8
1807 else if (t == VT_LDOUBLE)
1808 vpush_global_sym(&func_old_type, TOK___floatundixf);
1809 #endif
1810 else
1811 vpush_global_sym(&func_old_type, TOK___floatundidf);
1812 vrott(2);
1813 gfunc_call(1);
1814 vpushi(0);
1815 vtop->r = reg_fret(t);
1816 } else {
1817 gen_cvt_itof(t);
1820 #endif
1822 /* generic ftoi for unsigned long long case */
1823 static void gen_cvt_ftoi1(int t)
1825 int st;
1827 if (t == (VT_LLONG | VT_UNSIGNED)) {
1828 /* not handled natively */
1829 st = vtop->type.t & VT_BTYPE;
1830 if (st == VT_FLOAT)
1831 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1832 #if LDOUBLE_SIZE != 8
1833 else if (st == VT_LDOUBLE)
1834 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1835 #endif
1836 else
1837 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1838 vrott(2);
1839 gfunc_call(1);
1840 vpushi(0);
1841 vtop->r = REG_IRET;
1842 vtop->r2 = REG_LRET;
1843 } else {
1844 gen_cvt_ftoi(t);
1848 /* force char or short cast */
1849 static void force_charshort_cast(int t)
1851 int bits, dbt;
1852 dbt = t & VT_BTYPE;
1853 /* XXX: add optimization if lvalue : just change type and offset */
1854 if (dbt == VT_BYTE)
1855 bits = 8;
1856 else
1857 bits = 16;
1858 if (t & VT_UNSIGNED) {
1859 vpushi((1 << bits) - 1);
1860 gen_op('&');
1861 } else {
1862 bits = 32 - bits;
1863 vpushi(bits);
1864 gen_op(TOK_SHL);
1865 /* result must be signed or the SAR is converted to an SHL
1866 This was not the case when "t" was a signed short
1867 and the last value on the stack was an unsigned int */
1868 vtop->type.t &= ~VT_UNSIGNED;
1869 vpushi(bits);
1870 gen_op(TOK_SAR);
1874 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1875 static void gen_cast(CType *type)
1877 int sbt, dbt, sf, df, c, p;
1879 /* special delayed cast for char/short */
1880 /* XXX: in some cases (multiple cascaded casts), it may still
1881 be incorrect */
1882 if (vtop->r & VT_MUSTCAST) {
1883 vtop->r &= ~VT_MUSTCAST;
1884 force_charshort_cast(vtop->type.t);
1887 /* bitfields first get cast to ints */
1888 if (vtop->type.t & VT_BITFIELD) {
1889 gv(RC_INT);
1892 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1893 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1895 if (sbt != dbt) {
1896 sf = is_float(sbt);
1897 df = is_float(dbt);
1898 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1899 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1900 if (c) {
1901 /* constant case: we can do it now */
1902 /* XXX: in ISOC, cannot do it if error in convert */
1903 if (sbt == VT_FLOAT)
1904 vtop->c.ld = vtop->c.f;
1905 else if (sbt == VT_DOUBLE)
1906 vtop->c.ld = vtop->c.d;
1908 if (df) {
1909 if ((sbt & VT_BTYPE) == VT_LLONG) {
1910 if (sbt & VT_UNSIGNED)
1911 vtop->c.ld = vtop->c.ull;
1912 else
1913 vtop->c.ld = vtop->c.ll;
1914 } else if(!sf) {
1915 if (sbt & VT_UNSIGNED)
1916 vtop->c.ld = vtop->c.ui;
1917 else
1918 vtop->c.ld = vtop->c.i;
1921 if (dbt == VT_FLOAT)
1922 vtop->c.f = (float)vtop->c.ld;
1923 else if (dbt == VT_DOUBLE)
1924 vtop->c.d = (double)vtop->c.ld;
1925 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1926 vtop->c.ull = (unsigned long long)vtop->c.ld;
1927 } else if (sf && dbt == VT_BOOL) {
1928 vtop->c.i = (vtop->c.ld != 0);
1929 } else {
1930 if(sf)
1931 vtop->c.ll = (long long)vtop->c.ld;
1932 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1933 vtop->c.ll = vtop->c.ull;
1934 else if (sbt & VT_UNSIGNED)
1935 vtop->c.ll = vtop->c.ui;
1936 #ifdef TCC_TARGET_X86_64
1937 else if (sbt == VT_PTR)
1939 #endif
1940 else if (sbt != VT_LLONG)
1941 vtop->c.ll = vtop->c.i;
1943 if (dbt == (VT_LLONG|VT_UNSIGNED))
1944 vtop->c.ull = vtop->c.ll;
1945 else if (dbt == VT_BOOL)
1946 vtop->c.i = (vtop->c.ll != 0);
1947 #ifdef TCC_TARGET_X86_64
1948 else if (dbt == VT_PTR)
1950 #endif
1951 else if (dbt != VT_LLONG) {
1952 int s = 0;
1953 if ((dbt & VT_BTYPE) == VT_BYTE)
1954 s = 24;
1955 else if ((dbt & VT_BTYPE) == VT_SHORT)
1956 s = 16;
1957 if(dbt & VT_UNSIGNED)
1958 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1959 else
1960 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1963 } else if (p && dbt == VT_BOOL) {
1964 vtop->r = VT_CONST;
1965 vtop->c.i = 1;
1966 } else if (!nocode_wanted) {
1967 /* non constant case: generate code */
1968 if (sf && df) {
1969 /* convert from fp to fp */
1970 gen_cvt_ftof(dbt);
1971 } else if (df) {
1972 /* convert int to fp */
1973 gen_cvt_itof1(dbt);
1974 } else if (sf) {
1975 /* convert fp to int */
1976 if (dbt == VT_BOOL) {
1977 vpushi(0);
1978 gen_op(TOK_NE);
1979 } else {
1980 /* we handle char/short/etc... with generic code */
1981 if (dbt != (VT_INT | VT_UNSIGNED) &&
1982 dbt != (VT_LLONG | VT_UNSIGNED) &&
1983 dbt != VT_LLONG)
1984 dbt = VT_INT;
1985 gen_cvt_ftoi1(dbt);
1986 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1987 /* additional cast for char/short... */
1988 vtop->type.t = dbt;
1989 gen_cast(type);
1992 #ifndef TCC_TARGET_X86_64
1993 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1994 if ((sbt & VT_BTYPE) != VT_LLONG) {
1995 /* scalar to long long */
1996 /* machine independent conversion */
1997 gv(RC_INT);
1998 /* generate high word */
1999 if (sbt == (VT_INT | VT_UNSIGNED)) {
2000 vpushi(0);
2001 gv(RC_INT);
2002 } else {
2003 if (sbt == VT_PTR) {
2004 /* cast from pointer to int before we apply
2005 shift operation, which pointers don't support*/
2006 gen_cast(&int_type);
2008 gv_dup();
2009 vpushi(31);
2010 gen_op(TOK_SAR);
2012 /* patch second register */
2013 vtop[-1].r2 = vtop->r;
2014 vpop();
2016 #else
2017 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2018 (dbt & VT_BTYPE) == VT_PTR ||
2019 (dbt & VT_BTYPE) == VT_FUNC) {
2020 if ((sbt & VT_BTYPE) != VT_LLONG &&
2021 (sbt & VT_BTYPE) != VT_PTR &&
2022 (sbt & VT_BTYPE) != VT_FUNC) {
2023 /* need to convert from 32bit to 64bit */
2024 int r = gv(RC_INT);
2025 if (sbt != (VT_INT | VT_UNSIGNED)) {
2026 /* x86_64 specific: movslq */
2027 o(0x6348);
2028 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2031 #endif
2032 } else if (dbt == VT_BOOL) {
2033 /* scalar to bool */
2034 vpushi(0);
2035 gen_op(TOK_NE);
2036 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2037 (dbt & VT_BTYPE) == VT_SHORT) {
2038 if (sbt == VT_PTR) {
2039 vtop->type.t = VT_INT;
2040 tcc_warning("nonportable conversion from pointer to char/short");
2042 force_charshort_cast(dbt);
2043 } else if ((dbt & VT_BTYPE) == VT_INT) {
2044 /* scalar to int */
2045 if (sbt == VT_LLONG) {
2046 /* from long long: just take low order word */
2047 lexpand();
2048 vpop();
2050 /* if lvalue and single word type, nothing to do because
2051 the lvalue already contains the real type size (see
2052 VT_LVAL_xxx constants) */
2055 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2056 /* if we are casting between pointer types,
2057 we must update the VT_LVAL_xxx size */
2058 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2059 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2061 vtop->type = *type;
2064 /* return type size as known at compile time. Put alignment at 'a' */
2065 ST_FUNC int type_size(CType *type, int *a)
2067 Sym *s;
2068 int bt;
2070 bt = type->t & VT_BTYPE;
2071 if (bt == VT_STRUCT) {
2072 /* struct/union */
2073 s = type->ref;
2074 *a = s->r;
2075 return s->c;
2076 } else if (bt == VT_PTR) {
2077 if (type->t & VT_ARRAY) {
2078 int ts;
2080 s = type->ref;
2081 ts = type_size(&s->type, a);
2083 if (ts < 0 && s->c < 0)
2084 ts = -ts;
2086 return ts * s->c;
2087 } else {
2088 *a = PTR_SIZE;
2089 return PTR_SIZE;
2091 } else if (bt == VT_LDOUBLE) {
2092 *a = LDOUBLE_ALIGN;
2093 return LDOUBLE_SIZE;
2094 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2095 #ifdef TCC_TARGET_I386
2096 #ifdef TCC_TARGET_PE
2097 *a = 8;
2098 #else
2099 *a = 4;
2100 #endif
2101 #elif defined(TCC_TARGET_ARM)
2102 #ifdef TCC_ARM_EABI
2103 *a = 8;
2104 #else
2105 *a = 4;
2106 #endif
2107 #else
2108 *a = 8;
2109 #endif
2110 return 8;
2111 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2112 *a = 4;
2113 return 4;
2114 } else if (bt == VT_SHORT) {
2115 *a = 2;
2116 return 2;
2117 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2118 *a = 8;
2119 return 16;
2120 } else {
2121 /* char, void, function, _Bool */
2122 *a = 1;
2123 return 1;
2127 /* push type size as known at runtime time on top of value stack. Put
2128 alignment at 'a' */
2129 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2131 if (type->t & VT_VLA) {
2132 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2133 } else {
2134 vpushi(type_size(type, a));
2138 static void vla_sp_save(void) {
2139 if (!(vla_flags & VLA_SP_LOC_SET)) {
2140 *vla_sp_loc = (loc -= PTR_SIZE);
2141 vla_flags |= VLA_SP_LOC_SET;
2143 if (!(vla_flags & VLA_SP_SAVED)) {
2144 gen_vla_sp_save(*vla_sp_loc);
2145 vla_flags |= VLA_SP_SAVED;
2149 /* return the pointed type of t */
2150 static inline CType *pointed_type(CType *type)
2152 return &type->ref->type;
2155 /* modify type so that its it is a pointer to type. */
2156 ST_FUNC void mk_pointer(CType *type)
2158 Sym *s;
2159 s = sym_push(SYM_FIELD, type, 0, -1);
2160 type->t = VT_PTR | (type->t & ~VT_TYPE);
2161 type->ref = s;
2164 /* compare function types. OLD functions match any new functions */
2165 static int is_compatible_func(CType *type1, CType *type2)
2167 Sym *s1, *s2;
2169 s1 = type1->ref;
2170 s2 = type2->ref;
2171 if (!is_compatible_types(&s1->type, &s2->type))
2172 return 0;
2173 /* check func_call */
2174 if (s1->a.func_call != s2->a.func_call)
2175 return 0;
2176 /* XXX: not complete */
2177 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2178 return 1;
2179 if (s1->c != s2->c)
2180 return 0;
2181 while (s1 != NULL) {
2182 if (s2 == NULL)
2183 return 0;
2184 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2185 return 0;
2186 s1 = s1->next;
2187 s2 = s2->next;
2189 if (s2)
2190 return 0;
2191 return 1;
2194 /* return true if type1 and type2 are the same. If unqualified is
2195 true, qualifiers on the types are ignored.
2197 - enums are not checked as gcc __builtin_types_compatible_p ()
2199 static int compare_types(CType *type1, CType *type2, int unqualified)
2201 int bt1, t1, t2;
2203 t1 = type1->t & VT_TYPE;
2204 t2 = type2->t & VT_TYPE;
2205 if (unqualified) {
2206 /* strip qualifiers before comparing */
2207 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2208 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2210 /* Default Vs explicit signedness only matters for char */
2211 if ((t1 & VT_BTYPE) != VT_BYTE) {
2212 t1 &= ~VT_DEFSIGN;
2213 t2 &= ~VT_DEFSIGN;
2215 /* XXX: bitfields ? */
2216 if (t1 != t2)
2217 return 0;
2218 /* test more complicated cases */
2219 bt1 = t1 & VT_BTYPE;
2220 if (bt1 == VT_PTR) {
2221 type1 = pointed_type(type1);
2222 type2 = pointed_type(type2);
2223 return is_compatible_types(type1, type2);
2224 } else if (bt1 == VT_STRUCT) {
2225 return (type1->ref == type2->ref);
2226 } else if (bt1 == VT_FUNC) {
2227 return is_compatible_func(type1, type2);
2228 } else {
2229 return 1;
2233 /* return true if type1 and type2 are exactly the same (including
2234 qualifiers).
2236 static int is_compatible_types(CType *type1, CType *type2)
2238 return compare_types(type1,type2,0);
2241 /* return true if type1 and type2 are the same (ignoring qualifiers).
2243 static int is_compatible_parameter_types(CType *type1, CType *type2)
2245 return compare_types(type1,type2,1);
2248 /* print a type. If 'varstr' is not NULL, then the variable is also
2249 printed in the type */
2250 /* XXX: union */
2251 /* XXX: add array and function pointers */
2252 static void type_to_str(char *buf, int buf_size,
2253 CType *type, const char *varstr)
2255 int bt, v, t;
2256 Sym *s, *sa;
2257 char buf1[256];
2258 const char *tstr;
2260 t = type->t & VT_TYPE;
2261 bt = t & VT_BTYPE;
2262 buf[0] = '\0';
2263 if (t & VT_CONSTANT)
2264 pstrcat(buf, buf_size, "const ");
2265 if (t & VT_VOLATILE)
2266 pstrcat(buf, buf_size, "volatile ");
2267 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2268 pstrcat(buf, buf_size, "unsigned ");
2269 else if (t & VT_DEFSIGN)
2270 pstrcat(buf, buf_size, "signed ");
2271 switch(bt) {
2272 case VT_VOID:
2273 tstr = "void";
2274 goto add_tstr;
2275 case VT_BOOL:
2276 tstr = "_Bool";
2277 goto add_tstr;
2278 case VT_BYTE:
2279 tstr = "char";
2280 goto add_tstr;
2281 case VT_SHORT:
2282 tstr = "short";
2283 goto add_tstr;
2284 case VT_INT:
2285 tstr = "int";
2286 goto add_tstr;
2287 case VT_LONG:
2288 tstr = "long";
2289 goto add_tstr;
2290 case VT_LLONG:
2291 tstr = "long long";
2292 goto add_tstr;
2293 case VT_FLOAT:
2294 tstr = "float";
2295 goto add_tstr;
2296 case VT_DOUBLE:
2297 tstr = "double";
2298 goto add_tstr;
2299 case VT_LDOUBLE:
2300 tstr = "long double";
2301 add_tstr:
2302 pstrcat(buf, buf_size, tstr);
2303 break;
2304 case VT_ENUM:
2305 case VT_STRUCT:
2306 if (bt == VT_STRUCT)
2307 tstr = "struct ";
2308 else
2309 tstr = "enum ";
2310 pstrcat(buf, buf_size, tstr);
2311 v = type->ref->v & ~SYM_STRUCT;
2312 if (v >= SYM_FIRST_ANOM)
2313 pstrcat(buf, buf_size, "<anonymous>");
2314 else
2315 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2316 break;
2317 case VT_FUNC:
2318 s = type->ref;
2319 type_to_str(buf, buf_size, &s->type, varstr);
2320 pstrcat(buf, buf_size, "(");
2321 sa = s->next;
2322 while (sa != NULL) {
2323 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2324 pstrcat(buf, buf_size, buf1);
2325 sa = sa->next;
2326 if (sa)
2327 pstrcat(buf, buf_size, ", ");
2329 pstrcat(buf, buf_size, ")");
2330 goto no_var;
2331 case VT_PTR:
2332 s = type->ref;
2333 pstrcpy(buf1, sizeof(buf1), "*");
2334 if (varstr)
2335 pstrcat(buf1, sizeof(buf1), varstr);
2336 type_to_str(buf, buf_size, &s->type, buf1);
2337 goto no_var;
2339 if (varstr) {
2340 pstrcat(buf, buf_size, " ");
2341 pstrcat(buf, buf_size, varstr);
2343 no_var: ;
2346 /* verify type compatibility to store vtop in 'dt' type, and generate
2347 casts if needed. */
2348 static void gen_assign_cast(CType *dt)
2350 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2351 char buf1[256], buf2[256];
2352 int dbt, sbt;
2354 st = &vtop->type; /* source type */
2355 dbt = dt->t & VT_BTYPE;
2356 sbt = st->t & VT_BTYPE;
2357 if (sbt == VT_VOID || dbt == VT_VOID)
2358 tcc_error("cannot cast from/to void");
2359 if (dt->t & VT_CONSTANT)
2360 tcc_warning("assignment of read-only location");
2361 switch(dbt) {
2362 case VT_PTR:
2363 /* special cases for pointers */
2364 /* '0' can also be a pointer */
2365 if (is_null_pointer(vtop))
2366 goto type_ok;
2367 /* accept implicit pointer to integer cast with warning */
2368 if (is_integer_btype(sbt)) {
2369 tcc_warning("assignment makes pointer from integer without a cast");
2370 goto type_ok;
2372 type1 = pointed_type(dt);
2373 /* a function is implicitely a function pointer */
2374 if (sbt == VT_FUNC) {
2375 if ((type1->t & VT_BTYPE) != VT_VOID &&
2376 !is_compatible_types(pointed_type(dt), st))
2377 tcc_warning("assignment from incompatible pointer type");
2378 goto type_ok;
2380 if (sbt != VT_PTR)
2381 goto error;
2382 type2 = pointed_type(st);
2383 if ((type1->t & VT_BTYPE) == VT_VOID ||
2384 (type2->t & VT_BTYPE) == VT_VOID) {
2385 /* void * can match anything */
2386 } else {
2387 /* exact type match, except for unsigned */
2388 tmp_type1 = *type1;
2389 tmp_type2 = *type2;
2390 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2391 VT_VOLATILE);
2392 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2393 VT_VOLATILE);
2394 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2395 tcc_warning("assignment from incompatible pointer type");
2397 /* check const and volatile */
2398 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2399 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2400 tcc_warning("assignment discards qualifiers from pointer target type");
2401 break;
2402 case VT_BYTE:
2403 case VT_SHORT:
2404 case VT_INT:
2405 case VT_LLONG:
2406 if (sbt == VT_PTR || sbt == VT_FUNC) {
2407 tcc_warning("assignment makes integer from pointer without a cast");
2409 /* XXX: more tests */
2410 break;
2411 case VT_STRUCT:
2412 tmp_type1 = *dt;
2413 tmp_type2 = *st;
2414 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2415 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2416 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2417 error:
2418 type_to_str(buf1, sizeof(buf1), st, NULL);
2419 type_to_str(buf2, sizeof(buf2), dt, NULL);
2420 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2422 break;
2424 type_ok:
2425 gen_cast(dt);
2428 /* store vtop in lvalue pushed on stack */
2429 ST_FUNC void vstore(void)
2431 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2433 ft = vtop[-1].type.t;
2434 sbt = vtop->type.t & VT_BTYPE;
2435 dbt = ft & VT_BTYPE;
2436 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2437 (sbt == VT_INT && dbt == VT_SHORT))
2438 && !(vtop->type.t & VT_BITFIELD)) {
2439 /* optimize char/short casts */
2440 delayed_cast = VT_MUSTCAST;
2441 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2442 /* XXX: factorize */
2443 if (ft & VT_CONSTANT)
2444 tcc_warning("assignment of read-only location");
2445 } else {
2446 delayed_cast = 0;
2447 if (!(ft & VT_BITFIELD))
2448 gen_assign_cast(&vtop[-1].type);
2451 if (sbt == VT_STRUCT) {
2452 /* if structure, only generate pointer */
2453 /* structure assignment : generate memcpy */
2454 /* XXX: optimize if small size */
2455 if (!nocode_wanted) {
2456 size = type_size(&vtop->type, &align);
2458 /* destination */
2459 vswap();
2460 vtop->type.t = VT_PTR;
2461 gaddrof();
2463 /* address of memcpy() */
2464 #ifdef TCC_ARM_EABI
2465 if(!(align & 7))
2466 vpush_global_sym(&func_old_type, TOK_memcpy8);
2467 else if(!(align & 3))
2468 vpush_global_sym(&func_old_type, TOK_memcpy4);
2469 else
2470 #endif
2471 vpush_global_sym(&func_old_type, TOK_memcpy);
2473 vswap();
2474 /* source */
2475 vpushv(vtop - 2);
2476 vtop->type.t = VT_PTR;
2477 gaddrof();
2478 /* type size */
2479 vpushi(size);
2480 gfunc_call(3);
2481 } else {
2482 vswap();
2483 vpop();
2485 /* leave source on stack */
2486 } else if (ft & VT_BITFIELD) {
2487 /* bitfield store handling */
2488 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2489 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2490 /* remove bit field info to avoid loops */
2491 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2493 /* duplicate source into other register */
2494 gv_dup();
2495 vswap();
2496 vrott(3);
2498 if((ft & VT_BTYPE) == VT_BOOL) {
2499 gen_cast(&vtop[-1].type);
2500 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2503 /* duplicate destination */
2504 vdup();
2505 vtop[-1] = vtop[-2];
2507 /* mask and shift source */
2508 if((ft & VT_BTYPE) != VT_BOOL) {
2509 if((ft & VT_BTYPE) == VT_LLONG) {
2510 vpushll((1ULL << bit_size) - 1ULL);
2511 } else {
2512 vpushi((1 << bit_size) - 1);
2514 gen_op('&');
2516 vpushi(bit_pos);
2517 gen_op(TOK_SHL);
2518 /* load destination, mask and or with source */
2519 vswap();
2520 if((ft & VT_BTYPE) == VT_LLONG) {
2521 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2522 } else {
2523 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2525 gen_op('&');
2526 gen_op('|');
2527 /* store result */
2528 vstore();
2530 /* pop off shifted source from "duplicate source..." above */
2531 vpop();
2533 } else {
2534 #ifdef CONFIG_TCC_BCHECK
2535 /* bound check case */
2536 if (vtop[-1].r & VT_MUSTBOUND) {
2537 vswap();
2538 gbound();
2539 vswap();
2541 #endif
2542 if (!nocode_wanted) {
2543 rc = RC_INT;
2544 if (is_float(ft)) {
2545 rc = RC_FLOAT;
2546 #ifdef TCC_TARGET_X86_64
2547 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2548 rc = RC_ST0;
2549 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2550 rc = RC_FRET;
2552 #endif
2554 r = gv(rc); /* generate value */
2555 /* if lvalue was saved on stack, must read it */
2556 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2557 SValue sv;
2558 t = get_reg(RC_INT);
2559 #ifdef TCC_TARGET_X86_64
2560 sv.type.t = VT_PTR;
2561 #else
2562 sv.type.t = VT_INT;
2563 #endif
2564 sv.r = VT_LOCAL | VT_LVAL;
2565 sv.c.ul = vtop[-1].c.ul;
2566 load(t, &sv);
2567 vtop[-1].r = t | VT_LVAL;
2569 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2570 #ifdef TCC_TARGET_X86_64
2571 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2572 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2573 #else
2574 if ((ft & VT_BTYPE) == VT_LLONG) {
2575 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2576 #endif
2577 vtop[-1].type.t = load_type;
2578 store(r, vtop - 1);
2579 vswap();
2580 /* convert to int to increment easily */
2581 vtop->type.t = addr_type;
2582 gaddrof();
2583 vpushi(load_size);
2584 gen_op('+');
2585 vtop->r |= VT_LVAL;
2586 vswap();
2587 vtop[-1].type.t = load_type;
2588 /* XXX: it works because r2 is spilled last ! */
2589 store(vtop->r2, vtop - 1);
2590 } else {
2591 store(r, vtop - 1);
2594 vswap();
2595 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2596 vtop->r |= delayed_cast;
2600 /* post defines POST/PRE add. c is the token ++ or -- */
2601 ST_FUNC void inc(int post, int c)
2603 test_lvalue();
2604 vdup(); /* save lvalue */
2605 if (post) {
2606 gv_dup(); /* duplicate value */
2607 vrotb(3);
2608 vrotb(3);
2610 /* add constant */
2611 vpushi(c - TOK_MID);
2612 gen_op('+');
2613 vstore(); /* store value */
2614 if (post)
2615 vpop(); /* if post op, return saved value */
2618 /* Parse GNUC __attribute__ extension. Currently, the following
2619 extensions are recognized:
2620 - aligned(n) : set data/function alignment.
2621 - packed : force data alignment to 1
2622 - section(x) : generate data/code in this section.
2623 - unused : currently ignored, but may be used someday.
2624 - regparm(n) : pass function parameters in registers (i386 only)
2626 static void parse_attribute(AttributeDef *ad)
2628 int t, n;
2630 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2631 next();
2632 skip('(');
2633 skip('(');
2634 while (tok != ')') {
2635 if (tok < TOK_IDENT)
2636 expect("attribute name");
2637 t = tok;
2638 next();
2639 switch(t) {
2640 case TOK_SECTION1:
2641 case TOK_SECTION2:
2642 skip('(');
2643 if (tok != TOK_STR)
2644 expect("section name");
2645 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2646 next();
2647 skip(')');
2648 break;
2649 case TOK_ALIAS1:
2650 case TOK_ALIAS2:
2651 skip('(');
2652 if (tok != TOK_STR)
2653 expect("alias(\"target\")");
2654 ad->alias_target = /* save string as token, for later */
2655 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2656 next();
2657 skip(')');
2658 break;
2659 case TOK_ALIGNED1:
2660 case TOK_ALIGNED2:
2661 if (tok == '(') {
2662 next();
2663 n = expr_const();
2664 if (n <= 0 || (n & (n - 1)) != 0)
2665 tcc_error("alignment must be a positive power of two");
2666 skip(')');
2667 } else {
2668 n = MAX_ALIGN;
2670 ad->a.aligned = n;
2671 break;
2672 case TOK_PACKED1:
2673 case TOK_PACKED2:
2674 ad->a.packed = 1;
2675 break;
2676 case TOK_WEAK1:
2677 case TOK_WEAK2:
2678 ad->a.weak = 1;
2679 break;
2680 case TOK_UNUSED1:
2681 case TOK_UNUSED2:
2682 /* currently, no need to handle it because tcc does not
2683 track unused objects */
2684 break;
2685 case TOK_NORETURN1:
2686 case TOK_NORETURN2:
2687 /* currently, no need to handle it because tcc does not
2688 track unused objects */
2689 break;
2690 case TOK_CDECL1:
2691 case TOK_CDECL2:
2692 case TOK_CDECL3:
2693 ad->a.func_call = FUNC_CDECL;
2694 break;
2695 case TOK_STDCALL1:
2696 case TOK_STDCALL2:
2697 case TOK_STDCALL3:
2698 ad->a.func_call = FUNC_STDCALL;
2699 break;
2700 #ifdef TCC_TARGET_I386
2701 case TOK_REGPARM1:
2702 case TOK_REGPARM2:
2703 skip('(');
2704 n = expr_const();
2705 if (n > 3)
2706 n = 3;
2707 else if (n < 0)
2708 n = 0;
2709 if (n > 0)
2710 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2711 skip(')');
2712 break;
2713 case TOK_FASTCALL1:
2714 case TOK_FASTCALL2:
2715 case TOK_FASTCALL3:
2716 ad->a.func_call = FUNC_FASTCALLW;
2717 break;
2718 #endif
2719 case TOK_MODE:
2720 skip('(');
2721 switch(tok) {
2722 case TOK_MODE_DI:
2723 ad->a.mode = VT_LLONG + 1;
2724 break;
2725 case TOK_MODE_HI:
2726 ad->a.mode = VT_SHORT + 1;
2727 break;
2728 case TOK_MODE_SI:
2729 ad->a.mode = VT_INT + 1;
2730 break;
2731 default:
2732 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2733 break;
2735 next();
2736 skip(')');
2737 break;
2738 case TOK_DLLEXPORT:
2739 ad->a.func_export = 1;
2740 break;
2741 case TOK_DLLIMPORT:
2742 ad->a.func_import = 1;
2743 break;
2744 default:
2745 if (tcc_state->warn_unsupported)
2746 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2747 /* skip parameters */
2748 if (tok == '(') {
2749 int parenthesis = 0;
2750 do {
2751 if (tok == '(')
2752 parenthesis++;
2753 else if (tok == ')')
2754 parenthesis--;
2755 next();
2756 } while (parenthesis && tok != -1);
2758 break;
2760 if (tok != ',')
2761 break;
2762 next();
2764 skip(')');
2765 skip(')');
2769 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2770 static void struct_decl(CType *type, int u, int tdef)
2772 int a, v, size, align, maxalign, c, offset, flexible;
2773 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2774 Sym *s, *ss, *ass, **ps;
2775 AttributeDef ad;
2776 CType type1, btype;
2778 a = tok; /* save decl type */
2779 next();
2780 if (tok != '{') {
2781 v = tok;
2782 next();
2783 /* struct already defined ? return it */
2784 if (v < TOK_IDENT)
2785 expect("struct/union/enum name");
2786 s = struct_find(v);
2787 if (s) {
2788 if (s->type.t != a)
2789 tcc_error("invalid type");
2790 goto do_decl;
2791 } else if (tok >= TOK_IDENT && !tdef)
2792 tcc_error("unknown struct/union/enum");
2793 } else {
2794 v = anon_sym++;
2796 type1.t = a;
2797 type1.ref = NULL;
2798 /* we put an undefined size for struct/union */
2799 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2800 s->r = 0; /* default alignment is zero as gcc */
2801 /* put struct/union/enum name in type */
2802 do_decl:
2803 type->t = u;
2804 type->ref = s;
2806 if (tok == '{') {
2807 next();
2808 if (s->c != -1)
2809 tcc_error("struct/union/enum already defined");
2810 /* cannot be empty */
2811 c = 0;
2812 /* non empty enums are not allowed */
2813 if (a == TOK_ENUM) {
2814 for(;;) {
2815 v = tok;
2816 if (v < TOK_UIDENT)
2817 expect("identifier");
2818 ss = sym_find(v);
2819 if (ss && !local_stack)
2820 tcc_error("redefinition of enumerator '%s'",
2821 get_tok_str(v, NULL));
2822 next();
2823 if (tok == '=') {
2824 next();
2825 c = expr_const();
2827 /* enum symbols have static storage */
2828 ss = sym_push(v, &int_type, VT_CONST, c);
2829 ss->type.t |= VT_STATIC;
2830 if (tok != ',')
2831 break;
2832 next();
2833 c++;
2834 /* NOTE: we accept a trailing comma */
2835 if (tok == '}')
2836 break;
2838 s->c = type_size(&int_type, &align);
2839 skip('}');
2840 } else {
2841 maxalign = 1;
2842 ps = &s->next;
2843 prevbt = VT_INT;
2844 bit_pos = 0;
2845 offset = 0;
2846 flexible = 0;
2847 while (tok != '}') {
2848 parse_btype(&btype, &ad);
2849 while (1) {
2850 if (flexible)
2851 tcc_error("flexible array member '%s' not at the end of struct",
2852 get_tok_str(v, NULL));
2853 bit_size = -1;
2854 v = 0;
2855 type1 = btype;
2856 if (tok != ':') {
2857 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2858 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2859 expect("identifier");
2860 if (type_size(&type1, &align) < 0) {
2861 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2862 flexible = 1;
2863 else
2864 tcc_error("field '%s' has incomplete type",
2865 get_tok_str(v, NULL));
2867 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2868 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2869 tcc_error("invalid type for '%s'",
2870 get_tok_str(v, NULL));
2872 if (tok == ':') {
2873 next();
2874 bit_size = expr_const();
2875 /* XXX: handle v = 0 case for messages */
2876 if (bit_size < 0)
2877 tcc_error("negative width in bit-field '%s'",
2878 get_tok_str(v, NULL));
2879 if (v && bit_size == 0)
2880 tcc_error("zero width for bit-field '%s'",
2881 get_tok_str(v, NULL));
2883 size = type_size(&type1, &align);
2884 if (ad.a.aligned) {
2885 if (align < ad.a.aligned)
2886 align = ad.a.aligned;
2887 } else if (ad.a.packed) {
2888 align = 1;
2889 } else if (*tcc_state->pack_stack_ptr) {
2890 if (align > *tcc_state->pack_stack_ptr)
2891 align = *tcc_state->pack_stack_ptr;
2893 lbit_pos = 0;
2894 if (bit_size >= 0) {
2895 bt = type1.t & VT_BTYPE;
2896 if (bt != VT_INT &&
2897 bt != VT_BYTE &&
2898 bt != VT_SHORT &&
2899 bt != VT_BOOL &&
2900 bt != VT_ENUM &&
2901 bt != VT_LLONG)
2902 tcc_error("bitfields must have scalar type");
2903 bsize = size * 8;
2904 if (bit_size > bsize) {
2905 tcc_error("width of '%s' exceeds its type",
2906 get_tok_str(v, NULL));
2907 } else if (bit_size == bsize) {
2908 /* no need for bit fields */
2909 bit_pos = 0;
2910 } else if (bit_size == 0) {
2911 /* XXX: what to do if only padding in a
2912 structure ? */
2913 /* zero size: means to pad */
2914 bit_pos = 0;
2915 } else {
2916 /* we do not have enough room ?
2917 did the type change?
2918 is it a union? */
2919 if ((bit_pos + bit_size) > bsize ||
2920 bt != prevbt || a == TOK_UNION)
2921 bit_pos = 0;
2922 lbit_pos = bit_pos;
2923 /* XXX: handle LSB first */
2924 type1.t |= VT_BITFIELD |
2925 (bit_pos << VT_STRUCT_SHIFT) |
2926 (bit_size << (VT_STRUCT_SHIFT + 6));
2927 bit_pos += bit_size;
2929 prevbt = bt;
2930 } else {
2931 bit_pos = 0;
2933 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2934 /* add new memory data only if starting
2935 bit field */
2936 if (lbit_pos == 0) {
2937 if (a == TOK_STRUCT) {
2938 c = (c + align - 1) & -align;
2939 offset = c;
2940 if (size > 0)
2941 c += size;
2942 } else {
2943 offset = 0;
2944 if (size > c)
2945 c = size;
2947 if (align > maxalign)
2948 maxalign = align;
2950 #if 0
2951 printf("add field %s offset=%d",
2952 get_tok_str(v, NULL), offset);
2953 if (type1.t & VT_BITFIELD) {
2954 printf(" pos=%d size=%d",
2955 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2956 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2958 printf("\n");
2959 #endif
2961 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2962 ass = type1.ref;
2963 while ((ass = ass->next) != NULL) {
2964 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2965 *ps = ss;
2966 ps = &ss->next;
2968 } else if (v) {
2969 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2970 *ps = ss;
2971 ps = &ss->next;
2973 if (tok == ';' || tok == TOK_EOF)
2974 break;
2975 skip(',');
2977 skip(';');
2979 skip('}');
2980 /* store size and alignment */
2981 s->c = (c + maxalign - 1) & -maxalign;
2982 s->r = maxalign;
2987 /* return 1 if basic type is a type size (short, long, long long) */
2988 int is_btype_size (int bt)
2990 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
2993 /* return 0 if no type declaration. otherwise, return the basic type
2994 and skip it.
2996 static int parse_btype(CType *type, AttributeDef *ad)
2998 int t, u, bt_size, complete, type_found, typespec_found;
2999 Sym *s;
3000 CType type1;
3002 memset(ad, 0, sizeof(AttributeDef));
3003 complete = 0;
3004 type_found = 0;
3005 typespec_found = 0;
3006 t = 0;
3007 while(1) {
3008 switch(tok) {
3009 case TOK_EXTENSION:
3010 /* currently, we really ignore extension */
3011 next();
3012 continue;
3014 /* basic types */
3015 case TOK_CHAR:
3016 u = VT_BYTE;
3017 basic_type:
3018 next();
3019 basic_type1:
3020 if (complete)
3021 tcc_error("too many basic types");
3022 t |= u;
3023 bt_size = is_btype_size (u & VT_BTYPE);
3024 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3025 complete = 1;
3026 typespec_found = 1;
3027 break;
3028 case TOK_VOID:
3029 u = VT_VOID;
3030 goto basic_type;
3031 case TOK_SHORT:
3032 u = VT_SHORT;
3033 goto basic_type;
3034 case TOK_INT:
3035 u = VT_INT;
3036 goto basic_type;
3037 case TOK_LONG:
3038 next();
3039 if ((t & VT_BTYPE) == VT_DOUBLE) {
3040 #ifndef TCC_TARGET_PE
3041 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3042 #endif
3043 } else if ((t & VT_BTYPE) == VT_LONG) {
3044 t = (t & ~VT_BTYPE) | VT_LLONG;
3045 } else {
3046 u = VT_LONG;
3047 goto basic_type1;
3049 break;
3050 case TOK_BOOL:
3051 u = VT_BOOL;
3052 goto basic_type;
3053 case TOK_FLOAT:
3054 u = VT_FLOAT;
3055 goto basic_type;
3056 case TOK_DOUBLE:
3057 next();
3058 if ((t & VT_BTYPE) == VT_LONG) {
3059 #ifdef TCC_TARGET_PE
3060 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3061 #else
3062 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3063 #endif
3064 } else {
3065 u = VT_DOUBLE;
3066 goto basic_type1;
3068 break;
3069 case TOK_ENUM:
3070 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3071 basic_type2:
3072 u = type1.t;
3073 type->ref = type1.ref;
3074 goto basic_type1;
3075 case TOK_STRUCT:
3076 case TOK_UNION:
3077 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3078 goto basic_type2;
3080 /* type modifiers */
3081 case TOK_CONST1:
3082 case TOK_CONST2:
3083 case TOK_CONST3:
3084 t |= VT_CONSTANT;
3085 next();
3086 break;
3087 case TOK_VOLATILE1:
3088 case TOK_VOLATILE2:
3089 case TOK_VOLATILE3:
3090 t |= VT_VOLATILE;
3091 next();
3092 break;
3093 case TOK_SIGNED1:
3094 case TOK_SIGNED2:
3095 case TOK_SIGNED3:
3096 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3097 tcc_error("signed and unsigned modifier");
3098 typespec_found = 1;
3099 t |= VT_DEFSIGN;
3100 next();
3101 break;
3102 case TOK_REGISTER:
3103 case TOK_AUTO:
3104 case TOK_RESTRICT1:
3105 case TOK_RESTRICT2:
3106 case TOK_RESTRICT3:
3107 next();
3108 break;
3109 case TOK_UNSIGNED:
3110 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3111 tcc_error("signed and unsigned modifier");
3112 t |= VT_DEFSIGN | VT_UNSIGNED;
3113 next();
3114 typespec_found = 1;
3115 break;
3117 /* storage */
3118 case TOK_EXTERN:
3119 t |= VT_EXTERN;
3120 next();
3121 break;
3122 case TOK_STATIC:
3123 t |= VT_STATIC;
3124 next();
3125 break;
3126 case TOK_TYPEDEF:
3127 t |= VT_TYPEDEF;
3128 next();
3129 break;
3130 case TOK_INLINE1:
3131 case TOK_INLINE2:
3132 case TOK_INLINE3:
3133 t |= VT_INLINE;
3134 next();
3135 break;
3137 /* GNUC attribute */
3138 case TOK_ATTRIBUTE1:
3139 case TOK_ATTRIBUTE2:
3140 parse_attribute(ad);
3141 if (ad->a.mode) {
3142 u = ad->a.mode -1;
3143 t = (t & ~VT_BTYPE) | u;
3145 break;
3146 /* GNUC typeof */
3147 case TOK_TYPEOF1:
3148 case TOK_TYPEOF2:
3149 case TOK_TYPEOF3:
3150 next();
3151 parse_expr_type(&type1);
3152 /* remove all storage modifiers except typedef */
3153 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3154 goto basic_type2;
3155 default:
3156 if (typespec_found)
3157 goto the_end;
3158 s = sym_find(tok);
3159 if (!s || !(s->type.t & VT_TYPEDEF))
3160 goto the_end;
3161 t |= (s->type.t & ~VT_TYPEDEF);
3162 type->ref = s->type.ref;
3163 if (s->r) {
3164 /* get attributes from typedef */
3165 if (0 == ad->a.aligned)
3166 ad->a.aligned = s->a.aligned;
3167 if (0 == ad->a.func_call)
3168 ad->a.func_call = s->a.func_call;
3169 ad->a.packed |= s->a.packed;
3171 next();
3172 typespec_found = 1;
3173 break;
3175 type_found = 1;
3177 the_end:
3178 if (tcc_state->char_is_unsigned) {
3179 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3180 t |= VT_UNSIGNED;
3183 /* long is never used as type */
3184 if ((t & VT_BTYPE) == VT_LONG)
3185 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3186 t = (t & ~VT_BTYPE) | VT_INT;
3187 #else
3188 t = (t & ~VT_BTYPE) | VT_LLONG;
3189 #endif
3190 type->t = t;
3191 return type_found;
3194 /* convert a function parameter type (array to pointer and function to
3195 function pointer) */
3196 static inline void convert_parameter_type(CType *pt)
3198 /* remove const and volatile qualifiers (XXX: const could be used
3199 to indicate a const function parameter */
3200 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3201 /* array must be transformed to pointer according to ANSI C */
3202 pt->t &= ~VT_ARRAY;
3203 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3204 mk_pointer(pt);
3208 ST_FUNC void parse_asm_str(CString *astr)
3210 skip('(');
3211 /* read the string */
3212 if (tok != TOK_STR)
3213 expect("string constant");
3214 cstr_new(astr);
3215 while (tok == TOK_STR) {
3216 /* XXX: add \0 handling too ? */
3217 cstr_cat(astr, tokc.cstr->data);
3218 next();
3220 cstr_ccat(astr, '\0');
3223 /* Parse an asm label and return the label
3224 * Don't forget to free the CString in the caller! */
3225 static void asm_label_instr(CString *astr)
3227 next();
3228 parse_asm_str(astr);
3229 skip(')');
3230 #ifdef ASM_DEBUG
3231 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3232 #endif
3235 static void post_type(CType *type, AttributeDef *ad)
3237 int n, l, t1, arg_size, align;
3238 Sym **plast, *s, *first;
3239 AttributeDef ad1;
3240 CType pt;
3242 if (tok == '(') {
3243 /* function declaration */
3244 next();
3245 l = 0;
3246 first = NULL;
3247 plast = &first;
3248 arg_size = 0;
3249 if (tok != ')') {
3250 for(;;) {
3251 /* read param name and compute offset */
3252 if (l != FUNC_OLD) {
3253 if (!parse_btype(&pt, &ad1)) {
3254 if (l) {
3255 tcc_error("invalid type");
3256 } else {
3257 l = FUNC_OLD;
3258 goto old_proto;
3261 l = FUNC_NEW;
3262 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3263 break;
3264 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3265 if ((pt.t & VT_BTYPE) == VT_VOID)
3266 tcc_error("parameter declared as void");
3267 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3268 } else {
3269 old_proto:
3270 n = tok;
3271 if (n < TOK_UIDENT)
3272 expect("identifier");
3273 pt.t = VT_INT;
3274 next();
3276 convert_parameter_type(&pt);
3277 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3278 *plast = s;
3279 plast = &s->next;
3280 if (tok == ')')
3281 break;
3282 skip(',');
3283 if (l == FUNC_NEW && tok == TOK_DOTS) {
3284 l = FUNC_ELLIPSIS;
3285 next();
3286 break;
3290 /* if no parameters, then old type prototype */
3291 if (l == 0)
3292 l = FUNC_OLD;
3293 skip(')');
3294 /* NOTE: const is ignored in returned type as it has a special
3295 meaning in gcc / C++ */
3296 type->t &= ~VT_CONSTANT;
3297 /* some ancient pre-K&R C allows a function to return an array
3298 and the array brackets to be put after the arguments, such
3299 that "int c()[]" means something like "int[] c()" */
3300 if (tok == '[') {
3301 next();
3302 skip(']'); /* only handle simple "[]" */
3303 type->t |= VT_PTR;
3305 /* we push a anonymous symbol which will contain the function prototype */
3306 ad->a.func_args = arg_size;
3307 s = sym_push(SYM_FIELD, type, 0, l);
3308 s->a = ad->a;
3309 s->next = first;
3310 type->t = VT_FUNC;
3311 type->ref = s;
3312 } else if (tok == '[') {
3313 /* array definition */
3314 next();
3315 if (tok == TOK_RESTRICT1)
3316 next();
3317 n = -1;
3318 t1 = 0;
3319 if (tok != ']') {
3320 if (!local_stack || nocode_wanted)
3321 vpushi(expr_const());
3322 else gexpr();
3323 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3324 n = vtop->c.i;
3325 if (n < 0)
3326 tcc_error("invalid array size");
3327 } else {
3328 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3329 tcc_error("size of variable length array should be an integer");
3330 t1 = VT_VLA;
3333 skip(']');
3334 /* parse next post type */
3335 post_type(type, ad);
3336 if (type->t == VT_FUNC)
3337 tcc_error("declaration of an array of functions");
3338 t1 |= type->t & VT_VLA;
3340 if (t1 & VT_VLA) {
3341 loc -= type_size(&int_type, &align);
3342 loc &= -align;
3343 n = loc;
3345 vla_runtime_type_size(type, &align);
3346 gen_op('*');
3347 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3348 vswap();
3349 vstore();
3351 if (n != -1)
3352 vpop();
3354 /* we push an anonymous symbol which will contain the array
3355 element type */
3356 s = sym_push(SYM_FIELD, type, 0, n);
3357 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3358 type->ref = s;
3362 /* Parse a type declaration (except basic type), and return the type
3363 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3364 expected. 'type' should contain the basic type. 'ad' is the
3365 attribute definition of the basic type. It can be modified by
3366 type_decl().
3368 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3370 Sym *s;
3371 CType type1, *type2;
3372 int qualifiers, storage;
3374 while (tok == '*') {
3375 qualifiers = 0;
3376 redo:
3377 next();
3378 switch(tok) {
3379 case TOK_CONST1:
3380 case TOK_CONST2:
3381 case TOK_CONST3:
3382 qualifiers |= VT_CONSTANT;
3383 goto redo;
3384 case TOK_VOLATILE1:
3385 case TOK_VOLATILE2:
3386 case TOK_VOLATILE3:
3387 qualifiers |= VT_VOLATILE;
3388 goto redo;
3389 case TOK_RESTRICT1:
3390 case TOK_RESTRICT2:
3391 case TOK_RESTRICT3:
3392 goto redo;
3394 mk_pointer(type);
3395 type->t |= qualifiers;
3398 /* XXX: clarify attribute handling */
3399 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3400 parse_attribute(ad);
3402 /* recursive type */
3403 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3404 type1.t = 0; /* XXX: same as int */
3405 if (tok == '(') {
3406 next();
3407 /* XXX: this is not correct to modify 'ad' at this point, but
3408 the syntax is not clear */
3409 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3410 parse_attribute(ad);
3411 type_decl(&type1, ad, v, td);
3412 skip(')');
3413 } else {
3414 /* type identifier */
3415 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3416 *v = tok;
3417 next();
3418 } else {
3419 if (!(td & TYPE_ABSTRACT))
3420 expect("identifier");
3421 *v = 0;
3424 storage = type->t & VT_STORAGE;
3425 type->t &= ~VT_STORAGE;
3426 if (storage & VT_STATIC) {
3427 int saved_nocode_wanted = nocode_wanted;
3428 nocode_wanted = 1;
3429 post_type(type, ad);
3430 nocode_wanted = saved_nocode_wanted;
3431 } else
3432 post_type(type, ad);
3433 type->t |= storage;
3434 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3435 parse_attribute(ad);
3437 if (!type1.t)
3438 return;
3439 /* append type at the end of type1 */
3440 type2 = &type1;
3441 for(;;) {
3442 s = type2->ref;
3443 type2 = &s->type;
3444 if (!type2->t) {
3445 *type2 = *type;
3446 break;
3449 *type = type1;
3452 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3453 ST_FUNC int lvalue_type(int t)
3455 int bt, r;
3456 r = VT_LVAL;
3457 bt = t & VT_BTYPE;
3458 if (bt == VT_BYTE || bt == VT_BOOL)
3459 r |= VT_LVAL_BYTE;
3460 else if (bt == VT_SHORT)
3461 r |= VT_LVAL_SHORT;
3462 else
3463 return r;
3464 if (t & VT_UNSIGNED)
3465 r |= VT_LVAL_UNSIGNED;
3466 return r;
3469 /* indirection with full error checking and bound check */
3470 ST_FUNC void indir(void)
3472 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3473 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3474 return;
3475 expect("pointer");
3477 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3478 gv(RC_INT);
3479 vtop->type = *pointed_type(&vtop->type);
3480 /* Arrays and functions are never lvalues */
3481 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3482 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3483 vtop->r |= lvalue_type(vtop->type.t);
3484 /* if bound checking, the referenced pointer must be checked */
3485 #ifdef CONFIG_TCC_BCHECK
3486 if (tcc_state->do_bounds_check)
3487 vtop->r |= VT_MUSTBOUND;
3488 #endif
3492 /* pass a parameter to a function and do type checking and casting */
3493 static void gfunc_param_typed(Sym *func, Sym *arg)
3495 int func_type;
3496 CType type;
3498 func_type = func->c;
3499 if (func_type == FUNC_OLD ||
3500 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3501 /* default casting : only need to convert float to double */
3502 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3503 type.t = VT_DOUBLE;
3504 gen_cast(&type);
3505 } else if (vtop->type.t & VT_BITFIELD) {
3506 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3507 gen_cast(&type);
3509 } else if (arg == NULL) {
3510 tcc_error("too many arguments to function");
3511 } else {
3512 type = arg->type;
3513 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3514 gen_assign_cast(&type);
3518 /* parse an expression of the form '(type)' or '(expr)' and return its
3519 type */
3520 static void parse_expr_type(CType *type)
3522 int n;
3523 AttributeDef ad;
3525 skip('(');
3526 if (parse_btype(type, &ad)) {
3527 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3528 } else {
3529 expr_type(type);
3531 skip(')');
3534 static void parse_type(CType *type)
3536 AttributeDef ad;
3537 int n;
3539 if (!parse_btype(type, &ad)) {
3540 expect("type");
3542 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3545 static void vpush_tokc(int t)
3547 CType type;
3548 type.t = t;
3549 type.ref = 0;
3550 vsetc(&type, VT_CONST, &tokc);
3553 ST_FUNC void unary(void)
3555 int n, t, align, size, r, sizeof_caller;
3556 CType type;
3557 Sym *s;
3558 AttributeDef ad;
3559 static int in_sizeof = 0;
3561 sizeof_caller = in_sizeof;
3562 in_sizeof = 0;
3563 /* XXX: GCC 2.95.3 does not generate a table although it should be
3564 better here */
3565 tok_next:
3566 switch(tok) {
3567 case TOK_EXTENSION:
3568 next();
3569 goto tok_next;
3570 case TOK_CINT:
3571 case TOK_CCHAR:
3572 case TOK_LCHAR:
3573 vpushi(tokc.i);
3574 next();
3575 break;
3576 case TOK_CUINT:
3577 vpush_tokc(VT_INT | VT_UNSIGNED);
3578 next();
3579 break;
3580 case TOK_CLLONG:
3581 vpush_tokc(VT_LLONG);
3582 next();
3583 break;
3584 case TOK_CULLONG:
3585 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3586 next();
3587 break;
3588 case TOK_CFLOAT:
3589 vpush_tokc(VT_FLOAT);
3590 next();
3591 break;
3592 case TOK_CDOUBLE:
3593 vpush_tokc(VT_DOUBLE);
3594 next();
3595 break;
3596 case TOK_CLDOUBLE:
3597 vpush_tokc(VT_LDOUBLE);
3598 next();
3599 break;
3600 case TOK___FUNCTION__:
3601 if (!gnu_ext)
3602 goto tok_identifier;
3603 /* fall thru */
3604 case TOK___FUNC__:
3606 void *ptr;
3607 int len;
3608 /* special function name identifier */
3609 len = strlen(funcname) + 1;
3610 /* generate char[len] type */
3611 type.t = VT_BYTE;
3612 mk_pointer(&type);
3613 type.t |= VT_ARRAY;
3614 type.ref->c = len;
3615 vpush_ref(&type, data_section, data_section->data_offset, len);
3616 ptr = section_ptr_add(data_section, len);
3617 memcpy(ptr, funcname, len);
3618 next();
3620 break;
3621 case TOK_LSTR:
3622 #ifdef TCC_TARGET_PE
3623 t = VT_SHORT | VT_UNSIGNED;
3624 #else
3625 t = VT_INT;
3626 #endif
3627 goto str_init;
3628 case TOK_STR:
3629 /* string parsing */
3630 t = VT_BYTE;
3631 str_init:
3632 if (tcc_state->warn_write_strings)
3633 t |= VT_CONSTANT;
3634 type.t = t;
3635 mk_pointer(&type);
3636 type.t |= VT_ARRAY;
3637 memset(&ad, 0, sizeof(AttributeDef));
3638 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3639 break;
3640 case '(':
3641 next();
3642 /* cast ? */
3643 if (parse_btype(&type, &ad)) {
3644 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3645 skip(')');
3646 /* check ISOC99 compound literal */
3647 if (tok == '{') {
3648 /* data is allocated locally by default */
3649 if (global_expr)
3650 r = VT_CONST;
3651 else
3652 r = VT_LOCAL;
3653 /* all except arrays are lvalues */
3654 if (!(type.t & VT_ARRAY))
3655 r |= lvalue_type(type.t);
3656 memset(&ad, 0, sizeof(AttributeDef));
3657 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3658 } else {
3659 if (sizeof_caller) {
3660 vpush(&type);
3661 return;
3663 unary();
3664 gen_cast(&type);
3666 } else if (tok == '{') {
3667 /* save all registers */
3668 save_regs(0);
3669 /* statement expression : we do not accept break/continue
3670 inside as GCC does */
3671 block(NULL, NULL, NULL, NULL, 0, 1);
3672 skip(')');
3673 } else {
3674 gexpr();
3675 skip(')');
3677 break;
3678 case '*':
3679 next();
3680 unary();
3681 indir();
3682 break;
3683 case '&':
3684 next();
3685 unary();
3686 /* functions names must be treated as function pointers,
3687 except for unary '&' and sizeof. Since we consider that
3688 functions are not lvalues, we only have to handle it
3689 there and in function calls. */
3690 /* arrays can also be used although they are not lvalues */
3691 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3692 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3693 test_lvalue();
3694 mk_pointer(&vtop->type);
3695 gaddrof();
3696 break;
3697 case '!':
3698 next();
3699 unary();
3700 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3701 CType boolean;
3702 boolean.t = VT_BOOL;
3703 gen_cast(&boolean);
3704 vtop->c.i = !vtop->c.i;
3705 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3706 vtop->c.i = vtop->c.i ^ 1;
3707 else {
3708 save_regs(1);
3709 vseti(VT_JMP, gvtst(1, 0));
3711 break;
3712 case '~':
3713 next();
3714 unary();
3715 vpushi(-1);
3716 gen_op('^');
3717 break;
3718 case '+':
3719 next();
3720 unary();
3721 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3722 tcc_error("pointer not accepted for unary plus");
3723 /* In order to force cast, we add zero, except for floating point
3724 where we really need an noop (otherwise -0.0 will be transformed
3725 into +0.0). */
3726 if (!is_float(vtop->type.t)) {
3727 vpushi(0);
3728 gen_op('+');
3730 break;
3731 case TOK_SIZEOF:
3732 case TOK_ALIGNOF1:
3733 case TOK_ALIGNOF2:
3734 t = tok;
3735 next();
3736 in_sizeof++;
3737 unary_type(&type); // Perform a in_sizeof = 0;
3738 size = type_size(&type, &align);
3739 if (t == TOK_SIZEOF) {
3740 if (!(type.t & VT_VLA)) {
3741 if (size < 0)
3742 tcc_error("sizeof applied to an incomplete type");
3743 vpushs(size);
3744 } else {
3745 vla_runtime_type_size(&type, &align);
3747 } else {
3748 vpushs(align);
3750 vtop->type.t |= VT_UNSIGNED;
3751 break;
3753 case TOK_builtin_types_compatible_p:
3755 CType type1, type2;
3756 next();
3757 skip('(');
3758 parse_type(&type1);
3759 skip(',');
3760 parse_type(&type2);
3761 skip(')');
3762 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3763 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3764 vpushi(is_compatible_types(&type1, &type2));
3766 break;
3767 case TOK_builtin_constant_p:
3769 int saved_nocode_wanted, res;
3770 next();
3771 skip('(');
3772 saved_nocode_wanted = nocode_wanted;
3773 nocode_wanted = 1;
3774 gexpr();
3775 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3776 vpop();
3777 nocode_wanted = saved_nocode_wanted;
3778 skip(')');
3779 vpushi(res);
3781 break;
3782 case TOK_builtin_frame_address:
3784 int level;
3785 CType type;
3786 next();
3787 skip('(');
3788 if (tok != TOK_CINT || tokc.i < 0) {
3789 tcc_error("__builtin_frame_address only takes positive integers");
3791 level = tokc.i;
3792 next();
3793 skip(')');
3794 type.t = VT_VOID;
3795 mk_pointer(&type);
3796 vset(&type, VT_LOCAL, 0); /* local frame */
3797 while (level--) {
3798 mk_pointer(&vtop->type);
3799 indir(); /* -> parent frame */
3802 break;
3803 #ifdef TCC_TARGET_X86_64
3804 #ifdef TCC_TARGET_PE
3805 case TOK_builtin_va_start:
3807 next();
3808 skip('(');
3809 expr_eq();
3810 skip(',');
3811 expr_eq();
3812 skip(')');
3813 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3814 tcc_error("__builtin_va_start expects a local variable");
3815 vtop->r &= ~(VT_LVAL | VT_REF);
3816 vtop->type = char_pointer_type;
3817 vstore();
3819 break;
3820 #else
3821 case TOK_builtin_va_arg_types:
3823 CType type;
3824 next();
3825 skip('(');
3826 parse_type(&type);
3827 skip(')');
3828 vpushi(classify_x86_64_va_arg(&type));
3830 break;
3831 #endif
3832 #endif
3833 case TOK_INC:
3834 case TOK_DEC:
3835 t = tok;
3836 next();
3837 unary();
3838 inc(0, t);
3839 break;
3840 case '-':
3841 next();
3842 unary();
3843 t = vtop->type.t & VT_BTYPE;
3844 if (is_float(t)) {
3845 /* In IEEE negate(x) isn't subtract(0,x), but rather
3846 subtract(-0, x). */
3847 vpush(&vtop->type);
3848 if (t == VT_FLOAT)
3849 vtop->c.f = -0.0f;
3850 else if (t == VT_DOUBLE)
3851 vtop->c.d = -0.0;
3852 else
3853 vtop->c.ld = -0.0;
3854 } else
3855 vpushi(0);
3856 vswap();
3857 gen_op('-');
3858 break;
3859 case TOK_LAND:
3860 if (!gnu_ext)
3861 goto tok_identifier;
3862 next();
3863 /* allow to take the address of a label */
3864 if (tok < TOK_UIDENT)
3865 expect("label identifier");
3866 s = label_find(tok);
3867 if (!s) {
3868 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3869 } else {
3870 if (s->r == LABEL_DECLARED)
3871 s->r = LABEL_FORWARD;
3873 if (!s->type.t) {
3874 s->type.t = VT_VOID;
3875 mk_pointer(&s->type);
3876 s->type.t |= VT_STATIC;
3878 vpushsym(&s->type, s);
3879 next();
3880 break;
3882 // special qnan , snan and infinity values
3883 case TOK___NAN__:
3884 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3885 next();
3886 break;
3887 case TOK___SNAN__:
3888 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3889 next();
3890 break;
3891 case TOK___INF__:
3892 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3893 next();
3894 break;
3896 default:
3897 tok_identifier:
3898 t = tok;
3899 next();
3900 if (t < TOK_UIDENT)
3901 expect("identifier");
3902 s = sym_find(t);
3903 if (!s) {
3904 const char *name = get_tok_str(t, NULL);
3905 if (tok != '(')
3906 tcc_error("'%s' undeclared", name);
3907 /* for simple function calls, we tolerate undeclared
3908 external reference to int() function */
3909 if (tcc_state->warn_implicit_function_declaration
3910 #ifdef TCC_TARGET_PE
3911 /* people must be warned about using undeclared WINAPI functions
3912 (which usually start with uppercase letter) */
3913 || (name[0] >= 'A' && name[0] <= 'Z')
3914 #endif
3916 tcc_warning("implicit declaration of function '%s'", name);
3917 s = external_global_sym(t, &func_old_type, 0);
3919 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3920 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3921 /* if referencing an inline function, then we generate a
3922 symbol to it if not already done. It will have the
3923 effect to generate code for it at the end of the
3924 compilation unit. Inline function as always
3925 generated in the text section. */
3926 if (!s->c)
3927 put_extern_sym(s, text_section, 0, 0);
3928 r = VT_SYM | VT_CONST;
3929 } else {
3930 r = s->r;
3932 vset(&s->type, r, s->c);
3933 /* if forward reference, we must point to s */
3934 if (vtop->r & VT_SYM) {
3935 vtop->sym = s;
3936 vtop->c.ptr_offset = 0;
3938 break;
3941 /* post operations */
3942 while (1) {
3943 if (tok == TOK_INC || tok == TOK_DEC) {
3944 inc(1, tok);
3945 next();
3946 } else if (tok == '.' || tok == TOK_ARROW) {
3947 int qualifiers;
3948 /* field */
3949 if (tok == TOK_ARROW)
3950 indir();
3951 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3952 test_lvalue();
3953 gaddrof();
3954 next();
3955 /* expect pointer on structure */
3956 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3957 expect("struct or union");
3958 s = vtop->type.ref;
3959 /* find field */
3960 tok |= SYM_FIELD;
3961 while ((s = s->next) != NULL) {
3962 if (s->v == tok)
3963 break;
3965 if (!s)
3966 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3967 /* add field offset to pointer */
3968 vtop->type = char_pointer_type; /* change type to 'char *' */
3969 vpushi(s->c);
3970 gen_op('+');
3971 /* change type to field type, and set to lvalue */
3972 vtop->type = s->type;
3973 vtop->type.t |= qualifiers;
3974 /* an array is never an lvalue */
3975 if (!(vtop->type.t & VT_ARRAY)) {
3976 vtop->r |= lvalue_type(vtop->type.t);
3977 #ifdef CONFIG_TCC_BCHECK
3978 /* if bound checking, the referenced pointer must be checked */
3979 if (tcc_state->do_bounds_check)
3980 vtop->r |= VT_MUSTBOUND;
3981 #endif
3983 next();
3984 } else if (tok == '[') {
3985 next();
3986 gexpr();
3987 gen_op('+');
3988 indir();
3989 skip(']');
3990 } else if (tok == '(') {
3991 SValue ret;
3992 Sym *sa;
3993 int nb_args, ret_nregs, ret_align, variadic;
3995 /* function call */
3996 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3997 /* pointer test (no array accepted) */
3998 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3999 vtop->type = *pointed_type(&vtop->type);
4000 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4001 goto error_func;
4002 } else {
4003 error_func:
4004 expect("function pointer");
4006 } else {
4007 vtop->r &= ~VT_LVAL; /* no lvalue */
4009 /* get return type */
4010 s = vtop->type.ref;
4011 next();
4012 sa = s->next; /* first parameter */
4013 nb_args = 0;
4014 ret.r2 = VT_CONST;
4015 /* compute first implicit argument if a structure is returned */
4016 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4017 variadic = (s->c == FUNC_ELLIPSIS);
4018 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4019 &ret_align);
4020 if (!ret_nregs) {
4021 /* get some space for the returned structure */
4022 size = type_size(&s->type, &align);
4023 loc = (loc - size) & -align;
4024 ret.type = s->type;
4025 ret.r = VT_LOCAL | VT_LVAL;
4026 /* pass it as 'int' to avoid structure arg passing
4027 problems */
4028 vseti(VT_LOCAL, loc);
4029 ret.c = vtop->c;
4030 nb_args++;
4032 } else {
4033 ret_nregs = 1;
4034 ret.type = s->type;
4037 if (ret_nregs) {
4038 /* return in register */
4039 if (is_float(ret.type.t)) {
4040 ret.r = reg_fret(ret.type.t);
4041 #ifdef TCC_TARGET_X86_64
4042 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4043 ret.r2 = REG_QRET;
4044 #endif
4045 } else {
4046 #ifdef TCC_TARGET_X86_64
4047 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4048 #else
4049 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4050 #endif
4051 ret.r2 = REG_LRET;
4052 ret.r = REG_IRET;
4054 ret.c.i = 0;
4056 if (tok != ')') {
4057 for(;;) {
4058 expr_eq();
4059 gfunc_param_typed(s, sa);
4060 nb_args++;
4061 if (sa)
4062 sa = sa->next;
4063 if (tok == ')')
4064 break;
4065 skip(',');
4068 if (sa)
4069 tcc_error("too few arguments to function");
4070 skip(')');
4071 if (!nocode_wanted) {
4072 gfunc_call(nb_args);
4073 } else {
4074 vtop -= (nb_args + 1);
4077 /* return value */
4078 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4079 vsetc(&ret.type, r, &ret.c);
4080 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4083 /* handle packed struct return */
4084 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4085 int addr, offset;
4087 size = type_size(&s->type, &align);
4088 loc = (loc - size) & -align;
4089 addr = loc;
4090 offset = 0;
4091 for (;;) {
4092 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4093 vswap();
4094 vstore();
4095 vtop--;
4096 if (--ret_nregs == 0)
4097 break;
4098 /* XXX: compatible with arm only: ret_align == register_size */
4099 offset += ret_align;
4101 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4103 } else {
4104 break;
4109 ST_FUNC void expr_prod(void)
4111 int t;
4113 unary();
4114 while (tok == '*' || tok == '/' || tok == '%') {
4115 t = tok;
4116 next();
4117 unary();
4118 gen_op(t);
4122 ST_FUNC void expr_sum(void)
4124 int t;
4126 expr_prod();
4127 while (tok == '+' || tok == '-') {
4128 t = tok;
4129 next();
4130 expr_prod();
4131 gen_op(t);
4135 static void expr_shift(void)
4137 int t;
4139 expr_sum();
4140 while (tok == TOK_SHL || tok == TOK_SAR) {
4141 t = tok;
4142 next();
4143 expr_sum();
4144 gen_op(t);
4148 static void expr_cmp(void)
4150 int t;
4152 expr_shift();
4153 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4154 tok == TOK_ULT || tok == TOK_UGE) {
4155 t = tok;
4156 next();
4157 expr_shift();
4158 gen_op(t);
4162 static void expr_cmpeq(void)
4164 int t;
4166 expr_cmp();
4167 while (tok == TOK_EQ || tok == TOK_NE) {
4168 t = tok;
4169 next();
4170 expr_cmp();
4171 gen_op(t);
4175 static void expr_and(void)
4177 expr_cmpeq();
4178 while (tok == '&') {
4179 next();
4180 expr_cmpeq();
4181 gen_op('&');
4185 static void expr_xor(void)
4187 expr_and();
4188 while (tok == '^') {
4189 next();
4190 expr_and();
4191 gen_op('^');
4195 static void expr_or(void)
4197 expr_xor();
4198 while (tok == '|') {
4199 next();
4200 expr_xor();
4201 gen_op('|');
4205 /* XXX: fix this mess */
4206 static void expr_land_const(void)
4208 expr_or();
4209 while (tok == TOK_LAND) {
4210 next();
4211 expr_or();
4212 gen_op(TOK_LAND);
4216 /* XXX: fix this mess */
4217 static void expr_lor_const(void)
4219 expr_land_const();
4220 while (tok == TOK_LOR) {
4221 next();
4222 expr_land_const();
4223 gen_op(TOK_LOR);
4227 /* only used if non constant */
4228 static void expr_land(void)
4230 int t;
4232 expr_or();
4233 if (tok == TOK_LAND) {
4234 t = 0;
4235 save_regs(1);
4236 for(;;) {
4237 t = gvtst(1, t);
4238 if (tok != TOK_LAND) {
4239 vseti(VT_JMPI, t);
4240 break;
4242 next();
4243 expr_or();
4248 static void expr_lor(void)
4250 int t;
4252 expr_land();
4253 if (tok == TOK_LOR) {
4254 t = 0;
4255 save_regs(1);
4256 for(;;) {
4257 t = gvtst(0, t);
4258 if (tok != TOK_LOR) {
4259 vseti(VT_JMP, t);
4260 break;
4262 next();
4263 expr_land();
4268 /* XXX: better constant handling */
4269 static void expr_cond(void)
4271 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4272 SValue sv;
4273 CType type, type1, type2;
4275 if (const_wanted) {
4276 expr_lor_const();
4277 if (tok == '?') {
4278 CType boolean;
4279 int c;
4280 boolean.t = VT_BOOL;
4281 vdup();
4282 gen_cast(&boolean);
4283 c = vtop->c.i;
4284 vpop();
4285 next();
4286 if (tok != ':' || !gnu_ext) {
4287 vpop();
4288 gexpr();
4290 if (!c)
4291 vpop();
4292 skip(':');
4293 expr_cond();
4294 if (c)
4295 vpop();
4297 } else {
4298 expr_lor();
4299 if (tok == '?') {
4300 next();
4301 if (vtop != vstack) {
4302 /* needed to avoid having different registers saved in
4303 each branch */
4304 if (is_float(vtop->type.t)) {
4305 rc = RC_FLOAT;
4306 #ifdef TCC_TARGET_X86_64
4307 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4308 rc = RC_ST0;
4310 #endif
4312 else
4313 rc = RC_INT;
4314 gv(rc);
4315 save_regs(1);
4317 if (tok == ':' && gnu_ext) {
4318 gv_dup();
4319 tt = gvtst(1, 0);
4320 } else {
4321 tt = gvtst(1, 0);
4322 gexpr();
4324 type1 = vtop->type;
4325 sv = *vtop; /* save value to handle it later */
4326 vtop--; /* no vpop so that FP stack is not flushed */
4327 skip(':');
4328 u = gjmp(0);
4329 gsym(tt);
4330 expr_cond();
4331 type2 = vtop->type;
4333 t1 = type1.t;
4334 bt1 = t1 & VT_BTYPE;
4335 t2 = type2.t;
4336 bt2 = t2 & VT_BTYPE;
4337 /* cast operands to correct type according to ISOC rules */
4338 if (is_float(bt1) || is_float(bt2)) {
4339 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4340 type.t = VT_LDOUBLE;
4341 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4342 type.t = VT_DOUBLE;
4343 } else {
4344 type.t = VT_FLOAT;
4346 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4347 /* cast to biggest op */
4348 type.t = VT_LLONG;
4349 /* convert to unsigned if it does not fit in a long long */
4350 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4351 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4352 type.t |= VT_UNSIGNED;
4353 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4354 /* If one is a null ptr constant the result type
4355 is the other. */
4356 if (is_null_pointer (vtop))
4357 type = type1;
4358 else if (is_null_pointer (&sv))
4359 type = type2;
4360 /* XXX: test pointer compatibility, C99 has more elaborate
4361 rules here. */
4362 else
4363 type = type1;
4364 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4365 /* XXX: test function pointer compatibility */
4366 type = bt1 == VT_FUNC ? type1 : type2;
4367 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4368 /* XXX: test structure compatibility */
4369 type = bt1 == VT_STRUCT ? type1 : type2;
4370 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4371 /* NOTE: as an extension, we accept void on only one side */
4372 type.t = VT_VOID;
4373 } else {
4374 /* integer operations */
4375 type.t = VT_INT;
4376 /* convert to unsigned if it does not fit in an integer */
4377 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4378 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4379 type.t |= VT_UNSIGNED;
4382 /* now we convert second operand */
4383 gen_cast(&type);
4384 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4385 gaddrof();
4386 rc = RC_INT;
4387 if (is_float(type.t)) {
4388 rc = RC_FLOAT;
4389 #ifdef TCC_TARGET_X86_64
4390 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4391 rc = RC_ST0;
4393 #endif
4394 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4395 /* for long longs, we use fixed registers to avoid having
4396 to handle a complicated move */
4397 rc = RC_IRET;
4400 r2 = gv(rc);
4401 /* this is horrible, but we must also convert first
4402 operand */
4403 tt = gjmp(0);
4404 gsym(u);
4405 /* put again first value and cast it */
4406 *vtop = sv;
4407 gen_cast(&type);
4408 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4409 gaddrof();
4410 r1 = gv(rc);
4411 move_reg(r2, r1, type.t);
4412 vtop->r = r2;
4413 gsym(tt);
4418 static void expr_eq(void)
4420 int t;
4422 expr_cond();
4423 if (tok == '=' ||
4424 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4425 tok == TOK_A_XOR || tok == TOK_A_OR ||
4426 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4427 test_lvalue();
4428 t = tok;
4429 next();
4430 if (t == '=') {
4431 expr_eq();
4432 } else {
4433 vdup();
4434 expr_eq();
4435 gen_op(t & 0x7f);
4437 vstore();
4441 ST_FUNC void gexpr(void)
4443 while (1) {
4444 expr_eq();
4445 if (tok != ',')
4446 break;
4447 vpop();
4448 next();
4452 /* parse an expression and return its type without any side effect. */
4453 static void expr_type(CType *type)
4455 int saved_nocode_wanted;
4457 saved_nocode_wanted = nocode_wanted;
4458 nocode_wanted = 1;
4459 gexpr();
4460 *type = vtop->type;
4461 vpop();
4462 nocode_wanted = saved_nocode_wanted;
4465 /* parse a unary expression and return its type without any side
4466 effect. */
4467 static void unary_type(CType *type)
4469 int a;
4471 a = nocode_wanted;
4472 nocode_wanted = 1;
4473 unary();
4474 *type = vtop->type;
4475 vpop();
4476 nocode_wanted = a;
4479 /* parse a constant expression and return value in vtop. */
4480 static void expr_const1(void)
4482 int a;
4483 a = const_wanted;
4484 const_wanted = 1;
4485 expr_cond();
4486 const_wanted = a;
4489 /* parse an integer constant and return its value. */
4490 ST_FUNC int expr_const(void)
4492 int c;
4493 expr_const1();
4494 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4495 expect("constant expression");
4496 c = vtop->c.i;
4497 vpop();
4498 return c;
4501 /* return the label token if current token is a label, otherwise
4502 return zero */
4503 static int is_label(void)
4505 int last_tok;
4507 /* fast test first */
4508 if (tok < TOK_UIDENT)
4509 return 0;
4510 /* no need to save tokc because tok is an identifier */
4511 last_tok = tok;
4512 next();
4513 if (tok == ':') {
4514 next();
4515 return last_tok;
4516 } else {
4517 unget_tok(last_tok);
4518 return 0;
4522 static void label_or_decl(int l)
4524 int last_tok;
4526 /* fast test first */
4527 if (tok >= TOK_UIDENT)
4529 /* no need to save tokc because tok is an identifier */
4530 last_tok = tok;
4531 next();
4532 if (tok == ':') {
4533 unget_tok(last_tok);
4534 return;
4536 unget_tok(last_tok);
4538 decl(l);
4541 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4542 int case_reg, int is_expr)
4544 int a, b, c, d;
4545 Sym *s, *frame_bottom;
4547 /* generate line number info */
4548 if (tcc_state->do_debug &&
4549 (last_line_num != file->line_num || last_ind != ind)) {
4550 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4551 last_ind = ind;
4552 last_line_num = file->line_num;
4555 if (is_expr) {
4556 /* default return value is (void) */
4557 vpushi(0);
4558 vtop->type.t = VT_VOID;
4561 if (tok == TOK_IF) {
4562 /* if test */
4563 next();
4564 skip('(');
4565 gexpr();
4566 skip(')');
4567 a = gvtst(1, 0);
4568 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4569 c = tok;
4570 if (c == TOK_ELSE) {
4571 next();
4572 d = gjmp(0);
4573 gsym(a);
4574 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4575 gsym(d); /* patch else jmp */
4576 } else
4577 gsym(a);
4578 } else if (tok == TOK_WHILE) {
4579 next();
4580 d = ind;
4581 skip('(');
4582 gexpr();
4583 skip(')');
4584 a = gvtst(1, 0);
4585 b = 0;
4586 block(&a, &b, case_sym, def_sym, case_reg, 0);
4587 gjmp_addr(d);
4588 gsym(a);
4589 gsym_addr(b, d);
4590 } else if (tok == '{') {
4591 Sym *llabel;
4592 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4594 next();
4595 /* record local declaration stack position */
4596 s = local_stack;
4597 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4598 frame_bottom->next = scope_stack_bottom;
4599 scope_stack_bottom = frame_bottom;
4600 llabel = local_label_stack;
4602 /* save VLA state */
4603 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4604 if (saved_vla_sp_loc != &vla_sp_root_loc)
4605 vla_sp_loc = &block_vla_sp_loc;
4607 saved_vla_flags = vla_flags;
4608 vla_flags |= VLA_NEED_NEW_FRAME;
4610 /* handle local labels declarations */
4611 if (tok == TOK_LABEL) {
4612 next();
4613 for(;;) {
4614 if (tok < TOK_UIDENT)
4615 expect("label identifier");
4616 label_push(&local_label_stack, tok, LABEL_DECLARED);
4617 next();
4618 if (tok == ',') {
4619 next();
4620 } else {
4621 skip(';');
4622 break;
4626 while (tok != '}') {
4627 label_or_decl(VT_LOCAL);
4628 if (tok != '}') {
4629 if (is_expr)
4630 vpop();
4631 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4634 /* pop locally defined labels */
4635 label_pop(&local_label_stack, llabel);
4636 if(is_expr) {
4637 /* XXX: this solution makes only valgrind happy...
4638 triggered by gcc.c-torture/execute/20000917-1.c */
4639 Sym *p;
4640 switch(vtop->type.t & VT_BTYPE) {
4641 case VT_PTR:
4642 case VT_STRUCT:
4643 case VT_ENUM:
4644 case VT_FUNC:
4645 for(p=vtop->type.ref;p;p=p->prev)
4646 if(p->prev==s)
4647 tcc_error("unsupported expression type");
4650 /* pop locally defined symbols */
4651 scope_stack_bottom = scope_stack_bottom->next;
4652 sym_pop(&local_stack, s);
4654 /* Pop VLA frames and restore stack pointer if required */
4655 if (saved_vla_sp_loc != &vla_sp_root_loc)
4656 *saved_vla_sp_loc = block_vla_sp_loc;
4657 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4658 vla_sp_loc = saved_vla_sp_loc;
4659 gen_vla_sp_restore(*vla_sp_loc);
4661 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4663 next();
4664 } else if (tok == TOK_RETURN) {
4665 next();
4666 if (tok != ';') {
4667 gexpr();
4668 gen_assign_cast(&func_vt);
4669 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4670 CType type, ret_type;
4671 int ret_align, ret_nregs;
4672 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4673 &ret_align);
4674 if (0 == ret_nregs) {
4675 /* if returning structure, must copy it to implicit
4676 first pointer arg location */
4677 type = func_vt;
4678 mk_pointer(&type);
4679 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4680 indir();
4681 vswap();
4682 /* copy structure value to pointer */
4683 vstore();
4684 } else {
4685 /* returning structure packed into registers */
4686 int r, size, addr, align;
4687 size = type_size(&func_vt,&align);
4688 if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1)))
4689 && (align & (ret_align-1))) {
4690 loc = (loc - size) & -align;
4691 addr = loc;
4692 type = func_vt;
4693 vset(&type, VT_LOCAL | VT_LVAL, addr);
4694 vswap();
4695 vstore();
4696 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
4698 vtop->type = ret_type;
4699 if (is_float(ret_type.t))
4700 r = rc_fret(ret_type.t);
4701 else
4702 r = RC_IRET;
4704 for (;;) {
4705 gv(r);
4706 if (--ret_nregs == 0)
4707 break;
4708 /* We assume that when a structure is returned in multiple
4709 registers, their classes are consecutive values of the
4710 suite s(n) = 2^n */
4711 r <<= 1;
4712 /* XXX: compatible with arm only: ret_align == register_size */
4713 vtop->c.i += ret_align;
4714 vtop->r = VT_LOCAL | VT_LVAL;
4717 } else if (is_float(func_vt.t)) {
4718 gv(rc_fret(func_vt.t));
4719 } else {
4720 gv(RC_IRET);
4722 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4724 skip(';');
4725 rsym = gjmp(rsym); /* jmp */
4726 } else if (tok == TOK_BREAK) {
4727 /* compute jump */
4728 if (!bsym)
4729 tcc_error("cannot break");
4730 *bsym = gjmp(*bsym);
4731 next();
4732 skip(';');
4733 } else if (tok == TOK_CONTINUE) {
4734 /* compute jump */
4735 if (!csym)
4736 tcc_error("cannot continue");
4737 *csym = gjmp(*csym);
4738 next();
4739 skip(';');
4740 } else if (tok == TOK_FOR) {
4741 int e;
4742 next();
4743 skip('(');
4744 s = local_stack;
4745 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4746 frame_bottom->next = scope_stack_bottom;
4747 scope_stack_bottom = frame_bottom;
4748 if (tok != ';') {
4749 /* c99 for-loop init decl? */
4750 if (!decl0(VT_LOCAL, 1)) {
4751 /* no, regular for-loop init expr */
4752 gexpr();
4753 vpop();
4756 skip(';');
4757 d = ind;
4758 c = ind;
4759 a = 0;
4760 b = 0;
4761 if (tok != ';') {
4762 gexpr();
4763 a = gvtst(1, 0);
4765 skip(';');
4766 if (tok != ')') {
4767 e = gjmp(0);
4768 c = ind;
4769 gexpr();
4770 vpop();
4771 gjmp_addr(d);
4772 gsym(e);
4774 skip(')');
4775 block(&a, &b, case_sym, def_sym, case_reg, 0);
4776 gjmp_addr(c);
4777 gsym(a);
4778 gsym_addr(b, c);
4779 scope_stack_bottom = scope_stack_bottom->next;
4780 sym_pop(&local_stack, s);
4781 } else
4782 if (tok == TOK_DO) {
4783 next();
4784 a = 0;
4785 b = 0;
4786 d = ind;
4787 block(&a, &b, case_sym, def_sym, case_reg, 0);
4788 skip(TOK_WHILE);
4789 skip('(');
4790 gsym(b);
4791 gexpr();
4792 c = gvtst(0, 0);
4793 gsym_addr(c, d);
4794 skip(')');
4795 gsym(a);
4796 skip(';');
4797 } else
4798 if (tok == TOK_SWITCH) {
4799 next();
4800 skip('(');
4801 gexpr();
4802 /* XXX: other types than integer */
4803 case_reg = gv(RC_INT);
4804 vpop();
4805 skip(')');
4806 a = 0;
4807 b = gjmp(0); /* jump to first case */
4808 c = 0;
4809 block(&a, csym, &b, &c, case_reg, 0);
4810 /* if no default, jmp after switch */
4811 if (c == 0)
4812 c = ind;
4813 /* default label */
4814 gsym_addr(b, c);
4815 /* break label */
4816 gsym(a);
4817 } else
4818 if (tok == TOK_CASE) {
4819 int v1, v2;
4820 if (!case_sym)
4821 expect("switch");
4822 next();
4823 v1 = expr_const();
4824 v2 = v1;
4825 if (gnu_ext && tok == TOK_DOTS) {
4826 next();
4827 v2 = expr_const();
4828 if (v2 < v1)
4829 tcc_warning("empty case range");
4831 /* since a case is like a label, we must skip it with a jmp */
4832 b = gjmp(0);
4833 gsym(*case_sym);
4834 vseti(case_reg, 0);
4835 vpushi(v1);
4836 if (v1 == v2) {
4837 gen_op(TOK_EQ);
4838 *case_sym = gtst(1, 0);
4839 } else {
4840 gen_op(TOK_GE);
4841 *case_sym = gtst(1, 0);
4842 vseti(case_reg, 0);
4843 vpushi(v2);
4844 gen_op(TOK_LE);
4845 *case_sym = gtst(1, *case_sym);
4847 gsym(b);
4848 skip(':');
4849 is_expr = 0;
4850 goto block_after_label;
4851 } else
4852 if (tok == TOK_DEFAULT) {
4853 next();
4854 skip(':');
4855 if (!def_sym)
4856 expect("switch");
4857 if (*def_sym)
4858 tcc_error("too many 'default'");
4859 *def_sym = ind;
4860 is_expr = 0;
4861 goto block_after_label;
4862 } else
4863 if (tok == TOK_GOTO) {
4864 next();
4865 if (tok == '*' && gnu_ext) {
4866 /* computed goto */
4867 next();
4868 gexpr();
4869 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4870 expect("pointer");
4871 ggoto();
4872 } else if (tok >= TOK_UIDENT) {
4873 s = label_find(tok);
4874 /* put forward definition if needed */
4875 if (!s) {
4876 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4877 } else {
4878 if (s->r == LABEL_DECLARED)
4879 s->r = LABEL_FORWARD;
4881 /* label already defined */
4882 if (vla_flags & VLA_IN_SCOPE) {
4883 /* If VLAs are in use, save the current stack pointer and
4884 reset the stack pointer to what it was at function entry
4885 (label will restore stack pointer in inner scopes) */
4886 vla_sp_save();
4887 gen_vla_sp_restore(vla_sp_root_loc);
4889 if (s->r & LABEL_FORWARD)
4890 s->jnext = gjmp(s->jnext);
4891 else
4892 gjmp_addr(s->jnext);
4893 next();
4894 } else {
4895 expect("label identifier");
4897 skip(';');
4898 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4899 asm_instr();
4900 } else {
4901 b = is_label();
4902 if (b) {
4903 /* label case */
4904 if (vla_flags & VLA_IN_SCOPE) {
4905 /* save/restore stack pointer across label
4906 this is a no-op when combined with the load immediately
4907 after the label unless we arrive via goto */
4908 vla_sp_save();
4910 s = label_find(b);
4911 if (s) {
4912 if (s->r == LABEL_DEFINED)
4913 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4914 gsym(s->jnext);
4915 s->r = LABEL_DEFINED;
4916 } else {
4917 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4919 s->jnext = ind;
4920 if (vla_flags & VLA_IN_SCOPE) {
4921 gen_vla_sp_restore(*vla_sp_loc);
4922 vla_flags |= VLA_NEED_NEW_FRAME;
4924 /* we accept this, but it is a mistake */
4925 block_after_label:
4926 if (tok == '}') {
4927 tcc_warning("deprecated use of label at end of compound statement");
4928 } else {
4929 if (is_expr)
4930 vpop();
4931 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4933 } else {
4934 /* expression case */
4935 if (tok != ';') {
4936 if (is_expr) {
4937 vpop();
4938 gexpr();
4939 } else {
4940 gexpr();
4941 vpop();
4944 skip(';');
4949 /* t is the array or struct type. c is the array or struct
4950 address. cur_index/cur_field is the pointer to the current
4951 value. 'size_only' is true if only size info is needed (only used
4952 in arrays) */
4953 static void decl_designator(CType *type, Section *sec, unsigned long c,
4954 int *cur_index, Sym **cur_field,
4955 int size_only)
4957 Sym *s, *f;
4958 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4959 CType type1;
4961 notfirst = 0;
4962 elem_size = 0;
4963 nb_elems = 1;
4964 if (gnu_ext && (l = is_label()) != 0)
4965 goto struct_field;
4966 while (tok == '[' || tok == '.') {
4967 if (tok == '[') {
4968 if (!(type->t & VT_ARRAY))
4969 expect("array type");
4970 s = type->ref;
4971 next();
4972 index = expr_const();
4973 if (index < 0 || (s->c >= 0 && index >= s->c))
4974 expect("invalid index");
4975 if (tok == TOK_DOTS && gnu_ext) {
4976 next();
4977 index_last = expr_const();
4978 if (index_last < 0 ||
4979 (s->c >= 0 && index_last >= s->c) ||
4980 index_last < index)
4981 expect("invalid index");
4982 } else {
4983 index_last = index;
4985 skip(']');
4986 if (!notfirst)
4987 *cur_index = index_last;
4988 type = pointed_type(type);
4989 elem_size = type_size(type, &align);
4990 c += index * elem_size;
4991 /* NOTE: we only support ranges for last designator */
4992 nb_elems = index_last - index + 1;
4993 if (nb_elems != 1) {
4994 notfirst = 1;
4995 break;
4997 } else {
4998 next();
4999 l = tok;
5000 next();
5001 struct_field:
5002 if ((type->t & VT_BTYPE) != VT_STRUCT)
5003 expect("struct/union type");
5004 s = type->ref;
5005 l |= SYM_FIELD;
5006 f = s->next;
5007 while (f) {
5008 if (f->v == l)
5009 break;
5010 f = f->next;
5012 if (!f)
5013 expect("field");
5014 if (!notfirst)
5015 *cur_field = f;
5016 /* XXX: fix this mess by using explicit storage field */
5017 type1 = f->type;
5018 type1.t |= (type->t & ~VT_TYPE);
5019 type = &type1;
5020 c += f->c;
5022 notfirst = 1;
5024 if (notfirst) {
5025 if (tok == '=') {
5026 next();
5027 } else {
5028 if (!gnu_ext)
5029 expect("=");
5031 } else {
5032 if (type->t & VT_ARRAY) {
5033 index = *cur_index;
5034 type = pointed_type(type);
5035 c += index * type_size(type, &align);
5036 } else {
5037 f = *cur_field;
5038 if (!f)
5039 tcc_error("too many field init");
5040 /* XXX: fix this mess by using explicit storage field */
5041 type1 = f->type;
5042 type1.t |= (type->t & ~VT_TYPE);
5043 type = &type1;
5044 c += f->c;
5047 decl_initializer(type, sec, c, 0, size_only);
5049 /* XXX: make it more general */
5050 if (!size_only && nb_elems > 1) {
5051 unsigned long c_end;
5052 uint8_t *src, *dst;
5053 int i;
5055 if (!sec)
5056 tcc_error("range init not supported yet for dynamic storage");
5057 c_end = c + nb_elems * elem_size;
5058 if (c_end > sec->data_allocated)
5059 section_realloc(sec, c_end);
5060 src = sec->data + c;
5061 dst = src;
5062 for(i = 1; i < nb_elems; i++) {
5063 dst += elem_size;
5064 memcpy(dst, src, elem_size);
5069 #define EXPR_VAL 0
5070 #define EXPR_CONST 1
5071 #define EXPR_ANY 2
5073 /* store a value or an expression directly in global data or in local array */
5074 static void init_putv(CType *type, Section *sec, unsigned long c,
5075 int v, int expr_type)
5077 int saved_global_expr, bt, bit_pos, bit_size;
5078 void *ptr;
5079 unsigned long long bit_mask;
5080 CType dtype;
5082 switch(expr_type) {
5083 case EXPR_VAL:
5084 vpushi(v);
5085 break;
5086 case EXPR_CONST:
5087 /* compound literals must be allocated globally in this case */
5088 saved_global_expr = global_expr;
5089 global_expr = 1;
5090 expr_const1();
5091 global_expr = saved_global_expr;
5092 /* NOTE: symbols are accepted */
5093 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5094 tcc_error("initializer element is not constant");
5095 break;
5096 case EXPR_ANY:
5097 expr_eq();
5098 break;
5101 dtype = *type;
5102 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5104 if (sec) {
5105 /* XXX: not portable */
5106 /* XXX: generate error if incorrect relocation */
5107 gen_assign_cast(&dtype);
5108 bt = type->t & VT_BTYPE;
5109 /* we'll write at most 12 bytes */
5110 if (c + 12 > sec->data_allocated) {
5111 section_realloc(sec, c + 12);
5113 ptr = sec->data + c;
5114 /* XXX: make code faster ? */
5115 if (!(type->t & VT_BITFIELD)) {
5116 bit_pos = 0;
5117 bit_size = 32;
5118 bit_mask = -1LL;
5119 } else {
5120 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5121 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5122 bit_mask = (1LL << bit_size) - 1;
5124 if ((vtop->r & VT_SYM) &&
5125 (bt == VT_BYTE ||
5126 bt == VT_SHORT ||
5127 bt == VT_DOUBLE ||
5128 bt == VT_LDOUBLE ||
5129 bt == VT_LLONG ||
5130 (bt == VT_INT && bit_size != 32)))
5131 tcc_error("initializer element is not computable at load time");
5132 switch(bt) {
5133 case VT_BOOL:
5134 vtop->c.i = (vtop->c.i != 0);
5135 case VT_BYTE:
5136 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5137 break;
5138 case VT_SHORT:
5139 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5140 break;
5141 case VT_DOUBLE:
5142 *(double *)ptr = vtop->c.d;
5143 break;
5144 case VT_LDOUBLE:
5145 *(long double *)ptr = vtop->c.ld;
5146 break;
5147 case VT_LLONG:
5148 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5149 break;
5150 case VT_PTR:
5151 if (vtop->r & VT_SYM) {
5152 greloc(sec, vtop->sym, c, R_DATA_PTR);
5154 *(addr_t *)ptr |= (vtop->c.ptr_offset & bit_mask) << bit_pos;
5155 break;
5156 default:
5157 if (vtop->r & VT_SYM) {
5158 greloc(sec, vtop->sym, c, R_DATA_PTR);
5160 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5161 break;
5163 vtop--;
5164 } else {
5165 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5166 vswap();
5167 vstore();
5168 vpop();
5172 /* put zeros for variable based init */
5173 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5175 if (sec) {
5176 /* nothing to do because globals are already set to zero */
5177 } else {
5178 vpush_global_sym(&func_old_type, TOK_memset);
5179 vseti(VT_LOCAL, c);
5180 #ifdef TCC_TARGET_ARM
5181 vpushs(size);
5182 vpushi(0);
5183 #else
5184 vpushi(0);
5185 vpushs(size);
5186 #endif
5187 gfunc_call(3);
5191 /* 't' contains the type and storage info. 'c' is the offset of the
5192 object in section 'sec'. If 'sec' is NULL, it means stack based
5193 allocation. 'first' is true if array '{' must be read (multi
5194 dimension implicit array init handling). 'size_only' is true if
5195 size only evaluation is wanted (only for arrays). */
5196 static void decl_initializer(CType *type, Section *sec, unsigned long c,
5197 int first, int size_only)
5199 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
5200 int size1, align1, expr_type;
5201 Sym *s, *f;
5202 CType *t1;
5204 if (type->t & VT_VLA) {
5205 int a;
5207 /* save current stack pointer */
5208 if (vla_flags & VLA_NEED_NEW_FRAME) {
5209 vla_sp_save();
5210 vla_flags = VLA_IN_SCOPE;
5211 vla_sp_loc = &vla_sp_loc_tmp;
5214 vla_runtime_type_size(type, &a);
5215 gen_vla_alloc(type, a);
5216 vset(type, VT_LOCAL|VT_LVAL, c);
5217 vswap();
5218 vstore();
5219 vpop();
5220 } else if (type->t & VT_ARRAY) {
5221 s = type->ref;
5222 n = s->c;
5223 array_length = 0;
5224 t1 = pointed_type(type);
5225 size1 = type_size(t1, &align1);
5227 no_oblock = 1;
5228 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5229 tok == '{') {
5230 if (tok != '{')
5231 tcc_error("character array initializer must be a literal,"
5232 " optionally enclosed in braces");
5233 skip('{');
5234 no_oblock = 0;
5237 /* only parse strings here if correct type (otherwise: handle
5238 them as ((w)char *) expressions */
5239 if ((tok == TOK_LSTR &&
5240 #ifdef TCC_TARGET_PE
5241 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5242 #else
5243 (t1->t & VT_BTYPE) == VT_INT
5244 #endif
5245 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5246 while (tok == TOK_STR || tok == TOK_LSTR) {
5247 int cstr_len, ch;
5248 CString *cstr;
5250 cstr = tokc.cstr;
5251 /* compute maximum number of chars wanted */
5252 if (tok == TOK_STR)
5253 cstr_len = cstr->size;
5254 else
5255 cstr_len = cstr->size / sizeof(nwchar_t);
5256 cstr_len--;
5257 nb = cstr_len;
5258 if (n >= 0 && nb > (n - array_length))
5259 nb = n - array_length;
5260 if (!size_only) {
5261 if (cstr_len > nb)
5262 tcc_warning("initializer-string for array is too long");
5263 /* in order to go faster for common case (char
5264 string in global variable, we handle it
5265 specifically */
5266 if (sec && tok == TOK_STR && size1 == 1) {
5267 memcpy(sec->data + c + array_length, cstr->data, nb);
5268 } else {
5269 for(i=0;i<nb;i++) {
5270 if (tok == TOK_STR)
5271 ch = ((unsigned char *)cstr->data)[i];
5272 else
5273 ch = ((nwchar_t *)cstr->data)[i];
5274 init_putv(t1, sec, c + (array_length + i) * size1,
5275 ch, EXPR_VAL);
5279 array_length += nb;
5280 next();
5282 /* only add trailing zero if enough storage (no
5283 warning in this case since it is standard) */
5284 if (n < 0 || array_length < n) {
5285 if (!size_only) {
5286 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5288 array_length++;
5290 } else {
5291 index = 0;
5292 while (tok != '}') {
5293 decl_designator(type, sec, c, &index, NULL, size_only);
5294 if (n >= 0 && index >= n)
5295 tcc_error("index too large");
5296 /* must put zero in holes (note that doing it that way
5297 ensures that it even works with designators) */
5298 if (!size_only && array_length < index) {
5299 init_putz(t1, sec, c + array_length * size1,
5300 (index - array_length) * size1);
5302 index++;
5303 if (index > array_length)
5304 array_length = index;
5305 /* special test for multi dimensional arrays (may not
5306 be strictly correct if designators are used at the
5307 same time) */
5308 if (index >= n && no_oblock)
5309 break;
5310 if (tok == '}')
5311 break;
5312 skip(',');
5315 if (!no_oblock)
5316 skip('}');
5317 /* put zeros at the end */
5318 if (!size_only && n >= 0 && array_length < n) {
5319 init_putz(t1, sec, c + array_length * size1,
5320 (n - array_length) * size1);
5322 /* patch type size if needed */
5323 if (n < 0)
5324 s->c = array_length;
5325 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5326 (sec || !first || tok == '{')) {
5327 int par_count;
5329 /* NOTE: the previous test is a specific case for automatic
5330 struct/union init */
5331 /* XXX: union needs only one init */
5333 /* XXX: this test is incorrect for local initializers
5334 beginning with ( without {. It would be much more difficult
5335 to do it correctly (ideally, the expression parser should
5336 be used in all cases) */
5337 par_count = 0;
5338 if (tok == '(') {
5339 AttributeDef ad1;
5340 CType type1;
5341 next();
5342 while (tok == '(') {
5343 par_count++;
5344 next();
5346 if (!parse_btype(&type1, &ad1))
5347 expect("cast");
5348 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5349 #if 0
5350 if (!is_assignable_types(type, &type1))
5351 tcc_error("invalid type for cast");
5352 #endif
5353 skip(')');
5355 no_oblock = 1;
5356 if (first || tok == '{') {
5357 skip('{');
5358 no_oblock = 0;
5360 s = type->ref;
5361 f = s->next;
5362 array_length = 0;
5363 index = 0;
5364 n = s->c;
5365 while (tok != '}') {
5366 decl_designator(type, sec, c, NULL, &f, size_only);
5367 index = f->c;
5368 if (!size_only && array_length < index) {
5369 init_putz(type, sec, c + array_length,
5370 index - array_length);
5372 index = index + type_size(&f->type, &align1);
5373 if (index > array_length)
5374 array_length = index;
5376 /* gr: skip fields from same union - ugly. */
5377 while (f->next) {
5378 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5379 /* test for same offset */
5380 if (f->next->c != f->c)
5381 break;
5382 /* if yes, test for bitfield shift */
5383 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5384 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5385 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5386 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5387 if (bit_pos_1 != bit_pos_2)
5388 break;
5390 f = f->next;
5393 f = f->next;
5394 if (no_oblock && f == NULL)
5395 break;
5396 if (tok == '}')
5397 break;
5398 skip(',');
5400 /* put zeros at the end */
5401 if (!size_only && array_length < n) {
5402 init_putz(type, sec, c + array_length,
5403 n - array_length);
5405 if (!no_oblock)
5406 skip('}');
5407 while (par_count) {
5408 skip(')');
5409 par_count--;
5411 } else if (tok == '{') {
5412 next();
5413 decl_initializer(type, sec, c, first, size_only);
5414 skip('}');
5415 } else if (size_only) {
5416 /* just skip expression */
5417 parlevel = parlevel1 = 0;
5418 while ((parlevel > 0 || parlevel1 > 0 ||
5419 (tok != '}' && tok != ',')) && tok != -1) {
5420 if (tok == '(')
5421 parlevel++;
5422 else if (tok == ')')
5423 parlevel--;
5424 else if (tok == '{')
5425 parlevel1++;
5426 else if (tok == '}')
5427 parlevel1--;
5428 next();
5430 } else {
5431 /* currently, we always use constant expression for globals
5432 (may change for scripting case) */
5433 expr_type = EXPR_CONST;
5434 if (!sec)
5435 expr_type = EXPR_ANY;
5436 init_putv(type, sec, c, 0, expr_type);
5440 /* parse an initializer for type 't' if 'has_init' is non zero, and
5441 allocate space in local or global data space ('r' is either
5442 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5443 variable 'v' with an associated name represented by 'asm_label' of
5444 scope 'scope' is declared before initializers are parsed. If 'v' is
5445 zero, then a reference to the new object is put in the value stack.
5446 If 'has_init' is 2, a special parsing is done to handle string
5447 constants. */
5448 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5449 int has_init, int v, char *asm_label,
5450 int scope)
5452 int size, align, addr, data_offset;
5453 int level;
5454 ParseState saved_parse_state = {0};
5455 TokenString init_str;
5456 Section *sec;
5457 Sym *flexible_array;
5459 flexible_array = NULL;
5460 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5461 Sym *field = type->ref->next;
5462 if (field) {
5463 while (field->next)
5464 field = field->next;
5465 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5466 flexible_array = field;
5470 size = type_size(type, &align);
5471 /* If unknown size, we must evaluate it before
5472 evaluating initializers because
5473 initializers can generate global data too
5474 (e.g. string pointers or ISOC99 compound
5475 literals). It also simplifies local
5476 initializers handling */
5477 tok_str_new(&init_str);
5478 if (size < 0 || (flexible_array && has_init)) {
5479 if (!has_init)
5480 tcc_error("unknown type size");
5481 /* get all init string */
5482 if (has_init == 2) {
5483 /* only get strings */
5484 while (tok == TOK_STR || tok == TOK_LSTR) {
5485 tok_str_add_tok(&init_str);
5486 next();
5488 } else {
5489 level = 0;
5490 while (level > 0 || (tok != ',' && tok != ';')) {
5491 if (tok < 0)
5492 tcc_error("unexpected end of file in initializer");
5493 tok_str_add_tok(&init_str);
5494 if (tok == '{')
5495 level++;
5496 else if (tok == '}') {
5497 level--;
5498 if (level <= 0) {
5499 next();
5500 break;
5503 next();
5506 tok_str_add(&init_str, -1);
5507 tok_str_add(&init_str, 0);
5509 /* compute size */
5510 save_parse_state(&saved_parse_state);
5512 macro_ptr = init_str.str;
5513 next();
5514 decl_initializer(type, NULL, 0, 1, 1);
5515 /* prepare second initializer parsing */
5516 macro_ptr = init_str.str;
5517 next();
5519 /* if still unknown size, error */
5520 size = type_size(type, &align);
5521 if (size < 0)
5522 tcc_error("unknown type size");
5524 if (flexible_array)
5525 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5526 /* take into account specified alignment if bigger */
5527 if (ad->a.aligned) {
5528 if (ad->a.aligned > align)
5529 align = ad->a.aligned;
5530 } else if (ad->a.packed) {
5531 align = 1;
5533 if ((r & VT_VALMASK) == VT_LOCAL) {
5534 sec = NULL;
5535 #ifdef CONFIG_TCC_BCHECK
5536 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5537 loc--;
5539 #endif
5540 loc = (loc - size) & -align;
5541 addr = loc;
5542 #ifdef CONFIG_TCC_BCHECK
5543 /* handles bounds */
5544 /* XXX: currently, since we do only one pass, we cannot track
5545 '&' operators, so we add only arrays */
5546 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5547 unsigned long *bounds_ptr;
5548 /* add padding between regions */
5549 loc--;
5550 /* then add local bound info */
5551 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5552 bounds_ptr[0] = addr;
5553 bounds_ptr[1] = size;
5555 #endif
5556 if (v) {
5557 /* local variable */
5558 sym_push(v, type, r, addr);
5559 } else {
5560 /* push local reference */
5561 vset(type, r, addr);
5563 } else {
5564 Sym *sym;
5566 sym = NULL;
5567 if (v && scope == VT_CONST) {
5568 /* see if the symbol was already defined */
5569 sym = sym_find(v);
5570 if (sym) {
5571 if (!is_compatible_types(&sym->type, type))
5572 tcc_error("incompatible types for redefinition of '%s'",
5573 get_tok_str(v, NULL));
5574 if (sym->type.t & VT_EXTERN) {
5575 /* if the variable is extern, it was not allocated */
5576 sym->type.t &= ~VT_EXTERN;
5577 /* set array size if it was ommited in extern
5578 declaration */
5579 if ((sym->type.t & VT_ARRAY) &&
5580 sym->type.ref->c < 0 &&
5581 type->ref->c >= 0)
5582 sym->type.ref->c = type->ref->c;
5583 } else {
5584 /* we accept several definitions of the same
5585 global variable. this is tricky, because we
5586 must play with the SHN_COMMON type of the symbol */
5587 /* XXX: should check if the variable was already
5588 initialized. It is incorrect to initialized it
5589 twice */
5590 /* no init data, we won't add more to the symbol */
5591 if (!has_init)
5592 goto no_alloc;
5597 /* allocate symbol in corresponding section */
5598 sec = ad->section;
5599 if (!sec) {
5600 if (has_init)
5601 sec = data_section;
5602 else if (tcc_state->nocommon)
5603 sec = bss_section;
5605 if (sec) {
5606 data_offset = sec->data_offset;
5607 data_offset = (data_offset + align - 1) & -align;
5608 addr = data_offset;
5609 /* very important to increment global pointer at this time
5610 because initializers themselves can create new initializers */
5611 data_offset += size;
5612 #ifdef CONFIG_TCC_BCHECK
5613 /* add padding if bound check */
5614 if (tcc_state->do_bounds_check)
5615 data_offset++;
5616 #endif
5617 sec->data_offset = data_offset;
5618 /* allocate section space to put the data */
5619 if (sec->sh_type != SHT_NOBITS &&
5620 data_offset > sec->data_allocated)
5621 section_realloc(sec, data_offset);
5622 /* align section if needed */
5623 if (align > sec->sh_addralign)
5624 sec->sh_addralign = align;
5625 } else {
5626 addr = 0; /* avoid warning */
5629 if (v) {
5630 if (scope != VT_CONST || !sym) {
5631 sym = sym_push(v, type, r | VT_SYM, 0);
5632 sym->asm_label = asm_label;
5634 /* update symbol definition */
5635 if (sec) {
5636 put_extern_sym(sym, sec, addr, size);
5637 } else {
5638 ElfW(Sym) *esym;
5639 /* put a common area */
5640 put_extern_sym(sym, NULL, align, size);
5641 /* XXX: find a nicer way */
5642 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5643 esym->st_shndx = SHN_COMMON;
5645 } else {
5646 /* push global reference */
5647 sym = get_sym_ref(type, sec, addr, size);
5648 vpushsym(type, sym);
5650 /* patch symbol weakness */
5651 if (type->t & VT_WEAK)
5652 weaken_symbol(sym);
5653 #ifdef CONFIG_TCC_BCHECK
5654 /* handles bounds now because the symbol must be defined
5655 before for the relocation */
5656 if (tcc_state->do_bounds_check) {
5657 unsigned long *bounds_ptr;
5659 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5660 /* then add global bound info */
5661 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5662 bounds_ptr[0] = 0; /* relocated */
5663 bounds_ptr[1] = size;
5665 #endif
5667 if (has_init || (type->t & VT_VLA)) {
5668 decl_initializer(type, sec, addr, 1, 0);
5669 /* restore parse state if needed */
5670 if (init_str.str) {
5671 tok_str_free(init_str.str);
5672 restore_parse_state(&saved_parse_state);
5674 /* patch flexible array member size back to -1, */
5675 /* for possible subsequent similar declarations */
5676 if (flexible_array)
5677 flexible_array->type.ref->c = -1;
5679 no_alloc: ;
5682 static void put_func_debug(Sym *sym)
5684 char buf[512];
5686 /* stabs info */
5687 /* XXX: we put here a dummy type */
5688 snprintf(buf, sizeof(buf), "%s:%c1",
5689 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5690 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5691 cur_text_section, sym->c);
5692 /* //gr gdb wants a line at the function */
5693 put_stabn(N_SLINE, 0, file->line_num, 0);
5694 last_ind = 0;
5695 last_line_num = 0;
5698 /* parse an old style function declaration list */
5699 /* XXX: check multiple parameter */
5700 static void func_decl_list(Sym *func_sym)
5702 AttributeDef ad;
5703 int v;
5704 Sym *s;
5705 CType btype, type;
5707 /* parse each declaration */
5708 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5709 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5710 if (!parse_btype(&btype, &ad))
5711 expect("declaration list");
5712 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5713 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5714 tok == ';') {
5715 /* we accept no variable after */
5716 } else {
5717 for(;;) {
5718 type = btype;
5719 type_decl(&type, &ad, &v, TYPE_DIRECT);
5720 /* find parameter in function parameter list */
5721 s = func_sym->next;
5722 while (s != NULL) {
5723 if ((s->v & ~SYM_FIELD) == v)
5724 goto found;
5725 s = s->next;
5727 tcc_error("declaration for parameter '%s' but no such parameter",
5728 get_tok_str(v, NULL));
5729 found:
5730 /* check that no storage specifier except 'register' was given */
5731 if (type.t & VT_STORAGE)
5732 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5733 convert_parameter_type(&type);
5734 /* we can add the type (NOTE: it could be local to the function) */
5735 s->type = type;
5736 /* accept other parameters */
5737 if (tok == ',')
5738 next();
5739 else
5740 break;
5743 skip(';');
5747 /* parse a function defined by symbol 'sym' and generate its code in
5748 'cur_text_section' */
5749 static void gen_function(Sym *sym)
5751 int saved_nocode_wanted = nocode_wanted;
5752 nocode_wanted = 0;
5753 ind = cur_text_section->data_offset;
5754 /* NOTE: we patch the symbol size later */
5755 put_extern_sym(sym, cur_text_section, ind, 0);
5756 funcname = get_tok_str(sym->v, NULL);
5757 func_ind = ind;
5758 /* Initialize VLA state */
5759 vla_sp_loc = &vla_sp_root_loc;
5760 vla_flags = VLA_NEED_NEW_FRAME;
5761 /* put debug symbol */
5762 if (tcc_state->do_debug)
5763 put_func_debug(sym);
5764 /* push a dummy symbol to enable local sym storage */
5765 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5766 gfunc_prolog(&sym->type);
5767 #ifdef CONFIG_TCC_BCHECK
5768 if (tcc_state->do_bounds_check
5769 && !strcmp(get_tok_str(sym->v, NULL), "main")) {
5770 int i;
5772 sym = local_stack;
5773 for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) {
5774 if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD)
5775 break;
5776 vpush_global_sym(&func_old_type, TOK___bound_main_arg);
5777 vset(&sym->type, sym->r, sym->c);
5778 gfunc_call(1);
5781 #endif
5782 rsym = 0;
5783 block(NULL, NULL, NULL, NULL, 0, 0);
5784 gsym(rsym);
5785 gfunc_epilog();
5786 cur_text_section->data_offset = ind;
5787 label_pop(&global_label_stack, NULL);
5788 /* reset local stack */
5789 scope_stack_bottom = NULL;
5790 sym_pop(&local_stack, NULL);
5791 /* end of function */
5792 /* patch symbol size */
5793 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5794 ind - func_ind;
5795 /* patch symbol weakness (this definition overrules any prototype) */
5796 if (sym->type.t & VT_WEAK)
5797 weaken_symbol(sym);
5798 if (tcc_state->do_debug) {
5799 put_stabn(N_FUN, 0, 0, ind - func_ind);
5801 /* It's better to crash than to generate wrong code */
5802 cur_text_section = NULL;
5803 funcname = ""; /* for safety */
5804 func_vt.t = VT_VOID; /* for safety */
5805 func_var = 0; /* for safety */
5806 ind = 0; /* for safety */
5807 nocode_wanted = saved_nocode_wanted;
5810 ST_FUNC void gen_inline_functions(void)
5812 Sym *sym;
5813 int *str, inline_generated, i;
5814 struct InlineFunc *fn;
5816 /* iterate while inline function are referenced */
5817 for(;;) {
5818 inline_generated = 0;
5819 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5820 fn = tcc_state->inline_fns[i];
5821 sym = fn->sym;
5822 if (sym && sym->c) {
5823 /* the function was used: generate its code and
5824 convert it to a normal function */
5825 str = fn->token_str;
5826 fn->sym = NULL;
5827 if (file)
5828 pstrcpy(file->filename, sizeof file->filename, fn->filename);
5829 sym->r = VT_SYM | VT_CONST;
5830 sym->type.t &= ~VT_INLINE;
5832 macro_ptr = str;
5833 next();
5834 cur_text_section = text_section;
5835 gen_function(sym);
5836 macro_ptr = NULL; /* fail safe */
5838 inline_generated = 1;
5841 if (!inline_generated)
5842 break;
5844 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5845 fn = tcc_state->inline_fns[i];
5846 str = fn->token_str;
5847 tok_str_free(str);
5849 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5852 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5853 static int decl0(int l, int is_for_loop_init)
5855 int v, has_init, r;
5856 CType type, btype;
5857 Sym *sym;
5858 AttributeDef ad;
5860 while (1) {
5861 if (!parse_btype(&btype, &ad)) {
5862 if (is_for_loop_init)
5863 return 0;
5864 /* skip redundant ';' */
5865 /* XXX: find more elegant solution */
5866 if (tok == ';') {
5867 next();
5868 continue;
5870 if (l == VT_CONST &&
5871 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5872 /* global asm block */
5873 asm_global_instr();
5874 continue;
5876 /* special test for old K&R protos without explicit int
5877 type. Only accepted when defining global data */
5878 if (l == VT_LOCAL || tok < TOK_DEFINE)
5879 break;
5880 btype.t = VT_INT;
5882 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5883 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5884 tok == ';') {
5885 /* we accept no variable after */
5886 next();
5887 continue;
5889 while (1) { /* iterate thru each declaration */
5890 char *asm_label; // associated asm label
5891 type = btype;
5892 type_decl(&type, &ad, &v, TYPE_DIRECT);
5893 #if 0
5895 char buf[500];
5896 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5897 printf("type = '%s'\n", buf);
5899 #endif
5900 if ((type.t & VT_BTYPE) == VT_FUNC) {
5901 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5902 tcc_error("function without file scope cannot be static");
5904 /* if old style function prototype, we accept a
5905 declaration list */
5906 sym = type.ref;
5907 if (sym->c == FUNC_OLD)
5908 func_decl_list(sym);
5911 asm_label = NULL;
5912 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5913 CString astr;
5915 asm_label_instr(&astr);
5916 asm_label = tcc_strdup(astr.data);
5917 cstr_free(&astr);
5919 /* parse one last attribute list, after asm label */
5920 parse_attribute(&ad);
5923 if (ad.a.weak)
5924 type.t |= VT_WEAK;
5925 #ifdef TCC_TARGET_PE
5926 if (ad.a.func_import)
5927 type.t |= VT_IMPORT;
5928 if (ad.a.func_export)
5929 type.t |= VT_EXPORT;
5930 #endif
5931 if (tok == '{') {
5932 if (l == VT_LOCAL)
5933 tcc_error("cannot use local functions");
5934 if ((type.t & VT_BTYPE) != VT_FUNC)
5935 expect("function definition");
5937 /* reject abstract declarators in function definition */
5938 sym = type.ref;
5939 while ((sym = sym->next) != NULL)
5940 if (!(sym->v & ~SYM_FIELD))
5941 expect("identifier");
5943 /* XXX: cannot do better now: convert extern line to static inline */
5944 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5945 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5947 sym = sym_find(v);
5948 if (sym) {
5949 Sym *ref;
5950 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5951 goto func_error1;
5953 ref = sym->type.ref;
5954 if (0 == ref->a.func_proto)
5955 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
5957 /* use func_call from prototype if not defined */
5958 if (ref->a.func_call != FUNC_CDECL
5959 && type.ref->a.func_call == FUNC_CDECL)
5960 type.ref->a.func_call = ref->a.func_call;
5962 /* use export from prototype */
5963 if (ref->a.func_export)
5964 type.ref->a.func_export = 1;
5966 /* use static from prototype */
5967 if (sym->type.t & VT_STATIC)
5968 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5970 if (!is_compatible_types(&sym->type, &type)) {
5971 func_error1:
5972 tcc_error("incompatible types for redefinition of '%s'",
5973 get_tok_str(v, NULL));
5975 type.ref->a.func_proto = 0;
5976 /* if symbol is already defined, then put complete type */
5977 sym->type = type;
5978 } else {
5979 /* put function symbol */
5980 sym = global_identifier_push(v, type.t, 0);
5981 sym->type.ref = type.ref;
5984 /* static inline functions are just recorded as a kind
5985 of macro. Their code will be emitted at the end of
5986 the compilation unit only if they are used */
5987 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5988 (VT_INLINE | VT_STATIC)) {
5989 TokenString func_str;
5990 int block_level;
5991 struct InlineFunc *fn;
5992 const char *filename;
5994 tok_str_new(&func_str);
5996 block_level = 0;
5997 for(;;) {
5998 int t;
5999 if (tok == TOK_EOF)
6000 tcc_error("unexpected end of file");
6001 tok_str_add_tok(&func_str);
6002 t = tok;
6003 next();
6004 if (t == '{') {
6005 block_level++;
6006 } else if (t == '}') {
6007 block_level--;
6008 if (block_level == 0)
6009 break;
6012 tok_str_add(&func_str, -1);
6013 tok_str_add(&func_str, 0);
6014 filename = file ? file->filename : "";
6015 fn = tcc_malloc(sizeof *fn + strlen(filename));
6016 strcpy(fn->filename, filename);
6017 fn->sym = sym;
6018 fn->token_str = func_str.str;
6019 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
6021 } else {
6022 /* compute text section */
6023 cur_text_section = ad.section;
6024 if (!cur_text_section)
6025 cur_text_section = text_section;
6026 sym->r = VT_SYM | VT_CONST;
6027 gen_function(sym);
6029 break;
6030 } else {
6031 if (btype.t & VT_TYPEDEF) {
6032 /* save typedefed type */
6033 /* XXX: test storage specifiers ? */
6034 sym = sym_push(v, &type, 0, 0);
6035 sym->a = ad.a;
6036 sym->type.t |= VT_TYPEDEF;
6037 } else {
6038 r = 0;
6039 if ((type.t & VT_BTYPE) == VT_FUNC) {
6040 /* external function definition */
6041 /* specific case for func_call attribute */
6042 ad.a.func_proto = 1;
6043 type.ref->a = ad.a;
6044 } else if (!(type.t & VT_ARRAY)) {
6045 /* not lvalue if array */
6046 r |= lvalue_type(type.t);
6048 has_init = (tok == '=');
6049 if (has_init && (type.t & VT_VLA))
6050 tcc_error("Variable length array cannot be initialized");
6051 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
6052 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6053 !has_init && l == VT_CONST && type.ref->c < 0)) {
6054 /* external variable or function */
6055 /* NOTE: as GCC, uninitialized global static
6056 arrays of null size are considered as
6057 extern */
6058 sym = external_sym(v, &type, r, asm_label);
6060 if (type.t & VT_WEAK)
6061 weaken_symbol(sym);
6063 if (ad.alias_target) {
6064 Section tsec;
6065 Elf32_Sym *esym;
6066 Sym *alias_target;
6068 alias_target = sym_find(ad.alias_target);
6069 if (!alias_target || !alias_target->c)
6070 tcc_error("unsupported forward __alias__ attribute");
6071 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
6072 tsec.sh_num = esym->st_shndx;
6073 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
6075 } else {
6076 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
6077 if (type.t & VT_STATIC)
6078 r |= VT_CONST;
6079 else
6080 r |= l;
6081 if (has_init)
6082 next();
6083 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
6086 if (tok != ',') {
6087 if (is_for_loop_init)
6088 return 1;
6089 skip(';');
6090 break;
6092 next();
6094 ad.a.aligned = 0;
6097 return 0;
6100 ST_FUNC void decl(int l)
6102 decl0(l, 0);