tccpe: improve dllimport/export and use for tcc_add_symbol
[tinycc.git] / tccgen.c
blobbe8d80f966b44ec2b12d8c4f25114d475fb97697
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_DLLEXPORT:
2505 ad->func_export = 1;
2506 break;
2507 case TOK_DLLIMPORT:
2508 ad->func_import = 1;
2509 break;
2510 default:
2511 if (tcc_state->warn_unsupported)
2512 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2513 /* skip parameters */
2514 if (tok == '(') {
2515 int parenthesis = 0;
2516 do {
2517 if (tok == '(')
2518 parenthesis++;
2519 else if (tok == ')')
2520 parenthesis--;
2521 next();
2522 } while (parenthesis && tok != -1);
2524 break;
2526 if (tok != ',')
2527 break;
2528 next();
2530 skip(')');
2531 skip(')');
2535 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2536 static void struct_decl(CType *type, int u)
2538 int a, v, size, align, maxalign, c, offset;
2539 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2540 Sym *s, *ss, *ass, **ps;
2541 AttributeDef ad;
2542 CType type1, btype;
2544 a = tok; /* save decl type */
2545 next();
2546 if (tok != '{') {
2547 v = tok;
2548 next();
2549 /* struct already defined ? return it */
2550 if (v < TOK_IDENT)
2551 expect("struct/union/enum name");
2552 s = struct_find(v);
2553 if (s) {
2554 if (s->type.t != a)
2555 error("invalid type");
2556 goto do_decl;
2558 } else {
2559 v = anon_sym++;
2561 type1.t = a;
2562 /* we put an undefined size for struct/union */
2563 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2564 s->r = 0; /* default alignment is zero as gcc */
2565 /* put struct/union/enum name in type */
2566 do_decl:
2567 type->t = u;
2568 type->ref = s;
2570 if (tok == '{') {
2571 next();
2572 if (s->c != -1)
2573 error("struct/union/enum already defined");
2574 /* cannot be empty */
2575 c = 0;
2576 /* non empty enums are not allowed */
2577 if (a == TOK_ENUM) {
2578 for(;;) {
2579 v = tok;
2580 if (v < TOK_UIDENT)
2581 expect("identifier");
2582 next();
2583 if (tok == '=') {
2584 next();
2585 c = expr_const();
2587 /* enum symbols have static storage */
2588 ss = sym_push(v, &int_type, VT_CONST, c);
2589 ss->type.t |= VT_STATIC;
2590 if (tok != ',')
2591 break;
2592 next();
2593 c++;
2594 /* NOTE: we accept a trailing comma */
2595 if (tok == '}')
2596 break;
2598 skip('}');
2599 } else {
2600 maxalign = 1;
2601 ps = &s->next;
2602 prevbt = VT_INT;
2603 bit_pos = 0;
2604 offset = 0;
2605 while (tok != '}') {
2606 parse_btype(&btype, &ad);
2607 while (1) {
2608 bit_size = -1;
2609 v = 0;
2610 type1 = btype;
2611 if (tok != ':') {
2612 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2613 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2614 expect("identifier");
2615 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2616 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2617 error("invalid type for '%s'",
2618 get_tok_str(v, NULL));
2620 if (tok == ':') {
2621 next();
2622 bit_size = expr_const();
2623 /* XXX: handle v = 0 case for messages */
2624 if (bit_size < 0)
2625 error("negative width in bit-field '%s'",
2626 get_tok_str(v, NULL));
2627 if (v && bit_size == 0)
2628 error("zero width for bit-field '%s'",
2629 get_tok_str(v, NULL));
2631 size = type_size(&type1, &align);
2632 if (ad.aligned) {
2633 if (align < ad.aligned)
2634 align = ad.aligned;
2635 } else if (ad.packed) {
2636 align = 1;
2637 } else if (*tcc_state->pack_stack_ptr) {
2638 if (align > *tcc_state->pack_stack_ptr)
2639 align = *tcc_state->pack_stack_ptr;
2641 lbit_pos = 0;
2642 if (bit_size >= 0) {
2643 bt = type1.t & VT_BTYPE;
2644 if (bt != VT_INT &&
2645 bt != VT_BYTE &&
2646 bt != VT_SHORT &&
2647 bt != VT_BOOL &&
2648 bt != VT_ENUM &&
2649 bt != VT_LLONG)
2650 error("bitfields must have scalar type");
2651 bsize = size * 8;
2652 if (bit_size > bsize) {
2653 error("width of '%s' exceeds its type",
2654 get_tok_str(v, NULL));
2655 } else if (bit_size == bsize) {
2656 /* no need for bit fields */
2657 bit_pos = 0;
2658 } else if (bit_size == 0) {
2659 /* XXX: what to do if only padding in a
2660 structure ? */
2661 /* zero size: means to pad */
2662 bit_pos = 0;
2663 } else {
2664 /* we do not have enough room ?
2665 did the type change?
2666 is it a union? */
2667 if ((bit_pos + bit_size) > bsize ||
2668 bt != prevbt || a == TOK_UNION)
2669 bit_pos = 0;
2670 lbit_pos = bit_pos;
2671 /* XXX: handle LSB first */
2672 type1.t |= VT_BITFIELD |
2673 (bit_pos << VT_STRUCT_SHIFT) |
2674 (bit_size << (VT_STRUCT_SHIFT + 6));
2675 bit_pos += bit_size;
2677 prevbt = bt;
2678 } else {
2679 bit_pos = 0;
2681 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2682 /* add new memory data only if starting
2683 bit field */
2684 if (lbit_pos == 0) {
2685 if (a == TOK_STRUCT) {
2686 c = (c + align - 1) & -align;
2687 offset = c;
2688 if (size > 0)
2689 c += size;
2690 } else {
2691 offset = 0;
2692 if (size > c)
2693 c = size;
2695 if (align > maxalign)
2696 maxalign = align;
2698 #if 0
2699 printf("add field %s offset=%d",
2700 get_tok_str(v, NULL), offset);
2701 if (type1.t & VT_BITFIELD) {
2702 printf(" pos=%d size=%d",
2703 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2704 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2706 printf("\n");
2707 #endif
2709 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2710 ass = type1.ref;
2711 while ((ass = ass->next) != NULL) {
2712 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2713 *ps = ss;
2714 ps = &ss->next;
2716 } else if (v) {
2717 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2718 *ps = ss;
2719 ps = &ss->next;
2721 if (tok == ';' || tok == TOK_EOF)
2722 break;
2723 skip(',');
2725 skip(';');
2727 skip('}');
2728 /* store size and alignment */
2729 s->c = (c + maxalign - 1) & -maxalign;
2730 s->r = maxalign;
2735 /* return 0 if no type declaration. otherwise, return the basic type
2736 and skip it.
2738 static int parse_btype(CType *type, AttributeDef *ad)
2740 int t, u, type_found, typespec_found, typedef_found;
2741 Sym *s;
2742 CType type1;
2744 memset(ad, 0, sizeof(AttributeDef));
2745 type_found = 0;
2746 typespec_found = 0;
2747 typedef_found = 0;
2748 t = 0;
2749 while(1) {
2750 switch(tok) {
2751 case TOK_EXTENSION:
2752 /* currently, we really ignore extension */
2753 next();
2754 continue;
2756 /* basic types */
2757 case TOK_CHAR:
2758 u = VT_BYTE;
2759 basic_type:
2760 next();
2761 basic_type1:
2762 if ((t & VT_BTYPE) != 0)
2763 error("too many basic types");
2764 t |= u;
2765 typespec_found = 1;
2766 break;
2767 case TOK_VOID:
2768 u = VT_VOID;
2769 goto basic_type;
2770 case TOK_SHORT:
2771 u = VT_SHORT;
2772 goto basic_type;
2773 case TOK_INT:
2774 next();
2775 typespec_found = 1;
2776 break;
2777 case TOK_LONG:
2778 next();
2779 if ((t & VT_BTYPE) == VT_DOUBLE) {
2780 #ifndef TCC_TARGET_PE
2781 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2782 #endif
2783 } else if ((t & VT_BTYPE) == VT_LONG) {
2784 t = (t & ~VT_BTYPE) | VT_LLONG;
2785 } else {
2786 u = VT_LONG;
2787 goto basic_type1;
2789 break;
2790 case TOK_BOOL:
2791 u = VT_BOOL;
2792 goto basic_type;
2793 case TOK_FLOAT:
2794 u = VT_FLOAT;
2795 goto basic_type;
2796 case TOK_DOUBLE:
2797 next();
2798 if ((t & VT_BTYPE) == VT_LONG) {
2799 #ifdef TCC_TARGET_PE
2800 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2801 #else
2802 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2803 #endif
2804 } else {
2805 u = VT_DOUBLE;
2806 goto basic_type1;
2808 break;
2809 case TOK_ENUM:
2810 struct_decl(&type1, VT_ENUM);
2811 basic_type2:
2812 u = type1.t;
2813 type->ref = type1.ref;
2814 goto basic_type1;
2815 case TOK_STRUCT:
2816 case TOK_UNION:
2817 struct_decl(&type1, VT_STRUCT);
2818 goto basic_type2;
2820 /* type modifiers */
2821 case TOK_CONST1:
2822 case TOK_CONST2:
2823 case TOK_CONST3:
2824 t |= VT_CONSTANT;
2825 next();
2826 break;
2827 case TOK_VOLATILE1:
2828 case TOK_VOLATILE2:
2829 case TOK_VOLATILE3:
2830 t |= VT_VOLATILE;
2831 next();
2832 break;
2833 case TOK_SIGNED1:
2834 case TOK_SIGNED2:
2835 case TOK_SIGNED3:
2836 typespec_found = 1;
2837 t |= VT_SIGNED;
2838 next();
2839 break;
2840 case TOK_REGISTER:
2841 case TOK_AUTO:
2842 case TOK_RESTRICT1:
2843 case TOK_RESTRICT2:
2844 case TOK_RESTRICT3:
2845 next();
2846 break;
2847 case TOK_UNSIGNED:
2848 t |= VT_UNSIGNED;
2849 next();
2850 typespec_found = 1;
2851 break;
2853 /* storage */
2854 case TOK_EXTERN:
2855 t |= VT_EXTERN;
2856 next();
2857 break;
2858 case TOK_STATIC:
2859 t |= VT_STATIC;
2860 next();
2861 break;
2862 case TOK_TYPEDEF:
2863 t |= VT_TYPEDEF;
2864 next();
2865 break;
2866 case TOK_INLINE1:
2867 case TOK_INLINE2:
2868 case TOK_INLINE3:
2869 t |= VT_INLINE;
2870 next();
2871 break;
2873 /* GNUC attribute */
2874 case TOK_ATTRIBUTE1:
2875 case TOK_ATTRIBUTE2:
2876 parse_attribute(ad);
2877 break;
2878 /* GNUC typeof */
2879 case TOK_TYPEOF1:
2880 case TOK_TYPEOF2:
2881 case TOK_TYPEOF3:
2882 next();
2883 parse_expr_type(&type1);
2884 goto basic_type2;
2885 default:
2886 if (typespec_found || typedef_found)
2887 goto the_end;
2888 s = sym_find(tok);
2889 if (!s || !(s->type.t & VT_TYPEDEF))
2890 goto the_end;
2891 typedef_found = 1;
2892 t |= (s->type.t & ~VT_TYPEDEF);
2893 type->ref = s->type.ref;
2894 if (s->r) {
2895 /* get attributes from typedef */
2896 if (0 == ad->aligned)
2897 ad->aligned = FUNC_ALIGN(s->r);
2898 if (0 == ad->func_call)
2899 ad->func_call = FUNC_CALL(s->r);
2900 ad->packed |= FUNC_PACKED(s->r);
2902 next();
2903 typespec_found = 1;
2904 break;
2906 type_found = 1;
2908 the_end:
2909 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2910 error("signed and unsigned modifier");
2911 if (tcc_state->char_is_unsigned) {
2912 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2913 t |= VT_UNSIGNED;
2915 t &= ~VT_SIGNED;
2917 /* long is never used as type */
2918 if ((t & VT_BTYPE) == VT_LONG)
2919 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2920 t = (t & ~VT_BTYPE) | VT_INT;
2921 #else
2922 t = (t & ~VT_BTYPE) | VT_LLONG;
2923 #endif
2924 type->t = t;
2925 return type_found;
2928 /* convert a function parameter type (array to pointer and function to
2929 function pointer) */
2930 static inline void convert_parameter_type(CType *pt)
2932 /* remove const and volatile qualifiers (XXX: const could be used
2933 to indicate a const function parameter */
2934 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2935 /* array must be transformed to pointer according to ANSI C */
2936 pt->t &= ~VT_ARRAY;
2937 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2938 mk_pointer(pt);
2942 static void post_type(CType *type, AttributeDef *ad)
2944 int n, l, t1, arg_size, align;
2945 Sym **plast, *s, *first;
2946 AttributeDef ad1;
2947 CType pt;
2949 if (tok == '(') {
2950 /* function declaration */
2951 next();
2952 l = 0;
2953 first = NULL;
2954 plast = &first;
2955 arg_size = 0;
2956 if (tok != ')') {
2957 for(;;) {
2958 /* read param name and compute offset */
2959 if (l != FUNC_OLD) {
2960 if (!parse_btype(&pt, &ad1)) {
2961 if (l) {
2962 error("invalid type");
2963 } else {
2964 l = FUNC_OLD;
2965 goto old_proto;
2968 l = FUNC_NEW;
2969 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2970 break;
2971 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2972 if ((pt.t & VT_BTYPE) == VT_VOID)
2973 error("parameter declared as void");
2974 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
2975 } else {
2976 old_proto:
2977 n = tok;
2978 if (n < TOK_UIDENT)
2979 expect("identifier");
2980 pt.t = VT_INT;
2981 next();
2983 convert_parameter_type(&pt);
2984 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
2985 *plast = s;
2986 plast = &s->next;
2987 if (tok == ')')
2988 break;
2989 skip(',');
2990 if (l == FUNC_NEW && tok == TOK_DOTS) {
2991 l = FUNC_ELLIPSIS;
2992 next();
2993 break;
2997 /* if no parameters, then old type prototype */
2998 if (l == 0)
2999 l = FUNC_OLD;
3000 skip(')');
3001 t1 = type->t & VT_STORAGE;
3002 /* NOTE: const is ignored in returned type as it has a special
3003 meaning in gcc / C++ */
3004 type->t &= ~(VT_STORAGE | VT_CONSTANT);
3005 post_type(type, ad);
3006 /* we push a anonymous symbol which will contain the function prototype */
3007 ad->func_args = arg_size;
3008 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3009 s->next = first;
3010 type->t = t1 | VT_FUNC;
3011 type->ref = s;
3012 } else if (tok == '[') {
3013 /* array definition */
3014 next();
3015 if (tok == TOK_RESTRICT1)
3016 next();
3017 n = -1;
3018 if (tok != ']') {
3019 n = expr_const();
3020 if (n < 0)
3021 error("invalid array size");
3023 skip(']');
3024 /* parse next post type */
3025 t1 = type->t & VT_STORAGE;
3026 type->t &= ~VT_STORAGE;
3027 post_type(type, ad);
3029 /* we push a anonymous symbol which will contain the array
3030 element type */
3031 s = sym_push(SYM_FIELD, type, 0, n);
3032 type->t = t1 | VT_ARRAY | VT_PTR;
3033 type->ref = s;
3037 /* Parse a type declaration (except basic type), and return the type
3038 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3039 expected. 'type' should contain the basic type. 'ad' is the
3040 attribute definition of the basic type. It can be modified by
3041 type_decl().
3043 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3045 Sym *s;
3046 CType type1, *type2;
3047 int qualifiers;
3049 while (tok == '*') {
3050 qualifiers = 0;
3051 redo:
3052 next();
3053 switch(tok) {
3054 case TOK_CONST1:
3055 case TOK_CONST2:
3056 case TOK_CONST3:
3057 qualifiers |= VT_CONSTANT;
3058 goto redo;
3059 case TOK_VOLATILE1:
3060 case TOK_VOLATILE2:
3061 case TOK_VOLATILE3:
3062 qualifiers |= VT_VOLATILE;
3063 goto redo;
3064 case TOK_RESTRICT1:
3065 case TOK_RESTRICT2:
3066 case TOK_RESTRICT3:
3067 goto redo;
3069 mk_pointer(type);
3070 type->t |= qualifiers;
3073 /* XXX: clarify attribute handling */
3074 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3075 parse_attribute(ad);
3077 /* recursive type */
3078 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3079 type1.t = 0; /* XXX: same as int */
3080 if (tok == '(') {
3081 next();
3082 /* XXX: this is not correct to modify 'ad' at this point, but
3083 the syntax is not clear */
3084 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3085 parse_attribute(ad);
3086 type_decl(&type1, ad, v, td);
3087 skip(')');
3088 } else {
3089 /* type identifier */
3090 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3091 *v = tok;
3092 next();
3093 } else {
3094 if (!(td & TYPE_ABSTRACT))
3095 expect("identifier");
3096 *v = 0;
3099 post_type(type, ad);
3100 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3101 parse_attribute(ad);
3102 if (!type1.t)
3103 return;
3104 /* append type at the end of type1 */
3105 type2 = &type1;
3106 for(;;) {
3107 s = type2->ref;
3108 type2 = &s->type;
3109 if (!type2->t) {
3110 *type2 = *type;
3111 break;
3114 *type = type1;
3117 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3118 ST_FUNC int lvalue_type(int t)
3120 int bt, r;
3121 r = VT_LVAL;
3122 bt = t & VT_BTYPE;
3123 if (bt == VT_BYTE || bt == VT_BOOL)
3124 r |= VT_LVAL_BYTE;
3125 else if (bt == VT_SHORT)
3126 r |= VT_LVAL_SHORT;
3127 else
3128 return r;
3129 if (t & VT_UNSIGNED)
3130 r |= VT_LVAL_UNSIGNED;
3131 return r;
3134 /* indirection with full error checking and bound check */
3135 ST_FUNC void indir(void)
3137 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3138 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3139 return;
3140 expect("pointer");
3142 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3143 gv(RC_INT);
3144 vtop->type = *pointed_type(&vtop->type);
3145 /* Arrays and functions are never lvalues */
3146 if (!(vtop->type.t & VT_ARRAY)
3147 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3148 vtop->r |= lvalue_type(vtop->type.t);
3149 /* if bound checking, the referenced pointer must be checked */
3150 #ifdef CONFIG_TCC_BCHECK
3151 if (tcc_state->do_bounds_check)
3152 vtop->r |= VT_MUSTBOUND;
3153 #endif
3157 /* pass a parameter to a function and do type checking and casting */
3158 static void gfunc_param_typed(Sym *func, Sym *arg)
3160 int func_type;
3161 CType type;
3163 func_type = func->c;
3164 if (func_type == FUNC_OLD ||
3165 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3166 /* default casting : only need to convert float to double */
3167 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3168 type.t = VT_DOUBLE;
3169 gen_cast(&type);
3171 } else if (arg == NULL) {
3172 error("too many arguments to function");
3173 } else {
3174 type = arg->type;
3175 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3176 gen_assign_cast(&type);
3180 /* parse an expression of the form '(type)' or '(expr)' and return its
3181 type */
3182 static void parse_expr_type(CType *type)
3184 int n;
3185 AttributeDef ad;
3187 skip('(');
3188 if (parse_btype(type, &ad)) {
3189 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3190 } else {
3191 expr_type(type);
3193 skip(')');
3196 static void parse_type(CType *type)
3198 AttributeDef ad;
3199 int n;
3201 if (!parse_btype(type, &ad)) {
3202 expect("type");
3204 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3207 static void vpush_tokc(int t)
3209 CType type;
3210 type.t = t;
3211 type.ref = 0;
3212 vsetc(&type, VT_CONST, &tokc);
3215 ST_FUNC void unary(void)
3217 int n, t, align, size, r;
3218 CType type;
3219 Sym *s;
3220 AttributeDef ad;
3222 /* XXX: GCC 2.95.3 does not generate a table although it should be
3223 better here */
3224 tok_next:
3225 switch(tok) {
3226 case TOK_EXTENSION:
3227 next();
3228 goto tok_next;
3229 case TOK_CINT:
3230 case TOK_CCHAR:
3231 case TOK_LCHAR:
3232 vpushi(tokc.i);
3233 next();
3234 break;
3235 case TOK_CUINT:
3236 vpush_tokc(VT_INT | VT_UNSIGNED);
3237 next();
3238 break;
3239 case TOK_CLLONG:
3240 vpush_tokc(VT_LLONG);
3241 next();
3242 break;
3243 case TOK_CULLONG:
3244 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3245 next();
3246 break;
3247 case TOK_CFLOAT:
3248 vpush_tokc(VT_FLOAT);
3249 next();
3250 break;
3251 case TOK_CDOUBLE:
3252 vpush_tokc(VT_DOUBLE);
3253 next();
3254 break;
3255 case TOK_CLDOUBLE:
3256 vpush_tokc(VT_LDOUBLE);
3257 next();
3258 break;
3259 case TOK___FUNCTION__:
3260 if (!gnu_ext)
3261 goto tok_identifier;
3262 /* fall thru */
3263 case TOK___FUNC__:
3265 void *ptr;
3266 int len;
3267 /* special function name identifier */
3268 len = strlen(funcname) + 1;
3269 /* generate char[len] type */
3270 type.t = VT_BYTE;
3271 mk_pointer(&type);
3272 type.t |= VT_ARRAY;
3273 type.ref->c = len;
3274 vpush_ref(&type, data_section, data_section->data_offset, len);
3275 ptr = section_ptr_add(data_section, len);
3276 memcpy(ptr, funcname, len);
3277 next();
3279 break;
3280 case TOK_LSTR:
3281 #ifdef TCC_TARGET_PE
3282 t = VT_SHORT | VT_UNSIGNED;
3283 #else
3284 t = VT_INT;
3285 #endif
3286 goto str_init;
3287 case TOK_STR:
3288 /* string parsing */
3289 t = VT_BYTE;
3290 str_init:
3291 if (tcc_state->warn_write_strings)
3292 t |= VT_CONSTANT;
3293 type.t = t;
3294 mk_pointer(&type);
3295 type.t |= VT_ARRAY;
3296 memset(&ad, 0, sizeof(AttributeDef));
3297 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3298 break;
3299 case '(':
3300 next();
3301 /* cast ? */
3302 if (parse_btype(&type, &ad)) {
3303 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3304 skip(')');
3305 /* check ISOC99 compound literal */
3306 if (tok == '{') {
3307 /* data is allocated locally by default */
3308 if (global_expr)
3309 r = VT_CONST;
3310 else
3311 r = VT_LOCAL;
3312 /* all except arrays are lvalues */
3313 if (!(type.t & VT_ARRAY))
3314 r |= lvalue_type(type.t);
3315 memset(&ad, 0, sizeof(AttributeDef));
3316 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3317 } else {
3318 unary();
3319 gen_cast(&type);
3321 } else if (tok == '{') {
3322 /* save all registers */
3323 save_regs(0);
3324 /* statement expression : we do not accept break/continue
3325 inside as GCC does */
3326 block(NULL, NULL, NULL, NULL, 0, 1);
3327 skip(')');
3328 } else {
3329 gexpr();
3330 skip(')');
3332 break;
3333 case '*':
3334 next();
3335 unary();
3336 indir();
3337 break;
3338 case '&':
3339 next();
3340 unary();
3341 /* functions names must be treated as function pointers,
3342 except for unary '&' and sizeof. Since we consider that
3343 functions are not lvalues, we only have to handle it
3344 there and in function calls. */
3345 /* arrays can also be used although they are not lvalues */
3346 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3347 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3348 test_lvalue();
3349 mk_pointer(&vtop->type);
3350 gaddrof();
3351 break;
3352 case '!':
3353 next();
3354 unary();
3355 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3356 CType boolean;
3357 boolean.t = VT_BOOL;
3358 gen_cast(&boolean);
3359 vtop->c.i = !vtop->c.i;
3360 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3361 vtop->c.i = vtop->c.i ^ 1;
3362 else {
3363 save_regs(1);
3364 vseti(VT_JMP, gtst(1, 0));
3366 break;
3367 case '~':
3368 next();
3369 unary();
3370 vpushi(-1);
3371 gen_op('^');
3372 break;
3373 case '+':
3374 next();
3375 /* in order to force cast, we add zero */
3376 unary();
3377 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3378 error("pointer not accepted for unary plus");
3379 vpushi(0);
3380 gen_op('+');
3381 break;
3382 case TOK_SIZEOF:
3383 case TOK_ALIGNOF1:
3384 case TOK_ALIGNOF2:
3385 t = tok;
3386 next();
3387 if (tok == '(') {
3388 parse_expr_type(&type);
3389 } else {
3390 unary_type(&type);
3392 size = type_size(&type, &align);
3393 if (t == TOK_SIZEOF) {
3394 if (size < 0)
3395 error("sizeof applied to an incomplete type");
3396 vpushi(size);
3397 } else {
3398 vpushi(align);
3400 vtop->type.t |= VT_UNSIGNED;
3401 break;
3403 case TOK_builtin_types_compatible_p:
3405 CType type1, type2;
3406 next();
3407 skip('(');
3408 parse_type(&type1);
3409 skip(',');
3410 parse_type(&type2);
3411 skip(')');
3412 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3413 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3414 vpushi(is_compatible_types(&type1, &type2));
3416 break;
3417 case TOK_builtin_constant_p:
3419 int saved_nocode_wanted, res;
3420 next();
3421 skip('(');
3422 saved_nocode_wanted = nocode_wanted;
3423 nocode_wanted = 1;
3424 gexpr();
3425 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3426 vpop();
3427 nocode_wanted = saved_nocode_wanted;
3428 skip(')');
3429 vpushi(res);
3431 break;
3432 case TOK_builtin_frame_address:
3434 CType type;
3435 next();
3436 skip('(');
3437 if (tok != TOK_CINT) {
3438 error("__builtin_frame_address only takes integers");
3440 if (tokc.i != 0) {
3441 error("TCC only supports __builtin_frame_address(0)");
3443 next();
3444 skip(')');
3445 type.t = VT_VOID;
3446 mk_pointer(&type);
3447 vset(&type, VT_LOCAL, 0);
3449 break;
3450 #ifdef TCC_TARGET_X86_64
3451 case TOK_builtin_malloc:
3452 tok = TOK_malloc;
3453 goto tok_identifier;
3454 case TOK_builtin_free:
3455 tok = TOK_free;
3456 goto tok_identifier;
3457 #endif
3458 case TOK_INC:
3459 case TOK_DEC:
3460 t = tok;
3461 next();
3462 unary();
3463 inc(0, t);
3464 break;
3465 case '-':
3466 next();
3467 vpushi(0);
3468 unary();
3469 gen_op('-');
3470 break;
3471 case TOK_LAND:
3472 if (!gnu_ext)
3473 goto tok_identifier;
3474 next();
3475 /* allow to take the address of a label */
3476 if (tok < TOK_UIDENT)
3477 expect("label identifier");
3478 s = label_find(tok);
3479 if (!s) {
3480 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3481 } else {
3482 if (s->r == LABEL_DECLARED)
3483 s->r = LABEL_FORWARD;
3485 if (!s->type.t) {
3486 s->type.t = VT_VOID;
3487 mk_pointer(&s->type);
3488 s->type.t |= VT_STATIC;
3490 vset(&s->type, VT_CONST | VT_SYM, 0);
3491 vtop->sym = s;
3492 next();
3493 break;
3494 default:
3495 tok_identifier:
3496 t = tok;
3497 next();
3498 if (t < TOK_UIDENT)
3499 expect("identifier");
3500 s = sym_find(t);
3501 if (!s) {
3502 if (tok != '(')
3503 error("'%s' undeclared", get_tok_str(t, NULL));
3504 /* for simple function calls, we tolerate undeclared
3505 external reference to int() function */
3506 if (tcc_state->warn_implicit_function_declaration)
3507 warning("implicit declaration of function '%s'",
3508 get_tok_str(t, NULL));
3509 s = external_global_sym(t, &func_old_type, 0);
3511 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3512 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3513 /* if referencing an inline function, then we generate a
3514 symbol to it if not already done. It will have the
3515 effect to generate code for it at the end of the
3516 compilation unit. Inline function as always
3517 generated in the text section. */
3518 if (!s->c)
3519 put_extern_sym(s, text_section, 0, 0);
3520 r = VT_SYM | VT_CONST;
3521 } else {
3522 r = s->r;
3524 vset(&s->type, r, s->c);
3525 /* if forward reference, we must point to s */
3526 if (vtop->r & VT_SYM) {
3527 vtop->sym = s;
3528 vtop->c.ul = 0;
3530 break;
3533 /* post operations */
3534 while (1) {
3535 if (tok == TOK_INC || tok == TOK_DEC) {
3536 inc(1, tok);
3537 next();
3538 } else if (tok == '.' || tok == TOK_ARROW) {
3539 int qualifiers;
3540 /* field */
3541 if (tok == TOK_ARROW)
3542 indir();
3543 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3544 test_lvalue();
3545 gaddrof();
3546 next();
3547 /* expect pointer on structure */
3548 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3549 expect("struct or union");
3550 s = vtop->type.ref;
3551 /* find field */
3552 tok |= SYM_FIELD;
3553 while ((s = s->next) != NULL) {
3554 if (s->v == tok)
3555 break;
3557 if (!s)
3558 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3559 /* add field offset to pointer */
3560 vtop->type = char_pointer_type; /* change type to 'char *' */
3561 vpushi(s->c);
3562 gen_op('+');
3563 /* change type to field type, and set to lvalue */
3564 vtop->type = s->type;
3565 vtop->type.t |= qualifiers;
3566 /* an array is never an lvalue */
3567 if (!(vtop->type.t & VT_ARRAY)) {
3568 vtop->r |= lvalue_type(vtop->type.t);
3569 #ifdef CONFIG_TCC_BCHECK
3570 /* if bound checking, the referenced pointer must be checked */
3571 if (tcc_state->do_bounds_check)
3572 vtop->r |= VT_MUSTBOUND;
3573 #endif
3575 next();
3576 } else if (tok == '[') {
3577 next();
3578 gexpr();
3579 gen_op('+');
3580 indir();
3581 skip(']');
3582 } else if (tok == '(') {
3583 SValue ret;
3584 Sym *sa;
3585 int nb_args;
3587 /* function call */
3588 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3589 /* pointer test (no array accepted) */
3590 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3591 vtop->type = *pointed_type(&vtop->type);
3592 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3593 goto error_func;
3594 } else {
3595 error_func:
3596 expect("function pointer");
3598 } else {
3599 vtop->r &= ~VT_LVAL; /* no lvalue */
3601 /* get return type */
3602 s = vtop->type.ref;
3603 next();
3604 sa = s->next; /* first parameter */
3605 nb_args = 0;
3606 ret.r2 = VT_CONST;
3607 /* compute first implicit argument if a structure is returned */
3608 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3609 /* get some space for the returned structure */
3610 size = type_size(&s->type, &align);
3611 loc = (loc - size) & -align;
3612 ret.type = s->type;
3613 ret.r = VT_LOCAL | VT_LVAL;
3614 /* pass it as 'int' to avoid structure arg passing
3615 problems */
3616 vseti(VT_LOCAL, loc);
3617 ret.c = vtop->c;
3618 nb_args++;
3619 } else {
3620 ret.type = s->type;
3621 /* return in register */
3622 if (is_float(ret.type.t)) {
3623 ret.r = reg_fret(ret.type.t);
3624 } else {
3625 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3626 ret.r2 = REG_LRET;
3627 ret.r = REG_IRET;
3629 ret.c.i = 0;
3631 if (tok != ')') {
3632 for(;;) {
3633 expr_eq();
3634 gfunc_param_typed(s, sa);
3635 nb_args++;
3636 if (sa)
3637 sa = sa->next;
3638 if (tok == ')')
3639 break;
3640 skip(',');
3643 if (sa)
3644 error("too few arguments to function");
3645 skip(')');
3646 if (!nocode_wanted) {
3647 gfunc_call(nb_args);
3648 } else {
3649 vtop -= (nb_args + 1);
3651 /* return value */
3652 vsetc(&ret.type, ret.r, &ret.c);
3653 vtop->r2 = ret.r2;
3654 } else {
3655 break;
3660 static void uneq(void)
3662 int t;
3664 unary();
3665 if (tok == '=' ||
3666 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3667 tok == TOK_A_XOR || tok == TOK_A_OR ||
3668 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3669 test_lvalue();
3670 t = tok;
3671 next();
3672 if (t == '=') {
3673 expr_eq();
3674 } else {
3675 vdup();
3676 expr_eq();
3677 gen_op(t & 0x7f);
3679 vstore();
3683 ST_FUNC void expr_prod(void)
3685 int t;
3687 uneq();
3688 while (tok == '*' || tok == '/' || tok == '%') {
3689 t = tok;
3690 next();
3691 uneq();
3692 gen_op(t);
3696 ST_FUNC void expr_sum(void)
3698 int t;
3700 expr_prod();
3701 while (tok == '+' || tok == '-') {
3702 t = tok;
3703 next();
3704 expr_prod();
3705 gen_op(t);
3709 static void expr_shift(void)
3711 int t;
3713 expr_sum();
3714 while (tok == TOK_SHL || tok == TOK_SAR) {
3715 t = tok;
3716 next();
3717 expr_sum();
3718 gen_op(t);
3722 static void expr_cmp(void)
3724 int t;
3726 expr_shift();
3727 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3728 tok == TOK_ULT || tok == TOK_UGE) {
3729 t = tok;
3730 next();
3731 expr_shift();
3732 gen_op(t);
3736 static void expr_cmpeq(void)
3738 int t;
3740 expr_cmp();
3741 while (tok == TOK_EQ || tok == TOK_NE) {
3742 t = tok;
3743 next();
3744 expr_cmp();
3745 gen_op(t);
3749 static void expr_and(void)
3751 expr_cmpeq();
3752 while (tok == '&') {
3753 next();
3754 expr_cmpeq();
3755 gen_op('&');
3759 static void expr_xor(void)
3761 expr_and();
3762 while (tok == '^') {
3763 next();
3764 expr_and();
3765 gen_op('^');
3769 static void expr_or(void)
3771 expr_xor();
3772 while (tok == '|') {
3773 next();
3774 expr_xor();
3775 gen_op('|');
3779 /* XXX: fix this mess */
3780 static void expr_land_const(void)
3782 expr_or();
3783 while (tok == TOK_LAND) {
3784 next();
3785 expr_or();
3786 gen_op(TOK_LAND);
3790 /* XXX: fix this mess */
3791 static void expr_lor_const(void)
3793 expr_land_const();
3794 while (tok == TOK_LOR) {
3795 next();
3796 expr_land_const();
3797 gen_op(TOK_LOR);
3801 /* only used if non constant */
3802 static void expr_land(void)
3804 int t;
3806 expr_or();
3807 if (tok == TOK_LAND) {
3808 t = 0;
3809 save_regs(1);
3810 for(;;) {
3811 t = gtst(1, t);
3812 if (tok != TOK_LAND) {
3813 vseti(VT_JMPI, t);
3814 break;
3816 next();
3817 expr_or();
3822 static void expr_lor(void)
3824 int t;
3826 expr_land();
3827 if (tok == TOK_LOR) {
3828 t = 0;
3829 save_regs(1);
3830 for(;;) {
3831 t = gtst(0, t);
3832 if (tok != TOK_LOR) {
3833 vseti(VT_JMP, t);
3834 break;
3836 next();
3837 expr_land();
3842 /* XXX: better constant handling */
3843 static void expr_eq(void)
3845 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3846 SValue sv;
3847 CType type, type1, type2;
3849 if (const_wanted) {
3850 expr_lor_const();
3851 if (tok == '?') {
3852 CType boolean;
3853 int c;
3854 boolean.t = VT_BOOL;
3855 vdup();
3856 gen_cast(&boolean);
3857 c = vtop->c.i;
3858 vpop();
3859 next();
3860 if (tok != ':' || !gnu_ext) {
3861 vpop();
3862 gexpr();
3864 if (!c)
3865 vpop();
3866 skip(':');
3867 expr_eq();
3868 if (c)
3869 vpop();
3871 } else {
3872 expr_lor();
3873 if (tok == '?') {
3874 next();
3875 if (vtop != vstack) {
3876 /* needed to avoid having different registers saved in
3877 each branch */
3878 if (is_float(vtop->type.t)) {
3879 rc = RC_FLOAT;
3880 #ifdef TCC_TARGET_X86_64
3881 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3882 rc = RC_ST0;
3884 #endif
3886 else
3887 rc = RC_INT;
3888 gv(rc);
3889 save_regs(1);
3891 if (tok == ':' && gnu_ext) {
3892 gv_dup();
3893 tt = gtst(1, 0);
3894 } else {
3895 tt = gtst(1, 0);
3896 gexpr();
3898 type1 = vtop->type;
3899 sv = *vtop; /* save value to handle it later */
3900 vtop--; /* no vpop so that FP stack is not flushed */
3901 skip(':');
3902 u = gjmp(0);
3903 gsym(tt);
3904 expr_eq();
3905 type2 = vtop->type;
3907 t1 = type1.t;
3908 bt1 = t1 & VT_BTYPE;
3909 t2 = type2.t;
3910 bt2 = t2 & VT_BTYPE;
3911 /* cast operands to correct type according to ISOC rules */
3912 if (is_float(bt1) || is_float(bt2)) {
3913 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3914 type.t = VT_LDOUBLE;
3915 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3916 type.t = VT_DOUBLE;
3917 } else {
3918 type.t = VT_FLOAT;
3920 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3921 /* cast to biggest op */
3922 type.t = VT_LLONG;
3923 /* convert to unsigned if it does not fit in a long long */
3924 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3925 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3926 type.t |= VT_UNSIGNED;
3927 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3928 /* XXX: test pointer compatibility */
3929 type = type1;
3930 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3931 /* XXX: test function pointer compatibility */
3932 type = type1;
3933 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3934 /* XXX: test structure compatibility */
3935 type = type1;
3936 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3937 /* NOTE: as an extension, we accept void on only one side */
3938 type.t = VT_VOID;
3939 } else {
3940 /* integer operations */
3941 type.t = VT_INT;
3942 /* convert to unsigned if it does not fit in an integer */
3943 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3944 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3945 type.t |= VT_UNSIGNED;
3948 /* now we convert second operand */
3949 gen_cast(&type);
3950 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3951 gaddrof();
3952 rc = RC_INT;
3953 if (is_float(type.t)) {
3954 rc = RC_FLOAT;
3955 #ifdef TCC_TARGET_X86_64
3956 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3957 rc = RC_ST0;
3959 #endif
3960 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3961 /* for long longs, we use fixed registers to avoid having
3962 to handle a complicated move */
3963 rc = RC_IRET;
3966 r2 = gv(rc);
3967 /* this is horrible, but we must also convert first
3968 operand */
3969 tt = gjmp(0);
3970 gsym(u);
3971 /* put again first value and cast it */
3972 *vtop = sv;
3973 gen_cast(&type);
3974 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3975 gaddrof();
3976 r1 = gv(rc);
3977 move_reg(r2, r1);
3978 vtop->r = r2;
3979 gsym(tt);
3984 ST_FUNC void gexpr(void)
3986 while (1) {
3987 expr_eq();
3988 if (tok != ',')
3989 break;
3990 vpop();
3991 next();
3995 /* parse an expression and return its type without any side effect. */
3996 static void expr_type(CType *type)
3998 int saved_nocode_wanted;
4000 saved_nocode_wanted = nocode_wanted;
4001 nocode_wanted = 1;
4002 gexpr();
4003 *type = vtop->type;
4004 vpop();
4005 nocode_wanted = saved_nocode_wanted;
4008 /* parse a unary expression and return its type without any side
4009 effect. */
4010 static void unary_type(CType *type)
4012 int a;
4014 a = nocode_wanted;
4015 nocode_wanted = 1;
4016 unary();
4017 *type = vtop->type;
4018 vpop();
4019 nocode_wanted = a;
4022 /* parse a constant expression and return value in vtop. */
4023 static void expr_const1(void)
4025 int a;
4026 a = const_wanted;
4027 const_wanted = 1;
4028 expr_eq();
4029 const_wanted = a;
4032 /* parse an integer constant and return its value. */
4033 ST_FUNC int expr_const(void)
4035 int c;
4036 expr_const1();
4037 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4038 expect("constant expression");
4039 c = vtop->c.i;
4040 vpop();
4041 return c;
4044 /* return the label token if current token is a label, otherwise
4045 return zero */
4046 static int is_label(void)
4048 int last_tok;
4050 /* fast test first */
4051 if (tok < TOK_UIDENT)
4052 return 0;
4053 /* no need to save tokc because tok is an identifier */
4054 last_tok = tok;
4055 next();
4056 if (tok == ':') {
4057 next();
4058 return last_tok;
4059 } else {
4060 unget_tok(last_tok);
4061 return 0;
4065 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4066 int case_reg, int is_expr)
4068 int a, b, c, d;
4069 Sym *s;
4071 /* generate line number info */
4072 if (tcc_state->do_debug &&
4073 (last_line_num != file->line_num || last_ind != ind)) {
4074 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4075 last_ind = ind;
4076 last_line_num = file->line_num;
4079 if (is_expr) {
4080 /* default return value is (void) */
4081 vpushi(0);
4082 vtop->type.t = VT_VOID;
4085 if (tok == TOK_IF) {
4086 /* if test */
4087 next();
4088 skip('(');
4089 gexpr();
4090 skip(')');
4091 a = gtst(1, 0);
4092 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4093 c = tok;
4094 if (c == TOK_ELSE) {
4095 next();
4096 d = gjmp(0);
4097 gsym(a);
4098 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4099 gsym(d); /* patch else jmp */
4100 } else
4101 gsym(a);
4102 } else if (tok == TOK_WHILE) {
4103 next();
4104 d = ind;
4105 skip('(');
4106 gexpr();
4107 skip(')');
4108 a = gtst(1, 0);
4109 b = 0;
4110 block(&a, &b, case_sym, def_sym, case_reg, 0);
4111 gjmp_addr(d);
4112 gsym(a);
4113 gsym_addr(b, d);
4114 } else if (tok == '{') {
4115 Sym *llabel;
4117 next();
4118 /* record local declaration stack position */
4119 s = local_stack;
4120 llabel = local_label_stack;
4121 /* handle local labels declarations */
4122 if (tok == TOK_LABEL) {
4123 next();
4124 for(;;) {
4125 if (tok < TOK_UIDENT)
4126 expect("label identifier");
4127 label_push(&local_label_stack, tok, LABEL_DECLARED);
4128 next();
4129 if (tok == ',') {
4130 next();
4131 } else {
4132 skip(';');
4133 break;
4137 while (tok != '}') {
4138 decl(VT_LOCAL);
4139 if (tok != '}') {
4140 if (is_expr)
4141 vpop();
4142 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4145 /* pop locally defined labels */
4146 label_pop(&local_label_stack, llabel);
4147 /* pop locally defined symbols */
4148 if(is_expr) {
4149 /* XXX: this solution makes only valgrind happy...
4150 triggered by gcc.c-torture/execute/20000917-1.c */
4151 Sym *p;
4152 switch(vtop->type.t & VT_BTYPE) {
4153 case VT_PTR:
4154 case VT_STRUCT:
4155 case VT_ENUM:
4156 case VT_FUNC:
4157 for(p=vtop->type.ref;p;p=p->prev)
4158 if(p->prev==s)
4159 error("unsupported expression type");
4162 sym_pop(&local_stack, s);
4163 next();
4164 } else if (tok == TOK_RETURN) {
4165 next();
4166 if (tok != ';') {
4167 gexpr();
4168 gen_assign_cast(&func_vt);
4169 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4170 CType type;
4171 /* if returning structure, must copy it to implicit
4172 first pointer arg location */
4173 #ifdef TCC_ARM_EABI
4174 int align, size;
4175 size = type_size(&func_vt,&align);
4176 if(size <= 4)
4178 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4179 && (align & 3))
4181 int addr;
4182 loc = (loc - size) & -4;
4183 addr = loc;
4184 type = func_vt;
4185 vset(&type, VT_LOCAL | VT_LVAL, addr);
4186 vswap();
4187 vstore();
4188 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4190 vtop->type = int_type;
4191 gv(RC_IRET);
4192 } else {
4193 #endif
4194 type = func_vt;
4195 mk_pointer(&type);
4196 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4197 indir();
4198 vswap();
4199 /* copy structure value to pointer */
4200 vstore();
4201 #ifdef TCC_ARM_EABI
4203 #endif
4204 } else if (is_float(func_vt.t)) {
4205 gv(rc_fret(func_vt.t));
4206 } else {
4207 gv(RC_IRET);
4209 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4211 skip(';');
4212 rsym = gjmp(rsym); /* jmp */
4213 } else if (tok == TOK_BREAK) {
4214 /* compute jump */
4215 if (!bsym)
4216 error("cannot break");
4217 *bsym = gjmp(*bsym);
4218 next();
4219 skip(';');
4220 } else if (tok == TOK_CONTINUE) {
4221 /* compute jump */
4222 if (!csym)
4223 error("cannot continue");
4224 *csym = gjmp(*csym);
4225 next();
4226 skip(';');
4227 } else if (tok == TOK_FOR) {
4228 int e;
4229 next();
4230 skip('(');
4231 if (tok != ';') {
4232 gexpr();
4233 vpop();
4235 skip(';');
4236 d = ind;
4237 c = ind;
4238 a = 0;
4239 b = 0;
4240 if (tok != ';') {
4241 gexpr();
4242 a = gtst(1, 0);
4244 skip(';');
4245 if (tok != ')') {
4246 e = gjmp(0);
4247 c = ind;
4248 gexpr();
4249 vpop();
4250 gjmp_addr(d);
4251 gsym(e);
4253 skip(')');
4254 block(&a, &b, case_sym, def_sym, case_reg, 0);
4255 gjmp_addr(c);
4256 gsym(a);
4257 gsym_addr(b, c);
4258 } else
4259 if (tok == TOK_DO) {
4260 next();
4261 a = 0;
4262 b = 0;
4263 d = ind;
4264 block(&a, &b, case_sym, def_sym, case_reg, 0);
4265 skip(TOK_WHILE);
4266 skip('(');
4267 gsym(b);
4268 gexpr();
4269 c = gtst(0, 0);
4270 gsym_addr(c, d);
4271 skip(')');
4272 gsym(a);
4273 skip(';');
4274 } else
4275 if (tok == TOK_SWITCH) {
4276 next();
4277 skip('(');
4278 gexpr();
4279 /* XXX: other types than integer */
4280 case_reg = gv(RC_INT);
4281 vpop();
4282 skip(')');
4283 a = 0;
4284 b = gjmp(0); /* jump to first case */
4285 c = 0;
4286 block(&a, csym, &b, &c, case_reg, 0);
4287 /* if no default, jmp after switch */
4288 if (c == 0)
4289 c = ind;
4290 /* default label */
4291 gsym_addr(b, c);
4292 /* break label */
4293 gsym(a);
4294 } else
4295 if (tok == TOK_CASE) {
4296 int v1, v2;
4297 if (!case_sym)
4298 expect("switch");
4299 next();
4300 v1 = expr_const();
4301 v2 = v1;
4302 if (gnu_ext && tok == TOK_DOTS) {
4303 next();
4304 v2 = expr_const();
4305 if (v2 < v1)
4306 warning("empty case range");
4308 /* since a case is like a label, we must skip it with a jmp */
4309 b = gjmp(0);
4310 gsym(*case_sym);
4311 vseti(case_reg, 0);
4312 vpushi(v1);
4313 if (v1 == v2) {
4314 gen_op(TOK_EQ);
4315 *case_sym = gtst(1, 0);
4316 } else {
4317 gen_op(TOK_GE);
4318 *case_sym = gtst(1, 0);
4319 vseti(case_reg, 0);
4320 vpushi(v2);
4321 gen_op(TOK_LE);
4322 *case_sym = gtst(1, *case_sym);
4324 gsym(b);
4325 skip(':');
4326 is_expr = 0;
4327 goto block_after_label;
4328 } else
4329 if (tok == TOK_DEFAULT) {
4330 next();
4331 skip(':');
4332 if (!def_sym)
4333 expect("switch");
4334 if (*def_sym)
4335 error("too many 'default'");
4336 *def_sym = ind;
4337 is_expr = 0;
4338 goto block_after_label;
4339 } else
4340 if (tok == TOK_GOTO) {
4341 next();
4342 if (tok == '*' && gnu_ext) {
4343 /* computed goto */
4344 next();
4345 gexpr();
4346 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4347 expect("pointer");
4348 ggoto();
4349 } else if (tok >= TOK_UIDENT) {
4350 s = label_find(tok);
4351 /* put forward definition if needed */
4352 if (!s) {
4353 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4354 } else {
4355 if (s->r == LABEL_DECLARED)
4356 s->r = LABEL_FORWARD;
4358 /* label already defined */
4359 if (s->r & LABEL_FORWARD)
4360 s->jnext = gjmp(s->jnext);
4361 else
4362 gjmp_addr(s->jnext);
4363 next();
4364 } else {
4365 expect("label identifier");
4367 skip(';');
4368 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4369 asm_instr();
4370 } else {
4371 b = is_label();
4372 if (b) {
4373 /* label case */
4374 s = label_find(b);
4375 if (s) {
4376 if (s->r == LABEL_DEFINED)
4377 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4378 gsym(s->jnext);
4379 s->r = LABEL_DEFINED;
4380 } else {
4381 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4383 s->jnext = ind;
4384 /* we accept this, but it is a mistake */
4385 block_after_label:
4386 if (tok == '}') {
4387 warning("deprecated use of label at end of compound statement");
4388 } else {
4389 if (is_expr)
4390 vpop();
4391 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4393 } else {
4394 /* expression case */
4395 if (tok != ';') {
4396 if (is_expr) {
4397 vpop();
4398 gexpr();
4399 } else {
4400 gexpr();
4401 vpop();
4404 skip(';');
4409 /* t is the array or struct type. c is the array or struct
4410 address. cur_index/cur_field is the pointer to the current
4411 value. 'size_only' is true if only size info is needed (only used
4412 in arrays) */
4413 static void decl_designator(CType *type, Section *sec, unsigned long c,
4414 int *cur_index, Sym **cur_field,
4415 int size_only)
4417 Sym *s, *f;
4418 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4419 CType type1;
4421 notfirst = 0;
4422 elem_size = 0;
4423 nb_elems = 1;
4424 if (gnu_ext && (l = is_label()) != 0)
4425 goto struct_field;
4426 while (tok == '[' || tok == '.') {
4427 if (tok == '[') {
4428 if (!(type->t & VT_ARRAY))
4429 expect("array type");
4430 s = type->ref;
4431 next();
4432 index = expr_const();
4433 if (index < 0 || (s->c >= 0 && index >= s->c))
4434 expect("invalid index");
4435 if (tok == TOK_DOTS && gnu_ext) {
4436 next();
4437 index_last = expr_const();
4438 if (index_last < 0 ||
4439 (s->c >= 0 && index_last >= s->c) ||
4440 index_last < index)
4441 expect("invalid index");
4442 } else {
4443 index_last = index;
4445 skip(']');
4446 if (!notfirst)
4447 *cur_index = index_last;
4448 type = pointed_type(type);
4449 elem_size = type_size(type, &align);
4450 c += index * elem_size;
4451 /* NOTE: we only support ranges for last designator */
4452 nb_elems = index_last - index + 1;
4453 if (nb_elems != 1) {
4454 notfirst = 1;
4455 break;
4457 } else {
4458 next();
4459 l = tok;
4460 next();
4461 struct_field:
4462 if ((type->t & VT_BTYPE) != VT_STRUCT)
4463 expect("struct/union type");
4464 s = type->ref;
4465 l |= SYM_FIELD;
4466 f = s->next;
4467 while (f) {
4468 if (f->v == l)
4469 break;
4470 f = f->next;
4472 if (!f)
4473 expect("field");
4474 if (!notfirst)
4475 *cur_field = f;
4476 /* XXX: fix this mess by using explicit storage field */
4477 type1 = f->type;
4478 type1.t |= (type->t & ~VT_TYPE);
4479 type = &type1;
4480 c += f->c;
4482 notfirst = 1;
4484 if (notfirst) {
4485 if (tok == '=') {
4486 next();
4487 } else {
4488 if (!gnu_ext)
4489 expect("=");
4491 } else {
4492 if (type->t & VT_ARRAY) {
4493 index = *cur_index;
4494 type = pointed_type(type);
4495 c += index * type_size(type, &align);
4496 } else {
4497 f = *cur_field;
4498 if (!f)
4499 error("too many field init");
4500 /* XXX: fix this mess by using explicit storage field */
4501 type1 = f->type;
4502 type1.t |= (type->t & ~VT_TYPE);
4503 type = &type1;
4504 c += f->c;
4507 decl_initializer(type, sec, c, 0, size_only);
4509 /* XXX: make it more general */
4510 if (!size_only && nb_elems > 1) {
4511 unsigned long c_end;
4512 uint8_t *src, *dst;
4513 int i;
4515 if (!sec)
4516 error("range init not supported yet for dynamic storage");
4517 c_end = c + nb_elems * elem_size;
4518 if (c_end > sec->data_allocated)
4519 section_realloc(sec, c_end);
4520 src = sec->data + c;
4521 dst = src;
4522 for(i = 1; i < nb_elems; i++) {
4523 dst += elem_size;
4524 memcpy(dst, src, elem_size);
4529 #define EXPR_VAL 0
4530 #define EXPR_CONST 1
4531 #define EXPR_ANY 2
4533 /* store a value or an expression directly in global data or in local array */
4534 static void init_putv(CType *type, Section *sec, unsigned long c,
4535 int v, int expr_type)
4537 int saved_global_expr, bt, bit_pos, bit_size;
4538 void *ptr;
4539 unsigned long long bit_mask;
4540 CType dtype;
4542 switch(expr_type) {
4543 case EXPR_VAL:
4544 vpushi(v);
4545 break;
4546 case EXPR_CONST:
4547 /* compound literals must be allocated globally in this case */
4548 saved_global_expr = global_expr;
4549 global_expr = 1;
4550 expr_const1();
4551 global_expr = saved_global_expr;
4552 /* NOTE: symbols are accepted */
4553 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4554 error("initializer element is not constant");
4555 break;
4556 case EXPR_ANY:
4557 expr_eq();
4558 break;
4561 dtype = *type;
4562 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4564 if (sec) {
4565 /* XXX: not portable */
4566 /* XXX: generate error if incorrect relocation */
4567 gen_assign_cast(&dtype);
4568 bt = type->t & VT_BTYPE;
4569 /* we'll write at most 12 bytes */
4570 if (c + 12 > sec->data_allocated) {
4571 section_realloc(sec, c + 12);
4573 ptr = sec->data + c;
4574 /* XXX: make code faster ? */
4575 if (!(type->t & VT_BITFIELD)) {
4576 bit_pos = 0;
4577 bit_size = 32;
4578 bit_mask = -1LL;
4579 } else {
4580 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4581 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4582 bit_mask = (1LL << bit_size) - 1;
4584 if ((vtop->r & VT_SYM) &&
4585 (bt == VT_BYTE ||
4586 bt == VT_SHORT ||
4587 bt == VT_DOUBLE ||
4588 bt == VT_LDOUBLE ||
4589 bt == VT_LLONG ||
4590 (bt == VT_INT && bit_size != 32)))
4591 error("initializer element is not computable at load time");
4592 switch(bt) {
4593 case VT_BOOL:
4594 vtop->c.i = (vtop->c.i != 0);
4595 case VT_BYTE:
4596 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4597 break;
4598 case VT_SHORT:
4599 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4600 break;
4601 case VT_DOUBLE:
4602 *(double *)ptr = vtop->c.d;
4603 break;
4604 case VT_LDOUBLE:
4605 *(long double *)ptr = vtop->c.ld;
4606 break;
4607 case VT_LLONG:
4608 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4609 break;
4610 default:
4611 if (vtop->r & VT_SYM) {
4612 greloc(sec, vtop->sym, c, R_DATA_PTR);
4614 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4615 break;
4617 vtop--;
4618 } else {
4619 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4620 vswap();
4621 vstore();
4622 vpop();
4626 /* put zeros for variable based init */
4627 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4629 if (sec) {
4630 /* nothing to do because globals are already set to zero */
4631 } else {
4632 vpush_global_sym(&func_old_type, TOK_memset);
4633 vseti(VT_LOCAL, c);
4634 vpushi(0);
4635 vpushi(size);
4636 gfunc_call(3);
4640 /* 't' contains the type and storage info. 'c' is the offset of the
4641 object in section 'sec'. If 'sec' is NULL, it means stack based
4642 allocation. 'first' is true if array '{' must be read (multi
4643 dimension implicit array init handling). 'size_only' is true if
4644 size only evaluation is wanted (only for arrays). */
4645 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4646 int first, int size_only)
4648 int index, array_length, n, no_oblock, nb, parlevel, i;
4649 int size1, align1, expr_type;
4650 Sym *s, *f;
4651 CType *t1;
4653 if (type->t & VT_ARRAY) {
4654 s = type->ref;
4655 n = s->c;
4656 array_length = 0;
4657 t1 = pointed_type(type);
4658 size1 = type_size(t1, &align1);
4660 no_oblock = 1;
4661 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4662 tok == '{') {
4663 skip('{');
4664 no_oblock = 0;
4667 /* only parse strings here if correct type (otherwise: handle
4668 them as ((w)char *) expressions */
4669 if ((tok == TOK_LSTR &&
4670 #ifdef TCC_TARGET_PE
4671 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4672 #else
4673 (t1->t & VT_BTYPE) == VT_INT
4674 #endif
4675 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4676 while (tok == TOK_STR || tok == TOK_LSTR) {
4677 int cstr_len, ch;
4678 CString *cstr;
4680 cstr = tokc.cstr;
4681 /* compute maximum number of chars wanted */
4682 if (tok == TOK_STR)
4683 cstr_len = cstr->size;
4684 else
4685 cstr_len = cstr->size / sizeof(nwchar_t);
4686 cstr_len--;
4687 nb = cstr_len;
4688 if (n >= 0 && nb > (n - array_length))
4689 nb = n - array_length;
4690 if (!size_only) {
4691 if (cstr_len > nb)
4692 warning("initializer-string for array is too long");
4693 /* in order to go faster for common case (char
4694 string in global variable, we handle it
4695 specifically */
4696 if (sec && tok == TOK_STR && size1 == 1) {
4697 memcpy(sec->data + c + array_length, cstr->data, nb);
4698 } else {
4699 for(i=0;i<nb;i++) {
4700 if (tok == TOK_STR)
4701 ch = ((unsigned char *)cstr->data)[i];
4702 else
4703 ch = ((nwchar_t *)cstr->data)[i];
4704 init_putv(t1, sec, c + (array_length + i) * size1,
4705 ch, EXPR_VAL);
4709 array_length += nb;
4710 next();
4712 /* only add trailing zero if enough storage (no
4713 warning in this case since it is standard) */
4714 if (n < 0 || array_length < n) {
4715 if (!size_only) {
4716 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4718 array_length++;
4720 } else {
4721 index = 0;
4722 while (tok != '}') {
4723 decl_designator(type, sec, c, &index, NULL, size_only);
4724 if (n >= 0 && index >= n)
4725 error("index too large");
4726 /* must put zero in holes (note that doing it that way
4727 ensures that it even works with designators) */
4728 if (!size_only && array_length < index) {
4729 init_putz(t1, sec, c + array_length * size1,
4730 (index - array_length) * size1);
4732 index++;
4733 if (index > array_length)
4734 array_length = index;
4735 /* special test for multi dimensional arrays (may not
4736 be strictly correct if designators are used at the
4737 same time) */
4738 if (index >= n && no_oblock)
4739 break;
4740 if (tok == '}')
4741 break;
4742 skip(',');
4745 if (!no_oblock)
4746 skip('}');
4747 /* put zeros at the end */
4748 if (!size_only && n >= 0 && array_length < n) {
4749 init_putz(t1, sec, c + array_length * size1,
4750 (n - array_length) * size1);
4752 /* patch type size if needed */
4753 if (n < 0)
4754 s->c = array_length;
4755 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4756 (sec || !first || tok == '{')) {
4757 int par_count;
4759 /* NOTE: the previous test is a specific case for automatic
4760 struct/union init */
4761 /* XXX: union needs only one init */
4763 /* XXX: this test is incorrect for local initializers
4764 beginning with ( without {. It would be much more difficult
4765 to do it correctly (ideally, the expression parser should
4766 be used in all cases) */
4767 par_count = 0;
4768 if (tok == '(') {
4769 AttributeDef ad1;
4770 CType type1;
4771 next();
4772 while (tok == '(') {
4773 par_count++;
4774 next();
4776 if (!parse_btype(&type1, &ad1))
4777 expect("cast");
4778 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4779 #if 0
4780 if (!is_assignable_types(type, &type1))
4781 error("invalid type for cast");
4782 #endif
4783 skip(')');
4785 no_oblock = 1;
4786 if (first || tok == '{') {
4787 skip('{');
4788 no_oblock = 0;
4790 s = type->ref;
4791 f = s->next;
4792 array_length = 0;
4793 index = 0;
4794 n = s->c;
4795 while (tok != '}') {
4796 decl_designator(type, sec, c, NULL, &f, size_only);
4797 index = f->c;
4798 if (!size_only && array_length < index) {
4799 init_putz(type, sec, c + array_length,
4800 index - array_length);
4802 index = index + type_size(&f->type, &align1);
4803 if (index > array_length)
4804 array_length = index;
4806 /* gr: skip fields from same union - ugly. */
4807 while (f->next) {
4808 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4809 /* test for same offset */
4810 if (f->next->c != f->c)
4811 break;
4812 /* if yes, test for bitfield shift */
4813 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4814 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4815 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4816 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4817 if (bit_pos_1 != bit_pos_2)
4818 break;
4820 f = f->next;
4823 f = f->next;
4824 if (no_oblock && f == NULL)
4825 break;
4826 if (tok == '}')
4827 break;
4828 skip(',');
4830 /* put zeros at the end */
4831 if (!size_only && array_length < n) {
4832 init_putz(type, sec, c + array_length,
4833 n - array_length);
4835 if (!no_oblock)
4836 skip('}');
4837 while (par_count) {
4838 skip(')');
4839 par_count--;
4841 } else if (tok == '{') {
4842 next();
4843 decl_initializer(type, sec, c, first, size_only);
4844 skip('}');
4845 } else if (size_only) {
4846 /* just skip expression */
4847 parlevel = 0;
4848 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4849 tok != -1) {
4850 if (tok == '(')
4851 parlevel++;
4852 else if (tok == ')')
4853 parlevel--;
4854 next();
4856 } else {
4857 /* currently, we always use constant expression for globals
4858 (may change for scripting case) */
4859 expr_type = EXPR_CONST;
4860 if (!sec)
4861 expr_type = EXPR_ANY;
4862 init_putv(type, sec, c, 0, expr_type);
4866 /* parse an initializer for type 't' if 'has_init' is non zero, and
4867 allocate space in local or global data space ('r' is either
4868 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4869 variable 'v' of scope 'scope' is declared before initializers are
4870 parsed. If 'v' is zero, then a reference to the new object is put
4871 in the value stack. If 'has_init' is 2, a special parsing is done
4872 to handle string constants. */
4873 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4874 int has_init, int v, int scope)
4876 int size, align, addr, data_offset;
4877 int level;
4878 ParseState saved_parse_state = {0};
4879 TokenString init_str;
4880 Section *sec;
4882 size = type_size(type, &align);
4883 /* If unknown size, we must evaluate it before
4884 evaluating initializers because
4885 initializers can generate global data too
4886 (e.g. string pointers or ISOC99 compound
4887 literals). It also simplifies local
4888 initializers handling */
4889 tok_str_new(&init_str);
4890 if (size < 0) {
4891 if (!has_init)
4892 error("unknown type size");
4893 /* get all init string */
4894 if (has_init == 2) {
4895 /* only get strings */
4896 while (tok == TOK_STR || tok == TOK_LSTR) {
4897 tok_str_add_tok(&init_str);
4898 next();
4900 } else {
4901 level = 0;
4902 while (level > 0 || (tok != ',' && tok != ';')) {
4903 if (tok < 0)
4904 error("unexpected end of file in initializer");
4905 tok_str_add_tok(&init_str);
4906 if (tok == '{')
4907 level++;
4908 else if (tok == '}') {
4909 level--;
4910 if (level <= 0) {
4911 next();
4912 break;
4915 next();
4918 tok_str_add(&init_str, -1);
4919 tok_str_add(&init_str, 0);
4921 /* compute size */
4922 save_parse_state(&saved_parse_state);
4924 macro_ptr = init_str.str;
4925 next();
4926 decl_initializer(type, NULL, 0, 1, 1);
4927 /* prepare second initializer parsing */
4928 macro_ptr = init_str.str;
4929 next();
4931 /* if still unknown size, error */
4932 size = type_size(type, &align);
4933 if (size < 0)
4934 error("unknown type size");
4936 /* take into account specified alignment if bigger */
4937 if (ad->aligned) {
4938 if (ad->aligned > align)
4939 align = ad->aligned;
4940 } else if (ad->packed) {
4941 align = 1;
4943 if ((r & VT_VALMASK) == VT_LOCAL) {
4944 sec = NULL;
4945 #ifdef CONFIG_TCC_BCHECK
4946 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4947 loc--;
4948 #endif
4949 loc = (loc - size) & -align;
4950 addr = loc;
4951 #ifdef CONFIG_TCC_BCHECK
4952 /* handles bounds */
4953 /* XXX: currently, since we do only one pass, we cannot track
4954 '&' operators, so we add only arrays */
4955 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4956 unsigned long *bounds_ptr;
4957 /* add padding between regions */
4958 loc--;
4959 /* then add local bound info */
4960 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4961 bounds_ptr[0] = addr;
4962 bounds_ptr[1] = size;
4964 #endif
4965 if (v) {
4966 /* local variable */
4967 sym_push(v, type, r, addr);
4968 } else {
4969 /* push local reference */
4970 vset(type, r, addr);
4972 } else {
4973 Sym *sym;
4975 sym = NULL;
4976 if (v && scope == VT_CONST) {
4977 /* see if the symbol was already defined */
4978 sym = sym_find(v);
4979 if (sym) {
4980 if (!is_compatible_types(&sym->type, type))
4981 error("incompatible types for redefinition of '%s'",
4982 get_tok_str(v, NULL));
4983 if (sym->type.t & VT_EXTERN) {
4984 /* if the variable is extern, it was not allocated */
4985 sym->type.t &= ~VT_EXTERN;
4986 /* set array size if it was ommited in extern
4987 declaration */
4988 if ((sym->type.t & VT_ARRAY) &&
4989 sym->type.ref->c < 0 &&
4990 type->ref->c >= 0)
4991 sym->type.ref->c = type->ref->c;
4992 } else {
4993 /* we accept several definitions of the same
4994 global variable. this is tricky, because we
4995 must play with the SHN_COMMON type of the symbol */
4996 /* XXX: should check if the variable was already
4997 initialized. It is incorrect to initialized it
4998 twice */
4999 /* no init data, we won't add more to the symbol */
5000 if (!has_init)
5001 goto no_alloc;
5006 /* allocate symbol in corresponding section */
5007 sec = ad->section;
5008 if (!sec) {
5009 if (has_init)
5010 sec = data_section;
5011 else if (tcc_state->nocommon)
5012 sec = bss_section;
5014 if (sec) {
5015 data_offset = sec->data_offset;
5016 data_offset = (data_offset + align - 1) & -align;
5017 addr = data_offset;
5018 /* very important to increment global pointer at this time
5019 because initializers themselves can create new initializers */
5020 data_offset += size;
5021 #ifdef CONFIG_TCC_BCHECK
5022 /* add padding if bound check */
5023 if (tcc_state->do_bounds_check)
5024 data_offset++;
5025 #endif
5026 sec->data_offset = data_offset;
5027 /* allocate section space to put the data */
5028 if (sec->sh_type != SHT_NOBITS &&
5029 data_offset > sec->data_allocated)
5030 section_realloc(sec, data_offset);
5031 /* align section if needed */
5032 if (align > sec->sh_addralign)
5033 sec->sh_addralign = align;
5034 } else {
5035 addr = 0; /* avoid warning */
5038 if (v) {
5039 if (scope != VT_CONST || !sym) {
5040 sym = sym_push(v, type, r | VT_SYM, 0);
5042 /* update symbol definition */
5043 if (sec) {
5044 put_extern_sym(sym, sec, addr, size);
5045 } else {
5046 ElfW(Sym) *esym;
5047 /* put a common area */
5048 put_extern_sym(sym, NULL, align, size);
5049 /* XXX: find a nicer way */
5050 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5051 esym->st_shndx = SHN_COMMON;
5053 } else {
5054 CValue cval;
5056 /* push global reference */
5057 sym = get_sym_ref(type, sec, addr, size);
5058 cval.ul = 0;
5059 vsetc(type, VT_CONST | VT_SYM, &cval);
5060 vtop->sym = sym;
5062 #ifdef CONFIG_TCC_BCHECK
5063 /* handles bounds now because the symbol must be defined
5064 before for the relocation */
5065 if (tcc_state->do_bounds_check) {
5066 unsigned long *bounds_ptr;
5068 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5069 /* then add global bound info */
5070 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5071 bounds_ptr[0] = 0; /* relocated */
5072 bounds_ptr[1] = size;
5074 #endif
5076 if (has_init) {
5077 decl_initializer(type, sec, addr, 1, 0);
5078 /* restore parse state if needed */
5079 if (init_str.str) {
5080 tok_str_free(init_str.str);
5081 restore_parse_state(&saved_parse_state);
5084 no_alloc: ;
5087 static void put_func_debug(Sym *sym)
5089 char buf[512];
5091 /* stabs info */
5092 /* XXX: we put here a dummy type */
5093 snprintf(buf, sizeof(buf), "%s:%c1",
5094 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5095 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5096 cur_text_section, sym->c);
5097 /* //gr gdb wants a line at the function */
5098 put_stabn(N_SLINE, 0, file->line_num, 0);
5099 last_ind = 0;
5100 last_line_num = 0;
5103 /* parse an old style function declaration list */
5104 /* XXX: check multiple parameter */
5105 static void func_decl_list(Sym *func_sym)
5107 AttributeDef ad;
5108 int v;
5109 Sym *s;
5110 CType btype, type;
5112 /* parse each declaration */
5113 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
5114 if (!parse_btype(&btype, &ad))
5115 expect("declaration list");
5116 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5117 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5118 tok == ';') {
5119 /* we accept no variable after */
5120 } else {
5121 for(;;) {
5122 type = btype;
5123 type_decl(&type, &ad, &v, TYPE_DIRECT);
5124 /* find parameter in function parameter list */
5125 s = func_sym->next;
5126 while (s != NULL) {
5127 if ((s->v & ~SYM_FIELD) == v)
5128 goto found;
5129 s = s->next;
5131 error("declaration for parameter '%s' but no such parameter",
5132 get_tok_str(v, NULL));
5133 found:
5134 /* check that no storage specifier except 'register' was given */
5135 if (type.t & VT_STORAGE)
5136 error("storage class specified for '%s'", get_tok_str(v, NULL));
5137 convert_parameter_type(&type);
5138 /* we can add the type (NOTE: it could be local to the function) */
5139 s->type = type;
5140 /* accept other parameters */
5141 if (tok == ',')
5142 next();
5143 else
5144 break;
5147 skip(';');
5151 /* parse a function defined by symbol 'sym' and generate its code in
5152 'cur_text_section' */
5153 static void gen_function(Sym *sym)
5155 int saved_nocode_wanted = nocode_wanted;
5156 nocode_wanted = 0;
5157 ind = cur_text_section->data_offset;
5158 /* NOTE: we patch the symbol size later */
5159 put_extern_sym(sym, cur_text_section, ind, 0);
5160 funcname = get_tok_str(sym->v, NULL);
5161 func_ind = ind;
5162 /* put debug symbol */
5163 if (tcc_state->do_debug)
5164 put_func_debug(sym);
5165 /* push a dummy symbol to enable local sym storage */
5166 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5167 gfunc_prolog(&sym->type);
5168 rsym = 0;
5169 block(NULL, NULL, NULL, NULL, 0, 0);
5170 gsym(rsym);
5171 gfunc_epilog();
5172 cur_text_section->data_offset = ind;
5173 label_pop(&global_label_stack, NULL);
5174 sym_pop(&local_stack, NULL); /* reset local stack */
5175 /* end of function */
5176 /* patch symbol size */
5177 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5178 ind - func_ind;
5179 if (tcc_state->do_debug) {
5180 put_stabn(N_FUN, 0, 0, ind - func_ind);
5182 /* It's better to crash than to generate wrong code */
5183 cur_text_section = NULL;
5184 funcname = ""; /* for safety */
5185 func_vt.t = VT_VOID; /* for safety */
5186 ind = 0; /* for safety */
5187 nocode_wanted = saved_nocode_wanted;
5190 ST_FUNC void gen_inline_functions(void)
5192 Sym *sym;
5193 int *str, inline_generated, i;
5194 struct InlineFunc *fn;
5196 /* iterate while inline function are referenced */
5197 for(;;) {
5198 inline_generated = 0;
5199 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5200 fn = tcc_state->inline_fns[i];
5201 sym = fn->sym;
5202 if (sym && sym->c) {
5203 /* the function was used: generate its code and
5204 convert it to a normal function */
5205 str = fn->token_str;
5206 fn->sym = NULL;
5207 if (file)
5208 strcpy(file->filename, fn->filename);
5209 sym->r = VT_SYM | VT_CONST;
5210 sym->type.t &= ~VT_INLINE;
5212 macro_ptr = str;
5213 next();
5214 cur_text_section = text_section;
5215 gen_function(sym);
5216 macro_ptr = NULL; /* fail safe */
5218 inline_generated = 1;
5221 if (!inline_generated)
5222 break;
5224 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5225 fn = tcc_state->inline_fns[i];
5226 str = fn->token_str;
5227 tok_str_free(str);
5229 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5232 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5233 ST_FUNC void decl(int l)
5235 int v, has_init, r;
5236 CType type, btype;
5237 Sym *sym;
5238 AttributeDef ad;
5240 while (1) {
5241 if (!parse_btype(&btype, &ad)) {
5242 /* skip redundant ';' */
5243 /* XXX: find more elegant solution */
5244 if (tok == ';') {
5245 next();
5246 continue;
5248 if (l == VT_CONST &&
5249 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5250 /* global asm block */
5251 asm_global_instr();
5252 continue;
5254 /* special test for old K&R protos without explicit int
5255 type. Only accepted when defining global data */
5256 if (l == VT_LOCAL || tok < TOK_DEFINE)
5257 break;
5258 btype.t = VT_INT;
5260 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5261 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5262 tok == ';') {
5263 /* we accept no variable after */
5264 next();
5265 continue;
5267 while (1) { /* iterate thru each declaration */
5268 type = btype;
5269 type_decl(&type, &ad, &v, TYPE_DIRECT);
5270 #if 0
5272 char buf[500];
5273 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5274 printf("type = '%s'\n", buf);
5276 #endif
5277 if ((type.t & VT_BTYPE) == VT_FUNC) {
5278 /* if old style function prototype, we accept a
5279 declaration list */
5280 sym = type.ref;
5281 if (sym->c == FUNC_OLD)
5282 func_decl_list(sym);
5285 #ifdef TCC_TARGET_PE
5286 if (ad.func_import)
5287 type.t |= VT_IMPORT;
5288 if (ad.func_export)
5289 type.t |= VT_EXPORT;
5290 #endif
5291 if (tok == '{') {
5292 if (l == VT_LOCAL)
5293 error("cannot use local functions");
5294 if ((type.t & VT_BTYPE) != VT_FUNC)
5295 expect("function definition");
5297 /* reject abstract declarators in function definition */
5298 sym = type.ref;
5299 while ((sym = sym->next) != NULL)
5300 if (!(sym->v & ~SYM_FIELD))
5301 expect("identifier");
5303 /* XXX: cannot do better now: convert extern line to static inline */
5304 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5305 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5307 sym = sym_find(v);
5308 if (sym) {
5309 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5310 goto func_error1;
5312 r = sym->type.ref->r;
5313 /* use func_call from prototype if not defined */
5314 if (FUNC_CALL(r) != FUNC_CDECL
5315 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5316 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5318 /* use export from prototype */
5319 if (FUNC_EXPORT(r))
5320 FUNC_EXPORT(type.ref->r) = 1;
5322 /* use static from prototype */
5323 if (sym->type.t & VT_STATIC)
5324 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5326 if (!is_compatible_types(&sym->type, &type)) {
5327 func_error1:
5328 error("incompatible types for redefinition of '%s'",
5329 get_tok_str(v, NULL));
5331 /* if symbol is already defined, then put complete type */
5332 sym->type = type;
5333 } else {
5334 /* put function symbol */
5335 sym = global_identifier_push(v, type.t, 0);
5336 sym->type.ref = type.ref;
5339 /* static inline functions are just recorded as a kind
5340 of macro. Their code will be emitted at the end of
5341 the compilation unit only if they are used */
5342 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5343 (VT_INLINE | VT_STATIC)) {
5344 TokenString func_str;
5345 int block_level;
5346 struct InlineFunc *fn;
5347 const char *filename;
5349 tok_str_new(&func_str);
5351 block_level = 0;
5352 for(;;) {
5353 int t;
5354 if (tok == TOK_EOF)
5355 error("unexpected end of file");
5356 tok_str_add_tok(&func_str);
5357 t = tok;
5358 next();
5359 if (t == '{') {
5360 block_level++;
5361 } else if (t == '}') {
5362 block_level--;
5363 if (block_level == 0)
5364 break;
5367 tok_str_add(&func_str, -1);
5368 tok_str_add(&func_str, 0);
5369 filename = file ? file->filename : "";
5370 fn = tcc_malloc(sizeof *fn + strlen(filename));
5371 strcpy(fn->filename, filename);
5372 fn->sym = sym;
5373 fn->token_str = func_str.str;
5374 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5376 } else {
5377 /* compute text section */
5378 cur_text_section = ad.section;
5379 if (!cur_text_section)
5380 cur_text_section = text_section;
5381 sym->r = VT_SYM | VT_CONST;
5382 gen_function(sym);
5384 break;
5385 } else {
5386 if (btype.t & VT_TYPEDEF) {
5387 /* save typedefed type */
5388 /* XXX: test storage specifiers ? */
5389 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5390 sym->type.t |= VT_TYPEDEF;
5391 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5392 /* external function definition */
5393 /* specific case for func_call attribute */
5394 type.ref->r = INT_ATTR(&ad);
5395 external_sym(v, &type, 0);
5396 } else {
5397 /* not lvalue if array */
5398 r = 0;
5399 if (!(type.t & VT_ARRAY))
5400 r |= lvalue_type(type.t);
5401 has_init = (tok == '=');
5402 if ((btype.t & VT_EXTERN) ||
5403 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5404 !has_init && l == VT_CONST && type.ref->c < 0)) {
5405 /* external variable */
5406 /* NOTE: as GCC, uninitialized global static
5407 arrays of null size are considered as
5408 extern */
5409 external_sym(v, &type, r);
5410 } else {
5411 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5412 if (type.t & VT_STATIC)
5413 r |= VT_CONST;
5414 else
5415 r |= l;
5416 if (has_init)
5417 next();
5418 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
5421 if (tok != ',') {
5422 skip(';');
5423 break;
5425 next();