implemented C99 for loop with variable declaration
[tinycc.git] / tccgen.c
blob1197ff913c663b2aea7c77bffe07948edd1b6ae2
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 constant of type "type" with useless value */
288 void vpush(CType *type)
290 CValue cval;
291 vsetc(type, VT_CONST, &cval);
294 /* push integer constant */
295 ST_FUNC void vpushi(int v)
297 CValue cval;
298 cval.i = v;
299 vsetc(&int_type, VT_CONST, &cval);
302 /* push long long constant */
303 static void vpushll(long long v)
305 CValue cval;
306 CType ctype;
307 ctype.t = VT_LLONG;
308 ctype.ref = 0;
309 cval.ull = v;
310 vsetc(&ctype, VT_CONST, &cval);
313 /* push arbitrary 64bit constant */
314 void vpush64(int ty, unsigned long long v)
316 CValue cval;
317 CType ctype;
318 ctype.t = ty;
319 cval.ull = v;
320 vsetc(&ctype, VT_CONST, &cval);
323 /* Return a static symbol pointing to a section */
324 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
326 int v;
327 Sym *sym;
329 v = anon_sym++;
330 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
331 sym->type.ref = type->ref;
332 sym->r = VT_CONST | VT_SYM;
333 put_extern_sym(sym, sec, offset, size);
334 return sym;
337 /* push a reference to a section offset by adding a dummy symbol */
338 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
340 CValue cval;
342 cval.ul = 0;
343 vsetc(type, VT_CONST | VT_SYM, &cval);
344 vtop->sym = get_sym_ref(type, sec, offset, size);
347 /* define a new external reference to a symbol 'v' of type 'u' */
348 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
350 Sym *s;
352 s = sym_find(v);
353 if (!s) {
354 /* push forward reference */
355 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
356 s->type.ref = type->ref;
357 s->r = r | VT_CONST | VT_SYM;
359 return s;
362 /* define a new external reference to a symbol 'v' of type 'u' */
363 static Sym *external_sym(int v, CType *type, int r)
365 Sym *s;
367 s = sym_find(v);
368 if (!s) {
369 /* push forward reference */
370 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
371 s->type.t |= VT_EXTERN;
372 } else if (s->type.ref == func_old_type.ref) {
373 s->type.ref = type->ref;
374 s->r = r | VT_CONST | VT_SYM;
375 s->type.t |= VT_EXTERN;
376 } else if (!is_compatible_types(&s->type, type)) {
377 error("incompatible types for redefinition of '%s'",
378 get_tok_str(v, NULL));
380 return s;
383 /* push a reference to global symbol v */
384 ST_FUNC void vpush_global_sym(CType *type, int v)
386 Sym *sym;
387 CValue cval;
389 sym = external_global_sym(v, type, 0);
390 cval.ul = 0;
391 vsetc(type, VT_CONST | VT_SYM, &cval);
392 vtop->sym = sym;
395 ST_FUNC void vset(CType *type, int r, int v)
397 CValue cval;
399 cval.i = v;
400 vsetc(type, r, &cval);
403 static void vseti(int r, int v)
405 CType type;
406 type.t = VT_INT;
407 type.ref = 0;
408 vset(&type, r, v);
411 ST_FUNC void vswap(void)
413 SValue tmp;
415 tmp = vtop[0];
416 vtop[0] = vtop[-1];
417 vtop[-1] = tmp;
420 ST_FUNC void vpushv(SValue *v)
422 if (vtop >= vstack + (VSTACK_SIZE - 1))
423 error("memory full");
424 vtop++;
425 *vtop = *v;
428 static void vdup(void)
430 vpushv(vtop);
433 /* save r to the memory stack, and mark it as being free */
434 ST_FUNC void save_reg(int r)
436 int l, saved, size, align;
437 SValue *p, sv;
438 CType *type;
440 /* modify all stack values */
441 saved = 0;
442 l = 0;
443 for(p=vstack;p<=vtop;p++) {
444 if ((p->r & VT_VALMASK) == r ||
445 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
446 /* must save value on stack if not already done */
447 if (!saved) {
448 /* NOTE: must reload 'r' because r might be equal to r2 */
449 r = p->r & VT_VALMASK;
450 /* store register in the stack */
451 type = &p->type;
452 if ((p->r & VT_LVAL) ||
453 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
454 #ifdef TCC_TARGET_X86_64
455 type = &char_pointer_type;
456 #else
457 type = &int_type;
458 #endif
459 size = type_size(type, &align);
460 loc = (loc - size) & -align;
461 sv.type.t = type->t;
462 sv.r = VT_LOCAL | VT_LVAL;
463 sv.c.ul = loc;
464 store(r, &sv);
465 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
466 /* x86 specific: need to pop fp register ST0 if saved */
467 if (r == TREG_ST0) {
468 o(0xd8dd); /* fstp %st(0) */
470 #endif
471 #ifndef TCC_TARGET_X86_64
472 /* special long long case */
473 if ((type->t & VT_BTYPE) == VT_LLONG) {
474 sv.c.ul += 4;
475 store(p->r2, &sv);
477 #endif
478 l = loc;
479 saved = 1;
481 /* mark that stack entry as being saved on the stack */
482 if (p->r & VT_LVAL) {
483 /* also clear the bounded flag because the
484 relocation address of the function was stored in
485 p->c.ul */
486 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
487 } else {
488 p->r = lvalue_type(p->type.t) | VT_LOCAL;
490 p->r2 = VT_CONST;
491 p->c.ul = l;
496 #ifdef TCC_TARGET_ARM
497 /* find a register of class 'rc2' with at most one reference on stack.
498 * If none, call get_reg(rc) */
499 ST_FUNC int get_reg_ex(int rc, int rc2)
501 int r;
502 SValue *p;
504 for(r=0;r<NB_REGS;r++) {
505 if (reg_classes[r] & rc2) {
506 int n;
507 n=0;
508 for(p = vstack; p <= vtop; p++) {
509 if ((p->r & VT_VALMASK) == r ||
510 (p->r2 & VT_VALMASK) == r)
511 n++;
513 if (n <= 1)
514 return r;
517 return get_reg(rc);
519 #endif
521 /* find a free register of class 'rc'. If none, save one register */
522 ST_FUNC int get_reg(int rc)
524 int r;
525 SValue *p;
527 /* find a free register */
528 for(r=0;r<NB_REGS;r++) {
529 if (reg_classes[r] & rc) {
530 for(p=vstack;p<=vtop;p++) {
531 if ((p->r & VT_VALMASK) == r ||
532 (p->r2 & VT_VALMASK) == r)
533 goto notfound;
535 return r;
537 notfound: ;
540 /* no register left : free the first one on the stack (VERY
541 IMPORTANT to start from the bottom to ensure that we don't
542 spill registers used in gen_opi()) */
543 for(p=vstack;p<=vtop;p++) {
544 r = p->r & VT_VALMASK;
545 if (r < VT_CONST && (reg_classes[r] & rc))
546 goto save_found;
547 /* also look at second register (if long long) */
548 r = p->r2 & VT_VALMASK;
549 if (r < VT_CONST && (reg_classes[r] & rc)) {
550 save_found:
551 save_reg(r);
552 return r;
555 /* Should never comes here */
556 return -1;
559 /* save registers up to (vtop - n) stack entry */
560 ST_FUNC void save_regs(int n)
562 int r;
563 SValue *p, *p1;
564 p1 = vtop - n;
565 for(p = vstack;p <= p1; p++) {
566 r = p->r & VT_VALMASK;
567 if (r < VT_CONST) {
568 save_reg(r);
573 /* move register 's' to 'r', and flush previous value of r to memory
574 if needed */
575 static void move_reg(int r, int s)
577 SValue sv;
579 if (r != s) {
580 save_reg(r);
581 sv.type.t = VT_INT;
582 sv.r = s;
583 sv.c.ul = 0;
584 load(r, &sv);
588 /* get address of vtop (vtop MUST BE an lvalue) */
589 static void gaddrof(void)
591 vtop->r &= ~VT_LVAL;
592 /* tricky: if saved lvalue, then we can go back to lvalue */
593 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
594 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
597 #ifdef CONFIG_TCC_BCHECK
598 /* generate lvalue bound code */
599 static void gbound(void)
601 int lval_type;
602 CType type1;
604 vtop->r &= ~VT_MUSTBOUND;
605 /* if lvalue, then use checking code before dereferencing */
606 if (vtop->r & VT_LVAL) {
607 /* if not VT_BOUNDED value, then make one */
608 if (!(vtop->r & VT_BOUNDED)) {
609 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
610 /* must save type because we must set it to int to get pointer */
611 type1 = vtop->type;
612 vtop->type.t = VT_INT;
613 gaddrof();
614 vpushi(0);
615 gen_bounded_ptr_add();
616 vtop->r |= lval_type;
617 vtop->type = type1;
619 /* then check for dereferencing */
620 gen_bounded_ptr_deref();
623 #endif
625 /* store vtop a register belonging to class 'rc'. lvalues are
626 converted to values. Cannot be used if cannot be converted to
627 register value (such as structures). */
628 ST_FUNC int gv(int rc)
630 int r, rc2, bit_pos, bit_size, size, align, i;
632 /* NOTE: get_reg can modify vstack[] */
633 if (vtop->type.t & VT_BITFIELD) {
634 CType type;
635 int bits = 32;
636 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
637 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
638 /* remove bit field info to avoid loops */
639 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
640 /* cast to int to propagate signedness in following ops */
641 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
642 type.t = VT_LLONG;
643 bits = 64;
644 } else
645 type.t = VT_INT;
646 if((vtop->type.t & VT_UNSIGNED) ||
647 (vtop->type.t & VT_BTYPE) == VT_BOOL)
648 type.t |= VT_UNSIGNED;
649 gen_cast(&type);
650 /* generate shifts */
651 vpushi(bits - (bit_pos + bit_size));
652 gen_op(TOK_SHL);
653 vpushi(bits - bit_size);
654 /* NOTE: transformed to SHR if unsigned */
655 gen_op(TOK_SAR);
656 r = gv(rc);
657 } else {
658 if (is_float(vtop->type.t) &&
659 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
660 Sym *sym;
661 int *ptr;
662 unsigned long offset;
663 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
664 CValue check;
665 #endif
667 /* XXX: unify with initializers handling ? */
668 /* CPUs usually cannot use float constants, so we store them
669 generically in data segment */
670 size = type_size(&vtop->type, &align);
671 offset = (data_section->data_offset + align - 1) & -align;
672 data_section->data_offset = offset;
673 /* XXX: not portable yet */
674 #if defined(__i386__) || defined(__x86_64__)
675 /* Zero pad x87 tenbyte long doubles */
676 if (size == LDOUBLE_SIZE)
677 vtop->c.tab[2] &= 0xffff;
678 #endif
679 ptr = section_ptr_add(data_section, size);
680 size = size >> 2;
681 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
682 check.d = 1;
683 if(check.tab[0])
684 for(i=0;i<size;i++)
685 ptr[i] = vtop->c.tab[size-1-i];
686 else
687 #endif
688 for(i=0;i<size;i++)
689 ptr[i] = vtop->c.tab[i];
690 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
691 vtop->r |= VT_LVAL | VT_SYM;
692 vtop->sym = sym;
693 vtop->c.ul = 0;
695 #ifdef CONFIG_TCC_BCHECK
696 if (vtop->r & VT_MUSTBOUND)
697 gbound();
698 #endif
700 r = vtop->r & VT_VALMASK;
701 rc2 = RC_INT;
702 if (rc == RC_IRET)
703 rc2 = RC_LRET;
704 /* need to reload if:
705 - constant
706 - lvalue (need to dereference pointer)
707 - already a register, but not in the right class */
708 if (r >= VT_CONST
709 || (vtop->r & VT_LVAL)
710 || !(reg_classes[r] & rc)
711 #ifndef TCC_TARGET_X86_64
712 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
713 #endif
716 r = get_reg(rc);
717 #ifndef TCC_TARGET_X86_64
718 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
719 int r2;
720 unsigned long long ll;
721 /* two register type load : expand to two words
722 temporarily */
723 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
724 /* load constant */
725 ll = vtop->c.ull;
726 vtop->c.ui = ll; /* first word */
727 load(r, vtop);
728 vtop->r = r; /* save register value */
729 vpushi(ll >> 32); /* second word */
730 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
731 (vtop->r & VT_LVAL)) {
732 /* We do not want to modifier the long long
733 pointer here, so the safest (and less
734 efficient) is to save all the other registers
735 in the stack. XXX: totally inefficient. */
736 save_regs(1);
737 /* load from memory */
738 load(r, vtop);
739 vdup();
740 vtop[-1].r = r; /* save register value */
741 /* increment pointer to get second word */
742 vtop->type.t = VT_INT;
743 gaddrof();
744 vpushi(4);
745 gen_op('+');
746 vtop->r |= VT_LVAL;
747 } else {
748 /* move registers */
749 load(r, vtop);
750 vdup();
751 vtop[-1].r = r; /* save register value */
752 vtop->r = vtop[-1].r2;
754 /* allocate second register */
755 r2 = get_reg(rc2);
756 load(r2, vtop);
757 vpop();
758 /* write second register */
759 vtop->r2 = r2;
760 } else
761 #endif
762 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
763 int t1, t;
764 /* lvalue of scalar type : need to use lvalue type
765 because of possible cast */
766 t = vtop->type.t;
767 t1 = t;
768 /* compute memory access type */
769 if (vtop->r & VT_LVAL_BYTE)
770 t = VT_BYTE;
771 else if (vtop->r & VT_LVAL_SHORT)
772 t = VT_SHORT;
773 if (vtop->r & VT_LVAL_UNSIGNED)
774 t |= VT_UNSIGNED;
775 vtop->type.t = t;
776 load(r, vtop);
777 /* restore wanted type */
778 vtop->type.t = t1;
779 } else {
780 /* one register type load */
781 load(r, vtop);
784 vtop->r = r;
785 #ifdef TCC_TARGET_C67
786 /* uses register pairs for doubles */
787 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
788 vtop->r2 = r+1;
789 #endif
791 return r;
794 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
795 ST_FUNC void gv2(int rc1, int rc2)
797 int v;
799 /* generate more generic register first. But VT_JMP or VT_CMP
800 values must be generated first in all cases to avoid possible
801 reload errors */
802 v = vtop[0].r & VT_VALMASK;
803 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
804 vswap();
805 gv(rc1);
806 vswap();
807 gv(rc2);
808 /* test if reload is needed for first register */
809 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
810 vswap();
811 gv(rc1);
812 vswap();
814 } else {
815 gv(rc2);
816 vswap();
817 gv(rc1);
818 vswap();
819 /* test if reload is needed for first register */
820 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
821 gv(rc2);
826 /* wrapper around RC_FRET to return a register by type */
827 static int rc_fret(int t)
829 #ifdef TCC_TARGET_X86_64
830 if (t == VT_LDOUBLE) {
831 return RC_ST0;
833 #endif
834 return RC_FRET;
837 /* wrapper around REG_FRET to return a register by type */
838 static int reg_fret(int t)
840 #ifdef TCC_TARGET_X86_64
841 if (t == VT_LDOUBLE) {
842 return TREG_ST0;
844 #endif
845 return REG_FRET;
848 /* expand long long on stack in two int registers */
849 static void lexpand(void)
851 int u;
853 u = vtop->type.t & VT_UNSIGNED;
854 gv(RC_INT);
855 vdup();
856 vtop[0].r = vtop[-1].r2;
857 vtop[0].r2 = VT_CONST;
858 vtop[-1].r2 = VT_CONST;
859 vtop[0].type.t = VT_INT | u;
860 vtop[-1].type.t = VT_INT | u;
863 #ifdef TCC_TARGET_ARM
864 /* expand long long on stack */
865 ST_FUNC void lexpand_nr(void)
867 int u,v;
869 u = vtop->type.t & VT_UNSIGNED;
870 vdup();
871 vtop->r2 = VT_CONST;
872 vtop->type.t = VT_INT | u;
873 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
874 if (v == VT_CONST) {
875 vtop[-1].c.ui = vtop->c.ull;
876 vtop->c.ui = vtop->c.ull >> 32;
877 vtop->r = VT_CONST;
878 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
879 vtop->c.ui += 4;
880 vtop->r = vtop[-1].r;
881 } else if (v > VT_CONST) {
882 vtop--;
883 lexpand();
884 } else
885 vtop->r = vtop[-1].r2;
886 vtop[-1].r2 = VT_CONST;
887 vtop[-1].type.t = VT_INT | u;
889 #endif
891 /* build a long long from two ints */
892 static void lbuild(int t)
894 gv2(RC_INT, RC_INT);
895 vtop[-1].r2 = vtop[0].r;
896 vtop[-1].type.t = t;
897 vpop();
900 /* rotate n first stack elements to the bottom
901 I1 ... In -> I2 ... In I1 [top is right]
903 static void vrotb(int n)
905 int i;
906 SValue tmp;
908 tmp = vtop[-n + 1];
909 for(i=-n+1;i!=0;i++)
910 vtop[i] = vtop[i+1];
911 vtop[0] = tmp;
914 /* rotate n first stack elements to the top
915 I1 ... In -> In I1 ... I(n-1) [top is right]
917 ST_FUNC void vrott(int n)
919 int i;
920 SValue tmp;
922 tmp = vtop[0];
923 for(i = 0;i < n - 1; i++)
924 vtop[-i] = vtop[-i - 1];
925 vtop[-n + 1] = tmp;
928 #ifdef TCC_TARGET_ARM
929 /* like vrott but in other direction
930 In ... I1 -> I(n-1) ... I1 In [top is right]
932 ST_FUNC void vnrott(int n)
934 int i;
935 SValue tmp;
937 tmp = vtop[-n + 1];
938 for(i = n - 1; i > 0; i--)
939 vtop[-i] = vtop[-i + 1];
940 vtop[0] = tmp;
942 #endif
944 /* pop stack value */
945 ST_FUNC void vpop(void)
947 int v;
948 v = vtop->r & VT_VALMASK;
949 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
950 /* for x86, we need to pop the FP stack */
951 if (v == TREG_ST0 && !nocode_wanted) {
952 o(0xd8dd); /* fstp %st(0) */
953 } else
954 #endif
955 if (v == VT_JMP || v == VT_JMPI) {
956 /* need to put correct jump if && or || without test */
957 gsym(vtop->c.ul);
959 vtop--;
962 /* convert stack entry to register and duplicate its value in another
963 register */
964 static void gv_dup(void)
966 int rc, t, r, r1;
967 SValue sv;
969 t = vtop->type.t;
970 if ((t & VT_BTYPE) == VT_LLONG) {
971 lexpand();
972 gv_dup();
973 vswap();
974 vrotb(3);
975 gv_dup();
976 vrotb(4);
977 /* stack: H L L1 H1 */
978 lbuild(t);
979 vrotb(3);
980 vrotb(3);
981 vswap();
982 lbuild(t);
983 vswap();
984 } else {
985 /* duplicate value */
986 rc = RC_INT;
987 sv.type.t = VT_INT;
988 if (is_float(t)) {
989 rc = RC_FLOAT;
990 #ifdef TCC_TARGET_X86_64
991 if ((t & VT_BTYPE) == VT_LDOUBLE) {
992 rc = RC_ST0;
994 #endif
995 sv.type.t = t;
997 r = gv(rc);
998 r1 = get_reg(rc);
999 sv.r = r;
1000 sv.c.ul = 0;
1001 load(r1, &sv); /* move r to r1 */
1002 vdup();
1003 /* duplicates value */
1004 if (r != r1)
1005 vtop->r = r1;
1009 #ifndef TCC_TARGET_X86_64
1010 /* generate CPU independent (unsigned) long long operations */
1011 static void gen_opl(int op)
1013 int t, a, b, op1, c, i;
1014 int func;
1015 unsigned short reg_iret = REG_IRET;
1016 unsigned short reg_lret = REG_LRET;
1017 SValue tmp;
1019 switch(op) {
1020 case '/':
1021 case TOK_PDIV:
1022 func = TOK___divdi3;
1023 goto gen_func;
1024 case TOK_UDIV:
1025 func = TOK___udivdi3;
1026 goto gen_func;
1027 case '%':
1028 func = TOK___moddi3;
1029 goto gen_mod_func;
1030 case TOK_UMOD:
1031 func = TOK___umoddi3;
1032 gen_mod_func:
1033 #ifdef TCC_ARM_EABI
1034 reg_iret = TREG_R2;
1035 reg_lret = TREG_R3;
1036 #endif
1037 gen_func:
1038 /* call generic long long function */
1039 vpush_global_sym(&func_old_type, func);
1040 vrott(3);
1041 gfunc_call(2);
1042 vpushi(0);
1043 vtop->r = reg_iret;
1044 vtop->r2 = reg_lret;
1045 break;
1046 case '^':
1047 case '&':
1048 case '|':
1049 case '*':
1050 case '+':
1051 case '-':
1052 t = vtop->type.t;
1053 vswap();
1054 lexpand();
1055 vrotb(3);
1056 lexpand();
1057 /* stack: L1 H1 L2 H2 */
1058 tmp = vtop[0];
1059 vtop[0] = vtop[-3];
1060 vtop[-3] = tmp;
1061 tmp = vtop[-2];
1062 vtop[-2] = vtop[-3];
1063 vtop[-3] = tmp;
1064 vswap();
1065 /* stack: H1 H2 L1 L2 */
1066 if (op == '*') {
1067 vpushv(vtop - 1);
1068 vpushv(vtop - 1);
1069 gen_op(TOK_UMULL);
1070 lexpand();
1071 /* stack: H1 H2 L1 L2 ML MH */
1072 for(i=0;i<4;i++)
1073 vrotb(6);
1074 /* stack: ML MH H1 H2 L1 L2 */
1075 tmp = vtop[0];
1076 vtop[0] = vtop[-2];
1077 vtop[-2] = tmp;
1078 /* stack: ML MH H1 L2 H2 L1 */
1079 gen_op('*');
1080 vrotb(3);
1081 vrotb(3);
1082 gen_op('*');
1083 /* stack: ML MH M1 M2 */
1084 gen_op('+');
1085 gen_op('+');
1086 } else if (op == '+' || op == '-') {
1087 /* XXX: add non carry method too (for MIPS or alpha) */
1088 if (op == '+')
1089 op1 = TOK_ADDC1;
1090 else
1091 op1 = TOK_SUBC1;
1092 gen_op(op1);
1093 /* stack: H1 H2 (L1 op L2) */
1094 vrotb(3);
1095 vrotb(3);
1096 gen_op(op1 + 1); /* TOK_xxxC2 */
1097 } else {
1098 gen_op(op);
1099 /* stack: H1 H2 (L1 op L2) */
1100 vrotb(3);
1101 vrotb(3);
1102 /* stack: (L1 op L2) H1 H2 */
1103 gen_op(op);
1104 /* stack: (L1 op L2) (H1 op H2) */
1106 /* stack: L H */
1107 lbuild(t);
1108 break;
1109 case TOK_SAR:
1110 case TOK_SHR:
1111 case TOK_SHL:
1112 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1113 t = vtop[-1].type.t;
1114 vswap();
1115 lexpand();
1116 vrotb(3);
1117 /* stack: L H shift */
1118 c = (int)vtop->c.i;
1119 /* constant: simpler */
1120 /* NOTE: all comments are for SHL. the other cases are
1121 done by swaping words */
1122 vpop();
1123 if (op != TOK_SHL)
1124 vswap();
1125 if (c >= 32) {
1126 /* stack: L H */
1127 vpop();
1128 if (c > 32) {
1129 vpushi(c - 32);
1130 gen_op(op);
1132 if (op != TOK_SAR) {
1133 vpushi(0);
1134 } else {
1135 gv_dup();
1136 vpushi(31);
1137 gen_op(TOK_SAR);
1139 vswap();
1140 } else {
1141 vswap();
1142 gv_dup();
1143 /* stack: H L L */
1144 vpushi(c);
1145 gen_op(op);
1146 vswap();
1147 vpushi(32 - c);
1148 if (op == TOK_SHL)
1149 gen_op(TOK_SHR);
1150 else
1151 gen_op(TOK_SHL);
1152 vrotb(3);
1153 /* stack: L L H */
1154 vpushi(c);
1155 if (op == TOK_SHL)
1156 gen_op(TOK_SHL);
1157 else
1158 gen_op(TOK_SHR);
1159 gen_op('|');
1161 if (op != TOK_SHL)
1162 vswap();
1163 lbuild(t);
1164 } else {
1165 /* XXX: should provide a faster fallback on x86 ? */
1166 switch(op) {
1167 case TOK_SAR:
1168 func = TOK___ashrdi3;
1169 goto gen_func;
1170 case TOK_SHR:
1171 func = TOK___lshrdi3;
1172 goto gen_func;
1173 case TOK_SHL:
1174 func = TOK___ashldi3;
1175 goto gen_func;
1178 break;
1179 default:
1180 /* compare operations */
1181 t = vtop->type.t;
1182 vswap();
1183 lexpand();
1184 vrotb(3);
1185 lexpand();
1186 /* stack: L1 H1 L2 H2 */
1187 tmp = vtop[-1];
1188 vtop[-1] = vtop[-2];
1189 vtop[-2] = tmp;
1190 /* stack: L1 L2 H1 H2 */
1191 /* compare high */
1192 op1 = op;
1193 /* when values are equal, we need to compare low words. since
1194 the jump is inverted, we invert the test too. */
1195 if (op1 == TOK_LT)
1196 op1 = TOK_LE;
1197 else if (op1 == TOK_GT)
1198 op1 = TOK_GE;
1199 else if (op1 == TOK_ULT)
1200 op1 = TOK_ULE;
1201 else if (op1 == TOK_UGT)
1202 op1 = TOK_UGE;
1203 a = 0;
1204 b = 0;
1205 gen_op(op1);
1206 if (op1 != TOK_NE) {
1207 a = gtst(1, 0);
1209 if (op != TOK_EQ) {
1210 /* generate non equal test */
1211 /* XXX: NOT PORTABLE yet */
1212 if (a == 0) {
1213 b = gtst(0, 0);
1214 } else {
1215 #if defined(TCC_TARGET_I386)
1216 b = psym(0x850f, 0);
1217 #elif defined(TCC_TARGET_ARM)
1218 b = ind;
1219 o(0x1A000000 | encbranch(ind, 0, 1));
1220 #elif defined(TCC_TARGET_C67)
1221 error("not implemented");
1222 #else
1223 #error not supported
1224 #endif
1227 /* compare low. Always unsigned */
1228 op1 = op;
1229 if (op1 == TOK_LT)
1230 op1 = TOK_ULT;
1231 else if (op1 == TOK_LE)
1232 op1 = TOK_ULE;
1233 else if (op1 == TOK_GT)
1234 op1 = TOK_UGT;
1235 else if (op1 == TOK_GE)
1236 op1 = TOK_UGE;
1237 gen_op(op1);
1238 a = gtst(1, a);
1239 gsym(b);
1240 vseti(VT_JMPI, a);
1241 break;
1244 #endif
1246 /* handle integer constant optimizations and various machine
1247 independent opt */
1248 static void gen_opic(int op)
1250 int c1, c2, t1, t2, n;
1251 SValue *v1, *v2;
1252 long long l1, l2;
1253 typedef unsigned long long U;
1255 v1 = vtop - 1;
1256 v2 = vtop;
1257 t1 = v1->type.t & VT_BTYPE;
1258 t2 = v2->type.t & VT_BTYPE;
1260 if (t1 == VT_LLONG)
1261 l1 = v1->c.ll;
1262 else if (v1->type.t & VT_UNSIGNED)
1263 l1 = v1->c.ui;
1264 else
1265 l1 = v1->c.i;
1267 if (t2 == VT_LLONG)
1268 l2 = v2->c.ll;
1269 else if (v2->type.t & VT_UNSIGNED)
1270 l2 = v2->c.ui;
1271 else
1272 l2 = v2->c.i;
1274 /* currently, we cannot do computations with forward symbols */
1275 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1276 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1277 if (c1 && c2) {
1278 switch(op) {
1279 case '+': l1 += l2; break;
1280 case '-': l1 -= l2; break;
1281 case '&': l1 &= l2; break;
1282 case '^': l1 ^= l2; break;
1283 case '|': l1 |= l2; break;
1284 case '*': l1 *= l2; break;
1286 case TOK_PDIV:
1287 case '/':
1288 case '%':
1289 case TOK_UDIV:
1290 case TOK_UMOD:
1291 /* if division by zero, generate explicit division */
1292 if (l2 == 0) {
1293 if (const_wanted)
1294 error("division by zero in constant");
1295 goto general_case;
1297 switch(op) {
1298 default: l1 /= l2; break;
1299 case '%': l1 %= l2; break;
1300 case TOK_UDIV: l1 = (U)l1 / l2; break;
1301 case TOK_UMOD: l1 = (U)l1 % l2; break;
1303 break;
1304 case TOK_SHL: l1 <<= l2; break;
1305 case TOK_SHR: l1 = (U)l1 >> l2; break;
1306 case TOK_SAR: l1 >>= l2; break;
1307 /* tests */
1308 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1309 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1310 case TOK_EQ: l1 = l1 == l2; break;
1311 case TOK_NE: l1 = l1 != l2; break;
1312 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1313 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1314 case TOK_LT: l1 = l1 < l2; break;
1315 case TOK_GE: l1 = l1 >= l2; break;
1316 case TOK_LE: l1 = l1 <= l2; break;
1317 case TOK_GT: l1 = l1 > l2; break;
1318 /* logical */
1319 case TOK_LAND: l1 = l1 && l2; break;
1320 case TOK_LOR: l1 = l1 || l2; break;
1321 default:
1322 goto general_case;
1324 v1->c.ll = l1;
1325 vtop--;
1326 } else {
1327 /* if commutative ops, put c2 as constant */
1328 if (c1 && (op == '+' || op == '&' || op == '^' ||
1329 op == '|' || op == '*')) {
1330 vswap();
1331 c2 = c1; //c = c1, c1 = c2, c2 = c;
1332 l2 = l1; //l = l1, l1 = l2, l2 = l;
1334 /* Filter out NOP operations like x*1, x-0, x&-1... */
1335 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1336 op == TOK_PDIV) &&
1337 l2 == 1) ||
1338 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1339 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1340 l2 == 0) ||
1341 (op == '&' &&
1342 l2 == -1))) {
1343 /* nothing to do */
1344 vtop--;
1345 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1346 /* try to use shifts instead of muls or divs */
1347 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1348 n = -1;
1349 while (l2) {
1350 l2 >>= 1;
1351 n++;
1353 vtop->c.ll = n;
1354 if (op == '*')
1355 op = TOK_SHL;
1356 else if (op == TOK_PDIV)
1357 op = TOK_SAR;
1358 else
1359 op = TOK_SHR;
1361 goto general_case;
1362 } else if (c2 && (op == '+' || op == '-') &&
1363 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1364 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1365 /* symbol + constant case */
1366 if (op == '-')
1367 l2 = -l2;
1368 vtop--;
1369 vtop->c.ll += l2;
1370 } else {
1371 general_case:
1372 if (!nocode_wanted) {
1373 /* call low level op generator */
1374 if (t1 == VT_LLONG || t2 == VT_LLONG)
1375 gen_opl(op);
1376 else
1377 gen_opi(op);
1378 } else {
1379 vtop--;
1385 /* generate a floating point operation with constant propagation */
1386 static void gen_opif(int op)
1388 int c1, c2;
1389 SValue *v1, *v2;
1390 long double f1, f2;
1392 v1 = vtop - 1;
1393 v2 = vtop;
1394 /* currently, we cannot do computations with forward symbols */
1395 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1396 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1397 if (c1 && c2) {
1398 if (v1->type.t == VT_FLOAT) {
1399 f1 = v1->c.f;
1400 f2 = v2->c.f;
1401 } else if (v1->type.t == VT_DOUBLE) {
1402 f1 = v1->c.d;
1403 f2 = v2->c.d;
1404 } else {
1405 f1 = v1->c.ld;
1406 f2 = v2->c.ld;
1409 /* NOTE: we only do constant propagation if finite number (not
1410 NaN or infinity) (ANSI spec) */
1411 if (!ieee_finite(f1) || !ieee_finite(f2))
1412 goto general_case;
1414 switch(op) {
1415 case '+': f1 += f2; break;
1416 case '-': f1 -= f2; break;
1417 case '*': f1 *= f2; break;
1418 case '/':
1419 if (f2 == 0.0) {
1420 if (const_wanted)
1421 error("division by zero in constant");
1422 goto general_case;
1424 f1 /= f2;
1425 break;
1426 /* XXX: also handles tests ? */
1427 default:
1428 goto general_case;
1430 /* XXX: overflow test ? */
1431 if (v1->type.t == VT_FLOAT) {
1432 v1->c.f = f1;
1433 } else if (v1->type.t == VT_DOUBLE) {
1434 v1->c.d = f1;
1435 } else {
1436 v1->c.ld = f1;
1438 vtop--;
1439 } else {
1440 general_case:
1441 if (!nocode_wanted) {
1442 gen_opf(op);
1443 } else {
1444 vtop--;
1449 static int pointed_size(CType *type)
1451 int align;
1452 return type_size(pointed_type(type), &align);
1455 static inline int is_null_pointer(SValue *p)
1457 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1458 return 0;
1459 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1460 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1463 static inline int is_integer_btype(int bt)
1465 return (bt == VT_BYTE || bt == VT_SHORT ||
1466 bt == VT_INT || bt == VT_LLONG);
1469 /* check types for comparison or substraction of pointers */
1470 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1472 CType *type1, *type2, tmp_type1, tmp_type2;
1473 int bt1, bt2;
1475 /* null pointers are accepted for all comparisons as gcc */
1476 if (is_null_pointer(p1) || is_null_pointer(p2))
1477 return;
1478 type1 = &p1->type;
1479 type2 = &p2->type;
1480 bt1 = type1->t & VT_BTYPE;
1481 bt2 = type2->t & VT_BTYPE;
1482 /* accept comparison between pointer and integer with a warning */
1483 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1484 if (op != TOK_LOR && op != TOK_LAND )
1485 warning("comparison between pointer and integer");
1486 return;
1489 /* both must be pointers or implicit function pointers */
1490 if (bt1 == VT_PTR) {
1491 type1 = pointed_type(type1);
1492 } else if (bt1 != VT_FUNC)
1493 goto invalid_operands;
1495 if (bt2 == VT_PTR) {
1496 type2 = pointed_type(type2);
1497 } else if (bt2 != VT_FUNC) {
1498 invalid_operands:
1499 error("invalid operands to binary %s", get_tok_str(op, NULL));
1501 if ((type1->t & VT_BTYPE) == VT_VOID ||
1502 (type2->t & VT_BTYPE) == VT_VOID)
1503 return;
1504 tmp_type1 = *type1;
1505 tmp_type2 = *type2;
1506 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1507 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1508 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1509 /* gcc-like error if '-' is used */
1510 if (op == '-')
1511 goto invalid_operands;
1512 else
1513 warning("comparison of distinct pointer types lacks a cast");
1517 /* generic gen_op: handles types problems */
1518 ST_FUNC void gen_op(int op)
1520 int u, t1, t2, bt1, bt2, t;
1521 CType type1;
1523 t1 = vtop[-1].type.t;
1524 t2 = vtop[0].type.t;
1525 bt1 = t1 & VT_BTYPE;
1526 bt2 = t2 & VT_BTYPE;
1528 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1529 /* at least one operand is a pointer */
1530 /* relationnal op: must be both pointers */
1531 if (op >= TOK_ULT && op <= TOK_LOR) {
1532 check_comparison_pointer_types(vtop - 1, vtop, op);
1533 /* pointers are handled are unsigned */
1534 #ifdef TCC_TARGET_X86_64
1535 t = VT_LLONG | VT_UNSIGNED;
1536 #else
1537 t = VT_INT | VT_UNSIGNED;
1538 #endif
1539 goto std_op;
1541 /* if both pointers, then it must be the '-' op */
1542 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1543 if (op != '-')
1544 error("cannot use pointers here");
1545 check_comparison_pointer_types(vtop - 1, vtop, op);
1546 /* XXX: check that types are compatible */
1547 u = pointed_size(&vtop[-1].type);
1548 gen_opic(op);
1549 /* set to integer type */
1550 #ifdef TCC_TARGET_X86_64
1551 vtop->type.t = VT_LLONG;
1552 #else
1553 vtop->type.t = VT_INT;
1554 #endif
1555 vpushi(u);
1556 gen_op(TOK_PDIV);
1557 } else {
1558 /* exactly one pointer : must be '+' or '-'. */
1559 if (op != '-' && op != '+')
1560 error("cannot use pointers here");
1561 /* Put pointer as first operand */
1562 if (bt2 == VT_PTR) {
1563 vswap();
1564 swap(&t1, &t2);
1566 type1 = vtop[-1].type;
1567 type1.t &= ~VT_ARRAY;
1568 u = pointed_size(&vtop[-1].type);
1569 if (u < 0)
1570 error("unknown array element size");
1571 #ifdef TCC_TARGET_X86_64
1572 vpushll(u);
1573 #else
1574 /* XXX: cast to int ? (long long case) */
1575 vpushi(u);
1576 #endif
1577 gen_op('*');
1578 #ifdef CONFIG_TCC_BCHECK
1579 /* if evaluating constant expression, no code should be
1580 generated, so no bound check */
1581 if (tcc_state->do_bounds_check && !const_wanted) {
1582 /* if bounded pointers, we generate a special code to
1583 test bounds */
1584 if (op == '-') {
1585 vpushi(0);
1586 vswap();
1587 gen_op('-');
1589 gen_bounded_ptr_add();
1590 } else
1591 #endif
1593 gen_opic(op);
1595 /* put again type if gen_opic() swaped operands */
1596 vtop->type = type1;
1598 } else if (is_float(bt1) || is_float(bt2)) {
1599 /* compute bigger type and do implicit casts */
1600 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1601 t = VT_LDOUBLE;
1602 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1603 t = VT_DOUBLE;
1604 } else {
1605 t = VT_FLOAT;
1607 /* floats can only be used for a few operations */
1608 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1609 (op < TOK_ULT || op > TOK_GT))
1610 error("invalid operands for binary operation");
1611 goto std_op;
1612 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1613 /* cast to biggest op */
1614 t = VT_LLONG;
1615 /* convert to unsigned if it does not fit in a long long */
1616 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1617 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1618 t |= VT_UNSIGNED;
1619 goto std_op;
1620 } else {
1621 /* integer operations */
1622 t = VT_INT;
1623 /* convert to unsigned if it does not fit in an integer */
1624 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1625 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1626 t |= VT_UNSIGNED;
1627 std_op:
1628 /* XXX: currently, some unsigned operations are explicit, so
1629 we modify them here */
1630 if (t & VT_UNSIGNED) {
1631 if (op == TOK_SAR)
1632 op = TOK_SHR;
1633 else if (op == '/')
1634 op = TOK_UDIV;
1635 else if (op == '%')
1636 op = TOK_UMOD;
1637 else if (op == TOK_LT)
1638 op = TOK_ULT;
1639 else if (op == TOK_GT)
1640 op = TOK_UGT;
1641 else if (op == TOK_LE)
1642 op = TOK_ULE;
1643 else if (op == TOK_GE)
1644 op = TOK_UGE;
1646 vswap();
1647 type1.t = t;
1648 gen_cast(&type1);
1649 vswap();
1650 /* special case for shifts and long long: we keep the shift as
1651 an integer */
1652 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1653 type1.t = VT_INT;
1654 gen_cast(&type1);
1655 if (is_float(t))
1656 gen_opif(op);
1657 else
1658 gen_opic(op);
1659 if (op >= TOK_ULT && op <= TOK_GT) {
1660 /* relationnal op: the result is an int */
1661 vtop->type.t = VT_INT;
1662 } else {
1663 vtop->type.t = t;
1668 #ifndef TCC_TARGET_ARM
1669 /* generic itof for unsigned long long case */
1670 static void gen_cvt_itof1(int t)
1672 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1673 (VT_LLONG | VT_UNSIGNED)) {
1675 if (t == VT_FLOAT)
1676 vpush_global_sym(&func_old_type, TOK___floatundisf);
1677 #if LDOUBLE_SIZE != 8
1678 else if (t == VT_LDOUBLE)
1679 vpush_global_sym(&func_old_type, TOK___floatundixf);
1680 #endif
1681 else
1682 vpush_global_sym(&func_old_type, TOK___floatundidf);
1683 vrott(2);
1684 gfunc_call(1);
1685 vpushi(0);
1686 vtop->r = reg_fret(t);
1687 } else {
1688 gen_cvt_itof(t);
1691 #endif
1693 /* generic ftoi for unsigned long long case */
1694 static void gen_cvt_ftoi1(int t)
1696 int st;
1698 if (t == (VT_LLONG | VT_UNSIGNED)) {
1699 /* not handled natively */
1700 st = vtop->type.t & VT_BTYPE;
1701 if (st == VT_FLOAT)
1702 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1703 #if LDOUBLE_SIZE != 8
1704 else if (st == VT_LDOUBLE)
1705 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1706 #endif
1707 else
1708 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1709 vrott(2);
1710 gfunc_call(1);
1711 vpushi(0);
1712 vtop->r = REG_IRET;
1713 vtop->r2 = REG_LRET;
1714 } else {
1715 gen_cvt_ftoi(t);
1719 /* force char or short cast */
1720 static void force_charshort_cast(int t)
1722 int bits, dbt;
1723 dbt = t & VT_BTYPE;
1724 /* XXX: add optimization if lvalue : just change type and offset */
1725 if (dbt == VT_BYTE)
1726 bits = 8;
1727 else
1728 bits = 16;
1729 if (t & VT_UNSIGNED) {
1730 vpushi((1 << bits) - 1);
1731 gen_op('&');
1732 } else {
1733 bits = 32 - bits;
1734 vpushi(bits);
1735 gen_op(TOK_SHL);
1736 /* result must be signed or the SAR is converted to an SHL
1737 This was not the case when "t" was a signed short
1738 and the last value on the stack was an unsigned int */
1739 vtop->type.t &= ~VT_UNSIGNED;
1740 vpushi(bits);
1741 gen_op(TOK_SAR);
1745 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1746 static void gen_cast(CType *type)
1748 int sbt, dbt, sf, df, c, p;
1750 /* special delayed cast for char/short */
1751 /* XXX: in some cases (multiple cascaded casts), it may still
1752 be incorrect */
1753 if (vtop->r & VT_MUSTCAST) {
1754 vtop->r &= ~VT_MUSTCAST;
1755 force_charshort_cast(vtop->type.t);
1758 /* bitfields first get cast to ints */
1759 if (vtop->type.t & VT_BITFIELD) {
1760 gv(RC_INT);
1763 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1764 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1766 if (sbt != dbt) {
1767 sf = is_float(sbt);
1768 df = is_float(dbt);
1769 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1770 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1771 if (c) {
1772 /* constant case: we can do it now */
1773 /* XXX: in ISOC, cannot do it if error in convert */
1774 if (sbt == VT_FLOAT)
1775 vtop->c.ld = vtop->c.f;
1776 else if (sbt == VT_DOUBLE)
1777 vtop->c.ld = vtop->c.d;
1779 if (df) {
1780 if ((sbt & VT_BTYPE) == VT_LLONG) {
1781 if (sbt & VT_UNSIGNED)
1782 vtop->c.ld = vtop->c.ull;
1783 else
1784 vtop->c.ld = vtop->c.ll;
1785 } else if(!sf) {
1786 if (sbt & VT_UNSIGNED)
1787 vtop->c.ld = vtop->c.ui;
1788 else
1789 vtop->c.ld = vtop->c.i;
1792 if (dbt == VT_FLOAT)
1793 vtop->c.f = (float)vtop->c.ld;
1794 else if (dbt == VT_DOUBLE)
1795 vtop->c.d = (double)vtop->c.ld;
1796 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1797 vtop->c.ull = (unsigned long long)vtop->c.ld;
1798 } else if (sf && dbt == VT_BOOL) {
1799 vtop->c.i = (vtop->c.ld != 0);
1800 } else {
1801 if(sf)
1802 vtop->c.ll = (long long)vtop->c.ld;
1803 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1804 vtop->c.ll = vtop->c.ull;
1805 else if (sbt & VT_UNSIGNED)
1806 vtop->c.ll = vtop->c.ui;
1807 #ifdef TCC_TARGET_X86_64
1808 else if (sbt == VT_PTR)
1810 #endif
1811 else if (sbt != VT_LLONG)
1812 vtop->c.ll = vtop->c.i;
1814 if (dbt == (VT_LLONG|VT_UNSIGNED))
1815 vtop->c.ull = vtop->c.ll;
1816 else if (dbt == VT_BOOL)
1817 vtop->c.i = (vtop->c.ll != 0);
1818 else if (dbt != VT_LLONG) {
1819 int s = 0;
1820 if ((dbt & VT_BTYPE) == VT_BYTE)
1821 s = 24;
1822 else if ((dbt & VT_BTYPE) == VT_SHORT)
1823 s = 16;
1825 if(dbt & VT_UNSIGNED)
1826 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1827 else
1828 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1831 } else if (p && dbt == VT_BOOL) {
1832 vtop->r = VT_CONST;
1833 vtop->c.i = 1;
1834 } else if (!nocode_wanted) {
1835 /* non constant case: generate code */
1836 if (sf && df) {
1837 /* convert from fp to fp */
1838 gen_cvt_ftof(dbt);
1839 } else if (df) {
1840 /* convert int to fp */
1841 gen_cvt_itof1(dbt);
1842 } else if (sf) {
1843 /* convert fp to int */
1844 if (dbt == VT_BOOL) {
1845 vpushi(0);
1846 gen_op(TOK_NE);
1847 } else {
1848 /* we handle char/short/etc... with generic code */
1849 if (dbt != (VT_INT | VT_UNSIGNED) &&
1850 dbt != (VT_LLONG | VT_UNSIGNED) &&
1851 dbt != VT_LLONG)
1852 dbt = VT_INT;
1853 gen_cvt_ftoi1(dbt);
1854 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1855 /* additional cast for char/short... */
1856 vtop->type.t = dbt;
1857 gen_cast(type);
1860 #ifndef TCC_TARGET_X86_64
1861 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1862 if ((sbt & VT_BTYPE) != VT_LLONG) {
1863 /* scalar to long long */
1864 /* machine independent conversion */
1865 gv(RC_INT);
1866 /* generate high word */
1867 if (sbt == (VT_INT | VT_UNSIGNED)) {
1868 vpushi(0);
1869 gv(RC_INT);
1870 } else {
1871 if (sbt == VT_PTR) {
1872 /* cast from pointer to int before we apply
1873 shift operation, which pointers don't support*/
1874 gen_cast(&int_type);
1876 gv_dup();
1877 vpushi(31);
1878 gen_op(TOK_SAR);
1880 /* patch second register */
1881 vtop[-1].r2 = vtop->r;
1882 vpop();
1884 #else
1885 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1886 (dbt & VT_BTYPE) == VT_PTR) {
1887 /* XXX: not sure if this is perfect... need more tests */
1888 if ((sbt & VT_BTYPE) != VT_LLONG) {
1889 int r = gv(RC_INT);
1890 if (sbt != (VT_INT | VT_UNSIGNED) &&
1891 sbt != VT_PTR && sbt != VT_FUNC) {
1892 /* x86_64 specific: movslq */
1893 o(0x6348);
1894 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1897 #endif
1898 } else if (dbt == VT_BOOL) {
1899 /* scalar to bool */
1900 vpushi(0);
1901 gen_op(TOK_NE);
1902 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1903 (dbt & VT_BTYPE) == VT_SHORT) {
1904 if (sbt == VT_PTR) {
1905 vtop->type.t = VT_INT;
1906 warning("nonportable conversion from pointer to char/short");
1908 force_charshort_cast(dbt);
1909 } else if ((dbt & VT_BTYPE) == VT_INT) {
1910 /* scalar to int */
1911 if (sbt == VT_LLONG) {
1912 /* from long long: just take low order word */
1913 lexpand();
1914 vpop();
1916 /* if lvalue and single word type, nothing to do because
1917 the lvalue already contains the real type size (see
1918 VT_LVAL_xxx constants) */
1921 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1922 /* if we are casting between pointer types,
1923 we must update the VT_LVAL_xxx size */
1924 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1925 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1927 vtop->type = *type;
1930 /* return type size. Put alignment at 'a' */
1931 ST_FUNC int type_size(CType *type, int *a)
1933 Sym *s;
1934 int bt;
1936 bt = type->t & VT_BTYPE;
1937 if (bt == VT_STRUCT) {
1938 /* struct/union */
1939 s = type->ref;
1940 *a = s->r;
1941 return s->c;
1942 } else if (bt == VT_PTR) {
1943 if (type->t & VT_ARRAY) {
1944 int ts;
1946 s = type->ref;
1947 ts = type_size(&s->type, a);
1949 if (ts < 0 && s->c < 0)
1950 ts = -ts;
1952 return ts * s->c;
1953 } else {
1954 *a = PTR_SIZE;
1955 return PTR_SIZE;
1957 } else if (bt == VT_LDOUBLE) {
1958 *a = LDOUBLE_ALIGN;
1959 return LDOUBLE_SIZE;
1960 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1961 #ifdef TCC_TARGET_I386
1962 #ifdef TCC_TARGET_PE
1963 *a = 8;
1964 #else
1965 *a = 4;
1966 #endif
1967 #elif defined(TCC_TARGET_ARM)
1968 #ifdef TCC_ARM_EABI
1969 *a = 8;
1970 #else
1971 *a = 4;
1972 #endif
1973 #else
1974 *a = 8;
1975 #endif
1976 return 8;
1977 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1978 *a = 4;
1979 return 4;
1980 } else if (bt == VT_SHORT) {
1981 *a = 2;
1982 return 2;
1983 } else {
1984 /* char, void, function, _Bool */
1985 *a = 1;
1986 return 1;
1990 /* return the pointed type of t */
1991 static inline CType *pointed_type(CType *type)
1993 return &type->ref->type;
1996 /* modify type so that its it is a pointer to type. */
1997 ST_FUNC void mk_pointer(CType *type)
1999 Sym *s;
2000 s = sym_push(SYM_FIELD, type, 0, -1);
2001 type->t = VT_PTR | (type->t & ~VT_TYPE);
2002 type->ref = s;
2005 /* compare function types. OLD functions match any new functions */
2006 static int is_compatible_func(CType *type1, CType *type2)
2008 Sym *s1, *s2;
2010 s1 = type1->ref;
2011 s2 = type2->ref;
2012 if (!is_compatible_types(&s1->type, &s2->type))
2013 return 0;
2014 /* check func_call */
2015 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2016 return 0;
2017 /* XXX: not complete */
2018 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2019 return 1;
2020 if (s1->c != s2->c)
2021 return 0;
2022 while (s1 != NULL) {
2023 if (s2 == NULL)
2024 return 0;
2025 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2026 return 0;
2027 s1 = s1->next;
2028 s2 = s2->next;
2030 if (s2)
2031 return 0;
2032 return 1;
2035 /* return true if type1 and type2 are the same. If unqualified is
2036 true, qualifiers on the types are ignored.
2038 - enums are not checked as gcc __builtin_types_compatible_p ()
2040 static int compare_types(CType *type1, CType *type2, int unqualified)
2042 int bt1, t1, t2;
2044 t1 = type1->t & VT_TYPE;
2045 t2 = type2->t & VT_TYPE;
2046 if (unqualified) {
2047 /* strip qualifiers before comparing */
2048 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2049 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2051 /* XXX: bitfields ? */
2052 if (t1 != t2)
2053 return 0;
2054 /* test more complicated cases */
2055 bt1 = t1 & VT_BTYPE;
2056 if (bt1 == VT_PTR) {
2057 type1 = pointed_type(type1);
2058 type2 = pointed_type(type2);
2059 return is_compatible_types(type1, type2);
2060 } else if (bt1 == VT_STRUCT) {
2061 return (type1->ref == type2->ref);
2062 } else if (bt1 == VT_FUNC) {
2063 return is_compatible_func(type1, type2);
2064 } else {
2065 return 1;
2069 /* return true if type1 and type2 are exactly the same (including
2070 qualifiers).
2072 static int is_compatible_types(CType *type1, CType *type2)
2074 return compare_types(type1,type2,0);
2077 /* return true if type1 and type2 are the same (ignoring qualifiers).
2079 static int is_compatible_parameter_types(CType *type1, CType *type2)
2081 return compare_types(type1,type2,1);
2084 /* print a type. If 'varstr' is not NULL, then the variable is also
2085 printed in the type */
2086 /* XXX: union */
2087 /* XXX: add array and function pointers */
2088 static void type_to_str(char *buf, int buf_size,
2089 CType *type, const char *varstr)
2091 int bt, v, t;
2092 Sym *s, *sa;
2093 char buf1[256];
2094 const char *tstr;
2096 t = type->t & VT_TYPE;
2097 bt = t & VT_BTYPE;
2098 buf[0] = '\0';
2099 if (t & VT_CONSTANT)
2100 pstrcat(buf, buf_size, "const ");
2101 if (t & VT_VOLATILE)
2102 pstrcat(buf, buf_size, "volatile ");
2103 if (t & VT_UNSIGNED)
2104 pstrcat(buf, buf_size, "unsigned ");
2105 switch(bt) {
2106 case VT_VOID:
2107 tstr = "void";
2108 goto add_tstr;
2109 case VT_BOOL:
2110 tstr = "_Bool";
2111 goto add_tstr;
2112 case VT_BYTE:
2113 tstr = "char";
2114 goto add_tstr;
2115 case VT_SHORT:
2116 tstr = "short";
2117 goto add_tstr;
2118 case VT_INT:
2119 tstr = "int";
2120 goto add_tstr;
2121 case VT_LONG:
2122 tstr = "long";
2123 goto add_tstr;
2124 case VT_LLONG:
2125 tstr = "long long";
2126 goto add_tstr;
2127 case VT_FLOAT:
2128 tstr = "float";
2129 goto add_tstr;
2130 case VT_DOUBLE:
2131 tstr = "double";
2132 goto add_tstr;
2133 case VT_LDOUBLE:
2134 tstr = "long double";
2135 add_tstr:
2136 pstrcat(buf, buf_size, tstr);
2137 break;
2138 case VT_ENUM:
2139 case VT_STRUCT:
2140 if (bt == VT_STRUCT)
2141 tstr = "struct ";
2142 else
2143 tstr = "enum ";
2144 pstrcat(buf, buf_size, tstr);
2145 v = type->ref->v & ~SYM_STRUCT;
2146 if (v >= SYM_FIRST_ANOM)
2147 pstrcat(buf, buf_size, "<anonymous>");
2148 else
2149 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2150 break;
2151 case VT_FUNC:
2152 s = type->ref;
2153 type_to_str(buf, buf_size, &s->type, varstr);
2154 pstrcat(buf, buf_size, "(");
2155 sa = s->next;
2156 while (sa != NULL) {
2157 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2158 pstrcat(buf, buf_size, buf1);
2159 sa = sa->next;
2160 if (sa)
2161 pstrcat(buf, buf_size, ", ");
2163 pstrcat(buf, buf_size, ")");
2164 goto no_var;
2165 case VT_PTR:
2166 s = type->ref;
2167 pstrcpy(buf1, sizeof(buf1), "*");
2168 if (varstr)
2169 pstrcat(buf1, sizeof(buf1), varstr);
2170 type_to_str(buf, buf_size, &s->type, buf1);
2171 goto no_var;
2173 if (varstr) {
2174 pstrcat(buf, buf_size, " ");
2175 pstrcat(buf, buf_size, varstr);
2177 no_var: ;
2180 /* verify type compatibility to store vtop in 'dt' type, and generate
2181 casts if needed. */
2182 static void gen_assign_cast(CType *dt)
2184 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2185 char buf1[256], buf2[256];
2186 int dbt, sbt;
2188 st = &vtop->type; /* source type */
2189 dbt = dt->t & VT_BTYPE;
2190 sbt = st->t & VT_BTYPE;
2191 if (dt->t & VT_CONSTANT)
2192 warning("assignment of read-only location");
2193 switch(dbt) {
2194 case VT_PTR:
2195 /* special cases for pointers */
2196 /* '0' can also be a pointer */
2197 if (is_null_pointer(vtop))
2198 goto type_ok;
2199 /* accept implicit pointer to integer cast with warning */
2200 if (is_integer_btype(sbt)) {
2201 warning("assignment makes pointer from integer without a cast");
2202 goto type_ok;
2204 type1 = pointed_type(dt);
2205 /* a function is implicitely a function pointer */
2206 if (sbt == VT_FUNC) {
2207 if ((type1->t & VT_BTYPE) != VT_VOID &&
2208 !is_compatible_types(pointed_type(dt), st))
2209 warning("assignment from incompatible pointer type");
2210 goto type_ok;
2212 if (sbt != VT_PTR)
2213 goto error;
2214 type2 = pointed_type(st);
2215 if ((type1->t & VT_BTYPE) == VT_VOID ||
2216 (type2->t & VT_BTYPE) == VT_VOID) {
2217 /* void * can match anything */
2218 } else {
2219 /* exact type match, except for unsigned */
2220 tmp_type1 = *type1;
2221 tmp_type2 = *type2;
2222 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2223 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2224 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2225 warning("assignment from incompatible pointer type");
2227 /* check const and volatile */
2228 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2229 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2230 warning("assignment discards qualifiers from pointer target type");
2231 break;
2232 case VT_BYTE:
2233 case VT_SHORT:
2234 case VT_INT:
2235 case VT_LLONG:
2236 if (sbt == VT_PTR || sbt == VT_FUNC) {
2237 warning("assignment makes integer from pointer without a cast");
2239 /* XXX: more tests */
2240 break;
2241 case VT_STRUCT:
2242 tmp_type1 = *dt;
2243 tmp_type2 = *st;
2244 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2245 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2246 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2247 error:
2248 type_to_str(buf1, sizeof(buf1), st, NULL);
2249 type_to_str(buf2, sizeof(buf2), dt, NULL);
2250 error("cannot cast '%s' to '%s'", buf1, buf2);
2252 break;
2254 type_ok:
2255 gen_cast(dt);
2258 /* store vtop in lvalue pushed on stack */
2259 ST_FUNC void vstore(void)
2261 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2263 ft = vtop[-1].type.t;
2264 sbt = vtop->type.t & VT_BTYPE;
2265 dbt = ft & VT_BTYPE;
2266 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2267 (sbt == VT_INT && dbt == VT_SHORT)) {
2268 /* optimize char/short casts */
2269 delayed_cast = VT_MUSTCAST;
2270 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2271 /* XXX: factorize */
2272 if (ft & VT_CONSTANT)
2273 warning("assignment of read-only location");
2274 } else {
2275 delayed_cast = 0;
2276 if (!(ft & VT_BITFIELD))
2277 gen_assign_cast(&vtop[-1].type);
2280 if (sbt == VT_STRUCT) {
2281 /* if structure, only generate pointer */
2282 /* structure assignment : generate memcpy */
2283 /* XXX: optimize if small size */
2284 if (!nocode_wanted) {
2285 size = type_size(&vtop->type, &align);
2287 /* destination */
2288 vswap();
2289 vtop->type.t = VT_PTR;
2290 gaddrof();
2292 /* address of memcpy() */
2293 #ifdef TCC_ARM_EABI
2294 if(!(align & 7))
2295 vpush_global_sym(&func_old_type, TOK_memcpy8);
2296 else if(!(align & 3))
2297 vpush_global_sym(&func_old_type, TOK_memcpy4);
2298 else
2299 #endif
2300 vpush_global_sym(&func_old_type, TOK_memcpy);
2302 vswap();
2303 /* source */
2304 vpushv(vtop - 2);
2305 vtop->type.t = VT_PTR;
2306 gaddrof();
2307 /* type size */
2308 vpushi(size);
2309 gfunc_call(3);
2310 } else {
2311 vswap();
2312 vpop();
2314 /* leave source on stack */
2315 } else if (ft & VT_BITFIELD) {
2316 /* bitfield store handling */
2317 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2318 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2319 /* remove bit field info to avoid loops */
2320 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2322 /* duplicate source into other register */
2323 gv_dup();
2324 vswap();
2325 vrott(3);
2327 if((ft & VT_BTYPE) == VT_BOOL) {
2328 gen_cast(&vtop[-1].type);
2329 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2332 /* duplicate destination */
2333 vdup();
2334 vtop[-1] = vtop[-2];
2336 /* mask and shift source */
2337 if((ft & VT_BTYPE) != VT_BOOL) {
2338 if((ft & VT_BTYPE) == VT_LLONG) {
2339 vpushll((1ULL << bit_size) - 1ULL);
2340 } else {
2341 vpushi((1 << bit_size) - 1);
2343 gen_op('&');
2345 vpushi(bit_pos);
2346 gen_op(TOK_SHL);
2347 /* load destination, mask and or with source */
2348 vswap();
2349 if((ft & VT_BTYPE) == VT_LLONG) {
2350 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2351 } else {
2352 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2354 gen_op('&');
2355 gen_op('|');
2356 /* store result */
2357 vstore();
2359 /* pop off shifted source from "duplicate source..." above */
2360 vpop();
2362 } else {
2363 #ifdef CONFIG_TCC_BCHECK
2364 /* bound check case */
2365 if (vtop[-1].r & VT_MUSTBOUND) {
2366 vswap();
2367 gbound();
2368 vswap();
2370 #endif
2371 if (!nocode_wanted) {
2372 rc = RC_INT;
2373 if (is_float(ft)) {
2374 rc = RC_FLOAT;
2375 #ifdef TCC_TARGET_X86_64
2376 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2377 rc = RC_ST0;
2379 #endif
2381 r = gv(rc); /* generate value */
2382 /* if lvalue was saved on stack, must read it */
2383 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2384 SValue sv;
2385 t = get_reg(RC_INT);
2386 #ifdef TCC_TARGET_X86_64
2387 sv.type.t = VT_PTR;
2388 #else
2389 sv.type.t = VT_INT;
2390 #endif
2391 sv.r = VT_LOCAL | VT_LVAL;
2392 sv.c.ul = vtop[-1].c.ul;
2393 load(t, &sv);
2394 vtop[-1].r = t | VT_LVAL;
2396 store(r, vtop - 1);
2397 #ifndef TCC_TARGET_X86_64
2398 /* two word case handling : store second register at word + 4 */
2399 if ((ft & VT_BTYPE) == VT_LLONG) {
2400 vswap();
2401 /* convert to int to increment easily */
2402 vtop->type.t = VT_INT;
2403 gaddrof();
2404 vpushi(4);
2405 gen_op('+');
2406 vtop->r |= VT_LVAL;
2407 vswap();
2408 /* XXX: it works because r2 is spilled last ! */
2409 store(vtop->r2, vtop - 1);
2411 #endif
2413 vswap();
2414 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2415 vtop->r |= delayed_cast;
2419 /* post defines POST/PRE add. c is the token ++ or -- */
2420 ST_FUNC void inc(int post, int c)
2422 test_lvalue();
2423 vdup(); /* save lvalue */
2424 if (post) {
2425 gv_dup(); /* duplicate value */
2426 vrotb(3);
2427 vrotb(3);
2429 /* add constant */
2430 vpushi(c - TOK_MID);
2431 gen_op('+');
2432 vstore(); /* store value */
2433 if (post)
2434 vpop(); /* if post op, return saved value */
2437 /* Parse GNUC __attribute__ extension. Currently, the following
2438 extensions are recognized:
2439 - aligned(n) : set data/function alignment.
2440 - packed : force data alignment to 1
2441 - section(x) : generate data/code in this section.
2442 - unused : currently ignored, but may be used someday.
2443 - regparm(n) : pass function parameters in registers (i386 only)
2445 static void parse_attribute(AttributeDef *ad)
2447 int t, n;
2449 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2450 next();
2451 skip('(');
2452 skip('(');
2453 while (tok != ')') {
2454 if (tok < TOK_IDENT)
2455 expect("attribute name");
2456 t = tok;
2457 next();
2458 switch(t) {
2459 case TOK_SECTION1:
2460 case TOK_SECTION2:
2461 skip('(');
2462 if (tok != TOK_STR)
2463 expect("section name");
2464 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2465 next();
2466 skip(')');
2467 break;
2468 case TOK_ALIGNED1:
2469 case TOK_ALIGNED2:
2470 if (tok == '(') {
2471 next();
2472 n = expr_const();
2473 if (n <= 0 || (n & (n - 1)) != 0)
2474 error("alignment must be a positive power of two");
2475 skip(')');
2476 } else {
2477 n = MAX_ALIGN;
2479 ad->aligned = n;
2480 break;
2481 case TOK_PACKED1:
2482 case TOK_PACKED2:
2483 ad->packed = 1;
2484 break;
2485 case TOK_WEAK1:
2486 case TOK_WEAK2:
2487 ad->weak = 1;
2488 break;
2489 case TOK_UNUSED1:
2490 case TOK_UNUSED2:
2491 /* currently, no need to handle it because tcc does not
2492 track unused objects */
2493 break;
2494 case TOK_NORETURN1:
2495 case TOK_NORETURN2:
2496 /* currently, no need to handle it because tcc does not
2497 track unused objects */
2498 break;
2499 case TOK_CDECL1:
2500 case TOK_CDECL2:
2501 case TOK_CDECL3:
2502 ad->func_call = FUNC_CDECL;
2503 break;
2504 case TOK_STDCALL1:
2505 case TOK_STDCALL2:
2506 case TOK_STDCALL3:
2507 ad->func_call = FUNC_STDCALL;
2508 break;
2509 #ifdef TCC_TARGET_I386
2510 case TOK_REGPARM1:
2511 case TOK_REGPARM2:
2512 skip('(');
2513 n = expr_const();
2514 if (n > 3)
2515 n = 3;
2516 else if (n < 0)
2517 n = 0;
2518 if (n > 0)
2519 ad->func_call = FUNC_FASTCALL1 + n - 1;
2520 skip(')');
2521 break;
2522 case TOK_FASTCALL1:
2523 case TOK_FASTCALL2:
2524 case TOK_FASTCALL3:
2525 ad->func_call = FUNC_FASTCALLW;
2526 break;
2527 #endif
2528 case TOK_MODE:
2529 skip('(');
2530 switch(tok) {
2531 case TOK_MODE_DI:
2532 ad->mode = VT_LLONG + 1;
2533 break;
2534 case TOK_MODE_HI:
2535 ad->mode = VT_SHORT + 1;
2536 break;
2537 case TOK_MODE_SI:
2538 ad->mode = VT_INT + 1;
2539 break;
2540 default:
2541 warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2542 break;
2544 next();
2545 skip(')');
2546 break;
2547 case TOK_DLLEXPORT:
2548 ad->func_export = 1;
2549 break;
2550 case TOK_DLLIMPORT:
2551 ad->func_import = 1;
2552 break;
2553 default:
2554 if (tcc_state->warn_unsupported)
2555 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2556 /* skip parameters */
2557 if (tok == '(') {
2558 int parenthesis = 0;
2559 do {
2560 if (tok == '(')
2561 parenthesis++;
2562 else if (tok == ')')
2563 parenthesis--;
2564 next();
2565 } while (parenthesis && tok != -1);
2567 break;
2569 if (tok != ',')
2570 break;
2571 next();
2573 skip(')');
2574 skip(')');
2578 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2579 static void struct_decl(CType *type, int u)
2581 int a, v, size, align, maxalign, c, offset;
2582 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2583 Sym *s, *ss, *ass, **ps;
2584 AttributeDef ad;
2585 CType type1, btype;
2587 a = tok; /* save decl type */
2588 next();
2589 if (tok != '{') {
2590 v = tok;
2591 next();
2592 /* struct already defined ? return it */
2593 if (v < TOK_IDENT)
2594 expect("struct/union/enum name");
2595 s = struct_find(v);
2596 if (s) {
2597 if (s->type.t != a)
2598 error("invalid type");
2599 goto do_decl;
2601 } else {
2602 v = anon_sym++;
2604 type1.t = a;
2605 /* we put an undefined size for struct/union */
2606 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2607 s->r = 0; /* default alignment is zero as gcc */
2608 /* put struct/union/enum name in type */
2609 do_decl:
2610 type->t = u;
2611 type->ref = s;
2613 if (tok == '{') {
2614 next();
2615 if (s->c != -1)
2616 error("struct/union/enum already defined");
2617 /* cannot be empty */
2618 c = 0;
2619 /* non empty enums are not allowed */
2620 if (a == TOK_ENUM) {
2621 for(;;) {
2622 v = tok;
2623 if (v < TOK_UIDENT)
2624 expect("identifier");
2625 next();
2626 if (tok == '=') {
2627 next();
2628 c = expr_const();
2630 /* enum symbols have static storage */
2631 ss = sym_push(v, &int_type, VT_CONST, c);
2632 ss->type.t |= VT_STATIC;
2633 if (tok != ',')
2634 break;
2635 next();
2636 c++;
2637 /* NOTE: we accept a trailing comma */
2638 if (tok == '}')
2639 break;
2641 skip('}');
2642 } else {
2643 maxalign = 1;
2644 ps = &s->next;
2645 prevbt = VT_INT;
2646 bit_pos = 0;
2647 offset = 0;
2648 while (tok != '}') {
2649 parse_btype(&btype, &ad);
2650 while (1) {
2651 bit_size = -1;
2652 v = 0;
2653 type1 = btype;
2654 if (tok != ':') {
2655 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2656 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2657 expect("identifier");
2658 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2659 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2660 error("invalid type for '%s'",
2661 get_tok_str(v, NULL));
2663 if (tok == ':') {
2664 next();
2665 bit_size = expr_const();
2666 /* XXX: handle v = 0 case for messages */
2667 if (bit_size < 0)
2668 error("negative width in bit-field '%s'",
2669 get_tok_str(v, NULL));
2670 if (v && bit_size == 0)
2671 error("zero width for bit-field '%s'",
2672 get_tok_str(v, NULL));
2674 size = type_size(&type1, &align);
2675 if (ad.aligned) {
2676 if (align < ad.aligned)
2677 align = ad.aligned;
2678 } else if (ad.packed) {
2679 align = 1;
2680 } else if (*tcc_state->pack_stack_ptr) {
2681 if (align > *tcc_state->pack_stack_ptr)
2682 align = *tcc_state->pack_stack_ptr;
2684 lbit_pos = 0;
2685 if (bit_size >= 0) {
2686 bt = type1.t & VT_BTYPE;
2687 if (bt != VT_INT &&
2688 bt != VT_BYTE &&
2689 bt != VT_SHORT &&
2690 bt != VT_BOOL &&
2691 bt != VT_ENUM &&
2692 bt != VT_LLONG)
2693 error("bitfields must have scalar type");
2694 bsize = size * 8;
2695 if (bit_size > bsize) {
2696 error("width of '%s' exceeds its type",
2697 get_tok_str(v, NULL));
2698 } else if (bit_size == bsize) {
2699 /* no need for bit fields */
2700 bit_pos = 0;
2701 } else if (bit_size == 0) {
2702 /* XXX: what to do if only padding in a
2703 structure ? */
2704 /* zero size: means to pad */
2705 bit_pos = 0;
2706 } else {
2707 /* we do not have enough room ?
2708 did the type change?
2709 is it a union? */
2710 if ((bit_pos + bit_size) > bsize ||
2711 bt != prevbt || a == TOK_UNION)
2712 bit_pos = 0;
2713 lbit_pos = bit_pos;
2714 /* XXX: handle LSB first */
2715 type1.t |= VT_BITFIELD |
2716 (bit_pos << VT_STRUCT_SHIFT) |
2717 (bit_size << (VT_STRUCT_SHIFT + 6));
2718 bit_pos += bit_size;
2720 prevbt = bt;
2721 } else {
2722 bit_pos = 0;
2724 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2725 /* add new memory data only if starting
2726 bit field */
2727 if (lbit_pos == 0) {
2728 if (a == TOK_STRUCT) {
2729 c = (c + align - 1) & -align;
2730 offset = c;
2731 if (size > 0)
2732 c += size;
2733 } else {
2734 offset = 0;
2735 if (size > c)
2736 c = size;
2738 if (align > maxalign)
2739 maxalign = align;
2741 #if 0
2742 printf("add field %s offset=%d",
2743 get_tok_str(v, NULL), offset);
2744 if (type1.t & VT_BITFIELD) {
2745 printf(" pos=%d size=%d",
2746 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2747 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2749 printf("\n");
2750 #endif
2752 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2753 ass = type1.ref;
2754 while ((ass = ass->next) != NULL) {
2755 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2756 *ps = ss;
2757 ps = &ss->next;
2759 } else if (v) {
2760 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2761 *ps = ss;
2762 ps = &ss->next;
2764 if (tok == ';' || tok == TOK_EOF)
2765 break;
2766 skip(',');
2768 skip(';');
2770 skip('}');
2771 /* store size and alignment */
2772 s->c = (c + maxalign - 1) & -maxalign;
2773 s->r = maxalign;
2778 /* return 0 if no type declaration. otherwise, return the basic type
2779 and skip it.
2781 static int parse_btype(CType *type, AttributeDef *ad)
2783 int t, u, type_found, typespec_found, typedef_found;
2784 Sym *s;
2785 CType type1;
2787 memset(ad, 0, sizeof(AttributeDef));
2788 type_found = 0;
2789 typespec_found = 0;
2790 typedef_found = 0;
2791 t = 0;
2792 while(1) {
2793 switch(tok) {
2794 case TOK_EXTENSION:
2795 /* currently, we really ignore extension */
2796 next();
2797 continue;
2799 /* basic types */
2800 case TOK_CHAR:
2801 u = VT_BYTE;
2802 basic_type:
2803 next();
2804 basic_type1:
2805 if ((t & VT_BTYPE) != 0)
2806 error("too many basic types");
2807 t |= u;
2808 typespec_found = 1;
2809 break;
2810 case TOK_VOID:
2811 u = VT_VOID;
2812 goto basic_type;
2813 case TOK_SHORT:
2814 u = VT_SHORT;
2815 goto basic_type;
2816 case TOK_INT:
2817 next();
2818 typespec_found = 1;
2819 break;
2820 case TOK_LONG:
2821 next();
2822 if ((t & VT_BTYPE) == VT_DOUBLE) {
2823 #ifndef TCC_TARGET_PE
2824 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2825 #endif
2826 } else if ((t & VT_BTYPE) == VT_LONG) {
2827 t = (t & ~VT_BTYPE) | VT_LLONG;
2828 } else {
2829 u = VT_LONG;
2830 goto basic_type1;
2832 break;
2833 case TOK_BOOL:
2834 u = VT_BOOL;
2835 goto basic_type;
2836 case TOK_FLOAT:
2837 u = VT_FLOAT;
2838 goto basic_type;
2839 case TOK_DOUBLE:
2840 next();
2841 if ((t & VT_BTYPE) == VT_LONG) {
2842 #ifdef TCC_TARGET_PE
2843 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2844 #else
2845 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2846 #endif
2847 } else {
2848 u = VT_DOUBLE;
2849 goto basic_type1;
2851 break;
2852 case TOK_ENUM:
2853 struct_decl(&type1, VT_ENUM);
2854 basic_type2:
2855 u = type1.t;
2856 type->ref = type1.ref;
2857 goto basic_type1;
2858 case TOK_STRUCT:
2859 case TOK_UNION:
2860 struct_decl(&type1, VT_STRUCT);
2861 goto basic_type2;
2863 /* type modifiers */
2864 case TOK_CONST1:
2865 case TOK_CONST2:
2866 case TOK_CONST3:
2867 t |= VT_CONSTANT;
2868 next();
2869 break;
2870 case TOK_VOLATILE1:
2871 case TOK_VOLATILE2:
2872 case TOK_VOLATILE3:
2873 t |= VT_VOLATILE;
2874 next();
2875 break;
2876 case TOK_SIGNED1:
2877 case TOK_SIGNED2:
2878 case TOK_SIGNED3:
2879 typespec_found = 1;
2880 t |= VT_SIGNED;
2881 next();
2882 break;
2883 case TOK_REGISTER:
2884 case TOK_AUTO:
2885 case TOK_RESTRICT1:
2886 case TOK_RESTRICT2:
2887 case TOK_RESTRICT3:
2888 next();
2889 break;
2890 case TOK_UNSIGNED:
2891 t |= VT_UNSIGNED;
2892 next();
2893 typespec_found = 1;
2894 break;
2896 /* storage */
2897 case TOK_EXTERN:
2898 t |= VT_EXTERN;
2899 next();
2900 break;
2901 case TOK_STATIC:
2902 t |= VT_STATIC;
2903 next();
2904 break;
2905 case TOK_TYPEDEF:
2906 t |= VT_TYPEDEF;
2907 next();
2908 break;
2909 case TOK_INLINE1:
2910 case TOK_INLINE2:
2911 case TOK_INLINE3:
2912 t |= VT_INLINE;
2913 next();
2914 break;
2916 /* GNUC attribute */
2917 case TOK_ATTRIBUTE1:
2918 case TOK_ATTRIBUTE2:
2919 parse_attribute(ad);
2920 if (ad->mode) {
2921 u = ad->mode -1;
2922 t = (t & ~VT_BTYPE) | u;
2924 break;
2925 /* GNUC typeof */
2926 case TOK_TYPEOF1:
2927 case TOK_TYPEOF2:
2928 case TOK_TYPEOF3:
2929 next();
2930 parse_expr_type(&type1);
2931 goto basic_type2;
2932 default:
2933 if (typespec_found || typedef_found)
2934 goto the_end;
2935 s = sym_find(tok);
2936 if (!s || !(s->type.t & VT_TYPEDEF))
2937 goto the_end;
2938 typedef_found = 1;
2939 t |= (s->type.t & ~VT_TYPEDEF);
2940 type->ref = s->type.ref;
2941 if (s->r) {
2942 /* get attributes from typedef */
2943 if (0 == ad->aligned)
2944 ad->aligned = FUNC_ALIGN(s->r);
2945 if (0 == ad->func_call)
2946 ad->func_call = FUNC_CALL(s->r);
2947 ad->packed |= FUNC_PACKED(s->r);
2949 next();
2950 typespec_found = 1;
2951 break;
2953 type_found = 1;
2955 the_end:
2956 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2957 error("signed and unsigned modifier");
2958 if (tcc_state->char_is_unsigned) {
2959 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2960 t |= VT_UNSIGNED;
2962 t &= ~VT_SIGNED;
2964 /* long is never used as type */
2965 if ((t & VT_BTYPE) == VT_LONG)
2966 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2967 t = (t & ~VT_BTYPE) | VT_INT;
2968 #else
2969 t = (t & ~VT_BTYPE) | VT_LLONG;
2970 #endif
2971 type->t = t;
2972 return type_found;
2975 /* convert a function parameter type (array to pointer and function to
2976 function pointer) */
2977 static inline void convert_parameter_type(CType *pt)
2979 /* remove const and volatile qualifiers (XXX: const could be used
2980 to indicate a const function parameter */
2981 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2982 /* array must be transformed to pointer according to ANSI C */
2983 pt->t &= ~VT_ARRAY;
2984 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2985 mk_pointer(pt);
2989 static void post_type(CType *type, AttributeDef *ad)
2991 int n, l, t1, arg_size, align;
2992 Sym **plast, *s, *first;
2993 AttributeDef ad1;
2994 CType pt;
2996 if (tok == '(') {
2997 /* function declaration */
2998 next();
2999 l = 0;
3000 first = NULL;
3001 plast = &first;
3002 arg_size = 0;
3003 if (tok != ')') {
3004 for(;;) {
3005 /* read param name and compute offset */
3006 if (l != FUNC_OLD) {
3007 if (!parse_btype(&pt, &ad1)) {
3008 if (l) {
3009 error("invalid type");
3010 } else {
3011 l = FUNC_OLD;
3012 goto old_proto;
3015 l = FUNC_NEW;
3016 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3017 break;
3018 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3019 if ((pt.t & VT_BTYPE) == VT_VOID)
3020 error("parameter declared as void");
3021 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3022 } else {
3023 old_proto:
3024 n = tok;
3025 if (n < TOK_UIDENT)
3026 expect("identifier");
3027 pt.t = VT_INT;
3028 next();
3030 convert_parameter_type(&pt);
3031 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3032 *plast = s;
3033 plast = &s->next;
3034 if (tok == ')')
3035 break;
3036 skip(',');
3037 if (l == FUNC_NEW && tok == TOK_DOTS) {
3038 l = FUNC_ELLIPSIS;
3039 next();
3040 break;
3044 /* if no parameters, then old type prototype */
3045 if (l == 0)
3046 l = FUNC_OLD;
3047 skip(')');
3048 t1 = type->t & VT_STORAGE;
3049 /* NOTE: const is ignored in returned type as it has a special
3050 meaning in gcc / C++ */
3051 type->t &= ~(VT_STORAGE | VT_CONSTANT);
3052 post_type(type, ad);
3053 /* we push a anonymous symbol which will contain the function prototype */
3054 ad->func_args = arg_size;
3055 s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l);
3056 s->next = first;
3057 type->t = t1 | VT_FUNC;
3058 type->ref = s;
3059 } else if (tok == '[') {
3060 /* array definition */
3061 next();
3062 if (tok == TOK_RESTRICT1)
3063 next();
3064 n = -1;
3065 if (tok != ']') {
3066 n = expr_const();
3067 if (n < 0)
3068 error("invalid array size");
3070 skip(']');
3071 /* parse next post type */
3072 t1 = type->t & VT_STORAGE;
3073 type->t &= ~VT_STORAGE;
3074 post_type(type, ad);
3076 /* we push a anonymous symbol which will contain the array
3077 element type */
3078 s = sym_push(SYM_FIELD, type, 0, n);
3079 type->t = t1 | VT_ARRAY | VT_PTR;
3080 type->ref = s;
3084 /* Parse a type declaration (except basic type), and return the type
3085 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3086 expected. 'type' should contain the basic type. 'ad' is the
3087 attribute definition of the basic type. It can be modified by
3088 type_decl().
3090 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3092 Sym *s;
3093 CType type1, *type2;
3094 int qualifiers;
3096 while (tok == '*') {
3097 qualifiers = 0;
3098 redo:
3099 next();
3100 switch(tok) {
3101 case TOK_CONST1:
3102 case TOK_CONST2:
3103 case TOK_CONST3:
3104 qualifiers |= VT_CONSTANT;
3105 goto redo;
3106 case TOK_VOLATILE1:
3107 case TOK_VOLATILE2:
3108 case TOK_VOLATILE3:
3109 qualifiers |= VT_VOLATILE;
3110 goto redo;
3111 case TOK_RESTRICT1:
3112 case TOK_RESTRICT2:
3113 case TOK_RESTRICT3:
3114 goto redo;
3116 mk_pointer(type);
3117 type->t |= qualifiers;
3120 /* XXX: clarify attribute handling */
3121 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3122 parse_attribute(ad);
3124 /* recursive type */
3125 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3126 type1.t = 0; /* XXX: same as int */
3127 if (tok == '(') {
3128 next();
3129 /* XXX: this is not correct to modify 'ad' at this point, but
3130 the syntax is not clear */
3131 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3132 parse_attribute(ad);
3133 type_decl(&type1, ad, v, td);
3134 skip(')');
3135 } else {
3136 /* type identifier */
3137 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3138 *v = tok;
3139 next();
3140 } else {
3141 if (!(td & TYPE_ABSTRACT))
3142 expect("identifier");
3143 *v = 0;
3146 post_type(type, ad);
3147 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3148 parse_attribute(ad);
3149 if (!type1.t)
3150 return;
3151 /* append type at the end of type1 */
3152 type2 = &type1;
3153 for(;;) {
3154 s = type2->ref;
3155 type2 = &s->type;
3156 if (!type2->t) {
3157 *type2 = *type;
3158 break;
3161 *type = type1;
3164 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3165 ST_FUNC int lvalue_type(int t)
3167 int bt, r;
3168 r = VT_LVAL;
3169 bt = t & VT_BTYPE;
3170 if (bt == VT_BYTE || bt == VT_BOOL)
3171 r |= VT_LVAL_BYTE;
3172 else if (bt == VT_SHORT)
3173 r |= VT_LVAL_SHORT;
3174 else
3175 return r;
3176 if (t & VT_UNSIGNED)
3177 r |= VT_LVAL_UNSIGNED;
3178 return r;
3181 /* indirection with full error checking and bound check */
3182 ST_FUNC void indir(void)
3184 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3185 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3186 return;
3187 expect("pointer");
3189 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3190 gv(RC_INT);
3191 vtop->type = *pointed_type(&vtop->type);
3192 /* Arrays and functions are never lvalues */
3193 if (!(vtop->type.t & VT_ARRAY)
3194 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3195 vtop->r |= lvalue_type(vtop->type.t);
3196 /* if bound checking, the referenced pointer must be checked */
3197 #ifdef CONFIG_TCC_BCHECK
3198 if (tcc_state->do_bounds_check)
3199 vtop->r |= VT_MUSTBOUND;
3200 #endif
3204 /* pass a parameter to a function and do type checking and casting */
3205 static void gfunc_param_typed(Sym *func, Sym *arg)
3207 int func_type;
3208 CType type;
3210 func_type = func->c;
3211 if (func_type == FUNC_OLD ||
3212 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3213 /* default casting : only need to convert float to double */
3214 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3215 type.t = VT_DOUBLE;
3216 gen_cast(&type);
3218 } else if (arg == NULL) {
3219 error("too many arguments to function");
3220 } else {
3221 type = arg->type;
3222 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3223 gen_assign_cast(&type);
3227 /* parse an expression of the form '(type)' or '(expr)' and return its
3228 type */
3229 static void parse_expr_type(CType *type)
3231 int n;
3232 AttributeDef ad;
3234 skip('(');
3235 if (parse_btype(type, &ad)) {
3236 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3237 } else {
3238 expr_type(type);
3240 skip(')');
3243 static void parse_type(CType *type)
3245 AttributeDef ad;
3246 int n;
3248 if (!parse_btype(type, &ad)) {
3249 expect("type");
3251 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3254 static void vpush_tokc(int t)
3256 CType type;
3257 type.t = t;
3258 type.ref = 0;
3259 vsetc(&type, VT_CONST, &tokc);
3262 ST_FUNC void unary(void)
3264 int n, t, align, size, r, sizeof_caller;
3265 CType type;
3266 Sym *s;
3267 AttributeDef ad;
3268 static int in_sizeof = 0;
3270 sizeof_caller = in_sizeof;
3271 in_sizeof = 0;
3272 /* XXX: GCC 2.95.3 does not generate a table although it should be
3273 better here */
3274 tok_next:
3275 switch(tok) {
3276 case TOK_EXTENSION:
3277 next();
3278 goto tok_next;
3279 case TOK_CINT:
3280 case TOK_CCHAR:
3281 case TOK_LCHAR:
3282 vpushi(tokc.i);
3283 next();
3284 break;
3285 case TOK_CUINT:
3286 vpush_tokc(VT_INT | VT_UNSIGNED);
3287 next();
3288 break;
3289 case TOK_CLLONG:
3290 vpush_tokc(VT_LLONG);
3291 next();
3292 break;
3293 case TOK_CULLONG:
3294 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3295 next();
3296 break;
3297 case TOK_CFLOAT:
3298 vpush_tokc(VT_FLOAT);
3299 next();
3300 break;
3301 case TOK_CDOUBLE:
3302 vpush_tokc(VT_DOUBLE);
3303 next();
3304 break;
3305 case TOK_CLDOUBLE:
3306 vpush_tokc(VT_LDOUBLE);
3307 next();
3308 break;
3309 case TOK___FUNCTION__:
3310 if (!gnu_ext)
3311 goto tok_identifier;
3312 /* fall thru */
3313 case TOK___FUNC__:
3315 void *ptr;
3316 int len;
3317 /* special function name identifier */
3318 len = strlen(funcname) + 1;
3319 /* generate char[len] type */
3320 type.t = VT_BYTE;
3321 mk_pointer(&type);
3322 type.t |= VT_ARRAY;
3323 type.ref->c = len;
3324 vpush_ref(&type, data_section, data_section->data_offset, len);
3325 ptr = section_ptr_add(data_section, len);
3326 memcpy(ptr, funcname, len);
3327 next();
3329 break;
3330 case TOK_LSTR:
3331 #ifdef TCC_TARGET_PE
3332 t = VT_SHORT | VT_UNSIGNED;
3333 #else
3334 t = VT_INT;
3335 #endif
3336 goto str_init;
3337 case TOK_STR:
3338 /* string parsing */
3339 t = VT_BYTE;
3340 str_init:
3341 if (tcc_state->warn_write_strings)
3342 t |= VT_CONSTANT;
3343 type.t = t;
3344 mk_pointer(&type);
3345 type.t |= VT_ARRAY;
3346 memset(&ad, 0, sizeof(AttributeDef));
3347 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3348 break;
3349 case '(':
3350 next();
3351 /* cast ? */
3352 if (parse_btype(&type, &ad)) {
3353 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3354 skip(')');
3355 /* check ISOC99 compound literal */
3356 if (tok == '{') {
3357 /* data is allocated locally by default */
3358 if (global_expr)
3359 r = VT_CONST;
3360 else
3361 r = VT_LOCAL;
3362 /* all except arrays are lvalues */
3363 if (!(type.t & VT_ARRAY))
3364 r |= lvalue_type(type.t);
3365 memset(&ad, 0, sizeof(AttributeDef));
3366 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3367 } else {
3368 if (sizeof_caller) {
3369 vpush(&type);
3370 return;
3372 unary();
3373 gen_cast(&type);
3375 } else if (tok == '{') {
3376 /* save all registers */
3377 save_regs(0);
3378 /* statement expression : we do not accept break/continue
3379 inside as GCC does */
3380 block(NULL, NULL, NULL, NULL, 0, 1);
3381 skip(')');
3382 } else {
3383 gexpr();
3384 skip(')');
3386 break;
3387 case '*':
3388 next();
3389 unary();
3390 indir();
3391 break;
3392 case '&':
3393 next();
3394 unary();
3395 /* functions names must be treated as function pointers,
3396 except for unary '&' and sizeof. Since we consider that
3397 functions are not lvalues, we only have to handle it
3398 there and in function calls. */
3399 /* arrays can also be used although they are not lvalues */
3400 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3401 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3402 test_lvalue();
3403 mk_pointer(&vtop->type);
3404 gaddrof();
3405 break;
3406 case '!':
3407 next();
3408 unary();
3409 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3410 CType boolean;
3411 boolean.t = VT_BOOL;
3412 gen_cast(&boolean);
3413 vtop->c.i = !vtop->c.i;
3414 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3415 vtop->c.i = vtop->c.i ^ 1;
3416 else {
3417 save_regs(1);
3418 vseti(VT_JMP, gtst(1, 0));
3420 break;
3421 case '~':
3422 next();
3423 unary();
3424 vpushi(-1);
3425 gen_op('^');
3426 break;
3427 case '+':
3428 next();
3429 /* in order to force cast, we add zero */
3430 unary();
3431 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3432 error("pointer not accepted for unary plus");
3433 vpushi(0);
3434 gen_op('+');
3435 break;
3436 case TOK_SIZEOF:
3437 case TOK_ALIGNOF1:
3438 case TOK_ALIGNOF2:
3439 t = tok;
3440 next();
3441 in_sizeof++;
3442 unary_type(&type); // Perform a in_sizeof = 0;
3443 size = type_size(&type, &align);
3444 if (t == TOK_SIZEOF) {
3445 if (size < 0)
3446 error("sizeof applied to an incomplete type");
3447 vpushi(size);
3448 } else {
3449 vpushi(align);
3451 vtop->type.t |= VT_UNSIGNED;
3452 break;
3454 case TOK_builtin_types_compatible_p:
3456 CType type1, type2;
3457 next();
3458 skip('(');
3459 parse_type(&type1);
3460 skip(',');
3461 parse_type(&type2);
3462 skip(')');
3463 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3464 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3465 vpushi(is_compatible_types(&type1, &type2));
3467 break;
3468 case TOK_builtin_constant_p:
3470 int saved_nocode_wanted, res;
3471 next();
3472 skip('(');
3473 saved_nocode_wanted = nocode_wanted;
3474 nocode_wanted = 1;
3475 gexpr();
3476 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3477 vpop();
3478 nocode_wanted = saved_nocode_wanted;
3479 skip(')');
3480 vpushi(res);
3482 break;
3483 case TOK_builtin_frame_address:
3485 CType type;
3486 next();
3487 skip('(');
3488 if (tok != TOK_CINT) {
3489 error("__builtin_frame_address only takes integers");
3491 if (tokc.i != 0) {
3492 error("TCC only supports __builtin_frame_address(0)");
3494 next();
3495 skip(')');
3496 type.t = VT_VOID;
3497 mk_pointer(&type);
3498 vset(&type, VT_LOCAL, 0);
3500 break;
3501 #ifdef TCC_TARGET_X86_64
3502 case TOK_builtin_malloc:
3503 tok = TOK_malloc;
3504 goto tok_identifier;
3505 case TOK_builtin_free:
3506 tok = TOK_free;
3507 goto tok_identifier;
3508 #endif
3509 case TOK_INC:
3510 case TOK_DEC:
3511 t = tok;
3512 next();
3513 unary();
3514 inc(0, t);
3515 break;
3516 case '-':
3517 next();
3518 vpushi(0);
3519 unary();
3520 gen_op('-');
3521 break;
3522 case TOK_LAND:
3523 if (!gnu_ext)
3524 goto tok_identifier;
3525 next();
3526 /* allow to take the address of a label */
3527 if (tok < TOK_UIDENT)
3528 expect("label identifier");
3529 s = label_find(tok);
3530 if (!s) {
3531 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3532 } else {
3533 if (s->r == LABEL_DECLARED)
3534 s->r = LABEL_FORWARD;
3536 if (!s->type.t) {
3537 s->type.t = VT_VOID;
3538 mk_pointer(&s->type);
3539 s->type.t |= VT_STATIC;
3541 vset(&s->type, VT_CONST | VT_SYM, 0);
3542 vtop->sym = s;
3543 next();
3544 break;
3546 // special qnan , snan and infinity values
3547 case TOK___NAN__:
3548 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3549 next();
3550 break;
3551 case TOK___SNAN__:
3552 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3553 next();
3554 break;
3555 case TOK___INF__:
3556 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3557 next();
3558 break;
3560 default:
3561 tok_identifier:
3562 t = tok;
3563 next();
3564 if (t < TOK_UIDENT)
3565 expect("identifier");
3566 s = sym_find(t);
3567 if (!s) {
3568 if (tok != '(')
3569 error("'%s' undeclared", get_tok_str(t, NULL));
3570 /* for simple function calls, we tolerate undeclared
3571 external reference to int() function */
3572 if (tcc_state->warn_implicit_function_declaration)
3573 warning("implicit declaration of function '%s'",
3574 get_tok_str(t, NULL));
3575 s = external_global_sym(t, &func_old_type, 0);
3577 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3578 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3579 /* if referencing an inline function, then we generate a
3580 symbol to it if not already done. It will have the
3581 effect to generate code for it at the end of the
3582 compilation unit. Inline function as always
3583 generated in the text section. */
3584 if (!s->c)
3585 put_extern_sym(s, text_section, 0, 0);
3586 r = VT_SYM | VT_CONST;
3587 } else {
3588 r = s->r;
3590 vset(&s->type, r, s->c);
3591 /* if forward reference, we must point to s */
3592 if (vtop->r & VT_SYM) {
3593 vtop->sym = s;
3594 vtop->c.ul = 0;
3596 break;
3599 /* post operations */
3600 while (1) {
3601 if (tok == TOK_INC || tok == TOK_DEC) {
3602 inc(1, tok);
3603 next();
3604 } else if (tok == '.' || tok == TOK_ARROW) {
3605 int qualifiers;
3606 /* field */
3607 if (tok == TOK_ARROW)
3608 indir();
3609 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3610 test_lvalue();
3611 gaddrof();
3612 next();
3613 /* expect pointer on structure */
3614 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3615 expect("struct or union");
3616 s = vtop->type.ref;
3617 /* find field */
3618 tok |= SYM_FIELD;
3619 while ((s = s->next) != NULL) {
3620 if (s->v == tok)
3621 break;
3623 if (!s)
3624 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3625 /* add field offset to pointer */
3626 vtop->type = char_pointer_type; /* change type to 'char *' */
3627 vpushi(s->c);
3628 gen_op('+');
3629 /* change type to field type, and set to lvalue */
3630 vtop->type = s->type;
3631 vtop->type.t |= qualifiers;
3632 /* an array is never an lvalue */
3633 if (!(vtop->type.t & VT_ARRAY)) {
3634 vtop->r |= lvalue_type(vtop->type.t);
3635 #ifdef CONFIG_TCC_BCHECK
3636 /* if bound checking, the referenced pointer must be checked */
3637 if (tcc_state->do_bounds_check)
3638 vtop->r |= VT_MUSTBOUND;
3639 #endif
3641 next();
3642 } else if (tok == '[') {
3643 next();
3644 gexpr();
3645 gen_op('+');
3646 indir();
3647 skip(']');
3648 } else if (tok == '(') {
3649 SValue ret;
3650 Sym *sa;
3651 int nb_args;
3653 /* function call */
3654 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3655 /* pointer test (no array accepted) */
3656 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3657 vtop->type = *pointed_type(&vtop->type);
3658 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3659 goto error_func;
3660 } else {
3661 error_func:
3662 expect("function pointer");
3664 } else {
3665 vtop->r &= ~VT_LVAL; /* no lvalue */
3667 /* get return type */
3668 s = vtop->type.ref;
3669 next();
3670 sa = s->next; /* first parameter */
3671 nb_args = 0;
3672 ret.r2 = VT_CONST;
3673 /* compute first implicit argument if a structure is returned */
3674 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3675 /* get some space for the returned structure */
3676 size = type_size(&s->type, &align);
3677 loc = (loc - size) & -align;
3678 ret.type = s->type;
3679 ret.r = VT_LOCAL | VT_LVAL;
3680 /* pass it as 'int' to avoid structure arg passing
3681 problems */
3682 vseti(VT_LOCAL, loc);
3683 ret.c = vtop->c;
3684 nb_args++;
3685 } else {
3686 ret.type = s->type;
3687 /* return in register */
3688 if (is_float(ret.type.t)) {
3689 ret.r = reg_fret(ret.type.t);
3690 } else {
3691 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3692 ret.r2 = REG_LRET;
3693 ret.r = REG_IRET;
3695 ret.c.i = 0;
3697 if (tok != ')') {
3698 for(;;) {
3699 expr_eq();
3700 gfunc_param_typed(s, sa);
3701 nb_args++;
3702 if (sa)
3703 sa = sa->next;
3704 if (tok == ')')
3705 break;
3706 skip(',');
3709 if (sa)
3710 error("too few arguments to function");
3711 skip(')');
3712 if (!nocode_wanted) {
3713 gfunc_call(nb_args);
3714 } else {
3715 vtop -= (nb_args + 1);
3717 /* return value */
3718 vsetc(&ret.type, ret.r, &ret.c);
3719 vtop->r2 = ret.r2;
3720 } else {
3721 break;
3726 static void uneq(void)
3728 int t;
3730 unary();
3731 if (tok == '=' ||
3732 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3733 tok == TOK_A_XOR || tok == TOK_A_OR ||
3734 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3735 test_lvalue();
3736 t = tok;
3737 next();
3738 if (t == '=') {
3739 expr_eq();
3740 } else {
3741 vdup();
3742 expr_eq();
3743 gen_op(t & 0x7f);
3745 vstore();
3749 ST_FUNC void expr_prod(void)
3751 int t;
3753 uneq();
3754 while (tok == '*' || tok == '/' || tok == '%') {
3755 t = tok;
3756 next();
3757 uneq();
3758 gen_op(t);
3762 ST_FUNC void expr_sum(void)
3764 int t;
3766 expr_prod();
3767 while (tok == '+' || tok == '-') {
3768 t = tok;
3769 next();
3770 expr_prod();
3771 gen_op(t);
3775 static void expr_shift(void)
3777 int t;
3779 expr_sum();
3780 while (tok == TOK_SHL || tok == TOK_SAR) {
3781 t = tok;
3782 next();
3783 expr_sum();
3784 gen_op(t);
3788 static void expr_cmp(void)
3790 int t;
3792 expr_shift();
3793 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3794 tok == TOK_ULT || tok == TOK_UGE) {
3795 t = tok;
3796 next();
3797 expr_shift();
3798 gen_op(t);
3802 static void expr_cmpeq(void)
3804 int t;
3806 expr_cmp();
3807 while (tok == TOK_EQ || tok == TOK_NE) {
3808 t = tok;
3809 next();
3810 expr_cmp();
3811 gen_op(t);
3815 static void expr_and(void)
3817 expr_cmpeq();
3818 while (tok == '&') {
3819 next();
3820 expr_cmpeq();
3821 gen_op('&');
3825 static void expr_xor(void)
3827 expr_and();
3828 while (tok == '^') {
3829 next();
3830 expr_and();
3831 gen_op('^');
3835 static void expr_or(void)
3837 expr_xor();
3838 while (tok == '|') {
3839 next();
3840 expr_xor();
3841 gen_op('|');
3845 /* XXX: fix this mess */
3846 static void expr_land_const(void)
3848 expr_or();
3849 while (tok == TOK_LAND) {
3850 next();
3851 expr_or();
3852 gen_op(TOK_LAND);
3856 /* XXX: fix this mess */
3857 static void expr_lor_const(void)
3859 expr_land_const();
3860 while (tok == TOK_LOR) {
3861 next();
3862 expr_land_const();
3863 gen_op(TOK_LOR);
3867 /* only used if non constant */
3868 static void expr_land(void)
3870 int t;
3872 expr_or();
3873 if (tok == TOK_LAND) {
3874 t = 0;
3875 save_regs(1);
3876 for(;;) {
3877 t = gtst(1, t);
3878 if (tok != TOK_LAND) {
3879 vseti(VT_JMPI, t);
3880 break;
3882 next();
3883 expr_or();
3888 static void expr_lor(void)
3890 int t;
3892 expr_land();
3893 if (tok == TOK_LOR) {
3894 t = 0;
3895 save_regs(1);
3896 for(;;) {
3897 t = gtst(0, t);
3898 if (tok != TOK_LOR) {
3899 vseti(VT_JMP, t);
3900 break;
3902 next();
3903 expr_land();
3908 /* XXX: better constant handling */
3909 static void expr_eq(void)
3911 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3912 SValue sv;
3913 CType type, type1, type2;
3915 if (const_wanted) {
3916 expr_lor_const();
3917 if (tok == '?') {
3918 CType boolean;
3919 int c;
3920 boolean.t = VT_BOOL;
3921 vdup();
3922 gen_cast(&boolean);
3923 c = vtop->c.i;
3924 vpop();
3925 next();
3926 if (tok != ':' || !gnu_ext) {
3927 vpop();
3928 gexpr();
3930 if (!c)
3931 vpop();
3932 skip(':');
3933 expr_eq();
3934 if (c)
3935 vpop();
3937 } else {
3938 expr_lor();
3939 if (tok == '?') {
3940 next();
3941 if (vtop != vstack) {
3942 /* needed to avoid having different registers saved in
3943 each branch */
3944 if (is_float(vtop->type.t)) {
3945 rc = RC_FLOAT;
3946 #ifdef TCC_TARGET_X86_64
3947 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3948 rc = RC_ST0;
3950 #endif
3952 else
3953 rc = RC_INT;
3954 gv(rc);
3955 save_regs(1);
3957 if (tok == ':' && gnu_ext) {
3958 gv_dup();
3959 tt = gtst(1, 0);
3960 } else {
3961 tt = gtst(1, 0);
3962 gexpr();
3964 type1 = vtop->type;
3965 sv = *vtop; /* save value to handle it later */
3966 vtop--; /* no vpop so that FP stack is not flushed */
3967 skip(':');
3968 u = gjmp(0);
3969 gsym(tt);
3970 expr_eq();
3971 type2 = vtop->type;
3973 t1 = type1.t;
3974 bt1 = t1 & VT_BTYPE;
3975 t2 = type2.t;
3976 bt2 = t2 & VT_BTYPE;
3977 /* cast operands to correct type according to ISOC rules */
3978 if (is_float(bt1) || is_float(bt2)) {
3979 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3980 type.t = VT_LDOUBLE;
3981 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3982 type.t = VT_DOUBLE;
3983 } else {
3984 type.t = VT_FLOAT;
3986 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3987 /* cast to biggest op */
3988 type.t = VT_LLONG;
3989 /* convert to unsigned if it does not fit in a long long */
3990 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3991 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3992 type.t |= VT_UNSIGNED;
3993 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3994 /* XXX: test pointer compatibility */
3995 type = type1;
3996 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3997 /* XXX: test function pointer compatibility */
3998 type = type1;
3999 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4000 /* XXX: test structure compatibility */
4001 type = type1;
4002 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4003 /* NOTE: as an extension, we accept void on only one side */
4004 type.t = VT_VOID;
4005 } else {
4006 /* integer operations */
4007 type.t = VT_INT;
4008 /* convert to unsigned if it does not fit in an integer */
4009 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4010 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4011 type.t |= VT_UNSIGNED;
4014 /* now we convert second operand */
4015 gen_cast(&type);
4016 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4017 gaddrof();
4018 rc = RC_INT;
4019 if (is_float(type.t)) {
4020 rc = RC_FLOAT;
4021 #ifdef TCC_TARGET_X86_64
4022 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4023 rc = RC_ST0;
4025 #endif
4026 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4027 /* for long longs, we use fixed registers to avoid having
4028 to handle a complicated move */
4029 rc = RC_IRET;
4032 r2 = gv(rc);
4033 /* this is horrible, but we must also convert first
4034 operand */
4035 tt = gjmp(0);
4036 gsym(u);
4037 /* put again first value and cast it */
4038 *vtop = sv;
4039 gen_cast(&type);
4040 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4041 gaddrof();
4042 r1 = gv(rc);
4043 move_reg(r2, r1);
4044 vtop->r = r2;
4045 gsym(tt);
4050 ST_FUNC void gexpr(void)
4052 while (1) {
4053 expr_eq();
4054 if (tok != ',')
4055 break;
4056 vpop();
4057 next();
4061 /* parse an expression and return its type without any side effect. */
4062 static void expr_type(CType *type)
4064 int saved_nocode_wanted;
4066 saved_nocode_wanted = nocode_wanted;
4067 nocode_wanted = 1;
4068 gexpr();
4069 *type = vtop->type;
4070 vpop();
4071 nocode_wanted = saved_nocode_wanted;
4074 /* parse a unary expression and return its type without any side
4075 effect. */
4076 static void unary_type(CType *type)
4078 int a;
4080 a = nocode_wanted;
4081 nocode_wanted = 1;
4082 unary();
4083 *type = vtop->type;
4084 vpop();
4085 nocode_wanted = a;
4088 /* parse a constant expression and return value in vtop. */
4089 static void expr_const1(void)
4091 int a;
4092 a = const_wanted;
4093 const_wanted = 1;
4094 expr_eq();
4095 const_wanted = a;
4098 /* parse an integer constant and return its value. */
4099 ST_FUNC int expr_const(void)
4101 int c;
4102 expr_const1();
4103 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4104 expect("constant expression");
4105 c = vtop->c.i;
4106 vpop();
4107 return c;
4110 /* return the label token if current token is a label, otherwise
4111 return zero */
4112 static int is_label(void)
4114 int last_tok;
4116 /* fast test first */
4117 if (tok < TOK_UIDENT)
4118 return 0;
4119 /* no need to save tokc because tok is an identifier */
4120 last_tok = tok;
4121 next();
4122 if (tok == ':') {
4123 next();
4124 return last_tok;
4125 } else {
4126 unget_tok(last_tok);
4127 return 0;
4131 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4132 int case_reg, int is_expr)
4134 int a, b, c, d;
4135 Sym *s;
4137 /* generate line number info */
4138 if (tcc_state->do_debug &&
4139 (last_line_num != file->line_num || last_ind != ind)) {
4140 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4141 last_ind = ind;
4142 last_line_num = file->line_num;
4145 if (is_expr) {
4146 /* default return value is (void) */
4147 vpushi(0);
4148 vtop->type.t = VT_VOID;
4151 if (tok == TOK_IF) {
4152 /* if test */
4153 next();
4154 skip('(');
4155 gexpr();
4156 skip(')');
4157 a = gtst(1, 0);
4158 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4159 c = tok;
4160 if (c == TOK_ELSE) {
4161 next();
4162 d = gjmp(0);
4163 gsym(a);
4164 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4165 gsym(d); /* patch else jmp */
4166 } else
4167 gsym(a);
4168 } else if (tok == TOK_WHILE) {
4169 next();
4170 d = ind;
4171 skip('(');
4172 gexpr();
4173 skip(')');
4174 a = gtst(1, 0);
4175 b = 0;
4176 block(&a, &b, case_sym, def_sym, case_reg, 0);
4177 gjmp_addr(d);
4178 gsym(a);
4179 gsym_addr(b, d);
4180 } else if (tok == '{') {
4181 Sym *llabel;
4183 next();
4184 /* record local declaration stack position */
4185 s = local_stack;
4186 llabel = local_label_stack;
4187 /* handle local labels declarations */
4188 if (tok == TOK_LABEL) {
4189 next();
4190 for(;;) {
4191 if (tok < TOK_UIDENT)
4192 expect("label identifier");
4193 label_push(&local_label_stack, tok, LABEL_DECLARED);
4194 next();
4195 if (tok == ',') {
4196 next();
4197 } else {
4198 skip(';');
4199 break;
4203 while (tok != '}') {
4204 decl(VT_LOCAL);
4205 if (tok != '}') {
4206 if (is_expr)
4207 vpop();
4208 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4211 /* pop locally defined labels */
4212 label_pop(&local_label_stack, llabel);
4213 /* pop locally defined symbols */
4214 if(is_expr) {
4215 /* XXX: this solution makes only valgrind happy...
4216 triggered by gcc.c-torture/execute/20000917-1.c */
4217 Sym *p;
4218 switch(vtop->type.t & VT_BTYPE) {
4219 case VT_PTR:
4220 case VT_STRUCT:
4221 case VT_ENUM:
4222 case VT_FUNC:
4223 for(p=vtop->type.ref;p;p=p->prev)
4224 if(p->prev==s)
4225 error("unsupported expression type");
4228 sym_pop(&local_stack, s);
4229 next();
4230 } else if (tok == TOK_RETURN) {
4231 next();
4232 if (tok != ';') {
4233 gexpr();
4234 gen_assign_cast(&func_vt);
4235 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4236 CType type;
4237 /* if returning structure, must copy it to implicit
4238 first pointer arg location */
4239 #ifdef TCC_ARM_EABI
4240 int align, size;
4241 size = type_size(&func_vt,&align);
4242 if(size <= 4)
4244 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4245 && (align & 3))
4247 int addr;
4248 loc = (loc - size) & -4;
4249 addr = loc;
4250 type = func_vt;
4251 vset(&type, VT_LOCAL | VT_LVAL, addr);
4252 vswap();
4253 vstore();
4254 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
4256 vtop->type = int_type;
4257 gv(RC_IRET);
4258 } else {
4259 #endif
4260 type = func_vt;
4261 mk_pointer(&type);
4262 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
4263 indir();
4264 vswap();
4265 /* copy structure value to pointer */
4266 vstore();
4267 #ifdef TCC_ARM_EABI
4269 #endif
4270 } else if (is_float(func_vt.t)) {
4271 gv(rc_fret(func_vt.t));
4272 } else {
4273 gv(RC_IRET);
4275 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
4277 skip(';');
4278 rsym = gjmp(rsym); /* jmp */
4279 } else if (tok == TOK_BREAK) {
4280 /* compute jump */
4281 if (!bsym)
4282 error("cannot break");
4283 *bsym = gjmp(*bsym);
4284 next();
4285 skip(';');
4286 } else if (tok == TOK_CONTINUE) {
4287 /* compute jump */
4288 if (!csym)
4289 error("cannot continue");
4290 *csym = gjmp(*csym);
4291 next();
4292 skip(';');
4293 } else if (tok == TOK_FOR) {
4294 int e, c99_for_decl = 0;
4295 next();
4296 skip('(');
4297 if (tok != ';') {
4298 if (tok < TOK_UIDENT) {
4299 // handle C99 for loop construct
4300 c99_for_decl = 1;
4302 /* record local declaration stack position */
4303 s = local_stack;
4305 decl(VT_LOCAL);
4306 if (is_expr)
4307 vpop();
4308 } else {
4309 gexpr();
4310 vpop();
4311 skip(';');
4313 } else {
4314 skip(';');
4316 d = ind;
4317 c = ind;
4318 a = 0;
4319 b = 0;
4320 if (tok != ';') {
4321 gexpr();
4322 a = gtst(1, 0);
4324 skip(';');
4325 if (tok != ')') {
4326 e = gjmp(0);
4327 c = ind;
4328 gexpr();
4329 vpop();
4330 gjmp_addr(d);
4331 gsym(e);
4333 skip(')');
4334 block(&a, &b, case_sym, def_sym, case_reg, 0);
4335 gjmp_addr(c);
4336 gsym(a);
4337 gsym_addr(b, c);
4339 if (c99_for_decl) {
4340 /* pop locally defined symbols */
4341 if(is_expr) {
4342 /* XXX: this solution makes only valgrind happy...
4343 triggered by gcc.c-torture/execute/20000917-1.c */
4344 Sym *p;
4345 switch(vtop->type.t & VT_BTYPE) {
4346 case VT_PTR:
4347 case VT_STRUCT:
4348 case VT_ENUM:
4349 case VT_FUNC:
4350 for(p=vtop->type.ref;p;p=p->prev)
4351 if(p->prev==s)
4352 error("unsupported expression type");
4355 sym_pop(&local_stack, s);
4357 } else
4358 if (tok == TOK_DO) {
4359 next();
4360 a = 0;
4361 b = 0;
4362 d = ind;
4363 block(&a, &b, case_sym, def_sym, case_reg, 0);
4364 skip(TOK_WHILE);
4365 skip('(');
4366 gsym(b);
4367 gexpr();
4368 c = gtst(0, 0);
4369 gsym_addr(c, d);
4370 skip(')');
4371 gsym(a);
4372 skip(';');
4373 } else
4374 if (tok == TOK_SWITCH) {
4375 next();
4376 skip('(');
4377 gexpr();
4378 /* XXX: other types than integer */
4379 case_reg = gv(RC_INT);
4380 vpop();
4381 skip(')');
4382 a = 0;
4383 b = gjmp(0); /* jump to first case */
4384 c = 0;
4385 block(&a, csym, &b, &c, case_reg, 0);
4386 /* if no default, jmp after switch */
4387 if (c == 0)
4388 c = ind;
4389 /* default label */
4390 gsym_addr(b, c);
4391 /* break label */
4392 gsym(a);
4393 } else
4394 if (tok == TOK_CASE) {
4395 int v1, v2;
4396 if (!case_sym)
4397 expect("switch");
4398 next();
4399 v1 = expr_const();
4400 v2 = v1;
4401 if (gnu_ext && tok == TOK_DOTS) {
4402 next();
4403 v2 = expr_const();
4404 if (v2 < v1)
4405 warning("empty case range");
4407 /* since a case is like a label, we must skip it with a jmp */
4408 b = gjmp(0);
4409 gsym(*case_sym);
4410 vseti(case_reg, 0);
4411 vpushi(v1);
4412 if (v1 == v2) {
4413 gen_op(TOK_EQ);
4414 *case_sym = gtst(1, 0);
4415 } else {
4416 gen_op(TOK_GE);
4417 *case_sym = gtst(1, 0);
4418 vseti(case_reg, 0);
4419 vpushi(v2);
4420 gen_op(TOK_LE);
4421 *case_sym = gtst(1, *case_sym);
4423 gsym(b);
4424 skip(':');
4425 is_expr = 0;
4426 goto block_after_label;
4427 } else
4428 if (tok == TOK_DEFAULT) {
4429 next();
4430 skip(':');
4431 if (!def_sym)
4432 expect("switch");
4433 if (*def_sym)
4434 error("too many 'default'");
4435 *def_sym = ind;
4436 is_expr = 0;
4437 goto block_after_label;
4438 } else
4439 if (tok == TOK_GOTO) {
4440 next();
4441 if (tok == '*' && gnu_ext) {
4442 /* computed goto */
4443 next();
4444 gexpr();
4445 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4446 expect("pointer");
4447 ggoto();
4448 } else if (tok >= TOK_UIDENT) {
4449 s = label_find(tok);
4450 /* put forward definition if needed */
4451 if (!s) {
4452 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4453 } else {
4454 if (s->r == LABEL_DECLARED)
4455 s->r = LABEL_FORWARD;
4457 /* label already defined */
4458 if (s->r & LABEL_FORWARD)
4459 s->jnext = gjmp(s->jnext);
4460 else
4461 gjmp_addr(s->jnext);
4462 next();
4463 } else {
4464 expect("label identifier");
4466 skip(';');
4467 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4468 asm_instr();
4469 } else {
4470 b = is_label();
4471 if (b) {
4472 /* label case */
4473 s = label_find(b);
4474 if (s) {
4475 if (s->r == LABEL_DEFINED)
4476 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4477 gsym(s->jnext);
4478 s->r = LABEL_DEFINED;
4479 } else {
4480 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4482 s->jnext = ind;
4483 /* we accept this, but it is a mistake */
4484 block_after_label:
4485 if (tok == '}') {
4486 warning("deprecated use of label at end of compound statement");
4487 } else {
4488 if (is_expr)
4489 vpop();
4490 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4492 } else {
4493 /* expression case */
4494 if (tok != ';') {
4495 if (is_expr) {
4496 vpop();
4497 gexpr();
4498 } else {
4499 gexpr();
4500 vpop();
4503 skip(';');
4508 /* t is the array or struct type. c is the array or struct
4509 address. cur_index/cur_field is the pointer to the current
4510 value. 'size_only' is true if only size info is needed (only used
4511 in arrays) */
4512 static void decl_designator(CType *type, Section *sec, unsigned long c,
4513 int *cur_index, Sym **cur_field,
4514 int size_only)
4516 Sym *s, *f;
4517 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4518 CType type1;
4520 notfirst = 0;
4521 elem_size = 0;
4522 nb_elems = 1;
4523 if (gnu_ext && (l = is_label()) != 0)
4524 goto struct_field;
4525 while (tok == '[' || tok == '.') {
4526 if (tok == '[') {
4527 if (!(type->t & VT_ARRAY))
4528 expect("array type");
4529 s = type->ref;
4530 next();
4531 index = expr_const();
4532 if (index < 0 || (s->c >= 0 && index >= s->c))
4533 expect("invalid index");
4534 if (tok == TOK_DOTS && gnu_ext) {
4535 next();
4536 index_last = expr_const();
4537 if (index_last < 0 ||
4538 (s->c >= 0 && index_last >= s->c) ||
4539 index_last < index)
4540 expect("invalid index");
4541 } else {
4542 index_last = index;
4544 skip(']');
4545 if (!notfirst)
4546 *cur_index = index_last;
4547 type = pointed_type(type);
4548 elem_size = type_size(type, &align);
4549 c += index * elem_size;
4550 /* NOTE: we only support ranges for last designator */
4551 nb_elems = index_last - index + 1;
4552 if (nb_elems != 1) {
4553 notfirst = 1;
4554 break;
4556 } else {
4557 next();
4558 l = tok;
4559 next();
4560 struct_field:
4561 if ((type->t & VT_BTYPE) != VT_STRUCT)
4562 expect("struct/union type");
4563 s = type->ref;
4564 l |= SYM_FIELD;
4565 f = s->next;
4566 while (f) {
4567 if (f->v == l)
4568 break;
4569 f = f->next;
4571 if (!f)
4572 expect("field");
4573 if (!notfirst)
4574 *cur_field = f;
4575 /* XXX: fix this mess by using explicit storage field */
4576 type1 = f->type;
4577 type1.t |= (type->t & ~VT_TYPE);
4578 type = &type1;
4579 c += f->c;
4581 notfirst = 1;
4583 if (notfirst) {
4584 if (tok == '=') {
4585 next();
4586 } else {
4587 if (!gnu_ext)
4588 expect("=");
4590 } else {
4591 if (type->t & VT_ARRAY) {
4592 index = *cur_index;
4593 type = pointed_type(type);
4594 c += index * type_size(type, &align);
4595 } else {
4596 f = *cur_field;
4597 if (!f)
4598 error("too many field init");
4599 /* XXX: fix this mess by using explicit storage field */
4600 type1 = f->type;
4601 type1.t |= (type->t & ~VT_TYPE);
4602 type = &type1;
4603 c += f->c;
4606 decl_initializer(type, sec, c, 0, size_only);
4608 /* XXX: make it more general */
4609 if (!size_only && nb_elems > 1) {
4610 unsigned long c_end;
4611 uint8_t *src, *dst;
4612 int i;
4614 if (!sec)
4615 error("range init not supported yet for dynamic storage");
4616 c_end = c + nb_elems * elem_size;
4617 if (c_end > sec->data_allocated)
4618 section_realloc(sec, c_end);
4619 src = sec->data + c;
4620 dst = src;
4621 for(i = 1; i < nb_elems; i++) {
4622 dst += elem_size;
4623 memcpy(dst, src, elem_size);
4628 #define EXPR_VAL 0
4629 #define EXPR_CONST 1
4630 #define EXPR_ANY 2
4632 /* store a value or an expression directly in global data or in local array */
4633 static void init_putv(CType *type, Section *sec, unsigned long c,
4634 int v, int expr_type)
4636 int saved_global_expr, bt, bit_pos, bit_size;
4637 void *ptr;
4638 unsigned long long bit_mask;
4639 CType dtype;
4641 switch(expr_type) {
4642 case EXPR_VAL:
4643 vpushi(v);
4644 break;
4645 case EXPR_CONST:
4646 /* compound literals must be allocated globally in this case */
4647 saved_global_expr = global_expr;
4648 global_expr = 1;
4649 expr_const1();
4650 global_expr = saved_global_expr;
4651 /* NOTE: symbols are accepted */
4652 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4653 error("initializer element is not constant");
4654 break;
4655 case EXPR_ANY:
4656 expr_eq();
4657 break;
4660 dtype = *type;
4661 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4663 if (sec) {
4664 /* XXX: not portable */
4665 /* XXX: generate error if incorrect relocation */
4666 gen_assign_cast(&dtype);
4667 bt = type->t & VT_BTYPE;
4668 /* we'll write at most 12 bytes */
4669 if (c + 12 > sec->data_allocated) {
4670 section_realloc(sec, c + 12);
4672 ptr = sec->data + c;
4673 /* XXX: make code faster ? */
4674 if (!(type->t & VT_BITFIELD)) {
4675 bit_pos = 0;
4676 bit_size = 32;
4677 bit_mask = -1LL;
4678 } else {
4679 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4680 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4681 bit_mask = (1LL << bit_size) - 1;
4683 if ((vtop->r & VT_SYM) &&
4684 (bt == VT_BYTE ||
4685 bt == VT_SHORT ||
4686 bt == VT_DOUBLE ||
4687 bt == VT_LDOUBLE ||
4688 bt == VT_LLONG ||
4689 (bt == VT_INT && bit_size != 32)))
4690 error("initializer element is not computable at load time");
4691 switch(bt) {
4692 case VT_BOOL:
4693 vtop->c.i = (vtop->c.i != 0);
4694 case VT_BYTE:
4695 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4696 break;
4697 case VT_SHORT:
4698 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4699 break;
4700 case VT_DOUBLE:
4701 *(double *)ptr = vtop->c.d;
4702 break;
4703 case VT_LDOUBLE:
4704 *(long double *)ptr = vtop->c.ld;
4705 break;
4706 case VT_LLONG:
4707 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4708 break;
4709 default:
4710 if (vtop->r & VT_SYM) {
4711 greloc(sec, vtop->sym, c, R_DATA_PTR);
4713 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4714 break;
4716 vtop--;
4717 } else {
4718 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4719 vswap();
4720 vstore();
4721 vpop();
4725 /* put zeros for variable based init */
4726 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4728 if (sec) {
4729 /* nothing to do because globals are already set to zero */
4730 } else {
4731 vpush_global_sym(&func_old_type, TOK_memset);
4732 vseti(VT_LOCAL, c);
4733 vpushi(0);
4734 vpushi(size);
4735 gfunc_call(3);
4739 /* 't' contains the type and storage info. 'c' is the offset of the
4740 object in section 'sec'. If 'sec' is NULL, it means stack based
4741 allocation. 'first' is true if array '{' must be read (multi
4742 dimension implicit array init handling). 'size_only' is true if
4743 size only evaluation is wanted (only for arrays). */
4744 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4745 int first, int size_only)
4747 int index, array_length, n, no_oblock, nb, parlevel, i;
4748 int size1, align1, expr_type;
4749 Sym *s, *f;
4750 CType *t1;
4752 if (type->t & VT_ARRAY) {
4753 s = type->ref;
4754 n = s->c;
4755 array_length = 0;
4756 t1 = pointed_type(type);
4757 size1 = type_size(t1, &align1);
4759 no_oblock = 1;
4760 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4761 tok == '{') {
4762 if (tok != '{')
4763 error("character array initializer must be a literal,"
4764 " optionally enclosed in braces");
4765 skip('{');
4766 no_oblock = 0;
4769 /* only parse strings here if correct type (otherwise: handle
4770 them as ((w)char *) expressions */
4771 if ((tok == TOK_LSTR &&
4772 #ifdef TCC_TARGET_PE
4773 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4774 #else
4775 (t1->t & VT_BTYPE) == VT_INT
4776 #endif
4777 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4778 while (tok == TOK_STR || tok == TOK_LSTR) {
4779 int cstr_len, ch;
4780 CString *cstr;
4782 cstr = tokc.cstr;
4783 /* compute maximum number of chars wanted */
4784 if (tok == TOK_STR)
4785 cstr_len = cstr->size;
4786 else
4787 cstr_len = cstr->size / sizeof(nwchar_t);
4788 cstr_len--;
4789 nb = cstr_len;
4790 if (n >= 0 && nb > (n - array_length))
4791 nb = n - array_length;
4792 if (!size_only) {
4793 if (cstr_len > nb)
4794 warning("initializer-string for array is too long");
4795 /* in order to go faster for common case (char
4796 string in global variable, we handle it
4797 specifically */
4798 if (sec && tok == TOK_STR && size1 == 1) {
4799 memcpy(sec->data + c + array_length, cstr->data, nb);
4800 } else {
4801 for(i=0;i<nb;i++) {
4802 if (tok == TOK_STR)
4803 ch = ((unsigned char *)cstr->data)[i];
4804 else
4805 ch = ((nwchar_t *)cstr->data)[i];
4806 init_putv(t1, sec, c + (array_length + i) * size1,
4807 ch, EXPR_VAL);
4811 array_length += nb;
4812 next();
4814 /* only add trailing zero if enough storage (no
4815 warning in this case since it is standard) */
4816 if (n < 0 || array_length < n) {
4817 if (!size_only) {
4818 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4820 array_length++;
4822 } else {
4823 index = 0;
4824 while (tok != '}') {
4825 decl_designator(type, sec, c, &index, NULL, size_only);
4826 if (n >= 0 && index >= n)
4827 error("index too large");
4828 /* must put zero in holes (note that doing it that way
4829 ensures that it even works with designators) */
4830 if (!size_only && array_length < index) {
4831 init_putz(t1, sec, c + array_length * size1,
4832 (index - array_length) * size1);
4834 index++;
4835 if (index > array_length)
4836 array_length = index;
4837 /* special test for multi dimensional arrays (may not
4838 be strictly correct if designators are used at the
4839 same time) */
4840 if (index >= n && no_oblock)
4841 break;
4842 if (tok == '}')
4843 break;
4844 skip(',');
4847 if (!no_oblock)
4848 skip('}');
4849 /* put zeros at the end */
4850 if (!size_only && n >= 0 && array_length < n) {
4851 init_putz(t1, sec, c + array_length * size1,
4852 (n - array_length) * size1);
4854 /* patch type size if needed */
4855 if (n < 0)
4856 s->c = array_length;
4857 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4858 (sec || !first || tok == '{')) {
4859 int par_count;
4861 /* NOTE: the previous test is a specific case for automatic
4862 struct/union init */
4863 /* XXX: union needs only one init */
4865 /* XXX: this test is incorrect for local initializers
4866 beginning with ( without {. It would be much more difficult
4867 to do it correctly (ideally, the expression parser should
4868 be used in all cases) */
4869 par_count = 0;
4870 /* Coo: I think we must not deal '(' */
4871 if (tok == '(') {
4872 AttributeDef ad1;
4873 CType type1;
4874 next();
4875 while (tok == '(') {
4876 par_count++;
4877 next();
4879 if (!parse_btype(&type1, &ad1))
4880 expect("cast");
4881 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4882 #if 0
4883 if (!is_assignable_types(type, &type1))
4884 error("invalid type for cast");
4885 #endif
4886 skip(')');
4888 no_oblock = 1;
4889 if (first || tok == '{') {
4890 skip('{');
4891 no_oblock = 0;
4893 s = type->ref;
4894 f = s->next;
4895 /* Coo: skip empty struct */
4896 while (f->next && (f->type.t&VT_BTYPE)==VT_STRUCT && !f->type.ref->c)
4897 f=f->next;
4898 array_length = 0;
4899 index = 0;
4900 n = s->c;
4901 while (tok != '}') {
4902 int bit_pos;
4903 decl_designator(type, sec, c, NULL, &f, size_only);
4904 index = f->c;
4905 if (!size_only && array_length < index) {
4906 init_putz(type, sec, c + array_length,
4907 index - array_length);
4909 index = index + type_size(&f->type, &align1);
4910 if (index > array_length)
4911 array_length = index;
4913 /* Coo: skip fields from same union */
4914 if (!(f->type.t&VT_BITFIELD))
4915 bit_pos=index*8;
4916 else
4917 bit_pos=f->c*8+((f->type.t>>VT_STRUCT_SHIFT)&0x3f)+((f->type.t>>(VT_STRUCT_SHIFT+6))&0x3f);
4919 f=f->next;
4920 while (f && (((f->type.t&VT_BTYPE)==VT_STRUCT && !f->type.ref->c) ||
4921 f->c*8+((f->type.t&VT_BITFIELD)?((f->type.t>>VT_STRUCT_SHIFT)&0x3f):0)<bit_pos));
4923 if (no_oblock && f == NULL)
4924 break;
4925 if (tok == '}')
4926 break;
4927 /* Coo: skip ')' in front of ',' for initializer */
4928 while (tok==')' && par_count) {
4929 next();
4930 par_count--;
4932 skip(',');
4934 /* put zeros at the end */
4935 if (!size_only && array_length < n) {
4936 init_putz(type, sec, c + array_length,
4937 n - array_length);
4939 if (!no_oblock)
4940 skip('}');
4941 while (par_count) {
4942 skip(')');
4943 par_count--;
4945 } else if (tok == '{') {
4946 next();
4947 decl_initializer(type, sec, c, first, size_only);
4948 skip('}');
4949 } else if (size_only) {
4950 /* just skip expression */
4951 parlevel = 0;
4952 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4953 tok != -1) {
4954 if (tok == '(')
4955 parlevel++;
4956 else if (tok == ')')
4957 parlevel--;
4958 next();
4960 } else {
4961 /* currently, we always use constant expression for globals
4962 (may change for scripting case) */
4963 expr_type = EXPR_CONST;
4964 if (!sec)
4965 expr_type = EXPR_ANY;
4966 init_putv(type, sec, c, 0, expr_type);
4970 /* parse an initializer for type 't' if 'has_init' is non zero, and
4971 allocate space in local or global data space ('r' is either
4972 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4973 variable 'v' of scope 'scope' is declared before initializers are
4974 parsed. If 'v' is zero, then a reference to the new object is put
4975 in the value stack. If 'has_init' is 2, a special parsing is done
4976 to handle string constants. */
4977 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4978 int has_init, int v, int scope)
4980 int size, align, addr, data_offset;
4981 int level;
4982 ParseState saved_parse_state = {0};
4983 TokenString init_str;
4984 Section *sec;
4986 size = type_size(type, &align);
4987 /* If unknown size, we must evaluate it before
4988 evaluating initializers because
4989 initializers can generate global data too
4990 (e.g. string pointers or ISOC99 compound
4991 literals). It also simplifies local
4992 initializers handling */
4993 tok_str_new(&init_str);
4994 if (size < 0) {
4995 if (!has_init)
4996 error("unknown type size");
4997 /* get all init string */
4998 if (has_init == 2) {
4999 /* only get strings */
5000 while (tok == TOK_STR || tok == TOK_LSTR) {
5001 tok_str_add_tok(&init_str);
5002 next();
5004 } else {
5005 level = 0;
5006 while (level > 0 || (tok != ',' && tok != ';')) {
5007 if (tok < 0)
5008 error("unexpected end of file in initializer");
5009 tok_str_add_tok(&init_str);
5010 if (tok == '{')
5011 level++;
5012 else if (tok == '}') {
5013 level--;
5014 if (level <= 0) {
5015 next();
5016 break;
5019 next();
5022 tok_str_add(&init_str, -1);
5023 tok_str_add(&init_str, 0);
5025 /* compute size */
5026 save_parse_state(&saved_parse_state);
5028 macro_ptr = init_str.str;
5029 next();
5030 decl_initializer(type, NULL, 0, 1, 1);
5031 /* prepare second initializer parsing */
5032 macro_ptr = init_str.str;
5033 next();
5035 /* if still unknown size, error */
5036 size = type_size(type, &align);
5037 if (size < 0)
5038 error("unknown type size");
5040 /* take into account specified alignment if bigger */
5041 if (ad->aligned) {
5042 if (ad->aligned > align)
5043 align = ad->aligned;
5044 } else if (ad->packed) {
5045 align = 1;
5047 if ((r & VT_VALMASK) == VT_LOCAL) {
5048 sec = NULL;
5049 #ifdef CONFIG_TCC_BCHECK
5050 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
5051 loc--;
5052 #endif
5053 loc = (loc - size) & -align;
5054 addr = loc;
5055 #ifdef CONFIG_TCC_BCHECK
5056 /* handles bounds */
5057 /* XXX: currently, since we do only one pass, we cannot track
5058 '&' operators, so we add only arrays */
5059 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5060 unsigned long *bounds_ptr;
5061 /* add padding between regions */
5062 loc--;
5063 /* then add local bound info */
5064 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5065 bounds_ptr[0] = addr;
5066 bounds_ptr[1] = size;
5068 #endif
5069 if (v) {
5070 /* local variable */
5071 sym_push(v, type, r, addr);
5072 } else {
5073 /* push local reference */
5074 vset(type, r, addr);
5076 } else {
5077 Sym *sym;
5079 sym = NULL;
5080 if (v && scope == VT_CONST) {
5081 /* see if the symbol was already defined */
5082 sym = sym_find(v);
5083 if (sym) {
5084 if (!is_compatible_types(&sym->type, type))
5085 error("incompatible types for redefinition of '%s'",
5086 get_tok_str(v, NULL));
5087 if (sym->type.t & VT_EXTERN) {
5088 /* if the variable is extern, it was not allocated */
5089 sym->type.t &= ~VT_EXTERN;
5090 /* set array size if it was ommited in extern
5091 declaration */
5092 if ((sym->type.t & VT_ARRAY) &&
5093 sym->type.ref->c < 0 &&
5094 type->ref->c >= 0)
5095 sym->type.ref->c = type->ref->c;
5096 } else {
5097 /* we accept several definitions of the same
5098 global variable. this is tricky, because we
5099 must play with the SHN_COMMON type of the symbol */
5100 /* XXX: should check if the variable was already
5101 initialized. It is incorrect to initialized it
5102 twice */
5103 /* no init data, we won't add more to the symbol */
5104 if (!has_init)
5105 goto no_alloc;
5110 /* allocate symbol in corresponding section */
5111 sec = ad->section;
5112 if (!sec) {
5113 if (has_init)
5114 sec = data_section;
5115 else if (tcc_state->nocommon)
5116 sec = bss_section;
5118 if (sec) {
5119 data_offset = sec->data_offset;
5120 data_offset = (data_offset + align - 1) & -align;
5121 addr = data_offset;
5122 /* very important to increment global pointer at this time
5123 because initializers themselves can create new initializers */
5124 data_offset += size;
5125 #ifdef CONFIG_TCC_BCHECK
5126 /* add padding if bound check */
5127 if (tcc_state->do_bounds_check)
5128 data_offset++;
5129 #endif
5130 sec->data_offset = data_offset;
5131 /* allocate section space to put the data */
5132 if (sec->sh_type != SHT_NOBITS &&
5133 data_offset > sec->data_allocated)
5134 section_realloc(sec, data_offset);
5135 /* align section if needed */
5136 if (align > sec->sh_addralign)
5137 sec->sh_addralign = align;
5138 } else {
5139 addr = 0; /* avoid warning */
5142 if (v) {
5143 if (scope != VT_CONST || !sym) {
5144 sym = sym_push(v, type, r | VT_SYM, 0);
5146 /* update symbol definition */
5147 if (sec) {
5148 put_extern_sym(sym, sec, addr, size);
5149 } else {
5150 ElfW(Sym) *esym;
5151 /* put a common area */
5152 put_extern_sym(sym, NULL, align, size);
5153 /* XXX: find a nicer way */
5154 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5155 esym->st_shndx = SHN_COMMON;
5157 } else {
5158 CValue cval;
5160 /* push global reference */
5161 sym = get_sym_ref(type, sec, addr, size);
5162 cval.ul = 0;
5163 vsetc(type, VT_CONST | VT_SYM, &cval);
5164 vtop->sym = sym;
5166 #ifdef CONFIG_TCC_BCHECK
5167 /* handles bounds now because the symbol must be defined
5168 before for the relocation */
5169 if (tcc_state->do_bounds_check) {
5170 unsigned long *bounds_ptr;
5172 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
5173 /* then add global bound info */
5174 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5175 bounds_ptr[0] = 0; /* relocated */
5176 bounds_ptr[1] = size;
5178 #endif
5180 if (has_init) {
5181 decl_initializer(type, sec, addr, 1, 0);
5182 /* restore parse state if needed */
5183 if (init_str.str) {
5184 tok_str_free(init_str.str);
5185 restore_parse_state(&saved_parse_state);
5188 no_alloc: ;
5191 static void put_func_debug(Sym *sym)
5193 char buf[512];
5195 /* stabs info */
5196 /* XXX: we put here a dummy type */
5197 snprintf(buf, sizeof(buf), "%s:%c1",
5198 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5199 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5200 cur_text_section, sym->c);
5201 /* //gr gdb wants a line at the function */
5202 put_stabn(N_SLINE, 0, file->line_num, 0);
5203 last_ind = 0;
5204 last_line_num = 0;
5207 /* parse an old style function declaration list */
5208 /* XXX: check multiple parameter */
5209 static void func_decl_list(Sym *func_sym)
5211 AttributeDef ad;
5212 int v;
5213 Sym *s;
5214 CType btype, type;
5216 /* parse each declaration */
5217 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
5218 if (!parse_btype(&btype, &ad))
5219 expect("declaration list");
5220 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5221 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5222 tok == ';') {
5223 /* we accept no variable after */
5224 } else {
5225 for(;;) {
5226 type = btype;
5227 type_decl(&type, &ad, &v, TYPE_DIRECT);
5228 /* find parameter in function parameter list */
5229 s = func_sym->next;
5230 while (s != NULL) {
5231 if ((s->v & ~SYM_FIELD) == v)
5232 goto found;
5233 s = s->next;
5235 error("declaration for parameter '%s' but no such parameter",
5236 get_tok_str(v, NULL));
5237 found:
5238 /* check that no storage specifier except 'register' was given */
5239 if (type.t & VT_STORAGE)
5240 error("storage class specified for '%s'", get_tok_str(v, NULL));
5241 convert_parameter_type(&type);
5242 /* we can add the type (NOTE: it could be local to the function) */
5243 s->type = type;
5244 /* accept other parameters */
5245 if (tok == ',')
5246 next();
5247 else
5248 break;
5251 skip(';');
5255 /* parse a function defined by symbol 'sym' and generate its code in
5256 'cur_text_section' */
5257 static void gen_function(Sym *sym)
5259 int saved_nocode_wanted = nocode_wanted;
5260 nocode_wanted = 0;
5261 ind = cur_text_section->data_offset;
5262 /* NOTE: we patch the symbol size later */
5263 put_extern_sym(sym, cur_text_section, ind, 0);
5264 funcname = get_tok_str(sym->v, NULL);
5265 func_ind = ind;
5266 /* put debug symbol */
5267 if (tcc_state->do_debug)
5268 put_func_debug(sym);
5269 /* push a dummy symbol to enable local sym storage */
5270 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5271 gfunc_prolog(&sym->type);
5272 rsym = 0;
5273 block(NULL, NULL, NULL, NULL, 0, 0);
5274 gsym(rsym);
5275 gfunc_epilog();
5276 cur_text_section->data_offset = ind;
5277 label_pop(&global_label_stack, NULL);
5278 sym_pop(&local_stack, NULL); /* reset local stack */
5279 /* end of function */
5280 /* patch symbol size */
5281 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5282 ind - func_ind;
5283 if (tcc_state->do_debug) {
5284 put_stabn(N_FUN, 0, 0, ind - func_ind);
5286 /* It's better to crash than to generate wrong code */
5287 cur_text_section = NULL;
5288 funcname = ""; /* for safety */
5289 func_vt.t = VT_VOID; /* for safety */
5290 ind = 0; /* for safety */
5291 nocode_wanted = saved_nocode_wanted;
5294 ST_FUNC void gen_inline_functions(void)
5296 Sym *sym;
5297 int *str, inline_generated, i;
5298 struct InlineFunc *fn;
5300 /* iterate while inline function are referenced */
5301 for(;;) {
5302 inline_generated = 0;
5303 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5304 fn = tcc_state->inline_fns[i];
5305 sym = fn->sym;
5306 if (sym && sym->c) {
5307 /* the function was used: generate its code and
5308 convert it to a normal function */
5309 str = fn->token_str;
5310 fn->sym = NULL;
5311 if (file)
5312 strcpy(file->filename, fn->filename);
5313 sym->r = VT_SYM | VT_CONST;
5314 sym->type.t &= ~VT_INLINE;
5316 macro_ptr = str;
5317 next();
5318 cur_text_section = text_section;
5319 gen_function(sym);
5320 macro_ptr = NULL; /* fail safe */
5322 inline_generated = 1;
5325 if (!inline_generated)
5326 break;
5328 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
5329 fn = tcc_state->inline_fns[i];
5330 str = fn->token_str;
5331 tok_str_free(str);
5333 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
5336 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
5337 ST_FUNC void decl(int l)
5339 int v, has_init, r;
5340 CType type, btype;
5341 Sym *sym;
5342 AttributeDef ad;
5344 while (1) {
5345 if (!parse_btype(&btype, &ad)) {
5346 /* skip redundant ';' */
5347 /* XXX: find more elegant solution */
5348 if (tok == ';') {
5349 next();
5350 continue;
5352 if (l == VT_CONST &&
5353 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5354 /* global asm block */
5355 asm_global_instr();
5356 continue;
5358 /* special test for old K&R protos without explicit int
5359 type. Only accepted when defining global data */
5360 if (l == VT_LOCAL || tok < TOK_DEFINE)
5361 break;
5362 btype.t = VT_INT;
5364 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5365 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5366 tok == ';') {
5367 /* we accept no variable after */
5368 next();
5369 continue;
5371 while (1) { /* iterate thru each declaration */
5372 type = btype;
5373 type_decl(&type, &ad, &v, TYPE_DIRECT);
5374 #if 0
5376 char buf[500];
5377 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5378 printf("type = '%s'\n", buf);
5380 #endif
5381 if ((type.t & VT_BTYPE) == VT_FUNC) {
5382 /* if old style function prototype, we accept a
5383 declaration list */
5384 sym = type.ref;
5385 if (sym->c == FUNC_OLD)
5386 func_decl_list(sym);
5389 #ifdef TCC_TARGET_PE
5390 if (ad.func_import)
5391 type.t |= VT_IMPORT;
5392 if (ad.func_export)
5393 type.t |= VT_EXPORT;
5394 #endif
5395 if (tok == '{') {
5396 if (l == VT_LOCAL)
5397 error("cannot use local functions");
5398 if ((type.t & VT_BTYPE) != VT_FUNC)
5399 expect("function definition");
5401 /* reject abstract declarators in function definition */
5402 sym = type.ref;
5403 while ((sym = sym->next) != NULL)
5404 if (!(sym->v & ~SYM_FIELD))
5405 expect("identifier");
5407 /* XXX: cannot do better now: convert extern line to static inline */
5408 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5409 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5411 sym = sym_find(v);
5412 if (sym) {
5413 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5414 goto func_error1;
5416 r = sym->type.ref->r;
5417 /* use func_call from prototype if not defined */
5418 if (FUNC_CALL(r) != FUNC_CDECL
5419 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5420 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5422 /* use export from prototype */
5423 if (FUNC_EXPORT(r))
5424 FUNC_EXPORT(type.ref->r) = 1;
5426 /* use static from prototype */
5427 if (sym->type.t & VT_STATIC)
5428 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5430 if (!is_compatible_types(&sym->type, &type)) {
5431 func_error1:
5432 error("incompatible types for redefinition of '%s'",
5433 get_tok_str(v, NULL));
5435 /* if symbol is already defined, then put complete type */
5436 sym->type = type;
5437 } else {
5438 /* put function symbol */
5439 sym = global_identifier_push(v, type.t, 0);
5440 sym->type.ref = type.ref;
5443 /* static inline functions are just recorded as a kind
5444 of macro. Their code will be emitted at the end of
5445 the compilation unit only if they are used */
5446 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5447 (VT_INLINE | VT_STATIC)) {
5448 TokenString func_str;
5449 int block_level;
5450 struct InlineFunc *fn;
5451 const char *filename;
5453 tok_str_new(&func_str);
5455 block_level = 0;
5456 for(;;) {
5457 int t;
5458 if (tok == TOK_EOF)
5459 error("unexpected end of file");
5460 tok_str_add_tok(&func_str);
5461 t = tok;
5462 next();
5463 if (t == '{') {
5464 block_level++;
5465 } else if (t == '}') {
5466 block_level--;
5467 if (block_level == 0)
5468 break;
5471 tok_str_add(&func_str, -1);
5472 tok_str_add(&func_str, 0);
5473 filename = file ? file->filename : "";
5474 fn = tcc_malloc(sizeof *fn + strlen(filename));
5475 strcpy(fn->filename, filename);
5476 fn->sym = sym;
5477 fn->token_str = func_str.str;
5478 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5480 } else {
5481 /* compute text section */
5482 cur_text_section = ad.section;
5483 if (!cur_text_section)
5484 cur_text_section = text_section;
5485 sym->r = VT_SYM | VT_CONST;
5486 gen_function(sym);
5488 break;
5489 } else {
5490 if (btype.t & VT_TYPEDEF) {
5491 /* save typedefed type */
5492 /* XXX: test storage specifiers ? */
5493 sym = sym_push(v, &type, INT_ATTR(&ad), 0);
5494 sym->type.t |= VT_TYPEDEF;
5495 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5496 Sym *fn;
5497 /* external function definition */
5498 /* specific case for func_call attribute */
5499 type.ref->r = INT_ATTR(&ad);
5500 fn = external_sym(v, &type, 0);
5502 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
5503 char target[256];
5505 *target = 0;
5506 next();
5507 skip('(');
5508 /* Part 1: __USER_LABEL_PREFIX__ (user defined) */
5509 if (tok == TOK_STR)
5510 pstrcat(target, sizeof(target), tokc.cstr->data);
5511 else
5512 pstrcat(target, sizeof(target), get_tok_str(tok, NULL));
5514 next();
5515 /* Part 2: api name */
5516 if (tok == TOK_STR)
5517 pstrcat(target, sizeof(target), tokc.cstr->data);
5518 else
5519 pstrcat(target, sizeof(target), get_tok_str(tok, NULL));
5521 next();
5522 skip(')');
5523 if (tcc_state->warn_unsupported)
5524 warning("ignoring redirection from %s to %s\n", get_tok_str(v, NULL), target);
5526 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
5527 parse_attribute((AttributeDef *) &fn->type.ref->r);
5529 } else {
5530 /* not lvalue if array */
5531 r = 0;
5532 if (!(type.t & VT_ARRAY))
5533 r |= lvalue_type(type.t);
5534 has_init = (tok == '=');
5535 if ((btype.t & VT_EXTERN) ||
5536 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5537 !has_init && l == VT_CONST && type.ref->c < 0)) {
5538 /* external variable */
5539 /* NOTE: as GCC, uninitialized global static
5540 arrays of null size are considered as
5541 extern */
5542 external_sym(v, &type, r);
5543 } else {
5544 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5545 if (type.t & VT_STATIC)
5546 r |= VT_CONST;
5547 else
5548 r |= l;
5549 if (has_init)
5550 next();
5551 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
5554 if (tok != ',') {
5555 skip(';');
5556 break;
5558 next();