bitfields: promote to signed int
[tinycc.git] / tccgen.c
blob00f6aa353567eab08aae428fce237188ab1bd353
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 Sym *sym_free_first;
34 ST_DATA void **sym_pools;
35 ST_DATA int nb_sym_pools;
37 ST_DATA Sym *global_stack;
38 ST_DATA Sym *local_stack;
39 ST_DATA Sym *define_stack;
40 ST_DATA Sym *global_label_stack;
41 ST_DATA Sym *local_label_stack;
42 static int local_scope;
43 static int in_sizeof;
44 static int section_sym;
46 ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */
47 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
48 ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
50 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop, *pvtop;
52 ST_DATA int const_wanted; /* true if constant wanted */
53 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
54 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
55 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
56 ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
57 ST_DATA int func_vc;
58 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
59 ST_DATA const char *funcname;
61 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
63 ST_DATA struct switch_t {
64 struct case_t {
65 int64_t v1, v2;
66 int sym;
67 } **p; int n; /* list of case ranges */
68 int def_sym; /* default symbol */
69 } *cur_switch; /* current switch */
71 /* ------------------------------------------------------------------------- */
73 static void gen_cast(CType *type);
74 static inline CType *pointed_type(CType *type);
75 static int is_compatible_types(CType *type1, CType *type2);
76 static int parse_btype(CType *type, AttributeDef *ad);
77 static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
78 static void parse_expr_type(CType *type);
79 static void init_putv(CType *type, Section *sec, unsigned long c);
80 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
81 static void block(int *bsym, int *csym, int is_expr);
82 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
83 static int decl0(int l, int is_for_loop_init, Sym *);
84 static void expr_eq(void);
85 static void vla_runtime_type_size(CType *type, int *a);
86 static void vla_sp_restore(void);
87 static void vla_sp_restore_root(void);
88 static int is_compatible_parameter_types(CType *type1, CType *type2);
89 static inline int64_t expr_const64(void);
90 ST_FUNC void vpush64(int ty, unsigned long long v);
91 ST_FUNC void vpush(CType *type);
92 ST_FUNC int gvtst(int inv, int t);
93 ST_FUNC int is_btype_size(int bt);
94 static void gen_inline_functions(TCCState *s);
96 ST_INLN int is_float(int t)
98 int bt;
99 bt = t & VT_BTYPE;
100 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
103 /* we use our own 'finite' function to avoid potential problems with
104 non standard math libs */
105 /* XXX: endianness dependent */
106 ST_FUNC int ieee_finite(double d)
108 int p[4];
109 memcpy(p, &d, sizeof(double));
110 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
113 ST_FUNC void test_lvalue(void)
115 if (!(vtop->r & VT_LVAL))
116 expect("lvalue");
119 ST_FUNC void check_vstack(void)
121 if (pvtop != vtop)
122 tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
125 /* ------------------------------------------------------------------------- */
126 /* vstack debugging aid */
128 #if 0
129 void pv (const char *lbl, int a, int b)
131 int i;
132 for (i = a; i < a + b; ++i) {
133 SValue *p = &vtop[-i];
134 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
135 lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
138 #endif
140 /* ------------------------------------------------------------------------- */
141 /* start of translation unit info */
142 ST_FUNC void tcc_debug_start(TCCState *s1)
144 if (s1->do_debug) {
145 char buf[512];
147 /* file info: full path + filename */
148 section_sym = put_elf_sym(symtab_section, 0, 0,
149 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
150 text_section->sh_num, NULL);
151 getcwd(buf, sizeof(buf));
152 #ifdef _WIN32
153 normalize_slashes(buf);
154 #endif
155 pstrcat(buf, sizeof(buf), "/");
156 put_stabs_r(buf, N_SO, 0, 0,
157 text_section->data_offset, text_section, section_sym);
158 put_stabs_r(file->filename, N_SO, 0, 0,
159 text_section->data_offset, text_section, section_sym);
160 last_ind = 0;
161 last_line_num = 0;
164 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
165 symbols can be safely used */
166 put_elf_sym(symtab_section, 0, 0,
167 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
168 SHN_ABS, file->filename);
171 /* put end of translation unit info */
172 ST_FUNC void tcc_debug_end(TCCState *s1)
174 if (!s1->do_debug)
175 return;
176 put_stabs_r(NULL, N_SO, 0, 0,
177 text_section->data_offset, text_section, section_sym);
181 /* generate line number info */
182 ST_FUNC void tcc_debug_line(TCCState *s1)
184 if (!s1->do_debug)
185 return;
186 if ((last_line_num != file->line_num || last_ind != ind)) {
187 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
188 last_ind = ind;
189 last_line_num = file->line_num;
193 /* put function symbol */
194 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
196 char buf[512];
198 if (!s1->do_debug)
199 return;
201 /* stabs info */
202 /* XXX: we put here a dummy type */
203 snprintf(buf, sizeof(buf), "%s:%c1",
204 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
205 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
206 cur_text_section, sym->c);
207 /* //gr gdb wants a line at the function */
208 put_stabn(N_SLINE, 0, file->line_num, 0);
210 last_ind = 0;
211 last_line_num = 0;
214 /* put function size */
215 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
217 if (!s1->do_debug)
218 return;
219 put_stabn(N_FUN, 0, 0, size);
222 /* ------------------------------------------------------------------------- */
223 ST_FUNC void tccgen_start(TCCState *s1)
225 cur_text_section = NULL;
226 funcname = "";
227 anon_sym = SYM_FIRST_ANOM;
228 section_sym = 0;
229 const_wanted = 0;
230 nocode_wanted = 1;
232 /* define some often used types */
233 int_type.t = VT_INT;
234 char_pointer_type.t = VT_BYTE;
235 mk_pointer(&char_pointer_type);
236 #if PTR_SIZE == 4
237 size_type.t = VT_INT;
238 #else
239 size_type.t = VT_LLONG;
240 #endif
241 func_old_type.t = VT_FUNC;
242 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
244 tcc_debug_start(s1);
246 #ifdef TCC_TARGET_ARM
247 arm_init(s1);
248 #endif
251 ST_FUNC void tccgen_end(TCCState *s1)
253 gen_inline_functions(s1);
254 check_vstack();
255 /* end of translation unit info */
256 tcc_debug_end(s1);
259 /* ------------------------------------------------------------------------- */
260 /* apply storage attributes to Elf symbol */
262 static void update_storage(Sym *sym)
264 int t;
265 ElfW(Sym) *esym;
267 if (0 == sym->c)
268 return;
270 t = sym->type.t;
271 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
273 if (t & VT_VIS_MASK)
274 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
275 | ((t & VT_VIS_MASK) >> VT_VIS_SHIFT);
277 if (t & VT_WEAK)
278 esym->st_info = ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(esym->st_info));
280 #ifdef TCC_TARGET_PE
281 if (t & VT_EXPORT)
282 esym->st_other |= ST_PE_EXPORT;
283 #endif
286 /* ------------------------------------------------------------------------- */
287 /* update sym->c so that it points to an external symbol in section
288 'section' with value 'value' */
290 ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
291 addr_t value, unsigned long size,
292 int can_add_underscore)
294 int sym_type, sym_bind, sh_num, info, other, t;
295 ElfW(Sym) *esym;
296 const char *name;
297 char buf1[256];
298 #ifdef CONFIG_TCC_BCHECK
299 char buf[32];
300 #endif
302 if (section == NULL)
303 sh_num = SHN_UNDEF;
304 else if (section == SECTION_ABS)
305 sh_num = SHN_ABS;
306 else
307 sh_num = section->sh_num;
309 if (!sym->c) {
310 name = get_tok_str(sym->v, NULL);
311 #ifdef CONFIG_TCC_BCHECK
312 if (tcc_state->do_bounds_check) {
313 /* XXX: avoid doing that for statics ? */
314 /* if bound checking is activated, we change some function
315 names by adding the "__bound" prefix */
316 switch(sym->v) {
317 #ifdef TCC_TARGET_PE
318 /* XXX: we rely only on malloc hooks */
319 case TOK_malloc:
320 case TOK_free:
321 case TOK_realloc:
322 case TOK_memalign:
323 case TOK_calloc:
324 #endif
325 case TOK_memcpy:
326 case TOK_memmove:
327 case TOK_memset:
328 case TOK_strlen:
329 case TOK_strcpy:
330 case TOK_alloca:
331 strcpy(buf, "__bound_");
332 strcat(buf, name);
333 name = buf;
334 break;
337 #endif
338 t = sym->type.t;
339 if ((t & VT_BTYPE) == VT_FUNC) {
340 sym_type = STT_FUNC;
341 } else if ((t & VT_BTYPE) == VT_VOID) {
342 sym_type = STT_NOTYPE;
343 } else {
344 sym_type = STT_OBJECT;
346 if (t & VT_STATIC)
347 sym_bind = STB_LOCAL;
348 else
349 sym_bind = STB_GLOBAL;
350 other = 0;
351 #ifdef TCC_TARGET_PE
352 if (sym_type == STT_FUNC && sym->type.ref) {
353 Sym *ref = sym->type.ref;
354 if (ref->a.func_call == FUNC_STDCALL && can_add_underscore) {
355 sprintf(buf1, "_%s@%d", name, ref->a.func_args * PTR_SIZE);
356 name = buf1;
357 other |= ST_PE_STDCALL;
358 can_add_underscore = 0;
361 if (t & VT_IMPORT)
362 other |= ST_PE_IMPORT;
363 #endif
364 if (tcc_state->leading_underscore && can_add_underscore) {
365 buf1[0] = '_';
366 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
367 name = buf1;
369 if (sym->asm_label)
370 name = get_tok_str(sym->asm_label, NULL);
371 info = ELFW(ST_INFO)(sym_bind, sym_type);
372 sym->c = set_elf_sym(symtab_section, value, size, info, other, sh_num, name);
373 } else {
374 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
375 esym->st_value = value;
376 esym->st_size = size;
377 esym->st_shndx = sh_num;
379 update_storage(sym);
382 ST_FUNC void put_extern_sym(Sym *sym, Section *section,
383 addr_t value, unsigned long size)
385 put_extern_sym2(sym, section, value, size, 1);
388 /* add a new relocation entry to symbol 'sym' in section 's' */
389 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
390 addr_t addend)
392 int c = 0;
394 if (nocode_wanted && s == cur_text_section)
395 return;
397 if (sym) {
398 if (0 == sym->c)
399 put_extern_sym(sym, NULL, 0, 0);
400 c = sym->c;
403 /* now we can add ELF relocation info */
404 put_elf_reloca(symtab_section, s, offset, type, c, addend);
407 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
409 greloca(s, sym, offset, type, 0);
412 /* ------------------------------------------------------------------------- */
413 /* symbol allocator */
414 static Sym *__sym_malloc(void)
416 Sym *sym_pool, *sym, *last_sym;
417 int i;
419 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
420 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
422 last_sym = sym_free_first;
423 sym = sym_pool;
424 for(i = 0; i < SYM_POOL_NB; i++) {
425 sym->next = last_sym;
426 last_sym = sym;
427 sym++;
429 sym_free_first = last_sym;
430 return last_sym;
433 static inline Sym *sym_malloc(void)
435 Sym *sym;
436 #ifndef SYM_DEBUG
437 sym = sym_free_first;
438 if (!sym)
439 sym = __sym_malloc();
440 sym_free_first = sym->next;
441 return sym;
442 #else
443 sym = tcc_malloc(sizeof(Sym));
444 return sym;
445 #endif
448 ST_INLN void sym_free(Sym *sym)
450 #ifndef SYM_DEBUG
451 sym->next = sym_free_first;
452 sym_free_first = sym;
453 #else
454 tcc_free(sym);
455 #endif
458 /* push, without hashing */
459 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
461 Sym *s;
463 s = sym_malloc();
464 s->scope = 0;
465 s->v = v;
466 s->type.t = t;
467 s->type.ref = NULL;
468 #ifdef _WIN64
469 s->d = NULL;
470 #endif
471 s->c = c;
472 s->next = NULL;
473 /* add in stack */
474 s->prev = *ps;
475 *ps = s;
476 return s;
479 /* find a symbol and return its associated structure. 's' is the top
480 of the symbol stack */
481 ST_FUNC Sym *sym_find2(Sym *s, int v)
483 while (s) {
484 if (s->v == v)
485 return s;
486 else if (s->v == -1)
487 return NULL;
488 s = s->prev;
490 return NULL;
493 /* structure lookup */
494 ST_INLN Sym *struct_find(int v)
496 v -= TOK_IDENT;
497 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
498 return NULL;
499 return table_ident[v]->sym_struct;
502 /* find an identifier */
503 ST_INLN Sym *sym_find(int v)
505 v -= TOK_IDENT;
506 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
507 return NULL;
508 return table_ident[v]->sym_identifier;
511 /* push a given symbol on the symbol stack */
512 ST_FUNC Sym *sym_push(int v, CType *type, int r, long c)
514 Sym *s, **ps;
515 TokenSym *ts;
517 if (local_stack)
518 ps = &local_stack;
519 else
520 ps = &global_stack;
521 s = sym_push2(ps, v, type->t, c);
522 s->type.ref = type->ref;
523 s->r = r;
524 /* don't record fields or anonymous symbols */
525 /* XXX: simplify */
526 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
527 /* record symbol in token array */
528 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
529 if (v & SYM_STRUCT)
530 ps = &ts->sym_struct;
531 else
532 ps = &ts->sym_identifier;
533 s->prev_tok = *ps;
534 *ps = s;
535 s->scope = local_scope;
536 if (s->prev_tok && s->prev_tok->scope == s->scope)
537 tcc_error("redeclaration of '%s'",
538 get_tok_str(v & ~SYM_STRUCT, NULL));
540 return s;
543 /* push a global identifier */
544 ST_FUNC Sym *global_identifier_push(int v, int t, long c)
546 Sym *s, **ps;
547 s = sym_push2(&global_stack, v, t, c);
548 /* don't record anonymous symbol */
549 if (v < SYM_FIRST_ANOM) {
550 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
551 /* modify the top most local identifier, so that
552 sym_identifier will point to 's' when popped */
553 while (*ps != NULL)
554 ps = &(*ps)->prev_tok;
555 s->prev_tok = NULL;
556 *ps = s;
558 return s;
561 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
562 pop them yet from the list, but do remove them from the token array. */
563 ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep)
565 Sym *s, *ss, **ps;
566 TokenSym *ts;
567 int v;
569 s = *ptop;
570 while(s != b) {
571 ss = s->prev;
572 v = s->v;
573 /* remove symbol in token array */
574 /* XXX: simplify */
575 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
576 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
577 if (v & SYM_STRUCT)
578 ps = &ts->sym_struct;
579 else
580 ps = &ts->sym_identifier;
581 *ps = s->prev_tok;
583 if (!keep)
584 sym_free(s);
585 s = ss;
587 if (!keep)
588 *ptop = b;
591 /* ------------------------------------------------------------------------- */
593 static void vsetc(CType *type, int r, CValue *vc)
595 int v;
597 if (vtop >= vstack + (VSTACK_SIZE - 1))
598 tcc_error("memory full (vstack)");
599 /* cannot let cpu flags if other instruction are generated. Also
600 avoid leaving VT_JMP anywhere except on the top of the stack
601 because it would complicate the code generator.
603 Don't do this when nocode_wanted. vtop might come from
604 !nocode_wanted regions (see 88_codeopt.c) and transforming
605 it to a register without actually generating code is wrong
606 as their value might still be used for real. All values
607 we push under nocode_wanted will eventually be popped
608 again, so that the VT_CMP/VT_JMP value will be in vtop
609 when code is unsuppressed again.
611 Same logic below in vswap(); */
612 if (vtop >= vstack && !nocode_wanted) {
613 v = vtop->r & VT_VALMASK;
614 if (v == VT_CMP || (v & ~1) == VT_JMP)
615 gv(RC_INT);
618 vtop++;
619 vtop->type = *type;
620 vtop->r = r;
621 vtop->r2 = VT_CONST;
622 vtop->c = *vc;
623 vtop->sym = NULL;
626 ST_FUNC void vswap(void)
628 SValue tmp;
629 /* cannot vswap cpu flags. See comment at vsetc() above */
630 if (vtop >= vstack && !nocode_wanted) {
631 int v = vtop->r & VT_VALMASK;
632 if (v == VT_CMP || (v & ~1) == VT_JMP)
633 gv(RC_INT);
635 tmp = vtop[0];
636 vtop[0] = vtop[-1];
637 vtop[-1] = tmp;
640 /* pop stack value */
641 ST_FUNC void vpop(void)
643 int v;
644 v = vtop->r & VT_VALMASK;
645 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
646 /* for x86, we need to pop the FP stack */
647 if (v == TREG_ST0) {
648 o(0xd8dd); /* fstp %st(0) */
649 } else
650 #endif
651 if (v == VT_JMP || v == VT_JMPI) {
652 /* need to put correct jump if && or || without test */
653 gsym(vtop->c.i);
655 vtop--;
658 /* push constant of type "type" with useless value */
659 ST_FUNC void vpush(CType *type)
661 CValue cval;
662 vsetc(type, VT_CONST, &cval);
665 /* push integer constant */
666 ST_FUNC void vpushi(int v)
668 CValue cval;
669 cval.i = v;
670 vsetc(&int_type, VT_CONST, &cval);
673 /* push a pointer sized constant */
674 static void vpushs(addr_t v)
676 CValue cval;
677 cval.i = v;
678 vsetc(&size_type, VT_CONST, &cval);
681 /* push arbitrary 64bit constant */
682 ST_FUNC void vpush64(int ty, unsigned long long v)
684 CValue cval;
685 CType ctype;
686 ctype.t = ty;
687 ctype.ref = NULL;
688 cval.i = v;
689 vsetc(&ctype, VT_CONST, &cval);
692 /* push long long constant */
693 static inline void vpushll(long long v)
695 vpush64(VT_LLONG, v);
698 ST_FUNC void vset(CType *type, int r, long v)
700 CValue cval;
702 cval.i = v;
703 vsetc(type, r, &cval);
706 static void vseti(int r, int v)
708 CType type;
709 type.t = VT_INT;
710 type.ref = 0;
711 vset(&type, r, v);
714 ST_FUNC void vpushv(SValue *v)
716 if (vtop >= vstack + (VSTACK_SIZE - 1))
717 tcc_error("memory full (vstack)");
718 vtop++;
719 *vtop = *v;
722 static void vdup(void)
724 vpushv(vtop);
727 /* rotate n first stack elements to the bottom
728 I1 ... In -> I2 ... In I1 [top is right]
730 ST_FUNC void vrotb(int n)
732 int i;
733 SValue tmp;
735 tmp = vtop[-n + 1];
736 for(i=-n+1;i!=0;i++)
737 vtop[i] = vtop[i+1];
738 vtop[0] = tmp;
741 /* rotate the n elements before entry e towards the top
742 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
744 ST_FUNC void vrote(SValue *e, int n)
746 int i;
747 SValue tmp;
749 tmp = *e;
750 for(i = 0;i < n - 1; i++)
751 e[-i] = e[-i - 1];
752 e[-n + 1] = tmp;
755 /* rotate n first stack elements to the top
756 I1 ... In -> In I1 ... I(n-1) [top is right]
758 ST_FUNC void vrott(int n)
760 vrote(vtop, n);
763 /* push a symbol value of TYPE */
764 static inline void vpushsym(CType *type, Sym *sym)
766 CValue cval;
767 cval.i = 0;
768 vsetc(type, VT_CONST | VT_SYM, &cval);
769 vtop->sym = sym;
772 /* Return a static symbol pointing to a section */
773 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
775 int v;
776 Sym *sym;
778 v = anon_sym++;
779 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
780 sym->type.ref = type->ref;
781 sym->r = VT_CONST | VT_SYM;
782 put_extern_sym(sym, sec, offset, size);
783 return sym;
786 /* push a reference to a section offset by adding a dummy symbol */
787 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
789 vpushsym(type, get_sym_ref(type, sec, offset, size));
792 /* define a new external reference to a symbol 'v' of type 'u' */
793 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
795 Sym *s;
797 s = sym_find(v);
798 if (!s) {
799 /* push forward reference */
800 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
801 s->type.ref = type->ref;
802 s->r = r | VT_CONST | VT_SYM;
804 return s;
807 /* Merge some storage attributes. */
808 static void patch_storage(Sym *sym, CType *type)
810 int t;
811 if (!is_compatible_types(&sym->type, type))
812 tcc_error("incompatible types for redefinition of '%s'",
813 get_tok_str(sym->v, NULL));
814 t = type->t;
815 #ifdef TCC_TARGET_PE
816 if ((sym->type.t ^ t) & VT_IMPORT)
817 tcc_error("incompatible dll linkage for redefinition of '%s'",
818 get_tok_str(sym->v, NULL));
819 #endif
820 sym->type.t |= t & (VT_EXPORT|VT_WEAK);
821 if (t & VT_VIS_MASK) {
822 int vis = sym->type.t & VT_VIS_MASK;
823 int vis2 = t & VT_VIS_MASK;
824 if (vis == (STV_DEFAULT << VT_VIS_SHIFT))
825 vis = vis2;
826 else if (vis2 != (STV_DEFAULT << VT_VIS_SHIFT))
827 vis = (vis < vis2) ? vis : vis2;
828 sym->type.t = (sym->type.t & ~VT_VIS_MASK) | vis;
832 /* define a new external reference to a symbol 'v' */
833 static Sym *external_sym(int v, CType *type, int r)
835 Sym *s;
836 s = sym_find(v);
837 if (!s) {
838 /* push forward reference */
839 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
840 s->type.t |= VT_EXTERN;
841 } else {
842 if (s->type.ref == func_old_type.ref) {
843 s->type.ref = type->ref;
844 s->r = r | VT_CONST | VT_SYM;
845 s->type.t |= VT_EXTERN;
847 patch_storage(s, type);
848 update_storage(s);
850 return s;
853 /* push a reference to global symbol v */
854 ST_FUNC void vpush_global_sym(CType *type, int v)
856 vpushsym(type, external_global_sym(v, type, 0));
859 /* save registers up to (vtop - n) stack entry */
860 ST_FUNC void save_regs(int n)
862 SValue *p, *p1;
863 for(p = vstack, p1 = vtop - n; p <= p1; p++)
864 save_reg(p->r);
867 /* save r to the memory stack, and mark it as being free */
868 ST_FUNC void save_reg(int r)
870 save_reg_upstack(r, 0);
873 /* save r to the memory stack, and mark it as being free,
874 if seen up to (vtop - n) stack entry */
875 ST_FUNC void save_reg_upstack(int r, int n)
877 int l, saved, size, align;
878 SValue *p, *p1, sv;
879 CType *type;
881 if ((r &= VT_VALMASK) >= VT_CONST)
882 return;
883 if (nocode_wanted)
884 return;
886 /* modify all stack values */
887 saved = 0;
888 l = 0;
889 for(p = vstack, p1 = vtop - n; p <= p1; p++) {
890 if ((p->r & VT_VALMASK) == r ||
891 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
892 /* must save value on stack if not already done */
893 if (!saved) {
894 /* NOTE: must reload 'r' because r might be equal to r2 */
895 r = p->r & VT_VALMASK;
896 /* store register in the stack */
897 type = &p->type;
898 if ((p->r & VT_LVAL) ||
899 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
900 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
901 type = &char_pointer_type;
902 #else
903 type = &int_type;
904 #endif
905 size = type_size(type, &align);
906 loc = (loc - size) & -align;
907 sv.type.t = type->t;
908 sv.r = VT_LOCAL | VT_LVAL;
909 sv.c.i = loc;
910 store(r, &sv);
911 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
912 /* x86 specific: need to pop fp register ST0 if saved */
913 if (r == TREG_ST0) {
914 o(0xd8dd); /* fstp %st(0) */
916 #endif
917 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
918 /* special long long case */
919 if ((type->t & VT_BTYPE) == VT_LLONG) {
920 sv.c.i += 4;
921 store(p->r2, &sv);
923 #endif
924 l = loc;
925 saved = 1;
927 /* mark that stack entry as being saved on the stack */
928 if (p->r & VT_LVAL) {
929 /* also clear the bounded flag because the
930 relocation address of the function was stored in
931 p->c.i */
932 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
933 } else {
934 p->r = lvalue_type(p->type.t) | VT_LOCAL;
936 p->r2 = VT_CONST;
937 p->c.i = l;
942 #ifdef TCC_TARGET_ARM
943 /* find a register of class 'rc2' with at most one reference on stack.
944 * If none, call get_reg(rc) */
945 ST_FUNC int get_reg_ex(int rc, int rc2)
947 int r;
948 SValue *p;
950 for(r=0;r<NB_REGS;r++) {
951 if (reg_classes[r] & rc2) {
952 int n;
953 n=0;
954 for(p = vstack; p <= vtop; p++) {
955 if ((p->r & VT_VALMASK) == r ||
956 (p->r2 & VT_VALMASK) == r)
957 n++;
959 if (n <= 1)
960 return r;
963 return get_reg(rc);
965 #endif
967 /* find a free register of class 'rc'. If none, save one register */
968 ST_FUNC int get_reg(int rc)
970 int r;
971 SValue *p;
973 /* find a free register */
974 for(r=0;r<NB_REGS;r++) {
975 if (reg_classes[r] & rc) {
976 if (nocode_wanted)
977 return r;
978 for(p=vstack;p<=vtop;p++) {
979 if ((p->r & VT_VALMASK) == r ||
980 (p->r2 & VT_VALMASK) == r)
981 goto notfound;
983 return r;
985 notfound: ;
988 /* no register left : free the first one on the stack (VERY
989 IMPORTANT to start from the bottom to ensure that we don't
990 spill registers used in gen_opi()) */
991 for(p=vstack;p<=vtop;p++) {
992 /* look at second register (if long long) */
993 r = p->r2 & VT_VALMASK;
994 if (r < VT_CONST && (reg_classes[r] & rc))
995 goto save_found;
996 r = p->r & VT_VALMASK;
997 if (r < VT_CONST && (reg_classes[r] & rc)) {
998 save_found:
999 save_reg(r);
1000 return r;
1003 /* Should never comes here */
1004 return -1;
1007 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1008 if needed */
1009 static void move_reg(int r, int s, int t)
1011 SValue sv;
1013 if (r != s) {
1014 save_reg(r);
1015 sv.type.t = t;
1016 sv.type.ref = NULL;
1017 sv.r = s;
1018 sv.c.i = 0;
1019 load(r, &sv);
1023 /* get address of vtop (vtop MUST BE an lvalue) */
1024 ST_FUNC void gaddrof(void)
1026 vtop->r &= ~VT_LVAL;
1027 /* tricky: if saved lvalue, then we can go back to lvalue */
1028 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
1029 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
1034 #ifdef CONFIG_TCC_BCHECK
1035 /* generate lvalue bound code */
1036 static void gbound(void)
1038 int lval_type;
1039 CType type1;
1041 vtop->r &= ~VT_MUSTBOUND;
1042 /* if lvalue, then use checking code before dereferencing */
1043 if (vtop->r & VT_LVAL) {
1044 /* if not VT_BOUNDED value, then make one */
1045 if (!(vtop->r & VT_BOUNDED)) {
1046 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
1047 /* must save type because we must set it to int to get pointer */
1048 type1 = vtop->type;
1049 vtop->type.t = VT_PTR;
1050 gaddrof();
1051 vpushi(0);
1052 gen_bounded_ptr_add();
1053 vtop->r |= lval_type;
1054 vtop->type = type1;
1056 /* then check for dereferencing */
1057 gen_bounded_ptr_deref();
1060 #endif
1062 /* store vtop a register belonging to class 'rc'. lvalues are
1063 converted to values. Cannot be used if cannot be converted to
1064 register value (such as structures). */
1065 ST_FUNC int gv(int rc)
1067 int r, bit_pos, bit_size, size, align;
1068 int rc2;
1070 /* NOTE: get_reg can modify vstack[] */
1071 if (vtop->type.t & VT_BITFIELD) {
1072 CType type;
1073 int bits = 32;
1074 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
1075 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
1076 /* remove bit field info to avoid loops */
1077 vtop->type.t &= ~VT_BITFIELD & ((1 << VT_STRUCT_SHIFT) - 1);
1078 /* cast to int to propagate signedness in following ops */
1079 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1080 type.t = VT_LLONG;
1081 bits = 64;
1082 } else
1083 type.t = VT_INT;
1084 if((vtop->type.t & VT_UNSIGNED) ||
1085 (vtop->type.t & VT_BTYPE) == VT_BOOL ||
1086 (((vtop->type.t & VT_BTYPE) == VT_ENUM) &&
1087 vtop->type.ref->a.unsigned_enum))
1088 type.t |= VT_UNSIGNED;
1089 gen_cast(&type);
1090 /* generate shifts */
1091 vpushi(bits - (bit_pos + bit_size));
1092 gen_op(TOK_SHL);
1093 vpushi(bits - bit_size);
1094 /* NOTE: transformed to SHR if unsigned */
1095 gen_op(TOK_SAR);
1096 r = gv(rc);
1097 } else {
1098 if (is_float(vtop->type.t) &&
1099 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1100 unsigned long offset;
1101 /* CPUs usually cannot use float constants, so we store them
1102 generically in data segment */
1103 size = type_size(&vtop->type, &align);
1104 offset = section_add(data_section, size, align);
1105 vpush_ref(&vtop->type, data_section, offset, size);
1106 vswap();
1107 init_putv(&vtop->type, data_section, offset);
1108 vtop->r |= VT_LVAL;
1110 #ifdef CONFIG_TCC_BCHECK
1111 if (vtop->r & VT_MUSTBOUND)
1112 gbound();
1113 #endif
1115 r = vtop->r & VT_VALMASK;
1116 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
1117 #ifndef TCC_TARGET_ARM64
1118 if (rc == RC_IRET)
1119 rc2 = RC_LRET;
1120 #ifdef TCC_TARGET_X86_64
1121 else if (rc == RC_FRET)
1122 rc2 = RC_QRET;
1123 #endif
1124 #endif
1125 /* need to reload if:
1126 - constant
1127 - lvalue (need to dereference pointer)
1128 - already a register, but not in the right class */
1129 if (r >= VT_CONST
1130 || (vtop->r & VT_LVAL)
1131 || !(reg_classes[r] & rc)
1132 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1133 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
1134 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
1135 #else
1136 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
1137 #endif
1140 r = get_reg(rc);
1141 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1142 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
1143 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
1144 #else
1145 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1146 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
1147 unsigned long long ll;
1148 #endif
1149 int r2, original_type;
1150 original_type = vtop->type.t;
1151 /* two register type load : expand to two words
1152 temporarily */
1153 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1154 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1155 /* load constant */
1156 ll = vtop->c.i;
1157 vtop->c.i = ll; /* first word */
1158 load(r, vtop);
1159 vtop->r = r; /* save register value */
1160 vpushi(ll >> 32); /* second word */
1161 } else
1162 #endif
1163 if (vtop->r & VT_LVAL) {
1164 /* We do not want to modifier the long long
1165 pointer here, so the safest (and less
1166 efficient) is to save all the other registers
1167 in the stack. XXX: totally inefficient. */
1168 #if 0
1169 save_regs(1);
1170 #else
1171 /* lvalue_save: save only if used further down the stack */
1172 save_reg_upstack(vtop->r, 1);
1173 #endif
1174 /* load from memory */
1175 vtop->type.t = load_type;
1176 load(r, vtop);
1177 vdup();
1178 vtop[-1].r = r; /* save register value */
1179 /* increment pointer to get second word */
1180 vtop->type.t = addr_type;
1181 gaddrof();
1182 vpushi(load_size);
1183 gen_op('+');
1184 vtop->r |= VT_LVAL;
1185 vtop->type.t = load_type;
1186 } else {
1187 /* move registers */
1188 load(r, vtop);
1189 vdup();
1190 vtop[-1].r = r; /* save register value */
1191 vtop->r = vtop[-1].r2;
1193 /* Allocate second register. Here we rely on the fact that
1194 get_reg() tries first to free r2 of an SValue. */
1195 r2 = get_reg(rc2);
1196 load(r2, vtop);
1197 vpop();
1198 /* write second register */
1199 vtop->r2 = r2;
1200 vtop->type.t = original_type;
1201 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
1202 int t1, t;
1203 /* lvalue of scalar type : need to use lvalue type
1204 because of possible cast */
1205 t = vtop->type.t;
1206 t1 = t;
1207 /* compute memory access type */
1208 if (vtop->r & VT_LVAL_BYTE)
1209 t = VT_BYTE;
1210 else if (vtop->r & VT_LVAL_SHORT)
1211 t = VT_SHORT;
1212 if (vtop->r & VT_LVAL_UNSIGNED)
1213 t |= VT_UNSIGNED;
1214 vtop->type.t = t;
1215 load(r, vtop);
1216 /* restore wanted type */
1217 vtop->type.t = t1;
1218 } else {
1219 /* one register type load */
1220 load(r, vtop);
1223 vtop->r = r;
1224 #ifdef TCC_TARGET_C67
1225 /* uses register pairs for doubles */
1226 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1227 vtop->r2 = r+1;
1228 #endif
1230 return r;
1233 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1234 ST_FUNC void gv2(int rc1, int rc2)
1236 int v;
1238 /* generate more generic register first. But VT_JMP or VT_CMP
1239 values must be generated first in all cases to avoid possible
1240 reload errors */
1241 v = vtop[0].r & VT_VALMASK;
1242 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
1243 vswap();
1244 gv(rc1);
1245 vswap();
1246 gv(rc2);
1247 /* test if reload is needed for first register */
1248 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
1249 vswap();
1250 gv(rc1);
1251 vswap();
1253 } else {
1254 gv(rc2);
1255 vswap();
1256 gv(rc1);
1257 vswap();
1258 /* test if reload is needed for first register */
1259 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
1260 gv(rc2);
1265 #ifndef TCC_TARGET_ARM64
1266 /* wrapper around RC_FRET to return a register by type */
1267 static int rc_fret(int t)
1269 #ifdef TCC_TARGET_X86_64
1270 if (t == VT_LDOUBLE) {
1271 return RC_ST0;
1273 #endif
1274 return RC_FRET;
1276 #endif
1278 /* wrapper around REG_FRET to return a register by type */
1279 static int reg_fret(int t)
1281 #ifdef TCC_TARGET_X86_64
1282 if (t == VT_LDOUBLE) {
1283 return TREG_ST0;
1285 #endif
1286 return REG_FRET;
1289 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1290 /* expand 64bit on stack in two ints */
1291 static void lexpand(void)
1293 int u, v;
1294 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
1295 v = vtop->r & (VT_VALMASK | VT_LVAL);
1296 if (v == VT_CONST) {
1297 vdup();
1298 vtop[0].c.i >>= 32;
1299 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1300 vdup();
1301 vtop[0].c.i += 4;
1302 } else {
1303 gv(RC_INT);
1304 vdup();
1305 vtop[0].r = vtop[-1].r2;
1306 vtop[0].r2 = vtop[-1].r2 = VT_CONST;
1308 vtop[0].type.t = vtop[-1].type.t = VT_INT | u;
1310 #endif
1312 #ifdef TCC_TARGET_ARM
1313 /* expand long long on stack */
1314 ST_FUNC void lexpand_nr(void)
1316 int u,v;
1318 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
1319 vdup();
1320 vtop->r2 = VT_CONST;
1321 vtop->type.t = VT_INT | u;
1322 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
1323 if (v == VT_CONST) {
1324 vtop[-1].c.i = vtop->c.i;
1325 vtop->c.i = vtop->c.i >> 32;
1326 vtop->r = VT_CONST;
1327 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1328 vtop->c.i += 4;
1329 vtop->r = vtop[-1].r;
1330 } else if (v > VT_CONST) {
1331 vtop--;
1332 lexpand();
1333 } else
1334 vtop->r = vtop[-1].r2;
1335 vtop[-1].r2 = VT_CONST;
1336 vtop[-1].type.t = VT_INT | u;
1338 #endif
1340 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1341 /* build a long long from two ints */
1342 static void lbuild(int t)
1344 gv2(RC_INT, RC_INT);
1345 vtop[-1].r2 = vtop[0].r;
1346 vtop[-1].type.t = t;
1347 vpop();
1349 #endif
1351 /* convert stack entry to register and duplicate its value in another
1352 register */
1353 static void gv_dup(void)
1355 int rc, t, r, r1;
1356 SValue sv;
1358 t = vtop->type.t;
1359 #if !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_ARM64)
1360 if ((t & VT_BTYPE) == VT_LLONG) {
1361 lexpand();
1362 gv_dup();
1363 vswap();
1364 vrotb(3);
1365 gv_dup();
1366 vrotb(4);
1367 /* stack: H L L1 H1 */
1368 lbuild(t);
1369 vrotb(3);
1370 vrotb(3);
1371 vswap();
1372 lbuild(t);
1373 vswap();
1374 } else
1375 #endif
1377 /* duplicate value */
1378 rc = RC_INT;
1379 sv.type.t = VT_INT;
1380 if (is_float(t)) {
1381 rc = RC_FLOAT;
1382 #ifdef TCC_TARGET_X86_64
1383 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1384 rc = RC_ST0;
1386 #endif
1387 sv.type.t = t;
1389 r = gv(rc);
1390 r1 = get_reg(rc);
1391 sv.r = r;
1392 sv.c.i = 0;
1393 load(r1, &sv); /* move r to r1 */
1394 vdup();
1395 /* duplicates value */
1396 if (r != r1)
1397 vtop->r = r1;
1401 /* Generate value test
1403 * Generate a test for any value (jump, comparison and integers) */
1404 ST_FUNC int gvtst(int inv, int t)
1406 int v = vtop->r & VT_VALMASK;
1407 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1408 vpushi(0);
1409 gen_op(TOK_NE);
1411 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1412 /* constant jmp optimization */
1413 if ((vtop->c.i != 0) != inv)
1414 t = gjmp(t);
1415 vtop--;
1416 return t;
1418 return gtst(inv, t);
1421 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
1422 /* generate CPU independent (unsigned) long long operations */
1423 static void gen_opl(int op)
1425 int t, a, b, op1, c, i;
1426 int func;
1427 unsigned short reg_iret = REG_IRET;
1428 unsigned short reg_lret = REG_LRET;
1429 SValue tmp;
1431 switch(op) {
1432 case '/':
1433 case TOK_PDIV:
1434 func = TOK___divdi3;
1435 goto gen_func;
1436 case TOK_UDIV:
1437 func = TOK___udivdi3;
1438 goto gen_func;
1439 case '%':
1440 func = TOK___moddi3;
1441 goto gen_mod_func;
1442 case TOK_UMOD:
1443 func = TOK___umoddi3;
1444 gen_mod_func:
1445 #ifdef TCC_ARM_EABI
1446 reg_iret = TREG_R2;
1447 reg_lret = TREG_R3;
1448 #endif
1449 gen_func:
1450 /* call generic long long function */
1451 vpush_global_sym(&func_old_type, func);
1452 vrott(3);
1453 gfunc_call(2);
1454 vpushi(0);
1455 vtop->r = reg_iret;
1456 vtop->r2 = reg_lret;
1457 break;
1458 case '^':
1459 case '&':
1460 case '|':
1461 case '*':
1462 case '+':
1463 case '-':
1464 //pv("gen_opl A",0,2);
1465 t = vtop->type.t;
1466 vswap();
1467 lexpand();
1468 vrotb(3);
1469 lexpand();
1470 /* stack: L1 H1 L2 H2 */
1471 tmp = vtop[0];
1472 vtop[0] = vtop[-3];
1473 vtop[-3] = tmp;
1474 tmp = vtop[-2];
1475 vtop[-2] = vtop[-3];
1476 vtop[-3] = tmp;
1477 vswap();
1478 /* stack: H1 H2 L1 L2 */
1479 //pv("gen_opl B",0,4);
1480 if (op == '*') {
1481 vpushv(vtop - 1);
1482 vpushv(vtop - 1);
1483 gen_op(TOK_UMULL);
1484 lexpand();
1485 /* stack: H1 H2 L1 L2 ML MH */
1486 for(i=0;i<4;i++)
1487 vrotb(6);
1488 /* stack: ML MH H1 H2 L1 L2 */
1489 tmp = vtop[0];
1490 vtop[0] = vtop[-2];
1491 vtop[-2] = tmp;
1492 /* stack: ML MH H1 L2 H2 L1 */
1493 gen_op('*');
1494 vrotb(3);
1495 vrotb(3);
1496 gen_op('*');
1497 /* stack: ML MH M1 M2 */
1498 gen_op('+');
1499 gen_op('+');
1500 } else if (op == '+' || op == '-') {
1501 /* XXX: add non carry method too (for MIPS or alpha) */
1502 if (op == '+')
1503 op1 = TOK_ADDC1;
1504 else
1505 op1 = TOK_SUBC1;
1506 gen_op(op1);
1507 /* stack: H1 H2 (L1 op L2) */
1508 vrotb(3);
1509 vrotb(3);
1510 gen_op(op1 + 1); /* TOK_xxxC2 */
1511 } else {
1512 gen_op(op);
1513 /* stack: H1 H2 (L1 op L2) */
1514 vrotb(3);
1515 vrotb(3);
1516 /* stack: (L1 op L2) H1 H2 */
1517 gen_op(op);
1518 /* stack: (L1 op L2) (H1 op H2) */
1520 /* stack: L H */
1521 lbuild(t);
1522 break;
1523 case TOK_SAR:
1524 case TOK_SHR:
1525 case TOK_SHL:
1526 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1527 t = vtop[-1].type.t;
1528 vswap();
1529 lexpand();
1530 vrotb(3);
1531 /* stack: L H shift */
1532 c = (int)vtop->c.i;
1533 /* constant: simpler */
1534 /* NOTE: all comments are for SHL. the other cases are
1535 done by swapping words */
1536 vpop();
1537 if (op != TOK_SHL)
1538 vswap();
1539 if (c >= 32) {
1540 /* stack: L H */
1541 vpop();
1542 if (c > 32) {
1543 vpushi(c - 32);
1544 gen_op(op);
1546 if (op != TOK_SAR) {
1547 vpushi(0);
1548 } else {
1549 gv_dup();
1550 vpushi(31);
1551 gen_op(TOK_SAR);
1553 vswap();
1554 } else {
1555 vswap();
1556 gv_dup();
1557 /* stack: H L L */
1558 vpushi(c);
1559 gen_op(op);
1560 vswap();
1561 vpushi(32 - c);
1562 if (op == TOK_SHL)
1563 gen_op(TOK_SHR);
1564 else
1565 gen_op(TOK_SHL);
1566 vrotb(3);
1567 /* stack: L L H */
1568 vpushi(c);
1569 if (op == TOK_SHL)
1570 gen_op(TOK_SHL);
1571 else
1572 gen_op(TOK_SHR);
1573 gen_op('|');
1575 if (op != TOK_SHL)
1576 vswap();
1577 lbuild(t);
1578 } else {
1579 /* XXX: should provide a faster fallback on x86 ? */
1580 switch(op) {
1581 case TOK_SAR:
1582 func = TOK___ashrdi3;
1583 goto gen_func;
1584 case TOK_SHR:
1585 func = TOK___lshrdi3;
1586 goto gen_func;
1587 case TOK_SHL:
1588 func = TOK___ashldi3;
1589 goto gen_func;
1592 break;
1593 default:
1594 /* compare operations */
1595 t = vtop->type.t;
1596 vswap();
1597 lexpand();
1598 vrotb(3);
1599 lexpand();
1600 /* stack: L1 H1 L2 H2 */
1601 tmp = vtop[-1];
1602 vtop[-1] = vtop[-2];
1603 vtop[-2] = tmp;
1604 /* stack: L1 L2 H1 H2 */
1605 /* compare high */
1606 op1 = op;
1607 /* when values are equal, we need to compare low words. since
1608 the jump is inverted, we invert the test too. */
1609 if (op1 == TOK_LT)
1610 op1 = TOK_LE;
1611 else if (op1 == TOK_GT)
1612 op1 = TOK_GE;
1613 else if (op1 == TOK_ULT)
1614 op1 = TOK_ULE;
1615 else if (op1 == TOK_UGT)
1616 op1 = TOK_UGE;
1617 a = 0;
1618 b = 0;
1619 gen_op(op1);
1620 if (op == TOK_NE) {
1621 b = gvtst(0, 0);
1622 } else {
1623 a = gvtst(1, 0);
1624 if (op != TOK_EQ) {
1625 /* generate non equal test */
1626 vpushi(TOK_NE);
1627 vtop->r = VT_CMP;
1628 b = gvtst(0, 0);
1631 /* compare low. Always unsigned */
1632 op1 = op;
1633 if (op1 == TOK_LT)
1634 op1 = TOK_ULT;
1635 else if (op1 == TOK_LE)
1636 op1 = TOK_ULE;
1637 else if (op1 == TOK_GT)
1638 op1 = TOK_UGT;
1639 else if (op1 == TOK_GE)
1640 op1 = TOK_UGE;
1641 gen_op(op1);
1642 a = gvtst(1, a);
1643 gsym(b);
1644 vseti(VT_JMPI, a);
1645 break;
1648 #endif
1650 static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b)
1652 uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b);
1653 return (a ^ b) >> 63 ? -x : x;
1656 static int gen_opic_lt(uint64_t a, uint64_t b)
1658 return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63);
1661 /* handle integer constant optimizations and various machine
1662 independent opt */
1663 static void gen_opic(int op)
1665 SValue *v1 = vtop - 1;
1666 SValue *v2 = vtop;
1667 int t1 = v1->type.t & VT_BTYPE;
1668 int t2 = v2->type.t & VT_BTYPE;
1669 int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1670 int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1671 uint64_t l1 = c1 ? v1->c.i : 0;
1672 uint64_t l2 = c2 ? v2->c.i : 0;
1673 int shm = (t1 == VT_LLONG) ? 63 : 31;
1675 if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
1676 l1 = ((uint32_t)l1 |
1677 (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
1678 if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR))
1679 l2 = ((uint32_t)l2 |
1680 (v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000)));
1682 if (c1 && c2) {
1683 switch(op) {
1684 case '+': l1 += l2; break;
1685 case '-': l1 -= l2; break;
1686 case '&': l1 &= l2; break;
1687 case '^': l1 ^= l2; break;
1688 case '|': l1 |= l2; break;
1689 case '*': l1 *= l2; break;
1691 case TOK_PDIV:
1692 case '/':
1693 case '%':
1694 case TOK_UDIV:
1695 case TOK_UMOD:
1696 /* if division by zero, generate explicit division */
1697 if (l2 == 0) {
1698 if (const_wanted)
1699 tcc_error("division by zero in constant");
1700 goto general_case;
1702 switch(op) {
1703 default: l1 = gen_opic_sdiv(l1, l2); break;
1704 case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break;
1705 case TOK_UDIV: l1 = l1 / l2; break;
1706 case TOK_UMOD: l1 = l1 % l2; break;
1708 break;
1709 case TOK_SHL: l1 <<= (l2 & shm); break;
1710 case TOK_SHR: l1 >>= (l2 & shm); break;
1711 case TOK_SAR:
1712 l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm);
1713 break;
1714 /* tests */
1715 case TOK_ULT: l1 = l1 < l2; break;
1716 case TOK_UGE: l1 = l1 >= l2; break;
1717 case TOK_EQ: l1 = l1 == l2; break;
1718 case TOK_NE: l1 = l1 != l2; break;
1719 case TOK_ULE: l1 = l1 <= l2; break;
1720 case TOK_UGT: l1 = l1 > l2; break;
1721 case TOK_LT: l1 = gen_opic_lt(l1, l2); break;
1722 case TOK_GE: l1 = !gen_opic_lt(l1, l2); break;
1723 case TOK_LE: l1 = !gen_opic_lt(l2, l1); break;
1724 case TOK_GT: l1 = gen_opic_lt(l2, l1); break;
1725 /* logical */
1726 case TOK_LAND: l1 = l1 && l2; break;
1727 case TOK_LOR: l1 = l1 || l2; break;
1728 default:
1729 goto general_case;
1731 if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
1732 l1 = ((uint32_t)l1 |
1733 (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
1734 v1->c.i = l1;
1735 vtop--;
1736 } else {
1737 /* if commutative ops, put c2 as constant */
1738 if (c1 && (op == '+' || op == '&' || op == '^' ||
1739 op == '|' || op == '*')) {
1740 vswap();
1741 c2 = c1; //c = c1, c1 = c2, c2 = c;
1742 l2 = l1; //l = l1, l1 = l2, l2 = l;
1744 if (!const_wanted &&
1745 c1 && ((l1 == 0 &&
1746 (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
1747 (l1 == -1 && op == TOK_SAR))) {
1748 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1749 vtop--;
1750 } else if (!const_wanted &&
1751 c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
1752 (l2 == -1 && op == '|') ||
1753 (l2 == 0xffffffff && t2 != VT_LLONG && op == '|') ||
1754 (l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
1755 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1756 if (l2 == 1)
1757 vtop->c.i = 0;
1758 vswap();
1759 vtop--;
1760 } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1761 op == TOK_PDIV) &&
1762 l2 == 1) ||
1763 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1764 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1765 l2 == 0) ||
1766 (op == '&' &&
1767 l2 == -1))) {
1768 /* filter out NOP operations like x*1, x-0, x&-1... */
1769 vtop--;
1770 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1771 /* try to use shifts instead of muls or divs */
1772 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1773 int n = -1;
1774 while (l2) {
1775 l2 >>= 1;
1776 n++;
1778 vtop->c.i = n;
1779 if (op == '*')
1780 op = TOK_SHL;
1781 else if (op == TOK_PDIV)
1782 op = TOK_SAR;
1783 else
1784 op = TOK_SHR;
1786 goto general_case;
1787 } else if (c2 && (op == '+' || op == '-') &&
1788 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1789 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1790 /* symbol + constant case */
1791 if (op == '-')
1792 l2 = -l2;
1793 l2 += vtop[-1].c.i;
1794 /* The backends can't always deal with addends to symbols
1795 larger than +-1<<31. Don't construct such. */
1796 if ((int)l2 != l2)
1797 goto general_case;
1798 vtop--;
1799 vtop->c.i = l2;
1800 } else {
1801 general_case:
1802 /* call low level op generator */
1803 if (t1 == VT_LLONG || t2 == VT_LLONG ||
1804 (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
1805 gen_opl(op);
1806 else
1807 gen_opi(op);
1812 /* generate a floating point operation with constant propagation */
1813 static void gen_opif(int op)
1815 int c1, c2;
1816 SValue *v1, *v2;
1817 long double f1, f2;
1819 v1 = vtop - 1;
1820 v2 = vtop;
1821 /* currently, we cannot do computations with forward symbols */
1822 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1823 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1824 if (c1 && c2) {
1825 if (v1->type.t == VT_FLOAT) {
1826 f1 = v1->c.f;
1827 f2 = v2->c.f;
1828 } else if (v1->type.t == VT_DOUBLE) {
1829 f1 = v1->c.d;
1830 f2 = v2->c.d;
1831 } else {
1832 f1 = v1->c.ld;
1833 f2 = v2->c.ld;
1836 /* NOTE: we only do constant propagation if finite number (not
1837 NaN or infinity) (ANSI spec) */
1838 if (!ieee_finite(f1) || !ieee_finite(f2))
1839 goto general_case;
1841 switch(op) {
1842 case '+': f1 += f2; break;
1843 case '-': f1 -= f2; break;
1844 case '*': f1 *= f2; break;
1845 case '/':
1846 if (f2 == 0.0) {
1847 if (const_wanted)
1848 tcc_error("division by zero in constant");
1849 goto general_case;
1851 f1 /= f2;
1852 break;
1853 /* XXX: also handles tests ? */
1854 default:
1855 goto general_case;
1857 /* XXX: overflow test ? */
1858 if (v1->type.t == VT_FLOAT) {
1859 v1->c.f = f1;
1860 } else if (v1->type.t == VT_DOUBLE) {
1861 v1->c.d = f1;
1862 } else {
1863 v1->c.ld = f1;
1865 vtop--;
1866 } else {
1867 general_case:
1868 gen_opf(op);
1872 static int pointed_size(CType *type)
1874 int align;
1875 return type_size(pointed_type(type), &align);
1878 static void vla_runtime_pointed_size(CType *type)
1880 int align;
1881 vla_runtime_type_size(pointed_type(type), &align);
1884 static inline int is_null_pointer(SValue *p)
1886 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1887 return 0;
1888 return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) ||
1889 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) ||
1890 ((p->type.t & VT_BTYPE) == VT_PTR &&
1891 (PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0));
1894 static inline int is_integer_btype(int bt)
1896 return (bt == VT_BYTE || bt == VT_SHORT ||
1897 bt == VT_INT || bt == VT_LLONG);
1900 /* check types for comparison or subtraction of pointers */
1901 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1903 CType *type1, *type2, tmp_type1, tmp_type2;
1904 int bt1, bt2;
1906 /* null pointers are accepted for all comparisons as gcc */
1907 if (is_null_pointer(p1) || is_null_pointer(p2))
1908 return;
1909 type1 = &p1->type;
1910 type2 = &p2->type;
1911 bt1 = type1->t & VT_BTYPE;
1912 bt2 = type2->t & VT_BTYPE;
1913 /* accept comparison between pointer and integer with a warning */
1914 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1915 if (op != TOK_LOR && op != TOK_LAND )
1916 tcc_warning("comparison between pointer and integer");
1917 return;
1920 /* both must be pointers or implicit function pointers */
1921 if (bt1 == VT_PTR) {
1922 type1 = pointed_type(type1);
1923 } else if (bt1 != VT_FUNC)
1924 goto invalid_operands;
1926 if (bt2 == VT_PTR) {
1927 type2 = pointed_type(type2);
1928 } else if (bt2 != VT_FUNC) {
1929 invalid_operands:
1930 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1932 if ((type1->t & VT_BTYPE) == VT_VOID ||
1933 (type2->t & VT_BTYPE) == VT_VOID)
1934 return;
1935 tmp_type1 = *type1;
1936 tmp_type2 = *type2;
1937 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1938 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1939 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1940 /* gcc-like error if '-' is used */
1941 if (op == '-')
1942 goto invalid_operands;
1943 else
1944 tcc_warning("comparison of distinct pointer types lacks a cast");
1948 /* generic gen_op: handles types problems */
1949 ST_FUNC void gen_op(int op)
1951 int u, t1, t2, bt1, bt2, t;
1952 CType type1;
1954 redo:
1955 t1 = vtop[-1].type.t;
1956 t2 = vtop[0].type.t;
1957 bt1 = t1 & VT_BTYPE;
1958 bt2 = t2 & VT_BTYPE;
1960 if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1961 tcc_error("operation on a struct");
1962 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
1963 if (bt2 == VT_FUNC) {
1964 mk_pointer(&vtop->type);
1965 gaddrof();
1967 if (bt1 == VT_FUNC) {
1968 vswap();
1969 mk_pointer(&vtop->type);
1970 gaddrof();
1971 vswap();
1973 goto redo;
1974 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
1975 /* at least one operand is a pointer */
1976 /* relational op: must be both pointers */
1977 if (op >= TOK_ULT && op <= TOK_LOR) {
1978 check_comparison_pointer_types(vtop - 1, vtop, op);
1979 /* pointers are handled are unsigned */
1980 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
1981 t = VT_LLONG | VT_UNSIGNED;
1982 #else
1983 t = VT_INT | VT_UNSIGNED;
1984 #endif
1985 goto std_op;
1987 /* if both pointers, then it must be the '-' op */
1988 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1989 if (op != '-')
1990 tcc_error("cannot use pointers here");
1991 check_comparison_pointer_types(vtop - 1, vtop, op);
1992 /* XXX: check that types are compatible */
1993 if (vtop[-1].type.t & VT_VLA) {
1994 vla_runtime_pointed_size(&vtop[-1].type);
1995 } else {
1996 vpushi(pointed_size(&vtop[-1].type));
1998 vrott(3);
1999 gen_opic(op);
2000 /* set to integer type */
2001 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2002 vtop->type.t = VT_LLONG;
2003 #else
2004 vtop->type.t = VT_INT;
2005 #endif
2006 vswap();
2007 gen_op(TOK_PDIV);
2008 } else {
2009 /* exactly one pointer : must be '+' or '-'. */
2010 if (op != '-' && op != '+')
2011 tcc_error("cannot use pointers here");
2012 /* Put pointer as first operand */
2013 if (bt2 == VT_PTR) {
2014 vswap();
2015 t = t1, t1 = t2, t2 = t;
2017 #if PTR_SIZE == 4
2018 if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG)
2019 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2020 gen_cast(&int_type);
2021 #endif
2022 type1 = vtop[-1].type;
2023 type1.t &= ~VT_ARRAY;
2024 if (vtop[-1].type.t & VT_VLA)
2025 vla_runtime_pointed_size(&vtop[-1].type);
2026 else {
2027 u = pointed_size(&vtop[-1].type);
2028 if (u < 0)
2029 tcc_error("unknown array element size");
2030 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2031 vpushll(u);
2032 #else
2033 /* XXX: cast to int ? (long long case) */
2034 vpushi(u);
2035 #endif
2037 gen_op('*');
2038 #if 0
2039 /* #ifdef CONFIG_TCC_BCHECK
2040 The main reason to removing this code:
2041 #include <stdio.h>
2042 int main ()
2044 int v[10];
2045 int i = 10;
2046 int j = 9;
2047 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2048 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2050 When this code is on. then the output looks like
2051 v+i-j = 0xfffffffe
2052 v+(i-j) = 0xbff84000
2054 /* if evaluating constant expression, no code should be
2055 generated, so no bound check */
2056 if (tcc_state->do_bounds_check && !const_wanted) {
2057 /* if bounded pointers, we generate a special code to
2058 test bounds */
2059 if (op == '-') {
2060 vpushi(0);
2061 vswap();
2062 gen_op('-');
2064 gen_bounded_ptr_add();
2065 } else
2066 #endif
2068 gen_opic(op);
2070 /* put again type if gen_opic() swaped operands */
2071 vtop->type = type1;
2073 } else if (is_float(bt1) || is_float(bt2)) {
2074 /* compute bigger type and do implicit casts */
2075 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
2076 t = VT_LDOUBLE;
2077 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
2078 t = VT_DOUBLE;
2079 } else {
2080 t = VT_FLOAT;
2082 /* floats can only be used for a few operations */
2083 if (op != '+' && op != '-' && op != '*' && op != '/' &&
2084 (op < TOK_ULT || op > TOK_GT))
2085 tcc_error("invalid operands for binary operation");
2086 goto std_op;
2087 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
2088 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
2089 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED))
2090 t |= VT_UNSIGNED;
2091 goto std_op;
2092 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
2093 /* cast to biggest op */
2094 t = VT_LLONG;
2095 /* convert to unsigned if it does not fit in a long long */
2096 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
2097 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
2098 t |= VT_UNSIGNED;
2099 goto std_op;
2100 } else {
2101 /* integer operations */
2102 t = VT_INT;
2103 /* convert to unsigned if it does not fit in an integer */
2104 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
2105 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
2106 t |= VT_UNSIGNED;
2107 std_op:
2108 /* XXX: currently, some unsigned operations are explicit, so
2109 we modify them here */
2110 if (t & VT_UNSIGNED) {
2111 if (op == TOK_SAR)
2112 op = TOK_SHR;
2113 else if (op == '/')
2114 op = TOK_UDIV;
2115 else if (op == '%')
2116 op = TOK_UMOD;
2117 else if (op == TOK_LT)
2118 op = TOK_ULT;
2119 else if (op == TOK_GT)
2120 op = TOK_UGT;
2121 else if (op == TOK_LE)
2122 op = TOK_ULE;
2123 else if (op == TOK_GE)
2124 op = TOK_UGE;
2126 vswap();
2127 type1.t = t;
2128 gen_cast(&type1);
2129 vswap();
2130 /* special case for shifts and long long: we keep the shift as
2131 an integer */
2132 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
2133 type1.t = VT_INT;
2134 gen_cast(&type1);
2135 if (is_float(t))
2136 gen_opif(op);
2137 else
2138 gen_opic(op);
2139 if (op >= TOK_ULT && op <= TOK_GT) {
2140 /* relational op: the result is an int */
2141 vtop->type.t = VT_INT;
2142 } else {
2143 vtop->type.t = t;
2146 // Make sure that we have converted to an rvalue:
2147 if (vtop->r & VT_LVAL)
2148 gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
2151 #ifndef TCC_TARGET_ARM
2152 /* generic itof for unsigned long long case */
2153 static void gen_cvt_itof1(int t)
2155 #ifdef TCC_TARGET_ARM64
2156 gen_cvt_itof(t);
2157 #else
2158 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2159 (VT_LLONG | VT_UNSIGNED)) {
2161 if (t == VT_FLOAT)
2162 vpush_global_sym(&func_old_type, TOK___floatundisf);
2163 #if LDOUBLE_SIZE != 8
2164 else if (t == VT_LDOUBLE)
2165 vpush_global_sym(&func_old_type, TOK___floatundixf);
2166 #endif
2167 else
2168 vpush_global_sym(&func_old_type, TOK___floatundidf);
2169 vrott(2);
2170 gfunc_call(1);
2171 vpushi(0);
2172 vtop->r = reg_fret(t);
2173 } else {
2174 gen_cvt_itof(t);
2176 #endif
2178 #endif
2180 /* generic ftoi for unsigned long long case */
2181 static void gen_cvt_ftoi1(int t)
2183 #ifdef TCC_TARGET_ARM64
2184 gen_cvt_ftoi(t);
2185 #else
2186 int st;
2188 if (t == (VT_LLONG | VT_UNSIGNED)) {
2189 /* not handled natively */
2190 st = vtop->type.t & VT_BTYPE;
2191 if (st == VT_FLOAT)
2192 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
2193 #if LDOUBLE_SIZE != 8
2194 else if (st == VT_LDOUBLE)
2195 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
2196 #endif
2197 else
2198 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
2199 vrott(2);
2200 gfunc_call(1);
2201 vpushi(0);
2202 vtop->r = REG_IRET;
2203 vtop->r2 = REG_LRET;
2204 } else {
2205 gen_cvt_ftoi(t);
2207 #endif
2210 /* force char or short cast */
2211 static void force_charshort_cast(int t)
2213 int bits, dbt;
2214 dbt = t & VT_BTYPE;
2215 /* XXX: add optimization if lvalue : just change type and offset */
2216 if (dbt == VT_BYTE)
2217 bits = 8;
2218 else
2219 bits = 16;
2220 if (t & VT_UNSIGNED) {
2221 vpushi((1 << bits) - 1);
2222 gen_op('&');
2223 } else {
2224 if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
2225 bits = 64 - bits;
2226 else
2227 bits = 32 - bits;
2228 vpushi(bits);
2229 gen_op(TOK_SHL);
2230 /* result must be signed or the SAR is converted to an SHL
2231 This was not the case when "t" was a signed short
2232 and the last value on the stack was an unsigned int */
2233 vtop->type.t &= ~VT_UNSIGNED;
2234 vpushi(bits);
2235 gen_op(TOK_SAR);
2239 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2240 static void gen_cast(CType *type)
2242 int sbt, dbt, sf, df, c, p;
2244 /* special delayed cast for char/short */
2245 /* XXX: in some cases (multiple cascaded casts), it may still
2246 be incorrect */
2247 if (vtop->r & VT_MUSTCAST) {
2248 vtop->r &= ~VT_MUSTCAST;
2249 force_charshort_cast(vtop->type.t);
2252 /* bitfields first get cast to ints */
2253 if (vtop->type.t & VT_BITFIELD) {
2254 gv(RC_INT);
2257 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
2258 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
2260 if (sbt != dbt) {
2261 sf = is_float(sbt);
2262 df = is_float(dbt);
2263 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2264 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
2265 if (c) {
2266 /* constant case: we can do it now */
2267 /* XXX: in ISOC, cannot do it if error in convert */
2268 if (sbt == VT_FLOAT)
2269 vtop->c.ld = vtop->c.f;
2270 else if (sbt == VT_DOUBLE)
2271 vtop->c.ld = vtop->c.d;
2273 if (df) {
2274 if ((sbt & VT_BTYPE) == VT_LLONG) {
2275 if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 63))
2276 vtop->c.ld = vtop->c.i;
2277 else
2278 vtop->c.ld = -(long double)-vtop->c.i;
2279 } else if(!sf) {
2280 if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 31))
2281 vtop->c.ld = (uint32_t)vtop->c.i;
2282 else
2283 vtop->c.ld = -(long double)-(uint32_t)vtop->c.i;
2286 if (dbt == VT_FLOAT)
2287 vtop->c.f = (float)vtop->c.ld;
2288 else if (dbt == VT_DOUBLE)
2289 vtop->c.d = (double)vtop->c.ld;
2290 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
2291 vtop->c.i = vtop->c.ld;
2292 } else if (sf && dbt == VT_BOOL) {
2293 vtop->c.i = (vtop->c.ld != 0);
2294 } else {
2295 if(sf)
2296 vtop->c.i = vtop->c.ld;
2297 else if (sbt == (VT_LLONG|VT_UNSIGNED))
2299 else if (sbt & VT_UNSIGNED)
2300 vtop->c.i = (uint32_t)vtop->c.i;
2301 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2302 else if (sbt == VT_PTR)
2304 #endif
2305 else if (sbt != VT_LLONG)
2306 vtop->c.i = ((uint32_t)vtop->c.i |
2307 -(vtop->c.i & 0x80000000));
2309 if (dbt == (VT_LLONG|VT_UNSIGNED))
2311 else if (dbt == VT_BOOL)
2312 vtop->c.i = (vtop->c.i != 0);
2313 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2314 else if (dbt == VT_PTR)
2316 #endif
2317 else if (dbt != VT_LLONG) {
2318 uint32_t m = ((dbt & VT_BTYPE) == VT_BYTE ? 0xff :
2319 (dbt & VT_BTYPE) == VT_SHORT ? 0xffff :
2320 0xffffffff);
2321 vtop->c.i &= m;
2322 if (!(dbt & VT_UNSIGNED))
2323 vtop->c.i |= -(vtop->c.i & ((m >> 1) + 1));
2326 } else if (p && dbt == VT_BOOL) {
2327 vtop->r = VT_CONST;
2328 vtop->c.i = 1;
2329 } else {
2330 /* non constant case: generate code */
2331 if (sf && df) {
2332 /* convert from fp to fp */
2333 gen_cvt_ftof(dbt);
2334 } else if (df) {
2335 /* convert int to fp */
2336 gen_cvt_itof1(dbt);
2337 } else if (sf) {
2338 /* convert fp to int */
2339 if (dbt == VT_BOOL) {
2340 vpushi(0);
2341 gen_op(TOK_NE);
2342 } else {
2343 /* we handle char/short/etc... with generic code */
2344 if (dbt != (VT_INT | VT_UNSIGNED) &&
2345 dbt != (VT_LLONG | VT_UNSIGNED) &&
2346 dbt != VT_LLONG)
2347 dbt = VT_INT;
2348 gen_cvt_ftoi1(dbt);
2349 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
2350 /* additional cast for char/short... */
2351 vtop->type.t = dbt;
2352 gen_cast(type);
2355 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2356 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2357 if ((sbt & VT_BTYPE) != VT_LLONG) {
2358 /* scalar to long long */
2359 /* machine independent conversion */
2360 gv(RC_INT);
2361 /* generate high word */
2362 if (sbt == (VT_INT | VT_UNSIGNED)) {
2363 vpushi(0);
2364 gv(RC_INT);
2365 } else {
2366 if (sbt == VT_PTR) {
2367 /* cast from pointer to int before we apply
2368 shift operation, which pointers don't support*/
2369 gen_cast(&int_type);
2371 gv_dup();
2372 vpushi(31);
2373 gen_op(TOK_SAR);
2375 /* patch second register */
2376 vtop[-1].r2 = vtop->r;
2377 vpop();
2379 #else
2380 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2381 (dbt & VT_BTYPE) == VT_PTR ||
2382 (dbt & VT_BTYPE) == VT_FUNC) {
2383 if ((sbt & VT_BTYPE) != VT_LLONG &&
2384 (sbt & VT_BTYPE) != VT_PTR &&
2385 (sbt & VT_BTYPE) != VT_FUNC) {
2386 /* need to convert from 32bit to 64bit */
2387 gv(RC_INT);
2388 if (sbt != (VT_INT | VT_UNSIGNED)) {
2389 #if defined(TCC_TARGET_ARM64)
2390 gen_cvt_sxtw();
2391 #elif defined(TCC_TARGET_X86_64)
2392 int r = gv(RC_INT);
2393 /* x86_64 specific: movslq */
2394 o(0x6348);
2395 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2396 #else
2397 #error
2398 #endif
2401 #endif
2402 } else if (dbt == VT_BOOL) {
2403 /* scalar to bool */
2404 vpushi(0);
2405 gen_op(TOK_NE);
2406 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2407 (dbt & VT_BTYPE) == VT_SHORT) {
2408 if (sbt == VT_PTR) {
2409 vtop->type.t = VT_INT;
2410 tcc_warning("nonportable conversion from pointer to char/short");
2412 force_charshort_cast(dbt);
2413 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
2414 } else if ((dbt & VT_BTYPE) == VT_INT) {
2415 /* scalar to int */
2416 if ((sbt & VT_BTYPE) == VT_LLONG) {
2417 /* from long long: just take low order word */
2418 lexpand();
2419 vpop();
2421 /* if lvalue and single word type, nothing to do because
2422 the lvalue already contains the real type size (see
2423 VT_LVAL_xxx constants) */
2424 #endif
2427 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2428 /* if we are casting between pointer types,
2429 we must update the VT_LVAL_xxx size */
2430 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2431 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2433 vtop->type = *type;
2436 /* return type size as known at compile time. Put alignment at 'a' */
2437 ST_FUNC int type_size(CType *type, int *a)
2439 Sym *s;
2440 int bt;
2442 bt = type->t & VT_BTYPE;
2443 if (bt == VT_STRUCT) {
2444 /* struct/union */
2445 s = type->ref;
2446 *a = s->r;
2447 return s->c;
2448 } else if (bt == VT_PTR) {
2449 if (type->t & VT_ARRAY) {
2450 int ts;
2452 s = type->ref;
2453 ts = type_size(&s->type, a);
2455 if (ts < 0 && s->c < 0)
2456 ts = -ts;
2458 return ts * s->c;
2459 } else {
2460 *a = PTR_SIZE;
2461 return PTR_SIZE;
2463 } else if (bt == VT_LDOUBLE) {
2464 *a = LDOUBLE_ALIGN;
2465 return LDOUBLE_SIZE;
2466 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2467 #ifdef TCC_TARGET_I386
2468 #ifdef TCC_TARGET_PE
2469 *a = 8;
2470 #else
2471 *a = 4;
2472 #endif
2473 #elif defined(TCC_TARGET_ARM)
2474 #ifdef TCC_ARM_EABI
2475 *a = 8;
2476 #else
2477 *a = 4;
2478 #endif
2479 #else
2480 *a = 8;
2481 #endif
2482 return 8;
2483 } else if (bt == VT_INT || bt == VT_FLOAT) {
2484 *a = 4;
2485 return 4;
2486 } else if (bt == VT_SHORT) {
2487 *a = 2;
2488 return 2;
2489 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2490 *a = 8;
2491 return 16;
2492 } else if (bt == VT_ENUM) {
2493 *a = 4;
2494 /* Enums might be incomplete, so don't just return '4' here. */
2495 return type->ref->c;
2496 } else {
2497 /* char, void, function, _Bool */
2498 *a = 1;
2499 return 1;
2503 /* push type size as known at runtime time on top of value stack. Put
2504 alignment at 'a' */
2505 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2507 if (type->t & VT_VLA) {
2508 type_size(&type->ref->type, a);
2509 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2510 } else {
2511 vpushi(type_size(type, a));
2515 static void vla_sp_restore(void) {
2516 if (vlas_in_scope) {
2517 gen_vla_sp_restore(vla_sp_loc);
2521 static void vla_sp_restore_root(void) {
2522 if (vlas_in_scope) {
2523 gen_vla_sp_restore(vla_sp_root_loc);
2527 /* return the pointed type of t */
2528 static inline CType *pointed_type(CType *type)
2530 return &type->ref->type;
2533 /* modify type so that its it is a pointer to type. */
2534 ST_FUNC void mk_pointer(CType *type)
2536 Sym *s;
2537 s = sym_push(SYM_FIELD, type, 0, -1);
2538 type->t = VT_PTR | (type->t & ~VT_TYPE);
2539 type->ref = s;
2542 /* compare function types. OLD functions match any new functions */
2543 static int is_compatible_func(CType *type1, CType *type2)
2545 Sym *s1, *s2;
2547 s1 = type1->ref;
2548 s2 = type2->ref;
2549 if (!is_compatible_types(&s1->type, &s2->type))
2550 return 0;
2551 /* check func_call */
2552 if (s1->a.func_call != s2->a.func_call)
2553 return 0;
2554 /* XXX: not complete */
2555 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2556 return 1;
2557 if (s1->c != s2->c)
2558 return 0;
2559 while (s1 != NULL) {
2560 if (s2 == NULL)
2561 return 0;
2562 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2563 return 0;
2564 s1 = s1->next;
2565 s2 = s2->next;
2567 if (s2)
2568 return 0;
2569 return 1;
2572 /* return true if type1 and type2 are the same. If unqualified is
2573 true, qualifiers on the types are ignored.
2575 - enums are not checked as gcc __builtin_types_compatible_p ()
2577 static int compare_types(CType *type1, CType *type2, int unqualified)
2579 int bt1, t1, t2;
2581 t1 = type1->t & VT_TYPE;
2582 t2 = type2->t & VT_TYPE;
2583 if (unqualified) {
2584 /* strip qualifiers before comparing */
2585 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2586 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2588 /* Default Vs explicit signedness only matters for char */
2589 if ((t1 & VT_BTYPE) != VT_BYTE) {
2590 t1 &= ~VT_DEFSIGN;
2591 t2 &= ~VT_DEFSIGN;
2593 /* An enum is compatible with (unsigned) int. Ideally we would
2594 store the enums signedness in type->ref.a.<some_bit> and
2595 only accept unsigned enums with unsigned int and vice versa.
2596 But one of our callers (gen_assign_cast) always strips VT_UNSIGNED
2597 from pointer target types, so we can't add it here either. */
2598 if ((t1 & VT_BTYPE) == VT_ENUM) {
2599 t1 = VT_INT;
2600 if (type1->ref->a.unsigned_enum)
2601 t1 |= VT_UNSIGNED;
2603 if ((t2 & VT_BTYPE) == VT_ENUM) {
2604 t2 = VT_INT;
2605 if (type2->ref->a.unsigned_enum)
2606 t2 |= VT_UNSIGNED;
2608 /* XXX: bitfields ? */
2609 if (t1 != t2)
2610 return 0;
2611 /* test more complicated cases */
2612 bt1 = t1 & VT_BTYPE;
2613 if (bt1 == VT_PTR) {
2614 type1 = pointed_type(type1);
2615 type2 = pointed_type(type2);
2616 return is_compatible_types(type1, type2);
2617 } else if (bt1 == VT_STRUCT) {
2618 return (type1->ref == type2->ref);
2619 } else if (bt1 == VT_FUNC) {
2620 return is_compatible_func(type1, type2);
2621 } else {
2622 return 1;
2626 /* return true if type1 and type2 are exactly the same (including
2627 qualifiers).
2629 static int is_compatible_types(CType *type1, CType *type2)
2631 return compare_types(type1,type2,0);
2634 /* return true if type1 and type2 are the same (ignoring qualifiers).
2636 static int is_compatible_parameter_types(CType *type1, CType *type2)
2638 return compare_types(type1,type2,1);
2641 /* print a type. If 'varstr' is not NULL, then the variable is also
2642 printed in the type */
2643 /* XXX: union */
2644 /* XXX: add array and function pointers */
2645 static void type_to_str(char *buf, int buf_size,
2646 CType *type, const char *varstr)
2648 int bt, v, t;
2649 Sym *s, *sa;
2650 char buf1[256];
2651 const char *tstr;
2653 t = type->t;
2654 bt = t & VT_BTYPE;
2655 buf[0] = '\0';
2656 if (t & VT_CONSTANT)
2657 pstrcat(buf, buf_size, "const ");
2658 if (t & VT_VOLATILE)
2659 pstrcat(buf, buf_size, "volatile ");
2660 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2661 pstrcat(buf, buf_size, "unsigned ");
2662 else if (t & VT_DEFSIGN)
2663 pstrcat(buf, buf_size, "signed ");
2664 if (t & VT_EXTERN)
2665 pstrcat(buf, buf_size, "extern ");
2666 if (t & VT_STATIC)
2667 pstrcat(buf, buf_size, "static ");
2668 if (t & VT_TYPEDEF)
2669 pstrcat(buf, buf_size, "typedef ");
2670 if (t & VT_INLINE)
2671 pstrcat(buf, buf_size, "inline ");
2672 buf_size -= strlen(buf);
2673 buf += strlen(buf);
2674 switch(bt) {
2675 case VT_VOID:
2676 tstr = "void";
2677 goto add_tstr;
2678 case VT_BOOL:
2679 tstr = "_Bool";
2680 goto add_tstr;
2681 case VT_BYTE:
2682 tstr = "char";
2683 goto add_tstr;
2684 case VT_SHORT:
2685 tstr = "short";
2686 goto add_tstr;
2687 case VT_INT:
2688 tstr = "int";
2689 goto add_tstr;
2690 case VT_LONG:
2691 tstr = "long";
2692 goto add_tstr;
2693 case VT_LLONG:
2694 tstr = "long long";
2695 goto add_tstr;
2696 case VT_FLOAT:
2697 tstr = "float";
2698 goto add_tstr;
2699 case VT_DOUBLE:
2700 tstr = "double";
2701 goto add_tstr;
2702 case VT_LDOUBLE:
2703 tstr = "long double";
2704 add_tstr:
2705 pstrcat(buf, buf_size, tstr);
2706 break;
2707 case VT_ENUM:
2708 case VT_STRUCT:
2709 if (bt == VT_STRUCT)
2710 tstr = "struct ";
2711 else
2712 tstr = "enum ";
2713 pstrcat(buf, buf_size, tstr);
2714 v = type->ref->v & ~SYM_STRUCT;
2715 if (v >= SYM_FIRST_ANOM)
2716 pstrcat(buf, buf_size, "<anonymous>");
2717 else
2718 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2719 break;
2720 case VT_FUNC:
2721 s = type->ref;
2722 type_to_str(buf, buf_size, &s->type, varstr);
2723 pstrcat(buf, buf_size, "(");
2724 sa = s->next;
2725 while (sa != NULL) {
2726 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2727 pstrcat(buf, buf_size, buf1);
2728 sa = sa->next;
2729 if (sa)
2730 pstrcat(buf, buf_size, ", ");
2732 pstrcat(buf, buf_size, ")");
2733 goto no_var;
2734 case VT_PTR:
2735 s = type->ref;
2736 if (t & VT_ARRAY) {
2737 snprintf(buf1, sizeof(buf1), "%s[%ld]", varstr ? varstr : "", s->c);
2738 type_to_str(buf, buf_size, &s->type, buf1);
2739 goto no_var;
2741 pstrcpy(buf1, sizeof(buf1), "*");
2742 if (t & VT_CONSTANT)
2743 pstrcat(buf1, buf_size, "const ");
2744 if (t & VT_VOLATILE)
2745 pstrcat(buf1, buf_size, "volatile ");
2746 if (varstr)
2747 pstrcat(buf1, sizeof(buf1), varstr);
2748 type_to_str(buf, buf_size, &s->type, buf1);
2749 goto no_var;
2751 if (varstr) {
2752 pstrcat(buf, buf_size, " ");
2753 pstrcat(buf, buf_size, varstr);
2755 no_var: ;
2758 /* verify type compatibility to store vtop in 'dt' type, and generate
2759 casts if needed. */
2760 static void gen_assign_cast(CType *dt)
2762 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2763 char buf1[256], buf2[256];
2764 int dbt, sbt;
2766 st = &vtop->type; /* source type */
2767 dbt = dt->t & VT_BTYPE;
2768 sbt = st->t & VT_BTYPE;
2769 if (sbt == VT_VOID || dbt == VT_VOID) {
2770 if (sbt == VT_VOID && dbt == VT_VOID)
2771 ; /*
2772 It is Ok if both are void
2773 A test program:
2774 void func1() {}
2775 void func2() {
2776 return func1();
2778 gcc accepts this program
2780 else
2781 tcc_error("cannot cast from/to void");
2783 if (dt->t & VT_CONSTANT)
2784 tcc_warning("assignment of read-only location");
2785 switch(dbt) {
2786 case VT_PTR:
2787 /* special cases for pointers */
2788 /* '0' can also be a pointer */
2789 if (is_null_pointer(vtop))
2790 goto type_ok;
2791 /* accept implicit pointer to integer cast with warning */
2792 if (is_integer_btype(sbt)) {
2793 tcc_warning("assignment makes pointer from integer without a cast");
2794 goto type_ok;
2796 type1 = pointed_type(dt);
2797 /* a function is implicitly a function pointer */
2798 if (sbt == VT_FUNC) {
2799 if ((type1->t & VT_BTYPE) != VT_VOID &&
2800 !is_compatible_types(pointed_type(dt), st))
2801 tcc_warning("assignment from incompatible pointer type");
2802 goto type_ok;
2804 if (sbt != VT_PTR)
2805 goto error;
2806 type2 = pointed_type(st);
2807 if ((type1->t & VT_BTYPE) == VT_VOID ||
2808 (type2->t & VT_BTYPE) == VT_VOID) {
2809 /* void * can match anything */
2810 } else {
2811 /* exact type match, except for qualifiers */
2812 tmp_type1 = *type1;
2813 tmp_type2 = *type2;
2814 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2815 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2816 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2817 /* Like GCC don't warn by default for merely changes
2818 in pointer target signedness. Do warn for different
2819 base types, though, in particular for unsigned enums
2820 and signed int targets. */
2821 if ((tmp_type1.t & (VT_DEFSIGN | VT_UNSIGNED)) !=
2822 (tmp_type2.t & (VT_DEFSIGN | VT_UNSIGNED)) &&
2823 (tmp_type1.t & VT_BTYPE) == (tmp_type2.t & VT_BTYPE))
2825 else
2826 tcc_warning("assignment from incompatible pointer type");
2829 /* check const and volatile */
2830 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2831 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2832 tcc_warning("assignment discards qualifiers from pointer target type");
2833 break;
2834 case VT_BYTE:
2835 case VT_SHORT:
2836 case VT_INT:
2837 case VT_LLONG:
2838 if (sbt == VT_PTR || sbt == VT_FUNC) {
2839 tcc_warning("assignment makes integer from pointer without a cast");
2840 } else if (sbt == VT_STRUCT) {
2841 goto case_VT_STRUCT;
2843 /* XXX: more tests */
2844 break;
2845 case VT_STRUCT:
2846 case_VT_STRUCT:
2847 tmp_type1 = *dt;
2848 tmp_type2 = *st;
2849 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2850 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2851 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2852 error:
2853 type_to_str(buf1, sizeof(buf1), st, NULL);
2854 type_to_str(buf2, sizeof(buf2), dt, NULL);
2855 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2857 break;
2859 type_ok:
2860 gen_cast(dt);
2863 /* store vtop in lvalue pushed on stack */
2864 ST_FUNC void vstore(void)
2866 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2868 ft = vtop[-1].type.t;
2869 sbt = vtop->type.t & VT_BTYPE;
2870 dbt = ft & VT_BTYPE;
2871 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2872 (sbt == VT_INT && dbt == VT_SHORT))
2873 && !(vtop->type.t & VT_BITFIELD)) {
2874 /* optimize char/short casts */
2875 delayed_cast = VT_MUSTCAST;
2876 vtop->type.t = (ft & VT_TYPE & ~VT_BITFIELD &
2877 ((1 << VT_STRUCT_SHIFT) - 1));
2878 /* XXX: factorize */
2879 if (ft & VT_CONSTANT)
2880 tcc_warning("assignment of read-only location");
2881 } else {
2882 delayed_cast = 0;
2883 if (!(ft & VT_BITFIELD))
2884 gen_assign_cast(&vtop[-1].type);
2887 if (sbt == VT_STRUCT) {
2888 /* if structure, only generate pointer */
2889 /* structure assignment : generate memcpy */
2890 /* XXX: optimize if small size */
2891 size = type_size(&vtop->type, &align);
2893 /* destination */
2894 vswap();
2895 vtop->type.t = VT_PTR;
2896 gaddrof();
2898 /* address of memcpy() */
2899 #ifdef TCC_ARM_EABI
2900 if(!(align & 7))
2901 vpush_global_sym(&func_old_type, TOK_memcpy8);
2902 else if(!(align & 3))
2903 vpush_global_sym(&func_old_type, TOK_memcpy4);
2904 else
2905 #endif
2906 /* Use memmove, rather than memcpy, as dest and src may be same: */
2907 vpush_global_sym(&func_old_type, TOK_memmove);
2909 vswap();
2910 /* source */
2911 vpushv(vtop - 2);
2912 vtop->type.t = VT_PTR;
2913 gaddrof();
2914 /* type size */
2915 vpushi(size);
2916 gfunc_call(3);
2918 /* leave source on stack */
2919 } else if (ft & VT_BITFIELD) {
2920 /* bitfield store handling */
2922 /* save lvalue as expression result (example: s.b = s.a = n;) */
2923 vdup(), vtop[-1] = vtop[-2];
2925 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2926 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2927 /* remove bit field info to avoid loops */
2928 vtop[-1].type.t = ft & ~VT_BITFIELD & ((1 << VT_STRUCT_SHIFT) - 1);
2930 if((ft & VT_BTYPE) == VT_BOOL) {
2931 gen_cast(&vtop[-1].type);
2932 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2935 /* duplicate destination */
2936 vdup();
2937 vtop[-1] = vtop[-2];
2939 /* mask and shift source */
2940 if((ft & VT_BTYPE) != VT_BOOL) {
2941 if((ft & VT_BTYPE) == VT_LLONG) {
2942 vpushll((1ULL << bit_size) - 1ULL);
2943 } else {
2944 vpushi((1 << bit_size) - 1);
2946 gen_op('&');
2948 vpushi(bit_pos);
2949 gen_op(TOK_SHL);
2950 /* load destination, mask and or with source */
2951 vswap();
2952 if((ft & VT_BTYPE) == VT_LLONG) {
2953 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2954 } else {
2955 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2957 gen_op('&');
2958 gen_op('|');
2959 /* store result */
2960 vstore();
2961 /* ... and discard */
2962 vpop();
2964 } else {
2965 #ifdef CONFIG_TCC_BCHECK
2966 /* bound check case */
2967 if (vtop[-1].r & VT_MUSTBOUND) {
2968 vswap();
2969 gbound();
2970 vswap();
2972 #endif
2973 rc = RC_INT;
2974 if (is_float(ft)) {
2975 rc = RC_FLOAT;
2976 #ifdef TCC_TARGET_X86_64
2977 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2978 rc = RC_ST0;
2979 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2980 rc = RC_FRET;
2982 #endif
2984 r = gv(rc); /* generate value */
2985 /* if lvalue was saved on stack, must read it */
2986 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2987 SValue sv;
2988 t = get_reg(RC_INT);
2989 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
2990 sv.type.t = VT_PTR;
2991 #else
2992 sv.type.t = VT_INT;
2993 #endif
2994 sv.r = VT_LOCAL | VT_LVAL;
2995 sv.c.i = vtop[-1].c.i;
2996 load(t, &sv);
2997 vtop[-1].r = t | VT_LVAL;
2999 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
3000 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3001 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
3002 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
3003 #else
3004 if ((ft & VT_BTYPE) == VT_LLONG) {
3005 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
3006 #endif
3007 vtop[-1].type.t = load_type;
3008 store(r, vtop - 1);
3009 vswap();
3010 /* convert to int to increment easily */
3011 vtop->type.t = addr_type;
3012 gaddrof();
3013 vpushi(load_size);
3014 gen_op('+');
3015 vtop->r |= VT_LVAL;
3016 vswap();
3017 vtop[-1].type.t = load_type;
3018 /* XXX: it works because r2 is spilled last ! */
3019 store(vtop->r2, vtop - 1);
3020 } else {
3021 store(r, vtop - 1);
3024 vswap();
3025 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3026 vtop->r |= delayed_cast;
3030 /* post defines POST/PRE add. c is the token ++ or -- */
3031 ST_FUNC void inc(int post, int c)
3033 test_lvalue();
3034 vdup(); /* save lvalue */
3035 if (post) {
3036 gv_dup(); /* duplicate value */
3037 vrotb(3);
3038 vrotb(3);
3040 /* add constant */
3041 vpushi(c - TOK_MID);
3042 gen_op('+');
3043 vstore(); /* store value */
3044 if (post)
3045 vpop(); /* if post op, return saved value */
3048 ST_FUNC void parse_mult_str (CString *astr, const char *msg)
3050 /* read the string */
3051 if (tok != TOK_STR)
3052 expect(msg);
3053 cstr_new(astr);
3054 while (tok == TOK_STR) {
3055 /* XXX: add \0 handling too ? */
3056 cstr_cat(astr, tokc.str.data, -1);
3057 next();
3059 cstr_ccat(astr, '\0');
3062 /* If I is >= 1 and a power of two, returns log2(i)+1.
3063 If I is 0 returns 0. */
3064 static int exact_log2p1(int i)
3066 int ret;
3067 if (!i)
3068 return 0;
3069 for (ret = 1; i >= 1 << 8; ret += 8)
3070 i >>= 8;
3071 if (i >= 1 << 4)
3072 ret += 4, i >>= 4;
3073 if (i >= 1 << 2)
3074 ret += 2, i >>= 2;
3075 if (i >= 1 << 1)
3076 ret++;
3077 return ret;
3080 /* Parse GNUC __attribute__ extension. Currently, the following
3081 extensions are recognized:
3082 - aligned(n) : set data/function alignment.
3083 - packed : force data alignment to 1
3084 - section(x) : generate data/code in this section.
3085 - unused : currently ignored, but may be used someday.
3086 - regparm(n) : pass function parameters in registers (i386 only)
3088 static void parse_attribute(AttributeDef *ad)
3090 int t, n;
3091 CString astr;
3093 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
3094 next();
3095 skip('(');
3096 skip('(');
3097 while (tok != ')') {
3098 if (tok < TOK_IDENT)
3099 expect("attribute name");
3100 t = tok;
3101 next();
3102 switch(t) {
3103 case TOK_SECTION1:
3104 case TOK_SECTION2:
3105 skip('(');
3106 parse_mult_str(&astr, "section name");
3107 ad->section = find_section(tcc_state, (char *)astr.data);
3108 skip(')');
3109 cstr_free(&astr);
3110 break;
3111 case TOK_ALIAS1:
3112 case TOK_ALIAS2:
3113 skip('(');
3114 parse_mult_str(&astr, "alias(\"target\")");
3115 ad->alias_target = /* save string as token, for later */
3116 tok_alloc((char*)astr.data, astr.size-1)->tok;
3117 skip(')');
3118 cstr_free(&astr);
3119 break;
3120 case TOK_VISIBILITY1:
3121 case TOK_VISIBILITY2:
3122 skip('(');
3123 parse_mult_str(&astr,
3124 "visibility(\"default|hidden|internal|protected\")");
3125 if (!strcmp (astr.data, "default"))
3126 ad->a.visibility = STV_DEFAULT;
3127 else if (!strcmp (astr.data, "hidden"))
3128 ad->a.visibility = STV_HIDDEN;
3129 else if (!strcmp (astr.data, "internal"))
3130 ad->a.visibility = STV_INTERNAL;
3131 else if (!strcmp (astr.data, "protected"))
3132 ad->a.visibility = STV_PROTECTED;
3133 else
3134 expect("visibility(\"default|hidden|internal|protected\")");
3135 skip(')');
3136 cstr_free(&astr);
3137 break;
3138 case TOK_ALIGNED1:
3139 case TOK_ALIGNED2:
3140 if (tok == '(') {
3141 next();
3142 n = expr_const();
3143 if (n <= 0 || (n & (n - 1)) != 0)
3144 tcc_error("alignment must be a positive power of two");
3145 skip(')');
3146 } else {
3147 n = MAX_ALIGN;
3149 ad->a.aligned = exact_log2p1(n);
3150 if (n != 1 << (ad->a.aligned - 1))
3151 tcc_error("alignment of %d is larger than implemented", n);
3152 break;
3153 case TOK_PACKED1:
3154 case TOK_PACKED2:
3155 ad->a.packed = 1;
3156 break;
3157 case TOK_WEAK1:
3158 case TOK_WEAK2:
3159 ad->a.weak = 1;
3160 break;
3161 case TOK_UNUSED1:
3162 case TOK_UNUSED2:
3163 /* currently, no need to handle it because tcc does not
3164 track unused objects */
3165 break;
3166 case TOK_NORETURN1:
3167 case TOK_NORETURN2:
3168 /* currently, no need to handle it because tcc does not
3169 track unused objects */
3170 break;
3171 case TOK_CDECL1:
3172 case TOK_CDECL2:
3173 case TOK_CDECL3:
3174 ad->a.func_call = FUNC_CDECL;
3175 break;
3176 case TOK_STDCALL1:
3177 case TOK_STDCALL2:
3178 case TOK_STDCALL3:
3179 ad->a.func_call = FUNC_STDCALL;
3180 break;
3181 #ifdef TCC_TARGET_I386
3182 case TOK_REGPARM1:
3183 case TOK_REGPARM2:
3184 skip('(');
3185 n = expr_const();
3186 if (n > 3)
3187 n = 3;
3188 else if (n < 0)
3189 n = 0;
3190 if (n > 0)
3191 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
3192 skip(')');
3193 break;
3194 case TOK_FASTCALL1:
3195 case TOK_FASTCALL2:
3196 case TOK_FASTCALL3:
3197 ad->a.func_call = FUNC_FASTCALLW;
3198 break;
3199 #endif
3200 case TOK_MODE:
3201 skip('(');
3202 switch(tok) {
3203 case TOK_MODE_DI:
3204 ad->a.mode = VT_LLONG + 1;
3205 break;
3206 case TOK_MODE_QI:
3207 ad->a.mode = VT_BYTE + 1;
3208 break;
3209 case TOK_MODE_HI:
3210 ad->a.mode = VT_SHORT + 1;
3211 break;
3212 case TOK_MODE_SI:
3213 case TOK_MODE_word:
3214 ad->a.mode = VT_INT + 1;
3215 break;
3216 default:
3217 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
3218 break;
3220 next();
3221 skip(')');
3222 break;
3223 case TOK_DLLEXPORT:
3224 ad->a.func_export = 1;
3225 break;
3226 case TOK_DLLIMPORT:
3227 ad->a.func_import = 1;
3228 break;
3229 default:
3230 if (tcc_state->warn_unsupported)
3231 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
3232 /* skip parameters */
3233 if (tok == '(') {
3234 int parenthesis = 0;
3235 do {
3236 if (tok == '(')
3237 parenthesis++;
3238 else if (tok == ')')
3239 parenthesis--;
3240 next();
3241 } while (parenthesis && tok != -1);
3243 break;
3245 if (tok != ',')
3246 break;
3247 next();
3249 skip(')');
3250 skip(')');
3254 static Sym * find_field (CType *type, int v)
3256 Sym *s = type->ref;
3257 v |= SYM_FIELD;
3258 while ((s = s->next) != NULL) {
3259 if ((s->v & SYM_FIELD) &&
3260 (s->type.t & VT_BTYPE) == VT_STRUCT &&
3261 (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
3262 Sym *ret = find_field (&s->type, v);
3263 if (ret)
3264 return ret;
3266 if (s->v == v)
3267 break;
3269 return s;
3272 static void struct_add_offset (Sym *s, int offset)
3274 while ((s = s->next) != NULL) {
3275 if ((s->v & SYM_FIELD) &&
3276 (s->type.t & VT_BTYPE) == VT_STRUCT &&
3277 (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
3278 struct_add_offset(s->type.ref, offset);
3279 } else
3280 s->c += offset;
3284 static void struct_layout(CType *type, AttributeDef *ad)
3286 int align, maxalign, offset, c, bit_pos, bt, prevbt, prev_bit_size;
3287 int pcc = !tcc_state->ms_bitfields;
3288 int packwarn = tcc_state->warn_gcc_compat;
3289 int typealign, bit_size, size;
3291 Sym *f;
3292 if (ad->a.aligned)
3293 maxalign = 1 << (ad->a.aligned - 1);
3294 else
3295 maxalign = 1;
3296 offset = 0;
3297 c = 0;
3298 bit_pos = 0;
3299 prevbt = VT_STRUCT; /* make it never match */
3300 prev_bit_size = 0;
3301 size = 0;
3303 for (f = type->ref->next; f; f = f->next) {
3304 size = type_size(&f->type, &typealign);
3305 if (f->type.t & VT_BITFIELD)
3306 bit_size = (f->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
3307 else
3308 bit_size = -1;
3309 if (bit_size == 0 && pcc) {
3310 /* Zero-width bit-fields in PCC mode aren't affected
3311 by any packing (attribute or pragma). */
3312 align = typealign;
3313 } else if (f->r > 1) {
3314 align = f->r;
3315 } else if (ad->a.packed || f->r == 1) {
3316 align = 1;
3317 /* Packed fields or packed records don't let the base type
3318 influence the records type alignment. */
3319 typealign = 1;
3320 } else {
3321 align = typealign;
3323 if (type->ref->type.t != TOK_STRUCT) {
3324 if (pcc && bit_size >= 0)
3325 size = (bit_size + 7) >> 3;
3326 /* Bit position is already zero from our caller. */
3327 offset = 0;
3328 if (size > c)
3329 c = size;
3330 } else if (bit_size < 0) {
3331 int addbytes = pcc ? (bit_pos + 7) >> 3 : 0;
3332 prevbt = VT_STRUCT;
3333 prev_bit_size = 0;
3334 c = (c + addbytes + align - 1) & -align;
3335 offset = c;
3336 if (size > 0)
3337 c += size;
3338 bit_pos = 0;
3339 } else {
3340 /* A bit-field. Layout is more complicated. There are two
3341 options TCC implements: PCC compatible and MS compatible
3342 (PCC compatible is what GCC uses for almost all targets).
3343 In PCC layout the overall size of the struct (in c) is
3344 _excluding_ the current run of bit-fields (that is,
3345 there's at least additional bit_pos bits after c). In
3346 MS layout c does include the current run of bit-fields.
3348 This matters for calculating the natural alignment buckets
3349 in PCC mode. */
3351 /* 'align' will be used to influence records alignment,
3352 so it's the max of specified and type alignment, except
3353 in certain cases that depend on the mode. */
3354 if (align < typealign)
3355 align = typealign;
3356 if (pcc) {
3357 /* In PCC layout a non-packed bit-field is placed adjacent
3358 to the preceding bit-fields, except if it would overflow
3359 its container (depending on base type) or it's a zero-width
3360 bit-field. Packed non-zero-width bit-fields always are
3361 placed adjacent. */
3362 int ofs = (c * 8 + bit_pos) % (typealign * 8);
3363 int ofs2 = ofs + bit_size + (typealign * 8) - 1;
3364 if (bit_size == 0 ||
3365 (typealign != 1 &&
3366 (ofs2 / (typealign * 8)) > (size/typealign))) {
3367 c = (c + ((bit_pos + 7) >> 3) + typealign - 1) & -typealign;
3368 bit_pos = 0;
3369 } else if (bit_pos + bit_size > size * 8) {
3370 c += bit_pos >> 3;
3371 bit_pos &= 7;
3372 if (bit_pos + bit_size > size * 8) {
3373 c += 1, bit_pos = 0;
3374 if ((ad->a.packed || f->r) && packwarn) {
3375 tcc_warning("struct layout not compatible with GCC (internal limitation)");
3376 packwarn = 0;
3380 offset = c;
3381 /* In PCC layout named bit-fields influence the alignment
3382 of the containing struct using the base types alignment,
3383 except for packed fields (which here have correct
3384 align/typealign). */
3385 if ((f->v & SYM_FIRST_ANOM))
3386 align = 1;
3387 } else {
3388 bt = f->type.t & VT_BTYPE;
3389 if ((bit_pos + bit_size > size * 8) ||
3390 (bit_size > 0) == (bt != prevbt)) {
3391 c = (c + typealign - 1) & -typealign;
3392 offset = c;
3393 bit_pos = 0;
3394 /* In MS bitfield mode a bit-field run always uses
3395 at least as many bits as the underlying type.
3396 To start a new run it's also required that this
3397 or the last bit-field had non-zero width. */
3398 if (bit_size || prev_bit_size)
3399 c += size;
3401 /* In MS layout the records alignment is normally
3402 influenced by the field, except for a zero-width
3403 field at the start of a run (but by further zero-width
3404 fields it is again). */
3405 if (bit_size == 0 && prevbt != bt)
3406 align = 1;
3407 prevbt = bt;
3408 prev_bit_size = bit_size;
3410 f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
3411 | (bit_pos << VT_STRUCT_SHIFT);
3412 bit_pos += bit_size;
3413 if (pcc && bit_pos >= size * 8) {
3414 c += size;
3415 bit_pos -= size * 8;
3418 if (align > maxalign)
3419 maxalign = align;
3420 #if 0
3421 printf("set field %s offset=%d",
3422 get_tok_str(f->v & ~SYM_FIELD, NULL), offset);
3423 if (f->type.t & VT_BITFIELD) {
3424 printf(" pos=%d size=%d",
3425 (f->type.t >> VT_STRUCT_SHIFT) & 0x3f,
3426 (f->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
3428 printf("\n");
3429 #endif
3431 if (f->v & SYM_FIRST_ANOM && (f->type.t & VT_BTYPE) == VT_STRUCT) {
3432 Sym *ass;
3433 /* An anonymous struct/union. Adjust member offsets
3434 to reflect the real offset of our containing struct.
3435 Also set the offset of this anon member inside
3436 the outer struct to be zero. Via this it
3437 works when accessing the field offset directly
3438 (from base object), as well as when recursing
3439 members in initializer handling. */
3440 int v2 = f->type.ref->v;
3441 if (!(v2 & SYM_FIELD) &&
3442 (v2 & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
3443 Sym **pps;
3444 /* This happens only with MS extensions. The
3445 anon member has a named struct type, so it
3446 potentially is shared with other references.
3447 We need to unshare members so we can modify
3448 them. */
3449 ass = f->type.ref;
3450 f->type.ref = sym_push(anon_sym++ | SYM_FIELD,
3451 &f->type.ref->type, 0,
3452 f->type.ref->c);
3453 pps = &f->type.ref->next;
3454 while ((ass = ass->next) != NULL) {
3455 *pps = sym_push(ass->v, &ass->type, 0, ass->c);
3456 pps = &((*pps)->next);
3458 *pps = NULL;
3460 struct_add_offset(f->type.ref, offset);
3461 f->c = 0;
3462 } else {
3463 f->c = offset;
3466 f->r = 0;
3468 /* store size and alignment */
3469 type->ref->c = (c + (pcc ? (bit_pos + 7) >> 3 : 0)
3470 + maxalign - 1) & -maxalign;
3471 type->ref->r = maxalign;
3472 if (offset + size > type->ref->c)
3473 tcc_warning("will touch memory past end of the struct (internal limitation)");
3476 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3477 static void struct_decl(CType *type, AttributeDef *ad, int u)
3479 int a, v, size, align, flexible, alignoverride;
3480 long c;
3481 int bit_size, bsize, bt;
3482 Sym *s, *ss, **ps;
3483 AttributeDef ad1;
3484 CType type1, btype;
3486 a = tok; /* save decl type */
3487 next();
3488 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3489 parse_attribute(ad);
3490 if (tok != '{') {
3491 v = tok;
3492 next();
3493 /* struct already defined ? return it */
3494 if (v < TOK_IDENT)
3495 expect("struct/union/enum name");
3496 s = struct_find(v);
3497 if (s && (s->scope == local_scope || (tok != '{' && tok != ';'))) {
3498 if (s->type.t != a)
3499 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
3500 goto do_decl;
3502 } else {
3503 v = anon_sym++;
3505 /* Record the original enum/struct/union token. */
3506 type1.t = a;
3507 type1.ref = NULL;
3508 /* we put an undefined size for struct/union */
3509 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
3510 s->r = 0; /* default alignment is zero as gcc */
3511 /* put struct/union/enum name in type */
3512 do_decl:
3513 type->t = u;
3514 type->ref = s;
3516 if (tok == '{') {
3517 next();
3518 if (s->c != -1)
3519 tcc_error("struct/union/enum already defined");
3520 /* cannot be empty */
3521 c = 0;
3522 /* non empty enums are not allowed */
3523 if (a == TOK_ENUM) {
3524 int seen_neg = 0;
3525 int seen_wide = 0;
3526 for(;;) {
3527 CType *t = &int_type;
3528 v = tok;
3529 if (v < TOK_UIDENT)
3530 expect("identifier");
3531 ss = sym_find(v);
3532 if (ss && !local_stack)
3533 tcc_error("redefinition of enumerator '%s'",
3534 get_tok_str(v, NULL));
3535 next();
3536 if (tok == '=') {
3537 next();
3538 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
3539 c = expr_const64();
3540 #else
3541 /* We really want to support long long enums
3542 on i386 as well, but the Sym structure only
3543 holds a 'long' for associated constants,
3544 and enlarging it would bump its size (no
3545 available padding). So punt for now. */
3546 c = expr_const();
3547 #endif
3549 if (c < 0)
3550 seen_neg = 1;
3551 if (c != (int)c && (unsigned long)c != (unsigned int)c)
3552 seen_wide = 1, t = &size_type;
3553 /* enum symbols have static storage */
3554 ss = sym_push(v, t, VT_CONST, c);
3555 ss->type.t |= VT_STATIC;
3556 if (tok != ',')
3557 break;
3558 next();
3559 c++;
3560 /* NOTE: we accept a trailing comma */
3561 if (tok == '}')
3562 break;
3564 if (!seen_neg)
3565 s->a.unsigned_enum = 1;
3566 s->c = type_size(seen_wide ? &size_type : &int_type, &align);
3567 skip('}');
3568 } else {
3569 ps = &s->next;
3570 flexible = 0;
3571 while (tok != '}') {
3572 if (!parse_btype(&btype, &ad1)) {
3573 skip(';');
3574 continue;
3576 while (1) {
3577 if (flexible)
3578 tcc_error("flexible array member '%s' not at the end of struct",
3579 get_tok_str(v, NULL));
3580 bit_size = -1;
3581 v = 0;
3582 type1 = btype;
3583 if (tok != ':') {
3584 if (tok != ';')
3585 type_decl(&type1, &ad1, &v, TYPE_DIRECT);
3586 if (v == 0) {
3587 if ((type1.t & VT_BTYPE) != VT_STRUCT)
3588 expect("identifier");
3589 else {
3590 int v = btype.ref->v;
3591 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
3592 if (tcc_state->ms_extensions == 0)
3593 expect("identifier");
3597 if (type_size(&type1, &align) < 0) {
3598 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
3599 flexible = 1;
3600 else
3601 tcc_error("field '%s' has incomplete type",
3602 get_tok_str(v, NULL));
3604 if ((type1.t & VT_BTYPE) == VT_FUNC ||
3605 (type1.t & VT_STORAGE))
3606 tcc_error("invalid type for '%s'",
3607 get_tok_str(v, NULL));
3609 if (tok == ':') {
3610 next();
3611 bit_size = expr_const();
3612 /* XXX: handle v = 0 case for messages */
3613 if (bit_size < 0)
3614 tcc_error("negative width in bit-field '%s'",
3615 get_tok_str(v, NULL));
3616 if (v && bit_size == 0)
3617 tcc_error("zero width for bit-field '%s'",
3618 get_tok_str(v, NULL));
3619 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3620 parse_attribute(&ad1);
3622 size = type_size(&type1, &align);
3623 /* Only remember non-default alignment. */
3624 alignoverride = 0;
3625 if (ad1.a.aligned) {
3626 int speca = 1 << (ad1.a.aligned - 1);
3627 alignoverride = speca;
3628 } else if (ad1.a.packed || ad->a.packed) {
3629 alignoverride = 1;
3630 } else if (*tcc_state->pack_stack_ptr) {
3631 if (align >= *tcc_state->pack_stack_ptr)
3632 alignoverride = *tcc_state->pack_stack_ptr;
3634 if (bit_size >= 0) {
3635 bt = type1.t & VT_BTYPE;
3636 if (bt != VT_INT &&
3637 bt != VT_BYTE &&
3638 bt != VT_SHORT &&
3639 bt != VT_BOOL &&
3640 bt != VT_ENUM &&
3641 bt != VT_LLONG)
3642 tcc_error("bitfields must have scalar type");
3643 bsize = size * 8;
3644 if (bit_size > bsize) {
3645 tcc_error("width of '%s' exceeds its type",
3646 get_tok_str(v, NULL));
3647 } else if (bit_size == bsize) {
3648 /* no need for bit fields */
3650 } else {
3651 type1.t |= VT_BITFIELD |
3652 (0 << VT_STRUCT_SHIFT) |
3653 (bit_size << (VT_STRUCT_SHIFT + 6));
3656 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
3657 /* Remember we've seen a real field to check
3658 for placement of flexible array member. */
3659 c = 1;
3661 /* If member is a struct or bit-field, enforce
3662 placing into the struct (as anonymous). */
3663 if (v == 0 &&
3664 ((type1.t & VT_BTYPE) == VT_STRUCT ||
3665 bit_size >= 0)) {
3666 v = anon_sym++;
3668 if (v) {
3669 ss = sym_push(v | SYM_FIELD, &type1, alignoverride, 0);
3670 *ps = ss;
3671 ps = &ss->next;
3673 if (tok == ';' || tok == TOK_EOF)
3674 break;
3675 skip(',');
3677 skip(';');
3679 skip('}');
3680 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3681 parse_attribute(ad);
3682 struct_layout(type, ad);
3687 /* return 1 if basic type is a type size (short, long, long long) */
3688 ST_FUNC int is_btype_size(int bt)
3690 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
3693 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3694 are added to the element type, copied because it could be a typedef. */
3695 static void parse_btype_qualify(CType *type, int qualifiers)
3697 while (type->t & VT_ARRAY) {
3698 type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
3699 type = &type->ref->type;
3701 type->t |= qualifiers;
3704 /* return 0 if no type declaration. otherwise, return the basic type
3705 and skip it.
3707 static int parse_btype(CType *type, AttributeDef *ad)
3709 int t, u, bt_size, complete, type_found, typespec_found, g;
3710 Sym *s;
3711 CType type1;
3713 memset(ad, 0, sizeof(AttributeDef));
3714 complete = 0;
3715 type_found = 0;
3716 typespec_found = 0;
3717 t = 0;
3718 while(1) {
3719 switch(tok) {
3720 case TOK_EXTENSION:
3721 /* currently, we really ignore extension */
3722 next();
3723 continue;
3725 /* basic types */
3726 case TOK_CHAR:
3727 u = VT_BYTE;
3728 basic_type:
3729 next();
3730 basic_type1:
3731 if (complete)
3732 tcc_error("too many basic types");
3733 t |= u;
3734 bt_size = is_btype_size (u & VT_BTYPE);
3735 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3736 complete = 1;
3737 typespec_found = 1;
3738 break;
3739 case TOK_VOID:
3740 u = VT_VOID;
3741 goto basic_type;
3742 case TOK_SHORT:
3743 u = VT_SHORT;
3744 goto basic_type;
3745 case TOK_INT:
3746 u = VT_INT;
3747 goto basic_type;
3748 case TOK_LONG:
3749 next();
3750 if ((t & VT_BTYPE) == VT_DOUBLE) {
3751 #ifndef TCC_TARGET_PE
3752 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3753 #endif
3754 } else if ((t & VT_BTYPE) == VT_LONG) {
3755 t = (t & ~VT_BTYPE) | VT_LLONG;
3756 } else {
3757 u = VT_LONG;
3758 goto basic_type1;
3760 break;
3761 #ifdef TCC_TARGET_ARM64
3762 case TOK_UINT128:
3763 /* GCC's __uint128_t appears in some Linux header files. Make it a
3764 synonym for long double to get the size and alignment right. */
3765 u = VT_LDOUBLE;
3766 goto basic_type;
3767 #endif
3768 case TOK_BOOL:
3769 u = VT_BOOL;
3770 goto basic_type;
3771 case TOK_FLOAT:
3772 u = VT_FLOAT;
3773 goto basic_type;
3774 case TOK_DOUBLE:
3775 next();
3776 if ((t & VT_BTYPE) == VT_LONG) {
3777 #ifdef TCC_TARGET_PE
3778 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3779 #else
3780 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3781 #endif
3782 } else {
3783 u = VT_DOUBLE;
3784 goto basic_type1;
3786 break;
3787 case TOK_ENUM:
3788 struct_decl(&type1, ad, VT_ENUM);
3789 basic_type2:
3790 u = type1.t;
3791 type->ref = type1.ref;
3792 goto basic_type1;
3793 case TOK_STRUCT:
3794 case TOK_UNION:
3795 struct_decl(&type1, ad, VT_STRUCT);
3796 goto basic_type2;
3798 /* type modifiers */
3799 case TOK_CONST1:
3800 case TOK_CONST2:
3801 case TOK_CONST3:
3802 type->t = t;
3803 parse_btype_qualify(type, VT_CONSTANT);
3804 t = type->t;
3805 next();
3806 break;
3807 case TOK_VOLATILE1:
3808 case TOK_VOLATILE2:
3809 case TOK_VOLATILE3:
3810 type->t = t;
3811 parse_btype_qualify(type, VT_VOLATILE);
3812 t = type->t;
3813 next();
3814 break;
3815 case TOK_SIGNED1:
3816 case TOK_SIGNED2:
3817 case TOK_SIGNED3:
3818 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3819 tcc_error("signed and unsigned modifier");
3820 typespec_found = 1;
3821 t |= VT_DEFSIGN;
3822 next();
3823 break;
3824 case TOK_REGISTER:
3825 case TOK_AUTO:
3826 case TOK_RESTRICT1:
3827 case TOK_RESTRICT2:
3828 case TOK_RESTRICT3:
3829 next();
3830 break;
3831 case TOK_UNSIGNED:
3832 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3833 tcc_error("signed and unsigned modifier");
3834 t |= VT_DEFSIGN | VT_UNSIGNED;
3835 next();
3836 typespec_found = 1;
3837 break;
3839 /* storage */
3840 case TOK_EXTERN:
3841 g = VT_EXTERN;
3842 goto storage;
3843 case TOK_STATIC:
3844 g = VT_STATIC;
3845 goto storage;
3846 case TOK_TYPEDEF:
3847 g = VT_TYPEDEF;
3848 goto storage;
3849 storage:
3850 if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g)
3851 tcc_error("multiple storage classes");
3852 t |= g;
3853 next();
3854 break;
3855 case TOK_INLINE1:
3856 case TOK_INLINE2:
3857 case TOK_INLINE3:
3858 t |= VT_INLINE;
3859 next();
3860 break;
3862 /* GNUC attribute */
3863 case TOK_ATTRIBUTE1:
3864 case TOK_ATTRIBUTE2:
3865 parse_attribute(ad);
3866 if (ad->a.mode) {
3867 u = ad->a.mode -1;
3868 t = (t & ~VT_BTYPE) | u;
3870 break;
3871 /* GNUC typeof */
3872 case TOK_TYPEOF1:
3873 case TOK_TYPEOF2:
3874 case TOK_TYPEOF3:
3875 next();
3876 parse_expr_type(&type1);
3877 /* remove all storage modifiers except typedef */
3878 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3879 goto basic_type2;
3880 default:
3881 if (typespec_found)
3882 goto the_end;
3883 s = sym_find(tok);
3884 if (!s || !(s->type.t & VT_TYPEDEF))
3885 goto the_end;
3887 type->t = ((s->type.t & ~VT_TYPEDEF) |
3888 (t & ~(VT_CONSTANT | VT_VOLATILE)));
3889 type->ref = s->type.ref;
3890 if (t & (VT_CONSTANT | VT_VOLATILE))
3891 parse_btype_qualify(type, t & (VT_CONSTANT | VT_VOLATILE));
3892 t = type->t;
3894 if (s->r) {
3895 /* get attributes from typedef */
3896 if (0 == ad->a.aligned)
3897 ad->a.aligned = s->a.aligned;
3898 if (0 == ad->a.func_call)
3899 ad->a.func_call = s->a.func_call;
3900 ad->a.packed |= s->a.packed;
3902 next();
3903 typespec_found = 1;
3904 break;
3906 type_found = 1;
3908 the_end:
3909 if (tcc_state->char_is_unsigned) {
3910 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3911 t |= VT_UNSIGNED;
3914 /* long is never used as type */
3915 if ((t & VT_BTYPE) == VT_LONG)
3916 #if (!defined TCC_TARGET_X86_64 && !defined TCC_TARGET_ARM64) || \
3917 defined TCC_TARGET_PE
3918 t = (t & ~VT_BTYPE) | VT_INT;
3919 #else
3920 t = (t & ~VT_BTYPE) | VT_LLONG;
3921 #endif
3922 type->t = t;
3923 return type_found;
3926 /* convert a function parameter type (array to pointer and function to
3927 function pointer) */
3928 static inline void convert_parameter_type(CType *pt)
3930 /* remove const and volatile qualifiers (XXX: const could be used
3931 to indicate a const function parameter */
3932 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3933 /* array must be transformed to pointer according to ANSI C */
3934 pt->t &= ~VT_ARRAY;
3935 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3936 mk_pointer(pt);
3940 ST_FUNC void parse_asm_str(CString *astr)
3942 skip('(');
3943 parse_mult_str(astr, "string constant");
3946 /* Parse an asm label and return the token */
3947 static int asm_label_instr(void)
3949 int v;
3950 CString astr;
3952 next();
3953 parse_asm_str(&astr);
3954 skip(')');
3955 #ifdef ASM_DEBUG
3956 printf("asm_alias: \"%s\"\n", (char *)astr.data);
3957 #endif
3958 v = tok_alloc(astr.data, astr.size - 1)->tok;
3959 cstr_free(&astr);
3960 return v;
3963 static int post_type(CType *type, AttributeDef *ad, int storage, int td)
3965 int n, l, t1, arg_size, align;
3966 Sym **plast, *s, *first;
3967 AttributeDef ad1;
3968 CType pt;
3970 if (tok == '(') {
3971 /* function type, or recursive declarator (return if so) */
3972 next();
3973 if (td && !(td & TYPE_ABSTRACT))
3974 return 0;
3975 if (tok == ')')
3976 l = 0;
3977 else if (parse_btype(&pt, &ad1))
3978 l = FUNC_NEW;
3979 else if (td)
3980 return 0;
3981 else
3982 l = FUNC_OLD;
3983 first = NULL;
3984 plast = &first;
3985 arg_size = 0;
3986 if (l) {
3987 for(;;) {
3988 /* read param name and compute offset */
3989 if (l != FUNC_OLD) {
3990 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3991 break;
3992 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3993 if ((pt.t & VT_BTYPE) == VT_VOID)
3994 tcc_error("parameter declared as void");
3995 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3996 } else {
3997 n = tok;
3998 if (n < TOK_UIDENT)
3999 expect("identifier");
4000 pt.t = VT_VOID; /* invalid type */
4001 next();
4003 convert_parameter_type(&pt);
4004 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
4005 *plast = s;
4006 plast = &s->next;
4007 if (tok == ')')
4008 break;
4009 skip(',');
4010 if (l == FUNC_NEW && tok == TOK_DOTS) {
4011 l = FUNC_ELLIPSIS;
4012 next();
4013 break;
4015 if (l == FUNC_NEW && !parse_btype(&pt, &ad1))
4016 tcc_error("invalid type");
4018 } else
4019 /* if no parameters, then old type prototype */
4020 l = FUNC_OLD;
4021 skip(')');
4022 /* NOTE: const is ignored in returned type as it has a special
4023 meaning in gcc / C++ */
4024 type->t &= ~VT_CONSTANT;
4025 /* some ancient pre-K&R C allows a function to return an array
4026 and the array brackets to be put after the arguments, such
4027 that "int c()[]" means something like "int[] c()" */
4028 if (tok == '[') {
4029 next();
4030 skip(']'); /* only handle simple "[]" */
4031 mk_pointer(type);
4033 /* we push a anonymous symbol which will contain the function prototype */
4034 ad->a.func_args = arg_size;
4035 s = sym_push(SYM_FIELD, type, 0, l);
4036 s->a = ad->a;
4037 s->next = first;
4038 type->t = VT_FUNC;
4039 type->ref = s;
4040 } else if (tok == '[') {
4041 int saved_nocode_wanted = nocode_wanted;
4042 /* array definition */
4043 next();
4044 if (tok == TOK_RESTRICT1)
4045 next();
4046 n = -1;
4047 t1 = 0;
4048 if (tok != ']') {
4049 if (!local_stack || (storage & VT_STATIC))
4050 vpushi(expr_const());
4051 else {
4052 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4053 length must always be evaluated, even under nocode_wanted,
4054 so that its size slot is initialized (e.g. under sizeof
4055 or typeof). */
4056 nocode_wanted = 0;
4057 gexpr();
4059 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4060 n = vtop->c.i;
4061 if (n < 0)
4062 tcc_error("invalid array size");
4063 } else {
4064 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
4065 tcc_error("size of variable length array should be an integer");
4066 t1 = VT_VLA;
4069 skip(']');
4070 /* parse next post type */
4071 post_type(type, ad, storage, 0);
4072 if (type->t == VT_FUNC)
4073 tcc_error("declaration of an array of functions");
4074 t1 |= type->t & VT_VLA;
4076 if (t1 & VT_VLA) {
4077 loc -= type_size(&int_type, &align);
4078 loc &= -align;
4079 n = loc;
4081 vla_runtime_type_size(type, &align);
4082 gen_op('*');
4083 vset(&int_type, VT_LOCAL|VT_LVAL, n);
4084 vswap();
4085 vstore();
4087 if (n != -1)
4088 vpop();
4089 nocode_wanted = saved_nocode_wanted;
4091 /* we push an anonymous symbol which will contain the array
4092 element type */
4093 s = sym_push(SYM_FIELD, type, 0, n);
4094 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
4095 type->ref = s;
4097 return 1;
4100 /* Parse a type declarator (except basic type), and return the type
4101 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4102 expected. 'type' should contain the basic type. 'ad' is the
4103 attribute definition of the basic type. It can be modified by
4104 type_decl(). If this (possibly abstract) declarator is a pointer chain
4105 it returns the innermost pointed to type (equals *type, but is a different
4106 pointer), otherwise returns type itself, that's used for recursive calls. */
4107 static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td)
4109 CType *post, *ret;
4110 int qualifiers, storage;
4112 /* recursive type, remove storage bits first, apply them later again */
4113 storage = type->t & VT_STORAGE;
4114 type->t &= ~VT_STORAGE;
4115 post = ret = type;
4116 while (tok == '*') {
4117 qualifiers = 0;
4118 redo:
4119 next();
4120 switch(tok) {
4121 case TOK_CONST1:
4122 case TOK_CONST2:
4123 case TOK_CONST3:
4124 qualifiers |= VT_CONSTANT;
4125 goto redo;
4126 case TOK_VOLATILE1:
4127 case TOK_VOLATILE2:
4128 case TOK_VOLATILE3:
4129 qualifiers |= VT_VOLATILE;
4130 goto redo;
4131 case TOK_RESTRICT1:
4132 case TOK_RESTRICT2:
4133 case TOK_RESTRICT3:
4134 goto redo;
4135 /* XXX: clarify attribute handling */
4136 case TOK_ATTRIBUTE1:
4137 case TOK_ATTRIBUTE2:
4138 parse_attribute(ad);
4139 break;
4141 mk_pointer(type);
4142 type->t |= qualifiers;
4143 if (ret == type)
4144 /* innermost pointed to type is the one for the first derivation */
4145 ret = pointed_type(type);
4148 if (tok == '(') {
4149 /* This is possibly a parameter type list for abstract declarators
4150 ('int ()'), use post_type for testing this. */
4151 if (!post_type(type, ad, 0, td)) {
4152 /* It's not, so it's a nested declarator, and the post operations
4153 apply to the innermost pointed to type (if any). */
4154 /* XXX: this is not correct to modify 'ad' at this point, but
4155 the syntax is not clear */
4156 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
4157 parse_attribute(ad);
4158 post = type_decl(type, ad, v, td);
4159 skip(')');
4161 } else if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
4162 /* type identifier */
4163 *v = tok;
4164 next();
4165 } else {
4166 if (!(td & TYPE_ABSTRACT))
4167 expect("identifier");
4168 *v = 0;
4170 post_type(post, ad, storage, 0);
4171 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
4172 parse_attribute(ad);
4173 type->t |= storage;
4174 return ret;
4177 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4178 ST_FUNC int lvalue_type(int t)
4180 int bt, r;
4181 r = VT_LVAL;
4182 bt = t & VT_BTYPE;
4183 if (bt == VT_BYTE || bt == VT_BOOL)
4184 r |= VT_LVAL_BYTE;
4185 else if (bt == VT_SHORT)
4186 r |= VT_LVAL_SHORT;
4187 else
4188 return r;
4189 if (t & VT_UNSIGNED)
4190 r |= VT_LVAL_UNSIGNED;
4191 return r;
4194 /* indirection with full error checking and bound check */
4195 ST_FUNC void indir(void)
4197 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
4198 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
4199 return;
4200 expect("pointer");
4202 if (vtop->r & VT_LVAL)
4203 gv(RC_INT);
4204 vtop->type = *pointed_type(&vtop->type);
4205 /* Arrays and functions are never lvalues */
4206 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
4207 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
4208 vtop->r |= lvalue_type(vtop->type.t);
4209 /* if bound checking, the referenced pointer must be checked */
4210 #ifdef CONFIG_TCC_BCHECK
4211 if (tcc_state->do_bounds_check)
4212 vtop->r |= VT_MUSTBOUND;
4213 #endif
4217 /* pass a parameter to a function and do type checking and casting */
4218 static void gfunc_param_typed(Sym *func, Sym *arg)
4220 int func_type;
4221 CType type;
4223 func_type = func->c;
4224 if (func_type == FUNC_OLD ||
4225 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
4226 /* default casting : only need to convert float to double */
4227 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
4228 type.t = VT_DOUBLE;
4229 gen_cast(&type);
4230 } else if (vtop->type.t & VT_BITFIELD) {
4231 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
4232 type.ref = vtop->type.ref;
4233 gen_cast(&type);
4235 } else if (arg == NULL) {
4236 tcc_error("too many arguments to function");
4237 } else {
4238 type = arg->type;
4239 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4240 gen_assign_cast(&type);
4244 /* parse an expression and return its type without any side effect.
4245 If UNRY we parse an unary expression, otherwise a full one. */
4246 static void expr_type(CType *type, int unry)
4248 nocode_wanted++;
4249 if (unry)
4250 unary();
4251 else
4252 gexpr();
4253 *type = vtop->type;
4254 vpop();
4255 nocode_wanted--;
4258 /* parse an expression of the form '(type)' or '(expr)' and return its
4259 type */
4260 static void parse_expr_type(CType *type)
4262 int n;
4263 AttributeDef ad;
4265 skip('(');
4266 if (parse_btype(type, &ad)) {
4267 type_decl(type, &ad, &n, TYPE_ABSTRACT);
4268 } else {
4269 expr_type(type, 0);
4271 skip(')');
4274 static void parse_type(CType *type)
4276 AttributeDef ad;
4277 int n;
4279 if (!parse_btype(type, &ad)) {
4280 expect("type");
4282 type_decl(type, &ad, &n, TYPE_ABSTRACT);
4285 static void parse_builtin_params(int nc, const char *args)
4287 char c, sep = '(';
4288 CType t;
4289 if (nc)
4290 nocode_wanted++;
4291 next();
4292 while ((c = *args++)) {
4293 skip(sep);
4294 sep = ',';
4295 switch (c) {
4296 case 'e': expr_eq(); continue;
4297 case 't': parse_type(&t); vpush(&t); continue;
4298 default: tcc_error("internal error"); break;
4301 skip(')');
4302 if (nc)
4303 nocode_wanted--;
4306 ST_FUNC void unary(void)
4308 int n, t, align, size, r, sizeof_caller;
4309 CType type;
4310 Sym *s;
4311 AttributeDef ad;
4313 sizeof_caller = in_sizeof;
4314 in_sizeof = 0;
4315 /* XXX: GCC 2.95.3 does not generate a table although it should be
4316 better here */
4317 tok_next:
4318 switch(tok) {
4319 case TOK_EXTENSION:
4320 next();
4321 goto tok_next;
4322 case TOK_CINT:
4323 case TOK_CCHAR:
4324 case TOK_LCHAR:
4325 t = VT_INT;
4326 push_tokc:
4327 type.t = t;
4328 type.ref = 0;
4329 vsetc(&type, VT_CONST, &tokc);
4330 next();
4331 break;
4332 case TOK_CUINT:
4333 t = VT_INT | VT_UNSIGNED;
4334 goto push_tokc;
4335 case TOK_CLLONG:
4336 t = VT_LLONG;
4337 goto push_tokc;
4338 case TOK_CULLONG:
4339 t = VT_LLONG | VT_UNSIGNED;
4340 goto push_tokc;
4341 case TOK_CFLOAT:
4342 t = VT_FLOAT;
4343 goto push_tokc;
4344 case TOK_CDOUBLE:
4345 t = VT_DOUBLE;
4346 goto push_tokc;
4347 case TOK_CLDOUBLE:
4348 t = VT_LDOUBLE;
4349 goto push_tokc;
4351 case TOK___FUNCTION__:
4352 if (!gnu_ext)
4353 goto tok_identifier;
4354 /* fall thru */
4355 case TOK___FUNC__:
4357 void *ptr;
4358 int len;
4359 /* special function name identifier */
4360 len = strlen(funcname) + 1;
4361 /* generate char[len] type */
4362 type.t = VT_BYTE;
4363 mk_pointer(&type);
4364 type.t |= VT_ARRAY;
4365 type.ref->c = len;
4366 vpush_ref(&type, data_section, data_section->data_offset, len);
4367 ptr = section_ptr_add(data_section, len);
4368 memcpy(ptr, funcname, len);
4369 next();
4371 break;
4372 case TOK_LSTR:
4373 #ifdef TCC_TARGET_PE
4374 t = VT_SHORT | VT_UNSIGNED;
4375 #else
4376 t = VT_INT;
4377 #endif
4378 goto str_init;
4379 case TOK_STR:
4380 /* string parsing */
4381 t = VT_BYTE;
4382 str_init:
4383 if (tcc_state->warn_write_strings)
4384 t |= VT_CONSTANT;
4385 type.t = t;
4386 mk_pointer(&type);
4387 type.t |= VT_ARRAY;
4388 memset(&ad, 0, sizeof(AttributeDef));
4389 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
4390 break;
4391 case '(':
4392 next();
4393 /* cast ? */
4394 if (parse_btype(&type, &ad)) {
4395 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
4396 skip(')');
4397 /* check ISOC99 compound literal */
4398 if (tok == '{') {
4399 /* data is allocated locally by default */
4400 if (global_expr)
4401 r = VT_CONST;
4402 else
4403 r = VT_LOCAL;
4404 /* all except arrays are lvalues */
4405 if (!(type.t & VT_ARRAY))
4406 r |= lvalue_type(type.t);
4407 memset(&ad, 0, sizeof(AttributeDef));
4408 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
4409 } else {
4410 if (sizeof_caller) {
4411 vpush(&type);
4412 return;
4414 unary();
4415 gen_cast(&type);
4417 } else if (tok == '{') {
4418 int saved_nocode_wanted = nocode_wanted;
4419 if (const_wanted)
4420 tcc_error("expected constant");
4421 /* save all registers */
4422 save_regs(0);
4423 /* statement expression : we do not accept break/continue
4424 inside as GCC does. We do retain the nocode_wanted state,
4425 as statement expressions can't ever be entered from the
4426 outside, so any reactivation of code emission (from labels
4427 or loop heads) can be disabled again after the end of it. */
4428 block(NULL, NULL, 1);
4429 nocode_wanted = saved_nocode_wanted;
4430 skip(')');
4431 } else {
4432 gexpr();
4433 skip(')');
4435 break;
4436 case '*':
4437 next();
4438 unary();
4439 indir();
4440 break;
4441 case '&':
4442 next();
4443 unary();
4444 /* functions names must be treated as function pointers,
4445 except for unary '&' and sizeof. Since we consider that
4446 functions are not lvalues, we only have to handle it
4447 there and in function calls. */
4448 /* arrays can also be used although they are not lvalues */
4449 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
4450 !(vtop->type.t & VT_ARRAY))
4451 test_lvalue();
4452 mk_pointer(&vtop->type);
4453 gaddrof();
4454 break;
4455 case '!':
4456 next();
4457 unary();
4458 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4459 CType boolean;
4460 boolean.t = VT_BOOL;
4461 gen_cast(&boolean);
4462 vtop->c.i = !vtop->c.i;
4463 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
4464 vtop->c.i ^= 1;
4465 else {
4466 save_regs(1);
4467 vseti(VT_JMP, gvtst(1, 0));
4469 break;
4470 case '~':
4471 next();
4472 unary();
4473 vpushi(-1);
4474 gen_op('^');
4475 break;
4476 case '+':
4477 next();
4478 unary();
4479 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
4480 tcc_error("pointer not accepted for unary plus");
4481 /* In order to force cast, we add zero, except for floating point
4482 where we really need an noop (otherwise -0.0 will be transformed
4483 into +0.0). */
4484 if (!is_float(vtop->type.t)) {
4485 vpushi(0);
4486 gen_op('+');
4488 break;
4489 case TOK_SIZEOF:
4490 case TOK_ALIGNOF1:
4491 case TOK_ALIGNOF2:
4492 t = tok;
4493 next();
4494 in_sizeof++;
4495 expr_type(&type, 1); // Perform a in_sizeof = 0;
4496 size = type_size(&type, &align);
4497 if (t == TOK_SIZEOF) {
4498 if (!(type.t & VT_VLA)) {
4499 if (size < 0)
4500 tcc_error("sizeof applied to an incomplete type");
4501 vpushs(size);
4502 } else {
4503 vla_runtime_type_size(&type, &align);
4505 } else {
4506 vpushs(align);
4508 vtop->type.t |= VT_UNSIGNED;
4509 break;
4511 case TOK_builtin_expect:
4512 /* __builtin_expect is a no-op for now */
4513 parse_builtin_params(0, "ee");
4514 vpop();
4515 break;
4516 case TOK_builtin_types_compatible_p:
4517 parse_builtin_params(0, "tt");
4518 vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
4519 vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
4520 n = is_compatible_types(&vtop[-1].type, &vtop[0].type);
4521 vtop -= 2;
4522 vpushi(n);
4523 break;
4524 case TOK_builtin_choose_expr:
4526 int64_t c;
4527 next();
4528 skip('(');
4529 c = expr_const64();
4530 skip(',');
4531 if (!c) {
4532 nocode_wanted++;
4534 expr_eq();
4535 if (!c) {
4536 vpop();
4537 nocode_wanted--;
4539 skip(',');
4540 if (c) {
4541 nocode_wanted++;
4543 expr_eq();
4544 if (c) {
4545 vpop();
4546 nocode_wanted--;
4548 skip(')');
4550 break;
4551 case TOK_builtin_constant_p:
4552 parse_builtin_params(1, "e");
4553 n = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4554 vtop--;
4555 vpushi(n);
4556 break;
4557 case TOK_builtin_frame_address:
4558 case TOK_builtin_return_address:
4560 int tok1 = tok;
4561 int level;
4562 CType type;
4563 next();
4564 skip('(');
4565 if (tok != TOK_CINT) {
4566 tcc_error("%s only takes positive integers",
4567 tok1 == TOK_builtin_return_address ?
4568 "__builtin_return_address" :
4569 "__builtin_frame_address");
4571 level = (uint32_t)tokc.i;
4572 next();
4573 skip(')');
4574 type.t = VT_VOID;
4575 mk_pointer(&type);
4576 vset(&type, VT_LOCAL, 0); /* local frame */
4577 while (level--) {
4578 mk_pointer(&vtop->type);
4579 indir(); /* -> parent frame */
4581 if (tok1 == TOK_builtin_return_address) {
4582 // assume return address is just above frame pointer on stack
4583 vpushi(PTR_SIZE);
4584 gen_op('+');
4585 mk_pointer(&vtop->type);
4586 indir();
4589 break;
4590 #ifdef TCC_TARGET_X86_64
4591 #ifdef TCC_TARGET_PE
4592 case TOK_builtin_va_start:
4593 parse_builtin_params(0, "ee");
4594 r = vtop->r & VT_VALMASK;
4595 if (r == VT_LLOCAL)
4596 r = VT_LOCAL;
4597 if (r != VT_LOCAL)
4598 tcc_error("__builtin_va_start expects a local variable");
4599 vtop->r = r;
4600 vtop->type = char_pointer_type;
4601 vtop->c.i += 8;
4602 vstore();
4603 break;
4604 #else
4605 case TOK_builtin_va_arg_types:
4606 parse_builtin_params(0, "t");
4607 vpushi(classify_x86_64_va_arg(&vtop->type));
4608 vswap();
4609 vpop();
4610 break;
4611 #endif
4612 #endif
4614 #ifdef TCC_TARGET_ARM64
4615 case TOK___va_start: {
4616 parse_builtin_params(0, "ee");
4617 //xx check types
4618 gen_va_start();
4619 vpushi(0);
4620 vtop->type.t = VT_VOID;
4621 break;
4623 case TOK___va_arg: {
4624 CType type;
4625 parse_builtin_params(0, "et");
4626 type = vtop->type;
4627 vpop();
4628 //xx check types
4629 gen_va_arg(&type);
4630 vtop->type = type;
4631 break;
4633 case TOK___arm64_clear_cache: {
4634 parse_builtin_params(0, "ee");
4635 gen_clear_cache();
4636 vpushi(0);
4637 vtop->type.t = VT_VOID;
4638 break;
4640 #endif
4641 /* pre operations */
4642 case TOK_INC:
4643 case TOK_DEC:
4644 t = tok;
4645 next();
4646 unary();
4647 inc(0, t);
4648 break;
4649 case '-':
4650 next();
4651 unary();
4652 t = vtop->type.t & VT_BTYPE;
4653 if (is_float(t)) {
4654 /* In IEEE negate(x) isn't subtract(0,x), but rather
4655 subtract(-0, x). */
4656 vpush(&vtop->type);
4657 if (t == VT_FLOAT)
4658 vtop->c.f = -1.0 * 0.0;
4659 else if (t == VT_DOUBLE)
4660 vtop->c.d = -1.0 * 0.0;
4661 else
4662 vtop->c.ld = -1.0 * 0.0;
4663 } else
4664 vpushi(0);
4665 vswap();
4666 gen_op('-');
4667 break;
4668 case TOK_LAND:
4669 if (!gnu_ext)
4670 goto tok_identifier;
4671 next();
4672 /* allow to take the address of a label */
4673 if (tok < TOK_UIDENT)
4674 expect("label identifier");
4675 s = label_find(tok);
4676 if (!s) {
4677 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4678 } else {
4679 if (s->r == LABEL_DECLARED)
4680 s->r = LABEL_FORWARD;
4682 if (!s->type.t) {
4683 s->type.t = VT_VOID;
4684 mk_pointer(&s->type);
4685 s->type.t |= VT_STATIC;
4687 vpushsym(&s->type, s);
4688 next();
4689 break;
4691 // special qnan , snan and infinity values
4692 case TOK___NAN__:
4693 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
4694 next();
4695 break;
4696 case TOK___SNAN__:
4697 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
4698 next();
4699 break;
4700 case TOK___INF__:
4701 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
4702 next();
4703 break;
4705 default:
4706 tok_identifier:
4707 t = tok;
4708 next();
4709 if (t < TOK_UIDENT)
4710 expect("identifier");
4711 s = sym_find(t);
4712 if (!s) {
4713 const char *name = get_tok_str(t, NULL);
4714 if (tok != '(')
4715 tcc_error("'%s' undeclared", name);
4716 /* for simple function calls, we tolerate undeclared
4717 external reference to int() function */
4718 if (tcc_state->warn_implicit_function_declaration
4719 #ifdef TCC_TARGET_PE
4720 /* people must be warned about using undeclared WINAPI functions
4721 (which usually start with uppercase letter) */
4722 || (name[0] >= 'A' && name[0] <= 'Z')
4723 #endif
4725 tcc_warning("implicit declaration of function '%s'", name);
4726 s = external_global_sym(t, &func_old_type, 0);
4729 r = s->r;
4730 /* A symbol that has a register is a local register variable,
4731 which starts out as VT_LOCAL value. */
4732 if ((r & VT_VALMASK) < VT_CONST)
4733 r = (r & ~VT_VALMASK) | VT_LOCAL;
4735 vset(&s->type, r, s->c);
4736 /* Point to s as backpointer (even without r&VT_SYM).
4737 Will be used by at least the x86 inline asm parser for
4738 regvars. */
4739 vtop->sym = s;
4740 if (vtop->r & VT_SYM) {
4741 vtop->c.i = 0;
4743 break;
4746 /* post operations */
4747 while (1) {
4748 if (tok == TOK_INC || tok == TOK_DEC) {
4749 inc(1, tok);
4750 next();
4751 } else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) {
4752 int qualifiers;
4753 /* field */
4754 if (tok == TOK_ARROW)
4755 indir();
4756 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
4757 test_lvalue();
4758 gaddrof();
4759 /* expect pointer on structure */
4760 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
4761 expect("struct or union");
4762 if (tok == TOK_CDOUBLE)
4763 expect("field name");
4764 next();
4765 if (tok == TOK_CINT || tok == TOK_CUINT)
4766 expect("field name");
4767 s = find_field(&vtop->type, tok);
4768 if (!s)
4769 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc));
4770 /* add field offset to pointer */
4771 vtop->type = char_pointer_type; /* change type to 'char *' */
4772 vpushi(s->c);
4773 gen_op('+');
4774 /* change type to field type, and set to lvalue */
4775 vtop->type = s->type;
4776 vtop->type.t |= qualifiers;
4777 /* an array is never an lvalue */
4778 if (!(vtop->type.t & VT_ARRAY)) {
4779 vtop->r |= lvalue_type(vtop->type.t);
4780 #ifdef CONFIG_TCC_BCHECK
4781 /* if bound checking, the referenced pointer must be checked */
4782 if (tcc_state->do_bounds_check && (vtop->r & VT_VALMASK) != VT_LOCAL)
4783 vtop->r |= VT_MUSTBOUND;
4784 #endif
4786 next();
4787 } else if (tok == '[') {
4788 next();
4789 gexpr();
4790 gen_op('+');
4791 indir();
4792 skip(']');
4793 } else if (tok == '(') {
4794 SValue ret;
4795 Sym *sa;
4796 int nb_args, ret_nregs, ret_align, regsize, variadic;
4798 /* function call */
4799 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
4800 /* pointer test (no array accepted) */
4801 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4802 vtop->type = *pointed_type(&vtop->type);
4803 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4804 goto error_func;
4805 } else {
4806 error_func:
4807 expect("function pointer");
4809 } else {
4810 vtop->r &= ~VT_LVAL; /* no lvalue */
4812 /* get return type */
4813 s = vtop->type.ref;
4814 next();
4815 sa = s->next; /* first parameter */
4816 nb_args = regsize = 0;
4817 ret.r2 = VT_CONST;
4818 /* compute first implicit argument if a structure is returned */
4819 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4820 variadic = (s->c == FUNC_ELLIPSIS);
4821 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4822 &ret_align, &regsize);
4823 if (!ret_nregs) {
4824 /* get some space for the returned structure */
4825 size = type_size(&s->type, &align);
4826 #ifdef TCC_TARGET_ARM64
4827 /* On arm64, a small struct is return in registers.
4828 It is much easier to write it to memory if we know
4829 that we are allowed to write some extra bytes, so
4830 round the allocated space up to a power of 2: */
4831 if (size < 16)
4832 while (size & (size - 1))
4833 size = (size | (size - 1)) + 1;
4834 #endif
4835 loc = (loc - size) & -align;
4836 ret.type = s->type;
4837 ret.r = VT_LOCAL | VT_LVAL;
4838 /* pass it as 'int' to avoid structure arg passing
4839 problems */
4840 vseti(VT_LOCAL, loc);
4841 ret.c = vtop->c;
4842 nb_args++;
4844 } else {
4845 ret_nregs = 1;
4846 ret.type = s->type;
4849 if (ret_nregs) {
4850 /* return in register */
4851 if (is_float(ret.type.t)) {
4852 ret.r = reg_fret(ret.type.t);
4853 #ifdef TCC_TARGET_X86_64
4854 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4855 ret.r2 = REG_QRET;
4856 #endif
4857 } else {
4858 #ifndef TCC_TARGET_ARM64
4859 #ifdef TCC_TARGET_X86_64
4860 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4861 #else
4862 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4863 #endif
4864 ret.r2 = REG_LRET;
4865 #endif
4866 ret.r = REG_IRET;
4868 ret.c.i = 0;
4870 if (tok != ')') {
4871 for(;;) {
4872 expr_eq();
4873 gfunc_param_typed(s, sa);
4874 nb_args++;
4875 if (sa)
4876 sa = sa->next;
4877 if (tok == ')')
4878 break;
4879 skip(',');
4882 if (sa)
4883 tcc_error("too few arguments to function");
4884 skip(')');
4885 gfunc_call(nb_args);
4887 /* return value */
4888 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4889 vsetc(&ret.type, r, &ret.c);
4890 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4893 /* handle packed struct return */
4894 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4895 int addr, offset;
4897 size = type_size(&s->type, &align);
4898 /* We're writing whole regs often, make sure there's enough
4899 space. Assume register size is power of 2. */
4900 if (regsize > align)
4901 align = regsize;
4902 loc = (loc - size) & -align;
4903 addr = loc;
4904 offset = 0;
4905 for (;;) {
4906 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4907 vswap();
4908 vstore();
4909 vtop--;
4910 if (--ret_nregs == 0)
4911 break;
4912 offset += regsize;
4914 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4916 } else {
4917 break;
4922 ST_FUNC void expr_prod(void)
4924 int t;
4926 unary();
4927 while (tok == '*' || tok == '/' || tok == '%') {
4928 t = tok;
4929 next();
4930 unary();
4931 gen_op(t);
4935 ST_FUNC void expr_sum(void)
4937 int t;
4939 expr_prod();
4940 while (tok == '+' || tok == '-') {
4941 t = tok;
4942 next();
4943 expr_prod();
4944 gen_op(t);
4948 static void expr_shift(void)
4950 int t;
4952 expr_sum();
4953 while (tok == TOK_SHL || tok == TOK_SAR) {
4954 t = tok;
4955 next();
4956 expr_sum();
4957 gen_op(t);
4961 static void expr_cmp(void)
4963 int t;
4965 expr_shift();
4966 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4967 tok == TOK_ULT || tok == TOK_UGE) {
4968 t = tok;
4969 next();
4970 expr_shift();
4971 gen_op(t);
4975 static void expr_cmpeq(void)
4977 int t;
4979 expr_cmp();
4980 while (tok == TOK_EQ || tok == TOK_NE) {
4981 t = tok;
4982 next();
4983 expr_cmp();
4984 gen_op(t);
4988 static void expr_and(void)
4990 expr_cmpeq();
4991 while (tok == '&') {
4992 next();
4993 expr_cmpeq();
4994 gen_op('&');
4998 static void expr_xor(void)
5000 expr_and();
5001 while (tok == '^') {
5002 next();
5003 expr_and();
5004 gen_op('^');
5008 static void expr_or(void)
5010 expr_xor();
5011 while (tok == '|') {
5012 next();
5013 expr_xor();
5014 gen_op('|');
5018 static void expr_land(void)
5020 expr_or();
5021 if (tok == TOK_LAND) {
5022 int t = 0;
5023 for(;;) {
5024 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5025 CType ctb;
5026 ctb.t = VT_BOOL;
5027 gen_cast(&ctb);
5028 if (vtop->c.i) {
5029 vpop();
5030 } else {
5031 nocode_wanted++;
5032 while (tok == TOK_LAND) {
5033 next();
5034 expr_or();
5035 vpop();
5037 nocode_wanted--;
5038 if (t)
5039 gsym(t);
5040 gen_cast(&int_type);
5041 break;
5043 } else {
5044 if (!t)
5045 save_regs(1);
5046 t = gvtst(1, t);
5048 if (tok != TOK_LAND) {
5049 if (t)
5050 vseti(VT_JMPI, t);
5051 else
5052 vpushi(1);
5053 break;
5055 next();
5056 expr_or();
5061 static void expr_lor(void)
5063 expr_land();
5064 if (tok == TOK_LOR) {
5065 int t = 0;
5066 for(;;) {
5067 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5068 CType ctb;
5069 ctb.t = VT_BOOL;
5070 gen_cast(&ctb);
5071 if (!vtop->c.i) {
5072 vpop();
5073 } else {
5074 nocode_wanted++;
5075 while (tok == TOK_LOR) {
5076 next();
5077 expr_land();
5078 vpop();
5080 nocode_wanted--;
5081 if (t)
5082 gsym(t);
5083 gen_cast(&int_type);
5084 break;
5086 } else {
5087 if (!t)
5088 save_regs(1);
5089 t = gvtst(0, t);
5091 if (tok != TOK_LOR) {
5092 if (t)
5093 vseti(VT_JMP, t);
5094 else
5095 vpushi(0);
5096 break;
5098 next();
5099 expr_land();
5104 /* Assuming vtop is a value used in a conditional context
5105 (i.e. compared with zero) return 0 if it's false, 1 if
5106 true and -1 if it can't be statically determined. */
5107 static int condition_3way(void)
5109 int c = -1;
5110 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
5111 (!(vtop->r & VT_SYM) ||
5112 !(vtop->sym->type.t & VT_WEAK))) {
5113 CType boolean;
5114 boolean.t = VT_BOOL;
5115 vdup();
5116 gen_cast(&boolean);
5117 c = vtop->c.i;
5118 vpop();
5120 return c;
5123 static void expr_cond(void)
5125 int tt, u, r1, r2, rc, t1, t2, bt1, bt2, islv, c, g;
5126 SValue sv;
5127 CType type, type1, type2;
5129 expr_lor();
5130 if (tok == '?') {
5131 next();
5132 c = condition_3way();
5133 g = (tok == ':' && gnu_ext);
5134 if (c < 0) {
5135 /* needed to avoid having different registers saved in
5136 each branch */
5137 if (is_float(vtop->type.t)) {
5138 rc = RC_FLOAT;
5139 #ifdef TCC_TARGET_X86_64
5140 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
5141 rc = RC_ST0;
5143 #endif
5144 } else
5145 rc = RC_INT;
5146 gv(rc);
5147 save_regs(1);
5148 if (g)
5149 gv_dup();
5150 tt = gvtst(1, 0);
5152 } else {
5153 if (!g)
5154 vpop();
5155 tt = 0;
5158 if (1) {
5159 if (c == 0)
5160 nocode_wanted++;
5161 if (!g)
5162 gexpr();
5164 type1 = vtop->type;
5165 sv = *vtop; /* save value to handle it later */
5166 vtop--; /* no vpop so that FP stack is not flushed */
5167 skip(':');
5169 u = 0;
5170 if (c < 0)
5171 u = gjmp(0);
5172 gsym(tt);
5174 if (c == 0)
5175 nocode_wanted--;
5176 if (c == 1)
5177 nocode_wanted++;
5178 expr_cond();
5179 if (c == 1)
5180 nocode_wanted--;
5182 type2 = vtop->type;
5183 t1 = type1.t;
5184 bt1 = t1 & VT_BTYPE;
5185 t2 = type2.t;
5186 bt2 = t2 & VT_BTYPE;
5187 /* cast operands to correct type according to ISOC rules */
5188 if (is_float(bt1) || is_float(bt2)) {
5189 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5190 type.t = VT_LDOUBLE;
5192 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5193 type.t = VT_DOUBLE;
5194 } else {
5195 type.t = VT_FLOAT;
5197 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5198 /* cast to biggest op */
5199 type.t = VT_LLONG;
5200 /* convert to unsigned if it does not fit in a long long */
5201 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
5202 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
5203 type.t |= VT_UNSIGNED;
5204 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
5205 /* If one is a null ptr constant the result type
5206 is the other. */
5207 if (is_null_pointer (vtop))
5208 type = type1;
5209 else if (is_null_pointer (&sv))
5210 type = type2;
5211 /* XXX: test pointer compatibility, C99 has more elaborate
5212 rules here. */
5213 else
5214 type = type1;
5215 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
5216 /* XXX: test function pointer compatibility */
5217 type = bt1 == VT_FUNC ? type1 : type2;
5218 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
5219 /* XXX: test structure compatibility */
5220 type = bt1 == VT_STRUCT ? type1 : type2;
5221 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
5222 /* NOTE: as an extension, we accept void on only one side */
5223 type.t = VT_VOID;
5224 } else {
5225 /* integer operations */
5226 type.t = VT_INT;
5227 /* convert to unsigned if it does not fit in an integer */
5228 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
5229 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
5230 type.t |= VT_UNSIGNED;
5232 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5233 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5234 islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
5235 islv &= c < 0;
5237 /* now we convert second operand */
5238 if (c != 1) {
5239 gen_cast(&type);
5240 if (islv) {
5241 mk_pointer(&vtop->type);
5242 gaddrof();
5243 } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
5244 gaddrof();
5247 rc = RC_INT;
5248 if (is_float(type.t)) {
5249 rc = RC_FLOAT;
5250 #ifdef TCC_TARGET_X86_64
5251 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
5252 rc = RC_ST0;
5254 #endif
5255 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
5256 /* for long longs, we use fixed registers to avoid having
5257 to handle a complicated move */
5258 rc = RC_IRET;
5261 tt = r2 = 0;
5262 if (c < 0) {
5263 r2 = gv(rc);
5264 tt = gjmp(0);
5266 gsym(u);
5268 /* this is horrible, but we must also convert first
5269 operand */
5270 if (c != 0) {
5271 *vtop = sv;
5272 gen_cast(&type);
5273 if (islv) {
5274 mk_pointer(&vtop->type);
5275 gaddrof();
5276 } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
5277 gaddrof();
5280 if (c < 0) {
5281 r1 = gv(rc);
5282 move_reg(r2, r1, type.t);
5283 vtop->r = r2;
5284 gsym(tt);
5285 if (islv)
5286 indir();
5292 static void expr_eq(void)
5294 int t;
5296 expr_cond();
5297 if (tok == '=' ||
5298 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
5299 tok == TOK_A_XOR || tok == TOK_A_OR ||
5300 tok == TOK_A_SHL || tok == TOK_A_SAR) {
5301 test_lvalue();
5302 t = tok;
5303 next();
5304 if (t == '=') {
5305 expr_eq();
5306 } else {
5307 vdup();
5308 expr_eq();
5309 gen_op(t & 0x7f);
5311 vstore();
5315 ST_FUNC void gexpr(void)
5317 while (1) {
5318 expr_eq();
5319 if (tok != ',')
5320 break;
5321 vpop();
5322 next();
5326 /* parse a constant expression and return value in vtop. */
5327 static void expr_const1(void)
5329 const_wanted++;
5330 expr_cond();
5331 const_wanted--;
5334 /* parse an integer constant and return its value. */
5335 static inline int64_t expr_const64(void)
5337 int64_t c;
5338 expr_const1();
5339 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5340 expect("constant expression");
5341 c = vtop->c.i;
5342 vpop();
5343 return c;
5346 /* parse an integer constant and return its value.
5347 Complain if it doesn't fit 32bit (signed or unsigned). */
5348 ST_FUNC int expr_const(void)
5350 int c;
5351 int64_t wc = expr_const64();
5352 c = wc;
5353 if (c != wc && (unsigned)c != wc)
5354 tcc_error("constant exceeds 32 bit");
5355 return c;
5358 /* return the label token if current token is a label, otherwise
5359 return zero */
5360 static int is_label(void)
5362 int last_tok;
5364 /* fast test first */
5365 if (tok < TOK_UIDENT)
5366 return 0;
5367 /* no need to save tokc because tok is an identifier */
5368 last_tok = tok;
5369 next();
5370 if (tok == ':') {
5371 return last_tok;
5372 } else {
5373 unget_tok(last_tok);
5374 return 0;
5378 #ifndef TCC_TARGET_ARM64
5379 static void gfunc_return(CType *func_type)
5381 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
5382 CType type, ret_type;
5383 int ret_align, ret_nregs, regsize;
5384 ret_nregs = gfunc_sret(func_type, func_var, &ret_type,
5385 &ret_align, &regsize);
5386 if (0 == ret_nregs) {
5387 /* if returning structure, must copy it to implicit
5388 first pointer arg location */
5389 type = *func_type;
5390 mk_pointer(&type);
5391 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
5392 indir();
5393 vswap();
5394 /* copy structure value to pointer */
5395 vstore();
5396 } else {
5397 /* returning structure packed into registers */
5398 int r, size, addr, align;
5399 size = type_size(func_type,&align);
5400 if ((vtop->r != (VT_LOCAL | VT_LVAL) ||
5401 (vtop->c.i & (ret_align-1)))
5402 && (align & (ret_align-1))) {
5403 loc = (loc - size) & -ret_align;
5404 addr = loc;
5405 type = *func_type;
5406 vset(&type, VT_LOCAL | VT_LVAL, addr);
5407 vswap();
5408 vstore();
5409 vpop();
5410 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
5412 vtop->type = ret_type;
5413 if (is_float(ret_type.t))
5414 r = rc_fret(ret_type.t);
5415 else
5416 r = RC_IRET;
5418 if (ret_nregs == 1)
5419 gv(r);
5420 else {
5421 for (;;) {
5422 vdup();
5423 gv(r);
5424 vpop();
5425 if (--ret_nregs == 0)
5426 break;
5427 /* We assume that when a structure is returned in multiple
5428 registers, their classes are consecutive values of the
5429 suite s(n) = 2^n */
5430 r <<= 1;
5431 vtop->c.i += regsize;
5435 } else if (is_float(func_type->t)) {
5436 gv(rc_fret(func_type->t));
5437 } else {
5438 gv(RC_IRET);
5440 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5442 #endif
5444 static int case_cmp(const void *pa, const void *pb)
5446 int64_t a = (*(struct case_t**) pa)->v1;
5447 int64_t b = (*(struct case_t**) pb)->v1;
5448 return a < b ? -1 : a > b;
5451 static void gcase(struct case_t **base, int len, int *bsym)
5453 struct case_t *p;
5454 int e;
5455 int ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
5456 gv(RC_INT);
5457 while (len > 4) {
5458 /* binary search */
5459 p = base[len/2];
5460 vdup();
5461 if (ll)
5462 vpushll(p->v2);
5463 else
5464 vpushi(p->v2);
5465 gen_op(TOK_LE);
5466 e = gtst(1, 0);
5467 vdup();
5468 if (ll)
5469 vpushll(p->v1);
5470 else
5471 vpushi(p->v1);
5472 gen_op(TOK_GE);
5473 gtst_addr(0, p->sym); /* v1 <= x <= v2 */
5474 /* x < v1 */
5475 gcase(base, len/2, bsym);
5476 if (cur_switch->def_sym)
5477 gjmp_addr(cur_switch->def_sym);
5478 else
5479 *bsym = gjmp(*bsym);
5480 /* x > v2 */
5481 gsym(e);
5482 e = len/2 + 1;
5483 base += e; len -= e;
5485 /* linear scan */
5486 while (len--) {
5487 p = *base++;
5488 vdup();
5489 if (ll)
5490 vpushll(p->v2);
5491 else
5492 vpushi(p->v2);
5493 if (p->v1 == p->v2) {
5494 gen_op(TOK_EQ);
5495 gtst_addr(0, p->sym);
5496 } else {
5497 gen_op(TOK_LE);
5498 e = gtst(1, 0);
5499 vdup();
5500 if (ll)
5501 vpushll(p->v1);
5502 else
5503 vpushi(p->v1);
5504 gen_op(TOK_GE);
5505 gtst_addr(0, p->sym);
5506 gsym(e);
5511 static void block(int *bsym, int *csym, int is_expr)
5513 int a, b, c, d, cond;
5514 Sym *s;
5516 /* generate line number info */
5517 if (tcc_state->do_debug)
5518 tcc_debug_line(tcc_state);
5520 if (is_expr) {
5521 /* default return value is (void) */
5522 vpushi(0);
5523 vtop->type.t = VT_VOID;
5526 if (tok == TOK_IF) {
5527 /* if test */
5528 int saved_nocode_wanted = nocode_wanted;
5529 next();
5530 skip('(');
5531 gexpr();
5532 skip(')');
5533 cond = condition_3way();
5534 if (cond == 1)
5535 a = 0, vpop();
5536 else
5537 a = gvtst(1, 0);
5538 if (cond == 0)
5539 nocode_wanted |= 0x20000000;
5540 block(bsym, csym, 0);
5541 if (cond != 1)
5542 nocode_wanted = saved_nocode_wanted;
5543 c = tok;
5544 if (c == TOK_ELSE) {
5545 next();
5546 d = gjmp(0);
5547 gsym(a);
5548 if (cond == 1)
5549 nocode_wanted |= 0x20000000;
5550 block(bsym, csym, 0);
5551 gsym(d); /* patch else jmp */
5552 if (cond != 0)
5553 nocode_wanted = saved_nocode_wanted;
5554 } else
5555 gsym(a);
5556 } else if (tok == TOK_WHILE) {
5557 int saved_nocode_wanted;
5558 nocode_wanted &= ~0x20000000;
5559 next();
5560 d = ind;
5561 vla_sp_restore();
5562 skip('(');
5563 gexpr();
5564 skip(')');
5565 a = gvtst(1, 0);
5566 b = 0;
5567 ++local_scope;
5568 saved_nocode_wanted = nocode_wanted;
5569 block(&a, &b, 0);
5570 nocode_wanted = saved_nocode_wanted;
5571 --local_scope;
5572 gjmp_addr(d);
5573 gsym(a);
5574 gsym_addr(b, d);
5575 } else if (tok == '{') {
5576 Sym *llabel;
5577 int block_vla_sp_loc = vla_sp_loc, saved_vlas_in_scope = vlas_in_scope;
5579 next();
5580 /* record local declaration stack position */
5581 s = local_stack;
5582 llabel = local_label_stack;
5583 ++local_scope;
5585 /* handle local labels declarations */
5586 if (tok == TOK_LABEL) {
5587 next();
5588 for(;;) {
5589 if (tok < TOK_UIDENT)
5590 expect("label identifier");
5591 label_push(&local_label_stack, tok, LABEL_DECLARED);
5592 next();
5593 if (tok == ',') {
5594 next();
5595 } else {
5596 skip(';');
5597 break;
5601 while (tok != '}') {
5602 if ((a = is_label()))
5603 unget_tok(a);
5604 else
5605 decl(VT_LOCAL);
5606 if (tok != '}') {
5607 if (is_expr)
5608 vpop();
5609 block(bsym, csym, is_expr);
5612 /* pop locally defined labels */
5613 label_pop(&local_label_stack, llabel);
5614 /* pop locally defined symbols */
5615 --local_scope;
5616 /* In the is_expr case (a statement expression is finished here),
5617 vtop might refer to symbols on the local_stack. Either via the
5618 type or via vtop->sym. We can't pop those nor any that in turn
5619 might be referred to. To make it easier we don't roll back
5620 any symbols in that case; some upper level call to block() will
5621 do that. We do have to remove such symbols from the lookup
5622 tables, though. sym_pop will do that. */
5623 sym_pop(&local_stack, s, is_expr);
5625 /* Pop VLA frames and restore stack pointer if required */
5626 if (vlas_in_scope > saved_vlas_in_scope) {
5627 vla_sp_loc = saved_vlas_in_scope ? block_vla_sp_loc : vla_sp_root_loc;
5628 vla_sp_restore();
5630 vlas_in_scope = saved_vlas_in_scope;
5632 next();
5633 } else if (tok == TOK_RETURN) {
5634 next();
5635 if (tok != ';') {
5636 gexpr();
5637 gen_assign_cast(&func_vt);
5638 gfunc_return(&func_vt);
5640 skip(';');
5641 /* jump unless last stmt in top-level block */
5642 if (tok != '}' || local_scope != 1)
5643 rsym = gjmp(rsym);
5644 nocode_wanted |= 0x20000000;
5645 } else if (tok == TOK_BREAK) {
5646 /* compute jump */
5647 if (!bsym)
5648 tcc_error("cannot break");
5649 *bsym = gjmp(*bsym);
5650 next();
5651 skip(';');
5652 nocode_wanted |= 0x20000000;
5653 } else if (tok == TOK_CONTINUE) {
5654 /* compute jump */
5655 if (!csym)
5656 tcc_error("cannot continue");
5657 vla_sp_restore_root();
5658 *csym = gjmp(*csym);
5659 next();
5660 skip(';');
5661 } else if (tok == TOK_FOR) {
5662 int e;
5663 int saved_nocode_wanted;
5664 nocode_wanted &= ~0x20000000;
5665 next();
5666 skip('(');
5667 s = local_stack;
5668 ++local_scope;
5669 if (tok != ';') {
5670 /* c99 for-loop init decl? */
5671 if (!decl0(VT_LOCAL, 1, NULL)) {
5672 /* no, regular for-loop init expr */
5673 gexpr();
5674 vpop();
5677 skip(';');
5678 d = ind;
5679 c = ind;
5680 vla_sp_restore();
5681 a = 0;
5682 b = 0;
5683 if (tok != ';') {
5684 gexpr();
5685 a = gvtst(1, 0);
5687 skip(';');
5688 if (tok != ')') {
5689 e = gjmp(0);
5690 c = ind;
5691 vla_sp_restore();
5692 gexpr();
5693 vpop();
5694 gjmp_addr(d);
5695 gsym(e);
5697 skip(')');
5698 saved_nocode_wanted = nocode_wanted;
5699 block(&a, &b, 0);
5700 nocode_wanted = saved_nocode_wanted;
5701 gjmp_addr(c);
5702 gsym(a);
5703 gsym_addr(b, c);
5704 --local_scope;
5705 sym_pop(&local_stack, s, 0);
5707 } else
5708 if (tok == TOK_DO) {
5709 int saved_nocode_wanted;
5710 nocode_wanted &= ~0x20000000;
5711 next();
5712 a = 0;
5713 b = 0;
5714 d = ind;
5715 vla_sp_restore();
5716 saved_nocode_wanted = nocode_wanted;
5717 block(&a, &b, 0);
5718 skip(TOK_WHILE);
5719 skip('(');
5720 gsym(b);
5721 gexpr();
5722 c = gvtst(0, 0);
5723 gsym_addr(c, d);
5724 nocode_wanted = saved_nocode_wanted;
5725 skip(')');
5726 gsym(a);
5727 skip(';');
5728 } else
5729 if (tok == TOK_SWITCH) {
5730 struct switch_t *saved, sw;
5731 int saved_nocode_wanted = nocode_wanted;
5732 SValue switchval;
5733 next();
5734 skip('(');
5735 gexpr();
5736 skip(')');
5737 switchval = *vtop--;
5738 a = 0;
5739 b = gjmp(0); /* jump to first case */
5740 sw.p = NULL; sw.n = 0; sw.def_sym = 0;
5741 saved = cur_switch;
5742 cur_switch = &sw;
5743 block(&a, csym, 0);
5744 nocode_wanted = saved_nocode_wanted;
5745 a = gjmp(a); /* add implicit break */
5746 /* case lookup */
5747 gsym(b);
5748 qsort(sw.p, sw.n, sizeof(void*), case_cmp);
5749 for (b = 1; b < sw.n; b++)
5750 if (sw.p[b - 1]->v2 >= sw.p[b]->v1)
5751 tcc_error("duplicate case value");
5752 /* Our switch table sorting is signed, so the compared
5753 value needs to be as well when it's 64bit. */
5754 if ((switchval.type.t & VT_BTYPE) == VT_LLONG)
5755 switchval.type.t &= ~VT_UNSIGNED;
5756 vpushv(&switchval);
5757 gcase(sw.p, sw.n, &a);
5758 vpop();
5759 if (sw.def_sym)
5760 gjmp_addr(sw.def_sym);
5761 dynarray_reset(&sw.p, &sw.n);
5762 cur_switch = saved;
5763 /* break label */
5764 gsym(a);
5765 } else
5766 if (tok == TOK_CASE) {
5767 struct case_t *cr = tcc_malloc(sizeof(struct case_t));
5768 if (!cur_switch)
5769 expect("switch");
5770 nocode_wanted &= ~0x20000000;
5771 next();
5772 cr->v1 = cr->v2 = expr_const64();
5773 if (gnu_ext && tok == TOK_DOTS) {
5774 next();
5775 cr->v2 = expr_const64();
5776 if (cr->v2 < cr->v1)
5777 tcc_warning("empty case range");
5779 cr->sym = ind;
5780 dynarray_add(&cur_switch->p, &cur_switch->n, cr);
5781 skip(':');
5782 is_expr = 0;
5783 goto block_after_label;
5784 } else
5785 if (tok == TOK_DEFAULT) {
5786 next();
5787 skip(':');
5788 if (!cur_switch)
5789 expect("switch");
5790 if (cur_switch->def_sym)
5791 tcc_error("too many 'default'");
5792 cur_switch->def_sym = ind;
5793 is_expr = 0;
5794 goto block_after_label;
5795 } else
5796 if (tok == TOK_GOTO) {
5797 next();
5798 if (tok == '*' && gnu_ext) {
5799 /* computed goto */
5800 next();
5801 gexpr();
5802 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
5803 expect("pointer");
5804 ggoto();
5805 } else if (tok >= TOK_UIDENT) {
5806 s = label_find(tok);
5807 /* put forward definition if needed */
5808 if (!s) {
5809 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
5810 } else {
5811 if (s->r == LABEL_DECLARED)
5812 s->r = LABEL_FORWARD;
5814 vla_sp_restore_root();
5815 if (s->r & LABEL_FORWARD)
5816 s->jnext = gjmp(s->jnext);
5817 else
5818 gjmp_addr(s->jnext);
5819 next();
5820 } else {
5821 expect("label identifier");
5823 skip(';');
5824 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
5825 asm_instr();
5826 } else {
5827 b = is_label();
5828 if (b) {
5829 /* label case */
5830 next();
5831 s = label_find(b);
5832 if (s) {
5833 if (s->r == LABEL_DEFINED)
5834 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
5835 gsym(s->jnext);
5836 s->r = LABEL_DEFINED;
5837 } else {
5838 s = label_push(&global_label_stack, b, LABEL_DEFINED);
5840 s->jnext = ind;
5841 vla_sp_restore();
5842 /* we accept this, but it is a mistake */
5843 block_after_label:
5844 nocode_wanted &= ~0x20000000;
5845 if (tok == '}') {
5846 tcc_warning("deprecated use of label at end of compound statement");
5847 } else {
5848 if (is_expr)
5849 vpop();
5850 block(bsym, csym, is_expr);
5852 } else {
5853 /* expression case */
5854 if (tok != ';') {
5855 if (is_expr) {
5856 vpop();
5857 gexpr();
5858 } else {
5859 gexpr();
5860 vpop();
5863 skip(';');
5868 /* This skips over a stream of tokens containing balanced {} and ()
5869 pairs, stopping at outer ',' ';' and '}'. If STR then allocates
5870 and stores the skipped tokens in *STR. This doesn't check if
5871 () and {} are nested correctly, i.e. "({)}" is accepted. */
5872 static void skip_or_save_block(TokenString **str)
5874 int level = 0;
5875 if (str)
5876 *str = tok_str_alloc();
5878 while ((level > 0 || (tok != '}' && tok != ',' && tok != ';'))) {
5879 int t;
5880 if (tok == TOK_EOF) {
5881 if (str || level > 0)
5882 tcc_error("unexpected end of file");
5883 else
5884 break;
5886 if (str)
5887 tok_str_add_tok(*str);
5888 t = tok;
5889 next();
5890 if (t == '{' || t == '(') {
5891 level++;
5892 } else if (t == '}' || t == ')') {
5893 level--;
5894 if (level == 0)
5895 break;
5898 if (str) {
5899 tok_str_add(*str, -1);
5900 tok_str_add(*str, 0);
5904 #define EXPR_CONST 1
5905 #define EXPR_ANY 2
5907 static void parse_init_elem(int expr_type)
5909 int saved_global_expr;
5910 switch(expr_type) {
5911 case EXPR_CONST:
5912 /* compound literals must be allocated globally in this case */
5913 saved_global_expr = global_expr;
5914 global_expr = 1;
5915 expr_const1();
5916 global_expr = saved_global_expr;
5917 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
5918 (compound literals). */
5919 if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
5920 && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
5921 || vtop->sym->v < SYM_FIRST_ANOM))
5922 #ifdef TCC_TARGET_PE
5923 || (vtop->type.t & VT_IMPORT)
5924 #endif
5926 tcc_error("initializer element is not constant");
5927 break;
5928 case EXPR_ANY:
5929 expr_eq();
5930 break;
5934 /* put zeros for variable based init */
5935 static void init_putz(Section *sec, unsigned long c, int size)
5937 if (sec) {
5938 /* nothing to do because globals are already set to zero */
5939 } else {
5940 vpush_global_sym(&func_old_type, TOK_memset);
5941 vseti(VT_LOCAL, c);
5942 #ifdef TCC_TARGET_ARM
5943 vpushs(size);
5944 vpushi(0);
5945 #else
5946 vpushi(0);
5947 vpushs(size);
5948 #endif
5949 gfunc_call(3);
5953 /* t is the array or struct type. c is the array or struct
5954 address. cur_field is the pointer to the current
5955 field, for arrays the 'c' member contains the current start
5956 index. 'size_only' is true if only size info is needed (only used
5957 in arrays). al contains the already initialized length of the
5958 current container (starting at c). This returns the new length of that. */
5959 static int decl_designator(CType *type, Section *sec, unsigned long c,
5960 Sym **cur_field, int size_only, int al)
5962 Sym *s, *f;
5963 int index, index_last, align, l, nb_elems, elem_size;
5964 unsigned long corig = c;
5966 elem_size = 0;
5967 nb_elems = 1;
5968 if (gnu_ext && (l = is_label()) != 0)
5969 goto struct_field;
5970 /* NOTE: we only support ranges for last designator */
5971 while (nb_elems == 1 && (tok == '[' || tok == '.')) {
5972 if (tok == '[') {
5973 if (!(type->t & VT_ARRAY))
5974 expect("array type");
5975 next();
5976 index = index_last = expr_const();
5977 if (tok == TOK_DOTS && gnu_ext) {
5978 next();
5979 index_last = expr_const();
5981 skip(']');
5982 s = type->ref;
5983 if (index < 0 || (s->c >= 0 && index_last >= s->c) ||
5984 index_last < index)
5985 tcc_error("invalid index");
5986 if (cur_field)
5987 (*cur_field)->c = index_last;
5988 type = pointed_type(type);
5989 elem_size = type_size(type, &align);
5990 c += index * elem_size;
5991 nb_elems = index_last - index + 1;
5992 } else {
5993 next();
5994 l = tok;
5995 struct_field:
5996 next();
5997 if ((type->t & VT_BTYPE) != VT_STRUCT)
5998 expect("struct/union type");
5999 f = find_field(type, l);
6000 if (!f)
6001 expect("field");
6002 if (cur_field)
6003 *cur_field = f;
6004 type = &f->type;
6005 c += f->c;
6007 cur_field = NULL;
6009 if (!cur_field) {
6010 if (tok == '=') {
6011 next();
6012 } else if (!gnu_ext) {
6013 expect("=");
6015 } else {
6016 if (type->t & VT_ARRAY) {
6017 index = (*cur_field)->c;
6018 if (type->ref->c >= 0 && index >= type->ref->c)
6019 tcc_error("index too large");
6020 type = pointed_type(type);
6021 c += index * type_size(type, &align);
6022 } else {
6023 f = *cur_field;
6024 while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD))
6025 *cur_field = f = f->next;
6026 if (!f)
6027 tcc_error("too many field init");
6028 type = &f->type;
6029 c += f->c;
6032 /* must put zero in holes (note that doing it that way
6033 ensures that it even works with designators) */
6034 if (!size_only && c - corig > al)
6035 init_putz(sec, corig + al, c - corig - al);
6036 decl_initializer(type, sec, c, 0, size_only);
6038 /* XXX: make it more general */
6039 if (!size_only && nb_elems > 1) {
6040 unsigned long c_end;
6041 uint8_t *src, *dst;
6042 int i;
6044 if (!sec) {
6045 vset(type, VT_LOCAL|VT_LVAL, c);
6046 for (i = 1; i < nb_elems; i++) {
6047 vset(type, VT_LOCAL|VT_LVAL, c + elem_size * i);
6048 vswap();
6049 vstore();
6051 vpop();
6052 } else {
6053 c_end = c + nb_elems * elem_size;
6054 if (c_end > sec->data_allocated)
6055 section_realloc(sec, c_end);
6056 src = sec->data + c;
6057 dst = src;
6058 for(i = 1; i < nb_elems; i++) {
6059 dst += elem_size;
6060 memcpy(dst, src, elem_size);
6064 c += nb_elems * type_size(type, &align);
6065 if (c - corig > al)
6066 al = c - corig;
6067 return al;
6070 /* store a value or an expression directly in global data or in local array */
6071 static void init_putv(CType *type, Section *sec, unsigned long c)
6073 int bt, bit_pos, bit_size;
6074 void *ptr;
6075 unsigned long long bit_mask;
6076 CType dtype;
6078 dtype = *type;
6079 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
6081 if (sec) {
6082 int size, align;
6083 /* XXX: not portable */
6084 /* XXX: generate error if incorrect relocation */
6085 gen_assign_cast(&dtype);
6086 bt = type->t & VT_BTYPE;
6087 size = type_size(type, &align);
6088 section_reserve(sec, c + size);
6089 ptr = sec->data + c;
6090 /* XXX: make code faster ? */
6091 if (!(type->t & VT_BITFIELD)) {
6092 bit_pos = 0;
6093 bit_size = PTR_SIZE * 8;
6094 bit_mask = -1LL;
6095 } else {
6096 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
6097 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6098 bit_mask = (1LL << bit_size) - 1;
6100 if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
6101 vtop->sym->v >= SYM_FIRST_ANOM &&
6102 /* XXX This rejects compound literals like
6103 '(void *){ptr}'. The problem is that '&sym' is
6104 represented the same way, which would be ruled out
6105 by the SYM_FIRST_ANOM check above, but also '"string"'
6106 in 'char *p = "string"' is represented the same
6107 with the type being VT_PTR and the symbol being an
6108 anonymous one. That is, there's no difference in vtop
6109 between '(void *){x}' and '&(void *){x}'. Ignore
6110 pointer typed entities here. Hopefully no real code
6111 will every use compound literals with scalar type. */
6112 (vtop->type.t & VT_BTYPE) != VT_PTR) {
6113 /* These come from compound literals, memcpy stuff over. */
6114 Section *ssec;
6115 ElfW(Sym) *esym;
6116 ElfW_Rel *rel;
6117 esym = &((ElfW(Sym) *)symtab_section->data)[vtop->sym->c];
6118 ssec = tcc_state->sections[esym->st_shndx];
6119 memmove (ptr, ssec->data + esym->st_value, size);
6120 if (ssec->reloc) {
6121 /* We need to copy over all memory contents, and that
6122 includes relocations. Use the fact that relocs are
6123 created it order, so look from the end of relocs
6124 until we hit one before the copied region. */
6125 int num_relocs = ssec->reloc->data_offset / sizeof(*rel);
6126 rel = (ElfW_Rel*)(ssec->reloc->data + ssec->reloc->data_offset);
6127 while (num_relocs--) {
6128 rel--;
6129 if (rel->r_offset >= esym->st_value + size)
6130 continue;
6131 if (rel->r_offset < esym->st_value)
6132 break;
6133 /* Note: if the same fields are initialized multiple
6134 times (possible with designators) then we possibly
6135 add multiple relocations for the same offset here.
6136 That would lead to wrong code, the last reloc needs
6137 to win. We clean this up later after the whole
6138 initializer is parsed. */
6139 put_elf_reloca(symtab_section, sec,
6140 c + rel->r_offset - esym->st_value,
6141 ELFW(R_TYPE)(rel->r_info),
6142 ELFW(R_SYM)(rel->r_info),
6143 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6144 rel->r_addend
6145 #else
6147 #endif
6151 } else {
6152 if ((vtop->r & VT_SYM) &&
6153 (bt == VT_BYTE ||
6154 bt == VT_SHORT ||
6155 bt == VT_DOUBLE ||
6156 bt == VT_LDOUBLE ||
6157 #if PTR_SIZE == 8
6158 (bt == VT_LLONG && bit_size != 64) ||
6159 bt == VT_INT
6160 #else
6161 bt == VT_LLONG ||
6162 (bt == VT_INT && bit_size != 32)
6163 #endif
6165 tcc_error("initializer element is not computable at load time");
6166 switch(bt) {
6167 /* XXX: when cross-compiling we assume that each type has the
6168 same representation on host and target, which is likely to
6169 be wrong in the case of long double */
6170 case VT_BOOL:
6171 vtop->c.i = (vtop->c.i != 0);
6172 case VT_BYTE:
6173 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
6174 break;
6175 case VT_SHORT:
6176 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
6177 break;
6178 case VT_FLOAT:
6179 *(float*)ptr = vtop->c.f;
6180 break;
6181 case VT_DOUBLE:
6182 *(double *)ptr = vtop->c.d;
6183 break;
6184 case VT_LDOUBLE:
6185 if (sizeof(long double) == LDOUBLE_SIZE)
6186 *(long double *)ptr = vtop->c.ld;
6187 else if (sizeof(double) == LDOUBLE_SIZE)
6188 *(double *)ptr = (double)vtop->c.ld;
6189 #if (defined __i386__ || defined __x86_64__) && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
6190 else if (sizeof (long double) >= 10)
6191 memcpy(memset(ptr, 0, LDOUBLE_SIZE), &vtop->c.ld, 10);
6192 #endif
6193 else
6194 tcc_error("can't cross compile long double constants");
6195 break;
6196 #if PTR_SIZE != 8
6197 case VT_LLONG:
6198 *(long long *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
6199 break;
6200 #else
6201 case VT_LLONG:
6202 #endif
6203 case VT_PTR:
6205 addr_t val = (vtop->c.i & bit_mask) << bit_pos;
6206 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6207 if (vtop->r & VT_SYM)
6208 greloca(sec, vtop->sym, c, R_DATA_PTR, val);
6209 else
6210 *(addr_t *)ptr |= val;
6211 #else
6212 if (vtop->r & VT_SYM)
6213 greloc(sec, vtop->sym, c, R_DATA_PTR);
6214 *(addr_t *)ptr |= val;
6215 #endif
6216 break;
6218 default:
6220 int val = (vtop->c.i & bit_mask) << bit_pos;
6221 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
6222 if (vtop->r & VT_SYM)
6223 greloca(sec, vtop->sym, c, R_DATA_PTR, val);
6224 else
6225 *(int *)ptr |= val;
6226 #else
6227 if (vtop->r & VT_SYM)
6228 greloc(sec, vtop->sym, c, R_DATA_PTR);
6229 *(int *)ptr |= val;
6230 #endif
6231 break;
6235 vtop--;
6236 } else {
6237 vset(&dtype, VT_LOCAL|VT_LVAL, c);
6238 vswap();
6239 vstore();
6240 vpop();
6244 /* 't' contains the type and storage info. 'c' is the offset of the
6245 object in section 'sec'. If 'sec' is NULL, it means stack based
6246 allocation. 'first' is true if array '{' must be read (multi
6247 dimension implicit array init handling). 'size_only' is true if
6248 size only evaluation is wanted (only for arrays). */
6249 static void decl_initializer(CType *type, Section *sec, unsigned long c,
6250 int first, int size_only)
6252 int len, n, no_oblock, nb, i;
6253 int size1, align1;
6254 int have_elem;
6255 Sym *s, *f;
6256 Sym indexsym;
6257 CType *t1;
6259 /* If we currently are at an '}' or ',' we have read an initializer
6260 element in one of our callers, and not yet consumed it. */
6261 have_elem = tok == '}' || tok == ',';
6262 if (!have_elem && tok != '{' &&
6263 /* In case of strings we have special handling for arrays, so
6264 don't consume them as initializer value (which would commit them
6265 to some anonymous symbol). */
6266 tok != TOK_LSTR && tok != TOK_STR &&
6267 !size_only) {
6268 parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
6269 have_elem = 1;
6272 if (have_elem &&
6273 !(type->t & VT_ARRAY) &&
6274 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6275 The source type might have VT_CONSTANT set, which is
6276 of course assignable to non-const elements. */
6277 is_compatible_parameter_types(type, &vtop->type)) {
6278 init_putv(type, sec, c);
6279 } else if (type->t & VT_ARRAY) {
6280 s = type->ref;
6281 n = s->c;
6282 t1 = pointed_type(type);
6283 size1 = type_size(t1, &align1);
6285 no_oblock = 1;
6286 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
6287 tok == '{') {
6288 if (tok != '{')
6289 tcc_error("character array initializer must be a literal,"
6290 " optionally enclosed in braces");
6291 skip('{');
6292 no_oblock = 0;
6295 /* only parse strings here if correct type (otherwise: handle
6296 them as ((w)char *) expressions */
6297 if ((tok == TOK_LSTR &&
6298 #ifdef TCC_TARGET_PE
6299 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
6300 #else
6301 (t1->t & VT_BTYPE) == VT_INT
6302 #endif
6303 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
6304 len = 0;
6305 while (tok == TOK_STR || tok == TOK_LSTR) {
6306 int cstr_len, ch;
6308 /* compute maximum number of chars wanted */
6309 if (tok == TOK_STR)
6310 cstr_len = tokc.str.size;
6311 else
6312 cstr_len = tokc.str.size / sizeof(nwchar_t);
6313 cstr_len--;
6314 nb = cstr_len;
6315 if (n >= 0 && nb > (n - len))
6316 nb = n - len;
6317 if (!size_only) {
6318 if (cstr_len > nb)
6319 tcc_warning("initializer-string for array is too long");
6320 /* in order to go faster for common case (char
6321 string in global variable, we handle it
6322 specifically */
6323 if (sec && tok == TOK_STR && size1 == 1) {
6324 memcpy(sec->data + c + len, tokc.str.data, nb);
6325 } else {
6326 for(i=0;i<nb;i++) {
6327 if (tok == TOK_STR)
6328 ch = ((unsigned char *)tokc.str.data)[i];
6329 else
6330 ch = ((nwchar_t *)tokc.str.data)[i];
6331 vpushi(ch);
6332 init_putv(t1, sec, c + (len + i) * size1);
6336 len += nb;
6337 next();
6339 /* only add trailing zero if enough storage (no
6340 warning in this case since it is standard) */
6341 if (n < 0 || len < n) {
6342 if (!size_only) {
6343 vpushi(0);
6344 init_putv(t1, sec, c + (len * size1));
6346 len++;
6348 len *= size1;
6349 } else {
6350 indexsym.c = 0;
6351 f = &indexsym;
6353 do_init_list:
6354 len = 0;
6355 while (tok != '}' || have_elem) {
6356 len = decl_designator(type, sec, c, &f, size_only, len);
6357 have_elem = 0;
6358 if (type->t & VT_ARRAY) {
6359 ++indexsym.c;
6360 /* special test for multi dimensional arrays (may not
6361 be strictly correct if designators are used at the
6362 same time) */
6363 if (no_oblock && len >= n*size1)
6364 break;
6365 } else {
6366 if (s->type.t == TOK_UNION)
6367 f = NULL;
6368 else
6369 f = f->next;
6370 if (no_oblock && f == NULL)
6371 break;
6374 if (tok == '}')
6375 break;
6376 skip(',');
6379 /* put zeros at the end */
6380 if (!size_only && len < n*size1)
6381 init_putz(sec, c + len, n*size1 - len);
6382 if (!no_oblock)
6383 skip('}');
6384 /* patch type size if needed, which happens only for array types */
6385 if (n < 0)
6386 s->c = size1 == 1 ? len : ((len + size1 - 1)/size1);
6387 } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
6388 size1 = 1;
6389 no_oblock = 1;
6390 if (first || tok == '{') {
6391 skip('{');
6392 no_oblock = 0;
6394 s = type->ref;
6395 f = s->next;
6396 n = s->c;
6397 goto do_init_list;
6398 } else if (tok == '{') {
6399 next();
6400 decl_initializer(type, sec, c, first, size_only);
6401 skip('}');
6402 } else if (size_only) {
6403 /* If we supported only ISO C we wouldn't have to accept calling
6404 this on anything than an array size_only==1 (and even then
6405 only on the outermost level, so no recursion would be needed),
6406 because initializing a flex array member isn't supported.
6407 But GNU C supports it, so we need to recurse even into
6408 subfields of structs and arrays when size_only is set. */
6409 /* just skip expression */
6410 do {
6411 skip_or_save_block(NULL);
6412 } while (tok != '}' && tok != ',' && tok != -1);
6413 } else {
6414 if (!have_elem) {
6415 /* This should happen only when we haven't parsed
6416 the init element above for fear of committing a
6417 string constant to memory too early. */
6418 if (tok != TOK_STR && tok != TOK_LSTR)
6419 expect("string constant");
6420 parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
6422 init_putv(type, sec, c);
6426 /* parse an initializer for type 't' if 'has_init' is non zero, and
6427 allocate space in local or global data space ('r' is either
6428 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6429 variable 'v' of scope 'scope' is declared before initializers
6430 are parsed. If 'v' is zero, then a reference to the new object
6431 is put in the value stack. If 'has_init' is 2, a special parsing
6432 is done to handle string constants. */
6433 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
6434 int has_init, int v, int scope)
6436 int size, align, addr;
6437 ParseState saved_parse_state = {0};
6438 TokenString *init_str = NULL;
6439 Section *sec;
6440 Sym *flexible_array;
6442 flexible_array = NULL;
6443 if ((type->t & VT_BTYPE) == VT_STRUCT) {
6444 Sym *field = type->ref->next;
6445 if (field) {
6446 while (field->next)
6447 field = field->next;
6448 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
6449 flexible_array = field;
6453 size = type_size(type, &align);
6454 /* If unknown size, we must evaluate it before
6455 evaluating initializers because
6456 initializers can generate global data too
6457 (e.g. string pointers or ISOC99 compound
6458 literals). It also simplifies local
6459 initializers handling */
6460 if (size < 0 || (flexible_array && has_init)) {
6461 if (!has_init)
6462 tcc_error("unknown type size");
6463 /* get all init string */
6464 if (has_init == 2) {
6465 init_str = tok_str_alloc();
6466 /* only get strings */
6467 while (tok == TOK_STR || tok == TOK_LSTR) {
6468 tok_str_add_tok(init_str);
6469 next();
6471 tok_str_add(init_str, -1);
6472 tok_str_add(init_str, 0);
6473 } else {
6474 skip_or_save_block(&init_str);
6477 /* compute size */
6478 save_parse_state(&saved_parse_state);
6480 begin_macro(init_str, 1);
6481 next();
6482 decl_initializer(type, NULL, 0, 1, 1);
6483 /* prepare second initializer parsing */
6484 macro_ptr = init_str->str;
6485 next();
6487 /* if still unknown size, error */
6488 size = type_size(type, &align);
6489 if (size < 0)
6490 tcc_error("unknown type size");
6492 /* If there's a flex member and it was used in the initializer
6493 adjust size. */
6494 if (flexible_array &&
6495 flexible_array->type.ref->c > 0)
6496 size += flexible_array->type.ref->c
6497 * pointed_size(&flexible_array->type);
6498 /* take into account specified alignment if bigger */
6499 if (ad->a.aligned) {
6500 int speca = 1 << (ad->a.aligned - 1);
6501 if (speca > align)
6502 align = speca;
6503 } else if (ad->a.packed) {
6504 align = 1;
6506 if ((r & VT_VALMASK) == VT_LOCAL) {
6507 sec = NULL;
6508 #ifdef CONFIG_TCC_BCHECK
6509 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
6510 loc--;
6512 #endif
6513 loc = (loc - size) & -align;
6514 addr = loc;
6515 #ifdef CONFIG_TCC_BCHECK
6516 /* handles bounds */
6517 /* XXX: currently, since we do only one pass, we cannot track
6518 '&' operators, so we add only arrays */
6519 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
6520 addr_t *bounds_ptr;
6521 /* add padding between regions */
6522 loc--;
6523 /* then add local bound info */
6524 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t));
6525 bounds_ptr[0] = addr;
6526 bounds_ptr[1] = size;
6528 #endif
6529 if (v) {
6530 /* local variable */
6531 #ifdef CONFIG_TCC_ASM
6532 if (ad->asm_label) {
6533 int reg = asm_parse_regvar(ad->asm_label);
6534 if (reg >= 0)
6535 r = (r & ~VT_VALMASK) | reg;
6537 #endif
6538 sym_push(v, type, r, addr);
6539 } else {
6540 /* push local reference */
6541 vset(type, r, addr);
6543 } else {
6544 Sym *sym = NULL;
6545 if (v && scope == VT_CONST) {
6546 /* see if the symbol was already defined */
6547 sym = sym_find(v);
6548 if (sym) {
6549 patch_storage(sym, type);
6550 if (sym->type.t & VT_EXTERN) {
6551 /* if the variable is extern, it was not allocated */
6552 sym->type.t &= ~VT_EXTERN;
6553 /* set array size if it was omitted in extern
6554 declaration */
6555 if ((sym->type.t & VT_ARRAY) &&
6556 sym->type.ref->c < 0 &&
6557 type->ref->c >= 0)
6558 sym->type.ref->c = type->ref->c;
6559 } else if (!has_init) {
6560 /* we accept several definitions of the same
6561 global variable. this is tricky, because we
6562 must play with the SHN_COMMON type of the symbol */
6563 /* no init data, we won't add more to the symbol */
6564 update_storage(sym);
6565 goto no_alloc;
6566 } else if (sym->c) {
6567 ElfW(Sym) *esym;
6568 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
6569 if (esym->st_shndx == data_section->sh_num)
6570 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
6575 /* allocate symbol in corresponding section */
6576 sec = ad->section;
6577 if (!sec) {
6578 if (has_init)
6579 sec = data_section;
6580 else if (tcc_state->nocommon)
6581 sec = bss_section;
6584 if (sec) {
6585 addr = section_add(sec, size, align);
6586 #ifdef CONFIG_TCC_BCHECK
6587 /* add padding if bound check */
6588 if (tcc_state->do_bounds_check)
6589 section_add(sec, 1, 1);
6590 #endif
6591 } else {
6592 addr = align; /* SHN_COMMON is special, symbol value is align */
6593 sec = common_section;
6596 if (v) {
6597 if (!sym) {
6598 sym = sym_push(v, type, r | VT_SYM, 0);
6599 sym->asm_label = ad->asm_label;
6601 /* update symbol definition */
6602 put_extern_sym(sym, sec, addr, size);
6603 } else {
6604 /* push global reference */
6605 sym = get_sym_ref(type, sec, addr, size);
6606 vpushsym(type, sym);
6607 vtop->r |= r;
6610 #ifdef CONFIG_TCC_BCHECK
6611 /* handles bounds now because the symbol must be defined
6612 before for the relocation */
6613 if (tcc_state->do_bounds_check) {
6614 addr_t *bounds_ptr;
6616 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
6617 /* then add global bound info */
6618 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t));
6619 bounds_ptr[0] = 0; /* relocated */
6620 bounds_ptr[1] = size;
6622 #endif
6625 if (type->t & VT_VLA) {
6626 int a;
6628 /* save current stack pointer */
6629 if (vlas_in_scope == 0) {
6630 if (vla_sp_root_loc == -1)
6631 vla_sp_root_loc = (loc -= PTR_SIZE);
6632 gen_vla_sp_save(vla_sp_root_loc);
6635 vla_runtime_type_size(type, &a);
6636 gen_vla_alloc(type, a);
6637 gen_vla_sp_save(addr);
6638 vla_sp_loc = addr;
6639 vlas_in_scope++;
6641 } else if (has_init) {
6642 size_t oldreloc_offset = 0;
6643 if (sec && sec->reloc)
6644 oldreloc_offset = sec->reloc->data_offset;
6645 decl_initializer(type, sec, addr, 1, 0);
6646 if (sec && sec->reloc)
6647 squeeze_multi_relocs(sec, oldreloc_offset);
6648 /* patch flexible array member size back to -1, */
6649 /* for possible subsequent similar declarations */
6650 if (flexible_array)
6651 flexible_array->type.ref->c = -1;
6654 no_alloc:
6655 /* restore parse state if needed */
6656 if (init_str) {
6657 end_macro();
6658 restore_parse_state(&saved_parse_state);
6662 /* parse a function defined by symbol 'sym' and generate its code in
6663 'cur_text_section' */
6664 static void gen_function(Sym *sym)
6666 nocode_wanted = 0;
6667 ind = cur_text_section->data_offset;
6668 /* NOTE: we patch the symbol size later */
6669 put_extern_sym(sym, cur_text_section, ind, 0);
6670 funcname = get_tok_str(sym->v, NULL);
6671 func_ind = ind;
6672 /* Initialize VLA state */
6673 vla_sp_loc = -1;
6674 vla_sp_root_loc = -1;
6675 /* put debug symbol */
6676 tcc_debug_funcstart(tcc_state, sym);
6677 /* push a dummy symbol to enable local sym storage */
6678 sym_push2(&local_stack, SYM_FIELD, 0, 0);
6679 local_scope = 1; /* for function parameters */
6680 gfunc_prolog(&sym->type);
6681 local_scope = 0;
6682 rsym = 0;
6683 block(NULL, NULL, 0);
6684 nocode_wanted = 0;
6685 gsym(rsym);
6686 gfunc_epilog();
6687 cur_text_section->data_offset = ind;
6688 label_pop(&global_label_stack, NULL);
6689 /* reset local stack */
6690 local_scope = 0;
6691 sym_pop(&local_stack, NULL, 0);
6692 /* end of function */
6693 /* patch symbol size */
6694 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
6695 ind - func_ind;
6696 tcc_debug_funcend(tcc_state, ind - func_ind);
6697 /* It's better to crash than to generate wrong code */
6698 cur_text_section = NULL;
6699 funcname = ""; /* for safety */
6700 func_vt.t = VT_VOID; /* for safety */
6701 func_var = 0; /* for safety */
6702 ind = 0; /* for safety */
6703 nocode_wanted = 1;
6704 check_vstack();
6707 static void gen_inline_functions(TCCState *s)
6709 Sym *sym;
6710 int inline_generated, i, ln;
6711 struct InlineFunc *fn;
6713 ln = file->line_num;
6714 /* iterate while inline function are referenced */
6715 for(;;) {
6716 inline_generated = 0;
6717 for (i = 0; i < s->nb_inline_fns; ++i) {
6718 fn = s->inline_fns[i];
6719 sym = fn->sym;
6720 if (sym && sym->c) {
6721 /* the function was used: generate its code and
6722 convert it to a normal function */
6723 fn->sym = NULL;
6724 if (file)
6725 pstrcpy(file->filename, sizeof file->filename, fn->filename);
6726 sym->type.t &= ~VT_INLINE;
6728 begin_macro(fn->func_str, 1);
6729 next();
6730 cur_text_section = text_section;
6731 gen_function(sym);
6732 end_macro();
6734 inline_generated = 1;
6737 if (!inline_generated)
6738 break;
6740 file->line_num = ln;
6743 ST_FUNC void free_inline_functions(TCCState *s)
6745 int i;
6746 /* free tokens of unused inline functions */
6747 for (i = 0; i < s->nb_inline_fns; ++i) {
6748 struct InlineFunc *fn = s->inline_fns[i];
6749 if (fn->sym)
6750 tok_str_free(fn->func_str);
6752 dynarray_reset(&s->inline_fns, &s->nb_inline_fns);
6755 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
6756 if parsing old style parameter decl list (and FUNC_SYM is set then) */
6757 static int decl0(int l, int is_for_loop_init, Sym *func_sym)
6759 int v, has_init, r;
6760 CType type, btype;
6761 Sym *sym;
6762 AttributeDef ad;
6764 while (1) {
6765 if (!parse_btype(&btype, &ad)) {
6766 if (is_for_loop_init)
6767 return 0;
6768 /* skip redundant ';' if not in old parameter decl scope */
6769 if (tok == ';' && l != VT_CMP) {
6770 next();
6771 continue;
6773 if (l == VT_CONST &&
6774 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6775 /* global asm block */
6776 asm_global_instr();
6777 continue;
6779 /* special test for old K&R protos without explicit int
6780 type. Only accepted when defining global data */
6781 if (l != VT_CONST || tok < TOK_UIDENT)
6782 break;
6783 btype.t = VT_INT;
6785 if (((btype.t & VT_BTYPE) == VT_ENUM ||
6786 (btype.t & VT_BTYPE) == VT_STRUCT) &&
6787 tok == ';') {
6788 if ((btype.t & VT_BTYPE) == VT_STRUCT) {
6789 int v = btype.ref->v;
6790 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)
6791 tcc_warning("unnamed struct/union that defines no instances");
6793 next();
6794 continue;
6796 while (1) { /* iterate thru each declaration */
6797 type = btype;
6798 /* If the base type itself was an array type of unspecified
6799 size (like in 'typedef int arr[]; arr x = {1};') then
6800 we will overwrite the unknown size by the real one for
6801 this decl. We need to unshare the ref symbol holding
6802 that size. */
6803 if ((type.t & VT_ARRAY) && type.ref->c < 0) {
6804 type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c);
6806 type_decl(&type, &ad, &v, TYPE_DIRECT);
6807 #if 0
6809 char buf[500];
6810 type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL));
6811 printf("type = '%s'\n", buf);
6813 #endif
6814 if ((type.t & VT_BTYPE) == VT_FUNC) {
6815 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
6816 tcc_error("function without file scope cannot be static");
6818 /* if old style function prototype, we accept a
6819 declaration list */
6820 sym = type.ref;
6821 if (sym->c == FUNC_OLD && l == VT_CONST)
6822 decl0(VT_CMP, 0, sym);
6825 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6826 ad.asm_label = asm_label_instr();
6827 /* parse one last attribute list, after asm label */
6828 parse_attribute(&ad);
6829 if (tok == '{')
6830 expect(";");
6833 if (ad.a.weak)
6834 type.t |= VT_WEAK;
6835 #ifdef TCC_TARGET_PE
6836 if (ad.a.func_import || ad.a.func_export) {
6837 if (type.t & (VT_STATIC|VT_TYPEDEF))
6838 tcc_error("cannot have dll linkage with static or typedef");
6839 if (ad.a.func_export)
6840 type.t |= VT_EXPORT;
6841 else if ((type.t & VT_BTYPE) != VT_FUNC)
6842 type.t |= VT_IMPORT|VT_EXTERN;
6844 #endif
6845 type.t |= ad.a.visibility << VT_VIS_SHIFT;
6847 if (tok == '{') {
6848 if (l != VT_CONST)
6849 tcc_error("cannot use local functions");
6850 if ((type.t & VT_BTYPE) != VT_FUNC)
6851 expect("function definition");
6853 /* reject abstract declarators in function definition
6854 make old style params without decl have int type */
6855 sym = type.ref;
6856 while ((sym = sym->next) != NULL) {
6857 if (!(sym->v & ~SYM_FIELD))
6858 expect("identifier");
6859 if (sym->type.t == VT_VOID)
6860 sym->type = int_type;
6863 /* XXX: cannot do better now: convert extern line to static inline */
6864 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
6865 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6867 sym = sym_find(v);
6868 if (sym) {
6869 Sym *ref;
6870 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
6871 goto func_error1;
6873 ref = sym->type.ref;
6875 /* use func_call from prototype if not defined */
6876 if (ref->a.func_call != FUNC_CDECL
6877 && type.ref->a.func_call == FUNC_CDECL)
6878 type.ref->a.func_call = ref->a.func_call;
6880 /* use static from prototype */
6881 if (sym->type.t & VT_STATIC)
6882 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6884 /* If the definition has no visibility use the
6885 one from prototype. */
6886 if (! (type.t & VT_VIS_MASK))
6887 type.t |= sym->type.t & VT_VIS_MASK;
6889 /* apply other storage attributes from prototype */
6890 type.t |= sym->type.t & (VT_EXPORT|VT_WEAK);
6892 if (!is_compatible_types(&sym->type, &type)) {
6893 func_error1:
6894 tcc_error("incompatible types for redefinition of '%s'",
6895 get_tok_str(v, NULL));
6897 if (ref->a.func_body)
6898 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
6899 /* if symbol is already defined, then put complete type */
6900 sym->type = type;
6902 } else {
6903 /* put function symbol */
6904 sym = global_identifier_push(v, type.t, 0);
6905 sym->type.ref = type.ref;
6908 sym->type.ref->a.func_body = 1;
6909 sym->r = VT_SYM | VT_CONST;
6911 /* static inline functions are just recorded as a kind
6912 of macro. Their code will be emitted at the end of
6913 the compilation unit only if they are used */
6914 if ((type.t & (VT_INLINE | VT_STATIC)) ==
6915 (VT_INLINE | VT_STATIC)) {
6916 struct InlineFunc *fn;
6917 const char *filename;
6919 filename = file ? file->filename : "";
6920 fn = tcc_malloc(sizeof *fn + strlen(filename));
6921 strcpy(fn->filename, filename);
6922 fn->sym = sym;
6923 skip_or_save_block(&fn->func_str);
6924 dynarray_add(&tcc_state->inline_fns,
6925 &tcc_state->nb_inline_fns, fn);
6926 } else {
6927 /* compute text section */
6928 cur_text_section = ad.section;
6929 if (!cur_text_section)
6930 cur_text_section = text_section;
6931 gen_function(sym);
6933 break;
6934 } else {
6935 if (l == VT_CMP) {
6936 /* find parameter in function parameter list */
6937 for (sym = func_sym->next; sym; sym = sym->next)
6938 if ((sym->v & ~SYM_FIELD) == v)
6939 goto found;
6940 tcc_error("declaration for parameter '%s' but no such parameter",
6941 get_tok_str(v, NULL));
6942 found:
6943 if (type.t & VT_STORAGE) /* 'register' is okay */
6944 tcc_error("storage class specified for '%s'",
6945 get_tok_str(v, NULL));
6946 if (sym->type.t != VT_VOID)
6947 tcc_error("redefinition of parameter '%s'",
6948 get_tok_str(v, NULL));
6949 convert_parameter_type(&type);
6950 sym->type = type;
6951 } else if (type.t & VT_TYPEDEF) {
6952 /* save typedefed type */
6953 /* XXX: test storage specifiers ? */
6954 sym = sym_find(v);
6955 if (sym && sym->scope == local_scope) {
6956 if (!is_compatible_types(&sym->type, &type)
6957 || !(sym->type.t & VT_TYPEDEF))
6958 tcc_error("incompatible redefinition of '%s'",
6959 get_tok_str(v, NULL));
6960 sym->type = type;
6961 } else {
6962 sym = sym_push(v, &type, 0, 0);
6964 sym->a = ad.a;
6965 } else {
6966 r = 0;
6967 if ((type.t & VT_BTYPE) == VT_FUNC) {
6968 /* external function definition */
6969 /* specific case for func_call attribute */
6970 type.ref->a = ad.a;
6971 } else if (!(type.t & VT_ARRAY)) {
6972 /* not lvalue if array */
6973 r |= lvalue_type(type.t);
6975 has_init = (tok == '=');
6976 if (has_init && (type.t & VT_VLA))
6977 tcc_error("variable length array cannot be initialized");
6978 if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) ||
6979 ((type.t & VT_BTYPE) == VT_FUNC) ||
6980 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
6981 !has_init && l == VT_CONST && type.ref->c < 0)) {
6982 /* external variable or function */
6983 /* NOTE: as GCC, uninitialized global static
6984 arrays of null size are considered as
6985 extern */
6986 sym = external_sym(v, &type, r);
6987 sym->asm_label = ad.asm_label;
6988 if (ad.alias_target) {
6989 Section tsec;
6990 ElfW(Sym) *esym;
6991 Sym *alias_target;
6993 alias_target = sym_find(ad.alias_target);
6994 if (!alias_target || !alias_target->c)
6995 tcc_error("unsupported forward __alias__ attribute");
6996 esym = &((ElfW(Sym) *)symtab_section->data)[alias_target->c];
6997 tsec.sh_num = esym->st_shndx;
6998 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
7000 } else {
7001 if (type.t & VT_STATIC)
7002 r |= VT_CONST;
7003 else
7004 r |= l;
7005 if (has_init)
7006 next();
7007 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
7010 if (tok != ',') {
7011 if (is_for_loop_init)
7012 return 1;
7013 skip(';');
7014 break;
7016 next();
7018 ad.a.aligned = 0;
7021 return 0;
7024 ST_FUNC void decl(int l)
7026 decl0(l, 0, NULL);
7029 /* ------------------------------------------------------------------------- */