Fix parsing function macro invocations
[tinycc.git] / tccgen.c
blob603161106be8bdb69b7636df2ad90b771a761c97
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 /* pop stack value */
971 ST_FUNC void vpop(void)
973 int v;
974 v = vtop->r & VT_VALMASK;
975 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
976 /* for x86, we need to pop the FP stack */
977 if (v == TREG_ST0 && !nocode_wanted) {
978 o(0xd8dd); /* fstp %st(0) */
979 } else
980 #endif
981 if (v == VT_JMP || v == VT_JMPI) {
982 /* need to put correct jump if && or || without test */
983 gsym(vtop->c.ul);
985 vtop--;
988 /* convert stack entry to register and duplicate its value in another
989 register */
990 static void gv_dup(void)
992 int rc, t, r, r1;
993 SValue sv;
995 t = vtop->type.t;
996 if ((t & VT_BTYPE) == VT_LLONG) {
997 lexpand();
998 gv_dup();
999 vswap();
1000 vrotb(3);
1001 gv_dup();
1002 vrotb(4);
1003 /* stack: H L L1 H1 */
1004 lbuild(t);
1005 vrotb(3);
1006 vrotb(3);
1007 vswap();
1008 lbuild(t);
1009 vswap();
1010 } else {
1011 /* duplicate value */
1012 rc = RC_INT;
1013 sv.type.t = VT_INT;
1014 if (is_float(t)) {
1015 rc = RC_FLOAT;
1016 #ifdef TCC_TARGET_X86_64
1017 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1018 rc = RC_ST0;
1020 #endif
1021 sv.type.t = t;
1023 r = gv(rc);
1024 r1 = get_reg(rc);
1025 sv.r = r;
1026 sv.c.ul = 0;
1027 load(r1, &sv); /* move r to r1 */
1028 vdup();
1029 /* duplicates value */
1030 if (r != r1)
1031 vtop->r = r1;
1035 #ifndef TCC_TARGET_X86_64
1036 /* generate CPU independent (unsigned) long long operations */
1037 static void gen_opl(int op)
1039 int t, a, b, op1, c, i;
1040 int func;
1041 unsigned short reg_iret = REG_IRET;
1042 unsigned short reg_lret = REG_LRET;
1043 SValue tmp;
1045 switch(op) {
1046 case '/':
1047 case TOK_PDIV:
1048 func = TOK___divdi3;
1049 goto gen_func;
1050 case TOK_UDIV:
1051 func = TOK___udivdi3;
1052 goto gen_func;
1053 case '%':
1054 func = TOK___moddi3;
1055 goto gen_mod_func;
1056 case TOK_UMOD:
1057 func = TOK___umoddi3;
1058 gen_mod_func:
1059 #ifdef TCC_ARM_EABI
1060 reg_iret = TREG_R2;
1061 reg_lret = TREG_R3;
1062 #endif
1063 gen_func:
1064 /* call generic long long function */
1065 vpush_global_sym(&func_old_type, func);
1066 vrott(3);
1067 gfunc_call(2);
1068 vpushi(0);
1069 vtop->r = reg_iret;
1070 vtop->r2 = reg_lret;
1071 break;
1072 case '^':
1073 case '&':
1074 case '|':
1075 case '*':
1076 case '+':
1077 case '-':
1078 t = vtop->type.t;
1079 vswap();
1080 lexpand();
1081 vrotb(3);
1082 lexpand();
1083 /* stack: L1 H1 L2 H2 */
1084 tmp = vtop[0];
1085 vtop[0] = vtop[-3];
1086 vtop[-3] = tmp;
1087 tmp = vtop[-2];
1088 vtop[-2] = vtop[-3];
1089 vtop[-3] = tmp;
1090 vswap();
1091 /* stack: H1 H2 L1 L2 */
1092 if (op == '*') {
1093 vpushv(vtop - 1);
1094 vpushv(vtop - 1);
1095 gen_op(TOK_UMULL);
1096 lexpand();
1097 /* stack: H1 H2 L1 L2 ML MH */
1098 for(i=0;i<4;i++)
1099 vrotb(6);
1100 /* stack: ML MH H1 H2 L1 L2 */
1101 tmp = vtop[0];
1102 vtop[0] = vtop[-2];
1103 vtop[-2] = tmp;
1104 /* stack: ML MH H1 L2 H2 L1 */
1105 gen_op('*');
1106 vrotb(3);
1107 vrotb(3);
1108 gen_op('*');
1109 /* stack: ML MH M1 M2 */
1110 gen_op('+');
1111 gen_op('+');
1112 } else if (op == '+' || op == '-') {
1113 /* XXX: add non carry method too (for MIPS or alpha) */
1114 if (op == '+')
1115 op1 = TOK_ADDC1;
1116 else
1117 op1 = TOK_SUBC1;
1118 gen_op(op1);
1119 /* stack: H1 H2 (L1 op L2) */
1120 vrotb(3);
1121 vrotb(3);
1122 gen_op(op1 + 1); /* TOK_xxxC2 */
1123 } else {
1124 gen_op(op);
1125 /* stack: H1 H2 (L1 op L2) */
1126 vrotb(3);
1127 vrotb(3);
1128 /* stack: (L1 op L2) H1 H2 */
1129 gen_op(op);
1130 /* stack: (L1 op L2) (H1 op H2) */
1132 /* stack: L H */
1133 lbuild(t);
1134 break;
1135 case TOK_SAR:
1136 case TOK_SHR:
1137 case TOK_SHL:
1138 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1139 t = vtop[-1].type.t;
1140 vswap();
1141 lexpand();
1142 vrotb(3);
1143 /* stack: L H shift */
1144 c = (int)vtop->c.i;
1145 /* constant: simpler */
1146 /* NOTE: all comments are for SHL. the other cases are
1147 done by swaping words */
1148 vpop();
1149 if (op != TOK_SHL)
1150 vswap();
1151 if (c >= 32) {
1152 /* stack: L H */
1153 vpop();
1154 if (c > 32) {
1155 vpushi(c - 32);
1156 gen_op(op);
1158 if (op != TOK_SAR) {
1159 vpushi(0);
1160 } else {
1161 gv_dup();
1162 vpushi(31);
1163 gen_op(TOK_SAR);
1165 vswap();
1166 } else {
1167 vswap();
1168 gv_dup();
1169 /* stack: H L L */
1170 vpushi(c);
1171 gen_op(op);
1172 vswap();
1173 vpushi(32 - c);
1174 if (op == TOK_SHL)
1175 gen_op(TOK_SHR);
1176 else
1177 gen_op(TOK_SHL);
1178 vrotb(3);
1179 /* stack: L L H */
1180 vpushi(c);
1181 if (op == TOK_SHL)
1182 gen_op(TOK_SHL);
1183 else
1184 gen_op(TOK_SHR);
1185 gen_op('|');
1187 if (op != TOK_SHL)
1188 vswap();
1189 lbuild(t);
1190 } else {
1191 /* XXX: should provide a faster fallback on x86 ? */
1192 switch(op) {
1193 case TOK_SAR:
1194 func = TOK___ashrdi3;
1195 goto gen_func;
1196 case TOK_SHR:
1197 func = TOK___lshrdi3;
1198 goto gen_func;
1199 case TOK_SHL:
1200 func = TOK___ashldi3;
1201 goto gen_func;
1204 break;
1205 default:
1206 /* compare operations */
1207 t = vtop->type.t;
1208 vswap();
1209 lexpand();
1210 vrotb(3);
1211 lexpand();
1212 /* stack: L1 H1 L2 H2 */
1213 tmp = vtop[-1];
1214 vtop[-1] = vtop[-2];
1215 vtop[-2] = tmp;
1216 /* stack: L1 L2 H1 H2 */
1217 /* compare high */
1218 op1 = op;
1219 /* when values are equal, we need to compare low words. since
1220 the jump is inverted, we invert the test too. */
1221 if (op1 == TOK_LT)
1222 op1 = TOK_LE;
1223 else if (op1 == TOK_GT)
1224 op1 = TOK_GE;
1225 else if (op1 == TOK_ULT)
1226 op1 = TOK_ULE;
1227 else if (op1 == TOK_UGT)
1228 op1 = TOK_UGE;
1229 a = 0;
1230 b = 0;
1231 gen_op(op1);
1232 if (op1 != TOK_NE) {
1233 a = gtst(1, 0);
1235 if (op != TOK_EQ) {
1236 /* generate non equal test */
1237 /* XXX: NOT PORTABLE yet */
1238 if (a == 0) {
1239 b = gtst(0, 0);
1240 } else {
1241 #if defined(TCC_TARGET_I386)
1242 b = psym(0x850f, 0);
1243 #elif defined(TCC_TARGET_ARM)
1244 b = ind;
1245 o(0x1A000000 | encbranch(ind, 0, 1));
1246 #elif defined(TCC_TARGET_C67)
1247 tcc_error("not implemented");
1248 #else
1249 #error not supported
1250 #endif
1253 /* compare low. Always unsigned */
1254 op1 = op;
1255 if (op1 == TOK_LT)
1256 op1 = TOK_ULT;
1257 else if (op1 == TOK_LE)
1258 op1 = TOK_ULE;
1259 else if (op1 == TOK_GT)
1260 op1 = TOK_UGT;
1261 else if (op1 == TOK_GE)
1262 op1 = TOK_UGE;
1263 gen_op(op1);
1264 a = gtst(1, a);
1265 gsym(b);
1266 vseti(VT_JMPI, a);
1267 break;
1270 #endif
1272 /* handle integer constant optimizations and various machine
1273 independent opt */
1274 static void gen_opic(int op)
1276 int c1, c2, t1, t2, n;
1277 SValue *v1, *v2;
1278 long long l1, l2;
1279 typedef unsigned long long U;
1281 v1 = vtop - 1;
1282 v2 = vtop;
1283 t1 = v1->type.t & VT_BTYPE;
1284 t2 = v2->type.t & VT_BTYPE;
1286 if (t1 == VT_LLONG)
1287 l1 = v1->c.ll;
1288 else if (v1->type.t & VT_UNSIGNED)
1289 l1 = v1->c.ui;
1290 else
1291 l1 = v1->c.i;
1293 if (t2 == VT_LLONG)
1294 l2 = v2->c.ll;
1295 else if (v2->type.t & VT_UNSIGNED)
1296 l2 = v2->c.ui;
1297 else
1298 l2 = v2->c.i;
1300 /* currently, we cannot do computations with forward symbols */
1301 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1302 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1303 if (c1 && c2) {
1304 switch(op) {
1305 case '+': l1 += l2; break;
1306 case '-': l1 -= l2; break;
1307 case '&': l1 &= l2; break;
1308 case '^': l1 ^= l2; break;
1309 case '|': l1 |= l2; break;
1310 case '*': l1 *= l2; break;
1312 case TOK_PDIV:
1313 case '/':
1314 case '%':
1315 case TOK_UDIV:
1316 case TOK_UMOD:
1317 /* if division by zero, generate explicit division */
1318 if (l2 == 0) {
1319 if (const_wanted)
1320 tcc_error("division by zero in constant");
1321 goto general_case;
1323 switch(op) {
1324 default: l1 /= l2; break;
1325 case '%': l1 %= l2; break;
1326 case TOK_UDIV: l1 = (U)l1 / l2; break;
1327 case TOK_UMOD: l1 = (U)l1 % l2; break;
1329 break;
1330 case TOK_SHL: l1 <<= l2; break;
1331 case TOK_SHR: l1 = (U)l1 >> l2; break;
1332 case TOK_SAR: l1 >>= l2; break;
1333 /* tests */
1334 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1335 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1336 case TOK_EQ: l1 = l1 == l2; break;
1337 case TOK_NE: l1 = l1 != l2; break;
1338 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1339 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1340 case TOK_LT: l1 = l1 < l2; break;
1341 case TOK_GE: l1 = l1 >= l2; break;
1342 case TOK_LE: l1 = l1 <= l2; break;
1343 case TOK_GT: l1 = l1 > l2; break;
1344 /* logical */
1345 case TOK_LAND: l1 = l1 && l2; break;
1346 case TOK_LOR: l1 = l1 || l2; break;
1347 default:
1348 goto general_case;
1350 v1->c.ll = l1;
1351 vtop--;
1352 } else {
1353 /* if commutative ops, put c2 as constant */
1354 if (c1 && (op == '+' || op == '&' || op == '^' ||
1355 op == '|' || op == '*')) {
1356 vswap();
1357 c2 = c1; //c = c1, c1 = c2, c2 = c;
1358 l2 = l1; //l = l1, l1 = l2, l2 = l;
1360 /* Filter out NOP operations like x*1, x-0, x&-1... */
1361 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1362 op == TOK_PDIV) &&
1363 l2 == 1) ||
1364 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1365 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1366 l2 == 0) ||
1367 (op == '&' &&
1368 l2 == -1))) {
1369 /* nothing to do */
1370 vtop--;
1371 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1372 /* try to use shifts instead of muls or divs */
1373 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1374 n = -1;
1375 while (l2) {
1376 l2 >>= 1;
1377 n++;
1379 vtop->c.ll = n;
1380 if (op == '*')
1381 op = TOK_SHL;
1382 else if (op == TOK_PDIV)
1383 op = TOK_SAR;
1384 else
1385 op = TOK_SHR;
1387 goto general_case;
1388 } else if (c2 && (op == '+' || op == '-') &&
1389 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1390 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1391 /* symbol + constant case */
1392 if (op == '-')
1393 l2 = -l2;
1394 vtop--;
1395 vtop->c.ll += l2;
1396 } else {
1397 general_case:
1398 if (!nocode_wanted) {
1399 /* call low level op generator */
1400 if (t1 == VT_LLONG || t2 == VT_LLONG)
1401 gen_opl(op);
1402 else
1403 gen_opi(op);
1404 } else {
1405 vtop--;
1411 /* generate a floating point operation with constant propagation */
1412 static void gen_opif(int op)
1414 int c1, c2;
1415 SValue *v1, *v2;
1416 long double f1, f2;
1418 v1 = vtop - 1;
1419 v2 = vtop;
1420 /* currently, we cannot do computations with forward symbols */
1421 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1422 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1423 if (c1 && c2) {
1424 if (v1->type.t == VT_FLOAT) {
1425 f1 = v1->c.f;
1426 f2 = v2->c.f;
1427 } else if (v1->type.t == VT_DOUBLE) {
1428 f1 = v1->c.d;
1429 f2 = v2->c.d;
1430 } else {
1431 f1 = v1->c.ld;
1432 f2 = v2->c.ld;
1435 /* NOTE: we only do constant propagation if finite number (not
1436 NaN or infinity) (ANSI spec) */
1437 if (!ieee_finite(f1) || !ieee_finite(f2))
1438 goto general_case;
1440 switch(op) {
1441 case '+': f1 += f2; break;
1442 case '-': f1 -= f2; break;
1443 case '*': f1 *= f2; break;
1444 case '/':
1445 if (f2 == 0.0) {
1446 if (const_wanted)
1447 tcc_error("division by zero in constant");
1448 goto general_case;
1450 f1 /= f2;
1451 break;
1452 /* XXX: also handles tests ? */
1453 default:
1454 goto general_case;
1456 /* XXX: overflow test ? */
1457 if (v1->type.t == VT_FLOAT) {
1458 v1->c.f = f1;
1459 } else if (v1->type.t == VT_DOUBLE) {
1460 v1->c.d = f1;
1461 } else {
1462 v1->c.ld = f1;
1464 vtop--;
1465 } else {
1466 general_case:
1467 if (!nocode_wanted) {
1468 gen_opf(op);
1469 } else {
1470 vtop--;
1475 static int pointed_size(CType *type)
1477 int align;
1478 return type_size(pointed_type(type), &align);
1481 static void vla_runtime_pointed_size(CType *type)
1483 int align;
1484 vla_runtime_type_size(pointed_type(type), &align);
1487 static inline int is_null_pointer(SValue *p)
1489 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1490 return 0;
1491 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1492 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1493 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0);
1496 static inline int is_integer_btype(int bt)
1498 return (bt == VT_BYTE || bt == VT_SHORT ||
1499 bt == VT_INT || bt == VT_LLONG);
1502 /* check types for comparison or substraction of pointers */
1503 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1505 CType *type1, *type2, tmp_type1, tmp_type2;
1506 int bt1, bt2;
1508 /* null pointers are accepted for all comparisons as gcc */
1509 if (is_null_pointer(p1) || is_null_pointer(p2))
1510 return;
1511 type1 = &p1->type;
1512 type2 = &p2->type;
1513 bt1 = type1->t & VT_BTYPE;
1514 bt2 = type2->t & VT_BTYPE;
1515 /* accept comparison between pointer and integer with a warning */
1516 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1517 if (op != TOK_LOR && op != TOK_LAND )
1518 tcc_warning("comparison between pointer and integer");
1519 return;
1522 /* both must be pointers or implicit function pointers */
1523 if (bt1 == VT_PTR) {
1524 type1 = pointed_type(type1);
1525 } else if (bt1 != VT_FUNC)
1526 goto invalid_operands;
1528 if (bt2 == VT_PTR) {
1529 type2 = pointed_type(type2);
1530 } else if (bt2 != VT_FUNC) {
1531 invalid_operands:
1532 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1534 if ((type1->t & VT_BTYPE) == VT_VOID ||
1535 (type2->t & VT_BTYPE) == VT_VOID)
1536 return;
1537 tmp_type1 = *type1;
1538 tmp_type2 = *type2;
1539 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1540 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1541 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1542 /* gcc-like error if '-' is used */
1543 if (op == '-')
1544 goto invalid_operands;
1545 else
1546 tcc_warning("comparison of distinct pointer types lacks a cast");
1550 /* generic gen_op: handles types problems */
1551 ST_FUNC void gen_op(int op)
1553 int u, t1, t2, bt1, bt2, t;
1554 CType type1;
1556 t1 = vtop[-1].type.t;
1557 t2 = vtop[0].type.t;
1558 bt1 = t1 & VT_BTYPE;
1559 bt2 = t2 & VT_BTYPE;
1561 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1562 /* at least one operand is a pointer */
1563 /* relationnal op: must be both pointers */
1564 if (op >= TOK_ULT && op <= TOK_LOR) {
1565 check_comparison_pointer_types(vtop - 1, vtop, op);
1566 /* pointers are handled are unsigned */
1567 #ifdef TCC_TARGET_X86_64
1568 t = VT_LLONG | VT_UNSIGNED;
1569 #else
1570 t = VT_INT | VT_UNSIGNED;
1571 #endif
1572 goto std_op;
1574 /* if both pointers, then it must be the '-' op */
1575 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1576 if (op != '-')
1577 tcc_error("cannot use pointers here");
1578 check_comparison_pointer_types(vtop - 1, vtop, op);
1579 /* XXX: check that types are compatible */
1580 if (vtop[-1].type.t & VT_VLA) {
1581 vla_runtime_pointed_size(&vtop[-1].type);
1582 } else {
1583 vpushi(pointed_size(&vtop[-1].type));
1585 vrott(3);
1586 gen_opic(op);
1587 /* set to integer type */
1588 #ifdef TCC_TARGET_X86_64
1589 vtop->type.t = VT_LLONG;
1590 #else
1591 vtop->type.t = VT_INT;
1592 #endif
1593 vswap();
1594 gen_op(TOK_PDIV);
1595 } else {
1596 /* exactly one pointer : must be '+' or '-'. */
1597 if (op != '-' && op != '+')
1598 tcc_error("cannot use pointers here");
1599 /* Put pointer as first operand */
1600 if (bt2 == VT_PTR) {
1601 vswap();
1602 swap(&t1, &t2);
1604 type1 = vtop[-1].type;
1605 type1.t &= ~VT_ARRAY;
1606 if (vtop[-1].type.t & VT_VLA)
1607 vla_runtime_pointed_size(&vtop[-1].type);
1608 else {
1609 u = pointed_size(&vtop[-1].type);
1610 if (u < 0)
1611 tcc_error("unknown array element size");
1612 #ifdef TCC_TARGET_X86_64
1613 vpushll(u);
1614 #else
1615 /* XXX: cast to int ? (long long case) */
1616 vpushi(u);
1617 #endif
1619 gen_op('*');
1620 #ifdef CONFIG_TCC_BCHECK
1621 /* if evaluating constant expression, no code should be
1622 generated, so no bound check */
1623 if (tcc_state->do_bounds_check && !const_wanted) {
1624 /* if bounded pointers, we generate a special code to
1625 test bounds */
1626 if (op == '-') {
1627 vpushi(0);
1628 vswap();
1629 gen_op('-');
1631 gen_bounded_ptr_add();
1632 } else
1633 #endif
1635 gen_opic(op);
1637 /* put again type if gen_opic() swaped operands */
1638 vtop->type = type1;
1640 } else if (is_float(bt1) || is_float(bt2)) {
1641 /* compute bigger type and do implicit casts */
1642 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1643 t = VT_LDOUBLE;
1644 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1645 t = VT_DOUBLE;
1646 } else {
1647 t = VT_FLOAT;
1649 /* floats can only be used for a few operations */
1650 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1651 (op < TOK_ULT || op > TOK_GT))
1652 tcc_error("invalid operands for binary operation");
1653 goto std_op;
1654 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1655 /* cast to biggest op */
1656 t = VT_LLONG;
1657 /* convert to unsigned if it does not fit in a long long */
1658 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1659 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1660 t |= VT_UNSIGNED;
1661 goto std_op;
1662 } else {
1663 /* integer operations */
1664 t = VT_INT;
1665 /* convert to unsigned if it does not fit in an integer */
1666 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1667 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1668 t |= VT_UNSIGNED;
1669 std_op:
1670 /* XXX: currently, some unsigned operations are explicit, so
1671 we modify them here */
1672 if (t & VT_UNSIGNED) {
1673 if (op == TOK_SAR)
1674 op = TOK_SHR;
1675 else if (op == '/')
1676 op = TOK_UDIV;
1677 else if (op == '%')
1678 op = TOK_UMOD;
1679 else if (op == TOK_LT)
1680 op = TOK_ULT;
1681 else if (op == TOK_GT)
1682 op = TOK_UGT;
1683 else if (op == TOK_LE)
1684 op = TOK_ULE;
1685 else if (op == TOK_GE)
1686 op = TOK_UGE;
1688 vswap();
1689 type1.t = t;
1690 gen_cast(&type1);
1691 vswap();
1692 /* special case for shifts and long long: we keep the shift as
1693 an integer */
1694 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1695 type1.t = VT_INT;
1696 gen_cast(&type1);
1697 if (is_float(t))
1698 gen_opif(op);
1699 else
1700 gen_opic(op);
1701 if (op >= TOK_ULT && op <= TOK_GT) {
1702 /* relationnal op: the result is an int */
1703 vtop->type.t = VT_INT;
1704 } else {
1705 vtop->type.t = t;
1710 #ifndef TCC_TARGET_ARM
1711 /* generic itof for unsigned long long case */
1712 static void gen_cvt_itof1(int t)
1714 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1715 (VT_LLONG | VT_UNSIGNED)) {
1717 if (t == VT_FLOAT)
1718 vpush_global_sym(&func_old_type, TOK___floatundisf);
1719 #if LDOUBLE_SIZE != 8
1720 else if (t == VT_LDOUBLE)
1721 vpush_global_sym(&func_old_type, TOK___floatundixf);
1722 #endif
1723 else
1724 vpush_global_sym(&func_old_type, TOK___floatundidf);
1725 vrott(2);
1726 gfunc_call(1);
1727 vpushi(0);
1728 vtop->r = reg_fret(t);
1729 } else {
1730 gen_cvt_itof(t);
1733 #endif
1735 /* generic ftoi for unsigned long long case */
1736 static void gen_cvt_ftoi1(int t)
1738 int st;
1740 if (t == (VT_LLONG | VT_UNSIGNED)) {
1741 /* not handled natively */
1742 st = vtop->type.t & VT_BTYPE;
1743 if (st == VT_FLOAT)
1744 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1745 #if LDOUBLE_SIZE != 8
1746 else if (st == VT_LDOUBLE)
1747 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1748 #endif
1749 else
1750 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1751 vrott(2);
1752 gfunc_call(1);
1753 vpushi(0);
1754 vtop->r = REG_IRET;
1755 vtop->r2 = REG_LRET;
1756 } else {
1757 gen_cvt_ftoi(t);
1761 /* force char or short cast */
1762 static void force_charshort_cast(int t)
1764 int bits, dbt;
1765 dbt = t & VT_BTYPE;
1766 /* XXX: add optimization if lvalue : just change type and offset */
1767 if (dbt == VT_BYTE)
1768 bits = 8;
1769 else
1770 bits = 16;
1771 if (t & VT_UNSIGNED) {
1772 vpushi((1 << bits) - 1);
1773 gen_op('&');
1774 } else {
1775 bits = 32 - bits;
1776 vpushi(bits);
1777 gen_op(TOK_SHL);
1778 /* result must be signed or the SAR is converted to an SHL
1779 This was not the case when "t" was a signed short
1780 and the last value on the stack was an unsigned int */
1781 vtop->type.t &= ~VT_UNSIGNED;
1782 vpushi(bits);
1783 gen_op(TOK_SAR);
1787 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1788 static void gen_cast(CType *type)
1790 int sbt, dbt, sf, df, c, p;
1792 /* special delayed cast for char/short */
1793 /* XXX: in some cases (multiple cascaded casts), it may still
1794 be incorrect */
1795 if (vtop->r & VT_MUSTCAST) {
1796 vtop->r &= ~VT_MUSTCAST;
1797 force_charshort_cast(vtop->type.t);
1800 /* bitfields first get cast to ints */
1801 if (vtop->type.t & VT_BITFIELD) {
1802 gv(RC_INT);
1805 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1806 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1808 if (sbt != dbt) {
1809 sf = is_float(sbt);
1810 df = is_float(dbt);
1811 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1812 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1813 if (c) {
1814 /* constant case: we can do it now */
1815 /* XXX: in ISOC, cannot do it if error in convert */
1816 if (sbt == VT_FLOAT)
1817 vtop->c.ld = vtop->c.f;
1818 else if (sbt == VT_DOUBLE)
1819 vtop->c.ld = vtop->c.d;
1821 if (df) {
1822 if ((sbt & VT_BTYPE) == VT_LLONG) {
1823 if (sbt & VT_UNSIGNED)
1824 vtop->c.ld = vtop->c.ull;
1825 else
1826 vtop->c.ld = vtop->c.ll;
1827 } else if(!sf) {
1828 if (sbt & VT_UNSIGNED)
1829 vtop->c.ld = vtop->c.ui;
1830 else
1831 vtop->c.ld = vtop->c.i;
1834 if (dbt == VT_FLOAT)
1835 vtop->c.f = (float)vtop->c.ld;
1836 else if (dbt == VT_DOUBLE)
1837 vtop->c.d = (double)vtop->c.ld;
1838 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1839 vtop->c.ull = (unsigned long long)vtop->c.ld;
1840 } else if (sf && dbt == VT_BOOL) {
1841 vtop->c.i = (vtop->c.ld != 0);
1842 } else {
1843 if(sf)
1844 vtop->c.ll = (long long)vtop->c.ld;
1845 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1846 vtop->c.ll = vtop->c.ull;
1847 else if (sbt & VT_UNSIGNED)
1848 vtop->c.ll = vtop->c.ui;
1849 #ifdef TCC_TARGET_X86_64
1850 else if (sbt == VT_PTR)
1852 #endif
1853 else if (sbt != VT_LLONG)
1854 vtop->c.ll = vtop->c.i;
1856 if (dbt == (VT_LLONG|VT_UNSIGNED))
1857 vtop->c.ull = vtop->c.ll;
1858 else if (dbt == VT_BOOL)
1859 vtop->c.i = (vtop->c.ll != 0);
1860 else if (dbt != VT_LLONG) {
1861 int s = 0;
1862 if ((dbt & VT_BTYPE) == VT_BYTE)
1863 s = 24;
1864 else if ((dbt & VT_BTYPE) == VT_SHORT)
1865 s = 16;
1867 if(dbt & VT_UNSIGNED)
1868 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1869 else
1870 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1873 } else if (p && dbt == VT_BOOL) {
1874 vtop->r = VT_CONST;
1875 vtop->c.i = 1;
1876 } else if (!nocode_wanted) {
1877 /* non constant case: generate code */
1878 if (sf && df) {
1879 /* convert from fp to fp */
1880 gen_cvt_ftof(dbt);
1881 } else if (df) {
1882 /* convert int to fp */
1883 gen_cvt_itof1(dbt);
1884 } else if (sf) {
1885 /* convert fp to int */
1886 if (dbt == VT_BOOL) {
1887 vpushi(0);
1888 gen_op(TOK_NE);
1889 } else {
1890 /* we handle char/short/etc... with generic code */
1891 if (dbt != (VT_INT | VT_UNSIGNED) &&
1892 dbt != (VT_LLONG | VT_UNSIGNED) &&
1893 dbt != VT_LLONG)
1894 dbt = VT_INT;
1895 gen_cvt_ftoi1(dbt);
1896 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1897 /* additional cast for char/short... */
1898 vtop->type.t = dbt;
1899 gen_cast(type);
1902 #ifndef TCC_TARGET_X86_64
1903 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1904 if ((sbt & VT_BTYPE) != VT_LLONG) {
1905 /* scalar to long long */
1906 /* machine independent conversion */
1907 gv(RC_INT);
1908 /* generate high word */
1909 if (sbt == (VT_INT | VT_UNSIGNED)) {
1910 vpushi(0);
1911 gv(RC_INT);
1912 } else {
1913 if (sbt == VT_PTR) {
1914 /* cast from pointer to int before we apply
1915 shift operation, which pointers don't support*/
1916 gen_cast(&int_type);
1918 gv_dup();
1919 vpushi(31);
1920 gen_op(TOK_SAR);
1922 /* patch second register */
1923 vtop[-1].r2 = vtop->r;
1924 vpop();
1926 #else
1927 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1928 (dbt & VT_BTYPE) == VT_PTR ||
1929 (dbt & VT_BTYPE) == VT_FUNC) {
1930 if ((sbt & VT_BTYPE) != VT_LLONG &&
1931 (sbt & VT_BTYPE) != VT_PTR &&
1932 (sbt & VT_BTYPE) != VT_FUNC) {
1933 /* need to convert from 32bit to 64bit */
1934 int r = gv(RC_INT);
1935 if (sbt != (VT_INT | VT_UNSIGNED)) {
1936 /* x86_64 specific: movslq */
1937 o(0x6348);
1938 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1941 #endif
1942 } else if (dbt == VT_BOOL) {
1943 /* scalar to bool */
1944 vpushi(0);
1945 gen_op(TOK_NE);
1946 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1947 (dbt & VT_BTYPE) == VT_SHORT) {
1948 if (sbt == VT_PTR) {
1949 vtop->type.t = VT_INT;
1950 tcc_warning("nonportable conversion from pointer to char/short");
1952 force_charshort_cast(dbt);
1953 } else if ((dbt & VT_BTYPE) == VT_INT) {
1954 /* scalar to int */
1955 if (sbt == VT_LLONG) {
1956 /* from long long: just take low order word */
1957 lexpand();
1958 vpop();
1960 /* if lvalue and single word type, nothing to do because
1961 the lvalue already contains the real type size (see
1962 VT_LVAL_xxx constants) */
1965 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1966 /* if we are casting between pointer types,
1967 we must update the VT_LVAL_xxx size */
1968 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1969 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1971 vtop->type = *type;
1974 /* return type size as known at compile time. Put alignment at 'a' */
1975 ST_FUNC int type_size(CType *type, int *a)
1977 Sym *s;
1978 int bt;
1980 bt = type->t & VT_BTYPE;
1981 if (bt == VT_STRUCT) {
1982 /* struct/union */
1983 s = type->ref;
1984 *a = s->r;
1985 return s->c;
1986 } else if (bt == VT_PTR) {
1987 if (type->t & VT_ARRAY) {
1988 int ts;
1990 s = type->ref;
1991 ts = type_size(&s->type, a);
1993 if (ts < 0 && s->c < 0)
1994 ts = -ts;
1996 return ts * s->c;
1997 } else {
1998 *a = PTR_SIZE;
1999 return PTR_SIZE;
2001 } else if (bt == VT_LDOUBLE) {
2002 *a = LDOUBLE_ALIGN;
2003 return LDOUBLE_SIZE;
2004 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2005 #ifdef TCC_TARGET_I386
2006 #ifdef TCC_TARGET_PE
2007 *a = 8;
2008 #else
2009 *a = 4;
2010 #endif
2011 #elif defined(TCC_TARGET_ARM)
2012 #ifdef TCC_ARM_EABI
2013 *a = 8;
2014 #else
2015 *a = 4;
2016 #endif
2017 #else
2018 *a = 8;
2019 #endif
2020 return 8;
2021 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2022 *a = 4;
2023 return 4;
2024 } else if (bt == VT_SHORT) {
2025 *a = 2;
2026 return 2;
2027 } else {
2028 /* char, void, function, _Bool */
2029 *a = 1;
2030 return 1;
2034 /* push type size as known at runtime time on top of value stack. Put
2035 alignment at 'a' */
2036 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2038 if (type->t & VT_VLA) {
2039 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2040 } else {
2041 vpushi(type_size(type, a));
2045 /* return the pointed type of t */
2046 static inline CType *pointed_type(CType *type)
2048 return &type->ref->type;
2051 /* modify type so that its it is a pointer to type. */
2052 ST_FUNC void mk_pointer(CType *type)
2054 Sym *s;
2055 s = sym_push(SYM_FIELD, type, 0, -1);
2056 type->t = VT_PTR | (type->t & ~VT_TYPE);
2057 type->ref = s;
2060 /* compare function types. OLD functions match any new functions */
2061 static int is_compatible_func(CType *type1, CType *type2)
2063 Sym *s1, *s2;
2065 s1 = type1->ref;
2066 s2 = type2->ref;
2067 if (!is_compatible_types(&s1->type, &s2->type))
2068 return 0;
2069 /* check func_call */
2070 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2071 return 0;
2072 /* XXX: not complete */
2073 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2074 return 1;
2075 if (s1->c != s2->c)
2076 return 0;
2077 while (s1 != NULL) {
2078 if (s2 == NULL)
2079 return 0;
2080 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2081 return 0;
2082 s1 = s1->next;
2083 s2 = s2->next;
2085 if (s2)
2086 return 0;
2087 return 1;
2090 /* return true if type1 and type2 are the same. If unqualified is
2091 true, qualifiers on the types are ignored.
2093 - enums are not checked as gcc __builtin_types_compatible_p ()
2095 static int compare_types(CType *type1, CType *type2, int unqualified)
2097 int bt1, t1, t2;
2099 t1 = type1->t & VT_TYPE;
2100 t2 = type2->t & VT_TYPE;
2101 if (unqualified) {
2102 /* strip qualifiers before comparing */
2103 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2104 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2106 /* XXX: bitfields ? */
2107 if (t1 != t2)
2108 return 0;
2109 /* test more complicated cases */
2110 bt1 = t1 & VT_BTYPE;
2111 if (bt1 == VT_PTR) {
2112 type1 = pointed_type(type1);
2113 type2 = pointed_type(type2);
2114 return is_compatible_types(type1, type2);
2115 } else if (bt1 == VT_STRUCT) {
2116 return (type1->ref == type2->ref);
2117 } else if (bt1 == VT_FUNC) {
2118 return is_compatible_func(type1, type2);
2119 } else {
2120 return 1;
2124 /* return true if type1 and type2 are exactly the same (including
2125 qualifiers).
2127 static int is_compatible_types(CType *type1, CType *type2)
2129 return compare_types(type1,type2,0);
2132 /* return true if type1 and type2 are the same (ignoring qualifiers).
2134 static int is_compatible_parameter_types(CType *type1, CType *type2)
2136 return compare_types(type1,type2,1);
2139 /* print a type. If 'varstr' is not NULL, then the variable is also
2140 printed in the type */
2141 /* XXX: union */
2142 /* XXX: add array and function pointers */
2143 static void type_to_str(char *buf, int buf_size,
2144 CType *type, const char *varstr)
2146 int bt, v, t;
2147 Sym *s, *sa;
2148 char buf1[256];
2149 const char *tstr;
2151 t = type->t & VT_TYPE;
2152 bt = t & VT_BTYPE;
2153 buf[0] = '\0';
2154 if (t & VT_CONSTANT)
2155 pstrcat(buf, buf_size, "const ");
2156 if (t & VT_VOLATILE)
2157 pstrcat(buf, buf_size, "volatile ");
2158 if (t & VT_UNSIGNED)
2159 pstrcat(buf, buf_size, "unsigned ");
2160 switch(bt) {
2161 case VT_VOID:
2162 tstr = "void";
2163 goto add_tstr;
2164 case VT_BOOL:
2165 tstr = "_Bool";
2166 goto add_tstr;
2167 case VT_BYTE:
2168 tstr = "char";
2169 goto add_tstr;
2170 case VT_SHORT:
2171 tstr = "short";
2172 goto add_tstr;
2173 case VT_INT:
2174 tstr = "int";
2175 goto add_tstr;
2176 case VT_LONG:
2177 tstr = "long";
2178 goto add_tstr;
2179 case VT_LLONG:
2180 tstr = "long long";
2181 goto add_tstr;
2182 case VT_FLOAT:
2183 tstr = "float";
2184 goto add_tstr;
2185 case VT_DOUBLE:
2186 tstr = "double";
2187 goto add_tstr;
2188 case VT_LDOUBLE:
2189 tstr = "long double";
2190 add_tstr:
2191 pstrcat(buf, buf_size, tstr);
2192 break;
2193 case VT_ENUM:
2194 case VT_STRUCT:
2195 if (bt == VT_STRUCT)
2196 tstr = "struct ";
2197 else
2198 tstr = "enum ";
2199 pstrcat(buf, buf_size, tstr);
2200 v = type->ref->v & ~SYM_STRUCT;
2201 if (v >= SYM_FIRST_ANOM)
2202 pstrcat(buf, buf_size, "<anonymous>");
2203 else
2204 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2205 break;
2206 case VT_FUNC:
2207 s = type->ref;
2208 type_to_str(buf, buf_size, &s->type, varstr);
2209 pstrcat(buf, buf_size, "(");
2210 sa = s->next;
2211 while (sa != NULL) {
2212 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2213 pstrcat(buf, buf_size, buf1);
2214 sa = sa->next;
2215 if (sa)
2216 pstrcat(buf, buf_size, ", ");
2218 pstrcat(buf, buf_size, ")");
2219 goto no_var;
2220 case VT_PTR:
2221 s = type->ref;
2222 pstrcpy(buf1, sizeof(buf1), "*");
2223 if (varstr)
2224 pstrcat(buf1, sizeof(buf1), varstr);
2225 type_to_str(buf, buf_size, &s->type, buf1);
2226 goto no_var;
2228 if (varstr) {
2229 pstrcat(buf, buf_size, " ");
2230 pstrcat(buf, buf_size, varstr);
2232 no_var: ;
2235 /* verify type compatibility to store vtop in 'dt' type, and generate
2236 casts if needed. */
2237 static void gen_assign_cast(CType *dt)
2239 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2240 char buf1[256], buf2[256];
2241 int dbt, sbt;
2243 st = &vtop->type; /* source type */
2244 dbt = dt->t & VT_BTYPE;
2245 sbt = st->t & VT_BTYPE;
2246 if (sbt == VT_VOID)
2247 tcc_error("Cannot assign void value");
2248 if (dt->t & VT_CONSTANT)
2249 tcc_warning("assignment of read-only location");
2250 switch(dbt) {
2251 case VT_PTR:
2252 /* special cases for pointers */
2253 /* '0' can also be a pointer */
2254 if (is_null_pointer(vtop))
2255 goto type_ok;
2256 /* accept implicit pointer to integer cast with warning */
2257 if (is_integer_btype(sbt)) {
2258 tcc_warning("assignment makes pointer from integer without a cast");
2259 goto type_ok;
2261 type1 = pointed_type(dt);
2262 /* a function is implicitely a function pointer */
2263 if (sbt == VT_FUNC) {
2264 if ((type1->t & VT_BTYPE) != VT_VOID &&
2265 !is_compatible_types(pointed_type(dt), st))
2266 tcc_warning("assignment from incompatible pointer type");
2267 goto type_ok;
2269 if (sbt != VT_PTR)
2270 goto error;
2271 type2 = pointed_type(st);
2272 if ((type1->t & VT_BTYPE) == VT_VOID ||
2273 (type2->t & VT_BTYPE) == VT_VOID) {
2274 /* void * can match anything */
2275 } else {
2276 /* exact type match, except for unsigned */
2277 tmp_type1 = *type1;
2278 tmp_type2 = *type2;
2279 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2280 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2281 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2282 tcc_warning("assignment from incompatible pointer type");
2284 /* check const and volatile */
2285 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2286 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2287 tcc_warning("assignment discards qualifiers from pointer target type");
2288 break;
2289 case VT_BYTE:
2290 case VT_SHORT:
2291 case VT_INT:
2292 case VT_LLONG:
2293 if (sbt == VT_PTR || sbt == VT_FUNC) {
2294 tcc_warning("assignment makes integer from pointer without a cast");
2296 /* XXX: more tests */
2297 break;
2298 case VT_STRUCT:
2299 tmp_type1 = *dt;
2300 tmp_type2 = *st;
2301 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2302 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2303 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2304 error:
2305 type_to_str(buf1, sizeof(buf1), st, NULL);
2306 type_to_str(buf2, sizeof(buf2), dt, NULL);
2307 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2309 break;
2311 type_ok:
2312 gen_cast(dt);
2315 /* store vtop in lvalue pushed on stack */
2316 ST_FUNC void vstore(void)
2318 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2320 ft = vtop[-1].type.t;
2321 sbt = vtop->type.t & VT_BTYPE;
2322 dbt = ft & VT_BTYPE;
2323 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2324 (sbt == VT_INT && dbt == VT_SHORT))
2325 && !(vtop->type.t & VT_BITFIELD)) {
2326 /* optimize char/short casts */
2327 delayed_cast = VT_MUSTCAST;
2328 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2329 /* XXX: factorize */
2330 if (ft & VT_CONSTANT)
2331 tcc_warning("assignment of read-only location");
2332 } else {
2333 delayed_cast = 0;
2334 if (!(ft & VT_BITFIELD))
2335 gen_assign_cast(&vtop[-1].type);
2338 if (sbt == VT_STRUCT) {
2339 /* if structure, only generate pointer */
2340 /* structure assignment : generate memcpy */
2341 /* XXX: optimize if small size */
2342 if (!nocode_wanted) {
2343 size = type_size(&vtop->type, &align);
2345 /* destination */
2346 vswap();
2347 vtop->type.t = VT_PTR;
2348 gaddrof();
2350 /* address of memcpy() */
2351 #ifdef TCC_ARM_EABI
2352 if(!(align & 7))
2353 vpush_global_sym(&func_old_type, TOK_memcpy8);
2354 else if(!(align & 3))
2355 vpush_global_sym(&func_old_type, TOK_memcpy4);
2356 else
2357 #endif
2358 vpush_global_sym(&func_old_type, TOK_memcpy);
2360 vswap();
2361 /* source */
2362 vpushv(vtop - 2);
2363 vtop->type.t = VT_PTR;
2364 gaddrof();
2365 /* type size */
2366 vpushi(size);
2367 gfunc_call(3);
2368 } else {
2369 vswap();
2370 vpop();
2372 /* leave source on stack */
2373 } else if (ft & VT_BITFIELD) {
2374 /* bitfield store handling */
2375 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2376 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2377 /* remove bit field info to avoid loops */
2378 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2380 /* duplicate source into other register */
2381 gv_dup();
2382 vswap();
2383 vrott(3);
2385 if((ft & VT_BTYPE) == VT_BOOL) {
2386 gen_cast(&vtop[-1].type);
2387 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2390 /* duplicate destination */
2391 vdup();
2392 vtop[-1] = vtop[-2];
2394 /* mask and shift source */
2395 if((ft & VT_BTYPE) != VT_BOOL) {
2396 if((ft & VT_BTYPE) == VT_LLONG) {
2397 vpushll((1ULL << bit_size) - 1ULL);
2398 } else {
2399 vpushi((1 << bit_size) - 1);
2401 gen_op('&');
2403 vpushi(bit_pos);
2404 gen_op(TOK_SHL);
2405 /* load destination, mask and or with source */
2406 vswap();
2407 if((ft & VT_BTYPE) == VT_LLONG) {
2408 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2409 } else {
2410 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2412 gen_op('&');
2413 gen_op('|');
2414 /* store result */
2415 vstore();
2417 /* pop off shifted source from "duplicate source..." above */
2418 vpop();
2420 } else {
2421 #ifdef CONFIG_TCC_BCHECK
2422 /* bound check case */
2423 if (vtop[-1].r & VT_MUSTBOUND) {
2424 vswap();
2425 gbound();
2426 vswap();
2428 #endif
2429 if (!nocode_wanted) {
2430 rc = RC_INT;
2431 if (is_float(ft)) {
2432 rc = RC_FLOAT;
2433 #ifdef TCC_TARGET_X86_64
2434 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2435 rc = RC_ST0;
2437 #endif
2439 r = gv(rc); /* generate value */
2440 /* if lvalue was saved on stack, must read it */
2441 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2442 SValue sv;
2443 t = get_reg(RC_INT);
2444 #ifdef TCC_TARGET_X86_64
2445 sv.type.t = VT_PTR;
2446 #else
2447 sv.type.t = VT_INT;
2448 #endif
2449 sv.r = VT_LOCAL | VT_LVAL;
2450 sv.c.ul = vtop[-1].c.ul;
2451 load(t, &sv);
2452 vtop[-1].r = t | VT_LVAL;
2454 store(r, vtop - 1);
2455 #ifndef TCC_TARGET_X86_64
2456 /* two word case handling : store second register at word + 4 */
2457 if ((ft & VT_BTYPE) == VT_LLONG) {
2458 vswap();
2459 /* convert to int to increment easily */
2460 vtop->type.t = VT_INT;
2461 gaddrof();
2462 vpushi(4);
2463 gen_op('+');
2464 vtop->r |= VT_LVAL;
2465 vswap();
2466 /* XXX: it works because r2 is spilled last ! */
2467 store(vtop->r2, vtop - 1);
2469 #endif
2471 vswap();
2472 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2473 vtop->r |= delayed_cast;
2477 /* post defines POST/PRE add. c is the token ++ or -- */
2478 ST_FUNC void inc(int post, int c)
2480 test_lvalue();
2481 vdup(); /* save lvalue */
2482 if (post) {
2483 gv_dup(); /* duplicate value */
2484 vrotb(3);
2485 vrotb(3);
2487 /* add constant */
2488 vpushi(c - TOK_MID);
2489 gen_op('+');
2490 vstore(); /* store value */
2491 if (post)
2492 vpop(); /* if post op, return saved value */
2495 /* Parse GNUC __attribute__ extension. Currently, the following
2496 extensions are recognized:
2497 - aligned(n) : set data/function alignment.
2498 - packed : force data alignment to 1
2499 - section(x) : generate data/code in this section.
2500 - unused : currently ignored, but may be used someday.
2501 - regparm(n) : pass function parameters in registers (i386 only)
2503 static void parse_attribute(AttributeDef *ad)
2505 int t, n;
2507 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2508 next();
2509 skip('(');
2510 skip('(');
2511 while (tok != ')') {
2512 if (tok < TOK_IDENT)
2513 expect("attribute name");
2514 t = tok;
2515 next();
2516 switch(t) {
2517 case TOK_SECTION1:
2518 case TOK_SECTION2:
2519 skip('(');
2520 if (tok != TOK_STR)
2521 expect("section name");
2522 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2523 next();
2524 skip(')');
2525 break;
2526 case TOK_ALIAS1:
2527 case TOK_ALIAS2:
2528 skip('(');
2529 if (tok != TOK_STR)
2530 expect("alias(\"target\")");
2531 ad->alias_target = /* save string as token, for later */
2532 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2533 next();
2534 skip(')');
2535 break;
2536 case TOK_ALIGNED1:
2537 case TOK_ALIGNED2:
2538 if (tok == '(') {
2539 next();
2540 n = expr_const();
2541 if (n <= 0 || (n & (n - 1)) != 0)
2542 tcc_error("alignment must be a positive power of two");
2543 skip(')');
2544 } else {
2545 n = MAX_ALIGN;
2547 ad->aligned = n;
2548 break;
2549 case TOK_PACKED1:
2550 case TOK_PACKED2:
2551 ad->packed = 1;
2552 break;
2553 case TOK_WEAK1:
2554 case TOK_WEAK2:
2555 ad->weak = 1;
2556 break;
2557 case TOK_UNUSED1:
2558 case TOK_UNUSED2:
2559 /* currently, no need to handle it because tcc does not
2560 track unused objects */
2561 break;
2562 case TOK_NORETURN1:
2563 case TOK_NORETURN2:
2564 /* currently, no need to handle it because tcc does not
2565 track unused objects */
2566 break;
2567 case TOK_CDECL1:
2568 case TOK_CDECL2:
2569 case TOK_CDECL3:
2570 ad->func_call = FUNC_CDECL;
2571 break;
2572 case TOK_STDCALL1:
2573 case TOK_STDCALL2:
2574 case TOK_STDCALL3:
2575 ad->func_call = FUNC_STDCALL;
2576 break;
2577 #ifdef TCC_TARGET_I386
2578 case TOK_REGPARM1:
2579 case TOK_REGPARM2:
2580 skip('(');
2581 n = expr_const();
2582 if (n > 3)
2583 n = 3;
2584 else if (n < 0)
2585 n = 0;
2586 if (n > 0)
2587 ad->func_call = FUNC_FASTCALL1 + n - 1;
2588 skip(')');
2589 break;
2590 case TOK_FASTCALL1:
2591 case TOK_FASTCALL2:
2592 case TOK_FASTCALL3:
2593 ad->func_call = FUNC_FASTCALLW;
2594 break;
2595 #endif
2596 case TOK_MODE:
2597 skip('(');
2598 switch(tok) {
2599 case TOK_MODE_DI:
2600 ad->mode = VT_LLONG + 1;
2601 break;
2602 case TOK_MODE_HI:
2603 ad->mode = VT_SHORT + 1;
2604 break;
2605 case TOK_MODE_SI:
2606 ad->mode = VT_INT + 1;
2607 break;
2608 default:
2609 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2610 break;
2612 next();
2613 skip(')');
2614 break;
2615 case TOK_DLLEXPORT:
2616 ad->func_export = 1;
2617 break;
2618 case TOK_DLLIMPORT:
2619 ad->func_import = 1;
2620 break;
2621 default:
2622 if (tcc_state->warn_unsupported)
2623 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2624 /* skip parameters */
2625 if (tok == '(') {
2626 int parenthesis = 0;
2627 do {
2628 if (tok == '(')
2629 parenthesis++;
2630 else if (tok == ')')
2631 parenthesis--;
2632 next();
2633 } while (parenthesis && tok != -1);
2635 break;
2637 if (tok != ',')
2638 break;
2639 next();
2641 skip(')');
2642 skip(')');
2646 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2647 static void struct_decl(CType *type, int u)
2649 int a, v, size, align, maxalign, c, offset;
2650 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2651 Sym *s, *ss, *ass, **ps;
2652 AttributeDef ad;
2653 CType type1, btype;
2655 a = tok; /* save decl type */
2656 next();
2657 if (tok != '{') {
2658 v = tok;
2659 next();
2660 /* struct already defined ? return it */
2661 if (v < TOK_IDENT)
2662 expect("struct/union/enum name");
2663 s = struct_find(v);
2664 if (s) {
2665 if (s->type.t != a)
2666 tcc_error("invalid type");
2667 goto do_decl;
2669 } else {
2670 v = anon_sym++;
2672 type1.t = a;
2673 /* we put an undefined size for struct/union */
2674 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2675 s->r = 0; /* default alignment is zero as gcc */
2676 /* put struct/union/enum name in type */
2677 do_decl:
2678 type->t = u;
2679 type->ref = s;
2681 if (tok == '{') {
2682 next();
2683 if (s->c != -1)
2684 tcc_error("struct/union/enum already defined");
2685 /* cannot be empty */
2686 c = 0;
2687 /* non empty enums are not allowed */
2688 if (a == TOK_ENUM) {
2689 for(;;) {
2690 v = tok;
2691 if (v < TOK_UIDENT)
2692 expect("identifier");
2693 next();
2694 if (tok == '=') {
2695 next();
2696 c = expr_const();
2698 /* enum symbols have static storage */
2699 ss = sym_push(v, &int_type, VT_CONST, c);
2700 ss->type.t |= VT_STATIC;
2701 if (tok != ',')
2702 break;
2703 next();
2704 c++;
2705 /* NOTE: we accept a trailing comma */
2706 if (tok == '}')
2707 break;
2709 skip('}');
2710 } else {
2711 maxalign = 1;
2712 ps = &s->next;
2713 prevbt = VT_INT;
2714 bit_pos = 0;
2715 offset = 0;
2716 while (tok != '}') {
2717 parse_btype(&btype, &ad);
2718 while (1) {
2719 bit_size = -1;
2720 v = 0;
2721 type1 = btype;
2722 if (tok != ':') {
2723 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2724 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2725 expect("identifier");
2726 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2727 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2728 tcc_error("invalid type for '%s'",
2729 get_tok_str(v, NULL));
2731 if (tok == ':') {
2732 next();
2733 bit_size = expr_const();
2734 /* XXX: handle v = 0 case for messages */
2735 if (bit_size < 0)
2736 tcc_error("negative width in bit-field '%s'",
2737 get_tok_str(v, NULL));
2738 if (v && bit_size == 0)
2739 tcc_error("zero width for bit-field '%s'",
2740 get_tok_str(v, NULL));
2742 size = type_size(&type1, &align);
2743 if (ad.aligned) {
2744 if (align < ad.aligned)
2745 align = ad.aligned;
2746 } else if (ad.packed) {
2747 align = 1;
2748 } else if (*tcc_state->pack_stack_ptr) {
2749 if (align > *tcc_state->pack_stack_ptr)
2750 align = *tcc_state->pack_stack_ptr;
2752 lbit_pos = 0;
2753 if (bit_size >= 0) {
2754 bt = type1.t & VT_BTYPE;
2755 if (bt != VT_INT &&
2756 bt != VT_BYTE &&
2757 bt != VT_SHORT &&
2758 bt != VT_BOOL &&
2759 bt != VT_ENUM &&
2760 bt != VT_LLONG)
2761 tcc_error("bitfields must have scalar type");
2762 bsize = size * 8;
2763 if (bit_size > bsize) {
2764 tcc_error("width of '%s' exceeds its type",
2765 get_tok_str(v, NULL));
2766 } else if (bit_size == bsize) {
2767 /* no need for bit fields */
2768 bit_pos = 0;
2769 } else if (bit_size == 0) {
2770 /* XXX: what to do if only padding in a
2771 structure ? */
2772 /* zero size: means to pad */
2773 bit_pos = 0;
2774 } else {
2775 /* we do not have enough room ?
2776 did the type change?
2777 is it a union? */
2778 if ((bit_pos + bit_size) > bsize ||
2779 bt != prevbt || a == TOK_UNION)
2780 bit_pos = 0;
2781 lbit_pos = bit_pos;
2782 /* XXX: handle LSB first */
2783 type1.t |= VT_BITFIELD |
2784 (bit_pos << VT_STRUCT_SHIFT) |
2785 (bit_size << (VT_STRUCT_SHIFT + 6));
2786 bit_pos += bit_size;
2788 prevbt = bt;
2789 } else {
2790 bit_pos = 0;
2792 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2793 /* add new memory data only if starting
2794 bit field */
2795 if (lbit_pos == 0) {
2796 if (a == TOK_STRUCT) {
2797 c = (c + align - 1) & -align;
2798 offset = c;
2799 if (size > 0)
2800 c += size;
2801 } else {
2802 offset = 0;
2803 if (size > c)
2804 c = size;
2806 if (align > maxalign)
2807 maxalign = align;
2809 #if 0
2810 printf("add field %s offset=%d",
2811 get_tok_str(v, NULL), offset);
2812 if (type1.t & VT_BITFIELD) {
2813 printf(" pos=%d size=%d",
2814 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2815 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2817 printf("\n");
2818 #endif
2820 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2821 ass = type1.ref;
2822 while ((ass = ass->next) != NULL) {
2823 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2824 *ps = ss;
2825 ps = &ss->next;
2827 } else if (v) {
2828 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2829 *ps = ss;
2830 ps = &ss->next;
2832 if (tok == ';' || tok == TOK_EOF)
2833 break;
2834 skip(',');
2836 skip(';');
2838 skip('}');
2839 /* store size and alignment */
2840 s->c = (c + maxalign - 1) & -maxalign;
2841 s->r = maxalign;
2846 /* return 0 if no type declaration. otherwise, return the basic type
2847 and skip it.
2849 static int parse_btype(CType *type, AttributeDef *ad)
2851 int t, u, type_found, typespec_found, typedef_found;
2852 Sym *s;
2853 CType type1;
2855 memset(ad, 0, sizeof(AttributeDef));
2856 type_found = 0;
2857 typespec_found = 0;
2858 typedef_found = 0;
2859 t = 0;
2860 while(1) {
2861 switch(tok) {
2862 case TOK_EXTENSION:
2863 /* currently, we really ignore extension */
2864 next();
2865 continue;
2867 /* basic types */
2868 case TOK_CHAR:
2869 u = VT_BYTE;
2870 basic_type:
2871 next();
2872 basic_type1:
2873 if ((t & VT_BTYPE) != 0)
2874 tcc_error("too many basic types");
2875 t |= u;
2876 typespec_found = 1;
2877 break;
2878 case TOK_VOID:
2879 u = VT_VOID;
2880 goto basic_type;
2881 case TOK_SHORT:
2882 u = VT_SHORT;
2883 goto basic_type;
2884 case TOK_INT:
2885 next();
2886 typespec_found = 1;
2887 break;
2888 case TOK_LONG:
2889 next();
2890 if ((t & VT_BTYPE) == VT_DOUBLE) {
2891 #ifndef TCC_TARGET_PE
2892 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2893 #endif
2894 } else if ((t & VT_BTYPE) == VT_LONG) {
2895 t = (t & ~VT_BTYPE) | VT_LLONG;
2896 } else {
2897 u = VT_LONG;
2898 goto basic_type1;
2900 break;
2901 case TOK_BOOL:
2902 u = VT_BOOL;
2903 goto basic_type;
2904 case TOK_FLOAT:
2905 u = VT_FLOAT;
2906 goto basic_type;
2907 case TOK_DOUBLE:
2908 next();
2909 if ((t & VT_BTYPE) == VT_LONG) {
2910 #ifdef TCC_TARGET_PE
2911 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2912 #else
2913 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2914 #endif
2915 } else {
2916 u = VT_DOUBLE;
2917 goto basic_type1;
2919 break;
2920 case TOK_ENUM:
2921 struct_decl(&type1, VT_ENUM);
2922 basic_type2:
2923 u = type1.t;
2924 type->ref = type1.ref;
2925 goto basic_type1;
2926 case TOK_STRUCT:
2927 case TOK_UNION:
2928 struct_decl(&type1, VT_STRUCT);
2929 goto basic_type2;
2931 /* type modifiers */
2932 case TOK_CONST1:
2933 case TOK_CONST2:
2934 case TOK_CONST3:
2935 t |= VT_CONSTANT;
2936 next();
2937 break;
2938 case TOK_VOLATILE1:
2939 case TOK_VOLATILE2:
2940 case TOK_VOLATILE3:
2941 t |= VT_VOLATILE;
2942 next();
2943 break;
2944 case TOK_SIGNED1:
2945 case TOK_SIGNED2:
2946 case TOK_SIGNED3:
2947 typespec_found = 1;
2948 t |= VT_SIGNED;
2949 next();
2950 break;
2951 case TOK_REGISTER:
2952 case TOK_AUTO:
2953 case TOK_RESTRICT1:
2954 case TOK_RESTRICT2:
2955 case TOK_RESTRICT3:
2956 next();
2957 break;
2958 case TOK_UNSIGNED:
2959 t |= VT_UNSIGNED;
2960 next();
2961 typespec_found = 1;
2962 break;
2964 /* storage */
2965 case TOK_EXTERN:
2966 t |= VT_EXTERN;
2967 next();
2968 break;
2969 case TOK_STATIC:
2970 t |= VT_STATIC;
2971 next();
2972 break;
2973 case TOK_TYPEDEF:
2974 t |= VT_TYPEDEF;
2975 next();
2976 break;
2977 case TOK_INLINE1:
2978 case TOK_INLINE2:
2979 case TOK_INLINE3:
2980 t |= VT_INLINE;
2981 next();
2982 break;
2984 /* GNUC attribute */
2985 case TOK_ATTRIBUTE1:
2986 case TOK_ATTRIBUTE2:
2987 parse_attribute(ad);
2988 if (ad->mode) {
2989 u = ad->mode -1;
2990 t = (t & ~VT_BTYPE) | u;
2992 break;
2993 /* GNUC typeof */
2994 case TOK_TYPEOF1:
2995 case TOK_TYPEOF2:
2996 case TOK_TYPEOF3:
2997 next();
2998 parse_expr_type(&type1);
2999 /* remove all storage modifiers except typedef */
3000 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3001 goto basic_type2;
3002 default:
3003 if (typespec_found || typedef_found)
3004 goto the_end;
3005 s = sym_find(tok);
3006 if (!s || !(s->type.t & VT_TYPEDEF))
3007 goto the_end;
3008 typedef_found = 1;
3009 t |= (s->type.t & ~VT_TYPEDEF);
3010 type->ref = s->type.ref;
3011 if (s->r) {
3012 /* get attributes from typedef */
3013 if (0 == ad->aligned)
3014 ad->aligned = FUNC_ALIGN(s->r);
3015 if (0 == ad->func_call)
3016 ad->func_call = FUNC_CALL(s->r);
3017 ad->packed |= FUNC_PACKED(s->r);
3019 next();
3020 typespec_found = 1;
3021 break;
3023 type_found = 1;
3025 the_end:
3026 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3027 tcc_error("signed and unsigned modifier");
3028 if (tcc_state->char_is_unsigned) {
3029 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3030 t |= VT_UNSIGNED;
3032 t &= ~VT_SIGNED;
3034 /* long is never used as type */
3035 if ((t & VT_BTYPE) == VT_LONG)
3036 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3037 t = (t & ~VT_BTYPE) | VT_INT;
3038 #else
3039 t = (t & ~VT_BTYPE) | VT_LLONG;
3040 #endif
3041 type->t = t;
3042 return type_found;
3045 /* convert a function parameter type (array to pointer and function to
3046 function pointer) */
3047 static inline void convert_parameter_type(CType *pt)
3049 /* remove const and volatile qualifiers (XXX: const could be used
3050 to indicate a const function parameter */
3051 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3052 /* array must be transformed to pointer according to ANSI C */
3053 pt->t &= ~VT_ARRAY;
3054 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3055 mk_pointer(pt);
3059 ST_FUNC void parse_asm_str(CString *astr)
3061 skip('(');
3062 /* read the string */
3063 if (tok != TOK_STR)
3064 expect("string constant");
3065 cstr_new(astr);
3066 while (tok == TOK_STR) {
3067 /* XXX: add \0 handling too ? */
3068 cstr_cat(astr, tokc.cstr->data);
3069 next();
3071 cstr_ccat(astr, '\0');
3074 /* Parse an asm label and return the label
3075 * Don't forget to free the CString in the caller! */
3076 static void asm_label_instr(CString *astr)
3078 next();
3079 parse_asm_str(astr);
3080 skip(')');
3081 #ifdef ASM_DEBUG
3082 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3083 #endif
3086 static void post_type(CType *type, AttributeDef *ad)
3088 int n, l, t1, arg_size, align;
3089 Sym **plast, *s, *first;
3090 AttributeDef ad1;
3091 CType pt;
3093 if (tok == '(') {
3094 /* function declaration */
3095 next();
3096 l = 0;
3097 first = NULL;
3098 plast = &first;
3099 arg_size = 0;
3100 if (tok != ')') {
3101 for(;;) {
3102 /* read param name and compute offset */
3103 if (l != FUNC_OLD) {
3104 if (!parse_btype(&pt, &ad1)) {
3105 if (l) {
3106 tcc_error("invalid type");
3107 } else {
3108 l = FUNC_OLD;
3109 goto old_proto;
3112 l = FUNC_NEW;
3113 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3114 break;
3115 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3116 if ((pt.t & VT_BTYPE) == VT_VOID)
3117 tcc_error("parameter declared as void");
3118 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3119 } else {
3120 old_proto:
3121 n = tok;
3122 if (n < TOK_UIDENT)
3123 expect("identifier");
3124 pt.t = VT_INT;
3125 next();
3127 convert_parameter_type(&pt);
3128 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3129 *plast = s;
3130 plast = &s->next;
3131 if (tok == ')')
3132 break;
3133 skip(',');
3134 if (l == FUNC_NEW && tok == TOK_DOTS) {
3135 l = FUNC_ELLIPSIS;
3136 next();
3137 break;
3141 /* if no parameters, then old type prototype */
3142 if (l == 0)
3143 l = FUNC_OLD;
3144 skip(')');
3145 /* NOTE: const is ignored in returned type as it has a special
3146 meaning in gcc / C++ */
3147 type->t &= ~VT_CONSTANT;
3148 /* some ancient pre-K&R C allows a function to return an array
3149 and the array brackets to be put after the arguments, such
3150 that "int c()[]" means something like "int[] c()" */
3151 if (tok == '[') {
3152 next();
3153 skip(']'); /* only handle simple "[]" */
3154 type->t |= VT_PTR;
3156 /* we push a anonymous symbol which will contain the function prototype */
3157 ad->func_args = arg_size;
3158 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3159 s->next = first;
3160 type->t = VT_FUNC;
3161 type->ref = s;
3162 } else if (tok == '[') {
3163 /* array definition */
3164 next();
3165 if (tok == TOK_RESTRICT1)
3166 next();
3167 n = -1;
3168 t1 = 0;
3169 if (tok != ']') {
3170 if (!local_stack || nocode_wanted)
3171 vpushi(expr_const());
3172 else gexpr();
3173 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3174 n = vtop->c.i;
3175 if (n < 0)
3176 tcc_error("invalid array size");
3177 } else {
3178 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3179 tcc_error("size of variable length array should be an integer");
3180 t1 = VT_VLA;
3183 skip(']');
3184 /* parse next post type */
3185 post_type(type, ad);
3186 t1 |= type->t & VT_VLA;
3188 if (t1 & VT_VLA) {
3189 loc -= type_size(&int_type, &align);
3190 loc &= -align;
3191 n = loc;
3193 vla_runtime_type_size(type, &align);
3194 gen_op('*');
3195 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3196 vswap();
3197 vstore();
3199 if (n != -1)
3200 vpop();
3202 /* we push an anonymous symbol which will contain the array
3203 element type */
3204 s = sym_push(SYM_FIELD, type, 0, n);
3205 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3206 type->ref = s;
3210 /* Parse a type declaration (except basic type), and return the type
3211 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3212 expected. 'type' should contain the basic type. 'ad' is the
3213 attribute definition of the basic type. It can be modified by
3214 type_decl().
3216 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3218 Sym *s;
3219 CType type1, *type2;
3220 int qualifiers, storage;
3222 while (tok == '*') {
3223 qualifiers = 0;
3224 redo:
3225 next();
3226 switch(tok) {
3227 case TOK_CONST1:
3228 case TOK_CONST2:
3229 case TOK_CONST3:
3230 qualifiers |= VT_CONSTANT;
3231 goto redo;
3232 case TOK_VOLATILE1:
3233 case TOK_VOLATILE2:
3234 case TOK_VOLATILE3:
3235 qualifiers |= VT_VOLATILE;
3236 goto redo;
3237 case TOK_RESTRICT1:
3238 case TOK_RESTRICT2:
3239 case TOK_RESTRICT3:
3240 goto redo;
3242 mk_pointer(type);
3243 type->t |= qualifiers;
3246 /* XXX: clarify attribute handling */
3247 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3248 parse_attribute(ad);
3250 /* recursive type */
3251 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3252 type1.t = 0; /* XXX: same as int */
3253 if (tok == '(') {
3254 next();
3255 /* XXX: this is not correct to modify 'ad' at this point, but
3256 the syntax is not clear */
3257 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3258 parse_attribute(ad);
3259 type_decl(&type1, ad, v, td);
3260 skip(')');
3261 } else {
3262 /* type identifier */
3263 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3264 *v = tok;
3265 next();
3266 } else {
3267 if (!(td & TYPE_ABSTRACT))
3268 expect("identifier");
3269 *v = 0;
3272 storage = type->t & VT_STORAGE;
3273 type->t &= ~VT_STORAGE;
3274 post_type(type, ad);
3275 type->t |= storage;
3276 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3277 parse_attribute(ad);
3279 if (!type1.t)
3280 return;
3281 /* append type at the end of type1 */
3282 type2 = &type1;
3283 for(;;) {
3284 s = type2->ref;
3285 type2 = &s->type;
3286 if (!type2->t) {
3287 *type2 = *type;
3288 break;
3291 *type = type1;
3294 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3295 ST_FUNC int lvalue_type(int t)
3297 int bt, r;
3298 r = VT_LVAL;
3299 bt = t & VT_BTYPE;
3300 if (bt == VT_BYTE || bt == VT_BOOL)
3301 r |= VT_LVAL_BYTE;
3302 else if (bt == VT_SHORT)
3303 r |= VT_LVAL_SHORT;
3304 else
3305 return r;
3306 if (t & VT_UNSIGNED)
3307 r |= VT_LVAL_UNSIGNED;
3308 return r;
3311 /* indirection with full error checking and bound check */
3312 ST_FUNC void indir(void)
3314 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3315 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3316 return;
3317 expect("pointer");
3319 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3320 gv(RC_INT);
3321 vtop->type = *pointed_type(&vtop->type);
3322 /* Arrays and functions are never lvalues */
3323 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3324 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3325 vtop->r |= lvalue_type(vtop->type.t);
3326 /* if bound checking, the referenced pointer must be checked */
3327 #ifdef CONFIG_TCC_BCHECK
3328 if (tcc_state->do_bounds_check)
3329 vtop->r |= VT_MUSTBOUND;
3330 #endif
3334 /* pass a parameter to a function and do type checking and casting */
3335 static void gfunc_param_typed(Sym *func, Sym *arg)
3337 int func_type;
3338 CType type;
3340 func_type = func->c;
3341 if (func_type == FUNC_OLD ||
3342 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3343 /* default casting : only need to convert float to double */
3344 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3345 type.t = VT_DOUBLE;
3346 gen_cast(&type);
3348 } else if (arg == NULL) {
3349 tcc_error("too many arguments to function");
3350 } else {
3351 type = arg->type;
3352 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3353 gen_assign_cast(&type);
3357 /* parse an expression of the form '(type)' or '(expr)' and return its
3358 type */
3359 static void parse_expr_type(CType *type)
3361 int n;
3362 AttributeDef ad;
3364 skip('(');
3365 if (parse_btype(type, &ad)) {
3366 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3367 } else {
3368 expr_type(type);
3370 skip(')');
3373 static void parse_type(CType *type)
3375 AttributeDef ad;
3376 int n;
3378 if (!parse_btype(type, &ad)) {
3379 expect("type");
3381 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3384 static void vpush_tokc(int t)
3386 CType type;
3387 type.t = t;
3388 type.ref = 0;
3389 vsetc(&type, VT_CONST, &tokc);
3392 ST_FUNC void unary(void)
3394 int n, t, align, size, r, sizeof_caller;
3395 CType type;
3396 Sym *s;
3397 AttributeDef ad;
3398 static int in_sizeof = 0;
3400 sizeof_caller = in_sizeof;
3401 in_sizeof = 0;
3402 /* XXX: GCC 2.95.3 does not generate a table although it should be
3403 better here */
3404 tok_next:
3405 switch(tok) {
3406 case TOK_EXTENSION:
3407 next();
3408 goto tok_next;
3409 case TOK_CINT:
3410 case TOK_CCHAR:
3411 case TOK_LCHAR:
3412 vpushi(tokc.i);
3413 next();
3414 break;
3415 case TOK_CUINT:
3416 vpush_tokc(VT_INT | VT_UNSIGNED);
3417 next();
3418 break;
3419 case TOK_CLLONG:
3420 vpush_tokc(VT_LLONG);
3421 next();
3422 break;
3423 case TOK_CULLONG:
3424 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3425 next();
3426 break;
3427 case TOK_CFLOAT:
3428 vpush_tokc(VT_FLOAT);
3429 next();
3430 break;
3431 case TOK_CDOUBLE:
3432 vpush_tokc(VT_DOUBLE);
3433 next();
3434 break;
3435 case TOK_CLDOUBLE:
3436 vpush_tokc(VT_LDOUBLE);
3437 next();
3438 break;
3439 case TOK___FUNCTION__:
3440 if (!gnu_ext)
3441 goto tok_identifier;
3442 /* fall thru */
3443 case TOK___FUNC__:
3445 void *ptr;
3446 int len;
3447 /* special function name identifier */
3448 len = strlen(funcname) + 1;
3449 /* generate char[len] type */
3450 type.t = VT_BYTE;
3451 mk_pointer(&type);
3452 type.t |= VT_ARRAY;
3453 type.ref->c = len;
3454 vpush_ref(&type, data_section, data_section->data_offset, len);
3455 ptr = section_ptr_add(data_section, len);
3456 memcpy(ptr, funcname, len);
3457 next();
3459 break;
3460 case TOK_LSTR:
3461 #ifdef TCC_TARGET_PE
3462 t = VT_SHORT | VT_UNSIGNED;
3463 #else
3464 t = VT_INT;
3465 #endif
3466 goto str_init;
3467 case TOK_STR:
3468 /* string parsing */
3469 t = VT_BYTE;
3470 str_init:
3471 if (tcc_state->warn_write_strings)
3472 t |= VT_CONSTANT;
3473 type.t = t;
3474 mk_pointer(&type);
3475 type.t |= VT_ARRAY;
3476 memset(&ad, 0, sizeof(AttributeDef));
3477 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3478 break;
3479 case '(':
3480 next();
3481 /* cast ? */
3482 if (parse_btype(&type, &ad)) {
3483 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3484 skip(')');
3485 /* check ISOC99 compound literal */
3486 if (tok == '{') {
3487 /* data is allocated locally by default */
3488 if (global_expr)
3489 r = VT_CONST;
3490 else
3491 r = VT_LOCAL;
3492 /* all except arrays are lvalues */
3493 if (!(type.t & VT_ARRAY))
3494 r |= lvalue_type(type.t);
3495 memset(&ad, 0, sizeof(AttributeDef));
3496 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3497 } else {
3498 if (sizeof_caller) {
3499 vpush(&type);
3500 return;
3502 unary();
3503 gen_cast(&type);
3505 } else if (tok == '{') {
3506 /* save all registers */
3507 save_regs(0);
3508 /* statement expression : we do not accept break/continue
3509 inside as GCC does */
3510 block(NULL, NULL, NULL, NULL, 0, 1);
3511 skip(')');
3512 } else {
3513 gexpr();
3514 skip(')');
3516 break;
3517 case '*':
3518 next();
3519 unary();
3520 indir();
3521 break;
3522 case '&':
3523 next();
3524 unary();
3525 /* functions names must be treated as function pointers,
3526 except for unary '&' and sizeof. Since we consider that
3527 functions are not lvalues, we only have to handle it
3528 there and in function calls. */
3529 /* arrays can also be used although they are not lvalues */
3530 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3531 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3532 test_lvalue();
3533 mk_pointer(&vtop->type);
3534 gaddrof();
3535 break;
3536 case '!':
3537 next();
3538 unary();
3539 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3540 CType boolean;
3541 boolean.t = VT_BOOL;
3542 gen_cast(&boolean);
3543 vtop->c.i = !vtop->c.i;
3544 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3545 vtop->c.i = vtop->c.i ^ 1;
3546 else {
3547 save_regs(1);
3548 vseti(VT_JMP, gtst(1, 0));
3550 break;
3551 case '~':
3552 next();
3553 unary();
3554 vpushi(-1);
3555 gen_op('^');
3556 break;
3557 case '+':
3558 next();
3559 /* in order to force cast, we add zero */
3560 unary();
3561 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3562 tcc_error("pointer not accepted for unary plus");
3563 vpushi(0);
3564 gen_op('+');
3565 break;
3566 case TOK_SIZEOF:
3567 case TOK_ALIGNOF1:
3568 case TOK_ALIGNOF2:
3569 t = tok;
3570 next();
3571 in_sizeof++;
3572 unary_type(&type); // Perform a in_sizeof = 0;
3573 size = type_size(&type, &align);
3574 if (t == TOK_SIZEOF) {
3575 if (!(type.t & VT_VLA)) {
3576 if (size < 0)
3577 tcc_error("sizeof applied to an incomplete type");
3578 vpushi(size);
3579 } else {
3580 vla_runtime_type_size(&type, &align);
3582 } else {
3583 vpushi(align);
3585 vtop->type.t |= VT_UNSIGNED;
3586 break;
3588 case TOK_builtin_types_compatible_p:
3590 CType type1, type2;
3591 next();
3592 skip('(');
3593 parse_type(&type1);
3594 skip(',');
3595 parse_type(&type2);
3596 skip(')');
3597 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3598 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3599 vpushi(is_compatible_types(&type1, &type2));
3601 break;
3602 case TOK_builtin_constant_p:
3604 int saved_nocode_wanted, res;
3605 next();
3606 skip('(');
3607 saved_nocode_wanted = nocode_wanted;
3608 nocode_wanted = 1;
3609 gexpr();
3610 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3611 vpop();
3612 nocode_wanted = saved_nocode_wanted;
3613 skip(')');
3614 vpushi(res);
3616 break;
3617 case TOK_builtin_frame_address:
3619 CType type;
3620 next();
3621 skip('(');
3622 if (tok != TOK_CINT) {
3623 tcc_error("__builtin_frame_address only takes integers");
3625 if (tokc.i != 0) {
3626 tcc_error("TCC only supports __builtin_frame_address(0)");
3628 next();
3629 skip(')');
3630 type.t = VT_VOID;
3631 mk_pointer(&type);
3632 vset(&type, VT_LOCAL, 0);
3634 break;
3635 #ifdef TCC_TARGET_X86_64
3636 case TOK_builtin_va_arg_types:
3638 /* This definition must be synced with stdarg.h */
3639 enum __va_arg_type {
3640 __va_gen_reg, __va_float_reg, __va_stack
3642 CType type;
3643 int bt;
3644 next();
3645 skip('(');
3646 parse_type(&type);
3647 skip(')');
3648 bt = type.t & VT_BTYPE;
3649 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
3650 vpushi(__va_stack);
3651 } else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
3652 vpushi(__va_float_reg);
3653 } else {
3654 vpushi(__va_gen_reg);
3657 break;
3658 #endif
3659 case TOK_INC:
3660 case TOK_DEC:
3661 t = tok;
3662 next();
3663 unary();
3664 inc(0, t);
3665 break;
3666 case '-':
3667 next();
3668 vpushi(0);
3669 unary();
3670 gen_op('-');
3671 break;
3672 case TOK_LAND:
3673 if (!gnu_ext)
3674 goto tok_identifier;
3675 next();
3676 /* allow to take the address of a label */
3677 if (tok < TOK_UIDENT)
3678 expect("label identifier");
3679 s = label_find(tok);
3680 if (!s) {
3681 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3682 } else {
3683 if (s->r == LABEL_DECLARED)
3684 s->r = LABEL_FORWARD;
3686 if (!s->type.t) {
3687 s->type.t = VT_VOID;
3688 mk_pointer(&s->type);
3689 s->type.t |= VT_STATIC;
3691 vset(&s->type, VT_CONST | VT_SYM, 0);
3692 vtop->sym = s;
3693 next();
3694 break;
3696 // special qnan , snan and infinity values
3697 case TOK___NAN__:
3698 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3699 next();
3700 break;
3701 case TOK___SNAN__:
3702 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3703 next();
3704 break;
3705 case TOK___INF__:
3706 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3707 next();
3708 break;
3710 default:
3711 tok_identifier:
3712 t = tok;
3713 next();
3714 if (t < TOK_UIDENT)
3715 expect("identifier");
3716 s = sym_find(t);
3717 if (!s) {
3718 if (tok != '(')
3719 tcc_error("'%s' undeclared", get_tok_str(t, NULL));
3720 /* for simple function calls, we tolerate undeclared
3721 external reference to int() function */
3722 if (tcc_state->warn_implicit_function_declaration)
3723 tcc_warning("implicit declaration of function '%s'",
3724 get_tok_str(t, NULL));
3725 s = external_global_sym(t, &func_old_type, 0);
3727 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3728 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3729 /* if referencing an inline function, then we generate a
3730 symbol to it if not already done. It will have the
3731 effect to generate code for it at the end of the
3732 compilation unit. Inline function as always
3733 generated in the text section. */
3734 if (!s->c)
3735 put_extern_sym(s, text_section, 0, 0);
3736 r = VT_SYM | VT_CONST;
3737 } else {
3738 r = s->r;
3740 vset(&s->type, r, s->c);
3741 /* if forward reference, we must point to s */
3742 if (vtop->r & VT_SYM) {
3743 vtop->sym = s;
3744 vtop->c.ul = 0;
3746 break;
3749 /* post operations */
3750 while (1) {
3751 if (tok == TOK_INC || tok == TOK_DEC) {
3752 inc(1, tok);
3753 next();
3754 } else if (tok == '.' || tok == TOK_ARROW) {
3755 int qualifiers;
3756 /* field */
3757 if (tok == TOK_ARROW)
3758 indir();
3759 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3760 test_lvalue();
3761 gaddrof();
3762 next();
3763 /* expect pointer on structure */
3764 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3765 expect("struct or union");
3766 s = vtop->type.ref;
3767 /* find field */
3768 tok |= SYM_FIELD;
3769 while ((s = s->next) != NULL) {
3770 if (s->v == tok)
3771 break;
3773 if (!s)
3774 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3775 /* add field offset to pointer */
3776 vtop->type = char_pointer_type; /* change type to 'char *' */
3777 vpushi(s->c);
3778 gen_op('+');
3779 /* change type to field type, and set to lvalue */
3780 vtop->type = s->type;
3781 vtop->type.t |= qualifiers;
3782 /* an array is never an lvalue */
3783 if (!(vtop->type.t & VT_ARRAY)) {
3784 vtop->r |= lvalue_type(vtop->type.t);
3785 #ifdef CONFIG_TCC_BCHECK
3786 /* if bound checking, the referenced pointer must be checked */
3787 if (tcc_state->do_bounds_check)
3788 vtop->r |= VT_MUSTBOUND;
3789 #endif
3791 next();
3792 } else if (tok == '[') {
3793 next();
3794 gexpr();
3795 gen_op('+');
3796 indir();
3797 skip(']');
3798 } else if (tok == '(') {
3799 SValue ret;
3800 Sym *sa;
3801 int nb_args;
3803 /* function call */
3804 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3805 /* pointer test (no array accepted) */
3806 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3807 vtop->type = *pointed_type(&vtop->type);
3808 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3809 goto error_func;
3810 } else {
3811 error_func:
3812 expect("function pointer");
3814 } else {
3815 vtop->r &= ~VT_LVAL; /* no lvalue */
3817 /* get return type */
3818 s = vtop->type.ref;
3819 next();
3820 sa = s->next; /* first parameter */
3821 nb_args = 0;
3822 ret.r2 = VT_CONST;
3823 /* compute first implicit argument if a structure is returned */
3824 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3825 /* get some space for the returned structure */
3826 size = type_size(&s->type, &align);
3827 loc = (loc - size) & -align;
3828 ret.type = s->type;
3829 ret.r = VT_LOCAL | VT_LVAL;
3830 /* pass it as 'int' to avoid structure arg passing
3831 problems */
3832 vseti(VT_LOCAL, loc);
3833 ret.c = vtop->c;
3834 nb_args++;
3835 } else {
3836 ret.type = s->type;
3837 /* return in register */
3838 if (is_float(ret.type.t)) {
3839 ret.r = reg_fret(ret.type.t);
3840 } else {
3841 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3842 ret.r2 = REG_LRET;
3843 ret.r = REG_IRET;
3845 ret.c.i = 0;
3847 if (tok != ')') {
3848 for(;;) {
3849 expr_eq();
3850 gfunc_param_typed(s, sa);
3851 nb_args++;
3852 if (sa)
3853 sa = sa->next;
3854 if (tok == ')')
3855 break;
3856 skip(',');
3859 if (sa)
3860 tcc_error("too few arguments to function");
3861 skip(')');
3862 if (!nocode_wanted) {
3863 gfunc_call(nb_args);
3864 } else {
3865 vtop -= (nb_args + 1);
3867 /* return value */
3868 vsetc(&ret.type, ret.r, &ret.c);
3869 vtop->r2 = ret.r2;
3870 } else {
3871 break;
3876 ST_FUNC void expr_prod(void)
3878 int t;
3880 unary();
3881 while (tok == '*' || tok == '/' || tok == '%') {
3882 t = tok;
3883 next();
3884 unary();
3885 gen_op(t);
3889 ST_FUNC void expr_sum(void)
3891 int t;
3893 expr_prod();
3894 while (tok == '+' || tok == '-') {
3895 t = tok;
3896 next();
3897 expr_prod();
3898 gen_op(t);
3902 static void expr_shift(void)
3904 int t;
3906 expr_sum();
3907 while (tok == TOK_SHL || tok == TOK_SAR) {
3908 t = tok;
3909 next();
3910 expr_sum();
3911 gen_op(t);
3915 static void expr_cmp(void)
3917 int t;
3919 expr_shift();
3920 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3921 tok == TOK_ULT || tok == TOK_UGE) {
3922 t = tok;
3923 next();
3924 expr_shift();
3925 gen_op(t);
3929 static void expr_cmpeq(void)
3931 int t;
3933 expr_cmp();
3934 while (tok == TOK_EQ || tok == TOK_NE) {
3935 t = tok;
3936 next();
3937 expr_cmp();
3938 gen_op(t);
3942 static void expr_and(void)
3944 expr_cmpeq();
3945 while (tok == '&') {
3946 next();
3947 expr_cmpeq();
3948 gen_op('&');
3952 static void expr_xor(void)
3954 expr_and();
3955 while (tok == '^') {
3956 next();
3957 expr_and();
3958 gen_op('^');
3962 static void expr_or(void)
3964 expr_xor();
3965 while (tok == '|') {
3966 next();
3967 expr_xor();
3968 gen_op('|');
3972 /* XXX: fix this mess */
3973 static void expr_land_const(void)
3975 expr_or();
3976 while (tok == TOK_LAND) {
3977 next();
3978 expr_or();
3979 gen_op(TOK_LAND);
3983 /* XXX: fix this mess */
3984 static void expr_lor_const(void)
3986 expr_land_const();
3987 while (tok == TOK_LOR) {
3988 next();
3989 expr_land_const();
3990 gen_op(TOK_LOR);
3994 /* only used if non constant */
3995 static void expr_land(void)
3997 int t;
3999 expr_or();
4000 if (tok == TOK_LAND) {
4001 t = 0;
4002 save_regs(1);
4003 for(;;) {
4004 t = gtst(1, t);
4005 if (tok != TOK_LAND) {
4006 vseti(VT_JMPI, t);
4007 break;
4009 next();
4010 expr_or();
4015 static void expr_lor(void)
4017 int t;
4019 expr_land();
4020 if (tok == TOK_LOR) {
4021 t = 0;
4022 save_regs(1);
4023 for(;;) {
4024 t = gtst(0, t);
4025 if (tok != TOK_LOR) {
4026 vseti(VT_JMP, t);
4027 break;
4029 next();
4030 expr_land();
4035 /* XXX: better constant handling */
4036 static void expr_cond(void)
4038 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4039 SValue sv;
4040 CType type, type1, type2;
4042 if (const_wanted) {
4043 expr_lor_const();
4044 if (tok == '?') {
4045 CType boolean;
4046 int c;
4047 boolean.t = VT_BOOL;
4048 vdup();
4049 gen_cast(&boolean);
4050 c = vtop->c.i;
4051 vpop();
4052 next();
4053 if (tok != ':' || !gnu_ext) {
4054 vpop();
4055 gexpr();
4057 if (!c)
4058 vpop();
4059 skip(':');
4060 expr_cond();
4061 if (c)
4062 vpop();
4064 } else {
4065 expr_lor();
4066 if (tok == '?') {
4067 next();
4068 if (vtop != vstack) {
4069 /* needed to avoid having different registers saved in
4070 each branch */
4071 if (is_float(vtop->type.t)) {
4072 rc = RC_FLOAT;
4073 #ifdef TCC_TARGET_X86_64
4074 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4075 rc = RC_ST0;
4077 #endif
4079 else
4080 rc = RC_INT;
4081 gv(rc);
4082 save_regs(1);
4084 if (tok == ':' && gnu_ext) {
4085 gv_dup();
4086 tt = gtst(1, 0);
4087 } else {
4088 tt = gtst(1, 0);
4089 gexpr();
4091 type1 = vtop->type;
4092 sv = *vtop; /* save value to handle it later */
4093 vtop--; /* no vpop so that FP stack is not flushed */
4094 skip(':');
4095 u = gjmp(0);
4096 gsym(tt);
4097 expr_cond();
4098 type2 = vtop->type;
4100 t1 = type1.t;
4101 bt1 = t1 & VT_BTYPE;
4102 t2 = type2.t;
4103 bt2 = t2 & VT_BTYPE;
4104 /* cast operands to correct type according to ISOC rules */
4105 if (is_float(bt1) || is_float(bt2)) {
4106 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4107 type.t = VT_LDOUBLE;
4108 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4109 type.t = VT_DOUBLE;
4110 } else {
4111 type.t = VT_FLOAT;
4113 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4114 /* cast to biggest op */
4115 type.t = VT_LLONG;
4116 /* convert to unsigned if it does not fit in a long long */
4117 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4118 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4119 type.t |= VT_UNSIGNED;
4120 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4121 /* If one is a null ptr constant the result type
4122 is the other. */
4123 if (is_null_pointer (vtop))
4124 type = type1;
4125 else if (is_null_pointer (&sv))
4126 type = type2;
4127 /* XXX: test pointer compatibility, C99 has more elaborate
4128 rules here. */
4129 else
4130 type = type1;
4131 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4132 /* XXX: test function pointer compatibility */
4133 type = bt1 == VT_FUNC ? type1 : type2;
4134 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4135 /* XXX: test structure compatibility */
4136 type = bt1 == VT_STRUCT ? type1 : type2;
4137 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4138 /* NOTE: as an extension, we accept void on only one side */
4139 type.t = VT_VOID;
4140 } else {
4141 /* integer operations */
4142 type.t = VT_INT;
4143 /* convert to unsigned if it does not fit in an integer */
4144 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4145 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4146 type.t |= VT_UNSIGNED;
4149 /* now we convert second operand */
4150 gen_cast(&type);
4151 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4152 gaddrof();
4153 rc = RC_INT;
4154 if (is_float(type.t)) {
4155 rc = RC_FLOAT;
4156 #ifdef TCC_TARGET_X86_64
4157 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4158 rc = RC_ST0;
4160 #endif
4161 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4162 /* for long longs, we use fixed registers to avoid having
4163 to handle a complicated move */
4164 rc = RC_IRET;
4167 r2 = gv(rc);
4168 /* this is horrible, but we must also convert first
4169 operand */
4170 tt = gjmp(0);
4171 gsym(u);
4172 /* put again first value and cast it */
4173 *vtop = sv;
4174 gen_cast(&type);
4175 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4176 gaddrof();
4177 r1 = gv(rc);
4178 move_reg(r2, r1);
4179 vtop->r = r2;
4180 gsym(tt);
4185 static void expr_eq(void)
4187 int t;
4189 expr_cond();
4190 if (tok == '=' ||
4191 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4192 tok == TOK_A_XOR || tok == TOK_A_OR ||
4193 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4194 test_lvalue();
4195 t = tok;
4196 next();
4197 if (t == '=') {
4198 expr_eq();
4199 } else {
4200 vdup();
4201 expr_eq();
4202 gen_op(t & 0x7f);
4204 vstore();
4208 ST_FUNC void gexpr(void)
4210 while (1) {
4211 expr_eq();
4212 if (tok != ',')
4213 break;
4214 vpop();
4215 next();
4219 /* parse an expression and return its type without any side effect. */
4220 static void expr_type(CType *type)
4222 int saved_nocode_wanted;
4224 saved_nocode_wanted = nocode_wanted;
4225 nocode_wanted = 1;
4226 gexpr();
4227 *type = vtop->type;
4228 vpop();
4229 nocode_wanted = saved_nocode_wanted;
4232 /* parse a unary expression and return its type without any side
4233 effect. */
4234 static void unary_type(CType *type)
4236 int a;
4238 a = nocode_wanted;
4239 nocode_wanted = 1;
4240 unary();
4241 *type = vtop->type;
4242 vpop();
4243 nocode_wanted = a;
4246 /* parse a constant expression and return value in vtop. */
4247 static void expr_const1(void)
4249 int a;
4250 a = const_wanted;
4251 const_wanted = 1;
4252 expr_cond();
4253 const_wanted = a;
4256 /* parse an integer constant and return its value. */
4257 ST_FUNC int expr_const(void)
4259 int c;
4260 expr_const1();
4261 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4262 expect("constant expression");
4263 c = vtop->c.i;
4264 vpop();
4265 return c;
4268 /* return the label token if current token is a label, otherwise
4269 return zero */
4270 static int is_label(void)
4272 int last_tok;
4274 /* fast test first */
4275 if (tok < TOK_UIDENT)
4276 return 0;
4277 /* no need to save tokc because tok is an identifier */
4278 last_tok = tok;
4279 next();
4280 if (tok == ':') {
4281 next();
4282 return last_tok;
4283 } else {
4284 unget_tok(last_tok);
4285 return 0;
4289 static void label_or_decl(int l)
4291 int last_tok;
4293 /* fast test first */
4294 if (tok >= TOK_UIDENT)
4296 /* no need to save tokc because tok is an identifier */
4297 last_tok = tok;
4298 next();
4299 if (tok == ':') {
4300 unget_tok(last_tok);
4301 return;
4303 unget_tok(last_tok);
4305 decl(l);
4308 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4309 int case_reg, int is_expr)
4311 int a, b, c, d;
4312 Sym *s;
4314 /* generate line number info */
4315 if (tcc_state->do_debug &&
4316 (last_line_num != file->line_num || last_ind != ind)) {
4317 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4318 last_ind = ind;
4319 last_line_num = file->line_num;
4322 if (is_expr) {
4323 /* default return value is (void) */
4324 vpushi(0);
4325 vtop->type.t = VT_VOID;
4328 if (tok == TOK_IF) {
4329 /* if test */
4330 next();
4331 skip('(');
4332 gexpr();
4333 skip(')');
4334 a = gtst(1, 0);
4335 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4336 c = tok;
4337 if (c == TOK_ELSE) {
4338 next();
4339 d = gjmp(0);
4340 gsym(a);
4341 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4342 gsym(d); /* patch else jmp */
4343 } else
4344 gsym(a);
4345 } else if (tok == TOK_WHILE) {
4346 next();
4347 d = ind;
4348 skip('(');
4349 gexpr();
4350 skip(')');
4351 a = gtst(1, 0);
4352 b = 0;
4353 block(&a, &b, case_sym, def_sym, case_reg, 0);
4354 gjmp_addr(d);
4355 gsym(a);
4356 gsym_addr(b, d);
4357 } else if (tok == '{') {
4358 Sym *llabel;
4360 next();
4361 /* record local declaration stack position */
4362 s = local_stack;
4363 llabel = local_label_stack;
4364 /* handle local labels declarations */
4365 if (tok == TOK_LABEL) {
4366 next();
4367 for(;;) {
4368 if (tok < TOK_UIDENT)
4369 expect("label identifier");
4370 label_push(&local_label_stack, tok, LABEL_DECLARED);
4371 next();
4372 if (tok == ',') {
4373 next();
4374 } else {
4375 skip(';');
4376 break;
4380 while (tok != '}') {
4381 label_or_decl(VT_LOCAL);
4382 if (tok != '}') {
4383 if (is_expr)
4384 vpop();
4385 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4388 /* pop locally defined labels */
4389 label_pop(&local_label_stack, llabel);
4390 if(is_expr) {
4391 /* XXX: this solution makes only valgrind happy...
4392 triggered by gcc.c-torture/execute/20000917-1.c */
4393 Sym *p;
4394 switch(vtop->type.t & VT_BTYPE) {
4395 case VT_PTR:
4396 case VT_STRUCT:
4397 case VT_ENUM:
4398 case VT_FUNC:
4399 for(p=vtop->type.ref;p;p=p->prev)
4400 if(p->prev==s)
4401 tcc_error("unsupported expression type");
4404 /* pop locally defined symbols */
4405 sym_pop(&local_stack, s);
4406 next();
4407 } else if (tok == TOK_RETURN) {
4408 next();
4409 if (tok != ';') {
4410 gexpr();
4411 gen_assign_cast(&func_vt);
4412 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4413 CType type;
4414 /* if returning structure, must copy it to implicit
4415 first pointer arg location */
4416 #ifdef TCC_ARM_EABI
4417 int align, size;
4418 size = type_size(&func_vt,&align);
4419 if(size <= 4)
4421 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4422 && (align & 3))
4424 int addr;
4425 loc = (loc - size) & -4;
4426 addr = loc;
4427 type = func_vt;
4428 vset(&type, VT_LOCAL | VT_LVAL, addr);
4429 vswap();
4430 vstore();
4431 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4433 vtop->type = int_type;
4434 gv(RC_IRET);
4435 } else {
4436 #endif
4437 type = func_vt;
4438 mk_pointer(&type);
4439 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4440 indir();
4441 vswap();
4442 /* copy structure value to pointer */
4443 vstore();
4444 #ifdef TCC_ARM_EABI
4446 #endif
4447 } else if (is_float(func_vt.t)) {
4448 gv(rc_fret(func_vt.t));
4449 } else {
4450 gv(RC_IRET);
4452 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4454 skip(';');
4455 rsym = gjmp(rsym); /* jmp */
4456 } else if (tok == TOK_BREAK) {
4457 /* compute jump */
4458 if (!bsym)
4459 tcc_error("cannot break");
4460 *bsym = gjmp(*bsym);
4461 next();
4462 skip(';');
4463 } else if (tok == TOK_CONTINUE) {
4464 /* compute jump */
4465 if (!csym)
4466 tcc_error("cannot continue");
4467 *csym = gjmp(*csym);
4468 next();
4469 skip(';');
4470 } else if (tok == TOK_FOR) {
4471 int e;
4472 next();
4473 skip('(');
4474 s = local_stack;
4475 if (tok != ';') {
4476 /* c99 for-loop init decl? */
4477 if (!decl0(VT_LOCAL, 1)) {
4478 /* no, regular for-loop init expr */
4479 gexpr();
4480 vpop();
4483 skip(';');
4484 d = ind;
4485 c = ind;
4486 a = 0;
4487 b = 0;
4488 if (tok != ';') {
4489 gexpr();
4490 a = gtst(1, 0);
4492 skip(';');
4493 if (tok != ')') {
4494 e = gjmp(0);
4495 c = ind;
4496 gexpr();
4497 vpop();
4498 gjmp_addr(d);
4499 gsym(e);
4501 skip(')');
4502 block(&a, &b, case_sym, def_sym, case_reg, 0);
4503 gjmp_addr(c);
4504 gsym(a);
4505 gsym_addr(b, c);
4506 sym_pop(&local_stack, s);
4507 } else
4508 if (tok == TOK_DO) {
4509 next();
4510 a = 0;
4511 b = 0;
4512 d = ind;
4513 block(&a, &b, case_sym, def_sym, case_reg, 0);
4514 skip(TOK_WHILE);
4515 skip('(');
4516 gsym(b);
4517 gexpr();
4518 c = gtst(0, 0);
4519 gsym_addr(c, d);
4520 skip(')');
4521 gsym(a);
4522 skip(';');
4523 } else
4524 if (tok == TOK_SWITCH) {
4525 next();
4526 skip('(');
4527 gexpr();
4528 /* XXX: other types than integer */
4529 case_reg = gv(RC_INT);
4530 vpop();
4531 skip(')');
4532 a = 0;
4533 b = gjmp(0); /* jump to first case */
4534 c = 0;
4535 block(&a, csym, &b, &c, case_reg, 0);
4536 /* if no default, jmp after switch */
4537 if (c == 0)
4538 c = ind;
4539 /* default label */
4540 gsym_addr(b, c);
4541 /* break label */
4542 gsym(a);
4543 } else
4544 if (tok == TOK_CASE) {
4545 int v1, v2;
4546 if (!case_sym)
4547 expect("switch");
4548 next();
4549 v1 = expr_const();
4550 v2 = v1;
4551 if (gnu_ext && tok == TOK_DOTS) {
4552 next();
4553 v2 = expr_const();
4554 if (v2 < v1)
4555 tcc_warning("empty case range");
4557 /* since a case is like a label, we must skip it with a jmp */
4558 b = gjmp(0);
4559 gsym(*case_sym);
4560 vseti(case_reg, 0);
4561 vpushi(v1);
4562 if (v1 == v2) {
4563 gen_op(TOK_EQ);
4564 *case_sym = gtst(1, 0);
4565 } else {
4566 gen_op(TOK_GE);
4567 *case_sym = gtst(1, 0);
4568 vseti(case_reg, 0);
4569 vpushi(v2);
4570 gen_op(TOK_LE);
4571 *case_sym = gtst(1, *case_sym);
4573 gsym(b);
4574 skip(':');
4575 is_expr = 0;
4576 goto block_after_label;
4577 } else
4578 if (tok == TOK_DEFAULT) {
4579 next();
4580 skip(':');
4581 if (!def_sym)
4582 expect("switch");
4583 if (*def_sym)
4584 tcc_error("too many 'default'");
4585 *def_sym = ind;
4586 is_expr = 0;
4587 goto block_after_label;
4588 } else
4589 if (tok == TOK_GOTO) {
4590 next();
4591 if (tok == '*' && gnu_ext) {
4592 /* computed goto */
4593 next();
4594 gexpr();
4595 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4596 expect("pointer");
4597 ggoto();
4598 } else if (tok >= TOK_UIDENT) {
4599 s = label_find(tok);
4600 /* put forward definition if needed */
4601 if (!s) {
4602 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4603 } else {
4604 if (s->r == LABEL_DECLARED)
4605 s->r = LABEL_FORWARD;
4607 /* label already defined */
4608 if (s->r & LABEL_FORWARD)
4609 s->jnext = gjmp(s->jnext);
4610 else
4611 gjmp_addr(s->jnext);
4612 next();
4613 } else {
4614 expect("label identifier");
4616 skip(';');
4617 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4618 asm_instr();
4619 } else {
4620 b = is_label();
4621 if (b) {
4622 /* label case */
4623 s = label_find(b);
4624 if (s) {
4625 if (s->r == LABEL_DEFINED)
4626 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
4627 gsym(s->jnext);
4628 s->r = LABEL_DEFINED;
4629 } else {
4630 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4632 s->jnext = ind;
4633 /* we accept this, but it is a mistake */
4634 block_after_label:
4635 if (tok == '}') {
4636 tcc_warning("deprecated use of label at end of compound statement");
4637 } else {
4638 if (is_expr)
4639 vpop();
4640 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4642 } else {
4643 /* expression case */
4644 if (tok != ';') {
4645 if (is_expr) {
4646 vpop();
4647 gexpr();
4648 } else {
4649 gexpr();
4650 vpop();
4653 skip(';');
4658 /* t is the array or struct type. c is the array or struct
4659 address. cur_index/cur_field is the pointer to the current
4660 value. 'size_only' is true if only size info is needed (only used
4661 in arrays) */
4662 static void decl_designator(CType *type, Section *sec, unsigned long c,
4663 int *cur_index, Sym **cur_field,
4664 int size_only)
4666 Sym *s, *f;
4667 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4668 CType type1;
4670 notfirst = 0;
4671 elem_size = 0;
4672 nb_elems = 1;
4673 if (gnu_ext && (l = is_label()) != 0)
4674 goto struct_field;
4675 while (tok == '[' || tok == '.') {
4676 if (tok == '[') {
4677 if (!(type->t & VT_ARRAY))
4678 expect("array type");
4679 s = type->ref;
4680 next();
4681 index = expr_const();
4682 if (index < 0 || (s->c >= 0 && index >= s->c))
4683 expect("invalid index");
4684 if (tok == TOK_DOTS && gnu_ext) {
4685 next();
4686 index_last = expr_const();
4687 if (index_last < 0 ||
4688 (s->c >= 0 && index_last >= s->c) ||
4689 index_last < index)
4690 expect("invalid index");
4691 } else {
4692 index_last = index;
4694 skip(']');
4695 if (!notfirst)
4696 *cur_index = index_last;
4697 type = pointed_type(type);
4698 elem_size = type_size(type, &align);
4699 c += index * elem_size;
4700 /* NOTE: we only support ranges for last designator */
4701 nb_elems = index_last - index + 1;
4702 if (nb_elems != 1) {
4703 notfirst = 1;
4704 break;
4706 } else {
4707 next();
4708 l = tok;
4709 next();
4710 struct_field:
4711 if ((type->t & VT_BTYPE) != VT_STRUCT)
4712 expect("struct/union type");
4713 s = type->ref;
4714 l |= SYM_FIELD;
4715 f = s->next;
4716 while (f) {
4717 if (f->v == l)
4718 break;
4719 f = f->next;
4721 if (!f)
4722 expect("field");
4723 if (!notfirst)
4724 *cur_field = f;
4725 /* XXX: fix this mess by using explicit storage field */
4726 type1 = f->type;
4727 type1.t |= (type->t & ~VT_TYPE);
4728 type = &type1;
4729 c += f->c;
4731 notfirst = 1;
4733 if (notfirst) {
4734 if (tok == '=') {
4735 next();
4736 } else {
4737 if (!gnu_ext)
4738 expect("=");
4740 } else {
4741 if (type->t & VT_ARRAY) {
4742 index = *cur_index;
4743 type = pointed_type(type);
4744 c += index * type_size(type, &align);
4745 } else {
4746 f = *cur_field;
4747 if (!f)
4748 tcc_error("too many field init");
4749 /* XXX: fix this mess by using explicit storage field */
4750 type1 = f->type;
4751 type1.t |= (type->t & ~VT_TYPE);
4752 type = &type1;
4753 c += f->c;
4756 decl_initializer(type, sec, c, 0, size_only);
4758 /* XXX: make it more general */
4759 if (!size_only && nb_elems > 1) {
4760 unsigned long c_end;
4761 uint8_t *src, *dst;
4762 int i;
4764 if (!sec)
4765 tcc_error("range init not supported yet for dynamic storage");
4766 c_end = c + nb_elems * elem_size;
4767 if (c_end > sec->data_allocated)
4768 section_realloc(sec, c_end);
4769 src = sec->data + c;
4770 dst = src;
4771 for(i = 1; i < nb_elems; i++) {
4772 dst += elem_size;
4773 memcpy(dst, src, elem_size);
4778 #define EXPR_VAL 0
4779 #define EXPR_CONST 1
4780 #define EXPR_ANY 2
4782 /* store a value or an expression directly in global data or in local array */
4783 static void init_putv(CType *type, Section *sec, unsigned long c,
4784 int v, int expr_type)
4786 int saved_global_expr, bt, bit_pos, bit_size;
4787 void *ptr;
4788 unsigned long long bit_mask;
4789 CType dtype;
4791 switch(expr_type) {
4792 case EXPR_VAL:
4793 vpushi(v);
4794 break;
4795 case EXPR_CONST:
4796 /* compound literals must be allocated globally in this case */
4797 saved_global_expr = global_expr;
4798 global_expr = 1;
4799 expr_const1();
4800 global_expr = saved_global_expr;
4801 /* NOTE: symbols are accepted */
4802 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4803 tcc_error("initializer element is not constant");
4804 break;
4805 case EXPR_ANY:
4806 expr_eq();
4807 break;
4810 dtype = *type;
4811 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4813 if (sec) {
4814 /* XXX: not portable */
4815 /* XXX: generate error if incorrect relocation */
4816 gen_assign_cast(&dtype);
4817 bt = type->t & VT_BTYPE;
4818 /* we'll write at most 12 bytes */
4819 if (c + 12 > sec->data_allocated) {
4820 section_realloc(sec, c + 12);
4822 ptr = sec->data + c;
4823 /* XXX: make code faster ? */
4824 if (!(type->t & VT_BITFIELD)) {
4825 bit_pos = 0;
4826 bit_size = 32;
4827 bit_mask = -1LL;
4828 } else {
4829 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4830 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4831 bit_mask = (1LL << bit_size) - 1;
4833 if ((vtop->r & VT_SYM) &&
4834 (bt == VT_BYTE ||
4835 bt == VT_SHORT ||
4836 bt == VT_DOUBLE ||
4837 bt == VT_LDOUBLE ||
4838 bt == VT_LLONG ||
4839 (bt == VT_INT && bit_size != 32)))
4840 tcc_error("initializer element is not computable at load time");
4841 switch(bt) {
4842 case VT_BOOL:
4843 vtop->c.i = (vtop->c.i != 0);
4844 case VT_BYTE:
4845 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4846 break;
4847 case VT_SHORT:
4848 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4849 break;
4850 case VT_DOUBLE:
4851 *(double *)ptr = vtop->c.d;
4852 break;
4853 case VT_LDOUBLE:
4854 *(long double *)ptr = vtop->c.ld;
4855 break;
4856 case VT_LLONG:
4857 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4858 break;
4859 default:
4860 if (vtop->r & VT_SYM) {
4861 greloc(sec, vtop->sym, c, R_DATA_PTR);
4863 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4864 break;
4866 vtop--;
4867 } else {
4868 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4869 vswap();
4870 vstore();
4871 vpop();
4875 /* put zeros for variable based init */
4876 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4878 if (sec) {
4879 /* nothing to do because globals are already set to zero */
4880 } else {
4881 vpush_global_sym(&func_old_type, TOK_memset);
4882 vseti(VT_LOCAL, c);
4883 vpushi(0);
4884 vpushi(size);
4885 gfunc_call(3);
4889 /* 't' contains the type and storage info. 'c' is the offset of the
4890 object in section 'sec'. If 'sec' is NULL, it means stack based
4891 allocation. 'first' is true if array '{' must be read (multi
4892 dimension implicit array init handling). 'size_only' is true if
4893 size only evaluation is wanted (only for arrays). */
4894 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4895 int first, int size_only)
4897 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
4898 int size1, align1, expr_type;
4899 Sym *s, *f;
4900 CType *t1;
4902 if (type->t & VT_VLA) {
4903 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
4904 int a;
4905 CValue retcval;
4907 vpush_global_sym(&func_old_type, TOK_alloca);
4908 vla_runtime_type_size(type, &a);
4909 gfunc_call(1);
4911 /* return value */
4912 retcval.i = 0;
4913 vsetc(type, REG_IRET, &retcval);
4914 vset(type, VT_LOCAL|VT_LVAL, c);
4915 vswap();
4916 vstore();
4917 vpop();
4918 #else
4919 tcc_error("variable length arrays unsupported for this target");
4920 #endif
4921 } else if (type->t & VT_ARRAY) {
4922 s = type->ref;
4923 n = s->c;
4924 array_length = 0;
4925 t1 = pointed_type(type);
4926 size1 = type_size(t1, &align1);
4928 no_oblock = 1;
4929 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4930 tok == '{') {
4931 if (tok != '{')
4932 tcc_error("character array initializer must be a literal,"
4933 " optionally enclosed in braces");
4934 skip('{');
4935 no_oblock = 0;
4938 /* only parse strings here if correct type (otherwise: handle
4939 them as ((w)char *) expressions */
4940 if ((tok == TOK_LSTR &&
4941 #ifdef TCC_TARGET_PE
4942 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4943 #else
4944 (t1->t & VT_BTYPE) == VT_INT
4945 #endif
4946 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4947 while (tok == TOK_STR || tok == TOK_LSTR) {
4948 int cstr_len, ch;
4949 CString *cstr;
4951 cstr = tokc.cstr;
4952 /* compute maximum number of chars wanted */
4953 if (tok == TOK_STR)
4954 cstr_len = cstr->size;
4955 else
4956 cstr_len = cstr->size / sizeof(nwchar_t);
4957 cstr_len--;
4958 nb = cstr_len;
4959 if (n >= 0 && nb > (n - array_length))
4960 nb = n - array_length;
4961 if (!size_only) {
4962 if (cstr_len > nb)
4963 tcc_warning("initializer-string for array is too long");
4964 /* in order to go faster for common case (char
4965 string in global variable, we handle it
4966 specifically */
4967 if (sec && tok == TOK_STR && size1 == 1) {
4968 memcpy(sec->data + c + array_length, cstr->data, nb);
4969 } else {
4970 for(i=0;i<nb;i++) {
4971 if (tok == TOK_STR)
4972 ch = ((unsigned char *)cstr->data)[i];
4973 else
4974 ch = ((nwchar_t *)cstr->data)[i];
4975 init_putv(t1, sec, c + (array_length + i) * size1,
4976 ch, EXPR_VAL);
4980 array_length += nb;
4981 next();
4983 /* only add trailing zero if enough storage (no
4984 warning in this case since it is standard) */
4985 if (n < 0 || array_length < n) {
4986 if (!size_only) {
4987 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4989 array_length++;
4991 } else {
4992 index = 0;
4993 while (tok != '}') {
4994 decl_designator(type, sec, c, &index, NULL, size_only);
4995 if (n >= 0 && index >= n)
4996 tcc_error("index too large");
4997 /* must put zero in holes (note that doing it that way
4998 ensures that it even works with designators) */
4999 if (!size_only && array_length < index) {
5000 init_putz(t1, sec, c + array_length * size1,
5001 (index - array_length) * size1);
5003 index++;
5004 if (index > array_length)
5005 array_length = index;
5006 /* special test for multi dimensional arrays (may not
5007 be strictly correct if designators are used at the
5008 same time) */
5009 if (index >= n && no_oblock)
5010 break;
5011 if (tok == '}')
5012 break;
5013 skip(',');
5016 if (!no_oblock)
5017 skip('}');
5018 /* put zeros at the end */
5019 if (!size_only && n >= 0 && array_length < n) {
5020 init_putz(t1, sec, c + array_length * size1,
5021 (n - array_length) * size1);
5023 /* patch type size if needed */
5024 if (n < 0)
5025 s->c = array_length;
5026 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5027 (sec || !first || tok == '{')) {
5028 int par_count;
5030 /* NOTE: the previous test is a specific case for automatic
5031 struct/union init */
5032 /* XXX: union needs only one init */
5034 /* XXX: this test is incorrect for local initializers
5035 beginning with ( without {. It would be much more difficult
5036 to do it correctly (ideally, the expression parser should
5037 be used in all cases) */
5038 par_count = 0;
5039 if (tok == '(') {
5040 AttributeDef ad1;
5041 CType type1;
5042 next();
5043 while (tok == '(') {
5044 par_count++;
5045 next();
5047 if (!parse_btype(&type1, &ad1))
5048 expect("cast");
5049 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5050 #if 0
5051 if (!is_assignable_types(type, &type1))
5052 tcc_error("invalid type for cast");
5053 #endif
5054 skip(')');
5056 no_oblock = 1;
5057 if (first || tok == '{') {
5058 skip('{');
5059 no_oblock = 0;
5061 s = type->ref;
5062 f = s->next;
5063 array_length = 0;
5064 index = 0;
5065 n = s->c;
5066 while (tok != '}') {
5067 decl_designator(type, sec, c, NULL, &f, size_only);
5068 index = f->c;
5069 if (!size_only && array_length < index) {
5070 init_putz(type, sec, c + array_length,
5071 index - array_length);
5073 index = index + type_size(&f->type, &align1);
5074 if (index > array_length)
5075 array_length = index;
5077 /* gr: skip fields from same union - ugly. */
5078 while (f->next) {
5079 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
5080 /* test for same offset */
5081 if (f->next->c != f->c)
5082 break;
5083 /* if yes, test for bitfield shift */
5084 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
5085 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5086 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5087 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
5088 if (bit_pos_1 != bit_pos_2)
5089 break;
5091 f = f->next;
5094 f = f->next;
5095 if (no_oblock && f == NULL)
5096 break;
5097 if (tok == '}')
5098 break;
5099 skip(',');
5101 /* put zeros at the end */
5102 if (!size_only && array_length < n) {
5103 init_putz(type, sec, c + array_length,
5104 n - array_length);
5106 if (!no_oblock)
5107 skip('}');
5108 while (par_count) {
5109 skip(')');
5110 par_count--;
5112 } else if (tok == '{') {
5113 next();
5114 decl_initializer(type, sec, c, first, size_only);
5115 skip('}');
5116 } else if (size_only) {
5117 /* just skip expression */
5118 parlevel = parlevel1 = 0;
5119 while ((parlevel > 0 || parlevel1 > 0 ||
5120 (tok != '}' && tok != ',')) && tok != -1) {
5121 if (tok == '(')
5122 parlevel++;
5123 else if (tok == ')')
5124 parlevel--;
5125 else if (tok == '{')
5126 parlevel1++;
5127 else if (tok == '}')
5128 parlevel1--;
5129 next();
5131 } else {
5132 /* currently, we always use constant expression for globals
5133 (may change for scripting case) */
5134 expr_type = EXPR_CONST;
5135 if (!sec)
5136 expr_type = EXPR_ANY;
5137 init_putv(type, sec, c, 0, expr_type);
5141 /* parse an initializer for type 't' if 'has_init' is non zero, and
5142 allocate space in local or global data space ('r' is either
5143 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5144 variable 'v' with an associated name represented by 'asm_label' of
5145 scope 'scope' is declared before initializers are parsed. If 'v' is
5146 zero, then a reference to the new object is put in the value stack.
5147 If 'has_init' is 2, a special parsing is done to handle string
5148 constants. */
5149 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5150 int has_init, int v, char *asm_label,
5151 int scope)
5153 int size, align, addr, data_offset;
5154 int level;
5155 ParseState saved_parse_state = {0};
5156 TokenString init_str;
5157 Section *sec;
5158 Sym *flexible_array;
5160 flexible_array = NULL;
5161 if ((type->t & VT_BTYPE) == VT_STRUCT) {
5162 Sym *field;
5163 field = type->ref;
5164 while (field && field->next)
5165 field = field->next;
5166 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
5167 flexible_array = field;
5170 size = type_size(type, &align);
5171 /* If unknown size, we must evaluate it before
5172 evaluating initializers because
5173 initializers can generate global data too
5174 (e.g. string pointers or ISOC99 compound
5175 literals). It also simplifies local
5176 initializers handling */
5177 tok_str_new(&init_str);
5178 if (size < 0 || (flexible_array && has_init)) {
5179 if (!has_init)
5180 tcc_error("unknown type size");
5181 /* get all init string */
5182 if (has_init == 2) {
5183 /* only get strings */
5184 while (tok == TOK_STR || tok == TOK_LSTR) {
5185 tok_str_add_tok(&init_str);
5186 next();
5188 } else {
5189 level = 0;
5190 while (level > 0 || (tok != ',' && tok != ';')) {
5191 if (tok < 0)
5192 tcc_error("unexpected end of file in initializer");
5193 tok_str_add_tok(&init_str);
5194 if (tok == '{')
5195 level++;
5196 else if (tok == '}') {
5197 level--;
5198 if (level <= 0) {
5199 next();
5200 break;
5203 next();
5206 tok_str_add(&init_str, -1);
5207 tok_str_add(&init_str, 0);
5209 /* compute size */
5210 save_parse_state(&saved_parse_state);
5212 macro_ptr = init_str.str;
5213 next();
5214 decl_initializer(type, NULL, 0, 1, 1);
5215 /* prepare second initializer parsing */
5216 macro_ptr = init_str.str;
5217 next();
5219 /* if still unknown size, error */
5220 size = type_size(type, &align);
5221 if (size < 0)
5222 tcc_error("unknown type size");
5224 if (flexible_array)
5225 size += flexible_array->type.ref->c * pointed_size(&flexible_array->type);
5226 /* take into account specified alignment if bigger */
5227 if (ad->aligned) {
5228 if (ad->aligned > align)
5229 align = ad->aligned;
5230 } else if (ad->packed) {
5231 align = 1;
5233 if ((r & VT_VALMASK) == VT_LOCAL) {
5234 sec = NULL;
5235 #ifdef CONFIG_TCC_BCHECK
5236 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5237 loc--;
5239 #endif
5240 loc = (loc - size) & -align;
5241 addr = loc;
5242 #ifdef CONFIG_TCC_BCHECK
5243 /* handles bounds */
5244 /* XXX: currently, since we do only one pass, we cannot track
5245 '&' operators, so we add only arrays */
5246 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5247 unsigned long *bounds_ptr;
5248 /* add padding between regions */
5249 loc--;
5250 /* then add local bound info */
5251 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5252 bounds_ptr[0] = addr;
5253 bounds_ptr[1] = size;
5255 #endif
5256 if (v) {
5257 /* local variable */
5258 sym_push(v, type, r, addr);
5259 } else {
5260 /* push local reference */
5261 vset(type, r, addr);
5263 } else {
5264 Sym *sym;
5266 sym = NULL;
5267 if (v && scope == VT_CONST) {
5268 /* see if the symbol was already defined */
5269 sym = sym_find(v);
5270 if (sym) {
5271 if (!is_compatible_types(&sym->type, type))
5272 tcc_error("incompatible types for redefinition of '%s'",
5273 get_tok_str(v, NULL));
5274 if (sym->type.t & VT_EXTERN) {
5275 /* if the variable is extern, it was not allocated */
5276 sym->type.t &= ~VT_EXTERN;
5277 /* set array size if it was ommited in extern
5278 declaration */
5279 if ((sym->type.t & VT_ARRAY) &&
5280 sym->type.ref->c < 0 &&
5281 type->ref->c >= 0)
5282 sym->type.ref->c = type->ref->c;
5283 } else {
5284 /* we accept several definitions of the same
5285 global variable. this is tricky, because we
5286 must play with the SHN_COMMON type of the symbol */
5287 /* XXX: should check if the variable was already
5288 initialized. It is incorrect to initialized it
5289 twice */
5290 /* no init data, we won't add more to the symbol */
5291 if (!has_init)
5292 goto no_alloc;
5297 /* allocate symbol in corresponding section */
5298 sec = ad->section;
5299 if (!sec) {
5300 if (has_init)
5301 sec = data_section;
5302 else if (tcc_state->nocommon)
5303 sec = bss_section;
5305 if (sec) {
5306 data_offset = sec->data_offset;
5307 data_offset = (data_offset + align - 1) & -align;
5308 addr = data_offset;
5309 /* very important to increment global pointer at this time
5310 because initializers themselves can create new initializers */
5311 data_offset += size;
5312 #ifdef CONFIG_TCC_BCHECK
5313 /* add padding if bound check */
5314 if (tcc_state->do_bounds_check)
5315 data_offset++;
5316 #endif
5317 sec->data_offset = data_offset;
5318 /* allocate section space to put the data */
5319 if (sec->sh_type != SHT_NOBITS &&
5320 data_offset > sec->data_allocated)
5321 section_realloc(sec, data_offset);
5322 /* align section if needed */
5323 if (align > sec->sh_addralign)
5324 sec->sh_addralign = align;
5325 } else {
5326 addr = 0; /* avoid warning */
5329 if (v) {
5330 if (scope != VT_CONST || !sym) {
5331 sym = sym_push(v, type, r | VT_SYM, 0);
5332 sym->asm_label = asm_label;
5334 /* update symbol definition */
5335 if (sec) {
5336 put_extern_sym(sym, sec, addr, size);
5337 } else {
5338 ElfW(Sym) *esym;
5339 /* put a common area */
5340 put_extern_sym(sym, NULL, align, size);
5341 /* XXX: find a nicer way */
5342 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5343 esym->st_shndx = SHN_COMMON;
5345 } else {
5346 CValue cval;
5348 /* push global reference */
5349 sym = get_sym_ref(type, sec, addr, size);
5350 cval.ul = 0;
5351 vsetc(type, VT_CONST | VT_SYM, &cval);
5352 vtop->sym = sym;
5354 /* patch symbol weakness */
5355 if (type->t & VT_WEAK)
5356 weaken_symbol(sym);
5357 #ifdef CONFIG_TCC_BCHECK
5358 /* handles bounds now because the symbol must be defined
5359 before for the relocation */
5360 if (tcc_state->do_bounds_check) {
5361 unsigned long *bounds_ptr;
5363 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5364 /* then add global bound info */
5365 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5366 bounds_ptr[0] = 0; /* relocated */
5367 bounds_ptr[1] = size;
5369 #endif
5371 if (has_init || (type->t & VT_VLA)) {
5372 decl_initializer(type, sec, addr, 1, 0);
5373 /* restore parse state if needed */
5374 if (init_str.str) {
5375 tok_str_free(init_str.str);
5376 restore_parse_state(&saved_parse_state);
5378 /* patch flexible array member size back to -1, */
5379 /* for possible subsequent similar declarations */
5380 if (flexible_array)
5381 flexible_array->type.ref->c = -1;
5383 no_alloc: ;
5386 static void put_func_debug(Sym *sym)
5388 char buf[512];
5390 /* stabs info */
5391 /* XXX: we put here a dummy type */
5392 snprintf(buf, sizeof(buf), "%s:%c1",
5393 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5394 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5395 cur_text_section, sym->c);
5396 /* //gr gdb wants a line at the function */
5397 put_stabn(N_SLINE, 0, file->line_num, 0);
5398 last_ind = 0;
5399 last_line_num = 0;
5402 /* parse an old style function declaration list */
5403 /* XXX: check multiple parameter */
5404 static void func_decl_list(Sym *func_sym)
5406 AttributeDef ad;
5407 int v;
5408 Sym *s;
5409 CType btype, type;
5411 /* parse each declaration */
5412 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5413 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5414 if (!parse_btype(&btype, &ad))
5415 expect("declaration list");
5416 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5417 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5418 tok == ';') {
5419 /* we accept no variable after */
5420 } else {
5421 for(;;) {
5422 type = btype;
5423 type_decl(&type, &ad, &v, TYPE_DIRECT);
5424 /* find parameter in function parameter list */
5425 s = func_sym->next;
5426 while (s != NULL) {
5427 if ((s->v & ~SYM_FIELD) == v)
5428 goto found;
5429 s = s->next;
5431 tcc_error("declaration for parameter '%s' but no such parameter",
5432 get_tok_str(v, NULL));
5433 found:
5434 /* check that no storage specifier except 'register' was given */
5435 if (type.t & VT_STORAGE)
5436 tcc_error("storage class specified for '%s'", get_tok_str(v, NULL));
5437 convert_parameter_type(&type);
5438 /* we can add the type (NOTE: it could be local to the function) */
5439 s->type = type;
5440 /* accept other parameters */
5441 if (tok == ',')
5442 next();
5443 else
5444 break;
5447 skip(';');
5451 /* parse a function defined by symbol 'sym' and generate its code in
5452 'cur_text_section' */
5453 static void gen_function(Sym *sym)
5455 int saved_nocode_wanted = nocode_wanted;
5456 nocode_wanted = 0;
5457 ind = cur_text_section->data_offset;
5458 /* NOTE: we patch the symbol size later */
5459 put_extern_sym(sym, cur_text_section, ind, 0);
5460 funcname = get_tok_str(sym->v, NULL);
5461 func_ind = ind;
5462 /* put debug symbol */
5463 if (tcc_state->do_debug)
5464 put_func_debug(sym);
5465 /* push a dummy symbol to enable local sym storage */
5466 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5467 gfunc_prolog(&sym->type);
5468 rsym = 0;
5469 block(NULL, NULL, NULL, NULL, 0, 0);
5470 gsym(rsym);
5471 gfunc_epilog();
5472 cur_text_section->data_offset = ind;
5473 label_pop(&global_label_stack, NULL);
5474 sym_pop(&local_stack, NULL); /* reset local stack */
5475 /* end of function */
5476 /* patch symbol size */
5477 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5478 ind - func_ind;
5479 /* patch symbol weakness (this definition overrules any prototype) */
5480 if (sym->type.t & VT_WEAK)
5481 weaken_symbol(sym);
5482 if (tcc_state->do_debug) {
5483 put_stabn(N_FUN, 0, 0, ind - func_ind);
5485 /* It's better to crash than to generate wrong code */
5486 cur_text_section = NULL;
5487 funcname = ""; /* for safety */
5488 func_vt.t = VT_VOID; /* for safety */
5489 ind = 0; /* for safety */
5490 nocode_wanted = saved_nocode_wanted;
5493 ST_FUNC void gen_inline_functions(void)
5495 Sym *sym;
5496 int *str, inline_generated, i;
5497 struct InlineFunc *fn;
5499 /* iterate while inline function are referenced */
5500 for(;;) {
5501 inline_generated = 0;
5502 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5503 fn = tcc_state->inline_fns[i];
5504 sym = fn->sym;
5505 if (sym && sym->c) {
5506 /* the function was used: generate its code and
5507 convert it to a normal function */
5508 str = fn->token_str;
5509 fn->sym = NULL;
5510 if (file)
5511 strcpy(file->filename, fn->filename);
5512 sym->r = VT_SYM | VT_CONST;
5513 sym->type.t &= ~VT_INLINE;
5515 macro_ptr = str;
5516 next();
5517 cur_text_section = text_section;
5518 gen_function(sym);
5519 macro_ptr = NULL; /* fail safe */
5521 inline_generated = 1;
5524 if (!inline_generated)
5525 break;
5527 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5528 fn = tcc_state->inline_fns[i];
5529 str = fn->token_str;
5530 tok_str_free(str);
5532 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5535 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5536 static int decl0(int l, int is_for_loop_init)
5538 int v, has_init, r;
5539 CType type, btype;
5540 Sym *sym;
5541 AttributeDef ad;
5543 while (1) {
5544 if (!parse_btype(&btype, &ad)) {
5545 if (is_for_loop_init)
5546 return 0;
5547 /* skip redundant ';' */
5548 /* XXX: find more elegant solution */
5549 if (tok == ';') {
5550 next();
5551 continue;
5553 if (l == VT_CONST &&
5554 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5555 /* global asm block */
5556 asm_global_instr();
5557 continue;
5559 /* special test for old K&R protos without explicit int
5560 type. Only accepted when defining global data */
5561 if (l == VT_LOCAL || tok < TOK_DEFINE)
5562 break;
5563 btype.t = VT_INT;
5565 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5566 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5567 tok == ';') {
5568 /* we accept no variable after */
5569 next();
5570 continue;
5572 while (1) { /* iterate thru each declaration */
5573 char *asm_label; // associated asm label
5574 type = btype;
5575 type_decl(&type, &ad, &v, TYPE_DIRECT);
5576 #if 0
5578 char buf[500];
5579 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5580 printf("type = '%s'\n", buf);
5582 #endif
5583 if ((type.t & VT_BTYPE) == VT_FUNC) {
5584 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5585 tcc_error("function without file scope cannot be static");
5587 /* if old style function prototype, we accept a
5588 declaration list */
5589 sym = type.ref;
5590 if (sym->c == FUNC_OLD)
5591 func_decl_list(sym);
5594 asm_label = NULL;
5595 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5596 CString astr;
5598 asm_label_instr(&astr);
5599 asm_label = tcc_strdup(astr.data);
5600 cstr_free(&astr);
5602 /* parse one last attribute list, after asm label */
5603 parse_attribute(&ad);
5606 if (ad.weak)
5607 type.t |= VT_WEAK;
5608 #ifdef TCC_TARGET_PE
5609 if (ad.func_import)
5610 type.t |= VT_IMPORT;
5611 if (ad.func_export)
5612 type.t |= VT_EXPORT;
5613 #endif
5614 if (tok == '{') {
5615 if (l == VT_LOCAL)
5616 tcc_error("cannot use local functions");
5617 if ((type.t & VT_BTYPE) != VT_FUNC)
5618 expect("function definition");
5620 /* reject abstract declarators in function definition */
5621 sym = type.ref;
5622 while ((sym = sym->next) != NULL)
5623 if (!(sym->v & ~SYM_FIELD))
5624 expect("identifier");
5626 /* XXX: cannot do better now: convert extern line to static inline */
5627 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5628 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5630 sym = sym_find(v);
5631 if (sym) {
5632 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5633 goto func_error1;
5635 r = sym->type.ref->r;
5636 /* use func_call from prototype if not defined */
5637 if (FUNC_CALL(r) != FUNC_CDECL
5638 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5639 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5641 /* use export from prototype */
5642 if (FUNC_EXPORT(r))
5643 FUNC_EXPORT(type.ref->r) = 1;
5645 /* use static from prototype */
5646 if (sym->type.t & VT_STATIC)
5647 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5649 if (!is_compatible_types(&sym->type, &type)) {
5650 func_error1:
5651 tcc_error("incompatible types for redefinition of '%s'",
5652 get_tok_str(v, NULL));
5654 /* if symbol is already defined, then put complete type */
5655 sym->type = type;
5656 } else {
5657 /* put function symbol */
5658 sym = global_identifier_push(v, type.t, 0);
5659 sym->type.ref = type.ref;
5662 /* static inline functions are just recorded as a kind
5663 of macro. Their code will be emitted at the end of
5664 the compilation unit only if they are used */
5665 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5666 (VT_INLINE | VT_STATIC)) {
5667 TokenString func_str;
5668 int block_level;
5669 struct InlineFunc *fn;
5670 const char *filename;
5672 tok_str_new(&func_str);
5674 block_level = 0;
5675 for(;;) {
5676 int t;
5677 if (tok == TOK_EOF)
5678 tcc_error("unexpected end of file");
5679 tok_str_add_tok(&func_str);
5680 t = tok;
5681 next();
5682 if (t == '{') {
5683 block_level++;
5684 } else if (t == '}') {
5685 block_level--;
5686 if (block_level == 0)
5687 break;
5690 tok_str_add(&func_str, -1);
5691 tok_str_add(&func_str, 0);
5692 filename = file ? file->filename : "";
5693 fn = tcc_malloc(sizeof *fn + strlen(filename));
5694 strcpy(fn->filename, filename);
5695 fn->sym = sym;
5696 fn->token_str = func_str.str;
5697 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5699 } else {
5700 /* compute text section */
5701 cur_text_section = ad.section;
5702 if (!cur_text_section)
5703 cur_text_section = text_section;
5704 sym->r = VT_SYM | VT_CONST;
5705 gen_function(sym);
5707 break;
5708 } else {
5709 if (btype.t & VT_TYPEDEF) {
5710 /* save typedefed type */
5711 /* XXX: test storage specifiers ? */
5712 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5713 sym->type.t |= VT_TYPEDEF;
5714 } else {
5715 r = 0;
5716 if ((type.t & VT_BTYPE) == VT_FUNC) {
5717 /* external function definition */
5718 /* specific case for func_call attribute */
5719 type.ref->r = INT_ATTR(&ad);
5720 } else if (!(type.t & VT_ARRAY)) {
5721 /* not lvalue if array */
5722 r |= lvalue_type(type.t);
5724 has_init = (tok == '=');
5725 if (has_init && (type.t & VT_VLA))
5726 tcc_error("Variable length array cannot be initialized");
5727 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5728 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5729 !has_init && l == VT_CONST && type.ref->c < 0)) {
5730 /* external variable or function */
5731 /* NOTE: as GCC, uninitialized global static
5732 arrays of null size are considered as
5733 extern */
5734 sym = external_sym(v, &type, r, asm_label);
5736 if (type.t & VT_WEAK)
5737 weaken_symbol(sym);
5739 if (ad.alias_target) {
5740 Section tsec;
5741 Elf32_Sym *esym;
5742 Sym *alias_target;
5744 alias_target = sym_find(ad.alias_target);
5745 if (!alias_target || !alias_target->c)
5746 tcc_error("unsupported forward __alias__ attribute");
5747 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5748 tsec.sh_num = esym->st_shndx;
5749 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5751 } else {
5752 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5753 if (type.t & VT_STATIC)
5754 r |= VT_CONST;
5755 else
5756 r |= l;
5757 if (has_init)
5758 next();
5759 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5762 if (tok != ',') {
5763 if (is_for_loop_init)
5764 return 1;
5765 skip(';');
5766 break;
5768 next();
5770 ad.aligned = 0;
5773 return 0;
5776 ST_FUNC void decl(int l)
5778 decl0(l, 0);