x86_64: Fix compares with NaNs.
[tinycc/miki.git] / tccgen.c
blob9339b5f0f332732d09b95a2bfc0f12cd9a0e6f1f
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, size_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 /* we use our own 'finite' function to avoid potential problems with
94 non standard math libs */
95 /* XXX: endianness dependent */
96 ST_FUNC int ieee_finite(double d)
98 int *p = (int *)&d;
99 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
102 ST_FUNC void test_lvalue(void)
104 if (!(vtop->r & VT_LVAL))
105 expect("lvalue");
108 /* ------------------------------------------------------------------------- */
109 /* symbol allocator */
110 static Sym *__sym_malloc(void)
112 Sym *sym_pool, *sym, *last_sym;
113 int i;
115 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
116 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
118 last_sym = sym_free_first;
119 sym = sym_pool;
120 for(i = 0; i < SYM_POOL_NB; i++) {
121 sym->next = last_sym;
122 last_sym = sym;
123 sym++;
125 sym_free_first = last_sym;
126 return last_sym;
129 static inline Sym *sym_malloc(void)
131 Sym *sym;
132 sym = sym_free_first;
133 if (!sym)
134 sym = __sym_malloc();
135 sym_free_first = sym->next;
136 return sym;
139 ST_INLN void sym_free(Sym *sym)
141 sym->next = sym_free_first;
142 tcc_free(sym->asm_label);
143 sym_free_first = sym;
146 /* push, without hashing */
147 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
149 Sym *s;
150 s = sym_malloc();
151 s->asm_label = NULL;
152 s->v = v;
153 s->type.t = t;
154 s->type.ref = NULL;
155 #ifdef _WIN64
156 s->d = NULL;
157 #endif
158 s->c = c;
159 s->next = NULL;
160 /* add in stack */
161 s->prev = *ps;
162 *ps = s;
163 return s;
166 /* find a symbol and return its associated structure. 's' is the top
167 of the symbol stack */
168 ST_FUNC Sym *sym_find2(Sym *s, int v)
170 while (s) {
171 if (s->v == v)
172 return s;
173 s = s->prev;
175 return NULL;
178 /* structure lookup */
179 ST_INLN Sym *struct_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_struct;
187 /* find an identifier */
188 ST_INLN Sym *sym_find(int v)
190 v -= TOK_IDENT;
191 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
192 return NULL;
193 return table_ident[v]->sym_identifier;
196 /* push a given symbol on the symbol stack */
197 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
199 Sym *s, **ps;
200 TokenSym *ts;
202 if (local_stack)
203 ps = &local_stack;
204 else
205 ps = &global_stack;
206 s = sym_push2(ps, v, type->t, c);
207 s->type.ref = type->ref;
208 s->r = r;
209 /* don't record fields or anonymous symbols */
210 /* XXX: simplify */
211 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
212 /* record symbol in token array */
213 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
214 if (v & SYM_STRUCT)
215 ps = &ts->sym_struct;
216 else
217 ps = &ts->sym_identifier;
218 s->prev_tok = *ps;
219 *ps = s;
221 return s;
224 /* push a global identifier */
225 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
227 Sym *s, **ps;
228 s = sym_push2(&global_stack, v, t, c);
229 /* don't record anonymous symbol */
230 if (v < SYM_FIRST_ANOM) {
231 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
232 /* modify the top most local identifier, so that
233 sym_identifier will point to 's' when popped */
234 while (*ps != NULL)
235 ps = &(*ps)->prev_tok;
236 s->prev_tok = NULL;
237 *ps = s;
239 return s;
242 /* pop symbols until top reaches 'b' */
243 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
245 Sym *s, *ss, **ps;
246 TokenSym *ts;
247 int v;
249 s = *ptop;
250 while(s != b) {
251 ss = s->prev;
252 v = s->v;
253 /* remove symbol in token array */
254 /* XXX: simplify */
255 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
256 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
257 if (v & SYM_STRUCT)
258 ps = &ts->sym_struct;
259 else
260 ps = &ts->sym_identifier;
261 *ps = s->prev_tok;
263 sym_free(s);
264 s = ss;
266 *ptop = b;
269 static void weaken_symbol(Sym *sym)
271 sym->type.t |= VT_WEAK;
272 if (sym->c > 0) {
273 int esym_type;
274 ElfW(Sym) *esym;
276 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
277 esym_type = ELFW(ST_TYPE)(esym->st_info);
278 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
282 /* ------------------------------------------------------------------------- */
284 ST_FUNC void swap(int *p, int *q)
286 int t;
287 t = *p;
288 *p = *q;
289 *q = t;
292 static void vsetc(CType *type, int r, CValue *vc)
294 int v;
296 if (vtop >= vstack + (VSTACK_SIZE - 1))
297 tcc_error("memory full");
298 /* cannot let cpu flags if other instruction are generated. Also
299 avoid leaving VT_JMP anywhere except on the top of the stack
300 because it would complicate the code generator. */
301 if (vtop >= vstack) {
302 v = vtop->r & VT_VALMASK;
303 if (v == VT_CMP || (v & ~1) == VT_JMP)
304 gv(RC_INT);
306 vtop++;
307 vtop->type = *type;
308 vtop->r = r;
309 vtop->r2 = VT_CONST;
310 vtop->c = *vc;
313 /* push constant of type "type" with useless value */
314 void vpush(CType *type)
316 CValue cval;
317 vsetc(type, VT_CONST, &cval);
320 /* push integer constant */
321 ST_FUNC void vpushi(int v)
323 CValue cval;
324 cval.i = v;
325 vsetc(&int_type, VT_CONST, &cval);
328 /* push a pointer sized constant */
329 static void vpushs(long long v)
331 CValue cval;
332 if (PTR_SIZE == 4)
333 cval.i = (int)v;
334 else
335 cval.ull = v;
336 vsetc(&size_type, VT_CONST, &cval);
339 /* push long long constant */
340 static void vpushll(long long v)
342 CValue cval;
343 CType ctype;
344 ctype.t = VT_LLONG;
345 ctype.ref = 0;
346 cval.ull = v;
347 vsetc(&ctype, VT_CONST, &cval);
350 /* push arbitrary 64bit constant */
351 void vpush64(int ty, unsigned long long v)
353 CValue cval;
354 CType ctype;
355 ctype.t = ty;
356 cval.ull = v;
357 vsetc(&ctype, VT_CONST, &cval);
360 /* Return a static symbol pointing to a section */
361 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
363 int v;
364 Sym *sym;
366 v = anon_sym++;
367 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
368 sym->type.ref = type->ref;
369 sym->r = VT_CONST | VT_SYM;
370 put_extern_sym(sym, sec, offset, size);
371 return sym;
374 /* push a reference to a section offset by adding a dummy symbol */
375 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
377 CValue cval;
379 cval.ul = 0;
380 vsetc(type, VT_CONST | VT_SYM, &cval);
381 vtop->sym = get_sym_ref(type, sec, offset, size);
384 /* define a new external reference to a symbol 'v' of type 'u' */
385 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
387 Sym *s;
389 s = sym_find(v);
390 if (!s) {
391 /* push forward reference */
392 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
393 s->type.ref = type->ref;
394 s->r = r | VT_CONST | VT_SYM;
396 return s;
399 /* define a new external reference to a symbol 'v' with alternate asm
400 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
401 is no alternate name (most cases) */
402 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
404 Sym *s;
406 s = sym_find(v);
407 if (!s) {
408 /* push forward reference */
409 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
410 s->asm_label = asm_label;
411 s->type.t |= VT_EXTERN;
412 } else if (s->type.ref == func_old_type.ref) {
413 s->type.ref = type->ref;
414 s->r = r | VT_CONST | VT_SYM;
415 s->type.t |= VT_EXTERN;
416 } else if (!is_compatible_types(&s->type, type)) {
417 tcc_error("incompatible types for redefinition of '%s'",
418 get_tok_str(v, NULL));
420 return s;
423 /* push a reference to global symbol v */
424 ST_FUNC void vpush_global_sym(CType *type, int v)
426 Sym *sym;
427 CValue cval;
429 sym = external_global_sym(v, type, 0);
430 cval.ul = 0;
431 vsetc(type, VT_CONST | VT_SYM, &cval);
432 vtop->sym = sym;
435 ST_FUNC void vset(CType *type, int r, int v)
437 CValue cval;
439 cval.i = v;
440 vsetc(type, r, &cval);
443 static void vseti(int r, int v)
445 CType type;
446 type.t = VT_INT;
447 type.ref = 0;
448 vset(&type, r, v);
451 ST_FUNC void vswap(void)
453 SValue tmp;
455 /* cannot let cpu flags if other instruction are generated. Also
456 avoid leaving VT_JMP anywhere except on the top of the stack
457 because it would complicate the code generator. */
458 if (vtop >= vstack) {
459 int v = vtop->r & VT_VALMASK;
460 if (v == VT_CMP || (v & ~1) == VT_JMP)
461 gv(RC_INT);
463 tmp = vtop[0];
464 vtop[0] = vtop[-1];
465 vtop[-1] = tmp;
468 ST_FUNC void vpushv(SValue *v)
470 if (vtop >= vstack + (VSTACK_SIZE - 1))
471 tcc_error("memory full");
472 vtop++;
473 *vtop = *v;
476 static void vdup(void)
478 vpushv(vtop);
481 /* save r to the memory stack, and mark it as being free */
482 ST_FUNC void save_reg(int r)
484 int l, saved, size, align;
485 SValue *p, sv;
486 CType *type;
488 /* modify all stack values */
489 saved = 0;
490 l = 0;
491 for(p=vstack;p<=vtop;p++) {
492 if ((p->r & VT_VALMASK) == r ||
493 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
494 /* must save value on stack if not already done */
495 if (!saved) {
496 /* NOTE: must reload 'r' because r might be equal to r2 */
497 r = p->r & VT_VALMASK;
498 /* store register in the stack */
499 type = &p->type;
500 if ((p->r & VT_LVAL) ||
501 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
502 #ifdef TCC_TARGET_X86_64
503 type = &char_pointer_type;
504 #else
505 type = &int_type;
506 #endif
507 size = type_size(type, &align);
508 loc = (loc - size) & -align;
509 sv.type.t = type->t;
510 sv.r = VT_LOCAL | VT_LVAL;
511 sv.c.ul = loc;
512 store(r, &sv);
513 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
514 /* x86 specific: need to pop fp register ST0 if saved */
515 if (r == TREG_ST0) {
516 o(0xd8dd); /* fstp %st(0) */
518 #endif
519 #ifndef TCC_TARGET_X86_64
520 /* special long long case */
521 if ((type->t & VT_BTYPE) == VT_LLONG) {
522 sv.c.ul += 4;
523 store(p->r2, &sv);
525 #endif
526 l = loc;
527 saved = 1;
529 /* mark that stack entry as being saved on the stack */
530 if (p->r & VT_LVAL) {
531 /* also clear the bounded flag because the
532 relocation address of the function was stored in
533 p->c.ul */
534 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
535 } else {
536 p->r = lvalue_type(p->type.t) | VT_LOCAL;
538 p->r2 = VT_CONST;
539 p->c.ul = l;
544 #ifdef TCC_TARGET_ARM
545 /* find a register of class 'rc2' with at most one reference on stack.
546 * If none, call get_reg(rc) */
547 ST_FUNC int get_reg_ex(int rc, int rc2)
549 int r;
550 SValue *p;
552 for(r=0;r<NB_REGS;r++) {
553 if (reg_classes[r] & rc2) {
554 int n;
555 n=0;
556 for(p = vstack; p <= vtop; p++) {
557 if ((p->r & VT_VALMASK) == r ||
558 (p->r2 & VT_VALMASK) == r)
559 n++;
561 if (n <= 1)
562 return r;
565 return get_reg(rc);
567 #endif
569 /* find a free register of class 'rc'. If none, save one register */
570 ST_FUNC int get_reg(int rc)
572 int r;
573 SValue *p;
575 /* find a free register */
576 for(r=0;r<NB_REGS;r++) {
577 if (reg_classes[r] & rc) {
578 for(p=vstack;p<=vtop;p++) {
579 if ((p->r & VT_VALMASK) == r ||
580 (p->r2 & VT_VALMASK) == r)
581 goto notfound;
583 return r;
585 notfound: ;
588 /* no register left : free the first one on the stack (VERY
589 IMPORTANT to start from the bottom to ensure that we don't
590 spill registers used in gen_opi()) */
591 for(p=vstack;p<=vtop;p++) {
592 r = p->r & VT_VALMASK;
593 if (r < VT_CONST && (reg_classes[r] & rc))
594 goto save_found;
595 /* also look at second register (if long long) */
596 r = p->r2 & VT_VALMASK;
597 if (r < VT_CONST && (reg_classes[r] & rc)) {
598 save_found:
599 save_reg(r);
600 return r;
603 /* Should never comes here */
604 return -1;
607 /* save registers up to (vtop - n) stack entry */
608 ST_FUNC void save_regs(int n)
610 int r;
611 SValue *p, *p1;
612 p1 = vtop - n;
613 for(p = vstack;p <= p1; p++) {
614 r = p->r & VT_VALMASK;
615 if (r < VT_CONST) {
616 save_reg(r);
621 /* move register 's' to 'r', and flush previous value of r to memory
622 if needed */
623 static void move_reg(int r, int s)
625 SValue sv;
627 if (r != s) {
628 save_reg(r);
629 sv.type.t = VT_INT;
630 sv.r = s;
631 sv.c.ul = 0;
632 load(r, &sv);
636 /* get address of vtop (vtop MUST BE an lvalue) */
637 static void gaddrof(void)
639 if (vtop->r & VT_REF)
640 gv(RC_INT);
641 vtop->r &= ~VT_LVAL;
642 /* tricky: if saved lvalue, then we can go back to lvalue */
643 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
644 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
649 #ifdef CONFIG_TCC_BCHECK
650 /* generate lvalue bound code */
651 static void gbound(void)
653 int lval_type;
654 CType type1;
656 vtop->r &= ~VT_MUSTBOUND;
657 /* if lvalue, then use checking code before dereferencing */
658 if (vtop->r & VT_LVAL) {
659 /* if not VT_BOUNDED value, then make one */
660 if (!(vtop->r & VT_BOUNDED)) {
661 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
662 /* must save type because we must set it to int to get pointer */
663 type1 = vtop->type;
664 vtop->type.t = VT_INT;
665 gaddrof();
666 vpushi(0);
667 gen_bounded_ptr_add();
668 vtop->r |= lval_type;
669 vtop->type = type1;
671 /* then check for dereferencing */
672 gen_bounded_ptr_deref();
675 #endif
677 /* store vtop a register belonging to class 'rc'. lvalues are
678 converted to values. Cannot be used if cannot be converted to
679 register value (such as structures). */
680 ST_FUNC int gv(int rc)
682 int r, bit_pos, bit_size, size, align, i;
683 #ifndef TCC_TARGET_X86_64
684 int rc2;
685 #endif
687 /* NOTE: get_reg can modify vstack[] */
688 if (vtop->type.t & VT_BITFIELD) {
689 CType type;
690 int bits = 32;
691 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
692 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
693 /* remove bit field info to avoid loops */
694 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
695 /* cast to int to propagate signedness in following ops */
696 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
697 type.t = VT_LLONG;
698 bits = 64;
699 } else
700 type.t = VT_INT;
701 if((vtop->type.t & VT_UNSIGNED) ||
702 (vtop->type.t & VT_BTYPE) == VT_BOOL)
703 type.t |= VT_UNSIGNED;
704 gen_cast(&type);
705 /* generate shifts */
706 vpushi(bits - (bit_pos + bit_size));
707 gen_op(TOK_SHL);
708 vpushi(bits - bit_size);
709 /* NOTE: transformed to SHR if unsigned */
710 gen_op(TOK_SAR);
711 r = gv(rc);
712 } else {
713 if (is_float(vtop->type.t) &&
714 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
715 Sym *sym;
716 int *ptr;
717 unsigned long offset;
718 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
719 CValue check;
720 #endif
722 /* XXX: unify with initializers handling ? */
723 /* CPUs usually cannot use float constants, so we store them
724 generically in data segment */
725 size = type_size(&vtop->type, &align);
726 offset = (data_section->data_offset + align - 1) & -align;
727 data_section->data_offset = offset;
728 /* XXX: not portable yet */
729 #if defined(__i386__) || defined(__x86_64__)
730 /* Zero pad x87 tenbyte long doubles */
731 if (size == LDOUBLE_SIZE) {
732 vtop->c.tab[2] &= 0xffff;
733 #if LDOUBLE_SIZE == 16
734 vtop->c.tab[3] = 0;
735 #endif
737 #endif
738 ptr = section_ptr_add(data_section, size);
739 size = size >> 2;
740 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
741 check.d = 1;
742 if(check.tab[0])
743 for(i=0;i<size;i++)
744 ptr[i] = vtop->c.tab[size-1-i];
745 else
746 #endif
747 for(i=0;i<size;i++)
748 ptr[i] = vtop->c.tab[i];
749 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
750 vtop->r |= VT_LVAL | VT_SYM;
751 vtop->sym = sym;
752 vtop->c.ul = 0;
754 #ifdef CONFIG_TCC_BCHECK
755 if (vtop->r & VT_MUSTBOUND)
756 gbound();
757 #endif
759 r = vtop->r & VT_VALMASK;
760 #ifndef TCC_TARGET_X86_64
761 rc2 = RC_INT;
762 if (rc == RC_IRET)
763 rc2 = RC_LRET;
764 #endif
765 /* need to reload if:
766 - constant
767 - lvalue (need to dereference pointer)
768 - already a register, but not in the right class */
769 if (r >= VT_CONST
770 || (vtop->r & VT_LVAL)
771 || !(reg_classes[r] & rc)
772 #ifndef TCC_TARGET_X86_64
773 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
774 #endif
777 r = get_reg(rc);
778 #ifndef TCC_TARGET_X86_64
779 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
780 int r2;
781 unsigned long long ll;
782 /* two register type load : expand to two words
783 temporarily */
784 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
785 /* load constant */
786 ll = vtop->c.ull;
787 vtop->c.ui = ll; /* first word */
788 load(r, vtop);
789 vtop->r = r; /* save register value */
790 vpushi(ll >> 32); /* second word */
791 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
792 (vtop->r & VT_LVAL)) {
793 /* We do not want to modifier the long long
794 pointer here, so the safest (and less
795 efficient) is to save all the other registers
796 in the stack. XXX: totally inefficient. */
797 save_regs(1);
798 /* load from memory */
799 load(r, vtop);
800 vdup();
801 vtop[-1].r = r; /* save register value */
802 /* increment pointer to get second word */
803 vtop->type.t = VT_INT;
804 gaddrof();
805 vpushi(4);
806 gen_op('+');
807 vtop->r |= VT_LVAL;
808 } else {
809 /* move registers */
810 load(r, vtop);
811 vdup();
812 vtop[-1].r = r; /* save register value */
813 vtop->r = vtop[-1].r2;
815 /* allocate second register */
816 r2 = get_reg(rc2);
817 load(r2, vtop);
818 vpop();
819 /* write second register */
820 vtop->r2 = r2;
821 } else
822 #endif
823 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
824 int t1, t;
825 /* lvalue of scalar type : need to use lvalue type
826 because of possible cast */
827 t = vtop->type.t;
828 t1 = t;
829 /* compute memory access type */
830 if (vtop->r & VT_LVAL_BYTE)
831 t = VT_BYTE;
832 else if (vtop->r & VT_LVAL_SHORT)
833 t = VT_SHORT;
834 if (vtop->r & VT_LVAL_UNSIGNED)
835 t |= VT_UNSIGNED;
836 vtop->type.t = t;
837 load(r, vtop);
838 /* restore wanted type */
839 vtop->type.t = t1;
840 } else {
841 /* one register type load */
842 load(r, vtop);
845 vtop->r = r;
846 #ifdef TCC_TARGET_C67
847 /* uses register pairs for doubles */
848 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
849 vtop->r2 = r+1;
850 #endif
852 return r;
855 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
856 ST_FUNC void gv2(int rc1, int rc2)
858 int v;
860 /* generate more generic register first. But VT_JMP or VT_CMP
861 values must be generated first in all cases to avoid possible
862 reload errors */
863 v = vtop[0].r & VT_VALMASK;
864 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
865 vswap();
866 gv(rc1);
867 vswap();
868 gv(rc2);
869 /* test if reload is needed for first register */
870 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
871 vswap();
872 gv(rc1);
873 vswap();
875 } else {
876 gv(rc2);
877 vswap();
878 gv(rc1);
879 vswap();
880 /* test if reload is needed for first register */
881 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
882 gv(rc2);
887 /* wrapper around RC_FRET to return a register by type */
888 static int rc_fret(int t)
890 #ifdef TCC_TARGET_X86_64
891 if (t == VT_LDOUBLE) {
892 return RC_ST0;
894 #endif
895 return RC_FRET;
898 /* wrapper around REG_FRET to return a register by type */
899 static int reg_fret(int t)
901 #ifdef TCC_TARGET_X86_64
902 if (t == VT_LDOUBLE) {
903 return TREG_ST0;
905 #endif
906 return REG_FRET;
909 /* expand long long on stack in two int registers */
910 static void lexpand(void)
912 int u;
914 u = vtop->type.t & VT_UNSIGNED;
915 gv(RC_INT);
916 vdup();
917 vtop[0].r = vtop[-1].r2;
918 vtop[0].r2 = VT_CONST;
919 vtop[-1].r2 = VT_CONST;
920 vtop[0].type.t = VT_INT | u;
921 vtop[-1].type.t = VT_INT | u;
924 #ifdef TCC_TARGET_ARM
925 /* expand long long on stack */
926 ST_FUNC void lexpand_nr(void)
928 int u,v;
930 u = vtop->type.t & VT_UNSIGNED;
931 vdup();
932 vtop->r2 = VT_CONST;
933 vtop->type.t = VT_INT | u;
934 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
935 if (v == VT_CONST) {
936 vtop[-1].c.ui = vtop->c.ull;
937 vtop->c.ui = vtop->c.ull >> 32;
938 vtop->r = VT_CONST;
939 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
940 vtop->c.ui += 4;
941 vtop->r = vtop[-1].r;
942 } else if (v > VT_CONST) {
943 vtop--;
944 lexpand();
945 } else
946 vtop->r = vtop[-1].r2;
947 vtop[-1].r2 = VT_CONST;
948 vtop[-1].type.t = VT_INT | u;
950 #endif
952 /* build a long long from two ints */
953 static void lbuild(int t)
955 gv2(RC_INT, RC_INT);
956 vtop[-1].r2 = vtop[0].r;
957 vtop[-1].type.t = t;
958 vpop();
961 /* rotate n first stack elements to the bottom
962 I1 ... In -> I2 ... In I1 [top is right]
964 static void vrotb(int n)
966 int i;
967 SValue tmp;
969 tmp = vtop[-n + 1];
970 for(i=-n+1;i!=0;i++)
971 vtop[i] = vtop[i+1];
972 vtop[0] = tmp;
975 /* rotate n first stack elements to the top
976 I1 ... In -> In I1 ... I(n-1) [top is right]
978 ST_FUNC void vrott(int n)
980 int i;
981 SValue tmp;
983 tmp = vtop[0];
984 for(i = 0;i < n - 1; i++)
985 vtop[-i] = vtop[-i - 1];
986 vtop[-n + 1] = tmp;
989 /* pop stack value */
990 ST_FUNC void vpop(void)
992 int v;
993 v = vtop->r & VT_VALMASK;
994 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
995 /* for x86, we need to pop the FP stack */
996 if (v == TREG_ST0 && !nocode_wanted) {
997 o(0xd8dd); /* fstp %st(0) */
998 } else
999 #endif
1000 if (v == VT_JMP || v == VT_JMPI) {
1001 /* need to put correct jump if && or || without test */
1002 gsym(vtop->c.ul);
1004 vtop--;
1007 /* convert stack entry to register and duplicate its value in another
1008 register */
1009 static void gv_dup(void)
1011 int rc, t, r, r1;
1012 SValue sv;
1014 t = vtop->type.t;
1015 if ((t & VT_BTYPE) == VT_LLONG) {
1016 lexpand();
1017 gv_dup();
1018 vswap();
1019 vrotb(3);
1020 gv_dup();
1021 vrotb(4);
1022 /* stack: H L L1 H1 */
1023 lbuild(t);
1024 vrotb(3);
1025 vrotb(3);
1026 vswap();
1027 lbuild(t);
1028 vswap();
1029 } else {
1030 /* duplicate value */
1031 rc = RC_INT;
1032 sv.type.t = VT_INT;
1033 if (is_float(t)) {
1034 rc = RC_FLOAT;
1035 #ifdef TCC_TARGET_X86_64
1036 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1037 rc = RC_ST0;
1039 #endif
1040 sv.type.t = t;
1042 r = gv(rc);
1043 r1 = get_reg(rc);
1044 sv.r = r;
1045 sv.c.ul = 0;
1046 load(r1, &sv); /* move r to r1 */
1047 vdup();
1048 /* duplicates value */
1049 if (r != r1)
1050 vtop->r = r1;
1054 #ifndef TCC_TARGET_X86_64
1055 /* generate CPU independent (unsigned) long long operations */
1056 static void gen_opl(int op)
1058 int t, a, b, op1, c, i;
1059 int func;
1060 unsigned short reg_iret = REG_IRET;
1061 unsigned short reg_lret = REG_LRET;
1062 SValue tmp;
1064 switch(op) {
1065 case '/':
1066 case TOK_PDIV:
1067 func = TOK___divdi3;
1068 goto gen_func;
1069 case TOK_UDIV:
1070 func = TOK___udivdi3;
1071 goto gen_func;
1072 case '%':
1073 func = TOK___moddi3;
1074 goto gen_mod_func;
1075 case TOK_UMOD:
1076 func = TOK___umoddi3;
1077 gen_mod_func:
1078 #ifdef TCC_ARM_EABI
1079 reg_iret = TREG_R2;
1080 reg_lret = TREG_R3;
1081 #endif
1082 gen_func:
1083 /* call generic long long function */
1084 vpush_global_sym(&func_old_type, func);
1085 vrott(3);
1086 gfunc_call(2);
1087 vpushi(0);
1088 vtop->r = reg_iret;
1089 vtop->r2 = reg_lret;
1090 break;
1091 case '^':
1092 case '&':
1093 case '|':
1094 case '*':
1095 case '+':
1096 case '-':
1097 t = vtop->type.t;
1098 vswap();
1099 lexpand();
1100 vrotb(3);
1101 lexpand();
1102 /* stack: L1 H1 L2 H2 */
1103 tmp = vtop[0];
1104 vtop[0] = vtop[-3];
1105 vtop[-3] = tmp;
1106 tmp = vtop[-2];
1107 vtop[-2] = vtop[-3];
1108 vtop[-3] = tmp;
1109 vswap();
1110 /* stack: H1 H2 L1 L2 */
1111 if (op == '*') {
1112 vpushv(vtop - 1);
1113 vpushv(vtop - 1);
1114 gen_op(TOK_UMULL);
1115 lexpand();
1116 /* stack: H1 H2 L1 L2 ML MH */
1117 for(i=0;i<4;i++)
1118 vrotb(6);
1119 /* stack: ML MH H1 H2 L1 L2 */
1120 tmp = vtop[0];
1121 vtop[0] = vtop[-2];
1122 vtop[-2] = tmp;
1123 /* stack: ML MH H1 L2 H2 L1 */
1124 gen_op('*');
1125 vrotb(3);
1126 vrotb(3);
1127 gen_op('*');
1128 /* stack: ML MH M1 M2 */
1129 gen_op('+');
1130 gen_op('+');
1131 } else if (op == '+' || op == '-') {
1132 /* XXX: add non carry method too (for MIPS or alpha) */
1133 if (op == '+')
1134 op1 = TOK_ADDC1;
1135 else
1136 op1 = TOK_SUBC1;
1137 gen_op(op1);
1138 /* stack: H1 H2 (L1 op L2) */
1139 vrotb(3);
1140 vrotb(3);
1141 gen_op(op1 + 1); /* TOK_xxxC2 */
1142 } else {
1143 gen_op(op);
1144 /* stack: H1 H2 (L1 op L2) */
1145 vrotb(3);
1146 vrotb(3);
1147 /* stack: (L1 op L2) H1 H2 */
1148 gen_op(op);
1149 /* stack: (L1 op L2) (H1 op H2) */
1151 /* stack: L H */
1152 lbuild(t);
1153 break;
1154 case TOK_SAR:
1155 case TOK_SHR:
1156 case TOK_SHL:
1157 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1158 t = vtop[-1].type.t;
1159 vswap();
1160 lexpand();
1161 vrotb(3);
1162 /* stack: L H shift */
1163 c = (int)vtop->c.i;
1164 /* constant: simpler */
1165 /* NOTE: all comments are for SHL. the other cases are
1166 done by swaping words */
1167 vpop();
1168 if (op != TOK_SHL)
1169 vswap();
1170 if (c >= 32) {
1171 /* stack: L H */
1172 vpop();
1173 if (c > 32) {
1174 vpushi(c - 32);
1175 gen_op(op);
1177 if (op != TOK_SAR) {
1178 vpushi(0);
1179 } else {
1180 gv_dup();
1181 vpushi(31);
1182 gen_op(TOK_SAR);
1184 vswap();
1185 } else {
1186 vswap();
1187 gv_dup();
1188 /* stack: H L L */
1189 vpushi(c);
1190 gen_op(op);
1191 vswap();
1192 vpushi(32 - c);
1193 if (op == TOK_SHL)
1194 gen_op(TOK_SHR);
1195 else
1196 gen_op(TOK_SHL);
1197 vrotb(3);
1198 /* stack: L L H */
1199 vpushi(c);
1200 if (op == TOK_SHL)
1201 gen_op(TOK_SHL);
1202 else
1203 gen_op(TOK_SHR);
1204 gen_op('|');
1206 if (op != TOK_SHL)
1207 vswap();
1208 lbuild(t);
1209 } else {
1210 /* XXX: should provide a faster fallback on x86 ? */
1211 switch(op) {
1212 case TOK_SAR:
1213 func = TOK___ashrdi3;
1214 goto gen_func;
1215 case TOK_SHR:
1216 func = TOK___lshrdi3;
1217 goto gen_func;
1218 case TOK_SHL:
1219 func = TOK___ashldi3;
1220 goto gen_func;
1223 break;
1224 default:
1225 /* compare operations */
1226 t = vtop->type.t;
1227 vswap();
1228 lexpand();
1229 vrotb(3);
1230 lexpand();
1231 /* stack: L1 H1 L2 H2 */
1232 tmp = vtop[-1];
1233 vtop[-1] = vtop[-2];
1234 vtop[-2] = tmp;
1235 /* stack: L1 L2 H1 H2 */
1236 /* compare high */
1237 op1 = op;
1238 /* when values are equal, we need to compare low words. since
1239 the jump is inverted, we invert the test too. */
1240 if (op1 == TOK_LT)
1241 op1 = TOK_LE;
1242 else if (op1 == TOK_GT)
1243 op1 = TOK_GE;
1244 else if (op1 == TOK_ULT)
1245 op1 = TOK_ULE;
1246 else if (op1 == TOK_UGT)
1247 op1 = TOK_UGE;
1248 a = 0;
1249 b = 0;
1250 gen_op(op1);
1251 if (op1 != TOK_NE) {
1252 a = gtst(1, 0);
1254 if (op != TOK_EQ) {
1255 /* generate non equal test */
1256 /* XXX: NOT PORTABLE yet */
1257 if (a == 0) {
1258 b = gtst(0, 0);
1259 } else {
1260 #if defined(TCC_TARGET_I386)
1261 b = psym(0x850f, 0);
1262 #elif defined(TCC_TARGET_ARM)
1263 b = ind;
1264 o(0x1A000000 | encbranch(ind, 0, 1));
1265 #elif defined(TCC_TARGET_C67)
1266 tcc_error("not implemented");
1267 #else
1268 #error not supported
1269 #endif
1272 /* compare low. Always unsigned */
1273 op1 = op;
1274 if (op1 == TOK_LT)
1275 op1 = TOK_ULT;
1276 else if (op1 == TOK_LE)
1277 op1 = TOK_ULE;
1278 else if (op1 == TOK_GT)
1279 op1 = TOK_UGT;
1280 else if (op1 == TOK_GE)
1281 op1 = TOK_UGE;
1282 gen_op(op1);
1283 a = gtst(1, a);
1284 gsym(b);
1285 vseti(VT_JMPI, a);
1286 break;
1289 #endif
1291 /* handle integer constant optimizations and various machine
1292 independent opt */
1293 static void gen_opic(int op)
1295 int c1, c2, t1, t2, n;
1296 SValue *v1, *v2;
1297 long long l1, l2;
1298 typedef unsigned long long U;
1300 v1 = vtop - 1;
1301 v2 = vtop;
1302 t1 = v1->type.t & VT_BTYPE;
1303 t2 = v2->type.t & VT_BTYPE;
1305 if (t1 == VT_LLONG)
1306 l1 = v1->c.ll;
1307 else if (v1->type.t & VT_UNSIGNED)
1308 l1 = v1->c.ui;
1309 else
1310 l1 = v1->c.i;
1312 if (t2 == VT_LLONG)
1313 l2 = v2->c.ll;
1314 else if (v2->type.t & VT_UNSIGNED)
1315 l2 = v2->c.ui;
1316 else
1317 l2 = v2->c.i;
1319 /* currently, we cannot do computations with forward symbols */
1320 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1321 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1322 if (c1 && c2) {
1323 switch(op) {
1324 case '+': l1 += l2; break;
1325 case '-': l1 -= l2; break;
1326 case '&': l1 &= l2; break;
1327 case '^': l1 ^= l2; break;
1328 case '|': l1 |= l2; break;
1329 case '*': l1 *= l2; break;
1331 case TOK_PDIV:
1332 case '/':
1333 case '%':
1334 case TOK_UDIV:
1335 case TOK_UMOD:
1336 /* if division by zero, generate explicit division */
1337 if (l2 == 0) {
1338 if (const_wanted)
1339 tcc_error("division by zero in constant");
1340 goto general_case;
1342 switch(op) {
1343 default: l1 /= l2; break;
1344 case '%': l1 %= l2; break;
1345 case TOK_UDIV: l1 = (U)l1 / l2; break;
1346 case TOK_UMOD: l1 = (U)l1 % l2; break;
1348 break;
1349 case TOK_SHL: l1 <<= l2; break;
1350 case TOK_SHR: l1 = (U)l1 >> l2; break;
1351 case TOK_SAR: l1 >>= l2; break;
1352 /* tests */
1353 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1354 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1355 case TOK_EQ: l1 = l1 == l2; break;
1356 case TOK_NE: l1 = l1 != l2; break;
1357 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1358 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1359 case TOK_LT: l1 = l1 < l2; break;
1360 case TOK_GE: l1 = l1 >= l2; break;
1361 case TOK_LE: l1 = l1 <= l2; break;
1362 case TOK_GT: l1 = l1 > l2; break;
1363 /* logical */
1364 case TOK_LAND: l1 = l1 && l2; break;
1365 case TOK_LOR: l1 = l1 || l2; break;
1366 default:
1367 goto general_case;
1369 v1->c.ll = l1;
1370 vtop--;
1371 } else {
1372 /* if commutative ops, put c2 as constant */
1373 if (c1 && (op == '+' || op == '&' || op == '^' ||
1374 op == '|' || op == '*')) {
1375 vswap();
1376 c2 = c1; //c = c1, c1 = c2, c2 = c;
1377 l2 = l1; //l = l1, l1 = l2, l2 = l;
1379 /* Filter out NOP operations like x*1, x-0, x&-1... */
1380 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1381 op == TOK_PDIV) &&
1382 l2 == 1) ||
1383 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1384 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1385 l2 == 0) ||
1386 (op == '&' &&
1387 l2 == -1))) {
1388 /* nothing to do */
1389 vtop--;
1390 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1391 /* try to use shifts instead of muls or divs */
1392 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1393 n = -1;
1394 while (l2) {
1395 l2 >>= 1;
1396 n++;
1398 vtop->c.ll = n;
1399 if (op == '*')
1400 op = TOK_SHL;
1401 else if (op == TOK_PDIV)
1402 op = TOK_SAR;
1403 else
1404 op = TOK_SHR;
1406 goto general_case;
1407 } else if (c2 && (op == '+' || op == '-') &&
1408 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1409 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1410 /* symbol + constant case */
1411 if (op == '-')
1412 l2 = -l2;
1413 vtop--;
1414 vtop->c.ll += l2;
1415 } else {
1416 general_case:
1417 if (!nocode_wanted) {
1418 /* call low level op generator */
1419 if (t1 == VT_LLONG || t2 == VT_LLONG)
1420 gen_opl(op);
1421 else
1422 gen_opi(op);
1423 } else {
1424 vtop--;
1430 /* generate a floating point operation with constant propagation */
1431 static void gen_opif(int op)
1433 int c1, c2;
1434 SValue *v1, *v2;
1435 long double f1, f2;
1437 v1 = vtop - 1;
1438 v2 = vtop;
1439 /* currently, we cannot do computations with forward symbols */
1440 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1441 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1442 if (c1 && c2) {
1443 if (v1->type.t == VT_FLOAT) {
1444 f1 = v1->c.f;
1445 f2 = v2->c.f;
1446 } else if (v1->type.t == VT_DOUBLE) {
1447 f1 = v1->c.d;
1448 f2 = v2->c.d;
1449 } else {
1450 f1 = v1->c.ld;
1451 f2 = v2->c.ld;
1454 /* NOTE: we only do constant propagation if finite number (not
1455 NaN or infinity) (ANSI spec) */
1456 if (!ieee_finite(f1) || !ieee_finite(f2))
1457 goto general_case;
1459 switch(op) {
1460 case '+': f1 += f2; break;
1461 case '-': f1 -= f2; break;
1462 case '*': f1 *= f2; break;
1463 case '/':
1464 if (f2 == 0.0) {
1465 if (const_wanted)
1466 tcc_error("division by zero in constant");
1467 goto general_case;
1469 f1 /= f2;
1470 break;
1471 /* XXX: also handles tests ? */
1472 default:
1473 goto general_case;
1475 /* XXX: overflow test ? */
1476 if (v1->type.t == VT_FLOAT) {
1477 v1->c.f = f1;
1478 } else if (v1->type.t == VT_DOUBLE) {
1479 v1->c.d = f1;
1480 } else {
1481 v1->c.ld = f1;
1483 vtop--;
1484 } else {
1485 general_case:
1486 if (!nocode_wanted) {
1487 gen_opf(op);
1488 } else {
1489 vtop--;
1494 static int pointed_size(CType *type)
1496 int align;
1497 return type_size(pointed_type(type), &align);
1500 static void vla_runtime_pointed_size(CType *type)
1502 int align;
1503 vla_runtime_type_size(pointed_type(type), &align);
1506 static inline int is_null_pointer(SValue *p)
1508 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1509 return 0;
1510 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1511 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1512 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0);
1515 static inline int is_integer_btype(int bt)
1517 return (bt == VT_BYTE || bt == VT_SHORT ||
1518 bt == VT_INT || bt == VT_LLONG);
1521 /* check types for comparison or substraction of pointers */
1522 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1524 CType *type1, *type2, tmp_type1, tmp_type2;
1525 int bt1, bt2;
1527 /* null pointers are accepted for all comparisons as gcc */
1528 if (is_null_pointer(p1) || is_null_pointer(p2))
1529 return;
1530 type1 = &p1->type;
1531 type2 = &p2->type;
1532 bt1 = type1->t & VT_BTYPE;
1533 bt2 = type2->t & VT_BTYPE;
1534 /* accept comparison between pointer and integer with a warning */
1535 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1536 if (op != TOK_LOR && op != TOK_LAND )
1537 tcc_warning("comparison between pointer and integer");
1538 return;
1541 /* both must be pointers or implicit function pointers */
1542 if (bt1 == VT_PTR) {
1543 type1 = pointed_type(type1);
1544 } else if (bt1 != VT_FUNC)
1545 goto invalid_operands;
1547 if (bt2 == VT_PTR) {
1548 type2 = pointed_type(type2);
1549 } else if (bt2 != VT_FUNC) {
1550 invalid_operands:
1551 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1553 if ((type1->t & VT_BTYPE) == VT_VOID ||
1554 (type2->t & VT_BTYPE) == VT_VOID)
1555 return;
1556 tmp_type1 = *type1;
1557 tmp_type2 = *type2;
1558 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1559 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1560 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1561 /* gcc-like error if '-' is used */
1562 if (op == '-')
1563 goto invalid_operands;
1564 else
1565 tcc_warning("comparison of distinct pointer types lacks a cast");
1569 /* generic gen_op: handles types problems */
1570 ST_FUNC void gen_op(int op)
1572 int u, t1, t2, bt1, bt2, t;
1573 CType type1;
1575 t1 = vtop[-1].type.t;
1576 t2 = vtop[0].type.t;
1577 bt1 = t1 & VT_BTYPE;
1578 bt2 = t2 & VT_BTYPE;
1580 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1581 /* at least one operand is a pointer */
1582 /* relationnal op: must be both pointers */
1583 if (op >= TOK_ULT && op <= TOK_LOR) {
1584 check_comparison_pointer_types(vtop - 1, vtop, op);
1585 /* pointers are handled are unsigned */
1586 #ifdef TCC_TARGET_X86_64
1587 t = VT_LLONG | VT_UNSIGNED;
1588 #else
1589 t = VT_INT | VT_UNSIGNED;
1590 #endif
1591 goto std_op;
1593 /* if both pointers, then it must be the '-' op */
1594 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1595 if (op != '-')
1596 tcc_error("cannot use pointers here");
1597 check_comparison_pointer_types(vtop - 1, vtop, op);
1598 /* XXX: check that types are compatible */
1599 if (vtop[-1].type.t & VT_VLA) {
1600 vla_runtime_pointed_size(&vtop[-1].type);
1601 } else {
1602 vpushi(pointed_size(&vtop[-1].type));
1604 vrott(3);
1605 gen_opic(op);
1606 /* set to integer type */
1607 #ifdef TCC_TARGET_X86_64
1608 vtop->type.t = VT_LLONG;
1609 #else
1610 vtop->type.t = VT_INT;
1611 #endif
1612 vswap();
1613 gen_op(TOK_PDIV);
1614 } else {
1615 /* exactly one pointer : must be '+' or '-'. */
1616 if (op != '-' && op != '+')
1617 tcc_error("cannot use pointers here");
1618 /* Put pointer as first operand */
1619 if (bt2 == VT_PTR) {
1620 vswap();
1621 swap(&t1, &t2);
1623 type1 = vtop[-1].type;
1624 type1.t &= ~VT_ARRAY;
1625 if (vtop[-1].type.t & VT_VLA)
1626 vla_runtime_pointed_size(&vtop[-1].type);
1627 else {
1628 u = pointed_size(&vtop[-1].type);
1629 if (u < 0)
1630 tcc_error("unknown array element size");
1631 #ifdef TCC_TARGET_X86_64
1632 vpushll(u);
1633 #else
1634 /* XXX: cast to int ? (long long case) */
1635 vpushi(u);
1636 #endif
1638 gen_op('*');
1639 #ifdef CONFIG_TCC_BCHECK
1640 /* if evaluating constant expression, no code should be
1641 generated, so no bound check */
1642 if (tcc_state->do_bounds_check && !const_wanted) {
1643 /* if bounded pointers, we generate a special code to
1644 test bounds */
1645 if (op == '-') {
1646 vpushi(0);
1647 vswap();
1648 gen_op('-');
1650 gen_bounded_ptr_add();
1651 } else
1652 #endif
1654 gen_opic(op);
1656 /* put again type if gen_opic() swaped operands */
1657 vtop->type = type1;
1659 } else if (is_float(bt1) || is_float(bt2)) {
1660 /* compute bigger type and do implicit casts */
1661 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1662 t = VT_LDOUBLE;
1663 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1664 t = VT_DOUBLE;
1665 } else {
1666 t = VT_FLOAT;
1668 /* floats can only be used for a few operations */
1669 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1670 (op < TOK_ULT || op > TOK_GT))
1671 tcc_error("invalid operands for binary operation");
1672 goto std_op;
1673 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1674 /* cast to biggest op */
1675 t = VT_LLONG;
1676 /* convert to unsigned if it does not fit in a long long */
1677 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1678 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1679 t |= VT_UNSIGNED;
1680 goto std_op;
1681 } else {
1682 /* integer operations */
1683 t = VT_INT;
1684 /* convert to unsigned if it does not fit in an integer */
1685 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1686 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1687 t |= VT_UNSIGNED;
1688 std_op:
1689 /* XXX: currently, some unsigned operations are explicit, so
1690 we modify them here */
1691 if (t & VT_UNSIGNED) {
1692 if (op == TOK_SAR)
1693 op = TOK_SHR;
1694 else if (op == '/')
1695 op = TOK_UDIV;
1696 else if (op == '%')
1697 op = TOK_UMOD;
1698 else if (op == TOK_LT)
1699 op = TOK_ULT;
1700 else if (op == TOK_GT)
1701 op = TOK_UGT;
1702 else if (op == TOK_LE)
1703 op = TOK_ULE;
1704 else if (op == TOK_GE)
1705 op = TOK_UGE;
1707 vswap();
1708 type1.t = t;
1709 gen_cast(&type1);
1710 vswap();
1711 /* special case for shifts and long long: we keep the shift as
1712 an integer */
1713 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1714 type1.t = VT_INT;
1715 gen_cast(&type1);
1716 if (is_float(t))
1717 gen_opif(op);
1718 else
1719 gen_opic(op);
1720 if (op >= TOK_ULT && op <= TOK_GT) {
1721 /* relationnal op: the result is an int */
1722 vtop->type.t = VT_INT;
1723 } else {
1724 vtop->type.t = t;
1729 #ifndef TCC_TARGET_ARM
1730 /* generic itof for unsigned long long case */
1731 static void gen_cvt_itof1(int t)
1733 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1734 (VT_LLONG | VT_UNSIGNED)) {
1736 if (t == VT_FLOAT)
1737 vpush_global_sym(&func_old_type, TOK___floatundisf);
1738 #if LDOUBLE_SIZE != 8
1739 else if (t == VT_LDOUBLE)
1740 vpush_global_sym(&func_old_type, TOK___floatundixf);
1741 #endif
1742 else
1743 vpush_global_sym(&func_old_type, TOK___floatundidf);
1744 vrott(2);
1745 gfunc_call(1);
1746 vpushi(0);
1747 vtop->r = reg_fret(t);
1748 } else {
1749 gen_cvt_itof(t);
1752 #endif
1754 /* generic ftoi for unsigned long long case */
1755 static void gen_cvt_ftoi1(int t)
1757 int st;
1759 if (t == (VT_LLONG | VT_UNSIGNED)) {
1760 /* not handled natively */
1761 st = vtop->type.t & VT_BTYPE;
1762 if (st == VT_FLOAT)
1763 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1764 #if LDOUBLE_SIZE != 8
1765 else if (st == VT_LDOUBLE)
1766 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1767 #endif
1768 else
1769 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1770 vrott(2);
1771 gfunc_call(1);
1772 vpushi(0);
1773 vtop->r = REG_IRET;
1774 vtop->r2 = REG_LRET;
1775 } else {
1776 gen_cvt_ftoi(t);
1780 /* force char or short cast */
1781 static void force_charshort_cast(int t)
1783 int bits, dbt;
1784 dbt = t & VT_BTYPE;
1785 /* XXX: add optimization if lvalue : just change type and offset */
1786 if (dbt == VT_BYTE)
1787 bits = 8;
1788 else
1789 bits = 16;
1790 if (t & VT_UNSIGNED) {
1791 vpushi((1 << bits) - 1);
1792 gen_op('&');
1793 } else {
1794 bits = 32 - bits;
1795 vpushi(bits);
1796 gen_op(TOK_SHL);
1797 /* result must be signed or the SAR is converted to an SHL
1798 This was not the case when "t" was a signed short
1799 and the last value on the stack was an unsigned int */
1800 vtop->type.t &= ~VT_UNSIGNED;
1801 vpushi(bits);
1802 gen_op(TOK_SAR);
1806 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1807 static void gen_cast(CType *type)
1809 int sbt, dbt, sf, df, c, p;
1811 /* special delayed cast for char/short */
1812 /* XXX: in some cases (multiple cascaded casts), it may still
1813 be incorrect */
1814 if (vtop->r & VT_MUSTCAST) {
1815 vtop->r &= ~VT_MUSTCAST;
1816 force_charshort_cast(vtop->type.t);
1819 /* bitfields first get cast to ints */
1820 if (vtop->type.t & VT_BITFIELD) {
1821 gv(RC_INT);
1824 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1825 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1827 if (sbt != dbt) {
1828 sf = is_float(sbt);
1829 df = is_float(dbt);
1830 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1831 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1832 if (c) {
1833 /* constant case: we can do it now */
1834 /* XXX: in ISOC, cannot do it if error in convert */
1835 if (sbt == VT_FLOAT)
1836 vtop->c.ld = vtop->c.f;
1837 else if (sbt == VT_DOUBLE)
1838 vtop->c.ld = vtop->c.d;
1840 if (df) {
1841 if ((sbt & VT_BTYPE) == VT_LLONG) {
1842 if (sbt & VT_UNSIGNED)
1843 vtop->c.ld = vtop->c.ull;
1844 else
1845 vtop->c.ld = vtop->c.ll;
1846 } else if(!sf) {
1847 if (sbt & VT_UNSIGNED)
1848 vtop->c.ld = vtop->c.ui;
1849 else
1850 vtop->c.ld = vtop->c.i;
1853 if (dbt == VT_FLOAT)
1854 vtop->c.f = (float)vtop->c.ld;
1855 else if (dbt == VT_DOUBLE)
1856 vtop->c.d = (double)vtop->c.ld;
1857 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1858 vtop->c.ull = (unsigned long long)vtop->c.ld;
1859 } else if (sf && dbt == VT_BOOL) {
1860 vtop->c.i = (vtop->c.ld != 0);
1861 } else {
1862 if(sf)
1863 vtop->c.ll = (long long)vtop->c.ld;
1864 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1865 vtop->c.ll = vtop->c.ull;
1866 else if (sbt & VT_UNSIGNED)
1867 vtop->c.ll = vtop->c.ui;
1868 #ifdef TCC_TARGET_X86_64
1869 else if (sbt == VT_PTR)
1871 #endif
1872 else if (sbt != VT_LLONG)
1873 vtop->c.ll = vtop->c.i;
1875 if (dbt == (VT_LLONG|VT_UNSIGNED))
1876 vtop->c.ull = vtop->c.ll;
1877 else if (dbt == VT_BOOL)
1878 vtop->c.i = (vtop->c.ll != 0);
1879 else if (dbt != VT_LLONG) {
1880 int s = 0;
1881 if ((dbt & VT_BTYPE) == VT_BYTE)
1882 s = 24;
1883 else if ((dbt & VT_BTYPE) == VT_SHORT)
1884 s = 16;
1886 if(dbt & VT_UNSIGNED)
1887 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1888 else
1889 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1892 } else if (p && dbt == VT_BOOL) {
1893 vtop->r = VT_CONST;
1894 vtop->c.i = 1;
1895 } else if (!nocode_wanted) {
1896 /* non constant case: generate code */
1897 if (sf && df) {
1898 /* convert from fp to fp */
1899 gen_cvt_ftof(dbt);
1900 } else if (df) {
1901 /* convert int to fp */
1902 gen_cvt_itof1(dbt);
1903 } else if (sf) {
1904 /* convert fp to int */
1905 if (dbt == VT_BOOL) {
1906 vpushi(0);
1907 gen_op(TOK_NE);
1908 } else {
1909 /* we handle char/short/etc... with generic code */
1910 if (dbt != (VT_INT | VT_UNSIGNED) &&
1911 dbt != (VT_LLONG | VT_UNSIGNED) &&
1912 dbt != VT_LLONG)
1913 dbt = VT_INT;
1914 gen_cvt_ftoi1(dbt);
1915 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1916 /* additional cast for char/short... */
1917 vtop->type.t = dbt;
1918 gen_cast(type);
1921 #ifndef TCC_TARGET_X86_64
1922 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1923 if ((sbt & VT_BTYPE) != VT_LLONG) {
1924 /* scalar to long long */
1925 /* machine independent conversion */
1926 gv(RC_INT);
1927 /* generate high word */
1928 if (sbt == (VT_INT | VT_UNSIGNED)) {
1929 vpushi(0);
1930 gv(RC_INT);
1931 } else {
1932 if (sbt == VT_PTR) {
1933 /* cast from pointer to int before we apply
1934 shift operation, which pointers don't support*/
1935 gen_cast(&int_type);
1937 gv_dup();
1938 vpushi(31);
1939 gen_op(TOK_SAR);
1941 /* patch second register */
1942 vtop[-1].r2 = vtop->r;
1943 vpop();
1945 #else
1946 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1947 (dbt & VT_BTYPE) == VT_PTR ||
1948 (dbt & VT_BTYPE) == VT_FUNC) {
1949 if ((sbt & VT_BTYPE) != VT_LLONG &&
1950 (sbt & VT_BTYPE) != VT_PTR &&
1951 (sbt & VT_BTYPE) != VT_FUNC) {
1952 /* need to convert from 32bit to 64bit */
1953 int r = gv(RC_INT);
1954 if (sbt != (VT_INT | VT_UNSIGNED)) {
1955 /* x86_64 specific: movslq */
1956 o(0x6348);
1957 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1960 #endif
1961 } else if (dbt == VT_BOOL) {
1962 /* scalar to bool */
1963 vpushi(0);
1964 gen_op(TOK_NE);
1965 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1966 (dbt & VT_BTYPE) == VT_SHORT) {
1967 if (sbt == VT_PTR) {
1968 vtop->type.t = VT_INT;
1969 tcc_warning("nonportable conversion from pointer to char/short");
1971 force_charshort_cast(dbt);
1972 } else if ((dbt & VT_BTYPE) == VT_INT) {
1973 /* scalar to int */
1974 if (sbt == VT_LLONG) {
1975 /* from long long: just take low order word */
1976 lexpand();
1977 vpop();
1979 /* if lvalue and single word type, nothing to do because
1980 the lvalue already contains the real type size (see
1981 VT_LVAL_xxx constants) */
1984 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1985 /* if we are casting between pointer types,
1986 we must update the VT_LVAL_xxx size */
1987 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1988 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1990 vtop->type = *type;
1993 /* return type size as known at compile time. Put alignment at 'a' */
1994 ST_FUNC int type_size(CType *type, int *a)
1996 Sym *s;
1997 int bt;
1999 bt = type->t & VT_BTYPE;
2000 if (bt == VT_STRUCT) {
2001 /* struct/union */
2002 s = type->ref;
2003 *a = s->r;
2004 return s->c;
2005 } else if (bt == VT_PTR) {
2006 if (type->t & VT_ARRAY) {
2007 int ts;
2009 s = type->ref;
2010 ts = type_size(&s->type, a);
2012 if (ts < 0 && s->c < 0)
2013 ts = -ts;
2015 return ts * s->c;
2016 } else {
2017 *a = PTR_SIZE;
2018 return PTR_SIZE;
2020 } else if (bt == VT_LDOUBLE) {
2021 *a = LDOUBLE_ALIGN;
2022 return LDOUBLE_SIZE;
2023 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2024 #ifdef TCC_TARGET_I386
2025 #ifdef TCC_TARGET_PE
2026 *a = 8;
2027 #else
2028 *a = 4;
2029 #endif
2030 #elif defined(TCC_TARGET_ARM)
2031 #ifdef TCC_ARM_EABI
2032 *a = 8;
2033 #else
2034 *a = 4;
2035 #endif
2036 #else
2037 *a = 8;
2038 #endif
2039 return 8;
2040 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2041 *a = 4;
2042 return 4;
2043 } else if (bt == VT_SHORT) {
2044 *a = 2;
2045 return 2;
2046 } else {
2047 /* char, void, function, _Bool */
2048 *a = 1;
2049 return 1;
2053 /* push type size as known at runtime time on top of value stack. Put
2054 alignment at 'a' */
2055 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2057 if (type->t & VT_VLA) {
2058 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2059 } else {
2060 vpushi(type_size(type, a));
2064 /* return the pointed type of t */
2065 static inline CType *pointed_type(CType *type)
2067 return &type->ref->type;
2070 /* modify type so that its it is a pointer to type. */
2071 ST_FUNC void mk_pointer(CType *type)
2073 Sym *s;
2074 s = sym_push(SYM_FIELD, type, 0, -1);
2075 type->t = VT_PTR | (type->t & ~VT_TYPE);
2076 type->ref = s;
2079 /* compare function types. OLD functions match any new functions */
2080 static int is_compatible_func(CType *type1, CType *type2)
2082 Sym *s1, *s2;
2084 s1 = type1->ref;
2085 s2 = type2->ref;
2086 if (!is_compatible_types(&s1->type, &s2->type))
2087 return 0;
2088 /* check func_call */
2089 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2090 return 0;
2091 /* XXX: not complete */
2092 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2093 return 1;
2094 if (s1->c != s2->c)
2095 return 0;
2096 while (s1 != NULL) {
2097 if (s2 == NULL)
2098 return 0;
2099 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2100 return 0;
2101 s1 = s1->next;
2102 s2 = s2->next;
2104 if (s2)
2105 return 0;
2106 return 1;
2109 /* return true if type1 and type2 are the same. If unqualified is
2110 true, qualifiers on the types are ignored.
2112 - enums are not checked as gcc __builtin_types_compatible_p ()
2114 static int compare_types(CType *type1, CType *type2, int unqualified)
2116 int bt1, t1, t2;
2118 t1 = type1->t & VT_TYPE;
2119 t2 = type2->t & VT_TYPE;
2120 if (unqualified) {
2121 /* strip qualifiers before comparing */
2122 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2123 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2125 /* XXX: bitfields ? */
2126 if (t1 != t2)
2127 return 0;
2128 /* test more complicated cases */
2129 bt1 = t1 & VT_BTYPE;
2130 if (bt1 == VT_PTR) {
2131 type1 = pointed_type(type1);
2132 type2 = pointed_type(type2);
2133 return is_compatible_types(type1, type2);
2134 } else if (bt1 == VT_STRUCT) {
2135 return (type1->ref == type2->ref);
2136 } else if (bt1 == VT_FUNC) {
2137 return is_compatible_func(type1, type2);
2138 } else {
2139 return 1;
2143 /* return true if type1 and type2 are exactly the same (including
2144 qualifiers).
2146 static int is_compatible_types(CType *type1, CType *type2)
2148 return compare_types(type1,type2,0);
2151 /* return true if type1 and type2 are the same (ignoring qualifiers).
2153 static int is_compatible_parameter_types(CType *type1, CType *type2)
2155 return compare_types(type1,type2,1);
2158 /* print a type. If 'varstr' is not NULL, then the variable is also
2159 printed in the type */
2160 /* XXX: union */
2161 /* XXX: add array and function pointers */
2162 static void type_to_str(char *buf, int buf_size,
2163 CType *type, const char *varstr)
2165 int bt, v, t;
2166 Sym *s, *sa;
2167 char buf1[256];
2168 const char *tstr;
2170 t = type->t & VT_TYPE;
2171 bt = t & VT_BTYPE;
2172 buf[0] = '\0';
2173 if (t & VT_CONSTANT)
2174 pstrcat(buf, buf_size, "const ");
2175 if (t & VT_VOLATILE)
2176 pstrcat(buf, buf_size, "volatile ");
2177 if (t & VT_UNSIGNED)
2178 pstrcat(buf, buf_size, "unsigned ");
2179 switch(bt) {
2180 case VT_VOID:
2181 tstr = "void";
2182 goto add_tstr;
2183 case VT_BOOL:
2184 tstr = "_Bool";
2185 goto add_tstr;
2186 case VT_BYTE:
2187 tstr = "char";
2188 goto add_tstr;
2189 case VT_SHORT:
2190 tstr = "short";
2191 goto add_tstr;
2192 case VT_INT:
2193 tstr = "int";
2194 goto add_tstr;
2195 case VT_LONG:
2196 tstr = "long";
2197 goto add_tstr;
2198 case VT_LLONG:
2199 tstr = "long long";
2200 goto add_tstr;
2201 case VT_FLOAT:
2202 tstr = "float";
2203 goto add_tstr;
2204 case VT_DOUBLE:
2205 tstr = "double";
2206 goto add_tstr;
2207 case VT_LDOUBLE:
2208 tstr = "long double";
2209 add_tstr:
2210 pstrcat(buf, buf_size, tstr);
2211 break;
2212 case VT_ENUM:
2213 case VT_STRUCT:
2214 if (bt == VT_STRUCT)
2215 tstr = "struct ";
2216 else
2217 tstr = "enum ";
2218 pstrcat(buf, buf_size, tstr);
2219 v = type->ref->v & ~SYM_STRUCT;
2220 if (v >= SYM_FIRST_ANOM)
2221 pstrcat(buf, buf_size, "<anonymous>");
2222 else
2223 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2224 break;
2225 case VT_FUNC:
2226 s = type->ref;
2227 type_to_str(buf, buf_size, &s->type, varstr);
2228 pstrcat(buf, buf_size, "(");
2229 sa = s->next;
2230 while (sa != NULL) {
2231 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2232 pstrcat(buf, buf_size, buf1);
2233 sa = sa->next;
2234 if (sa)
2235 pstrcat(buf, buf_size, ", ");
2237 pstrcat(buf, buf_size, ")");
2238 goto no_var;
2239 case VT_PTR:
2240 s = type->ref;
2241 pstrcpy(buf1, sizeof(buf1), "*");
2242 if (varstr)
2243 pstrcat(buf1, sizeof(buf1), varstr);
2244 type_to_str(buf, buf_size, &s->type, buf1);
2245 goto no_var;
2247 if (varstr) {
2248 pstrcat(buf, buf_size, " ");
2249 pstrcat(buf, buf_size, varstr);
2251 no_var: ;
2254 /* verify type compatibility to store vtop in 'dt' type, and generate
2255 casts if needed. */
2256 static void gen_assign_cast(CType *dt)
2258 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2259 char buf1[256], buf2[256];
2260 int dbt, sbt;
2262 st = &vtop->type; /* source type */
2263 dbt = dt->t & VT_BTYPE;
2264 sbt = st->t & VT_BTYPE;
2265 if (sbt == VT_VOID)
2266 tcc_error("Cannot assign void value");
2267 if (dt->t & VT_CONSTANT)
2268 tcc_warning("assignment of read-only location");
2269 switch(dbt) {
2270 case VT_PTR:
2271 /* special cases for pointers */
2272 /* '0' can also be a pointer */
2273 if (is_null_pointer(vtop))
2274 goto type_ok;
2275 /* accept implicit pointer to integer cast with warning */
2276 if (is_integer_btype(sbt)) {
2277 tcc_warning("assignment makes pointer from integer without a cast");
2278 goto type_ok;
2280 type1 = pointed_type(dt);
2281 /* a function is implicitely a function pointer */
2282 if (sbt == VT_FUNC) {
2283 if ((type1->t & VT_BTYPE) != VT_VOID &&
2284 !is_compatible_types(pointed_type(dt), st))
2285 tcc_warning("assignment from incompatible pointer type");
2286 goto type_ok;
2288 if (sbt != VT_PTR)
2289 goto error;
2290 type2 = pointed_type(st);
2291 if ((type1->t & VT_BTYPE) == VT_VOID ||
2292 (type2->t & VT_BTYPE) == VT_VOID) {
2293 /* void * can match anything */
2294 } else {
2295 /* exact type match, except for unsigned */
2296 tmp_type1 = *type1;
2297 tmp_type2 = *type2;
2298 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2299 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2300 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2301 tcc_warning("assignment from incompatible pointer type");
2303 /* check const and volatile */
2304 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2305 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2306 tcc_warning("assignment discards qualifiers from pointer target type");
2307 break;
2308 case VT_BYTE:
2309 case VT_SHORT:
2310 case VT_INT:
2311 case VT_LLONG:
2312 if (sbt == VT_PTR || sbt == VT_FUNC) {
2313 tcc_warning("assignment makes integer from pointer without a cast");
2315 /* XXX: more tests */
2316 break;
2317 case VT_STRUCT:
2318 tmp_type1 = *dt;
2319 tmp_type2 = *st;
2320 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2321 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2322 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2323 error:
2324 type_to_str(buf1, sizeof(buf1), st, NULL);
2325 type_to_str(buf2, sizeof(buf2), dt, NULL);
2326 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2328 break;
2330 type_ok:
2331 gen_cast(dt);
2334 /* store vtop in lvalue pushed on stack */
2335 ST_FUNC void vstore(void)
2337 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2339 ft = vtop[-1].type.t;
2340 sbt = vtop->type.t & VT_BTYPE;
2341 dbt = ft & VT_BTYPE;
2342 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2343 (sbt == VT_INT && dbt == VT_SHORT))
2344 && !(vtop->type.t & VT_BITFIELD)) {
2345 /* optimize char/short casts */
2346 delayed_cast = VT_MUSTCAST;
2347 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2348 /* XXX: factorize */
2349 if (ft & VT_CONSTANT)
2350 tcc_warning("assignment of read-only location");
2351 } else {
2352 delayed_cast = 0;
2353 if (!(ft & VT_BITFIELD))
2354 gen_assign_cast(&vtop[-1].type);
2357 if (sbt == VT_STRUCT) {
2358 /* if structure, only generate pointer */
2359 /* structure assignment : generate memcpy */
2360 /* XXX: optimize if small size */
2361 if (!nocode_wanted) {
2362 size = type_size(&vtop->type, &align);
2364 /* destination */
2365 vswap();
2366 vtop->type.t = VT_PTR;
2367 gaddrof();
2369 /* address of memcpy() */
2370 #ifdef TCC_ARM_EABI
2371 if(!(align & 7))
2372 vpush_global_sym(&func_old_type, TOK_memcpy8);
2373 else if(!(align & 3))
2374 vpush_global_sym(&func_old_type, TOK_memcpy4);
2375 else
2376 #endif
2377 vpush_global_sym(&func_old_type, TOK_memcpy);
2379 vswap();
2380 /* source */
2381 vpushv(vtop - 2);
2382 vtop->type.t = VT_PTR;
2383 gaddrof();
2384 /* type size */
2385 vpushi(size);
2386 gfunc_call(3);
2387 } else {
2388 vswap();
2389 vpop();
2391 /* leave source on stack */
2392 } else if (ft & VT_BITFIELD) {
2393 /* bitfield store handling */
2394 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2395 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2396 /* remove bit field info to avoid loops */
2397 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2399 /* duplicate source into other register */
2400 gv_dup();
2401 vswap();
2402 vrott(3);
2404 if((ft & VT_BTYPE) == VT_BOOL) {
2405 gen_cast(&vtop[-1].type);
2406 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2409 /* duplicate destination */
2410 vdup();
2411 vtop[-1] = vtop[-2];
2413 /* mask and shift source */
2414 if((ft & VT_BTYPE) != VT_BOOL) {
2415 if((ft & VT_BTYPE) == VT_LLONG) {
2416 vpushll((1ULL << bit_size) - 1ULL);
2417 } else {
2418 vpushi((1 << bit_size) - 1);
2420 gen_op('&');
2422 vpushi(bit_pos);
2423 gen_op(TOK_SHL);
2424 /* load destination, mask and or with source */
2425 vswap();
2426 if((ft & VT_BTYPE) == VT_LLONG) {
2427 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2428 } else {
2429 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2431 gen_op('&');
2432 gen_op('|');
2433 /* store result */
2434 vstore();
2436 /* pop off shifted source from "duplicate source..." above */
2437 vpop();
2439 } else {
2440 #ifdef CONFIG_TCC_BCHECK
2441 /* bound check case */
2442 if (vtop[-1].r & VT_MUSTBOUND) {
2443 vswap();
2444 gbound();
2445 vswap();
2447 #endif
2448 if (!nocode_wanted) {
2449 rc = RC_INT;
2450 if (is_float(ft)) {
2451 rc = RC_FLOAT;
2452 #ifdef TCC_TARGET_X86_64
2453 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2454 rc = RC_ST0;
2456 #endif
2458 r = gv(rc); /* generate value */
2459 /* if lvalue was saved on stack, must read it */
2460 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2461 SValue sv;
2462 t = get_reg(RC_INT);
2463 #ifdef TCC_TARGET_X86_64
2464 sv.type.t = VT_PTR;
2465 #else
2466 sv.type.t = VT_INT;
2467 #endif
2468 sv.r = VT_LOCAL | VT_LVAL;
2469 sv.c.ul = vtop[-1].c.ul;
2470 load(t, &sv);
2471 vtop[-1].r = t | VT_LVAL;
2473 store(r, vtop - 1);
2474 #ifndef TCC_TARGET_X86_64
2475 /* two word case handling : store second register at word + 4 */
2476 if ((ft & VT_BTYPE) == VT_LLONG) {
2477 vswap();
2478 /* convert to int to increment easily */
2479 vtop->type.t = VT_INT;
2480 gaddrof();
2481 vpushi(4);
2482 gen_op('+');
2483 vtop->r |= VT_LVAL;
2484 vswap();
2485 /* XXX: it works because r2 is spilled last ! */
2486 store(vtop->r2, vtop - 1);
2488 #endif
2490 vswap();
2491 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2492 vtop->r |= delayed_cast;
2496 /* post defines POST/PRE add. c is the token ++ or -- */
2497 ST_FUNC void inc(int post, int c)
2499 test_lvalue();
2500 vdup(); /* save lvalue */
2501 if (post) {
2502 gv_dup(); /* duplicate value */
2503 vrotb(3);
2504 vrotb(3);
2506 /* add constant */
2507 vpushi(c - TOK_MID);
2508 gen_op('+');
2509 vstore(); /* store value */
2510 if (post)
2511 vpop(); /* if post op, return saved value */
2514 /* Parse GNUC __attribute__ extension. Currently, the following
2515 extensions are recognized:
2516 - aligned(n) : set data/function alignment.
2517 - packed : force data alignment to 1
2518 - section(x) : generate data/code in this section.
2519 - unused : currently ignored, but may be used someday.
2520 - regparm(n) : pass function parameters in registers (i386 only)
2522 static void parse_attribute(AttributeDef *ad)
2524 int t, n;
2526 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2527 next();
2528 skip('(');
2529 skip('(');
2530 while (tok != ')') {
2531 if (tok < TOK_IDENT)
2532 expect("attribute name");
2533 t = tok;
2534 next();
2535 switch(t) {
2536 case TOK_SECTION1:
2537 case TOK_SECTION2:
2538 skip('(');
2539 if (tok != TOK_STR)
2540 expect("section name");
2541 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2542 next();
2543 skip(')');
2544 break;
2545 case TOK_ALIAS1:
2546 case TOK_ALIAS2:
2547 skip('(');
2548 if (tok != TOK_STR)
2549 expect("alias(\"target\")");
2550 ad->alias_target = /* save string as token, for later */
2551 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2552 next();
2553 skip(')');
2554 break;
2555 case TOK_ALIGNED1:
2556 case TOK_ALIGNED2:
2557 if (tok == '(') {
2558 next();
2559 n = expr_const();
2560 if (n <= 0 || (n & (n - 1)) != 0)
2561 tcc_error("alignment must be a positive power of two");
2562 skip(')');
2563 } else {
2564 n = MAX_ALIGN;
2566 ad->aligned = n;
2567 break;
2568 case TOK_PACKED1:
2569 case TOK_PACKED2:
2570 ad->packed = 1;
2571 break;
2572 case TOK_WEAK1:
2573 case TOK_WEAK2:
2574 ad->weak = 1;
2575 break;
2576 case TOK_UNUSED1:
2577 case TOK_UNUSED2:
2578 /* currently, no need to handle it because tcc does not
2579 track unused objects */
2580 break;
2581 case TOK_NORETURN1:
2582 case TOK_NORETURN2:
2583 /* currently, no need to handle it because tcc does not
2584 track unused objects */
2585 break;
2586 case TOK_CDECL1:
2587 case TOK_CDECL2:
2588 case TOK_CDECL3:
2589 ad->func_call = FUNC_CDECL;
2590 break;
2591 case TOK_STDCALL1:
2592 case TOK_STDCALL2:
2593 case TOK_STDCALL3:
2594 ad->func_call = FUNC_STDCALL;
2595 break;
2596 #ifdef TCC_TARGET_I386
2597 case TOK_REGPARM1:
2598 case TOK_REGPARM2:
2599 skip('(');
2600 n = expr_const();
2601 if (n > 3)
2602 n = 3;
2603 else if (n < 0)
2604 n = 0;
2605 if (n > 0)
2606 ad->func_call = FUNC_FASTCALL1 + n - 1;
2607 skip(')');
2608 break;
2609 case TOK_FASTCALL1:
2610 case TOK_FASTCALL2:
2611 case TOK_FASTCALL3:
2612 ad->func_call = FUNC_FASTCALLW;
2613 break;
2614 #endif
2615 case TOK_MODE:
2616 skip('(');
2617 switch(tok) {
2618 case TOK_MODE_DI:
2619 ad->mode = VT_LLONG + 1;
2620 break;
2621 case TOK_MODE_HI:
2622 ad->mode = VT_SHORT + 1;
2623 break;
2624 case TOK_MODE_SI:
2625 ad->mode = VT_INT + 1;
2626 break;
2627 default:
2628 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2629 break;
2631 next();
2632 skip(')');
2633 break;
2634 case TOK_DLLEXPORT:
2635 ad->func_export = 1;
2636 break;
2637 case TOK_DLLIMPORT:
2638 ad->func_import = 1;
2639 break;
2640 default:
2641 if (tcc_state->warn_unsupported)
2642 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2643 /* skip parameters */
2644 if (tok == '(') {
2645 int parenthesis = 0;
2646 do {
2647 if (tok == '(')
2648 parenthesis++;
2649 else if (tok == ')')
2650 parenthesis--;
2651 next();
2652 } while (parenthesis && tok != -1);
2654 break;
2656 if (tok != ',')
2657 break;
2658 next();
2660 skip(')');
2661 skip(')');
2665 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2666 static void struct_decl(CType *type, int u)
2668 int a, v, size, align, maxalign, c, offset;
2669 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2670 Sym *s, *ss, *ass, **ps;
2671 AttributeDef ad;
2672 CType type1, btype;
2674 a = tok; /* save decl type */
2675 next();
2676 if (tok != '{') {
2677 v = tok;
2678 next();
2679 /* struct already defined ? return it */
2680 if (v < TOK_IDENT)
2681 expect("struct/union/enum name");
2682 s = struct_find(v);
2683 if (s) {
2684 if (s->type.t != a)
2685 tcc_error("invalid type");
2686 goto do_decl;
2688 } else {
2689 v = anon_sym++;
2691 type1.t = a;
2692 /* we put an undefined size for struct/union */
2693 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2694 s->r = 0; /* default alignment is zero as gcc */
2695 /* put struct/union/enum name in type */
2696 do_decl:
2697 type->t = u;
2698 type->ref = s;
2700 if (tok == '{') {
2701 next();
2702 if (s->c != -1)
2703 tcc_error("struct/union/enum already defined");
2704 /* cannot be empty */
2705 c = 0;
2706 /* non empty enums are not allowed */
2707 if (a == TOK_ENUM) {
2708 for(;;) {
2709 v = tok;
2710 if (v < TOK_UIDENT)
2711 expect("identifier");
2712 next();
2713 if (tok == '=') {
2714 next();
2715 c = expr_const();
2717 /* enum symbols have static storage */
2718 ss = sym_push(v, &int_type, VT_CONST, c);
2719 ss->type.t |= VT_STATIC;
2720 if (tok != ',')
2721 break;
2722 next();
2723 c++;
2724 /* NOTE: we accept a trailing comma */
2725 if (tok == '}')
2726 break;
2728 skip('}');
2729 } else {
2730 maxalign = 1;
2731 ps = &s->next;
2732 prevbt = VT_INT;
2733 bit_pos = 0;
2734 offset = 0;
2735 while (tok != '}') {
2736 parse_btype(&btype, &ad);
2737 while (1) {
2738 bit_size = -1;
2739 v = 0;
2740 type1 = btype;
2741 if (tok != ':') {
2742 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2743 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2744 expect("identifier");
2745 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2746 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2747 tcc_error("invalid type for '%s'",
2748 get_tok_str(v, NULL));
2750 if (tok == ':') {
2751 next();
2752 bit_size = expr_const();
2753 /* XXX: handle v = 0 case for messages */
2754 if (bit_size < 0)
2755 tcc_error("negative width in bit-field '%s'",
2756 get_tok_str(v, NULL));
2757 if (v && bit_size == 0)
2758 tcc_error("zero width for bit-field '%s'",
2759 get_tok_str(v, NULL));
2761 size = type_size(&type1, &align);
2762 if (ad.aligned) {
2763 if (align < ad.aligned)
2764 align = ad.aligned;
2765 } else if (ad.packed) {
2766 align = 1;
2767 } else if (*tcc_state->pack_stack_ptr) {
2768 if (align > *tcc_state->pack_stack_ptr)
2769 align = *tcc_state->pack_stack_ptr;
2771 lbit_pos = 0;
2772 if (bit_size >= 0) {
2773 bt = type1.t & VT_BTYPE;
2774 if (bt != VT_INT &&
2775 bt != VT_BYTE &&
2776 bt != VT_SHORT &&
2777 bt != VT_BOOL &&
2778 bt != VT_ENUM &&
2779 bt != VT_LLONG)
2780 tcc_error("bitfields must have scalar type");
2781 bsize = size * 8;
2782 if (bit_size > bsize) {
2783 tcc_error("width of '%s' exceeds its type",
2784 get_tok_str(v, NULL));
2785 } else if (bit_size == bsize) {
2786 /* no need for bit fields */
2787 bit_pos = 0;
2788 } else if (bit_size == 0) {
2789 /* XXX: what to do if only padding in a
2790 structure ? */
2791 /* zero size: means to pad */
2792 bit_pos = 0;
2793 } else {
2794 /* we do not have enough room ?
2795 did the type change?
2796 is it a union? */
2797 if ((bit_pos + bit_size) > bsize ||
2798 bt != prevbt || a == TOK_UNION)
2799 bit_pos = 0;
2800 lbit_pos = bit_pos;
2801 /* XXX: handle LSB first */
2802 type1.t |= VT_BITFIELD |
2803 (bit_pos << VT_STRUCT_SHIFT) |
2804 (bit_size << (VT_STRUCT_SHIFT + 6));
2805 bit_pos += bit_size;
2807 prevbt = bt;
2808 } else {
2809 bit_pos = 0;
2811 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2812 /* add new memory data only if starting
2813 bit field */
2814 if (lbit_pos == 0) {
2815 if (a == TOK_STRUCT) {
2816 c = (c + align - 1) & -align;
2817 offset = c;
2818 if (size > 0)
2819 c += size;
2820 } else {
2821 offset = 0;
2822 if (size > c)
2823 c = size;
2825 if (align > maxalign)
2826 maxalign = align;
2828 #if 0
2829 printf("add field %s offset=%d",
2830 get_tok_str(v, NULL), offset);
2831 if (type1.t & VT_BITFIELD) {
2832 printf(" pos=%d size=%d",
2833 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2834 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2836 printf("\n");
2837 #endif
2839 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2840 ass = type1.ref;
2841 while ((ass = ass->next) != NULL) {
2842 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2843 *ps = ss;
2844 ps = &ss->next;
2846 } else if (v) {
2847 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2848 *ps = ss;
2849 ps = &ss->next;
2851 if (tok == ';' || tok == TOK_EOF)
2852 break;
2853 skip(',');
2855 skip(';');
2857 skip('}');
2858 /* store size and alignment */
2859 s->c = (c + maxalign - 1) & -maxalign;
2860 s->r = maxalign;
2865 /* return 0 if no type declaration. otherwise, return the basic type
2866 and skip it.
2868 static int parse_btype(CType *type, AttributeDef *ad)
2870 int t, u, type_found, typespec_found, typedef_found;
2871 Sym *s;
2872 CType type1;
2874 memset(ad, 0, sizeof(AttributeDef));
2875 type_found = 0;
2876 typespec_found = 0;
2877 typedef_found = 0;
2878 t = 0;
2879 while(1) {
2880 switch(tok) {
2881 case TOK_EXTENSION:
2882 /* currently, we really ignore extension */
2883 next();
2884 continue;
2886 /* basic types */
2887 case TOK_CHAR:
2888 u = VT_BYTE;
2889 basic_type:
2890 next();
2891 basic_type1:
2892 if ((t & VT_BTYPE) != 0)
2893 tcc_error("too many basic types");
2894 t |= u;
2895 typespec_found = 1;
2896 break;
2897 case TOK_VOID:
2898 u = VT_VOID;
2899 goto basic_type;
2900 case TOK_SHORT:
2901 u = VT_SHORT;
2902 goto basic_type;
2903 case TOK_INT:
2904 next();
2905 typespec_found = 1;
2906 break;
2907 case TOK_LONG:
2908 next();
2909 if ((t & VT_BTYPE) == VT_DOUBLE) {
2910 #ifndef TCC_TARGET_PE
2911 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2912 #endif
2913 } else if ((t & VT_BTYPE) == VT_LONG) {
2914 t = (t & ~VT_BTYPE) | VT_LLONG;
2915 } else {
2916 u = VT_LONG;
2917 goto basic_type1;
2919 break;
2920 case TOK_BOOL:
2921 u = VT_BOOL;
2922 goto basic_type;
2923 case TOK_FLOAT:
2924 u = VT_FLOAT;
2925 goto basic_type;
2926 case TOK_DOUBLE:
2927 next();
2928 if ((t & VT_BTYPE) == VT_LONG) {
2929 #ifdef TCC_TARGET_PE
2930 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2931 #else
2932 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2933 #endif
2934 } else {
2935 u = VT_DOUBLE;
2936 goto basic_type1;
2938 break;
2939 case TOK_ENUM:
2940 struct_decl(&type1, VT_ENUM);
2941 basic_type2:
2942 u = type1.t;
2943 type->ref = type1.ref;
2944 goto basic_type1;
2945 case TOK_STRUCT:
2946 case TOK_UNION:
2947 struct_decl(&type1, VT_STRUCT);
2948 goto basic_type2;
2950 /* type modifiers */
2951 case TOK_CONST1:
2952 case TOK_CONST2:
2953 case TOK_CONST3:
2954 t |= VT_CONSTANT;
2955 next();
2956 break;
2957 case TOK_VOLATILE1:
2958 case TOK_VOLATILE2:
2959 case TOK_VOLATILE3:
2960 t |= VT_VOLATILE;
2961 next();
2962 break;
2963 case TOK_SIGNED1:
2964 case TOK_SIGNED2:
2965 case TOK_SIGNED3:
2966 typespec_found = 1;
2967 t |= VT_SIGNED;
2968 next();
2969 break;
2970 case TOK_REGISTER:
2971 case TOK_AUTO:
2972 case TOK_RESTRICT1:
2973 case TOK_RESTRICT2:
2974 case TOK_RESTRICT3:
2975 next();
2976 break;
2977 case TOK_UNSIGNED:
2978 t |= VT_UNSIGNED;
2979 next();
2980 typespec_found = 1;
2981 break;
2983 /* storage */
2984 case TOK_EXTERN:
2985 t |= VT_EXTERN;
2986 next();
2987 break;
2988 case TOK_STATIC:
2989 t |= VT_STATIC;
2990 next();
2991 break;
2992 case TOK_TYPEDEF:
2993 t |= VT_TYPEDEF;
2994 next();
2995 break;
2996 case TOK_INLINE1:
2997 case TOK_INLINE2:
2998 case TOK_INLINE3:
2999 t |= VT_INLINE;
3000 next();
3001 break;
3003 /* GNUC attribute */
3004 case TOK_ATTRIBUTE1:
3005 case TOK_ATTRIBUTE2:
3006 parse_attribute(ad);
3007 if (ad->mode) {
3008 u = ad->mode -1;
3009 t = (t & ~VT_BTYPE) | u;
3011 break;
3012 /* GNUC typeof */
3013 case TOK_TYPEOF1:
3014 case TOK_TYPEOF2:
3015 case TOK_TYPEOF3:
3016 next();
3017 parse_expr_type(&type1);
3018 /* remove all storage modifiers except typedef */
3019 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3020 goto basic_type2;
3021 default:
3022 if (typespec_found || typedef_found)
3023 goto the_end;
3024 s = sym_find(tok);
3025 if (!s || !(s->type.t & VT_TYPEDEF))
3026 goto the_end;
3027 typedef_found = 1;
3028 t |= (s->type.t & ~VT_TYPEDEF);
3029 type->ref = s->type.ref;
3030 if (s->r) {
3031 /* get attributes from typedef */
3032 if (0 == ad->aligned)
3033 ad->aligned = FUNC_ALIGN(s->r);
3034 if (0 == ad->func_call)
3035 ad->func_call = FUNC_CALL(s->r);
3036 ad->packed |= FUNC_PACKED(s->r);
3038 next();
3039 typespec_found = 1;
3040 break;
3042 type_found = 1;
3044 the_end:
3045 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3046 tcc_error("signed and unsigned modifier");
3047 if (tcc_state->char_is_unsigned) {
3048 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3049 t |= VT_UNSIGNED;
3051 t &= ~VT_SIGNED;
3053 /* long is never used as type */
3054 if ((t & VT_BTYPE) == VT_LONG)
3055 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3056 t = (t & ~VT_BTYPE) | VT_INT;
3057 #else
3058 t = (t & ~VT_BTYPE) | VT_LLONG;
3059 #endif
3060 type->t = t;
3061 return type_found;
3064 /* convert a function parameter type (array to pointer and function to
3065 function pointer) */
3066 static inline void convert_parameter_type(CType *pt)
3068 /* remove const and volatile qualifiers (XXX: const could be used
3069 to indicate a const function parameter */
3070 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3071 /* array must be transformed to pointer according to ANSI C */
3072 pt->t &= ~VT_ARRAY;
3073 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3074 mk_pointer(pt);
3078 ST_FUNC void parse_asm_str(CString *astr)
3080 skip('(');
3081 /* read the string */
3082 if (tok != TOK_STR)
3083 expect("string constant");
3084 cstr_new(astr);
3085 while (tok == TOK_STR) {
3086 /* XXX: add \0 handling too ? */
3087 cstr_cat(astr, tokc.cstr->data);
3088 next();
3090 cstr_ccat(astr, '\0');
3093 /* Parse an asm label and return the label
3094 * Don't forget to free the CString in the caller! */
3095 static void asm_label_instr(CString *astr)
3097 next();
3098 parse_asm_str(astr);
3099 skip(')');
3100 #ifdef ASM_DEBUG
3101 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3102 #endif
3105 static void post_type(CType *type, AttributeDef *ad)
3107 int n, l, t1, arg_size, align;
3108 Sym **plast, *s, *first;
3109 AttributeDef ad1;
3110 CType pt;
3112 if (tok == '(') {
3113 /* function declaration */
3114 next();
3115 l = 0;
3116 first = NULL;
3117 plast = &first;
3118 arg_size = 0;
3119 if (tok != ')') {
3120 for(;;) {
3121 /* read param name and compute offset */
3122 if (l != FUNC_OLD) {
3123 if (!parse_btype(&pt, &ad1)) {
3124 if (l) {
3125 tcc_error("invalid type");
3126 } else {
3127 l = FUNC_OLD;
3128 goto old_proto;
3131 l = FUNC_NEW;
3132 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3133 break;
3134 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3135 if ((pt.t & VT_BTYPE) == VT_VOID)
3136 tcc_error("parameter declared as void");
3137 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3138 } else {
3139 old_proto:
3140 n = tok;
3141 if (n < TOK_UIDENT)
3142 expect("identifier");
3143 pt.t = VT_INT;
3144 next();
3146 convert_parameter_type(&pt);
3147 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3148 *plast = s;
3149 plast = &s->next;
3150 if (tok == ')')
3151 break;
3152 skip(',');
3153 if (l == FUNC_NEW && tok == TOK_DOTS) {
3154 l = FUNC_ELLIPSIS;
3155 next();
3156 break;
3160 /* if no parameters, then old type prototype */
3161 if (l == 0)
3162 l = FUNC_OLD;
3163 skip(')');
3164 /* NOTE: const is ignored in returned type as it has a special
3165 meaning in gcc / C++ */
3166 type->t &= ~VT_CONSTANT;
3167 /* some ancient pre-K&R C allows a function to return an array
3168 and the array brackets to be put after the arguments, such
3169 that "int c()[]" means something like "int[] c()" */
3170 if (tok == '[') {
3171 next();
3172 skip(']'); /* only handle simple "[]" */
3173 type->t |= VT_PTR;
3175 /* we push a anonymous symbol which will contain the function prototype */
3176 ad->func_args = arg_size;
3177 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3178 s->next = first;
3179 type->t = VT_FUNC;
3180 type->ref = s;
3181 } else if (tok == '[') {
3182 /* array definition */
3183 next();
3184 if (tok == TOK_RESTRICT1)
3185 next();
3186 n = -1;
3187 t1 = 0;
3188 if (tok != ']') {
3189 if (!local_stack || nocode_wanted)
3190 vpushi(expr_const());
3191 else gexpr();
3192 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3193 n = vtop->c.i;
3194 if (n < 0)
3195 tcc_error("invalid array size");
3196 } else {
3197 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3198 tcc_error("size of variable length array should be an integer");
3199 t1 = VT_VLA;
3202 skip(']');
3203 /* parse next post type */
3204 post_type(type, ad);
3205 t1 |= type->t & VT_VLA;
3207 if (t1 & VT_VLA) {
3208 loc -= type_size(&int_type, &align);
3209 loc &= -align;
3210 n = loc;
3212 vla_runtime_type_size(type, &align);
3213 gen_op('*');
3214 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3215 vswap();
3216 vstore();
3218 if (n != -1)
3219 vpop();
3221 /* we push an anonymous symbol which will contain the array
3222 element type */
3223 s = sym_push(SYM_FIELD, type, 0, n);
3224 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3225 type->ref = s;
3229 /* Parse a type declaration (except basic type), and return the type
3230 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3231 expected. 'type' should contain the basic type. 'ad' is the
3232 attribute definition of the basic type. It can be modified by
3233 type_decl().
3235 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3237 Sym *s;
3238 CType type1, *type2;
3239 int qualifiers, storage;
3241 while (tok == '*') {
3242 qualifiers = 0;
3243 redo:
3244 next();
3245 switch(tok) {
3246 case TOK_CONST1:
3247 case TOK_CONST2:
3248 case TOK_CONST3:
3249 qualifiers |= VT_CONSTANT;
3250 goto redo;
3251 case TOK_VOLATILE1:
3252 case TOK_VOLATILE2:
3253 case TOK_VOLATILE3:
3254 qualifiers |= VT_VOLATILE;
3255 goto redo;
3256 case TOK_RESTRICT1:
3257 case TOK_RESTRICT2:
3258 case TOK_RESTRICT3:
3259 goto redo;
3261 mk_pointer(type);
3262 type->t |= qualifiers;
3265 /* XXX: clarify attribute handling */
3266 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3267 parse_attribute(ad);
3269 /* recursive type */
3270 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3271 type1.t = 0; /* XXX: same as int */
3272 if (tok == '(') {
3273 next();
3274 /* XXX: this is not correct to modify 'ad' at this point, but
3275 the syntax is not clear */
3276 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3277 parse_attribute(ad);
3278 type_decl(&type1, ad, v, td);
3279 skip(')');
3280 } else {
3281 /* type identifier */
3282 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3283 *v = tok;
3284 next();
3285 } else {
3286 if (!(td & TYPE_ABSTRACT))
3287 expect("identifier");
3288 *v = 0;
3291 storage = type->t & VT_STORAGE;
3292 type->t &= ~VT_STORAGE;
3293 post_type(type, ad);
3294 type->t |= storage;
3295 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3296 parse_attribute(ad);
3298 if (!type1.t)
3299 return;
3300 /* append type at the end of type1 */
3301 type2 = &type1;
3302 for(;;) {
3303 s = type2->ref;
3304 type2 = &s->type;
3305 if (!type2->t) {
3306 *type2 = *type;
3307 break;
3310 *type = type1;
3313 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3314 ST_FUNC int lvalue_type(int t)
3316 int bt, r;
3317 r = VT_LVAL;
3318 bt = t & VT_BTYPE;
3319 if (bt == VT_BYTE || bt == VT_BOOL)
3320 r |= VT_LVAL_BYTE;
3321 else if (bt == VT_SHORT)
3322 r |= VT_LVAL_SHORT;
3323 else
3324 return r;
3325 if (t & VT_UNSIGNED)
3326 r |= VT_LVAL_UNSIGNED;
3327 return r;
3330 /* indirection with full error checking and bound check */
3331 ST_FUNC void indir(void)
3333 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3334 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3335 return;
3336 expect("pointer");
3338 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3339 gv(RC_INT);
3340 vtop->type = *pointed_type(&vtop->type);
3341 /* Arrays and functions are never lvalues */
3342 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3343 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3344 vtop->r |= lvalue_type(vtop->type.t);
3345 /* if bound checking, the referenced pointer must be checked */
3346 #ifdef CONFIG_TCC_BCHECK
3347 if (tcc_state->do_bounds_check)
3348 vtop->r |= VT_MUSTBOUND;
3349 #endif
3353 /* pass a parameter to a function and do type checking and casting */
3354 static void gfunc_param_typed(Sym *func, Sym *arg)
3356 int func_type;
3357 CType type;
3359 func_type = func->c;
3360 if (func_type == FUNC_OLD ||
3361 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3362 /* default casting : only need to convert float to double */
3363 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3364 type.t = VT_DOUBLE;
3365 gen_cast(&type);
3367 } else if (arg == NULL) {
3368 tcc_error("too many arguments to function");
3369 } else {
3370 type = arg->type;
3371 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3372 gen_assign_cast(&type);
3376 /* parse an expression of the form '(type)' or '(expr)' and return its
3377 type */
3378 static void parse_expr_type(CType *type)
3380 int n;
3381 AttributeDef ad;
3383 skip('(');
3384 if (parse_btype(type, &ad)) {
3385 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3386 } else {
3387 expr_type(type);
3389 skip(')');
3392 static void parse_type(CType *type)
3394 AttributeDef ad;
3395 int n;
3397 if (!parse_btype(type, &ad)) {
3398 expect("type");
3400 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3403 static void vpush_tokc(int t)
3405 CType type;
3406 type.t = t;
3407 type.ref = 0;
3408 vsetc(&type, VT_CONST, &tokc);
3411 ST_FUNC void unary(void)
3413 int n, t, align, size, r, sizeof_caller;
3414 CType type;
3415 Sym *s;
3416 AttributeDef ad;
3417 static int in_sizeof = 0;
3419 sizeof_caller = in_sizeof;
3420 in_sizeof = 0;
3421 /* XXX: GCC 2.95.3 does not generate a table although it should be
3422 better here */
3423 tok_next:
3424 switch(tok) {
3425 case TOK_EXTENSION:
3426 next();
3427 goto tok_next;
3428 case TOK_CINT:
3429 case TOK_CCHAR:
3430 case TOK_LCHAR:
3431 vpushi(tokc.i);
3432 next();
3433 break;
3434 case TOK_CUINT:
3435 vpush_tokc(VT_INT | VT_UNSIGNED);
3436 next();
3437 break;
3438 case TOK_CLLONG:
3439 vpush_tokc(VT_LLONG);
3440 next();
3441 break;
3442 case TOK_CULLONG:
3443 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3444 next();
3445 break;
3446 case TOK_CFLOAT:
3447 vpush_tokc(VT_FLOAT);
3448 next();
3449 break;
3450 case TOK_CDOUBLE:
3451 vpush_tokc(VT_DOUBLE);
3452 next();
3453 break;
3454 case TOK_CLDOUBLE:
3455 vpush_tokc(VT_LDOUBLE);
3456 next();
3457 break;
3458 case TOK___FUNCTION__:
3459 if (!gnu_ext)
3460 goto tok_identifier;
3461 /* fall thru */
3462 case TOK___FUNC__:
3464 void *ptr;
3465 int len;
3466 /* special function name identifier */
3467 len = strlen(funcname) + 1;
3468 /* generate char[len] type */
3469 type.t = VT_BYTE;
3470 mk_pointer(&type);
3471 type.t |= VT_ARRAY;
3472 type.ref->c = len;
3473 vpush_ref(&type, data_section, data_section->data_offset, len);
3474 ptr = section_ptr_add(data_section, len);
3475 memcpy(ptr, funcname, len);
3476 next();
3478 break;
3479 case TOK_LSTR:
3480 #ifdef TCC_TARGET_PE
3481 t = VT_SHORT | VT_UNSIGNED;
3482 #else
3483 t = VT_INT;
3484 #endif
3485 goto str_init;
3486 case TOK_STR:
3487 /* string parsing */
3488 t = VT_BYTE;
3489 str_init:
3490 if (tcc_state->warn_write_strings)
3491 t |= VT_CONSTANT;
3492 type.t = t;
3493 mk_pointer(&type);
3494 type.t |= VT_ARRAY;
3495 memset(&ad, 0, sizeof(AttributeDef));
3496 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3497 break;
3498 case '(':
3499 next();
3500 /* cast ? */
3501 if (parse_btype(&type, &ad)) {
3502 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3503 skip(')');
3504 /* check ISOC99 compound literal */
3505 if (tok == '{') {
3506 /* data is allocated locally by default */
3507 if (global_expr)
3508 r = VT_CONST;
3509 else
3510 r = VT_LOCAL;
3511 /* all except arrays are lvalues */
3512 if (!(type.t & VT_ARRAY))
3513 r |= lvalue_type(type.t);
3514 memset(&ad, 0, sizeof(AttributeDef));
3515 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3516 } else {
3517 if (sizeof_caller) {
3518 vpush(&type);
3519 return;
3521 unary();
3522 gen_cast(&type);
3524 } else if (tok == '{') {
3525 /* save all registers */
3526 save_regs(0);
3527 /* statement expression : we do not accept break/continue
3528 inside as GCC does */
3529 block(NULL, NULL, NULL, NULL, 0, 1);
3530 skip(')');
3531 } else {
3532 gexpr();
3533 skip(')');
3535 break;
3536 case '*':
3537 next();
3538 unary();
3539 indir();
3540 break;
3541 case '&':
3542 next();
3543 unary();
3544 /* functions names must be treated as function pointers,
3545 except for unary '&' and sizeof. Since we consider that
3546 functions are not lvalues, we only have to handle it
3547 there and in function calls. */
3548 /* arrays can also be used although they are not lvalues */
3549 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3550 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3551 test_lvalue();
3552 mk_pointer(&vtop->type);
3553 gaddrof();
3554 break;
3555 case '!':
3556 next();
3557 unary();
3558 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3559 CType boolean;
3560 boolean.t = VT_BOOL;
3561 gen_cast(&boolean);
3562 vtop->c.i = !vtop->c.i;
3563 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3564 vtop->c.i = vtop->c.i ^ 1;
3565 else {
3566 save_regs(1);
3567 vseti(VT_JMP, gtst(1, 0));
3569 break;
3570 case '~':
3571 next();
3572 unary();
3573 vpushi(-1);
3574 gen_op('^');
3575 break;
3576 case '+':
3577 next();
3578 /* in order to force cast, we add zero */
3579 unary();
3580 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3581 tcc_error("pointer not accepted for unary plus");
3582 vpushi(0);
3583 gen_op('+');
3584 break;
3585 case TOK_SIZEOF:
3586 case TOK_ALIGNOF1:
3587 case TOK_ALIGNOF2:
3588 t = tok;
3589 next();
3590 in_sizeof++;
3591 unary_type(&type); // Perform a in_sizeof = 0;
3592 size = type_size(&type, &align);
3593 if (t == TOK_SIZEOF) {
3594 if (!(type.t & VT_VLA)) {
3595 if (size < 0)
3596 tcc_error("sizeof applied to an incomplete type");
3597 vpushs(size);
3598 } else {
3599 vla_runtime_type_size(&type, &align);
3601 } else {
3602 vpushs(align);
3604 vtop->type.t |= VT_UNSIGNED;
3605 break;
3607 case TOK_builtin_types_compatible_p:
3609 CType type1, type2;
3610 next();
3611 skip('(');
3612 parse_type(&type1);
3613 skip(',');
3614 parse_type(&type2);
3615 skip(')');
3616 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3617 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3618 vpushi(is_compatible_types(&type1, &type2));
3620 break;
3621 case TOK_builtin_constant_p:
3623 int saved_nocode_wanted, res;
3624 next();
3625 skip('(');
3626 saved_nocode_wanted = nocode_wanted;
3627 nocode_wanted = 1;
3628 gexpr();
3629 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3630 vpop();
3631 nocode_wanted = saved_nocode_wanted;
3632 skip(')');
3633 vpushi(res);
3635 break;
3636 case TOK_builtin_frame_address:
3638 CType type;
3639 next();
3640 skip('(');
3641 if (tok != TOK_CINT) {
3642 tcc_error("__builtin_frame_address only takes integers");
3644 if (tokc.i != 0) {
3645 tcc_error("TCC only supports __builtin_frame_address(0)");
3647 next();
3648 skip(')');
3649 type.t = VT_VOID;
3650 mk_pointer(&type);
3651 vset(&type, VT_LOCAL, 0);
3653 break;
3654 #ifdef TCC_TARGET_X86_64
3655 case TOK_builtin_va_arg_types:
3657 /* This definition must be synced with stdarg.h */
3658 enum __va_arg_type {
3659 __va_gen_reg, __va_float_reg, __va_stack
3661 CType type;
3662 int bt;
3663 next();
3664 skip('(');
3665 parse_type(&type);
3666 skip(')');
3667 bt = type.t & VT_BTYPE;
3668 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
3669 vpushi(__va_stack);
3670 } else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
3671 vpushi(__va_float_reg);
3672 } else {
3673 vpushi(__va_gen_reg);
3676 break;
3677 #endif
3678 case TOK_INC:
3679 case TOK_DEC:
3680 t = tok;
3681 next();
3682 unary();
3683 inc(0, t);
3684 break;
3685 case '-':
3686 next();
3687 vpushi(0);
3688 unary();
3689 gen_op('-');
3690 break;
3691 case TOK_LAND:
3692 if (!gnu_ext)
3693 goto tok_identifier;
3694 next();
3695 /* allow to take the address of a label */
3696 if (tok < TOK_UIDENT)
3697 expect("label identifier");
3698 s = label_find(tok);
3699 if (!s) {
3700 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3701 } else {
3702 if (s->r == LABEL_DECLARED)
3703 s->r = LABEL_FORWARD;
3705 if (!s->type.t) {
3706 s->type.t = VT_VOID;
3707 mk_pointer(&s->type);
3708 s->type.t |= VT_STATIC;
3710 vset(&s->type, VT_CONST | VT_SYM, 0);
3711 vtop->sym = s;
3712 next();
3713 break;
3715 // special qnan , snan and infinity values
3716 case TOK___NAN__:
3717 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3718 next();
3719 break;
3720 case TOK___SNAN__:
3721 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3722 next();
3723 break;
3724 case TOK___INF__:
3725 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3726 next();
3727 break;
3729 default:
3730 tok_identifier:
3731 t = tok;
3732 next();
3733 if (t < TOK_UIDENT)
3734 expect("identifier");
3735 s = sym_find(t);
3736 if (!s) {
3737 if (tok != '(')
3738 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3739 /* for simple function calls, we tolerate undeclared
3740 external reference to int() function */
3741 if (tcc_state->warn_implicit_function_declaration)
3742 tcc_warning("implicit declaration of function '%s'",
3743 get_tok_str(t, NULL));
3744 s = external_global_sym(t, &func_old_type, 0);
3746 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3747 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3748 /* if referencing an inline function, then we generate a
3749 symbol to it if not already done. It will have the
3750 effect to generate code for it at the end of the
3751 compilation unit. Inline function as always
3752 generated in the text section. */
3753 if (!s->c)
3754 put_extern_sym(s, text_section, 0, 0);
3755 r = VT_SYM | VT_CONST;
3756 } else {
3757 r = s->r;
3759 vset(&s->type, r, s->c);
3760 /* if forward reference, we must point to s */
3761 if (vtop->r & VT_SYM) {
3762 vtop->sym = s;
3763 vtop->c.ul = 0;
3765 break;
3768 /* post operations */
3769 while (1) {
3770 if (tok == TOK_INC || tok == TOK_DEC) {
3771 inc(1, tok);
3772 next();
3773 } else if (tok == '.' || tok == TOK_ARROW) {
3774 int qualifiers;
3775 /* field */
3776 if (tok == TOK_ARROW)
3777 indir();
3778 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3779 test_lvalue();
3780 gaddrof();
3781 next();
3782 /* expect pointer on structure */
3783 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3784 expect("struct or union");
3785 s = vtop->type.ref;
3786 /* find field */
3787 tok |= SYM_FIELD;
3788 while ((s = s->next) != NULL) {
3789 if (s->v == tok)
3790 break;
3792 if (!s)
3793 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3794 /* add field offset to pointer */
3795 vtop->type = char_pointer_type; /* change type to 'char *' */
3796 vpushi(s->c);
3797 gen_op('+');
3798 /* change type to field type, and set to lvalue */
3799 vtop->type = s->type;
3800 vtop->type.t |= qualifiers;
3801 /* an array is never an lvalue */
3802 if (!(vtop->type.t & VT_ARRAY)) {
3803 vtop->r |= lvalue_type(vtop->type.t);
3804 #ifdef CONFIG_TCC_BCHECK
3805 /* if bound checking, the referenced pointer must be checked */
3806 if (tcc_state->do_bounds_check)
3807 vtop->r |= VT_MUSTBOUND;
3808 #endif
3810 next();
3811 } else if (tok == '[') {
3812 next();
3813 gexpr();
3814 gen_op('+');
3815 indir();
3816 skip(']');
3817 } else if (tok == '(') {
3818 SValue ret;
3819 Sym *sa;
3820 int nb_args;
3822 /* function call */
3823 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3824 /* pointer test (no array accepted) */
3825 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3826 vtop->type = *pointed_type(&vtop->type);
3827 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3828 goto error_func;
3829 } else {
3830 error_func:
3831 expect("function pointer");
3833 } else {
3834 vtop->r &= ~VT_LVAL; /* no lvalue */
3836 /* get return type */
3837 s = vtop->type.ref;
3838 next();
3839 sa = s->next; /* first parameter */
3840 nb_args = 0;
3841 ret.r2 = VT_CONST;
3842 /* compute first implicit argument if a structure is returned */
3843 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3844 /* get some space for the returned structure */
3845 size = type_size(&s->type, &align);
3846 loc = (loc - size) & -align;
3847 ret.type = s->type;
3848 ret.r = VT_LOCAL | VT_LVAL;
3849 /* pass it as 'int' to avoid structure arg passing
3850 problems */
3851 vseti(VT_LOCAL, loc);
3852 ret.c = vtop->c;
3853 nb_args++;
3854 } else {
3855 ret.type = s->type;
3856 /* return in register */
3857 if (is_float(ret.type.t)) {
3858 ret.r = reg_fret(ret.type.t);
3859 } else {
3860 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3861 ret.r2 = REG_LRET;
3862 ret.r = REG_IRET;
3864 ret.c.i = 0;
3866 if (tok != ')') {
3867 for(;;) {
3868 expr_eq();
3869 gfunc_param_typed(s, sa);
3870 nb_args++;
3871 if (sa)
3872 sa = sa->next;
3873 if (tok == ')')
3874 break;
3875 skip(',');
3878 if (sa)
3879 tcc_error("too few arguments to function");
3880 skip(')');
3881 if (!nocode_wanted) {
3882 gfunc_call(nb_args);
3883 } else {
3884 vtop -= (nb_args + 1);
3886 /* return value */
3887 vsetc(&ret.type, ret.r, &ret.c);
3888 vtop->r2 = ret.r2;
3889 } else {
3890 break;
3895 ST_FUNC void expr_prod(void)
3897 int t;
3899 unary();
3900 while (tok == '*' || tok == '/' || tok == '%') {
3901 t = tok;
3902 next();
3903 unary();
3904 gen_op(t);
3908 ST_FUNC void expr_sum(void)
3910 int t;
3912 expr_prod();
3913 while (tok == '+' || tok == '-') {
3914 t = tok;
3915 next();
3916 expr_prod();
3917 gen_op(t);
3921 static void expr_shift(void)
3923 int t;
3925 expr_sum();
3926 while (tok == TOK_SHL || tok == TOK_SAR) {
3927 t = tok;
3928 next();
3929 expr_sum();
3930 gen_op(t);
3934 static void expr_cmp(void)
3936 int t;
3938 expr_shift();
3939 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3940 tok == TOK_ULT || tok == TOK_UGE) {
3941 t = tok;
3942 next();
3943 expr_shift();
3944 gen_op(t);
3948 static void expr_cmpeq(void)
3950 int t;
3952 expr_cmp();
3953 while (tok == TOK_EQ || tok == TOK_NE) {
3954 t = tok;
3955 next();
3956 expr_cmp();
3957 gen_op(t);
3961 static void expr_and(void)
3963 expr_cmpeq();
3964 while (tok == '&') {
3965 next();
3966 expr_cmpeq();
3967 gen_op('&');
3971 static void expr_xor(void)
3973 expr_and();
3974 while (tok == '^') {
3975 next();
3976 expr_and();
3977 gen_op('^');
3981 static void expr_or(void)
3983 expr_xor();
3984 while (tok == '|') {
3985 next();
3986 expr_xor();
3987 gen_op('|');
3991 /* XXX: fix this mess */
3992 static void expr_land_const(void)
3994 expr_or();
3995 while (tok == TOK_LAND) {
3996 next();
3997 expr_or();
3998 gen_op(TOK_LAND);
4002 /* XXX: fix this mess */
4003 static void expr_lor_const(void)
4005 expr_land_const();
4006 while (tok == TOK_LOR) {
4007 next();
4008 expr_land_const();
4009 gen_op(TOK_LOR);
4013 /* only used if non constant */
4014 static void expr_land(void)
4016 int t;
4018 expr_or();
4019 if (tok == TOK_LAND) {
4020 t = 0;
4021 save_regs(1);
4022 for(;;) {
4023 t = gtst(1, t);
4024 if (tok != TOK_LAND) {
4025 vseti(VT_JMPI, t);
4026 break;
4028 next();
4029 expr_or();
4034 static void expr_lor(void)
4036 int t;
4038 expr_land();
4039 if (tok == TOK_LOR) {
4040 t = 0;
4041 save_regs(1);
4042 for(;;) {
4043 t = gtst(0, t);
4044 if (tok != TOK_LOR) {
4045 vseti(VT_JMP, t);
4046 break;
4048 next();
4049 expr_land();
4054 /* XXX: better constant handling */
4055 static void expr_cond(void)
4057 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4058 SValue sv;
4059 CType type, type1, type2;
4061 if (const_wanted) {
4062 expr_lor_const();
4063 if (tok == '?') {
4064 CType boolean;
4065 int c;
4066 boolean.t = VT_BOOL;
4067 vdup();
4068 gen_cast(&boolean);
4069 c = vtop->c.i;
4070 vpop();
4071 next();
4072 if (tok != ':' || !gnu_ext) {
4073 vpop();
4074 gexpr();
4076 if (!c)
4077 vpop();
4078 skip(':');
4079 expr_cond();
4080 if (c)
4081 vpop();
4083 } else {
4084 expr_lor();
4085 if (tok == '?') {
4086 next();
4087 if (vtop != vstack) {
4088 /* needed to avoid having different registers saved in
4089 each branch */
4090 if (is_float(vtop->type.t)) {
4091 rc = RC_FLOAT;
4092 #ifdef TCC_TARGET_X86_64
4093 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4094 rc = RC_ST0;
4096 #endif
4098 else
4099 rc = RC_INT;
4100 gv(rc);
4101 save_regs(1);
4103 if (tok == ':' && gnu_ext) {
4104 gv_dup();
4105 tt = gtst(1, 0);
4106 } else {
4107 tt = gtst(1, 0);
4108 gexpr();
4110 type1 = vtop->type;
4111 sv = *vtop; /* save value to handle it later */
4112 vtop--; /* no vpop so that FP stack is not flushed */
4113 skip(':');
4114 u = gjmp(0);
4115 gsym(tt);
4116 expr_cond();
4117 type2 = vtop->type;
4119 t1 = type1.t;
4120 bt1 = t1 & VT_BTYPE;
4121 t2 = type2.t;
4122 bt2 = t2 & VT_BTYPE;
4123 /* cast operands to correct type according to ISOC rules */
4124 if (is_float(bt1) || is_float(bt2)) {
4125 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4126 type.t = VT_LDOUBLE;
4127 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4128 type.t = VT_DOUBLE;
4129 } else {
4130 type.t = VT_FLOAT;
4132 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4133 /* cast to biggest op */
4134 type.t = VT_LLONG;
4135 /* convert to unsigned if it does not fit in a long long */
4136 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4137 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4138 type.t |= VT_UNSIGNED;
4139 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4140 /* If one is a null ptr constant the result type
4141 is the other. */
4142 if (is_null_pointer (vtop))
4143 type = type1;
4144 else if (is_null_pointer (&sv))
4145 type = type2;
4146 /* XXX: test pointer compatibility, C99 has more elaborate
4147 rules here. */
4148 else
4149 type = type1;
4150 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4151 /* XXX: test function pointer compatibility */
4152 type = bt1 == VT_FUNC ? type1 : type2;
4153 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4154 /* XXX: test structure compatibility */
4155 type = bt1 == VT_STRUCT ? type1 : type2;
4156 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4157 /* NOTE: as an extension, we accept void on only one side */
4158 type.t = VT_VOID;
4159 } else {
4160 /* integer operations */
4161 type.t = VT_INT;
4162 /* convert to unsigned if it does not fit in an integer */
4163 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4164 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4165 type.t |= VT_UNSIGNED;
4168 /* now we convert second operand */
4169 gen_cast(&type);
4170 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4171 gaddrof();
4172 rc = RC_INT;
4173 if (is_float(type.t)) {
4174 rc = RC_FLOAT;
4175 #ifdef TCC_TARGET_X86_64
4176 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4177 rc = RC_ST0;
4179 #endif
4180 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4181 /* for long longs, we use fixed registers to avoid having
4182 to handle a complicated move */
4183 rc = RC_IRET;
4186 r2 = gv(rc);
4187 /* this is horrible, but we must also convert first
4188 operand */
4189 tt = gjmp(0);
4190 gsym(u);
4191 /* put again first value and cast it */
4192 *vtop = sv;
4193 gen_cast(&type);
4194 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4195 gaddrof();
4196 r1 = gv(rc);
4197 move_reg(r2, r1);
4198 vtop->r = r2;
4199 gsym(tt);
4204 static void expr_eq(void)
4206 int t;
4208 expr_cond();
4209 if (tok == '=' ||
4210 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4211 tok == TOK_A_XOR || tok == TOK_A_OR ||
4212 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4213 test_lvalue();
4214 t = tok;
4215 next();
4216 if (t == '=') {
4217 expr_eq();
4218 } else {
4219 vdup();
4220 expr_eq();
4221 gen_op(t & 0x7f);
4223 vstore();
4227 ST_FUNC void gexpr(void)
4229 while (1) {
4230 expr_eq();
4231 if (tok != ',')
4232 break;
4233 vpop();
4234 next();
4238 /* parse an expression and return its type without any side effect. */
4239 static void expr_type(CType *type)
4241 int saved_nocode_wanted;
4243 saved_nocode_wanted = nocode_wanted;
4244 nocode_wanted = 1;
4245 gexpr();
4246 *type = vtop->type;
4247 vpop();
4248 nocode_wanted = saved_nocode_wanted;
4251 /* parse a unary expression and return its type without any side
4252 effect. */
4253 static void unary_type(CType *type)
4255 int a;
4257 a = nocode_wanted;
4258 nocode_wanted = 1;
4259 unary();
4260 *type = vtop->type;
4261 vpop();
4262 nocode_wanted = a;
4265 /* parse a constant expression and return value in vtop. */
4266 static void expr_const1(void)
4268 int a;
4269 a = const_wanted;
4270 const_wanted = 1;
4271 expr_cond();
4272 const_wanted = a;
4275 /* parse an integer constant and return its value. */
4276 ST_FUNC int expr_const(void)
4278 int c;
4279 expr_const1();
4280 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4281 expect("constant expression");
4282 c = vtop->c.i;
4283 vpop();
4284 return c;
4287 /* return the label token if current token is a label, otherwise
4288 return zero */
4289 static int is_label(void)
4291 int last_tok;
4293 /* fast test first */
4294 if (tok < TOK_UIDENT)
4295 return 0;
4296 /* no need to save tokc because tok is an identifier */
4297 last_tok = tok;
4298 next();
4299 if (tok == ':') {
4300 next();
4301 return last_tok;
4302 } else {
4303 unget_tok(last_tok);
4304 return 0;
4308 static void label_or_decl(int l)
4310 int last_tok;
4312 /* fast test first */
4313 if (tok >= TOK_UIDENT)
4315 /* no need to save tokc because tok is an identifier */
4316 last_tok = tok;
4317 next();
4318 if (tok == ':') {
4319 unget_tok(last_tok);
4320 return;
4322 unget_tok(last_tok);
4324 decl(l);
4327 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4328 int case_reg, int is_expr)
4330 int a, b, c, d;
4331 Sym *s;
4333 /* generate line number info */
4334 if (tcc_state->do_debug &&
4335 (last_line_num != file->line_num || last_ind != ind)) {
4336 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4337 last_ind = ind;
4338 last_line_num = file->line_num;
4341 if (is_expr) {
4342 /* default return value is (void) */
4343 vpushi(0);
4344 vtop->type.t = VT_VOID;
4347 if (tok == TOK_IF) {
4348 /* if test */
4349 next();
4350 skip('(');
4351 gexpr();
4352 skip(')');
4353 a = gtst(1, 0);
4354 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4355 c = tok;
4356 if (c == TOK_ELSE) {
4357 next();
4358 d = gjmp(0);
4359 gsym(a);
4360 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4361 gsym(d); /* patch else jmp */
4362 } else
4363 gsym(a);
4364 } else if (tok == TOK_WHILE) {
4365 next();
4366 d = ind;
4367 skip('(');
4368 gexpr();
4369 skip(')');
4370 a = gtst(1, 0);
4371 b = 0;
4372 block(&a, &b, case_sym, def_sym, case_reg, 0);
4373 gjmp_addr(d);
4374 gsym(a);
4375 gsym_addr(b, d);
4376 } else if (tok == '{') {
4377 Sym *llabel;
4379 next();
4380 /* record local declaration stack position */
4381 s = local_stack;
4382 llabel = local_label_stack;
4383 /* handle local labels declarations */
4384 if (tok == TOK_LABEL) {
4385 next();
4386 for(;;) {
4387 if (tok < TOK_UIDENT)
4388 expect("label identifier");
4389 label_push(&local_label_stack, tok, LABEL_DECLARED);
4390 next();
4391 if (tok == ',') {
4392 next();
4393 } else {
4394 skip(';');
4395 break;
4399 while (tok != '}') {
4400 label_or_decl(VT_LOCAL);
4401 if (tok != '}') {
4402 if (is_expr)
4403 vpop();
4404 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4407 /* pop locally defined labels */
4408 label_pop(&local_label_stack, llabel);
4409 if(is_expr) {
4410 /* XXX: this solution makes only valgrind happy...
4411 triggered by gcc.c-torture/execute/20000917-1.c */
4412 Sym *p;
4413 switch(vtop->type.t & VT_BTYPE) {
4414 case VT_PTR:
4415 case VT_STRUCT:
4416 case VT_ENUM:
4417 case VT_FUNC:
4418 for(p=vtop->type.ref;p;p=p->prev)
4419 if(p->prev==s)
4420 tcc_error("unsupported expression type");
4423 /* pop locally defined symbols */
4424 sym_pop(&local_stack, s);
4425 next();
4426 } else if (tok == TOK_RETURN) {
4427 next();
4428 if (tok != ';') {
4429 gexpr();
4430 gen_assign_cast(&func_vt);
4431 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4432 CType type;
4433 /* if returning structure, must copy it to implicit
4434 first pointer arg location */
4435 #ifdef TCC_ARM_EABI
4436 int align, size;
4437 size = type_size(&func_vt,&align);
4438 if(size <= 4)
4440 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4441 && (align & 3))
4443 int addr;
4444 loc = (loc - size) & -4;
4445 addr = loc;
4446 type = func_vt;
4447 vset(&type, VT_LOCAL | VT_LVAL, addr);
4448 vswap();
4449 vstore();
4450 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4452 vtop->type = int_type;
4453 gv(RC_IRET);
4454 } else {
4455 #endif
4456 type = func_vt;
4457 mk_pointer(&type);
4458 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4459 indir();
4460 vswap();
4461 /* copy structure value to pointer */
4462 vstore();
4463 #ifdef TCC_ARM_EABI
4465 #endif
4466 } else if (is_float(func_vt.t)) {
4467 gv(rc_fret(func_vt.t));
4468 } else {
4469 gv(RC_IRET);
4471 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4473 skip(';');
4474 rsym = gjmp(rsym); /* jmp */
4475 } else if (tok == TOK_BREAK) {
4476 /* compute jump */
4477 if (!bsym)
4478 tcc_error("cannot break");
4479 *bsym = gjmp(*bsym);
4480 next();
4481 skip(';');
4482 } else if (tok == TOK_CONTINUE) {
4483 /* compute jump */
4484 if (!csym)
4485 tcc_error("cannot continue");
4486 *csym = gjmp(*csym);
4487 next();
4488 skip(';');
4489 } else if (tok == TOK_FOR) {
4490 int e;
4491 next();
4492 skip('(');
4493 s = local_stack;
4494 if (tok != ';') {
4495 /* c99 for-loop init decl? */
4496 if (!decl0(VT_LOCAL, 1)) {
4497 /* no, regular for-loop init expr */
4498 gexpr();
4499 vpop();
4502 skip(';');
4503 d = ind;
4504 c = ind;
4505 a = 0;
4506 b = 0;
4507 if (tok != ';') {
4508 gexpr();
4509 a = gtst(1, 0);
4511 skip(';');
4512 if (tok != ')') {
4513 e = gjmp(0);
4514 c = ind;
4515 gexpr();
4516 vpop();
4517 gjmp_addr(d);
4518 gsym(e);
4520 skip(')');
4521 block(&a, &b, case_sym, def_sym, case_reg, 0);
4522 gjmp_addr(c);
4523 gsym(a);
4524 gsym_addr(b, c);
4525 sym_pop(&local_stack, s);
4526 } else
4527 if (tok == TOK_DO) {
4528 next();
4529 a = 0;
4530 b = 0;
4531 d = ind;
4532 block(&a, &b, case_sym, def_sym, case_reg, 0);
4533 skip(TOK_WHILE);
4534 skip('(');
4535 gsym(b);
4536 gexpr();
4537 c = gtst(0, 0);
4538 gsym_addr(c, d);
4539 skip(')');
4540 gsym(a);
4541 skip(';');
4542 } else
4543 if (tok == TOK_SWITCH) {
4544 next();
4545 skip('(');
4546 gexpr();
4547 /* XXX: other types than integer */
4548 case_reg = gv(RC_INT);
4549 vpop();
4550 skip(')');
4551 a = 0;
4552 b = gjmp(0); /* jump to first case */
4553 c = 0;
4554 block(&a, csym, &b, &c, case_reg, 0);
4555 /* if no default, jmp after switch */
4556 if (c == 0)
4557 c = ind;
4558 /* default label */
4559 gsym_addr(b, c);
4560 /* break label */
4561 gsym(a);
4562 } else
4563 if (tok == TOK_CASE) {
4564 int v1, v2;
4565 if (!case_sym)
4566 expect("switch");
4567 next();
4568 v1 = expr_const();
4569 v2 = v1;
4570 if (gnu_ext && tok == TOK_DOTS) {
4571 next();
4572 v2 = expr_const();
4573 if (v2 < v1)
4574 tcc_warning("empty case range");
4576 /* since a case is like a label, we must skip it with a jmp */
4577 b = gjmp(0);
4578 gsym(*case_sym);
4579 vseti(case_reg, 0);
4580 vpushi(v1);
4581 if (v1 == v2) {
4582 gen_op(TOK_EQ);
4583 *case_sym = gtst(1, 0);
4584 } else {
4585 gen_op(TOK_GE);
4586 *case_sym = gtst(1, 0);
4587 vseti(case_reg, 0);
4588 vpushi(v2);
4589 gen_op(TOK_LE);
4590 *case_sym = gtst(1, *case_sym);
4592 gsym(b);
4593 skip(':');
4594 is_expr = 0;
4595 goto block_after_label;
4596 } else
4597 if (tok == TOK_DEFAULT) {
4598 next();
4599 skip(':');
4600 if (!def_sym)
4601 expect("switch");
4602 if (*def_sym)
4603 tcc_error("too many 'default'");
4604 *def_sym = ind;
4605 is_expr = 0;
4606 goto block_after_label;
4607 } else
4608 if (tok == TOK_GOTO) {
4609 next();
4610 if (tok == '*' && gnu_ext) {
4611 /* computed goto */
4612 next();
4613 gexpr();
4614 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4615 expect("pointer");
4616 ggoto();
4617 } else if (tok >= TOK_UIDENT) {
4618 s = label_find(tok);
4619 /* put forward definition if needed */
4620 if (!s) {
4621 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4622 } else {
4623 if (s->r == LABEL_DECLARED)
4624 s->r = LABEL_FORWARD;
4626 /* label already defined */
4627 if (s->r & LABEL_FORWARD)
4628 s->jnext = gjmp(s->jnext);
4629 else
4630 gjmp_addr(s->jnext);
4631 next();
4632 } else {
4633 expect("label identifier");
4635 skip(';');
4636 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4637 asm_instr();
4638 } else {
4639 b = is_label();
4640 if (b) {
4641 /* label case */
4642 s = label_find(b);
4643 if (s) {
4644 if (s->r == LABEL_DEFINED)
4645 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4646 gsym(s->jnext);
4647 s->r = LABEL_DEFINED;
4648 } else {
4649 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4651 s->jnext = ind;
4652 /* we accept this, but it is a mistake */
4653 block_after_label:
4654 if (tok == '}') {
4655 tcc_warning("deprecated use of label at end of compound statement");
4656 } else {
4657 if (is_expr)
4658 vpop();
4659 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4661 } else {
4662 /* expression case */
4663 if (tok != ';') {
4664 if (is_expr) {
4665 vpop();
4666 gexpr();
4667 } else {
4668 gexpr();
4669 vpop();
4672 skip(';');
4677 /* t is the array or struct type. c is the array or struct
4678 address. cur_index/cur_field is the pointer to the current
4679 value. 'size_only' is true if only size info is needed (only used
4680 in arrays) */
4681 static void decl_designator(CType *type, Section *sec, unsigned long c,
4682 int *cur_index, Sym **cur_field,
4683 int size_only)
4685 Sym *s, *f;
4686 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4687 CType type1;
4689 notfirst = 0;
4690 elem_size = 0;
4691 nb_elems = 1;
4692 if (gnu_ext && (l = is_label()) != 0)
4693 goto struct_field;
4694 while (tok == '[' || tok == '.') {
4695 if (tok == '[') {
4696 if (!(type->t & VT_ARRAY))
4697 expect("array type");
4698 s = type->ref;
4699 next();
4700 index = expr_const();
4701 if (index < 0 || (s->c >= 0 && index >= s->c))
4702 expect("invalid index");
4703 if (tok == TOK_DOTS && gnu_ext) {
4704 next();
4705 index_last = expr_const();
4706 if (index_last < 0 ||
4707 (s->c >= 0 && index_last >= s->c) ||
4708 index_last < index)
4709 expect("invalid index");
4710 } else {
4711 index_last = index;
4713 skip(']');
4714 if (!notfirst)
4715 *cur_index = index_last;
4716 type = pointed_type(type);
4717 elem_size = type_size(type, &align);
4718 c += index * elem_size;
4719 /* NOTE: we only support ranges for last designator */
4720 nb_elems = index_last - index + 1;
4721 if (nb_elems != 1) {
4722 notfirst = 1;
4723 break;
4725 } else {
4726 next();
4727 l = tok;
4728 next();
4729 struct_field:
4730 if ((type->t & VT_BTYPE) != VT_STRUCT)
4731 expect("struct/union type");
4732 s = type->ref;
4733 l |= SYM_FIELD;
4734 f = s->next;
4735 while (f) {
4736 if (f->v == l)
4737 break;
4738 f = f->next;
4740 if (!f)
4741 expect("field");
4742 if (!notfirst)
4743 *cur_field = f;
4744 /* XXX: fix this mess by using explicit storage field */
4745 type1 = f->type;
4746 type1.t |= (type->t & ~VT_TYPE);
4747 type = &type1;
4748 c += f->c;
4750 notfirst = 1;
4752 if (notfirst) {
4753 if (tok == '=') {
4754 next();
4755 } else {
4756 if (!gnu_ext)
4757 expect("=");
4759 } else {
4760 if (type->t & VT_ARRAY) {
4761 index = *cur_index;
4762 type = pointed_type(type);
4763 c += index * type_size(type, &align);
4764 } else {
4765 f = *cur_field;
4766 if (!f)
4767 tcc_error("too many field init");
4768 /* XXX: fix this mess by using explicit storage field */
4769 type1 = f->type;
4770 type1.t |= (type->t & ~VT_TYPE);
4771 type = &type1;
4772 c += f->c;
4775 decl_initializer(type, sec, c, 0, size_only);
4777 /* XXX: make it more general */
4778 if (!size_only && nb_elems > 1) {
4779 unsigned long c_end;
4780 uint8_t *src, *dst;
4781 int i;
4783 if (!sec)
4784 tcc_error("range init not supported yet for dynamic storage");
4785 c_end = c + nb_elems * elem_size;
4786 if (c_end > sec->data_allocated)
4787 section_realloc(sec, c_end);
4788 src = sec->data + c;
4789 dst = src;
4790 for(i = 1; i < nb_elems; i++) {
4791 dst += elem_size;
4792 memcpy(dst, src, elem_size);
4797 #define EXPR_VAL 0
4798 #define EXPR_CONST 1
4799 #define EXPR_ANY 2
4801 /* store a value or an expression directly in global data or in local array */
4802 static void init_putv(CType *type, Section *sec, unsigned long c,
4803 int v, int expr_type)
4805 int saved_global_expr, bt, bit_pos, bit_size;
4806 void *ptr;
4807 unsigned long long bit_mask;
4808 CType dtype;
4810 switch(expr_type) {
4811 case EXPR_VAL:
4812 vpushi(v);
4813 break;
4814 case EXPR_CONST:
4815 /* compound literals must be allocated globally in this case */
4816 saved_global_expr = global_expr;
4817 global_expr = 1;
4818 expr_const1();
4819 global_expr = saved_global_expr;
4820 /* NOTE: symbols are accepted */
4821 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4822 tcc_error("initializer element is not constant");
4823 break;
4824 case EXPR_ANY:
4825 expr_eq();
4826 break;
4829 dtype = *type;
4830 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4832 if (sec) {
4833 /* XXX: not portable */
4834 /* XXX: generate error if incorrect relocation */
4835 gen_assign_cast(&dtype);
4836 bt = type->t & VT_BTYPE;
4837 /* we'll write at most 12 bytes */
4838 if (c + 12 > sec->data_allocated) {
4839 section_realloc(sec, c + 12);
4841 ptr = sec->data + c;
4842 /* XXX: make code faster ? */
4843 if (!(type->t & VT_BITFIELD)) {
4844 bit_pos = 0;
4845 bit_size = 32;
4846 bit_mask = -1LL;
4847 } else {
4848 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4849 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4850 bit_mask = (1LL << bit_size) - 1;
4852 if ((vtop->r & VT_SYM) &&
4853 (bt == VT_BYTE ||
4854 bt == VT_SHORT ||
4855 bt == VT_DOUBLE ||
4856 bt == VT_LDOUBLE ||
4857 bt == VT_LLONG ||
4858 (bt == VT_INT && bit_size != 32)))
4859 tcc_error("initializer element is not computable at load time");
4860 switch(bt) {
4861 case VT_BOOL:
4862 vtop->c.i = (vtop->c.i != 0);
4863 case VT_BYTE:
4864 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4865 break;
4866 case VT_SHORT:
4867 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4868 break;
4869 case VT_DOUBLE:
4870 *(double *)ptr = vtop->c.d;
4871 break;
4872 case VT_LDOUBLE:
4873 *(long double *)ptr = vtop->c.ld;
4874 break;
4875 case VT_LLONG:
4876 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4877 break;
4878 default:
4879 if (vtop->r & VT_SYM) {
4880 greloc(sec, vtop->sym, c, R_DATA_PTR);
4882 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4883 break;
4885 vtop--;
4886 } else {
4887 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4888 vswap();
4889 vstore();
4890 vpop();
4894 /* put zeros for variable based init */
4895 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4897 if (sec) {
4898 /* nothing to do because globals are already set to zero */
4899 } else {
4900 vpush_global_sym(&func_old_type, TOK_memset);
4901 vseti(VT_LOCAL, c);
4902 vpushi(0);
4903 vpushi(size);
4904 gfunc_call(3);
4908 /* 't' contains the type and storage info. 'c' is the offset of the
4909 object in section 'sec'. If 'sec' is NULL, it means stack based
4910 allocation. 'first' is true if array '{' must be read (multi
4911 dimension implicit array init handling). 'size_only' is true if
4912 size only evaluation is wanted (only for arrays). */
4913 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4914 int first, int size_only)
4916 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
4917 int size1, align1, expr_type;
4918 Sym *s, *f;
4919 CType *t1;
4921 if (type->t & VT_VLA) {
4922 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4923 int a;
4924 CValue retcval;
4926 vpush_global_sym(&func_old_type, TOK_alloca);
4927 vla_runtime_type_size(type, &a);
4928 gfunc_call(1);
4930 /* return value */
4931 retcval.i = 0;
4932 vsetc(type, REG_IRET, &retcval);
4933 vset(type, VT_LOCAL|VT_LVAL, c);
4934 vswap();
4935 vstore();
4936 vpop();
4937 #else
4938 tcc_error("variable length arrays unsupported for this target");
4939 #endif
4940 } else if (type->t & VT_ARRAY) {
4941 s = type->ref;
4942 n = s->c;
4943 array_length = 0;
4944 t1 = pointed_type(type);
4945 size1 = type_size(t1, &align1);
4947 no_oblock = 1;
4948 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4949 tok == '{') {
4950 if (tok != '{')
4951 tcc_error("character array initializer must be a literal,"
4952 " optionally enclosed in braces");
4953 skip('{');
4954 no_oblock = 0;
4957 /* only parse strings here if correct type (otherwise: handle
4958 them as ((w)char *) expressions */
4959 if ((tok == TOK_LSTR &&
4960 #ifdef TCC_TARGET_PE
4961 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4962 #else
4963 (t1->t & VT_BTYPE) == VT_INT
4964 #endif
4965 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4966 while (tok == TOK_STR || tok == TOK_LSTR) {
4967 int cstr_len, ch;
4968 CString *cstr;
4970 cstr = tokc.cstr;
4971 /* compute maximum number of chars wanted */
4972 if (tok == TOK_STR)
4973 cstr_len = cstr->size;
4974 else
4975 cstr_len = cstr->size / sizeof(nwchar_t);
4976 cstr_len--;
4977 nb = cstr_len;
4978 if (n >= 0 && nb > (n - array_length))
4979 nb = n - array_length;
4980 if (!size_only) {
4981 if (cstr_len > nb)
4982 tcc_warning("initializer-string for array is too long");
4983 /* in order to go faster for common case (char
4984 string in global variable, we handle it
4985 specifically */
4986 if (sec && tok == TOK_STR && size1 == 1) {
4987 memcpy(sec->data + c + array_length, cstr->data, nb);
4988 } else {
4989 for(i=0;i<nb;i++) {
4990 if (tok == TOK_STR)
4991 ch = ((unsigned char *)cstr->data)[i];
4992 else
4993 ch = ((nwchar_t *)cstr->data)[i];
4994 init_putv(t1, sec, c + (array_length + i) * size1,
4995 ch, EXPR_VAL);
4999 array_length += nb;
5000 next();
5002 /* only add trailing zero if enough storage (no
5003 warning in this case since it is standard) */
5004 if (n < 0 || array_length < n) {
5005 if (!size_only) {
5006 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5008 array_length++;
5010 } else {
5011 index = 0;
5012 while (tok != '}') {
5013 decl_designator(type, sec, c, &index, NULL, size_only);
5014 if (n >= 0 && index >= n)
5015 tcc_error("index too large");
5016 /* must put zero in holes (note that doing it that way
5017 ensures that it even works with designators) */
5018 if (!size_only && array_length < index) {
5019 init_putz(t1, sec, c + array_length * size1,
5020 (index - array_length) * size1);
5022 index++;
5023 if (index > array_length)
5024 array_length = index;
5025 /* special test for multi dimensional arrays (may not
5026 be strictly correct if designators are used at the
5027 same time) */
5028 if (index >= n && no_oblock)
5029 break;
5030 if (tok == '}')
5031 break;
5032 skip(',');
5035 if (!no_oblock)
5036 skip('}');
5037 /* put zeros at the end */
5038 if (!size_only && n >= 0 && array_length < n) {
5039 init_putz(t1, sec, c + array_length * size1,
5040 (n - array_length) * size1);
5042 /* patch type size if needed */
5043 if (n < 0)
5044 s->c = array_length;
5045 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5046 (sec || !first || tok == '{')) {
5047 int par_count;
5049 /* NOTE: the previous test is a specific case for automatic
5050 struct/union init */
5051 /* XXX: union needs only one init */
5053 /* XXX: this test is incorrect for local initializers
5054 beginning with ( without {. It would be much more difficult
5055 to do it correctly (ideally, the expression parser should
5056 be used in all cases) */
5057 par_count = 0;
5058 if (tok == '(') {
5059 AttributeDef ad1;
5060 CType type1;
5061 next();
5062 while (tok == '(') {
5063 par_count++;
5064 next();
5066 if (!parse_btype(&type1, &ad1))
5067 expect("cast");
5068 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5069 #if 0
5070 if (!is_assignable_types(type, &type1))
5071 tcc_error("invalid type for cast");
5072 #endif
5073 skip(')');
5075 no_oblock = 1;
5076 if (first || tok == '{') {
5077 skip('{');
5078 no_oblock = 0;
5080 s = type->ref;
5081 f = s->next;
5082 array_length = 0;
5083 index = 0;
5084 n = s->c;
5085 while (tok != '}') {
5086 decl_designator(type, sec, c, NULL, &f, size_only);
5087 index = f->c;
5088 if (!size_only && array_length < index) {
5089 init_putz(type, sec, c + array_length,
5090 index - array_length);
5092 index = index + type_size(&f->type, &align1);
5093 if (index > array_length)
5094 array_length = index;
5096 /* gr: skip fields from same union - ugly. */
5097 while (f->next) {
5098 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5099 /* test for same offset */
5100 if (f->next->c != f->c)
5101 break;
5102 /* if yes, test for bitfield shift */
5103 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5104 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5105 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5106 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5107 if (bit_pos_1 != bit_pos_2)
5108 break;
5110 f = f->next;
5113 f = f->next;
5114 if (no_oblock && f == NULL)
5115 break;
5116 if (tok == '}')
5117 break;
5118 skip(',');
5120 /* put zeros at the end */
5121 if (!size_only && array_length < n) {
5122 init_putz(type, sec, c + array_length,
5123 n - array_length);
5125 if (!no_oblock)
5126 skip('}');
5127 while (par_count) {
5128 skip(')');
5129 par_count--;
5131 } else if (tok == '{') {
5132 next();
5133 decl_initializer(type, sec, c, first, size_only);
5134 skip('}');
5135 } else if (size_only) {
5136 /* just skip expression */
5137 parlevel = parlevel1 = 0;
5138 while ((parlevel > 0 || parlevel1 > 0 ||
5139 (tok != '}' && tok != ',')) && tok != -1) {
5140 if (tok == '(')
5141 parlevel++;
5142 else if (tok == ')')
5143 parlevel--;
5144 else if (tok == '{')
5145 parlevel1++;
5146 else if (tok == '}')
5147 parlevel1--;
5148 next();
5150 } else {
5151 /* currently, we always use constant expression for globals
5152 (may change for scripting case) */
5153 expr_type = EXPR_CONST;
5154 if (!sec)
5155 expr_type = EXPR_ANY;
5156 init_putv(type, sec, c, 0, expr_type);
5160 /* parse an initializer for type 't' if 'has_init' is non zero, and
5161 allocate space in local or global data space ('r' is either
5162 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5163 variable 'v' with an associated name represented by 'asm_label' of
5164 scope 'scope' is declared before initializers are parsed. If 'v' is
5165 zero, then a reference to the new object is put in the value stack.
5166 If 'has_init' is 2, a special parsing is done to handle string
5167 constants. */
5168 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5169 int has_init, int v, char *asm_label,
5170 int scope)
5172 int size, align, addr, data_offset;
5173 int level;
5174 ParseState saved_parse_state = {0};
5175 TokenString init_str;
5176 Section *sec;
5177 Sym *flexible_array;
5179 flexible_array = NULL;
5180 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5181 Sym *field;
5182 field = type->ref;
5183 while (field && field->next)
5184 field = field->next;
5185 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5186 flexible_array = field;
5189 size = type_size(type, &align);
5190 /* If unknown size, we must evaluate it before
5191 evaluating initializers because
5192 initializers can generate global data too
5193 (e.g. string pointers or ISOC99 compound
5194 literals). It also simplifies local
5195 initializers handling */
5196 tok_str_new(&init_str);
5197 if (size < 0 || (flexible_array && has_init)) {
5198 if (!has_init)
5199 tcc_error("unknown type size");
5200 /* get all init string */
5201 if (has_init == 2) {
5202 /* only get strings */
5203 while (tok == TOK_STR || tok == TOK_LSTR) {
5204 tok_str_add_tok(&init_str);
5205 next();
5207 } else {
5208 level = 0;
5209 while (level > 0 || (tok != ',' && tok != ';')) {
5210 if (tok < 0)
5211 tcc_error("unexpected end of file in initializer");
5212 tok_str_add_tok(&init_str);
5213 if (tok == '{')
5214 level++;
5215 else if (tok == '}') {
5216 level--;
5217 if (level <= 0) {
5218 next();
5219 break;
5222 next();
5225 tok_str_add(&init_str, -1);
5226 tok_str_add(&init_str, 0);
5228 /* compute size */
5229 save_parse_state(&saved_parse_state);
5231 macro_ptr = init_str.str;
5232 next();
5233 decl_initializer(type, NULL, 0, 1, 1);
5234 /* prepare second initializer parsing */
5235 macro_ptr = init_str.str;
5236 next();
5238 /* if still unknown size, error */
5239 size = type_size(type, &align);
5240 if (size < 0)
5241 tcc_error("unknown type size");
5243 if (flexible_array)
5244 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5245 /* take into account specified alignment if bigger */
5246 if (ad->aligned) {
5247 if (ad->aligned > align)
5248 align = ad->aligned;
5249 } else if (ad->packed) {
5250 align = 1;
5252 if ((r & VT_VALMASK) == VT_LOCAL) {
5253 sec = NULL;
5254 #ifdef CONFIG_TCC_BCHECK
5255 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5256 loc--;
5258 #endif
5259 loc = (loc - size) & -align;
5260 addr = loc;
5261 #ifdef CONFIG_TCC_BCHECK
5262 /* handles bounds */
5263 /* XXX: currently, since we do only one pass, we cannot track
5264 '&' operators, so we add only arrays */
5265 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5266 unsigned long *bounds_ptr;
5267 /* add padding between regions */
5268 loc--;
5269 /* then add local bound info */
5270 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5271 bounds_ptr[0] = addr;
5272 bounds_ptr[1] = size;
5274 #endif
5275 if (v) {
5276 /* local variable */
5277 sym_push(v, type, r, addr);
5278 } else {
5279 /* push local reference */
5280 vset(type, r, addr);
5282 } else {
5283 Sym *sym;
5285 sym = NULL;
5286 if (v && scope == VT_CONST) {
5287 /* see if the symbol was already defined */
5288 sym = sym_find(v);
5289 if (sym) {
5290 if (!is_compatible_types(&sym->type, type))
5291 tcc_error("incompatible types for redefinition of '%s'",
5292 get_tok_str(v, NULL));
5293 if (sym->type.t & VT_EXTERN) {
5294 /* if the variable is extern, it was not allocated */
5295 sym->type.t &= ~VT_EXTERN;
5296 /* set array size if it was ommited in extern
5297 declaration */
5298 if ((sym->type.t & VT_ARRAY) &&
5299 sym->type.ref->c < 0 &&
5300 type->ref->c >= 0)
5301 sym->type.ref->c = type->ref->c;
5302 } else {
5303 /* we accept several definitions of the same
5304 global variable. this is tricky, because we
5305 must play with the SHN_COMMON type of the symbol */
5306 /* XXX: should check if the variable was already
5307 initialized. It is incorrect to initialized it
5308 twice */
5309 /* no init data, we won't add more to the symbol */
5310 if (!has_init)
5311 goto no_alloc;
5316 /* allocate symbol in corresponding section */
5317 sec = ad->section;
5318 if (!sec) {
5319 if (has_init)
5320 sec = data_section;
5321 else if (tcc_state->nocommon)
5322 sec = bss_section;
5324 if (sec) {
5325 data_offset = sec->data_offset;
5326 data_offset = (data_offset + align - 1) & -align;
5327 addr = data_offset;
5328 /* very important to increment global pointer at this time
5329 because initializers themselves can create new initializers */
5330 data_offset += size;
5331 #ifdef CONFIG_TCC_BCHECK
5332 /* add padding if bound check */
5333 if (tcc_state->do_bounds_check)
5334 data_offset++;
5335 #endif
5336 sec->data_offset = data_offset;
5337 /* allocate section space to put the data */
5338 if (sec->sh_type != SHT_NOBITS &&
5339 data_offset > sec->data_allocated)
5340 section_realloc(sec, data_offset);
5341 /* align section if needed */
5342 if (align > sec->sh_addralign)
5343 sec->sh_addralign = align;
5344 } else {
5345 addr = 0; /* avoid warning */
5348 if (v) {
5349 if (scope != VT_CONST || !sym) {
5350 sym = sym_push(v, type, r | VT_SYM, 0);
5351 sym->asm_label = asm_label;
5353 /* update symbol definition */
5354 if (sec) {
5355 put_extern_sym(sym, sec, addr, size);
5356 } else {
5357 ElfW(Sym) *esym;
5358 /* put a common area */
5359 put_extern_sym(sym, NULL, align, size);
5360 /* XXX: find a nicer way */
5361 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5362 esym->st_shndx = SHN_COMMON;
5364 } else {
5365 CValue cval;
5367 /* push global reference */
5368 sym = get_sym_ref(type, sec, addr, size);
5369 cval.ul = 0;
5370 vsetc(type, VT_CONST | VT_SYM, &cval);
5371 vtop->sym = sym;
5373 /* patch symbol weakness */
5374 if (type->t & VT_WEAK)
5375 weaken_symbol(sym);
5376 #ifdef CONFIG_TCC_BCHECK
5377 /* handles bounds now because the symbol must be defined
5378 before for the relocation */
5379 if (tcc_state->do_bounds_check) {
5380 unsigned long *bounds_ptr;
5382 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5383 /* then add global bound info */
5384 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5385 bounds_ptr[0] = 0; /* relocated */
5386 bounds_ptr[1] = size;
5388 #endif
5390 if (has_init || (type->t & VT_VLA)) {
5391 decl_initializer(type, sec, addr, 1, 0);
5392 /* restore parse state if needed */
5393 if (init_str.str) {
5394 tok_str_free(init_str.str);
5395 restore_parse_state(&saved_parse_state);
5397 /* patch flexible array member size back to -1, */
5398 /* for possible subsequent similar declarations */
5399 if (flexible_array)
5400 flexible_array->type.ref->c = -1;
5402 no_alloc: ;
5405 static void put_func_debug(Sym *sym)
5407 char buf[512];
5409 /* stabs info */
5410 /* XXX: we put here a dummy type */
5411 snprintf(buf, sizeof(buf), "%s:%c1",
5412 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5413 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5414 cur_text_section, sym->c);
5415 /* //gr gdb wants a line at the function */
5416 put_stabn(N_SLINE, 0, file->line_num, 0);
5417 last_ind = 0;
5418 last_line_num = 0;
5421 /* parse an old style function declaration list */
5422 /* XXX: check multiple parameter */
5423 static void func_decl_list(Sym *func_sym)
5425 AttributeDef ad;
5426 int v;
5427 Sym *s;
5428 CType btype, type;
5430 /* parse each declaration */
5431 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5432 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5433 if (!parse_btype(&btype, &ad))
5434 expect("declaration list");
5435 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5436 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5437 tok == ';') {
5438 /* we accept no variable after */
5439 } else {
5440 for(;;) {
5441 type = btype;
5442 type_decl(&type, &ad, &v, TYPE_DIRECT);
5443 /* find parameter in function parameter list */
5444 s = func_sym->next;
5445 while (s != NULL) {
5446 if ((s->v & ~SYM_FIELD) == v)
5447 goto found;
5448 s = s->next;
5450 tcc_error("declaration for parameter '%s' but no such parameter",
5451 get_tok_str(v, NULL));
5452 found:
5453 /* check that no storage specifier except 'register' was given */
5454 if (type.t & VT_STORAGE)
5455 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5456 convert_parameter_type(&type);
5457 /* we can add the type (NOTE: it could be local to the function) */
5458 s->type = type;
5459 /* accept other parameters */
5460 if (tok == ',')
5461 next();
5462 else
5463 break;
5466 skip(';');
5470 /* parse a function defined by symbol 'sym' and generate its code in
5471 'cur_text_section' */
5472 static void gen_function(Sym *sym)
5474 int saved_nocode_wanted = nocode_wanted;
5475 nocode_wanted = 0;
5476 ind = cur_text_section->data_offset;
5477 /* NOTE: we patch the symbol size later */
5478 put_extern_sym(sym, cur_text_section, ind, 0);
5479 funcname = get_tok_str(sym->v, NULL);
5480 func_ind = ind;
5481 /* put debug symbol */
5482 if (tcc_state->do_debug)
5483 put_func_debug(sym);
5484 /* push a dummy symbol to enable local sym storage */
5485 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5486 gfunc_prolog(&sym->type);
5487 rsym = 0;
5488 block(NULL, NULL, NULL, NULL, 0, 0);
5489 gsym(rsym);
5490 gfunc_epilog();
5491 cur_text_section->data_offset = ind;
5492 label_pop(&global_label_stack, NULL);
5493 sym_pop(&local_stack, NULL); /* reset local stack */
5494 /* end of function */
5495 /* patch symbol size */
5496 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5497 ind - func_ind;
5498 /* patch symbol weakness (this definition overrules any prototype) */
5499 if (sym->type.t & VT_WEAK)
5500 weaken_symbol(sym);
5501 if (tcc_state->do_debug) {
5502 put_stabn(N_FUN, 0, 0, ind - func_ind);
5504 /* It's better to crash than to generate wrong code */
5505 cur_text_section = NULL;
5506 funcname = ""; /* for safety */
5507 func_vt.t = VT_VOID; /* for safety */
5508 ind = 0; /* for safety */
5509 nocode_wanted = saved_nocode_wanted;
5512 ST_FUNC void gen_inline_functions(void)
5514 Sym *sym;
5515 int *str, inline_generated, i;
5516 struct InlineFunc *fn;
5518 /* iterate while inline function are referenced */
5519 for(;;) {
5520 inline_generated = 0;
5521 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5522 fn = tcc_state->inline_fns[i];
5523 sym = fn->sym;
5524 if (sym && sym->c) {
5525 /* the function was used: generate its code and
5526 convert it to a normal function */
5527 str = fn->token_str;
5528 fn->sym = NULL;
5529 if (file)
5530 strcpy(file->filename, fn->filename);
5531 sym->r = VT_SYM | VT_CONST;
5532 sym->type.t &= ~VT_INLINE;
5534 macro_ptr = str;
5535 next();
5536 cur_text_section = text_section;
5537 gen_function(sym);
5538 macro_ptr = NULL; /* fail safe */
5540 inline_generated = 1;
5543 if (!inline_generated)
5544 break;
5546 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5547 fn = tcc_state->inline_fns[i];
5548 str = fn->token_str;
5549 tok_str_free(str);
5551 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5554 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5555 static int decl0(int l, int is_for_loop_init)
5557 int v, has_init, r;
5558 CType type, btype;
5559 Sym *sym;
5560 AttributeDef ad;
5562 while (1) {
5563 if (!parse_btype(&btype, &ad)) {
5564 if (is_for_loop_init)
5565 return 0;
5566 /* skip redundant ';' */
5567 /* XXX: find more elegant solution */
5568 if (tok == ';') {
5569 next();
5570 continue;
5572 if (l == VT_CONST &&
5573 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5574 /* global asm block */
5575 asm_global_instr();
5576 continue;
5578 /* special test for old K&R protos without explicit int
5579 type. Only accepted when defining global data */
5580 if (l == VT_LOCAL || tok < TOK_DEFINE)
5581 break;
5582 btype.t = VT_INT;
5584 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5585 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5586 tok == ';') {
5587 /* we accept no variable after */
5588 next();
5589 continue;
5591 while (1) { /* iterate thru each declaration */
5592 char *asm_label; // associated asm label
5593 type = btype;
5594 type_decl(&type, &ad, &v, TYPE_DIRECT);
5595 #if 0
5597 char buf[500];
5598 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5599 printf("type = '%s'\n", buf);
5601 #endif
5602 if ((type.t & VT_BTYPE) == VT_FUNC) {
5603 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5604 tcc_error("function without file scope cannot be static");
5606 /* if old style function prototype, we accept a
5607 declaration list */
5608 sym = type.ref;
5609 if (sym->c == FUNC_OLD)
5610 func_decl_list(sym);
5613 asm_label = NULL;
5614 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5615 CString astr;
5617 asm_label_instr(&astr);
5618 asm_label = tcc_strdup(astr.data);
5619 cstr_free(&astr);
5621 /* parse one last attribute list, after asm label */
5622 parse_attribute(&ad);
5625 if (ad.weak)
5626 type.t |= VT_WEAK;
5627 #ifdef TCC_TARGET_PE
5628 if (ad.func_import)
5629 type.t |= VT_IMPORT;
5630 if (ad.func_export)
5631 type.t |= VT_EXPORT;
5632 #endif
5633 if (tok == '{') {
5634 if (l == VT_LOCAL)
5635 tcc_error("cannot use local functions");
5636 if ((type.t & VT_BTYPE) != VT_FUNC)
5637 expect("function definition");
5639 /* reject abstract declarators in function definition */
5640 sym = type.ref;
5641 while ((sym = sym->next) != NULL)
5642 if (!(sym->v & ~SYM_FIELD))
5643 expect("identifier");
5645 /* XXX: cannot do better now: convert extern line to static inline */
5646 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5647 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5649 sym = sym_find(v);
5650 if (sym) {
5651 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5652 goto func_error1;
5654 r = sym->type.ref->r;
5655 /* use func_call from prototype if not defined */
5656 if (FUNC_CALL(r) != FUNC_CDECL
5657 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5658 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5660 /* use export from prototype */
5661 if (FUNC_EXPORT(r))
5662 FUNC_EXPORT(type.ref->r) = 1;
5664 /* use static from prototype */
5665 if (sym->type.t & VT_STATIC)
5666 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5668 if (!is_compatible_types(&sym->type, &type)) {
5669 func_error1:
5670 tcc_error("incompatible types for redefinition of '%s'",
5671 get_tok_str(v, NULL));
5673 /* if symbol is already defined, then put complete type */
5674 sym->type = type;
5675 } else {
5676 /* put function symbol */
5677 sym = global_identifier_push(v, type.t, 0);
5678 sym->type.ref = type.ref;
5681 /* static inline functions are just recorded as a kind
5682 of macro. Their code will be emitted at the end of
5683 the compilation unit only if they are used */
5684 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5685 (VT_INLINE | VT_STATIC)) {
5686 TokenString func_str;
5687 int block_level;
5688 struct InlineFunc *fn;
5689 const char *filename;
5691 tok_str_new(&func_str);
5693 block_level = 0;
5694 for(;;) {
5695 int t;
5696 if (tok == TOK_EOF)
5697 tcc_error("unexpected end of file");
5698 tok_str_add_tok(&func_str);
5699 t = tok;
5700 next();
5701 if (t == '{') {
5702 block_level++;
5703 } else if (t == '}') {
5704 block_level--;
5705 if (block_level == 0)
5706 break;
5709 tok_str_add(&func_str, -1);
5710 tok_str_add(&func_str, 0);
5711 filename = file ? file->filename : "";
5712 fn = tcc_malloc(sizeof *fn + strlen(filename));
5713 strcpy(fn->filename, filename);
5714 fn->sym = sym;
5715 fn->token_str = func_str.str;
5716 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5718 } else {
5719 /* compute text section */
5720 cur_text_section = ad.section;
5721 if (!cur_text_section)
5722 cur_text_section = text_section;
5723 sym->r = VT_SYM | VT_CONST;
5724 gen_function(sym);
5726 break;
5727 } else {
5728 if (btype.t & VT_TYPEDEF) {
5729 /* save typedefed type */
5730 /* XXX: test storage specifiers ? */
5731 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5732 sym->type.t |= VT_TYPEDEF;
5733 } else {
5734 r = 0;
5735 if ((type.t & VT_BTYPE) == VT_FUNC) {
5736 /* external function definition */
5737 /* specific case for func_call attribute */
5738 type.ref->r = INT_ATTR(&ad);
5739 } else if (!(type.t & VT_ARRAY)) {
5740 /* not lvalue if array */
5741 r |= lvalue_type(type.t);
5743 has_init = (tok == '=');
5744 if (has_init && (type.t & VT_VLA))
5745 tcc_error("Variable length array cannot be initialized");
5746 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5747 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5748 !has_init && l == VT_CONST && type.ref->c < 0)) {
5749 /* external variable or function */
5750 /* NOTE: as GCC, uninitialized global static
5751 arrays of null size are considered as
5752 extern */
5753 sym = external_sym(v, &type, r, asm_label);
5755 if (type.t & VT_WEAK)
5756 weaken_symbol(sym);
5758 if (ad.alias_target) {
5759 Section tsec;
5760 Elf32_Sym *esym;
5761 Sym *alias_target;
5763 alias_target = sym_find(ad.alias_target);
5764 if (!alias_target || !alias_target->c)
5765 tcc_error("unsupported forward __alias__ attribute");
5766 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5767 tsec.sh_num = esym->st_shndx;
5768 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5770 } else {
5771 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5772 if (type.t & VT_STATIC)
5773 r |= VT_CONST;
5774 else
5775 r |= l;
5776 if (has_init)
5777 next();
5778 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5781 if (tok != ',') {
5782 if (is_for_loop_init)
5783 return 1;
5784 skip(';');
5785 break;
5787 next();
5789 ad.aligned = 0;
5792 return 0;
5795 ST_FUNC void decl(int l)
5797 decl0(l, 0);