__typeof(t) should not include storage modifiers of t
[tinycc/miki.git] / tccgen.c
blobda39d36d97533766f561a011e92b6ff5f14ab2ec
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 void expr_eq(void);
80 static void unary_type(CType *type);
81 static int is_compatible_parameter_types(CType *type1, CType *type2);
82 static void expr_type(CType *type);
84 ST_INLN int is_float(int t)
86 int bt;
87 bt = t & VT_BTYPE;
88 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
91 ST_FUNC void test_lvalue(void)
93 if (!(vtop->r & VT_LVAL))
94 expect("lvalue");
97 /* ------------------------------------------------------------------------- */
98 /* symbol allocator */
99 static Sym *__sym_malloc(void)
101 Sym *sym_pool, *sym, *last_sym;
102 int i;
104 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
105 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
107 last_sym = sym_free_first;
108 sym = sym_pool;
109 for(i = 0; i < SYM_POOL_NB; i++) {
110 sym->next = last_sym;
111 last_sym = sym;
112 sym++;
114 sym_free_first = last_sym;
115 return last_sym;
118 static inline Sym *sym_malloc(void)
120 Sym *sym;
121 sym = sym_free_first;
122 if (!sym)
123 sym = __sym_malloc();
124 sym_free_first = sym->next;
125 return sym;
128 ST_INLN void sym_free(Sym *sym)
130 sym->next = sym_free_first;
131 tcc_free(sym->asm_label);
132 sym_free_first = sym;
135 /* push, without hashing */
136 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
138 Sym *s;
139 s = sym_malloc();
140 s->asm_label = NULL;
141 s->v = v;
142 s->type.t = t;
143 s->type.ref = NULL;
144 #ifdef _WIN64
145 s->d = NULL;
146 #endif
147 s->c = c;
148 s->next = NULL;
149 /* add in stack */
150 s->prev = *ps;
151 *ps = s;
152 return s;
155 /* find a symbol and return its associated structure. 's' is the top
156 of the symbol stack */
157 ST_FUNC Sym *sym_find2(Sym *s, int v)
159 while (s) {
160 if (s->v == v)
161 return s;
162 s = s->prev;
164 return NULL;
167 /* structure lookup */
168 ST_INLN Sym *struct_find(int v)
170 v -= TOK_IDENT;
171 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
172 return NULL;
173 return table_ident[v]->sym_struct;
176 /* find an identifier */
177 ST_INLN Sym *sym_find(int v)
179 v -= TOK_IDENT;
180 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
181 return NULL;
182 return table_ident[v]->sym_identifier;
185 /* push a given symbol on the symbol stack */
186 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
188 Sym *s, **ps;
189 TokenSym *ts;
191 if (local_stack)
192 ps = &local_stack;
193 else
194 ps = &global_stack;
195 s = sym_push2(ps, v, type->t, c);
196 s->type.ref = type->ref;
197 s->r = r;
198 /* don't record fields or anonymous symbols */
199 /* XXX: simplify */
200 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
201 /* record symbol in token array */
202 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
203 if (v & SYM_STRUCT)
204 ps = &ts->sym_struct;
205 else
206 ps = &ts->sym_identifier;
207 s->prev_tok = *ps;
208 *ps = s;
210 return s;
213 /* push a global identifier */
214 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
216 Sym *s, **ps;
217 s = sym_push2(&global_stack, v, t, c);
218 /* don't record anonymous symbol */
219 if (v < SYM_FIRST_ANOM) {
220 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
221 /* modify the top most local identifier, so that
222 sym_identifier will point to 's' when popped */
223 while (*ps != NULL)
224 ps = &(*ps)->prev_tok;
225 s->prev_tok = NULL;
226 *ps = s;
228 return s;
231 /* pop symbols until top reaches 'b' */
232 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
234 Sym *s, *ss, **ps;
235 TokenSym *ts;
236 int v;
238 s = *ptop;
239 while(s != b) {
240 ss = s->prev;
241 v = s->v;
242 /* remove symbol in token array */
243 /* XXX: simplify */
244 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
245 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
246 if (v & SYM_STRUCT)
247 ps = &ts->sym_struct;
248 else
249 ps = &ts->sym_identifier;
250 *ps = s->prev_tok;
252 sym_free(s);
253 s = ss;
255 *ptop = b;
258 /* ------------------------------------------------------------------------- */
260 ST_FUNC void swap(int *p, int *q)
262 int t;
263 t = *p;
264 *p = *q;
265 *q = t;
268 static void vsetc(CType *type, int r, CValue *vc)
270 int v;
272 if (vtop >= vstack + (VSTACK_SIZE - 1))
273 error("memory full");
274 /* cannot let cpu flags if other instruction are generated. Also
275 avoid leaving VT_JMP anywhere except on the top of the stack
276 because it would complicate the code generator. */
277 if (vtop >= vstack) {
278 v = vtop->r & VT_VALMASK;
279 if (v == VT_CMP || (v & ~1) == VT_JMP)
280 gv(RC_INT);
282 vtop++;
283 vtop->type = *type;
284 vtop->r = r;
285 vtop->r2 = VT_CONST;
286 vtop->c = *vc;
289 /* push constant of type "type" with useless value */
290 void vpush(CType *type)
292 CValue cval;
293 vsetc(type, VT_CONST, &cval);
296 /* push integer constant */
297 ST_FUNC void vpushi(int v)
299 CValue cval;
300 cval.i = v;
301 vsetc(&int_type, VT_CONST, &cval);
304 /* push long long constant */
305 static void vpushll(long long v)
307 CValue cval;
308 CType ctype;
309 ctype.t = VT_LLONG;
310 ctype.ref = 0;
311 cval.ull = v;
312 vsetc(&ctype, VT_CONST, &cval);
315 /* push arbitrary 64bit constant */
316 void vpush64(int ty, unsigned long long v)
318 CValue cval;
319 CType ctype;
320 ctype.t = ty;
321 cval.ull = v;
322 vsetc(&ctype, VT_CONST, &cval);
325 /* Return a static symbol pointing to a section */
326 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
328 int v;
329 Sym *sym;
331 v = anon_sym++;
332 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
333 sym->type.ref = type->ref;
334 sym->r = VT_CONST | VT_SYM;
335 put_extern_sym(sym, sec, offset, size);
336 return sym;
339 /* push a reference to a section offset by adding a dummy symbol */
340 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
342 CValue cval;
344 cval.ul = 0;
345 vsetc(type, VT_CONST | VT_SYM, &cval);
346 vtop->sym = get_sym_ref(type, sec, offset, size);
349 /* define a new external reference to a symbol 'v' of type 'u' */
350 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
352 Sym *s;
354 s = sym_find(v);
355 if (!s) {
356 /* push forward reference */
357 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
358 s->type.ref = type->ref;
359 s->r = r | VT_CONST | VT_SYM;
361 return s;
364 /* define a new external reference to a symbol 'v' with alternate asm
365 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
366 is no alternate name (most cases) */
367 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
369 Sym *s;
371 s = sym_find(v);
372 if (!s) {
373 /* push forward reference */
374 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
375 s->asm_label = asm_label;
376 s->type.t |= VT_EXTERN;
377 } else if (s->type.ref == func_old_type.ref) {
378 s->type.ref = type->ref;
379 s->r = r | VT_CONST | VT_SYM;
380 s->type.t |= VT_EXTERN;
381 } else if (!is_compatible_types(&s->type, type)) {
382 error("incompatible types for redefinition of '%s'",
383 get_tok_str(v, NULL));
385 return s;
388 /* push a reference to global symbol v */
389 ST_FUNC void vpush_global_sym(CType *type, int v)
391 Sym *sym;
392 CValue cval;
394 sym = external_global_sym(v, type, 0);
395 cval.ul = 0;
396 vsetc(type, VT_CONST | VT_SYM, &cval);
397 vtop->sym = sym;
400 ST_FUNC void vset(CType *type, int r, int v)
402 CValue cval;
404 cval.i = v;
405 vsetc(type, r, &cval);
408 static void vseti(int r, int v)
410 CType type;
411 type.t = VT_INT;
412 type.ref = 0;
413 vset(&type, r, v);
416 ST_FUNC void vswap(void)
418 SValue tmp;
420 tmp = vtop[0];
421 vtop[0] = vtop[-1];
422 vtop[-1] = tmp;
425 ST_FUNC void vpushv(SValue *v)
427 if (vtop >= vstack + (VSTACK_SIZE - 1))
428 error("memory full");
429 vtop++;
430 *vtop = *v;
433 static void vdup(void)
435 vpushv(vtop);
438 /* save r to the memory stack, and mark it as being free */
439 ST_FUNC void save_reg(int r)
441 int l, saved, size, align;
442 SValue *p, sv;
443 CType *type;
445 /* modify all stack values */
446 saved = 0;
447 l = 0;
448 for(p=vstack;p<=vtop;p++) {
449 if ((p->r & VT_VALMASK) == r ||
450 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
451 /* must save value on stack if not already done */
452 if (!saved) {
453 /* NOTE: must reload 'r' because r might be equal to r2 */
454 r = p->r & VT_VALMASK;
455 /* store register in the stack */
456 type = &p->type;
457 if ((p->r & VT_LVAL) ||
458 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
459 #ifdef TCC_TARGET_X86_64
460 type = &char_pointer_type;
461 #else
462 type = &int_type;
463 #endif
464 size = type_size(type, &align);
465 loc = (loc - size) & -align;
466 sv.type.t = type->t;
467 sv.r = VT_LOCAL | VT_LVAL;
468 sv.c.ul = loc;
469 store(r, &sv);
470 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
471 /* x86 specific: need to pop fp register ST0 if saved */
472 if (r == TREG_ST0) {
473 o(0xd8dd); /* fstp %st(0) */
475 #endif
476 #ifndef TCC_TARGET_X86_64
477 /* special long long case */
478 if ((type->t & VT_BTYPE) == VT_LLONG) {
479 sv.c.ul += 4;
480 store(p->r2, &sv);
482 #endif
483 l = loc;
484 saved = 1;
486 /* mark that stack entry as being saved on the stack */
487 if (p->r & VT_LVAL) {
488 /* also clear the bounded flag because the
489 relocation address of the function was stored in
490 p->c.ul */
491 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
492 } else {
493 p->r = lvalue_type(p->type.t) | VT_LOCAL;
495 p->r2 = VT_CONST;
496 p->c.ul = l;
501 #ifdef TCC_TARGET_ARM
502 /* find a register of class 'rc2' with at most one reference on stack.
503 * If none, call get_reg(rc) */
504 ST_FUNC int get_reg_ex(int rc, int rc2)
506 int r;
507 SValue *p;
509 for(r=0;r<NB_REGS;r++) {
510 if (reg_classes[r] & rc2) {
511 int n;
512 n=0;
513 for(p = vstack; p <= vtop; p++) {
514 if ((p->r & VT_VALMASK) == r ||
515 (p->r2 & VT_VALMASK) == r)
516 n++;
518 if (n <= 1)
519 return r;
522 return get_reg(rc);
524 #endif
526 /* find a free register of class 'rc'. If none, save one register */
527 ST_FUNC int get_reg(int rc)
529 int r;
530 SValue *p;
532 /* find a free register */
533 for(r=0;r<NB_REGS;r++) {
534 if (reg_classes[r] & rc) {
535 for(p=vstack;p<=vtop;p++) {
536 if ((p->r & VT_VALMASK) == r ||
537 (p->r2 & VT_VALMASK) == r)
538 goto notfound;
540 return r;
542 notfound: ;
545 /* no register left : free the first one on the stack (VERY
546 IMPORTANT to start from the bottom to ensure that we don't
547 spill registers used in gen_opi()) */
548 for(p=vstack;p<=vtop;p++) {
549 r = p->r & VT_VALMASK;
550 if (r < VT_CONST && (reg_classes[r] & rc))
551 goto save_found;
552 /* also look at second register (if long long) */
553 r = p->r2 & VT_VALMASK;
554 if (r < VT_CONST && (reg_classes[r] & rc)) {
555 save_found:
556 save_reg(r);
557 return r;
560 /* Should never comes here */
561 return -1;
564 /* save registers up to (vtop - n) stack entry */
565 ST_FUNC void save_regs(int n)
567 int r;
568 SValue *p, *p1;
569 p1 = vtop - n;
570 for(p = vstack;p <= p1; p++) {
571 r = p->r & VT_VALMASK;
572 if (r < VT_CONST) {
573 save_reg(r);
578 /* move register 's' to 'r', and flush previous value of r to memory
579 if needed */
580 static void move_reg(int r, int s)
582 SValue sv;
584 if (r != s) {
585 save_reg(r);
586 sv.type.t = VT_INT;
587 sv.r = s;
588 sv.c.ul = 0;
589 load(r, &sv);
593 /* get address of vtop (vtop MUST BE an lvalue) */
594 static void gaddrof(void)
596 vtop->r &= ~VT_LVAL;
597 /* tricky: if saved lvalue, then we can go back to lvalue */
598 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
599 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
602 #ifdef CONFIG_TCC_BCHECK
603 /* generate lvalue bound code */
604 static void gbound(void)
606 int lval_type;
607 CType type1;
609 vtop->r &= ~VT_MUSTBOUND;
610 /* if lvalue, then use checking code before dereferencing */
611 if (vtop->r & VT_LVAL) {
612 /* if not VT_BOUNDED value, then make one */
613 if (!(vtop->r & VT_BOUNDED)) {
614 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
615 /* must save type because we must set it to int to get pointer */
616 type1 = vtop->type;
617 vtop->type.t = VT_INT;
618 gaddrof();
619 vpushi(0);
620 gen_bounded_ptr_add();
621 vtop->r |= lval_type;
622 vtop->type = type1;
624 /* then check for dereferencing */
625 gen_bounded_ptr_deref();
628 #endif
630 /* store vtop a register belonging to class 'rc'. lvalues are
631 converted to values. Cannot be used if cannot be converted to
632 register value (such as structures). */
633 ST_FUNC int gv(int rc)
635 int r, rc2, bit_pos, bit_size, size, align, i;
637 /* NOTE: get_reg can modify vstack[] */
638 if (vtop->type.t & VT_BITFIELD) {
639 CType type;
640 int bits = 32;
641 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
642 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
643 /* remove bit field info to avoid loops */
644 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
645 /* cast to int to propagate signedness in following ops */
646 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
647 type.t = VT_LLONG;
648 bits = 64;
649 } else
650 type.t = VT_INT;
651 if((vtop->type.t & VT_UNSIGNED) ||
652 (vtop->type.t & VT_BTYPE) == VT_BOOL)
653 type.t |= VT_UNSIGNED;
654 gen_cast(&type);
655 /* generate shifts */
656 vpushi(bits - (bit_pos + bit_size));
657 gen_op(TOK_SHL);
658 vpushi(bits - bit_size);
659 /* NOTE: transformed to SHR if unsigned */
660 gen_op(TOK_SAR);
661 r = gv(rc);
662 } else {
663 if (is_float(vtop->type.t) &&
664 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
665 Sym *sym;
666 int *ptr;
667 unsigned long offset;
668 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
669 CValue check;
670 #endif
672 /* XXX: unify with initializers handling ? */
673 /* CPUs usually cannot use float constants, so we store them
674 generically in data segment */
675 size = type_size(&vtop->type, &align);
676 offset = (data_section->data_offset + align - 1) & -align;
677 data_section->data_offset = offset;
678 /* XXX: not portable yet */
679 #if defined(__i386__) || defined(__x86_64__)
680 /* Zero pad x87 tenbyte long doubles */
681 if (size == LDOUBLE_SIZE)
682 vtop->c.tab[2] &= 0xffff;
683 #endif
684 ptr = section_ptr_add(data_section, size);
685 size = size >> 2;
686 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
687 check.d = 1;
688 if(check.tab[0])
689 for(i=0;i<size;i++)
690 ptr[i] = vtop->c.tab[size-1-i];
691 else
692 #endif
693 for(i=0;i<size;i++)
694 ptr[i] = vtop->c.tab[i];
695 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
696 vtop->r |= VT_LVAL | VT_SYM;
697 vtop->sym = sym;
698 vtop->c.ul = 0;
700 #ifdef CONFIG_TCC_BCHECK
701 if (vtop->r & VT_MUSTBOUND)
702 gbound();
703 #endif
705 r = vtop->r & VT_VALMASK;
706 rc2 = RC_INT;
707 if (rc == RC_IRET)
708 rc2 = RC_LRET;
709 /* need to reload if:
710 - constant
711 - lvalue (need to dereference pointer)
712 - already a register, but not in the right class */
713 if (r >= VT_CONST
714 || (vtop->r & VT_LVAL)
715 || !(reg_classes[r] & rc)
716 #ifndef TCC_TARGET_X86_64
717 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
718 #endif
721 r = get_reg(rc);
722 #ifndef TCC_TARGET_X86_64
723 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
724 int r2;
725 unsigned long long ll;
726 /* two register type load : expand to two words
727 temporarily */
728 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
729 /* load constant */
730 ll = vtop->c.ull;
731 vtop->c.ui = ll; /* first word */
732 load(r, vtop);
733 vtop->r = r; /* save register value */
734 vpushi(ll >> 32); /* second word */
735 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
736 (vtop->r & VT_LVAL)) {
737 /* We do not want to modifier the long long
738 pointer here, so the safest (and less
739 efficient) is to save all the other registers
740 in the stack. XXX: totally inefficient. */
741 save_regs(1);
742 /* load from memory */
743 load(r, vtop);
744 vdup();
745 vtop[-1].r = r; /* save register value */
746 /* increment pointer to get second word */
747 vtop->type.t = VT_INT;
748 gaddrof();
749 vpushi(4);
750 gen_op('+');
751 vtop->r |= VT_LVAL;
752 } else {
753 /* move registers */
754 load(r, vtop);
755 vdup();
756 vtop[-1].r = r; /* save register value */
757 vtop->r = vtop[-1].r2;
759 /* allocate second register */
760 r2 = get_reg(rc2);
761 load(r2, vtop);
762 vpop();
763 /* write second register */
764 vtop->r2 = r2;
765 } else
766 #endif
767 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
768 int t1, t;
769 /* lvalue of scalar type : need to use lvalue type
770 because of possible cast */
771 t = vtop->type.t;
772 t1 = t;
773 /* compute memory access type */
774 if (vtop->r & VT_LVAL_BYTE)
775 t = VT_BYTE;
776 else if (vtop->r & VT_LVAL_SHORT)
777 t = VT_SHORT;
778 if (vtop->r & VT_LVAL_UNSIGNED)
779 t |= VT_UNSIGNED;
780 vtop->type.t = t;
781 load(r, vtop);
782 /* restore wanted type */
783 vtop->type.t = t1;
784 } else {
785 /* one register type load */
786 load(r, vtop);
789 vtop->r = r;
790 #ifdef TCC_TARGET_C67
791 /* uses register pairs for doubles */
792 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
793 vtop->r2 = r+1;
794 #endif
796 return r;
799 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
800 ST_FUNC void gv2(int rc1, int rc2)
802 int v;
804 /* generate more generic register first. But VT_JMP or VT_CMP
805 values must be generated first in all cases to avoid possible
806 reload errors */
807 v = vtop[0].r & VT_VALMASK;
808 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
809 vswap();
810 gv(rc1);
811 vswap();
812 gv(rc2);
813 /* test if reload is needed for first register */
814 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
815 vswap();
816 gv(rc1);
817 vswap();
819 } else {
820 gv(rc2);
821 vswap();
822 gv(rc1);
823 vswap();
824 /* test if reload is needed for first register */
825 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
826 gv(rc2);
831 /* wrapper around RC_FRET to return a register by type */
832 static int rc_fret(int t)
834 #ifdef TCC_TARGET_X86_64
835 if (t == VT_LDOUBLE) {
836 return RC_ST0;
838 #endif
839 return RC_FRET;
842 /* wrapper around REG_FRET to return a register by type */
843 static int reg_fret(int t)
845 #ifdef TCC_TARGET_X86_64
846 if (t == VT_LDOUBLE) {
847 return TREG_ST0;
849 #endif
850 return REG_FRET;
853 /* expand long long on stack in two int registers */
854 static void lexpand(void)
856 int u;
858 u = vtop->type.t & VT_UNSIGNED;
859 gv(RC_INT);
860 vdup();
861 vtop[0].r = vtop[-1].r2;
862 vtop[0].r2 = VT_CONST;
863 vtop[-1].r2 = VT_CONST;
864 vtop[0].type.t = VT_INT | u;
865 vtop[-1].type.t = VT_INT | u;
868 #ifdef TCC_TARGET_ARM
869 /* expand long long on stack */
870 ST_FUNC void lexpand_nr(void)
872 int u,v;
874 u = vtop->type.t & VT_UNSIGNED;
875 vdup();
876 vtop->r2 = VT_CONST;
877 vtop->type.t = VT_INT | u;
878 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
879 if (v == VT_CONST) {
880 vtop[-1].c.ui = vtop->c.ull;
881 vtop->c.ui = vtop->c.ull >> 32;
882 vtop->r = VT_CONST;
883 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
884 vtop->c.ui += 4;
885 vtop->r = vtop[-1].r;
886 } else if (v > VT_CONST) {
887 vtop--;
888 lexpand();
889 } else
890 vtop->r = vtop[-1].r2;
891 vtop[-1].r2 = VT_CONST;
892 vtop[-1].type.t = VT_INT | u;
894 #endif
896 /* build a long long from two ints */
897 static void lbuild(int t)
899 gv2(RC_INT, RC_INT);
900 vtop[-1].r2 = vtop[0].r;
901 vtop[-1].type.t = t;
902 vpop();
905 /* rotate n first stack elements to the bottom
906 I1 ... In -> I2 ... In I1 [top is right]
908 static void vrotb(int n)
910 int i;
911 SValue tmp;
913 tmp = vtop[-n + 1];
914 for(i=-n+1;i!=0;i++)
915 vtop[i] = vtop[i+1];
916 vtop[0] = tmp;
919 /* rotate n first stack elements to the top
920 I1 ... In -> In I1 ... I(n-1) [top is right]
922 ST_FUNC void vrott(int n)
924 int i;
925 SValue tmp;
927 tmp = vtop[0];
928 for(i = 0;i < n - 1; i++)
929 vtop[-i] = vtop[-i - 1];
930 vtop[-n + 1] = tmp;
933 #ifdef TCC_TARGET_ARM
934 /* like vrott but in other direction
935 In ... I1 -> I(n-1) ... I1 In [top is right]
937 ST_FUNC void vnrott(int n)
939 int i;
940 SValue tmp;
942 tmp = vtop[-n + 1];
943 for(i = n - 1; i > 0; i--)
944 vtop[-i] = vtop[-i + 1];
945 vtop[0] = tmp;
947 #endif
949 /* pop stack value */
950 ST_FUNC void vpop(void)
952 int v;
953 v = vtop->r & VT_VALMASK;
954 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
955 /* for x86, we need to pop the FP stack */
956 if (v == TREG_ST0 && !nocode_wanted) {
957 o(0xd8dd); /* fstp %st(0) */
958 } else
959 #endif
960 if (v == VT_JMP || v == VT_JMPI) {
961 /* need to put correct jump if && or || without test */
962 gsym(vtop->c.ul);
964 vtop--;
967 /* convert stack entry to register and duplicate its value in another
968 register */
969 static void gv_dup(void)
971 int rc, t, r, r1;
972 SValue sv;
974 t = vtop->type.t;
975 if ((t & VT_BTYPE) == VT_LLONG) {
976 lexpand();
977 gv_dup();
978 vswap();
979 vrotb(3);
980 gv_dup();
981 vrotb(4);
982 /* stack: H L L1 H1 */
983 lbuild(t);
984 vrotb(3);
985 vrotb(3);
986 vswap();
987 lbuild(t);
988 vswap();
989 } else {
990 /* duplicate value */
991 rc = RC_INT;
992 sv.type.t = VT_INT;
993 if (is_float(t)) {
994 rc = RC_FLOAT;
995 #ifdef TCC_TARGET_X86_64
996 if ((t & VT_BTYPE) == VT_LDOUBLE) {
997 rc = RC_ST0;
999 #endif
1000 sv.type.t = t;
1002 r = gv(rc);
1003 r1 = get_reg(rc);
1004 sv.r = r;
1005 sv.c.ul = 0;
1006 load(r1, &sv); /* move r to r1 */
1007 vdup();
1008 /* duplicates value */
1009 if (r != r1)
1010 vtop->r = r1;
1014 #ifndef TCC_TARGET_X86_64
1015 /* generate CPU independent (unsigned) long long operations */
1016 static void gen_opl(int op)
1018 int t, a, b, op1, c, i;
1019 int func;
1020 unsigned short reg_iret = REG_IRET;
1021 unsigned short reg_lret = REG_LRET;
1022 SValue tmp;
1024 switch(op) {
1025 case '/':
1026 case TOK_PDIV:
1027 func = TOK___divdi3;
1028 goto gen_func;
1029 case TOK_UDIV:
1030 func = TOK___udivdi3;
1031 goto gen_func;
1032 case '%':
1033 func = TOK___moddi3;
1034 goto gen_mod_func;
1035 case TOK_UMOD:
1036 func = TOK___umoddi3;
1037 gen_mod_func:
1038 #ifdef TCC_ARM_EABI
1039 reg_iret = TREG_R2;
1040 reg_lret = TREG_R3;
1041 #endif
1042 gen_func:
1043 /* call generic long long function */
1044 vpush_global_sym(&func_old_type, func);
1045 vrott(3);
1046 gfunc_call(2);
1047 vpushi(0);
1048 vtop->r = reg_iret;
1049 vtop->r2 = reg_lret;
1050 break;
1051 case '^':
1052 case '&':
1053 case '|':
1054 case '*':
1055 case '+':
1056 case '-':
1057 t = vtop->type.t;
1058 vswap();
1059 lexpand();
1060 vrotb(3);
1061 lexpand();
1062 /* stack: L1 H1 L2 H2 */
1063 tmp = vtop[0];
1064 vtop[0] = vtop[-3];
1065 vtop[-3] = tmp;
1066 tmp = vtop[-2];
1067 vtop[-2] = vtop[-3];
1068 vtop[-3] = tmp;
1069 vswap();
1070 /* stack: H1 H2 L1 L2 */
1071 if (op == '*') {
1072 vpushv(vtop - 1);
1073 vpushv(vtop - 1);
1074 gen_op(TOK_UMULL);
1075 lexpand();
1076 /* stack: H1 H2 L1 L2 ML MH */
1077 for(i=0;i<4;i++)
1078 vrotb(6);
1079 /* stack: ML MH H1 H2 L1 L2 */
1080 tmp = vtop[0];
1081 vtop[0] = vtop[-2];
1082 vtop[-2] = tmp;
1083 /* stack: ML MH H1 L2 H2 L1 */
1084 gen_op('*');
1085 vrotb(3);
1086 vrotb(3);
1087 gen_op('*');
1088 /* stack: ML MH M1 M2 */
1089 gen_op('+');
1090 gen_op('+');
1091 } else if (op == '+' || op == '-') {
1092 /* XXX: add non carry method too (for MIPS or alpha) */
1093 if (op == '+')
1094 op1 = TOK_ADDC1;
1095 else
1096 op1 = TOK_SUBC1;
1097 gen_op(op1);
1098 /* stack: H1 H2 (L1 op L2) */
1099 vrotb(3);
1100 vrotb(3);
1101 gen_op(op1 + 1); /* TOK_xxxC2 */
1102 } else {
1103 gen_op(op);
1104 /* stack: H1 H2 (L1 op L2) */
1105 vrotb(3);
1106 vrotb(3);
1107 /* stack: (L1 op L2) H1 H2 */
1108 gen_op(op);
1109 /* stack: (L1 op L2) (H1 op H2) */
1111 /* stack: L H */
1112 lbuild(t);
1113 break;
1114 case TOK_SAR:
1115 case TOK_SHR:
1116 case TOK_SHL:
1117 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1118 t = vtop[-1].type.t;
1119 vswap();
1120 lexpand();
1121 vrotb(3);
1122 /* stack: L H shift */
1123 c = (int)vtop->c.i;
1124 /* constant: simpler */
1125 /* NOTE: all comments are for SHL. the other cases are
1126 done by swaping words */
1127 vpop();
1128 if (op != TOK_SHL)
1129 vswap();
1130 if (c >= 32) {
1131 /* stack: L H */
1132 vpop();
1133 if (c > 32) {
1134 vpushi(c - 32);
1135 gen_op(op);
1137 if (op != TOK_SAR) {
1138 vpushi(0);
1139 } else {
1140 gv_dup();
1141 vpushi(31);
1142 gen_op(TOK_SAR);
1144 vswap();
1145 } else {
1146 vswap();
1147 gv_dup();
1148 /* stack: H L L */
1149 vpushi(c);
1150 gen_op(op);
1151 vswap();
1152 vpushi(32 - c);
1153 if (op == TOK_SHL)
1154 gen_op(TOK_SHR);
1155 else
1156 gen_op(TOK_SHL);
1157 vrotb(3);
1158 /* stack: L L H */
1159 vpushi(c);
1160 if (op == TOK_SHL)
1161 gen_op(TOK_SHL);
1162 else
1163 gen_op(TOK_SHR);
1164 gen_op('|');
1166 if (op != TOK_SHL)
1167 vswap();
1168 lbuild(t);
1169 } else {
1170 /* XXX: should provide a faster fallback on x86 ? */
1171 switch(op) {
1172 case TOK_SAR:
1173 func = TOK___ashrdi3;
1174 goto gen_func;
1175 case TOK_SHR:
1176 func = TOK___lshrdi3;
1177 goto gen_func;
1178 case TOK_SHL:
1179 func = TOK___ashldi3;
1180 goto gen_func;
1183 break;
1184 default:
1185 /* compare operations */
1186 t = vtop->type.t;
1187 vswap();
1188 lexpand();
1189 vrotb(3);
1190 lexpand();
1191 /* stack: L1 H1 L2 H2 */
1192 tmp = vtop[-1];
1193 vtop[-1] = vtop[-2];
1194 vtop[-2] = tmp;
1195 /* stack: L1 L2 H1 H2 */
1196 /* compare high */
1197 op1 = op;
1198 /* when values are equal, we need to compare low words. since
1199 the jump is inverted, we invert the test too. */
1200 if (op1 == TOK_LT)
1201 op1 = TOK_LE;
1202 else if (op1 == TOK_GT)
1203 op1 = TOK_GE;
1204 else if (op1 == TOK_ULT)
1205 op1 = TOK_ULE;
1206 else if (op1 == TOK_UGT)
1207 op1 = TOK_UGE;
1208 a = 0;
1209 b = 0;
1210 gen_op(op1);
1211 if (op1 != TOK_NE) {
1212 a = gtst(1, 0);
1214 if (op != TOK_EQ) {
1215 /* generate non equal test */
1216 /* XXX: NOT PORTABLE yet */
1217 if (a == 0) {
1218 b = gtst(0, 0);
1219 } else {
1220 #if defined(TCC_TARGET_I386)
1221 b = psym(0x850f, 0);
1222 #elif defined(TCC_TARGET_ARM)
1223 b = ind;
1224 o(0x1A000000 | encbranch(ind, 0, 1));
1225 #elif defined(TCC_TARGET_C67)
1226 error("not implemented");
1227 #else
1228 #error not supported
1229 #endif
1232 /* compare low. Always unsigned */
1233 op1 = op;
1234 if (op1 == TOK_LT)
1235 op1 = TOK_ULT;
1236 else if (op1 == TOK_LE)
1237 op1 = TOK_ULE;
1238 else if (op1 == TOK_GT)
1239 op1 = TOK_UGT;
1240 else if (op1 == TOK_GE)
1241 op1 = TOK_UGE;
1242 gen_op(op1);
1243 a = gtst(1, a);
1244 gsym(b);
1245 vseti(VT_JMPI, a);
1246 break;
1249 #endif
1251 /* handle integer constant optimizations and various machine
1252 independent opt */
1253 static void gen_opic(int op)
1255 int c1, c2, t1, t2, n;
1256 SValue *v1, *v2;
1257 long long l1, l2;
1258 typedef unsigned long long U;
1260 v1 = vtop - 1;
1261 v2 = vtop;
1262 t1 = v1->type.t & VT_BTYPE;
1263 t2 = v2->type.t & VT_BTYPE;
1265 if (t1 == VT_LLONG)
1266 l1 = v1->c.ll;
1267 else if (v1->type.t & VT_UNSIGNED)
1268 l1 = v1->c.ui;
1269 else
1270 l1 = v1->c.i;
1272 if (t2 == VT_LLONG)
1273 l2 = v2->c.ll;
1274 else if (v2->type.t & VT_UNSIGNED)
1275 l2 = v2->c.ui;
1276 else
1277 l2 = v2->c.i;
1279 /* currently, we cannot do computations with forward symbols */
1280 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1281 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1282 if (c1 && c2) {
1283 switch(op) {
1284 case '+': l1 += l2; break;
1285 case '-': l1 -= l2; break;
1286 case '&': l1 &= l2; break;
1287 case '^': l1 ^= l2; break;
1288 case '|': l1 |= l2; break;
1289 case '*': l1 *= l2; break;
1291 case TOK_PDIV:
1292 case '/':
1293 case '%':
1294 case TOK_UDIV:
1295 case TOK_UMOD:
1296 /* if division by zero, generate explicit division */
1297 if (l2 == 0) {
1298 if (const_wanted)
1299 error("division by zero in constant");
1300 goto general_case;
1302 switch(op) {
1303 default: l1 /= l2; break;
1304 case '%': l1 %= l2; break;
1305 case TOK_UDIV: l1 = (U)l1 / l2; break;
1306 case TOK_UMOD: l1 = (U)l1 % l2; break;
1308 break;
1309 case TOK_SHL: l1 <<= l2; break;
1310 case TOK_SHR: l1 = (U)l1 >> l2; break;
1311 case TOK_SAR: l1 >>= l2; break;
1312 /* tests */
1313 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1314 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1315 case TOK_EQ: l1 = l1 == l2; break;
1316 case TOK_NE: l1 = l1 != l2; break;
1317 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1318 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1319 case TOK_LT: l1 = l1 < l2; break;
1320 case TOK_GE: l1 = l1 >= l2; break;
1321 case TOK_LE: l1 = l1 <= l2; break;
1322 case TOK_GT: l1 = l1 > l2; break;
1323 /* logical */
1324 case TOK_LAND: l1 = l1 && l2; break;
1325 case TOK_LOR: l1 = l1 || l2; break;
1326 default:
1327 goto general_case;
1329 v1->c.ll = l1;
1330 vtop--;
1331 } else {
1332 /* if commutative ops, put c2 as constant */
1333 if (c1 && (op == '+' || op == '&' || op == '^' ||
1334 op == '|' || op == '*')) {
1335 vswap();
1336 c2 = c1; //c = c1, c1 = c2, c2 = c;
1337 l2 = l1; //l = l1, l1 = l2, l2 = l;
1339 /* Filter out NOP operations like x*1, x-0, x&-1... */
1340 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1341 op == TOK_PDIV) &&
1342 l2 == 1) ||
1343 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1344 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1345 l2 == 0) ||
1346 (op == '&' &&
1347 l2 == -1))) {
1348 /* nothing to do */
1349 vtop--;
1350 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1351 /* try to use shifts instead of muls or divs */
1352 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1353 n = -1;
1354 while (l2) {
1355 l2 >>= 1;
1356 n++;
1358 vtop->c.ll = n;
1359 if (op == '*')
1360 op = TOK_SHL;
1361 else if (op == TOK_PDIV)
1362 op = TOK_SAR;
1363 else
1364 op = TOK_SHR;
1366 goto general_case;
1367 } else if (c2 && (op == '+' || op == '-') &&
1368 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1369 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1370 /* symbol + constant case */
1371 if (op == '-')
1372 l2 = -l2;
1373 vtop--;
1374 vtop->c.ll += l2;
1375 } else {
1376 general_case:
1377 if (!nocode_wanted) {
1378 /* call low level op generator */
1379 if (t1 == VT_LLONG || t2 == VT_LLONG)
1380 gen_opl(op);
1381 else
1382 gen_opi(op);
1383 } else {
1384 vtop--;
1390 /* generate a floating point operation with constant propagation */
1391 static void gen_opif(int op)
1393 int c1, c2;
1394 SValue *v1, *v2;
1395 long double f1, f2;
1397 v1 = vtop - 1;
1398 v2 = vtop;
1399 /* currently, we cannot do computations with forward symbols */
1400 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1401 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1402 if (c1 && c2) {
1403 if (v1->type.t == VT_FLOAT) {
1404 f1 = v1->c.f;
1405 f2 = v2->c.f;
1406 } else if (v1->type.t == VT_DOUBLE) {
1407 f1 = v1->c.d;
1408 f2 = v2->c.d;
1409 } else {
1410 f1 = v1->c.ld;
1411 f2 = v2->c.ld;
1414 /* NOTE: we only do constant propagation if finite number (not
1415 NaN or infinity) (ANSI spec) */
1416 if (!ieee_finite(f1) || !ieee_finite(f2))
1417 goto general_case;
1419 switch(op) {
1420 case '+': f1 += f2; break;
1421 case '-': f1 -= f2; break;
1422 case '*': f1 *= f2; break;
1423 case '/':
1424 if (f2 == 0.0) {
1425 if (const_wanted)
1426 error("division by zero in constant");
1427 goto general_case;
1429 f1 /= f2;
1430 break;
1431 /* XXX: also handles tests ? */
1432 default:
1433 goto general_case;
1435 /* XXX: overflow test ? */
1436 if (v1->type.t == VT_FLOAT) {
1437 v1->c.f = f1;
1438 } else if (v1->type.t == VT_DOUBLE) {
1439 v1->c.d = f1;
1440 } else {
1441 v1->c.ld = f1;
1443 vtop--;
1444 } else {
1445 general_case:
1446 if (!nocode_wanted) {
1447 gen_opf(op);
1448 } else {
1449 vtop--;
1454 static int pointed_size(CType *type)
1456 int align;
1457 return type_size(pointed_type(type), &align);
1460 static inline int is_null_pointer(SValue *p)
1462 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1463 return 0;
1464 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1465 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1468 static inline int is_integer_btype(int bt)
1470 return (bt == VT_BYTE || bt == VT_SHORT ||
1471 bt == VT_INT || bt == VT_LLONG);
1474 /* check types for comparison or substraction of pointers */
1475 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1477 CType *type1, *type2, tmp_type1, tmp_type2;
1478 int bt1, bt2;
1480 /* null pointers are accepted for all comparisons as gcc */
1481 if (is_null_pointer(p1) || is_null_pointer(p2))
1482 return;
1483 type1 = &p1->type;
1484 type2 = &p2->type;
1485 bt1 = type1->t & VT_BTYPE;
1486 bt2 = type2->t & VT_BTYPE;
1487 /* accept comparison between pointer and integer with a warning */
1488 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1489 if (op != TOK_LOR && op != TOK_LAND )
1490 warning("comparison between pointer and integer");
1491 return;
1494 /* both must be pointers or implicit function pointers */
1495 if (bt1 == VT_PTR) {
1496 type1 = pointed_type(type1);
1497 } else if (bt1 != VT_FUNC)
1498 goto invalid_operands;
1500 if (bt2 == VT_PTR) {
1501 type2 = pointed_type(type2);
1502 } else if (bt2 != VT_FUNC) {
1503 invalid_operands:
1504 error("invalid operands to binary %s", get_tok_str(op, NULL));
1506 if ((type1->t & VT_BTYPE) == VT_VOID ||
1507 (type2->t & VT_BTYPE) == VT_VOID)
1508 return;
1509 tmp_type1 = *type1;
1510 tmp_type2 = *type2;
1511 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1512 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1513 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1514 /* gcc-like error if '-' is used */
1515 if (op == '-')
1516 goto invalid_operands;
1517 else
1518 warning("comparison of distinct pointer types lacks a cast");
1522 /* generic gen_op: handles types problems */
1523 ST_FUNC void gen_op(int op)
1525 int u, t1, t2, bt1, bt2, t;
1526 CType type1;
1528 t1 = vtop[-1].type.t;
1529 t2 = vtop[0].type.t;
1530 bt1 = t1 & VT_BTYPE;
1531 bt2 = t2 & VT_BTYPE;
1533 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1534 /* at least one operand is a pointer */
1535 /* relationnal op: must be both pointers */
1536 if (op >= TOK_ULT && op <= TOK_LOR) {
1537 check_comparison_pointer_types(vtop - 1, vtop, op);
1538 /* pointers are handled are unsigned */
1539 #ifdef TCC_TARGET_X86_64
1540 t = VT_LLONG | VT_UNSIGNED;
1541 #else
1542 t = VT_INT | VT_UNSIGNED;
1543 #endif
1544 goto std_op;
1546 /* if both pointers, then it must be the '-' op */
1547 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1548 if (op != '-')
1549 error("cannot use pointers here");
1550 check_comparison_pointer_types(vtop - 1, vtop, op);
1551 /* XXX: check that types are compatible */
1552 u = pointed_size(&vtop[-1].type);
1553 gen_opic(op);
1554 /* set to integer type */
1555 #ifdef TCC_TARGET_X86_64
1556 vtop->type.t = VT_LLONG;
1557 #else
1558 vtop->type.t = VT_INT;
1559 #endif
1560 vpushi(u);
1561 gen_op(TOK_PDIV);
1562 } else {
1563 /* exactly one pointer : must be '+' or '-'. */
1564 if (op != '-' && op != '+')
1565 error("cannot use pointers here");
1566 /* Put pointer as first operand */
1567 if (bt2 == VT_PTR) {
1568 vswap();
1569 swap(&t1, &t2);
1571 type1 = vtop[-1].type;
1572 type1.t &= ~VT_ARRAY;
1573 u = pointed_size(&vtop[-1].type);
1574 if (u < 0)
1575 error("unknown array element size");
1576 #ifdef TCC_TARGET_X86_64
1577 vpushll(u);
1578 #else
1579 /* XXX: cast to int ? (long long case) */
1580 vpushi(u);
1581 #endif
1582 gen_op('*');
1583 #ifdef CONFIG_TCC_BCHECK
1584 /* if evaluating constant expression, no code should be
1585 generated, so no bound check */
1586 if (tcc_state->do_bounds_check && !const_wanted) {
1587 /* if bounded pointers, we generate a special code to
1588 test bounds */
1589 if (op == '-') {
1590 vpushi(0);
1591 vswap();
1592 gen_op('-');
1594 gen_bounded_ptr_add();
1595 } else
1596 #endif
1598 gen_opic(op);
1600 /* put again type if gen_opic() swaped operands */
1601 vtop->type = type1;
1603 } else if (is_float(bt1) || is_float(bt2)) {
1604 /* compute bigger type and do implicit casts */
1605 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1606 t = VT_LDOUBLE;
1607 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1608 t = VT_DOUBLE;
1609 } else {
1610 t = VT_FLOAT;
1612 /* floats can only be used for a few operations */
1613 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1614 (op < TOK_ULT || op > TOK_GT))
1615 error("invalid operands for binary operation");
1616 goto std_op;
1617 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1618 /* cast to biggest op */
1619 t = VT_LLONG;
1620 /* convert to unsigned if it does not fit in a long long */
1621 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1622 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1623 t |= VT_UNSIGNED;
1624 goto std_op;
1625 } else {
1626 /* integer operations */
1627 t = VT_INT;
1628 /* convert to unsigned if it does not fit in an integer */
1629 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1630 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1631 t |= VT_UNSIGNED;
1632 std_op:
1633 /* XXX: currently, some unsigned operations are explicit, so
1634 we modify them here */
1635 if (t & VT_UNSIGNED) {
1636 if (op == TOK_SAR)
1637 op = TOK_SHR;
1638 else if (op == '/')
1639 op = TOK_UDIV;
1640 else if (op == '%')
1641 op = TOK_UMOD;
1642 else if (op == TOK_LT)
1643 op = TOK_ULT;
1644 else if (op == TOK_GT)
1645 op = TOK_UGT;
1646 else if (op == TOK_LE)
1647 op = TOK_ULE;
1648 else if (op == TOK_GE)
1649 op = TOK_UGE;
1651 vswap();
1652 type1.t = t;
1653 gen_cast(&type1);
1654 vswap();
1655 /* special case for shifts and long long: we keep the shift as
1656 an integer */
1657 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1658 type1.t = VT_INT;
1659 gen_cast(&type1);
1660 if (is_float(t))
1661 gen_opif(op);
1662 else
1663 gen_opic(op);
1664 if (op >= TOK_ULT && op <= TOK_GT) {
1665 /* relationnal op: the result is an int */
1666 vtop->type.t = VT_INT;
1667 } else {
1668 vtop->type.t = t;
1673 #ifndef TCC_TARGET_ARM
1674 /* generic itof for unsigned long long case */
1675 static void gen_cvt_itof1(int t)
1677 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1678 (VT_LLONG | VT_UNSIGNED)) {
1680 if (t == VT_FLOAT)
1681 vpush_global_sym(&func_old_type, TOK___floatundisf);
1682 #if LDOUBLE_SIZE != 8
1683 else if (t == VT_LDOUBLE)
1684 vpush_global_sym(&func_old_type, TOK___floatundixf);
1685 #endif
1686 else
1687 vpush_global_sym(&func_old_type, TOK___floatundidf);
1688 vrott(2);
1689 gfunc_call(1);
1690 vpushi(0);
1691 vtop->r = reg_fret(t);
1692 } else {
1693 gen_cvt_itof(t);
1696 #endif
1698 /* generic ftoi for unsigned long long case */
1699 static void gen_cvt_ftoi1(int t)
1701 int st;
1703 if (t == (VT_LLONG | VT_UNSIGNED)) {
1704 /* not handled natively */
1705 st = vtop->type.t & VT_BTYPE;
1706 if (st == VT_FLOAT)
1707 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1708 #if LDOUBLE_SIZE != 8
1709 else if (st == VT_LDOUBLE)
1710 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1711 #endif
1712 else
1713 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1714 vrott(2);
1715 gfunc_call(1);
1716 vpushi(0);
1717 vtop->r = REG_IRET;
1718 vtop->r2 = REG_LRET;
1719 } else {
1720 gen_cvt_ftoi(t);
1724 /* force char or short cast */
1725 static void force_charshort_cast(int t)
1727 int bits, dbt;
1728 dbt = t & VT_BTYPE;
1729 /* XXX: add optimization if lvalue : just change type and offset */
1730 if (dbt == VT_BYTE)
1731 bits = 8;
1732 else
1733 bits = 16;
1734 if (t & VT_UNSIGNED) {
1735 vpushi((1 << bits) - 1);
1736 gen_op('&');
1737 } else {
1738 bits = 32 - bits;
1739 vpushi(bits);
1740 gen_op(TOK_SHL);
1741 /* result must be signed or the SAR is converted to an SHL
1742 This was not the case when "t" was a signed short
1743 and the last value on the stack was an unsigned int */
1744 vtop->type.t &= ~VT_UNSIGNED;
1745 vpushi(bits);
1746 gen_op(TOK_SAR);
1750 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1751 static void gen_cast(CType *type)
1753 int sbt, dbt, sf, df, c, p;
1755 /* special delayed cast for char/short */
1756 /* XXX: in some cases (multiple cascaded casts), it may still
1757 be incorrect */
1758 if (vtop->r & VT_MUSTCAST) {
1759 vtop->r &= ~VT_MUSTCAST;
1760 force_charshort_cast(vtop->type.t);
1763 /* bitfields first get cast to ints */
1764 if (vtop->type.t & VT_BITFIELD) {
1765 gv(RC_INT);
1768 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1769 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1771 if (sbt != dbt) {
1772 sf = is_float(sbt);
1773 df = is_float(dbt);
1774 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1775 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1776 if (c) {
1777 /* constant case: we can do it now */
1778 /* XXX: in ISOC, cannot do it if error in convert */
1779 if (sbt == VT_FLOAT)
1780 vtop->c.ld = vtop->c.f;
1781 else if (sbt == VT_DOUBLE)
1782 vtop->c.ld = vtop->c.d;
1784 if (df) {
1785 if ((sbt & VT_BTYPE) == VT_LLONG) {
1786 if (sbt & VT_UNSIGNED)
1787 vtop->c.ld = vtop->c.ull;
1788 else
1789 vtop->c.ld = vtop->c.ll;
1790 } else if(!sf) {
1791 if (sbt & VT_UNSIGNED)
1792 vtop->c.ld = vtop->c.ui;
1793 else
1794 vtop->c.ld = vtop->c.i;
1797 if (dbt == VT_FLOAT)
1798 vtop->c.f = (float)vtop->c.ld;
1799 else if (dbt == VT_DOUBLE)
1800 vtop->c.d = (double)vtop->c.ld;
1801 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1802 vtop->c.ull = (unsigned long long)vtop->c.ld;
1803 } else if (sf && dbt == VT_BOOL) {
1804 vtop->c.i = (vtop->c.ld != 0);
1805 } else {
1806 if(sf)
1807 vtop->c.ll = (long long)vtop->c.ld;
1808 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1809 vtop->c.ll = vtop->c.ull;
1810 else if (sbt & VT_UNSIGNED)
1811 vtop->c.ll = vtop->c.ui;
1812 #ifdef TCC_TARGET_X86_64
1813 else if (sbt == VT_PTR)
1815 #endif
1816 else if (sbt != VT_LLONG)
1817 vtop->c.ll = vtop->c.i;
1819 if (dbt == (VT_LLONG|VT_UNSIGNED))
1820 vtop->c.ull = vtop->c.ll;
1821 else if (dbt == VT_BOOL)
1822 vtop->c.i = (vtop->c.ll != 0);
1823 else if (dbt != VT_LLONG) {
1824 int s = 0;
1825 if ((dbt & VT_BTYPE) == VT_BYTE)
1826 s = 24;
1827 else if ((dbt & VT_BTYPE) == VT_SHORT)
1828 s = 16;
1830 if(dbt & VT_UNSIGNED)
1831 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1832 else
1833 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1836 } else if (p && dbt == VT_BOOL) {
1837 vtop->r = VT_CONST;
1838 vtop->c.i = 1;
1839 } else if (!nocode_wanted) {
1840 /* non constant case: generate code */
1841 if (sf && df) {
1842 /* convert from fp to fp */
1843 gen_cvt_ftof(dbt);
1844 } else if (df) {
1845 /* convert int to fp */
1846 gen_cvt_itof1(dbt);
1847 } else if (sf) {
1848 /* convert fp to int */
1849 if (dbt == VT_BOOL) {
1850 vpushi(0);
1851 gen_op(TOK_NE);
1852 } else {
1853 /* we handle char/short/etc... with generic code */
1854 if (dbt != (VT_INT | VT_UNSIGNED) &&
1855 dbt != (VT_LLONG | VT_UNSIGNED) &&
1856 dbt != VT_LLONG)
1857 dbt = VT_INT;
1858 gen_cvt_ftoi1(dbt);
1859 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1860 /* additional cast for char/short... */
1861 vtop->type.t = dbt;
1862 gen_cast(type);
1865 #ifndef TCC_TARGET_X86_64
1866 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1867 if ((sbt & VT_BTYPE) != VT_LLONG) {
1868 /* scalar to long long */
1869 /* machine independent conversion */
1870 gv(RC_INT);
1871 /* generate high word */
1872 if (sbt == (VT_INT | VT_UNSIGNED)) {
1873 vpushi(0);
1874 gv(RC_INT);
1875 } else {
1876 if (sbt == VT_PTR) {
1877 /* cast from pointer to int before we apply
1878 shift operation, which pointers don't support*/
1879 gen_cast(&int_type);
1881 gv_dup();
1882 vpushi(31);
1883 gen_op(TOK_SAR);
1885 /* patch second register */
1886 vtop[-1].r2 = vtop->r;
1887 vpop();
1889 #else
1890 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1891 (dbt & VT_BTYPE) == VT_PTR ||
1892 (dbt & VT_BTYPE) == VT_FUNC) {
1893 if ((sbt & VT_BTYPE) != VT_LLONG &&
1894 (sbt & VT_BTYPE) != VT_PTR &&
1895 (sbt & VT_BTYPE) != VT_FUNC) {
1896 /* need to convert from 32bit to 64bit */
1897 int r = gv(RC_INT);
1898 if (sbt != (VT_INT | VT_UNSIGNED)) {
1899 /* x86_64 specific: movslq */
1900 o(0x6348);
1901 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1904 #endif
1905 } else if (dbt == VT_BOOL) {
1906 /* scalar to bool */
1907 vpushi(0);
1908 gen_op(TOK_NE);
1909 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1910 (dbt & VT_BTYPE) == VT_SHORT) {
1911 if (sbt == VT_PTR) {
1912 vtop->type.t = VT_INT;
1913 warning("nonportable conversion from pointer to char/short");
1915 force_charshort_cast(dbt);
1916 } else if ((dbt & VT_BTYPE) == VT_INT) {
1917 /* scalar to int */
1918 if (sbt == VT_LLONG) {
1919 /* from long long: just take low order word */
1920 lexpand();
1921 vpop();
1923 /* if lvalue and single word type, nothing to do because
1924 the lvalue already contains the real type size (see
1925 VT_LVAL_xxx constants) */
1928 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1929 /* if we are casting between pointer types,
1930 we must update the VT_LVAL_xxx size */
1931 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1932 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1934 vtop->type = *type;
1937 /* return type size. Put alignment at 'a' */
1938 ST_FUNC int type_size(CType *type, int *a)
1940 Sym *s;
1941 int bt;
1943 bt = type->t & VT_BTYPE;
1944 if (bt == VT_STRUCT) {
1945 /* struct/union */
1946 s = type->ref;
1947 *a = s->r & 0xffffff;
1948 return s->c;
1949 } else if (bt == VT_PTR) {
1950 if (type->t & VT_ARRAY) {
1951 int ts;
1953 s = type->ref;
1954 ts = type_size(&s->type, a);
1956 if (ts < 0 && s->c < 0)
1957 ts = -ts;
1959 return ts * s->c;
1960 } else {
1961 *a = PTR_SIZE;
1962 return PTR_SIZE;
1964 } else if (bt == VT_LDOUBLE) {
1965 *a = LDOUBLE_ALIGN;
1966 return LDOUBLE_SIZE;
1967 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1968 #ifdef TCC_TARGET_I386
1969 #ifdef TCC_TARGET_PE
1970 *a = 8;
1971 #else
1972 *a = 4;
1973 #endif
1974 #elif defined(TCC_TARGET_ARM)
1975 #ifdef TCC_ARM_EABI
1976 *a = 8;
1977 #else
1978 *a = 4;
1979 #endif
1980 #else
1981 *a = 8;
1982 #endif
1983 return 8;
1984 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1985 *a = 4;
1986 return 4;
1987 } else if (bt == VT_SHORT) {
1988 *a = 2;
1989 return 2;
1990 } else {
1991 /* char, void, function, _Bool */
1992 *a = 1;
1993 return 1;
1997 /* return the pointed type of t */
1998 static inline CType *pointed_type(CType *type)
2000 return &type->ref->type;
2003 /* modify type so that its it is a pointer to type. */
2004 ST_FUNC void mk_pointer(CType *type)
2006 Sym *s;
2007 s = sym_push(SYM_FIELD, type, 0, -1);
2008 type->t = VT_PTR | (type->t & ~VT_TYPE);
2009 type->ref = s;
2012 /* compare function types. OLD functions match any new functions */
2013 static int is_compatible_func(CType *type1, CType *type2)
2015 Sym *s1, *s2;
2017 s1 = type1->ref;
2018 s2 = type2->ref;
2019 if (!is_compatible_types(&s1->type, &s2->type))
2020 return 0;
2021 /* check func_call */
2022 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2023 return 0;
2024 /* XXX: not complete */
2025 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2026 return 1;
2027 if (s1->c != s2->c)
2028 return 0;
2029 while (s1 != NULL) {
2030 if (s2 == NULL)
2031 return 0;
2032 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2033 return 0;
2034 s1 = s1->next;
2035 s2 = s2->next;
2037 if (s2)
2038 return 0;
2039 return 1;
2042 /* return true if type1 and type2 are the same. If unqualified is
2043 true, qualifiers on the types are ignored.
2045 - enums are not checked as gcc __builtin_types_compatible_p ()
2047 static int compare_types(CType *type1, CType *type2, int unqualified)
2049 int bt1, t1, t2;
2051 t1 = type1->t & VT_TYPE;
2052 t2 = type2->t & VT_TYPE;
2053 if (unqualified) {
2054 /* strip qualifiers before comparing */
2055 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2056 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2058 /* XXX: bitfields ? */
2059 if (t1 != t2)
2060 return 0;
2061 /* test more complicated cases */
2062 bt1 = t1 & VT_BTYPE;
2063 if (bt1 == VT_PTR) {
2064 type1 = pointed_type(type1);
2065 type2 = pointed_type(type2);
2066 return is_compatible_types(type1, type2);
2067 } else if (bt1 == VT_STRUCT) {
2068 return (type1->ref == type2->ref);
2069 } else if (bt1 == VT_FUNC) {
2070 return is_compatible_func(type1, type2);
2071 } else {
2072 return 1;
2076 /* return true if type1 and type2 are exactly the same (including
2077 qualifiers).
2079 static int is_compatible_types(CType *type1, CType *type2)
2081 return compare_types(type1,type2,0);
2084 /* return true if type1 and type2 are the same (ignoring qualifiers).
2086 static int is_compatible_parameter_types(CType *type1, CType *type2)
2088 return compare_types(type1,type2,1);
2091 /* print a type. If 'varstr' is not NULL, then the variable is also
2092 printed in the type */
2093 /* XXX: union */
2094 /* XXX: add array and function pointers */
2095 static void type_to_str(char *buf, int buf_size,
2096 CType *type, const char *varstr)
2098 int bt, v, t;
2099 Sym *s, *sa;
2100 char buf1[256];
2101 const char *tstr;
2103 t = type->t & VT_TYPE;
2104 bt = t & VT_BTYPE;
2105 buf[0] = '\0';
2106 if (t & VT_CONSTANT)
2107 pstrcat(buf, buf_size, "const ");
2108 if (t & VT_VOLATILE)
2109 pstrcat(buf, buf_size, "volatile ");
2110 if (t & VT_UNSIGNED)
2111 pstrcat(buf, buf_size, "unsigned ");
2112 switch(bt) {
2113 case VT_VOID:
2114 tstr = "void";
2115 goto add_tstr;
2116 case VT_BOOL:
2117 tstr = "_Bool";
2118 goto add_tstr;
2119 case VT_BYTE:
2120 tstr = "char";
2121 goto add_tstr;
2122 case VT_SHORT:
2123 tstr = "short";
2124 goto add_tstr;
2125 case VT_INT:
2126 tstr = "int";
2127 goto add_tstr;
2128 case VT_LONG:
2129 tstr = "long";
2130 goto add_tstr;
2131 case VT_LLONG:
2132 tstr = "long long";
2133 goto add_tstr;
2134 case VT_FLOAT:
2135 tstr = "float";
2136 goto add_tstr;
2137 case VT_DOUBLE:
2138 tstr = "double";
2139 goto add_tstr;
2140 case VT_LDOUBLE:
2141 tstr = "long double";
2142 add_tstr:
2143 pstrcat(buf, buf_size, tstr);
2144 break;
2145 case VT_ENUM:
2146 case VT_STRUCT:
2147 if (bt == VT_STRUCT)
2148 tstr = "struct ";
2149 else
2150 tstr = "enum ";
2151 pstrcat(buf, buf_size, tstr);
2152 v = type->ref->v & ~SYM_STRUCT;
2153 if (v >= SYM_FIRST_ANOM)
2154 pstrcat(buf, buf_size, "<anonymous>");
2155 else
2156 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2157 break;
2158 case VT_FUNC:
2159 s = type->ref;
2160 type_to_str(buf, buf_size, &s->type, varstr);
2161 pstrcat(buf, buf_size, "(");
2162 sa = s->next;
2163 while (sa != NULL) {
2164 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2165 pstrcat(buf, buf_size, buf1);
2166 sa = sa->next;
2167 if (sa)
2168 pstrcat(buf, buf_size, ", ");
2170 pstrcat(buf, buf_size, ")");
2171 goto no_var;
2172 case VT_PTR:
2173 s = type->ref;
2174 pstrcpy(buf1, sizeof(buf1), "*");
2175 if (varstr)
2176 pstrcat(buf1, sizeof(buf1), varstr);
2177 type_to_str(buf, buf_size, &s->type, buf1);
2178 goto no_var;
2180 if (varstr) {
2181 pstrcat(buf, buf_size, " ");
2182 pstrcat(buf, buf_size, varstr);
2184 no_var: ;
2187 /* verify type compatibility to store vtop in 'dt' type, and generate
2188 casts if needed. */
2189 static void gen_assign_cast(CType *dt)
2191 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2192 char buf1[256], buf2[256];
2193 int dbt, sbt;
2195 st = &vtop->type; /* source type */
2196 dbt = dt->t & VT_BTYPE;
2197 sbt = st->t & VT_BTYPE;
2198 if (dt->t & VT_CONSTANT)
2199 warning("assignment of read-only location");
2200 switch(dbt) {
2201 case VT_PTR:
2202 /* special cases for pointers */
2203 /* '0' can also be a pointer */
2204 if (is_null_pointer(vtop))
2205 goto type_ok;
2206 /* accept implicit pointer to integer cast with warning */
2207 if (is_integer_btype(sbt)) {
2208 warning("assignment makes pointer from integer without a cast");
2209 goto type_ok;
2211 type1 = pointed_type(dt);
2212 /* a function is implicitely a function pointer */
2213 if (sbt == VT_FUNC) {
2214 if ((type1->t & VT_BTYPE) != VT_VOID &&
2215 !is_compatible_types(pointed_type(dt), st))
2216 warning("assignment from incompatible pointer type");
2217 goto type_ok;
2219 if (sbt != VT_PTR)
2220 goto error;
2221 type2 = pointed_type(st);
2222 if ((type1->t & VT_BTYPE) == VT_VOID ||
2223 (type2->t & VT_BTYPE) == VT_VOID) {
2224 /* void * can match anything */
2225 } else {
2226 /* exact type match, except for unsigned */
2227 tmp_type1 = *type1;
2228 tmp_type2 = *type2;
2229 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2230 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2231 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2232 warning("assignment from incompatible pointer type");
2234 /* check const and volatile */
2235 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2236 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2237 warning("assignment discards qualifiers from pointer target type");
2238 break;
2239 case VT_BYTE:
2240 case VT_SHORT:
2241 case VT_INT:
2242 case VT_LLONG:
2243 if (sbt == VT_PTR || sbt == VT_FUNC) {
2244 warning("assignment makes integer from pointer without a cast");
2246 /* XXX: more tests */
2247 break;
2248 case VT_STRUCT:
2249 tmp_type1 = *dt;
2250 tmp_type2 = *st;
2251 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2252 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2253 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2254 error:
2255 type_to_str(buf1, sizeof(buf1), st, NULL);
2256 type_to_str(buf2, sizeof(buf2), dt, NULL);
2257 error("cannot cast '%s' to '%s'", buf1, buf2);
2259 break;
2261 type_ok:
2262 gen_cast(dt);
2265 /* store vtop in lvalue pushed on stack */
2266 ST_FUNC void vstore(void)
2268 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2270 ft = vtop[-1].type.t;
2271 sbt = vtop->type.t & VT_BTYPE;
2272 dbt = ft & VT_BTYPE;
2273 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2274 (sbt == VT_INT && dbt == VT_SHORT)) {
2275 /* optimize char/short casts */
2276 delayed_cast = VT_MUSTCAST;
2277 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2278 /* XXX: factorize */
2279 if (ft & VT_CONSTANT)
2280 warning("assignment of read-only location");
2281 } else {
2282 delayed_cast = 0;
2283 if (!(ft & VT_BITFIELD))
2284 gen_assign_cast(&vtop[-1].type);
2287 if (sbt == VT_STRUCT) {
2288 /* if structure, only generate pointer */
2289 /* structure assignment : generate memcpy */
2290 /* XXX: optimize if small size */
2291 if (!nocode_wanted) {
2292 size = type_size(&vtop->type, &align);
2294 /* destination */
2295 vswap();
2296 vtop->type.t = VT_PTR;
2297 gaddrof();
2299 /* address of memcpy() */
2300 #ifdef TCC_ARM_EABI
2301 if(!(align & 7))
2302 vpush_global_sym(&func_old_type, TOK_memcpy8);
2303 else if(!(align & 3))
2304 vpush_global_sym(&func_old_type, TOK_memcpy4);
2305 else
2306 #endif
2307 vpush_global_sym(&func_old_type, TOK_memcpy);
2309 vswap();
2310 /* source */
2311 vpushv(vtop - 2);
2312 vtop->type.t = VT_PTR;
2313 gaddrof();
2314 /* type size */
2315 vpushi(size);
2316 gfunc_call(3);
2317 } else {
2318 vswap();
2319 vpop();
2321 /* leave source on stack */
2322 } else if (ft & VT_BITFIELD) {
2323 /* bitfield store handling */
2324 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2325 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2326 /* remove bit field info to avoid loops */
2327 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2329 /* duplicate source into other register */
2330 gv_dup();
2331 vswap();
2332 vrott(3);
2334 if((ft & VT_BTYPE) == VT_BOOL) {
2335 gen_cast(&vtop[-1].type);
2336 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2339 /* duplicate destination */
2340 vdup();
2341 vtop[-1] = vtop[-2];
2343 /* mask and shift source */
2344 if((ft & VT_BTYPE) != VT_BOOL) {
2345 if((ft & VT_BTYPE) == VT_LLONG) {
2346 vpushll((1ULL << bit_size) - 1ULL);
2347 } else {
2348 vpushi((1 << bit_size) - 1);
2350 gen_op('&');
2352 vpushi(bit_pos);
2353 gen_op(TOK_SHL);
2354 /* load destination, mask and or with source */
2355 vswap();
2356 if((ft & VT_BTYPE) == VT_LLONG) {
2357 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2358 } else {
2359 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2361 gen_op('&');
2362 gen_op('|');
2363 /* store result */
2364 vstore();
2366 /* pop off shifted source from "duplicate source..." above */
2367 vpop();
2369 } else {
2370 #ifdef CONFIG_TCC_BCHECK
2371 /* bound check case */
2372 if (vtop[-1].r & VT_MUSTBOUND) {
2373 vswap();
2374 gbound();
2375 vswap();
2377 #endif
2378 if (!nocode_wanted) {
2379 rc = RC_INT;
2380 if (is_float(ft)) {
2381 rc = RC_FLOAT;
2382 #ifdef TCC_TARGET_X86_64
2383 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2384 rc = RC_ST0;
2386 #endif
2388 r = gv(rc); /* generate value */
2389 /* if lvalue was saved on stack, must read it */
2390 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2391 SValue sv;
2392 t = get_reg(RC_INT);
2393 #ifdef TCC_TARGET_X86_64
2394 sv.type.t = VT_PTR;
2395 #else
2396 sv.type.t = VT_INT;
2397 #endif
2398 sv.r = VT_LOCAL | VT_LVAL;
2399 sv.c.ul = vtop[-1].c.ul;
2400 load(t, &sv);
2401 vtop[-1].r = t | VT_LVAL;
2403 store(r, vtop - 1);
2404 #ifndef TCC_TARGET_X86_64
2405 /* two word case handling : store second register at word + 4 */
2406 if ((ft & VT_BTYPE) == VT_LLONG) {
2407 vswap();
2408 /* convert to int to increment easily */
2409 vtop->type.t = VT_INT;
2410 gaddrof();
2411 vpushi(4);
2412 gen_op('+');
2413 vtop->r |= VT_LVAL;
2414 vswap();
2415 /* XXX: it works because r2 is spilled last ! */
2416 store(vtop->r2, vtop - 1);
2418 #endif
2420 vswap();
2421 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2422 vtop->r |= delayed_cast;
2426 /* post defines POST/PRE add. c is the token ++ or -- */
2427 ST_FUNC void inc(int post, int c)
2429 test_lvalue();
2430 vdup(); /* save lvalue */
2431 if (post) {
2432 gv_dup(); /* duplicate value */
2433 vrotb(3);
2434 vrotb(3);
2436 /* add constant */
2437 vpushi(c - TOK_MID);
2438 gen_op('+');
2439 vstore(); /* store value */
2440 if (post)
2441 vpop(); /* if post op, return saved value */
2444 /* Parse GNUC __attribute__ extension. Currently, the following
2445 extensions are recognized:
2446 - aligned(n) : set data/function alignment.
2447 - packed : force data alignment to 1
2448 - section(x) : generate data/code in this section.
2449 - unused : currently ignored, but may be used someday.
2450 - regparm(n) : pass function parameters in registers (i386 only)
2452 static void parse_attribute(AttributeDef *ad)
2454 int t, n;
2456 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2457 next();
2458 skip('(');
2459 skip('(');
2460 while (tok != ')') {
2461 if (tok < TOK_IDENT)
2462 expect("attribute name");
2463 t = tok;
2464 next();
2465 switch(t) {
2466 case TOK_SECTION1:
2467 case TOK_SECTION2:
2468 skip('(');
2469 if (tok != TOK_STR)
2470 expect("section name");
2471 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2472 next();
2473 skip(')');
2474 break;
2475 case TOK_ALIAS1:
2476 case TOK_ALIAS2:
2477 skip('(');
2478 if (tok != TOK_STR)
2479 expect("alias(\"target\")");
2480 ad->alias_target = /* save string as token, for later */
2481 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2482 next();
2483 skip(')');
2484 break;
2485 case TOK_ALIGNED1:
2486 case TOK_ALIGNED2:
2487 if (tok == '(') {
2488 next();
2489 n = expr_const();
2490 if (n <= 0 || (n & (n - 1)) != 0)
2491 error("alignment must be a positive power of two");
2492 skip(')');
2493 } else {
2494 n = MAX_ALIGN;
2496 ad->aligned = n;
2497 break;
2498 case TOK_PACKED1:
2499 case TOK_PACKED2:
2500 ad->packed = 1;
2501 break;
2502 case TOK_WEAK1:
2503 case TOK_WEAK2:
2504 ad->weak = 1;
2505 break;
2506 case TOK_UNUSED1:
2507 case TOK_UNUSED2:
2508 /* currently, no need to handle it because tcc does not
2509 track unused objects */
2510 break;
2511 case TOK_NORETURN1:
2512 case TOK_NORETURN2:
2513 /* currently, no need to handle it because tcc does not
2514 track unused objects */
2515 break;
2516 case TOK_CDECL1:
2517 case TOK_CDECL2:
2518 case TOK_CDECL3:
2519 ad->func_call = FUNC_CDECL;
2520 break;
2521 case TOK_STDCALL1:
2522 case TOK_STDCALL2:
2523 case TOK_STDCALL3:
2524 ad->func_call = FUNC_STDCALL;
2525 break;
2526 #ifdef TCC_TARGET_I386
2527 case TOK_REGPARM1:
2528 case TOK_REGPARM2:
2529 skip('(');
2530 n = expr_const();
2531 if (n > 3)
2532 n = 3;
2533 else if (n < 0)
2534 n = 0;
2535 if (n > 0)
2536 ad->func_call = FUNC_FASTCALL1 + n - 1;
2537 skip(')');
2538 break;
2539 case TOK_FASTCALL1:
2540 case TOK_FASTCALL2:
2541 case TOK_FASTCALL3:
2542 ad->func_call = FUNC_FASTCALLW;
2543 break;
2544 #endif
2545 case TOK_MODE:
2546 skip('(');
2547 switch(tok) {
2548 case TOK_MODE_DI:
2549 ad->mode = VT_LLONG + 1;
2550 break;
2551 case TOK_MODE_HI:
2552 ad->mode = VT_SHORT + 1;
2553 break;
2554 case TOK_MODE_SI:
2555 ad->mode = VT_INT + 1;
2556 break;
2557 default:
2558 warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2559 break;
2561 next();
2562 skip(')');
2563 break;
2564 case TOK_DLLEXPORT:
2565 ad->func_export = 1;
2566 break;
2567 case TOK_DLLIMPORT:
2568 ad->func_import = 1;
2569 break;
2570 default:
2571 if (tcc_state->warn_unsupported)
2572 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2573 /* skip parameters */
2574 if (tok == '(') {
2575 int parenthesis = 0;
2576 do {
2577 if (tok == '(')
2578 parenthesis++;
2579 else if (tok == ')')
2580 parenthesis--;
2581 next();
2582 } while (parenthesis && tok != -1);
2584 break;
2586 if (tok != ',')
2587 break;
2588 next();
2590 skip(')');
2591 skip(')');
2595 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2596 static void struct_decl(CType *type, int u)
2598 int a, v, size, align, maxalign, c, offset;
2599 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt, resize;
2600 Sym *s, *ss, *ass, **ps;
2601 AttributeDef ad;
2602 CType type1, btype;
2604 a = tok; /* save decl type */
2605 next();
2606 if (tok != '{') {
2607 v = tok;
2608 next();
2609 /* struct already defined ? return it */
2610 if (v < TOK_IDENT)
2611 expect("struct/union/enum name");
2612 s = struct_find(v);
2613 if (s) {
2614 if (s->type.t != a)
2615 error("invalid type");
2616 goto do_decl;
2618 } else {
2619 v = anon_sym++;
2621 type1.t = a;
2622 /* we put an undefined size for struct/union */
2623 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2624 s->r = 0; /* default alignment is zero as gcc */
2625 /* put struct/union/enum name in type */
2626 do_decl:
2627 type->t = u;
2628 type->ref = s;
2630 if (tok == '{') {
2631 next();
2632 if (s->c != -1)
2633 error("struct/union/enum already defined");
2634 /* cannot be empty */
2635 c = 0;
2636 /* non empty enums are not allowed */
2637 if (a == TOK_ENUM) {
2638 for(;;) {
2639 v = tok;
2640 if (v < TOK_UIDENT)
2641 expect("identifier");
2642 next();
2643 if (tok == '=') {
2644 next();
2645 c = expr_const();
2647 /* enum symbols have static storage */
2648 ss = sym_push(v, &int_type, VT_CONST, c);
2649 ss->type.t |= VT_STATIC;
2650 if (tok != ',')
2651 break;
2652 next();
2653 c++;
2654 /* NOTE: we accept a trailing comma */
2655 if (tok == '}')
2656 break;
2658 skip('}');
2659 } else {
2660 resize = 0;
2661 maxalign = 1;
2662 ps = &s->next;
2663 prevbt = VT_INT;
2664 bit_pos = 0;
2665 offset = 0;
2666 while (tok != '}') {
2667 parse_btype(&btype, &ad);
2668 while (1) {
2669 bit_size = -1;
2670 v = 0;
2671 type1 = btype;
2672 if (tok != ':') {
2673 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2674 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2675 expect("identifier");
2676 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2677 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2678 error("invalid type for '%s'",
2679 get_tok_str(v, NULL));
2681 if (tok == ':') {
2682 next();
2683 bit_size = expr_const();
2684 /* XXX: handle v = 0 case for messages */
2685 if (bit_size < 0)
2686 error("negative width in bit-field '%s'",
2687 get_tok_str(v, NULL));
2688 if (v && bit_size == 0)
2689 error("zero width for bit-field '%s'",
2690 get_tok_str(v, NULL));
2692 size = type_size(&type1, &align);
2693 if (ad.aligned) {
2694 if (align < ad.aligned)
2695 align = ad.aligned;
2696 } else if (ad.packed) {
2697 align = 1;
2698 } else if (*tcc_state->pack_stack_ptr) {
2699 if (align > *tcc_state->pack_stack_ptr)
2700 align = *tcc_state->pack_stack_ptr;
2702 lbit_pos = 0;
2703 if (bit_size >= 0) {
2704 bt = type1.t & VT_BTYPE;
2705 if (bt != VT_INT &&
2706 bt != VT_BYTE &&
2707 bt != VT_SHORT &&
2708 bt != VT_BOOL &&
2709 bt != VT_ENUM &&
2710 bt != VT_LLONG)
2711 error("bitfields must have scalar type");
2712 bsize = size * 8;
2713 if (bit_size > bsize) {
2714 error("width of '%s' exceeds its type",
2715 get_tok_str(v, NULL));
2716 } else if (bit_size == bsize) {
2717 /* no need for bit fields */
2718 bit_pos = 0;
2719 } else if (bit_size == 0) {
2720 /* XXX: what to do if only padding in a
2721 structure ? */
2722 /* zero size: means to pad */
2723 bit_pos = 0;
2724 } else {
2725 /* we do not have enough room ?
2726 did the type change?
2727 is it a union? */
2728 if ((bit_pos + bit_size) > bsize ||
2729 bt != prevbt || a == TOK_UNION)
2730 bit_pos = 0;
2731 lbit_pos = bit_pos;
2732 /* XXX: handle LSB first */
2733 type1.t |= VT_BITFIELD |
2734 (bit_pos << VT_STRUCT_SHIFT) |
2735 (bit_size << (VT_STRUCT_SHIFT + 6));
2736 bit_pos += bit_size;
2738 prevbt = bt;
2739 } else {
2740 bit_pos = 0;
2742 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2743 /* add new memory data only if starting
2744 bit field */
2745 if (lbit_pos == 0) {
2746 if (a == TOK_STRUCT) {
2747 c = (c + align - 1) & -align;
2748 offset = c;
2749 if (size > 0)
2750 c += size;
2751 if (size < 0)
2752 resize = 1;
2753 } else {
2754 offset = 0;
2755 if (size > c)
2756 c = size;
2758 if (align > maxalign)
2759 maxalign = align;
2761 #if 0
2762 printf("add field %s offset=%d",
2763 get_tok_str(v, NULL), offset);
2764 if (type1.t & VT_BITFIELD) {
2765 printf(" pos=%d size=%d",
2766 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2767 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2769 printf("\n");
2770 #endif
2772 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2773 ass = type1.ref;
2774 while ((ass = ass->next) != NULL) {
2775 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2776 *ps = ss;
2777 ps = &ss->next;
2779 } else if (v) {
2780 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2781 *ps = ss;
2782 ps = &ss->next;
2784 if (tok == ';' || tok == TOK_EOF)
2785 break;
2786 skip(',');
2788 skip(';');
2790 skip('}');
2791 /* store size and alignment */
2792 s->c = (c + maxalign - 1) & -maxalign;
2793 s->r = maxalign | (resize ? (1 << 31) : 0);
2798 /* return 0 if no type declaration. otherwise, return the basic type
2799 and skip it.
2801 static int parse_btype(CType *type, AttributeDef *ad)
2803 int t, u, type_found, typespec_found, typedef_found;
2804 Sym *s;
2805 CType type1;
2807 memset(ad, 0, sizeof(AttributeDef));
2808 type_found = 0;
2809 typespec_found = 0;
2810 typedef_found = 0;
2811 t = 0;
2812 while(1) {
2813 switch(tok) {
2814 case TOK_EXTENSION:
2815 /* currently, we really ignore extension */
2816 next();
2817 continue;
2819 /* basic types */
2820 case TOK_CHAR:
2821 u = VT_BYTE;
2822 basic_type:
2823 next();
2824 basic_type1:
2825 if ((t & VT_BTYPE) != 0)
2826 error("too many basic types");
2827 t |= u;
2828 typespec_found = 1;
2829 break;
2830 case TOK_VOID:
2831 u = VT_VOID;
2832 goto basic_type;
2833 case TOK_SHORT:
2834 u = VT_SHORT;
2835 goto basic_type;
2836 case TOK_INT:
2837 next();
2838 typespec_found = 1;
2839 break;
2840 case TOK_LONG:
2841 next();
2842 if ((t & VT_BTYPE) == VT_DOUBLE) {
2843 #ifndef TCC_TARGET_PE
2844 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2845 #endif
2846 } else if ((t & VT_BTYPE) == VT_LONG) {
2847 t = (t & ~VT_BTYPE) | VT_LLONG;
2848 } else {
2849 u = VT_LONG;
2850 goto basic_type1;
2852 break;
2853 case TOK_BOOL:
2854 u = VT_BOOL;
2855 goto basic_type;
2856 case TOK_FLOAT:
2857 u = VT_FLOAT;
2858 goto basic_type;
2859 case TOK_DOUBLE:
2860 next();
2861 if ((t & VT_BTYPE) == VT_LONG) {
2862 #ifdef TCC_TARGET_PE
2863 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2864 #else
2865 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2866 #endif
2867 } else {
2868 u = VT_DOUBLE;
2869 goto basic_type1;
2871 break;
2872 case TOK_ENUM:
2873 struct_decl(&type1, VT_ENUM);
2874 basic_type2:
2875 u = type1.t;
2876 type->ref = type1.ref;
2877 goto basic_type1;
2878 case TOK_STRUCT:
2879 case TOK_UNION:
2880 struct_decl(&type1, VT_STRUCT);
2881 goto basic_type2;
2883 /* type modifiers */
2884 case TOK_CONST1:
2885 case TOK_CONST2:
2886 case TOK_CONST3:
2887 t |= VT_CONSTANT;
2888 next();
2889 break;
2890 case TOK_VOLATILE1:
2891 case TOK_VOLATILE2:
2892 case TOK_VOLATILE3:
2893 t |= VT_VOLATILE;
2894 next();
2895 break;
2896 case TOK_SIGNED1:
2897 case TOK_SIGNED2:
2898 case TOK_SIGNED3:
2899 typespec_found = 1;
2900 t |= VT_SIGNED;
2901 next();
2902 break;
2903 case TOK_REGISTER:
2904 case TOK_AUTO:
2905 case TOK_RESTRICT1:
2906 case TOK_RESTRICT2:
2907 case TOK_RESTRICT3:
2908 next();
2909 break;
2910 case TOK_UNSIGNED:
2911 t |= VT_UNSIGNED;
2912 next();
2913 typespec_found = 1;
2914 break;
2916 /* storage */
2917 case TOK_EXTERN:
2918 t |= VT_EXTERN;
2919 next();
2920 break;
2921 case TOK_STATIC:
2922 t |= VT_STATIC;
2923 next();
2924 break;
2925 case TOK_TYPEDEF:
2926 t |= VT_TYPEDEF;
2927 next();
2928 break;
2929 case TOK_INLINE1:
2930 case TOK_INLINE2:
2931 case TOK_INLINE3:
2932 t |= VT_INLINE;
2933 next();
2934 break;
2936 /* GNUC attribute */
2937 case TOK_ATTRIBUTE1:
2938 case TOK_ATTRIBUTE2:
2939 parse_attribute(ad);
2940 if (ad->mode) {
2941 u = ad->mode -1;
2942 t = (t & ~VT_BTYPE) | u;
2944 break;
2945 /* GNUC typeof */
2946 case TOK_TYPEOF1:
2947 case TOK_TYPEOF2:
2948 case TOK_TYPEOF3:
2949 next();
2950 parse_expr_type(&type1);
2951 /* remove all storage modifiers except typedef */
2952 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
2953 goto basic_type2;
2954 default:
2955 if (typespec_found || typedef_found)
2956 goto the_end;
2957 s = sym_find(tok);
2958 if (!s || !(s->type.t & VT_TYPEDEF))
2959 goto the_end;
2960 typedef_found = 1;
2961 t |= (s->type.t & ~VT_TYPEDEF);
2962 type->ref = s->type.ref;
2963 if (s->r) {
2964 /* get attributes from typedef */
2965 if (0 == ad->aligned)
2966 ad->aligned = FUNC_ALIGN(s->r);
2967 if (0 == ad->func_call)
2968 ad->func_call = FUNC_CALL(s->r);
2969 ad->packed |= FUNC_PACKED(s->r);
2971 next();
2972 typespec_found = 1;
2973 break;
2975 type_found = 1;
2977 the_end:
2978 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2979 error("signed and unsigned modifier");
2980 if (tcc_state->char_is_unsigned) {
2981 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2982 t |= VT_UNSIGNED;
2984 t &= ~VT_SIGNED;
2986 /* long is never used as type */
2987 if ((t & VT_BTYPE) == VT_LONG)
2988 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2989 t = (t & ~VT_BTYPE) | VT_INT;
2990 #else
2991 t = (t & ~VT_BTYPE) | VT_LLONG;
2992 #endif
2993 type->t = t;
2994 return type_found;
2997 /* convert a function parameter type (array to pointer and function to
2998 function pointer) */
2999 static inline void convert_parameter_type(CType *pt)
3001 /* remove const and volatile qualifiers (XXX: const could be used
3002 to indicate a const function parameter */
3003 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3004 /* array must be transformed to pointer according to ANSI C */
3005 pt->t &= ~VT_ARRAY;
3006 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3007 mk_pointer(pt);
3011 ST_FUNC void parse_asm_str(CString *astr)
3013 skip('(');
3014 /* read the string */
3015 if (tok != TOK_STR)
3016 expect("string constant");
3017 cstr_new(astr);
3018 while (tok == TOK_STR) {
3019 /* XXX: add \0 handling too ? */
3020 cstr_cat(astr, tokc.cstr->data);
3021 next();
3023 cstr_ccat(astr, '\0');
3026 /* Parse an asm label and return the label
3027 * Don't forget to free the CString in the caller! */
3028 static void asm_label_instr(CString *astr)
3030 next();
3031 parse_asm_str(astr);
3032 skip(')');
3033 #ifdef ASM_DEBUG
3034 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3035 #endif
3038 static void post_type(CType *type, AttributeDef *ad)
3040 int n, l, t1, arg_size, align;
3041 Sym **plast, *s, *first;
3042 AttributeDef ad1;
3043 CType pt;
3045 if (tok == '(') {
3046 /* function declaration */
3047 next();
3048 l = 0;
3049 first = NULL;
3050 plast = &first;
3051 arg_size = 0;
3052 if (tok != ')') {
3053 for(;;) {
3054 /* read param name and compute offset */
3055 if (l != FUNC_OLD) {
3056 if (!parse_btype(&pt, &ad1)) {
3057 if (l) {
3058 error("invalid type");
3059 } else {
3060 l = FUNC_OLD;
3061 goto old_proto;
3064 l = FUNC_NEW;
3065 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3066 break;
3067 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3068 if ((pt.t & VT_BTYPE) == VT_VOID)
3069 error("parameter declared as void");
3070 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3071 } else {
3072 old_proto:
3073 n = tok;
3074 if (n < TOK_UIDENT)
3075 expect("identifier");
3076 pt.t = VT_INT;
3077 next();
3079 convert_parameter_type(&pt);
3080 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3081 *plast = s;
3082 plast = &s->next;
3083 if (tok == ')')
3084 break;
3085 skip(',');
3086 if (l == FUNC_NEW && tok == TOK_DOTS) {
3087 l = FUNC_ELLIPSIS;
3088 next();
3089 break;
3093 /* if no parameters, then old type prototype */
3094 if (l == 0)
3095 l = FUNC_OLD;
3096 skip(')');
3097 t1 = type->t & VT_STORAGE;
3098 /* NOTE: const is ignored in returned type as it has a special
3099 meaning in gcc / C++ */
3100 type->t &= ~(VT_STORAGE | VT_CONSTANT);
3101 post_type(type, ad);
3102 /* we push a anonymous symbol which will contain the function prototype */
3103 ad->func_args = arg_size;
3104 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3105 s->next = first;
3106 type->t = t1 | VT_FUNC;
3107 type->ref = s;
3108 } else if (tok == '[') {
3109 /* array definition */
3110 next();
3111 if (tok == TOK_RESTRICT1)
3112 next();
3113 n = -1;
3114 if (tok != ']') {
3115 n = expr_const();
3116 if (n < 0)
3117 error("invalid array size");
3119 skip(']');
3120 /* parse next post type */
3121 t1 = type->t & VT_STORAGE;
3122 type->t &= ~VT_STORAGE;
3123 post_type(type, ad);
3125 /* we push a anonymous symbol which will contain the array
3126 element type */
3127 s = sym_push(SYM_FIELD, type, 0, n);
3128 if (n < 0)
3129 ARRAY_RESIZE(s->r) = 1;
3130 type->t = t1 | VT_ARRAY | VT_PTR;
3131 type->ref = s;
3135 /* Parse a type declaration (except basic type), and return the type
3136 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3137 expected. 'type' should contain the basic type. 'ad' is the
3138 attribute definition of the basic type. It can be modified by
3139 type_decl().
3141 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3143 Sym *s;
3144 CType type1, *type2;
3145 int qualifiers;
3147 while (tok == '*') {
3148 qualifiers = 0;
3149 redo:
3150 next();
3151 switch(tok) {
3152 case TOK_CONST1:
3153 case TOK_CONST2:
3154 case TOK_CONST3:
3155 qualifiers |= VT_CONSTANT;
3156 goto redo;
3157 case TOK_VOLATILE1:
3158 case TOK_VOLATILE2:
3159 case TOK_VOLATILE3:
3160 qualifiers |= VT_VOLATILE;
3161 goto redo;
3162 case TOK_RESTRICT1:
3163 case TOK_RESTRICT2:
3164 case TOK_RESTRICT3:
3165 goto redo;
3167 mk_pointer(type);
3168 type->t |= qualifiers;
3171 /* XXX: clarify attribute handling */
3172 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3173 parse_attribute(ad);
3175 /* recursive type */
3176 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3177 type1.t = 0; /* XXX: same as int */
3178 if (tok == '(') {
3179 next();
3180 /* XXX: this is not correct to modify 'ad' at this point, but
3181 the syntax is not clear */
3182 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3183 parse_attribute(ad);
3184 type_decl(&type1, ad, v, td);
3185 skip(')');
3186 } else {
3187 /* type identifier */
3188 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3189 *v = tok;
3190 next();
3191 } else {
3192 if (!(td & TYPE_ABSTRACT))
3193 expect("identifier");
3194 *v = 0;
3197 post_type(type, ad);
3198 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3199 parse_attribute(ad);
3201 if (!type1.t)
3202 return;
3203 /* append type at the end of type1 */
3204 type2 = &type1;
3205 for(;;) {
3206 s = type2->ref;
3207 type2 = &s->type;
3208 if (!type2->t) {
3209 *type2 = *type;
3210 break;
3213 *type = type1;
3216 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3217 ST_FUNC int lvalue_type(int t)
3219 int bt, r;
3220 r = VT_LVAL;
3221 bt = t & VT_BTYPE;
3222 if (bt == VT_BYTE || bt == VT_BOOL)
3223 r |= VT_LVAL_BYTE;
3224 else if (bt == VT_SHORT)
3225 r |= VT_LVAL_SHORT;
3226 else
3227 return r;
3228 if (t & VT_UNSIGNED)
3229 r |= VT_LVAL_UNSIGNED;
3230 return r;
3233 /* indirection with full error checking and bound check */
3234 ST_FUNC void indir(void)
3236 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3237 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3238 return;
3239 expect("pointer");
3241 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3242 gv(RC_INT);
3243 vtop->type = *pointed_type(&vtop->type);
3244 /* Arrays and functions are never lvalues */
3245 if (!(vtop->type.t & VT_ARRAY)
3246 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3247 vtop->r |= lvalue_type(vtop->type.t);
3248 /* if bound checking, the referenced pointer must be checked */
3249 #ifdef CONFIG_TCC_BCHECK
3250 if (tcc_state->do_bounds_check)
3251 vtop->r |= VT_MUSTBOUND;
3252 #endif
3256 /* pass a parameter to a function and do type checking and casting */
3257 static void gfunc_param_typed(Sym *func, Sym *arg)
3259 int func_type;
3260 CType type;
3262 func_type = func->c;
3263 if (func_type == FUNC_OLD ||
3264 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3265 /* default casting : only need to convert float to double */
3266 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3267 type.t = VT_DOUBLE;
3268 gen_cast(&type);
3270 } else if (arg == NULL) {
3271 error("too many arguments to function");
3272 } else {
3273 type = arg->type;
3274 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3275 gen_assign_cast(&type);
3279 /* parse an expression of the form '(type)' or '(expr)' and return its
3280 type */
3281 static void parse_expr_type(CType *type)
3283 int n;
3284 AttributeDef ad;
3286 skip('(');
3287 if (parse_btype(type, &ad)) {
3288 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3289 } else {
3290 expr_type(type);
3292 skip(')');
3295 static void parse_type(CType *type)
3297 AttributeDef ad;
3298 int n;
3300 if (!parse_btype(type, &ad)) {
3301 expect("type");
3303 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3306 static void vpush_tokc(int t)
3308 CType type;
3309 type.t = t;
3310 type.ref = 0;
3311 vsetc(&type, VT_CONST, &tokc);
3314 ST_FUNC void unary(void)
3316 int n, t, align, size, r, sizeof_caller;
3317 CType type;
3318 Sym *s;
3319 AttributeDef ad;
3320 static int in_sizeof = 0;
3322 sizeof_caller = in_sizeof;
3323 in_sizeof = 0;
3324 /* XXX: GCC 2.95.3 does not generate a table although it should be
3325 better here */
3326 tok_next:
3327 switch(tok) {
3328 case TOK_EXTENSION:
3329 next();
3330 goto tok_next;
3331 case TOK_CINT:
3332 case TOK_CCHAR:
3333 case TOK_LCHAR:
3334 vpushi(tokc.i);
3335 next();
3336 break;
3337 case TOK_CUINT:
3338 vpush_tokc(VT_INT | VT_UNSIGNED);
3339 next();
3340 break;
3341 case TOK_CLLONG:
3342 vpush_tokc(VT_LLONG);
3343 next();
3344 break;
3345 case TOK_CULLONG:
3346 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3347 next();
3348 break;
3349 case TOK_CFLOAT:
3350 vpush_tokc(VT_FLOAT);
3351 next();
3352 break;
3353 case TOK_CDOUBLE:
3354 vpush_tokc(VT_DOUBLE);
3355 next();
3356 break;
3357 case TOK_CLDOUBLE:
3358 vpush_tokc(VT_LDOUBLE);
3359 next();
3360 break;
3361 case TOK___FUNCTION__:
3362 if (!gnu_ext)
3363 goto tok_identifier;
3364 /* fall thru */
3365 case TOK___FUNC__:
3367 void *ptr;
3368 int len;
3369 /* special function name identifier */
3370 len = strlen(funcname) + 1;
3371 /* generate char[len] type */
3372 type.t = VT_BYTE;
3373 mk_pointer(&type);
3374 type.t |= VT_ARRAY;
3375 type.ref->c = len;
3376 vpush_ref(&type, data_section, data_section->data_offset, len);
3377 ptr = section_ptr_add(data_section, len);
3378 memcpy(ptr, funcname, len);
3379 next();
3381 break;
3382 case TOK_LSTR:
3383 #ifdef TCC_TARGET_PE
3384 t = VT_SHORT | VT_UNSIGNED;
3385 #else
3386 t = VT_INT;
3387 #endif
3388 goto str_init;
3389 case TOK_STR:
3390 /* string parsing */
3391 t = VT_BYTE;
3392 str_init:
3393 if (tcc_state->warn_write_strings)
3394 t |= VT_CONSTANT;
3395 type.t = t;
3396 mk_pointer(&type);
3397 type.t |= VT_ARRAY;
3398 memset(&ad, 0, sizeof(AttributeDef));
3399 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3400 break;
3401 case '(':
3402 next();
3403 /* cast ? */
3404 if (parse_btype(&type, &ad)) {
3405 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3406 skip(')');
3407 /* check ISOC99 compound literal */
3408 if (tok == '{') {
3409 /* data is allocated locally by default */
3410 if (global_expr)
3411 r = VT_CONST;
3412 else
3413 r = VT_LOCAL;
3414 /* all except arrays are lvalues */
3415 if (!(type.t & VT_ARRAY))
3416 r |= lvalue_type(type.t);
3417 memset(&ad, 0, sizeof(AttributeDef));
3418 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3419 } else {
3420 if (sizeof_caller) {
3421 vpush(&type);
3422 return;
3424 unary();
3425 gen_cast(&type);
3427 } else if (tok == '{') {
3428 /* save all registers */
3429 save_regs(0);
3430 /* statement expression : we do not accept break/continue
3431 inside as GCC does */
3432 block(NULL, NULL, NULL, NULL, 0, 1);
3433 skip(')');
3434 } else {
3435 gexpr();
3436 skip(')');
3438 break;
3439 case '*':
3440 next();
3441 unary();
3442 indir();
3443 break;
3444 case '&':
3445 next();
3446 unary();
3447 /* functions names must be treated as function pointers,
3448 except for unary '&' and sizeof. Since we consider that
3449 functions are not lvalues, we only have to handle it
3450 there and in function calls. */
3451 /* arrays can also be used although they are not lvalues */
3452 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3453 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3454 test_lvalue();
3455 mk_pointer(&vtop->type);
3456 gaddrof();
3457 break;
3458 case '!':
3459 next();
3460 unary();
3461 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3462 CType boolean;
3463 boolean.t = VT_BOOL;
3464 gen_cast(&boolean);
3465 vtop->c.i = !vtop->c.i;
3466 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3467 vtop->c.i = vtop->c.i ^ 1;
3468 else {
3469 save_regs(1);
3470 vseti(VT_JMP, gtst(1, 0));
3472 break;
3473 case '~':
3474 next();
3475 unary();
3476 vpushi(-1);
3477 gen_op('^');
3478 break;
3479 case '+':
3480 next();
3481 /* in order to force cast, we add zero */
3482 unary();
3483 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3484 error("pointer not accepted for unary plus");
3485 vpushi(0);
3486 gen_op('+');
3487 break;
3488 case TOK_SIZEOF:
3489 case TOK_ALIGNOF1:
3490 case TOK_ALIGNOF2:
3491 t = tok;
3492 next();
3493 in_sizeof++;
3494 unary_type(&type); // Perform a in_sizeof = 0;
3495 size = type_size(&type, &align);
3496 if (t == TOK_SIZEOF) {
3497 if (size < 0)
3498 error("sizeof applied to an incomplete type");
3499 vpushi(size);
3500 } else {
3501 vpushi(align);
3503 vtop->type.t |= VT_UNSIGNED;
3504 break;
3506 case TOK_builtin_types_compatible_p:
3508 CType type1, type2;
3509 next();
3510 skip('(');
3511 parse_type(&type1);
3512 skip(',');
3513 parse_type(&type2);
3514 skip(')');
3515 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3516 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3517 vpushi(is_compatible_types(&type1, &type2));
3519 break;
3520 case TOK_builtin_constant_p:
3522 int saved_nocode_wanted, res;
3523 next();
3524 skip('(');
3525 saved_nocode_wanted = nocode_wanted;
3526 nocode_wanted = 1;
3527 gexpr();
3528 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3529 vpop();
3530 nocode_wanted = saved_nocode_wanted;
3531 skip(')');
3532 vpushi(res);
3534 break;
3535 case TOK_builtin_frame_address:
3537 CType type;
3538 next();
3539 skip('(');
3540 if (tok != TOK_CINT) {
3541 error("__builtin_frame_address only takes integers");
3543 if (tokc.i != 0) {
3544 error("TCC only supports __builtin_frame_address(0)");
3546 next();
3547 skip(')');
3548 type.t = VT_VOID;
3549 mk_pointer(&type);
3550 vset(&type, VT_LOCAL, 0);
3552 break;
3553 #ifdef TCC_TARGET_X86_64
3554 case TOK_builtin_va_arg_types:
3556 /* This definition must be synced with stdarg.h */
3557 enum __va_arg_type {
3558 __va_gen_reg, __va_float_reg, __va_stack
3560 CType type;
3561 int bt;
3562 next();
3563 skip('(');
3564 parse_type(&type);
3565 skip(')');
3566 bt = type.t & VT_BTYPE;
3567 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
3568 vpushi(__va_stack);
3569 } else if (bt == VT_FLOAT || bt == VT_DOUBLE) {
3570 vpushi(__va_float_reg);
3571 } else {
3572 vpushi(__va_gen_reg);
3575 break;
3576 #endif
3577 case TOK_INC:
3578 case TOK_DEC:
3579 t = tok;
3580 next();
3581 unary();
3582 inc(0, t);
3583 break;
3584 case '-':
3585 next();
3586 vpushi(0);
3587 unary();
3588 gen_op('-');
3589 break;
3590 case TOK_LAND:
3591 if (!gnu_ext)
3592 goto tok_identifier;
3593 next();
3594 /* allow to take the address of a label */
3595 if (tok < TOK_UIDENT)
3596 expect("label identifier");
3597 s = label_find(tok);
3598 if (!s) {
3599 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3600 } else {
3601 if (s->r == LABEL_DECLARED)
3602 s->r = LABEL_FORWARD;
3604 if (!s->type.t) {
3605 s->type.t = VT_VOID;
3606 mk_pointer(&s->type);
3607 s->type.t |= VT_STATIC;
3609 vset(&s->type, VT_CONST | VT_SYM, 0);
3610 vtop->sym = s;
3611 next();
3612 break;
3614 // special qnan , snan and infinity values
3615 case TOK___NAN__:
3616 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3617 next();
3618 break;
3619 case TOK___SNAN__:
3620 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3621 next();
3622 break;
3623 case TOK___INF__:
3624 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3625 next();
3626 break;
3628 default:
3629 tok_identifier:
3630 t = tok;
3631 next();
3632 if (t < TOK_UIDENT)
3633 expect("identifier");
3634 s = sym_find(t);
3635 if (!s) {
3636 if (tok != '(')
3637 error("'%s' undeclared", get_tok_str(t, NULL));
3638 /* for simple function calls, we tolerate undeclared
3639 external reference to int() function */
3640 if (tcc_state->warn_implicit_function_declaration)
3641 warning("implicit declaration of function '%s'",
3642 get_tok_str(t, NULL));
3643 s = external_global_sym(t, &func_old_type, 0);
3645 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3646 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3647 /* if referencing an inline function, then we generate a
3648 symbol to it if not already done. It will have the
3649 effect to generate code for it at the end of the
3650 compilation unit. Inline function as always
3651 generated in the text section. */
3652 if (!s->c)
3653 put_extern_sym(s, text_section, 0, 0);
3654 r = VT_SYM | VT_CONST;
3655 } else {
3656 r = s->r;
3658 vset(&s->type, r, s->c);
3659 /* if forward reference, we must point to s */
3660 if (vtop->r & VT_SYM) {
3661 vtop->sym = s;
3662 vtop->c.ul = 0;
3664 break;
3667 /* post operations */
3668 while (1) {
3669 if (tok == TOK_INC || tok == TOK_DEC) {
3670 inc(1, tok);
3671 next();
3672 } else if (tok == '.' || tok == TOK_ARROW) {
3673 int qualifiers;
3674 /* field */
3675 if (tok == TOK_ARROW)
3676 indir();
3677 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3678 test_lvalue();
3679 gaddrof();
3680 next();
3681 /* expect pointer on structure */
3682 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3683 expect("struct or union");
3684 s = vtop->type.ref;
3685 /* find field */
3686 tok |= SYM_FIELD;
3687 while ((s = s->next) != NULL) {
3688 if (s->v == tok)
3689 break;
3691 if (!s)
3692 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3693 /* add field offset to pointer */
3694 vtop->type = char_pointer_type; /* change type to 'char *' */
3695 vpushi(s->c);
3696 gen_op('+');
3697 /* change type to field type, and set to lvalue */
3698 vtop->type = s->type;
3699 vtop->type.t |= qualifiers;
3700 /* an array is never an lvalue */
3701 if (!(vtop->type.t & VT_ARRAY)) {
3702 vtop->r |= lvalue_type(vtop->type.t);
3703 #ifdef CONFIG_TCC_BCHECK
3704 /* if bound checking, the referenced pointer must be checked */
3705 if (tcc_state->do_bounds_check)
3706 vtop->r |= VT_MUSTBOUND;
3707 #endif
3709 next();
3710 } else if (tok == '[') {
3711 next();
3712 gexpr();
3713 gen_op('+');
3714 indir();
3715 skip(']');
3716 } else if (tok == '(') {
3717 SValue ret;
3718 Sym *sa;
3719 int nb_args;
3721 /* function call */
3722 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3723 /* pointer test (no array accepted) */
3724 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3725 vtop->type = *pointed_type(&vtop->type);
3726 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3727 goto error_func;
3728 } else {
3729 error_func:
3730 expect("function pointer");
3732 } else {
3733 vtop->r &= ~VT_LVAL; /* no lvalue */
3735 /* get return type */
3736 s = vtop->type.ref;
3737 next();
3738 sa = s->next; /* first parameter */
3739 nb_args = 0;
3740 ret.r2 = VT_CONST;
3741 /* compute first implicit argument if a structure is returned */
3742 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3743 /* get some space for the returned structure */
3744 size = type_size(&s->type, &align);
3745 loc = (loc - size) & -align;
3746 ret.type = s->type;
3747 ret.r = VT_LOCAL | VT_LVAL;
3748 /* pass it as 'int' to avoid structure arg passing
3749 problems */
3750 vseti(VT_LOCAL, loc);
3751 ret.c = vtop->c;
3752 nb_args++;
3753 } else {
3754 ret.type = s->type;
3755 /* return in register */
3756 if (is_float(ret.type.t)) {
3757 ret.r = reg_fret(ret.type.t);
3758 } else {
3759 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3760 ret.r2 = REG_LRET;
3761 ret.r = REG_IRET;
3763 ret.c.i = 0;
3765 if (tok != ')') {
3766 for(;;) {
3767 expr_eq();
3768 gfunc_param_typed(s, sa);
3769 nb_args++;
3770 if (sa)
3771 sa = sa->next;
3772 if (tok == ')')
3773 break;
3774 skip(',');
3777 if (sa)
3778 error("too few arguments to function");
3779 skip(')');
3780 if (!nocode_wanted) {
3781 gfunc_call(nb_args);
3782 } else {
3783 vtop -= (nb_args + 1);
3785 /* return value */
3786 vsetc(&ret.type, ret.r, &ret.c);
3787 vtop->r2 = ret.r2;
3788 } else {
3789 break;
3794 ST_FUNC void expr_prod(void)
3796 int t;
3798 unary();
3799 while (tok == '*' || tok == '/' || tok == '%') {
3800 t = tok;
3801 next();
3802 unary();
3803 gen_op(t);
3807 ST_FUNC void expr_sum(void)
3809 int t;
3811 expr_prod();
3812 while (tok == '+' || tok == '-') {
3813 t = tok;
3814 next();
3815 expr_prod();
3816 gen_op(t);
3820 static void expr_shift(void)
3822 int t;
3824 expr_sum();
3825 while (tok == TOK_SHL || tok == TOK_SAR) {
3826 t = tok;
3827 next();
3828 expr_sum();
3829 gen_op(t);
3833 static void expr_cmp(void)
3835 int t;
3837 expr_shift();
3838 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3839 tok == TOK_ULT || tok == TOK_UGE) {
3840 t = tok;
3841 next();
3842 expr_shift();
3843 gen_op(t);
3847 static void expr_cmpeq(void)
3849 int t;
3851 expr_cmp();
3852 while (tok == TOK_EQ || tok == TOK_NE) {
3853 t = tok;
3854 next();
3855 expr_cmp();
3856 gen_op(t);
3860 static void expr_and(void)
3862 expr_cmpeq();
3863 while (tok == '&') {
3864 next();
3865 expr_cmpeq();
3866 gen_op('&');
3870 static void expr_xor(void)
3872 expr_and();
3873 while (tok == '^') {
3874 next();
3875 expr_and();
3876 gen_op('^');
3880 static void expr_or(void)
3882 expr_xor();
3883 while (tok == '|') {
3884 next();
3885 expr_xor();
3886 gen_op('|');
3890 /* XXX: fix this mess */
3891 static void expr_land_const(void)
3893 expr_or();
3894 while (tok == TOK_LAND) {
3895 next();
3896 expr_or();
3897 gen_op(TOK_LAND);
3901 /* XXX: fix this mess */
3902 static void expr_lor_const(void)
3904 expr_land_const();
3905 while (tok == TOK_LOR) {
3906 next();
3907 expr_land_const();
3908 gen_op(TOK_LOR);
3912 /* only used if non constant */
3913 static void expr_land(void)
3915 int t;
3917 expr_or();
3918 if (tok == TOK_LAND) {
3919 t = 0;
3920 save_regs(1);
3921 for(;;) {
3922 t = gtst(1, t);
3923 if (tok != TOK_LAND) {
3924 vseti(VT_JMPI, t);
3925 break;
3927 next();
3928 expr_or();
3933 static void expr_lor(void)
3935 int t;
3937 expr_land();
3938 if (tok == TOK_LOR) {
3939 t = 0;
3940 save_regs(1);
3941 for(;;) {
3942 t = gtst(0, t);
3943 if (tok != TOK_LOR) {
3944 vseti(VT_JMP, t);
3945 break;
3947 next();
3948 expr_land();
3953 /* XXX: better constant handling */
3954 static void expr_cond(void)
3956 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3957 SValue sv;
3958 CType type, type1, type2;
3960 if (const_wanted) {
3961 expr_lor_const();
3962 if (tok == '?') {
3963 CType boolean;
3964 int c;
3965 boolean.t = VT_BOOL;
3966 vdup();
3967 gen_cast(&boolean);
3968 c = vtop->c.i;
3969 vpop();
3970 next();
3971 if (tok != ':' || !gnu_ext) {
3972 vpop();
3973 gexpr();
3975 if (!c)
3976 vpop();
3977 skip(':');
3978 expr_cond();
3979 if (c)
3980 vpop();
3982 } else {
3983 expr_lor();
3984 if (tok == '?') {
3985 next();
3986 if (vtop != vstack) {
3987 /* needed to avoid having different registers saved in
3988 each branch */
3989 if (is_float(vtop->type.t)) {
3990 rc = RC_FLOAT;
3991 #ifdef TCC_TARGET_X86_64
3992 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3993 rc = RC_ST0;
3995 #endif
3997 else
3998 rc = RC_INT;
3999 gv(rc);
4000 save_regs(1);
4002 if (tok == ':' && gnu_ext) {
4003 gv_dup();
4004 tt = gtst(1, 0);
4005 } else {
4006 tt = gtst(1, 0);
4007 gexpr();
4009 type1 = vtop->type;
4010 sv = *vtop; /* save value to handle it later */
4011 vtop--; /* no vpop so that FP stack is not flushed */
4012 skip(':');
4013 u = gjmp(0);
4014 gsym(tt);
4015 expr_cond();
4016 type2 = vtop->type;
4018 t1 = type1.t;
4019 bt1 = t1 & VT_BTYPE;
4020 t2 = type2.t;
4021 bt2 = t2 & VT_BTYPE;
4022 /* cast operands to correct type according to ISOC rules */
4023 if (is_float(bt1) || is_float(bt2)) {
4024 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4025 type.t = VT_LDOUBLE;
4026 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4027 type.t = VT_DOUBLE;
4028 } else {
4029 type.t = VT_FLOAT;
4031 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4032 /* cast to biggest op */
4033 type.t = VT_LLONG;
4034 /* convert to unsigned if it does not fit in a long long */
4035 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4036 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4037 type.t |= VT_UNSIGNED;
4038 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4039 /* XXX: test pointer compatibility */
4040 type = type1;
4041 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4042 /* XXX: test function pointer compatibility */
4043 type = type1;
4044 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4045 /* XXX: test structure compatibility */
4046 type = type1;
4047 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4048 /* NOTE: as an extension, we accept void on only one side */
4049 type.t = VT_VOID;
4050 } else {
4051 /* integer operations */
4052 type.t = VT_INT;
4053 /* convert to unsigned if it does not fit in an integer */
4054 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4055 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4056 type.t |= VT_UNSIGNED;
4059 /* now we convert second operand */
4060 gen_cast(&type);
4061 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4062 gaddrof();
4063 rc = RC_INT;
4064 if (is_float(type.t)) {
4065 rc = RC_FLOAT;
4066 #ifdef TCC_TARGET_X86_64
4067 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4068 rc = RC_ST0;
4070 #endif
4071 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4072 /* for long longs, we use fixed registers to avoid having
4073 to handle a complicated move */
4074 rc = RC_IRET;
4077 r2 = gv(rc);
4078 /* this is horrible, but we must also convert first
4079 operand */
4080 tt = gjmp(0);
4081 gsym(u);
4082 /* put again first value and cast it */
4083 *vtop = sv;
4084 gen_cast(&type);
4085 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4086 gaddrof();
4087 r1 = gv(rc);
4088 move_reg(r2, r1);
4089 vtop->r = r2;
4090 gsym(tt);
4095 static void expr_eq(void)
4097 int t;
4099 expr_cond();
4100 if (tok == '=' ||
4101 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4102 tok == TOK_A_XOR || tok == TOK_A_OR ||
4103 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4104 test_lvalue();
4105 t = tok;
4106 next();
4107 if (t == '=') {
4108 expr_eq();
4109 } else {
4110 vdup();
4111 expr_eq();
4112 gen_op(t & 0x7f);
4114 vstore();
4118 ST_FUNC void gexpr(void)
4120 while (1) {
4121 expr_eq();
4122 if (tok != ',')
4123 break;
4124 vpop();
4125 next();
4129 /* parse an expression and return its type without any side effect. */
4130 static void expr_type(CType *type)
4132 int saved_nocode_wanted;
4134 saved_nocode_wanted = nocode_wanted;
4135 nocode_wanted = 1;
4136 gexpr();
4137 *type = vtop->type;
4138 vpop();
4139 nocode_wanted = saved_nocode_wanted;
4142 /* parse a unary expression and return its type without any side
4143 effect. */
4144 static void unary_type(CType *type)
4146 int a;
4148 a = nocode_wanted;
4149 nocode_wanted = 1;
4150 unary();
4151 *type = vtop->type;
4152 vpop();
4153 nocode_wanted = a;
4156 /* parse a constant expression and return value in vtop. */
4157 static void expr_const1(void)
4159 int a;
4160 a = const_wanted;
4161 const_wanted = 1;
4162 expr_cond();
4163 const_wanted = a;
4166 /* parse an integer constant and return its value. */
4167 ST_FUNC int expr_const(void)
4169 int c;
4170 expr_const1();
4171 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4172 expect("constant expression");
4173 c = vtop->c.i;
4174 vpop();
4175 return c;
4178 /* return the label token if current token is a label, otherwise
4179 return zero */
4180 static int is_label(void)
4182 int last_tok;
4184 /* fast test first */
4185 if (tok < TOK_UIDENT)
4186 return 0;
4187 /* no need to save tokc because tok is an identifier */
4188 last_tok = tok;
4189 next();
4190 if (tok == ':') {
4191 next();
4192 return last_tok;
4193 } else {
4194 unget_tok(last_tok);
4195 return 0;
4199 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4200 int case_reg, int is_expr)
4202 int a, b, c, d;
4203 Sym *s;
4205 /* generate line number info */
4206 if (tcc_state->do_debug &&
4207 (last_line_num != file->line_num || last_ind != ind)) {
4208 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4209 last_ind = ind;
4210 last_line_num = file->line_num;
4213 if (is_expr) {
4214 /* default return value is (void) */
4215 vpushi(0);
4216 vtop->type.t = VT_VOID;
4219 if (tok == TOK_IF) {
4220 /* if test */
4221 next();
4222 skip('(');
4223 gexpr();
4224 skip(')');
4225 a = gtst(1, 0);
4226 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4227 c = tok;
4228 if (c == TOK_ELSE) {
4229 next();
4230 d = gjmp(0);
4231 gsym(a);
4232 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4233 gsym(d); /* patch else jmp */
4234 } else
4235 gsym(a);
4236 } else if (tok == TOK_WHILE) {
4237 next();
4238 d = ind;
4239 skip('(');
4240 gexpr();
4241 skip(')');
4242 a = gtst(1, 0);
4243 b = 0;
4244 block(&a, &b, case_sym, def_sym, case_reg, 0);
4245 gjmp_addr(d);
4246 gsym(a);
4247 gsym_addr(b, d);
4248 } else if (tok == '{') {
4249 Sym *llabel;
4251 next();
4252 /* record local declaration stack position */
4253 s = local_stack;
4254 llabel = local_label_stack;
4255 /* handle local labels declarations */
4256 if (tok == TOK_LABEL) {
4257 next();
4258 for(;;) {
4259 if (tok < TOK_UIDENT)
4260 expect("label identifier");
4261 label_push(&local_label_stack, tok, LABEL_DECLARED);
4262 next();
4263 if (tok == ',') {
4264 next();
4265 } else {
4266 skip(';');
4267 break;
4271 while (tok != '}') {
4272 decl(VT_LOCAL);
4273 if (tok != '}') {
4274 if (is_expr)
4275 vpop();
4276 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4279 /* pop locally defined labels */
4280 label_pop(&local_label_stack, llabel);
4281 /* pop locally defined symbols */
4282 if(is_expr) {
4283 /* XXX: this solution makes only valgrind happy...
4284 triggered by gcc.c-torture/execute/20000917-1.c */
4285 Sym *p;
4286 switch(vtop->type.t & VT_BTYPE) {
4287 case VT_PTR:
4288 case VT_STRUCT:
4289 case VT_ENUM:
4290 case VT_FUNC:
4291 for(p=vtop->type.ref;p;p=p->prev)
4292 if(p->prev==s)
4293 error("unsupported expression type");
4296 sym_pop(&local_stack, s);
4297 next();
4298 } else if (tok == TOK_RETURN) {
4299 next();
4300 if (tok != ';') {
4301 gexpr();
4302 gen_assign_cast(&func_vt);
4303 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4304 CType type;
4305 /* if returning structure, must copy it to implicit
4306 first pointer arg location */
4307 #ifdef TCC_ARM_EABI
4308 int align, size;
4309 size = type_size(&func_vt,&align);
4310 if(size <= 4)
4312 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4313 && (align & 3))
4315 int addr;
4316 loc = (loc - size) & -4;
4317 addr = loc;
4318 type = func_vt;
4319 vset(&type, VT_LOCAL | VT_LVAL, addr);
4320 vswap();
4321 vstore();
4322 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4324 vtop->type = int_type;
4325 gv(RC_IRET);
4326 } else {
4327 #endif
4328 type = func_vt;
4329 mk_pointer(&type);
4330 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4331 indir();
4332 vswap();
4333 /* copy structure value to pointer */
4334 vstore();
4335 #ifdef TCC_ARM_EABI
4337 #endif
4338 } else if (is_float(func_vt.t)) {
4339 gv(rc_fret(func_vt.t));
4340 } else {
4341 gv(RC_IRET);
4343 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4345 skip(';');
4346 rsym = gjmp(rsym); /* jmp */
4347 } else if (tok == TOK_BREAK) {
4348 /* compute jump */
4349 if (!bsym)
4350 error("cannot break");
4351 *bsym = gjmp(*bsym);
4352 next();
4353 skip(';');
4354 } else if (tok == TOK_CONTINUE) {
4355 /* compute jump */
4356 if (!csym)
4357 error("cannot continue");
4358 *csym = gjmp(*csym);
4359 next();
4360 skip(';');
4361 } else if (tok == TOK_FOR) {
4362 int e;
4363 next();
4364 skip('(');
4365 if (tok != ';') {
4366 gexpr();
4367 vpop();
4369 skip(';');
4370 d = ind;
4371 c = ind;
4372 a = 0;
4373 b = 0;
4374 if (tok != ';') {
4375 gexpr();
4376 a = gtst(1, 0);
4378 skip(';');
4379 if (tok != ')') {
4380 e = gjmp(0);
4381 c = ind;
4382 gexpr();
4383 vpop();
4384 gjmp_addr(d);
4385 gsym(e);
4387 skip(')');
4388 block(&a, &b, case_sym, def_sym, case_reg, 0);
4389 gjmp_addr(c);
4390 gsym(a);
4391 gsym_addr(b, c);
4392 } else
4393 if (tok == TOK_DO) {
4394 next();
4395 a = 0;
4396 b = 0;
4397 d = ind;
4398 block(&a, &b, case_sym, def_sym, case_reg, 0);
4399 skip(TOK_WHILE);
4400 skip('(');
4401 gsym(b);
4402 gexpr();
4403 c = gtst(0, 0);
4404 gsym_addr(c, d);
4405 skip(')');
4406 gsym(a);
4407 skip(';');
4408 } else
4409 if (tok == TOK_SWITCH) {
4410 next();
4411 skip('(');
4412 gexpr();
4413 /* XXX: other types than integer */
4414 case_reg = gv(RC_INT);
4415 vpop();
4416 skip(')');
4417 a = 0;
4418 b = gjmp(0); /* jump to first case */
4419 c = 0;
4420 block(&a, csym, &b, &c, case_reg, 0);
4421 /* if no default, jmp after switch */
4422 if (c == 0)
4423 c = ind;
4424 /* default label */
4425 gsym_addr(b, c);
4426 /* break label */
4427 gsym(a);
4428 } else
4429 if (tok == TOK_CASE) {
4430 int v1, v2;
4431 if (!case_sym)
4432 expect("switch");
4433 next();
4434 v1 = expr_const();
4435 v2 = v1;
4436 if (gnu_ext && tok == TOK_DOTS) {
4437 next();
4438 v2 = expr_const();
4439 if (v2 < v1)
4440 warning("empty case range");
4442 /* since a case is like a label, we must skip it with a jmp */
4443 b = gjmp(0);
4444 gsym(*case_sym);
4445 vseti(case_reg, 0);
4446 vpushi(v1);
4447 if (v1 == v2) {
4448 gen_op(TOK_EQ);
4449 *case_sym = gtst(1, 0);
4450 } else {
4451 gen_op(TOK_GE);
4452 *case_sym = gtst(1, 0);
4453 vseti(case_reg, 0);
4454 vpushi(v2);
4455 gen_op(TOK_LE);
4456 *case_sym = gtst(1, *case_sym);
4458 gsym(b);
4459 skip(':');
4460 is_expr = 0;
4461 goto block_after_label;
4462 } else
4463 if (tok == TOK_DEFAULT) {
4464 next();
4465 skip(':');
4466 if (!def_sym)
4467 expect("switch");
4468 if (*def_sym)
4469 error("too many 'default'");
4470 *def_sym = ind;
4471 is_expr = 0;
4472 goto block_after_label;
4473 } else
4474 if (tok == TOK_GOTO) {
4475 next();
4476 if (tok == '*' && gnu_ext) {
4477 /* computed goto */
4478 next();
4479 gexpr();
4480 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4481 expect("pointer");
4482 ggoto();
4483 } else if (tok >= TOK_UIDENT) {
4484 s = label_find(tok);
4485 /* put forward definition if needed */
4486 if (!s) {
4487 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4488 } else {
4489 if (s->r == LABEL_DECLARED)
4490 s->r = LABEL_FORWARD;
4492 /* label already defined */
4493 if (s->r & LABEL_FORWARD)
4494 s->jnext = gjmp(s->jnext);
4495 else
4496 gjmp_addr(s->jnext);
4497 next();
4498 } else {
4499 expect("label identifier");
4501 skip(';');
4502 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4503 asm_instr();
4504 } else {
4505 b = is_label();
4506 if (b) {
4507 /* label case */
4508 s = label_find(b);
4509 if (s) {
4510 if (s->r == LABEL_DEFINED)
4511 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4512 gsym(s->jnext);
4513 s->r = LABEL_DEFINED;
4514 } else {
4515 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4517 s->jnext = ind;
4518 /* we accept this, but it is a mistake */
4519 block_after_label:
4520 if (tok == '}') {
4521 warning("deprecated use of label at end of compound statement");
4522 } else {
4523 if (is_expr)
4524 vpop();
4525 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4527 } else {
4528 /* expression case */
4529 if (tok != ';') {
4530 if (is_expr) {
4531 vpop();
4532 gexpr();
4533 } else {
4534 gexpr();
4535 vpop();
4538 skip(';');
4543 /* t is the array or struct type. c is the array or struct
4544 address. cur_index/cur_field is the pointer to the current
4545 value. 'size_only' is true if only size info is needed (only used
4546 in arrays) */
4547 static void decl_designator(CType *type, Section *sec, unsigned long c,
4548 int *cur_index, Sym **cur_field,
4549 int size_only)
4551 Sym *s, *f;
4552 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4553 CType type1;
4555 notfirst = 0;
4556 elem_size = 0;
4557 nb_elems = 1;
4558 if (gnu_ext && (l = is_label()) != 0)
4559 goto struct_field;
4560 while (tok == '[' || tok == '.') {
4561 if (tok == '[') {
4562 if (!(type->t & VT_ARRAY))
4563 expect("array type");
4564 s = type->ref;
4565 next();
4566 index = expr_const();
4567 if (index < 0 || (s->c >= 0 && index >= s->c))
4568 expect("invalid index");
4569 if (tok == TOK_DOTS && gnu_ext) {
4570 next();
4571 index_last = expr_const();
4572 if (index_last < 0 ||
4573 (s->c >= 0 && index_last >= s->c) ||
4574 index_last < index)
4575 expect("invalid index");
4576 } else {
4577 index_last = index;
4579 skip(']');
4580 if (!notfirst)
4581 *cur_index = index_last;
4582 type = pointed_type(type);
4583 elem_size = type_size(type, &align);
4584 c += index * elem_size;
4585 /* NOTE: we only support ranges for last designator */
4586 nb_elems = index_last - index + 1;
4587 if (nb_elems != 1) {
4588 notfirst = 1;
4589 break;
4591 } else {
4592 next();
4593 l = tok;
4594 next();
4595 struct_field:
4596 if ((type->t & VT_BTYPE) != VT_STRUCT)
4597 expect("struct/union type");
4598 s = type->ref;
4599 l |= SYM_FIELD;
4600 f = s->next;
4601 while (f) {
4602 if (f->v == l)
4603 break;
4604 f = f->next;
4606 if (!f)
4607 expect("field");
4608 if (!notfirst)
4609 *cur_field = f;
4610 /* XXX: fix this mess by using explicit storage field */
4611 type1 = f->type;
4612 type1.t |= (type->t & ~VT_TYPE);
4613 type = &type1;
4614 c += f->c;
4616 notfirst = 1;
4618 if (notfirst) {
4619 if (tok == '=') {
4620 next();
4621 } else {
4622 if (!gnu_ext)
4623 expect("=");
4625 } else {
4626 if (type->t & VT_ARRAY) {
4627 index = *cur_index;
4628 type = pointed_type(type);
4629 c += index * type_size(type, &align);
4630 } else {
4631 f = *cur_field;
4632 if (!f)
4633 error("too many field init");
4634 /* XXX: fix this mess by using explicit storage field */
4635 type1 = f->type;
4636 type1.t |= (type->t & ~VT_TYPE);
4637 type = &type1;
4638 c += f->c;
4641 decl_initializer(type, sec, c, 0, size_only);
4643 /* XXX: make it more general */
4644 if (!size_only && nb_elems > 1) {
4645 unsigned long c_end;
4646 uint8_t *src, *dst;
4647 int i;
4649 if (!sec)
4650 error("range init not supported yet for dynamic storage");
4651 c_end = c + nb_elems * elem_size;
4652 if (c_end > sec->data_allocated)
4653 section_realloc(sec, c_end);
4654 src = sec->data + c;
4655 dst = src;
4656 for(i = 1; i < nb_elems; i++) {
4657 dst += elem_size;
4658 memcpy(dst, src, elem_size);
4663 #define EXPR_VAL 0
4664 #define EXPR_CONST 1
4665 #define EXPR_ANY 2
4667 /* store a value or an expression directly in global data or in local array */
4668 static void init_putv(CType *type, Section *sec, unsigned long c,
4669 int v, int expr_type)
4671 int saved_global_expr, bt, bit_pos, bit_size;
4672 void *ptr;
4673 unsigned long long bit_mask;
4674 CType dtype;
4676 switch(expr_type) {
4677 case EXPR_VAL:
4678 vpushi(v);
4679 break;
4680 case EXPR_CONST:
4681 /* compound literals must be allocated globally in this case */
4682 saved_global_expr = global_expr;
4683 global_expr = 1;
4684 expr_const1();
4685 global_expr = saved_global_expr;
4686 /* NOTE: symbols are accepted */
4687 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4688 error("initializer element is not constant");
4689 break;
4690 case EXPR_ANY:
4691 expr_eq();
4692 break;
4695 dtype = *type;
4696 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4698 if (sec) {
4699 /* XXX: not portable */
4700 /* XXX: generate error if incorrect relocation */
4701 gen_assign_cast(&dtype);
4702 bt = type->t & VT_BTYPE;
4703 /* we'll write at most 12 bytes */
4704 if (c + 12 > sec->data_allocated) {
4705 section_realloc(sec, c + 12);
4707 ptr = sec->data + c;
4708 /* XXX: make code faster ? */
4709 if (!(type->t & VT_BITFIELD)) {
4710 bit_pos = 0;
4711 bit_size = 32;
4712 bit_mask = -1LL;
4713 } else {
4714 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4715 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4716 bit_mask = (1LL << bit_size) - 1;
4718 if ((vtop->r & VT_SYM) &&
4719 (bt == VT_BYTE ||
4720 bt == VT_SHORT ||
4721 bt == VT_DOUBLE ||
4722 bt == VT_LDOUBLE ||
4723 bt == VT_LLONG ||
4724 (bt == VT_INT && bit_size != 32)))
4725 error("initializer element is not computable at load time");
4726 switch(bt) {
4727 case VT_BOOL:
4728 vtop->c.i = (vtop->c.i != 0);
4729 case VT_BYTE:
4730 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4731 break;
4732 case VT_SHORT:
4733 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4734 break;
4735 case VT_DOUBLE:
4736 *(double *)ptr = vtop->c.d;
4737 break;
4738 case VT_LDOUBLE:
4739 *(long double *)ptr = vtop->c.ld;
4740 break;
4741 case VT_LLONG:
4742 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4743 break;
4744 default:
4745 if (vtop->r & VT_SYM) {
4746 greloc(sec, vtop->sym, c, R_DATA_PTR);
4748 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4749 break;
4751 vtop--;
4752 } else {
4753 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4754 vswap();
4755 vstore();
4756 vpop();
4760 /* put zeros for variable based init */
4761 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4763 if (sec) {
4764 /* nothing to do because globals are already set to zero */
4765 } else {
4766 vpush_global_sym(&func_old_type, TOK_memset);
4767 vseti(VT_LOCAL, c);
4768 vpushi(0);
4769 vpushi(size);
4770 gfunc_call(3);
4774 /* 't' contains the type and storage info. 'c' is the offset of the
4775 object in section 'sec'. If 'sec' is NULL, it means stack based
4776 allocation. 'first' is true if array '{' must be read (multi
4777 dimension implicit array init handling). 'size_only' is true if
4778 size only evaluation is wanted (only for arrays). */
4779 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4780 int first, int size_only)
4782 int index, array_length, n, no_oblock, nb, parlevel, parlevel1, i;
4783 int size1, align1, expr_type;
4784 Sym *s, *f;
4785 CType *t1;
4787 if (type->t & VT_ARRAY) {
4788 s = type->ref;
4789 n = s->c;
4790 array_length = 0;
4791 t1 = pointed_type(type);
4792 size1 = type_size(t1, &align1);
4794 no_oblock = 1;
4795 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4796 tok == '{') {
4797 if (tok != '{')
4798 error("character array initializer must be a literal,"
4799 " optionally enclosed in braces");
4800 skip('{');
4801 no_oblock = 0;
4804 /* only parse strings here if correct type (otherwise: handle
4805 them as ((w)char *) expressions */
4806 if ((tok == TOK_LSTR &&
4807 #ifdef TCC_TARGET_PE
4808 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4809 #else
4810 (t1->t & VT_BTYPE) == VT_INT
4811 #endif
4812 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4813 while (tok == TOK_STR || tok == TOK_LSTR) {
4814 int cstr_len, ch;
4815 CString *cstr;
4817 cstr = tokc.cstr;
4818 /* compute maximum number of chars wanted */
4819 if (tok == TOK_STR)
4820 cstr_len = cstr->size;
4821 else
4822 cstr_len = cstr->size / sizeof(nwchar_t);
4823 cstr_len--;
4824 nb = cstr_len;
4825 if (n >= 0 && nb > (n - array_length))
4826 nb = n - array_length;
4827 if (!size_only) {
4828 if (cstr_len > nb)
4829 warning("initializer-string for array is too long");
4830 /* in order to go faster for common case (char
4831 string in global variable, we handle it
4832 specifically */
4833 if (sec && tok == TOK_STR && size1 == 1) {
4834 memcpy(sec->data + c + array_length, cstr->data, nb);
4835 } else {
4836 for(i=0;i<nb;i++) {
4837 if (tok == TOK_STR)
4838 ch = ((unsigned char *)cstr->data)[i];
4839 else
4840 ch = ((nwchar_t *)cstr->data)[i];
4841 init_putv(t1, sec, c + (array_length + i) * size1,
4842 ch, EXPR_VAL);
4846 array_length += nb;
4847 next();
4849 /* only add trailing zero if enough storage (no
4850 warning in this case since it is standard) */
4851 if (n < 0 || array_length < n) {
4852 if (!size_only) {
4853 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4855 array_length++;
4857 } else {
4858 index = 0;
4859 if (ARRAY_RESIZE(s->r))
4860 n = -1;
4861 while (tok != '}') {
4862 decl_designator(type, sec, c, &index, NULL, size_only);
4863 if (n >= 0 && index >= n)
4864 error("index too large");
4865 /* must put zero in holes (note that doing it that way
4866 ensures that it even works with designators) */
4867 if (!size_only && array_length < index) {
4868 init_putz(t1, sec, c + array_length * size1,
4869 (index - array_length) * size1);
4871 index++;
4872 if (index > array_length)
4873 array_length = index;
4874 /* special test for multi dimensional arrays (may not
4875 be strictly correct if designators are used at the
4876 same time) */
4877 if (index >= n && no_oblock)
4878 break;
4879 if (tok == '}')
4880 break;
4881 skip(',');
4884 if (!no_oblock)
4885 skip('}');
4886 /* put zeros at the end */
4887 if (!size_only && n >= 0 && array_length < n) {
4888 init_putz(t1, sec, c + array_length * size1,
4889 (n - array_length) * size1);
4891 /* patch type size if needed */
4892 if (n < 0)
4893 s->c = array_length;
4894 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4895 (sec || !first || tok == '{')) {
4896 int par_count;
4898 /* NOTE: the previous test is a specific case for automatic
4899 struct/union init */
4900 /* XXX: union needs only one init */
4902 /* XXX: this test is incorrect for local initializers
4903 beginning with ( without {. It would be much more difficult
4904 to do it correctly (ideally, the expression parser should
4905 be used in all cases) */
4906 par_count = 0;
4907 if (tok == '(') {
4908 AttributeDef ad1;
4909 CType type1;
4910 next();
4911 while (tok == '(') {
4912 par_count++;
4913 next();
4915 if (!parse_btype(&type1, &ad1))
4916 expect("cast");
4917 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4918 #if 0
4919 if (!is_assignable_types(type, &type1))
4920 error("invalid type for cast");
4921 #endif
4922 skip(')');
4924 no_oblock = 1;
4925 if (first || tok == '{') {
4926 skip('{');
4927 no_oblock = 0;
4929 s = type->ref;
4930 f = s->next;
4931 array_length = 0;
4932 index = 0;
4933 n = s->c;
4934 if (s->r & (1<<31))
4935 n = -1;
4936 while (tok != '}') {
4937 decl_designator(type, sec, c, NULL, &f, size_only);
4938 index = f->c;
4939 if (!size_only && array_length < index) {
4940 init_putz(type, sec, c + array_length,
4941 index - array_length);
4943 index = index + type_size(&f->type, &align1);
4944 if (index > array_length)
4945 array_length = index;
4947 /* gr: skip fields from same union - ugly. */
4948 while (f->next) {
4949 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4950 /* test for same offset */
4951 if (f->next->c != f->c)
4952 break;
4953 /* if yes, test for bitfield shift */
4954 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4955 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4956 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4957 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4958 if (bit_pos_1 != bit_pos_2)
4959 break;
4961 f = f->next;
4964 f = f->next;
4965 if (no_oblock && f == NULL)
4966 break;
4967 if (tok == '}')
4968 break;
4969 skip(',');
4971 /* put zeros at the end */
4972 if (!size_only && n >= 0 && array_length < n) {
4973 init_putz(type, sec, c + array_length,
4974 n - array_length);
4976 if (!no_oblock)
4977 skip('}');
4978 while (par_count) {
4979 skip(')');
4980 par_count--;
4982 if (n < 0)
4983 s->c = array_length;
4984 } else if (tok == '{') {
4985 next();
4986 decl_initializer(type, sec, c, first, size_only);
4987 skip('}');
4988 } else if (size_only) {
4989 /* just skip expression */
4990 parlevel = parlevel1 = 0;
4991 while ((parlevel > 0 || parlevel1 > 0 ||
4992 (tok != '}' && tok != ',')) && tok != -1) {
4993 if (tok == '(')
4994 parlevel++;
4995 else if (tok == ')')
4996 parlevel--;
4997 else if (tok == '{')
4998 parlevel1++;
4999 else if (tok == '}')
5000 parlevel1--;
5001 next();
5003 } else {
5004 /* currently, we always use constant expression for globals
5005 (may change for scripting case) */
5006 expr_type = EXPR_CONST;
5007 if (!sec)
5008 expr_type = EXPR_ANY;
5009 init_putv(type, sec, c, 0, expr_type);
5013 /* parse an initializer for type 't' if 'has_init' is non zero, and
5014 allocate space in local or global data space ('r' is either
5015 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5016 variable 'v' with an associated name represented by 'asm_label' of
5017 scope 'scope' is declared before initializers are parsed. If 'v' is
5018 zero, then a reference to the new object is put in the value stack.
5019 If 'has_init' is 2, a special parsing is done to handle string
5020 constants. */
5021 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5022 int has_init, int v, char *asm_label,
5023 int scope)
5025 int size, align, addr, data_offset;
5026 int level;
5027 ParseState saved_parse_state = {0};
5028 TokenString init_str;
5029 Section *sec;
5031 /* resize the struct */
5032 if ((type->t & VT_BTYPE) == VT_STRUCT && (type->ref->r & (1<<31)) != 0)
5033 type->ref->c = -1;
5034 size = type_size(type, &align);
5035 /* If unknown size, we must evaluate it before
5036 evaluating initializers because
5037 initializers can generate global data too
5038 (e.g. string pointers or ISOC99 compound
5039 literals). It also simplifies local
5040 initializers handling */
5041 tok_str_new(&init_str);
5042 if (size < 0) {
5043 if (!has_init)
5044 error("unknown type size");
5045 /* get all init string */
5046 if (has_init == 2) {
5047 /* only get strings */
5048 while (tok == TOK_STR || tok == TOK_LSTR) {
5049 tok_str_add_tok(&init_str);
5050 next();
5052 } else {
5053 level = 0;
5054 while (level > 0 || (tok != ',' && tok != ';')) {
5055 if (tok < 0)
5056 error("unexpected end of file in initializer");
5057 tok_str_add_tok(&init_str);
5058 if (tok == '{')
5059 level++;
5060 else if (tok == '}') {
5061 level--;
5062 if (level <= 0) {
5063 next();
5064 break;
5067 next();
5070 tok_str_add(&init_str, -1);
5071 tok_str_add(&init_str, 0);
5073 /* compute size */
5074 save_parse_state(&saved_parse_state);
5076 macro_ptr = init_str.str;
5077 next();
5078 decl_initializer(type, NULL, 0, 1, 1);
5079 /* prepare second initializer parsing */
5080 macro_ptr = init_str.str;
5081 next();
5083 /* if still unknown size, error */
5084 size = type_size(type, &align);
5085 if (size < 0)
5086 error("unknown type size");
5088 /* take into account specified alignment if bigger */
5089 if (ad->aligned) {
5090 if (ad->aligned > align)
5091 align = ad->aligned;
5092 } else if (ad->packed) {
5093 align = 1;
5095 if ((r & VT_VALMASK) == VT_LOCAL) {
5096 sec = NULL;
5097 #ifdef CONFIG_TCC_BCHECK
5098 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
5099 loc--;
5100 #endif
5101 loc = (loc - size) & -align;
5102 addr = loc;
5103 #ifdef CONFIG_TCC_BCHECK
5104 /* handles bounds */
5105 /* XXX: currently, since we do only one pass, we cannot track
5106 '&' operators, so we add only arrays */
5107 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5108 unsigned long *bounds_ptr;
5109 /* add padding between regions */
5110 loc--;
5111 /* then add local bound info */
5112 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5113 bounds_ptr[0] = addr;
5114 bounds_ptr[1] = size;
5116 #endif
5117 if (v) {
5118 /* local variable */
5119 sym_push(v, type, r, addr);
5120 } else {
5121 /* push local reference */
5122 vset(type, r, addr);
5124 } else {
5125 Sym *sym;
5127 sym = NULL;
5128 if (v && scope == VT_CONST) {
5129 /* see if the symbol was already defined */
5130 sym = sym_find(v);
5131 if (sym) {
5132 if (!is_compatible_types(&sym->type, type))
5133 error("incompatible types for redefinition of '%s'",
5134 get_tok_str(v, NULL));
5135 if (sym->type.t & VT_EXTERN) {
5136 /* if the variable is extern, it was not allocated */
5137 sym->type.t &= ~VT_EXTERN;
5138 /* set array size if it was ommited in extern
5139 declaration */
5140 if ((sym->type.t & VT_ARRAY) &&
5141 sym->type.ref->c < 0 &&
5142 type->ref->c >= 0)
5143 sym->type.ref->c = type->ref->c;
5144 } else {
5145 /* we accept several definitions of the same
5146 global variable. this is tricky, because we
5147 must play with the SHN_COMMON type of the symbol */
5148 /* XXX: should check if the variable was already
5149 initialized. It is incorrect to initialized it
5150 twice */
5151 /* no init data, we won't add more to the symbol */
5152 if (!has_init)
5153 goto no_alloc;
5158 /* allocate symbol in corresponding section */
5159 sec = ad->section;
5160 if (!sec) {
5161 if (has_init)
5162 sec = data_section;
5163 else if (tcc_state->nocommon)
5164 sec = bss_section;
5166 if (sec) {
5167 data_offset = sec->data_offset;
5168 data_offset = (data_offset + align - 1) & -align;
5169 addr = data_offset;
5170 /* very important to increment global pointer at this time
5171 because initializers themselves can create new initializers */
5172 data_offset += size;
5173 #ifdef CONFIG_TCC_BCHECK
5174 /* add padding if bound check */
5175 if (tcc_state->do_bounds_check)
5176 data_offset++;
5177 #endif
5178 sec->data_offset = data_offset;
5179 /* allocate section space to put the data */
5180 if (sec->sh_type != SHT_NOBITS &&
5181 data_offset > sec->data_allocated)
5182 section_realloc(sec, data_offset);
5183 /* align section if needed */
5184 if (align > sec->sh_addralign)
5185 sec->sh_addralign = align;
5186 } else {
5187 addr = 0; /* avoid warning */
5190 if (v) {
5191 if (scope != VT_CONST || !sym) {
5192 sym = sym_push(v, type, r | VT_SYM, 0);
5193 sym->asm_label = asm_label;
5195 /* update symbol definition */
5196 if (sec) {
5197 put_extern_sym(sym, sec, addr, size);
5198 } else {
5199 ElfW(Sym) *esym;
5200 /* put a common area */
5201 put_extern_sym(sym, NULL, align, size);
5202 /* XXX: find a nicer way */
5203 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5204 esym->st_shndx = SHN_COMMON;
5206 } else {
5207 CValue cval;
5209 /* push global reference */
5210 sym = get_sym_ref(type, sec, addr, size);
5211 cval.ul = 0;
5212 vsetc(type, VT_CONST | VT_SYM, &cval);
5213 vtop->sym = sym;
5215 /* patch symbol weakness */
5216 if (type->t & VT_WEAK) {
5217 unsigned char *st_info =
5218 &((ElfW(Sym) *)symtab_section->data)[sym->c].st_info;
5219 *st_info = ELF32_ST_INFO(STB_WEAK, ELF32_ST_TYPE(*st_info));
5221 #ifdef CONFIG_TCC_BCHECK
5222 /* handles bounds now because the symbol must be defined
5223 before for the relocation */
5224 if (tcc_state->do_bounds_check) {
5225 unsigned long *bounds_ptr;
5227 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5228 /* then add global bound info */
5229 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5230 bounds_ptr[0] = 0; /* relocated */
5231 bounds_ptr[1] = size;
5233 #endif
5235 if (has_init) {
5236 decl_initializer(type, sec, addr, 1, 0);
5237 /* restore parse state if needed */
5238 if (init_str.str) {
5239 tok_str_free(init_str.str);
5240 restore_parse_state(&saved_parse_state);
5243 no_alloc: ;
5246 static void put_func_debug(Sym *sym)
5248 char buf[512];
5250 /* stabs info */
5251 /* XXX: we put here a dummy type */
5252 snprintf(buf, sizeof(buf), "%s:%c1",
5253 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5254 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5255 cur_text_section, sym->c);
5256 /* //gr gdb wants a line at the function */
5257 put_stabn(N_SLINE, 0, file->line_num, 0);
5258 last_ind = 0;
5259 last_line_num = 0;
5262 /* parse an old style function declaration list */
5263 /* XXX: check multiple parameter */
5264 static void func_decl_list(Sym *func_sym)
5266 AttributeDef ad;
5267 int v;
5268 Sym *s;
5269 CType btype, type;
5271 /* parse each declaration */
5272 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF &&
5273 tok != TOK_ASM1 && tok != TOK_ASM2 && tok != TOK_ASM3) {
5274 if (!parse_btype(&btype, &ad))
5275 expect("declaration list");
5276 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5277 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5278 tok == ';') {
5279 /* we accept no variable after */
5280 } else {
5281 for(;;) {
5282 type = btype;
5283 type_decl(&type, &ad, &v, TYPE_DIRECT);
5284 /* find parameter in function parameter list */
5285 s = func_sym->next;
5286 while (s != NULL) {
5287 if ((s->v & ~SYM_FIELD) == v)
5288 goto found;
5289 s = s->next;
5291 error("declaration for parameter '%s' but no such parameter",
5292 get_tok_str(v, NULL));
5293 found:
5294 /* check that no storage specifier except 'register' was given */
5295 if (type.t & VT_STORAGE)
5296 error("storage class specified for '%s'", get_tok_str(v, NULL));
5297 convert_parameter_type(&type);
5298 /* we can add the type (NOTE: it could be local to the function) */
5299 s->type = type;
5300 /* accept other parameters */
5301 if (tok == ',')
5302 next();
5303 else
5304 break;
5307 skip(';');
5311 /* parse a function defined by symbol 'sym' and generate its code in
5312 'cur_text_section' */
5313 static void gen_function(Sym *sym)
5315 int saved_nocode_wanted = nocode_wanted;
5316 nocode_wanted = 0;
5317 ind = cur_text_section->data_offset;
5318 /* NOTE: we patch the symbol size later */
5319 put_extern_sym(sym, cur_text_section, ind, 0);
5320 funcname = get_tok_str(sym->v, NULL);
5321 func_ind = ind;
5322 /* put debug symbol */
5323 if (tcc_state->do_debug)
5324 put_func_debug(sym);
5325 /* push a dummy symbol to enable local sym storage */
5326 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5327 gfunc_prolog(&sym->type);
5328 rsym = 0;
5329 block(NULL, NULL, NULL, NULL, 0, 0);
5330 gsym(rsym);
5331 gfunc_epilog();
5332 cur_text_section->data_offset = ind;
5333 label_pop(&global_label_stack, NULL);
5334 sym_pop(&local_stack, NULL); /* reset local stack */
5335 /* end of function */
5336 /* patch symbol size */
5337 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5338 ind - func_ind;
5339 /* patch symbol weakness (this definition overrules any prototype) */
5340 if (sym->type.t & VT_WEAK) {
5341 unsigned char *st_info =
5342 &((ElfW(Sym) *)symtab_section->data)[sym->c].st_info;
5343 *st_info = ELF32_ST_INFO(STB_WEAK, ELF32_ST_TYPE(*st_info));
5345 if (tcc_state->do_debug) {
5346 put_stabn(N_FUN, 0, 0, ind - func_ind);
5348 /* It's better to crash than to generate wrong code */
5349 cur_text_section = NULL;
5350 funcname = ""; /* for safety */
5351 func_vt.t = VT_VOID; /* for safety */
5352 ind = 0; /* for safety */
5353 nocode_wanted = saved_nocode_wanted;
5356 ST_FUNC void gen_inline_functions(void)
5358 Sym *sym;
5359 int *str, inline_generated, i;
5360 struct InlineFunc *fn;
5362 /* iterate while inline function are referenced */
5363 for(;;) {
5364 inline_generated = 0;
5365 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5366 fn = tcc_state->inline_fns[i];
5367 sym = fn->sym;
5368 if (sym && sym->c) {
5369 /* the function was used: generate its code and
5370 convert it to a normal function */
5371 str = fn->token_str;
5372 fn->sym = NULL;
5373 if (file)
5374 strcpy(file->filename, fn->filename);
5375 sym->r = VT_SYM | VT_CONST;
5376 sym->type.t &= ~VT_INLINE;
5378 macro_ptr = str;
5379 next();
5380 cur_text_section = text_section;
5381 gen_function(sym);
5382 macro_ptr = NULL; /* fail safe */
5384 inline_generated = 1;
5387 if (!inline_generated)
5388 break;
5390 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5391 fn = tcc_state->inline_fns[i];
5392 str = fn->token_str;
5393 tok_str_free(str);
5395 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5398 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5399 ST_FUNC void decl(int l)
5401 int v, has_init, r;
5402 CType type, btype;
5403 Sym *sym;
5404 AttributeDef ad;
5406 while (1) {
5407 if (!parse_btype(&btype, &ad)) {
5408 /* skip redundant ';' */
5409 /* XXX: find more elegant solution */
5410 if (tok == ';') {
5411 next();
5412 continue;
5414 if (l == VT_CONST &&
5415 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5416 /* global asm block */
5417 asm_global_instr();
5418 continue;
5420 /* special test for old K&R protos without explicit int
5421 type. Only accepted when defining global data */
5422 if (l == VT_LOCAL || tok < TOK_DEFINE)
5423 break;
5424 btype.t = VT_INT;
5426 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5427 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5428 tok == ';') {
5429 /* we accept no variable after */
5430 next();
5431 continue;
5433 while (1) { /* iterate thru each declaration */
5434 char *asm_label; // associated asm label
5435 type = btype;
5436 type_decl(&type, &ad, &v, TYPE_DIRECT);
5437 #if 0
5439 char buf[500];
5440 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5441 printf("type = '%s'\n", buf);
5443 #endif
5444 if ((type.t & VT_BTYPE) == VT_FUNC) {
5445 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
5446 error("function without file scope cannot be static");
5448 /* if old style function prototype, we accept a
5449 declaration list */
5450 sym = type.ref;
5451 if (sym->c == FUNC_OLD)
5452 func_decl_list(sym);
5455 asm_label = NULL;
5456 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5457 CString astr;
5459 asm_label_instr(&astr);
5460 asm_label = tcc_strdup(astr.data);
5461 cstr_free(&astr);
5463 /* parse one last attribute list, after asm label */
5464 parse_attribute(&ad);
5467 if (ad.weak)
5468 type.t |= VT_WEAK;
5469 #ifdef TCC_TARGET_PE
5470 if (ad.func_import)
5471 type.t |= VT_IMPORT;
5472 if (ad.func_export)
5473 type.t |= VT_EXPORT;
5474 #endif
5475 if (tok == '{') {
5476 if (l == VT_LOCAL)
5477 error("cannot use local functions");
5478 if ((type.t & VT_BTYPE) != VT_FUNC)
5479 expect("function definition");
5481 /* reject abstract declarators in function definition */
5482 sym = type.ref;
5483 while ((sym = sym->next) != NULL)
5484 if (!(sym->v & ~SYM_FIELD))
5485 expect("identifier");
5487 /* XXX: cannot do better now: convert extern line to static inline */
5488 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5489 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5491 sym = sym_find(v);
5492 if (sym) {
5493 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5494 goto func_error1;
5496 r = sym->type.ref->r;
5497 /* use func_call from prototype if not defined */
5498 if (FUNC_CALL(r) != FUNC_CDECL
5499 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5500 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5502 /* use export from prototype */
5503 if (FUNC_EXPORT(r))
5504 FUNC_EXPORT(type.ref->r) = 1;
5506 /* use static from prototype */
5507 if (sym->type.t & VT_STATIC)
5508 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5510 if (!is_compatible_types(&sym->type, &type)) {
5511 func_error1:
5512 error("incompatible types for redefinition of '%s'",
5513 get_tok_str(v, NULL));
5515 /* if symbol is already defined, then put complete type */
5516 sym->type = type;
5517 } else {
5518 /* put function symbol */
5519 sym = global_identifier_push(v, type.t, 0);
5520 sym->type.ref = type.ref;
5523 /* static inline functions are just recorded as a kind
5524 of macro. Their code will be emitted at the end of
5525 the compilation unit only if they are used */
5526 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5527 (VT_INLINE | VT_STATIC)) {
5528 TokenString func_str;
5529 int block_level;
5530 struct InlineFunc *fn;
5531 const char *filename;
5533 tok_str_new(&func_str);
5535 block_level = 0;
5536 for(;;) {
5537 int t;
5538 if (tok == TOK_EOF)
5539 error("unexpected end of file");
5540 tok_str_add_tok(&func_str);
5541 t = tok;
5542 next();
5543 if (t == '{') {
5544 block_level++;
5545 } else if (t == '}') {
5546 block_level--;
5547 if (block_level == 0)
5548 break;
5551 tok_str_add(&func_str, -1);
5552 tok_str_add(&func_str, 0);
5553 filename = file ? file->filename : "";
5554 fn = tcc_malloc(sizeof *fn + strlen(filename));
5555 strcpy(fn->filename, filename);
5556 fn->sym = sym;
5557 fn->token_str = func_str.str;
5558 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5560 } else {
5561 /* compute text section */
5562 cur_text_section = ad.section;
5563 if (!cur_text_section)
5564 cur_text_section = text_section;
5565 sym->r = VT_SYM | VT_CONST;
5566 gen_function(sym);
5568 break;
5569 } else {
5570 if (btype.t & VT_TYPEDEF) {
5571 /* save typedefed type */
5572 /* XXX: test storage specifiers ? */
5573 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5574 sym->type.t |= VT_TYPEDEF;
5575 } else {
5576 r = 0;
5577 if ((type.t & VT_BTYPE) == VT_FUNC) {
5578 /* external function definition */
5579 /* specific case for func_call attribute */
5580 type.ref->r = INT_ATTR(&ad);
5581 } else if (!(type.t & VT_ARRAY)) {
5582 /* not lvalue if array */
5583 r |= lvalue_type(type.t);
5585 has_init = (tok == '=');
5586 if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) ||
5587 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5588 !has_init && l == VT_CONST && type.ref->c < 0)) {
5589 /* external variable or function */
5590 /* NOTE: as GCC, uninitialized global static
5591 arrays of null size are considered as
5592 extern */
5593 sym = external_sym(v, &type, r, asm_label);
5595 if (ad.alias_target) {
5596 Section tsec;
5597 Elf32_Sym *esym;
5598 Sym *alias_target;
5600 alias_target = sym_find(ad.alias_target);
5601 if (!alias_target || !alias_target->c)
5602 error("unsupported forward __alias__ attribute");
5603 esym = &((Elf32_Sym *)symtab_section->data)[alias_target->c];
5604 tsec.sh_num = esym->st_shndx;
5605 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
5607 } else {
5608 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5609 if (type.t & VT_STATIC)
5610 r |= VT_CONST;
5611 else
5612 r |= l;
5613 if (has_init)
5614 next();
5615 decl_initializer_alloc(&type, &ad, r, has_init, v, asm_label, l);
5618 if (tok != ',') {
5619 skip(';');
5620 break;
5622 next();