win64: va_arg with structures
[tinycc.git] / tccgen.c
bloba7f943d95ac0566bc8ad225cd173da481fb8bba8
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 *define_stack;
54 ST_DATA Sym *global_label_stack;
55 ST_DATA Sym *local_label_stack;
57 ST_DATA SValue vstack[VSTACK_SIZE], *vtop;
59 ST_DATA int const_wanted; /* true if constant wanted */
60 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
61 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
63 ST_DATA int func_vc;
64 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
65 ST_DATA char *funcname;
67 ST_DATA CType char_pointer_type, func_old_type, int_type;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType *type);
71 static inline CType *pointed_type(CType *type);
72 static int is_compatible_types(CType *type1, CType *type2);
73 static int parse_btype(CType *type, AttributeDef *ad);
74 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
75 static void parse_expr_type(CType *type);
76 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
77 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
78 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
79 static int decl0(int l, int is_for_loop_init);
80 static void expr_eq(void);
81 static void unary_type(CType *type);
82 static void vla_runtime_type_size(CType *type, int *a);
83 static int is_compatible_parameter_types(CType *type1, CType *type2);
84 static void expr_type(CType *type);
86 ST_INLN int is_float(int t)
88 int bt;
89 bt = t & VT_BTYPE;
90 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
93 ST_FUNC void test_lvalue(void)
95 if (!(vtop->r & VT_LVAL))
96 expect("lvalue");
99 /* ------------------------------------------------------------------------- */
100 /* symbol allocator */
101 static Sym *__sym_malloc(void)
103 Sym *sym_pool, *sym, *last_sym;
104 int i;
106 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
107 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
109 last_sym = sym_free_first;
110 sym = sym_pool;
111 for(i = 0; i < SYM_POOL_NB; i++) {
112 sym->next = last_sym;
113 last_sym = sym;
114 sym++;
116 sym_free_first = last_sym;
117 return last_sym;
120 static inline Sym *sym_malloc(void)
122 Sym *sym;
123 sym = sym_free_first;
124 if (!sym)
125 sym = __sym_malloc();
126 sym_free_first = sym->next;
127 return sym;
130 ST_INLN void sym_free(Sym *sym)
132 sym->next = sym_free_first;
133 tcc_free(sym->asm_label);
134 sym_free_first = sym;
137 /* push, without hashing */
138 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
140 Sym *s;
141 s = sym_malloc();
142 s->asm_label = NULL;
143 s->v = v;
144 s->type.t = t;
145 s->type.ref = NULL;
146 #ifdef _WIN64
147 s->d = NULL;
148 #endif
149 s->c = c;
150 s->next = NULL;
151 /* add in stack */
152 s->prev = *ps;
153 *ps = s;
154 return s;
157 /* find a symbol and return its associated structure. 's' is the top
158 of the symbol stack */
159 ST_FUNC Sym *sym_find2(Sym *s, int v)
161 while (s) {
162 if (s->v == v)
163 return s;
164 s = s->prev;
166 return NULL;
169 /* structure lookup */
170 ST_INLN Sym *struct_find(int v)
172 v -= TOK_IDENT;
173 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
174 return NULL;
175 return table_ident[v]->sym_struct;
178 /* find an identifier */
179 ST_INLN Sym *sym_find(int v)
181 v -= TOK_IDENT;
182 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
183 return NULL;
184 return table_ident[v]->sym_identifier;
187 /* push a given symbol on the symbol stack */
188 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
190 Sym *s, **ps;
191 TokenSym *ts;
193 if (local_stack)
194 ps = &local_stack;
195 else
196 ps = &global_stack;
197 s = sym_push2(ps, v, type->t, c);
198 s->type.ref = type->ref;
199 s->r = r;
200 /* don't record fields or anonymous symbols */
201 /* XXX: simplify */
202 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
203 /* record symbol in token array */
204 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
205 if (v & SYM_STRUCT)
206 ps = &ts->sym_struct;
207 else
208 ps = &ts->sym_identifier;
209 s->prev_tok = *ps;
210 *ps = s;
212 return s;
215 /* push a global identifier */
216 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
218 Sym *s, **ps;
219 s = sym_push2(&global_stack, v, t, c);
220 /* don't record anonymous symbol */
221 if (v < SYM_FIRST_ANOM) {
222 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
223 /* modify the top most local identifier, so that
224 sym_identifier will point to 's' when popped */
225 while (*ps != NULL)
226 ps = &(*ps)->prev_tok;
227 s->prev_tok = NULL;
228 *ps = s;
230 return s;
233 /* pop symbols until top reaches 'b' */
234 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
236 Sym *s, *ss, **ps;
237 TokenSym *ts;
238 int v;
240 s = *ptop;
241 while(s != b) {
242 ss = s->prev;
243 v = s->v;
244 /* remove symbol in token array */
245 /* XXX: simplify */
246 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
247 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
248 if (v & SYM_STRUCT)
249 ps = &ts->sym_struct;
250 else
251 ps = &ts->sym_identifier;
252 *ps = s->prev_tok;
254 sym_free(s);
255 s = ss;
257 *ptop = b;
260 static void weaken_symbol(Sym *sym)
262 sym->type.t |= VT_WEAK;
263 if (sym->c > 0) {
264 int esym_type;
265 ElfW(Sym) *esym;
267 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
268 esym_type = ELFW(ST_TYPE)(esym->st_info);
269 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
273 /* ------------------------------------------------------------------------- */
275 ST_FUNC void swap(int *p, int *q)
277 int t;
278 t = *p;
279 *p = *q;
280 *q = t;
283 static void vsetc(CType *type, int r, CValue *vc)
285 int v;
287 if (vtop >= vstack + (VSTACK_SIZE - 1))
288 error("memory full");
289 /* cannot let cpu flags if other instruction are generated. Also
290 avoid leaving VT_JMP anywhere except on the top of the stack
291 because it would complicate the code generator. */
292 if (vtop >= vstack) {
293 v = vtop->r & VT_VALMASK;
294 if (v == VT_CMP || (v & ~1) == VT_JMP)
295 gv(RC_INT);
297 vtop++;
298 vtop->type = *type;
299 vtop->r = r;
300 vtop->r2 = VT_CONST;
301 vtop->c = *vc;
304 /* push constant of type "type" with useless value */
305 void vpush(CType *type)
307 CValue cval;
308 vsetc(type, VT_CONST, &cval);
311 /* push integer constant */
312 ST_FUNC void vpushi(int v)
314 CValue cval;
315 cval.i = v;
316 vsetc(&int_type, VT_CONST, &cval);
319 /* push long long constant */
320 static void vpushll(long long v)
322 CValue cval;
323 CType ctype;
324 ctype.t = VT_LLONG;
325 ctype.ref = 0;
326 cval.ull = v;
327 vsetc(&ctype, VT_CONST, &cval);
330 /* push arbitrary 64bit constant */
331 void vpush64(int ty, unsigned long long v)
333 CValue cval;
334 CType ctype;
335 ctype.t = ty;
336 cval.ull = v;
337 vsetc(&ctype, VT_CONST, &cval);
340 /* Return a static symbol pointing to a section */
341 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
343 int v;
344 Sym *sym;
346 v = anon_sym++;
347 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
348 sym->type.ref = type->ref;
349 sym->r = VT_CONST | VT_SYM;
350 put_extern_sym(sym, sec, offset, size);
351 return sym;
354 /* push a reference to a section offset by adding a dummy symbol */
355 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
357 CValue cval;
359 cval.ul = 0;
360 vsetc(type, VT_CONST | VT_SYM, &cval);
361 vtop->sym = get_sym_ref(type, sec, offset, size);
364 /* define a new external reference to a symbol 'v' of type 'u' */
365 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
367 Sym *s;
369 s = sym_find(v);
370 if (!s) {
371 /* push forward reference */
372 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
373 s->type.ref = type->ref;
374 s->r = r | VT_CONST | VT_SYM;
376 return s;
379 /* define a new external reference to a symbol 'v' with alternate asm
380 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
381 is no alternate name (most cases) */
382 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
384 Sym *s;
386 s = sym_find(v);
387 if (!s) {
388 /* push forward reference */
389 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
390 s->asm_label = asm_label;
391 s->type.t |= VT_EXTERN;
392 } else if (s->type.ref == func_old_type.ref) {
393 s->type.ref = type->ref;
394 s->r = r | VT_CONST | VT_SYM;
395 s->type.t |= VT_EXTERN;
396 } else if (!is_compatible_types(&s->type, type)) {
397 error("incompatible types for redefinition of '%s'",
398 get_tok_str(v, NULL));
400 return s;
403 /* push a reference to global symbol v */
404 ST_FUNC void vpush_global_sym(CType *type, int v)
406 Sym *sym;
407 CValue cval;
409 sym = external_global_sym(v, type, 0);
410 cval.ul = 0;
411 vsetc(type, VT_CONST | VT_SYM, &cval);
412 vtop->sym = sym;
415 ST_FUNC void vset(CType *type, int r, int v)
417 CValue cval;
419 cval.i = v;
420 vsetc(type, r, &cval);
423 static void vseti(int r, int v)
425 CType type;
426 type.t = VT_INT;
427 type.ref = 0;
428 vset(&type, r, v);
431 ST_FUNC void vswap(void)
433 SValue tmp;
435 tmp = vtop[0];
436 vtop[0] = vtop[-1];
437 vtop[-1] = tmp;
440 ST_FUNC void vpushv(SValue *v)
442 if (vtop >= vstack + (VSTACK_SIZE - 1))
443 error("memory full");
444 vtop++;
445 *vtop = *v;
448 static void vdup(void)
450 vpushv(vtop);
453 /* save r to the memory stack, and mark it as being free */
454 ST_FUNC void save_reg(int r)
456 int l, saved, size, align;
457 SValue *p, sv;
458 CType *type;
460 /* modify all stack values */
461 saved = 0;
462 l = 0;
463 for(p=vstack;p<=vtop;p++) {
464 if ((p->r & VT_VALMASK) == r ||
465 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
466 /* must save value on stack if not already done */
467 if (!saved) {
468 /* NOTE: must reload 'r' because r might be equal to r2 */
469 r = p->r & VT_VALMASK;
470 /* store register in the stack */
471 type = &p->type;
472 if ((p->r & VT_LVAL) ||
473 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
474 #ifdef TCC_TARGET_X86_64
475 type = &char_pointer_type;
476 #else
477 type = &int_type;
478 #endif
479 size = type_size(type, &align);
480 loc = (loc - size) & -align;
481 sv.type.t = type->t;
482 sv.r = VT_LOCAL | VT_LVAL;
483 sv.c.ul = loc;
484 store(r, &sv);
485 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
486 /* x86 specific: need to pop fp register ST0 if saved */
487 if (r == TREG_ST0) {
488 o(0xd8dd); /* fstp %st(0) */
490 #endif
491 #ifndef TCC_TARGET_X86_64
492 /* special long long case */
493 if ((type->t & VT_BTYPE) == VT_LLONG) {
494 sv.c.ul += 4;
495 store(p->r2, &sv);
497 #endif
498 l = loc;
499 saved = 1;
501 /* mark that stack entry as being saved on the stack */
502 if (p->r & VT_LVAL) {
503 /* also clear the bounded flag because the
504 relocation address of the function was stored in
505 p->c.ul */
506 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
507 } else {
508 p->r = lvalue_type(p->type.t) | VT_LOCAL;
510 p->r2 = VT_CONST;
511 p->c.ul = l;
516 #ifdef TCC_TARGET_ARM
517 /* find a register of class 'rc2' with at most one reference on stack.
518 * If none, call get_reg(rc) */
519 ST_FUNC int get_reg_ex(int rc, int rc2)
521 int r;
522 SValue *p;
524 for(r=0;r<NB_REGS;r++) {
525 if (reg_classes[r] & rc2) {
526 int n;
527 n=0;
528 for(p = vstack; p <= vtop; p++) {
529 if ((p->r & VT_VALMASK) == r ||
530 (p->r2 & VT_VALMASK) == r)
531 n++;
533 if (n <= 1)
534 return r;
537 return get_reg(rc);
539 #endif
541 /* find a free register of class 'rc'. If none, save one register */
542 ST_FUNC int get_reg(int rc)
544 int r;
545 SValue *p;
547 /* find a free register */
548 for(r=0;r<NB_REGS;r++) {
549 if (reg_classes[r] & rc) {
550 for(p=vstack;p<=vtop;p++) {
551 if ((p->r & VT_VALMASK) == r ||
552 (p->r2 & VT_VALMASK) == r)
553 goto notfound;
555 return r;
557 notfound: ;
560 /* no register left : free the first one on the stack (VERY
561 IMPORTANT to start from the bottom to ensure that we don't
562 spill registers used in gen_opi()) */
563 for(p=vstack;p<=vtop;p++) {
564 r = p->r & VT_VALMASK;
565 if (r < VT_CONST && (reg_classes[r] & rc))
566 goto save_found;
567 /* also look at second register (if long long) */
568 r = p->r2 & VT_VALMASK;
569 if (r < VT_CONST && (reg_classes[r] & rc)) {
570 save_found:
571 save_reg(r);
572 return r;
575 /* Should never comes here */
576 return -1;
579 /* save registers up to (vtop - n) stack entry */
580 ST_FUNC void save_regs(int n)
582 int r;
583 SValue *p, *p1;
584 p1 = vtop - n;
585 for(p = vstack;p <= p1; p++) {
586 r = p->r & VT_VALMASK;
587 if (r < VT_CONST) {
588 save_reg(r);
593 /* move register 's' to 'r', and flush previous value of r to memory
594 if needed */
595 static void move_reg(int r, int s)
597 SValue sv;
599 if (r != s) {
600 save_reg(r);
601 sv.type.t = VT_INT;
602 sv.r = s;
603 sv.c.ul = 0;
604 load(r, &sv);
608 /* get address of vtop (vtop MUST BE an lvalue) */
609 static void gaddrof(void)
611 if (vtop->r & VT_REF)
612 gv(RC_INT);
613 vtop->r &= ~VT_LVAL;
614 /* tricky: if saved lvalue, then we can go back to lvalue */
615 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
616 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
621 #ifdef CONFIG_TCC_BCHECK
622 /* generate lvalue bound code */
623 static void gbound(void)
625 int lval_type;
626 CType type1;
628 vtop->r &= ~VT_MUSTBOUND;
629 /* if lvalue, then use checking code before dereferencing */
630 if (vtop->r & VT_LVAL) {
631 /* if not VT_BOUNDED value, then make one */
632 if (!(vtop->r & VT_BOUNDED)) {
633 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
634 /* must save type because we must set it to int to get pointer */
635 type1 = vtop->type;
636 vtop->type.t = VT_INT;
637 gaddrof();
638 vpushi(0);
639 gen_bounded_ptr_add();
640 vtop->r |= lval_type;
641 vtop->type = type1;
643 /* then check for dereferencing */
644 gen_bounded_ptr_deref();
647 #endif
649 /* store vtop a register belonging to class 'rc'. lvalues are
650 converted to values. Cannot be used if cannot be converted to
651 register value (such as structures). */
652 ST_FUNC int gv(int rc)
654 int r, bit_pos, bit_size, size, align, i;
655 #ifndef TCC_TARGET_X86_64
656 int rc2;
657 #endif
659 /* NOTE: get_reg can modify vstack[] */
660 if (vtop->type.t & VT_BITFIELD) {
661 CType type;
662 int bits = 32;
663 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
664 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
665 /* remove bit field info to avoid loops */
666 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
667 /* cast to int to propagate signedness in following ops */
668 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
669 type.t = VT_LLONG;
670 bits = 64;
671 } else
672 type.t = VT_INT;
673 if((vtop->type.t & VT_UNSIGNED) ||
674 (vtop->type.t & VT_BTYPE) == VT_BOOL)
675 type.t |= VT_UNSIGNED;
676 gen_cast(&type);
677 /* generate shifts */
678 vpushi(bits - (bit_pos + bit_size));
679 gen_op(TOK_SHL);
680 vpushi(bits - bit_size);
681 /* NOTE: transformed to SHR if unsigned */
682 gen_op(TOK_SAR);
683 r = gv(rc);
684 } else {
685 if (is_float(vtop->type.t) &&
686 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
687 Sym *sym;
688 int *ptr;
689 unsigned long offset;
690 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
691 CValue check;
692 #endif
694 /* XXX: unify with initializers handling ? */
695 /* CPUs usually cannot use float constants, so we store them
696 generically in data segment */
697 size = type_size(&vtop->type, &align);
698 offset = (data_section->data_offset + align - 1) & -align;
699 data_section->data_offset = offset;
700 /* XXX: not portable yet */
701 #if defined(__i386__) || defined(__x86_64__)
702 /* Zero pad x87 tenbyte long doubles */
703 if (size == LDOUBLE_SIZE)
704 vtop->c.tab[2] &= 0xffff;
705 #endif
706 ptr = section_ptr_add(data_section, size);
707 size = size >> 2;
708 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
709 check.d = 1;
710 if(check.tab[0])
711 for(i=0;i<size;i++)
712 ptr[i] = vtop->c.tab[size-1-i];
713 else
714 #endif
715 for(i=0;i<size;i++)
716 ptr[i] = vtop->c.tab[i];
717 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
718 vtop->r |= VT_LVAL | VT_SYM;
719 vtop->sym = sym;
720 vtop->c.ul = 0;
722 #ifdef CONFIG_TCC_BCHECK
723 if (vtop->r & VT_MUSTBOUND)
724 gbound();
725 #endif
727 r = vtop->r & VT_VALMASK;
728 #ifndef TCC_TARGET_X86_64
729 rc2 = RC_INT;
730 if (rc == RC_IRET)
731 rc2 = RC_LRET;
732 #endif
733 /* need to reload if:
734 - constant
735 - lvalue (need to dereference pointer)
736 - already a register, but not in the right class */
737 if (r >= VT_CONST
738 || (vtop->r & VT_LVAL)
739 || !(reg_classes[r] & rc)
740 #ifndef TCC_TARGET_X86_64
741 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
742 #endif
745 r = get_reg(rc);
746 #ifndef TCC_TARGET_X86_64
747 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
748 int r2;
749 unsigned long long ll;
750 /* two register type load : expand to two words
751 temporarily */
752 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
753 /* load constant */
754 ll = vtop->c.ull;
755 vtop->c.ui = ll; /* first word */
756 load(r, vtop);
757 vtop->r = r; /* save register value */
758 vpushi(ll >> 32); /* second word */
759 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
760 (vtop->r & VT_LVAL)) {
761 /* We do not want to modifier the long long
762 pointer here, so the safest (and less
763 efficient) is to save all the other registers
764 in the stack. XXX: totally inefficient. */
765 save_regs(1);
766 /* load from memory */
767 load(r, vtop);
768 vdup();
769 vtop[-1].r = r; /* save register value */
770 /* increment pointer to get second word */
771 vtop->type.t = VT_INT;
772 gaddrof();
773 vpushi(4);
774 gen_op('+');
775 vtop->r |= VT_LVAL;
776 } else {
777 /* move registers */
778 load(r, vtop);
779 vdup();
780 vtop[-1].r = r; /* save register value */
781 vtop->r = vtop[-1].r2;
783 /* allocate second register */
784 r2 = get_reg(rc2);
785 load(r2, vtop);
786 vpop();
787 /* write second register */
788 vtop->r2 = r2;
789 } else
790 #endif
791 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
792 int t1, t;
793 /* lvalue of scalar type : need to use lvalue type
794 because of possible cast */
795 t = vtop->type.t;
796 t1 = t;
797 /* compute memory access type */
798 if (vtop->r & VT_LVAL_BYTE)
799 t = VT_BYTE;
800 else if (vtop->r & VT_LVAL_SHORT)
801 t = VT_SHORT;
802 if (vtop->r & VT_LVAL_UNSIGNED)
803 t |= VT_UNSIGNED;
804 vtop->type.t = t;
805 load(r, vtop);
806 /* restore wanted type */
807 vtop->type.t = t1;
808 } else {
809 /* one register type load */
810 load(r, vtop);
813 vtop->r = r;
814 #ifdef TCC_TARGET_C67
815 /* uses register pairs for doubles */
816 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
817 vtop->r2 = r+1;
818 #endif
820 return r;
823 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
824 ST_FUNC void gv2(int rc1, int rc2)
826 int v;
828 /* generate more generic register first. But VT_JMP or VT_CMP
829 values must be generated first in all cases to avoid possible
830 reload errors */
831 v = vtop[0].r & VT_VALMASK;
832 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
833 vswap();
834 gv(rc1);
835 vswap();
836 gv(rc2);
837 /* test if reload is needed for first register */
838 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
839 vswap();
840 gv(rc1);
841 vswap();
843 } else {
844 gv(rc2);
845 vswap();
846 gv(rc1);
847 vswap();
848 /* test if reload is needed for first register */
849 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
850 gv(rc2);
855 /* wrapper around RC_FRET to return a register by type */
856 static int rc_fret(int t)
858 #ifdef TCC_TARGET_X86_64
859 if (t == VT_LDOUBLE) {
860 return RC_ST0;
862 #endif
863 return RC_FRET;
866 /* wrapper around REG_FRET to return a register by type */
867 static int reg_fret(int t)
869 #ifdef TCC_TARGET_X86_64
870 if (t == VT_LDOUBLE) {
871 return TREG_ST0;
873 #endif
874 return REG_FRET;
877 /* expand long long on stack in two int registers */
878 static void lexpand(void)
880 int u;
882 u = vtop->type.t & VT_UNSIGNED;
883 gv(RC_INT);
884 vdup();
885 vtop[0].r = vtop[-1].r2;
886 vtop[0].r2 = VT_CONST;
887 vtop[-1].r2 = VT_CONST;
888 vtop[0].type.t = VT_INT | u;
889 vtop[-1].type.t = VT_INT | u;
892 #ifdef TCC_TARGET_ARM
893 /* expand long long on stack */
894 ST_FUNC void lexpand_nr(void)
896 int u,v;
898 u = vtop->type.t & VT_UNSIGNED;
899 vdup();
900 vtop->r2 = VT_CONST;
901 vtop->type.t = VT_INT | u;
902 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
903 if (v == VT_CONST) {
904 vtop[-1].c.ui = vtop->c.ull;
905 vtop->c.ui = vtop->c.ull >> 32;
906 vtop->r = VT_CONST;
907 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
908 vtop->c.ui += 4;
909 vtop->r = vtop[-1].r;
910 } else if (v > VT_CONST) {
911 vtop--;
912 lexpand();
913 } else
914 vtop->r = vtop[-1].r2;
915 vtop[-1].r2 = VT_CONST;
916 vtop[-1].type.t = VT_INT | u;
918 #endif
920 /* build a long long from two ints */
921 static void lbuild(int t)
923 gv2(RC_INT, RC_INT);
924 vtop[-1].r2 = vtop[0].r;
925 vtop[-1].type.t = t;
926 vpop();
929 /* rotate n first stack elements to the bottom
930 I1 ... In -> I2 ... In I1 [top is right]
932 static void vrotb(int n)
934 int i;
935 SValue tmp;
937 tmp = vtop[-n + 1];
938 for(i=-n+1;i!=0;i++)
939 vtop[i] = vtop[i+1];
940 vtop[0] = tmp;
943 /* rotate n first stack elements to the top
944 I1 ... In -> In I1 ... I(n-1) [top is right]
946 ST_FUNC void vrott(int n)
948 int i;
949 SValue tmp;
951 tmp = vtop[0];
952 for(i = 0;i < n - 1; i++)
953 vtop[-i] = vtop[-i - 1];
954 vtop[-n + 1] = tmp;
957 #ifdef TCC_TARGET_ARM
958 /* like vrott but in other direction
959 In ... I1 -> I(n-1) ... I1 In [top is right]
961 ST_FUNC void vnrott(int n)
963 int i;
964 SValue tmp;
966 tmp = vtop[-n + 1];
967 for(i = n - 1; i > 0; i--)
968 vtop[-i] = vtop[-i + 1];
969 vtop[0] = tmp;
971 #endif
973 /* pop stack value */
974 ST_FUNC void vpop(void)
976 int v;
977 v = vtop->r & VT_VALMASK;
978 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
979 /* for x86, we need to pop the FP stack */
980 if (v == TREG_ST0 && !nocode_wanted) {
981 o(0xd8dd); /* fstp %st(0) */
982 } else
983 #endif
984 if (v == VT_JMP || v == VT_JMPI) {
985 /* need to put correct jump if && or || without test */
986 gsym(vtop->c.ul);
988 vtop--;
991 /* convert stack entry to register and duplicate its value in another
992 register */
993 static void gv_dup(void)
995 int rc, t, r, r1;
996 SValue sv;
998 t = vtop->type.t;
999 if ((t & VT_BTYPE) == VT_LLONG) {
1000 lexpand();
1001 gv_dup();
1002 vswap();
1003 vrotb(3);
1004 gv_dup();
1005 vrotb(4);
1006 /* stack: H L L1 H1 */
1007 lbuild(t);
1008 vrotb(3);
1009 vrotb(3);
1010 vswap();
1011 lbuild(t);
1012 vswap();
1013 } else {
1014 /* duplicate value */
1015 rc = RC_INT;
1016 sv.type.t = VT_INT;
1017 if (is_float(t)) {
1018 rc = RC_FLOAT;
1019 #ifdef TCC_TARGET_X86_64
1020 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1021 rc = RC_ST0;
1023 #endif
1024 sv.type.t = t;
1026 r = gv(rc);
1027 r1 = get_reg(rc);
1028 sv.r = r;
1029 sv.c.ul = 0;
1030 load(r1, &sv); /* move r to r1 */
1031 vdup();
1032 /* duplicates value */
1033 if (r != r1)
1034 vtop->r = r1;
1038 #ifndef TCC_TARGET_X86_64
1039 /* generate CPU independent (unsigned) long long operations */
1040 static void gen_opl(int op)
1042 int t, a, b, op1, c, i;
1043 int func;
1044 unsigned short reg_iret = REG_IRET;
1045 unsigned short reg_lret = REG_LRET;
1046 SValue tmp;
1048 switch(op) {
1049 case '/':
1050 case TOK_PDIV:
1051 func = TOK___divdi3;
1052 goto gen_func;
1053 case TOK_UDIV:
1054 func = TOK___udivdi3;
1055 goto gen_func;
1056 case '%':
1057 func = TOK___moddi3;
1058 goto gen_mod_func;
1059 case TOK_UMOD:
1060 func = TOK___umoddi3;
1061 gen_mod_func:
1062 #ifdef TCC_ARM_EABI
1063 reg_iret = TREG_R2;
1064 reg_lret = TREG_R3;
1065 #endif
1066 gen_func:
1067 /* call generic long long function */
1068 vpush_global_sym(&func_old_type, func);
1069 vrott(3);
1070 gfunc_call(2);
1071 vpushi(0);
1072 vtop->r = reg_iret;
1073 vtop->r2 = reg_lret;
1074 break;
1075 case '^':
1076 case '&':
1077 case '|':
1078 case '*':
1079 case '+':
1080 case '-':
1081 t = vtop->type.t;
1082 vswap();
1083 lexpand();
1084 vrotb(3);
1085 lexpand();
1086 /* stack: L1 H1 L2 H2 */
1087 tmp = vtop[0];
1088 vtop[0] = vtop[-3];
1089 vtop[-3] = tmp;
1090 tmp = vtop[-2];
1091 vtop[-2] = vtop[-3];
1092 vtop[-3] = tmp;
1093 vswap();
1094 /* stack: H1 H2 L1 L2 */
1095 if (op == '*') {
1096 vpushv(vtop - 1);
1097 vpushv(vtop - 1);
1098 gen_op(TOK_UMULL);
1099 lexpand();
1100 /* stack: H1 H2 L1 L2 ML MH */
1101 for(i=0;i<4;i++)
1102 vrotb(6);
1103 /* stack: ML MH H1 H2 L1 L2 */
1104 tmp = vtop[0];
1105 vtop[0] = vtop[-2];
1106 vtop[-2] = tmp;
1107 /* stack: ML MH H1 L2 H2 L1 */
1108 gen_op('*');
1109 vrotb(3);
1110 vrotb(3);
1111 gen_op('*');
1112 /* stack: ML MH M1 M2 */
1113 gen_op('+');
1114 gen_op('+');
1115 } else if (op == '+' || op == '-') {
1116 /* XXX: add non carry method too (for MIPS or alpha) */
1117 if (op == '+')
1118 op1 = TOK_ADDC1;
1119 else
1120 op1 = TOK_SUBC1;
1121 gen_op(op1);
1122 /* stack: H1 H2 (L1 op L2) */
1123 vrotb(3);
1124 vrotb(3);
1125 gen_op(op1 + 1); /* TOK_xxxC2 */
1126 } else {
1127 gen_op(op);
1128 /* stack: H1 H2 (L1 op L2) */
1129 vrotb(3);
1130 vrotb(3);
1131 /* stack: (L1 op L2) H1 H2 */
1132 gen_op(op);
1133 /* stack: (L1 op L2) (H1 op H2) */
1135 /* stack: L H */
1136 lbuild(t);
1137 break;
1138 case TOK_SAR:
1139 case TOK_SHR:
1140 case TOK_SHL:
1141 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1142 t = vtop[-1].type.t;
1143 vswap();
1144 lexpand();
1145 vrotb(3);
1146 /* stack: L H shift */
1147 c = (int)vtop->c.i;
1148 /* constant: simpler */
1149 /* NOTE: all comments are for SHL. the other cases are
1150 done by swaping words */
1151 vpop();
1152 if (op != TOK_SHL)
1153 vswap();
1154 if (c >= 32) {
1155 /* stack: L H */
1156 vpop();
1157 if (c > 32) {
1158 vpushi(c - 32);
1159 gen_op(op);
1161 if (op != TOK_SAR) {
1162 vpushi(0);
1163 } else {
1164 gv_dup();
1165 vpushi(31);
1166 gen_op(TOK_SAR);
1168 vswap();
1169 } else {
1170 vswap();
1171 gv_dup();
1172 /* stack: H L L */
1173 vpushi(c);
1174 gen_op(op);
1175 vswap();
1176 vpushi(32 - c);
1177 if (op == TOK_SHL)
1178 gen_op(TOK_SHR);
1179 else
1180 gen_op(TOK_SHL);
1181 vrotb(3);
1182 /* stack: L L H */
1183 vpushi(c);
1184 if (op == TOK_SHL)
1185 gen_op(TOK_SHL);
1186 else
1187 gen_op(TOK_SHR);
1188 gen_op('|');
1190 if (op != TOK_SHL)
1191 vswap();
1192 lbuild(t);
1193 } else {
1194 /* XXX: should provide a faster fallback on x86 ? */
1195 switch(op) {
1196 case TOK_SAR:
1197 func = TOK___ashrdi3;
1198 goto gen_func;
1199 case TOK_SHR:
1200 func = TOK___lshrdi3;
1201 goto gen_func;
1202 case TOK_SHL:
1203 func = TOK___ashldi3;
1204 goto gen_func;
1207 break;
1208 default:
1209 /* compare operations */
1210 t = vtop->type.t;
1211 vswap();
1212 lexpand();
1213 vrotb(3);
1214 lexpand();
1215 /* stack: L1 H1 L2 H2 */
1216 tmp = vtop[-1];
1217 vtop[-1] = vtop[-2];
1218 vtop[-2] = tmp;
1219 /* stack: L1 L2 H1 H2 */
1220 /* compare high */
1221 op1 = op;
1222 /* when values are equal, we need to compare low words. since
1223 the jump is inverted, we invert the test too. */
1224 if (op1 == TOK_LT)
1225 op1 = TOK_LE;
1226 else if (op1 == TOK_GT)
1227 op1 = TOK_GE;
1228 else if (op1 == TOK_ULT)
1229 op1 = TOK_ULE;
1230 else if (op1 == TOK_UGT)
1231 op1 = TOK_UGE;
1232 a = 0;
1233 b = 0;
1234 gen_op(op1);
1235 if (op1 != TOK_NE) {
1236 a = gtst(1, 0);
1238 if (op != TOK_EQ) {
1239 /* generate non equal test */
1240 /* XXX: NOT PORTABLE yet */
1241 if (a == 0) {
1242 b = gtst(0, 0);
1243 } else {
1244 #if defined(TCC_TARGET_I386)
1245 b = psym(0x850f, 0);
1246 #elif defined(TCC_TARGET_ARM)
1247 b = ind;
1248 o(0x1A000000 | encbranch(ind, 0, 1));
1249 #elif defined(TCC_TARGET_C67)
1250 error("not implemented");
1251 #else
1252 #error not supported
1253 #endif
1256 /* compare low. Always unsigned */
1257 op1 = op;
1258 if (op1 == TOK_LT)
1259 op1 = TOK_ULT;
1260 else if (op1 == TOK_LE)
1261 op1 = TOK_ULE;
1262 else if (op1 == TOK_GT)
1263 op1 = TOK_UGT;
1264 else if (op1 == TOK_GE)
1265 op1 = TOK_UGE;
1266 gen_op(op1);
1267 a = gtst(1, a);
1268 gsym(b);
1269 vseti(VT_JMPI, a);
1270 break;
1273 #endif
1275 /* handle integer constant optimizations and various machine
1276 independent opt */
1277 static void gen_opic(int op)
1279 int c1, c2, t1, t2, n;
1280 SValue *v1, *v2;
1281 long long l1, l2;
1282 typedef unsigned long long U;
1284 v1 = vtop - 1;
1285 v2 = vtop;
1286 t1 = v1->type.t & VT_BTYPE;
1287 t2 = v2->type.t & VT_BTYPE;
1289 if (t1 == VT_LLONG)
1290 l1 = v1->c.ll;
1291 else if (v1->type.t & VT_UNSIGNED)
1292 l1 = v1->c.ui;
1293 else
1294 l1 = v1->c.i;
1296 if (t2 == VT_LLONG)
1297 l2 = v2->c.ll;
1298 else if (v2->type.t & VT_UNSIGNED)
1299 l2 = v2->c.ui;
1300 else
1301 l2 = v2->c.i;
1303 /* currently, we cannot do computations with forward symbols */
1304 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1305 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1306 if (c1 && c2) {
1307 switch(op) {
1308 case '+': l1 += l2; break;
1309 case '-': l1 -= l2; break;
1310 case '&': l1 &= l2; break;
1311 case '^': l1 ^= l2; break;
1312 case '|': l1 |= l2; break;
1313 case '*': l1 *= l2; break;
1315 case TOK_PDIV:
1316 case '/':
1317 case '%':
1318 case TOK_UDIV:
1319 case TOK_UMOD:
1320 /* if division by zero, generate explicit division */
1321 if (l2 == 0) {
1322 if (const_wanted)
1323 error("division by zero in constant");
1324 goto general_case;
1326 switch(op) {
1327 default: l1 /= l2; break;
1328 case '%': l1 %= l2; break;
1329 case TOK_UDIV: l1 = (U)l1 / l2; break;
1330 case TOK_UMOD: l1 = (U)l1 % l2; break;
1332 break;
1333 case TOK_SHL: l1 <<= l2; break;
1334 case TOK_SHR: l1 = (U)l1 >> l2; break;
1335 case TOK_SAR: l1 >>= l2; break;
1336 /* tests */
1337 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1338 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1339 case TOK_EQ: l1 = l1 == l2; break;
1340 case TOK_NE: l1 = l1 != l2; break;
1341 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1342 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1343 case TOK_LT: l1 = l1 < l2; break;
1344 case TOK_GE: l1 = l1 >= l2; break;
1345 case TOK_LE: l1 = l1 <= l2; break;
1346 case TOK_GT: l1 = l1 > l2; break;
1347 /* logical */
1348 case TOK_LAND: l1 = l1 && l2; break;
1349 case TOK_LOR: l1 = l1 || l2; break;
1350 default:
1351 goto general_case;
1353 v1->c.ll = l1;
1354 vtop--;
1355 } else {
1356 /* if commutative ops, put c2 as constant */
1357 if (c1 && (op == '+' || op == '&' || op == '^' ||
1358 op == '|' || op == '*')) {
1359 vswap();
1360 c2 = c1; //c = c1, c1 = c2, c2 = c;
1361 l2 = l1; //l = l1, l1 = l2, l2 = l;
1363 /* Filter out NOP operations like x*1, x-0, x&-1... */
1364 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1365 op == TOK_PDIV) &&
1366 l2 == 1) ||
1367 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1368 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1369 l2 == 0) ||
1370 (op == '&' &&
1371 l2 == -1))) {
1372 /* nothing to do */
1373 vtop--;
1374 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1375 /* try to use shifts instead of muls or divs */
1376 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1377 n = -1;
1378 while (l2) {
1379 l2 >>= 1;
1380 n++;
1382 vtop->c.ll = n;
1383 if (op == '*')
1384 op = TOK_SHL;
1385 else if (op == TOK_PDIV)
1386 op = TOK_SAR;
1387 else
1388 op = TOK_SHR;
1390 goto general_case;
1391 } else if (c2 && (op == '+' || op == '-') &&
1392 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1393 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1394 /* symbol + constant case */
1395 if (op == '-')
1396 l2 = -l2;
1397 vtop--;
1398 vtop->c.ll += l2;
1399 } else {
1400 general_case:
1401 if (!nocode_wanted) {
1402 /* call low level op generator */
1403 if (t1 == VT_LLONG || t2 == VT_LLONG)
1404 gen_opl(op);
1405 else
1406 gen_opi(op);
1407 } else {
1408 vtop--;
1414 /* generate a floating point operation with constant propagation */
1415 static void gen_opif(int op)
1417 int c1, c2;
1418 SValue *v1, *v2;
1419 long double f1, f2;
1421 v1 = vtop - 1;
1422 v2 = vtop;
1423 /* currently, we cannot do computations with forward symbols */
1424 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1425 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1426 if (c1 && c2) {
1427 if (v1->type.t == VT_FLOAT) {
1428 f1 = v1->c.f;
1429 f2 = v2->c.f;
1430 } else if (v1->type.t == VT_DOUBLE) {
1431 f1 = v1->c.d;
1432 f2 = v2->c.d;
1433 } else {
1434 f1 = v1->c.ld;
1435 f2 = v2->c.ld;
1438 /* NOTE: we only do constant propagation if finite number (not
1439 NaN or infinity) (ANSI spec) */
1440 if (!ieee_finite(f1) || !ieee_finite(f2))
1441 goto general_case;
1443 switch(op) {
1444 case '+': f1 += f2; break;
1445 case '-': f1 -= f2; break;
1446 case '*': f1 *= f2; break;
1447 case '/':
1448 if (f2 == 0.0) {
1449 if (const_wanted)
1450 error("division by zero in constant");
1451 goto general_case;
1453 f1 /= f2;
1454 break;
1455 /* XXX: also handles tests ? */
1456 default:
1457 goto general_case;
1459 /* XXX: overflow test ? */
1460 if (v1->type.t == VT_FLOAT) {
1461 v1->c.f = f1;
1462 } else if (v1->type.t == VT_DOUBLE) {
1463 v1->c.d = f1;
1464 } else {
1465 v1->c.ld = f1;
1467 vtop--;
1468 } else {
1469 general_case:
1470 if (!nocode_wanted) {
1471 gen_opf(op);
1472 } else {
1473 vtop--;
1478 static int pointed_size(CType *type)
1480 int align;
1481 return type_size(pointed_type(type), &align);
1484 static void vla_runtime_pointed_size(CType *type)
1486 int align;
1487 vla_runtime_type_size(pointed_type(type), &align);
1490 static inline int is_null_pointer(SValue *p)
1492 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1493 return 0;
1494 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1495 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1498 static inline int is_integer_btype(int bt)
1500 return (bt == VT_BYTE || bt == VT_SHORT ||
1501 bt == VT_INT || bt == VT_LLONG);
1504 /* check types for comparison or substraction of pointers */
1505 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1507 CType *type1, *type2, tmp_type1, tmp_type2;
1508 int bt1, bt2;
1510 /* null pointers are accepted for all comparisons as gcc */
1511 if (is_null_pointer(p1) || is_null_pointer(p2))
1512 return;
1513 type1 = &p1->type;
1514 type2 = &p2->type;
1515 bt1 = type1->t & VT_BTYPE;
1516 bt2 = type2->t & VT_BTYPE;
1517 /* accept comparison between pointer and integer with a warning */
1518 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1519 if (op != TOK_LOR && op != TOK_LAND )
1520 warning("comparison between pointer and integer");
1521 return;
1524 /* both must be pointers or implicit function pointers */
1525 if (bt1 == VT_PTR) {
1526 type1 = pointed_type(type1);
1527 } else if (bt1 != VT_FUNC)
1528 goto invalid_operands;
1530 if (bt2 == VT_PTR) {
1531 type2 = pointed_type(type2);
1532 } else if (bt2 != VT_FUNC) {
1533 invalid_operands:
1534 error("invalid operands to binary %s", get_tok_str(op, NULL));
1536 if ((type1->t & VT_BTYPE) == VT_VOID ||
1537 (type2->t & VT_BTYPE) == VT_VOID)
1538 return;
1539 tmp_type1 = *type1;
1540 tmp_type2 = *type2;
1541 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1542 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1543 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1544 /* gcc-like error if '-' is used */
1545 if (op == '-')
1546 goto invalid_operands;
1547 else
1548 warning("comparison of distinct pointer types lacks a cast");
1552 /* generic gen_op: handles types problems */
1553 ST_FUNC void gen_op(int op)
1555 int u, t1, t2, bt1, bt2, t;
1556 CType type1;
1558 t1 = vtop[-1].type.t;
1559 t2 = vtop[0].type.t;
1560 bt1 = t1 & VT_BTYPE;
1561 bt2 = t2 & VT_BTYPE;
1563 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1564 /* at least one operand is a pointer */
1565 /* relationnal op: must be both pointers */
1566 if (op >= TOK_ULT && op <= TOK_LOR) {
1567 check_comparison_pointer_types(vtop - 1, vtop, op);
1568 /* pointers are handled are unsigned */
1569 #ifdef TCC_TARGET_X86_64
1570 t = VT_LLONG | VT_UNSIGNED;
1571 #else
1572 t = VT_INT | VT_UNSIGNED;
1573 #endif
1574 goto std_op;
1576 /* if both pointers, then it must be the '-' op */
1577 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1578 if (op != '-')
1579 error("cannot use pointers here");
1580 check_comparison_pointer_types(vtop - 1, vtop, op);
1581 /* XXX: check that types are compatible */
1582 if (vtop[-1].type.t & VT_VLA) {
1583 vla_runtime_pointed_size(&vtop[-1].type);
1584 } else {
1585 vpushi(pointed_size(&vtop[-1].type));
1587 vrott(3);
1588 gen_opic(op);
1589 /* set to integer type */
1590 #ifdef TCC_TARGET_X86_64
1591 vtop->type.t = VT_LLONG;
1592 #else
1593 vtop->type.t = VT_INT;
1594 #endif
1595 vswap();
1596 gen_op(TOK_PDIV);
1597 } else {
1598 /* exactly one pointer : must be '+' or '-'. */
1599 if (op != '-' && op != '+')
1600 error("cannot use pointers here");
1601 /* Put pointer as first operand */
1602 if (bt2 == VT_PTR) {
1603 vswap();
1604 swap(&t1, &t2);
1606 type1 = vtop[-1].type;
1607 type1.t &= ~VT_ARRAY;
1608 if (vtop[-1].type.t & VT_VLA)
1609 vla_runtime_pointed_size(&vtop[-1].type);
1610 else {
1611 u = pointed_size(&vtop[-1].type);
1612 if (u < 0)
1613 error("unknown array element size");
1614 #ifdef TCC_TARGET_X86_64
1615 vpushll(u);
1616 #else
1617 /* XXX: cast to int ? (long long case) */
1618 vpushi(u);
1619 #endif
1621 gen_op('*');
1622 #ifdef CONFIG_TCC_BCHECK
1623 /* if evaluating constant expression, no code should be
1624 generated, so no bound check */
1625 if (tcc_state->do_bounds_check && !const_wanted) {
1626 /* if bounded pointers, we generate a special code to
1627 test bounds */
1628 if (op == '-') {
1629 vpushi(0);
1630 vswap();
1631 gen_op('-');
1633 gen_bounded_ptr_add();
1634 } else
1635 #endif
1637 gen_opic(op);
1639 /* put again type if gen_opic() swaped operands */
1640 vtop->type = type1;
1642 } else if (is_float(bt1) || is_float(bt2)) {
1643 /* compute bigger type and do implicit casts */
1644 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1645 t = VT_LDOUBLE;
1646 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1647 t = VT_DOUBLE;
1648 } else {
1649 t = VT_FLOAT;
1651 /* floats can only be used for a few operations */
1652 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1653 (op < TOK_ULT || op > TOK_GT))
1654 error("invalid operands for binary operation");
1655 goto std_op;
1656 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1657 /* cast to biggest op */
1658 t = VT_LLONG;
1659 /* convert to unsigned if it does not fit in a long long */
1660 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1661 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1662 t |= VT_UNSIGNED;
1663 goto std_op;
1664 } else {
1665 /* integer operations */
1666 t = VT_INT;
1667 /* convert to unsigned if it does not fit in an integer */
1668 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1669 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1670 t |= VT_UNSIGNED;
1671 std_op:
1672 /* XXX: currently, some unsigned operations are explicit, so
1673 we modify them here */
1674 if (t & VT_UNSIGNED) {
1675 if (op == TOK_SAR)
1676 op = TOK_SHR;
1677 else if (op == '/')
1678 op = TOK_UDIV;
1679 else if (op == '%')
1680 op = TOK_UMOD;
1681 else if (op == TOK_LT)
1682 op = TOK_ULT;
1683 else if (op == TOK_GT)
1684 op = TOK_UGT;
1685 else if (op == TOK_LE)
1686 op = TOK_ULE;
1687 else if (op == TOK_GE)
1688 op = TOK_UGE;
1690 vswap();
1691 type1.t = t;
1692 gen_cast(&type1);
1693 vswap();
1694 /* special case for shifts and long long: we keep the shift as
1695 an integer */
1696 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1697 type1.t = VT_INT;
1698 gen_cast(&type1);
1699 if (is_float(t))
1700 gen_opif(op);
1701 else
1702 gen_opic(op);
1703 if (op >= TOK_ULT && op <= TOK_GT) {
1704 /* relationnal op: the result is an int */
1705 vtop->type.t = VT_INT;
1706 } else {
1707 vtop->type.t = t;
1712 #ifndef TCC_TARGET_ARM
1713 /* generic itof for unsigned long long case */
1714 static void gen_cvt_itof1(int t)
1716 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1717 (VT_LLONG | VT_UNSIGNED)) {
1719 if (t == VT_FLOAT)
1720 vpush_global_sym(&func_old_type, TOK___floatundisf);
1721 #if LDOUBLE_SIZE != 8
1722 else if (t == VT_LDOUBLE)
1723 vpush_global_sym(&func_old_type, TOK___floatundixf);
1724 #endif
1725 else
1726 vpush_global_sym(&func_old_type, TOK___floatundidf);
1727 vrott(2);
1728 gfunc_call(1);
1729 vpushi(0);
1730 vtop->r = reg_fret(t);
1731 } else {
1732 gen_cvt_itof(t);
1735 #endif
1737 /* generic ftoi for unsigned long long case */
1738 static void gen_cvt_ftoi1(int t)
1740 int st;
1742 if (t == (VT_LLONG | VT_UNSIGNED)) {
1743 /* not handled natively */
1744 st = vtop->type.t & VT_BTYPE;
1745 if (st == VT_FLOAT)
1746 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1747 #if LDOUBLE_SIZE != 8
1748 else if (st == VT_LDOUBLE)
1749 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1750 #endif
1751 else
1752 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1753 vrott(2);
1754 gfunc_call(1);
1755 vpushi(0);
1756 vtop->r = REG_IRET;
1757 vtop->r2 = REG_LRET;
1758 } else {
1759 gen_cvt_ftoi(t);
1763 /* force char or short cast */
1764 static void force_charshort_cast(int t)
1766 int bits, dbt;
1767 dbt = t & VT_BTYPE;
1768 /* XXX: add optimization if lvalue : just change type and offset */
1769 if (dbt == VT_BYTE)
1770 bits = 8;
1771 else
1772 bits = 16;
1773 if (t & VT_UNSIGNED) {
1774 vpushi((1 << bits) - 1);
1775 gen_op('&');
1776 } else {
1777 bits = 32 - bits;
1778 vpushi(bits);
1779 gen_op(TOK_SHL);
1780 /* result must be signed or the SAR is converted to an SHL
1781 This was not the case when "t" was a signed short
1782 and the last value on the stack was an unsigned int */
1783 vtop->type.t &= ~VT_UNSIGNED;
1784 vpushi(bits);
1785 gen_op(TOK_SAR);
1789 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1790 static void gen_cast(CType *type)
1792 int sbt, dbt, sf, df, c, p;
1794 /* special delayed cast for char/short */
1795 /* XXX: in some cases (multiple cascaded casts), it may still
1796 be incorrect */
1797 if (vtop->r & VT_MUSTCAST) {
1798 vtop->r &= ~VT_MUSTCAST;
1799 force_charshort_cast(vtop->type.t);
1802 /* bitfields first get cast to ints */
1803 if (vtop->type.t & VT_BITFIELD) {
1804 gv(RC_INT);
1807 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1808 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1810 if (sbt != dbt) {
1811 sf = is_float(sbt);
1812 df = is_float(dbt);
1813 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1814 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1815 if (c) {
1816 /* constant case: we can do it now */
1817 /* XXX: in ISOC, cannot do it if error in convert */
1818 if (sbt == VT_FLOAT)
1819 vtop->c.ld = vtop->c.f;
1820 else if (sbt == VT_DOUBLE)
1821 vtop->c.ld = vtop->c.d;
1823 if (df) {
1824 if ((sbt & VT_BTYPE) == VT_LLONG) {
1825 if (sbt & VT_UNSIGNED)
1826 vtop->c.ld = vtop->c.ull;
1827 else
1828 vtop->c.ld = vtop->c.ll;
1829 } else if(!sf) {
1830 if (sbt & VT_UNSIGNED)
1831 vtop->c.ld = vtop->c.ui;
1832 else
1833 vtop->c.ld = vtop->c.i;
1836 if (dbt == VT_FLOAT)
1837 vtop->c.f = (float)vtop->c.ld;
1838 else if (dbt == VT_DOUBLE)
1839 vtop->c.d = (double)vtop->c.ld;
1840 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1841 vtop->c.ull = (unsigned long long)vtop->c.ld;
1842 } else if (sf && dbt == VT_BOOL) {
1843 vtop->c.i = (vtop->c.ld != 0);
1844 } else {
1845 if(sf)
1846 vtop->c.ll = (long long)vtop->c.ld;
1847 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1848 vtop->c.ll = vtop->c.ull;
1849 else if (sbt & VT_UNSIGNED)
1850 vtop->c.ll = vtop->c.ui;
1851 #ifdef TCC_TARGET_X86_64
1852 else if (sbt == VT_PTR)
1854 #endif
1855 else if (sbt != VT_LLONG)
1856 vtop->c.ll = vtop->c.i;
1858 if (dbt == (VT_LLONG|VT_UNSIGNED))
1859 vtop->c.ull = vtop->c.ll;
1860 else if (dbt == VT_BOOL)
1861 vtop->c.i = (vtop->c.ll != 0);
1862 else if (dbt != VT_LLONG) {
1863 int s = 0;
1864 if ((dbt & VT_BTYPE) == VT_BYTE)
1865 s = 24;
1866 else if ((dbt & VT_BTYPE) == VT_SHORT)
1867 s = 16;
1869 if(dbt & VT_UNSIGNED)
1870 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1871 else
1872 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1875 } else if (p && dbt == VT_BOOL) {
1876 vtop->r = VT_CONST;
1877 vtop->c.i = 1;
1878 } else if (!nocode_wanted) {
1879 /* non constant case: generate code */
1880 if (sf && df) {
1881 /* convert from fp to fp */
1882 gen_cvt_ftof(dbt);
1883 } else if (df) {
1884 /* convert int to fp */
1885 gen_cvt_itof1(dbt);
1886 } else if (sf) {
1887 /* convert fp to int */
1888 if (dbt == VT_BOOL) {
1889 vpushi(0);
1890 gen_op(TOK_NE);
1891 } else {
1892 /* we handle char/short/etc... with generic code */
1893 if (dbt != (VT_INT | VT_UNSIGNED) &&
1894 dbt != (VT_LLONG | VT_UNSIGNED) &&
1895 dbt != VT_LLONG)
1896 dbt = VT_INT;
1897 gen_cvt_ftoi1(dbt);
1898 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1899 /* additional cast for char/short... */
1900 vtop->type.t = dbt;
1901 gen_cast(type);
1904 #ifndef TCC_TARGET_X86_64
1905 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1906 if ((sbt & VT_BTYPE) != VT_LLONG) {
1907 /* scalar to long long */
1908 /* machine independent conversion */
1909 gv(RC_INT);
1910 /* generate high word */
1911 if (sbt == (VT_INT | VT_UNSIGNED)) {
1912 vpushi(0);
1913 gv(RC_INT);
1914 } else {
1915 if (sbt == VT_PTR) {
1916 /* cast from pointer to int before we apply
1917 shift operation, which pointers don't support*/
1918 gen_cast(&int_type);
1920 gv_dup();
1921 vpushi(31);
1922 gen_op(TOK_SAR);
1924 /* patch second register */
1925 vtop[-1].r2 = vtop->r;
1926 vpop();
1928 #else
1929 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1930 (dbt & VT_BTYPE) == VT_PTR ||
1931 (dbt & VT_BTYPE) == VT_FUNC) {
1932 if ((sbt & VT_BTYPE) != VT_LLONG &&
1933 (sbt & VT_BTYPE) != VT_PTR &&
1934 (sbt & VT_BTYPE) != VT_FUNC) {
1935 /* need to convert from 32bit to 64bit */
1936 int r = gv(RC_INT);
1937 if (sbt != (VT_INT | VT_UNSIGNED)) {
1938 /* x86_64 specific: movslq */
1939 o(0x6348);
1940 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1943 #endif
1944 } else if (dbt == VT_BOOL) {
1945 /* scalar to bool */
1946 vpushi(0);
1947 gen_op(TOK_NE);
1948 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1949 (dbt & VT_BTYPE) == VT_SHORT) {
1950 if (sbt == VT_PTR) {
1951 vtop->type.t = VT_INT;
1952 warning("nonportable conversion from pointer to char/short");
1954 force_charshort_cast(dbt);
1955 } else if ((dbt & VT_BTYPE) == VT_INT) {
1956 /* scalar to int */
1957 if (sbt == VT_LLONG) {
1958 /* from long long: just take low order word */
1959 lexpand();
1960 vpop();
1962 /* if lvalue and single word type, nothing to do because
1963 the lvalue already contains the real type size (see
1964 VT_LVAL_xxx constants) */
1967 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1968 /* if we are casting between pointer types,
1969 we must update the VT_LVAL_xxx size */
1970 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1971 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1973 vtop->type = *type;
1976 /* return type size as known at compile time. Put alignment at 'a' */
1977 ST_FUNC int type_size(CType *type, int *a)
1979 Sym *s;
1980 int bt;
1982 bt = type->t & VT_BTYPE;
1983 if (bt == VT_STRUCT) {
1984 /* struct/union */
1985 s = type->ref;
1986 *a = s->r;
1987 return s->c;
1988 } else if (bt == VT_PTR) {
1989 if (type->t & VT_ARRAY) {
1990 int ts;
1992 s = type->ref;
1993 ts = type_size(&s->type, a);
1995 if (ts < 0 && s->c < 0)
1996 ts = -ts;
1998 return ts * s->c;
1999 } else {
2000 *a = PTR_SIZE;
2001 return PTR_SIZE;
2003 } else if (bt == VT_LDOUBLE) {
2004 *a = LDOUBLE_ALIGN;
2005 return LDOUBLE_SIZE;
2006 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2007 #ifdef TCC_TARGET_I386
2008 #ifdef TCC_TARGET_PE
2009 *a = 8;
2010 #else
2011 *a = 4;
2012 #endif
2013 #elif defined(TCC_TARGET_ARM)
2014 #ifdef TCC_ARM_EABI
2015 *a = 8;
2016 #else
2017 *a = 4;
2018 #endif
2019 #else
2020 *a = 8;
2021 #endif
2022 return 8;
2023 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2024 *a = 4;
2025 return 4;
2026 } else if (bt == VT_SHORT) {
2027 *a = 2;
2028 return 2;
2029 } else {
2030 /* char, void, function, _Bool */
2031 *a = 1;
2032 return 1;
2036 /* push type size as known at runtime time on top of value stack. Put
2037 alignment at 'a' */
2038 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2040 if (type->t & VT_VLA) {
2041 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2042 } else {
2043 vpushi(type_size(type, a));
2047 /* return the pointed type of t */
2048 static inline CType *pointed_type(CType *type)
2050 return &type->ref->type;
2053 /* modify type so that its it is a pointer to type. */
2054 ST_FUNC void mk_pointer(CType *type)
2056 Sym *s;
2057 s = sym_push(SYM_FIELD, type, 0, -1);
2058 type->t = VT_PTR | (type->t & ~VT_TYPE);
2059 type->ref = s;
2062 /* compare function types. OLD functions match any new functions */
2063 static int is_compatible_func(CType *type1, CType *type2)
2065 Sym *s1, *s2;
2067 s1 = type1->ref;
2068 s2 = type2->ref;
2069 if (!is_compatible_types(&s1->type, &s2->type))
2070 return 0;
2071 /* check func_call */
2072 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2073 return 0;
2074 /* XXX: not complete */
2075 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2076 return 1;
2077 if (s1->c != s2->c)
2078 return 0;
2079 while (s1 != NULL) {
2080 if (s2 == NULL)
2081 return 0;
2082 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2083 return 0;
2084 s1 = s1->next;
2085 s2 = s2->next;
2087 if (s2)
2088 return 0;
2089 return 1;
2092 /* return true if type1 and type2 are the same. If unqualified is
2093 true, qualifiers on the types are ignored.
2095 - enums are not checked as gcc __builtin_types_compatible_p ()
2097 static int compare_types(CType *type1, CType *type2, int unqualified)
2099 int bt1, t1, t2;
2101 t1 = type1->t & VT_TYPE;
2102 t2 = type2->t & VT_TYPE;
2103 if (unqualified) {
2104 /* strip qualifiers before comparing */
2105 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2106 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2108 /* XXX: bitfields ? */
2109 if (t1 != t2)
2110 return 0;
2111 /* test more complicated cases */
2112 bt1 = t1 & VT_BTYPE;
2113 if (bt1 == VT_PTR) {
2114 type1 = pointed_type(type1);
2115 type2 = pointed_type(type2);
2116 return is_compatible_types(type1, type2);
2117 } else if (bt1 == VT_STRUCT) {
2118 return (type1->ref == type2->ref);
2119 } else if (bt1 == VT_FUNC) {
2120 return is_compatible_func(type1, type2);
2121 } else {
2122 return 1;
2126 /* return true if type1 and type2 are exactly the same (including
2127 qualifiers).
2129 static int is_compatible_types(CType *type1, CType *type2)
2131 return compare_types(type1,type2,0);
2134 /* return true if type1 and type2 are the same (ignoring qualifiers).
2136 static int is_compatible_parameter_types(CType *type1, CType *type2)
2138 return compare_types(type1,type2,1);
2141 /* print a type. If 'varstr' is not NULL, then the variable is also
2142 printed in the type */
2143 /* XXX: union */
2144 /* XXX: add array and function pointers */
2145 static void type_to_str(char *buf, int buf_size,
2146 CType *type, const char *varstr)
2148 int bt, v, t;
2149 Sym *s, *sa;
2150 char buf1[256];
2151 const char *tstr;
2153 t = type->t & VT_TYPE;
2154 bt = t & VT_BTYPE;
2155 buf[0] = '\0';
2156 if (t & VT_CONSTANT)
2157 pstrcat(buf, buf_size, "const ");
2158 if (t & VT_VOLATILE)
2159 pstrcat(buf, buf_size, "volatile ");
2160 if (t & VT_UNSIGNED)
2161 pstrcat(buf, buf_size, "unsigned ");
2162 switch(bt) {
2163 case VT_VOID:
2164 tstr = "void";
2165 goto add_tstr;
2166 case VT_BOOL:
2167 tstr = "_Bool";
2168 goto add_tstr;
2169 case VT_BYTE:
2170 tstr = "char";
2171 goto add_tstr;
2172 case VT_SHORT:
2173 tstr = "short";
2174 goto add_tstr;
2175 case VT_INT:
2176 tstr = "int";
2177 goto add_tstr;
2178 case VT_LONG:
2179 tstr = "long";
2180 goto add_tstr;
2181 case VT_LLONG:
2182 tstr = "long long";
2183 goto add_tstr;
2184 case VT_FLOAT:
2185 tstr = "float";
2186 goto add_tstr;
2187 case VT_DOUBLE:
2188 tstr = "double";
2189 goto add_tstr;
2190 case VT_LDOUBLE:
2191 tstr = "long double";
2192 add_tstr:
2193 pstrcat(buf, buf_size, tstr);
2194 break;
2195 case VT_ENUM:
2196 case VT_STRUCT:
2197 if (bt == VT_STRUCT)
2198 tstr = "struct ";
2199 else
2200 tstr = "enum ";
2201 pstrcat(buf, buf_size, tstr);
2202 v = type->ref->v & ~SYM_STRUCT;
2203 if (v >= SYM_FIRST_ANOM)
2204 pstrcat(buf, buf_size, "<anonymous>");
2205 else
2206 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2207 break;
2208 case VT_FUNC:
2209 s = type->ref;
2210 type_to_str(buf, buf_size, &s->type, varstr);
2211 pstrcat(buf, buf_size, "(");
2212 sa = s->next;
2213 while (sa != NULL) {
2214 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2215 pstrcat(buf, buf_size, buf1);
2216 sa = sa->next;
2217 if (sa)
2218 pstrcat(buf, buf_size, ", ");
2220 pstrcat(buf, buf_size, ")");
2221 goto no_var;
2222 case VT_PTR:
2223 s = type->ref;
2224 pstrcpy(buf1, sizeof(buf1), "*");
2225 if (varstr)
2226 pstrcat(buf1, sizeof(buf1), varstr);
2227 type_to_str(buf, buf_size, &s->type, buf1);
2228 goto no_var;
2230 if (varstr) {
2231 pstrcat(buf, buf_size, " ");
2232 pstrcat(buf, buf_size, varstr);
2234 no_var: ;
2237 /* verify type compatibility to store vtop in 'dt' type, and generate
2238 casts if needed. */
2239 static void gen_assign_cast(CType *dt)
2241 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2242 char buf1[256], buf2[256];
2243 int dbt, sbt;
2245 st = &vtop->type; /* source type */
2246 dbt = dt->t & VT_BTYPE;
2247 sbt = st->t & VT_BTYPE;
2248 if (dt->t & VT_CONSTANT)
2249 warning("assignment of read-only location");
2250 switch(dbt) {
2251 case VT_PTR:
2252 /* special cases for pointers */
2253 /* '0' can also be a pointer */
2254 if (is_null_pointer(vtop))
2255 goto type_ok;
2256 /* accept implicit pointer to integer cast with warning */
2257 if (is_integer_btype(sbt)) {
2258 warning("assignment makes pointer from integer without a cast");
2259 goto type_ok;
2261 type1 = pointed_type(dt);
2262 /* a function is implicitely a function pointer */
2263 if (sbt == VT_FUNC) {
2264 if ((type1->t & VT_BTYPE) != VT_VOID &&
2265 !is_compatible_types(pointed_type(dt), st))
2266 warning("assignment from incompatible pointer type");
2267 goto type_ok;
2269 if (sbt != VT_PTR)
2270 goto error;
2271 type2 = pointed_type(st);
2272 if ((type1->t & VT_BTYPE) == VT_VOID ||
2273 (type2->t & VT_BTYPE) == VT_VOID) {
2274 /* void * can match anything */
2275 } else {
2276 /* exact type match, except for unsigned */
2277 tmp_type1 = *type1;
2278 tmp_type2 = *type2;
2279 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2280 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2281 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2282 warning("assignment from incompatible pointer type");
2284 /* check const and volatile */
2285 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2286 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2287 warning("assignment discards qualifiers from pointer target type");
2288 break;
2289 case VT_BYTE:
2290 case VT_SHORT:
2291 case VT_INT:
2292 case VT_LLONG:
2293 if (sbt == VT_PTR || sbt == VT_FUNC) {
2294 warning("assignment makes integer from pointer without a cast");
2296 /* XXX: more tests */
2297 break;
2298 case VT_STRUCT:
2299 tmp_type1 = *dt;
2300 tmp_type2 = *st;
2301 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2302 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2303 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2304 error:
2305 type_to_str(buf1, sizeof(buf1), st, NULL);
2306 type_to_str(buf2, sizeof(buf2), dt, NULL);
2307 error("cannot cast '%s' to '%s'", buf1, buf2);
2309 break;
2311 type_ok:
2312 gen_cast(dt);
2315 /* store vtop in lvalue pushed on stack */
2316 ST_FUNC void vstore(void)
2318 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2320 ft = vtop[-1].type.t;
2321 sbt = vtop->type.t & VT_BTYPE;
2322 dbt = ft & VT_BTYPE;
2323 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2324 (sbt == VT_INT && dbt == VT_SHORT)) {
2325 /* optimize char/short casts */
2326 delayed_cast = VT_MUSTCAST;
2327 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2328 /* XXX: factorize */
2329 if (ft & VT_CONSTANT)
2330 warning("assignment of read-only location");
2331 } else {
2332 delayed_cast = 0;
2333 if (!(ft & VT_BITFIELD))
2334 gen_assign_cast(&vtop[-1].type);
2337 if (sbt == VT_STRUCT) {
2338 /* if structure, only generate pointer */
2339 /* structure assignment : generate memcpy */
2340 /* XXX: optimize if small size */
2341 if (!nocode_wanted) {
2342 size = type_size(&vtop->type, &align);
2344 /* destination */
2345 vswap();
2346 vtop->type.t = VT_PTR;
2347 gaddrof();
2349 /* address of memcpy() */
2350 #ifdef TCC_ARM_EABI
2351 if(!(align & 7))
2352 vpush_global_sym(&func_old_type, TOK_memcpy8);
2353 else if(!(align & 3))
2354 vpush_global_sym(&func_old_type, TOK_memcpy4);
2355 else
2356 #endif
2357 vpush_global_sym(&func_old_type, TOK_memcpy);
2359 vswap();
2360 /* source */
2361 vpushv(vtop - 2);
2362 vtop->type.t = VT_PTR;
2363 gaddrof();
2364 /* type size */
2365 vpushi(size);
2366 gfunc_call(3);
2367 } else {
2368 vswap();
2369 vpop();
2371 /* leave source on stack */
2372 } else if (ft & VT_BITFIELD) {
2373 /* bitfield store handling */
2374 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2375 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2376 /* remove bit field info to avoid loops */
2377 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2379 /* duplicate source into other register */
2380 gv_dup();
2381 vswap();
2382 vrott(3);
2384 if((ft & VT_BTYPE) == VT_BOOL) {
2385 gen_cast(&vtop[-1].type);
2386 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2389 /* duplicate destination */
2390 vdup();
2391 vtop[-1] = vtop[-2];
2393 /* mask and shift source */
2394 if((ft & VT_BTYPE) != VT_BOOL) {
2395 if((ft & VT_BTYPE) == VT_LLONG) {
2396 vpushll((1ULL << bit_size) - 1ULL);
2397 } else {
2398 vpushi((1 << bit_size) - 1);
2400 gen_op('&');
2402 vpushi(bit_pos);
2403 gen_op(TOK_SHL);
2404 /* load destination, mask and or with source */
2405 vswap();
2406 if((ft & VT_BTYPE) == VT_LLONG) {
2407 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2408 } else {
2409 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2411 gen_op('&');
2412 gen_op('|');
2413 /* store result */
2414 vstore();
2416 /* pop off shifted source from "duplicate source..." above */
2417 vpop();
2419 } else {
2420 #ifdef CONFIG_TCC_BCHECK
2421 /* bound check case */
2422 if (vtop[-1].r & VT_MUSTBOUND) {
2423 vswap();
2424 gbound();
2425 vswap();
2427 #endif
2428 if (!nocode_wanted) {
2429 rc = RC_INT;
2430 if (is_float(ft)) {
2431 rc = RC_FLOAT;
2432 #ifdef TCC_TARGET_X86_64
2433 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2434 rc = RC_ST0;
2436 #endif
2438 r = gv(rc); /* generate value */
2439 /* if lvalue was saved on stack, must read it */
2440 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2441 SValue sv;
2442 t = get_reg(RC_INT);
2443 #ifdef TCC_TARGET_X86_64
2444 sv.type.t = VT_PTR;
2445 #else
2446 sv.type.t = VT_INT;
2447 #endif
2448 sv.r = VT_LOCAL | VT_LVAL;
2449 sv.c.ul = vtop[-1].c.ul;
2450 load(t, &sv);
2451 vtop[-1].r = t | VT_LVAL;
2453 store(r, vtop - 1);
2454 #ifndef TCC_TARGET_X86_64
2455 /* two word case handling : store second register at word + 4 */
2456 if ((ft & VT_BTYPE) == VT_LLONG) {
2457 vswap();
2458 /* convert to int to increment easily */
2459 vtop->type.t = VT_INT;
2460 gaddrof();
2461 vpushi(4);
2462 gen_op('+');
2463 vtop->r |= VT_LVAL;
2464 vswap();
2465 /* XXX: it works because r2 is spilled last ! */
2466 store(vtop->r2, vtop - 1);
2468 #endif
2470 vswap();
2471 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2472 vtop->r |= delayed_cast;
2476 /* post defines POST/PRE add. c is the token ++ or -- */
2477 ST_FUNC void inc(int post, int c)
2479 test_lvalue();
2480 vdup(); /* save lvalue */
2481 if (post) {
2482 gv_dup(); /* duplicate value */
2483 vrotb(3);
2484 vrotb(3);
2486 /* add constant */
2487 vpushi(c - TOK_MID);
2488 gen_op('+');
2489 vstore(); /* store value */
2490 if (post)
2491 vpop(); /* if post op, return saved value */
2494 /* Parse GNUC __attribute__ extension. Currently, the following
2495 extensions are recognized:
2496 - aligned(n) : set data/function alignment.
2497 - packed : force data alignment to 1
2498 - section(x) : generate data/code in this section.
2499 - unused : currently ignored, but may be used someday.
2500 - regparm(n) : pass function parameters in registers (i386 only)
2502 static void parse_attribute(AttributeDef *ad)
2504 int t, n;
2506 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2507 next();
2508 skip('(');
2509 skip('(');
2510 while (tok != ')') {
2511 if (tok < TOK_IDENT)
2512 expect("attribute name");
2513 t = tok;
2514 next();
2515 switch(t) {
2516 case TOK_SECTION1:
2517 case TOK_SECTION2:
2518 skip('(');
2519 if (tok != TOK_STR)
2520 expect("section name");
2521 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2522 next();
2523 skip(')');
2524 break;
2525 case TOK_ALIAS1:
2526 case TOK_ALIAS2:
2527 skip('(');
2528 if (tok != TOK_STR)
2529 expect("alias(\"target\")");
2530 ad->alias_target = /* save string as token, for later */
2531 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2532 next();
2533 skip(')');
2534 break;
2535 case TOK_ALIGNED1:
2536 case TOK_ALIGNED2:
2537 if (tok == '(') {
2538 next();
2539 n = expr_const();
2540 if (n <= 0 || (n & (n - 1)) != 0)
2541 error("alignment must be a positive power of two");
2542 skip(')');
2543 } else {
2544 n = MAX_ALIGN;
2546 ad->aligned = n;
2547 break;
2548 case TOK_PACKED1:
2549 case TOK_PACKED2:
2550 ad->packed = 1;
2551 break;
2552 case TOK_WEAK1:
2553 case TOK_WEAK2:
2554 ad->weak = 1;
2555 break;
2556 case TOK_UNUSED1:
2557 case TOK_UNUSED2:
2558 /* currently, no need to handle it because tcc does not
2559 track unused objects */
2560 break;
2561 case TOK_NORETURN1:
2562 case TOK_NORETURN2:
2563 /* currently, no need to handle it because tcc does not
2564 track unused objects */
2565 break;
2566 case TOK_CDECL1:
2567 case TOK_CDECL2:
2568 case TOK_CDECL3:
2569 ad->func_call = FUNC_CDECL;
2570 break;
2571 case TOK_STDCALL1:
2572 case TOK_STDCALL2:
2573 case TOK_STDCALL3:
2574 ad->func_call = FUNC_STDCALL;
2575 break;
2576 #ifdef TCC_TARGET_I386
2577 case TOK_REGPARM1:
2578 case TOK_REGPARM2:
2579 skip('(');
2580 n = expr_const();
2581 if (n > 3)
2582 n = 3;
2583 else if (n < 0)
2584 n = 0;
2585 if (n > 0)
2586 ad->func_call = FUNC_FASTCALL1 + n - 1;
2587 skip(')');
2588 break;
2589 case TOK_FASTCALL1:
2590 case TOK_FASTCALL2:
2591 case TOK_FASTCALL3:
2592 ad->func_call = FUNC_FASTCALLW;
2593 break;
2594 #endif
2595 case TOK_MODE:
2596 skip('(');
2597 switch(tok) {
2598 case TOK_MODE_DI:
2599 ad->mode = VT_LLONG + 1;
2600 break;
2601 case TOK_MODE_HI:
2602 ad->mode = VT_SHORT + 1;
2603 break;
2604 case TOK_MODE_SI:
2605 ad->mode = VT_INT + 1;
2606 break;
2607 default:
2608 warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2609 break;
2611 next();
2612 skip(')');
2613 break;
2614 case TOK_DLLEXPORT:
2615 ad->func_export = 1;
2616 break;
2617 case TOK_DLLIMPORT:
2618 ad->func_import = 1;
2619 break;
2620 default:
2621 if (tcc_state->warn_unsupported)
2622 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2623 /* skip parameters */
2624 if (tok == '(') {
2625 int parenthesis = 0;
2626 do {
2627 if (tok == '(')
2628 parenthesis++;
2629 else if (tok == ')')
2630 parenthesis--;
2631 next();
2632 } while (parenthesis && tok != -1);
2634 break;
2636 if (tok != ',')
2637 break;
2638 next();
2640 skip(')');
2641 skip(')');
2645 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2646 static void struct_decl(CType *type, int u)
2648 int a, v, size, align, maxalign, c, offset;
2649 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2650 Sym *s, *ss, *ass, **ps;
2651 AttributeDef ad;
2652 CType type1, btype;
2654 a = tok; /* save decl type */
2655 next();
2656 if (tok != '{') {
2657 v = tok;
2658 next();
2659 /* struct already defined ? return it */
2660 if (v < TOK_IDENT)
2661 expect("struct/union/enum name");
2662 s = struct_find(v);
2663 if (s) {
2664 if (s->type.t != a)
2665 error("invalid type");
2666 goto do_decl;
2668 } else {
2669 v = anon_sym++;
2671 type1.t = a;
2672 /* we put an undefined size for struct/union */
2673 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2674 s->r = 0; /* default alignment is zero as gcc */
2675 /* put struct/union/enum name in type */
2676 do_decl:
2677 type->t = u;
2678 type->ref = s;
2680 if (tok == '{') {
2681 next();
2682 if (s->c != -1)
2683 error("struct/union/enum already defined");
2684 /* cannot be empty */
2685 c = 0;
2686 /* non empty enums are not allowed */
2687 if (a == TOK_ENUM) {
2688 for(;;) {
2689 v = tok;
2690 if (v < TOK_UIDENT)
2691 expect("identifier");
2692 next();
2693 if (tok == '=') {
2694 next();
2695 c = expr_const();
2697 /* enum symbols have static storage */
2698 ss = sym_push(v, &int_type, VT_CONST, c);
2699 ss->type.t |= VT_STATIC;
2700 if (tok != ',')
2701 break;
2702 next();
2703 c++;
2704 /* NOTE: we accept a trailing comma */
2705 if (tok == '}')
2706 break;
2708 skip('}');
2709 } else {
2710 maxalign = 1;
2711 ps = &s->next;
2712 prevbt = VT_INT;
2713 bit_pos = 0;
2714 offset = 0;
2715 while (tok != '}') {
2716 parse_btype(&btype, &ad);
2717 while (1) {
2718 bit_size = -1;
2719 v = 0;
2720 type1 = btype;
2721 if (tok != ':') {
2722 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2723 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2724 expect("identifier");
2725 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2726 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2727 error("invalid type for '%s'",
2728 get_tok_str(v, NULL));
2730 if (tok == ':') {
2731 next();
2732 bit_size = expr_const();
2733 /* XXX: handle v = 0 case for messages */
2734 if (bit_size < 0)
2735 error("negative width in bit-field '%s'",
2736 get_tok_str(v, NULL));
2737 if (v && bit_size == 0)
2738 error("zero width for bit-field '%s'",
2739 get_tok_str(v, NULL));
2741 size = type_size(&type1, &align);
2742 if (ad.aligned) {
2743 if (align < ad.aligned)
2744 align = ad.aligned;
2745 } else if (ad.packed) {
2746 align = 1;
2747 } else if (*tcc_state->pack_stack_ptr) {
2748 if (align > *tcc_state->pack_stack_ptr)
2749 align = *tcc_state->pack_stack_ptr;
2751 lbit_pos = 0;
2752 if (bit_size >= 0) {
2753 bt = type1.t & VT_BTYPE;
2754 if (bt != VT_INT &&
2755 bt != VT_BYTE &&
2756 bt != VT_SHORT &&
2757 bt != VT_BOOL &&
2758 bt != VT_ENUM &&
2759 bt != VT_LLONG)
2760 error("bitfields must have scalar type");
2761 bsize = size * 8;
2762 if (bit_size > bsize) {
2763 error("width of '%s' exceeds its type",
2764 get_tok_str(v, NULL));
2765 } else if (bit_size == bsize) {
2766 /* no need for bit fields */
2767 bit_pos = 0;
2768 } else if (bit_size == 0) {
2769 /* XXX: what to do if only padding in a
2770 structure ? */
2771 /* zero size: means to pad */
2772 bit_pos = 0;
2773 } else {
2774 /* we do not have enough room ?
2775 did the type change?
2776 is it a union? */
2777 if ((bit_pos + bit_size) > bsize ||
2778 bt != prevbt || a == TOK_UNION)
2779 bit_pos = 0;
2780 lbit_pos = bit_pos;
2781 /* XXX: handle LSB first */
2782 type1.t |= VT_BITFIELD |
2783 (bit_pos << VT_STRUCT_SHIFT) |
2784 (bit_size << (VT_STRUCT_SHIFT + 6));
2785 bit_pos += bit_size;
2787 prevbt = bt;
2788 } else {
2789 bit_pos = 0;
2791 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2792 /* add new memory data only if starting
2793 bit field */
2794 if (lbit_pos == 0) {
2795 if (a == TOK_STRUCT) {
2796 c = (c + align - 1) & -align;
2797 offset = c;
2798 if (size > 0)
2799 c += size;
2800 } else {
2801 offset = 0;
2802 if (size > c)
2803 c = size;
2805 if (align > maxalign)
2806 maxalign = align;
2808 #if 0
2809 printf("add field %s offset=%d",
2810 get_tok_str(v, NULL), offset);
2811 if (type1.t & VT_BITFIELD) {
2812 printf(" pos=%d size=%d",
2813 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2814 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2816 printf("\n");
2817 #endif
2819 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2820 ass = type1.ref;
2821 while ((ass = ass->next) != NULL) {
2822 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2823 *ps = ss;
2824 ps = &ss->next;
2826 } else if (v) {
2827 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2828 *ps = ss;
2829 ps = &ss->next;
2831 if (tok == ';' || tok == TOK_EOF)
2832 break;
2833 skip(',');
2835 skip(';');
2837 skip('}');
2838 /* store size and alignment */
2839 s->c = (c + maxalign - 1) & -maxalign;
2840 s->r = maxalign;
2845 /* return 0 if no type declaration. otherwise, return the basic type
2846 and skip it.
2848 static int parse_btype(CType *type, AttributeDef *ad)
2850 int t, u, type_found, typespec_found, typedef_found;
2851 Sym *s;
2852 CType type1;
2854 memset(ad, 0, sizeof(AttributeDef));
2855 type_found = 0;
2856 typespec_found = 0;
2857 typedef_found = 0;
2858 t = 0;
2859 while(1) {
2860 switch(tok) {
2861 case TOK_EXTENSION:
2862 /* currently, we really ignore extension */
2863 next();
2864 continue;
2866 /* basic types */
2867 case TOK_CHAR:
2868 u = VT_BYTE;
2869 basic_type:
2870 next();
2871 basic_type1:
2872 if ((t & VT_BTYPE) != 0)
2873 error("too many basic types");
2874 t |= u;
2875 typespec_found = 1;
2876 break;
2877 case TOK_VOID:
2878 u = VT_VOID;
2879 goto basic_type;
2880 case TOK_SHORT:
2881 u = VT_SHORT;
2882 goto basic_type;
2883 case TOK_INT:
2884 next();
2885 typespec_found = 1;
2886 break;
2887 case TOK_LONG:
2888 next();
2889 if ((t & VT_BTYPE) == VT_DOUBLE) {
2890 #ifndef TCC_TARGET_PE
2891 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2892 #endif
2893 } else if ((t & VT_BTYPE) == VT_LONG) {
2894 t = (t & ~VT_BTYPE) | VT_LLONG;
2895 } else {
2896 u = VT_LONG;
2897 goto basic_type1;
2899 break;
2900 case TOK_BOOL:
2901 u = VT_BOOL;
2902 goto basic_type;
2903 case TOK_FLOAT:
2904 u = VT_FLOAT;
2905 goto basic_type;
2906 case TOK_DOUBLE:
2907 next();
2908 if ((t & VT_BTYPE) == VT_LONG) {
2909 #ifdef TCC_TARGET_PE
2910 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2911 #else
2912 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2913 #endif
2914 } else {
2915 u = VT_DOUBLE;
2916 goto basic_type1;
2918 break;
2919 case TOK_ENUM:
2920 struct_decl(&type1, VT_ENUM);
2921 basic_type2:
2922 u = type1.t;
2923 type->ref = type1.ref;
2924 goto basic_type1;
2925 case TOK_STRUCT:
2926 case TOK_UNION:
2927 struct_decl(&type1, VT_STRUCT);
2928 goto basic_type2;
2930 /* type modifiers */
2931 case TOK_CONST1:
2932 case TOK_CONST2:
2933 case TOK_CONST3:
2934 t |= VT_CONSTANT;
2935 next();
2936 break;
2937 case TOK_VOLATILE1:
2938 case TOK_VOLATILE2:
2939 case TOK_VOLATILE3:
2940 t |= VT_VOLATILE;
2941 next();
2942 break;
2943 case TOK_SIGNED1:
2944 case TOK_SIGNED2:
2945 case TOK_SIGNED3:
2946 typespec_found = 1;
2947 t |= VT_SIGNED;
2948 next();
2949 break;
2950 case TOK_REGISTER:
2951 case TOK_AUTO:
2952 case TOK_RESTRICT1:
2953 case TOK_RESTRICT2:
2954 case TOK_RESTRICT3:
2955 next();
2956 break;
2957 case TOK_UNSIGNED:
2958 t |= VT_UNSIGNED;
2959 next();
2960 typespec_found = 1;
2961 break;
2963 /* storage */
2964 case TOK_EXTERN:
2965 t |= VT_EXTERN;
2966 next();
2967 break;
2968 case TOK_STATIC:
2969 t |= VT_STATIC;
2970 next();
2971 break;
2972 case TOK_TYPEDEF:
2973 t |= VT_TYPEDEF;
2974 next();
2975 break;
2976 case TOK_INLINE1:
2977 case TOK_INLINE2:
2978 case TOK_INLINE3:
2979 t |= VT_INLINE;
2980 next();
2981 break;
2983 /* GNUC attribute */
2984 case TOK_ATTRIBUTE1:
2985 case TOK_ATTRIBUTE2:
2986 parse_attribute(ad);
2987 if (ad->mode) {
2988 u = ad->mode -1;
2989 t = (t & ~VT_BTYPE) | u;
2991 break;
2992 /* GNUC typeof */
2993 case TOK_TYPEOF1:
2994 case TOK_TYPEOF2:
2995 case TOK_TYPEOF3:
2996 next();
2997 parse_expr_type(&type1);
2998 /* remove all storage modifiers except typedef */
2999 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3000 goto basic_type2;
3001 default:
3002 if (typespec_found || typedef_found)
3003 goto the_end;
3004 s = sym_find(tok);
3005 if (!s || !(s->type.t & VT_TYPEDEF))
3006 goto the_end;
3007 typedef_found = 1;
3008 t |= (s->type.t & ~VT_TYPEDEF);
3009 type->ref = s->type.ref;
3010 if (s->r) {
3011 /* get attributes from typedef */
3012 if (0 == ad->aligned)
3013 ad->aligned = FUNC_ALIGN(s->r);
3014 if (0 == ad->func_call)
3015 ad->func_call = FUNC_CALL(s->r);
3016 ad->packed |= FUNC_PACKED(s->r);
3018 next();
3019 typespec_found = 1;
3020 break;
3022 type_found = 1;
3024 the_end:
3025 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3026 error("signed and unsigned modifier");
3027 if (tcc_state->char_is_unsigned) {
3028 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3029 t |= VT_UNSIGNED;
3031 t &= ~VT_SIGNED;
3033 /* long is never used as type */
3034 if ((t & VT_BTYPE) == VT_LONG)
3035 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3036 t = (t & ~VT_BTYPE) | VT_INT;
3037 #else
3038 t = (t & ~VT_BTYPE) | VT_LLONG;
3039 #endif
3040 type->t = t;
3041 return type_found;
3044 /* convert a function parameter type (array to pointer and function to
3045 function pointer) */
3046 static inline void convert_parameter_type(CType *pt)
3048 /* remove const and volatile qualifiers (XXX: const could be used
3049 to indicate a const function parameter */
3050 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3051 /* array must be transformed to pointer according to ANSI C */
3052 pt->t &= ~VT_ARRAY;
3053 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3054 mk_pointer(pt);
3058 ST_FUNC void parse_asm_str(CString *astr)
3060 skip('(');
3061 /* read the string */
3062 if (tok != TOK_STR)
3063 expect("string constant");
3064 cstr_new(astr);
3065 while (tok == TOK_STR) {
3066 /* XXX: add \0 handling too ? */
3067 cstr_cat(astr, tokc.cstr->data);
3068 next();
3070 cstr_ccat(astr, '\0');
3073 /* Parse an asm label and return the label
3074 * Don't forget to free the CString in the caller! */
3075 static void asm_label_instr(CString *astr)
3077 next();
3078 parse_asm_str(astr);
3079 skip(')');
3080 #ifdef ASM_DEBUG
3081 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3082 #endif
3085 static void post_type(CType *type, AttributeDef *ad)
3087 int n, l, t1, arg_size, align;
3088 Sym **plast, *s, *first;
3089 AttributeDef ad1;
3090 CType pt;
3092 if (tok == '(') {
3093 /* function declaration */
3094 next();
3095 l = 0;
3096 first = NULL;
3097 plast = &first;
3098 arg_size = 0;
3099 if (tok != ')') {
3100 for(;;) {
3101 /* read param name and compute offset */
3102 if (l != FUNC_OLD) {
3103 if (!parse_btype(&pt, &ad1)) {
3104 if (l) {
3105 error("invalid type");
3106 } else {
3107 l = FUNC_OLD;
3108 goto old_proto;
3111 l = FUNC_NEW;
3112 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3113 break;
3114 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3115 if ((pt.t & VT_BTYPE) == VT_VOID)
3116 error("parameter declared as void");
3117 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3118 } else {
3119 old_proto:
3120 n = tok;
3121 if (n < TOK_UIDENT)
3122 expect("identifier");
3123 pt.t = VT_INT;
3124 next();
3126 convert_parameter_type(&pt);
3127 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3128 *plast = s;
3129 plast = &s->next;
3130 if (tok == ')')
3131 break;
3132 skip(',');
3133 if (l == FUNC_NEW && tok == TOK_DOTS) {
3134 l = FUNC_ELLIPSIS;
3135 next();
3136 break;
3140 /* if no parameters, then old type prototype */
3141 if (l == 0)
3142 l = FUNC_OLD;
3143 skip(')');
3144 /* NOTE: const is ignored in returned type as it has a special
3145 meaning in gcc / C++ */
3146 type->t &= ~VT_CONSTANT;
3147 /* some ancient pre-K&R C allows a function to return an array
3148 and the array brackets to be put after the arguments, such
3149 that "int c()[]" means something like "int[] c()" */
3150 if (tok == '[') {
3151 next();
3152 skip(']'); /* only handle simple "[]" */
3153 type->t |= VT_PTR;
3155 /* we push a anonymous symbol which will contain the function prototype */
3156 ad->func_args = arg_size;
3157 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3158 s->next = first;
3159 type->t = VT_FUNC;
3160 type->ref = s;
3161 } else if (tok == '[') {
3162 /* array definition */
3163 next();
3164 if (tok == TOK_RESTRICT1)
3165 next();
3166 n = -1;
3167 t1 = 0;
3168 if (tok != ']') {
3169 gexpr();
3170 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3171 n = vtop->c.i;
3172 if (n < 0)
3173 error("invalid array size");
3174 } else if (!local_stack) {
3175 error("expected constant expression (variably modified array at file scope)");
3176 } else {
3177 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3178 error("size of variable length array should be an integer");
3179 t1 = VT_VLA;
3182 skip(']');
3183 /* parse next post type */
3184 post_type(type, ad);
3185 t1 |= type->t & VT_VLA;
3187 if (t1 & VT_VLA) {
3188 loc -= type_size(&int_type, &align);
3189 loc &= -align;
3190 n = loc;
3192 vla_runtime_type_size(type, &align);
3193 gen_op('*');
3194 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3195 vswap();
3196 vstore();
3198 if (n != -1)
3199 vpop();
3201 /* we push an anonymous symbol which will contain the array
3202 element type */
3203 s = sym_push(SYM_FIELD, type, 0, n);
3204 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3205 type->ref = s;
3209 /* Parse a type declaration (except basic type), and return the type
3210 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3211 expected. 'type' should contain the basic type. 'ad' is the
3212 attribute definition of the basic type. It can be modified by
3213 type_decl().
3215 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3217 Sym *s;
3218 CType type1, *type2;
3219 int qualifiers, storage;
3221 while (tok == '*') {
3222 qualifiers = 0;
3223 redo:
3224 next();
3225 switch(tok) {
3226 case TOK_CONST1:
3227 case TOK_CONST2:
3228 case TOK_CONST3:
3229 qualifiers |= VT_CONSTANT;
3230 goto redo;
3231 case TOK_VOLATILE1:
3232 case TOK_VOLATILE2:
3233 case TOK_VOLATILE3:
3234 qualifiers |= VT_VOLATILE;
3235 goto redo;
3236 case TOK_RESTRICT1:
3237 case TOK_RESTRICT2:
3238 case TOK_RESTRICT3:
3239 goto redo;
3241 mk_pointer(type);
3242 type->t |= qualifiers;
3245 /* XXX: clarify attribute handling */
3246 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3247 parse_attribute(ad);
3249 /* recursive type */
3250 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3251 type1.t = 0; /* XXX: same as int */
3252 if (tok == '(') {
3253 next();
3254 /* XXX: this is not correct to modify 'ad' at this point, but
3255 the syntax is not clear */
3256 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3257 parse_attribute(ad);
3258 type_decl(&type1, ad, v, td);
3259 skip(')');
3260 } else {
3261 /* type identifier */
3262 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3263 *v = tok;
3264 next();
3265 } else {
3266 if (!(td & TYPE_ABSTRACT))
3267 expect("identifier");
3268 *v = 0;
3271 storage = type->t & VT_STORAGE;
3272 type->t &= ~VT_STORAGE;
3273 post_type(type, ad);
3274 type->t |= storage;
3275 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3276 parse_attribute(ad);
3278 if (!type1.t)
3279 return;
3280 /* append type at the end of type1 */
3281 type2 = &type1;
3282 for(;;) {
3283 s = type2->ref;
3284 type2 = &s->type;
3285 if (!type2->t) {
3286 *type2 = *type;
3287 break;
3290 *type = type1;
3293 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3294 ST_FUNC int lvalue_type(int t)
3296 int bt, r;
3297 r = VT_LVAL;
3298 bt = t & VT_BTYPE;
3299 if (bt == VT_BYTE || bt == VT_BOOL)
3300 r |= VT_LVAL_BYTE;
3301 else if (bt == VT_SHORT)
3302 r |= VT_LVAL_SHORT;
3303 else
3304 return r;
3305 if (t & VT_UNSIGNED)
3306 r |= VT_LVAL_UNSIGNED;
3307 return r;
3310 /* indirection with full error checking and bound check */
3311 ST_FUNC void indir(void)
3313 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3314 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3315 return;
3316 expect("pointer");
3318 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3319 gv(RC_INT);
3320 vtop->type = *pointed_type(&vtop->type);
3321 /* Arrays and functions are never lvalues */
3322 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3323 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3324 vtop->r |= lvalue_type(vtop->type.t);
3325 /* if bound checking, the referenced pointer must be checked */
3326 #ifdef CONFIG_TCC_BCHECK
3327 if (tcc_state->do_bounds_check)
3328 vtop->r |= VT_MUSTBOUND;
3329 #endif
3333 /* pass a parameter to a function and do type checking and casting */
3334 static void gfunc_param_typed(Sym *func, Sym *arg)
3336 int func_type;
3337 CType type;
3339 func_type = func->c;
3340 if (func_type == FUNC_OLD ||
3341 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3342 /* default casting : only need to convert float to double */
3343 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3344 type.t = VT_DOUBLE;
3345 gen_cast(&type);
3347 } else if (arg == NULL) {
3348 error("too many arguments to function");
3349 } else {
3350 type = arg->type;
3351 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3352 gen_assign_cast(&type);
3356 /* parse an expression of the form '(type)' or '(expr)' and return its
3357 type */
3358 static void parse_expr_type(CType *type)
3360 int n;
3361 AttributeDef ad;
3363 skip('(');
3364 if (parse_btype(type, &ad)) {
3365 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3366 } else {
3367 expr_type(type);
3369 skip(')');
3372 static void parse_type(CType *type)
3374 AttributeDef ad;
3375 int n;
3377 if (!parse_btype(type, &ad)) {
3378 expect("type");
3380 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3383 static void vpush_tokc(int t)
3385 CType type;
3386 type.t = t;
3387 type.ref = 0;
3388 vsetc(&type, VT_CONST, &tokc);
3391 ST_FUNC void unary(void)
3393 int n, t, align, size, r, sizeof_caller;
3394 CType type;
3395 Sym *s;
3396 AttributeDef ad;
3397 static int in_sizeof = 0;
3399 sizeof_caller = in_sizeof;
3400 in_sizeof = 0;
3401 /* XXX: GCC 2.95.3 does not generate a table although it should be
3402 better here */
3403 tok_next:
3404 switch(tok) {
3405 case TOK_EXTENSION:
3406 next();
3407 goto tok_next;
3408 case TOK_CINT:
3409 case TOK_CCHAR:
3410 case TOK_LCHAR:
3411 vpushi(tokc.i);
3412 next();
3413 break;
3414 case TOK_CUINT:
3415 vpush_tokc(VT_INT | VT_UNSIGNED);
3416 next();
3417 break;
3418 case TOK_CLLONG:
3419 vpush_tokc(VT_LLONG);
3420 next();
3421 break;
3422 case TOK_CULLONG:
3423 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3424 next();
3425 break;
3426 case TOK_CFLOAT:
3427 vpush_tokc(VT_FLOAT);
3428 next();
3429 break;
3430 case TOK_CDOUBLE:
3431 vpush_tokc(VT_DOUBLE);
3432 next();
3433 break;
3434 case TOK_CLDOUBLE:
3435 vpush_tokc(VT_LDOUBLE);
3436 next();
3437 break;
3438 case TOK___FUNCTION__:
3439 if (!gnu_ext)
3440 goto tok_identifier;
3441 /* fall thru */
3442 case TOK___FUNC__:
3444 void *ptr;
3445 int len;
3446 /* special function name identifier */
3447 len = strlen(funcname) + 1;
3448 /* generate char[len] type */
3449 type.t = VT_BYTE;
3450 mk_pointer(&type);
3451 type.t |= VT_ARRAY;
3452 type.ref->c = len;
3453 vpush_ref(&type, data_section, data_section->data_offset, len);
3454 ptr = section_ptr_add(data_section, len);
3455 memcpy(ptr, funcname, len);
3456 next();
3458 break;
3459 case TOK_LSTR:
3460 #ifdef TCC_TARGET_PE
3461 t = VT_SHORT | VT_UNSIGNED;
3462 #else
3463 t = VT_INT;
3464 #endif
3465 goto str_init;
3466 case TOK_STR:
3467 /* string parsing */
3468 t = VT_BYTE;
3469 str_init:
3470 if (tcc_state->warn_write_strings)
3471 t |= VT_CONSTANT;
3472 type.t = t;
3473 mk_pointer(&type);
3474 type.t |= VT_ARRAY;
3475 memset(&ad, 0, sizeof(AttributeDef));
3476 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3477 break;
3478 case '(':
3479 next();
3480 /* cast ? */
3481 if (parse_btype(&type, &ad)) {
3482 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3483 skip(')');
3484 /* check ISOC99 compound literal */
3485 if (tok == '{') {
3486 /* data is allocated locally by default */
3487 if (global_expr)
3488 r = VT_CONST;
3489 else
3490 r = VT_LOCAL;
3491 /* all except arrays are lvalues */
3492 if (!(type.t & VT_ARRAY))
3493 r |= lvalue_type(type.t);
3494 memset(&ad, 0, sizeof(AttributeDef));
3495 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3496 } else {
3497 if (sizeof_caller) {
3498 vpush(&type);
3499 return;
3501 unary();
3502 gen_cast(&type);
3504 } else if (tok == '{') {
3505 /* save all registers */
3506 save_regs(0);
3507 /* statement expression : we do not accept break/continue
3508 inside as GCC does */
3509 block(NULL, NULL, NULL, NULL, 0, 1);
3510 skip(')');
3511 } else {
3512 gexpr();
3513 skip(')');
3515 break;
3516 case '*':
3517 next();
3518 unary();
3519 indir();
3520 break;
3521 case '&':
3522 next();
3523 unary();
3524 /* functions names must be treated as function pointers,
3525 except for unary '&' and sizeof. Since we consider that
3526 functions are not lvalues, we only have to handle it
3527 there and in function calls. */
3528 /* arrays can also be used although they are not lvalues */
3529 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3530 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3531 test_lvalue();
3532 mk_pointer(&vtop->type);
3533 gaddrof();
3534 break;
3535 case '!':
3536 next();
3537 unary();
3538 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3539 CType boolean;
3540 boolean.t = VT_BOOL;
3541 gen_cast(&boolean);
3542 vtop->c.i = !vtop->c.i;
3543 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3544 vtop->c.i = vtop->c.i ^ 1;
3545 else {
3546 save_regs(1);
3547 vseti(VT_JMP, gtst(1, 0));
3549 break;
3550 case '~':
3551 next();
3552 unary();
3553 vpushi(-1);
3554 gen_op('^');
3555 break;
3556 case '+':
3557 next();
3558 /* in order to force cast, we add zero */
3559 unary();
3560 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3561 error("pointer not accepted for unary plus");
3562 vpushi(0);
3563 gen_op('+');
3564 break;
3565 case TOK_SIZEOF:
3566 case TOK_ALIGNOF1:
3567 case TOK_ALIGNOF2:
3568 t = tok;
3569 next();
3570 in_sizeof++;
3571 unary_type(&type); // Perform a in_sizeof = 0;
3572 size = type_size(&type, &align);
3573 if (t == TOK_SIZEOF) {
3574 if (!(type.t & VT_VLA)) {
3575 if (size < 0)
3576 error("sizeof applied to an incomplete type");
3577 vpushi(size);
3578 } else {
3579 vla_runtime_type_size(&type, &align);
3581 } else {
3582 vpushi(align);
3584 vtop->type.t |= VT_UNSIGNED;
3585 break;
3587 case TOK_builtin_types_compatible_p:
3589 CType type1, type2;
3590 next();
3591 skip('(');
3592 parse_type(&type1);
3593 skip(',');
3594 parse_type(&type2);
3595 skip(')');
3596 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3597 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3598 vpushi(is_compatible_types(&type1, &type2));
3600 break;
3601 case TOK_builtin_constant_p:
3603 int saved_nocode_wanted, res;
3604 next();
3605 skip('(');
3606 saved_nocode_wanted = nocode_wanted;
3607 nocode_wanted = 1;
3608 gexpr();
3609 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3610 vpop();
3611 nocode_wanted = saved_nocode_wanted;
3612 skip(')');
3613 vpushi(res);
3615 break;
3616 case TOK_builtin_frame_address:
3618 CType type;
3619 next();
3620 skip('(');
3621 if (tok != TOK_CINT) {
3622 error("__builtin_frame_address only takes integers");
3624 if (tokc.i != 0) {
3625 error("TCC only supports __builtin_frame_address(0)");
3627 next();
3628 skip(')');
3629 type.t = VT_VOID;
3630 mk_pointer(&type);
3631 vset(&type, VT_LOCAL, 0);
3633 break;
3634 #ifdef TCC_TARGET_X86_64
3635 case TOK_builtin_va_arg_types:
3637 /* This definition must be synced with stdarg.h */
3638 enum __va_arg_type {
3639 __va_gen_reg, __va_float_reg, __va_stack
3641 CType type;
3642 int bt;
3643 next();
3644 skip('(');
3645 parse_type(&type);
3646 skip(')');
3647 bt = type.t & VT_BTYPE;
3648 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
3649 vpushi(__va_stack);
3650 } else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
3651 vpushi(__va_float_reg);
3652 } else {
3653 vpushi(__va_gen_reg);
3656 break;
3657 #endif
3658 case TOK_INC:
3659 case TOK_DEC:
3660 t = tok;
3661 next();
3662 unary();
3663 inc(0, t);
3664 break;
3665 case '-':
3666 next();
3667 vpushi(0);
3668 unary();
3669 gen_op('-');
3670 break;
3671 case TOK_LAND:
3672 if (!gnu_ext)
3673 goto tok_identifier;
3674 next();
3675 /* allow to take the address of a label */
3676 if (tok < TOK_UIDENT)
3677 expect("label identifier");
3678 s = label_find(tok);
3679 if (!s) {
3680 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3681 } else {
3682 if (s->r == LABEL_DECLARED)
3683 s->r = LABEL_FORWARD;
3685 if (!s->type.t) {
3686 s->type.t = VT_VOID;
3687 mk_pointer(&s->type);
3688 s->type.t |= VT_STATIC;
3690 vset(&s->type, VT_CONST | VT_SYM, 0);
3691 vtop->sym = s;
3692 next();
3693 break;
3695 // special qnan , snan and infinity values
3696 case TOK___NAN__:
3697 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3698 next();
3699 break;
3700 case TOK___SNAN__:
3701 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3702 next();
3703 break;
3704 case TOK___INF__:
3705 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3706 next();
3707 break;
3709 default:
3710 tok_identifier:
3711 t = tok;
3712 next();
3713 if (t < TOK_UIDENT)
3714 expect("identifier");
3715 s = sym_find(t);
3716 if (!s) {
3717 if (tok != '(')
3718 error("'%s' undeclared", get_tok_str(t, NULL));
3719 /* for simple function calls, we tolerate undeclared
3720 external reference to int() function */
3721 if (tcc_state->warn_implicit_function_declaration)
3722 warning("implicit declaration of function '%s'",
3723 get_tok_str(t, NULL));
3724 s = external_global_sym(t, &func_old_type, 0);
3726 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3727 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3728 /* if referencing an inline function, then we generate a
3729 symbol to it if not already done. It will have the
3730 effect to generate code for it at the end of the
3731 compilation unit. Inline function as always
3732 generated in the text section. */
3733 if (!s->c)
3734 put_extern_sym(s, text_section, 0, 0);
3735 r = VT_SYM | VT_CONST;
3736 } else {
3737 r = s->r;
3739 vset(&s->type, r, s->c);
3740 /* if forward reference, we must point to s */
3741 if (vtop->r & VT_SYM) {
3742 vtop->sym = s;
3743 vtop->c.ul = 0;
3745 break;
3748 /* post operations */
3749 while (1) {
3750 if (tok == TOK_INC || tok == TOK_DEC) {
3751 inc(1, tok);
3752 next();
3753 } else if (tok == '.' || tok == TOK_ARROW) {
3754 int qualifiers;
3755 /* field */
3756 if (tok == TOK_ARROW)
3757 indir();
3758 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3759 test_lvalue();
3760 gaddrof();
3761 next();
3762 /* expect pointer on structure */
3763 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3764 expect("struct or union");
3765 s = vtop->type.ref;
3766 /* find field */
3767 tok |= SYM_FIELD;
3768 while ((s = s->next) != NULL) {
3769 if (s->v == tok)
3770 break;
3772 if (!s)
3773 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3774 /* add field offset to pointer */
3775 vtop->type = char_pointer_type; /* change type to 'char *' */
3776 vpushi(s->c);
3777 gen_op('+');
3778 /* change type to field type, and set to lvalue */
3779 vtop->type = s->type;
3780 vtop->type.t |= qualifiers;
3781 /* an array is never an lvalue */
3782 if (!(vtop->type.t & VT_ARRAY)) {
3783 vtop->r |= lvalue_type(vtop->type.t);
3784 #ifdef CONFIG_TCC_BCHECK
3785 /* if bound checking, the referenced pointer must be checked */
3786 if (tcc_state->do_bounds_check)
3787 vtop->r |= VT_MUSTBOUND;
3788 #endif
3790 next();
3791 } else if (tok == '[') {
3792 next();
3793 gexpr();
3794 gen_op('+');
3795 indir();
3796 skip(']');
3797 } else if (tok == '(') {
3798 SValue ret;
3799 Sym *sa;
3800 int nb_args;
3802 /* function call */
3803 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3804 /* pointer test (no array accepted) */
3805 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3806 vtop->type = *pointed_type(&vtop->type);
3807 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3808 goto error_func;
3809 } else {
3810 error_func:
3811 expect("function pointer");
3813 } else {
3814 vtop->r &= ~VT_LVAL; /* no lvalue */
3816 /* get return type */
3817 s = vtop->type.ref;
3818 next();
3819 sa = s->next; /* first parameter */
3820 nb_args = 0;
3821 ret.r2 = VT_CONST;
3822 /* compute first implicit argument if a structure is returned */
3823 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3824 /* get some space for the returned structure */
3825 size = type_size(&s->type, &align);
3826 loc = (loc - size) & -align;
3827 ret.type = s->type;
3828 ret.r = VT_LOCAL | VT_LVAL;
3829 /* pass it as 'int' to avoid structure arg passing
3830 problems */
3831 vseti(VT_LOCAL, loc);
3832 ret.c = vtop->c;
3833 nb_args++;
3834 } else {
3835 ret.type = s->type;
3836 /* return in register */
3837 if (is_float(ret.type.t)) {
3838 ret.r = reg_fret(ret.type.t);
3839 } else {
3840 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3841 ret.r2 = REG_LRET;
3842 ret.r = REG_IRET;
3844 ret.c.i = 0;
3846 if (tok != ')') {
3847 for(;;) {
3848 expr_eq();
3849 gfunc_param_typed(s, sa);
3850 nb_args++;
3851 if (sa)
3852 sa = sa->next;
3853 if (tok == ')')
3854 break;
3855 skip(',');
3858 if (sa)
3859 error("too few arguments to function");
3860 skip(')');
3861 if (!nocode_wanted) {
3862 gfunc_call(nb_args);
3863 } else {
3864 vtop -= (nb_args + 1);
3866 /* return value */
3867 vsetc(&ret.type, ret.r, &ret.c);
3868 vtop->r2 = ret.r2;
3869 } else {
3870 break;
3875 ST_FUNC void expr_prod(void)
3877 int t;
3879 unary();
3880 while (tok == '*' || tok == '/' || tok == '%') {
3881 t = tok;
3882 next();
3883 unary();
3884 gen_op(t);
3888 ST_FUNC void expr_sum(void)
3890 int t;
3892 expr_prod();
3893 while (tok == '+' || tok == '-') {
3894 t = tok;
3895 next();
3896 expr_prod();
3897 gen_op(t);
3901 static void expr_shift(void)
3903 int t;
3905 expr_sum();
3906 while (tok == TOK_SHL || tok == TOK_SAR) {
3907 t = tok;
3908 next();
3909 expr_sum();
3910 gen_op(t);
3914 static void expr_cmp(void)
3916 int t;
3918 expr_shift();
3919 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3920 tok == TOK_ULT || tok == TOK_UGE) {
3921 t = tok;
3922 next();
3923 expr_shift();
3924 gen_op(t);
3928 static void expr_cmpeq(void)
3930 int t;
3932 expr_cmp();
3933 while (tok == TOK_EQ || tok == TOK_NE) {
3934 t = tok;
3935 next();
3936 expr_cmp();
3937 gen_op(t);
3941 static void expr_and(void)
3943 expr_cmpeq();
3944 while (tok == '&') {
3945 next();
3946 expr_cmpeq();
3947 gen_op('&');
3951 static void expr_xor(void)
3953 expr_and();
3954 while (tok == '^') {
3955 next();
3956 expr_and();
3957 gen_op('^');
3961 static void expr_or(void)
3963 expr_xor();
3964 while (tok == '|') {
3965 next();
3966 expr_xor();
3967 gen_op('|');
3971 /* XXX: fix this mess */
3972 static void expr_land_const(void)
3974 expr_or();
3975 while (tok == TOK_LAND) {
3976 next();
3977 expr_or();
3978 gen_op(TOK_LAND);
3982 /* XXX: fix this mess */
3983 static void expr_lor_const(void)
3985 expr_land_const();
3986 while (tok == TOK_LOR) {
3987 next();
3988 expr_land_const();
3989 gen_op(TOK_LOR);
3993 /* only used if non constant */
3994 static void expr_land(void)
3996 int t;
3998 expr_or();
3999 if (tok == TOK_LAND) {
4000 t = 0;
4001 save_regs(1);
4002 for(;;) {
4003 t = gtst(1, t);
4004 if (tok != TOK_LAND) {
4005 vseti(VT_JMPI, t);
4006 break;
4008 next();
4009 expr_or();
4014 static void expr_lor(void)
4016 int t;
4018 expr_land();
4019 if (tok == TOK_LOR) {
4020 t = 0;
4021 save_regs(1);
4022 for(;;) {
4023 t = gtst(0, t);
4024 if (tok != TOK_LOR) {
4025 vseti(VT_JMP, t);
4026 break;
4028 next();
4029 expr_land();
4034 /* XXX: better constant handling */
4035 static void expr_cond(void)
4037 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4038 SValue sv;
4039 CType type, type1, type2;
4041 if (const_wanted) {
4042 expr_lor_const();
4043 } else {
4044 expr_lor();
4046 if (const_wanted || ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)) {
4047 if (tok == '?') {
4048 CType boolean;
4049 int c;
4050 boolean.t = VT_BOOL;
4051 vdup();
4052 gen_cast(&boolean);
4053 c = vtop->c.i;
4054 vpop();
4055 next();
4056 if (tok != ':' || !gnu_ext) {
4057 vpop();
4058 gexpr();
4060 if (!c)
4061 vpop();
4062 skip(':');
4063 expr_cond();
4064 if (c)
4065 vpop();
4067 } else {
4068 if (tok == '?') {
4069 next();
4070 if (vtop != vstack) {
4071 /* needed to avoid having different registers saved in
4072 each branch */
4073 if (is_float(vtop->type.t)) {
4074 rc = RC_FLOAT;
4075 #ifdef TCC_TARGET_X86_64
4076 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4077 rc = RC_ST0;
4079 #endif
4081 else
4082 rc = RC_INT;
4083 gv(rc);
4084 save_regs(1);
4086 if (tok == ':' && gnu_ext) {
4087 gv_dup();
4088 tt = gtst(1, 0);
4089 } else {
4090 tt = gtst(1, 0);
4091 gexpr();
4093 type1 = vtop->type;
4094 sv = *vtop; /* save value to handle it later */
4095 vtop--; /* no vpop so that FP stack is not flushed */
4096 skip(':');
4097 u = gjmp(0);
4098 gsym(tt);
4099 expr_cond();
4100 type2 = vtop->type;
4102 t1 = type1.t;
4103 bt1 = t1 & VT_BTYPE;
4104 t2 = type2.t;
4105 bt2 = t2 & VT_BTYPE;
4106 /* cast operands to correct type according to ISOC rules */
4107 if (is_float(bt1) || is_float(bt2)) {
4108 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4109 type.t = VT_LDOUBLE;
4110 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4111 type.t = VT_DOUBLE;
4112 } else {
4113 type.t = VT_FLOAT;
4115 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4116 /* cast to biggest op */
4117 type.t = VT_LLONG;
4118 /* convert to unsigned if it does not fit in a long long */
4119 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4120 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4121 type.t |= VT_UNSIGNED;
4122 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4123 /* XXX: test pointer compatibility */
4124 type = type1;
4125 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4126 /* XXX: test function pointer compatibility */
4127 type = type1;
4128 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4129 /* XXX: test structure compatibility */
4130 type = type1;
4131 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4132 /* NOTE: as an extension, we accept void on only one side */
4133 type.t = VT_VOID;
4134 } else {
4135 /* integer operations */
4136 type.t = VT_INT;
4137 /* convert to unsigned if it does not fit in an integer */
4138 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4139 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4140 type.t |= VT_UNSIGNED;
4143 /* now we convert second operand */
4144 gen_cast(&type);
4145 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4146 gaddrof();
4147 rc = RC_INT;
4148 if (is_float(type.t)) {
4149 rc = RC_FLOAT;
4150 #ifdef TCC_TARGET_X86_64
4151 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4152 rc = RC_ST0;
4154 #endif
4155 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4156 /* for long longs, we use fixed registers to avoid having
4157 to handle a complicated move */
4158 rc = RC_IRET;
4161 r2 = gv(rc);
4162 /* this is horrible, but we must also convert first
4163 operand */
4164 tt = gjmp(0);
4165 gsym(u);
4166 /* put again first value and cast it */
4167 *vtop = sv;
4168 gen_cast(&type);
4169 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4170 gaddrof();
4171 r1 = gv(rc);
4172 move_reg(r2, r1);
4173 vtop->r = r2;
4174 gsym(tt);
4179 static void expr_eq(void)
4181 int t;
4183 expr_cond();
4184 if (tok == '=' ||
4185 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4186 tok == TOK_A_XOR || tok == TOK_A_OR ||
4187 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4188 test_lvalue();
4189 t = tok;
4190 next();
4191 if (t == '=') {
4192 expr_eq();
4193 } else {
4194 vdup();
4195 expr_eq();
4196 gen_op(t & 0x7f);
4198 vstore();
4202 ST_FUNC void gexpr(void)
4204 while (1) {
4205 expr_eq();
4206 if (tok != ',')
4207 break;
4208 vpop();
4209 next();
4213 /* parse an expression and return its type without any side effect. */
4214 static void expr_type(CType *type)
4216 int saved_nocode_wanted;
4218 saved_nocode_wanted = nocode_wanted;
4219 nocode_wanted = 1;
4220 gexpr();
4221 *type = vtop->type;
4222 vpop();
4223 nocode_wanted = saved_nocode_wanted;
4226 /* parse a unary expression and return its type without any side
4227 effect. */
4228 static void unary_type(CType *type)
4230 int a;
4232 a = nocode_wanted;
4233 nocode_wanted = 1;
4234 unary();
4235 *type = vtop->type;
4236 vpop();
4237 nocode_wanted = a;
4240 /* parse a constant expression and return value in vtop. */
4241 static void expr_const1(void)
4243 int a;
4244 a = const_wanted;
4245 const_wanted = 1;
4246 expr_cond();
4247 const_wanted = a;
4250 /* parse an integer constant and return its value. */
4251 ST_FUNC int expr_const(void)
4253 int c;
4254 expr_const1();
4255 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4256 expect("constant expression");
4257 c = vtop->c.i;
4258 vpop();
4259 return c;
4262 /* return the label token if current token is a label, otherwise
4263 return zero */
4264 static int is_label(void)
4266 int last_tok;
4268 /* fast test first */
4269 if (tok < TOK_UIDENT)
4270 return 0;
4271 /* no need to save tokc because tok is an identifier */
4272 last_tok = tok;
4273 next();
4274 if (tok == ':') {
4275 next();
4276 return last_tok;
4277 } else {
4278 unget_tok(last_tok);
4279 return 0;
4283 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4284 int case_reg, int is_expr)
4286 int a, b, c, d;
4287 Sym *s;
4289 /* generate line number info */
4290 if (tcc_state->do_debug &&
4291 (last_line_num != file->line_num || last_ind != ind)) {
4292 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4293 last_ind = ind;
4294 last_line_num = file->line_num;
4297 if (is_expr) {
4298 /* default return value is (void) */
4299 vpushi(0);
4300 vtop->type.t = VT_VOID;
4303 if (tok == TOK_IF) {
4304 /* if test */
4305 next();
4306 skip('(');
4307 gexpr();
4308 skip(')');
4309 a = gtst(1, 0);
4310 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4311 c = tok;
4312 if (c == TOK_ELSE) {
4313 next();
4314 d = gjmp(0);
4315 gsym(a);
4316 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4317 gsym(d); /* patch else jmp */
4318 } else
4319 gsym(a);
4320 } else if (tok == TOK_WHILE) {
4321 next();
4322 d = ind;
4323 skip('(');
4324 gexpr();
4325 skip(')');
4326 a = gtst(1, 0);
4327 b = 0;
4328 block(&a, &b, case_sym, def_sym, case_reg, 0);
4329 gjmp_addr(d);
4330 gsym(a);
4331 gsym_addr(b, d);
4332 } else if (tok == '{') {
4333 Sym *llabel;
4335 next();
4336 /* record local declaration stack position */
4337 s = local_stack;
4338 llabel = local_label_stack;
4339 /* handle local labels declarations */
4340 if (tok == TOK_LABEL) {
4341 next();
4342 for(;;) {
4343 if (tok < TOK_UIDENT)
4344 expect("label identifier");
4345 label_push(&local_label_stack, tok, LABEL_DECLARED);
4346 next();
4347 if (tok == ',') {
4348 next();
4349 } else {
4350 skip(';');
4351 break;
4355 while (tok != '}') {
4356 decl(VT_LOCAL);
4357 if (tok != '}') {
4358 if (is_expr)
4359 vpop();
4360 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4363 /* pop locally defined labels */
4364 label_pop(&local_label_stack, llabel);
4365 if(is_expr) {
4366 /* XXX: this solution makes only valgrind happy...
4367 triggered by gcc.c-torture/execute/20000917-1.c */
4368 Sym *p;
4369 switch(vtop->type.t & VT_BTYPE) {
4370 case VT_PTR:
4371 case VT_STRUCT:
4372 case VT_ENUM:
4373 case VT_FUNC:
4374 for(p=vtop->type.ref;p;p=p->prev)
4375 if(p->prev==s)
4376 error("unsupported expression type");
4379 /* pop locally defined symbols */
4380 sym_pop(&local_stack, s);
4381 next();
4382 } else if (tok == TOK_RETURN) {
4383 next();
4384 if (tok != ';') {
4385 gexpr();
4386 gen_assign_cast(&func_vt);
4387 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4388 CType type;
4389 /* if returning structure, must copy it to implicit
4390 first pointer arg location */
4391 #ifdef TCC_ARM_EABI
4392 int align, size;
4393 size = type_size(&func_vt,&align);
4394 if(size <= 4)
4396 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4397 && (align & 3))
4399 int addr;
4400 loc = (loc - size) & -4;
4401 addr = loc;
4402 type = func_vt;
4403 vset(&type, VT_LOCAL | VT_LVAL, addr);
4404 vswap();
4405 vstore();
4406 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4408 vtop->type = int_type;
4409 gv(RC_IRET);
4410 } else {
4411 #endif
4412 type = func_vt;
4413 mk_pointer(&type);
4414 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4415 indir();
4416 vswap();
4417 /* copy structure value to pointer */
4418 vstore();
4419 #ifdef TCC_ARM_EABI
4421 #endif
4422 } else if (is_float(func_vt.t)) {
4423 gv(rc_fret(func_vt.t));
4424 } else {
4425 gv(RC_IRET);
4427 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4429 skip(';');
4430 rsym = gjmp(rsym); /* jmp */
4431 } else if (tok == TOK_BREAK) {
4432 /* compute jump */
4433 if (!bsym)
4434 error("cannot break");
4435 *bsym = gjmp(*bsym);
4436 next();
4437 skip(';');
4438 } else if (tok == TOK_CONTINUE) {
4439 /* compute jump */
4440 if (!csym)
4441 error("cannot continue");
4442 *csym = gjmp(*csym);
4443 next();
4444 skip(';');
4445 } else if (tok == TOK_FOR) {
4446 int e;
4447 next();
4448 skip('(');
4449 s = local_stack;
4450 if (tok != ';') {
4451 /* c99 for-loop init decl? */
4452 if (!decl0(VT_LOCAL, 1)) {
4453 /* no, regular for-loop init expr */
4454 gexpr();
4455 vpop();
4458 skip(';');
4459 d = ind;
4460 c = ind;
4461 a = 0;
4462 b = 0;
4463 if (tok != ';') {
4464 gexpr();
4465 a = gtst(1, 0);
4467 skip(';');
4468 if (tok != ')') {
4469 e = gjmp(0);
4470 c = ind;
4471 gexpr();
4472 vpop();
4473 gjmp_addr(d);
4474 gsym(e);
4476 skip(')');
4477 block(&a, &b, case_sym, def_sym, case_reg, 0);
4478 gjmp_addr(c);
4479 gsym(a);
4480 gsym_addr(b, c);
4481 sym_pop(&local_stack, s);
4482 } else
4483 if (tok == TOK_DO) {
4484 next();
4485 a = 0;
4486 b = 0;
4487 d = ind;
4488 block(&a, &b, case_sym, def_sym, case_reg, 0);
4489 skip(TOK_WHILE);
4490 skip('(');
4491 gsym(b);
4492 gexpr();
4493 c = gtst(0, 0);
4494 gsym_addr(c, d);
4495 skip(')');
4496 gsym(a);
4497 skip(';');
4498 } else
4499 if (tok == TOK_SWITCH) {
4500 next();
4501 skip('(');
4502 gexpr();
4503 /* XXX: other types than integer */
4504 case_reg = gv(RC_INT);
4505 vpop();
4506 skip(')');
4507 a = 0;
4508 b = gjmp(0); /* jump to first case */
4509 c = 0;
4510 block(&a, csym, &b, &c, case_reg, 0);
4511 /* if no default, jmp after switch */
4512 if (c == 0)
4513 c = ind;
4514 /* default label */
4515 gsym_addr(b, c);
4516 /* break label */
4517 gsym(a);
4518 } else
4519 if (tok == TOK_CASE) {
4520 int v1, v2;
4521 if (!case_sym)
4522 expect("switch");
4523 next();
4524 v1 = expr_const();
4525 v2 = v1;
4526 if (gnu_ext && tok == TOK_DOTS) {
4527 next();
4528 v2 = expr_const();
4529 if (v2 < v1)
4530 warning("empty case range");
4532 /* since a case is like a label, we must skip it with a jmp */
4533 b = gjmp(0);
4534 gsym(*case_sym);
4535 vseti(case_reg, 0);
4536 vpushi(v1);
4537 if (v1 == v2) {
4538 gen_op(TOK_EQ);
4539 *case_sym = gtst(1, 0);
4540 } else {
4541 gen_op(TOK_GE);
4542 *case_sym = gtst(1, 0);
4543 vseti(case_reg, 0);
4544 vpushi(v2);
4545 gen_op(TOK_LE);
4546 *case_sym = gtst(1, *case_sym);
4548 gsym(b);
4549 skip(':');
4550 is_expr = 0;
4551 goto block_after_label;
4552 } else
4553 if (tok == TOK_DEFAULT) {
4554 next();
4555 skip(':');
4556 if (!def_sym)
4557 expect("switch");
4558 if (*def_sym)
4559 error("too many 'default'");
4560 *def_sym = ind;
4561 is_expr = 0;
4562 goto block_after_label;
4563 } else
4564 if (tok == TOK_GOTO) {
4565 next();
4566 if (tok == '*' && gnu_ext) {
4567 /* computed goto */
4568 next();
4569 gexpr();
4570 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4571 expect("pointer");
4572 ggoto();
4573 } else if (tok >= TOK_UIDENT) {
4574 s = label_find(tok);
4575 /* put forward definition if needed */
4576 if (!s) {
4577 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4578 } else {
4579 if (s->r == LABEL_DECLARED)
4580 s->r = LABEL_FORWARD;
4582 /* label already defined */
4583 if (s->r & LABEL_FORWARD)
4584 s->jnext = gjmp(s->jnext);
4585 else
4586 gjmp_addr(s->jnext);
4587 next();
4588 } else {
4589 expect("label identifier");
4591 skip(';');
4592 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4593 asm_instr();
4594 } else {
4595 b = is_label();
4596 if (b) {
4597 /* label case */
4598 s = label_find(b);
4599 if (s) {
4600 if (s->r == LABEL_DEFINED)
4601 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4602 gsym(s->jnext);
4603 s->r = LABEL_DEFINED;
4604 } else {
4605 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4607 s->jnext = ind;
4608 /* we accept this, but it is a mistake */
4609 block_after_label:
4610 if (tok == '}') {
4611 warning("deprecated use of label at end of compound statement");
4612 } else {
4613 if (is_expr)
4614 vpop();
4615 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4617 } else {
4618 /* expression case */
4619 if (tok != ';') {
4620 if (is_expr) {
4621 vpop();
4622 gexpr();
4623 } else {
4624 gexpr();
4625 vpop();
4628 skip(';');
4633 /* t is the array or struct type. c is the array or struct
4634 address. cur_index/cur_field is the pointer to the current
4635 value. 'size_only' is true if only size info is needed (only used
4636 in arrays) */
4637 static void decl_designator(CType *type, Section *sec, unsigned long c,
4638 int *cur_index, Sym **cur_field,
4639 int size_only)
4641 Sym *s, *f;
4642 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4643 CType type1;
4645 notfirst = 0;
4646 elem_size = 0;
4647 nb_elems = 1;
4648 if (gnu_ext && (l = is_label()) != 0)
4649 goto struct_field;
4650 while (tok == '[' || tok == '.') {
4651 if (tok == '[') {
4652 if (!(type->t & VT_ARRAY))
4653 expect("array type");
4654 s = type->ref;
4655 next();
4656 index = expr_const();
4657 if (index < 0 || (s->c >= 0 && index >= s->c))
4658 expect("invalid index");
4659 if (tok == TOK_DOTS && gnu_ext) {
4660 next();
4661 index_last = expr_const();
4662 if (index_last < 0 ||
4663 (s->c >= 0 && index_last >= s->c) ||
4664 index_last < index)
4665 expect("invalid index");
4666 } else {
4667 index_last = index;
4669 skip(']');
4670 if (!notfirst)
4671 *cur_index = index_last;
4672 type = pointed_type(type);
4673 elem_size = type_size(type, &align);
4674 c += index * elem_size;
4675 /* NOTE: we only support ranges for last designator */
4676 nb_elems = index_last - index + 1;
4677 if (nb_elems != 1) {
4678 notfirst = 1;
4679 break;
4681 } else {
4682 next();
4683 l = tok;
4684 next();
4685 struct_field:
4686 if ((type->t & VT_BTYPE) != VT_STRUCT)
4687 expect("struct/union type");
4688 s = type->ref;
4689 l |= SYM_FIELD;
4690 f = s->next;
4691 while (f) {
4692 if (f->v == l)
4693 break;
4694 f = f->next;
4696 if (!f)
4697 expect("field");
4698 if (!notfirst)
4699 *cur_field = f;
4700 /* XXX: fix this mess by using explicit storage field */
4701 type1 = f->type;
4702 type1.t |= (type->t & ~VT_TYPE);
4703 type = &type1;
4704 c += f->c;
4706 notfirst = 1;
4708 if (notfirst) {
4709 if (tok == '=') {
4710 next();
4711 } else {
4712 if (!gnu_ext)
4713 expect("=");
4715 } else {
4716 if (type->t & VT_ARRAY) {
4717 index = *cur_index;
4718 type = pointed_type(type);
4719 c += index * type_size(type, &align);
4720 } else {
4721 f = *cur_field;
4722 if (!f)
4723 error("too many field init");
4724 /* XXX: fix this mess by using explicit storage field */
4725 type1 = f->type;
4726 type1.t |= (type->t & ~VT_TYPE);
4727 type = &type1;
4728 c += f->c;
4731 decl_initializer(type, sec, c, 0, size_only);
4733 /* XXX: make it more general */
4734 if (!size_only && nb_elems > 1) {
4735 unsigned long c_end;
4736 uint8_t *src, *dst;
4737 int i;
4739 if (!sec)
4740 error("range init not supported yet for dynamic storage");
4741 c_end = c + nb_elems * elem_size;
4742 if (c_end > sec->data_allocated)
4743 section_realloc(sec, c_end);
4744 src = sec->data + c;
4745 dst = src;
4746 for(i = 1; i < nb_elems; i++) {
4747 dst += elem_size;
4748 memcpy(dst, src, elem_size);
4753 #define EXPR_VAL 0
4754 #define EXPR_CONST 1
4755 #define EXPR_ANY 2
4757 /* store a value or an expression directly in global data or in local array */
4758 static void init_putv(CType *type, Section *sec, unsigned long c,
4759 int v, int expr_type)
4761 int saved_global_expr, bt, bit_pos, bit_size;
4762 void *ptr;
4763 unsigned long long bit_mask;
4764 CType dtype;
4766 switch(expr_type) {
4767 case EXPR_VAL:
4768 vpushi(v);
4769 break;
4770 case EXPR_CONST:
4771 /* compound literals must be allocated globally in this case */
4772 saved_global_expr = global_expr;
4773 global_expr = 1;
4774 expr_const1();
4775 global_expr = saved_global_expr;
4776 /* NOTE: symbols are accepted */
4777 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4778 error("initializer element is not constant");
4779 break;
4780 case EXPR_ANY:
4781 expr_eq();
4782 break;
4785 dtype = *type;
4786 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4788 if (sec) {
4789 /* XXX: not portable */
4790 /* XXX: generate error if incorrect relocation */
4791 gen_assign_cast(&dtype);
4792 bt = type->t & VT_BTYPE;
4793 /* we'll write at most 12 bytes */
4794 if (c + 12 > sec->data_allocated) {
4795 section_realloc(sec, c + 12);
4797 ptr = sec->data + c;
4798 /* XXX: make code faster ? */
4799 if (!(type->t & VT_BITFIELD)) {
4800 bit_pos = 0;
4801 bit_size = 32;
4802 bit_mask = -1LL;
4803 } else {
4804 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4805 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4806 bit_mask = (1LL << bit_size) - 1;
4808 if ((vtop->r & VT_SYM) &&
4809 (bt == VT_BYTE ||
4810 bt == VT_SHORT ||
4811 bt == VT_DOUBLE ||
4812 bt == VT_LDOUBLE ||
4813 bt == VT_LLONG ||
4814 (bt == VT_INT && bit_size != 32)))
4815 error("initializer element is not computable at load time");
4816 switch(bt) {
4817 case VT_BOOL:
4818 vtop->c.i = (vtop->c.i != 0);
4819 case VT_BYTE:
4820 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4821 break;
4822 case VT_SHORT:
4823 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4824 break;
4825 case VT_DOUBLE:
4826 *(double *)ptr = vtop->c.d;
4827 break;
4828 case VT_LDOUBLE:
4829 *(long double *)ptr = vtop->c.ld;
4830 break;
4831 case VT_LLONG:
4832 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4833 break;
4834 default:
4835 if (vtop->r & VT_SYM) {
4836 greloc(sec, vtop->sym, c, R_DATA_PTR);
4838 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4839 break;
4841 vtop--;
4842 } else {
4843 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4844 vswap();
4845 vstore();
4846 vpop();
4850 /* put zeros for variable based init */
4851 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4853 if (sec) {
4854 /* nothing to do because globals are already set to zero */
4855 } else {
4856 vpush_global_sym(&func_old_type, TOK_memset);
4857 vseti(VT_LOCAL, c);
4858 vpushi(0);
4859 vpushi(size);
4860 gfunc_call(3);
4864 /* 't' contains the type and storage info. 'c' is the offset of the
4865 object in section 'sec'. If 'sec' is NULL, it means stack based
4866 allocation. 'first' is true if array '{' must be read (multi
4867 dimension implicit array init handling). 'size_only' is true if
4868 size only evaluation is wanted (only for arrays). */
4869 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4870 int first, int size_only)
4872 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
4873 int size1, align1, expr_type;
4874 Sym *s, *f;
4875 CType *t1;
4877 if (type->t & VT_VLA) {
4878 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4879 int a;
4880 CValue retcval;
4882 vpush_global_sym(&func_old_type, TOK_alloca);
4883 vla_runtime_type_size(type, &a);
4884 gfunc_call(1);
4886 /* return value */
4887 retcval.i = 0;
4888 vsetc(type, REG_IRET, &retcval);
4889 vset(type, VT_LOCAL|VT_LVAL, c);
4890 vswap();
4891 vstore();
4892 vpop();
4893 #else
4894 error("variable length arrays unsupported for this target");
4895 #endif
4896 } else if (type->t & VT_ARRAY) {
4897 s = type->ref;
4898 n = s->c;
4899 array_length = 0;
4900 t1 = pointed_type(type);
4901 size1 = type_size(t1, &align1);
4903 no_oblock = 1;
4904 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4905 tok == '{') {
4906 if (tok != '{')
4907 error("character array initializer must be a literal,"
4908 " optionally enclosed in braces");
4909 skip('{');
4910 no_oblock = 0;
4913 /* only parse strings here if correct type (otherwise: handle
4914 them as ((w)char *) expressions */
4915 if ((tok == TOK_LSTR &&
4916 #ifdef TCC_TARGET_PE
4917 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4918 #else
4919 (t1->t & VT_BTYPE) == VT_INT
4920 #endif
4921 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4922 while (tok == TOK_STR || tok == TOK_LSTR) {
4923 int cstr_len, ch;
4924 CString *cstr;
4926 cstr = tokc.cstr;
4927 /* compute maximum number of chars wanted */
4928 if (tok == TOK_STR)
4929 cstr_len = cstr->size;
4930 else
4931 cstr_len = cstr->size / sizeof(nwchar_t);
4932 cstr_len--;
4933 nb = cstr_len;
4934 if (n >= 0 && nb > (n - array_length))
4935 nb = n - array_length;
4936 if (!size_only) {
4937 if (cstr_len > nb)
4938 warning("initializer-string for array is too long");
4939 /* in order to go faster for common case (char
4940 string in global variable, we handle it
4941 specifically */
4942 if (sec && tok == TOK_STR && size1 == 1) {
4943 memcpy(sec->data + c + array_length, cstr->data, nb);
4944 } else {
4945 for(i=0;i<nb;i++) {
4946 if (tok == TOK_STR)
4947 ch = ((unsigned char *)cstr->data)[i];
4948 else
4949 ch = ((nwchar_t *)cstr->data)[i];
4950 init_putv(t1, sec, c + (array_length + i) * size1,
4951 ch, EXPR_VAL);
4955 array_length += nb;
4956 next();
4958 /* only add trailing zero if enough storage (no
4959 warning in this case since it is standard) */
4960 if (n < 0 || array_length < n) {
4961 if (!size_only) {
4962 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4964 array_length++;
4966 } else {
4967 index = 0;
4968 while (tok != '}') {
4969 decl_designator(type, sec, c, &index, NULL, size_only);
4970 if (n >= 0 && index >= n)
4971 error("index too large");
4972 /* must put zero in holes (note that doing it that way
4973 ensures that it even works with designators) */
4974 if (!size_only && array_length < index) {
4975 init_putz(t1, sec, c + array_length * size1,
4976 (index - array_length) * size1);
4978 index++;
4979 if (index > array_length)
4980 array_length = index;
4981 /* special test for multi dimensional arrays (may not
4982 be strictly correct if designators are used at the
4983 same time) */
4984 if (index >= n && no_oblock)
4985 break;
4986 if (tok == '}')
4987 break;
4988 skip(',');
4991 if (!no_oblock)
4992 skip('}');
4993 /* put zeros at the end */
4994 if (!size_only && n >= 0 && array_length < n) {
4995 init_putz(t1, sec, c + array_length * size1,
4996 (n - array_length) * size1);
4998 /* patch type size if needed */
4999 if (n < 0)
5000 s->c = array_length;
5001 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5002 (sec || !first || tok == '{')) {
5003 int par_count;
5005 /* NOTE: the previous test is a specific case for automatic
5006 struct/union init */
5007 /* XXX: union needs only one init */
5009 /* XXX: this test is incorrect for local initializers
5010 beginning with ( without {. It would be much more difficult
5011 to do it correctly (ideally, the expression parser should
5012 be used in all cases) */
5013 par_count = 0;
5014 if (tok == '(') {
5015 AttributeDef ad1;
5016 CType type1;
5017 next();
5018 while (tok == '(') {
5019 par_count++;
5020 next();
5022 if (!parse_btype(&type1, &ad1))
5023 expect("cast");
5024 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5025 #if 0
5026 if (!is_assignable_types(type, &type1))
5027 error("invalid type for cast");
5028 #endif
5029 skip(')');
5031 no_oblock = 1;
5032 if (first || tok == '{') {
5033 skip('{');
5034 no_oblock = 0;
5036 s = type->ref;
5037 f = s->next;
5038 array_length = 0;
5039 index = 0;
5040 n = s->c;
5041 while (tok != '}') {
5042 decl_designator(type, sec, c, NULL, &f, size_only);
5043 index = f->c;
5044 if (!size_only && array_length < index) {
5045 init_putz(type, sec, c + array_length,
5046 index - array_length);
5048 index = index + type_size(&f->type, &align1);
5049 if (index > array_length)
5050 array_length = index;
5052 /* gr: skip fields from same union - ugly. */
5053 while (f->next) {
5054 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5055 /* test for same offset */
5056 if (f->next->c != f->c)
5057 break;
5058 /* if yes, test for bitfield shift */
5059 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5060 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5061 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5062 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5063 if (bit_pos_1 != bit_pos_2)
5064 break;
5066 f = f->next;
5069 f = f->next;
5070 if (no_oblock && f == NULL)
5071 break;
5072 if (tok == '}')
5073 break;
5074 skip(',');
5076 /* put zeros at the end */
5077 if (!size_only && array_length < n) {
5078 init_putz(type, sec, c + array_length,
5079 n - array_length);
5081 if (!no_oblock)
5082 skip('}');
5083 while (par_count) {
5084 skip(')');
5085 par_count--;
5087 } else if (tok == '{') {
5088 next();
5089 decl_initializer(type, sec, c, first, size_only);
5090 skip('}');
5091 } else if (size_only) {
5092 /* just skip expression */
5093 parlevel = parlevel1 = 0;
5094 while ((parlevel > 0 || parlevel1 > 0 ||
5095 (tok != '}' && tok != ',')) && tok != -1) {
5096 if (tok == '(')
5097 parlevel++;
5098 else if (tok == ')')
5099 parlevel--;
5100 else if (tok == '{')
5101 parlevel1++;
5102 else if (tok == '}')
5103 parlevel1--;
5104 next();
5106 } else {
5107 /* currently, we always use constant expression for globals
5108 (may change for scripting case) */
5109 expr_type = EXPR_CONST;
5110 if (!sec)
5111 expr_type = EXPR_ANY;
5112 init_putv(type, sec, c, 0, expr_type);
5116 /* parse an initializer for type 't' if 'has_init' is non zero, and
5117 allocate space in local or global data space ('r' is either
5118 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5119 variable 'v' with an associated name represented by 'asm_label' of
5120 scope 'scope' is declared before initializers are parsed. If 'v' is
5121 zero, then a reference to the new object is put in the value stack.
5122 If 'has_init' is 2, a special parsing is done to handle string
5123 constants. */
5124 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5125 int has_init, int v, char *asm_label,
5126 int scope)
5128 int size, align, addr, data_offset;
5129 int level;
5130 ParseState saved_parse_state = {0};
5131 TokenString init_str;
5132 Section *sec;
5133 Sym *flexible_array;
5135 flexible_array = NULL;
5136 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5137 Sym *field;
5138 field = type->ref;
5139 while (field && field->next)
5140 field = field->next;
5141 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5142 flexible_array = field;
5145 size = type_size(type, &align);
5146 /* If unknown size, we must evaluate it before
5147 evaluating initializers because
5148 initializers can generate global data too
5149 (e.g. string pointers or ISOC99 compound
5150 literals). It also simplifies local
5151 initializers handling */
5152 tok_str_new(&init_str);
5153 if (size < 0 || (flexible_array && has_init)) {
5154 if (!has_init)
5155 error("unknown type size");
5156 /* get all init string */
5157 if (has_init == 2) {
5158 /* only get strings */
5159 while (tok == TOK_STR || tok == TOK_LSTR) {
5160 tok_str_add_tok(&init_str);
5161 next();
5163 } else {
5164 level = 0;
5165 while (level > 0 || (tok != ',' && tok != ';')) {
5166 if (tok < 0)
5167 error("unexpected end of file in initializer");
5168 tok_str_add_tok(&init_str);
5169 if (tok == '{')
5170 level++;
5171 else if (tok == '}') {
5172 level--;
5173 if (level <= 0) {
5174 next();
5175 break;
5178 next();
5181 tok_str_add(&init_str, -1);
5182 tok_str_add(&init_str, 0);
5184 /* compute size */
5185 save_parse_state(&saved_parse_state);
5187 macro_ptr = init_str.str;
5188 next();
5189 decl_initializer(type, NULL, 0, 1, 1);
5190 /* prepare second initializer parsing */
5191 macro_ptr = init_str.str;
5192 next();
5194 /* if still unknown size, error */
5195 size = type_size(type, &align);
5196 if (size < 0)
5197 error("unknown type size");
5199 if (flexible_array)
5200 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5201 /* take into account specified alignment if bigger */
5202 if (ad->aligned) {
5203 if (ad->aligned > align)
5204 align = ad->aligned;
5205 } else if (ad->packed) {
5206 align = 1;
5208 if ((r & VT_VALMASK) == VT_LOCAL) {
5209 sec = NULL;
5210 #ifdef CONFIG_TCC_BCHECK
5211 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5212 loc--;
5214 #endif
5215 loc = (loc - size) & -align;
5216 addr = loc;
5217 #ifdef CONFIG_TCC_BCHECK
5218 /* handles bounds */
5219 /* XXX: currently, since we do only one pass, we cannot track
5220 '&' operators, so we add only arrays */
5221 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5222 unsigned long *bounds_ptr;
5223 /* add padding between regions */
5224 loc--;
5225 /* then add local bound info */
5226 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5227 bounds_ptr[0] = addr;
5228 bounds_ptr[1] = size;
5230 #endif
5231 if (v) {
5232 /* local variable */
5233 sym_push(v, type, r, addr);
5234 } else {
5235 /* push local reference */
5236 vset(type, r, addr);
5238 } else {
5239 Sym *sym;
5241 sym = NULL;
5242 if (v && scope == VT_CONST) {
5243 /* see if the symbol was already defined */
5244 sym = sym_find(v);
5245 if (sym) {
5246 if (!is_compatible_types(&sym->type, type))
5247 error("incompatible types for redefinition of '%s'",
5248 get_tok_str(v, NULL));
5249 if (sym->type.t & VT_EXTERN) {
5250 /* if the variable is extern, it was not allocated */
5251 sym->type.t &= ~VT_EXTERN;
5252 /* set array size if it was ommited in extern
5253 declaration */
5254 if ((sym->type.t & VT_ARRAY) &&
5255 sym->type.ref->c < 0 &&
5256 type->ref->c >= 0)
5257 sym->type.ref->c = type->ref->c;
5258 } else {
5259 /* we accept several definitions of the same
5260 global variable. this is tricky, because we
5261 must play with the SHN_COMMON type of the symbol */
5262 /* XXX: should check if the variable was already
5263 initialized. It is incorrect to initialized it
5264 twice */
5265 /* no init data, we won't add more to the symbol */
5266 if (!has_init)
5267 goto no_alloc;
5272 /* allocate symbol in corresponding section */
5273 sec = ad->section;
5274 if (!sec) {
5275 if (has_init)
5276 sec = data_section;
5277 else if (tcc_state->nocommon)
5278 sec = bss_section;
5280 if (sec) {
5281 data_offset = sec->data_offset;
5282 data_offset = (data_offset + align - 1) & -align;
5283 addr = data_offset;
5284 /* very important to increment global pointer at this time
5285 because initializers themselves can create new initializers */
5286 data_offset += size;
5287 #ifdef CONFIG_TCC_BCHECK
5288 /* add padding if bound check */
5289 if (tcc_state->do_bounds_check)
5290 data_offset++;
5291 #endif
5292 sec->data_offset = data_offset;
5293 /* allocate section space to put the data */
5294 if (sec->sh_type != SHT_NOBITS &&
5295 data_offset > sec->data_allocated)
5296 section_realloc(sec, data_offset);
5297 /* align section if needed */
5298 if (align > sec->sh_addralign)
5299 sec->sh_addralign = align;
5300 } else {
5301 addr = 0; /* avoid warning */
5304 if (v) {
5305 if (scope != VT_CONST || !sym) {
5306 sym = sym_push(v, type, r | VT_SYM, 0);
5307 sym->asm_label = asm_label;
5309 /* update symbol definition */
5310 if (sec) {
5311 put_extern_sym(sym, sec, addr, size);
5312 } else {
5313 ElfW(Sym) *esym;
5314 /* put a common area */
5315 put_extern_sym(sym, NULL, align, size);
5316 /* XXX: find a nicer way */
5317 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5318 esym->st_shndx = SHN_COMMON;
5320 } else {
5321 CValue cval;
5323 /* push global reference */
5324 sym = get_sym_ref(type, sec, addr, size);
5325 cval.ul = 0;
5326 vsetc(type, VT_CONST | VT_SYM, &cval);
5327 vtop->sym = sym;
5329 /* patch symbol weakness */
5330 if (type->t & VT_WEAK)
5331 weaken_symbol(sym);
5332 #ifdef CONFIG_TCC_BCHECK
5333 /* handles bounds now because the symbol must be defined
5334 before for the relocation */
5335 if (tcc_state->do_bounds_check) {
5336 unsigned long *bounds_ptr;
5338 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5339 /* then add global bound info */
5340 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5341 bounds_ptr[0] = 0; /* relocated */
5342 bounds_ptr[1] = size;
5344 #endif
5346 if (has_init || (type->t & VT_VLA)) {
5347 decl_initializer(type, sec, addr, 1, 0);
5348 /* restore parse state if needed */
5349 if (init_str.str) {
5350 tok_str_free(init_str.str);
5351 restore_parse_state(&saved_parse_state);
5353 /* patch flexible array member size back to -1, */
5354 /* for possible subsequent similar declarations */
5355 if (flexible_array)
5356 flexible_array->type.ref->c = -1;
5358 no_alloc: ;
5361 static void put_func_debug(Sym *sym)
5363 char buf[512];
5365 /* stabs info */
5366 /* XXX: we put here a dummy type */
5367 snprintf(buf, sizeof(buf), "%s:%c1",
5368 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5369 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5370 cur_text_section, sym->c);
5371 /* //gr gdb wants a line at the function */
5372 put_stabn(N_SLINE, 0, file->line_num, 0);
5373 last_ind = 0;
5374 last_line_num = 0;
5377 /* parse an old style function declaration list */
5378 /* XXX: check multiple parameter */
5379 static void func_decl_list(Sym *func_sym)
5381 AttributeDef ad;
5382 int v;
5383 Sym *s;
5384 CType btype, type;
5386 /* parse each declaration */
5387 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5388 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5389 if (!parse_btype(&btype, &ad))
5390 expect("declaration list");
5391 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5392 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5393 tok == ';') {
5394 /* we accept no variable after */
5395 } else {
5396 for(;;) {
5397 type = btype;
5398 type_decl(&type, &ad, &v, TYPE_DIRECT);
5399 /* find parameter in function parameter list */
5400 s = func_sym->next;
5401 while (s != NULL) {
5402 if ((s->v & ~SYM_FIELD) == v)
5403 goto found;
5404 s = s->next;
5406 error("declaration for parameter '%s' but no such parameter",
5407 get_tok_str(v, NULL));
5408 found:
5409 /* check that no storage specifier except 'register' was given */
5410 if (type.t & VT_STORAGE)
5411 error("storage class specified for '%s'", get_tok_str(v, NULL));
5412 convert_parameter_type(&type);
5413 /* we can add the type (NOTE: it could be local to the function) */
5414 s->type = type;
5415 /* accept other parameters */
5416 if (tok == ',')
5417 next();
5418 else
5419 break;
5422 skip(';');
5426 /* parse a function defined by symbol 'sym' and generate its code in
5427 'cur_text_section' */
5428 static void gen_function(Sym *sym)
5430 int saved_nocode_wanted = nocode_wanted;
5431 nocode_wanted = 0;
5432 ind = cur_text_section->data_offset;
5433 /* NOTE: we patch the symbol size later */
5434 put_extern_sym(sym, cur_text_section, ind, 0);
5435 funcname = get_tok_str(sym->v, NULL);
5436 func_ind = ind;
5437 /* put debug symbol */
5438 if (tcc_state->do_debug)
5439 put_func_debug(sym);
5440 /* push a dummy symbol to enable local sym storage */
5441 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5442 gfunc_prolog(&sym->type);
5443 rsym = 0;
5444 block(NULL, NULL, NULL, NULL, 0, 0);
5445 gsym(rsym);
5446 gfunc_epilog();
5447 cur_text_section->data_offset = ind;
5448 label_pop(&global_label_stack, NULL);
5449 sym_pop(&local_stack, NULL); /* reset local stack */
5450 /* end of function */
5451 /* patch symbol size */
5452 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5453 ind - func_ind;
5454 /* patch symbol weakness (this definition overrules any prototype) */
5455 if (sym->type.t & VT_WEAK)
5456 weaken_symbol(sym);
5457 if (tcc_state->do_debug) {
5458 put_stabn(N_FUN, 0, 0, ind - func_ind);
5460 /* It's better to crash than to generate wrong code */
5461 cur_text_section = NULL;
5462 funcname = ""; /* for safety */
5463 func_vt.t = VT_VOID; /* for safety */
5464 ind = 0; /* for safety */
5465 nocode_wanted = saved_nocode_wanted;
5468 ST_FUNC void gen_inline_functions(void)
5470 Sym *sym;
5471 int *str, inline_generated, i;
5472 struct InlineFunc *fn;
5474 /* iterate while inline function are referenced */
5475 for(;;) {
5476 inline_generated = 0;
5477 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5478 fn = tcc_state->inline_fns[i];
5479 sym = fn->sym;
5480 if (sym && sym->c) {
5481 /* the function was used: generate its code and
5482 convert it to a normal function */
5483 str = fn->token_str;
5484 fn->sym = NULL;
5485 if (file)
5486 strcpy(file->filename, fn->filename);
5487 sym->r = VT_SYM | VT_CONST;
5488 sym->type.t &= ~VT_INLINE;
5490 macro_ptr = str;
5491 next();
5492 cur_text_section = text_section;
5493 gen_function(sym);
5494 macro_ptr = NULL; /* fail safe */
5496 inline_generated = 1;
5499 if (!inline_generated)
5500 break;
5502 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5503 fn = tcc_state->inline_fns[i];
5504 str = fn->token_str;
5505 tok_str_free(str);
5507 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5510 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5511 static int decl0(int l, int is_for_loop_init)
5513 int v, has_init, r;
5514 CType type, btype;
5515 Sym *sym;
5516 AttributeDef ad;
5518 while (1) {
5519 if (!parse_btype(&btype, &ad)) {
5520 if (is_for_loop_init)
5521 return 0;
5522 /* skip redundant ';' */
5523 /* XXX: find more elegant solution */
5524 if (tok == ';') {
5525 next();
5526 continue;
5528 if (l == VT_CONST &&
5529 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5530 /* global asm block */
5531 asm_global_instr();
5532 continue;
5534 /* special test for old K&R protos without explicit int
5535 type. Only accepted when defining global data */
5536 if (l == VT_LOCAL || tok < TOK_DEFINE)
5537 break;
5538 btype.t = VT_INT;
5540 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5541 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5542 tok == ';') {
5543 /* we accept no variable after */
5544 next();
5545 continue;
5547 while (1) { /* iterate thru each declaration */
5548 char *asm_label; // associated asm label
5549 type = btype;
5550 type_decl(&type, &ad, &v, TYPE_DIRECT);
5551 #if 0
5553 char buf[500];
5554 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5555 printf("type = '%s'\n", buf);
5557 #endif
5558 if ((type.t & VT_BTYPE) == VT_FUNC) {
5559 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5560 error("function without file scope cannot be static");
5562 /* if old style function prototype, we accept a
5563 declaration list */
5564 sym = type.ref;
5565 if (sym->c == FUNC_OLD)
5566 func_decl_list(sym);
5569 asm_label = NULL;
5570 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5571 CString astr;
5573 asm_label_instr(&astr);
5574 asm_label = tcc_strdup(astr.data);
5575 cstr_free(&astr);
5577 /* parse one last attribute list, after asm label */
5578 parse_attribute(&ad);
5581 if (ad.weak)
5582 type.t |= VT_WEAK;
5583 #ifdef TCC_TARGET_PE
5584 if (ad.func_import)
5585 type.t |= VT_IMPORT;
5586 if (ad.func_export)
5587 type.t |= VT_EXPORT;
5588 #endif
5589 if (tok == '{') {
5590 if (l == VT_LOCAL)
5591 error("cannot use local functions");
5592 if ((type.t & VT_BTYPE) != VT_FUNC)
5593 expect("function definition");
5595 /* reject abstract declarators in function definition */
5596 sym = type.ref;
5597 while ((sym = sym->next) != NULL)
5598 if (!(sym->v & ~SYM_FIELD))
5599 expect("identifier");
5601 /* XXX: cannot do better now: convert extern line to static inline */
5602 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5603 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5605 sym = sym_find(v);
5606 if (sym) {
5607 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5608 goto func_error1;
5610 r = sym->type.ref->r;
5611 /* use func_call from prototype if not defined */
5612 if (FUNC_CALL(r) != FUNC_CDECL
5613 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5614 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5616 /* use export from prototype */
5617 if (FUNC_EXPORT(r))
5618 FUNC_EXPORT(type.ref->r) = 1;
5620 /* use static from prototype */
5621 if (sym->type.t & VT_STATIC)
5622 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5624 if (!is_compatible_types(&sym->type, &type)) {
5625 func_error1:
5626 error("incompatible types for redefinition of '%s'",
5627 get_tok_str(v, NULL));
5629 /* if symbol is already defined, then put complete type */
5630 sym->type = type;
5631 } else {
5632 /* put function symbol */
5633 sym = global_identifier_push(v, type.t, 0);
5634 sym->type.ref = type.ref;
5637 /* static inline functions are just recorded as a kind
5638 of macro. Their code will be emitted at the end of
5639 the compilation unit only if they are used */
5640 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5641 (VT_INLINE | VT_STATIC)) {
5642 TokenString func_str;
5643 int block_level;
5644 struct InlineFunc *fn;
5645 const char *filename;
5647 tok_str_new(&func_str);
5649 block_level = 0;
5650 for(;;) {
5651 int t;
5652 if (tok == TOK_EOF)
5653 error("unexpected end of file");
5654 tok_str_add_tok(&func_str);
5655 t = tok;
5656 next();
5657 if (t == '{') {
5658 block_level++;
5659 } else if (t == '}') {
5660 block_level--;
5661 if (block_level == 0)
5662 break;
5665 tok_str_add(&func_str, -1);
5666 tok_str_add(&func_str, 0);
5667 filename = file ? file->filename : "";
5668 fn = tcc_malloc(sizeof *fn + strlen(filename));
5669 strcpy(fn->filename, filename);
5670 fn->sym = sym;
5671 fn->token_str = func_str.str;
5672 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5674 } else {
5675 /* compute text section */
5676 cur_text_section = ad.section;
5677 if (!cur_text_section)
5678 cur_text_section = text_section;
5679 sym->r = VT_SYM | VT_CONST;
5680 gen_function(sym);
5682 break;
5683 } else {
5684 if (btype.t & VT_TYPEDEF) {
5685 /* save typedefed type */
5686 /* XXX: test storage specifiers ? */
5687 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5688 sym->type.t |= VT_TYPEDEF;
5689 } else {
5690 r = 0;
5691 if ((type.t & VT_BTYPE) == VT_FUNC) {
5692 /* external function definition */
5693 /* specific case for func_call attribute */
5694 type.ref->r = INT_ATTR(&ad);
5695 } else if (!(type.t & VT_ARRAY)) {
5696 /* not lvalue if array */
5697 r |= lvalue_type(type.t);
5699 has_init = (tok == '=');
5700 if (has_init && (type.t & VT_VLA))
5701 error("Variable length array cannot be initialized");
5702 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5703 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5704 !has_init && l == VT_CONST && type.ref->c < 0)) {
5705 /* external variable or function */
5706 /* NOTE: as GCC, uninitialized global static
5707 arrays of null size are considered as
5708 extern */
5709 sym = external_sym(v, &type, r, asm_label);
5711 if (type.t & VT_WEAK)
5712 weaken_symbol(sym);
5714 if (ad.alias_target) {
5715 Section tsec;
5716 Elf32_Sym *esym;
5717 Sym *alias_target;
5719 alias_target = sym_find(ad.alias_target);
5720 if (!alias_target || !alias_target->c)
5721 error("unsupported forward __alias__ attribute");
5722 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5723 tsec.sh_num = esym->st_shndx;
5724 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5726 } else {
5727 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5728 if (type.t & VT_STATIC)
5729 r |= VT_CONST;
5730 else
5731 r |= l;
5732 if (has_init)
5733 next();
5734 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5737 if (tok != ',') {
5738 if (is_for_loop_init)
5739 return 1;
5740 skip(';');
5741 break;
5743 next();
5745 ad.aligned = 0;
5748 return 0;
5751 ST_FUNC void decl(int l)
5753 decl0(l, 0);