Add support for --help
[tinycc.git] / tccgen.c
blobe761f917c846e9bc9489876d468659a22c524d2c
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, 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 sym_free_first = sym;
134 /* push, without hashing */
135 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
137 Sym *s;
138 s = sym_malloc();
139 s->v = v;
140 s->type.t = t;
141 s->type.ref = NULL;
142 #ifdef _WIN64
143 s->d = NULL;
144 #endif
145 s->c = c;
146 s->next = NULL;
147 /* add in stack */
148 s->prev = *ps;
149 *ps = s;
150 return s;
153 /* find a symbol and return its associated structure. 's' is the top
154 of the symbol stack */
155 ST_FUNC Sym *sym_find2(Sym *s, int v)
157 while (s) {
158 if (s->v == v)
159 return s;
160 s = s->prev;
162 return NULL;
165 /* structure lookup */
166 ST_INLN Sym *struct_find(int v)
168 v -= TOK_IDENT;
169 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
170 return NULL;
171 return table_ident[v]->sym_struct;
174 /* find an identifier */
175 ST_INLN Sym *sym_find(int v)
177 v -= TOK_IDENT;
178 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
179 return NULL;
180 return table_ident[v]->sym_identifier;
183 /* push a given symbol on the symbol stack */
184 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
186 Sym *s, **ps;
187 TokenSym *ts;
189 if (local_stack)
190 ps = &local_stack;
191 else
192 ps = &global_stack;
193 s = sym_push2(ps, v, type->t, c);
194 s->type.ref = type->ref;
195 s->r = r;
196 /* don't record fields or anonymous symbols */
197 /* XXX: simplify */
198 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
199 /* record symbol in token array */
200 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
201 if (v & SYM_STRUCT)
202 ps = &ts->sym_struct;
203 else
204 ps = &ts->sym_identifier;
205 s->prev_tok = *ps;
206 *ps = s;
208 return s;
211 /* push a global identifier */
212 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
214 Sym *s, **ps;
215 s = sym_push2(&global_stack, v, t, c);
216 /* don't record anonymous symbol */
217 if (v < SYM_FIRST_ANOM) {
218 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
219 /* modify the top most local identifier, so that
220 sym_identifier will point to 's' when popped */
221 while (*ps != NULL)
222 ps = &(*ps)->prev_tok;
223 s->prev_tok = NULL;
224 *ps = s;
226 return s;
229 /* pop symbols until top reaches 'b' */
230 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
232 Sym *s, *ss, **ps;
233 TokenSym *ts;
234 int v;
236 s = *ptop;
237 while(s != b) {
238 ss = s->prev;
239 v = s->v;
240 /* remove symbol in token array */
241 /* XXX: simplify */
242 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
243 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
244 if (v & SYM_STRUCT)
245 ps = &ts->sym_struct;
246 else
247 ps = &ts->sym_identifier;
248 *ps = s->prev_tok;
250 sym_free(s);
251 s = ss;
253 *ptop = b;
256 /* ------------------------------------------------------------------------- */
258 ST_FUNC void swap(int *p, int *q)
260 int t;
261 t = *p;
262 *p = *q;
263 *q = t;
266 static void vsetc(CType *type, int r, CValue *vc)
268 int v;
270 if (vtop >= vstack + (VSTACK_SIZE - 1))
271 error("memory full");
272 /* cannot let cpu flags if other instruction are generated. Also
273 avoid leaving VT_JMP anywhere except on the top of the stack
274 because it would complicate the code generator. */
275 if (vtop >= vstack) {
276 v = vtop->r & VT_VALMASK;
277 if (v == VT_CMP || (v & ~1) == VT_JMP)
278 gv(RC_INT);
280 vtop++;
281 vtop->type = *type;
282 vtop->r = r;
283 vtop->r2 = VT_CONST;
284 vtop->c = *vc;
287 /* push integer constant */
288 ST_FUNC void vpushi(int v)
290 CValue cval;
291 cval.i = v;
292 vsetc(&int_type, VT_CONST, &cval);
295 /* push long long constant */
296 static void vpushll(long long v)
298 CValue cval;
299 CType ctype;
300 ctype.t = VT_LLONG;
301 ctype.ref = 0;
302 cval.ull = v;
303 vsetc(&ctype, VT_CONST, &cval);
306 /* Return a static symbol pointing to a section */
307 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
309 int v;
310 Sym *sym;
312 v = anon_sym++;
313 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
314 sym->type.ref = type->ref;
315 sym->r = VT_CONST | VT_SYM;
316 put_extern_sym(sym, sec, offset, size);
317 return sym;
320 /* push a reference to a section offset by adding a dummy symbol */
321 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
323 CValue cval;
325 cval.ul = 0;
326 vsetc(type, VT_CONST | VT_SYM, &cval);
327 vtop->sym = get_sym_ref(type, sec, offset, size);
330 /* define a new external reference to a symbol 'v' of type 'u' */
331 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
333 Sym *s;
335 s = sym_find(v);
336 if (!s) {
337 /* push forward reference */
338 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
339 s->type.ref = type->ref;
340 s->r = r | VT_CONST | VT_SYM;
342 return s;
345 /* define a new external reference to a symbol 'v' of type 'u' */
346 static Sym *external_sym(int v, CType *type, int r)
348 Sym *s;
350 s = sym_find(v);
351 if (!s) {
352 /* push forward reference */
353 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
354 s->type.t |= VT_EXTERN;
355 } else if (s->type.ref == func_old_type.ref) {
356 s->type.ref = type->ref;
357 s->r = r | VT_CONST | VT_SYM;
358 s->type.t |= VT_EXTERN;
359 } else if (!is_compatible_types(&s->type, type)) {
360 error("incompatible types for redefinition of '%s'",
361 get_tok_str(v, NULL));
363 return s;
366 /* push a reference to global symbol v */
367 ST_FUNC void vpush_global_sym(CType *type, int v)
369 Sym *sym;
370 CValue cval;
372 sym = external_global_sym(v, type, 0);
373 cval.ul = 0;
374 vsetc(type, VT_CONST | VT_SYM, &cval);
375 vtop->sym = sym;
378 ST_FUNC void vset(CType *type, int r, int v)
380 CValue cval;
382 cval.i = v;
383 vsetc(type, r, &cval);
386 static void vseti(int r, int v)
388 CType type;
389 type.t = VT_INT;
390 type.ref = 0;
391 vset(&type, r, v);
394 ST_FUNC void vswap(void)
396 SValue tmp;
398 tmp = vtop[0];
399 vtop[0] = vtop[-1];
400 vtop[-1] = tmp;
403 ST_FUNC void vpushv(SValue *v)
405 if (vtop >= vstack + (VSTACK_SIZE - 1))
406 error("memory full");
407 vtop++;
408 *vtop = *v;
411 static void vdup(void)
413 vpushv(vtop);
416 /* save r to the memory stack, and mark it as being free */
417 ST_FUNC void save_reg(int r)
419 int l, saved, size, align;
420 SValue *p, sv;
421 CType *type;
423 /* modify all stack values */
424 saved = 0;
425 l = 0;
426 for(p=vstack;p<=vtop;p++) {
427 if ((p->r & VT_VALMASK) == r ||
428 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
429 /* must save value on stack if not already done */
430 if (!saved) {
431 /* NOTE: must reload 'r' because r might be equal to r2 */
432 r = p->r & VT_VALMASK;
433 /* store register in the stack */
434 type = &p->type;
435 if ((p->r & VT_LVAL) ||
436 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
437 #ifdef TCC_TARGET_X86_64
438 type = &char_pointer_type;
439 #else
440 type = &int_type;
441 #endif
442 size = type_size(type, &align);
443 loc = (loc - size) & -align;
444 sv.type.t = type->t;
445 sv.r = VT_LOCAL | VT_LVAL;
446 sv.c.ul = loc;
447 store(r, &sv);
448 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
449 /* x86 specific: need to pop fp register ST0 if saved */
450 if (r == TREG_ST0) {
451 o(0xd8dd); /* fstp %st(0) */
453 #endif
454 #ifndef TCC_TARGET_X86_64
455 /* special long long case */
456 if ((type->t & VT_BTYPE) == VT_LLONG) {
457 sv.c.ul += 4;
458 store(p->r2, &sv);
460 #endif
461 l = loc;
462 saved = 1;
464 /* mark that stack entry as being saved on the stack */
465 if (p->r & VT_LVAL) {
466 /* also clear the bounded flag because the
467 relocation address of the function was stored in
468 p->c.ul */
469 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
470 } else {
471 p->r = lvalue_type(p->type.t) | VT_LOCAL;
473 p->r2 = VT_CONST;
474 p->c.ul = l;
479 #ifdef TCC_TARGET_ARM
480 /* find a register of class 'rc2' with at most one reference on stack.
481 * If none, call get_reg(rc) */
482 ST_FUNC int get_reg_ex(int rc, int rc2)
484 int r;
485 SValue *p;
487 for(r=0;r<NB_REGS;r++) {
488 if (reg_classes[r] & rc2) {
489 int n;
490 n=0;
491 for(p = vstack; p <= vtop; p++) {
492 if ((p->r & VT_VALMASK) == r ||
493 (p->r2 & VT_VALMASK) == r)
494 n++;
496 if (n <= 1)
497 return r;
500 return get_reg(rc);
502 #endif
504 /* find a free register of class 'rc'. If none, save one register */
505 ST_FUNC int get_reg(int rc)
507 int r;
508 SValue *p;
510 /* find a free register */
511 for(r=0;r<NB_REGS;r++) {
512 if (reg_classes[r] & rc) {
513 for(p=vstack;p<=vtop;p++) {
514 if ((p->r & VT_VALMASK) == r ||
515 (p->r2 & VT_VALMASK) == r)
516 goto notfound;
518 return r;
520 notfound: ;
523 /* no register left : free the first one on the stack (VERY
524 IMPORTANT to start from the bottom to ensure that we don't
525 spill registers used in gen_opi()) */
526 for(p=vstack;p<=vtop;p++) {
527 r = p->r & VT_VALMASK;
528 if (r < VT_CONST && (reg_classes[r] & rc))
529 goto save_found;
530 /* also look at second register (if long long) */
531 r = p->r2 & VT_VALMASK;
532 if (r < VT_CONST && (reg_classes[r] & rc)) {
533 save_found:
534 save_reg(r);
535 return r;
538 /* Should never comes here */
539 return -1;
542 /* save registers up to (vtop - n) stack entry */
543 ST_FUNC void save_regs(int n)
545 int r;
546 SValue *p, *p1;
547 p1 = vtop - n;
548 for(p = vstack;p <= p1; p++) {
549 r = p->r & VT_VALMASK;
550 if (r < VT_CONST) {
551 save_reg(r);
556 /* move register 's' to 'r', and flush previous value of r to memory
557 if needed */
558 static void move_reg(int r, int s)
560 SValue sv;
562 if (r != s) {
563 save_reg(r);
564 sv.type.t = VT_INT;
565 sv.r = s;
566 sv.c.ul = 0;
567 load(r, &sv);
571 /* get address of vtop (vtop MUST BE an lvalue) */
572 static void gaddrof(void)
574 vtop->r &= ~VT_LVAL;
575 /* tricky: if saved lvalue, then we can go back to lvalue */
576 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
577 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
580 #ifdef CONFIG_TCC_BCHECK
581 /* generate lvalue bound code */
582 static void gbound(void)
584 int lval_type;
585 CType type1;
587 vtop->r &= ~VT_MUSTBOUND;
588 /* if lvalue, then use checking code before dereferencing */
589 if (vtop->r & VT_LVAL) {
590 /* if not VT_BOUNDED value, then make one */
591 if (!(vtop->r & VT_BOUNDED)) {
592 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
593 /* must save type because we must set it to int to get pointer */
594 type1 = vtop->type;
595 vtop->type.t = VT_INT;
596 gaddrof();
597 vpushi(0);
598 gen_bounded_ptr_add();
599 vtop->r |= lval_type;
600 vtop->type = type1;
602 /* then check for dereferencing */
603 gen_bounded_ptr_deref();
606 #endif
608 /* store vtop a register belonging to class 'rc'. lvalues are
609 converted to values. Cannot be used if cannot be converted to
610 register value (such as structures). */
611 ST_FUNC int gv(int rc)
613 int r, rc2, bit_pos, bit_size, size, align, i;
615 /* NOTE: get_reg can modify vstack[] */
616 if (vtop->type.t & VT_BITFIELD) {
617 CType type;
618 int bits = 32;
619 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
620 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
621 /* remove bit field info to avoid loops */
622 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
623 /* cast to int to propagate signedness in following ops */
624 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
625 type.t = VT_LLONG;
626 bits = 64;
627 } else
628 type.t = VT_INT;
629 if((vtop->type.t & VT_UNSIGNED) ||
630 (vtop->type.t & VT_BTYPE) == VT_BOOL)
631 type.t |= VT_UNSIGNED;
632 gen_cast(&type);
633 /* generate shifts */
634 vpushi(bits - (bit_pos + bit_size));
635 gen_op(TOK_SHL);
636 vpushi(bits - bit_size);
637 /* NOTE: transformed to SHR if unsigned */
638 gen_op(TOK_SAR);
639 r = gv(rc);
640 } else {
641 if (is_float(vtop->type.t) &&
642 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
643 Sym *sym;
644 int *ptr;
645 unsigned long offset;
646 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
647 CValue check;
648 #endif
650 /* XXX: unify with initializers handling ? */
651 /* CPUs usually cannot use float constants, so we store them
652 generically in data segment */
653 size = type_size(&vtop->type, &align);
654 offset = (data_section->data_offset + align - 1) & -align;
655 data_section->data_offset = offset;
656 /* XXX: not portable yet */
657 #if defined(__i386__) || defined(__x86_64__)
658 /* Zero pad x87 tenbyte long doubles */
659 if (size == LDOUBLE_SIZE)
660 vtop->c.tab[2] &= 0xffff;
661 #endif
662 ptr = section_ptr_add(data_section, size);
663 size = size >> 2;
664 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
665 check.d = 1;
666 if(check.tab[0])
667 for(i=0;i<size;i++)
668 ptr[i] = vtop->c.tab[size-1-i];
669 else
670 #endif
671 for(i=0;i<size;i++)
672 ptr[i] = vtop->c.tab[i];
673 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
674 vtop->r |= VT_LVAL | VT_SYM;
675 vtop->sym = sym;
676 vtop->c.ul = 0;
678 #ifdef CONFIG_TCC_BCHECK
679 if (vtop->r & VT_MUSTBOUND)
680 gbound();
681 #endif
683 r = vtop->r & VT_VALMASK;
684 rc2 = RC_INT;
685 if (rc == RC_IRET)
686 rc2 = RC_LRET;
687 /* need to reload if:
688 - constant
689 - lvalue (need to dereference pointer)
690 - already a register, but not in the right class */
691 if (r >= VT_CONST
692 || (vtop->r & VT_LVAL)
693 || !(reg_classes[r] & rc)
694 #ifndef TCC_TARGET_X86_64
695 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
696 #endif
699 r = get_reg(rc);
700 #ifndef TCC_TARGET_X86_64
701 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
702 int r2;
703 unsigned long long ll;
704 /* two register type load : expand to two words
705 temporarily */
706 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
707 /* load constant */
708 ll = vtop->c.ull;
709 vtop->c.ui = ll; /* first word */
710 load(r, vtop);
711 vtop->r = r; /* save register value */
712 vpushi(ll >> 32); /* second word */
713 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
714 (vtop->r & VT_LVAL)) {
715 /* We do not want to modifier the long long
716 pointer here, so the safest (and less
717 efficient) is to save all the other registers
718 in the stack. XXX: totally inefficient. */
719 save_regs(1);
720 /* load from memory */
721 load(r, vtop);
722 vdup();
723 vtop[-1].r = r; /* save register value */
724 /* increment pointer to get second word */
725 vtop->type.t = VT_INT;
726 gaddrof();
727 vpushi(4);
728 gen_op('+');
729 vtop->r |= VT_LVAL;
730 } else {
731 /* move registers */
732 load(r, vtop);
733 vdup();
734 vtop[-1].r = r; /* save register value */
735 vtop->r = vtop[-1].r2;
737 /* allocate second register */
738 r2 = get_reg(rc2);
739 load(r2, vtop);
740 vpop();
741 /* write second register */
742 vtop->r2 = r2;
743 } else
744 #endif
745 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
746 int t1, t;
747 /* lvalue of scalar type : need to use lvalue type
748 because of possible cast */
749 t = vtop->type.t;
750 t1 = t;
751 /* compute memory access type */
752 if (vtop->r & VT_LVAL_BYTE)
753 t = VT_BYTE;
754 else if (vtop->r & VT_LVAL_SHORT)
755 t = VT_SHORT;
756 if (vtop->r & VT_LVAL_UNSIGNED)
757 t |= VT_UNSIGNED;
758 vtop->type.t = t;
759 load(r, vtop);
760 /* restore wanted type */
761 vtop->type.t = t1;
762 } else {
763 /* one register type load */
764 load(r, vtop);
767 vtop->r = r;
768 #ifdef TCC_TARGET_C67
769 /* uses register pairs for doubles */
770 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
771 vtop->r2 = r+1;
772 #endif
774 return r;
777 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
778 ST_FUNC void gv2(int rc1, int rc2)
780 int v;
782 /* generate more generic register first. But VT_JMP or VT_CMP
783 values must be generated first in all cases to avoid possible
784 reload errors */
785 v = vtop[0].r & VT_VALMASK;
786 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
787 vswap();
788 gv(rc1);
789 vswap();
790 gv(rc2);
791 /* test if reload is needed for first register */
792 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
793 vswap();
794 gv(rc1);
795 vswap();
797 } else {
798 gv(rc2);
799 vswap();
800 gv(rc1);
801 vswap();
802 /* test if reload is needed for first register */
803 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
804 gv(rc2);
809 /* wrapper around RC_FRET to return a register by type */
810 static int rc_fret(int t)
812 #ifdef TCC_TARGET_X86_64
813 if (t == VT_LDOUBLE) {
814 return RC_ST0;
816 #endif
817 return RC_FRET;
820 /* wrapper around REG_FRET to return a register by type */
821 static int reg_fret(int t)
823 #ifdef TCC_TARGET_X86_64
824 if (t == VT_LDOUBLE) {
825 return TREG_ST0;
827 #endif
828 return REG_FRET;
831 /* expand long long on stack in two int registers */
832 static void lexpand(void)
834 int u;
836 u = vtop->type.t & VT_UNSIGNED;
837 gv(RC_INT);
838 vdup();
839 vtop[0].r = vtop[-1].r2;
840 vtop[0].r2 = VT_CONST;
841 vtop[-1].r2 = VT_CONST;
842 vtop[0].type.t = VT_INT | u;
843 vtop[-1].type.t = VT_INT | u;
846 #ifdef TCC_TARGET_ARM
847 /* expand long long on stack */
848 ST_FUNC void lexpand_nr(void)
850 int u,v;
852 u = vtop->type.t & VT_UNSIGNED;
853 vdup();
854 vtop->r2 = VT_CONST;
855 vtop->type.t = VT_INT | u;
856 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
857 if (v == VT_CONST) {
858 vtop[-1].c.ui = vtop->c.ull;
859 vtop->c.ui = vtop->c.ull >> 32;
860 vtop->r = VT_CONST;
861 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
862 vtop->c.ui += 4;
863 vtop->r = vtop[-1].r;
864 } else if (v > VT_CONST) {
865 vtop--;
866 lexpand();
867 } else
868 vtop->r = vtop[-1].r2;
869 vtop[-1].r2 = VT_CONST;
870 vtop[-1].type.t = VT_INT | u;
872 #endif
874 /* build a long long from two ints */
875 static void lbuild(int t)
877 gv2(RC_INT, RC_INT);
878 vtop[-1].r2 = vtop[0].r;
879 vtop[-1].type.t = t;
880 vpop();
883 /* rotate n first stack elements to the bottom
884 I1 ... In -> I2 ... In I1 [top is right]
886 static void vrotb(int n)
888 int i;
889 SValue tmp;
891 tmp = vtop[-n + 1];
892 for(i=-n+1;i!=0;i++)
893 vtop[i] = vtop[i+1];
894 vtop[0] = tmp;
897 /* rotate n first stack elements to the top
898 I1 ... In -> In I1 ... I(n-1) [top is right]
900 ST_FUNC void vrott(int n)
902 int i;
903 SValue tmp;
905 tmp = vtop[0];
906 for(i = 0;i < n - 1; i++)
907 vtop[-i] = vtop[-i - 1];
908 vtop[-n + 1] = tmp;
911 #ifdef TCC_TARGET_ARM
912 /* like vrott but in other direction
913 In ... I1 -> I(n-1) ... I1 In [top is right]
915 ST_FUNC void vnrott(int n)
917 int i;
918 SValue tmp;
920 tmp = vtop[-n + 1];
921 for(i = n - 1; i > 0; i--)
922 vtop[-i] = vtop[-i + 1];
923 vtop[0] = tmp;
925 #endif
927 /* pop stack value */
928 ST_FUNC void vpop(void)
930 int v;
931 v = vtop->r & VT_VALMASK;
932 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
933 /* for x86, we need to pop the FP stack */
934 if (v == TREG_ST0 && !nocode_wanted) {
935 o(0xd8dd); /* fstp %st(0) */
936 } else
937 #endif
938 if (v == VT_JMP || v == VT_JMPI) {
939 /* need to put correct jump if && or || without test */
940 gsym(vtop->c.ul);
942 vtop--;
945 /* convert stack entry to register and duplicate its value in another
946 register */
947 static void gv_dup(void)
949 int rc, t, r, r1;
950 SValue sv;
952 t = vtop->type.t;
953 if ((t & VT_BTYPE) == VT_LLONG) {
954 lexpand();
955 gv_dup();
956 vswap();
957 vrotb(3);
958 gv_dup();
959 vrotb(4);
960 /* stack: H L L1 H1 */
961 lbuild(t);
962 vrotb(3);
963 vrotb(3);
964 vswap();
965 lbuild(t);
966 vswap();
967 } else {
968 /* duplicate value */
969 rc = RC_INT;
970 sv.type.t = VT_INT;
971 if (is_float(t)) {
972 rc = RC_FLOAT;
973 #ifdef TCC_TARGET_X86_64
974 if ((t & VT_BTYPE) == VT_LDOUBLE) {
975 rc = RC_ST0;
977 #endif
978 sv.type.t = t;
980 r = gv(rc);
981 r1 = get_reg(rc);
982 sv.r = r;
983 sv.c.ul = 0;
984 load(r1, &sv); /* move r to r1 */
985 vdup();
986 /* duplicates value */
987 if (r != r1)
988 vtop->r = r1;
992 #ifndef TCC_TARGET_X86_64
993 /* generate CPU independent (unsigned) long long operations */
994 static void gen_opl(int op)
996 int t, a, b, op1, c, i;
997 int func;
998 unsigned short reg_iret = REG_IRET;
999 unsigned short reg_lret = REG_LRET;
1000 SValue tmp;
1002 switch(op) {
1003 case '/':
1004 case TOK_PDIV:
1005 func = TOK___divdi3;
1006 goto gen_func;
1007 case TOK_UDIV:
1008 func = TOK___udivdi3;
1009 goto gen_func;
1010 case '%':
1011 func = TOK___moddi3;
1012 goto gen_mod_func;
1013 case TOK_UMOD:
1014 func = TOK___umoddi3;
1015 gen_mod_func:
1016 #ifdef TCC_ARM_EABI
1017 reg_iret = TREG_R2;
1018 reg_lret = TREG_R3;
1019 #endif
1020 gen_func:
1021 /* call generic long long function */
1022 vpush_global_sym(&func_old_type, func);
1023 vrott(3);
1024 gfunc_call(2);
1025 vpushi(0);
1026 vtop->r = reg_iret;
1027 vtop->r2 = reg_lret;
1028 break;
1029 case '^':
1030 case '&':
1031 case '|':
1032 case '*':
1033 case '+':
1034 case '-':
1035 t = vtop->type.t;
1036 vswap();
1037 lexpand();
1038 vrotb(3);
1039 lexpand();
1040 /* stack: L1 H1 L2 H2 */
1041 tmp = vtop[0];
1042 vtop[0] = vtop[-3];
1043 vtop[-3] = tmp;
1044 tmp = vtop[-2];
1045 vtop[-2] = vtop[-3];
1046 vtop[-3] = tmp;
1047 vswap();
1048 /* stack: H1 H2 L1 L2 */
1049 if (op == '*') {
1050 vpushv(vtop - 1);
1051 vpushv(vtop - 1);
1052 gen_op(TOK_UMULL);
1053 lexpand();
1054 /* stack: H1 H2 L1 L2 ML MH */
1055 for(i=0;i<4;i++)
1056 vrotb(6);
1057 /* stack: ML MH H1 H2 L1 L2 */
1058 tmp = vtop[0];
1059 vtop[0] = vtop[-2];
1060 vtop[-2] = tmp;
1061 /* stack: ML MH H1 L2 H2 L1 */
1062 gen_op('*');
1063 vrotb(3);
1064 vrotb(3);
1065 gen_op('*');
1066 /* stack: ML MH M1 M2 */
1067 gen_op('+');
1068 gen_op('+');
1069 } else if (op == '+' || op == '-') {
1070 /* XXX: add non carry method too (for MIPS or alpha) */
1071 if (op == '+')
1072 op1 = TOK_ADDC1;
1073 else
1074 op1 = TOK_SUBC1;
1075 gen_op(op1);
1076 /* stack: H1 H2 (L1 op L2) */
1077 vrotb(3);
1078 vrotb(3);
1079 gen_op(op1 + 1); /* TOK_xxxC2 */
1080 } else {
1081 gen_op(op);
1082 /* stack: H1 H2 (L1 op L2) */
1083 vrotb(3);
1084 vrotb(3);
1085 /* stack: (L1 op L2) H1 H2 */
1086 gen_op(op);
1087 /* stack: (L1 op L2) (H1 op H2) */
1089 /* stack: L H */
1090 lbuild(t);
1091 break;
1092 case TOK_SAR:
1093 case TOK_SHR:
1094 case TOK_SHL:
1095 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1096 t = vtop[-1].type.t;
1097 vswap();
1098 lexpand();
1099 vrotb(3);
1100 /* stack: L H shift */
1101 c = (int)vtop->c.i;
1102 /* constant: simpler */
1103 /* NOTE: all comments are for SHL. the other cases are
1104 done by swaping words */
1105 vpop();
1106 if (op != TOK_SHL)
1107 vswap();
1108 if (c >= 32) {
1109 /* stack: L H */
1110 vpop();
1111 if (c > 32) {
1112 vpushi(c - 32);
1113 gen_op(op);
1115 if (op != TOK_SAR) {
1116 vpushi(0);
1117 } else {
1118 gv_dup();
1119 vpushi(31);
1120 gen_op(TOK_SAR);
1122 vswap();
1123 } else {
1124 vswap();
1125 gv_dup();
1126 /* stack: H L L */
1127 vpushi(c);
1128 gen_op(op);
1129 vswap();
1130 vpushi(32 - c);
1131 if (op == TOK_SHL)
1132 gen_op(TOK_SHR);
1133 else
1134 gen_op(TOK_SHL);
1135 vrotb(3);
1136 /* stack: L L H */
1137 vpushi(c);
1138 if (op == TOK_SHL)
1139 gen_op(TOK_SHL);
1140 else
1141 gen_op(TOK_SHR);
1142 gen_op('|');
1144 if (op != TOK_SHL)
1145 vswap();
1146 lbuild(t);
1147 } else {
1148 /* XXX: should provide a faster fallback on x86 ? */
1149 switch(op) {
1150 case TOK_SAR:
1151 func = TOK___ashrdi3;
1152 goto gen_func;
1153 case TOK_SHR:
1154 func = TOK___lshrdi3;
1155 goto gen_func;
1156 case TOK_SHL:
1157 func = TOK___ashldi3;
1158 goto gen_func;
1161 break;
1162 default:
1163 /* compare operations */
1164 t = vtop->type.t;
1165 vswap();
1166 lexpand();
1167 vrotb(3);
1168 lexpand();
1169 /* stack: L1 H1 L2 H2 */
1170 tmp = vtop[-1];
1171 vtop[-1] = vtop[-2];
1172 vtop[-2] = tmp;
1173 /* stack: L1 L2 H1 H2 */
1174 /* compare high */
1175 op1 = op;
1176 /* when values are equal, we need to compare low words. since
1177 the jump is inverted, we invert the test too. */
1178 if (op1 == TOK_LT)
1179 op1 = TOK_LE;
1180 else if (op1 == TOK_GT)
1181 op1 = TOK_GE;
1182 else if (op1 == TOK_ULT)
1183 op1 = TOK_ULE;
1184 else if (op1 == TOK_UGT)
1185 op1 = TOK_UGE;
1186 a = 0;
1187 b = 0;
1188 gen_op(op1);
1189 if (op1 != TOK_NE) {
1190 a = gtst(1, 0);
1192 if (op != TOK_EQ) {
1193 /* generate non equal test */
1194 /* XXX: NOT PORTABLE yet */
1195 if (a == 0) {
1196 b = gtst(0, 0);
1197 } else {
1198 #if defined(TCC_TARGET_I386)
1199 b = psym(0x850f, 0);
1200 #elif defined(TCC_TARGET_ARM)
1201 b = ind;
1202 o(0x1A000000 | encbranch(ind, 0, 1));
1203 #elif defined(TCC_TARGET_C67)
1204 error("not implemented");
1205 #else
1206 #error not supported
1207 #endif
1210 /* compare low. Always unsigned */
1211 op1 = op;
1212 if (op1 == TOK_LT)
1213 op1 = TOK_ULT;
1214 else if (op1 == TOK_LE)
1215 op1 = TOK_ULE;
1216 else if (op1 == TOK_GT)
1217 op1 = TOK_UGT;
1218 else if (op1 == TOK_GE)
1219 op1 = TOK_UGE;
1220 gen_op(op1);
1221 a = gtst(1, a);
1222 gsym(b);
1223 vseti(VT_JMPI, a);
1224 break;
1227 #endif
1229 /* handle integer constant optimizations and various machine
1230 independent opt */
1231 static void gen_opic(int op)
1233 int c1, c2, t1, t2, n;
1234 SValue *v1, *v2;
1235 long long l1, l2;
1236 typedef unsigned long long U;
1238 v1 = vtop - 1;
1239 v2 = vtop;
1240 t1 = v1->type.t & VT_BTYPE;
1241 t2 = v2->type.t & VT_BTYPE;
1243 if (t1 == VT_LLONG)
1244 l1 = v1->c.ll;
1245 else if (v1->type.t & VT_UNSIGNED)
1246 l1 = v1->c.ui;
1247 else
1248 l1 = v1->c.i;
1250 if (t2 == VT_LLONG)
1251 l2 = v2->c.ll;
1252 else if (v2->type.t & VT_UNSIGNED)
1253 l2 = v2->c.ui;
1254 else
1255 l2 = v2->c.i;
1257 /* currently, we cannot do computations with forward symbols */
1258 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1259 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1260 if (c1 && c2) {
1261 switch(op) {
1262 case '+': l1 += l2; break;
1263 case '-': l1 -= l2; break;
1264 case '&': l1 &= l2; break;
1265 case '^': l1 ^= l2; break;
1266 case '|': l1 |= l2; break;
1267 case '*': l1 *= l2; break;
1269 case TOK_PDIV:
1270 case '/':
1271 case '%':
1272 case TOK_UDIV:
1273 case TOK_UMOD:
1274 /* if division by zero, generate explicit division */
1275 if (l2 == 0) {
1276 if (const_wanted)
1277 error("division by zero in constant");
1278 goto general_case;
1280 switch(op) {
1281 default: l1 /= l2; break;
1282 case '%': l1 %= l2; break;
1283 case TOK_UDIV: l1 = (U)l1 / l2; break;
1284 case TOK_UMOD: l1 = (U)l1 % l2; break;
1286 break;
1287 case TOK_SHL: l1 <<= l2; break;
1288 case TOK_SHR: l1 = (U)l1 >> l2; break;
1289 case TOK_SAR: l1 >>= l2; break;
1290 /* tests */
1291 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1292 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1293 case TOK_EQ: l1 = l1 == l2; break;
1294 case TOK_NE: l1 = l1 != l2; break;
1295 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1296 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1297 case TOK_LT: l1 = l1 < l2; break;
1298 case TOK_GE: l1 = l1 >= l2; break;
1299 case TOK_LE: l1 = l1 <= l2; break;
1300 case TOK_GT: l1 = l1 > l2; break;
1301 /* logical */
1302 case TOK_LAND: l1 = l1 && l2; break;
1303 case TOK_LOR: l1 = l1 || l2; break;
1304 default:
1305 goto general_case;
1307 v1->c.ll = l1;
1308 vtop--;
1309 } else {
1310 /* if commutative ops, put c2 as constant */
1311 if (c1 && (op == '+' || op == '&' || op == '^' ||
1312 op == '|' || op == '*')) {
1313 vswap();
1314 c2 = c1; //c = c1, c1 = c2, c2 = c;
1315 l2 = l1; //l = l1, l1 = l2, l2 = l;
1317 /* Filter out NOP operations like x*1, x-0, x&-1... */
1318 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1319 op == TOK_PDIV) &&
1320 l2 == 1) ||
1321 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1322 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1323 l2 == 0) ||
1324 (op == '&' &&
1325 l2 == -1))) {
1326 /* nothing to do */
1327 vtop--;
1328 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1329 /* try to use shifts instead of muls or divs */
1330 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1331 n = -1;
1332 while (l2) {
1333 l2 >>= 1;
1334 n++;
1336 vtop->c.ll = n;
1337 if (op == '*')
1338 op = TOK_SHL;
1339 else if (op == TOK_PDIV)
1340 op = TOK_SAR;
1341 else
1342 op = TOK_SHR;
1344 goto general_case;
1345 } else if (c2 && (op == '+' || op == '-') &&
1346 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1347 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1348 /* symbol + constant case */
1349 if (op == '-')
1350 l2 = -l2;
1351 vtop--;
1352 vtop->c.ll += l2;
1353 } else {
1354 general_case:
1355 if (!nocode_wanted) {
1356 /* call low level op generator */
1357 if (t1 == VT_LLONG || t2 == VT_LLONG)
1358 gen_opl(op);
1359 else
1360 gen_opi(op);
1361 } else {
1362 vtop--;
1368 /* generate a floating point operation with constant propagation */
1369 static void gen_opif(int op)
1371 int c1, c2;
1372 SValue *v1, *v2;
1373 long double f1, f2;
1375 v1 = vtop - 1;
1376 v2 = vtop;
1377 /* currently, we cannot do computations with forward symbols */
1378 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1379 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1380 if (c1 && c2) {
1381 if (v1->type.t == VT_FLOAT) {
1382 f1 = v1->c.f;
1383 f2 = v2->c.f;
1384 } else if (v1->type.t == VT_DOUBLE) {
1385 f1 = v1->c.d;
1386 f2 = v2->c.d;
1387 } else {
1388 f1 = v1->c.ld;
1389 f2 = v2->c.ld;
1392 /* NOTE: we only do constant propagation if finite number (not
1393 NaN or infinity) (ANSI spec) */
1394 if (!ieee_finite(f1) || !ieee_finite(f2))
1395 goto general_case;
1397 switch(op) {
1398 case '+': f1 += f2; break;
1399 case '-': f1 -= f2; break;
1400 case '*': f1 *= f2; break;
1401 case '/':
1402 if (f2 == 0.0) {
1403 if (const_wanted)
1404 error("division by zero in constant");
1405 goto general_case;
1407 f1 /= f2;
1408 break;
1409 /* XXX: also handles tests ? */
1410 default:
1411 goto general_case;
1413 /* XXX: overflow test ? */
1414 if (v1->type.t == VT_FLOAT) {
1415 v1->c.f = f1;
1416 } else if (v1->type.t == VT_DOUBLE) {
1417 v1->c.d = f1;
1418 } else {
1419 v1->c.ld = f1;
1421 vtop--;
1422 } else {
1423 general_case:
1424 if (!nocode_wanted) {
1425 gen_opf(op);
1426 } else {
1427 vtop--;
1432 static int pointed_size(CType *type)
1434 int align;
1435 return type_size(pointed_type(type), &align);
1438 static inline int is_null_pointer(SValue *p)
1440 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1441 return 0;
1442 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1443 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1446 static inline int is_integer_btype(int bt)
1448 return (bt == VT_BYTE || bt == VT_SHORT ||
1449 bt == VT_INT || bt == VT_LLONG);
1452 /* check types for comparison or substraction of pointers */
1453 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1455 CType *type1, *type2, tmp_type1, tmp_type2;
1456 int bt1, bt2;
1458 /* null pointers are accepted for all comparisons as gcc */
1459 if (is_null_pointer(p1) || is_null_pointer(p2))
1460 return;
1461 type1 = &p1->type;
1462 type2 = &p2->type;
1463 bt1 = type1->t & VT_BTYPE;
1464 bt2 = type2->t & VT_BTYPE;
1465 /* accept comparison between pointer and integer with a warning */
1466 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1467 if (op != TOK_LOR && op != TOK_LAND )
1468 warning("comparison between pointer and integer");
1469 return;
1472 /* both must be pointers or implicit function pointers */
1473 if (bt1 == VT_PTR) {
1474 type1 = pointed_type(type1);
1475 } else if (bt1 != VT_FUNC)
1476 goto invalid_operands;
1478 if (bt2 == VT_PTR) {
1479 type2 = pointed_type(type2);
1480 } else if (bt2 != VT_FUNC) {
1481 invalid_operands:
1482 error("invalid operands to binary %s", get_tok_str(op, NULL));
1484 if ((type1->t & VT_BTYPE) == VT_VOID ||
1485 (type2->t & VT_BTYPE) == VT_VOID)
1486 return;
1487 tmp_type1 = *type1;
1488 tmp_type2 = *type2;
1489 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1490 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1491 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1492 /* gcc-like error if '-' is used */
1493 if (op == '-')
1494 goto invalid_operands;
1495 else
1496 warning("comparison of distinct pointer types lacks a cast");
1500 /* generic gen_op: handles types problems */
1501 ST_FUNC void gen_op(int op)
1503 int u, t1, t2, bt1, bt2, t;
1504 CType type1;
1506 t1 = vtop[-1].type.t;
1507 t2 = vtop[0].type.t;
1508 bt1 = t1 & VT_BTYPE;
1509 bt2 = t2 & VT_BTYPE;
1511 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1512 /* at least one operand is a pointer */
1513 /* relationnal op: must be both pointers */
1514 if (op >= TOK_ULT && op <= TOK_LOR) {
1515 check_comparison_pointer_types(vtop - 1, vtop, op);
1516 /* pointers are handled are unsigned */
1517 #ifdef TCC_TARGET_X86_64
1518 t = VT_LLONG | VT_UNSIGNED;
1519 #else
1520 t = VT_INT | VT_UNSIGNED;
1521 #endif
1522 goto std_op;
1524 /* if both pointers, then it must be the '-' op */
1525 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1526 if (op != '-')
1527 error("cannot use pointers here");
1528 check_comparison_pointer_types(vtop - 1, vtop, op);
1529 /* XXX: check that types are compatible */
1530 u = pointed_size(&vtop[-1].type);
1531 gen_opic(op);
1532 /* set to integer type */
1533 #ifdef TCC_TARGET_X86_64
1534 vtop->type.t = VT_LLONG;
1535 #else
1536 vtop->type.t = VT_INT;
1537 #endif
1538 vpushi(u);
1539 gen_op(TOK_PDIV);
1540 } else {
1541 /* exactly one pointer : must be '+' or '-'. */
1542 if (op != '-' && op != '+')
1543 error("cannot use pointers here");
1544 /* Put pointer as first operand */
1545 if (bt2 == VT_PTR) {
1546 vswap();
1547 swap(&t1, &t2);
1549 type1 = vtop[-1].type;
1550 type1.t &= ~VT_ARRAY;
1551 #ifdef TCC_TARGET_X86_64
1552 vpushll(pointed_size(&vtop[-1].type));
1553 #else
1554 /* XXX: cast to int ? (long long case) */
1555 vpushi(pointed_size(&vtop[-1].type));
1556 #endif
1557 gen_op('*');
1558 #ifdef CONFIG_TCC_BCHECK
1559 /* if evaluating constant expression, no code should be
1560 generated, so no bound check */
1561 if (tcc_state->do_bounds_check && !const_wanted) {
1562 /* if bounded pointers, we generate a special code to
1563 test bounds */
1564 if (op == '-') {
1565 vpushi(0);
1566 vswap();
1567 gen_op('-');
1569 gen_bounded_ptr_add();
1570 } else
1571 #endif
1573 gen_opic(op);
1575 /* put again type if gen_opic() swaped operands */
1576 vtop->type = type1;
1578 } else if (is_float(bt1) || is_float(bt2)) {
1579 /* compute bigger type and do implicit casts */
1580 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1581 t = VT_LDOUBLE;
1582 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1583 t = VT_DOUBLE;
1584 } else {
1585 t = VT_FLOAT;
1587 /* floats can only be used for a few operations */
1588 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1589 (op < TOK_ULT || op > TOK_GT))
1590 error("invalid operands for binary operation");
1591 goto std_op;
1592 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1593 /* cast to biggest op */
1594 t = VT_LLONG;
1595 /* convert to unsigned if it does not fit in a long long */
1596 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1597 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1598 t |= VT_UNSIGNED;
1599 goto std_op;
1600 } else {
1601 /* integer operations */
1602 t = VT_INT;
1603 /* convert to unsigned if it does not fit in an integer */
1604 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1605 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1606 t |= VT_UNSIGNED;
1607 std_op:
1608 /* XXX: currently, some unsigned operations are explicit, so
1609 we modify them here */
1610 if (t & VT_UNSIGNED) {
1611 if (op == TOK_SAR)
1612 op = TOK_SHR;
1613 else if (op == '/')
1614 op = TOK_UDIV;
1615 else if (op == '%')
1616 op = TOK_UMOD;
1617 else if (op == TOK_LT)
1618 op = TOK_ULT;
1619 else if (op == TOK_GT)
1620 op = TOK_UGT;
1621 else if (op == TOK_LE)
1622 op = TOK_ULE;
1623 else if (op == TOK_GE)
1624 op = TOK_UGE;
1626 vswap();
1627 type1.t = t;
1628 gen_cast(&type1);
1629 vswap();
1630 /* special case for shifts and long long: we keep the shift as
1631 an integer */
1632 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1633 type1.t = VT_INT;
1634 gen_cast(&type1);
1635 if (is_float(t))
1636 gen_opif(op);
1637 else
1638 gen_opic(op);
1639 if (op >= TOK_ULT && op <= TOK_GT) {
1640 /* relationnal op: the result is an int */
1641 vtop->type.t = VT_INT;
1642 } else {
1643 vtop->type.t = t;
1648 #ifndef TCC_TARGET_ARM
1649 /* generic itof for unsigned long long case */
1650 static void gen_cvt_itof1(int t)
1652 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1653 (VT_LLONG | VT_UNSIGNED)) {
1655 if (t == VT_FLOAT)
1656 vpush_global_sym(&func_old_type, TOK___floatundisf);
1657 #if LDOUBLE_SIZE != 8
1658 else if (t == VT_LDOUBLE)
1659 vpush_global_sym(&func_old_type, TOK___floatundixf);
1660 #endif
1661 else
1662 vpush_global_sym(&func_old_type, TOK___floatundidf);
1663 vrott(2);
1664 gfunc_call(1);
1665 vpushi(0);
1666 vtop->r = reg_fret(t);
1667 } else {
1668 gen_cvt_itof(t);
1671 #endif
1673 /* generic ftoi for unsigned long long case */
1674 static void gen_cvt_ftoi1(int t)
1676 int st;
1678 if (t == (VT_LLONG | VT_UNSIGNED)) {
1679 /* not handled natively */
1680 st = vtop->type.t & VT_BTYPE;
1681 if (st == VT_FLOAT)
1682 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1683 #if LDOUBLE_SIZE != 8
1684 else if (st == VT_LDOUBLE)
1685 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1686 #endif
1687 else
1688 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1689 vrott(2);
1690 gfunc_call(1);
1691 vpushi(0);
1692 vtop->r = REG_IRET;
1693 vtop->r2 = REG_LRET;
1694 } else {
1695 gen_cvt_ftoi(t);
1699 /* force char or short cast */
1700 static void force_charshort_cast(int t)
1702 int bits, dbt;
1703 dbt = t & VT_BTYPE;
1704 /* XXX: add optimization if lvalue : just change type and offset */
1705 if (dbt == VT_BYTE)
1706 bits = 8;
1707 else
1708 bits = 16;
1709 if (t & VT_UNSIGNED) {
1710 vpushi((1 << bits) - 1);
1711 gen_op('&');
1712 } else {
1713 bits = 32 - bits;
1714 vpushi(bits);
1715 gen_op(TOK_SHL);
1716 /* result must be signed or the SAR is converted to an SHL
1717 This was not the case when "t" was a signed short
1718 and the last value on the stack was an unsigned int */
1719 vtop->type.t &= ~VT_UNSIGNED;
1720 vpushi(bits);
1721 gen_op(TOK_SAR);
1725 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1726 static void gen_cast(CType *type)
1728 int sbt, dbt, sf, df, c, p;
1730 /* special delayed cast for char/short */
1731 /* XXX: in some cases (multiple cascaded casts), it may still
1732 be incorrect */
1733 if (vtop->r & VT_MUSTCAST) {
1734 vtop->r &= ~VT_MUSTCAST;
1735 force_charshort_cast(vtop->type.t);
1738 /* bitfields first get cast to ints */
1739 if (vtop->type.t & VT_BITFIELD) {
1740 gv(RC_INT);
1743 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1744 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1746 if (sbt != dbt) {
1747 sf = is_float(sbt);
1748 df = is_float(dbt);
1749 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1750 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1751 if (c) {
1752 /* constant case: we can do it now */
1753 /* XXX: in ISOC, cannot do it if error in convert */
1754 if (sbt == VT_FLOAT)
1755 vtop->c.ld = vtop->c.f;
1756 else if (sbt == VT_DOUBLE)
1757 vtop->c.ld = vtop->c.d;
1759 if (df) {
1760 if ((sbt & VT_BTYPE) == VT_LLONG) {
1761 if (sbt & VT_UNSIGNED)
1762 vtop->c.ld = vtop->c.ull;
1763 else
1764 vtop->c.ld = vtop->c.ll;
1765 } else if(!sf) {
1766 if (sbt & VT_UNSIGNED)
1767 vtop->c.ld = vtop->c.ui;
1768 else
1769 vtop->c.ld = vtop->c.i;
1772 if (dbt == VT_FLOAT)
1773 vtop->c.f = (float)vtop->c.ld;
1774 else if (dbt == VT_DOUBLE)
1775 vtop->c.d = (double)vtop->c.ld;
1776 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1777 vtop->c.ull = (unsigned long long)vtop->c.ld;
1778 } else if (sf && dbt == VT_BOOL) {
1779 vtop->c.i = (vtop->c.ld != 0);
1780 } else {
1781 if(sf)
1782 vtop->c.ll = (long long)vtop->c.ld;
1783 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1784 vtop->c.ll = vtop->c.ull;
1785 else if (sbt & VT_UNSIGNED)
1786 vtop->c.ll = vtop->c.ui;
1787 #ifdef TCC_TARGET_X86_64
1788 else if (sbt == VT_PTR)
1790 #endif
1791 else if (sbt != VT_LLONG)
1792 vtop->c.ll = vtop->c.i;
1794 if (dbt == (VT_LLONG|VT_UNSIGNED))
1795 vtop->c.ull = vtop->c.ll;
1796 else if (dbt == VT_BOOL)
1797 vtop->c.i = (vtop->c.ll != 0);
1798 else if (dbt != VT_LLONG) {
1799 int s = 0;
1800 if ((dbt & VT_BTYPE) == VT_BYTE)
1801 s = 24;
1802 else if ((dbt & VT_BTYPE) == VT_SHORT)
1803 s = 16;
1805 if(dbt & VT_UNSIGNED)
1806 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1807 else
1808 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1811 } else if (p && dbt == VT_BOOL) {
1812 vtop->r = VT_CONST;
1813 vtop->c.i = 1;
1814 } else if (!nocode_wanted) {
1815 /* non constant case: generate code */
1816 if (sf && df) {
1817 /* convert from fp to fp */
1818 gen_cvt_ftof(dbt);
1819 } else if (df) {
1820 /* convert int to fp */
1821 gen_cvt_itof1(dbt);
1822 } else if (sf) {
1823 /* convert fp to int */
1824 if (dbt == VT_BOOL) {
1825 vpushi(0);
1826 gen_op(TOK_NE);
1827 } else {
1828 /* we handle char/short/etc... with generic code */
1829 if (dbt != (VT_INT | VT_UNSIGNED) &&
1830 dbt != (VT_LLONG | VT_UNSIGNED) &&
1831 dbt != VT_LLONG)
1832 dbt = VT_INT;
1833 gen_cvt_ftoi1(dbt);
1834 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1835 /* additional cast for char/short... */
1836 vtop->type.t = dbt;
1837 gen_cast(type);
1840 #ifndef TCC_TARGET_X86_64
1841 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1842 if ((sbt & VT_BTYPE) != VT_LLONG) {
1843 /* scalar to long long */
1844 /* machine independent conversion */
1845 gv(RC_INT);
1846 /* generate high word */
1847 if (sbt == (VT_INT | VT_UNSIGNED)) {
1848 vpushi(0);
1849 gv(RC_INT);
1850 } else {
1851 if (sbt == VT_PTR) {
1852 /* cast from pointer to int before we apply
1853 shift operation, which pointers don't support*/
1854 gen_cast(&int_type);
1856 gv_dup();
1857 vpushi(31);
1858 gen_op(TOK_SAR);
1860 /* patch second register */
1861 vtop[-1].r2 = vtop->r;
1862 vpop();
1864 #else
1865 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1866 (dbt & VT_BTYPE) == VT_PTR) {
1867 /* XXX: not sure if this is perfect... need more tests */
1868 if ((sbt & VT_BTYPE) != VT_LLONG) {
1869 int r = gv(RC_INT);
1870 if (sbt != (VT_INT | VT_UNSIGNED) &&
1871 sbt != VT_PTR && sbt != VT_FUNC) {
1872 /* x86_64 specific: movslq */
1873 o(0x6348);
1874 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1877 #endif
1878 } else if (dbt == VT_BOOL) {
1879 /* scalar to bool */
1880 vpushi(0);
1881 gen_op(TOK_NE);
1882 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1883 (dbt & VT_BTYPE) == VT_SHORT) {
1884 if (sbt == VT_PTR) {
1885 vtop->type.t = VT_INT;
1886 warning("nonportable conversion from pointer to char/short");
1888 force_charshort_cast(dbt);
1889 } else if ((dbt & VT_BTYPE) == VT_INT) {
1890 /* scalar to int */
1891 if (sbt == VT_LLONG) {
1892 /* from long long: just take low order word */
1893 lexpand();
1894 vpop();
1896 /* if lvalue and single word type, nothing to do because
1897 the lvalue already contains the real type size (see
1898 VT_LVAL_xxx constants) */
1901 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1902 /* if we are casting between pointer types,
1903 we must update the VT_LVAL_xxx size */
1904 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1905 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1907 vtop->type = *type;
1910 /* return type size. Put alignment at 'a' */
1911 ST_FUNC int type_size(CType *type, int *a)
1913 Sym *s;
1914 int bt;
1916 bt = type->t & VT_BTYPE;
1917 if (bt == VT_STRUCT) {
1918 /* struct/union */
1919 s = type->ref;
1920 *a = s->r;
1921 return s->c;
1922 } else if (bt == VT_PTR) {
1923 if (type->t & VT_ARRAY) {
1924 int ts;
1926 s = type->ref;
1927 ts = type_size(&s->type, a);
1929 if (ts < 0 && s->c < 0)
1930 ts = -ts;
1932 return ts * s->c;
1933 } else {
1934 *a = PTR_SIZE;
1935 return PTR_SIZE;
1937 } else if (bt == VT_LDOUBLE) {
1938 *a = LDOUBLE_ALIGN;
1939 return LDOUBLE_SIZE;
1940 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1941 #ifdef TCC_TARGET_I386
1942 #ifdef TCC_TARGET_PE
1943 *a = 8;
1944 #else
1945 *a = 4;
1946 #endif
1947 #elif defined(TCC_TARGET_ARM)
1948 #ifdef TCC_ARM_EABI
1949 *a = 8;
1950 #else
1951 *a = 4;
1952 #endif
1953 #else
1954 *a = 8;
1955 #endif
1956 return 8;
1957 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1958 *a = 4;
1959 return 4;
1960 } else if (bt == VT_SHORT) {
1961 *a = 2;
1962 return 2;
1963 } else {
1964 /* char, void, function, _Bool */
1965 *a = 1;
1966 return 1;
1970 /* return the pointed type of t */
1971 static inline CType *pointed_type(CType *type)
1973 return &type->ref->type;
1976 /* modify type so that its it is a pointer to type. */
1977 ST_FUNC void mk_pointer(CType *type)
1979 Sym *s;
1980 s = sym_push(SYM_FIELD, type, 0, -1);
1981 type->t = VT_PTR | (type->t & ~VT_TYPE);
1982 type->ref = s;
1985 /* compare function types. OLD functions match any new functions */
1986 static int is_compatible_func(CType *type1, CType *type2)
1988 Sym *s1, *s2;
1990 s1 = type1->ref;
1991 s2 = type2->ref;
1992 if (!is_compatible_types(&s1->type, &s2->type))
1993 return 0;
1994 /* check func_call */
1995 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
1996 return 0;
1997 /* XXX: not complete */
1998 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
1999 return 1;
2000 if (s1->c != s2->c)
2001 return 0;
2002 while (s1 != NULL) {
2003 if (s2 == NULL)
2004 return 0;
2005 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2006 return 0;
2007 s1 = s1->next;
2008 s2 = s2->next;
2010 if (s2)
2011 return 0;
2012 return 1;
2015 /* return true if type1 and type2 are the same. If unqualified is
2016 true, qualifiers on the types are ignored.
2018 - enums are not checked as gcc __builtin_types_compatible_p ()
2020 static int compare_types(CType *type1, CType *type2, int unqualified)
2022 int bt1, t1, t2;
2024 t1 = type1->t & VT_TYPE;
2025 t2 = type2->t & VT_TYPE;
2026 if (unqualified) {
2027 /* strip qualifiers before comparing */
2028 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2029 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2031 /* XXX: bitfields ? */
2032 if (t1 != t2)
2033 return 0;
2034 /* test more complicated cases */
2035 bt1 = t1 & VT_BTYPE;
2036 if (bt1 == VT_PTR) {
2037 type1 = pointed_type(type1);
2038 type2 = pointed_type(type2);
2039 return is_compatible_types(type1, type2);
2040 } else if (bt1 == VT_STRUCT) {
2041 return (type1->ref == type2->ref);
2042 } else if (bt1 == VT_FUNC) {
2043 return is_compatible_func(type1, type2);
2044 } else {
2045 return 1;
2049 /* return true if type1 and type2 are exactly the same (including
2050 qualifiers).
2052 static int is_compatible_types(CType *type1, CType *type2)
2054 return compare_types(type1,type2,0);
2057 /* return true if type1 and type2 are the same (ignoring qualifiers).
2059 static int is_compatible_parameter_types(CType *type1, CType *type2)
2061 return compare_types(type1,type2,1);
2064 /* print a type. If 'varstr' is not NULL, then the variable is also
2065 printed in the type */
2066 /* XXX: union */
2067 /* XXX: add array and function pointers */
2068 static void type_to_str(char *buf, int buf_size,
2069 CType *type, const char *varstr)
2071 int bt, v, t;
2072 Sym *s, *sa;
2073 char buf1[256];
2074 const char *tstr;
2076 t = type->t & VT_TYPE;
2077 bt = t & VT_BTYPE;
2078 buf[0] = '\0';
2079 if (t & VT_CONSTANT)
2080 pstrcat(buf, buf_size, "const ");
2081 if (t & VT_VOLATILE)
2082 pstrcat(buf, buf_size, "volatile ");
2083 if (t & VT_UNSIGNED)
2084 pstrcat(buf, buf_size, "unsigned ");
2085 switch(bt) {
2086 case VT_VOID:
2087 tstr = "void";
2088 goto add_tstr;
2089 case VT_BOOL:
2090 tstr = "_Bool";
2091 goto add_tstr;
2092 case VT_BYTE:
2093 tstr = "char";
2094 goto add_tstr;
2095 case VT_SHORT:
2096 tstr = "short";
2097 goto add_tstr;
2098 case VT_INT:
2099 tstr = "int";
2100 goto add_tstr;
2101 case VT_LONG:
2102 tstr = "long";
2103 goto add_tstr;
2104 case VT_LLONG:
2105 tstr = "long long";
2106 goto add_tstr;
2107 case VT_FLOAT:
2108 tstr = "float";
2109 goto add_tstr;
2110 case VT_DOUBLE:
2111 tstr = "double";
2112 goto add_tstr;
2113 case VT_LDOUBLE:
2114 tstr = "long double";
2115 add_tstr:
2116 pstrcat(buf, buf_size, tstr);
2117 break;
2118 case VT_ENUM:
2119 case VT_STRUCT:
2120 if (bt == VT_STRUCT)
2121 tstr = "struct ";
2122 else
2123 tstr = "enum ";
2124 pstrcat(buf, buf_size, tstr);
2125 v = type->ref->v & ~SYM_STRUCT;
2126 if (v >= SYM_FIRST_ANOM)
2127 pstrcat(buf, buf_size, "<anonymous>");
2128 else
2129 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2130 break;
2131 case VT_FUNC:
2132 s = type->ref;
2133 type_to_str(buf, buf_size, &s->type, varstr);
2134 pstrcat(buf, buf_size, "(");
2135 sa = s->next;
2136 while (sa != NULL) {
2137 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2138 pstrcat(buf, buf_size, buf1);
2139 sa = sa->next;
2140 if (sa)
2141 pstrcat(buf, buf_size, ", ");
2143 pstrcat(buf, buf_size, ")");
2144 goto no_var;
2145 case VT_PTR:
2146 s = type->ref;
2147 pstrcpy(buf1, sizeof(buf1), "*");
2148 if (varstr)
2149 pstrcat(buf1, sizeof(buf1), varstr);
2150 type_to_str(buf, buf_size, &s->type, buf1);
2151 goto no_var;
2153 if (varstr) {
2154 pstrcat(buf, buf_size, " ");
2155 pstrcat(buf, buf_size, varstr);
2157 no_var: ;
2160 /* verify type compatibility to store vtop in 'dt' type, and generate
2161 casts if needed. */
2162 static void gen_assign_cast(CType *dt)
2164 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2165 char buf1[256], buf2[256];
2166 int dbt, sbt;
2168 st = &vtop->type; /* source type */
2169 dbt = dt->t & VT_BTYPE;
2170 sbt = st->t & VT_BTYPE;
2171 if (dt->t & VT_CONSTANT)
2172 warning("assignment of read-only location");
2173 switch(dbt) {
2174 case VT_PTR:
2175 /* special cases for pointers */
2176 /* '0' can also be a pointer */
2177 if (is_null_pointer(vtop))
2178 goto type_ok;
2179 /* accept implicit pointer to integer cast with warning */
2180 if (is_integer_btype(sbt)) {
2181 warning("assignment makes pointer from integer without a cast");
2182 goto type_ok;
2184 type1 = pointed_type(dt);
2185 /* a function is implicitely a function pointer */
2186 if (sbt == VT_FUNC) {
2187 if ((type1->t & VT_BTYPE) != VT_VOID &&
2188 !is_compatible_types(pointed_type(dt), st))
2189 warning("assignment from incompatible pointer type");
2190 goto type_ok;
2192 if (sbt != VT_PTR)
2193 goto error;
2194 type2 = pointed_type(st);
2195 if ((type1->t & VT_BTYPE) == VT_VOID ||
2196 (type2->t & VT_BTYPE) == VT_VOID) {
2197 /* void * can match anything */
2198 } else {
2199 /* exact type match, except for unsigned */
2200 tmp_type1 = *type1;
2201 tmp_type2 = *type2;
2202 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2203 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2204 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2205 warning("assignment from incompatible pointer type");
2207 /* check const and volatile */
2208 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2209 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2210 warning("assignment discards qualifiers from pointer target type");
2211 break;
2212 case VT_BYTE:
2213 case VT_SHORT:
2214 case VT_INT:
2215 case VT_LLONG:
2216 if (sbt == VT_PTR || sbt == VT_FUNC) {
2217 warning("assignment makes integer from pointer without a cast");
2219 /* XXX: more tests */
2220 break;
2221 case VT_STRUCT:
2222 tmp_type1 = *dt;
2223 tmp_type2 = *st;
2224 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2225 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2226 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2227 error:
2228 type_to_str(buf1, sizeof(buf1), st, NULL);
2229 type_to_str(buf2, sizeof(buf2), dt, NULL);
2230 error("cannot cast '%s' to '%s'", buf1, buf2);
2232 break;
2234 type_ok:
2235 gen_cast(dt);
2238 /* store vtop in lvalue pushed on stack */
2239 ST_FUNC void vstore(void)
2241 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2243 ft = vtop[-1].type.t;
2244 sbt = vtop->type.t & VT_BTYPE;
2245 dbt = ft & VT_BTYPE;
2246 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2247 (sbt == VT_INT && dbt == VT_SHORT)) {
2248 /* optimize char/short casts */
2249 delayed_cast = VT_MUSTCAST;
2250 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2251 /* XXX: factorize */
2252 if (ft & VT_CONSTANT)
2253 warning("assignment of read-only location");
2254 } else {
2255 delayed_cast = 0;
2256 if (!(ft & VT_BITFIELD))
2257 gen_assign_cast(&vtop[-1].type);
2260 if (sbt == VT_STRUCT) {
2261 /* if structure, only generate pointer */
2262 /* structure assignment : generate memcpy */
2263 /* XXX: optimize if small size */
2264 if (!nocode_wanted) {
2265 size = type_size(&vtop->type, &align);
2267 /* destination */
2268 vswap();
2269 vtop->type.t = VT_PTR;
2270 gaddrof();
2272 /* address of memcpy() */
2273 #ifdef TCC_ARM_EABI
2274 if(!(align & 7))
2275 vpush_global_sym(&func_old_type, TOK_memcpy8);
2276 else if(!(align & 3))
2277 vpush_global_sym(&func_old_type, TOK_memcpy4);
2278 else
2279 #endif
2280 vpush_global_sym(&func_old_type, TOK_memcpy);
2282 vswap();
2283 /* source */
2284 vpushv(vtop - 2);
2285 vtop->type.t = VT_PTR;
2286 gaddrof();
2287 /* type size */
2288 vpushi(size);
2289 gfunc_call(3);
2290 } else {
2291 vswap();
2292 vpop();
2294 /* leave source on stack */
2295 } else if (ft & VT_BITFIELD) {
2296 /* bitfield store handling */
2297 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2298 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2299 /* remove bit field info to avoid loops */
2300 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2302 /* duplicate source into other register */
2303 gv_dup();
2304 vswap();
2305 vrott(3);
2307 if((ft & VT_BTYPE) == VT_BOOL) {
2308 gen_cast(&vtop[-1].type);
2309 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2312 /* duplicate destination */
2313 vdup();
2314 vtop[-1] = vtop[-2];
2316 /* mask and shift source */
2317 if((ft & VT_BTYPE) != VT_BOOL) {
2318 if((ft & VT_BTYPE) == VT_LLONG) {
2319 vpushll((1ULL << bit_size) - 1ULL);
2320 } else {
2321 vpushi((1 << bit_size) - 1);
2323 gen_op('&');
2325 vpushi(bit_pos);
2326 gen_op(TOK_SHL);
2327 /* load destination, mask and or with source */
2328 vswap();
2329 if((ft & VT_BTYPE) == VT_LLONG) {
2330 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2331 } else {
2332 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2334 gen_op('&');
2335 gen_op('|');
2336 /* store result */
2337 vstore();
2339 /* pop off shifted source from "duplicate source..." above */
2340 vpop();
2342 } else {
2343 #ifdef CONFIG_TCC_BCHECK
2344 /* bound check case */
2345 if (vtop[-1].r & VT_MUSTBOUND) {
2346 vswap();
2347 gbound();
2348 vswap();
2350 #endif
2351 if (!nocode_wanted) {
2352 rc = RC_INT;
2353 if (is_float(ft)) {
2354 rc = RC_FLOAT;
2355 #ifdef TCC_TARGET_X86_64
2356 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2357 rc = RC_ST0;
2359 #endif
2361 r = gv(rc); /* generate value */
2362 /* if lvalue was saved on stack, must read it */
2363 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2364 SValue sv;
2365 t = get_reg(RC_INT);
2366 #ifdef TCC_TARGET_X86_64
2367 sv.type.t = VT_PTR;
2368 #else
2369 sv.type.t = VT_INT;
2370 #endif
2371 sv.r = VT_LOCAL | VT_LVAL;
2372 sv.c.ul = vtop[-1].c.ul;
2373 load(t, &sv);
2374 vtop[-1].r = t | VT_LVAL;
2376 store(r, vtop - 1);
2377 #ifndef TCC_TARGET_X86_64
2378 /* two word case handling : store second register at word + 4 */
2379 if ((ft & VT_BTYPE) == VT_LLONG) {
2380 vswap();
2381 /* convert to int to increment easily */
2382 vtop->type.t = VT_INT;
2383 gaddrof();
2384 vpushi(4);
2385 gen_op('+');
2386 vtop->r |= VT_LVAL;
2387 vswap();
2388 /* XXX: it works because r2 is spilled last ! */
2389 store(vtop->r2, vtop - 1);
2391 #endif
2393 vswap();
2394 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2395 vtop->r |= delayed_cast;
2399 /* post defines POST/PRE add. c is the token ++ or -- */
2400 ST_FUNC void inc(int post, int c)
2402 test_lvalue();
2403 vdup(); /* save lvalue */
2404 if (post) {
2405 gv_dup(); /* duplicate value */
2406 vrotb(3);
2407 vrotb(3);
2409 /* add constant */
2410 vpushi(c - TOK_MID);
2411 gen_op('+');
2412 vstore(); /* store value */
2413 if (post)
2414 vpop(); /* if post op, return saved value */
2417 /* Parse GNUC __attribute__ extension. Currently, the following
2418 extensions are recognized:
2419 - aligned(n) : set data/function alignment.
2420 - packed : force data alignment to 1
2421 - section(x) : generate data/code in this section.
2422 - unused : currently ignored, but may be used someday.
2423 - regparm(n) : pass function parameters in registers (i386 only)
2425 static void parse_attribute(AttributeDef *ad)
2427 int t, n;
2429 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2430 next();
2431 skip('(');
2432 skip('(');
2433 while (tok != ')') {
2434 if (tok < TOK_IDENT)
2435 expect("attribute name");
2436 t = tok;
2437 next();
2438 switch(t) {
2439 case TOK_SECTION1:
2440 case TOK_SECTION2:
2441 skip('(');
2442 if (tok != TOK_STR)
2443 expect("section name");
2444 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2445 next();
2446 skip(')');
2447 break;
2448 case TOK_ALIGNED1:
2449 case TOK_ALIGNED2:
2450 if (tok == '(') {
2451 next();
2452 n = expr_const();
2453 if (n <= 0 || (n & (n - 1)) != 0)
2454 error("alignment must be a positive power of two");
2455 skip(')');
2456 } else {
2457 n = MAX_ALIGN;
2459 ad->aligned = n;
2460 break;
2461 case TOK_PACKED1:
2462 case TOK_PACKED2:
2463 ad->packed = 1;
2464 break;
2465 case TOK_UNUSED1:
2466 case TOK_UNUSED2:
2467 /* currently, no need to handle it because tcc does not
2468 track unused objects */
2469 break;
2470 case TOK_NORETURN1:
2471 case TOK_NORETURN2:
2472 /* currently, no need to handle it because tcc does not
2473 track unused objects */
2474 break;
2475 case TOK_CDECL1:
2476 case TOK_CDECL2:
2477 case TOK_CDECL3:
2478 ad->func_call = FUNC_CDECL;
2479 break;
2480 case TOK_STDCALL1:
2481 case TOK_STDCALL2:
2482 case TOK_STDCALL3:
2483 ad->func_call = FUNC_STDCALL;
2484 break;
2485 #ifdef TCC_TARGET_I386
2486 case TOK_REGPARM1:
2487 case TOK_REGPARM2:
2488 skip('(');
2489 n = expr_const();
2490 if (n > 3)
2491 n = 3;
2492 else if (n < 0)
2493 n = 0;
2494 if (n > 0)
2495 ad->func_call = FUNC_FASTCALL1 + n - 1;
2496 skip(')');
2497 break;
2498 case TOK_FASTCALL1:
2499 case TOK_FASTCALL2:
2500 case TOK_FASTCALL3:
2501 ad->func_call = FUNC_FASTCALLW;
2502 break;
2503 #endif
2504 case TOK_MODE:
2505 skip('(');
2506 switch(tok) {
2507 case TOK_MODE_DI:
2508 ad->mode = VT_LLONG + 1;
2509 break;
2510 case TOK_MODE_HI:
2511 ad->mode = VT_SHORT + 1;
2512 break;
2513 case TOK_MODE_SI:
2514 ad->mode = VT_INT + 1;
2515 break;
2516 default:
2517 warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2518 break;
2520 next();
2521 skip(')');
2522 break;
2523 case TOK_DLLEXPORT:
2524 ad->func_export = 1;
2525 break;
2526 case TOK_DLLIMPORT:
2527 ad->func_import = 1;
2528 break;
2529 default:
2530 if (tcc_state->warn_unsupported)
2531 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2532 /* skip parameters */
2533 if (tok == '(') {
2534 int parenthesis = 0;
2535 do {
2536 if (tok == '(')
2537 parenthesis++;
2538 else if (tok == ')')
2539 parenthesis--;
2540 next();
2541 } while (parenthesis && tok != -1);
2543 break;
2545 if (tok != ',')
2546 break;
2547 next();
2549 skip(')');
2550 skip(')');
2554 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2555 static void struct_decl(CType *type, int u)
2557 int a, v, size, align, maxalign, c, offset;
2558 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2559 Sym *s, *ss, *ass, **ps;
2560 AttributeDef ad;
2561 CType type1, btype;
2563 a = tok; /* save decl type */
2564 next();
2565 if (tok != '{') {
2566 v = tok;
2567 next();
2568 /* struct already defined ? return it */
2569 if (v < TOK_IDENT)
2570 expect("struct/union/enum name");
2571 s = struct_find(v);
2572 if (s) {
2573 if (s->type.t != a)
2574 error("invalid type");
2575 goto do_decl;
2577 } else {
2578 v = anon_sym++;
2580 type1.t = a;
2581 /* we put an undefined size for struct/union */
2582 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2583 s->r = 0; /* default alignment is zero as gcc */
2584 /* put struct/union/enum name in type */
2585 do_decl:
2586 type->t = u;
2587 type->ref = s;
2589 if (tok == '{') {
2590 next();
2591 if (s->c != -1)
2592 error("struct/union/enum already defined");
2593 /* cannot be empty */
2594 c = 0;
2595 /* non empty enums are not allowed */
2596 if (a == TOK_ENUM) {
2597 for(;;) {
2598 v = tok;
2599 if (v < TOK_UIDENT)
2600 expect("identifier");
2601 next();
2602 if (tok == '=') {
2603 next();
2604 c = expr_const();
2606 /* enum symbols have static storage */
2607 ss = sym_push(v, &int_type, VT_CONST, c);
2608 ss->type.t |= VT_STATIC;
2609 if (tok != ',')
2610 break;
2611 next();
2612 c++;
2613 /* NOTE: we accept a trailing comma */
2614 if (tok == '}')
2615 break;
2617 skip('}');
2618 } else {
2619 maxalign = 1;
2620 ps = &s->next;
2621 prevbt = VT_INT;
2622 bit_pos = 0;
2623 offset = 0;
2624 while (tok != '}') {
2625 parse_btype(&btype, &ad);
2626 while (1) {
2627 bit_size = -1;
2628 v = 0;
2629 type1 = btype;
2630 if (tok != ':') {
2631 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2632 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2633 expect("identifier");
2634 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2635 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2636 error("invalid type for '%s'",
2637 get_tok_str(v, NULL));
2639 if (tok == ':') {
2640 next();
2641 bit_size = expr_const();
2642 /* XXX: handle v = 0 case for messages */
2643 if (bit_size < 0)
2644 error("negative width in bit-field '%s'",
2645 get_tok_str(v, NULL));
2646 if (v && bit_size == 0)
2647 error("zero width for bit-field '%s'",
2648 get_tok_str(v, NULL));
2650 size = type_size(&type1, &align);
2651 if (ad.aligned) {
2652 if (align < ad.aligned)
2653 align = ad.aligned;
2654 } else if (ad.packed) {
2655 align = 1;
2656 } else if (*tcc_state->pack_stack_ptr) {
2657 if (align > *tcc_state->pack_stack_ptr)
2658 align = *tcc_state->pack_stack_ptr;
2660 lbit_pos = 0;
2661 if (bit_size >= 0) {
2662 bt = type1.t & VT_BTYPE;
2663 if (bt != VT_INT &&
2664 bt != VT_BYTE &&
2665 bt != VT_SHORT &&
2666 bt != VT_BOOL &&
2667 bt != VT_ENUM &&
2668 bt != VT_LLONG)
2669 error("bitfields must have scalar type");
2670 bsize = size * 8;
2671 if (bit_size > bsize) {
2672 error("width of '%s' exceeds its type",
2673 get_tok_str(v, NULL));
2674 } else if (bit_size == bsize) {
2675 /* no need for bit fields */
2676 bit_pos = 0;
2677 } else if (bit_size == 0) {
2678 /* XXX: what to do if only padding in a
2679 structure ? */
2680 /* zero size: means to pad */
2681 bit_pos = 0;
2682 } else {
2683 /* we do not have enough room ?
2684 did the type change?
2685 is it a union? */
2686 if ((bit_pos + bit_size) > bsize ||
2687 bt != prevbt || a == TOK_UNION)
2688 bit_pos = 0;
2689 lbit_pos = bit_pos;
2690 /* XXX: handle LSB first */
2691 type1.t |= VT_BITFIELD |
2692 (bit_pos << VT_STRUCT_SHIFT) |
2693 (bit_size << (VT_STRUCT_SHIFT + 6));
2694 bit_pos += bit_size;
2696 prevbt = bt;
2697 } else {
2698 bit_pos = 0;
2700 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2701 /* add new memory data only if starting
2702 bit field */
2703 if (lbit_pos == 0) {
2704 if (a == TOK_STRUCT) {
2705 c = (c + align - 1) & -align;
2706 offset = c;
2707 if (size > 0)
2708 c += size;
2709 } else {
2710 offset = 0;
2711 if (size > c)
2712 c = size;
2714 if (align > maxalign)
2715 maxalign = align;
2717 #if 0
2718 printf("add field %s offset=%d",
2719 get_tok_str(v, NULL), offset);
2720 if (type1.t & VT_BITFIELD) {
2721 printf(" pos=%d size=%d",
2722 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2723 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2725 printf("\n");
2726 #endif
2728 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2729 ass = type1.ref;
2730 while ((ass = ass->next) != NULL) {
2731 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2732 *ps = ss;
2733 ps = &ss->next;
2735 } else if (v) {
2736 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2737 *ps = ss;
2738 ps = &ss->next;
2740 if (tok == ';' || tok == TOK_EOF)
2741 break;
2742 skip(',');
2744 skip(';');
2746 skip('}');
2747 /* store size and alignment */
2748 s->c = (c + maxalign - 1) & -maxalign;
2749 s->r = maxalign;
2754 /* return 0 if no type declaration. otherwise, return the basic type
2755 and skip it.
2757 static int parse_btype(CType *type, AttributeDef *ad)
2759 int t, u, type_found, typespec_found, typedef_found;
2760 Sym *s;
2761 CType type1;
2763 memset(ad, 0, sizeof(AttributeDef));
2764 type_found = 0;
2765 typespec_found = 0;
2766 typedef_found = 0;
2767 t = 0;
2768 while(1) {
2769 switch(tok) {
2770 case TOK_EXTENSION:
2771 /* currently, we really ignore extension */
2772 next();
2773 continue;
2775 /* basic types */
2776 case TOK_CHAR:
2777 u = VT_BYTE;
2778 basic_type:
2779 next();
2780 basic_type1:
2781 if ((t & VT_BTYPE) != 0)
2782 error("too many basic types");
2783 t |= u;
2784 typespec_found = 1;
2785 break;
2786 case TOK_VOID:
2787 u = VT_VOID;
2788 goto basic_type;
2789 case TOK_SHORT:
2790 u = VT_SHORT;
2791 goto basic_type;
2792 case TOK_INT:
2793 next();
2794 typespec_found = 1;
2795 break;
2796 case TOK_LONG:
2797 next();
2798 if ((t & VT_BTYPE) == VT_DOUBLE) {
2799 #ifndef TCC_TARGET_PE
2800 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2801 #endif
2802 } else if ((t & VT_BTYPE) == VT_LONG) {
2803 t = (t & ~VT_BTYPE) | VT_LLONG;
2804 } else {
2805 u = VT_LONG;
2806 goto basic_type1;
2808 break;
2809 case TOK_BOOL:
2810 u = VT_BOOL;
2811 goto basic_type;
2812 case TOK_FLOAT:
2813 u = VT_FLOAT;
2814 goto basic_type;
2815 case TOK_DOUBLE:
2816 next();
2817 if ((t & VT_BTYPE) == VT_LONG) {
2818 #ifdef TCC_TARGET_PE
2819 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2820 #else
2821 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2822 #endif
2823 } else {
2824 u = VT_DOUBLE;
2825 goto basic_type1;
2827 break;
2828 case TOK_ENUM:
2829 struct_decl(&type1, VT_ENUM);
2830 basic_type2:
2831 u = type1.t;
2832 type->ref = type1.ref;
2833 goto basic_type1;
2834 case TOK_STRUCT:
2835 case TOK_UNION:
2836 struct_decl(&type1, VT_STRUCT);
2837 goto basic_type2;
2839 /* type modifiers */
2840 case TOK_CONST1:
2841 case TOK_CONST2:
2842 case TOK_CONST3:
2843 t |= VT_CONSTANT;
2844 next();
2845 break;
2846 case TOK_VOLATILE1:
2847 case TOK_VOLATILE2:
2848 case TOK_VOLATILE3:
2849 t |= VT_VOLATILE;
2850 next();
2851 break;
2852 case TOK_SIGNED1:
2853 case TOK_SIGNED2:
2854 case TOK_SIGNED3:
2855 typespec_found = 1;
2856 t |= VT_SIGNED;
2857 next();
2858 break;
2859 case TOK_REGISTER:
2860 case TOK_AUTO:
2861 case TOK_RESTRICT1:
2862 case TOK_RESTRICT2:
2863 case TOK_RESTRICT3:
2864 next();
2865 break;
2866 case TOK_UNSIGNED:
2867 t |= VT_UNSIGNED;
2868 next();
2869 typespec_found = 1;
2870 break;
2872 /* storage */
2873 case TOK_EXTERN:
2874 t |= VT_EXTERN;
2875 next();
2876 break;
2877 case TOK_STATIC:
2878 t |= VT_STATIC;
2879 next();
2880 break;
2881 case TOK_TYPEDEF:
2882 t |= VT_TYPEDEF;
2883 next();
2884 break;
2885 case TOK_INLINE1:
2886 case TOK_INLINE2:
2887 case TOK_INLINE3:
2888 t |= VT_INLINE;
2889 next();
2890 break;
2892 /* GNUC attribute */
2893 case TOK_ATTRIBUTE1:
2894 case TOK_ATTRIBUTE2:
2895 parse_attribute(ad);
2896 if (ATTR_MODE(ad)) {
2897 u = ATTR_MODE(ad) -1;
2898 t = (t & ~VT_BTYPE) | u;
2900 break;
2901 /* GNUC typeof */
2902 case TOK_TYPEOF1:
2903 case TOK_TYPEOF2:
2904 case TOK_TYPEOF3:
2905 next();
2906 parse_expr_type(&type1);
2907 goto basic_type2;
2908 default:
2909 if (typespec_found || typedef_found)
2910 goto the_end;
2911 s = sym_find(tok);
2912 if (!s || !(s->type.t & VT_TYPEDEF))
2913 goto the_end;
2914 typedef_found = 1;
2915 t |= (s->type.t & ~VT_TYPEDEF);
2916 type->ref = s->type.ref;
2917 if (s->r) {
2918 /* get attributes from typedef */
2919 if (0 == ad->aligned)
2920 ad->aligned = FUNC_ALIGN(s->r);
2921 if (0 == ad->func_call)
2922 ad->func_call = FUNC_CALL(s->r);
2923 ad->packed |= FUNC_PACKED(s->r);
2925 next();
2926 typespec_found = 1;
2927 break;
2929 type_found = 1;
2931 the_end:
2932 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2933 error("signed and unsigned modifier");
2934 if (tcc_state->char_is_unsigned) {
2935 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2936 t |= VT_UNSIGNED;
2938 t &= ~VT_SIGNED;
2940 /* long is never used as type */
2941 if ((t & VT_BTYPE) == VT_LONG)
2942 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2943 t = (t & ~VT_BTYPE) | VT_INT;
2944 #else
2945 t = (t & ~VT_BTYPE) | VT_LLONG;
2946 #endif
2947 type->t = t;
2948 return type_found;
2951 /* convert a function parameter type (array to pointer and function to
2952 function pointer) */
2953 static inline void convert_parameter_type(CType *pt)
2955 /* remove const and volatile qualifiers (XXX: const could be used
2956 to indicate a const function parameter */
2957 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2958 /* array must be transformed to pointer according to ANSI C */
2959 pt->t &= ~VT_ARRAY;
2960 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2961 mk_pointer(pt);
2965 static void post_type(CType *type, AttributeDef *ad)
2967 int n, l, t1, arg_size, align;
2968 Sym **plast, *s, *first;
2969 AttributeDef ad1;
2970 CType pt;
2972 if (tok == '(') {
2973 /* function declaration */
2974 next();
2975 l = 0;
2976 first = NULL;
2977 plast = &first;
2978 arg_size = 0;
2979 if (tok != ')') {
2980 for(;;) {
2981 /* read param name and compute offset */
2982 if (l != FUNC_OLD) {
2983 if (!parse_btype(&pt, &ad1)) {
2984 if (l) {
2985 error("invalid type");
2986 } else {
2987 l = FUNC_OLD;
2988 goto old_proto;
2991 l = FUNC_NEW;
2992 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2993 break;
2994 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2995 if ((pt.t & VT_BTYPE) == VT_VOID)
2996 error("parameter declared as void");
2997 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
2998 } else {
2999 old_proto:
3000 n = tok;
3001 if (n < TOK_UIDENT)
3002 expect("identifier");
3003 pt.t = VT_INT;
3004 next();
3006 convert_parameter_type(&pt);
3007 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3008 *plast = s;
3009 plast = &s->next;
3010 if (tok == ')')
3011 break;
3012 skip(',');
3013 if (l == FUNC_NEW && tok == TOK_DOTS) {
3014 l = FUNC_ELLIPSIS;
3015 next();
3016 break;
3020 /* if no parameters, then old type prototype */
3021 if (l == 0)
3022 l = FUNC_OLD;
3023 skip(')');
3024 t1 = type->t & VT_STORAGE;
3025 /* NOTE: const is ignored in returned type as it has a special
3026 meaning in gcc / C++ */
3027 type->t &= ~(VT_STORAGE | VT_CONSTANT);
3028 post_type(type, ad);
3029 /* we push a anonymous symbol which will contain the function prototype */
3030 ad->func_args = arg_size;
3031 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3032 s->next = first;
3033 type->t = t1 | VT_FUNC;
3034 type->ref = s;
3035 } else if (tok == '[') {
3036 /* array definition */
3037 next();
3038 if (tok == TOK_RESTRICT1)
3039 next();
3040 n = -1;
3041 if (tok != ']') {
3042 n = expr_const();
3043 if (n < 0)
3044 error("invalid array size");
3046 skip(']');
3047 /* parse next post type */
3048 t1 = type->t & VT_STORAGE;
3049 type->t &= ~VT_STORAGE;
3050 post_type(type, ad);
3052 /* we push a anonymous symbol which will contain the array
3053 element type */
3054 s = sym_push(SYM_FIELD, type, 0, n);
3055 type->t = t1 | VT_ARRAY | VT_PTR;
3056 type->ref = s;
3060 /* Parse a type declaration (except basic type), and return the type
3061 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3062 expected. 'type' should contain the basic type. 'ad' is the
3063 attribute definition of the basic type. It can be modified by
3064 type_decl().
3066 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3068 Sym *s;
3069 CType type1, *type2;
3070 int qualifiers;
3072 while (tok == '*') {
3073 qualifiers = 0;
3074 redo:
3075 next();
3076 switch(tok) {
3077 case TOK_CONST1:
3078 case TOK_CONST2:
3079 case TOK_CONST3:
3080 qualifiers |= VT_CONSTANT;
3081 goto redo;
3082 case TOK_VOLATILE1:
3083 case TOK_VOLATILE2:
3084 case TOK_VOLATILE3:
3085 qualifiers |= VT_VOLATILE;
3086 goto redo;
3087 case TOK_RESTRICT1:
3088 case TOK_RESTRICT2:
3089 case TOK_RESTRICT3:
3090 goto redo;
3092 mk_pointer(type);
3093 type->t |= qualifiers;
3096 /* XXX: clarify attribute handling */
3097 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3098 parse_attribute(ad);
3100 /* recursive type */
3101 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3102 type1.t = 0; /* XXX: same as int */
3103 if (tok == '(') {
3104 next();
3105 /* XXX: this is not correct to modify 'ad' at this point, but
3106 the syntax is not clear */
3107 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3108 parse_attribute(ad);
3109 type_decl(&type1, ad, v, td);
3110 skip(')');
3111 } else {
3112 /* type identifier */
3113 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3114 *v = tok;
3115 next();
3116 } else {
3117 if (!(td & TYPE_ABSTRACT))
3118 expect("identifier");
3119 *v = 0;
3122 post_type(type, ad);
3123 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3124 parse_attribute(ad);
3125 if (!type1.t)
3126 return;
3127 /* append type at the end of type1 */
3128 type2 = &type1;
3129 for(;;) {
3130 s = type2->ref;
3131 type2 = &s->type;
3132 if (!type2->t) {
3133 *type2 = *type;
3134 break;
3137 *type = type1;
3140 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3141 ST_FUNC int lvalue_type(int t)
3143 int bt, r;
3144 r = VT_LVAL;
3145 bt = t & VT_BTYPE;
3146 if (bt == VT_BYTE || bt == VT_BOOL)
3147 r |= VT_LVAL_BYTE;
3148 else if (bt == VT_SHORT)
3149 r |= VT_LVAL_SHORT;
3150 else
3151 return r;
3152 if (t & VT_UNSIGNED)
3153 r |= VT_LVAL_UNSIGNED;
3154 return r;
3157 /* indirection with full error checking and bound check */
3158 ST_FUNC void indir(void)
3160 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3161 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3162 return;
3163 expect("pointer");
3165 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3166 gv(RC_INT);
3167 vtop->type = *pointed_type(&vtop->type);
3168 /* Arrays and functions are never lvalues */
3169 if (!(vtop->type.t & VT_ARRAY)
3170 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3171 vtop->r |= lvalue_type(vtop->type.t);
3172 /* if bound checking, the referenced pointer must be checked */
3173 #ifdef CONFIG_TCC_BCHECK
3174 if (tcc_state->do_bounds_check)
3175 vtop->r |= VT_MUSTBOUND;
3176 #endif
3180 /* pass a parameter to a function and do type checking and casting */
3181 static void gfunc_param_typed(Sym *func, Sym *arg)
3183 int func_type;
3184 CType type;
3186 func_type = func->c;
3187 if (func_type == FUNC_OLD ||
3188 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3189 /* default casting : only need to convert float to double */
3190 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3191 type.t = VT_DOUBLE;
3192 gen_cast(&type);
3194 } else if (arg == NULL) {
3195 error("too many arguments to function");
3196 } else {
3197 type = arg->type;
3198 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3199 gen_assign_cast(&type);
3203 /* parse an expression of the form '(type)' or '(expr)' and return its
3204 type */
3205 static void parse_expr_type(CType *type)
3207 int n;
3208 AttributeDef ad;
3210 skip('(');
3211 if (parse_btype(type, &ad)) {
3212 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3213 } else {
3214 expr_type(type);
3216 skip(')');
3219 static void parse_type(CType *type)
3221 AttributeDef ad;
3222 int n;
3224 if (!parse_btype(type, &ad)) {
3225 expect("type");
3227 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3230 static void vpush_tokc(int t)
3232 CType type;
3233 type.t = t;
3234 type.ref = 0;
3235 vsetc(&type, VT_CONST, &tokc);
3238 ST_FUNC void unary(void)
3240 int n, t, align, size, r;
3241 CType type;
3242 Sym *s;
3243 AttributeDef ad;
3245 /* XXX: GCC 2.95.3 does not generate a table although it should be
3246 better here */
3247 tok_next:
3248 switch(tok) {
3249 case TOK_EXTENSION:
3250 next();
3251 goto tok_next;
3252 case TOK_CINT:
3253 case TOK_CCHAR:
3254 case TOK_LCHAR:
3255 vpushi(tokc.i);
3256 next();
3257 break;
3258 case TOK_CUINT:
3259 vpush_tokc(VT_INT | VT_UNSIGNED);
3260 next();
3261 break;
3262 case TOK_CLLONG:
3263 vpush_tokc(VT_LLONG);
3264 next();
3265 break;
3266 case TOK_CULLONG:
3267 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3268 next();
3269 break;
3270 case TOK_CFLOAT:
3271 vpush_tokc(VT_FLOAT);
3272 next();
3273 break;
3274 case TOK_CDOUBLE:
3275 vpush_tokc(VT_DOUBLE);
3276 next();
3277 break;
3278 case TOK_CLDOUBLE:
3279 vpush_tokc(VT_LDOUBLE);
3280 next();
3281 break;
3282 case TOK___FUNCTION__:
3283 if (!gnu_ext)
3284 goto tok_identifier;
3285 /* fall thru */
3286 case TOK___FUNC__:
3288 void *ptr;
3289 int len;
3290 /* special function name identifier */
3291 len = strlen(funcname) + 1;
3292 /* generate char[len] type */
3293 type.t = VT_BYTE;
3294 mk_pointer(&type);
3295 type.t |= VT_ARRAY;
3296 type.ref->c = len;
3297 vpush_ref(&type, data_section, data_section->data_offset, len);
3298 ptr = section_ptr_add(data_section, len);
3299 memcpy(ptr, funcname, len);
3300 next();
3302 break;
3303 case TOK_LSTR:
3304 #ifdef TCC_TARGET_PE
3305 t = VT_SHORT | VT_UNSIGNED;
3306 #else
3307 t = VT_INT;
3308 #endif
3309 goto str_init;
3310 case TOK_STR:
3311 /* string parsing */
3312 t = VT_BYTE;
3313 str_init:
3314 if (tcc_state->warn_write_strings)
3315 t |= VT_CONSTANT;
3316 type.t = t;
3317 mk_pointer(&type);
3318 type.t |= VT_ARRAY;
3319 memset(&ad, 0, sizeof(AttributeDef));
3320 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3321 break;
3322 case '(':
3323 next();
3324 /* cast ? */
3325 if (parse_btype(&type, &ad)) {
3326 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3327 skip(')');
3328 /* check ISOC99 compound literal */
3329 if (tok == '{') {
3330 /* data is allocated locally by default */
3331 if (global_expr)
3332 r = VT_CONST;
3333 else
3334 r = VT_LOCAL;
3335 /* all except arrays are lvalues */
3336 if (!(type.t & VT_ARRAY))
3337 r |= lvalue_type(type.t);
3338 memset(&ad, 0, sizeof(AttributeDef));
3339 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3340 } else {
3341 unary();
3342 gen_cast(&type);
3344 } else if (tok == '{') {
3345 /* save all registers */
3346 save_regs(0);
3347 /* statement expression : we do not accept break/continue
3348 inside as GCC does */
3349 block(NULL, NULL, NULL, NULL, 0, 1);
3350 skip(')');
3351 } else {
3352 gexpr();
3353 skip(')');
3355 break;
3356 case '*':
3357 next();
3358 unary();
3359 indir();
3360 break;
3361 case '&':
3362 next();
3363 unary();
3364 /* functions names must be treated as function pointers,
3365 except for unary '&' and sizeof. Since we consider that
3366 functions are not lvalues, we only have to handle it
3367 there and in function calls. */
3368 /* arrays can also be used although they are not lvalues */
3369 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3370 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3371 test_lvalue();
3372 mk_pointer(&vtop->type);
3373 gaddrof();
3374 break;
3375 case '!':
3376 next();
3377 unary();
3378 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3379 CType boolean;
3380 boolean.t = VT_BOOL;
3381 gen_cast(&boolean);
3382 vtop->c.i = !vtop->c.i;
3383 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3384 vtop->c.i = vtop->c.i ^ 1;
3385 else {
3386 save_regs(1);
3387 vseti(VT_JMP, gtst(1, 0));
3389 break;
3390 case '~':
3391 next();
3392 unary();
3393 vpushi(-1);
3394 gen_op('^');
3395 break;
3396 case '+':
3397 next();
3398 /* in order to force cast, we add zero */
3399 unary();
3400 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3401 error("pointer not accepted for unary plus");
3402 vpushi(0);
3403 gen_op('+');
3404 break;
3405 case TOK_SIZEOF:
3406 case TOK_ALIGNOF1:
3407 case TOK_ALIGNOF2:
3408 t = tok;
3409 next();
3410 if (tok == '(') {
3411 parse_expr_type(&type);
3412 } else {
3413 unary_type(&type);
3415 size = type_size(&type, &align);
3416 if (t == TOK_SIZEOF) {
3417 if (size < 0)
3418 error("sizeof applied to an incomplete type");
3419 vpushi(size);
3420 } else {
3421 vpushi(align);
3423 vtop->type.t |= VT_UNSIGNED;
3424 break;
3426 case TOK_builtin_types_compatible_p:
3428 CType type1, type2;
3429 next();
3430 skip('(');
3431 parse_type(&type1);
3432 skip(',');
3433 parse_type(&type2);
3434 skip(')');
3435 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3436 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3437 vpushi(is_compatible_types(&type1, &type2));
3439 break;
3440 case TOK_builtin_constant_p:
3442 int saved_nocode_wanted, res;
3443 next();
3444 skip('(');
3445 saved_nocode_wanted = nocode_wanted;
3446 nocode_wanted = 1;
3447 gexpr();
3448 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3449 vpop();
3450 nocode_wanted = saved_nocode_wanted;
3451 skip(')');
3452 vpushi(res);
3454 break;
3455 case TOK_builtin_frame_address:
3457 CType type;
3458 next();
3459 skip('(');
3460 if (tok != TOK_CINT) {
3461 error("__builtin_frame_address only takes integers");
3463 if (tokc.i != 0) {
3464 error("TCC only supports __builtin_frame_address(0)");
3466 next();
3467 skip(')');
3468 type.t = VT_VOID;
3469 mk_pointer(&type);
3470 vset(&type, VT_LOCAL, 0);
3472 break;
3473 #ifdef TCC_TARGET_X86_64
3474 case TOK_builtin_malloc:
3475 tok = TOK_malloc;
3476 goto tok_identifier;
3477 case TOK_builtin_free:
3478 tok = TOK_free;
3479 goto tok_identifier;
3480 #endif
3481 case TOK_INC:
3482 case TOK_DEC:
3483 t = tok;
3484 next();
3485 unary();
3486 inc(0, t);
3487 break;
3488 case '-':
3489 next();
3490 vpushi(0);
3491 unary();
3492 gen_op('-');
3493 break;
3494 case TOK_LAND:
3495 if (!gnu_ext)
3496 goto tok_identifier;
3497 next();
3498 /* allow to take the address of a label */
3499 if (tok < TOK_UIDENT)
3500 expect("label identifier");
3501 s = label_find(tok);
3502 if (!s) {
3503 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3504 } else {
3505 if (s->r == LABEL_DECLARED)
3506 s->r = LABEL_FORWARD;
3508 if (!s->type.t) {
3509 s->type.t = VT_VOID;
3510 mk_pointer(&s->type);
3511 s->type.t |= VT_STATIC;
3513 vset(&s->type, VT_CONST | VT_SYM, 0);
3514 vtop->sym = s;
3515 next();
3516 break;
3517 default:
3518 tok_identifier:
3519 t = tok;
3520 next();
3521 if (t < TOK_UIDENT)
3522 expect("identifier");
3523 s = sym_find(t);
3524 if (!s) {
3525 if (tok != '(')
3526 error("'%s' undeclared", get_tok_str(t, NULL));
3527 /* for simple function calls, we tolerate undeclared
3528 external reference to int() function */
3529 if (tcc_state->warn_implicit_function_declaration)
3530 warning("implicit declaration of function '%s'",
3531 get_tok_str(t, NULL));
3532 s = external_global_sym(t, &func_old_type, 0);
3534 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3535 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3536 /* if referencing an inline function, then we generate a
3537 symbol to it if not already done. It will have the
3538 effect to generate code for it at the end of the
3539 compilation unit. Inline function as always
3540 generated in the text section. */
3541 if (!s->c)
3542 put_extern_sym(s, text_section, 0, 0);
3543 r = VT_SYM | VT_CONST;
3544 } else {
3545 r = s->r;
3547 vset(&s->type, r, s->c);
3548 /* if forward reference, we must point to s */
3549 if (vtop->r & VT_SYM) {
3550 vtop->sym = s;
3551 vtop->c.ul = 0;
3553 break;
3556 /* post operations */
3557 while (1) {
3558 if (tok == TOK_INC || tok == TOK_DEC) {
3559 inc(1, tok);
3560 next();
3561 } else if (tok == '.' || tok == TOK_ARROW) {
3562 int qualifiers;
3563 /* field */
3564 if (tok == TOK_ARROW)
3565 indir();
3566 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3567 test_lvalue();
3568 gaddrof();
3569 next();
3570 /* expect pointer on structure */
3571 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3572 expect("struct or union");
3573 s = vtop->type.ref;
3574 /* find field */
3575 tok |= SYM_FIELD;
3576 while ((s = s->next) != NULL) {
3577 if (s->v == tok)
3578 break;
3580 if (!s)
3581 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3582 /* add field offset to pointer */
3583 vtop->type = char_pointer_type; /* change type to 'char *' */
3584 vpushi(s->c);
3585 gen_op('+');
3586 /* change type to field type, and set to lvalue */
3587 vtop->type = s->type;
3588 vtop->type.t |= qualifiers;
3589 /* an array is never an lvalue */
3590 if (!(vtop->type.t & VT_ARRAY)) {
3591 vtop->r |= lvalue_type(vtop->type.t);
3592 #ifdef CONFIG_TCC_BCHECK
3593 /* if bound checking, the referenced pointer must be checked */
3594 if (tcc_state->do_bounds_check)
3595 vtop->r |= VT_MUSTBOUND;
3596 #endif
3598 next();
3599 } else if (tok == '[') {
3600 next();
3601 gexpr();
3602 gen_op('+');
3603 indir();
3604 skip(']');
3605 } else if (tok == '(') {
3606 SValue ret;
3607 Sym *sa;
3608 int nb_args;
3610 /* function call */
3611 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3612 /* pointer test (no array accepted) */
3613 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3614 vtop->type = *pointed_type(&vtop->type);
3615 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3616 goto error_func;
3617 } else {
3618 error_func:
3619 expect("function pointer");
3621 } else {
3622 vtop->r &= ~VT_LVAL; /* no lvalue */
3624 /* get return type */
3625 s = vtop->type.ref;
3626 next();
3627 sa = s->next; /* first parameter */
3628 nb_args = 0;
3629 ret.r2 = VT_CONST;
3630 /* compute first implicit argument if a structure is returned */
3631 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3632 /* get some space for the returned structure */
3633 size = type_size(&s->type, &align);
3634 loc = (loc - size) & -align;
3635 ret.type = s->type;
3636 ret.r = VT_LOCAL | VT_LVAL;
3637 /* pass it as 'int' to avoid structure arg passing
3638 problems */
3639 vseti(VT_LOCAL, loc);
3640 ret.c = vtop->c;
3641 nb_args++;
3642 } else {
3643 ret.type = s->type;
3644 /* return in register */
3645 if (is_float(ret.type.t)) {
3646 ret.r = reg_fret(ret.type.t);
3647 } else {
3648 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3649 ret.r2 = REG_LRET;
3650 ret.r = REG_IRET;
3652 ret.c.i = 0;
3654 if (tok != ')') {
3655 for(;;) {
3656 expr_eq();
3657 gfunc_param_typed(s, sa);
3658 nb_args++;
3659 if (sa)
3660 sa = sa->next;
3661 if (tok == ')')
3662 break;
3663 skip(',');
3666 if (sa)
3667 error("too few arguments to function");
3668 skip(')');
3669 if (!nocode_wanted) {
3670 gfunc_call(nb_args);
3671 } else {
3672 vtop -= (nb_args + 1);
3674 /* return value */
3675 vsetc(&ret.type, ret.r, &ret.c);
3676 vtop->r2 = ret.r2;
3677 } else {
3678 break;
3683 static void uneq(void)
3685 int t;
3687 unary();
3688 if (tok == '=' ||
3689 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3690 tok == TOK_A_XOR || tok == TOK_A_OR ||
3691 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3692 test_lvalue();
3693 t = tok;
3694 next();
3695 if (t == '=') {
3696 expr_eq();
3697 } else {
3698 vdup();
3699 expr_eq();
3700 gen_op(t & 0x7f);
3702 vstore();
3706 ST_FUNC void expr_prod(void)
3708 int t;
3710 uneq();
3711 while (tok == '*' || tok == '/' || tok == '%') {
3712 t = tok;
3713 next();
3714 uneq();
3715 gen_op(t);
3719 ST_FUNC void expr_sum(void)
3721 int t;
3723 expr_prod();
3724 while (tok == '+' || tok == '-') {
3725 t = tok;
3726 next();
3727 expr_prod();
3728 gen_op(t);
3732 static void expr_shift(void)
3734 int t;
3736 expr_sum();
3737 while (tok == TOK_SHL || tok == TOK_SAR) {
3738 t = tok;
3739 next();
3740 expr_sum();
3741 gen_op(t);
3745 static void expr_cmp(void)
3747 int t;
3749 expr_shift();
3750 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3751 tok == TOK_ULT || tok == TOK_UGE) {
3752 t = tok;
3753 next();
3754 expr_shift();
3755 gen_op(t);
3759 static void expr_cmpeq(void)
3761 int t;
3763 expr_cmp();
3764 while (tok == TOK_EQ || tok == TOK_NE) {
3765 t = tok;
3766 next();
3767 expr_cmp();
3768 gen_op(t);
3772 static void expr_and(void)
3774 expr_cmpeq();
3775 while (tok == '&') {
3776 next();
3777 expr_cmpeq();
3778 gen_op('&');
3782 static void expr_xor(void)
3784 expr_and();
3785 while (tok == '^') {
3786 next();
3787 expr_and();
3788 gen_op('^');
3792 static void expr_or(void)
3794 expr_xor();
3795 while (tok == '|') {
3796 next();
3797 expr_xor();
3798 gen_op('|');
3802 /* XXX: fix this mess */
3803 static void expr_land_const(void)
3805 expr_or();
3806 while (tok == TOK_LAND) {
3807 next();
3808 expr_or();
3809 gen_op(TOK_LAND);
3813 /* XXX: fix this mess */
3814 static void expr_lor_const(void)
3816 expr_land_const();
3817 while (tok == TOK_LOR) {
3818 next();
3819 expr_land_const();
3820 gen_op(TOK_LOR);
3824 /* only used if non constant */
3825 static void expr_land(void)
3827 int t;
3829 expr_or();
3830 if (tok == TOK_LAND) {
3831 t = 0;
3832 save_regs(1);
3833 for(;;) {
3834 t = gtst(1, t);
3835 if (tok != TOK_LAND) {
3836 vseti(VT_JMPI, t);
3837 break;
3839 next();
3840 expr_or();
3845 static void expr_lor(void)
3847 int t;
3849 expr_land();
3850 if (tok == TOK_LOR) {
3851 t = 0;
3852 save_regs(1);
3853 for(;;) {
3854 t = gtst(0, t);
3855 if (tok != TOK_LOR) {
3856 vseti(VT_JMP, t);
3857 break;
3859 next();
3860 expr_land();
3865 /* XXX: better constant handling */
3866 static void expr_eq(void)
3868 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3869 SValue sv;
3870 CType type, type1, type2;
3872 if (const_wanted) {
3873 expr_lor_const();
3874 if (tok == '?') {
3875 CType boolean;
3876 int c;
3877 boolean.t = VT_BOOL;
3878 vdup();
3879 gen_cast(&boolean);
3880 c = vtop->c.i;
3881 vpop();
3882 next();
3883 if (tok != ':' || !gnu_ext) {
3884 vpop();
3885 gexpr();
3887 if (!c)
3888 vpop();
3889 skip(':');
3890 expr_eq();
3891 if (c)
3892 vpop();
3894 } else {
3895 expr_lor();
3896 if (tok == '?') {
3897 next();
3898 if (vtop != vstack) {
3899 /* needed to avoid having different registers saved in
3900 each branch */
3901 if (is_float(vtop->type.t)) {
3902 rc = RC_FLOAT;
3903 #ifdef TCC_TARGET_X86_64
3904 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3905 rc = RC_ST0;
3907 #endif
3909 else
3910 rc = RC_INT;
3911 gv(rc);
3912 save_regs(1);
3914 if (tok == ':' && gnu_ext) {
3915 gv_dup();
3916 tt = gtst(1, 0);
3917 } else {
3918 tt = gtst(1, 0);
3919 gexpr();
3921 type1 = vtop->type;
3922 sv = *vtop; /* save value to handle it later */
3923 vtop--; /* no vpop so that FP stack is not flushed */
3924 skip(':');
3925 u = gjmp(0);
3926 gsym(tt);
3927 expr_eq();
3928 type2 = vtop->type;
3930 t1 = type1.t;
3931 bt1 = t1 & VT_BTYPE;
3932 t2 = type2.t;
3933 bt2 = t2 & VT_BTYPE;
3934 /* cast operands to correct type according to ISOC rules */
3935 if (is_float(bt1) || is_float(bt2)) {
3936 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3937 type.t = VT_LDOUBLE;
3938 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3939 type.t = VT_DOUBLE;
3940 } else {
3941 type.t = VT_FLOAT;
3943 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3944 /* cast to biggest op */
3945 type.t = VT_LLONG;
3946 /* convert to unsigned if it does not fit in a long long */
3947 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3948 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3949 type.t |= VT_UNSIGNED;
3950 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3951 /* XXX: test pointer compatibility */
3952 type = type1;
3953 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3954 /* XXX: test function pointer compatibility */
3955 type = type1;
3956 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3957 /* XXX: test structure compatibility */
3958 type = type1;
3959 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3960 /* NOTE: as an extension, we accept void on only one side */
3961 type.t = VT_VOID;
3962 } else {
3963 /* integer operations */
3964 type.t = VT_INT;
3965 /* convert to unsigned if it does not fit in an integer */
3966 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3967 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3968 type.t |= VT_UNSIGNED;
3971 /* now we convert second operand */
3972 gen_cast(&type);
3973 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3974 gaddrof();
3975 rc = RC_INT;
3976 if (is_float(type.t)) {
3977 rc = RC_FLOAT;
3978 #ifdef TCC_TARGET_X86_64
3979 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3980 rc = RC_ST0;
3982 #endif
3983 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3984 /* for long longs, we use fixed registers to avoid having
3985 to handle a complicated move */
3986 rc = RC_IRET;
3989 r2 = gv(rc);
3990 /* this is horrible, but we must also convert first
3991 operand */
3992 tt = gjmp(0);
3993 gsym(u);
3994 /* put again first value and cast it */
3995 *vtop = sv;
3996 gen_cast(&type);
3997 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3998 gaddrof();
3999 r1 = gv(rc);
4000 move_reg(r2, r1);
4001 vtop->r = r2;
4002 gsym(tt);
4007 ST_FUNC void gexpr(void)
4009 while (1) {
4010 expr_eq();
4011 if (tok != ',')
4012 break;
4013 vpop();
4014 next();
4018 /* parse an expression and return its type without any side effect. */
4019 static void expr_type(CType *type)
4021 int saved_nocode_wanted;
4023 saved_nocode_wanted = nocode_wanted;
4024 nocode_wanted = 1;
4025 gexpr();
4026 *type = vtop->type;
4027 vpop();
4028 nocode_wanted = saved_nocode_wanted;
4031 /* parse a unary expression and return its type without any side
4032 effect. */
4033 static void unary_type(CType *type)
4035 int a;
4037 a = nocode_wanted;
4038 nocode_wanted = 1;
4039 unary();
4040 *type = vtop->type;
4041 vpop();
4042 nocode_wanted = a;
4045 /* parse a constant expression and return value in vtop. */
4046 static void expr_const1(void)
4048 int a;
4049 a = const_wanted;
4050 const_wanted = 1;
4051 expr_eq();
4052 const_wanted = a;
4055 /* parse an integer constant and return its value. */
4056 ST_FUNC int expr_const(void)
4058 int c;
4059 expr_const1();
4060 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4061 expect("constant expression");
4062 c = vtop->c.i;
4063 vpop();
4064 return c;
4067 /* return the label token if current token is a label, otherwise
4068 return zero */
4069 static int is_label(void)
4071 int last_tok;
4073 /* fast test first */
4074 if (tok < TOK_UIDENT)
4075 return 0;
4076 /* no need to save tokc because tok is an identifier */
4077 last_tok = tok;
4078 next();
4079 if (tok == ':') {
4080 next();
4081 return last_tok;
4082 } else {
4083 unget_tok(last_tok);
4084 return 0;
4088 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4089 int case_reg, int is_expr)
4091 int a, b, c, d;
4092 Sym *s;
4094 /* generate line number info */
4095 if (tcc_state->do_debug &&
4096 (last_line_num != file->line_num || last_ind != ind)) {
4097 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4098 last_ind = ind;
4099 last_line_num = file->line_num;
4102 if (is_expr) {
4103 /* default return value is (void) */
4104 vpushi(0);
4105 vtop->type.t = VT_VOID;
4108 if (tok == TOK_IF) {
4109 /* if test */
4110 next();
4111 skip('(');
4112 gexpr();
4113 skip(')');
4114 a = gtst(1, 0);
4115 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4116 c = tok;
4117 if (c == TOK_ELSE) {
4118 next();
4119 d = gjmp(0);
4120 gsym(a);
4121 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4122 gsym(d); /* patch else jmp */
4123 } else
4124 gsym(a);
4125 } else if (tok == TOK_WHILE) {
4126 next();
4127 d = ind;
4128 skip('(');
4129 gexpr();
4130 skip(')');
4131 a = gtst(1, 0);
4132 b = 0;
4133 block(&a, &b, case_sym, def_sym, case_reg, 0);
4134 gjmp_addr(d);
4135 gsym(a);
4136 gsym_addr(b, d);
4137 } else if (tok == '{') {
4138 Sym *llabel;
4140 next();
4141 /* record local declaration stack position */
4142 s = local_stack;
4143 llabel = local_label_stack;
4144 /* handle local labels declarations */
4145 if (tok == TOK_LABEL) {
4146 next();
4147 for(;;) {
4148 if (tok < TOK_UIDENT)
4149 expect("label identifier");
4150 label_push(&local_label_stack, tok, LABEL_DECLARED);
4151 next();
4152 if (tok == ',') {
4153 next();
4154 } else {
4155 skip(';');
4156 break;
4160 while (tok != '}') {
4161 decl(VT_LOCAL);
4162 if (tok != '}') {
4163 if (is_expr)
4164 vpop();
4165 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4168 /* pop locally defined labels */
4169 label_pop(&local_label_stack, llabel);
4170 /* pop locally defined symbols */
4171 if(is_expr) {
4172 /* XXX: this solution makes only valgrind happy...
4173 triggered by gcc.c-torture/execute/20000917-1.c */
4174 Sym *p;
4175 switch(vtop->type.t & VT_BTYPE) {
4176 case VT_PTR:
4177 case VT_STRUCT:
4178 case VT_ENUM:
4179 case VT_FUNC:
4180 for(p=vtop->type.ref;p;p=p->prev)
4181 if(p->prev==s)
4182 error("unsupported expression type");
4185 sym_pop(&local_stack, s);
4186 next();
4187 } else if (tok == TOK_RETURN) {
4188 next();
4189 if (tok != ';') {
4190 gexpr();
4191 gen_assign_cast(&func_vt);
4192 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4193 CType type;
4194 /* if returning structure, must copy it to implicit
4195 first pointer arg location */
4196 #ifdef TCC_ARM_EABI
4197 int align, size;
4198 size = type_size(&func_vt,&align);
4199 if(size <= 4)
4201 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4202 && (align & 3))
4204 int addr;
4205 loc = (loc - size) & -4;
4206 addr = loc;
4207 type = func_vt;
4208 vset(&type, VT_LOCAL | VT_LVAL, addr);
4209 vswap();
4210 vstore();
4211 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4213 vtop->type = int_type;
4214 gv(RC_IRET);
4215 } else {
4216 #endif
4217 type = func_vt;
4218 mk_pointer(&type);
4219 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4220 indir();
4221 vswap();
4222 /* copy structure value to pointer */
4223 vstore();
4224 #ifdef TCC_ARM_EABI
4226 #endif
4227 } else if (is_float(func_vt.t)) {
4228 gv(rc_fret(func_vt.t));
4229 } else {
4230 gv(RC_IRET);
4232 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4234 skip(';');
4235 rsym = gjmp(rsym); /* jmp */
4236 } else if (tok == TOK_BREAK) {
4237 /* compute jump */
4238 if (!bsym)
4239 error("cannot break");
4240 *bsym = gjmp(*bsym);
4241 next();
4242 skip(';');
4243 } else if (tok == TOK_CONTINUE) {
4244 /* compute jump */
4245 if (!csym)
4246 error("cannot continue");
4247 *csym = gjmp(*csym);
4248 next();
4249 skip(';');
4250 } else if (tok == TOK_FOR) {
4251 int e;
4252 next();
4253 skip('(');
4254 if (tok != ';') {
4255 gexpr();
4256 vpop();
4258 skip(';');
4259 d = ind;
4260 c = ind;
4261 a = 0;
4262 b = 0;
4263 if (tok != ';') {
4264 gexpr();
4265 a = gtst(1, 0);
4267 skip(';');
4268 if (tok != ')') {
4269 e = gjmp(0);
4270 c = ind;
4271 gexpr();
4272 vpop();
4273 gjmp_addr(d);
4274 gsym(e);
4276 skip(')');
4277 block(&a, &b, case_sym, def_sym, case_reg, 0);
4278 gjmp_addr(c);
4279 gsym(a);
4280 gsym_addr(b, c);
4281 } else
4282 if (tok == TOK_DO) {
4283 next();
4284 a = 0;
4285 b = 0;
4286 d = ind;
4287 block(&a, &b, case_sym, def_sym, case_reg, 0);
4288 skip(TOK_WHILE);
4289 skip('(');
4290 gsym(b);
4291 gexpr();
4292 c = gtst(0, 0);
4293 gsym_addr(c, d);
4294 skip(')');
4295 gsym(a);
4296 skip(';');
4297 } else
4298 if (tok == TOK_SWITCH) {
4299 next();
4300 skip('(');
4301 gexpr();
4302 /* XXX: other types than integer */
4303 case_reg = gv(RC_INT);
4304 vpop();
4305 skip(')');
4306 a = 0;
4307 b = gjmp(0); /* jump to first case */
4308 c = 0;
4309 block(&a, csym, &b, &c, case_reg, 0);
4310 /* if no default, jmp after switch */
4311 if (c == 0)
4312 c = ind;
4313 /* default label */
4314 gsym_addr(b, c);
4315 /* break label */
4316 gsym(a);
4317 } else
4318 if (tok == TOK_CASE) {
4319 int v1, v2;
4320 if (!case_sym)
4321 expect("switch");
4322 next();
4323 v1 = expr_const();
4324 v2 = v1;
4325 if (gnu_ext && tok == TOK_DOTS) {
4326 next();
4327 v2 = expr_const();
4328 if (v2 < v1)
4329 warning("empty case range");
4331 /* since a case is like a label, we must skip it with a jmp */
4332 b = gjmp(0);
4333 gsym(*case_sym);
4334 vseti(case_reg, 0);
4335 vpushi(v1);
4336 if (v1 == v2) {
4337 gen_op(TOK_EQ);
4338 *case_sym = gtst(1, 0);
4339 } else {
4340 gen_op(TOK_GE);
4341 *case_sym = gtst(1, 0);
4342 vseti(case_reg, 0);
4343 vpushi(v2);
4344 gen_op(TOK_LE);
4345 *case_sym = gtst(1, *case_sym);
4347 gsym(b);
4348 skip(':');
4349 is_expr = 0;
4350 goto block_after_label;
4351 } else
4352 if (tok == TOK_DEFAULT) {
4353 next();
4354 skip(':');
4355 if (!def_sym)
4356 expect("switch");
4357 if (*def_sym)
4358 error("too many 'default'");
4359 *def_sym = ind;
4360 is_expr = 0;
4361 goto block_after_label;
4362 } else
4363 if (tok == TOK_GOTO) {
4364 next();
4365 if (tok == '*' && gnu_ext) {
4366 /* computed goto */
4367 next();
4368 gexpr();
4369 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4370 expect("pointer");
4371 ggoto();
4372 } else if (tok >= TOK_UIDENT) {
4373 s = label_find(tok);
4374 /* put forward definition if needed */
4375 if (!s) {
4376 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4377 } else {
4378 if (s->r == LABEL_DECLARED)
4379 s->r = LABEL_FORWARD;
4381 /* label already defined */
4382 if (s->r & LABEL_FORWARD)
4383 s->jnext = gjmp(s->jnext);
4384 else
4385 gjmp_addr(s->jnext);
4386 next();
4387 } else {
4388 expect("label identifier");
4390 skip(';');
4391 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4392 asm_instr();
4393 } else {
4394 b = is_label();
4395 if (b) {
4396 /* label case */
4397 s = label_find(b);
4398 if (s) {
4399 if (s->r == LABEL_DEFINED)
4400 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4401 gsym(s->jnext);
4402 s->r = LABEL_DEFINED;
4403 } else {
4404 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4406 s->jnext = ind;
4407 /* we accept this, but it is a mistake */
4408 block_after_label:
4409 if (tok == '}') {
4410 warning("deprecated use of label at end of compound statement");
4411 } else {
4412 if (is_expr)
4413 vpop();
4414 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4416 } else {
4417 /* expression case */
4418 if (tok != ';') {
4419 if (is_expr) {
4420 vpop();
4421 gexpr();
4422 } else {
4423 gexpr();
4424 vpop();
4427 skip(';');
4432 /* t is the array or struct type. c is the array or struct
4433 address. cur_index/cur_field is the pointer to the current
4434 value. 'size_only' is true if only size info is needed (only used
4435 in arrays) */
4436 static void decl_designator(CType *type, Section *sec, unsigned long c,
4437 int *cur_index, Sym **cur_field,
4438 int size_only)
4440 Sym *s, *f;
4441 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4442 CType type1;
4444 notfirst = 0;
4445 elem_size = 0;
4446 nb_elems = 1;
4447 if (gnu_ext && (l = is_label()) != 0)
4448 goto struct_field;
4449 while (tok == '[' || tok == '.') {
4450 if (tok == '[') {
4451 if (!(type->t & VT_ARRAY))
4452 expect("array type");
4453 s = type->ref;
4454 next();
4455 index = expr_const();
4456 if (index < 0 || (s->c >= 0 && index >= s->c))
4457 expect("invalid index");
4458 if (tok == TOK_DOTS && gnu_ext) {
4459 next();
4460 index_last = expr_const();
4461 if (index_last < 0 ||
4462 (s->c >= 0 && index_last >= s->c) ||
4463 index_last < index)
4464 expect("invalid index");
4465 } else {
4466 index_last = index;
4468 skip(']');
4469 if (!notfirst)
4470 *cur_index = index_last;
4471 type = pointed_type(type);
4472 elem_size = type_size(type, &align);
4473 c += index * elem_size;
4474 /* NOTE: we only support ranges for last designator */
4475 nb_elems = index_last - index + 1;
4476 if (nb_elems != 1) {
4477 notfirst = 1;
4478 break;
4480 } else {
4481 next();
4482 l = tok;
4483 next();
4484 struct_field:
4485 if ((type->t & VT_BTYPE) != VT_STRUCT)
4486 expect("struct/union type");
4487 s = type->ref;
4488 l |= SYM_FIELD;
4489 f = s->next;
4490 while (f) {
4491 if (f->v == l)
4492 break;
4493 f = f->next;
4495 if (!f)
4496 expect("field");
4497 if (!notfirst)
4498 *cur_field = f;
4499 /* XXX: fix this mess by using explicit storage field */
4500 type1 = f->type;
4501 type1.t |= (type->t & ~VT_TYPE);
4502 type = &type1;
4503 c += f->c;
4505 notfirst = 1;
4507 if (notfirst) {
4508 if (tok == '=') {
4509 next();
4510 } else {
4511 if (!gnu_ext)
4512 expect("=");
4514 } else {
4515 if (type->t & VT_ARRAY) {
4516 index = *cur_index;
4517 type = pointed_type(type);
4518 c += index * type_size(type, &align);
4519 } else {
4520 f = *cur_field;
4521 if (!f)
4522 error("too many field init");
4523 /* XXX: fix this mess by using explicit storage field */
4524 type1 = f->type;
4525 type1.t |= (type->t & ~VT_TYPE);
4526 type = &type1;
4527 c += f->c;
4530 decl_initializer(type, sec, c, 0, size_only);
4532 /* XXX: make it more general */
4533 if (!size_only && nb_elems > 1) {
4534 unsigned long c_end;
4535 uint8_t *src, *dst;
4536 int i;
4538 if (!sec)
4539 error("range init not supported yet for dynamic storage");
4540 c_end = c + nb_elems * elem_size;
4541 if (c_end > sec->data_allocated)
4542 section_realloc(sec, c_end);
4543 src = sec->data + c;
4544 dst = src;
4545 for(i = 1; i < nb_elems; i++) {
4546 dst += elem_size;
4547 memcpy(dst, src, elem_size);
4552 #define EXPR_VAL 0
4553 #define EXPR_CONST 1
4554 #define EXPR_ANY 2
4556 /* store a value or an expression directly in global data or in local array */
4557 static void init_putv(CType *type, Section *sec, unsigned long c,
4558 int v, int expr_type)
4560 int saved_global_expr, bt, bit_pos, bit_size;
4561 void *ptr;
4562 unsigned long long bit_mask;
4563 CType dtype;
4565 switch(expr_type) {
4566 case EXPR_VAL:
4567 vpushi(v);
4568 break;
4569 case EXPR_CONST:
4570 /* compound literals must be allocated globally in this case */
4571 saved_global_expr = global_expr;
4572 global_expr = 1;
4573 expr_const1();
4574 global_expr = saved_global_expr;
4575 /* NOTE: symbols are accepted */
4576 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4577 error("initializer element is not constant");
4578 break;
4579 case EXPR_ANY:
4580 expr_eq();
4581 break;
4584 dtype = *type;
4585 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4587 if (sec) {
4588 /* XXX: not portable */
4589 /* XXX: generate error if incorrect relocation */
4590 gen_assign_cast(&dtype);
4591 bt = type->t & VT_BTYPE;
4592 /* we'll write at most 12 bytes */
4593 if (c + 12 > sec->data_allocated) {
4594 section_realloc(sec, c + 12);
4596 ptr = sec->data + c;
4597 /* XXX: make code faster ? */
4598 if (!(type->t & VT_BITFIELD)) {
4599 bit_pos = 0;
4600 bit_size = 32;
4601 bit_mask = -1LL;
4602 } else {
4603 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4604 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4605 bit_mask = (1LL << bit_size) - 1;
4607 if ((vtop->r & VT_SYM) &&
4608 (bt == VT_BYTE ||
4609 bt == VT_SHORT ||
4610 bt == VT_DOUBLE ||
4611 bt == VT_LDOUBLE ||
4612 bt == VT_LLONG ||
4613 (bt == VT_INT && bit_size != 32)))
4614 error("initializer element is not computable at load time");
4615 switch(bt) {
4616 case VT_BOOL:
4617 vtop->c.i = (vtop->c.i != 0);
4618 case VT_BYTE:
4619 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4620 break;
4621 case VT_SHORT:
4622 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4623 break;
4624 case VT_DOUBLE:
4625 *(double *)ptr = vtop->c.d;
4626 break;
4627 case VT_LDOUBLE:
4628 *(long double *)ptr = vtop->c.ld;
4629 break;
4630 case VT_LLONG:
4631 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4632 break;
4633 default:
4634 if (vtop->r & VT_SYM) {
4635 greloc(sec, vtop->sym, c, R_DATA_PTR);
4637 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4638 break;
4640 vtop--;
4641 } else {
4642 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4643 vswap();
4644 vstore();
4645 vpop();
4649 /* put zeros for variable based init */
4650 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4652 if (sec) {
4653 /* nothing to do because globals are already set to zero */
4654 } else {
4655 vpush_global_sym(&func_old_type, TOK_memset);
4656 vseti(VT_LOCAL, c);
4657 vpushi(0);
4658 vpushi(size);
4659 gfunc_call(3);
4663 /* 't' contains the type and storage info. 'c' is the offset of the
4664 object in section 'sec'. If 'sec' is NULL, it means stack based
4665 allocation. 'first' is true if array '{' must be read (multi
4666 dimension implicit array init handling). 'size_only' is true if
4667 size only evaluation is wanted (only for arrays). */
4668 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4669 int first, int size_only)
4671 int index, array_length, n, no_oblock, nb, parlevel, i;
4672 int size1, align1, expr_type;
4673 Sym *s, *f;
4674 CType *t1;
4676 if (type->t & VT_ARRAY) {
4677 s = type->ref;
4678 n = s->c;
4679 array_length = 0;
4680 t1 = pointed_type(type);
4681 size1 = type_size(t1, &align1);
4683 no_oblock = 1;
4684 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4685 tok == '{') {
4686 skip('{');
4687 no_oblock = 0;
4690 /* only parse strings here if correct type (otherwise: handle
4691 them as ((w)char *) expressions */
4692 if ((tok == TOK_LSTR &&
4693 #ifdef TCC_TARGET_PE
4694 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4695 #else
4696 (t1->t & VT_BTYPE) == VT_INT
4697 #endif
4698 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4699 while (tok == TOK_STR || tok == TOK_LSTR) {
4700 int cstr_len, ch;
4701 CString *cstr;
4703 cstr = tokc.cstr;
4704 /* compute maximum number of chars wanted */
4705 if (tok == TOK_STR)
4706 cstr_len = cstr->size;
4707 else
4708 cstr_len = cstr->size / sizeof(nwchar_t);
4709 cstr_len--;
4710 nb = cstr_len;
4711 if (n >= 0 && nb > (n - array_length))
4712 nb = n - array_length;
4713 if (!size_only) {
4714 if (cstr_len > nb)
4715 warning("initializer-string for array is too long");
4716 /* in order to go faster for common case (char
4717 string in global variable, we handle it
4718 specifically */
4719 if (sec && tok == TOK_STR && size1 == 1) {
4720 memcpy(sec->data + c + array_length, cstr->data, nb);
4721 } else {
4722 for(i=0;i<nb;i++) {
4723 if (tok == TOK_STR)
4724 ch = ((unsigned char *)cstr->data)[i];
4725 else
4726 ch = ((nwchar_t *)cstr->data)[i];
4727 init_putv(t1, sec, c + (array_length + i) * size1,
4728 ch, EXPR_VAL);
4732 array_length += nb;
4733 next();
4735 /* only add trailing zero if enough storage (no
4736 warning in this case since it is standard) */
4737 if (n < 0 || array_length < n) {
4738 if (!size_only) {
4739 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4741 array_length++;
4743 } else {
4744 index = 0;
4745 while (tok != '}') {
4746 decl_designator(type, sec, c, &index, NULL, size_only);
4747 if (n >= 0 && index >= n)
4748 error("index too large");
4749 /* must put zero in holes (note that doing it that way
4750 ensures that it even works with designators) */
4751 if (!size_only && array_length < index) {
4752 init_putz(t1, sec, c + array_length * size1,
4753 (index - array_length) * size1);
4755 index++;
4756 if (index > array_length)
4757 array_length = index;
4758 /* special test for multi dimensional arrays (may not
4759 be strictly correct if designators are used at the
4760 same time) */
4761 if (index >= n && no_oblock)
4762 break;
4763 if (tok == '}')
4764 break;
4765 skip(',');
4768 if (!no_oblock)
4769 skip('}');
4770 /* put zeros at the end */
4771 if (!size_only && n >= 0 && array_length < n) {
4772 init_putz(t1, sec, c + array_length * size1,
4773 (n - array_length) * size1);
4775 /* patch type size if needed */
4776 if (n < 0)
4777 s->c = array_length;
4778 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4779 (sec || !first || tok == '{')) {
4780 int par_count;
4782 /* NOTE: the previous test is a specific case for automatic
4783 struct/union init */
4784 /* XXX: union needs only one init */
4786 /* XXX: this test is incorrect for local initializers
4787 beginning with ( without {. It would be much more difficult
4788 to do it correctly (ideally, the expression parser should
4789 be used in all cases) */
4790 par_count = 0;
4791 if (tok == '(') {
4792 AttributeDef ad1;
4793 CType type1;
4794 next();
4795 while (tok == '(') {
4796 par_count++;
4797 next();
4799 if (!parse_btype(&type1, &ad1))
4800 expect("cast");
4801 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4802 #if 0
4803 if (!is_assignable_types(type, &type1))
4804 error("invalid type for cast");
4805 #endif
4806 skip(')');
4808 no_oblock = 1;
4809 if (first || tok == '{') {
4810 skip('{');
4811 no_oblock = 0;
4813 s = type->ref;
4814 f = s->next;
4815 array_length = 0;
4816 index = 0;
4817 n = s->c;
4818 while (tok != '}') {
4819 decl_designator(type, sec, c, NULL, &f, size_only);
4820 index = f->c;
4821 if (!size_only && array_length < index) {
4822 init_putz(type, sec, c + array_length,
4823 index - array_length);
4825 index = index + type_size(&f->type, &align1);
4826 if (index > array_length)
4827 array_length = index;
4829 /* gr: skip fields from same union - ugly. */
4830 while (f->next) {
4831 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4832 /* test for same offset */
4833 if (f->next->c != f->c)
4834 break;
4835 /* if yes, test for bitfield shift */
4836 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4837 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4838 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4839 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4840 if (bit_pos_1 != bit_pos_2)
4841 break;
4843 f = f->next;
4846 f = f->next;
4847 if (no_oblock && f == NULL)
4848 break;
4849 if (tok == '}')
4850 break;
4851 skip(',');
4853 /* put zeros at the end */
4854 if (!size_only && array_length < n) {
4855 init_putz(type, sec, c + array_length,
4856 n - array_length);
4858 if (!no_oblock)
4859 skip('}');
4860 while (par_count) {
4861 skip(')');
4862 par_count--;
4864 } else if (tok == '{') {
4865 next();
4866 decl_initializer(type, sec, c, first, size_only);
4867 skip('}');
4868 } else if (size_only) {
4869 /* just skip expression */
4870 parlevel = 0;
4871 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4872 tok != -1) {
4873 if (tok == '(')
4874 parlevel++;
4875 else if (tok == ')')
4876 parlevel--;
4877 next();
4879 } else {
4880 /* currently, we always use constant expression for globals
4881 (may change for scripting case) */
4882 expr_type = EXPR_CONST;
4883 if (!sec)
4884 expr_type = EXPR_ANY;
4885 init_putv(type, sec, c, 0, expr_type);
4889 /* parse an initializer for type 't' if 'has_init' is non zero, and
4890 allocate space in local or global data space ('r' is either
4891 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4892 variable 'v' of scope 'scope' is declared before initializers are
4893 parsed. If 'v' is zero, then a reference to the new object is put
4894 in the value stack. If 'has_init' is 2, a special parsing is done
4895 to handle string constants. */
4896 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4897 int has_init, int v, int scope)
4899 int size, align, addr, data_offset;
4900 int level;
4901 ParseState saved_parse_state = {0};
4902 TokenString init_str;
4903 Section *sec;
4905 size = type_size(type, &align);
4906 /* If unknown size, we must evaluate it before
4907 evaluating initializers because
4908 initializers can generate global data too
4909 (e.g. string pointers or ISOC99 compound
4910 literals). It also simplifies local
4911 initializers handling */
4912 tok_str_new(&init_str);
4913 if (size < 0) {
4914 if (!has_init)
4915 error("unknown type size");
4916 /* get all init string */
4917 if (has_init == 2) {
4918 /* only get strings */
4919 while (tok == TOK_STR || tok == TOK_LSTR) {
4920 tok_str_add_tok(&init_str);
4921 next();
4923 } else {
4924 level = 0;
4925 while (level > 0 || (tok != ',' && tok != ';')) {
4926 if (tok < 0)
4927 error("unexpected end of file in initializer");
4928 tok_str_add_tok(&init_str);
4929 if (tok == '{')
4930 level++;
4931 else if (tok == '}') {
4932 level--;
4933 if (level <= 0) {
4934 next();
4935 break;
4938 next();
4941 tok_str_add(&init_str, -1);
4942 tok_str_add(&init_str, 0);
4944 /* compute size */
4945 save_parse_state(&saved_parse_state);
4947 macro_ptr = init_str.str;
4948 next();
4949 decl_initializer(type, NULL, 0, 1, 1);
4950 /* prepare second initializer parsing */
4951 macro_ptr = init_str.str;
4952 next();
4954 /* if still unknown size, error */
4955 size = type_size(type, &align);
4956 if (size < 0)
4957 error("unknown type size");
4959 /* take into account specified alignment if bigger */
4960 if (ad->aligned) {
4961 if (ad->aligned > align)
4962 align = ad->aligned;
4963 } else if (ad->packed) {
4964 align = 1;
4966 if ((r & VT_VALMASK) == VT_LOCAL) {
4967 sec = NULL;
4968 #ifdef CONFIG_TCC_BCHECK
4969 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4970 loc--;
4971 #endif
4972 loc = (loc - size) & -align;
4973 addr = loc;
4974 #ifdef CONFIG_TCC_BCHECK
4975 /* handles bounds */
4976 /* XXX: currently, since we do only one pass, we cannot track
4977 '&' operators, so we add only arrays */
4978 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4979 unsigned long *bounds_ptr;
4980 /* add padding between regions */
4981 loc--;
4982 /* then add local bound info */
4983 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4984 bounds_ptr[0] = addr;
4985 bounds_ptr[1] = size;
4987 #endif
4988 if (v) {
4989 /* local variable */
4990 sym_push(v, type, r, addr);
4991 } else {
4992 /* push local reference */
4993 vset(type, r, addr);
4995 } else {
4996 Sym *sym;
4998 sym = NULL;
4999 if (v && scope == VT_CONST) {
5000 /* see if the symbol was already defined */
5001 sym = sym_find(v);
5002 if (sym) {
5003 if (!is_compatible_types(&sym->type, type))
5004 error("incompatible types for redefinition of '%s'",
5005 get_tok_str(v, NULL));
5006 if (sym->type.t & VT_EXTERN) {
5007 /* if the variable is extern, it was not allocated */
5008 sym->type.t &= ~VT_EXTERN;
5009 /* set array size if it was ommited in extern
5010 declaration */
5011 if ((sym->type.t & VT_ARRAY) &&
5012 sym->type.ref->c < 0 &&
5013 type->ref->c >= 0)
5014 sym->type.ref->c = type->ref->c;
5015 } else {
5016 /* we accept several definitions of the same
5017 global variable. this is tricky, because we
5018 must play with the SHN_COMMON type of the symbol */
5019 /* XXX: should check if the variable was already
5020 initialized. It is incorrect to initialized it
5021 twice */
5022 /* no init data, we won't add more to the symbol */
5023 if (!has_init)
5024 goto no_alloc;
5029 /* allocate symbol in corresponding section */
5030 sec = ad->section;
5031 if (!sec) {
5032 if (has_init)
5033 sec = data_section;
5034 else if (tcc_state->nocommon)
5035 sec = bss_section;
5037 if (sec) {
5038 data_offset = sec->data_offset;
5039 data_offset = (data_offset + align - 1) & -align;
5040 addr = data_offset;
5041 /* very important to increment global pointer at this time
5042 because initializers themselves can create new initializers */
5043 data_offset += size;
5044 #ifdef CONFIG_TCC_BCHECK
5045 /* add padding if bound check */
5046 if (tcc_state->do_bounds_check)
5047 data_offset++;
5048 #endif
5049 sec->data_offset = data_offset;
5050 /* allocate section space to put the data */
5051 if (sec->sh_type != SHT_NOBITS &&
5052 data_offset > sec->data_allocated)
5053 section_realloc(sec, data_offset);
5054 /* align section if needed */
5055 if (align > sec->sh_addralign)
5056 sec->sh_addralign = align;
5057 } else {
5058 addr = 0; /* avoid warning */
5061 if (v) {
5062 if (scope != VT_CONST || !sym) {
5063 sym = sym_push(v, type, r | VT_SYM, 0);
5065 /* update symbol definition */
5066 if (sec) {
5067 put_extern_sym(sym, sec, addr, size);
5068 } else {
5069 ElfW(Sym) *esym;
5070 /* put a common area */
5071 put_extern_sym(sym, NULL, align, size);
5072 /* XXX: find a nicer way */
5073 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5074 esym->st_shndx = SHN_COMMON;
5076 } else {
5077 CValue cval;
5079 /* push global reference */
5080 sym = get_sym_ref(type, sec, addr, size);
5081 cval.ul = 0;
5082 vsetc(type, VT_CONST | VT_SYM, &cval);
5083 vtop->sym = sym;
5085 #ifdef CONFIG_TCC_BCHECK
5086 /* handles bounds now because the symbol must be defined
5087 before for the relocation */
5088 if (tcc_state->do_bounds_check) {
5089 unsigned long *bounds_ptr;
5091 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5092 /* then add global bound info */
5093 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5094 bounds_ptr[0] = 0; /* relocated */
5095 bounds_ptr[1] = size;
5097 #endif
5099 if (has_init) {
5100 decl_initializer(type, sec, addr, 1, 0);
5101 /* restore parse state if needed */
5102 if (init_str.str) {
5103 tok_str_free(init_str.str);
5104 restore_parse_state(&saved_parse_state);
5107 no_alloc: ;
5110 static void put_func_debug(Sym *sym)
5112 char buf[512];
5114 /* stabs info */
5115 /* XXX: we put here a dummy type */
5116 snprintf(buf, sizeof(buf), "%s:%c1",
5117 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5118 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5119 cur_text_section, sym->c);
5120 /* //gr gdb wants a line at the function */
5121 put_stabn(N_SLINE, 0, file->line_num, 0);
5122 last_ind = 0;
5123 last_line_num = 0;
5126 /* parse an old style function declaration list */
5127 /* XXX: check multiple parameter */
5128 static void func_decl_list(Sym *func_sym)
5130 AttributeDef ad;
5131 int v;
5132 Sym *s;
5133 CType btype, type;
5135 /* parse each declaration */
5136 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
5137 if (!parse_btype(&btype, &ad))
5138 expect("declaration list");
5139 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5140 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5141 tok == ';') {
5142 /* we accept no variable after */
5143 } else {
5144 for(;;) {
5145 type = btype;
5146 type_decl(&type, &ad, &v, TYPE_DIRECT);
5147 /* find parameter in function parameter list */
5148 s = func_sym->next;
5149 while (s != NULL) {
5150 if ((s->v & ~SYM_FIELD) == v)
5151 goto found;
5152 s = s->next;
5154 error("declaration for parameter '%s' but no such parameter",
5155 get_tok_str(v, NULL));
5156 found:
5157 /* check that no storage specifier except 'register' was given */
5158 if (type.t & VT_STORAGE)
5159 error("storage class specified for '%s'", get_tok_str(v, NULL));
5160 convert_parameter_type(&type);
5161 /* we can add the type (NOTE: it could be local to the function) */
5162 s->type = type;
5163 /* accept other parameters */
5164 if (tok == ',')
5165 next();
5166 else
5167 break;
5170 skip(';');
5174 /* parse a function defined by symbol 'sym' and generate its code in
5175 'cur_text_section' */
5176 static void gen_function(Sym *sym)
5178 int saved_nocode_wanted = nocode_wanted;
5179 nocode_wanted = 0;
5180 ind = cur_text_section->data_offset;
5181 /* NOTE: we patch the symbol size later */
5182 put_extern_sym(sym, cur_text_section, ind, 0);
5183 funcname = get_tok_str(sym->v, NULL);
5184 func_ind = ind;
5185 /* put debug symbol */
5186 if (tcc_state->do_debug)
5187 put_func_debug(sym);
5188 /* push a dummy symbol to enable local sym storage */
5189 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5190 gfunc_prolog(&sym->type);
5191 rsym = 0;
5192 block(NULL, NULL, NULL, NULL, 0, 0);
5193 gsym(rsym);
5194 gfunc_epilog();
5195 cur_text_section->data_offset = ind;
5196 label_pop(&global_label_stack, NULL);
5197 sym_pop(&local_stack, NULL); /* reset local stack */
5198 /* end of function */
5199 /* patch symbol size */
5200 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5201 ind - func_ind;
5202 if (tcc_state->do_debug) {
5203 put_stabn(N_FUN, 0, 0, ind - func_ind);
5205 /* It's better to crash than to generate wrong code */
5206 cur_text_section = NULL;
5207 funcname = ""; /* for safety */
5208 func_vt.t = VT_VOID; /* for safety */
5209 ind = 0; /* for safety */
5210 nocode_wanted = saved_nocode_wanted;
5213 ST_FUNC void gen_inline_functions(void)
5215 Sym *sym;
5216 int *str, inline_generated, i;
5217 struct InlineFunc *fn;
5219 /* iterate while inline function are referenced */
5220 for(;;) {
5221 inline_generated = 0;
5222 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5223 fn = tcc_state->inline_fns[i];
5224 sym = fn->sym;
5225 if (sym && sym->c) {
5226 /* the function was used: generate its code and
5227 convert it to a normal function */
5228 str = fn->token_str;
5229 fn->sym = NULL;
5230 if (file)
5231 strcpy(file->filename, fn->filename);
5232 sym->r = VT_SYM | VT_CONST;
5233 sym->type.t &= ~VT_INLINE;
5235 macro_ptr = str;
5236 next();
5237 cur_text_section = text_section;
5238 gen_function(sym);
5239 macro_ptr = NULL; /* fail safe */
5241 inline_generated = 1;
5244 if (!inline_generated)
5245 break;
5247 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5248 fn = tcc_state->inline_fns[i];
5249 str = fn->token_str;
5250 tok_str_free(str);
5252 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5255 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5256 ST_FUNC void decl(int l)
5258 int v, has_init, r;
5259 CType type, btype;
5260 Sym *sym;
5261 AttributeDef ad;
5263 while (1) {
5264 if (!parse_btype(&btype, &ad)) {
5265 /* skip redundant ';' */
5266 /* XXX: find more elegant solution */
5267 if (tok == ';') {
5268 next();
5269 continue;
5271 if (l == VT_CONST &&
5272 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5273 /* global asm block */
5274 asm_global_instr();
5275 continue;
5277 /* special test for old K&R protos without explicit int
5278 type. Only accepted when defining global data */
5279 if (l == VT_LOCAL || tok < TOK_DEFINE)
5280 break;
5281 btype.t = VT_INT;
5283 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5284 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5285 tok == ';') {
5286 /* we accept no variable after */
5287 next();
5288 continue;
5290 while (1) { /* iterate thru each declaration */
5291 type = btype;
5292 type_decl(&type, &ad, &v, TYPE_DIRECT);
5293 #if 0
5295 char buf[500];
5296 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5297 printf("type = '%s'\n", buf);
5299 #endif
5300 if ((type.t & VT_BTYPE) == VT_FUNC) {
5301 /* if old style function prototype, we accept a
5302 declaration list */
5303 sym = type.ref;
5304 if (sym->c == FUNC_OLD)
5305 func_decl_list(sym);
5308 #ifdef TCC_TARGET_PE
5309 if (ad.func_import)
5310 type.t |= VT_IMPORT;
5311 if (ad.func_export)
5312 type.t |= VT_EXPORT;
5313 #endif
5314 if (tok == '{') {
5315 if (l == VT_LOCAL)
5316 error("cannot use local functions");
5317 if ((type.t & VT_BTYPE) != VT_FUNC)
5318 expect("function definition");
5320 /* reject abstract declarators in function definition */
5321 sym = type.ref;
5322 while ((sym = sym->next) != NULL)
5323 if (!(sym->v & ~SYM_FIELD))
5324 expect("identifier");
5326 /* XXX: cannot do better now: convert extern line to static inline */
5327 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5328 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5330 sym = sym_find(v);
5331 if (sym) {
5332 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5333 goto func_error1;
5335 r = sym->type.ref->r;
5336 /* use func_call from prototype if not defined */
5337 if (FUNC_CALL(r) != FUNC_CDECL
5338 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5339 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5341 /* use export from prototype */
5342 if (FUNC_EXPORT(r))
5343 FUNC_EXPORT(type.ref->r) = 1;
5345 /* use static from prototype */
5346 if (sym->type.t & VT_STATIC)
5347 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5349 if (!is_compatible_types(&sym->type, &type)) {
5350 func_error1:
5351 error("incompatible types for redefinition of '%s'",
5352 get_tok_str(v, NULL));
5354 /* if symbol is already defined, then put complete type */
5355 sym->type = type;
5356 } else {
5357 /* put function symbol */
5358 sym = global_identifier_push(v, type.t, 0);
5359 sym->type.ref = type.ref;
5362 /* static inline functions are just recorded as a kind
5363 of macro. Their code will be emitted at the end of
5364 the compilation unit only if they are used */
5365 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5366 (VT_INLINE | VT_STATIC)) {
5367 TokenString func_str;
5368 int block_level;
5369 struct InlineFunc *fn;
5370 const char *filename;
5372 tok_str_new(&func_str);
5374 block_level = 0;
5375 for(;;) {
5376 int t;
5377 if (tok == TOK_EOF)
5378 error("unexpected end of file");
5379 tok_str_add_tok(&func_str);
5380 t = tok;
5381 next();
5382 if (t == '{') {
5383 block_level++;
5384 } else if (t == '}') {
5385 block_level--;
5386 if (block_level == 0)
5387 break;
5390 tok_str_add(&func_str, -1);
5391 tok_str_add(&func_str, 0);
5392 filename = file ? file->filename : "";
5393 fn = tcc_malloc(sizeof *fn + strlen(filename));
5394 strcpy(fn->filename, filename);
5395 fn->sym = sym;
5396 fn->token_str = func_str.str;
5397 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5399 } else {
5400 /* compute text section */
5401 cur_text_section = ad.section;
5402 if (!cur_text_section)
5403 cur_text_section = text_section;
5404 sym->r = VT_SYM | VT_CONST;
5405 gen_function(sym);
5407 break;
5408 } else {
5409 if (btype.t & VT_TYPEDEF) {
5410 /* save typedefed type */
5411 /* XXX: test storage specifiers ? */
5412 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5413 sym->type.t |= VT_TYPEDEF;
5414 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5415 /* external function definition */
5416 /* specific case for func_call attribute */
5417 type.ref->r = INT_ATTR(&ad);
5418 external_sym(v, &type, 0);
5419 } else {
5420 /* not lvalue if array */
5421 r = 0;
5422 if (!(type.t & VT_ARRAY))
5423 r |= lvalue_type(type.t);
5424 has_init = (tok == '=');
5425 if ((btype.t & VT_EXTERN) ||
5426 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5427 !has_init && l == VT_CONST && type.ref->c < 0)) {
5428 /* external variable */
5429 /* NOTE: as GCC, uninitialized global static
5430 arrays of null size are considered as
5431 extern */
5432 external_sym(v, &type, r);
5433 } else {
5434 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5435 if (type.t & VT_STATIC)
5436 r |= VT_CONST;
5437 else
5438 r |= l;
5439 if (has_init)
5440 next();
5441 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
5444 if (tok != ',') {
5445 skip(';');
5446 break;
5448 next();