Patch attempting to build OSX TinyCC.
[tinycc/miki.git] / tccgen.c
blob3cf37155a47cdb9dcd0660c98fa11cabf7aeebe7
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 /********************************************************/
24 /* global variables */
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
31 ST_DATA int rsym, anon_sym, ind, loc;
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *define_stack;
54 ST_DATA Sym *global_label_stack;
55 ST_DATA Sym *local_label_stack;
57 ST_DATA SValue vstack[VSTACK_SIZE], *vtop;
59 ST_DATA int const_wanted; /* true if constant wanted */
60 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
61 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
62 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
63 ST_DATA int func_vc;
64 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
65 ST_DATA char *funcname;
67 ST_DATA CType char_pointer_type, func_old_type, int_type;
69 /* ------------------------------------------------------------------------- */
70 static void gen_cast(CType *type);
71 static inline CType *pointed_type(CType *type);
72 static int is_compatible_types(CType *type1, CType *type2);
73 static int parse_btype(CType *type, AttributeDef *ad);
74 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
75 static void parse_expr_type(CType *type);
76 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
77 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
78 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
79 static int decl0(int l, int is_for_loop_init);
80 static void expr_eq(void);
81 static void unary_type(CType *type);
82 static void vla_runtime_type_size(CType *type, int *a);
83 static int is_compatible_parameter_types(CType *type1, CType *type2);
84 static void expr_type(CType *type);
86 ST_INLN int is_float(int t)
88 int bt;
89 bt = t & VT_BTYPE;
90 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
93 /* 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 long long constant */
329 static void vpushll(long long v)
331 CValue cval;
332 CType ctype;
333 ctype.t = VT_LLONG;
334 ctype.ref = 0;
335 cval.ull = v;
336 vsetc(&ctype, VT_CONST, &cval);
339 /* push arbitrary 64bit constant */
340 void vpush64(int ty, unsigned long long v)
342 CValue cval;
343 CType ctype;
344 ctype.t = ty;
345 cval.ull = v;
346 vsetc(&ctype, VT_CONST, &cval);
349 /* Return a static symbol pointing to a section */
350 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
352 int v;
353 Sym *sym;
355 v = anon_sym++;
356 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
357 sym->type.ref = type->ref;
358 sym->r = VT_CONST | VT_SYM;
359 put_extern_sym(sym, sec, offset, size);
360 return sym;
363 /* push a reference to a section offset by adding a dummy symbol */
364 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
366 CValue cval;
368 cval.ul = 0;
369 vsetc(type, VT_CONST | VT_SYM, &cval);
370 vtop->sym = get_sym_ref(type, sec, offset, size);
373 /* define a new external reference to a symbol 'v' of type 'u' */
374 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
376 Sym *s;
378 s = sym_find(v);
379 if (!s) {
380 /* push forward reference */
381 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
382 s->type.ref = type->ref;
383 s->r = r | VT_CONST | VT_SYM;
385 return s;
388 /* define a new external reference to a symbol 'v' with alternate asm
389 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
390 is no alternate name (most cases) */
391 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
393 Sym *s;
395 s = sym_find(v);
396 if (!s) {
397 /* push forward reference */
398 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
399 s->asm_label = asm_label;
400 s->type.t |= VT_EXTERN;
401 } else if (s->type.ref == func_old_type.ref) {
402 s->type.ref = type->ref;
403 s->r = r | VT_CONST | VT_SYM;
404 s->type.t |= VT_EXTERN;
405 } else if (!is_compatible_types(&s->type, type)) {
406 tcc_error("incompatible types for redefinition of '%s'",
407 get_tok_str(v, NULL));
409 return s;
412 /* push a reference to global symbol v */
413 ST_FUNC void vpush_global_sym(CType *type, int v)
415 Sym *sym;
416 CValue cval;
418 sym = external_global_sym(v, type, 0);
419 cval.ul = 0;
420 vsetc(type, VT_CONST | VT_SYM, &cval);
421 vtop->sym = sym;
424 ST_FUNC void vset(CType *type, int r, int v)
426 CValue cval;
428 cval.i = v;
429 vsetc(type, r, &cval);
432 static void vseti(int r, int v)
434 CType type;
435 type.t = VT_INT;
436 type.ref = 0;
437 vset(&type, r, v);
440 ST_FUNC void vswap(void)
442 SValue tmp;
444 tmp = vtop[0];
445 vtop[0] = vtop[-1];
446 vtop[-1] = tmp;
449 ST_FUNC void vpushv(SValue *v)
451 if (vtop >= vstack + (VSTACK_SIZE - 1))
452 tcc_error("memory full");
453 vtop++;
454 *vtop = *v;
457 static void vdup(void)
459 vpushv(vtop);
462 /* save r to the memory stack, and mark it as being free */
463 ST_FUNC void save_reg(int r)
465 int l, saved, size, align;
466 SValue *p, sv;
467 CType *type;
469 /* modify all stack values */
470 saved = 0;
471 l = 0;
472 for(p=vstack;p<=vtop;p++) {
473 if ((p->r & VT_VALMASK) == r ||
474 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
475 /* must save value on stack if not already done */
476 if (!saved) {
477 /* NOTE: must reload 'r' because r might be equal to r2 */
478 r = p->r & VT_VALMASK;
479 /* store register in the stack */
480 type = &p->type;
481 if ((p->r & VT_LVAL) ||
482 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
483 #ifdef TCC_TARGET_X86_64
484 type = &char_pointer_type;
485 #else
486 type = &int_type;
487 #endif
488 size = type_size(type, &align);
489 loc = (loc - size) & -align;
490 sv.type.t = type->t;
491 sv.r = VT_LOCAL | VT_LVAL;
492 sv.c.ul = loc;
493 store(r, &sv);
494 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
495 /* x86 specific: need to pop fp register ST0 if saved */
496 if (r == TREG_ST0) {
497 o(0xd8dd); /* fstp %st(0) */
499 #endif
500 #ifndef TCC_TARGET_X86_64
501 /* special long long case */
502 if ((type->t & VT_BTYPE) == VT_LLONG) {
503 sv.c.ul += 4;
504 store(p->r2, &sv);
506 #endif
507 l = loc;
508 saved = 1;
510 /* mark that stack entry as being saved on the stack */
511 if (p->r & VT_LVAL) {
512 /* also clear the bounded flag because the
513 relocation address of the function was stored in
514 p->c.ul */
515 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
516 } else {
517 p->r = lvalue_type(p->type.t) | VT_LOCAL;
519 p->r2 = VT_CONST;
520 p->c.ul = l;
525 #ifdef TCC_TARGET_ARM
526 /* find a register of class 'rc2' with at most one reference on stack.
527 * If none, call get_reg(rc) */
528 ST_FUNC int get_reg_ex(int rc, int rc2)
530 int r;
531 SValue *p;
533 for(r=0;r<NB_REGS;r++) {
534 if (reg_classes[r] & rc2) {
535 int n;
536 n=0;
537 for(p = vstack; p <= vtop; p++) {
538 if ((p->r & VT_VALMASK) == r ||
539 (p->r2 & VT_VALMASK) == r)
540 n++;
542 if (n <= 1)
543 return r;
546 return get_reg(rc);
548 #endif
550 /* find a free register of class 'rc'. If none, save one register */
551 ST_FUNC int get_reg(int rc)
553 int r;
554 SValue *p;
556 /* find a free register */
557 for(r=0;r<NB_REGS;r++) {
558 if (reg_classes[r] & rc) {
559 for(p=vstack;p<=vtop;p++) {
560 if ((p->r & VT_VALMASK) == r ||
561 (p->r2 & VT_VALMASK) == r)
562 goto notfound;
564 return r;
566 notfound: ;
569 /* no register left : free the first one on the stack (VERY
570 IMPORTANT to start from the bottom to ensure that we don't
571 spill registers used in gen_opi()) */
572 for(p=vstack;p<=vtop;p++) {
573 r = p->r & VT_VALMASK;
574 if (r < VT_CONST && (reg_classes[r] & rc))
575 goto save_found;
576 /* also look at second register (if long long) */
577 r = p->r2 & VT_VALMASK;
578 if (r < VT_CONST && (reg_classes[r] & rc)) {
579 save_found:
580 save_reg(r);
581 return r;
584 /* Should never comes here */
585 return -1;
588 /* save registers up to (vtop - n) stack entry */
589 ST_FUNC void save_regs(int n)
591 int r;
592 SValue *p, *p1;
593 p1 = vtop - n;
594 for(p = vstack;p <= p1; p++) {
595 r = p->r & VT_VALMASK;
596 if (r < VT_CONST) {
597 save_reg(r);
602 /* move register 's' to 'r', and flush previous value of r to memory
603 if needed */
604 static void move_reg(int r, int s)
606 SValue sv;
608 if (r != s) {
609 save_reg(r);
610 sv.type.t = VT_INT;
611 sv.r = s;
612 sv.c.ul = 0;
613 load(r, &sv);
617 /* get address of vtop (vtop MUST BE an lvalue) */
618 static void gaddrof(void)
620 if (vtop->r & VT_REF)
621 gv(RC_INT);
622 vtop->r &= ~VT_LVAL;
623 /* tricky: if saved lvalue, then we can go back to lvalue */
624 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
625 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
630 #ifdef CONFIG_TCC_BCHECK
631 /* generate lvalue bound code */
632 static void gbound(void)
634 int lval_type;
635 CType type1;
637 vtop->r &= ~VT_MUSTBOUND;
638 /* if lvalue, then use checking code before dereferencing */
639 if (vtop->r & VT_LVAL) {
640 /* if not VT_BOUNDED value, then make one */
641 if (!(vtop->r & VT_BOUNDED)) {
642 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
643 /* must save type because we must set it to int to get pointer */
644 type1 = vtop->type;
645 vtop->type.t = VT_INT;
646 gaddrof();
647 vpushi(0);
648 gen_bounded_ptr_add();
649 vtop->r |= lval_type;
650 vtop->type = type1;
652 /* then check for dereferencing */
653 gen_bounded_ptr_deref();
656 #endif
658 /* store vtop a register belonging to class 'rc'. lvalues are
659 converted to values. Cannot be used if cannot be converted to
660 register value (such as structures). */
661 ST_FUNC int gv(int rc)
663 int r, bit_pos, bit_size, size, align, i;
664 #ifndef TCC_TARGET_X86_64
665 int rc2;
666 #endif
668 /* NOTE: get_reg can modify vstack[] */
669 if (vtop->type.t & VT_BITFIELD) {
670 CType type;
671 int bits = 32;
672 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
673 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
674 /* remove bit field info to avoid loops */
675 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
676 /* cast to int to propagate signedness in following ops */
677 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
678 type.t = VT_LLONG;
679 bits = 64;
680 } else
681 type.t = VT_INT;
682 if((vtop->type.t & VT_UNSIGNED) ||
683 (vtop->type.t & VT_BTYPE) == VT_BOOL)
684 type.t |= VT_UNSIGNED;
685 gen_cast(&type);
686 /* generate shifts */
687 vpushi(bits - (bit_pos + bit_size));
688 gen_op(TOK_SHL);
689 vpushi(bits - bit_size);
690 /* NOTE: transformed to SHR if unsigned */
691 gen_op(TOK_SAR);
692 r = gv(rc);
693 } else {
694 if (is_float(vtop->type.t) &&
695 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
696 Sym *sym;
697 int *ptr;
698 unsigned long offset;
699 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
700 CValue check;
701 #endif
703 /* XXX: unify with initializers handling ? */
704 /* CPUs usually cannot use float constants, so we store them
705 generically in data segment */
706 size = type_size(&vtop->type, &align);
707 offset = (data_section->data_offset + align - 1) & -align;
708 data_section->data_offset = offset;
709 /* XXX: not portable yet */
710 #if defined(__i386__) || defined(__x86_64__)
711 /* Zero pad x87 tenbyte long doubles */
712 if (size == LDOUBLE_SIZE) {
713 vtop->c.tab[2] &= 0xffff;
714 #if LDOUBLE_SIZE == 16
715 vtop->c.tab[3] = 0;
716 #endif
718 #endif
719 ptr = section_ptr_add(data_section, size);
720 size = size >> 2;
721 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
722 check.d = 1;
723 if(check.tab[0])
724 for(i=0;i<size;i++)
725 ptr[i] = vtop->c.tab[size-1-i];
726 else
727 #endif
728 for(i=0;i<size;i++)
729 ptr[i] = vtop->c.tab[i];
730 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
731 vtop->r |= VT_LVAL | VT_SYM;
732 vtop->sym = sym;
733 vtop->c.ul = 0;
735 #ifdef CONFIG_TCC_BCHECK
736 if (vtop->r & VT_MUSTBOUND)
737 gbound();
738 #endif
740 r = vtop->r & VT_VALMASK;
741 #ifndef TCC_TARGET_X86_64
742 rc2 = RC_INT;
743 if (rc == RC_IRET)
744 rc2 = RC_LRET;
745 #endif
746 /* need to reload if:
747 - constant
748 - lvalue (need to dereference pointer)
749 - already a register, but not in the right class */
750 if (r >= VT_CONST
751 || (vtop->r & VT_LVAL)
752 || !(reg_classes[r] & rc)
753 #ifndef TCC_TARGET_X86_64
754 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
755 #endif
758 r = get_reg(rc);
759 #ifndef TCC_TARGET_X86_64
760 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
761 int r2;
762 unsigned long long ll;
763 /* two register type load : expand to two words
764 temporarily */
765 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
766 /* load constant */
767 ll = vtop->c.ull;
768 vtop->c.ui = ll; /* first word */
769 load(r, vtop);
770 vtop->r = r; /* save register value */
771 vpushi(ll >> 32); /* second word */
772 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
773 (vtop->r & VT_LVAL)) {
774 /* We do not want to modifier the long long
775 pointer here, so the safest (and less
776 efficient) is to save all the other registers
777 in the stack. XXX: totally inefficient. */
778 save_regs(1);
779 /* load from memory */
780 load(r, vtop);
781 vdup();
782 vtop[-1].r = r; /* save register value */
783 /* increment pointer to get second word */
784 vtop->type.t = VT_INT;
785 gaddrof();
786 vpushi(4);
787 gen_op('+');
788 vtop->r |= VT_LVAL;
789 } else {
790 /* move registers */
791 load(r, vtop);
792 vdup();
793 vtop[-1].r = r; /* save register value */
794 vtop->r = vtop[-1].r2;
796 /* allocate second register */
797 r2 = get_reg(rc2);
798 load(r2, vtop);
799 vpop();
800 /* write second register */
801 vtop->r2 = r2;
802 } else
803 #endif
804 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
805 int t1, t;
806 /* lvalue of scalar type : need to use lvalue type
807 because of possible cast */
808 t = vtop->type.t;
809 t1 = t;
810 /* compute memory access type */
811 if (vtop->r & VT_LVAL_BYTE)
812 t = VT_BYTE;
813 else if (vtop->r & VT_LVAL_SHORT)
814 t = VT_SHORT;
815 if (vtop->r & VT_LVAL_UNSIGNED)
816 t |= VT_UNSIGNED;
817 vtop->type.t = t;
818 load(r, vtop);
819 /* restore wanted type */
820 vtop->type.t = t1;
821 } else {
822 /* one register type load */
823 load(r, vtop);
826 vtop->r = r;
827 #ifdef TCC_TARGET_C67
828 /* uses register pairs for doubles */
829 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
830 vtop->r2 = r+1;
831 #endif
833 return r;
836 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
837 ST_FUNC void gv2(int rc1, int rc2)
839 int v;
841 /* generate more generic register first. But VT_JMP or VT_CMP
842 values must be generated first in all cases to avoid possible
843 reload errors */
844 v = vtop[0].r & VT_VALMASK;
845 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
846 vswap();
847 gv(rc1);
848 vswap();
849 gv(rc2);
850 /* test if reload is needed for first register */
851 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
852 vswap();
853 gv(rc1);
854 vswap();
856 } else {
857 gv(rc2);
858 vswap();
859 gv(rc1);
860 vswap();
861 /* test if reload is needed for first register */
862 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
863 gv(rc2);
868 /* wrapper around RC_FRET to return a register by type */
869 static int rc_fret(int t)
871 #ifdef TCC_TARGET_X86_64
872 if (t == VT_LDOUBLE) {
873 return RC_ST0;
875 #endif
876 return RC_FRET;
879 /* wrapper around REG_FRET to return a register by type */
880 static int reg_fret(int t)
882 #ifdef TCC_TARGET_X86_64
883 if (t == VT_LDOUBLE) {
884 return TREG_ST0;
886 #endif
887 return REG_FRET;
890 /* expand long long on stack in two int registers */
891 static void lexpand(void)
893 int u;
895 u = vtop->type.t & VT_UNSIGNED;
896 gv(RC_INT);
897 vdup();
898 vtop[0].r = vtop[-1].r2;
899 vtop[0].r2 = VT_CONST;
900 vtop[-1].r2 = VT_CONST;
901 vtop[0].type.t = VT_INT | u;
902 vtop[-1].type.t = VT_INT | u;
905 #ifdef TCC_TARGET_ARM
906 /* expand long long on stack */
907 ST_FUNC void lexpand_nr(void)
909 int u,v;
911 u = vtop->type.t & VT_UNSIGNED;
912 vdup();
913 vtop->r2 = VT_CONST;
914 vtop->type.t = VT_INT | u;
915 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
916 if (v == VT_CONST) {
917 vtop[-1].c.ui = vtop->c.ull;
918 vtop->c.ui = vtop->c.ull >> 32;
919 vtop->r = VT_CONST;
920 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
921 vtop->c.ui += 4;
922 vtop->r = vtop[-1].r;
923 } else if (v > VT_CONST) {
924 vtop--;
925 lexpand();
926 } else
927 vtop->r = vtop[-1].r2;
928 vtop[-1].r2 = VT_CONST;
929 vtop[-1].type.t = VT_INT | u;
931 #endif
933 /* build a long long from two ints */
934 static void lbuild(int t)
936 gv2(RC_INT, RC_INT);
937 vtop[-1].r2 = vtop[0].r;
938 vtop[-1].type.t = t;
939 vpop();
942 /* rotate n first stack elements to the bottom
943 I1 ... In -> I2 ... In I1 [top is right]
945 static void vrotb(int n)
947 int i;
948 SValue tmp;
950 tmp = vtop[-n + 1];
951 for(i=-n+1;i!=0;i++)
952 vtop[i] = vtop[i+1];
953 vtop[0] = tmp;
956 /* rotate n first stack elements to the top
957 I1 ... In -> In I1 ... I(n-1) [top is right]
959 ST_FUNC void vrott(int n)
961 int i;
962 SValue tmp;
964 tmp = vtop[0];
965 for(i = 0;i < n - 1; i++)
966 vtop[-i] = vtop[-i - 1];
967 vtop[-n + 1] = tmp;
970 #ifdef TCC_TARGET_ARM
971 /* like vrott but in other direction
972 In ... I1 -> I(n-1) ... I1 In [top is right]
974 ST_FUNC void vnrott(int n)
976 int i;
977 SValue tmp;
979 tmp = vtop[-n + 1];
980 for(i = n - 1; i > 0; i--)
981 vtop[-i] = vtop[-i + 1];
982 vtop[0] = tmp;
984 #endif
986 /* pop stack value */
987 ST_FUNC void vpop(void)
989 int v;
990 v = vtop->r & VT_VALMASK;
991 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
992 /* for x86, we need to pop the FP stack */
993 if (v == TREG_ST0 && !nocode_wanted) {
994 o(0xd8dd); /* fstp %st(0) */
995 } else
996 #endif
997 if (v == VT_JMP || v == VT_JMPI) {
998 /* need to put correct jump if && or || without test */
999 gsym(vtop->c.ul);
1001 vtop--;
1004 /* convert stack entry to register and duplicate its value in another
1005 register */
1006 static void gv_dup(void)
1008 int rc, t, r, r1;
1009 SValue sv;
1011 t = vtop->type.t;
1012 if ((t & VT_BTYPE) == VT_LLONG) {
1013 lexpand();
1014 gv_dup();
1015 vswap();
1016 vrotb(3);
1017 gv_dup();
1018 vrotb(4);
1019 /* stack: H L L1 H1 */
1020 lbuild(t);
1021 vrotb(3);
1022 vrotb(3);
1023 vswap();
1024 lbuild(t);
1025 vswap();
1026 } else {
1027 /* duplicate value */
1028 rc = RC_INT;
1029 sv.type.t = VT_INT;
1030 if (is_float(t)) {
1031 rc = RC_FLOAT;
1032 #ifdef TCC_TARGET_X86_64
1033 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1034 rc = RC_ST0;
1036 #endif
1037 sv.type.t = t;
1039 r = gv(rc);
1040 r1 = get_reg(rc);
1041 sv.r = r;
1042 sv.c.ul = 0;
1043 load(r1, &sv); /* move r to r1 */
1044 vdup();
1045 /* duplicates value */
1046 if (r != r1)
1047 vtop->r = r1;
1051 #ifndef TCC_TARGET_X86_64
1052 /* generate CPU independent (unsigned) long long operations */
1053 static void gen_opl(int op)
1055 int t, a, b, op1, c, i;
1056 int func;
1057 unsigned short reg_iret = REG_IRET;
1058 unsigned short reg_lret = REG_LRET;
1059 SValue tmp;
1061 switch(op) {
1062 case '/':
1063 case TOK_PDIV:
1064 func = TOK___divdi3;
1065 goto gen_func;
1066 case TOK_UDIV:
1067 func = TOK___udivdi3;
1068 goto gen_func;
1069 case '%':
1070 func = TOK___moddi3;
1071 goto gen_mod_func;
1072 case TOK_UMOD:
1073 func = TOK___umoddi3;
1074 gen_mod_func:
1075 #ifdef TCC_ARM_EABI
1076 reg_iret = TREG_R2;
1077 reg_lret = TREG_R3;
1078 #endif
1079 gen_func:
1080 /* call generic long long function */
1081 vpush_global_sym(&func_old_type, func);
1082 vrott(3);
1083 gfunc_call(2);
1084 vpushi(0);
1085 vtop->r = reg_iret;
1086 vtop->r2 = reg_lret;
1087 break;
1088 case '^':
1089 case '&':
1090 case '|':
1091 case '*':
1092 case '+':
1093 case '-':
1094 t = vtop->type.t;
1095 vswap();
1096 lexpand();
1097 vrotb(3);
1098 lexpand();
1099 /* stack: L1 H1 L2 H2 */
1100 tmp = vtop[0];
1101 vtop[0] = vtop[-3];
1102 vtop[-3] = tmp;
1103 tmp = vtop[-2];
1104 vtop[-2] = vtop[-3];
1105 vtop[-3] = tmp;
1106 vswap();
1107 /* stack: H1 H2 L1 L2 */
1108 if (op == '*') {
1109 vpushv(vtop - 1);
1110 vpushv(vtop - 1);
1111 gen_op(TOK_UMULL);
1112 lexpand();
1113 /* stack: H1 H2 L1 L2 ML MH */
1114 for(i=0;i<4;i++)
1115 vrotb(6);
1116 /* stack: ML MH H1 H2 L1 L2 */
1117 tmp = vtop[0];
1118 vtop[0] = vtop[-2];
1119 vtop[-2] = tmp;
1120 /* stack: ML MH H1 L2 H2 L1 */
1121 gen_op('*');
1122 vrotb(3);
1123 vrotb(3);
1124 gen_op('*');
1125 /* stack: ML MH M1 M2 */
1126 gen_op('+');
1127 gen_op('+');
1128 } else if (op == '+' || op == '-') {
1129 /* XXX: add non carry method too (for MIPS or alpha) */
1130 if (op == '+')
1131 op1 = TOK_ADDC1;
1132 else
1133 op1 = TOK_SUBC1;
1134 gen_op(op1);
1135 /* stack: H1 H2 (L1 op L2) */
1136 vrotb(3);
1137 vrotb(3);
1138 gen_op(op1 + 1); /* TOK_xxxC2 */
1139 } else {
1140 gen_op(op);
1141 /* stack: H1 H2 (L1 op L2) */
1142 vrotb(3);
1143 vrotb(3);
1144 /* stack: (L1 op L2) H1 H2 */
1145 gen_op(op);
1146 /* stack: (L1 op L2) (H1 op H2) */
1148 /* stack: L H */
1149 lbuild(t);
1150 break;
1151 case TOK_SAR:
1152 case TOK_SHR:
1153 case TOK_SHL:
1154 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1155 t = vtop[-1].type.t;
1156 vswap();
1157 lexpand();
1158 vrotb(3);
1159 /* stack: L H shift */
1160 c = (int)vtop->c.i;
1161 /* constant: simpler */
1162 /* NOTE: all comments are for SHL. the other cases are
1163 done by swaping words */
1164 vpop();
1165 if (op != TOK_SHL)
1166 vswap();
1167 if (c >= 32) {
1168 /* stack: L H */
1169 vpop();
1170 if (c > 32) {
1171 vpushi(c - 32);
1172 gen_op(op);
1174 if (op != TOK_SAR) {
1175 vpushi(0);
1176 } else {
1177 gv_dup();
1178 vpushi(31);
1179 gen_op(TOK_SAR);
1181 vswap();
1182 } else {
1183 vswap();
1184 gv_dup();
1185 /* stack: H L L */
1186 vpushi(c);
1187 gen_op(op);
1188 vswap();
1189 vpushi(32 - c);
1190 if (op == TOK_SHL)
1191 gen_op(TOK_SHR);
1192 else
1193 gen_op(TOK_SHL);
1194 vrotb(3);
1195 /* stack: L L H */
1196 vpushi(c);
1197 if (op == TOK_SHL)
1198 gen_op(TOK_SHL);
1199 else
1200 gen_op(TOK_SHR);
1201 gen_op('|');
1203 if (op != TOK_SHL)
1204 vswap();
1205 lbuild(t);
1206 } else {
1207 /* XXX: should provide a faster fallback on x86 ? */
1208 switch(op) {
1209 case TOK_SAR:
1210 func = TOK___ashrdi3;
1211 goto gen_func;
1212 case TOK_SHR:
1213 func = TOK___lshrdi3;
1214 goto gen_func;
1215 case TOK_SHL:
1216 func = TOK___ashldi3;
1217 goto gen_func;
1220 break;
1221 default:
1222 /* compare operations */
1223 t = vtop->type.t;
1224 vswap();
1225 lexpand();
1226 vrotb(3);
1227 lexpand();
1228 /* stack: L1 H1 L2 H2 */
1229 tmp = vtop[-1];
1230 vtop[-1] = vtop[-2];
1231 vtop[-2] = tmp;
1232 /* stack: L1 L2 H1 H2 */
1233 /* compare high */
1234 op1 = op;
1235 /* when values are equal, we need to compare low words. since
1236 the jump is inverted, we invert the test too. */
1237 if (op1 == TOK_LT)
1238 op1 = TOK_LE;
1239 else if (op1 == TOK_GT)
1240 op1 = TOK_GE;
1241 else if (op1 == TOK_ULT)
1242 op1 = TOK_ULE;
1243 else if (op1 == TOK_UGT)
1244 op1 = TOK_UGE;
1245 a = 0;
1246 b = 0;
1247 gen_op(op1);
1248 if (op1 != TOK_NE) {
1249 a = gtst(1, 0);
1251 if (op != TOK_EQ) {
1252 /* generate non equal test */
1253 /* XXX: NOT PORTABLE yet */
1254 if (a == 0) {
1255 b = gtst(0, 0);
1256 } else {
1257 #if defined(TCC_TARGET_I386)
1258 b = psym(0x850f, 0);
1259 #elif defined(TCC_TARGET_ARM)
1260 b = ind;
1261 o(0x1A000000 | encbranch(ind, 0, 1));
1262 #elif defined(TCC_TARGET_C67)
1263 tcc_error("not implemented");
1264 #else
1265 #error not supported
1266 #endif
1269 /* compare low. Always unsigned */
1270 op1 = op;
1271 if (op1 == TOK_LT)
1272 op1 = TOK_ULT;
1273 else if (op1 == TOK_LE)
1274 op1 = TOK_ULE;
1275 else if (op1 == TOK_GT)
1276 op1 = TOK_UGT;
1277 else if (op1 == TOK_GE)
1278 op1 = TOK_UGE;
1279 gen_op(op1);
1280 a = gtst(1, a);
1281 gsym(b);
1282 vseti(VT_JMPI, a);
1283 break;
1286 #endif
1288 /* handle integer constant optimizations and various machine
1289 independent opt */
1290 static void gen_opic(int op)
1292 int c1, c2, t1, t2, n;
1293 SValue *v1, *v2;
1294 long long l1, l2;
1295 typedef unsigned long long U;
1297 v1 = vtop - 1;
1298 v2 = vtop;
1299 t1 = v1->type.t & VT_BTYPE;
1300 t2 = v2->type.t & VT_BTYPE;
1302 if (t1 == VT_LLONG)
1303 l1 = v1->c.ll;
1304 else if (v1->type.t & VT_UNSIGNED)
1305 l1 = v1->c.ui;
1306 else
1307 l1 = v1->c.i;
1309 if (t2 == VT_LLONG)
1310 l2 = v2->c.ll;
1311 else if (v2->type.t & VT_UNSIGNED)
1312 l2 = v2->c.ui;
1313 else
1314 l2 = v2->c.i;
1316 /* currently, we cannot do computations with forward symbols */
1317 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1318 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1319 if (c1 && c2) {
1320 switch(op) {
1321 case '+': l1 += l2; break;
1322 case '-': l1 -= l2; break;
1323 case '&': l1 &= l2; break;
1324 case '^': l1 ^= l2; break;
1325 case '|': l1 |= l2; break;
1326 case '*': l1 *= l2; break;
1328 case TOK_PDIV:
1329 case '/':
1330 case '%':
1331 case TOK_UDIV:
1332 case TOK_UMOD:
1333 /* if division by zero, generate explicit division */
1334 if (l2 == 0) {
1335 if (const_wanted)
1336 tcc_error("division by zero in constant");
1337 goto general_case;
1339 switch(op) {
1340 default: l1 /= l2; break;
1341 case '%': l1 %= l2; break;
1342 case TOK_UDIV: l1 = (U)l1 / l2; break;
1343 case TOK_UMOD: l1 = (U)l1 % l2; break;
1345 break;
1346 case TOK_SHL: l1 <<= l2; break;
1347 case TOK_SHR: l1 = (U)l1 >> l2; break;
1348 case TOK_SAR: l1 >>= l2; break;
1349 /* tests */
1350 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1351 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1352 case TOK_EQ: l1 = l1 == l2; break;
1353 case TOK_NE: l1 = l1 != l2; break;
1354 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1355 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1356 case TOK_LT: l1 = l1 < l2; break;
1357 case TOK_GE: l1 = l1 >= l2; break;
1358 case TOK_LE: l1 = l1 <= l2; break;
1359 case TOK_GT: l1 = l1 > l2; break;
1360 /* logical */
1361 case TOK_LAND: l1 = l1 && l2; break;
1362 case TOK_LOR: l1 = l1 || l2; break;
1363 default:
1364 goto general_case;
1366 v1->c.ll = l1;
1367 vtop--;
1368 } else {
1369 /* if commutative ops, put c2 as constant */
1370 if (c1 && (op == '+' || op == '&' || op == '^' ||
1371 op == '|' || op == '*')) {
1372 vswap();
1373 c2 = c1; //c = c1, c1 = c2, c2 = c;
1374 l2 = l1; //l = l1, l1 = l2, l2 = l;
1376 /* Filter out NOP operations like x*1, x-0, x&-1... */
1377 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1378 op == TOK_PDIV) &&
1379 l2 == 1) ||
1380 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1381 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1382 l2 == 0) ||
1383 (op == '&' &&
1384 l2 == -1))) {
1385 /* nothing to do */
1386 vtop--;
1387 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1388 /* try to use shifts instead of muls or divs */
1389 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1390 n = -1;
1391 while (l2) {
1392 l2 >>= 1;
1393 n++;
1395 vtop->c.ll = n;
1396 if (op == '*')
1397 op = TOK_SHL;
1398 else if (op == TOK_PDIV)
1399 op = TOK_SAR;
1400 else
1401 op = TOK_SHR;
1403 goto general_case;
1404 } else if (c2 && (op == '+' || op == '-') &&
1405 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1406 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1407 /* symbol + constant case */
1408 if (op == '-')
1409 l2 = -l2;
1410 vtop--;
1411 vtop->c.ll += l2;
1412 } else {
1413 general_case:
1414 if (!nocode_wanted) {
1415 /* call low level op generator */
1416 if (t1 == VT_LLONG || t2 == VT_LLONG)
1417 gen_opl(op);
1418 else
1419 gen_opi(op);
1420 } else {
1421 vtop--;
1427 /* generate a floating point operation with constant propagation */
1428 static void gen_opif(int op)
1430 int c1, c2;
1431 SValue *v1, *v2;
1432 long double f1, f2;
1434 v1 = vtop - 1;
1435 v2 = vtop;
1436 /* currently, we cannot do computations with forward symbols */
1437 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1438 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1439 if (c1 && c2) {
1440 if (v1->type.t == VT_FLOAT) {
1441 f1 = v1->c.f;
1442 f2 = v2->c.f;
1443 } else if (v1->type.t == VT_DOUBLE) {
1444 f1 = v1->c.d;
1445 f2 = v2->c.d;
1446 } else {
1447 f1 = v1->c.ld;
1448 f2 = v2->c.ld;
1451 /* NOTE: we only do constant propagation if finite number (not
1452 NaN or infinity) (ANSI spec) */
1453 if (!ieee_finite(f1) || !ieee_finite(f2))
1454 goto general_case;
1456 switch(op) {
1457 case '+': f1 += f2; break;
1458 case '-': f1 -= f2; break;
1459 case '*': f1 *= f2; break;
1460 case '/':
1461 if (f2 == 0.0) {
1462 if (const_wanted)
1463 tcc_error("division by zero in constant");
1464 goto general_case;
1466 f1 /= f2;
1467 break;
1468 /* XXX: also handles tests ? */
1469 default:
1470 goto general_case;
1472 /* XXX: overflow test ? */
1473 if (v1->type.t == VT_FLOAT) {
1474 v1->c.f = f1;
1475 } else if (v1->type.t == VT_DOUBLE) {
1476 v1->c.d = f1;
1477 } else {
1478 v1->c.ld = f1;
1480 vtop--;
1481 } else {
1482 general_case:
1483 if (!nocode_wanted) {
1484 gen_opf(op);
1485 } else {
1486 vtop--;
1491 static int pointed_size(CType *type)
1493 int align;
1494 return type_size(pointed_type(type), &align);
1497 static void vla_runtime_pointed_size(CType *type)
1499 int align;
1500 vla_runtime_type_size(pointed_type(type), &align);
1503 static inline int is_null_pointer(SValue *p)
1505 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1506 return 0;
1507 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1508 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1511 static inline int is_integer_btype(int bt)
1513 return (bt == VT_BYTE || bt == VT_SHORT ||
1514 bt == VT_INT || bt == VT_LLONG);
1517 /* check types for comparison or substraction of pointers */
1518 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1520 CType *type1, *type2, tmp_type1, tmp_type2;
1521 int bt1, bt2;
1523 /* null pointers are accepted for all comparisons as gcc */
1524 if (is_null_pointer(p1) || is_null_pointer(p2))
1525 return;
1526 type1 = &p1->type;
1527 type2 = &p2->type;
1528 bt1 = type1->t & VT_BTYPE;
1529 bt2 = type2->t & VT_BTYPE;
1530 /* accept comparison between pointer and integer with a warning */
1531 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1532 if (op != TOK_LOR && op != TOK_LAND )
1533 tcc_warning("comparison between pointer and integer");
1534 return;
1537 /* both must be pointers or implicit function pointers */
1538 if (bt1 == VT_PTR) {
1539 type1 = pointed_type(type1);
1540 } else if (bt1 != VT_FUNC)
1541 goto invalid_operands;
1543 if (bt2 == VT_PTR) {
1544 type2 = pointed_type(type2);
1545 } else if (bt2 != VT_FUNC) {
1546 invalid_operands:
1547 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1549 if ((type1->t & VT_BTYPE) == VT_VOID ||
1550 (type2->t & VT_BTYPE) == VT_VOID)
1551 return;
1552 tmp_type1 = *type1;
1553 tmp_type2 = *type2;
1554 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1555 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1556 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1557 /* gcc-like error if '-' is used */
1558 if (op == '-')
1559 goto invalid_operands;
1560 else
1561 tcc_warning("comparison of distinct pointer types lacks a cast");
1565 /* generic gen_op: handles types problems */
1566 ST_FUNC void gen_op(int op)
1568 int u, t1, t2, bt1, bt2, t;
1569 CType type1;
1571 t1 = vtop[-1].type.t;
1572 t2 = vtop[0].type.t;
1573 bt1 = t1 & VT_BTYPE;
1574 bt2 = t2 & VT_BTYPE;
1576 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1577 /* at least one operand is a pointer */
1578 /* relationnal op: must be both pointers */
1579 if (op >= TOK_ULT && op <= TOK_LOR) {
1580 check_comparison_pointer_types(vtop - 1, vtop, op);
1581 /* pointers are handled are unsigned */
1582 #ifdef TCC_TARGET_X86_64
1583 t = VT_LLONG | VT_UNSIGNED;
1584 #else
1585 t = VT_INT | VT_UNSIGNED;
1586 #endif
1587 goto std_op;
1589 /* if both pointers, then it must be the '-' op */
1590 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1591 if (op != '-')
1592 tcc_error("cannot use pointers here");
1593 check_comparison_pointer_types(vtop - 1, vtop, op);
1594 /* XXX: check that types are compatible */
1595 if (vtop[-1].type.t & VT_VLA) {
1596 vla_runtime_pointed_size(&vtop[-1].type);
1597 } else {
1598 vpushi(pointed_size(&vtop[-1].type));
1600 vrott(3);
1601 gen_opic(op);
1602 /* set to integer type */
1603 #ifdef TCC_TARGET_X86_64
1604 vtop->type.t = VT_LLONG;
1605 #else
1606 vtop->type.t = VT_INT;
1607 #endif
1608 vswap();
1609 gen_op(TOK_PDIV);
1610 } else {
1611 /* exactly one pointer : must be '+' or '-'. */
1612 if (op != '-' && op != '+')
1613 tcc_error("cannot use pointers here");
1614 /* Put pointer as first operand */
1615 if (bt2 == VT_PTR) {
1616 vswap();
1617 swap(&t1, &t2);
1619 type1 = vtop[-1].type;
1620 type1.t &= ~VT_ARRAY;
1621 if (vtop[-1].type.t & VT_VLA)
1622 vla_runtime_pointed_size(&vtop[-1].type);
1623 else {
1624 u = pointed_size(&vtop[-1].type);
1625 if (u < 0)
1626 tcc_error("unknown array element size");
1627 #ifdef TCC_TARGET_X86_64
1628 vpushll(u);
1629 #else
1630 /* XXX: cast to int ? (long long case) */
1631 vpushi(u);
1632 #endif
1634 gen_op('*');
1635 #ifdef CONFIG_TCC_BCHECK
1636 /* if evaluating constant expression, no code should be
1637 generated, so no bound check */
1638 if (tcc_state->do_bounds_check && !const_wanted) {
1639 /* if bounded pointers, we generate a special code to
1640 test bounds */
1641 if (op == '-') {
1642 vpushi(0);
1643 vswap();
1644 gen_op('-');
1646 gen_bounded_ptr_add();
1647 } else
1648 #endif
1650 gen_opic(op);
1652 /* put again type if gen_opic() swaped operands */
1653 vtop->type = type1;
1655 } else if (is_float(bt1) || is_float(bt2)) {
1656 /* compute bigger type and do implicit casts */
1657 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1658 t = VT_LDOUBLE;
1659 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1660 t = VT_DOUBLE;
1661 } else {
1662 t = VT_FLOAT;
1664 /* floats can only be used for a few operations */
1665 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1666 (op < TOK_ULT || op > TOK_GT))
1667 tcc_error("invalid operands for binary operation");
1668 goto std_op;
1669 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1670 /* cast to biggest op */
1671 t = VT_LLONG;
1672 /* convert to unsigned if it does not fit in a long long */
1673 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1674 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1675 t |= VT_UNSIGNED;
1676 goto std_op;
1677 } else {
1678 /* integer operations */
1679 t = VT_INT;
1680 /* convert to unsigned if it does not fit in an integer */
1681 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1682 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1683 t |= VT_UNSIGNED;
1684 std_op:
1685 /* XXX: currently, some unsigned operations are explicit, so
1686 we modify them here */
1687 if (t & VT_UNSIGNED) {
1688 if (op == TOK_SAR)
1689 op = TOK_SHR;
1690 else if (op == '/')
1691 op = TOK_UDIV;
1692 else if (op == '%')
1693 op = TOK_UMOD;
1694 else if (op == TOK_LT)
1695 op = TOK_ULT;
1696 else if (op == TOK_GT)
1697 op = TOK_UGT;
1698 else if (op == TOK_LE)
1699 op = TOK_ULE;
1700 else if (op == TOK_GE)
1701 op = TOK_UGE;
1703 vswap();
1704 type1.t = t;
1705 gen_cast(&type1);
1706 vswap();
1707 /* special case for shifts and long long: we keep the shift as
1708 an integer */
1709 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1710 type1.t = VT_INT;
1711 gen_cast(&type1);
1712 if (is_float(t))
1713 gen_opif(op);
1714 else
1715 gen_opic(op);
1716 if (op >= TOK_ULT && op <= TOK_GT) {
1717 /* relationnal op: the result is an int */
1718 vtop->type.t = VT_INT;
1719 } else {
1720 vtop->type.t = t;
1725 #ifndef TCC_TARGET_ARM
1726 /* generic itof for unsigned long long case */
1727 static void gen_cvt_itof1(int t)
1729 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1730 (VT_LLONG | VT_UNSIGNED)) {
1732 if (t == VT_FLOAT)
1733 vpush_global_sym(&func_old_type, TOK___floatundisf);
1734 #if LDOUBLE_SIZE != 8
1735 else if (t == VT_LDOUBLE)
1736 vpush_global_sym(&func_old_type, TOK___floatundixf);
1737 #endif
1738 else
1739 vpush_global_sym(&func_old_type, TOK___floatundidf);
1740 vrott(2);
1741 gfunc_call(1);
1742 vpushi(0);
1743 vtop->r = reg_fret(t);
1744 } else {
1745 gen_cvt_itof(t);
1748 #endif
1750 /* generic ftoi for unsigned long long case */
1751 static void gen_cvt_ftoi1(int t)
1753 int st;
1755 if (t == (VT_LLONG | VT_UNSIGNED)) {
1756 /* not handled natively */
1757 st = vtop->type.t & VT_BTYPE;
1758 if (st == VT_FLOAT)
1759 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1760 #if LDOUBLE_SIZE != 8
1761 else if (st == VT_LDOUBLE)
1762 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1763 #endif
1764 else
1765 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1766 vrott(2);
1767 gfunc_call(1);
1768 vpushi(0);
1769 vtop->r = REG_IRET;
1770 vtop->r2 = REG_LRET;
1771 } else {
1772 gen_cvt_ftoi(t);
1776 /* force char or short cast */
1777 static void force_charshort_cast(int t)
1779 int bits, dbt;
1780 dbt = t & VT_BTYPE;
1781 /* XXX: add optimization if lvalue : just change type and offset */
1782 if (dbt == VT_BYTE)
1783 bits = 8;
1784 else
1785 bits = 16;
1786 if (t & VT_UNSIGNED) {
1787 vpushi((1 << bits) - 1);
1788 gen_op('&');
1789 } else {
1790 bits = 32 - bits;
1791 vpushi(bits);
1792 gen_op(TOK_SHL);
1793 /* result must be signed or the SAR is converted to an SHL
1794 This was not the case when "t" was a signed short
1795 and the last value on the stack was an unsigned int */
1796 vtop->type.t &= ~VT_UNSIGNED;
1797 vpushi(bits);
1798 gen_op(TOK_SAR);
1802 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1803 static void gen_cast(CType *type)
1805 int sbt, dbt, sf, df, c, p;
1807 /* special delayed cast for char/short */
1808 /* XXX: in some cases (multiple cascaded casts), it may still
1809 be incorrect */
1810 if (vtop->r & VT_MUSTCAST) {
1811 vtop->r &= ~VT_MUSTCAST;
1812 force_charshort_cast(vtop->type.t);
1815 /* bitfields first get cast to ints */
1816 if (vtop->type.t & VT_BITFIELD) {
1817 gv(RC_INT);
1820 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1821 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1823 if (sbt != dbt) {
1824 sf = is_float(sbt);
1825 df = is_float(dbt);
1826 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1827 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1828 if (c) {
1829 /* constant case: we can do it now */
1830 /* XXX: in ISOC, cannot do it if error in convert */
1831 if (sbt == VT_FLOAT)
1832 vtop->c.ld = vtop->c.f;
1833 else if (sbt == VT_DOUBLE)
1834 vtop->c.ld = vtop->c.d;
1836 if (df) {
1837 if ((sbt & VT_BTYPE) == VT_LLONG) {
1838 if (sbt & VT_UNSIGNED)
1839 vtop->c.ld = vtop->c.ull;
1840 else
1841 vtop->c.ld = vtop->c.ll;
1842 } else if(!sf) {
1843 if (sbt & VT_UNSIGNED)
1844 vtop->c.ld = vtop->c.ui;
1845 else
1846 vtop->c.ld = vtop->c.i;
1849 if (dbt == VT_FLOAT)
1850 vtop->c.f = (float)vtop->c.ld;
1851 else if (dbt == VT_DOUBLE)
1852 vtop->c.d = (double)vtop->c.ld;
1853 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1854 vtop->c.ull = (unsigned long long)vtop->c.ld;
1855 } else if (sf && dbt == VT_BOOL) {
1856 vtop->c.i = (vtop->c.ld != 0);
1857 } else {
1858 if(sf)
1859 vtop->c.ll = (long long)vtop->c.ld;
1860 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1861 vtop->c.ll = vtop->c.ull;
1862 else if (sbt & VT_UNSIGNED)
1863 vtop->c.ll = vtop->c.ui;
1864 #ifdef TCC_TARGET_X86_64
1865 else if (sbt == VT_PTR)
1867 #endif
1868 else if (sbt != VT_LLONG)
1869 vtop->c.ll = vtop->c.i;
1871 if (dbt == (VT_LLONG|VT_UNSIGNED))
1872 vtop->c.ull = vtop->c.ll;
1873 else if (dbt == VT_BOOL)
1874 vtop->c.i = (vtop->c.ll != 0);
1875 else if (dbt != VT_LLONG) {
1876 int s = 0;
1877 if ((dbt & VT_BTYPE) == VT_BYTE)
1878 s = 24;
1879 else if ((dbt & VT_BTYPE) == VT_SHORT)
1880 s = 16;
1882 if(dbt & VT_UNSIGNED)
1883 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1884 else
1885 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1888 } else if (p && dbt == VT_BOOL) {
1889 vtop->r = VT_CONST;
1890 vtop->c.i = 1;
1891 } else if (!nocode_wanted) {
1892 /* non constant case: generate code */
1893 if (sf && df) {
1894 /* convert from fp to fp */
1895 gen_cvt_ftof(dbt);
1896 } else if (df) {
1897 /* convert int to fp */
1898 gen_cvt_itof1(dbt);
1899 } else if (sf) {
1900 /* convert fp to int */
1901 if (dbt == VT_BOOL) {
1902 vpushi(0);
1903 gen_op(TOK_NE);
1904 } else {
1905 /* we handle char/short/etc... with generic code */
1906 if (dbt != (VT_INT | VT_UNSIGNED) &&
1907 dbt != (VT_LLONG | VT_UNSIGNED) &&
1908 dbt != VT_LLONG)
1909 dbt = VT_INT;
1910 gen_cvt_ftoi1(dbt);
1911 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1912 /* additional cast for char/short... */
1913 vtop->type.t = dbt;
1914 gen_cast(type);
1917 #ifndef TCC_TARGET_X86_64
1918 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1919 if ((sbt & VT_BTYPE) != VT_LLONG) {
1920 /* scalar to long long */
1921 /* machine independent conversion */
1922 gv(RC_INT);
1923 /* generate high word */
1924 if (sbt == (VT_INT | VT_UNSIGNED)) {
1925 vpushi(0);
1926 gv(RC_INT);
1927 } else {
1928 if (sbt == VT_PTR) {
1929 /* cast from pointer to int before we apply
1930 shift operation, which pointers don't support*/
1931 gen_cast(&int_type);
1933 gv_dup();
1934 vpushi(31);
1935 gen_op(TOK_SAR);
1937 /* patch second register */
1938 vtop[-1].r2 = vtop->r;
1939 vpop();
1941 #else
1942 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1943 (dbt & VT_BTYPE) == VT_PTR ||
1944 (dbt & VT_BTYPE) == VT_FUNC) {
1945 if ((sbt & VT_BTYPE) != VT_LLONG &&
1946 (sbt & VT_BTYPE) != VT_PTR &&
1947 (sbt & VT_BTYPE) != VT_FUNC) {
1948 /* need to convert from 32bit to 64bit */
1949 int r = gv(RC_INT);
1950 if (sbt != (VT_INT | VT_UNSIGNED)) {
1951 /* x86_64 specific: movslq */
1952 o(0x6348);
1953 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1956 #endif
1957 } else if (dbt == VT_BOOL) {
1958 /* scalar to bool */
1959 vpushi(0);
1960 gen_op(TOK_NE);
1961 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1962 (dbt & VT_BTYPE) == VT_SHORT) {
1963 if (sbt == VT_PTR) {
1964 vtop->type.t = VT_INT;
1965 tcc_warning("nonportable conversion from pointer to char/short");
1967 force_charshort_cast(dbt);
1968 } else if ((dbt & VT_BTYPE) == VT_INT) {
1969 /* scalar to int */
1970 if (sbt == VT_LLONG) {
1971 /* from long long: just take low order word */
1972 lexpand();
1973 vpop();
1975 /* if lvalue and single word type, nothing to do because
1976 the lvalue already contains the real type size (see
1977 VT_LVAL_xxx constants) */
1980 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1981 /* if we are casting between pointer types,
1982 we must update the VT_LVAL_xxx size */
1983 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1984 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1986 vtop->type = *type;
1989 /* return type size as known at compile time. Put alignment at 'a' */
1990 ST_FUNC int type_size(CType *type, int *a)
1992 Sym *s;
1993 int bt;
1995 bt = type->t & VT_BTYPE;
1996 if (bt == VT_STRUCT) {
1997 /* struct/union */
1998 s = type->ref;
1999 *a = s->r;
2000 return s->c;
2001 } else if (bt == VT_PTR) {
2002 if (type->t & VT_ARRAY) {
2003 int ts;
2005 s = type->ref;
2006 ts = type_size(&s->type, a);
2008 if (ts < 0 && s->c < 0)
2009 ts = -ts;
2011 return ts * s->c;
2012 } else {
2013 *a = PTR_SIZE;
2014 return PTR_SIZE;
2016 } else if (bt == VT_LDOUBLE) {
2017 *a = LDOUBLE_ALIGN;
2018 return LDOUBLE_SIZE;
2019 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2020 #ifdef TCC_TARGET_I386
2021 #ifdef TCC_TARGET_PE
2022 *a = 8;
2023 #else
2024 *a = 4;
2025 #endif
2026 #elif defined(TCC_TARGET_ARM)
2027 #ifdef TCC_ARM_EABI
2028 *a = 8;
2029 #else
2030 *a = 4;
2031 #endif
2032 #else
2033 *a = 8;
2034 #endif
2035 return 8;
2036 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2037 *a = 4;
2038 return 4;
2039 } else if (bt == VT_SHORT) {
2040 *a = 2;
2041 return 2;
2042 } else {
2043 /* char, void, function, _Bool */
2044 *a = 1;
2045 return 1;
2049 /* push type size as known at runtime time on top of value stack. Put
2050 alignment at 'a' */
2051 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2053 if (type->t & VT_VLA) {
2054 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2055 } else {
2056 vpushi(type_size(type, a));
2060 /* return the pointed type of t */
2061 static inline CType *pointed_type(CType *type)
2063 return &type->ref->type;
2066 /* modify type so that its it is a pointer to type. */
2067 ST_FUNC void mk_pointer(CType *type)
2069 Sym *s;
2070 s = sym_push(SYM_FIELD, type, 0, -1);
2071 type->t = VT_PTR | (type->t & ~VT_TYPE);
2072 type->ref = s;
2075 /* compare function types. OLD functions match any new functions */
2076 static int is_compatible_func(CType *type1, CType *type2)
2078 Sym *s1, *s2;
2080 s1 = type1->ref;
2081 s2 = type2->ref;
2082 if (!is_compatible_types(&s1->type, &s2->type))
2083 return 0;
2084 /* check func_call */
2085 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2086 return 0;
2087 /* XXX: not complete */
2088 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2089 return 1;
2090 if (s1->c != s2->c)
2091 return 0;
2092 while (s1 != NULL) {
2093 if (s2 == NULL)
2094 return 0;
2095 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2096 return 0;
2097 s1 = s1->next;
2098 s2 = s2->next;
2100 if (s2)
2101 return 0;
2102 return 1;
2105 /* return true if type1 and type2 are the same. If unqualified is
2106 true, qualifiers on the types are ignored.
2108 - enums are not checked as gcc __builtin_types_compatible_p ()
2110 static int compare_types(CType *type1, CType *type2, int unqualified)
2112 int bt1, t1, t2;
2114 t1 = type1->t & VT_TYPE;
2115 t2 = type2->t & VT_TYPE;
2116 if (unqualified) {
2117 /* strip qualifiers before comparing */
2118 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2119 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2121 /* XXX: bitfields ? */
2122 if (t1 != t2)
2123 return 0;
2124 /* test more complicated cases */
2125 bt1 = t1 & VT_BTYPE;
2126 if (bt1 == VT_PTR) {
2127 type1 = pointed_type(type1);
2128 type2 = pointed_type(type2);
2129 return is_compatible_types(type1, type2);
2130 } else if (bt1 == VT_STRUCT) {
2131 return (type1->ref == type2->ref);
2132 } else if (bt1 == VT_FUNC) {
2133 return is_compatible_func(type1, type2);
2134 } else {
2135 return 1;
2139 /* return true if type1 and type2 are exactly the same (including
2140 qualifiers).
2142 static int is_compatible_types(CType *type1, CType *type2)
2144 return compare_types(type1,type2,0);
2147 /* return true if type1 and type2 are the same (ignoring qualifiers).
2149 static int is_compatible_parameter_types(CType *type1, CType *type2)
2151 return compare_types(type1,type2,1);
2154 /* print a type. If 'varstr' is not NULL, then the variable is also
2155 printed in the type */
2156 /* XXX: union */
2157 /* XXX: add array and function pointers */
2158 static void type_to_str(char *buf, int buf_size,
2159 CType *type, const char *varstr)
2161 int bt, v, t;
2162 Sym *s, *sa;
2163 char buf1[256];
2164 const char *tstr;
2166 t = type->t & VT_TYPE;
2167 bt = t & VT_BTYPE;
2168 buf[0] = '\0';
2169 if (t & VT_CONSTANT)
2170 pstrcat(buf, buf_size, "const ");
2171 if (t & VT_VOLATILE)
2172 pstrcat(buf, buf_size, "volatile ");
2173 if (t & VT_UNSIGNED)
2174 pstrcat(buf, buf_size, "unsigned ");
2175 switch(bt) {
2176 case VT_VOID:
2177 tstr = "void";
2178 goto add_tstr;
2179 case VT_BOOL:
2180 tstr = "_Bool";
2181 goto add_tstr;
2182 case VT_BYTE:
2183 tstr = "char";
2184 goto add_tstr;
2185 case VT_SHORT:
2186 tstr = "short";
2187 goto add_tstr;
2188 case VT_INT:
2189 tstr = "int";
2190 goto add_tstr;
2191 case VT_LONG:
2192 tstr = "long";
2193 goto add_tstr;
2194 case VT_LLONG:
2195 tstr = "long long";
2196 goto add_tstr;
2197 case VT_FLOAT:
2198 tstr = "float";
2199 goto add_tstr;
2200 case VT_DOUBLE:
2201 tstr = "double";
2202 goto add_tstr;
2203 case VT_LDOUBLE:
2204 tstr = "long double";
2205 add_tstr:
2206 pstrcat(buf, buf_size, tstr);
2207 break;
2208 case VT_ENUM:
2209 case VT_STRUCT:
2210 if (bt == VT_STRUCT)
2211 tstr = "struct ";
2212 else
2213 tstr = "enum ";
2214 pstrcat(buf, buf_size, tstr);
2215 v = type->ref->v & ~SYM_STRUCT;
2216 if (v >= SYM_FIRST_ANOM)
2217 pstrcat(buf, buf_size, "<anonymous>");
2218 else
2219 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2220 break;
2221 case VT_FUNC:
2222 s = type->ref;
2223 type_to_str(buf, buf_size, &s->type, varstr);
2224 pstrcat(buf, buf_size, "(");
2225 sa = s->next;
2226 while (sa != NULL) {
2227 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2228 pstrcat(buf, buf_size, buf1);
2229 sa = sa->next;
2230 if (sa)
2231 pstrcat(buf, buf_size, ", ");
2233 pstrcat(buf, buf_size, ")");
2234 goto no_var;
2235 case VT_PTR:
2236 s = type->ref;
2237 pstrcpy(buf1, sizeof(buf1), "*");
2238 if (varstr)
2239 pstrcat(buf1, sizeof(buf1), varstr);
2240 type_to_str(buf, buf_size, &s->type, buf1);
2241 goto no_var;
2243 if (varstr) {
2244 pstrcat(buf, buf_size, " ");
2245 pstrcat(buf, buf_size, varstr);
2247 no_var: ;
2250 /* verify type compatibility to store vtop in 'dt' type, and generate
2251 casts if needed. */
2252 static void gen_assign_cast(CType *dt)
2254 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2255 char buf1[256], buf2[256];
2256 int dbt, sbt;
2258 st = &vtop->type; /* source type */
2259 dbt = dt->t & VT_BTYPE;
2260 sbt = st->t & VT_BTYPE;
2261 if (sbt == VT_VOID)
2262 tcc_error("Cannot assign void value");
2263 if (dt->t & VT_CONSTANT)
2264 tcc_warning("assignment of read-only location");
2265 switch(dbt) {
2266 case VT_PTR:
2267 /* special cases for pointers */
2268 /* '0' can also be a pointer */
2269 if (is_null_pointer(vtop))
2270 goto type_ok;
2271 /* accept implicit pointer to integer cast with warning */
2272 if (is_integer_btype(sbt)) {
2273 tcc_warning("assignment makes pointer from integer without a cast");
2274 goto type_ok;
2276 type1 = pointed_type(dt);
2277 /* a function is implicitely a function pointer */
2278 if (sbt == VT_FUNC) {
2279 if ((type1->t & VT_BTYPE) != VT_VOID &&
2280 !is_compatible_types(pointed_type(dt), st))
2281 tcc_warning("assignment from incompatible pointer type");
2282 goto type_ok;
2284 if (sbt != VT_PTR)
2285 goto error;
2286 type2 = pointed_type(st);
2287 if ((type1->t & VT_BTYPE) == VT_VOID ||
2288 (type2->t & VT_BTYPE) == VT_VOID) {
2289 /* void * can match anything */
2290 } else {
2291 /* exact type match, except for unsigned */
2292 tmp_type1 = *type1;
2293 tmp_type2 = *type2;
2294 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2295 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2296 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2297 tcc_warning("assignment from incompatible pointer type");
2299 /* check const and volatile */
2300 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2301 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2302 tcc_warning("assignment discards qualifiers from pointer target type");
2303 break;
2304 case VT_BYTE:
2305 case VT_SHORT:
2306 case VT_INT:
2307 case VT_LLONG:
2308 if (sbt == VT_PTR || sbt == VT_FUNC) {
2309 tcc_warning("assignment makes integer from pointer without a cast");
2311 /* XXX: more tests */
2312 break;
2313 case VT_STRUCT:
2314 tmp_type1 = *dt;
2315 tmp_type2 = *st;
2316 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2317 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2318 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2319 error:
2320 type_to_str(buf1, sizeof(buf1), st, NULL);
2321 type_to_str(buf2, sizeof(buf2), dt, NULL);
2322 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2324 break;
2326 type_ok:
2327 gen_cast(dt);
2330 /* store vtop in lvalue pushed on stack */
2331 ST_FUNC void vstore(void)
2333 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2335 ft = vtop[-1].type.t;
2336 sbt = vtop->type.t & VT_BTYPE;
2337 dbt = ft & VT_BTYPE;
2338 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2339 (sbt == VT_INT && dbt == VT_SHORT)) {
2340 /* optimize char/short casts */
2341 delayed_cast = VT_MUSTCAST;
2342 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2343 /* XXX: factorize */
2344 if (ft & VT_CONSTANT)
2345 tcc_warning("assignment of read-only location");
2346 } else {
2347 delayed_cast = 0;
2348 if (!(ft & VT_BITFIELD))
2349 gen_assign_cast(&vtop[-1].type);
2352 if (sbt == VT_STRUCT) {
2353 /* if structure, only generate pointer */
2354 /* structure assignment : generate memcpy */
2355 /* XXX: optimize if small size */
2356 if (!nocode_wanted) {
2357 size = type_size(&vtop->type, &align);
2359 /* destination */
2360 vswap();
2361 vtop->type.t = VT_PTR;
2362 gaddrof();
2364 /* address of memcpy() */
2365 #ifdef TCC_ARM_EABI
2366 if(!(align & 7))
2367 vpush_global_sym(&func_old_type, TOK_memcpy8);
2368 else if(!(align & 3))
2369 vpush_global_sym(&func_old_type, TOK_memcpy4);
2370 else
2371 #endif
2372 vpush_global_sym(&func_old_type, TOK_memcpy);
2374 vswap();
2375 /* source */
2376 vpushv(vtop - 2);
2377 vtop->type.t = VT_PTR;
2378 gaddrof();
2379 /* type size */
2380 vpushi(size);
2381 gfunc_call(3);
2382 } else {
2383 vswap();
2384 vpop();
2386 /* leave source on stack */
2387 } else if (ft & VT_BITFIELD) {
2388 /* bitfield store handling */
2389 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2390 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2391 /* remove bit field info to avoid loops */
2392 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2394 /* duplicate source into other register */
2395 gv_dup();
2396 vswap();
2397 vrott(3);
2399 if((ft & VT_BTYPE) == VT_BOOL) {
2400 gen_cast(&vtop[-1].type);
2401 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2404 /* duplicate destination */
2405 vdup();
2406 vtop[-1] = vtop[-2];
2408 /* mask and shift source */
2409 if((ft & VT_BTYPE) != VT_BOOL) {
2410 if((ft & VT_BTYPE) == VT_LLONG) {
2411 vpushll((1ULL << bit_size) - 1ULL);
2412 } else {
2413 vpushi((1 << bit_size) - 1);
2415 gen_op('&');
2417 vpushi(bit_pos);
2418 gen_op(TOK_SHL);
2419 /* load destination, mask and or with source */
2420 vswap();
2421 if((ft & VT_BTYPE) == VT_LLONG) {
2422 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2423 } else {
2424 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2426 gen_op('&');
2427 gen_op('|');
2428 /* store result */
2429 vstore();
2431 /* pop off shifted source from "duplicate source..." above */
2432 vpop();
2434 } else {
2435 #ifdef CONFIG_TCC_BCHECK
2436 /* bound check case */
2437 if (vtop[-1].r & VT_MUSTBOUND) {
2438 vswap();
2439 gbound();
2440 vswap();
2442 #endif
2443 if (!nocode_wanted) {
2444 rc = RC_INT;
2445 if (is_float(ft)) {
2446 rc = RC_FLOAT;
2447 #ifdef TCC_TARGET_X86_64
2448 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2449 rc = RC_ST0;
2451 #endif
2453 r = gv(rc); /* generate value */
2454 /* if lvalue was saved on stack, must read it */
2455 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2456 SValue sv;
2457 t = get_reg(RC_INT);
2458 #ifdef TCC_TARGET_X86_64
2459 sv.type.t = VT_PTR;
2460 #else
2461 sv.type.t = VT_INT;
2462 #endif
2463 sv.r = VT_LOCAL | VT_LVAL;
2464 sv.c.ul = vtop[-1].c.ul;
2465 load(t, &sv);
2466 vtop[-1].r = t | VT_LVAL;
2468 store(r, vtop - 1);
2469 #ifndef TCC_TARGET_X86_64
2470 /* two word case handling : store second register at word + 4 */
2471 if ((ft & VT_BTYPE) == VT_LLONG) {
2472 vswap();
2473 /* convert to int to increment easily */
2474 vtop->type.t = VT_INT;
2475 gaddrof();
2476 vpushi(4);
2477 gen_op('+');
2478 vtop->r |= VT_LVAL;
2479 vswap();
2480 /* XXX: it works because r2 is spilled last ! */
2481 store(vtop->r2, vtop - 1);
2483 #endif
2485 vswap();
2486 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2487 vtop->r |= delayed_cast;
2491 /* post defines POST/PRE add. c is the token ++ or -- */
2492 ST_FUNC void inc(int post, int c)
2494 test_lvalue();
2495 vdup(); /* save lvalue */
2496 if (post) {
2497 gv_dup(); /* duplicate value */
2498 vrotb(3);
2499 vrotb(3);
2501 /* add constant */
2502 vpushi(c - TOK_MID);
2503 gen_op('+');
2504 vstore(); /* store value */
2505 if (post)
2506 vpop(); /* if post op, return saved value */
2509 /* Parse GNUC __attribute__ extension. Currently, the following
2510 extensions are recognized:
2511 - aligned(n) : set data/function alignment.
2512 - packed : force data alignment to 1
2513 - section(x) : generate data/code in this section.
2514 - unused : currently ignored, but may be used someday.
2515 - regparm(n) : pass function parameters in registers (i386 only)
2517 static void parse_attribute(AttributeDef *ad)
2519 int t, n;
2521 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2522 next();
2523 skip('(');
2524 skip('(');
2525 while (tok != ')') {
2526 if (tok < TOK_IDENT)
2527 expect("attribute name");
2528 t = tok;
2529 next();
2530 switch(t) {
2531 case TOK_SECTION1:
2532 case TOK_SECTION2:
2533 skip('(');
2534 if (tok != TOK_STR)
2535 expect("section name");
2536 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2537 next();
2538 skip(')');
2539 break;
2540 case TOK_ALIAS1:
2541 case TOK_ALIAS2:
2542 skip('(');
2543 if (tok != TOK_STR)
2544 expect("alias(\"target\")");
2545 ad->alias_target = /* save string as token, for later */
2546 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2547 next();
2548 skip(')');
2549 break;
2550 case TOK_ALIGNED1:
2551 case TOK_ALIGNED2:
2552 if (tok == '(') {
2553 next();
2554 n = expr_const();
2555 if (n <= 0 || (n & (n - 1)) != 0)
2556 tcc_error("alignment must be a positive power of two");
2557 skip(')');
2558 } else {
2559 n = MAX_ALIGN;
2561 ad->aligned = n;
2562 break;
2563 case TOK_PACKED1:
2564 case TOK_PACKED2:
2565 ad->packed = 1;
2566 break;
2567 case TOK_WEAK1:
2568 case TOK_WEAK2:
2569 ad->weak = 1;
2570 break;
2571 case TOK_UNUSED1:
2572 case TOK_UNUSED2:
2573 /* currently, no need to handle it because tcc does not
2574 track unused objects */
2575 break;
2576 case TOK_NORETURN1:
2577 case TOK_NORETURN2:
2578 /* currently, no need to handle it because tcc does not
2579 track unused objects */
2580 break;
2581 case TOK_CDECL1:
2582 case TOK_CDECL2:
2583 case TOK_CDECL3:
2584 ad->func_call = FUNC_CDECL;
2585 break;
2586 case TOK_STDCALL1:
2587 case TOK_STDCALL2:
2588 case TOK_STDCALL3:
2589 ad->func_call = FUNC_STDCALL;
2590 break;
2591 #ifdef TCC_TARGET_I386
2592 case TOK_REGPARM1:
2593 case TOK_REGPARM2:
2594 skip('(');
2595 n = expr_const();
2596 if (n > 3)
2597 n = 3;
2598 else if (n < 0)
2599 n = 0;
2600 if (n > 0)
2601 ad->func_call = FUNC_FASTCALL1 + n - 1;
2602 skip(')');
2603 break;
2604 case TOK_FASTCALL1:
2605 case TOK_FASTCALL2:
2606 case TOK_FASTCALL3:
2607 ad->func_call = FUNC_FASTCALLW;
2608 break;
2609 #endif
2610 case TOK_MODE:
2611 skip('(');
2612 switch(tok) {
2613 case TOK_MODE_DI:
2614 ad->mode = VT_LLONG + 1;
2615 break;
2616 case TOK_MODE_HI:
2617 ad->mode = VT_SHORT + 1;
2618 break;
2619 case TOK_MODE_SI:
2620 ad->mode = VT_INT + 1;
2621 break;
2622 default:
2623 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2624 break;
2626 next();
2627 skip(')');
2628 break;
2629 case TOK_DLLEXPORT:
2630 ad->func_export = 1;
2631 break;
2632 case TOK_DLLIMPORT:
2633 ad->func_import = 1;
2634 break;
2635 default:
2636 if (tcc_state->warn_unsupported)
2637 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2638 /* skip parameters */
2639 if (tok == '(') {
2640 int parenthesis = 0;
2641 do {
2642 if (tok == '(')
2643 parenthesis++;
2644 else if (tok == ')')
2645 parenthesis--;
2646 next();
2647 } while (parenthesis && tok != -1);
2649 break;
2651 if (tok != ',')
2652 break;
2653 next();
2655 skip(')');
2656 skip(')');
2660 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2661 static void struct_decl(CType *type, int u)
2663 int a, v, size, align, maxalign, c, offset;
2664 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2665 Sym *s, *ss, *ass, **ps;
2666 AttributeDef ad;
2667 CType type1, btype;
2669 a = tok; /* save decl type */
2670 next();
2671 if (tok != '{') {
2672 v = tok;
2673 next();
2674 /* struct already defined ? return it */
2675 if (v < TOK_IDENT)
2676 expect("struct/union/enum name");
2677 s = struct_find(v);
2678 if (s) {
2679 if (s->type.t != a)
2680 tcc_error("invalid type");
2681 goto do_decl;
2683 } else {
2684 v = anon_sym++;
2686 type1.t = a;
2687 /* we put an undefined size for struct/union */
2688 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2689 s->r = 0; /* default alignment is zero as gcc */
2690 /* put struct/union/enum name in type */
2691 do_decl:
2692 type->t = u;
2693 type->ref = s;
2695 if (tok == '{') {
2696 next();
2697 if (s->c != -1)
2698 tcc_error("struct/union/enum already defined");
2699 /* cannot be empty */
2700 c = 0;
2701 /* non empty enums are not allowed */
2702 if (a == TOK_ENUM) {
2703 for(;;) {
2704 v = tok;
2705 if (v < TOK_UIDENT)
2706 expect("identifier");
2707 next();
2708 if (tok == '=') {
2709 next();
2710 c = expr_const();
2712 /* enum symbols have static storage */
2713 ss = sym_push(v, &int_type, VT_CONST, c);
2714 ss->type.t |= VT_STATIC;
2715 if (tok != ',')
2716 break;
2717 next();
2718 c++;
2719 /* NOTE: we accept a trailing comma */
2720 if (tok == '}')
2721 break;
2723 skip('}');
2724 } else {
2725 maxalign = 1;
2726 ps = &s->next;
2727 prevbt = VT_INT;
2728 bit_pos = 0;
2729 offset = 0;
2730 while (tok != '}') {
2731 parse_btype(&btype, &ad);
2732 while (1) {
2733 bit_size = -1;
2734 v = 0;
2735 type1 = btype;
2736 if (tok != ':') {
2737 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2738 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2739 expect("identifier");
2740 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2741 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2742 tcc_error("invalid type for '%s'",
2743 get_tok_str(v, NULL));
2745 if (tok == ':') {
2746 next();
2747 bit_size = expr_const();
2748 /* XXX: handle v = 0 case for messages */
2749 if (bit_size < 0)
2750 tcc_error("negative width in bit-field '%s'",
2751 get_tok_str(v, NULL));
2752 if (v && bit_size == 0)
2753 tcc_error("zero width for bit-field '%s'",
2754 get_tok_str(v, NULL));
2756 size = type_size(&type1, &align);
2757 if (ad.aligned) {
2758 if (align < ad.aligned)
2759 align = ad.aligned;
2760 } else if (ad.packed) {
2761 align = 1;
2762 } else if (*tcc_state->pack_stack_ptr) {
2763 if (align > *tcc_state->pack_stack_ptr)
2764 align = *tcc_state->pack_stack_ptr;
2766 lbit_pos = 0;
2767 if (bit_size >= 0) {
2768 bt = type1.t & VT_BTYPE;
2769 if (bt != VT_INT &&
2770 bt != VT_BYTE &&
2771 bt != VT_SHORT &&
2772 bt != VT_BOOL &&
2773 bt != VT_ENUM &&
2774 bt != VT_LLONG)
2775 tcc_error("bitfields must have scalar type");
2776 bsize = size * 8;
2777 if (bit_size > bsize) {
2778 tcc_error("width of '%s' exceeds its type",
2779 get_tok_str(v, NULL));
2780 } else if (bit_size == bsize) {
2781 /* no need for bit fields */
2782 bit_pos = 0;
2783 } else if (bit_size == 0) {
2784 /* XXX: what to do if only padding in a
2785 structure ? */
2786 /* zero size: means to pad */
2787 bit_pos = 0;
2788 } else {
2789 /* we do not have enough room ?
2790 did the type change?
2791 is it a union? */
2792 if ((bit_pos + bit_size) > bsize ||
2793 bt != prevbt || a == TOK_UNION)
2794 bit_pos = 0;
2795 lbit_pos = bit_pos;
2796 /* XXX: handle LSB first */
2797 type1.t |= VT_BITFIELD |
2798 (bit_pos << VT_STRUCT_SHIFT) |
2799 (bit_size << (VT_STRUCT_SHIFT + 6));
2800 bit_pos += bit_size;
2802 prevbt = bt;
2803 } else {
2804 bit_pos = 0;
2806 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2807 /* add new memory data only if starting
2808 bit field */
2809 if (lbit_pos == 0) {
2810 if (a == TOK_STRUCT) {
2811 c = (c + align - 1) & -align;
2812 offset = c;
2813 if (size > 0)
2814 c += size;
2815 } else {
2816 offset = 0;
2817 if (size > c)
2818 c = size;
2820 if (align > maxalign)
2821 maxalign = align;
2823 #if 0
2824 printf("add field %s offset=%d",
2825 get_tok_str(v, NULL), offset);
2826 if (type1.t & VT_BITFIELD) {
2827 printf(" pos=%d size=%d",
2828 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2829 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2831 printf("\n");
2832 #endif
2834 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2835 ass = type1.ref;
2836 while ((ass = ass->next) != NULL) {
2837 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2838 *ps = ss;
2839 ps = &ss->next;
2841 } else if (v) {
2842 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2843 *ps = ss;
2844 ps = &ss->next;
2846 if (tok == ';' || tok == TOK_EOF)
2847 break;
2848 skip(',');
2850 skip(';');
2852 skip('}');
2853 /* store size and alignment */
2854 s->c = (c + maxalign - 1) & -maxalign;
2855 s->r = maxalign;
2860 /* return 0 if no type declaration. otherwise, return the basic type
2861 and skip it.
2863 static int parse_btype(CType *type, AttributeDef *ad)
2865 int t, u, type_found, typespec_found, typedef_found;
2866 Sym *s;
2867 CType type1;
2869 memset(ad, 0, sizeof(AttributeDef));
2870 type_found = 0;
2871 typespec_found = 0;
2872 typedef_found = 0;
2873 t = 0;
2874 while(1) {
2875 switch(tok) {
2876 case TOK_EXTENSION:
2877 /* currently, we really ignore extension */
2878 next();
2879 continue;
2881 /* basic types */
2882 case TOK_CHAR:
2883 u = VT_BYTE;
2884 basic_type:
2885 next();
2886 basic_type1:
2887 if ((t & VT_BTYPE) != 0)
2888 tcc_error("too many basic types");
2889 t |= u;
2890 typespec_found = 1;
2891 break;
2892 case TOK_VOID:
2893 u = VT_VOID;
2894 goto basic_type;
2895 case TOK_SHORT:
2896 u = VT_SHORT;
2897 goto basic_type;
2898 case TOK_INT:
2899 next();
2900 typespec_found = 1;
2901 break;
2902 case TOK_LONG:
2903 next();
2904 if ((t & VT_BTYPE) == VT_DOUBLE) {
2905 #ifndef TCC_TARGET_PE
2906 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2907 #endif
2908 } else if ((t & VT_BTYPE) == VT_LONG) {
2909 t = (t & ~VT_BTYPE) | VT_LLONG;
2910 } else {
2911 u = VT_LONG;
2912 goto basic_type1;
2914 break;
2915 case TOK_BOOL:
2916 u = VT_BOOL;
2917 goto basic_type;
2918 case TOK_FLOAT:
2919 u = VT_FLOAT;
2920 goto basic_type;
2921 case TOK_DOUBLE:
2922 next();
2923 if ((t & VT_BTYPE) == VT_LONG) {
2924 #ifdef TCC_TARGET_PE
2925 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2926 #else
2927 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2928 #endif
2929 } else {
2930 u = VT_DOUBLE;
2931 goto basic_type1;
2933 break;
2934 case TOK_ENUM:
2935 struct_decl(&type1, VT_ENUM);
2936 basic_type2:
2937 u = type1.t;
2938 type->ref = type1.ref;
2939 goto basic_type1;
2940 case TOK_STRUCT:
2941 case TOK_UNION:
2942 struct_decl(&type1, VT_STRUCT);
2943 goto basic_type2;
2945 /* type modifiers */
2946 case TOK_CONST1:
2947 case TOK_CONST2:
2948 case TOK_CONST3:
2949 t |= VT_CONSTANT;
2950 next();
2951 break;
2952 case TOK_VOLATILE1:
2953 case TOK_VOLATILE2:
2954 case TOK_VOLATILE3:
2955 t |= VT_VOLATILE;
2956 next();
2957 break;
2958 case TOK_SIGNED1:
2959 case TOK_SIGNED2:
2960 case TOK_SIGNED3:
2961 typespec_found = 1;
2962 t |= VT_SIGNED;
2963 next();
2964 break;
2965 case TOK_REGISTER:
2966 case TOK_AUTO:
2967 case TOK_RESTRICT1:
2968 case TOK_RESTRICT2:
2969 case TOK_RESTRICT3:
2970 next();
2971 break;
2972 case TOK_UNSIGNED:
2973 t |= VT_UNSIGNED;
2974 next();
2975 typespec_found = 1;
2976 break;
2978 /* storage */
2979 case TOK_EXTERN:
2980 t |= VT_EXTERN;
2981 next();
2982 break;
2983 case TOK_STATIC:
2984 t |= VT_STATIC;
2985 next();
2986 break;
2987 case TOK_TYPEDEF:
2988 t |= VT_TYPEDEF;
2989 next();
2990 break;
2991 case TOK_INLINE1:
2992 case TOK_INLINE2:
2993 case TOK_INLINE3:
2994 t |= VT_INLINE;
2995 next();
2996 break;
2998 /* GNUC attribute */
2999 case TOK_ATTRIBUTE1:
3000 case TOK_ATTRIBUTE2:
3001 parse_attribute(ad);
3002 if (ad->mode) {
3003 u = ad->mode -1;
3004 t = (t & ~VT_BTYPE) | u;
3006 break;
3007 /* GNUC typeof */
3008 case TOK_TYPEOF1:
3009 case TOK_TYPEOF2:
3010 case TOK_TYPEOF3:
3011 next();
3012 parse_expr_type(&type1);
3013 /* remove all storage modifiers except typedef */
3014 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3015 goto basic_type2;
3016 default:
3017 if (typespec_found || typedef_found)
3018 goto the_end;
3019 s = sym_find(tok);
3020 if (!s || !(s->type.t & VT_TYPEDEF))
3021 goto the_end;
3022 typedef_found = 1;
3023 t |= (s->type.t & ~VT_TYPEDEF);
3024 type->ref = s->type.ref;
3025 if (s->r) {
3026 /* get attributes from typedef */
3027 if (0 == ad->aligned)
3028 ad->aligned = FUNC_ALIGN(s->r);
3029 if (0 == ad->func_call)
3030 ad->func_call = FUNC_CALL(s->r);
3031 ad->packed |= FUNC_PACKED(s->r);
3033 next();
3034 typespec_found = 1;
3035 break;
3037 type_found = 1;
3039 the_end:
3040 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3041 tcc_error("signed and unsigned modifier");
3042 if (tcc_state->char_is_unsigned) {
3043 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3044 t |= VT_UNSIGNED;
3046 t &= ~VT_SIGNED;
3048 /* long is never used as type */
3049 if ((t & VT_BTYPE) == VT_LONG)
3050 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3051 t = (t & ~VT_BTYPE) | VT_INT;
3052 #else
3053 t = (t & ~VT_BTYPE) | VT_LLONG;
3054 #endif
3055 type->t = t;
3056 return type_found;
3059 /* convert a function parameter type (array to pointer and function to
3060 function pointer) */
3061 static inline void convert_parameter_type(CType *pt)
3063 /* remove const and volatile qualifiers (XXX: const could be used
3064 to indicate a const function parameter */
3065 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3066 /* array must be transformed to pointer according to ANSI C */
3067 pt->t &= ~VT_ARRAY;
3068 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3069 mk_pointer(pt);
3073 ST_FUNC void parse_asm_str(CString *astr)
3075 skip('(');
3076 /* read the string */
3077 if (tok != TOK_STR)
3078 expect("string constant");
3079 cstr_new(astr);
3080 while (tok == TOK_STR) {
3081 /* XXX: add \0 handling too ? */
3082 cstr_cat(astr, tokc.cstr->data);
3083 next();
3085 cstr_ccat(astr, '\0');
3088 /* Parse an asm label and return the label
3089 * Don't forget to free the CString in the caller! */
3090 static void asm_label_instr(CString *astr)
3092 next();
3093 parse_asm_str(astr);
3094 skip(')');
3095 #ifdef ASM_DEBUG
3096 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3097 #endif
3100 static void post_type(CType *type, AttributeDef *ad)
3102 int n, l, t1, arg_size, align;
3103 Sym **plast, *s, *first;
3104 AttributeDef ad1;
3105 CType pt;
3107 if (tok == '(') {
3108 /* function declaration */
3109 next();
3110 l = 0;
3111 first = NULL;
3112 plast = &first;
3113 arg_size = 0;
3114 if (tok != ')') {
3115 for(;;) {
3116 /* read param name and compute offset */
3117 if (l != FUNC_OLD) {
3118 if (!parse_btype(&pt, &ad1)) {
3119 if (l) {
3120 tcc_error("invalid type");
3121 } else {
3122 l = FUNC_OLD;
3123 goto old_proto;
3126 l = FUNC_NEW;
3127 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3128 break;
3129 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3130 if ((pt.t & VT_BTYPE) == VT_VOID)
3131 tcc_error("parameter declared as void");
3132 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3133 } else {
3134 old_proto:
3135 n = tok;
3136 if (n < TOK_UIDENT)
3137 expect("identifier");
3138 pt.t = VT_INT;
3139 next();
3141 convert_parameter_type(&pt);
3142 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3143 *plast = s;
3144 plast = &s->next;
3145 if (tok == ')')
3146 break;
3147 skip(',');
3148 if (l == FUNC_NEW && tok == TOK_DOTS) {
3149 l = FUNC_ELLIPSIS;
3150 next();
3151 break;
3155 /* if no parameters, then old type prototype */
3156 if (l == 0)
3157 l = FUNC_OLD;
3158 skip(')');
3159 /* NOTE: const is ignored in returned type as it has a special
3160 meaning in gcc / C++ */
3161 type->t &= ~VT_CONSTANT;
3162 /* some ancient pre-K&R C allows a function to return an array
3163 and the array brackets to be put after the arguments, such
3164 that "int c()[]" means something like "int[] c()" */
3165 if (tok == '[') {
3166 next();
3167 skip(']'); /* only handle simple "[]" */
3168 type->t |= VT_PTR;
3170 /* we push a anonymous symbol which will contain the function prototype */
3171 ad->func_args = arg_size;
3172 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3173 s->next = first;
3174 type->t = VT_FUNC;
3175 type->ref = s;
3176 } else if (tok == '[') {
3177 /* array definition */
3178 next();
3179 if (tok == TOK_RESTRICT1)
3180 next();
3181 n = -1;
3182 t1 = 0;
3183 if (tok != ']') {
3184 if (!local_stack || nocode_wanted)
3185 vpushi(expr_const());
3186 else gexpr();
3187 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3188 n = vtop->c.i;
3189 if (n < 0)
3190 tcc_error("invalid array size");
3191 } else {
3192 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3193 tcc_error("size of variable length array should be an integer");
3194 t1 = VT_VLA;
3197 skip(']');
3198 /* parse next post type */
3199 post_type(type, ad);
3200 t1 |= type->t & VT_VLA;
3202 if (t1 & VT_VLA) {
3203 loc -= type_size(&int_type, &align);
3204 loc &= -align;
3205 n = loc;
3207 vla_runtime_type_size(type, &align);
3208 gen_op('*');
3209 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3210 vswap();
3211 vstore();
3213 if (n != -1)
3214 vpop();
3216 /* we push an anonymous symbol which will contain the array
3217 element type */
3218 s = sym_push(SYM_FIELD, type, 0, n);
3219 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3220 type->ref = s;
3224 /* Parse a type declaration (except basic type), and return the type
3225 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3226 expected. 'type' should contain the basic type. 'ad' is the
3227 attribute definition of the basic type. It can be modified by
3228 type_decl().
3230 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3232 Sym *s;
3233 CType type1, *type2;
3234 int qualifiers, storage;
3236 while (tok == '*') {
3237 qualifiers = 0;
3238 redo:
3239 next();
3240 switch(tok) {
3241 case TOK_CONST1:
3242 case TOK_CONST2:
3243 case TOK_CONST3:
3244 qualifiers |= VT_CONSTANT;
3245 goto redo;
3246 case TOK_VOLATILE1:
3247 case TOK_VOLATILE2:
3248 case TOK_VOLATILE3:
3249 qualifiers |= VT_VOLATILE;
3250 goto redo;
3251 case TOK_RESTRICT1:
3252 case TOK_RESTRICT2:
3253 case TOK_RESTRICT3:
3254 goto redo;
3256 mk_pointer(type);
3257 type->t |= qualifiers;
3260 /* XXX: clarify attribute handling */
3261 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3262 parse_attribute(ad);
3264 /* recursive type */
3265 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3266 type1.t = 0; /* XXX: same as int */
3267 if (tok == '(') {
3268 next();
3269 /* XXX: this is not correct to modify 'ad' at this point, but
3270 the syntax is not clear */
3271 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3272 parse_attribute(ad);
3273 type_decl(&type1, ad, v, td);
3274 skip(')');
3275 } else {
3276 /* type identifier */
3277 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3278 *v = tok;
3279 next();
3280 } else {
3281 if (!(td & TYPE_ABSTRACT))
3282 expect("identifier");
3283 *v = 0;
3286 storage = type->t & VT_STORAGE;
3287 type->t &= ~VT_STORAGE;
3288 post_type(type, ad);
3289 type->t |= storage;
3290 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3291 parse_attribute(ad);
3293 if (!type1.t)
3294 return;
3295 /* append type at the end of type1 */
3296 type2 = &type1;
3297 for(;;) {
3298 s = type2->ref;
3299 type2 = &s->type;
3300 if (!type2->t) {
3301 *type2 = *type;
3302 break;
3305 *type = type1;
3308 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3309 ST_FUNC int lvalue_type(int t)
3311 int bt, r;
3312 r = VT_LVAL;
3313 bt = t & VT_BTYPE;
3314 if (bt == VT_BYTE || bt == VT_BOOL)
3315 r |= VT_LVAL_BYTE;
3316 else if (bt == VT_SHORT)
3317 r |= VT_LVAL_SHORT;
3318 else
3319 return r;
3320 if (t & VT_UNSIGNED)
3321 r |= VT_LVAL_UNSIGNED;
3322 return r;
3325 /* indirection with full error checking and bound check */
3326 ST_FUNC void indir(void)
3328 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3329 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3330 return;
3331 expect("pointer");
3333 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3334 gv(RC_INT);
3335 vtop->type = *pointed_type(&vtop->type);
3336 /* Arrays and functions are never lvalues */
3337 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3338 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3339 vtop->r |= lvalue_type(vtop->type.t);
3340 /* if bound checking, the referenced pointer must be checked */
3341 #ifdef CONFIG_TCC_BCHECK
3342 if (tcc_state->do_bounds_check)
3343 vtop->r |= VT_MUSTBOUND;
3344 #endif
3348 /* pass a parameter to a function and do type checking and casting */
3349 static void gfunc_param_typed(Sym *func, Sym *arg)
3351 int func_type;
3352 CType type;
3354 func_type = func->c;
3355 if (func_type == FUNC_OLD ||
3356 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3357 /* default casting : only need to convert float to double */
3358 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3359 type.t = VT_DOUBLE;
3360 gen_cast(&type);
3362 } else if (arg == NULL) {
3363 tcc_error("too many arguments to function");
3364 } else {
3365 type = arg->type;
3366 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3367 gen_assign_cast(&type);
3371 /* parse an expression of the form '(type)' or '(expr)' and return its
3372 type */
3373 static void parse_expr_type(CType *type)
3375 int n;
3376 AttributeDef ad;
3378 skip('(');
3379 if (parse_btype(type, &ad)) {
3380 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3381 } else {
3382 expr_type(type);
3384 skip(')');
3387 static void parse_type(CType *type)
3389 AttributeDef ad;
3390 int n;
3392 if (!parse_btype(type, &ad)) {
3393 expect("type");
3395 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3398 static void vpush_tokc(int t)
3400 CType type;
3401 type.t = t;
3402 type.ref = 0;
3403 vsetc(&type, VT_CONST, &tokc);
3406 ST_FUNC void unary(void)
3408 int n, t, align, size, r, sizeof_caller;
3409 CType type;
3410 Sym *s;
3411 AttributeDef ad;
3412 static int in_sizeof = 0;
3414 sizeof_caller = in_sizeof;
3415 in_sizeof = 0;
3416 /* XXX: GCC 2.95.3 does not generate a table although it should be
3417 better here */
3418 tok_next:
3419 switch(tok) {
3420 case TOK_EXTENSION:
3421 next();
3422 goto tok_next;
3423 case TOK_CINT:
3424 case TOK_CCHAR:
3425 case TOK_LCHAR:
3426 vpushi(tokc.i);
3427 next();
3428 break;
3429 case TOK_CUINT:
3430 vpush_tokc(VT_INT | VT_UNSIGNED);
3431 next();
3432 break;
3433 case TOK_CLLONG:
3434 vpush_tokc(VT_LLONG);
3435 next();
3436 break;
3437 case TOK_CULLONG:
3438 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3439 next();
3440 break;
3441 case TOK_CFLOAT:
3442 vpush_tokc(VT_FLOAT);
3443 next();
3444 break;
3445 case TOK_CDOUBLE:
3446 vpush_tokc(VT_DOUBLE);
3447 next();
3448 break;
3449 case TOK_CLDOUBLE:
3450 vpush_tokc(VT_LDOUBLE);
3451 next();
3452 break;
3453 case TOK___FUNCTION__:
3454 if (!gnu_ext)
3455 goto tok_identifier;
3456 /* fall thru */
3457 case TOK___FUNC__:
3459 void *ptr;
3460 int len;
3461 /* special function name identifier */
3462 len = strlen(funcname) + 1;
3463 /* generate char[len] type */
3464 type.t = VT_BYTE;
3465 mk_pointer(&type);
3466 type.t |= VT_ARRAY;
3467 type.ref->c = len;
3468 vpush_ref(&type, data_section, data_section->data_offset, len);
3469 ptr = section_ptr_add(data_section, len);
3470 memcpy(ptr, funcname, len);
3471 next();
3473 break;
3474 case TOK_LSTR:
3475 #ifdef TCC_TARGET_PE
3476 t = VT_SHORT | VT_UNSIGNED;
3477 #else
3478 t = VT_INT;
3479 #endif
3480 goto str_init;
3481 case TOK_STR:
3482 /* string parsing */
3483 t = VT_BYTE;
3484 str_init:
3485 if (tcc_state->warn_write_strings)
3486 t |= VT_CONSTANT;
3487 type.t = t;
3488 mk_pointer(&type);
3489 type.t |= VT_ARRAY;
3490 memset(&ad, 0, sizeof(AttributeDef));
3491 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3492 break;
3493 case '(':
3494 next();
3495 /* cast ? */
3496 if (parse_btype(&type, &ad)) {
3497 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3498 skip(')');
3499 /* check ISOC99 compound literal */
3500 if (tok == '{') {
3501 /* data is allocated locally by default */
3502 if (global_expr)
3503 r = VT_CONST;
3504 else
3505 r = VT_LOCAL;
3506 /* all except arrays are lvalues */
3507 if (!(type.t & VT_ARRAY))
3508 r |= lvalue_type(type.t);
3509 memset(&ad, 0, sizeof(AttributeDef));
3510 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3511 } else {
3512 if (sizeof_caller) {
3513 vpush(&type);
3514 return;
3516 unary();
3517 gen_cast(&type);
3519 } else if (tok == '{') {
3520 /* save all registers */
3521 save_regs(0);
3522 /* statement expression : we do not accept break/continue
3523 inside as GCC does */
3524 block(NULL, NULL, NULL, NULL, 0, 1);
3525 skip(')');
3526 } else {
3527 gexpr();
3528 skip(')');
3530 break;
3531 case '*':
3532 next();
3533 unary();
3534 indir();
3535 break;
3536 case '&':
3537 next();
3538 unary();
3539 /* functions names must be treated as function pointers,
3540 except for unary '&' and sizeof. Since we consider that
3541 functions are not lvalues, we only have to handle it
3542 there and in function calls. */
3543 /* arrays can also be used although they are not lvalues */
3544 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3545 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3546 test_lvalue();
3547 mk_pointer(&vtop->type);
3548 gaddrof();
3549 break;
3550 case '!':
3551 next();
3552 unary();
3553 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3554 CType boolean;
3555 boolean.t = VT_BOOL;
3556 gen_cast(&boolean);
3557 vtop->c.i = !vtop->c.i;
3558 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3559 vtop->c.i = vtop->c.i ^ 1;
3560 else {
3561 save_regs(1);
3562 vseti(VT_JMP, gtst(1, 0));
3564 break;
3565 case '~':
3566 next();
3567 unary();
3568 vpushi(-1);
3569 gen_op('^');
3570 break;
3571 case '+':
3572 next();
3573 /* in order to force cast, we add zero */
3574 unary();
3575 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3576 tcc_error("pointer not accepted for unary plus");
3577 vpushi(0);
3578 gen_op('+');
3579 break;
3580 case TOK_SIZEOF:
3581 case TOK_ALIGNOF1:
3582 case TOK_ALIGNOF2:
3583 t = tok;
3584 next();
3585 in_sizeof++;
3586 unary_type(&type); // Perform a in_sizeof = 0;
3587 size = type_size(&type, &align);
3588 if (t == TOK_SIZEOF) {
3589 if (!(type.t & VT_VLA)) {
3590 if (size < 0)
3591 tcc_error("sizeof applied to an incomplete type");
3592 vpushi(size);
3593 } else {
3594 vla_runtime_type_size(&type, &align);
3596 } else {
3597 vpushi(align);
3599 vtop->type.t |= VT_UNSIGNED;
3600 break;
3602 case TOK_builtin_types_compatible_p:
3604 CType type1, type2;
3605 next();
3606 skip('(');
3607 parse_type(&type1);
3608 skip(',');
3609 parse_type(&type2);
3610 skip(')');
3611 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3612 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3613 vpushi(is_compatible_types(&type1, &type2));
3615 break;
3616 case TOK_builtin_constant_p:
3618 int saved_nocode_wanted, res;
3619 next();
3620 skip('(');
3621 saved_nocode_wanted = nocode_wanted;
3622 nocode_wanted = 1;
3623 gexpr();
3624 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3625 vpop();
3626 nocode_wanted = saved_nocode_wanted;
3627 skip(')');
3628 vpushi(res);
3630 break;
3631 case TOK_builtin_frame_address:
3633 CType type;
3634 next();
3635 skip('(');
3636 if (tok != TOK_CINT) {
3637 tcc_error("__builtin_frame_address only takes integers");
3639 if (tokc.i != 0) {
3640 tcc_error("TCC only supports __builtin_frame_address(0)");
3642 next();
3643 skip(')');
3644 type.t = VT_VOID;
3645 mk_pointer(&type);
3646 vset(&type, VT_LOCAL, 0);
3648 break;
3649 #ifdef TCC_TARGET_X86_64
3650 case TOK_builtin_va_arg_types:
3652 /* This definition must be synced with stdarg.h */
3653 enum __va_arg_type {
3654 __va_gen_reg, __va_float_reg, __va_stack
3656 CType type;
3657 int bt;
3658 next();
3659 skip('(');
3660 parse_type(&type);
3661 skip(')');
3662 bt = type.t & VT_BTYPE;
3663 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
3664 vpushi(__va_stack);
3665 } else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
3666 vpushi(__va_float_reg);
3667 } else {
3668 vpushi(__va_gen_reg);
3671 break;
3672 #endif
3673 case TOK_INC:
3674 case TOK_DEC:
3675 t = tok;
3676 next();
3677 unary();
3678 inc(0, t);
3679 break;
3680 case '-':
3681 next();
3682 vpushi(0);
3683 unary();
3684 gen_op('-');
3685 break;
3686 case TOK_LAND:
3687 if (!gnu_ext)
3688 goto tok_identifier;
3689 next();
3690 /* allow to take the address of a label */
3691 if (tok < TOK_UIDENT)
3692 expect("label identifier");
3693 s = label_find(tok);
3694 if (!s) {
3695 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3696 } else {
3697 if (s->r == LABEL_DECLARED)
3698 s->r = LABEL_FORWARD;
3700 if (!s->type.t) {
3701 s->type.t = VT_VOID;
3702 mk_pointer(&s->type);
3703 s->type.t |= VT_STATIC;
3705 vset(&s->type, VT_CONST | VT_SYM, 0);
3706 vtop->sym = s;
3707 next();
3708 break;
3710 // special qnan , snan and infinity values
3711 case TOK___NAN__:
3712 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3713 next();
3714 break;
3715 case TOK___SNAN__:
3716 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3717 next();
3718 break;
3719 case TOK___INF__:
3720 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3721 next();
3722 break;
3724 default:
3725 tok_identifier:
3726 t = tok;
3727 next();
3728 if (t < TOK_UIDENT)
3729 expect("identifier");
3730 s = sym_find(t);
3731 if (!s) {
3732 if (tok != '(')
3733 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3734 /* for simple function calls, we tolerate undeclared
3735 external reference to int() function */
3736 if (tcc_state->warn_implicit_function_declaration)
3737 tcc_warning("implicit declaration of function '%s'",
3738 get_tok_str(t, NULL));
3739 s = external_global_sym(t, &func_old_type, 0);
3741 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3742 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3743 /* if referencing an inline function, then we generate a
3744 symbol to it if not already done. It will have the
3745 effect to generate code for it at the end of the
3746 compilation unit. Inline function as always
3747 generated in the text section. */
3748 if (!s->c)
3749 put_extern_sym(s, text_section, 0, 0);
3750 r = VT_SYM | VT_CONST;
3751 } else {
3752 r = s->r;
3754 vset(&s->type, r, s->c);
3755 /* if forward reference, we must point to s */
3756 if (vtop->r & VT_SYM) {
3757 vtop->sym = s;
3758 vtop->c.ul = 0;
3760 break;
3763 /* post operations */
3764 while (1) {
3765 if (tok == TOK_INC || tok == TOK_DEC) {
3766 inc(1, tok);
3767 next();
3768 } else if (tok == '.' || tok == TOK_ARROW) {
3769 int qualifiers;
3770 /* field */
3771 if (tok == TOK_ARROW)
3772 indir();
3773 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3774 test_lvalue();
3775 gaddrof();
3776 next();
3777 /* expect pointer on structure */
3778 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3779 expect("struct or union");
3780 s = vtop->type.ref;
3781 /* find field */
3782 tok |= SYM_FIELD;
3783 while ((s = s->next) != NULL) {
3784 if (s->v == tok)
3785 break;
3787 if (!s)
3788 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3789 /* add field offset to pointer */
3790 vtop->type = char_pointer_type; /* change type to 'char *' */
3791 vpushi(s->c);
3792 gen_op('+');
3793 /* change type to field type, and set to lvalue */
3794 vtop->type = s->type;
3795 vtop->type.t |= qualifiers;
3796 /* an array is never an lvalue */
3797 if (!(vtop->type.t & VT_ARRAY)) {
3798 vtop->r |= lvalue_type(vtop->type.t);
3799 #ifdef CONFIG_TCC_BCHECK
3800 /* if bound checking, the referenced pointer must be checked */
3801 if (tcc_state->do_bounds_check)
3802 vtop->r |= VT_MUSTBOUND;
3803 #endif
3805 next();
3806 } else if (tok == '[') {
3807 next();
3808 gexpr();
3809 gen_op('+');
3810 indir();
3811 skip(']');
3812 } else if (tok == '(') {
3813 SValue ret;
3814 Sym *sa;
3815 int nb_args;
3817 /* function call */
3818 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3819 /* pointer test (no array accepted) */
3820 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3821 vtop->type = *pointed_type(&vtop->type);
3822 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3823 goto error_func;
3824 } else {
3825 error_func:
3826 expect("function pointer");
3828 } else {
3829 vtop->r &= ~VT_LVAL; /* no lvalue */
3831 /* get return type */
3832 s = vtop->type.ref;
3833 next();
3834 sa = s->next; /* first parameter */
3835 nb_args = 0;
3836 ret.r2 = VT_CONST;
3837 /* compute first implicit argument if a structure is returned */
3838 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3839 /* get some space for the returned structure */
3840 size = type_size(&s->type, &align);
3841 loc = (loc - size) & -align;
3842 ret.type = s->type;
3843 ret.r = VT_LOCAL | VT_LVAL;
3844 /* pass it as 'int' to avoid structure arg passing
3845 problems */
3846 vseti(VT_LOCAL, loc);
3847 ret.c = vtop->c;
3848 nb_args++;
3849 } else {
3850 ret.type = s->type;
3851 /* return in register */
3852 if (is_float(ret.type.t)) {
3853 ret.r = reg_fret(ret.type.t);
3854 } else {
3855 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3856 ret.r2 = REG_LRET;
3857 ret.r = REG_IRET;
3859 ret.c.i = 0;
3861 if (tok != ')') {
3862 for(;;) {
3863 expr_eq();
3864 gfunc_param_typed(s, sa);
3865 nb_args++;
3866 if (sa)
3867 sa = sa->next;
3868 if (tok == ')')
3869 break;
3870 skip(',');
3873 if (sa)
3874 tcc_error("too few arguments to function");
3875 skip(')');
3876 if (!nocode_wanted) {
3877 gfunc_call(nb_args);
3878 } else {
3879 vtop -= (nb_args + 1);
3881 /* return value */
3882 vsetc(&ret.type, ret.r, &ret.c);
3883 vtop->r2 = ret.r2;
3884 } else {
3885 break;
3890 ST_FUNC void expr_prod(void)
3892 int t;
3894 unary();
3895 while (tok == '*' || tok == '/' || tok == '%') {
3896 t = tok;
3897 next();
3898 unary();
3899 gen_op(t);
3903 ST_FUNC void expr_sum(void)
3905 int t;
3907 expr_prod();
3908 while (tok == '+' || tok == '-') {
3909 t = tok;
3910 next();
3911 expr_prod();
3912 gen_op(t);
3916 static void expr_shift(void)
3918 int t;
3920 expr_sum();
3921 while (tok == TOK_SHL || tok == TOK_SAR) {
3922 t = tok;
3923 next();
3924 expr_sum();
3925 gen_op(t);
3929 static void expr_cmp(void)
3931 int t;
3933 expr_shift();
3934 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3935 tok == TOK_ULT || tok == TOK_UGE) {
3936 t = tok;
3937 next();
3938 expr_shift();
3939 gen_op(t);
3943 static void expr_cmpeq(void)
3945 int t;
3947 expr_cmp();
3948 while (tok == TOK_EQ || tok == TOK_NE) {
3949 t = tok;
3950 next();
3951 expr_cmp();
3952 gen_op(t);
3956 static void expr_and(void)
3958 expr_cmpeq();
3959 while (tok == '&') {
3960 next();
3961 expr_cmpeq();
3962 gen_op('&');
3966 static void expr_xor(void)
3968 expr_and();
3969 while (tok == '^') {
3970 next();
3971 expr_and();
3972 gen_op('^');
3976 static void expr_or(void)
3978 expr_xor();
3979 while (tok == '|') {
3980 next();
3981 expr_xor();
3982 gen_op('|');
3986 /* XXX: fix this mess */
3987 static void expr_land_const(void)
3989 expr_or();
3990 while (tok == TOK_LAND) {
3991 next();
3992 expr_or();
3993 gen_op(TOK_LAND);
3997 /* XXX: fix this mess */
3998 static void expr_lor_const(void)
4000 expr_land_const();
4001 while (tok == TOK_LOR) {
4002 next();
4003 expr_land_const();
4004 gen_op(TOK_LOR);
4008 /* only used if non constant */
4009 static void expr_land(void)
4011 int t;
4013 expr_or();
4014 if (tok == TOK_LAND) {
4015 t = 0;
4016 save_regs(1);
4017 for(;;) {
4018 t = gtst(1, t);
4019 if (tok != TOK_LAND) {
4020 vseti(VT_JMPI, t);
4021 break;
4023 next();
4024 expr_or();
4029 static void expr_lor(void)
4031 int t;
4033 expr_land();
4034 if (tok == TOK_LOR) {
4035 t = 0;
4036 save_regs(1);
4037 for(;;) {
4038 t = gtst(0, t);
4039 if (tok != TOK_LOR) {
4040 vseti(VT_JMP, t);
4041 break;
4043 next();
4044 expr_land();
4049 /* XXX: better constant handling */
4050 static void expr_cond(void)
4052 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4053 SValue sv;
4054 CType type, type1, type2;
4056 if (const_wanted) {
4057 expr_lor_const();
4058 if (tok == '?') {
4059 CType boolean;
4060 int c;
4061 boolean.t = VT_BOOL;
4062 vdup();
4063 gen_cast(&boolean);
4064 c = vtop->c.i;
4065 vpop();
4066 next();
4067 if (tok != ':' || !gnu_ext) {
4068 vpop();
4069 gexpr();
4071 if (!c)
4072 vpop();
4073 skip(':');
4074 expr_cond();
4075 if (c)
4076 vpop();
4078 } else {
4079 expr_lor();
4080 if (tok == '?') {
4081 next();
4082 if (vtop != vstack) {
4083 /* needed to avoid having different registers saved in
4084 each branch */
4085 if (is_float(vtop->type.t)) {
4086 rc = RC_FLOAT;
4087 #ifdef TCC_TARGET_X86_64
4088 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4089 rc = RC_ST0;
4091 #endif
4093 else
4094 rc = RC_INT;
4095 gv(rc);
4096 save_regs(1);
4098 if (tok == ':' && gnu_ext) {
4099 gv_dup();
4100 tt = gtst(1, 0);
4101 } else {
4102 tt = gtst(1, 0);
4103 gexpr();
4105 type1 = vtop->type;
4106 sv = *vtop; /* save value to handle it later */
4107 vtop--; /* no vpop so that FP stack is not flushed */
4108 skip(':');
4109 u = gjmp(0);
4110 gsym(tt);
4111 expr_cond();
4112 type2 = vtop->type;
4114 t1 = type1.t;
4115 bt1 = t1 & VT_BTYPE;
4116 t2 = type2.t;
4117 bt2 = t2 & VT_BTYPE;
4118 /* cast operands to correct type according to ISOC rules */
4119 if (is_float(bt1) || is_float(bt2)) {
4120 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4121 type.t = VT_LDOUBLE;
4122 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4123 type.t = VT_DOUBLE;
4124 } else {
4125 type.t = VT_FLOAT;
4127 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4128 /* cast to biggest op */
4129 type.t = VT_LLONG;
4130 /* convert to unsigned if it does not fit in a long long */
4131 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4132 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4133 type.t |= VT_UNSIGNED;
4134 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4135 /* XXX: test pointer compatibility */
4136 type = type1;
4137 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4138 /* XXX: test function pointer compatibility */
4139 type = type1;
4140 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4141 /* XXX: test structure compatibility */
4142 type = type1;
4143 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4144 /* NOTE: as an extension, we accept void on only one side */
4145 type.t = VT_VOID;
4146 } else {
4147 /* integer operations */
4148 type.t = VT_INT;
4149 /* convert to unsigned if it does not fit in an integer */
4150 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4151 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4152 type.t |= VT_UNSIGNED;
4155 /* now we convert second operand */
4156 gen_cast(&type);
4157 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4158 gaddrof();
4159 rc = RC_INT;
4160 if (is_float(type.t)) {
4161 rc = RC_FLOAT;
4162 #ifdef TCC_TARGET_X86_64
4163 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4164 rc = RC_ST0;
4166 #endif
4167 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4168 /* for long longs, we use fixed registers to avoid having
4169 to handle a complicated move */
4170 rc = RC_IRET;
4173 r2 = gv(rc);
4174 /* this is horrible, but we must also convert first
4175 operand */
4176 tt = gjmp(0);
4177 gsym(u);
4178 /* put again first value and cast it */
4179 *vtop = sv;
4180 gen_cast(&type);
4181 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4182 gaddrof();
4183 r1 = gv(rc);
4184 move_reg(r2, r1);
4185 vtop->r = r2;
4186 gsym(tt);
4191 static void expr_eq(void)
4193 int t;
4195 expr_cond();
4196 if (tok == '=' ||
4197 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4198 tok == TOK_A_XOR || tok == TOK_A_OR ||
4199 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4200 test_lvalue();
4201 t = tok;
4202 next();
4203 if (t == '=') {
4204 expr_eq();
4205 } else {
4206 vdup();
4207 expr_eq();
4208 gen_op(t & 0x7f);
4210 vstore();
4214 ST_FUNC void gexpr(void)
4216 while (1) {
4217 expr_eq();
4218 if (tok != ',')
4219 break;
4220 vpop();
4221 next();
4225 /* parse an expression and return its type without any side effect. */
4226 static void expr_type(CType *type)
4228 int saved_nocode_wanted;
4230 saved_nocode_wanted = nocode_wanted;
4231 nocode_wanted = 1;
4232 gexpr();
4233 *type = vtop->type;
4234 vpop();
4235 nocode_wanted = saved_nocode_wanted;
4238 /* parse a unary expression and return its type without any side
4239 effect. */
4240 static void unary_type(CType *type)
4242 int a;
4244 a = nocode_wanted;
4245 nocode_wanted = 1;
4246 unary();
4247 *type = vtop->type;
4248 vpop();
4249 nocode_wanted = a;
4252 /* parse a constant expression and return value in vtop. */
4253 static void expr_const1(void)
4255 int a;
4256 a = const_wanted;
4257 const_wanted = 1;
4258 expr_cond();
4259 const_wanted = a;
4262 /* parse an integer constant and return its value. */
4263 ST_FUNC int expr_const(void)
4265 int c;
4266 expr_const1();
4267 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4268 expect("constant expression");
4269 c = vtop->c.i;
4270 vpop();
4271 return c;
4274 /* return the label token if current token is a label, otherwise
4275 return zero */
4276 static int is_label(void)
4278 int last_tok;
4280 /* fast test first */
4281 if (tok < TOK_UIDENT)
4282 return 0;
4283 /* no need to save tokc because tok is an identifier */
4284 last_tok = tok;
4285 next();
4286 if (tok == ':') {
4287 next();
4288 return last_tok;
4289 } else {
4290 unget_tok(last_tok);
4291 return 0;
4295 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4296 int case_reg, int is_expr)
4298 int a, b, c, d;
4299 Sym *s;
4301 /* generate line number info */
4302 if (tcc_state->do_debug &&
4303 (last_line_num != file->line_num || last_ind != ind)) {
4304 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4305 last_ind = ind;
4306 last_line_num = file->line_num;
4309 if (is_expr) {
4310 /* default return value is (void) */
4311 vpushi(0);
4312 vtop->type.t = VT_VOID;
4315 if (tok == TOK_IF) {
4316 /* if test */
4317 next();
4318 skip('(');
4319 gexpr();
4320 skip(')');
4321 a = gtst(1, 0);
4322 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4323 c = tok;
4324 if (c == TOK_ELSE) {
4325 next();
4326 d = gjmp(0);
4327 gsym(a);
4328 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4329 gsym(d); /* patch else jmp */
4330 } else
4331 gsym(a);
4332 } else if (tok == TOK_WHILE) {
4333 next();
4334 d = ind;
4335 skip('(');
4336 gexpr();
4337 skip(')');
4338 a = gtst(1, 0);
4339 b = 0;
4340 block(&a, &b, case_sym, def_sym, case_reg, 0);
4341 gjmp_addr(d);
4342 gsym(a);
4343 gsym_addr(b, d);
4344 } else if (tok == '{') {
4345 Sym *llabel;
4347 next();
4348 /* record local declaration stack position */
4349 s = local_stack;
4350 llabel = local_label_stack;
4351 /* handle local labels declarations */
4352 if (tok == TOK_LABEL) {
4353 next();
4354 for(;;) {
4355 if (tok < TOK_UIDENT)
4356 expect("label identifier");
4357 label_push(&local_label_stack, tok, LABEL_DECLARED);
4358 next();
4359 if (tok == ',') {
4360 next();
4361 } else {
4362 skip(';');
4363 break;
4367 while (tok != '}') {
4368 decl(VT_LOCAL);
4369 if (tok != '}') {
4370 if (is_expr)
4371 vpop();
4372 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4375 /* pop locally defined labels */
4376 label_pop(&local_label_stack, llabel);
4377 if(is_expr) {
4378 /* XXX: this solution makes only valgrind happy...
4379 triggered by gcc.c-torture/execute/20000917-1.c */
4380 Sym *p;
4381 switch(vtop->type.t & VT_BTYPE) {
4382 case VT_PTR:
4383 case VT_STRUCT:
4384 case VT_ENUM:
4385 case VT_FUNC:
4386 for(p=vtop->type.ref;p;p=p->prev)
4387 if(p->prev==s)
4388 tcc_error("unsupported expression type");
4391 /* pop locally defined symbols */
4392 sym_pop(&local_stack, s);
4393 next();
4394 } else if (tok == TOK_RETURN) {
4395 next();
4396 if (tok != ';') {
4397 gexpr();
4398 gen_assign_cast(&func_vt);
4399 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4400 CType type;
4401 /* if returning structure, must copy it to implicit
4402 first pointer arg location */
4403 #ifdef TCC_ARM_EABI
4404 int align, size;
4405 size = type_size(&func_vt,&align);
4406 if(size <= 4)
4408 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4409 && (align & 3))
4411 int addr;
4412 loc = (loc - size) & -4;
4413 addr = loc;
4414 type = func_vt;
4415 vset(&type, VT_LOCAL | VT_LVAL, addr);
4416 vswap();
4417 vstore();
4418 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4420 vtop->type = int_type;
4421 gv(RC_IRET);
4422 } else {
4423 #endif
4424 type = func_vt;
4425 mk_pointer(&type);
4426 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4427 indir();
4428 vswap();
4429 /* copy structure value to pointer */
4430 vstore();
4431 #ifdef TCC_ARM_EABI
4433 #endif
4434 } else if (is_float(func_vt.t)) {
4435 gv(rc_fret(func_vt.t));
4436 } else {
4437 gv(RC_IRET);
4439 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4441 skip(';');
4442 rsym = gjmp(rsym); /* jmp */
4443 } else if (tok == TOK_BREAK) {
4444 /* compute jump */
4445 if (!bsym)
4446 tcc_error("cannot break");
4447 *bsym = gjmp(*bsym);
4448 next();
4449 skip(';');
4450 } else if (tok == TOK_CONTINUE) {
4451 /* compute jump */
4452 if (!csym)
4453 tcc_error("cannot continue");
4454 *csym = gjmp(*csym);
4455 next();
4456 skip(';');
4457 } else if (tok == TOK_FOR) {
4458 int e;
4459 next();
4460 skip('(');
4461 s = local_stack;
4462 if (tok != ';') {
4463 /* c99 for-loop init decl? */
4464 if (!decl0(VT_LOCAL, 1)) {
4465 /* no, regular for-loop init expr */
4466 gexpr();
4467 vpop();
4470 skip(';');
4471 d = ind;
4472 c = ind;
4473 a = 0;
4474 b = 0;
4475 if (tok != ';') {
4476 gexpr();
4477 a = gtst(1, 0);
4479 skip(';');
4480 if (tok != ')') {
4481 e = gjmp(0);
4482 c = ind;
4483 gexpr();
4484 vpop();
4485 gjmp_addr(d);
4486 gsym(e);
4488 skip(')');
4489 block(&a, &b, case_sym, def_sym, case_reg, 0);
4490 gjmp_addr(c);
4491 gsym(a);
4492 gsym_addr(b, c);
4493 sym_pop(&local_stack, s);
4494 } else
4495 if (tok == TOK_DO) {
4496 next();
4497 a = 0;
4498 b = 0;
4499 d = ind;
4500 block(&a, &b, case_sym, def_sym, case_reg, 0);
4501 skip(TOK_WHILE);
4502 skip('(');
4503 gsym(b);
4504 gexpr();
4505 c = gtst(0, 0);
4506 gsym_addr(c, d);
4507 skip(')');
4508 gsym(a);
4509 skip(';');
4510 } else
4511 if (tok == TOK_SWITCH) {
4512 next();
4513 skip('(');
4514 gexpr();
4515 /* XXX: other types than integer */
4516 case_reg = gv(RC_INT);
4517 vpop();
4518 skip(')');
4519 a = 0;
4520 b = gjmp(0); /* jump to first case */
4521 c = 0;
4522 block(&a, csym, &b, &c, case_reg, 0);
4523 /* if no default, jmp after switch */
4524 if (c == 0)
4525 c = ind;
4526 /* default label */
4527 gsym_addr(b, c);
4528 /* break label */
4529 gsym(a);
4530 } else
4531 if (tok == TOK_CASE) {
4532 int v1, v2;
4533 if (!case_sym)
4534 expect("switch");
4535 next();
4536 v1 = expr_const();
4537 v2 = v1;
4538 if (gnu_ext && tok == TOK_DOTS) {
4539 next();
4540 v2 = expr_const();
4541 if (v2 < v1)
4542 tcc_warning("empty case range");
4544 /* since a case is like a label, we must skip it with a jmp */
4545 b = gjmp(0);
4546 gsym(*case_sym);
4547 vseti(case_reg, 0);
4548 vpushi(v1);
4549 if (v1 == v2) {
4550 gen_op(TOK_EQ);
4551 *case_sym = gtst(1, 0);
4552 } else {
4553 gen_op(TOK_GE);
4554 *case_sym = gtst(1, 0);
4555 vseti(case_reg, 0);
4556 vpushi(v2);
4557 gen_op(TOK_LE);
4558 *case_sym = gtst(1, *case_sym);
4560 gsym(b);
4561 skip(':');
4562 is_expr = 0;
4563 goto block_after_label;
4564 } else
4565 if (tok == TOK_DEFAULT) {
4566 next();
4567 skip(':');
4568 if (!def_sym)
4569 expect("switch");
4570 if (*def_sym)
4571 tcc_error("too many 'default'");
4572 *def_sym = ind;
4573 is_expr = 0;
4574 goto block_after_label;
4575 } else
4576 if (tok == TOK_GOTO) {
4577 next();
4578 if (tok == '*' && gnu_ext) {
4579 /* computed goto */
4580 next();
4581 gexpr();
4582 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4583 expect("pointer");
4584 ggoto();
4585 } else if (tok >= TOK_UIDENT) {
4586 s = label_find(tok);
4587 /* put forward definition if needed */
4588 if (!s) {
4589 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4590 } else {
4591 if (s->r == LABEL_DECLARED)
4592 s->r = LABEL_FORWARD;
4594 /* label already defined */
4595 if (s->r & LABEL_FORWARD)
4596 s->jnext = gjmp(s->jnext);
4597 else
4598 gjmp_addr(s->jnext);
4599 next();
4600 } else {
4601 expect("label identifier");
4603 skip(';');
4604 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4605 asm_instr();
4606 } else {
4607 b = is_label();
4608 if (b) {
4609 /* label case */
4610 s = label_find(b);
4611 if (s) {
4612 if (s->r == LABEL_DEFINED)
4613 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4614 gsym(s->jnext);
4615 s->r = LABEL_DEFINED;
4616 } else {
4617 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4619 s->jnext = ind;
4620 /* we accept this, but it is a mistake */
4621 block_after_label:
4622 if (tok == '}') {
4623 tcc_warning("deprecated use of label at end of compound statement");
4624 } else {
4625 if (is_expr)
4626 vpop();
4627 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4629 } else {
4630 /* expression case */
4631 if (tok != ';') {
4632 if (is_expr) {
4633 vpop();
4634 gexpr();
4635 } else {
4636 gexpr();
4637 vpop();
4640 skip(';');
4645 /* t is the array or struct type. c is the array or struct
4646 address. cur_index/cur_field is the pointer to the current
4647 value. 'size_only' is true if only size info is needed (only used
4648 in arrays) */
4649 static void decl_designator(CType *type, Section *sec, unsigned long c,
4650 int *cur_index, Sym **cur_field,
4651 int size_only)
4653 Sym *s, *f;
4654 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4655 CType type1;
4657 notfirst = 0;
4658 elem_size = 0;
4659 nb_elems = 1;
4660 if (gnu_ext && (l = is_label()) != 0)
4661 goto struct_field;
4662 while (tok == '[' || tok == '.') {
4663 if (tok == '[') {
4664 if (!(type->t & VT_ARRAY))
4665 expect("array type");
4666 s = type->ref;
4667 next();
4668 index = expr_const();
4669 if (index < 0 || (s->c >= 0 && index >= s->c))
4670 expect("invalid index");
4671 if (tok == TOK_DOTS && gnu_ext) {
4672 next();
4673 index_last = expr_const();
4674 if (index_last < 0 ||
4675 (s->c >= 0 && index_last >= s->c) ||
4676 index_last < index)
4677 expect("invalid index");
4678 } else {
4679 index_last = index;
4681 skip(']');
4682 if (!notfirst)
4683 *cur_index = index_last;
4684 type = pointed_type(type);
4685 elem_size = type_size(type, &align);
4686 c += index * elem_size;
4687 /* NOTE: we only support ranges for last designator */
4688 nb_elems = index_last - index + 1;
4689 if (nb_elems != 1) {
4690 notfirst = 1;
4691 break;
4693 } else {
4694 next();
4695 l = tok;
4696 next();
4697 struct_field:
4698 if ((type->t & VT_BTYPE) != VT_STRUCT)
4699 expect("struct/union type");
4700 s = type->ref;
4701 l |= SYM_FIELD;
4702 f = s->next;
4703 while (f) {
4704 if (f->v == l)
4705 break;
4706 f = f->next;
4708 if (!f)
4709 expect("field");
4710 if (!notfirst)
4711 *cur_field = f;
4712 /* XXX: fix this mess by using explicit storage field */
4713 type1 = f->type;
4714 type1.t |= (type->t & ~VT_TYPE);
4715 type = &type1;
4716 c += f->c;
4718 notfirst = 1;
4720 if (notfirst) {
4721 if (tok == '=') {
4722 next();
4723 } else {
4724 if (!gnu_ext)
4725 expect("=");
4727 } else {
4728 if (type->t & VT_ARRAY) {
4729 index = *cur_index;
4730 type = pointed_type(type);
4731 c += index * type_size(type, &align);
4732 } else {
4733 f = *cur_field;
4734 if (!f)
4735 tcc_error("too many field init");
4736 /* XXX: fix this mess by using explicit storage field */
4737 type1 = f->type;
4738 type1.t |= (type->t & ~VT_TYPE);
4739 type = &type1;
4740 c += f->c;
4743 decl_initializer(type, sec, c, 0, size_only);
4745 /* XXX: make it more general */
4746 if (!size_only && nb_elems > 1) {
4747 unsigned long c_end;
4748 uint8_t *src, *dst;
4749 int i;
4751 if (!sec)
4752 tcc_error("range init not supported yet for dynamic storage");
4753 c_end = c + nb_elems * elem_size;
4754 if (c_end > sec->data_allocated)
4755 section_realloc(sec, c_end);
4756 src = sec->data + c;
4757 dst = src;
4758 for(i = 1; i < nb_elems; i++) {
4759 dst += elem_size;
4760 memcpy(dst, src, elem_size);
4765 #define EXPR_VAL 0
4766 #define EXPR_CONST 1
4767 #define EXPR_ANY 2
4769 /* store a value or an expression directly in global data or in local array */
4770 static void init_putv(CType *type, Section *sec, unsigned long c,
4771 int v, int expr_type)
4773 int saved_global_expr, bt, bit_pos, bit_size;
4774 void *ptr;
4775 unsigned long long bit_mask;
4776 CType dtype;
4778 switch(expr_type) {
4779 case EXPR_VAL:
4780 vpushi(v);
4781 break;
4782 case EXPR_CONST:
4783 /* compound literals must be allocated globally in this case */
4784 saved_global_expr = global_expr;
4785 global_expr = 1;
4786 expr_const1();
4787 global_expr = saved_global_expr;
4788 /* NOTE: symbols are accepted */
4789 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4790 tcc_error("initializer element is not constant");
4791 break;
4792 case EXPR_ANY:
4793 expr_eq();
4794 break;
4797 dtype = *type;
4798 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4800 if (sec) {
4801 /* XXX: not portable */
4802 /* XXX: generate error if incorrect relocation */
4803 gen_assign_cast(&dtype);
4804 bt = type->t & VT_BTYPE;
4805 /* we'll write at most 12 bytes */
4806 if (c + 12 > sec->data_allocated) {
4807 section_realloc(sec, c + 12);
4809 ptr = sec->data + c;
4810 /* XXX: make code faster ? */
4811 if (!(type->t & VT_BITFIELD)) {
4812 bit_pos = 0;
4813 bit_size = 32;
4814 bit_mask = -1LL;
4815 } else {
4816 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4817 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4818 bit_mask = (1LL << bit_size) - 1;
4820 if ((vtop->r & VT_SYM) &&
4821 (bt == VT_BYTE ||
4822 bt == VT_SHORT ||
4823 bt == VT_DOUBLE ||
4824 bt == VT_LDOUBLE ||
4825 bt == VT_LLONG ||
4826 (bt == VT_INT && bit_size != 32)))
4827 tcc_error("initializer element is not computable at load time");
4828 switch(bt) {
4829 case VT_BOOL:
4830 vtop->c.i = (vtop->c.i != 0);
4831 case VT_BYTE:
4832 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4833 break;
4834 case VT_SHORT:
4835 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4836 break;
4837 case VT_DOUBLE:
4838 *(double *)ptr = vtop->c.d;
4839 break;
4840 case VT_LDOUBLE:
4841 *(long double *)ptr = vtop->c.ld;
4842 break;
4843 case VT_LLONG:
4844 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4845 break;
4846 default:
4847 if (vtop->r & VT_SYM) {
4848 greloc(sec, vtop->sym, c, R_DATA_PTR);
4850 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4851 break;
4853 vtop--;
4854 } else {
4855 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4856 vswap();
4857 vstore();
4858 vpop();
4862 /* put zeros for variable based init */
4863 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4865 if (sec) {
4866 /* nothing to do because globals are already set to zero */
4867 } else {
4868 vpush_global_sym(&func_old_type, TOK_memset);
4869 vseti(VT_LOCAL, c);
4870 vpushi(0);
4871 vpushi(size);
4872 gfunc_call(3);
4876 /* 't' contains the type and storage info. 'c' is the offset of the
4877 object in section 'sec'. If 'sec' is NULL, it means stack based
4878 allocation. 'first' is true if array '{' must be read (multi
4879 dimension implicit array init handling). 'size_only' is true if
4880 size only evaluation is wanted (only for arrays). */
4881 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4882 int first, int size_only)
4884 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
4885 int size1, align1, expr_type;
4886 Sym *s, *f;
4887 CType *t1;
4889 if (type->t & VT_VLA) {
4890 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4891 int a;
4892 CValue retcval;
4894 vpush_global_sym(&func_old_type, TOK_alloca);
4895 vla_runtime_type_size(type, &a);
4896 gfunc_call(1);
4898 /* return value */
4899 retcval.i = 0;
4900 vsetc(type, REG_IRET, &retcval);
4901 vset(type, VT_LOCAL|VT_LVAL, c);
4902 vswap();
4903 vstore();
4904 vpop();
4905 #else
4906 tcc_error("variable length arrays unsupported for this target");
4907 #endif
4908 } else if (type->t & VT_ARRAY) {
4909 s = type->ref;
4910 n = s->c;
4911 array_length = 0;
4912 t1 = pointed_type(type);
4913 size1 = type_size(t1, &align1);
4915 no_oblock = 1;
4916 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4917 tok == '{') {
4918 if (tok != '{')
4919 tcc_error("character array initializer must be a literal,"
4920 " optionally enclosed in braces");
4921 skip('{');
4922 no_oblock = 0;
4925 /* only parse strings here if correct type (otherwise: handle
4926 them as ((w)char *) expressions */
4927 if ((tok == TOK_LSTR &&
4928 #ifdef TCC_TARGET_PE
4929 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4930 #else
4931 (t1->t & VT_BTYPE) == VT_INT
4932 #endif
4933 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4934 while (tok == TOK_STR || tok == TOK_LSTR) {
4935 int cstr_len, ch;
4936 CString *cstr;
4938 cstr = tokc.cstr;
4939 /* compute maximum number of chars wanted */
4940 if (tok == TOK_STR)
4941 cstr_len = cstr->size;
4942 else
4943 cstr_len = cstr->size / sizeof(nwchar_t);
4944 cstr_len--;
4945 nb = cstr_len;
4946 if (n >= 0 && nb > (n - array_length))
4947 nb = n - array_length;
4948 if (!size_only) {
4949 if (cstr_len > nb)
4950 tcc_warning("initializer-string for array is too long");
4951 /* in order to go faster for common case (char
4952 string in global variable, we handle it
4953 specifically */
4954 if (sec && tok == TOK_STR && size1 == 1) {
4955 memcpy(sec->data + c + array_length, cstr->data, nb);
4956 } else {
4957 for(i=0;i<nb;i++) {
4958 if (tok == TOK_STR)
4959 ch = ((unsigned char *)cstr->data)[i];
4960 else
4961 ch = ((nwchar_t *)cstr->data)[i];
4962 init_putv(t1, sec, c + (array_length + i) * size1,
4963 ch, EXPR_VAL);
4967 array_length += nb;
4968 next();
4970 /* only add trailing zero if enough storage (no
4971 warning in this case since it is standard) */
4972 if (n < 0 || array_length < n) {
4973 if (!size_only) {
4974 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4976 array_length++;
4978 } else {
4979 index = 0;
4980 while (tok != '}') {
4981 decl_designator(type, sec, c, &index, NULL, size_only);
4982 if (n >= 0 && index >= n)
4983 tcc_error("index too large");
4984 /* must put zero in holes (note that doing it that way
4985 ensures that it even works with designators) */
4986 if (!size_only && array_length < index) {
4987 init_putz(t1, sec, c + array_length * size1,
4988 (index - array_length) * size1);
4990 index++;
4991 if (index > array_length)
4992 array_length = index;
4993 /* special test for multi dimensional arrays (may not
4994 be strictly correct if designators are used at the
4995 same time) */
4996 if (index >= n && no_oblock)
4997 break;
4998 if (tok == '}')
4999 break;
5000 skip(',');
5003 if (!no_oblock)
5004 skip('}');
5005 /* put zeros at the end */
5006 if (!size_only && n >= 0 && array_length < n) {
5007 init_putz(t1, sec, c + array_length * size1,
5008 (n - array_length) * size1);
5010 /* patch type size if needed */
5011 if (n < 0)
5012 s->c = array_length;
5013 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5014 (sec || !first || tok == '{')) {
5015 int par_count;
5017 /* NOTE: the previous test is a specific case for automatic
5018 struct/union init */
5019 /* XXX: union needs only one init */
5021 /* XXX: this test is incorrect for local initializers
5022 beginning with ( without {. It would be much more difficult
5023 to do it correctly (ideally, the expression parser should
5024 be used in all cases) */
5025 par_count = 0;
5026 if (tok == '(') {
5027 AttributeDef ad1;
5028 CType type1;
5029 next();
5030 while (tok == '(') {
5031 par_count++;
5032 next();
5034 if (!parse_btype(&type1, &ad1))
5035 expect("cast");
5036 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5037 #if 0
5038 if (!is_assignable_types(type, &type1))
5039 tcc_error("invalid type for cast");
5040 #endif
5041 skip(')');
5043 no_oblock = 1;
5044 if (first || tok == '{') {
5045 skip('{');
5046 no_oblock = 0;
5048 s = type->ref;
5049 f = s->next;
5050 array_length = 0;
5051 index = 0;
5052 n = s->c;
5053 while (tok != '}') {
5054 decl_designator(type, sec, c, NULL, &f, size_only);
5055 index = f->c;
5056 if (!size_only && array_length < index) {
5057 init_putz(type, sec, c + array_length,
5058 index - array_length);
5060 index = index + type_size(&f->type, &align1);
5061 if (index > array_length)
5062 array_length = index;
5064 /* gr: skip fields from same union - ugly. */
5065 while (f->next) {
5066 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5067 /* test for same offset */
5068 if (f->next->c != f->c)
5069 break;
5070 /* if yes, test for bitfield shift */
5071 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5072 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5073 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5074 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5075 if (bit_pos_1 != bit_pos_2)
5076 break;
5078 f = f->next;
5081 f = f->next;
5082 if (no_oblock && f == NULL)
5083 break;
5084 if (tok == '}')
5085 break;
5086 skip(',');
5088 /* put zeros at the end */
5089 if (!size_only && array_length < n) {
5090 init_putz(type, sec, c + array_length,
5091 n - array_length);
5093 if (!no_oblock)
5094 skip('}');
5095 while (par_count) {
5096 skip(')');
5097 par_count--;
5099 } else if (tok == '{') {
5100 next();
5101 decl_initializer(type, sec, c, first, size_only);
5102 skip('}');
5103 } else if (size_only) {
5104 /* just skip expression */
5105 parlevel = parlevel1 = 0;
5106 while ((parlevel > 0 || parlevel1 > 0 ||
5107 (tok != '}' && tok != ',')) && tok != -1) {
5108 if (tok == '(')
5109 parlevel++;
5110 else if (tok == ')')
5111 parlevel--;
5112 else if (tok == '{')
5113 parlevel1++;
5114 else if (tok == '}')
5115 parlevel1--;
5116 next();
5118 } else {
5119 /* currently, we always use constant expression for globals
5120 (may change for scripting case) */
5121 expr_type = EXPR_CONST;
5122 if (!sec)
5123 expr_type = EXPR_ANY;
5124 init_putv(type, sec, c, 0, expr_type);
5128 /* parse an initializer for type 't' if 'has_init' is non zero, and
5129 allocate space in local or global data space ('r' is either
5130 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5131 variable 'v' with an associated name represented by 'asm_label' of
5132 scope 'scope' is declared before initializers are parsed. If 'v' is
5133 zero, then a reference to the new object is put in the value stack.
5134 If 'has_init' is 2, a special parsing is done to handle string
5135 constants. */
5136 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5137 int has_init, int v, char *asm_label,
5138 int scope)
5140 int size, align, addr, data_offset;
5141 int level;
5142 ParseState saved_parse_state = {0};
5143 TokenString init_str;
5144 Section *sec;
5145 Sym *flexible_array;
5147 flexible_array = NULL;
5148 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5149 Sym *field;
5150 field = type->ref;
5151 while (field && field->next)
5152 field = field->next;
5153 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5154 flexible_array = field;
5157 size = type_size(type, &align);
5158 /* If unknown size, we must evaluate it before
5159 evaluating initializers because
5160 initializers can generate global data too
5161 (e.g. string pointers or ISOC99 compound
5162 literals). It also simplifies local
5163 initializers handling */
5164 tok_str_new(&init_str);
5165 if (size < 0 || (flexible_array && has_init)) {
5166 if (!has_init)
5167 tcc_error("unknown type size");
5168 /* get all init string */
5169 if (has_init == 2) {
5170 /* only get strings */
5171 while (tok == TOK_STR || tok == TOK_LSTR) {
5172 tok_str_add_tok(&init_str);
5173 next();
5175 } else {
5176 level = 0;
5177 while (level > 0 || (tok != ',' && tok != ';')) {
5178 if (tok < 0)
5179 tcc_error("unexpected end of file in initializer");
5180 tok_str_add_tok(&init_str);
5181 if (tok == '{')
5182 level++;
5183 else if (tok == '}') {
5184 level--;
5185 if (level <= 0) {
5186 next();
5187 break;
5190 next();
5193 tok_str_add(&init_str, -1);
5194 tok_str_add(&init_str, 0);
5196 /* compute size */
5197 save_parse_state(&saved_parse_state);
5199 macro_ptr = init_str.str;
5200 next();
5201 decl_initializer(type, NULL, 0, 1, 1);
5202 /* prepare second initializer parsing */
5203 macro_ptr = init_str.str;
5204 next();
5206 /* if still unknown size, error */
5207 size = type_size(type, &align);
5208 if (size < 0)
5209 tcc_error("unknown type size");
5211 if (flexible_array)
5212 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5213 /* take into account specified alignment if bigger */
5214 if (ad->aligned) {
5215 if (ad->aligned > align)
5216 align = ad->aligned;
5217 } else if (ad->packed) {
5218 align = 1;
5220 if ((r & VT_VALMASK) == VT_LOCAL) {
5221 sec = NULL;
5222 #ifdef CONFIG_TCC_BCHECK
5223 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5224 loc--;
5226 #endif
5227 loc = (loc - size) & -align;
5228 addr = loc;
5229 #ifdef CONFIG_TCC_BCHECK
5230 /* handles bounds */
5231 /* XXX: currently, since we do only one pass, we cannot track
5232 '&' operators, so we add only arrays */
5233 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5234 unsigned long *bounds_ptr;
5235 /* add padding between regions */
5236 loc--;
5237 /* then add local bound info */
5238 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5239 bounds_ptr[0] = addr;
5240 bounds_ptr[1] = size;
5242 #endif
5243 if (v) {
5244 /* local variable */
5245 sym_push(v, type, r, addr);
5246 } else {
5247 /* push local reference */
5248 vset(type, r, addr);
5250 } else {
5251 Sym *sym;
5253 sym = NULL;
5254 if (v && scope == VT_CONST) {
5255 /* see if the symbol was already defined */
5256 sym = sym_find(v);
5257 if (sym) {
5258 if (!is_compatible_types(&sym->type, type))
5259 tcc_error("incompatible types for redefinition of '%s'",
5260 get_tok_str(v, NULL));
5261 if (sym->type.t & VT_EXTERN) {
5262 /* if the variable is extern, it was not allocated */
5263 sym->type.t &= ~VT_EXTERN;
5264 /* set array size if it was ommited in extern
5265 declaration */
5266 if ((sym->type.t & VT_ARRAY) &&
5267 sym->type.ref->c < 0 &&
5268 type->ref->c >= 0)
5269 sym->type.ref->c = type->ref->c;
5270 } else {
5271 /* we accept several definitions of the same
5272 global variable. this is tricky, because we
5273 must play with the SHN_COMMON type of the symbol */
5274 /* XXX: should check if the variable was already
5275 initialized. It is incorrect to initialized it
5276 twice */
5277 /* no init data, we won't add more to the symbol */
5278 if (!has_init)
5279 goto no_alloc;
5284 /* allocate symbol in corresponding section */
5285 sec = ad->section;
5286 if (!sec) {
5287 if (has_init)
5288 sec = data_section;
5289 else if (tcc_state->nocommon)
5290 sec = bss_section;
5292 if (sec) {
5293 data_offset = sec->data_offset;
5294 data_offset = (data_offset + align - 1) & -align;
5295 addr = data_offset;
5296 /* very important to increment global pointer at this time
5297 because initializers themselves can create new initializers */
5298 data_offset += size;
5299 #ifdef CONFIG_TCC_BCHECK
5300 /* add padding if bound check */
5301 if (tcc_state->do_bounds_check)
5302 data_offset++;
5303 #endif
5304 sec->data_offset = data_offset;
5305 /* allocate section space to put the data */
5306 if (sec->sh_type != SHT_NOBITS &&
5307 data_offset > sec->data_allocated)
5308 section_realloc(sec, data_offset);
5309 /* align section if needed */
5310 if (align > sec->sh_addralign)
5311 sec->sh_addralign = align;
5312 } else {
5313 addr = 0; /* avoid warning */
5316 if (v) {
5317 if (scope != VT_CONST || !sym) {
5318 sym = sym_push(v, type, r | VT_SYM, 0);
5319 sym->asm_label = asm_label;
5321 /* update symbol definition */
5322 if (sec) {
5323 put_extern_sym(sym, sec, addr, size);
5324 } else {
5325 ElfW(Sym) *esym;
5326 /* put a common area */
5327 put_extern_sym(sym, NULL, align, size);
5328 /* XXX: find a nicer way */
5329 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5330 esym->st_shndx = SHN_COMMON;
5332 } else {
5333 CValue cval;
5335 /* push global reference */
5336 sym = get_sym_ref(type, sec, addr, size);
5337 cval.ul = 0;
5338 vsetc(type, VT_CONST | VT_SYM, &cval);
5339 vtop->sym = sym;
5341 /* patch symbol weakness */
5342 if (type->t & VT_WEAK)
5343 weaken_symbol(sym);
5344 #ifdef CONFIG_TCC_BCHECK
5345 /* handles bounds now because the symbol must be defined
5346 before for the relocation */
5347 if (tcc_state->do_bounds_check) {
5348 unsigned long *bounds_ptr;
5350 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5351 /* then add global bound info */
5352 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5353 bounds_ptr[0] = 0; /* relocated */
5354 bounds_ptr[1] = size;
5356 #endif
5358 if (has_init || (type->t & VT_VLA)) {
5359 decl_initializer(type, sec, addr, 1, 0);
5360 /* restore parse state if needed */
5361 if (init_str.str) {
5362 tok_str_free(init_str.str);
5363 restore_parse_state(&saved_parse_state);
5365 /* patch flexible array member size back to -1, */
5366 /* for possible subsequent similar declarations */
5367 if (flexible_array)
5368 flexible_array->type.ref->c = -1;
5370 no_alloc: ;
5373 static void put_func_debug(Sym *sym)
5375 char buf[512];
5377 /* stabs info */
5378 /* XXX: we put here a dummy type */
5379 snprintf(buf, sizeof(buf), "%s:%c1",
5380 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5381 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5382 cur_text_section, sym->c);
5383 /* //gr gdb wants a line at the function */
5384 put_stabn(N_SLINE, 0, file->line_num, 0);
5385 last_ind = 0;
5386 last_line_num = 0;
5389 /* parse an old style function declaration list */
5390 /* XXX: check multiple parameter */
5391 static void func_decl_list(Sym *func_sym)
5393 AttributeDef ad;
5394 int v;
5395 Sym *s;
5396 CType btype, type;
5398 /* parse each declaration */
5399 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5400 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5401 if (!parse_btype(&btype, &ad))
5402 expect("declaration list");
5403 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5404 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5405 tok == ';') {
5406 /* we accept no variable after */
5407 } else {
5408 for(;;) {
5409 type = btype;
5410 type_decl(&type, &ad, &v, TYPE_DIRECT);
5411 /* find parameter in function parameter list */
5412 s = func_sym->next;
5413 while (s != NULL) {
5414 if ((s->v & ~SYM_FIELD) == v)
5415 goto found;
5416 s = s->next;
5418 tcc_error("declaration for parameter '%s' but no such parameter",
5419 get_tok_str(v, NULL));
5420 found:
5421 /* check that no storage specifier except 'register' was given */
5422 if (type.t & VT_STORAGE)
5423 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5424 convert_parameter_type(&type);
5425 /* we can add the type (NOTE: it could be local to the function) */
5426 s->type = type;
5427 /* accept other parameters */
5428 if (tok == ',')
5429 next();
5430 else
5431 break;
5434 skip(';');
5438 /* parse a function defined by symbol 'sym' and generate its code in
5439 'cur_text_section' */
5440 static void gen_function(Sym *sym)
5442 int saved_nocode_wanted = nocode_wanted;
5443 nocode_wanted = 0;
5444 ind = cur_text_section->data_offset;
5445 /* NOTE: we patch the symbol size later */
5446 put_extern_sym(sym, cur_text_section, ind, 0);
5447 funcname = get_tok_str(sym->v, NULL);
5448 func_ind = ind;
5449 /* put debug symbol */
5450 if (tcc_state->do_debug)
5451 put_func_debug(sym);
5452 /* push a dummy symbol to enable local sym storage */
5453 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5454 gfunc_prolog(&sym->type);
5455 rsym = 0;
5456 block(NULL, NULL, NULL, NULL, 0, 0);
5457 gsym(rsym);
5458 gfunc_epilog();
5459 cur_text_section->data_offset = ind;
5460 label_pop(&global_label_stack, NULL);
5461 sym_pop(&local_stack, NULL); /* reset local stack */
5462 /* end of function */
5463 /* patch symbol size */
5464 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5465 ind - func_ind;
5466 /* patch symbol weakness (this definition overrules any prototype) */
5467 if (sym->type.t & VT_WEAK)
5468 weaken_symbol(sym);
5469 if (tcc_state->do_debug) {
5470 put_stabn(N_FUN, 0, 0, ind - func_ind);
5472 /* It's better to crash than to generate wrong code */
5473 cur_text_section = NULL;
5474 funcname = ""; /* for safety */
5475 func_vt.t = VT_VOID; /* for safety */
5476 ind = 0; /* for safety */
5477 nocode_wanted = saved_nocode_wanted;
5480 ST_FUNC void gen_inline_functions(void)
5482 Sym *sym;
5483 int *str, inline_generated, i;
5484 struct InlineFunc *fn;
5486 /* iterate while inline function are referenced */
5487 for(;;) {
5488 inline_generated = 0;
5489 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5490 fn = tcc_state->inline_fns[i];
5491 sym = fn->sym;
5492 if (sym && sym->c) {
5493 /* the function was used: generate its code and
5494 convert it to a normal function */
5495 str = fn->token_str;
5496 fn->sym = NULL;
5497 if (file)
5498 strcpy(file->filename, fn->filename);
5499 sym->r = VT_SYM | VT_CONST;
5500 sym->type.t &= ~VT_INLINE;
5502 macro_ptr = str;
5503 next();
5504 cur_text_section = text_section;
5505 gen_function(sym);
5506 macro_ptr = NULL; /* fail safe */
5508 inline_generated = 1;
5511 if (!inline_generated)
5512 break;
5514 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5515 fn = tcc_state->inline_fns[i];
5516 str = fn->token_str;
5517 tok_str_free(str);
5519 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5522 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5523 static int decl0(int l, int is_for_loop_init)
5525 int v, has_init, r;
5526 CType type, btype;
5527 Sym *sym;
5528 AttributeDef ad;
5530 while (1) {
5531 if (!parse_btype(&btype, &ad)) {
5532 if (is_for_loop_init)
5533 return 0;
5534 /* skip redundant ';' */
5535 /* XXX: find more elegant solution */
5536 if (tok == ';') {
5537 next();
5538 continue;
5540 if (l == VT_CONST &&
5541 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5542 /* global asm block */
5543 asm_global_instr();
5544 continue;
5546 /* special test for old K&R protos without explicit int
5547 type. Only accepted when defining global data */
5548 if (l == VT_LOCAL || tok < TOK_DEFINE)
5549 break;
5550 btype.t = VT_INT;
5552 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5553 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5554 tok == ';') {
5555 /* we accept no variable after */
5556 next();
5557 continue;
5559 while (1) { /* iterate thru each declaration */
5560 char *asm_label; // associated asm label
5561 type = btype;
5562 type_decl(&type, &ad, &v, TYPE_DIRECT);
5563 #if 0
5565 char buf[500];
5566 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5567 printf("type = '%s'\n", buf);
5569 #endif
5570 if ((type.t & VT_BTYPE) == VT_FUNC) {
5571 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5572 tcc_error("function without file scope cannot be static");
5574 /* if old style function prototype, we accept a
5575 declaration list */
5576 sym = type.ref;
5577 if (sym->c == FUNC_OLD)
5578 func_decl_list(sym);
5581 asm_label = NULL;
5582 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5583 CString astr;
5585 asm_label_instr(&astr);
5586 asm_label = tcc_strdup(astr.data);
5587 cstr_free(&astr);
5589 /* parse one last attribute list, after asm label */
5590 parse_attribute(&ad);
5593 if (ad.weak)
5594 type.t |= VT_WEAK;
5595 #ifdef TCC_TARGET_PE
5596 if (ad.func_import)
5597 type.t |= VT_IMPORT;
5598 if (ad.func_export)
5599 type.t |= VT_EXPORT;
5600 #endif
5601 if (tok == '{') {
5602 if (l == VT_LOCAL)
5603 tcc_error("cannot use local functions");
5604 if ((type.t & VT_BTYPE) != VT_FUNC)
5605 expect("function definition");
5607 /* reject abstract declarators in function definition */
5608 sym = type.ref;
5609 while ((sym = sym->next) != NULL)
5610 if (!(sym->v & ~SYM_FIELD))
5611 expect("identifier");
5613 /* XXX: cannot do better now: convert extern line to static inline */
5614 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5615 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5617 sym = sym_find(v);
5618 if (sym) {
5619 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5620 goto func_error1;
5622 r = sym->type.ref->r;
5623 /* use func_call from prototype if not defined */
5624 if (FUNC_CALL(r) != FUNC_CDECL
5625 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5626 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5628 /* use export from prototype */
5629 if (FUNC_EXPORT(r))
5630 FUNC_EXPORT(type.ref->r) = 1;
5632 /* use static from prototype */
5633 if (sym->type.t & VT_STATIC)
5634 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5636 if (!is_compatible_types(&sym->type, &type)) {
5637 func_error1:
5638 tcc_error("incompatible types for redefinition of '%s'",
5639 get_tok_str(v, NULL));
5641 /* if symbol is already defined, then put complete type */
5642 sym->type = type;
5643 } else {
5644 /* put function symbol */
5645 sym = global_identifier_push(v, type.t, 0);
5646 sym->type.ref = type.ref;
5649 /* static inline functions are just recorded as a kind
5650 of macro. Their code will be emitted at the end of
5651 the compilation unit only if they are used */
5652 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5653 (VT_INLINE | VT_STATIC)) {
5654 TokenString func_str;
5655 int block_level;
5656 struct InlineFunc *fn;
5657 const char *filename;
5659 tok_str_new(&func_str);
5661 block_level = 0;
5662 for(;;) {
5663 int t;
5664 if (tok == TOK_EOF)
5665 tcc_error("unexpected end of file");
5666 tok_str_add_tok(&func_str);
5667 t = tok;
5668 next();
5669 if (t == '{') {
5670 block_level++;
5671 } else if (t == '}') {
5672 block_level--;
5673 if (block_level == 0)
5674 break;
5677 tok_str_add(&func_str, -1);
5678 tok_str_add(&func_str, 0);
5679 filename = file ? file->filename : "";
5680 fn = tcc_malloc(sizeof *fn + strlen(filename));
5681 strcpy(fn->filename, filename);
5682 fn->sym = sym;
5683 fn->token_str = func_str.str;
5684 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5686 } else {
5687 /* compute text section */
5688 cur_text_section = ad.section;
5689 if (!cur_text_section)
5690 cur_text_section = text_section;
5691 sym->r = VT_SYM | VT_CONST;
5692 gen_function(sym);
5694 break;
5695 } else {
5696 if (btype.t & VT_TYPEDEF) {
5697 /* save typedefed type */
5698 /* XXX: test storage specifiers ? */
5699 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5700 sym->type.t |= VT_TYPEDEF;
5701 } else {
5702 r = 0;
5703 if ((type.t & VT_BTYPE) == VT_FUNC) {
5704 /* external function definition */
5705 /* specific case for func_call attribute */
5706 type.ref->r = INT_ATTR(&ad);
5707 } else if (!(type.t & VT_ARRAY)) {
5708 /* not lvalue if array */
5709 r |= lvalue_type(type.t);
5711 has_init = (tok == '=');
5712 if (has_init && (type.t & VT_VLA))
5713 tcc_error("Variable length array cannot be initialized");
5714 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5715 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5716 !has_init && l == VT_CONST && type.ref->c < 0)) {
5717 /* external variable or function */
5718 /* NOTE: as GCC, uninitialized global static
5719 arrays of null size are considered as
5720 extern */
5721 sym = external_sym(v, &type, r, asm_label);
5723 if (type.t & VT_WEAK)
5724 weaken_symbol(sym);
5726 if (ad.alias_target) {
5727 Section tsec;
5728 Elf32_Sym *esym;
5729 Sym *alias_target;
5731 alias_target = sym_find(ad.alias_target);
5732 if (!alias_target || !alias_target->c)
5733 tcc_error("unsupported forward __alias__ attribute");
5734 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5735 tsec.sh_num = esym->st_shndx;
5736 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5738 } else {
5739 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5740 if (type.t & VT_STATIC)
5741 r |= VT_CONST;
5742 else
5743 r |= l;
5744 if (has_init)
5745 next();
5746 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5749 if (tok != ',') {
5750 if (is_for_loop_init)
5751 return 1;
5752 skip(';');
5753 break;
5755 next();
5757 ad.aligned = 0;
5760 return 0;
5763 ST_FUNC void decl(int l)
5765 decl0(l, 0);