refactor enums
[tinycc.git] / tccgen.c
blob49938b95cfa74147445cfb7ac9c03eb227ce6d8f
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 void gen_cast_s(int t);
75 static inline CType *pointed_type(CType *type);
76 static int is_compatible_types(CType *type1, CType *type2);
77 static int parse_btype(CType *type, AttributeDef *ad);
78 static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
79 static void parse_expr_type(CType *type);
80 static void init_putv(CType *type, Section *sec, unsigned long c);
81 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
82 static void block(int *bsym, int *csym, int is_expr);
83 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
84 static int decl0(int l, int is_for_loop_init, Sym *);
85 static void expr_eq(void);
86 static void vla_runtime_type_size(CType *type, int *a);
87 static void vla_sp_restore(void);
88 static void vla_sp_restore_root(void);
89 static int is_compatible_unqualified_types(CType *type1, CType *type2);
90 static inline int64_t expr_const64(void);
91 ST_FUNC void vpush64(int ty, unsigned long long v);
92 ST_FUNC void vpush(CType *type);
93 ST_FUNC int gvtst(int inv, int t);
94 static void gen_inline_functions(TCCState *s);
95 static void skip_or_save_block(TokenString **str);
97 ST_INLN int is_float(int t)
99 int bt;
100 bt = t & VT_BTYPE;
101 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
104 /* we use our own 'finite' function to avoid potential problems with
105 non standard math libs */
106 /* XXX: endianness dependent */
107 ST_FUNC int ieee_finite(double d)
109 int p[4];
110 memcpy(p, &d, sizeof(double));
111 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
114 ST_FUNC void test_lvalue(void)
116 if (!(vtop->r & VT_LVAL))
117 expect("lvalue");
120 ST_FUNC void check_vstack(void)
122 if (pvtop != vtop)
123 tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
126 /* ------------------------------------------------------------------------- */
127 /* vstack debugging aid */
129 #if 0
130 void pv (const char *lbl, int a, int b)
132 int i;
133 for (i = a; i < a + b; ++i) {
134 SValue *p = &vtop[-i];
135 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
136 lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
139 #endif
141 /* ------------------------------------------------------------------------- */
142 /* start of translation unit info */
143 ST_FUNC void tcc_debug_start(TCCState *s1)
145 if (s1->do_debug) {
146 char buf[512];
148 /* file info: full path + filename */
149 section_sym = put_elf_sym(symtab_section, 0, 0,
150 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
151 text_section->sh_num, NULL);
152 getcwd(buf, sizeof(buf));
153 #ifdef _WIN32
154 normalize_slashes(buf);
155 #endif
156 pstrcat(buf, sizeof(buf), "/");
157 put_stabs_r(buf, N_SO, 0, 0,
158 text_section->data_offset, text_section, section_sym);
159 put_stabs_r(file->filename, N_SO, 0, 0,
160 text_section->data_offset, text_section, section_sym);
161 last_ind = 0;
162 last_line_num = 0;
165 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
166 symbols can be safely used */
167 put_elf_sym(symtab_section, 0, 0,
168 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
169 SHN_ABS, file->filename);
172 /* put end of translation unit info */
173 ST_FUNC void tcc_debug_end(TCCState *s1)
175 if (!s1->do_debug)
176 return;
177 put_stabs_r(NULL, N_SO, 0, 0,
178 text_section->data_offset, text_section, section_sym);
182 /* generate line number info */
183 ST_FUNC void tcc_debug_line(TCCState *s1)
185 if (!s1->do_debug)
186 return;
187 if ((last_line_num != file->line_num || last_ind != ind)) {
188 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
189 last_ind = ind;
190 last_line_num = file->line_num;
194 /* put function symbol */
195 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
197 char buf[512];
199 if (!s1->do_debug)
200 return;
202 /* stabs info */
203 /* XXX: we put here a dummy type */
204 snprintf(buf, sizeof(buf), "%s:%c1",
205 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
206 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
207 cur_text_section, sym->c);
208 /* //gr gdb wants a line at the function */
209 put_stabn(N_SLINE, 0, file->line_num, 0);
211 last_ind = 0;
212 last_line_num = 0;
215 /* put function size */
216 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
218 if (!s1->do_debug)
219 return;
220 put_stabn(N_FUN, 0, 0, size);
223 /* ------------------------------------------------------------------------- */
224 ST_FUNC void tccgen_start(TCCState *s1)
226 cur_text_section = NULL;
227 funcname = "";
228 anon_sym = SYM_FIRST_ANOM;
229 section_sym = 0;
230 const_wanted = 0;
231 nocode_wanted = 1;
233 /* define some often used types */
234 int_type.t = VT_INT;
235 char_pointer_type.t = VT_BYTE;
236 mk_pointer(&char_pointer_type);
237 #if PTR_SIZE == 4
238 size_type.t = VT_INT;
239 #else
240 size_type.t = VT_LLONG;
241 #endif
242 func_old_type.t = VT_FUNC;
243 func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
244 func_old_type.ref->f.func_call = FUNC_CDECL;
245 func_old_type.ref->f.func_type = FUNC_OLD;
247 tcc_debug_start(s1);
249 #ifdef TCC_TARGET_ARM
250 arm_init(s1);
251 #endif
254 ST_FUNC void tccgen_end(TCCState *s1)
256 gen_inline_functions(s1);
257 check_vstack();
258 /* end of translation unit info */
259 tcc_debug_end(s1);
262 /* ------------------------------------------------------------------------- */
263 /* apply storage attributes to Elf symbol */
265 static void update_storage(Sym *sym)
267 ElfW(Sym) *esym;
268 if (0 == sym->c)
269 return;
270 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
271 if (sym->a.visibility)
272 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
273 | sym->a.visibility;
274 if (sym->a.weak)
275 esym->st_info = ELFW(ST_INFO)(STB_WEAK, ELFW(ST_TYPE)(esym->st_info));
276 #ifdef TCC_TARGET_PE
277 if (sym->a.dllimport)
278 esym->st_other |= ST_PE_IMPORT;
279 if (sym->a.dllexport)
280 esym->st_other |= ST_PE_EXPORT;
281 #endif
282 #if 0
283 printf("storage %s: vis=%d weak=%d exp=%d imp=%d\n",
284 get_tok_str(sym->v, NULL),
285 sym->a.visibility,
286 sym->a.weak,
287 sym->a.dllexport,
288 sym->a.dllimport
290 #endif
293 /* ------------------------------------------------------------------------- */
294 /* update sym->c so that it points to an external symbol in section
295 'section' with value 'value' */
297 ST_FUNC void put_extern_sym2(Sym *sym, Section *section,
298 addr_t value, unsigned long size,
299 int can_add_underscore)
301 int sym_type, sym_bind, sh_num, info, other, t;
302 ElfW(Sym) *esym;
303 const char *name;
304 char buf1[256];
305 #ifdef CONFIG_TCC_BCHECK
306 char buf[32];
307 #endif
309 if (section == NULL)
310 sh_num = SHN_UNDEF;
311 else if (section == SECTION_ABS)
312 sh_num = SHN_ABS;
313 else
314 sh_num = section->sh_num;
316 if (!sym->c) {
317 name = get_tok_str(sym->v, NULL);
318 #ifdef CONFIG_TCC_BCHECK
319 if (tcc_state->do_bounds_check) {
320 /* XXX: avoid doing that for statics ? */
321 /* if bound checking is activated, we change some function
322 names by adding the "__bound" prefix */
323 switch(sym->v) {
324 #ifdef TCC_TARGET_PE
325 /* XXX: we rely only on malloc hooks */
326 case TOK_malloc:
327 case TOK_free:
328 case TOK_realloc:
329 case TOK_memalign:
330 case TOK_calloc:
331 #endif
332 case TOK_memcpy:
333 case TOK_memmove:
334 case TOK_memset:
335 case TOK_strlen:
336 case TOK_strcpy:
337 case TOK_alloca:
338 strcpy(buf, "__bound_");
339 strcat(buf, name);
340 name = buf;
341 break;
344 #endif
345 t = sym->type.t;
346 if ((t & VT_BTYPE) == VT_FUNC) {
347 sym_type = STT_FUNC;
348 } else if ((t & VT_BTYPE) == VT_VOID) {
349 sym_type = STT_NOTYPE;
350 } else {
351 sym_type = STT_OBJECT;
353 if (t & VT_STATIC)
354 sym_bind = STB_LOCAL;
355 else
356 sym_bind = STB_GLOBAL;
357 other = 0;
358 #ifdef TCC_TARGET_PE
359 if (sym_type == STT_FUNC && sym->type.ref) {
360 Sym *ref = sym->type.ref;
361 if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) {
362 sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE);
363 name = buf1;
364 other |= ST_PE_STDCALL;
365 can_add_underscore = 0;
368 #endif
369 if (tcc_state->leading_underscore && can_add_underscore) {
370 buf1[0] = '_';
371 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
372 name = buf1;
374 if (sym->asm_label)
375 name = get_tok_str(sym->asm_label, NULL);
376 info = ELFW(ST_INFO)(sym_bind, sym_type);
377 sym->c = set_elf_sym(symtab_section, value, size, info, other, sh_num, name);
378 } else {
379 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
380 esym->st_value = value;
381 esym->st_size = size;
382 esym->st_shndx = sh_num;
384 update_storage(sym);
387 ST_FUNC void put_extern_sym(Sym *sym, Section *section,
388 addr_t value, unsigned long size)
390 put_extern_sym2(sym, section, value, size, 1);
393 /* add a new relocation entry to symbol 'sym' in section 's' */
394 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
395 addr_t addend)
397 int c = 0;
399 if (nocode_wanted && s == cur_text_section)
400 return;
402 if (sym) {
403 if (0 == sym->c)
404 put_extern_sym(sym, NULL, 0, 0);
405 c = sym->c;
408 /* now we can add ELF relocation info */
409 put_elf_reloca(symtab_section, s, offset, type, c, addend);
412 #if PTR_SIZE == 4
413 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
415 greloca(s, sym, offset, type, 0);
417 #endif
419 /* ------------------------------------------------------------------------- */
420 /* symbol allocator */
421 static Sym *__sym_malloc(void)
423 Sym *sym_pool, *sym, *last_sym;
424 int i;
426 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
427 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
429 last_sym = sym_free_first;
430 sym = sym_pool;
431 for(i = 0; i < SYM_POOL_NB; i++) {
432 sym->next = last_sym;
433 last_sym = sym;
434 sym++;
436 sym_free_first = last_sym;
437 return last_sym;
440 static inline Sym *sym_malloc(void)
442 Sym *sym;
443 #ifndef SYM_DEBUG
444 sym = sym_free_first;
445 if (!sym)
446 sym = __sym_malloc();
447 sym_free_first = sym->next;
448 return sym;
449 #else
450 sym = tcc_malloc(sizeof(Sym));
451 return sym;
452 #endif
455 ST_INLN void sym_free(Sym *sym)
457 #ifndef SYM_DEBUG
458 sym->next = sym_free_first;
459 sym_free_first = sym;
460 #else
461 tcc_free(sym);
462 #endif
465 /* push, without hashing */
466 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c)
468 Sym *s;
470 s = sym_malloc();
471 memset(s, 0, sizeof *s);
472 s->v = v;
473 s->type.t = t;
474 s->c = c;
475 /* add in stack */
476 s->prev = *ps;
477 *ps = s;
478 return s;
481 /* find a symbol and return its associated structure. 's' is the top
482 of the symbol stack */
483 ST_FUNC Sym *sym_find2(Sym *s, int v)
485 while (s) {
486 if (s->v == v)
487 return s;
488 else if (s->v == -1)
489 return NULL;
490 s = s->prev;
492 return NULL;
495 /* structure lookup */
496 ST_INLN Sym *struct_find(int v)
498 v -= TOK_IDENT;
499 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
500 return NULL;
501 return table_ident[v]->sym_struct;
504 /* find an identifier */
505 ST_INLN Sym *sym_find(int v)
507 v -= TOK_IDENT;
508 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
509 return NULL;
510 return table_ident[v]->sym_identifier;
513 /* push a given symbol on the symbol stack */
514 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
516 Sym *s, **ps;
517 TokenSym *ts;
519 if (local_stack)
520 ps = &local_stack;
521 else
522 ps = &global_stack;
523 s = sym_push2(ps, v, type->t, c);
524 s->type.ref = type->ref;
525 s->r = r;
526 /* don't record fields or anonymous symbols */
527 /* XXX: simplify */
528 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
529 /* record symbol in token array */
530 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
531 if (v & SYM_STRUCT)
532 ps = &ts->sym_struct;
533 else
534 ps = &ts->sym_identifier;
535 s->prev_tok = *ps;
536 *ps = s;
537 s->sym_scope = local_scope;
538 if (s->prev_tok && s->prev_tok->sym_scope == s->sym_scope)
539 tcc_error("redeclaration of '%s'",
540 get_tok_str(v & ~SYM_STRUCT, NULL));
542 return s;
545 /* push a global identifier */
546 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
548 Sym *s, **ps;
549 s = sym_push2(&global_stack, v, t, c);
550 /* don't record anonymous symbol */
551 if (v < SYM_FIRST_ANOM) {
552 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
553 /* modify the top most local identifier, so that
554 sym_identifier will point to 's' when popped */
555 while (*ps != NULL)
556 ps = &(*ps)->prev_tok;
557 s->prev_tok = NULL;
558 *ps = s;
560 return s;
563 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
564 pop them yet from the list, but do remove them from the token array. */
565 ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep)
567 Sym *s, *ss, **ps;
568 TokenSym *ts;
569 int v;
571 s = *ptop;
572 while(s != b) {
573 ss = s->prev;
574 v = s->v;
575 /* remove symbol in token array */
576 /* XXX: simplify */
577 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
578 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
579 if (v & SYM_STRUCT)
580 ps = &ts->sym_struct;
581 else
582 ps = &ts->sym_identifier;
583 *ps = s->prev_tok;
585 if (!keep)
586 sym_free(s);
587 s = ss;
589 if (!keep)
590 *ptop = b;
593 /* ------------------------------------------------------------------------- */
595 static void vsetc(CType *type, int r, CValue *vc)
597 int v;
599 if (vtop >= vstack + (VSTACK_SIZE - 1))
600 tcc_error("memory full (vstack)");
601 /* cannot let cpu flags if other instruction are generated. Also
602 avoid leaving VT_JMP anywhere except on the top of the stack
603 because it would complicate the code generator.
605 Don't do this when nocode_wanted. vtop might come from
606 !nocode_wanted regions (see 88_codeopt.c) and transforming
607 it to a register without actually generating code is wrong
608 as their value might still be used for real. All values
609 we push under nocode_wanted will eventually be popped
610 again, so that the VT_CMP/VT_JMP value will be in vtop
611 when code is unsuppressed again.
613 Same logic below in vswap(); */
614 if (vtop >= vstack && !nocode_wanted) {
615 v = vtop->r & VT_VALMASK;
616 if (v == VT_CMP || (v & ~1) == VT_JMP)
617 gv(RC_INT);
620 vtop++;
621 vtop->type = *type;
622 vtop->r = r;
623 vtop->r2 = VT_CONST;
624 vtop->c = *vc;
625 vtop->sym = NULL;
628 ST_FUNC void vswap(void)
630 SValue tmp;
631 /* cannot vswap cpu flags. See comment at vsetc() above */
632 if (vtop >= vstack && !nocode_wanted) {
633 int v = vtop->r & VT_VALMASK;
634 if (v == VT_CMP || (v & ~1) == VT_JMP)
635 gv(RC_INT);
637 tmp = vtop[0];
638 vtop[0] = vtop[-1];
639 vtop[-1] = tmp;
642 /* pop stack value */
643 ST_FUNC void vpop(void)
645 int v;
646 v = vtop->r & VT_VALMASK;
647 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
648 /* for x86, we need to pop the FP stack */
649 if (v == TREG_ST0) {
650 o(0xd8dd); /* fstp %st(0) */
651 } else
652 #endif
653 if (v == VT_JMP || v == VT_JMPI) {
654 /* need to put correct jump if && or || without test */
655 gsym(vtop->c.i);
657 vtop--;
660 /* push constant of type "type" with useless value */
661 ST_FUNC void vpush(CType *type)
663 vset(type, VT_CONST, 0);
666 /* push integer constant */
667 ST_FUNC void vpushi(int v)
669 CValue cval;
670 cval.i = v;
671 vsetc(&int_type, VT_CONST, &cval);
674 /* push a pointer sized constant */
675 static void vpushs(addr_t v)
677 CValue cval;
678 cval.i = v;
679 vsetc(&size_type, VT_CONST, &cval);
682 /* push arbitrary 64bit constant */
683 ST_FUNC void vpush64(int ty, unsigned long long v)
685 CValue cval;
686 CType ctype;
687 ctype.t = ty;
688 ctype.ref = NULL;
689 cval.i = v;
690 vsetc(&ctype, VT_CONST, &cval);
693 /* push long long constant */
694 static inline void vpushll(long long v)
696 vpush64(VT_LLONG, v);
699 ST_FUNC void vset(CType *type, int r, int v)
701 CValue cval;
703 cval.i = v;
704 vsetc(type, r, &cval);
707 static void vseti(int r, int v)
709 CType type;
710 type.t = VT_INT;
711 type.ref = NULL;
712 vset(&type, r, v);
715 ST_FUNC void vpushv(SValue *v)
717 if (vtop >= vstack + (VSTACK_SIZE - 1))
718 tcc_error("memory full (vstack)");
719 vtop++;
720 *vtop = *v;
723 static void vdup(void)
725 vpushv(vtop);
728 /* rotate n first stack elements to the bottom
729 I1 ... In -> I2 ... In I1 [top is right]
731 ST_FUNC void vrotb(int n)
733 int i;
734 SValue tmp;
736 tmp = vtop[-n + 1];
737 for(i=-n+1;i!=0;i++)
738 vtop[i] = vtop[i+1];
739 vtop[0] = tmp;
742 /* rotate the n elements before entry e towards the top
743 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
745 ST_FUNC void vrote(SValue *e, int n)
747 int i;
748 SValue tmp;
750 tmp = *e;
751 for(i = 0;i < n - 1; i++)
752 e[-i] = e[-i - 1];
753 e[-n + 1] = tmp;
756 /* rotate n first stack elements to the top
757 I1 ... In -> In I1 ... I(n-1) [top is right]
759 ST_FUNC void vrott(int n)
761 vrote(vtop, n);
764 /* push a symbol value of TYPE */
765 static inline void vpushsym(CType *type, Sym *sym)
767 CValue cval;
768 cval.i = 0;
769 vsetc(type, VT_CONST | VT_SYM, &cval);
770 vtop->sym = sym;
773 /* Return a static symbol pointing to a section */
774 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
776 int v;
777 Sym *sym;
779 v = anon_sym++;
780 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
781 sym->type.ref = type->ref;
782 sym->r = VT_CONST | VT_SYM;
783 put_extern_sym(sym, sec, offset, size);
784 return sym;
787 /* push a reference to a section offset by adding a dummy symbol */
788 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
790 vpushsym(type, get_sym_ref(type, sec, offset, size));
793 /* define a new external reference to a symbol 'v' of type 'u' */
794 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
796 Sym *s;
798 s = sym_find(v);
799 if (!s) {
800 /* push forward reference */
801 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
802 s->type.ref = type->ref;
803 s->r = r | VT_CONST | VT_SYM;
805 return s;
808 /* Merge some storage attributes. */
809 static void patch_storage(Sym *sym, AttributeDef *ad, CType *type)
811 if (type && !is_compatible_types(&sym->type, type))
812 tcc_error("incompatible types for redefinition of '%s'",
813 get_tok_str(sym->v, NULL));
814 #ifdef TCC_TARGET_PE
815 if (sym->a.dllimport != ad->a.dllimport)
816 tcc_error("incompatible dll linkage for redefinition of '%s'",
817 get_tok_str(sym->v, NULL));
818 #endif
819 sym->a.dllexport |= ad->a.dllexport;
820 sym->a.weak |= ad->a.weak;
821 if (ad->a.visibility) {
822 int vis = sym->a.visibility;
823 int vis2 = ad->a.visibility;
824 if (vis == STV_DEFAULT)
825 vis = vis2;
826 else if (vis2 != STV_DEFAULT)
827 vis = (vis < vis2) ? vis : vis2;
828 sym->a.visibility = vis;
830 if (ad->a.aligned)
831 sym->a.aligned = ad->a.aligned;
832 if (ad->asm_label)
833 sym->asm_label = ad->asm_label;
834 update_storage(sym);
837 /* define a new external reference to a symbol 'v' */
838 static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad)
840 Sym *s;
841 s = sym_find(v);
842 if (!s) {
843 /* push forward reference */
844 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
845 s->type.t |= VT_EXTERN;
846 s->a = ad->a;
847 s->sym_scope = 0;
848 } else {
849 if (s->type.ref == func_old_type.ref) {
850 s->type.ref = type->ref;
851 s->r = r | VT_CONST | VT_SYM;
852 s->type.t |= VT_EXTERN;
854 patch_storage(s, ad, type);
856 return s;
859 /* push a reference to global symbol v */
860 ST_FUNC void vpush_global_sym(CType *type, int v)
862 vpushsym(type, external_global_sym(v, type, 0));
865 /* save registers up to (vtop - n) stack entry */
866 ST_FUNC void save_regs(int n)
868 SValue *p, *p1;
869 for(p = vstack, p1 = vtop - n; p <= p1; p++)
870 save_reg(p->r);
873 /* save r to the memory stack, and mark it as being free */
874 ST_FUNC void save_reg(int r)
876 save_reg_upstack(r, 0);
879 /* save r to the memory stack, and mark it as being free,
880 if seen up to (vtop - n) stack entry */
881 ST_FUNC void save_reg_upstack(int r, int n)
883 int l, saved, size, align;
884 SValue *p, *p1, sv;
885 CType *type;
887 if ((r &= VT_VALMASK) >= VT_CONST)
888 return;
889 if (nocode_wanted)
890 return;
892 /* modify all stack values */
893 saved = 0;
894 l = 0;
895 for(p = vstack, p1 = vtop - n; p <= p1; p++) {
896 if ((p->r & VT_VALMASK) == r ||
897 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
898 /* must save value on stack if not already done */
899 if (!saved) {
900 /* NOTE: must reload 'r' because r might be equal to r2 */
901 r = p->r & VT_VALMASK;
902 /* store register in the stack */
903 type = &p->type;
904 if ((p->r & VT_LVAL) ||
905 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
906 #if PTR_SIZE == 8
907 type = &char_pointer_type;
908 #else
909 type = &int_type;
910 #endif
911 size = type_size(type, &align);
912 loc = (loc - size) & -align;
913 sv.type.t = type->t;
914 sv.r = VT_LOCAL | VT_LVAL;
915 sv.c.i = loc;
916 store(r, &sv);
917 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
918 /* x86 specific: need to pop fp register ST0 if saved */
919 if (r == TREG_ST0) {
920 o(0xd8dd); /* fstp %st(0) */
922 #endif
923 #if PTR_SIZE == 4
924 /* special long long case */
925 if ((type->t & VT_BTYPE) == VT_LLONG) {
926 sv.c.i += 4;
927 store(p->r2, &sv);
929 #endif
930 l = loc;
931 saved = 1;
933 /* mark that stack entry as being saved on the stack */
934 if (p->r & VT_LVAL) {
935 /* also clear the bounded flag because the
936 relocation address of the function was stored in
937 p->c.i */
938 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
939 } else {
940 p->r = lvalue_type(p->type.t) | VT_LOCAL;
942 p->r2 = VT_CONST;
943 p->c.i = l;
948 #ifdef TCC_TARGET_ARM
949 /* find a register of class 'rc2' with at most one reference on stack.
950 * If none, call get_reg(rc) */
951 ST_FUNC int get_reg_ex(int rc, int rc2)
953 int r;
954 SValue *p;
956 for(r=0;r<NB_REGS;r++) {
957 if (reg_classes[r] & rc2) {
958 int n;
959 n=0;
960 for(p = vstack; p <= vtop; p++) {
961 if ((p->r & VT_VALMASK) == r ||
962 (p->r2 & VT_VALMASK) == r)
963 n++;
965 if (n <= 1)
966 return r;
969 return get_reg(rc);
971 #endif
973 /* find a free register of class 'rc'. If none, save one register */
974 ST_FUNC int get_reg(int rc)
976 int r;
977 SValue *p;
979 /* find a free register */
980 for(r=0;r<NB_REGS;r++) {
981 if (reg_classes[r] & rc) {
982 if (nocode_wanted)
983 return r;
984 for(p=vstack;p<=vtop;p++) {
985 if ((p->r & VT_VALMASK) == r ||
986 (p->r2 & VT_VALMASK) == r)
987 goto notfound;
989 return r;
991 notfound: ;
994 /* no register left : free the first one on the stack (VERY
995 IMPORTANT to start from the bottom to ensure that we don't
996 spill registers used in gen_opi()) */
997 for(p=vstack;p<=vtop;p++) {
998 /* look at second register (if long long) */
999 r = p->r2 & VT_VALMASK;
1000 if (r < VT_CONST && (reg_classes[r] & rc))
1001 goto save_found;
1002 r = p->r & VT_VALMASK;
1003 if (r < VT_CONST && (reg_classes[r] & rc)) {
1004 save_found:
1005 save_reg(r);
1006 return r;
1009 /* Should never comes here */
1010 return -1;
1013 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1014 if needed */
1015 static void move_reg(int r, int s, int t)
1017 SValue sv;
1019 if (r != s) {
1020 save_reg(r);
1021 sv.type.t = t;
1022 sv.type.ref = NULL;
1023 sv.r = s;
1024 sv.c.i = 0;
1025 load(r, &sv);
1029 /* get address of vtop (vtop MUST BE an lvalue) */
1030 ST_FUNC void gaddrof(void)
1032 vtop->r &= ~VT_LVAL;
1033 /* tricky: if saved lvalue, then we can go back to lvalue */
1034 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
1035 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
1040 #ifdef CONFIG_TCC_BCHECK
1041 /* generate lvalue bound code */
1042 static void gbound(void)
1044 int lval_type;
1045 CType type1;
1047 vtop->r &= ~VT_MUSTBOUND;
1048 /* if lvalue, then use checking code before dereferencing */
1049 if (vtop->r & VT_LVAL) {
1050 /* if not VT_BOUNDED value, then make one */
1051 if (!(vtop->r & VT_BOUNDED)) {
1052 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
1053 /* must save type because we must set it to int to get pointer */
1054 type1 = vtop->type;
1055 vtop->type.t = VT_PTR;
1056 gaddrof();
1057 vpushi(0);
1058 gen_bounded_ptr_add();
1059 vtop->r |= lval_type;
1060 vtop->type = type1;
1062 /* then check for dereferencing */
1063 gen_bounded_ptr_deref();
1066 #endif
1068 /* store vtop a register belonging to class 'rc'. lvalues are
1069 converted to values. Cannot be used if cannot be converted to
1070 register value (such as structures). */
1071 ST_FUNC int gv(int rc)
1073 int r, bit_pos, bit_size, size, align;
1074 int rc2;
1076 /* NOTE: get_reg can modify vstack[] */
1077 if (vtop->type.t & VT_BITFIELD) {
1078 CType type;
1079 int bits = 32;
1080 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
1081 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
1082 /* remove bit field info to avoid loops */
1083 vtop->type.t &= ~VT_BITFIELD & ((1 << VT_STRUCT_SHIFT) - 1);
1084 /* cast to int to propagate signedness in following ops */
1085 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1086 type.t = VT_LLONG;
1087 bits = 64;
1088 } else
1089 type.t = VT_INT;
1090 if((vtop->type.t & VT_UNSIGNED)
1091 || (vtop->type.t & VT_BTYPE) == VT_BOOL)
1092 type.t |= VT_UNSIGNED;
1093 gen_cast(&type);
1094 /* generate shifts */
1095 vpushi(bits - (bit_pos + bit_size));
1096 gen_op(TOK_SHL);
1097 vpushi(bits - bit_size);
1098 /* NOTE: transformed to SHR if unsigned */
1099 gen_op(TOK_SAR);
1100 r = gv(rc);
1101 } else {
1102 if (is_float(vtop->type.t) &&
1103 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1104 unsigned long offset;
1105 /* CPUs usually cannot use float constants, so we store them
1106 generically in data segment */
1107 size = type_size(&vtop->type, &align);
1108 offset = section_add(data_section, size, align);
1109 vpush_ref(&vtop->type, data_section, offset, size);
1110 vswap();
1111 init_putv(&vtop->type, data_section, offset);
1112 vtop->r |= VT_LVAL;
1114 #ifdef CONFIG_TCC_BCHECK
1115 if (vtop->r & VT_MUSTBOUND)
1116 gbound();
1117 #endif
1119 r = vtop->r & VT_VALMASK;
1120 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
1121 #ifndef TCC_TARGET_ARM64
1122 if (rc == RC_IRET)
1123 rc2 = RC_LRET;
1124 #ifdef TCC_TARGET_X86_64
1125 else if (rc == RC_FRET)
1126 rc2 = RC_QRET;
1127 #endif
1128 #endif
1129 /* need to reload if:
1130 - constant
1131 - lvalue (need to dereference pointer)
1132 - already a register, but not in the right class */
1133 if (r >= VT_CONST
1134 || (vtop->r & VT_LVAL)
1135 || !(reg_classes[r] & rc)
1136 #if PTR_SIZE == 8
1137 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
1138 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
1139 #else
1140 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
1141 #endif
1144 r = get_reg(rc);
1145 #if PTR_SIZE == 8
1146 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
1147 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
1148 #else
1149 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1150 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
1151 unsigned long long ll;
1152 #endif
1153 int r2, original_type;
1154 original_type = vtop->type.t;
1155 /* two register type load : expand to two words
1156 temporarily */
1157 #if PTR_SIZE == 4
1158 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1159 /* load constant */
1160 ll = vtop->c.i;
1161 vtop->c.i = ll; /* first word */
1162 load(r, vtop);
1163 vtop->r = r; /* save register value */
1164 vpushi(ll >> 32); /* second word */
1165 } else
1166 #endif
1167 if (vtop->r & VT_LVAL) {
1168 /* We do not want to modifier the long long
1169 pointer here, so the safest (and less
1170 efficient) is to save all the other registers
1171 in the stack. XXX: totally inefficient. */
1172 #if 0
1173 save_regs(1);
1174 #else
1175 /* lvalue_save: save only if used further down the stack */
1176 save_reg_upstack(vtop->r, 1);
1177 #endif
1178 /* load from memory */
1179 vtop->type.t = load_type;
1180 load(r, vtop);
1181 vdup();
1182 vtop[-1].r = r; /* save register value */
1183 /* increment pointer to get second word */
1184 vtop->type.t = addr_type;
1185 gaddrof();
1186 vpushi(load_size);
1187 gen_op('+');
1188 vtop->r |= VT_LVAL;
1189 vtop->type.t = load_type;
1190 } else {
1191 /* move registers */
1192 load(r, vtop);
1193 vdup();
1194 vtop[-1].r = r; /* save register value */
1195 vtop->r = vtop[-1].r2;
1197 /* Allocate second register. Here we rely on the fact that
1198 get_reg() tries first to free r2 of an SValue. */
1199 r2 = get_reg(rc2);
1200 load(r2, vtop);
1201 vpop();
1202 /* write second register */
1203 vtop->r2 = r2;
1204 vtop->type.t = original_type;
1205 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
1206 int t1, t;
1207 /* lvalue of scalar type : need to use lvalue type
1208 because of possible cast */
1209 t = vtop->type.t;
1210 t1 = t;
1211 /* compute memory access type */
1212 if (vtop->r & VT_LVAL_BYTE)
1213 t = VT_BYTE;
1214 else if (vtop->r & VT_LVAL_SHORT)
1215 t = VT_SHORT;
1216 if (vtop->r & VT_LVAL_UNSIGNED)
1217 t |= VT_UNSIGNED;
1218 vtop->type.t = t;
1219 load(r, vtop);
1220 /* restore wanted type */
1221 vtop->type.t = t1;
1222 } else {
1223 /* one register type load */
1224 load(r, vtop);
1227 vtop->r = r;
1228 #ifdef TCC_TARGET_C67
1229 /* uses register pairs for doubles */
1230 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1231 vtop->r2 = r+1;
1232 #endif
1234 return r;
1237 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1238 ST_FUNC void gv2(int rc1, int rc2)
1240 int v;
1242 /* generate more generic register first. But VT_JMP or VT_CMP
1243 values must be generated first in all cases to avoid possible
1244 reload errors */
1245 v = vtop[0].r & VT_VALMASK;
1246 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
1247 vswap();
1248 gv(rc1);
1249 vswap();
1250 gv(rc2);
1251 /* test if reload is needed for first register */
1252 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
1253 vswap();
1254 gv(rc1);
1255 vswap();
1257 } else {
1258 gv(rc2);
1259 vswap();
1260 gv(rc1);
1261 vswap();
1262 /* test if reload is needed for first register */
1263 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
1264 gv(rc2);
1269 #ifndef TCC_TARGET_ARM64
1270 /* wrapper around RC_FRET to return a register by type */
1271 static int rc_fret(int t)
1273 #ifdef TCC_TARGET_X86_64
1274 if (t == VT_LDOUBLE) {
1275 return RC_ST0;
1277 #endif
1278 return RC_FRET;
1280 #endif
1282 /* wrapper around REG_FRET to return a register by type */
1283 static int reg_fret(int t)
1285 #ifdef TCC_TARGET_X86_64
1286 if (t == VT_LDOUBLE) {
1287 return TREG_ST0;
1289 #endif
1290 return REG_FRET;
1293 #if PTR_SIZE == 4
1294 /* expand 64bit on stack in two ints */
1295 static void lexpand(void)
1297 int u, v;
1298 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
1299 v = vtop->r & (VT_VALMASK | VT_LVAL);
1300 if (v == VT_CONST) {
1301 vdup();
1302 vtop[0].c.i >>= 32;
1303 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1304 vdup();
1305 vtop[0].c.i += 4;
1306 } else {
1307 gv(RC_INT);
1308 vdup();
1309 vtop[0].r = vtop[-1].r2;
1310 vtop[0].r2 = vtop[-1].r2 = VT_CONST;
1312 vtop[0].type.t = vtop[-1].type.t = VT_INT | u;
1314 #endif
1316 #ifdef TCC_TARGET_ARM
1317 /* expand long long on stack */
1318 ST_FUNC void lexpand_nr(void)
1320 int u,v;
1322 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
1323 vdup();
1324 vtop->r2 = VT_CONST;
1325 vtop->type.t = VT_INT | u;
1326 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
1327 if (v == VT_CONST) {
1328 vtop[-1].c.i = vtop->c.i;
1329 vtop->c.i = vtop->c.i >> 32;
1330 vtop->r = VT_CONST;
1331 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1332 vtop->c.i += 4;
1333 vtop->r = vtop[-1].r;
1334 } else if (v > VT_CONST) {
1335 vtop--;
1336 lexpand();
1337 } else
1338 vtop->r = vtop[-1].r2;
1339 vtop[-1].r2 = VT_CONST;
1340 vtop[-1].type.t = VT_INT | u;
1342 #endif
1344 #if PTR_SIZE == 4
1345 /* build a long long from two ints */
1346 static void lbuild(int t)
1348 gv2(RC_INT, RC_INT);
1349 vtop[-1].r2 = vtop[0].r;
1350 vtop[-1].type.t = t;
1351 vpop();
1353 #endif
1355 /* convert stack entry to register and duplicate its value in another
1356 register */
1357 static void gv_dup(void)
1359 int rc, t, r, r1;
1360 SValue sv;
1362 t = vtop->type.t;
1363 #if PTR_SIZE == 4
1364 if ((t & VT_BTYPE) == VT_LLONG) {
1365 lexpand();
1366 gv_dup();
1367 vswap();
1368 vrotb(3);
1369 gv_dup();
1370 vrotb(4);
1371 /* stack: H L L1 H1 */
1372 lbuild(t);
1373 vrotb(3);
1374 vrotb(3);
1375 vswap();
1376 lbuild(t);
1377 vswap();
1378 } else
1379 #endif
1381 /* duplicate value */
1382 rc = RC_INT;
1383 sv.type.t = VT_INT;
1384 if (is_float(t)) {
1385 rc = RC_FLOAT;
1386 #ifdef TCC_TARGET_X86_64
1387 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1388 rc = RC_ST0;
1390 #endif
1391 sv.type.t = t;
1393 r = gv(rc);
1394 r1 = get_reg(rc);
1395 sv.r = r;
1396 sv.c.i = 0;
1397 load(r1, &sv); /* move r to r1 */
1398 vdup();
1399 /* duplicates value */
1400 if (r != r1)
1401 vtop->r = r1;
1405 /* Generate value test
1407 * Generate a test for any value (jump, comparison and integers) */
1408 ST_FUNC int gvtst(int inv, int t)
1410 int v = vtop->r & VT_VALMASK;
1411 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1412 vpushi(0);
1413 gen_op(TOK_NE);
1415 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1416 /* constant jmp optimization */
1417 if ((vtop->c.i != 0) != inv)
1418 t = gjmp(t);
1419 vtop--;
1420 return t;
1422 return gtst(inv, t);
1425 #if PTR_SIZE == 4
1426 /* generate CPU independent (unsigned) long long operations */
1427 static void gen_opl(int op)
1429 int t, a, b, op1, c, i;
1430 int func;
1431 unsigned short reg_iret = REG_IRET;
1432 unsigned short reg_lret = REG_LRET;
1433 SValue tmp;
1435 switch(op) {
1436 case '/':
1437 case TOK_PDIV:
1438 func = TOK___divdi3;
1439 goto gen_func;
1440 case TOK_UDIV:
1441 func = TOK___udivdi3;
1442 goto gen_func;
1443 case '%':
1444 func = TOK___moddi3;
1445 goto gen_mod_func;
1446 case TOK_UMOD:
1447 func = TOK___umoddi3;
1448 gen_mod_func:
1449 #ifdef TCC_ARM_EABI
1450 reg_iret = TREG_R2;
1451 reg_lret = TREG_R3;
1452 #endif
1453 gen_func:
1454 /* call generic long long function */
1455 vpush_global_sym(&func_old_type, func);
1456 vrott(3);
1457 gfunc_call(2);
1458 vpushi(0);
1459 vtop->r = reg_iret;
1460 vtop->r2 = reg_lret;
1461 break;
1462 case '^':
1463 case '&':
1464 case '|':
1465 case '*':
1466 case '+':
1467 case '-':
1468 //pv("gen_opl A",0,2);
1469 t = vtop->type.t;
1470 vswap();
1471 lexpand();
1472 vrotb(3);
1473 lexpand();
1474 /* stack: L1 H1 L2 H2 */
1475 tmp = vtop[0];
1476 vtop[0] = vtop[-3];
1477 vtop[-3] = tmp;
1478 tmp = vtop[-2];
1479 vtop[-2] = vtop[-3];
1480 vtop[-3] = tmp;
1481 vswap();
1482 /* stack: H1 H2 L1 L2 */
1483 //pv("gen_opl B",0,4);
1484 if (op == '*') {
1485 vpushv(vtop - 1);
1486 vpushv(vtop - 1);
1487 gen_op(TOK_UMULL);
1488 lexpand();
1489 /* stack: H1 H2 L1 L2 ML MH */
1490 for(i=0;i<4;i++)
1491 vrotb(6);
1492 /* stack: ML MH H1 H2 L1 L2 */
1493 tmp = vtop[0];
1494 vtop[0] = vtop[-2];
1495 vtop[-2] = tmp;
1496 /* stack: ML MH H1 L2 H2 L1 */
1497 gen_op('*');
1498 vrotb(3);
1499 vrotb(3);
1500 gen_op('*');
1501 /* stack: ML MH M1 M2 */
1502 gen_op('+');
1503 gen_op('+');
1504 } else if (op == '+' || op == '-') {
1505 /* XXX: add non carry method too (for MIPS or alpha) */
1506 if (op == '+')
1507 op1 = TOK_ADDC1;
1508 else
1509 op1 = TOK_SUBC1;
1510 gen_op(op1);
1511 /* stack: H1 H2 (L1 op L2) */
1512 vrotb(3);
1513 vrotb(3);
1514 gen_op(op1 + 1); /* TOK_xxxC2 */
1515 } else {
1516 gen_op(op);
1517 /* stack: H1 H2 (L1 op L2) */
1518 vrotb(3);
1519 vrotb(3);
1520 /* stack: (L1 op L2) H1 H2 */
1521 gen_op(op);
1522 /* stack: (L1 op L2) (H1 op H2) */
1524 /* stack: L H */
1525 lbuild(t);
1526 break;
1527 case TOK_SAR:
1528 case TOK_SHR:
1529 case TOK_SHL:
1530 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1531 t = vtop[-1].type.t;
1532 vswap();
1533 lexpand();
1534 vrotb(3);
1535 /* stack: L H shift */
1536 c = (int)vtop->c.i;
1537 /* constant: simpler */
1538 /* NOTE: all comments are for SHL. the other cases are
1539 done by swapping words */
1540 vpop();
1541 if (op != TOK_SHL)
1542 vswap();
1543 if (c >= 32) {
1544 /* stack: L H */
1545 vpop();
1546 if (c > 32) {
1547 vpushi(c - 32);
1548 gen_op(op);
1550 if (op != TOK_SAR) {
1551 vpushi(0);
1552 } else {
1553 gv_dup();
1554 vpushi(31);
1555 gen_op(TOK_SAR);
1557 vswap();
1558 } else {
1559 vswap();
1560 gv_dup();
1561 /* stack: H L L */
1562 vpushi(c);
1563 gen_op(op);
1564 vswap();
1565 vpushi(32 - c);
1566 if (op == TOK_SHL)
1567 gen_op(TOK_SHR);
1568 else
1569 gen_op(TOK_SHL);
1570 vrotb(3);
1571 /* stack: L L H */
1572 vpushi(c);
1573 if (op == TOK_SHL)
1574 gen_op(TOK_SHL);
1575 else
1576 gen_op(TOK_SHR);
1577 gen_op('|');
1579 if (op != TOK_SHL)
1580 vswap();
1581 lbuild(t);
1582 } else {
1583 /* XXX: should provide a faster fallback on x86 ? */
1584 switch(op) {
1585 case TOK_SAR:
1586 func = TOK___ashrdi3;
1587 goto gen_func;
1588 case TOK_SHR:
1589 func = TOK___lshrdi3;
1590 goto gen_func;
1591 case TOK_SHL:
1592 func = TOK___ashldi3;
1593 goto gen_func;
1596 break;
1597 default:
1598 /* compare operations */
1599 t = vtop->type.t;
1600 vswap();
1601 lexpand();
1602 vrotb(3);
1603 lexpand();
1604 /* stack: L1 H1 L2 H2 */
1605 tmp = vtop[-1];
1606 vtop[-1] = vtop[-2];
1607 vtop[-2] = tmp;
1608 /* stack: L1 L2 H1 H2 */
1609 /* compare high */
1610 op1 = op;
1611 /* when values are equal, we need to compare low words. since
1612 the jump is inverted, we invert the test too. */
1613 if (op1 == TOK_LT)
1614 op1 = TOK_LE;
1615 else if (op1 == TOK_GT)
1616 op1 = TOK_GE;
1617 else if (op1 == TOK_ULT)
1618 op1 = TOK_ULE;
1619 else if (op1 == TOK_UGT)
1620 op1 = TOK_UGE;
1621 a = 0;
1622 b = 0;
1623 gen_op(op1);
1624 if (op == TOK_NE) {
1625 b = gvtst(0, 0);
1626 } else {
1627 a = gvtst(1, 0);
1628 if (op != TOK_EQ) {
1629 /* generate non equal test */
1630 vpushi(TOK_NE);
1631 vtop->r = VT_CMP;
1632 b = gvtst(0, 0);
1635 /* compare low. Always unsigned */
1636 op1 = op;
1637 if (op1 == TOK_LT)
1638 op1 = TOK_ULT;
1639 else if (op1 == TOK_LE)
1640 op1 = TOK_ULE;
1641 else if (op1 == TOK_GT)
1642 op1 = TOK_UGT;
1643 else if (op1 == TOK_GE)
1644 op1 = TOK_UGE;
1645 gen_op(op1);
1646 a = gvtst(1, a);
1647 gsym(b);
1648 vseti(VT_JMPI, a);
1649 break;
1652 #endif
1654 static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b)
1656 uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b);
1657 return (a ^ b) >> 63 ? -x : x;
1660 static int gen_opic_lt(uint64_t a, uint64_t b)
1662 return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63);
1665 /* handle integer constant optimizations and various machine
1666 independent opt */
1667 static void gen_opic(int op)
1669 SValue *v1 = vtop - 1;
1670 SValue *v2 = vtop;
1671 int t1 = v1->type.t & VT_BTYPE;
1672 int t2 = v2->type.t & VT_BTYPE;
1673 int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1674 int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1675 uint64_t l1 = c1 ? v1->c.i : 0;
1676 uint64_t l2 = c2 ? v2->c.i : 0;
1677 int shm = (t1 == VT_LLONG) ? 63 : 31;
1679 if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
1680 l1 = ((uint32_t)l1 |
1681 (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
1682 if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR))
1683 l2 = ((uint32_t)l2 |
1684 (v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000)));
1686 if (c1 && c2) {
1687 switch(op) {
1688 case '+': l1 += l2; break;
1689 case '-': l1 -= l2; break;
1690 case '&': l1 &= l2; break;
1691 case '^': l1 ^= l2; break;
1692 case '|': l1 |= l2; break;
1693 case '*': l1 *= l2; break;
1695 case TOK_PDIV:
1696 case '/':
1697 case '%':
1698 case TOK_UDIV:
1699 case TOK_UMOD:
1700 /* if division by zero, generate explicit division */
1701 if (l2 == 0) {
1702 if (const_wanted)
1703 tcc_error("division by zero in constant");
1704 goto general_case;
1706 switch(op) {
1707 default: l1 = gen_opic_sdiv(l1, l2); break;
1708 case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break;
1709 case TOK_UDIV: l1 = l1 / l2; break;
1710 case TOK_UMOD: l1 = l1 % l2; break;
1712 break;
1713 case TOK_SHL: l1 <<= (l2 & shm); break;
1714 case TOK_SHR: l1 >>= (l2 & shm); break;
1715 case TOK_SAR:
1716 l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm);
1717 break;
1718 /* tests */
1719 case TOK_ULT: l1 = l1 < l2; break;
1720 case TOK_UGE: l1 = l1 >= l2; break;
1721 case TOK_EQ: l1 = l1 == l2; break;
1722 case TOK_NE: l1 = l1 != l2; break;
1723 case TOK_ULE: l1 = l1 <= l2; break;
1724 case TOK_UGT: l1 = l1 > l2; break;
1725 case TOK_LT: l1 = gen_opic_lt(l1, l2); break;
1726 case TOK_GE: l1 = !gen_opic_lt(l1, l2); break;
1727 case TOK_LE: l1 = !gen_opic_lt(l2, l1); break;
1728 case TOK_GT: l1 = gen_opic_lt(l2, l1); break;
1729 /* logical */
1730 case TOK_LAND: l1 = l1 && l2; break;
1731 case TOK_LOR: l1 = l1 || l2; break;
1732 default:
1733 goto general_case;
1735 if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
1736 l1 = ((uint32_t)l1 |
1737 (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
1738 v1->c.i = l1;
1739 vtop--;
1740 } else {
1741 /* if commutative ops, put c2 as constant */
1742 if (c1 && (op == '+' || op == '&' || op == '^' ||
1743 op == '|' || op == '*')) {
1744 vswap();
1745 c2 = c1; //c = c1, c1 = c2, c2 = c;
1746 l2 = l1; //l = l1, l1 = l2, l2 = l;
1748 if (!const_wanted &&
1749 c1 && ((l1 == 0 &&
1750 (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
1751 (l1 == -1 && op == TOK_SAR))) {
1752 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
1753 vtop--;
1754 } else if (!const_wanted &&
1755 c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
1756 (op == '|' &&
1757 (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))) ||
1758 (l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
1759 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
1760 if (l2 == 1)
1761 vtop->c.i = 0;
1762 vswap();
1763 vtop--;
1764 } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1765 op == TOK_PDIV) &&
1766 l2 == 1) ||
1767 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1768 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1769 l2 == 0) ||
1770 (op == '&' &&
1771 (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))))) {
1772 /* filter out NOP operations like x*1, x-0, x&-1... */
1773 vtop--;
1774 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1775 /* try to use shifts instead of muls or divs */
1776 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1777 int n = -1;
1778 while (l2) {
1779 l2 >>= 1;
1780 n++;
1782 vtop->c.i = n;
1783 if (op == '*')
1784 op = TOK_SHL;
1785 else if (op == TOK_PDIV)
1786 op = TOK_SAR;
1787 else
1788 op = TOK_SHR;
1790 goto general_case;
1791 } else if (c2 && (op == '+' || op == '-') &&
1792 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1793 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1794 /* symbol + constant case */
1795 if (op == '-')
1796 l2 = -l2;
1797 l2 += vtop[-1].c.i;
1798 /* The backends can't always deal with addends to symbols
1799 larger than +-1<<31. Don't construct such. */
1800 if ((int)l2 != l2)
1801 goto general_case;
1802 vtop--;
1803 vtop->c.i = l2;
1804 } else {
1805 general_case:
1806 /* call low level op generator */
1807 if (t1 == VT_LLONG || t2 == VT_LLONG ||
1808 (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
1809 gen_opl(op);
1810 else
1811 gen_opi(op);
1816 /* generate a floating point operation with constant propagation */
1817 static void gen_opif(int op)
1819 int c1, c2;
1820 SValue *v1, *v2;
1821 #if defined _MSC_VER && defined _AMD64_
1822 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
1823 volatile
1824 #endif
1825 long double f1, f2;
1827 v1 = vtop - 1;
1828 v2 = vtop;
1829 /* currently, we cannot do computations with forward symbols */
1830 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1831 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1832 if (c1 && c2) {
1833 if (v1->type.t == VT_FLOAT) {
1834 f1 = v1->c.f;
1835 f2 = v2->c.f;
1836 } else if (v1->type.t == VT_DOUBLE) {
1837 f1 = v1->c.d;
1838 f2 = v2->c.d;
1839 } else {
1840 f1 = v1->c.ld;
1841 f2 = v2->c.ld;
1844 /* NOTE: we only do constant propagation if finite number (not
1845 NaN or infinity) (ANSI spec) */
1846 if (!ieee_finite(f1) || !ieee_finite(f2))
1847 goto general_case;
1849 switch(op) {
1850 case '+': f1 += f2; break;
1851 case '-': f1 -= f2; break;
1852 case '*': f1 *= f2; break;
1853 case '/':
1854 if (f2 == 0.0) {
1855 if (const_wanted)
1856 tcc_error("division by zero in constant");
1857 goto general_case;
1859 f1 /= f2;
1860 break;
1861 /* XXX: also handles tests ? */
1862 default:
1863 goto general_case;
1865 /* XXX: overflow test ? */
1866 if (v1->type.t == VT_FLOAT) {
1867 v1->c.f = f1;
1868 } else if (v1->type.t == VT_DOUBLE) {
1869 v1->c.d = f1;
1870 } else {
1871 v1->c.ld = f1;
1873 vtop--;
1874 } else {
1875 general_case:
1876 gen_opf(op);
1880 static int pointed_size(CType *type)
1882 int align;
1883 return type_size(pointed_type(type), &align);
1886 static void vla_runtime_pointed_size(CType *type)
1888 int align;
1889 vla_runtime_type_size(pointed_type(type), &align);
1892 static inline int is_null_pointer(SValue *p)
1894 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1895 return 0;
1896 return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) ||
1897 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) ||
1898 ((p->type.t & VT_BTYPE) == VT_PTR &&
1899 (PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0));
1902 static inline int is_integer_btype(int bt)
1904 return (bt == VT_BYTE || bt == VT_SHORT ||
1905 bt == VT_INT || bt == VT_LLONG);
1908 /* check types for comparison or subtraction of pointers */
1909 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1911 CType *type1, *type2, tmp_type1, tmp_type2;
1912 int bt1, bt2;
1914 /* null pointers are accepted for all comparisons as gcc */
1915 if (is_null_pointer(p1) || is_null_pointer(p2))
1916 return;
1917 type1 = &p1->type;
1918 type2 = &p2->type;
1919 bt1 = type1->t & VT_BTYPE;
1920 bt2 = type2->t & VT_BTYPE;
1921 /* accept comparison between pointer and integer with a warning */
1922 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1923 if (op != TOK_LOR && op != TOK_LAND )
1924 tcc_warning("comparison between pointer and integer");
1925 return;
1928 /* both must be pointers or implicit function pointers */
1929 if (bt1 == VT_PTR) {
1930 type1 = pointed_type(type1);
1931 } else if (bt1 != VT_FUNC)
1932 goto invalid_operands;
1934 if (bt2 == VT_PTR) {
1935 type2 = pointed_type(type2);
1936 } else if (bt2 != VT_FUNC) {
1937 invalid_operands:
1938 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1940 if ((type1->t & VT_BTYPE) == VT_VOID ||
1941 (type2->t & VT_BTYPE) == VT_VOID)
1942 return;
1943 tmp_type1 = *type1;
1944 tmp_type2 = *type2;
1945 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1946 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1947 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1948 /* gcc-like error if '-' is used */
1949 if (op == '-')
1950 goto invalid_operands;
1951 else
1952 tcc_warning("comparison of distinct pointer types lacks a cast");
1956 /* generic gen_op: handles types problems */
1957 ST_FUNC void gen_op(int op)
1959 int u, t1, t2, bt1, bt2, t;
1960 CType type1;
1962 redo:
1963 t1 = vtop[-1].type.t;
1964 t2 = vtop[0].type.t;
1965 bt1 = t1 & VT_BTYPE;
1966 bt2 = t2 & VT_BTYPE;
1968 if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1969 tcc_error("operation on a struct");
1970 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
1971 if (bt2 == VT_FUNC) {
1972 mk_pointer(&vtop->type);
1973 gaddrof();
1975 if (bt1 == VT_FUNC) {
1976 vswap();
1977 mk_pointer(&vtop->type);
1978 gaddrof();
1979 vswap();
1981 goto redo;
1982 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
1983 /* at least one operand is a pointer */
1984 /* relational op: must be both pointers */
1985 if (op >= TOK_ULT && op <= TOK_LOR) {
1986 check_comparison_pointer_types(vtop - 1, vtop, op);
1987 /* pointers are handled are unsigned */
1988 #if PTR_SIZE == 8
1989 t = VT_LLONG | VT_UNSIGNED;
1990 #else
1991 t = VT_INT | VT_UNSIGNED;
1992 #endif
1993 goto std_op;
1995 /* if both pointers, then it must be the '-' op */
1996 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1997 if (op != '-')
1998 tcc_error("cannot use pointers here");
1999 check_comparison_pointer_types(vtop - 1, vtop, op);
2000 /* XXX: check that types are compatible */
2001 if (vtop[-1].type.t & VT_VLA) {
2002 vla_runtime_pointed_size(&vtop[-1].type);
2003 } else {
2004 vpushi(pointed_size(&vtop[-1].type));
2006 vrott(3);
2007 gen_opic(op);
2008 /* set to integer type */
2009 #if PTR_SIZE == 8
2010 vtop->type.t = VT_LLONG;
2011 #else
2012 vtop->type.t = VT_INT;
2013 #endif
2014 vswap();
2015 gen_op(TOK_PDIV);
2016 } else {
2017 /* exactly one pointer : must be '+' or '-'. */
2018 if (op != '-' && op != '+')
2019 tcc_error("cannot use pointers here");
2020 /* Put pointer as first operand */
2021 if (bt2 == VT_PTR) {
2022 vswap();
2023 t = t1, t1 = t2, t2 = t;
2025 #if PTR_SIZE == 4
2026 if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG)
2027 /* XXX: truncate here because gen_opl can't handle ptr + long long */
2028 gen_cast_s(VT_INT);
2029 #endif
2030 type1 = vtop[-1].type;
2031 type1.t &= ~VT_ARRAY;
2032 if (vtop[-1].type.t & VT_VLA)
2033 vla_runtime_pointed_size(&vtop[-1].type);
2034 else {
2035 u = pointed_size(&vtop[-1].type);
2036 if (u < 0)
2037 tcc_error("unknown array element size");
2038 #if PTR_SIZE == 8
2039 vpushll(u);
2040 #else
2041 /* XXX: cast to int ? (long long case) */
2042 vpushi(u);
2043 #endif
2045 gen_op('*');
2046 #if 0
2047 /* #ifdef CONFIG_TCC_BCHECK
2048 The main reason to removing this code:
2049 #include <stdio.h>
2050 int main ()
2052 int v[10];
2053 int i = 10;
2054 int j = 9;
2055 fprintf(stderr, "v+i-j = %p\n", v+i-j);
2056 fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
2058 When this code is on. then the output looks like
2059 v+i-j = 0xfffffffe
2060 v+(i-j) = 0xbff84000
2062 /* if evaluating constant expression, no code should be
2063 generated, so no bound check */
2064 if (tcc_state->do_bounds_check && !const_wanted) {
2065 /* if bounded pointers, we generate a special code to
2066 test bounds */
2067 if (op == '-') {
2068 vpushi(0);
2069 vswap();
2070 gen_op('-');
2072 gen_bounded_ptr_add();
2073 } else
2074 #endif
2076 gen_opic(op);
2078 /* put again type if gen_opic() swaped operands */
2079 vtop->type = type1;
2081 } else if (is_float(bt1) || is_float(bt2)) {
2082 /* compute bigger type and do implicit casts */
2083 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
2084 t = VT_LDOUBLE;
2085 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
2086 t = VT_DOUBLE;
2087 } else {
2088 t = VT_FLOAT;
2090 /* floats can only be used for a few operations */
2091 if (op != '+' && op != '-' && op != '*' && op != '/' &&
2092 (op < TOK_ULT || op > TOK_GT))
2093 tcc_error("invalid operands for binary operation");
2094 goto std_op;
2095 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
2096 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
2097 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED))
2098 t |= VT_UNSIGNED;
2099 goto std_op;
2100 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
2101 /* cast to biggest op */
2102 t = VT_LLONG;
2103 /* convert to unsigned if it does not fit in a long long */
2104 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
2105 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
2106 t |= VT_UNSIGNED;
2107 goto std_op;
2108 } else {
2109 /* integer operations */
2110 t = VT_INT;
2111 /* convert to unsigned if it does not fit in an integer */
2112 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
2113 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
2114 t |= VT_UNSIGNED;
2115 std_op:
2116 /* XXX: currently, some unsigned operations are explicit, so
2117 we modify them here */
2118 if (t & VT_UNSIGNED) {
2119 if (op == TOK_SAR)
2120 op = TOK_SHR;
2121 else if (op == '/')
2122 op = TOK_UDIV;
2123 else if (op == '%')
2124 op = TOK_UMOD;
2125 else if (op == TOK_LT)
2126 op = TOK_ULT;
2127 else if (op == TOK_GT)
2128 op = TOK_UGT;
2129 else if (op == TOK_LE)
2130 op = TOK_ULE;
2131 else if (op == TOK_GE)
2132 op = TOK_UGE;
2134 vswap();
2135 type1.t = t;
2136 type1.ref = NULL;
2137 gen_cast(&type1);
2138 vswap();
2139 /* special case for shifts and long long: we keep the shift as
2140 an integer */
2141 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
2142 type1.t = VT_INT;
2143 gen_cast(&type1);
2144 if (is_float(t))
2145 gen_opif(op);
2146 else
2147 gen_opic(op);
2148 if (op >= TOK_ULT && op <= TOK_GT) {
2149 /* relational op: the result is an int */
2150 vtop->type.t = VT_INT;
2151 } else {
2152 vtop->type.t = t;
2155 // Make sure that we have converted to an rvalue:
2156 if (vtop->r & VT_LVAL)
2157 gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
2160 #ifndef TCC_TARGET_ARM
2161 /* generic itof for unsigned long long case */
2162 static void gen_cvt_itof1(int t)
2164 #ifdef TCC_TARGET_ARM64
2165 gen_cvt_itof(t);
2166 #else
2167 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2168 (VT_LLONG | VT_UNSIGNED)) {
2170 if (t == VT_FLOAT)
2171 vpush_global_sym(&func_old_type, TOK___floatundisf);
2172 #if LDOUBLE_SIZE != 8
2173 else if (t == VT_LDOUBLE)
2174 vpush_global_sym(&func_old_type, TOK___floatundixf);
2175 #endif
2176 else
2177 vpush_global_sym(&func_old_type, TOK___floatundidf);
2178 vrott(2);
2179 gfunc_call(1);
2180 vpushi(0);
2181 vtop->r = reg_fret(t);
2182 } else {
2183 gen_cvt_itof(t);
2185 #endif
2187 #endif
2189 /* generic ftoi for unsigned long long case */
2190 static void gen_cvt_ftoi1(int t)
2192 #ifdef TCC_TARGET_ARM64
2193 gen_cvt_ftoi(t);
2194 #else
2195 int st;
2197 if (t == (VT_LLONG | VT_UNSIGNED)) {
2198 /* not handled natively */
2199 st = vtop->type.t & VT_BTYPE;
2200 if (st == VT_FLOAT)
2201 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
2202 #if LDOUBLE_SIZE != 8
2203 else if (st == VT_LDOUBLE)
2204 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
2205 #endif
2206 else
2207 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
2208 vrott(2);
2209 gfunc_call(1);
2210 vpushi(0);
2211 vtop->r = REG_IRET;
2212 vtop->r2 = REG_LRET;
2213 } else {
2214 gen_cvt_ftoi(t);
2216 #endif
2219 /* force char or short cast */
2220 static void force_charshort_cast(int t)
2222 int bits, dbt;
2223 dbt = t & VT_BTYPE;
2224 /* XXX: add optimization if lvalue : just change type and offset */
2225 if (dbt == VT_BYTE)
2226 bits = 8;
2227 else
2228 bits = 16;
2229 if (t & VT_UNSIGNED) {
2230 vpushi((1 << bits) - 1);
2231 gen_op('&');
2232 } else {
2233 if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
2234 bits = 64 - bits;
2235 else
2236 bits = 32 - bits;
2237 vpushi(bits);
2238 gen_op(TOK_SHL);
2239 /* result must be signed or the SAR is converted to an SHL
2240 This was not the case when "t" was a signed short
2241 and the last value on the stack was an unsigned int */
2242 vtop->type.t &= ~VT_UNSIGNED;
2243 vpushi(bits);
2244 gen_op(TOK_SAR);
2248 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2249 static void gen_cast_s(int t)
2251 CType type;
2252 type.t = t;
2253 type.ref = NULL;
2254 gen_cast(&type);
2257 static void gen_cast(CType *type)
2259 int sbt, dbt, sf, df, c, p;
2261 /* special delayed cast for char/short */
2262 /* XXX: in some cases (multiple cascaded casts), it may still
2263 be incorrect */
2264 if (vtop->r & VT_MUSTCAST) {
2265 vtop->r &= ~VT_MUSTCAST;
2266 force_charshort_cast(vtop->type.t);
2269 /* bitfields first get cast to ints */
2270 if (vtop->type.t & VT_BITFIELD) {
2271 gv(RC_INT);
2274 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
2275 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
2277 if (sbt != dbt) {
2278 sf = is_float(sbt);
2279 df = is_float(dbt);
2280 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2281 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
2282 if (c) {
2283 /* constant case: we can do it now */
2284 /* XXX: in ISOC, cannot do it if error in convert */
2285 if (sbt == VT_FLOAT)
2286 vtop->c.ld = vtop->c.f;
2287 else if (sbt == VT_DOUBLE)
2288 vtop->c.ld = vtop->c.d;
2290 if (df) {
2291 if ((sbt & VT_BTYPE) == VT_LLONG) {
2292 if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 63))
2293 vtop->c.ld = vtop->c.i;
2294 else
2295 vtop->c.ld = -(long double)-vtop->c.i;
2296 } else if(!sf) {
2297 if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 31))
2298 vtop->c.ld = (uint32_t)vtop->c.i;
2299 else
2300 vtop->c.ld = -(long double)-(uint32_t)vtop->c.i;
2303 if (dbt == VT_FLOAT)
2304 vtop->c.f = (float)vtop->c.ld;
2305 else if (dbt == VT_DOUBLE)
2306 vtop->c.d = (double)vtop->c.ld;
2307 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
2308 vtop->c.i = vtop->c.ld;
2309 } else if (sf && dbt == VT_BOOL) {
2310 vtop->c.i = (vtop->c.ld != 0);
2311 } else {
2312 if(sf)
2313 vtop->c.i = vtop->c.ld;
2314 else if (sbt == (VT_LLONG|VT_UNSIGNED))
2316 else if (sbt & VT_UNSIGNED)
2317 vtop->c.i = (uint32_t)vtop->c.i;
2318 #if PTR_SIZE == 8
2319 else if (sbt == VT_PTR)
2321 #endif
2322 else if (sbt != VT_LLONG)
2323 vtop->c.i = ((uint32_t)vtop->c.i |
2324 -(vtop->c.i & 0x80000000));
2326 if (dbt == (VT_LLONG|VT_UNSIGNED))
2328 else if (dbt == VT_BOOL)
2329 vtop->c.i = (vtop->c.i != 0);
2330 #if PTR_SIZE == 8
2331 else if (dbt == VT_PTR)
2333 #endif
2334 else if (dbt != VT_LLONG) {
2335 uint32_t m = ((dbt & VT_BTYPE) == VT_BYTE ? 0xff :
2336 (dbt & VT_BTYPE) == VT_SHORT ? 0xffff :
2337 0xffffffff);
2338 vtop->c.i &= m;
2339 if (!(dbt & VT_UNSIGNED))
2340 vtop->c.i |= -(vtop->c.i & ((m >> 1) + 1));
2343 } else if (p && dbt == VT_BOOL) {
2344 vtop->r = VT_CONST;
2345 vtop->c.i = 1;
2346 } else {
2347 /* non constant case: generate code */
2348 if (sf && df) {
2349 /* convert from fp to fp */
2350 gen_cvt_ftof(dbt);
2351 } else if (df) {
2352 /* convert int to fp */
2353 gen_cvt_itof1(dbt);
2354 } else if (sf) {
2355 /* convert fp to int */
2356 if (dbt == VT_BOOL) {
2357 vpushi(0);
2358 gen_op(TOK_NE);
2359 } else {
2360 /* we handle char/short/etc... with generic code */
2361 if (dbt != (VT_INT | VT_UNSIGNED) &&
2362 dbt != (VT_LLONG | VT_UNSIGNED) &&
2363 dbt != VT_LLONG)
2364 dbt = VT_INT;
2365 gen_cvt_ftoi1(dbt);
2366 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
2367 /* additional cast for char/short... */
2368 vtop->type.t = dbt;
2369 gen_cast(type);
2372 #if PTR_SIZE == 4
2373 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2374 if ((sbt & VT_BTYPE) != VT_LLONG) {
2375 /* scalar to long long */
2376 /* machine independent conversion */
2377 gv(RC_INT);
2378 /* generate high word */
2379 if (sbt == (VT_INT | VT_UNSIGNED)) {
2380 vpushi(0);
2381 gv(RC_INT);
2382 } else {
2383 if (sbt == VT_PTR) {
2384 /* cast from pointer to int before we apply
2385 shift operation, which pointers don't support*/
2386 gen_cast_s(VT_INT);
2388 gv_dup();
2389 vpushi(31);
2390 gen_op(TOK_SAR);
2392 /* patch second register */
2393 vtop[-1].r2 = vtop->r;
2394 vpop();
2396 #else
2397 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2398 (dbt & VT_BTYPE) == VT_PTR ||
2399 (dbt & VT_BTYPE) == VT_FUNC) {
2400 if ((sbt & VT_BTYPE) != VT_LLONG &&
2401 (sbt & VT_BTYPE) != VT_PTR &&
2402 (sbt & VT_BTYPE) != VT_FUNC) {
2403 /* need to convert from 32bit to 64bit */
2404 gv(RC_INT);
2405 if (sbt != (VT_INT | VT_UNSIGNED)) {
2406 #if defined(TCC_TARGET_ARM64)
2407 gen_cvt_sxtw();
2408 #elif defined(TCC_TARGET_X86_64)
2409 int r = gv(RC_INT);
2410 /* x86_64 specific: movslq */
2411 o(0x6348);
2412 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2413 #else
2414 #error
2415 #endif
2418 #endif
2419 } else if (dbt == VT_BOOL) {
2420 /* scalar to bool */
2421 vpushi(0);
2422 gen_op(TOK_NE);
2423 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2424 (dbt & VT_BTYPE) == VT_SHORT) {
2425 if (sbt == VT_PTR) {
2426 vtop->type.t = VT_INT;
2427 tcc_warning("nonportable conversion from pointer to char/short");
2429 force_charshort_cast(dbt);
2430 #if PTR_SIZE == 4
2431 } else if ((dbt & VT_BTYPE) == VT_INT) {
2432 /* scalar to int */
2433 if ((sbt & VT_BTYPE) == VT_LLONG) {
2434 /* from long long: just take low order word */
2435 lexpand();
2436 vpop();
2438 /* if lvalue and single word type, nothing to do because
2439 the lvalue already contains the real type size (see
2440 VT_LVAL_xxx constants) */
2441 #endif
2444 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2445 /* if we are casting between pointer types,
2446 we must update the VT_LVAL_xxx size */
2447 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2448 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2450 vtop->type = *type;
2453 /* return type size as known at compile time. Put alignment at 'a' */
2454 ST_FUNC int type_size(CType *type, int *a)
2456 Sym *s;
2457 int bt;
2459 bt = type->t & VT_BTYPE;
2460 if (bt == VT_STRUCT) {
2461 /* struct/union */
2462 s = type->ref;
2463 *a = s->r;
2464 return s->c;
2465 } else if (bt == VT_PTR) {
2466 if (type->t & VT_ARRAY) {
2467 int ts;
2469 s = type->ref;
2470 ts = type_size(&s->type, a);
2472 if (ts < 0 && s->c < 0)
2473 ts = -ts;
2475 return ts * s->c;
2476 } else {
2477 *a = PTR_SIZE;
2478 return PTR_SIZE;
2480 } else if (IS_ENUM(type->t) && type->ref->c == -1) {
2481 return -1; /* incomplete enum */
2482 } else if (bt == VT_LDOUBLE) {
2483 *a = LDOUBLE_ALIGN;
2484 return LDOUBLE_SIZE;
2485 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2486 #ifdef TCC_TARGET_I386
2487 #ifdef TCC_TARGET_PE
2488 *a = 8;
2489 #else
2490 *a = 4;
2491 #endif
2492 #elif defined(TCC_TARGET_ARM)
2493 #ifdef TCC_ARM_EABI
2494 *a = 8;
2495 #else
2496 *a = 4;
2497 #endif
2498 #else
2499 *a = 8;
2500 #endif
2501 return 8;
2502 } else if (bt == VT_INT || bt == VT_FLOAT) {
2503 *a = 4;
2504 return 4;
2505 } else if (bt == VT_SHORT) {
2506 *a = 2;
2507 return 2;
2508 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2509 *a = 8;
2510 return 16;
2511 } else {
2512 /* char, void, function, _Bool */
2513 *a = 1;
2514 return 1;
2518 /* push type size as known at runtime time on top of value stack. Put
2519 alignment at 'a' */
2520 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2522 if (type->t & VT_VLA) {
2523 type_size(&type->ref->type, a);
2524 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2525 } else {
2526 vpushi(type_size(type, a));
2530 static void vla_sp_restore(void) {
2531 if (vlas_in_scope) {
2532 gen_vla_sp_restore(vla_sp_loc);
2536 static void vla_sp_restore_root(void) {
2537 if (vlas_in_scope) {
2538 gen_vla_sp_restore(vla_sp_root_loc);
2542 /* return the pointed type of t */
2543 static inline CType *pointed_type(CType *type)
2545 return &type->ref->type;
2548 /* modify type so that its it is a pointer to type. */
2549 ST_FUNC void mk_pointer(CType *type)
2551 Sym *s;
2552 s = sym_push(SYM_FIELD, type, 0, -1);
2553 type->t = VT_PTR | (type->t & VT_STORAGE);
2554 type->ref = s;
2557 /* compare function types. OLD functions match any new functions */
2558 static int is_compatible_func(CType *type1, CType *type2)
2560 Sym *s1, *s2;
2562 s1 = type1->ref;
2563 s2 = type2->ref;
2564 if (!is_compatible_types(&s1->type, &s2->type))
2565 return 0;
2566 /* check func_call */
2567 if (s1->f.func_call != s2->f.func_call)
2568 return 0;
2569 /* XXX: not complete */
2570 if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD)
2571 return 1;
2572 if (s1->f.func_type != s2->f.func_type)
2573 return 0;
2574 while (s1 != NULL) {
2575 if (s2 == NULL)
2576 return 0;
2577 if (!is_compatible_unqualified_types(&s1->type, &s2->type))
2578 return 0;
2579 s1 = s1->next;
2580 s2 = s2->next;
2582 if (s2)
2583 return 0;
2584 return 1;
2587 /* return true if type1 and type2 are the same. If unqualified is
2588 true, qualifiers on the types are ignored.
2590 - enums are not checked as gcc __builtin_types_compatible_p ()
2592 static int compare_types(CType *type1, CType *type2, int unqualified)
2594 int bt1, t1, t2;
2596 t1 = type1->t & VT_TYPE;
2597 t2 = type2->t & VT_TYPE;
2598 if (unqualified) {
2599 /* strip qualifiers before comparing */
2600 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2601 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2603 /* Default Vs explicit signedness only matters for char */
2604 if ((t1 & VT_BTYPE) != VT_BYTE) {
2605 t1 &= ~VT_DEFSIGN;
2606 t2 &= ~VT_DEFSIGN;
2609 /* XXX: bitfields ? */
2610 if (t1 != t2)
2611 return 0;
2612 /* test more complicated cases */
2613 bt1 = t1 & VT_BTYPE;
2614 if (bt1 == VT_PTR) {
2615 type1 = pointed_type(type1);
2616 type2 = pointed_type(type2);
2617 return is_compatible_types(type1, type2);
2618 } else if (bt1 == VT_STRUCT) {
2619 return (type1->ref == type2->ref);
2620 } else if (bt1 == VT_FUNC) {
2621 return is_compatible_func(type1, type2);
2622 } else {
2623 return 1;
2627 /* return true if type1 and type2 are exactly the same (including
2628 qualifiers).
2630 static int is_compatible_types(CType *type1, CType *type2)
2632 return compare_types(type1,type2,0);
2635 /* return true if type1 and type2 are the same (ignoring qualifiers).
2637 static int is_compatible_unqualified_types(CType *type1, CType *type2)
2639 return compare_types(type1,type2,1);
2642 /* print a type. If 'varstr' is not NULL, then the variable is also
2643 printed in the type */
2644 /* XXX: union */
2645 /* XXX: add array and function pointers */
2646 static void type_to_str(char *buf, int buf_size,
2647 CType *type, const char *varstr)
2649 int bt, v, t;
2650 Sym *s, *sa;
2651 char buf1[256];
2652 const char *tstr;
2654 t = type->t;
2655 bt = t & VT_BTYPE;
2656 buf[0] = '\0';
2657 if (t & VT_CONSTANT)
2658 pstrcat(buf, buf_size, "const ");
2659 if (t & VT_VOLATILE)
2660 pstrcat(buf, buf_size, "volatile ");
2661 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2662 pstrcat(buf, buf_size, "unsigned ");
2663 else if (t & VT_DEFSIGN)
2664 pstrcat(buf, buf_size, "signed ");
2665 if (t & VT_EXTERN)
2666 pstrcat(buf, buf_size, "extern ");
2667 if (t & VT_STATIC)
2668 pstrcat(buf, buf_size, "static ");
2669 if (t & VT_TYPEDEF)
2670 pstrcat(buf, buf_size, "typedef ");
2671 if (t & VT_INLINE)
2672 pstrcat(buf, buf_size, "inline ");
2673 buf_size -= strlen(buf);
2674 buf += strlen(buf);
2675 if (IS_ENUM(type->t)) {
2676 tstr = "enum ";
2677 goto tstruct;
2679 switch(bt) {
2680 case VT_VOID:
2681 tstr = "void";
2682 goto add_tstr;
2683 case VT_BOOL:
2684 tstr = "_Bool";
2685 goto add_tstr;
2686 case VT_BYTE:
2687 tstr = "char";
2688 goto add_tstr;
2689 case VT_SHORT:
2690 tstr = "short";
2691 goto add_tstr;
2692 case VT_INT:
2693 tstr = "int";
2694 goto add_tstr;
2695 case VT_LONG:
2696 tstr = "long";
2697 goto add_tstr;
2698 case VT_LLONG:
2699 tstr = "long long";
2700 goto add_tstr;
2701 case VT_FLOAT:
2702 tstr = "float";
2703 goto add_tstr;
2704 case VT_DOUBLE:
2705 tstr = "double";
2706 goto add_tstr;
2707 case VT_LDOUBLE:
2708 tstr = "long double";
2709 add_tstr:
2710 pstrcat(buf, buf_size, tstr);
2711 break;
2712 case VT_STRUCT:
2713 tstr = "struct ";
2714 if (IS_UNION(t))
2715 tstr = "union ";
2716 tstruct:
2717 pstrcat(buf, buf_size, tstr);
2718 v = type->ref->v & ~SYM_STRUCT;
2719 if (v >= SYM_FIRST_ANOM)
2720 pstrcat(buf, buf_size, "<anonymous>");
2721 else
2722 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2723 break;
2724 case VT_FUNC:
2725 s = type->ref;
2726 type_to_str(buf, buf_size, &s->type, varstr);
2727 pstrcat(buf, buf_size, "(");
2728 sa = s->next;
2729 while (sa != NULL) {
2730 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2731 pstrcat(buf, buf_size, buf1);
2732 sa = sa->next;
2733 if (sa)
2734 pstrcat(buf, buf_size, ", ");
2736 pstrcat(buf, buf_size, ")");
2737 goto no_var;
2738 case VT_PTR:
2739 s = type->ref;
2740 if (t & VT_ARRAY) {
2741 snprintf(buf1, sizeof(buf1), "%s[%d]", varstr ? varstr : "", s->c);
2742 type_to_str(buf, buf_size, &s->type, buf1);
2743 goto no_var;
2745 pstrcpy(buf1, sizeof(buf1), "*");
2746 if (t & VT_CONSTANT)
2747 pstrcat(buf1, buf_size, "const ");
2748 if (t & VT_VOLATILE)
2749 pstrcat(buf1, buf_size, "volatile ");
2750 if (varstr)
2751 pstrcat(buf1, sizeof(buf1), varstr);
2752 type_to_str(buf, buf_size, &s->type, buf1);
2753 goto no_var;
2755 if (varstr) {
2756 pstrcat(buf, buf_size, " ");
2757 pstrcat(buf, buf_size, varstr);
2759 no_var: ;
2762 /* verify type compatibility to store vtop in 'dt' type, and generate
2763 casts if needed. */
2764 static void gen_assign_cast(CType *dt)
2766 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2767 char buf1[256], buf2[256];
2768 int dbt, sbt;
2770 st = &vtop->type; /* source type */
2771 dbt = dt->t & VT_BTYPE;
2772 sbt = st->t & VT_BTYPE;
2773 if (sbt == VT_VOID || dbt == VT_VOID) {
2774 if (sbt == VT_VOID && dbt == VT_VOID)
2775 ; /*
2776 It is Ok if both are void
2777 A test program:
2778 void func1() {}
2779 void func2() {
2780 return func1();
2782 gcc accepts this program
2784 else
2785 tcc_error("cannot cast from/to void");
2787 if (dt->t & VT_CONSTANT)
2788 tcc_warning("assignment of read-only location");
2789 switch(dbt) {
2790 case VT_PTR:
2791 /* special cases for pointers */
2792 /* '0' can also be a pointer */
2793 if (is_null_pointer(vtop))
2794 goto type_ok;
2795 /* accept implicit pointer to integer cast with warning */
2796 if (is_integer_btype(sbt)) {
2797 tcc_warning("assignment makes pointer from integer without a cast");
2798 goto type_ok;
2800 type1 = pointed_type(dt);
2801 /* a function is implicitly a function pointer */
2802 if (sbt == VT_FUNC) {
2803 if ((type1->t & VT_BTYPE) != VT_VOID &&
2804 !is_compatible_types(pointed_type(dt), st))
2805 tcc_warning("assignment from incompatible pointer type");
2806 goto type_ok;
2808 if (sbt != VT_PTR)
2809 goto error;
2810 type2 = pointed_type(st);
2811 if ((type1->t & VT_BTYPE) == VT_VOID ||
2812 (type2->t & VT_BTYPE) == VT_VOID) {
2813 /* void * can match anything */
2814 } else {
2815 //printf("types %08x %08x\n", type1->t, type2->t);
2816 /* exact type match, except for qualifiers */
2817 if (!is_compatible_unqualified_types(type1, type2)) {
2818 /* Like GCC don't warn by default for merely changes
2819 in pointer target signedness. Do warn for different
2820 base types, though, in particular for unsigned enums
2821 and signed int targets. */
2822 if ((type1->t & VT_BTYPE) != (type2->t & VT_BTYPE)
2823 || IS_ENUM(type1->t) || IS_ENUM(type2->t)
2825 tcc_warning("assignment from incompatible pointer type");
2828 /* check const and volatile */
2829 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2830 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2831 tcc_warning("assignment discards qualifiers from pointer target type");
2832 break;
2833 case VT_BYTE:
2834 case VT_SHORT:
2835 case VT_INT:
2836 case VT_LLONG:
2837 if (sbt == VT_PTR || sbt == VT_FUNC) {
2838 tcc_warning("assignment makes integer from pointer without a cast");
2839 } else if (sbt == VT_STRUCT) {
2840 goto case_VT_STRUCT;
2842 /* XXX: more tests */
2843 break;
2844 case VT_STRUCT:
2845 case_VT_STRUCT:
2846 tmp_type1 = *dt;
2847 tmp_type2 = *st;
2848 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2849 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2850 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2851 error:
2852 type_to_str(buf1, sizeof(buf1), st, NULL);
2853 type_to_str(buf2, sizeof(buf2), dt, NULL);
2854 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2856 break;
2858 type_ok:
2859 gen_cast(dt);
2862 /* store vtop in lvalue pushed on stack */
2863 ST_FUNC void vstore(void)
2865 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2867 ft = vtop[-1].type.t;
2868 sbt = vtop->type.t & VT_BTYPE;
2869 dbt = ft & VT_BTYPE;
2870 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2871 (sbt == VT_INT && dbt == VT_SHORT))
2872 && !(vtop->type.t & VT_BITFIELD)) {
2873 /* optimize char/short casts */
2874 delayed_cast = VT_MUSTCAST;
2875 vtop->type.t = ft & VT_TYPE;
2876 /* XXX: factorize */
2877 if (ft & VT_CONSTANT)
2878 tcc_warning("assignment of read-only location");
2879 } else {
2880 delayed_cast = 0;
2881 if (!(ft & VT_BITFIELD))
2882 gen_assign_cast(&vtop[-1].type);
2885 if (sbt == VT_STRUCT) {
2886 /* if structure, only generate pointer */
2887 /* structure assignment : generate memcpy */
2888 /* XXX: optimize if small size */
2889 size = type_size(&vtop->type, &align);
2891 /* destination */
2892 vswap();
2893 vtop->type.t = VT_PTR;
2894 gaddrof();
2896 /* address of memcpy() */
2897 #ifdef TCC_ARM_EABI
2898 if(!(align & 7))
2899 vpush_global_sym(&func_old_type, TOK_memcpy8);
2900 else if(!(align & 3))
2901 vpush_global_sym(&func_old_type, TOK_memcpy4);
2902 else
2903 #endif
2904 /* Use memmove, rather than memcpy, as dest and src may be same: */
2905 vpush_global_sym(&func_old_type, TOK_memmove);
2907 vswap();
2908 /* source */
2909 vpushv(vtop - 2);
2910 vtop->type.t = VT_PTR;
2911 gaddrof();
2912 /* type size */
2913 vpushi(size);
2914 gfunc_call(3);
2916 /* leave source on stack */
2917 } else if (ft & VT_BITFIELD) {
2918 /* bitfield store handling */
2920 /* save lvalue as expression result (example: s.b = s.a = n;) */
2921 vdup(), vtop[-1] = vtop[-2];
2923 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2924 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2925 /* remove bit field info to avoid loops */
2926 vtop[-1].type.t = ft & ~VT_BITFIELD & ((1 << VT_STRUCT_SHIFT) - 1);
2928 if((ft & VT_BTYPE) == VT_BOOL) {
2929 gen_cast(&vtop[-1].type);
2930 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2933 /* duplicate destination */
2934 vdup();
2935 vtop[-1] = vtop[-2];
2937 /* mask and shift source */
2938 if((ft & VT_BTYPE) != VT_BOOL) {
2939 if((ft & VT_BTYPE) == VT_LLONG) {
2940 vpushll((1ULL << bit_size) - 1ULL);
2941 } else {
2942 vpushi((1 << bit_size) - 1);
2944 gen_op('&');
2946 vpushi(bit_pos);
2947 gen_op(TOK_SHL);
2948 /* load destination, mask and or with source */
2949 vswap();
2950 if((ft & VT_BTYPE) == VT_LLONG) {
2951 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2952 } else {
2953 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2955 gen_op('&');
2956 gen_op('|');
2957 /* store result */
2958 vstore();
2959 /* ... and discard */
2960 vpop();
2962 } else {
2963 #ifdef CONFIG_TCC_BCHECK
2964 /* bound check case */
2965 if (vtop[-1].r & VT_MUSTBOUND) {
2966 vswap();
2967 gbound();
2968 vswap();
2970 #endif
2971 rc = RC_INT;
2972 if (is_float(ft)) {
2973 rc = RC_FLOAT;
2974 #ifdef TCC_TARGET_X86_64
2975 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2976 rc = RC_ST0;
2977 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2978 rc = RC_FRET;
2980 #endif
2982 r = gv(rc); /* generate value */
2983 /* if lvalue was saved on stack, must read it */
2984 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2985 SValue sv;
2986 t = get_reg(RC_INT);
2987 #if PTR_SIZE == 8
2988 sv.type.t = VT_PTR;
2989 #else
2990 sv.type.t = VT_INT;
2991 #endif
2992 sv.r = VT_LOCAL | VT_LVAL;
2993 sv.c.i = vtop[-1].c.i;
2994 load(t, &sv);
2995 vtop[-1].r = t | VT_LVAL;
2997 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2998 #if PTR_SIZE == 8
2999 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
3000 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
3001 #else
3002 if ((ft & VT_BTYPE) == VT_LLONG) {
3003 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
3004 #endif
3005 vtop[-1].type.t = load_type;
3006 store(r, vtop - 1);
3007 vswap();
3008 /* convert to int to increment easily */
3009 vtop->type.t = addr_type;
3010 gaddrof();
3011 vpushi(load_size);
3012 gen_op('+');
3013 vtop->r |= VT_LVAL;
3014 vswap();
3015 vtop[-1].type.t = load_type;
3016 /* XXX: it works because r2 is spilled last ! */
3017 store(vtop->r2, vtop - 1);
3018 } else {
3019 store(r, vtop - 1);
3022 vswap();
3023 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3024 vtop->r |= delayed_cast;
3028 /* post defines POST/PRE add. c is the token ++ or -- */
3029 ST_FUNC void inc(int post, int c)
3031 test_lvalue();
3032 vdup(); /* save lvalue */
3033 if (post) {
3034 gv_dup(); /* duplicate value */
3035 vrotb(3);
3036 vrotb(3);
3038 /* add constant */
3039 vpushi(c - TOK_MID);
3040 gen_op('+');
3041 vstore(); /* store value */
3042 if (post)
3043 vpop(); /* if post op, return saved value */
3046 ST_FUNC void parse_mult_str (CString *astr, const char *msg)
3048 /* read the string */
3049 if (tok != TOK_STR)
3050 expect(msg);
3051 cstr_new(astr);
3052 while (tok == TOK_STR) {
3053 /* XXX: add \0 handling too ? */
3054 cstr_cat(astr, tokc.str.data, -1);
3055 next();
3057 cstr_ccat(astr, '\0');
3060 /* If I is >= 1 and a power of two, returns log2(i)+1.
3061 If I is 0 returns 0. */
3062 static int exact_log2p1(int i)
3064 int ret;
3065 if (!i)
3066 return 0;
3067 for (ret = 1; i >= 1 << 8; ret += 8)
3068 i >>= 8;
3069 if (i >= 1 << 4)
3070 ret += 4, i >>= 4;
3071 if (i >= 1 << 2)
3072 ret += 2, i >>= 2;
3073 if (i >= 1 << 1)
3074 ret++;
3075 return ret;
3078 /* Parse __attribute__((...)) GNUC extension. */
3079 static void parse_attribute(AttributeDef *ad)
3081 int t, n;
3082 CString astr;
3084 redo:
3085 if (tok != TOK_ATTRIBUTE1 && tok != TOK_ATTRIBUTE2)
3086 return;
3087 next();
3088 skip('(');
3089 skip('(');
3090 while (tok != ')') {
3091 if (tok < TOK_IDENT)
3092 expect("attribute name");
3093 t = tok;
3094 next();
3095 switch(t) {
3096 case TOK_SECTION1:
3097 case TOK_SECTION2:
3098 skip('(');
3099 parse_mult_str(&astr, "section name");
3100 ad->section = find_section(tcc_state, (char *)astr.data);
3101 skip(')');
3102 cstr_free(&astr);
3103 break;
3104 case TOK_ALIAS1:
3105 case TOK_ALIAS2:
3106 skip('(');
3107 parse_mult_str(&astr, "alias(\"target\")");
3108 ad->alias_target = /* save string as token, for later */
3109 tok_alloc((char*)astr.data, astr.size-1)->tok;
3110 skip(')');
3111 cstr_free(&astr);
3112 break;
3113 case TOK_VISIBILITY1:
3114 case TOK_VISIBILITY2:
3115 skip('(');
3116 parse_mult_str(&astr,
3117 "visibility(\"default|hidden|internal|protected\")");
3118 if (!strcmp (astr.data, "default"))
3119 ad->a.visibility = STV_DEFAULT;
3120 else if (!strcmp (astr.data, "hidden"))
3121 ad->a.visibility = STV_HIDDEN;
3122 else if (!strcmp (astr.data, "internal"))
3123 ad->a.visibility = STV_INTERNAL;
3124 else if (!strcmp (astr.data, "protected"))
3125 ad->a.visibility = STV_PROTECTED;
3126 else
3127 expect("visibility(\"default|hidden|internal|protected\")");
3128 skip(')');
3129 cstr_free(&astr);
3130 break;
3131 case TOK_ALIGNED1:
3132 case TOK_ALIGNED2:
3133 if (tok == '(') {
3134 next();
3135 n = expr_const();
3136 if (n <= 0 || (n & (n - 1)) != 0)
3137 tcc_error("alignment must be a positive power of two");
3138 skip(')');
3139 } else {
3140 n = MAX_ALIGN;
3142 ad->a.aligned = exact_log2p1(n);
3143 if (n != 1 << (ad->a.aligned - 1))
3144 tcc_error("alignment of %d is larger than implemented", n);
3145 break;
3146 case TOK_PACKED1:
3147 case TOK_PACKED2:
3148 ad->a.packed = 1;
3149 break;
3150 case TOK_WEAK1:
3151 case TOK_WEAK2:
3152 ad->a.weak = 1;
3153 break;
3154 case TOK_UNUSED1:
3155 case TOK_UNUSED2:
3156 /* currently, no need to handle it because tcc does not
3157 track unused objects */
3158 break;
3159 case TOK_NORETURN1:
3160 case TOK_NORETURN2:
3161 /* currently, no need to handle it because tcc does not
3162 track unused objects */
3163 break;
3164 case TOK_CDECL1:
3165 case TOK_CDECL2:
3166 case TOK_CDECL3:
3167 ad->f.func_call = FUNC_CDECL;
3168 break;
3169 case TOK_STDCALL1:
3170 case TOK_STDCALL2:
3171 case TOK_STDCALL3:
3172 ad->f.func_call = FUNC_STDCALL;
3173 break;
3174 #ifdef TCC_TARGET_I386
3175 case TOK_REGPARM1:
3176 case TOK_REGPARM2:
3177 skip('(');
3178 n = expr_const();
3179 if (n > 3)
3180 n = 3;
3181 else if (n < 0)
3182 n = 0;
3183 if (n > 0)
3184 ad->f.func_call = FUNC_FASTCALL1 + n - 1;
3185 skip(')');
3186 break;
3187 case TOK_FASTCALL1:
3188 case TOK_FASTCALL2:
3189 case TOK_FASTCALL3:
3190 ad->f.func_call = FUNC_FASTCALLW;
3191 break;
3192 #endif
3193 case TOK_MODE:
3194 skip('(');
3195 switch(tok) {
3196 case TOK_MODE_DI:
3197 ad->attr_mode = VT_LLONG + 1;
3198 break;
3199 case TOK_MODE_QI:
3200 ad->attr_mode = VT_BYTE + 1;
3201 break;
3202 case TOK_MODE_HI:
3203 ad->attr_mode = VT_SHORT + 1;
3204 break;
3205 case TOK_MODE_SI:
3206 case TOK_MODE_word:
3207 ad->attr_mode = VT_INT + 1;
3208 break;
3209 default:
3210 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
3211 break;
3213 next();
3214 skip(')');
3215 break;
3216 case TOK_DLLEXPORT:
3217 ad->a.dllexport = 1;
3218 break;
3219 case TOK_DLLIMPORT:
3220 ad->a.dllimport = 1;
3221 break;
3222 default:
3223 if (tcc_state->warn_unsupported)
3224 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
3225 /* skip parameters */
3226 if (tok == '(') {
3227 int parenthesis = 0;
3228 do {
3229 if (tok == '(')
3230 parenthesis++;
3231 else if (tok == ')')
3232 parenthesis--;
3233 next();
3234 } while (parenthesis && tok != -1);
3236 break;
3238 if (tok != ',')
3239 break;
3240 next();
3242 skip(')');
3243 skip(')');
3244 goto redo;
3247 static Sym * find_field (CType *type, int v)
3249 Sym *s = type->ref;
3250 v |= SYM_FIELD;
3251 while ((s = s->next) != NULL) {
3252 if ((s->v & SYM_FIELD) &&
3253 (s->type.t & VT_BTYPE) == VT_STRUCT &&
3254 (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
3255 Sym *ret = find_field (&s->type, v);
3256 if (ret)
3257 return ret;
3259 if (s->v == v)
3260 break;
3262 return s;
3265 static void struct_add_offset (Sym *s, int offset)
3267 while ((s = s->next) != NULL) {
3268 if ((s->v & SYM_FIELD) &&
3269 (s->type.t & VT_BTYPE) == VT_STRUCT &&
3270 (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
3271 struct_add_offset(s->type.ref, offset);
3272 } else
3273 s->c += offset;
3277 static void struct_layout(CType *type, AttributeDef *ad)
3279 int align, maxalign, offset, c, bit_pos, bt, prevbt, prev_bit_size;
3280 int pcc = !tcc_state->ms_bitfields;
3281 int packwarn = tcc_state->warn_gcc_compat;
3282 int typealign, bit_size, size;
3284 Sym *f;
3285 if (ad->a.aligned)
3286 maxalign = 1 << (ad->a.aligned - 1);
3287 else
3288 maxalign = 1;
3289 offset = 0;
3290 c = 0;
3291 bit_pos = 0;
3292 prevbt = VT_STRUCT; /* make it never match */
3293 prev_bit_size = 0;
3294 size = 0;
3296 for (f = type->ref->next; f; f = f->next) {
3297 size = type_size(&f->type, &typealign);
3298 if (f->type.t & VT_BITFIELD)
3299 bit_size = (f->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
3300 else
3301 bit_size = -1;
3302 if (bit_size == 0 && pcc) {
3303 /* Zero-width bit-fields in PCC mode aren't affected
3304 by any packing (attribute or pragma). */
3305 align = typealign;
3306 } else if (f->r > 1) {
3307 align = f->r;
3308 } else if (ad->a.packed || f->r == 1) {
3309 align = 1;
3310 /* Packed fields or packed records don't let the base type
3311 influence the records type alignment. */
3312 typealign = 1;
3313 } else {
3314 align = typealign;
3316 if (type->ref->type.t == VT_UNION) {
3317 if (pcc && bit_size >= 0)
3318 size = (bit_size + 7) >> 3;
3319 /* Bit position is already zero from our caller. */
3320 offset = 0;
3321 if (size > c)
3322 c = size;
3323 } else if (bit_size < 0) {
3324 int addbytes = pcc ? (bit_pos + 7) >> 3 : 0;
3325 prevbt = VT_STRUCT;
3326 prev_bit_size = 0;
3327 c = (c + addbytes + align - 1) & -align;
3328 offset = c;
3329 if (size > 0)
3330 c += size;
3331 bit_pos = 0;
3332 } else {
3333 /* A bit-field. Layout is more complicated. There are two
3334 options TCC implements: PCC compatible and MS compatible
3335 (PCC compatible is what GCC uses for almost all targets).
3336 In PCC layout the overall size of the struct (in c) is
3337 _excluding_ the current run of bit-fields (that is,
3338 there's at least additional bit_pos bits after c). In
3339 MS layout c does include the current run of bit-fields.
3341 This matters for calculating the natural alignment buckets
3342 in PCC mode. */
3344 /* 'align' will be used to influence records alignment,
3345 so it's the max of specified and type alignment, except
3346 in certain cases that depend on the mode. */
3347 if (align < typealign)
3348 align = typealign;
3349 if (pcc) {
3350 /* In PCC layout a non-packed bit-field is placed adjacent
3351 to the preceding bit-fields, except if it would overflow
3352 its container (depending on base type) or it's a zero-width
3353 bit-field. Packed non-zero-width bit-fields always are
3354 placed adjacent. */
3355 int ofs = (c * 8 + bit_pos) % (typealign * 8);
3356 int ofs2 = ofs + bit_size + (typealign * 8) - 1;
3357 if (bit_size == 0 ||
3358 (typealign != 1 &&
3359 (ofs2 / (typealign * 8)) > (size/typealign))) {
3360 c = (c + ((bit_pos + 7) >> 3) + typealign - 1) & -typealign;
3361 bit_pos = 0;
3362 } else if (bit_pos + bit_size > size * 8) {
3363 c += bit_pos >> 3;
3364 bit_pos &= 7;
3365 if (bit_pos + bit_size > size * 8) {
3366 c += 1, bit_pos = 0;
3367 if ((ad->a.packed || f->r) && packwarn) {
3368 tcc_warning("struct layout not compatible with GCC (internal limitation)");
3369 packwarn = 0;
3373 offset = c;
3374 /* In PCC layout named bit-fields influence the alignment
3375 of the containing struct using the base types alignment,
3376 except for packed fields (which here have correct
3377 align/typealign). */
3378 if ((f->v & SYM_FIRST_ANOM))
3379 align = 1;
3380 } else {
3381 bt = f->type.t & VT_BTYPE;
3382 if ((bit_pos + bit_size > size * 8) ||
3383 (bit_size > 0) == (bt != prevbt)) {
3384 c = (c + typealign - 1) & -typealign;
3385 offset = c;
3386 bit_pos = 0;
3387 /* In MS bitfield mode a bit-field run always uses
3388 at least as many bits as the underlying type.
3389 To start a new run it's also required that this
3390 or the last bit-field had non-zero width. */
3391 if (bit_size || prev_bit_size)
3392 c += size;
3394 /* In MS layout the records alignment is normally
3395 influenced by the field, except for a zero-width
3396 field at the start of a run (but by further zero-width
3397 fields it is again). */
3398 if (bit_size == 0 && prevbt != bt)
3399 align = 1;
3400 prevbt = bt;
3401 prev_bit_size = bit_size;
3403 f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
3404 | (bit_pos << VT_STRUCT_SHIFT);
3405 bit_pos += bit_size;
3406 if (pcc && bit_pos >= size * 8) {
3407 c += size;
3408 bit_pos -= size * 8;
3411 if (align > maxalign)
3412 maxalign = align;
3413 #if 0
3414 printf("set field %s offset=%d",
3415 get_tok_str(f->v & ~SYM_FIELD, NULL), offset);
3416 if (f->type.t & VT_BITFIELD) {
3417 printf(" pos=%d size=%d",
3418 (f->type.t >> VT_STRUCT_SHIFT) & 0x3f,
3419 (f->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
3421 printf("\n");
3422 #endif
3424 if (f->v & SYM_FIRST_ANOM && (f->type.t & VT_BTYPE) == VT_STRUCT) {
3425 Sym *ass;
3426 /* An anonymous struct/union. Adjust member offsets
3427 to reflect the real offset of our containing struct.
3428 Also set the offset of this anon member inside
3429 the outer struct to be zero. Via this it
3430 works when accessing the field offset directly
3431 (from base object), as well as when recursing
3432 members in initializer handling. */
3433 int v2 = f->type.ref->v;
3434 if (!(v2 & SYM_FIELD) &&
3435 (v2 & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
3436 Sym **pps;
3437 /* This happens only with MS extensions. The
3438 anon member has a named struct type, so it
3439 potentially is shared with other references.
3440 We need to unshare members so we can modify
3441 them. */
3442 ass = f->type.ref;
3443 f->type.ref = sym_push(anon_sym++ | SYM_FIELD,
3444 &f->type.ref->type, 0,
3445 f->type.ref->c);
3446 pps = &f->type.ref->next;
3447 while ((ass = ass->next) != NULL) {
3448 *pps = sym_push(ass->v, &ass->type, 0, ass->c);
3449 pps = &((*pps)->next);
3451 *pps = NULL;
3453 struct_add_offset(f->type.ref, offset);
3454 f->c = 0;
3455 } else {
3456 f->c = offset;
3459 f->r = 0;
3461 /* store size and alignment */
3462 type->ref->c = (c + (pcc ? (bit_pos + 7) >> 3 : 0)
3463 + maxalign - 1) & -maxalign;
3464 type->ref->r = maxalign;
3465 if (offset + size > type->ref->c && type->ref->c)
3466 tcc_warning("will touch memory past end of the struct (internal limitation)");
3469 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
3470 static void struct_decl(CType *type, int u)
3472 int v, c, size, align, flexible, alignoverride;
3473 int bit_size, bsize, bt;
3474 Sym *s, *ss, **ps;
3475 AttributeDef ad, ad1;
3476 CType type1, btype;
3478 memset(&ad, 0, sizeof ad);
3479 next();
3480 parse_attribute(&ad);
3481 if (tok != '{') {
3482 v = tok;
3483 next();
3484 /* struct already defined ? return it */
3485 if (v < TOK_IDENT)
3486 expect("struct/union/enum name");
3487 s = struct_find(v);
3488 if (s && (s->sym_scope == local_scope || tok != '{')) {
3489 if (u == s->type.t)
3490 goto do_decl;
3491 if (u == VT_ENUM && IS_ENUM(s->type.t))
3492 goto do_decl;
3493 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
3495 } else {
3496 v = anon_sym++;
3498 /* Record the original enum/struct/union token. */
3499 type1.t = u;
3500 type1.ref = NULL;
3501 /* we put an undefined size for struct/union */
3502 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
3503 s->r = 0; /* default alignment is zero as gcc */
3504 do_decl:
3505 type->t = s->type.t;
3506 type->ref = s;
3508 if (tok == '{') {
3509 next();
3510 if (s->c != -1)
3511 tcc_error("struct/union/enum already defined");
3512 /* cannot be empty */
3513 /* non empty enums are not allowed */
3514 ps = &s->next;
3515 if (u == VT_ENUM) {
3516 long long ll = 0, pl = 0, nl = 0;
3517 CType t;
3518 t.ref = s;
3519 /* enum symbols have static storage */
3520 t.t = VT_INT|VT_STATIC|VT_ENUM_VAL;
3521 for(;;) {
3522 v = tok;
3523 if (v < TOK_UIDENT)
3524 expect("identifier");
3525 ss = sym_find(v);
3526 if (ss && !local_stack)
3527 tcc_error("redefinition of enumerator '%s'",
3528 get_tok_str(v, NULL));
3529 next();
3530 if (tok == '=') {
3531 next();
3532 ll = expr_const64();
3534 ss = sym_push(v, &t, VT_CONST, 0);
3535 ss->enum_val = ll;
3536 *ps = ss, ps = &ss->next;
3537 if (ll < nl)
3538 nl = ll;
3539 if (ll > pl)
3540 pl = ll;
3541 if (tok != ',')
3542 break;
3543 next();
3544 ll++;
3545 /* NOTE: we accept a trailing comma */
3546 if (tok == '}')
3547 break;
3549 skip('}');
3550 /* set integral type of the enum */
3551 t.t = VT_INT;
3552 if (nl == 0) {
3553 if (pl != (unsigned)pl)
3554 t.t = VT_LLONG;
3555 t.t |= VT_UNSIGNED;
3556 } else if (pl != (int)pl || nl != (int)nl)
3557 t.t = VT_LLONG;
3558 s->type.t = type->t = t.t | VT_ENUM;
3559 s->c = 0;
3560 /* set type for enum members */
3561 for (ss = s->next; ss; ss = ss->next) {
3562 ll = ss->enum_val;
3563 if (ll == (int)ll) /* default is int if it fits */
3564 continue;
3565 if (t.t & VT_UNSIGNED) {
3566 ss->type.t |= VT_UNSIGNED;
3567 if (ll == (unsigned)ll)
3568 continue;
3570 ss->type.t = (ss->type.t & ~VT_BTYPE) | VT_LLONG;
3572 } else {
3573 c = 0;
3574 flexible = 0;
3575 while (tok != '}') {
3576 if (!parse_btype(&btype, &ad1)) {
3577 skip(';');
3578 continue;
3580 while (1) {
3581 if (flexible)
3582 tcc_error("flexible array member '%s' not at the end of struct",
3583 get_tok_str(v, NULL));
3584 bit_size = -1;
3585 v = 0;
3586 type1 = btype;
3587 if (tok != ':') {
3588 if (tok != ';')
3589 type_decl(&type1, &ad1, &v, TYPE_DIRECT);
3590 if (v == 0) {
3591 if ((type1.t & VT_BTYPE) != VT_STRUCT)
3592 expect("identifier");
3593 else {
3594 int v = btype.ref->v;
3595 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
3596 if (tcc_state->ms_extensions == 0)
3597 expect("identifier");
3601 if (type_size(&type1, &align) < 0) {
3602 if ((u == VT_STRUCT) && (type1.t & VT_ARRAY) && c)
3603 flexible = 1;
3604 else
3605 tcc_error("field '%s' has incomplete type",
3606 get_tok_str(v, NULL));
3608 if ((type1.t & VT_BTYPE) == VT_FUNC ||
3609 (type1.t & VT_STORAGE))
3610 tcc_error("invalid type for '%s'",
3611 get_tok_str(v, NULL));
3613 if (tok == ':') {
3614 next();
3615 bit_size = expr_const();
3616 /* XXX: handle v = 0 case for messages */
3617 if (bit_size < 0)
3618 tcc_error("negative width in bit-field '%s'",
3619 get_tok_str(v, NULL));
3620 if (v && bit_size == 0)
3621 tcc_error("zero width for bit-field '%s'",
3622 get_tok_str(v, NULL));
3623 parse_attribute(&ad1);
3625 size = type_size(&type1, &align);
3626 /* Only remember non-default alignment. */
3627 alignoverride = 0;
3628 if (ad1.a.aligned) {
3629 int speca = 1 << (ad1.a.aligned - 1);
3630 alignoverride = speca;
3631 } else if (ad1.a.packed || ad.a.packed) {
3632 alignoverride = 1;
3633 } else if (*tcc_state->pack_stack_ptr) {
3634 if (align >= *tcc_state->pack_stack_ptr)
3635 alignoverride = *tcc_state->pack_stack_ptr;
3637 if (bit_size >= 0) {
3638 bt = type1.t & VT_BTYPE;
3639 if (bt != VT_INT &&
3640 bt != VT_BYTE &&
3641 bt != VT_SHORT &&
3642 bt != VT_BOOL &&
3643 bt != VT_LLONG)
3644 tcc_error("bitfields must have scalar type");
3645 bsize = size * 8;
3646 if (bit_size > bsize) {
3647 tcc_error("width of '%s' exceeds its type",
3648 get_tok_str(v, NULL));
3649 } else if (bit_size == bsize) {
3650 /* no need for bit fields */
3652 } else {
3653 type1.t = (type1.t & ~VT_STRUCT_MASK)
3654 | VT_BITFIELD
3655 | (bit_size << (VT_STRUCT_SHIFT + 6));
3658 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
3659 /* Remember we've seen a real field to check
3660 for placement of flexible array member. */
3661 c = 1;
3663 /* If member is a struct or bit-field, enforce
3664 placing into the struct (as anonymous). */
3665 if (v == 0 &&
3666 ((type1.t & VT_BTYPE) == VT_STRUCT ||
3667 bit_size >= 0)) {
3668 v = anon_sym++;
3670 if (v) {
3671 ss = sym_push(v | SYM_FIELD, &type1, alignoverride, 0);
3672 *ps = ss;
3673 ps = &ss->next;
3675 if (tok == ';' || tok == TOK_EOF)
3676 break;
3677 skip(',');
3679 skip(';');
3681 skip('}');
3682 parse_attribute(&ad);
3683 struct_layout(type, &ad);
3688 /* Add type qualifiers to a type. If the type is an array then the qualifiers
3689 are added to the element type, copied because it could be a typedef. */
3690 static void parse_btype_qualify(CType *type, int qualifiers)
3692 while (type->t & VT_ARRAY) {
3693 type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
3694 type = &type->ref->type;
3696 type->t |= qualifiers;
3699 /* return 0 if no type declaration. otherwise, return the basic type
3700 and skip it.
3702 static int parse_btype(CType *type, AttributeDef *ad)
3704 int t, u, bt, st, type_found, typespec_found, g;
3705 Sym *s;
3706 CType type1;
3708 memset(ad, 0, sizeof(AttributeDef));
3709 type_found = 0;
3710 typespec_found = 0;
3711 t = VT_INT;
3712 bt = st = -1;
3713 type->ref = NULL;
3715 while(1) {
3716 switch(tok) {
3717 case TOK_EXTENSION:
3718 /* currently, we really ignore extension */
3719 next();
3720 continue;
3722 /* basic types */
3723 case TOK_CHAR:
3724 u = VT_BYTE;
3725 basic_type:
3726 next();
3727 basic_type1:
3728 if (u == VT_SHORT || u == VT_LONG) {
3729 if (st != -1 || (bt != -1 && bt != VT_INT))
3730 tmbt: tcc_error("too many basic types");
3731 st = u;
3732 } else {
3733 if (bt != -1 || (st != -1 && u != VT_INT))
3734 goto tmbt;
3735 bt = u;
3737 if (u != VT_INT)
3738 t = (t & ~VT_BTYPE) | u;
3739 typespec_found = 1;
3740 break;
3741 case TOK_VOID:
3742 u = VT_VOID;
3743 goto basic_type;
3744 case TOK_SHORT:
3745 u = VT_SHORT;
3746 goto basic_type;
3747 case TOK_INT:
3748 u = VT_INT;
3749 goto basic_type;
3750 case TOK_LONG:
3751 if ((t & VT_BTYPE) == VT_DOUBLE) {
3752 #ifndef TCC_TARGET_PE
3753 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3754 #endif
3755 } else if ((t & VT_BTYPE) == VT_LONG) {
3756 t = (t & ~VT_BTYPE) | VT_LLONG;
3757 } else {
3758 u = VT_LONG;
3759 goto basic_type;
3761 next();
3762 break;
3763 #ifdef TCC_TARGET_ARM64
3764 case TOK_UINT128:
3765 /* GCC's __uint128_t appears in some Linux header files. Make it a
3766 synonym for long double to get the size and alignment right. */
3767 u = VT_LDOUBLE;
3768 goto basic_type;
3769 #endif
3770 case TOK_BOOL:
3771 u = VT_BOOL;
3772 goto basic_type;
3773 case TOK_FLOAT:
3774 u = VT_FLOAT;
3775 goto basic_type;
3776 case TOK_DOUBLE:
3777 if ((t & VT_BTYPE) == VT_LONG) {
3778 #ifdef TCC_TARGET_PE
3779 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3780 #else
3781 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3782 #endif
3783 } else {
3784 u = VT_DOUBLE;
3785 goto basic_type;
3787 next();
3788 break;
3789 case TOK_ENUM:
3790 struct_decl(&type1, VT_ENUM);
3791 basic_type2:
3792 u = type1.t;
3793 type->ref = type1.ref;
3794 goto basic_type1;
3795 case TOK_STRUCT:
3796 struct_decl(&type1, VT_STRUCT);
3797 goto basic_type2;
3798 case TOK_UNION:
3799 struct_decl(&type1, VT_UNION);
3800 goto basic_type2;
3802 /* type modifiers */
3803 case TOK_CONST1:
3804 case TOK_CONST2:
3805 case TOK_CONST3:
3806 type->t = t;
3807 parse_btype_qualify(type, VT_CONSTANT);
3808 t = type->t;
3809 next();
3810 break;
3811 case TOK_VOLATILE1:
3812 case TOK_VOLATILE2:
3813 case TOK_VOLATILE3:
3814 type->t = t;
3815 parse_btype_qualify(type, VT_VOLATILE);
3816 t = type->t;
3817 next();
3818 break;
3819 case TOK_SIGNED1:
3820 case TOK_SIGNED2:
3821 case TOK_SIGNED3:
3822 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3823 tcc_error("signed and unsigned modifier");
3824 t |= VT_DEFSIGN;
3825 next();
3826 typespec_found = 1;
3827 break;
3828 case TOK_REGISTER:
3829 case TOK_AUTO:
3830 case TOK_RESTRICT1:
3831 case TOK_RESTRICT2:
3832 case TOK_RESTRICT3:
3833 next();
3834 break;
3835 case TOK_UNSIGNED:
3836 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3837 tcc_error("signed and unsigned modifier");
3838 t |= VT_DEFSIGN | VT_UNSIGNED;
3839 next();
3840 typespec_found = 1;
3841 break;
3843 /* storage */
3844 case TOK_EXTERN:
3845 g = VT_EXTERN;
3846 goto storage;
3847 case TOK_STATIC:
3848 g = VT_STATIC;
3849 goto storage;
3850 case TOK_TYPEDEF:
3851 g = VT_TYPEDEF;
3852 goto storage;
3853 storage:
3854 if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g)
3855 tcc_error("multiple storage classes");
3856 t |= g;
3857 next();
3858 break;
3859 case TOK_INLINE1:
3860 case TOK_INLINE2:
3861 case TOK_INLINE3:
3862 t |= VT_INLINE;
3863 next();
3864 break;
3866 /* GNUC attribute */
3867 case TOK_ATTRIBUTE1:
3868 case TOK_ATTRIBUTE2:
3869 parse_attribute(ad);
3870 if (ad->attr_mode) {
3871 u = ad->attr_mode -1;
3872 t = (t & ~VT_BTYPE) | u;
3874 break;
3875 /* GNUC typeof */
3876 case TOK_TYPEOF1:
3877 case TOK_TYPEOF2:
3878 case TOK_TYPEOF3:
3879 next();
3880 parse_expr_type(&type1);
3881 /* remove all storage modifiers except typedef */
3882 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3883 goto basic_type2;
3884 default:
3885 if (typespec_found)
3886 goto the_end;
3887 s = sym_find(tok);
3888 if (!s || !(s->type.t & VT_TYPEDEF))
3889 goto the_end;
3890 t &= ~VT_BTYPE;
3891 u = t & ~(VT_CONSTANT | VT_VOLATILE), t ^= u;
3892 type->t = (s->type.t & ~VT_TYPEDEF) | u;
3893 type->ref = s->type.ref;
3894 if (t)
3895 parse_btype_qualify(type, t);
3896 t = type->t;
3897 /* get attributes from typedef */
3898 if (s->a.aligned && 0 == ad->a.aligned)
3899 ad->a.aligned = s->a.aligned;
3900 if (s->f.func_call && 0 == ad->f.func_call)
3901 ad->f.func_call = s->f.func_call;
3902 if (s->a.packed)
3903 ad->a.packed = 1;
3904 next();
3905 typespec_found = 1;
3906 st = bt = -2;
3907 break;
3909 type_found = 1;
3911 the_end:
3912 if (tcc_state->char_is_unsigned) {
3913 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3914 t |= VT_UNSIGNED;
3917 /* long is never used as type */
3918 if ((t & VT_BTYPE) == VT_LONG)
3919 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
3920 t = (t & ~VT_BTYPE) | VT_LLONG;
3921 #else
3922 t = (t & ~VT_BTYPE) | VT_INT;
3923 #endif
3924 type->t = t;
3925 return type_found;
3928 /* convert a function parameter type (array to pointer and function to
3929 function pointer) */
3930 static inline void convert_parameter_type(CType *pt)
3932 /* remove const and volatile qualifiers (XXX: const could be used
3933 to indicate a const function parameter */
3934 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3935 /* array must be transformed to pointer according to ANSI C */
3936 pt->t &= ~VT_ARRAY;
3937 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3938 mk_pointer(pt);
3942 ST_FUNC void parse_asm_str(CString *astr)
3944 skip('(');
3945 parse_mult_str(astr, "string constant");
3948 /* Parse an asm label and return the token */
3949 static int asm_label_instr(void)
3951 int v;
3952 CString astr;
3954 next();
3955 parse_asm_str(&astr);
3956 skip(')');
3957 #ifdef ASM_DEBUG
3958 printf("asm_alias: \"%s\"\n", (char *)astr.data);
3959 #endif
3960 v = tok_alloc(astr.data, astr.size - 1)->tok;
3961 cstr_free(&astr);
3962 return v;
3965 static int post_type(CType *type, AttributeDef *ad, int storage, int td)
3967 int n, l, t1, arg_size, align;
3968 Sym **plast, *s, *first;
3969 AttributeDef ad1;
3970 CType pt;
3972 if (tok == '(') {
3973 /* function type, or recursive declarator (return if so) */
3974 next();
3975 if (td && !(td & TYPE_ABSTRACT))
3976 return 0;
3977 if (tok == ')')
3978 l = 0;
3979 else if (parse_btype(&pt, &ad1))
3980 l = FUNC_NEW;
3981 else if (td)
3982 return 0;
3983 else
3984 l = FUNC_OLD;
3985 first = NULL;
3986 plast = &first;
3987 arg_size = 0;
3988 if (l) {
3989 for(;;) {
3990 /* read param name and compute offset */
3991 if (l != FUNC_OLD) {
3992 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3993 break;
3994 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3995 if ((pt.t & VT_BTYPE) == VT_VOID)
3996 tcc_error("parameter declared as void");
3997 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3998 } else {
3999 n = tok;
4000 if (n < TOK_UIDENT)
4001 expect("identifier");
4002 pt.t = VT_VOID; /* invalid type */
4003 next();
4005 convert_parameter_type(&pt);
4006 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
4007 *plast = s;
4008 plast = &s->next;
4009 if (tok == ')')
4010 break;
4011 skip(',');
4012 if (l == FUNC_NEW && tok == TOK_DOTS) {
4013 l = FUNC_ELLIPSIS;
4014 next();
4015 break;
4017 if (l == FUNC_NEW && !parse_btype(&pt, &ad1))
4018 tcc_error("invalid type");
4020 } else
4021 /* if no parameters, then old type prototype */
4022 l = FUNC_OLD;
4023 skip(')');
4024 /* NOTE: const is ignored in returned type as it has a special
4025 meaning in gcc / C++ */
4026 type->t &= ~VT_CONSTANT;
4027 /* some ancient pre-K&R C allows a function to return an array
4028 and the array brackets to be put after the arguments, such
4029 that "int c()[]" means something like "int[] c()" */
4030 if (tok == '[') {
4031 next();
4032 skip(']'); /* only handle simple "[]" */
4033 mk_pointer(type);
4035 /* we push a anonymous symbol which will contain the function prototype */
4036 ad->f.func_args = arg_size;
4037 ad->f.func_type = l;
4038 s = sym_push(SYM_FIELD, type, 0, 0);
4039 s->a = ad->a;
4040 s->f = ad->f;
4041 s->next = first;
4042 type->t = VT_FUNC;
4043 type->ref = s;
4044 } else if (tok == '[') {
4045 int saved_nocode_wanted = nocode_wanted;
4046 /* array definition */
4047 next();
4048 if (tok == TOK_RESTRICT1)
4049 next();
4050 n = -1;
4051 t1 = 0;
4052 if (tok != ']') {
4053 if (!local_stack || (storage & VT_STATIC))
4054 vpushi(expr_const());
4055 else {
4056 /* VLAs (which can only happen with local_stack && !VT_STATIC)
4057 length must always be evaluated, even under nocode_wanted,
4058 so that its size slot is initialized (e.g. under sizeof
4059 or typeof). */
4060 nocode_wanted = 0;
4061 gexpr();
4063 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4064 n = vtop->c.i;
4065 if (n < 0)
4066 tcc_error("invalid array size");
4067 } else {
4068 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
4069 tcc_error("size of variable length array should be an integer");
4070 t1 = VT_VLA;
4073 skip(']');
4074 /* parse next post type */
4075 post_type(type, ad, storage, 0);
4076 if (type->t == VT_FUNC)
4077 tcc_error("declaration of an array of functions");
4078 t1 |= type->t & VT_VLA;
4080 if (t1 & VT_VLA) {
4081 loc -= type_size(&int_type, &align);
4082 loc &= -align;
4083 n = loc;
4085 vla_runtime_type_size(type, &align);
4086 gen_op('*');
4087 vset(&int_type, VT_LOCAL|VT_LVAL, n);
4088 vswap();
4089 vstore();
4091 if (n != -1)
4092 vpop();
4093 nocode_wanted = saved_nocode_wanted;
4095 /* we push an anonymous symbol which will contain the array
4096 element type */
4097 s = sym_push(SYM_FIELD, type, 0, n);
4098 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
4099 type->ref = s;
4101 return 1;
4104 /* Parse a type declarator (except basic type), and return the type
4105 in 'type'. 'td' is a bitmask indicating which kind of type decl is
4106 expected. 'type' should contain the basic type. 'ad' is the
4107 attribute definition of the basic type. It can be modified by
4108 type_decl(). If this (possibly abstract) declarator is a pointer chain
4109 it returns the innermost pointed to type (equals *type, but is a different
4110 pointer), otherwise returns type itself, that's used for recursive calls. */
4111 static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td)
4113 CType *post, *ret;
4114 int qualifiers, storage;
4116 /* recursive type, remove storage bits first, apply them later again */
4117 storage = type->t & VT_STORAGE;
4118 type->t &= ~VT_STORAGE;
4119 post = ret = type;
4121 while (tok == '*') {
4122 qualifiers = 0;
4123 redo:
4124 next();
4125 switch(tok) {
4126 case TOK_CONST1:
4127 case TOK_CONST2:
4128 case TOK_CONST3:
4129 qualifiers |= VT_CONSTANT;
4130 goto redo;
4131 case TOK_VOLATILE1:
4132 case TOK_VOLATILE2:
4133 case TOK_VOLATILE3:
4134 qualifiers |= VT_VOLATILE;
4135 goto redo;
4136 case TOK_RESTRICT1:
4137 case TOK_RESTRICT2:
4138 case TOK_RESTRICT3:
4139 goto redo;
4140 /* XXX: clarify attribute handling */
4141 case TOK_ATTRIBUTE1:
4142 case TOK_ATTRIBUTE2:
4143 parse_attribute(ad);
4144 break;
4146 mk_pointer(type);
4147 type->t |= qualifiers;
4148 if (ret == type)
4149 /* innermost pointed to type is the one for the first derivation */
4150 ret = pointed_type(type);
4153 if (tok == '(') {
4154 /* This is possibly a parameter type list for abstract declarators
4155 ('int ()'), use post_type for testing this. */
4156 if (!post_type(type, ad, 0, td)) {
4157 /* It's not, so it's a nested declarator, and the post operations
4158 apply to the innermost pointed to type (if any). */
4159 /* XXX: this is not correct to modify 'ad' at this point, but
4160 the syntax is not clear */
4161 parse_attribute(ad);
4162 post = type_decl(type, ad, v, td);
4163 skip(')');
4165 } else if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
4166 /* type identifier */
4167 *v = tok;
4168 next();
4169 } else {
4170 if (!(td & TYPE_ABSTRACT))
4171 expect("identifier");
4172 *v = 0;
4174 post_type(post, ad, storage, 0);
4175 parse_attribute(ad);
4176 type->t |= storage;
4177 return ret;
4180 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
4181 ST_FUNC int lvalue_type(int t)
4183 int bt, r;
4184 r = VT_LVAL;
4185 bt = t & VT_BTYPE;
4186 if (bt == VT_BYTE || bt == VT_BOOL)
4187 r |= VT_LVAL_BYTE;
4188 else if (bt == VT_SHORT)
4189 r |= VT_LVAL_SHORT;
4190 else
4191 return r;
4192 if (t & VT_UNSIGNED)
4193 r |= VT_LVAL_UNSIGNED;
4194 return r;
4197 /* indirection with full error checking and bound check */
4198 ST_FUNC void indir(void)
4200 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
4201 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
4202 return;
4203 expect("pointer");
4205 if (vtop->r & VT_LVAL)
4206 gv(RC_INT);
4207 vtop->type = *pointed_type(&vtop->type);
4208 /* Arrays and functions are never lvalues */
4209 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
4210 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
4211 vtop->r |= lvalue_type(vtop->type.t);
4212 /* if bound checking, the referenced pointer must be checked */
4213 #ifdef CONFIG_TCC_BCHECK
4214 if (tcc_state->do_bounds_check)
4215 vtop->r |= VT_MUSTBOUND;
4216 #endif
4220 /* pass a parameter to a function and do type checking and casting */
4221 static void gfunc_param_typed(Sym *func, Sym *arg)
4223 int func_type;
4224 CType type;
4226 func_type = func->f.func_type;
4227 if (func_type == FUNC_OLD ||
4228 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
4229 /* default casting : only need to convert float to double */
4230 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
4231 gen_cast_s(VT_DOUBLE);
4232 } else if (vtop->type.t & VT_BITFIELD) {
4233 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
4234 type.ref = vtop->type.ref;
4235 gen_cast(&type);
4237 } else if (arg == NULL) {
4238 tcc_error("too many arguments to function");
4239 } else {
4240 type = arg->type;
4241 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4242 gen_assign_cast(&type);
4246 /* parse an expression and return its type without any side effect.
4247 If UNRY we parse an unary expression, otherwise a full one. */
4248 static void expr_type(CType *type, int unry)
4250 nocode_wanted++;
4251 if (unry)
4252 unary();
4253 else
4254 gexpr();
4255 *type = vtop->type;
4256 vpop();
4257 nocode_wanted--;
4260 /* parse an expression of the form '(type)' or '(expr)' and return its
4261 type */
4262 static void parse_expr_type(CType *type)
4264 int n;
4265 AttributeDef ad;
4267 skip('(');
4268 if (parse_btype(type, &ad)) {
4269 type_decl(type, &ad, &n, TYPE_ABSTRACT);
4270 } else {
4271 expr_type(type, 0);
4273 skip(')');
4276 static void parse_type(CType *type)
4278 AttributeDef ad;
4279 int n;
4281 if (!parse_btype(type, &ad)) {
4282 expect("type");
4284 type_decl(type, &ad, &n, TYPE_ABSTRACT);
4287 static void parse_builtin_params(int nc, const char *args)
4289 char c, sep = '(';
4290 CType t;
4291 if (nc)
4292 nocode_wanted++;
4293 next();
4294 while ((c = *args++)) {
4295 skip(sep);
4296 sep = ',';
4297 switch (c) {
4298 case 'e': expr_eq(); continue;
4299 case 't': parse_type(&t); vpush(&t); continue;
4300 default: tcc_error("internal error"); break;
4303 skip(')');
4304 if (nc)
4305 nocode_wanted--;
4308 ST_FUNC void unary(void)
4310 int n, t, align, size, r, sizeof_caller;
4311 CType type;
4312 Sym *s;
4313 AttributeDef ad;
4315 sizeof_caller = in_sizeof;
4316 in_sizeof = 0;
4317 type.ref = NULL;
4318 /* XXX: GCC 2.95.3 does not generate a table although it should be
4319 better here */
4320 tok_next:
4321 switch(tok) {
4322 case TOK_EXTENSION:
4323 next();
4324 goto tok_next;
4325 case TOK_CINT:
4326 case TOK_CCHAR:
4327 case TOK_LCHAR:
4328 t = VT_INT;
4329 push_tokc:
4330 type.t = t;
4331 vsetc(&type, VT_CONST, &tokc);
4332 next();
4333 break;
4334 case TOK_CUINT:
4335 t = VT_INT | VT_UNSIGNED;
4336 goto push_tokc;
4337 case TOK_CLLONG:
4338 t = VT_LLONG;
4339 goto push_tokc;
4340 case TOK_CULLONG:
4341 t = VT_LLONG | VT_UNSIGNED;
4342 goto push_tokc;
4343 case TOK_CFLOAT:
4344 t = VT_FLOAT;
4345 goto push_tokc;
4346 case TOK_CDOUBLE:
4347 t = VT_DOUBLE;
4348 goto push_tokc;
4349 case TOK_CLDOUBLE:
4350 t = VT_LDOUBLE;
4351 goto push_tokc;
4353 case TOK___FUNCTION__:
4354 if (!gnu_ext)
4355 goto tok_identifier;
4356 /* fall thru */
4357 case TOK___FUNC__:
4359 void *ptr;
4360 int len;
4361 /* special function name identifier */
4362 len = strlen(funcname) + 1;
4363 /* generate char[len] type */
4364 type.t = VT_BYTE;
4365 mk_pointer(&type);
4366 type.t |= VT_ARRAY;
4367 type.ref->c = len;
4368 vpush_ref(&type, data_section, data_section->data_offset, len);
4369 ptr = section_ptr_add(data_section, len);
4370 memcpy(ptr, funcname, len);
4371 next();
4373 break;
4374 case TOK_LSTR:
4375 #ifdef TCC_TARGET_PE
4376 t = VT_SHORT | VT_UNSIGNED;
4377 #else
4378 t = VT_INT;
4379 #endif
4380 goto str_init;
4381 case TOK_STR:
4382 /* string parsing */
4383 t = VT_BYTE;
4384 if (tcc_state->char_is_unsigned)
4385 t = VT_BYTE | VT_UNSIGNED;
4386 str_init:
4387 if (tcc_state->warn_write_strings)
4388 t |= VT_CONSTANT;
4389 type.t = t;
4390 mk_pointer(&type);
4391 type.t |= VT_ARRAY;
4392 memset(&ad, 0, sizeof(AttributeDef));
4393 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
4394 break;
4395 case '(':
4396 next();
4397 /* cast ? */
4398 if (parse_btype(&type, &ad)) {
4399 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
4400 skip(')');
4401 /* check ISOC99 compound literal */
4402 if (tok == '{') {
4403 /* data is allocated locally by default */
4404 if (global_expr)
4405 r = VT_CONST;
4406 else
4407 r = VT_LOCAL;
4408 /* all except arrays are lvalues */
4409 if (!(type.t & VT_ARRAY))
4410 r |= lvalue_type(type.t);
4411 memset(&ad, 0, sizeof(AttributeDef));
4412 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
4413 } else {
4414 if (sizeof_caller) {
4415 vpush(&type);
4416 return;
4418 unary();
4419 gen_cast(&type);
4421 } else if (tok == '{') {
4422 int saved_nocode_wanted = nocode_wanted;
4423 if (const_wanted)
4424 tcc_error("expected constant");
4425 /* save all registers */
4426 save_regs(0);
4427 /* statement expression : we do not accept break/continue
4428 inside as GCC does. We do retain the nocode_wanted state,
4429 as statement expressions can't ever be entered from the
4430 outside, so any reactivation of code emission (from labels
4431 or loop heads) can be disabled again after the end of it. */
4432 block(NULL, NULL, 1);
4433 nocode_wanted = saved_nocode_wanted;
4434 skip(')');
4435 } else {
4436 gexpr();
4437 skip(')');
4439 break;
4440 case '*':
4441 next();
4442 unary();
4443 indir();
4444 break;
4445 case '&':
4446 next();
4447 unary();
4448 /* functions names must be treated as function pointers,
4449 except for unary '&' and sizeof. Since we consider that
4450 functions are not lvalues, we only have to handle it
4451 there and in function calls. */
4452 /* arrays can also be used although they are not lvalues */
4453 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
4454 !(vtop->type.t & VT_ARRAY))
4455 test_lvalue();
4456 mk_pointer(&vtop->type);
4457 gaddrof();
4458 break;
4459 case '!':
4460 next();
4461 unary();
4462 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4463 gen_cast_s(VT_BOOL);
4464 vtop->c.i = !vtop->c.i;
4465 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
4466 vtop->c.i ^= 1;
4467 else {
4468 save_regs(1);
4469 vseti(VT_JMP, gvtst(1, 0));
4471 break;
4472 case '~':
4473 next();
4474 unary();
4475 vpushi(-1);
4476 gen_op('^');
4477 break;
4478 case '+':
4479 next();
4480 unary();
4481 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
4482 tcc_error("pointer not accepted for unary plus");
4483 /* In order to force cast, we add zero, except for floating point
4484 where we really need an noop (otherwise -0.0 will be transformed
4485 into +0.0). */
4486 if (!is_float(vtop->type.t)) {
4487 vpushi(0);
4488 gen_op('+');
4490 break;
4491 case TOK_SIZEOF:
4492 case TOK_ALIGNOF1:
4493 case TOK_ALIGNOF2:
4494 t = tok;
4495 next();
4496 in_sizeof++;
4497 expr_type(&type, 1); // Perform a in_sizeof = 0;
4498 size = type_size(&type, &align);
4499 if (t == TOK_SIZEOF) {
4500 if (!(type.t & VT_VLA)) {
4501 if (size < 0)
4502 tcc_error("sizeof applied to an incomplete type");
4503 vpushs(size);
4504 } else {
4505 vla_runtime_type_size(&type, &align);
4507 } else {
4508 vpushs(align);
4510 vtop->type.t |= VT_UNSIGNED;
4511 break;
4513 case TOK_builtin_expect:
4514 /* __builtin_expect is a no-op for now */
4515 parse_builtin_params(0, "ee");
4516 vpop();
4517 break;
4518 case TOK_builtin_types_compatible_p:
4519 parse_builtin_params(0, "tt");
4520 vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
4521 vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
4522 n = is_compatible_types(&vtop[-1].type, &vtop[0].type);
4523 vtop -= 2;
4524 vpushi(n);
4525 break;
4526 case TOK_builtin_choose_expr:
4528 int64_t c;
4529 next();
4530 skip('(');
4531 c = expr_const64();
4532 skip(',');
4533 if (!c) {
4534 nocode_wanted++;
4536 expr_eq();
4537 if (!c) {
4538 vpop();
4539 nocode_wanted--;
4541 skip(',');
4542 if (c) {
4543 nocode_wanted++;
4545 expr_eq();
4546 if (c) {
4547 vpop();
4548 nocode_wanted--;
4550 skip(')');
4552 break;
4553 case TOK_builtin_constant_p:
4554 parse_builtin_params(1, "e");
4555 n = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4556 vtop--;
4557 vpushi(n);
4558 break;
4559 case TOK_builtin_frame_address:
4560 case TOK_builtin_return_address:
4562 int tok1 = tok;
4563 int level;
4564 next();
4565 skip('(');
4566 if (tok != TOK_CINT) {
4567 tcc_error("%s only takes positive integers",
4568 tok1 == TOK_builtin_return_address ?
4569 "__builtin_return_address" :
4570 "__builtin_frame_address");
4572 level = (uint32_t)tokc.i;
4573 next();
4574 skip(')');
4575 type.t = VT_VOID;
4576 mk_pointer(&type);
4577 vset(&type, VT_LOCAL, 0); /* local frame */
4578 while (level--) {
4579 mk_pointer(&vtop->type);
4580 indir(); /* -> parent frame */
4582 if (tok1 == TOK_builtin_return_address) {
4583 // assume return address is just above frame pointer on stack
4584 vpushi(PTR_SIZE);
4585 gen_op('+');
4586 mk_pointer(&vtop->type);
4587 indir();
4590 break;
4591 #ifdef TCC_TARGET_X86_64
4592 #ifdef TCC_TARGET_PE
4593 case TOK_builtin_va_start:
4594 parse_builtin_params(0, "ee");
4595 r = vtop->r & VT_VALMASK;
4596 if (r == VT_LLOCAL)
4597 r = VT_LOCAL;
4598 if (r != VT_LOCAL)
4599 tcc_error("__builtin_va_start expects a local variable");
4600 vtop->r = r;
4601 vtop->type = char_pointer_type;
4602 vtop->c.i += 8;
4603 vstore();
4604 break;
4605 #else
4606 case TOK_builtin_va_arg_types:
4607 parse_builtin_params(0, "t");
4608 vpushi(classify_x86_64_va_arg(&vtop->type));
4609 vswap();
4610 vpop();
4611 break;
4612 #endif
4613 #endif
4615 #ifdef TCC_TARGET_ARM64
4616 case TOK___va_start: {
4617 parse_builtin_params(0, "ee");
4618 //xx check types
4619 gen_va_start();
4620 vpushi(0);
4621 vtop->type.t = VT_VOID;
4622 break;
4624 case TOK___va_arg: {
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 case TOK_GENERIC:
4693 CType controlling_type;
4694 int has_default = 0;
4695 int has_match = 0;
4696 CType cur_type;
4697 AttributeDef ad_tmp;
4698 int learn = 0;
4699 TokenString *str = NULL;
4700 ParseState saved_parse_state;
4702 next();
4703 skip('(');
4704 expr_type(&controlling_type, 1);
4705 if (controlling_type.t & VT_ARRAY)
4706 controlling_type.t = VT_PTR;
4707 controlling_type.t &= ~VT_CONSTANT;
4708 for (;;) {
4709 learn = 0;
4710 skip(',');
4711 if (tok == TOK_DEFAULT) {
4712 if (has_default)
4713 tcc_error("too many 'default'");
4714 if (!has_match) {
4715 has_default = 1;
4716 learn = 1;
4718 next();
4719 } else {
4720 int itmp;
4722 parse_btype(&cur_type, &ad_tmp);
4723 type_decl(&cur_type, &ad_tmp, &itmp, TYPE_ABSTRACT);
4724 if (compare_types(&controlling_type, &cur_type, 0)) {
4725 if (has_match) {
4726 tcc_error("type march twice");
4728 if (has_default)
4729 tok_str_free(str);
4730 has_match = 1;
4731 learn = 1;
4734 skip(':');
4735 if (learn) {
4736 skip_or_save_block(&str);
4737 } else {
4738 skip_or_save_block(NULL);
4740 if (tok == ',')
4741 continue;
4742 else if (tok == ')')
4743 break;
4745 if (!has_match && !has_default) {
4746 char buf[256];
4748 type_to_str(buf, 256, &controlling_type, NULL);
4749 tcc_error("_Generic sellector of type '%s' is not compatible with any assosiation",
4750 buf);
4752 skip(')');
4753 save_parse_state(&saved_parse_state);
4754 begin_macro(str, 1);
4755 next();
4756 expr_eq();
4757 end_macro();
4758 restore_parse_state(&saved_parse_state);
4759 break;
4761 // special qnan , snan and infinity values
4762 case TOK___NAN__:
4763 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
4764 next();
4765 break;
4766 case TOK___SNAN__:
4767 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
4768 next();
4769 break;
4770 case TOK___INF__:
4771 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
4772 next();
4773 break;
4775 default:
4776 tok_identifier:
4777 t = tok;
4778 next();
4779 if (t < TOK_UIDENT)
4780 expect("identifier");
4781 s = sym_find(t);
4782 if (!s) {
4783 const char *name = get_tok_str(t, NULL);
4784 if (tok != '(')
4785 tcc_error("'%s' undeclared", name);
4786 /* for simple function calls, we tolerate undeclared
4787 external reference to int() function */
4788 if (tcc_state->warn_implicit_function_declaration
4789 #ifdef TCC_TARGET_PE
4790 /* people must be warned about using undeclared WINAPI functions
4791 (which usually start with uppercase letter) */
4792 || (name[0] >= 'A' && name[0] <= 'Z')
4793 #endif
4795 tcc_warning("implicit declaration of function '%s'", name);
4796 s = external_global_sym(t, &func_old_type, 0);
4799 r = s->r;
4800 /* A symbol that has a register is a local register variable,
4801 which starts out as VT_LOCAL value. */
4802 if ((r & VT_VALMASK) < VT_CONST)
4803 r = (r & ~VT_VALMASK) | VT_LOCAL;
4805 vset(&s->type, r, s->c);
4806 /* Point to s as backpointer (even without r&VT_SYM).
4807 Will be used by at least the x86 inline asm parser for
4808 regvars. */
4809 vtop->sym = s;
4811 if (r & VT_SYM) {
4812 vtop->c.i = 0;
4813 } else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) {
4814 vtop->c.i = s->enum_val;
4816 break;
4819 /* post operations */
4820 while (1) {
4821 if (tok == TOK_INC || tok == TOK_DEC) {
4822 inc(1, tok);
4823 next();
4824 } else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) {
4825 int qualifiers;
4826 /* field */
4827 if (tok == TOK_ARROW)
4828 indir();
4829 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
4830 test_lvalue();
4831 gaddrof();
4832 /* expect pointer on structure */
4833 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
4834 expect("struct or union");
4835 if (tok == TOK_CDOUBLE)
4836 expect("field name");
4837 next();
4838 if (tok == TOK_CINT || tok == TOK_CUINT)
4839 expect("field name");
4840 s = find_field(&vtop->type, tok);
4841 if (!s)
4842 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc));
4843 /* add field offset to pointer */
4844 vtop->type = char_pointer_type; /* change type to 'char *' */
4845 vpushi(s->c);
4846 gen_op('+');
4847 /* change type to field type, and set to lvalue */
4848 vtop->type = s->type;
4849 vtop->type.t |= qualifiers;
4850 /* an array is never an lvalue */
4851 if (!(vtop->type.t & VT_ARRAY)) {
4852 vtop->r |= lvalue_type(vtop->type.t);
4853 #ifdef CONFIG_TCC_BCHECK
4854 /* if bound checking, the referenced pointer must be checked */
4855 if (tcc_state->do_bounds_check && (vtop->r & VT_VALMASK) != VT_LOCAL)
4856 vtop->r |= VT_MUSTBOUND;
4857 #endif
4859 next();
4860 } else if (tok == '[') {
4861 next();
4862 gexpr();
4863 gen_op('+');
4864 indir();
4865 skip(']');
4866 } else if (tok == '(') {
4867 SValue ret;
4868 Sym *sa;
4869 int nb_args, ret_nregs, ret_align, regsize, variadic;
4871 /* function call */
4872 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
4873 /* pointer test (no array accepted) */
4874 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4875 vtop->type = *pointed_type(&vtop->type);
4876 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4877 goto error_func;
4878 } else {
4879 error_func:
4880 expect("function pointer");
4882 } else {
4883 vtop->r &= ~VT_LVAL; /* no lvalue */
4885 /* get return type */
4886 s = vtop->type.ref;
4887 next();
4888 sa = s->next; /* first parameter */
4889 nb_args = regsize = 0;
4890 ret.r2 = VT_CONST;
4891 /* compute first implicit argument if a structure is returned */
4892 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4893 variadic = (s->f.func_type == FUNC_ELLIPSIS);
4894 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4895 &ret_align, &regsize);
4896 if (!ret_nregs) {
4897 /* get some space for the returned structure */
4898 size = type_size(&s->type, &align);
4899 #ifdef TCC_TARGET_ARM64
4900 /* On arm64, a small struct is return in registers.
4901 It is much easier to write it to memory if we know
4902 that we are allowed to write some extra bytes, so
4903 round the allocated space up to a power of 2: */
4904 if (size < 16)
4905 while (size & (size - 1))
4906 size = (size | (size - 1)) + 1;
4907 #endif
4908 loc = (loc - size) & -align;
4909 ret.type = s->type;
4910 ret.r = VT_LOCAL | VT_LVAL;
4911 /* pass it as 'int' to avoid structure arg passing
4912 problems */
4913 vseti(VT_LOCAL, loc);
4914 ret.c = vtop->c;
4915 nb_args++;
4917 } else {
4918 ret_nregs = 1;
4919 ret.type = s->type;
4922 if (ret_nregs) {
4923 /* return in register */
4924 if (is_float(ret.type.t)) {
4925 ret.r = reg_fret(ret.type.t);
4926 #ifdef TCC_TARGET_X86_64
4927 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4928 ret.r2 = REG_QRET;
4929 #endif
4930 } else {
4931 #ifndef TCC_TARGET_ARM64
4932 #ifdef TCC_TARGET_X86_64
4933 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4934 #else
4935 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4936 #endif
4937 ret.r2 = REG_LRET;
4938 #endif
4939 ret.r = REG_IRET;
4941 ret.c.i = 0;
4943 if (tok != ')') {
4944 for(;;) {
4945 expr_eq();
4946 gfunc_param_typed(s, sa);
4947 nb_args++;
4948 if (sa)
4949 sa = sa->next;
4950 if (tok == ')')
4951 break;
4952 skip(',');
4955 if (sa)
4956 tcc_error("too few arguments to function");
4957 skip(')');
4958 gfunc_call(nb_args);
4960 /* return value */
4961 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4962 vsetc(&ret.type, r, &ret.c);
4963 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4966 /* handle packed struct return */
4967 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4968 int addr, offset;
4970 size = type_size(&s->type, &align);
4971 /* We're writing whole regs often, make sure there's enough
4972 space. Assume register size is power of 2. */
4973 if (regsize > align)
4974 align = regsize;
4975 loc = (loc - size) & -align;
4976 addr = loc;
4977 offset = 0;
4978 for (;;) {
4979 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4980 vswap();
4981 vstore();
4982 vtop--;
4983 if (--ret_nregs == 0)
4984 break;
4985 offset += regsize;
4987 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4989 } else {
4990 break;
4995 ST_FUNC void expr_prod(void)
4997 int t;
4999 unary();
5000 while (tok == '*' || tok == '/' || tok == '%') {
5001 t = tok;
5002 next();
5003 unary();
5004 gen_op(t);
5008 ST_FUNC void expr_sum(void)
5010 int t;
5012 expr_prod();
5013 while (tok == '+' || tok == '-') {
5014 t = tok;
5015 next();
5016 expr_prod();
5017 gen_op(t);
5021 static void expr_shift(void)
5023 int t;
5025 expr_sum();
5026 while (tok == TOK_SHL || tok == TOK_SAR) {
5027 t = tok;
5028 next();
5029 expr_sum();
5030 gen_op(t);
5034 static void expr_cmp(void)
5036 int t;
5038 expr_shift();
5039 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
5040 tok == TOK_ULT || tok == TOK_UGE) {
5041 t = tok;
5042 next();
5043 expr_shift();
5044 gen_op(t);
5048 static void expr_cmpeq(void)
5050 int t;
5052 expr_cmp();
5053 while (tok == TOK_EQ || tok == TOK_NE) {
5054 t = tok;
5055 next();
5056 expr_cmp();
5057 gen_op(t);
5061 static void expr_and(void)
5063 expr_cmpeq();
5064 while (tok == '&') {
5065 next();
5066 expr_cmpeq();
5067 gen_op('&');
5071 static void expr_xor(void)
5073 expr_and();
5074 while (tok == '^') {
5075 next();
5076 expr_and();
5077 gen_op('^');
5081 static void expr_or(void)
5083 expr_xor();
5084 while (tok == '|') {
5085 next();
5086 expr_xor();
5087 gen_op('|');
5091 static void expr_land(void)
5093 expr_or();
5094 if (tok == TOK_LAND) {
5095 int t = 0;
5096 for(;;) {
5097 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5098 gen_cast_s(VT_BOOL);
5099 if (vtop->c.i) {
5100 vpop();
5101 } else {
5102 nocode_wanted++;
5103 while (tok == TOK_LAND) {
5104 next();
5105 expr_or();
5106 vpop();
5108 nocode_wanted--;
5109 if (t)
5110 gsym(t);
5111 gen_cast_s(VT_INT);
5112 break;
5114 } else {
5115 if (!t)
5116 save_regs(1);
5117 t = gvtst(1, t);
5119 if (tok != TOK_LAND) {
5120 if (t)
5121 vseti(VT_JMPI, t);
5122 else
5123 vpushi(1);
5124 break;
5126 next();
5127 expr_or();
5132 static void expr_lor(void)
5134 expr_land();
5135 if (tok == TOK_LOR) {
5136 int t = 0;
5137 for(;;) {
5138 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5139 gen_cast_s(VT_BOOL);
5140 if (!vtop->c.i) {
5141 vpop();
5142 } else {
5143 nocode_wanted++;
5144 while (tok == TOK_LOR) {
5145 next();
5146 expr_land();
5147 vpop();
5149 nocode_wanted--;
5150 if (t)
5151 gsym(t);
5152 gen_cast_s(VT_INT);
5153 break;
5155 } else {
5156 if (!t)
5157 save_regs(1);
5158 t = gvtst(0, t);
5160 if (tok != TOK_LOR) {
5161 if (t)
5162 vseti(VT_JMP, t);
5163 else
5164 vpushi(0);
5165 break;
5167 next();
5168 expr_land();
5173 /* Assuming vtop is a value used in a conditional context
5174 (i.e. compared with zero) return 0 if it's false, 1 if
5175 true and -1 if it can't be statically determined. */
5176 static int condition_3way(void)
5178 int c = -1;
5179 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
5180 (!(vtop->r & VT_SYM) || !vtop->sym->a.weak)) {
5181 vdup();
5182 gen_cast_s(VT_BOOL);
5183 c = vtop->c.i;
5184 vpop();
5186 return c;
5189 static void expr_cond(void)
5191 int tt, u, r1, r2, rc, t1, t2, bt1, bt2, islv, c, g;
5192 SValue sv;
5193 CType type, type1, type2;
5195 expr_lor();
5196 if (tok == '?') {
5197 next();
5198 c = condition_3way();
5199 g = (tok == ':' && gnu_ext);
5200 if (c < 0) {
5201 /* needed to avoid having different registers saved in
5202 each branch */
5203 if (is_float(vtop->type.t)) {
5204 rc = RC_FLOAT;
5205 #ifdef TCC_TARGET_X86_64
5206 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
5207 rc = RC_ST0;
5209 #endif
5210 } else
5211 rc = RC_INT;
5212 gv(rc);
5213 save_regs(1);
5214 if (g)
5215 gv_dup();
5216 tt = gvtst(1, 0);
5218 } else {
5219 if (!g)
5220 vpop();
5221 tt = 0;
5224 if (1) {
5225 if (c == 0)
5226 nocode_wanted++;
5227 if (!g)
5228 gexpr();
5230 type1 = vtop->type;
5231 sv = *vtop; /* save value to handle it later */
5232 vtop--; /* no vpop so that FP stack is not flushed */
5233 skip(':');
5235 u = 0;
5236 if (c < 0)
5237 u = gjmp(0);
5238 gsym(tt);
5240 if (c == 0)
5241 nocode_wanted--;
5242 if (c == 1)
5243 nocode_wanted++;
5244 expr_cond();
5245 if (c == 1)
5246 nocode_wanted--;
5248 type2 = vtop->type;
5249 t1 = type1.t;
5250 bt1 = t1 & VT_BTYPE;
5251 t2 = type2.t;
5252 bt2 = t2 & VT_BTYPE;
5253 type.ref = NULL;
5255 /* cast operands to correct type according to ISOC rules */
5256 if (is_float(bt1) || is_float(bt2)) {
5257 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5258 type.t = VT_LDOUBLE;
5260 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5261 type.t = VT_DOUBLE;
5262 } else {
5263 type.t = VT_FLOAT;
5265 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5266 /* cast to biggest op */
5267 type.t = VT_LLONG;
5268 /* convert to unsigned if it does not fit in a long long */
5269 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
5270 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
5271 type.t |= VT_UNSIGNED;
5272 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
5273 /* If one is a null ptr constant the result type
5274 is the other. */
5275 if (is_null_pointer (vtop))
5276 type = type1;
5277 else if (is_null_pointer (&sv))
5278 type = type2;
5279 /* XXX: test pointer compatibility, C99 has more elaborate
5280 rules here. */
5281 else
5282 type = type1;
5283 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
5284 /* XXX: test function pointer compatibility */
5285 type = bt1 == VT_FUNC ? type1 : type2;
5286 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
5287 /* XXX: test structure compatibility */
5288 type = bt1 == VT_STRUCT ? type1 : type2;
5289 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
5290 /* NOTE: as an extension, we accept void on only one side */
5291 type.t = VT_VOID;
5292 } else {
5293 /* integer operations */
5294 type.t = VT_INT;
5295 /* convert to unsigned if it does not fit in an integer */
5296 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
5297 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
5298 type.t |= VT_UNSIGNED;
5300 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
5301 that `(expr ? a : b).mem` does not error with "lvalue expected" */
5302 islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
5303 islv &= c < 0;
5305 /* now we convert second operand */
5306 if (c != 1) {
5307 gen_cast(&type);
5308 if (islv) {
5309 mk_pointer(&vtop->type);
5310 gaddrof();
5311 } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
5312 gaddrof();
5315 rc = RC_INT;
5316 if (is_float(type.t)) {
5317 rc = RC_FLOAT;
5318 #ifdef TCC_TARGET_X86_64
5319 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
5320 rc = RC_ST0;
5322 #endif
5323 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
5324 /* for long longs, we use fixed registers to avoid having
5325 to handle a complicated move */
5326 rc = RC_IRET;
5329 tt = r2 = 0;
5330 if (c < 0) {
5331 r2 = gv(rc);
5332 tt = gjmp(0);
5334 gsym(u);
5336 /* this is horrible, but we must also convert first
5337 operand */
5338 if (c != 0) {
5339 *vtop = sv;
5340 gen_cast(&type);
5341 if (islv) {
5342 mk_pointer(&vtop->type);
5343 gaddrof();
5344 } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
5345 gaddrof();
5348 if (c < 0) {
5349 r1 = gv(rc);
5350 move_reg(r2, r1, type.t);
5351 vtop->r = r2;
5352 gsym(tt);
5353 if (islv)
5354 indir();
5360 static void expr_eq(void)
5362 int t;
5364 expr_cond();
5365 if (tok == '=' ||
5366 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
5367 tok == TOK_A_XOR || tok == TOK_A_OR ||
5368 tok == TOK_A_SHL || tok == TOK_A_SAR) {
5369 test_lvalue();
5370 t = tok;
5371 next();
5372 if (t == '=') {
5373 expr_eq();
5374 } else {
5375 vdup();
5376 expr_eq();
5377 gen_op(t & 0x7f);
5379 vstore();
5383 ST_FUNC void gexpr(void)
5385 while (1) {
5386 expr_eq();
5387 if (tok != ',')
5388 break;
5389 vpop();
5390 next();
5394 /* parse a constant expression and return value in vtop. */
5395 static void expr_const1(void)
5397 const_wanted++;
5398 expr_cond();
5399 const_wanted--;
5402 /* parse an integer constant and return its value. */
5403 static inline int64_t expr_const64(void)
5405 int64_t c;
5406 expr_const1();
5407 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5408 expect("constant expression");
5409 c = vtop->c.i;
5410 vpop();
5411 return c;
5414 /* parse an integer constant and return its value.
5415 Complain if it doesn't fit 32bit (signed or unsigned). */
5416 ST_FUNC int expr_const(void)
5418 int c;
5419 int64_t wc = expr_const64();
5420 c = wc;
5421 if (c != wc && (unsigned)c != wc)
5422 tcc_error("constant exceeds 32 bit");
5423 return c;
5426 /* return the label token if current token is a label, otherwise
5427 return zero */
5428 static int is_label(void)
5430 int last_tok;
5432 /* fast test first */
5433 if (tok < TOK_UIDENT)
5434 return 0;
5435 /* no need to save tokc because tok is an identifier */
5436 last_tok = tok;
5437 next();
5438 if (tok == ':') {
5439 return last_tok;
5440 } else {
5441 unget_tok(last_tok);
5442 return 0;
5446 #ifndef TCC_TARGET_ARM64
5447 static void gfunc_return(CType *func_type)
5449 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
5450 CType type, ret_type;
5451 int ret_align, ret_nregs, regsize;
5452 ret_nregs = gfunc_sret(func_type, func_var, &ret_type,
5453 &ret_align, &regsize);
5454 if (0 == ret_nregs) {
5455 /* if returning structure, must copy it to implicit
5456 first pointer arg location */
5457 type = *func_type;
5458 mk_pointer(&type);
5459 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
5460 indir();
5461 vswap();
5462 /* copy structure value to pointer */
5463 vstore();
5464 } else {
5465 /* returning structure packed into registers */
5466 int r, size, addr, align;
5467 size = type_size(func_type,&align);
5468 if ((vtop->r != (VT_LOCAL | VT_LVAL) ||
5469 (vtop->c.i & (ret_align-1)))
5470 && (align & (ret_align-1))) {
5471 loc = (loc - size) & -ret_align;
5472 addr = loc;
5473 type = *func_type;
5474 vset(&type, VT_LOCAL | VT_LVAL, addr);
5475 vswap();
5476 vstore();
5477 vpop();
5478 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
5480 vtop->type = ret_type;
5481 if (is_float(ret_type.t))
5482 r = rc_fret(ret_type.t);
5483 else
5484 r = RC_IRET;
5486 if (ret_nregs == 1)
5487 gv(r);
5488 else {
5489 for (;;) {
5490 vdup();
5491 gv(r);
5492 vpop();
5493 if (--ret_nregs == 0)
5494 break;
5495 /* We assume that when a structure is returned in multiple
5496 registers, their classes are consecutive values of the
5497 suite s(n) = 2^n */
5498 r <<= 1;
5499 vtop->c.i += regsize;
5503 } else if (is_float(func_type->t)) {
5504 gv(rc_fret(func_type->t));
5505 } else {
5506 gv(RC_IRET);
5508 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5510 #endif
5512 static int case_cmp(const void *pa, const void *pb)
5514 int64_t a = (*(struct case_t**) pa)->v1;
5515 int64_t b = (*(struct case_t**) pb)->v1;
5516 return a < b ? -1 : a > b;
5519 static void gcase(struct case_t **base, int len, int *bsym)
5521 struct case_t *p;
5522 int e;
5523 int ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
5524 gv(RC_INT);
5525 while (len > 4) {
5526 /* binary search */
5527 p = base[len/2];
5528 vdup();
5529 if (ll)
5530 vpushll(p->v2);
5531 else
5532 vpushi(p->v2);
5533 gen_op(TOK_LE);
5534 e = gtst(1, 0);
5535 vdup();
5536 if (ll)
5537 vpushll(p->v1);
5538 else
5539 vpushi(p->v1);
5540 gen_op(TOK_GE);
5541 gtst_addr(0, p->sym); /* v1 <= x <= v2 */
5542 /* x < v1 */
5543 gcase(base, len/2, bsym);
5544 if (cur_switch->def_sym)
5545 gjmp_addr(cur_switch->def_sym);
5546 else
5547 *bsym = gjmp(*bsym);
5548 /* x > v2 */
5549 gsym(e);
5550 e = len/2 + 1;
5551 base += e; len -= e;
5553 /* linear scan */
5554 while (len--) {
5555 p = *base++;
5556 vdup();
5557 if (ll)
5558 vpushll(p->v2);
5559 else
5560 vpushi(p->v2);
5561 if (p->v1 == p->v2) {
5562 gen_op(TOK_EQ);
5563 gtst_addr(0, p->sym);
5564 } else {
5565 gen_op(TOK_LE);
5566 e = gtst(1, 0);
5567 vdup();
5568 if (ll)
5569 vpushll(p->v1);
5570 else
5571 vpushi(p->v1);
5572 gen_op(TOK_GE);
5573 gtst_addr(0, p->sym);
5574 gsym(e);
5579 static void block(int *bsym, int *csym, int is_expr)
5581 int a, b, c, d, cond;
5582 Sym *s;
5584 /* generate line number info */
5585 if (tcc_state->do_debug)
5586 tcc_debug_line(tcc_state);
5588 if (is_expr) {
5589 /* default return value is (void) */
5590 vpushi(0);
5591 vtop->type.t = VT_VOID;
5594 if (tok == TOK_IF) {
5595 /* if test */
5596 int saved_nocode_wanted = nocode_wanted;
5597 next();
5598 skip('(');
5599 gexpr();
5600 skip(')');
5601 cond = condition_3way();
5602 if (cond == 1)
5603 a = 0, vpop();
5604 else
5605 a = gvtst(1, 0);
5606 if (cond == 0)
5607 nocode_wanted |= 0x20000000;
5608 block(bsym, csym, 0);
5609 if (cond != 1)
5610 nocode_wanted = saved_nocode_wanted;
5611 c = tok;
5612 if (c == TOK_ELSE) {
5613 next();
5614 d = gjmp(0);
5615 gsym(a);
5616 if (cond == 1)
5617 nocode_wanted |= 0x20000000;
5618 block(bsym, csym, 0);
5619 gsym(d); /* patch else jmp */
5620 if (cond != 0)
5621 nocode_wanted = saved_nocode_wanted;
5622 } else
5623 gsym(a);
5624 } else if (tok == TOK_WHILE) {
5625 int saved_nocode_wanted;
5626 nocode_wanted &= ~0x20000000;
5627 next();
5628 d = ind;
5629 vla_sp_restore();
5630 skip('(');
5631 gexpr();
5632 skip(')');
5633 a = gvtst(1, 0);
5634 b = 0;
5635 ++local_scope;
5636 saved_nocode_wanted = nocode_wanted;
5637 block(&a, &b, 0);
5638 nocode_wanted = saved_nocode_wanted;
5639 --local_scope;
5640 gjmp_addr(d);
5641 gsym(a);
5642 gsym_addr(b, d);
5643 } else if (tok == '{') {
5644 Sym *llabel;
5645 int block_vla_sp_loc = vla_sp_loc, saved_vlas_in_scope = vlas_in_scope;
5647 next();
5648 /* record local declaration stack position */
5649 s = local_stack;
5650 llabel = local_label_stack;
5651 ++local_scope;
5653 /* handle local labels declarations */
5654 if (tok == TOK_LABEL) {
5655 next();
5656 for(;;) {
5657 if (tok < TOK_UIDENT)
5658 expect("label identifier");
5659 label_push(&local_label_stack, tok, LABEL_DECLARED);
5660 next();
5661 if (tok == ',') {
5662 next();
5663 } else {
5664 skip(';');
5665 break;
5669 while (tok != '}') {
5670 if ((a = is_label()))
5671 unget_tok(a);
5672 else
5673 decl(VT_LOCAL);
5674 if (tok != '}') {
5675 if (is_expr)
5676 vpop();
5677 block(bsym, csym, is_expr);
5680 /* pop locally defined labels */
5681 label_pop(&local_label_stack, llabel);
5682 /* pop locally defined symbols */
5683 --local_scope;
5684 /* In the is_expr case (a statement expression is finished here),
5685 vtop might refer to symbols on the local_stack. Either via the
5686 type or via vtop->sym. We can't pop those nor any that in turn
5687 might be referred to. To make it easier we don't roll back
5688 any symbols in that case; some upper level call to block() will
5689 do that. We do have to remove such symbols from the lookup
5690 tables, though. sym_pop will do that. */
5691 sym_pop(&local_stack, s, is_expr);
5693 /* Pop VLA frames and restore stack pointer if required */
5694 if (vlas_in_scope > saved_vlas_in_scope) {
5695 vla_sp_loc = saved_vlas_in_scope ? block_vla_sp_loc : vla_sp_root_loc;
5696 vla_sp_restore();
5698 vlas_in_scope = saved_vlas_in_scope;
5700 next();
5701 } else if (tok == TOK_RETURN) {
5702 next();
5703 if (tok != ';') {
5704 gexpr();
5705 gen_assign_cast(&func_vt);
5706 gfunc_return(&func_vt);
5708 skip(';');
5709 /* jump unless last stmt in top-level block */
5710 if (tok != '}' || local_scope != 1)
5711 rsym = gjmp(rsym);
5712 nocode_wanted |= 0x20000000;
5713 } else if (tok == TOK_BREAK) {
5714 /* compute jump */
5715 if (!bsym)
5716 tcc_error("cannot break");
5717 *bsym = gjmp(*bsym);
5718 next();
5719 skip(';');
5720 nocode_wanted |= 0x20000000;
5721 } else if (tok == TOK_CONTINUE) {
5722 /* compute jump */
5723 if (!csym)
5724 tcc_error("cannot continue");
5725 vla_sp_restore_root();
5726 *csym = gjmp(*csym);
5727 next();
5728 skip(';');
5729 } else if (tok == TOK_FOR) {
5730 int e;
5731 int saved_nocode_wanted;
5732 nocode_wanted &= ~0x20000000;
5733 next();
5734 skip('(');
5735 s = local_stack;
5736 ++local_scope;
5737 if (tok != ';') {
5738 /* c99 for-loop init decl? */
5739 if (!decl0(VT_LOCAL, 1, NULL)) {
5740 /* no, regular for-loop init expr */
5741 gexpr();
5742 vpop();
5745 skip(';');
5746 d = ind;
5747 c = ind;
5748 vla_sp_restore();
5749 a = 0;
5750 b = 0;
5751 if (tok != ';') {
5752 gexpr();
5753 a = gvtst(1, 0);
5755 skip(';');
5756 if (tok != ')') {
5757 e = gjmp(0);
5758 c = ind;
5759 vla_sp_restore();
5760 gexpr();
5761 vpop();
5762 gjmp_addr(d);
5763 gsym(e);
5765 skip(')');
5766 saved_nocode_wanted = nocode_wanted;
5767 block(&a, &b, 0);
5768 nocode_wanted = saved_nocode_wanted;
5769 gjmp_addr(c);
5770 gsym(a);
5771 gsym_addr(b, c);
5772 --local_scope;
5773 sym_pop(&local_stack, s, 0);
5775 } else
5776 if (tok == TOK_DO) {
5777 int saved_nocode_wanted;
5778 nocode_wanted &= ~0x20000000;
5779 next();
5780 a = 0;
5781 b = 0;
5782 d = ind;
5783 vla_sp_restore();
5784 saved_nocode_wanted = nocode_wanted;
5785 block(&a, &b, 0);
5786 skip(TOK_WHILE);
5787 skip('(');
5788 gsym(b);
5789 gexpr();
5790 c = gvtst(0, 0);
5791 gsym_addr(c, d);
5792 nocode_wanted = saved_nocode_wanted;
5793 skip(')');
5794 gsym(a);
5795 skip(';');
5796 } else
5797 if (tok == TOK_SWITCH) {
5798 struct switch_t *saved, sw;
5799 int saved_nocode_wanted = nocode_wanted;
5800 SValue switchval;
5801 next();
5802 skip('(');
5803 gexpr();
5804 skip(')');
5805 switchval = *vtop--;
5806 a = 0;
5807 b = gjmp(0); /* jump to first case */
5808 sw.p = NULL; sw.n = 0; sw.def_sym = 0;
5809 saved = cur_switch;
5810 cur_switch = &sw;
5811 block(&a, csym, 0);
5812 nocode_wanted = saved_nocode_wanted;
5813 a = gjmp(a); /* add implicit break */
5814 /* case lookup */
5815 gsym(b);
5816 qsort(sw.p, sw.n, sizeof(void*), case_cmp);
5817 for (b = 1; b < sw.n; b++)
5818 if (sw.p[b - 1]->v2 >= sw.p[b]->v1)
5819 tcc_error("duplicate case value");
5820 /* Our switch table sorting is signed, so the compared
5821 value needs to be as well when it's 64bit. */
5822 if ((switchval.type.t & VT_BTYPE) == VT_LLONG)
5823 switchval.type.t &= ~VT_UNSIGNED;
5824 vpushv(&switchval);
5825 gcase(sw.p, sw.n, &a);
5826 vpop();
5827 if (sw.def_sym)
5828 gjmp_addr(sw.def_sym);
5829 dynarray_reset(&sw.p, &sw.n);
5830 cur_switch = saved;
5831 /* break label */
5832 gsym(a);
5833 } else
5834 if (tok == TOK_CASE) {
5835 struct case_t *cr = tcc_malloc(sizeof(struct case_t));
5836 if (!cur_switch)
5837 expect("switch");
5838 nocode_wanted &= ~0x20000000;
5839 next();
5840 cr->v1 = cr->v2 = expr_const64();
5841 if (gnu_ext && tok == TOK_DOTS) {
5842 next();
5843 cr->v2 = expr_const64();
5844 if (cr->v2 < cr->v1)
5845 tcc_warning("empty case range");
5847 cr->sym = ind;
5848 dynarray_add(&cur_switch->p, &cur_switch->n, cr);
5849 skip(':');
5850 is_expr = 0;
5851 goto block_after_label;
5852 } else
5853 if (tok == TOK_DEFAULT) {
5854 next();
5855 skip(':');
5856 if (!cur_switch)
5857 expect("switch");
5858 if (cur_switch->def_sym)
5859 tcc_error("too many 'default'");
5860 cur_switch->def_sym = ind;
5861 is_expr = 0;
5862 goto block_after_label;
5863 } else
5864 if (tok == TOK_GOTO) {
5865 next();
5866 if (tok == '*' && gnu_ext) {
5867 /* computed goto */
5868 next();
5869 gexpr();
5870 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
5871 expect("pointer");
5872 ggoto();
5873 } else if (tok >= TOK_UIDENT) {
5874 s = label_find(tok);
5875 /* put forward definition if needed */
5876 if (!s) {
5877 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
5878 } else {
5879 if (s->r == LABEL_DECLARED)
5880 s->r = LABEL_FORWARD;
5882 vla_sp_restore_root();
5883 if (s->r & LABEL_FORWARD)
5884 s->jnext = gjmp(s->jnext);
5885 else
5886 gjmp_addr(s->jnext);
5887 next();
5888 } else {
5889 expect("label identifier");
5891 skip(';');
5892 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
5893 asm_instr();
5894 } else {
5895 b = is_label();
5896 if (b) {
5897 /* label case */
5898 next();
5899 s = label_find(b);
5900 if (s) {
5901 if (s->r == LABEL_DEFINED)
5902 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
5903 gsym(s->jnext);
5904 s->r = LABEL_DEFINED;
5905 } else {
5906 s = label_push(&global_label_stack, b, LABEL_DEFINED);
5908 s->jnext = ind;
5909 vla_sp_restore();
5910 /* we accept this, but it is a mistake */
5911 block_after_label:
5912 nocode_wanted &= ~0x20000000;
5913 if (tok == '}') {
5914 tcc_warning("deprecated use of label at end of compound statement");
5915 } else {
5916 if (is_expr)
5917 vpop();
5918 block(bsym, csym, is_expr);
5920 } else {
5921 /* expression case */
5922 if (tok != ';') {
5923 if (is_expr) {
5924 vpop();
5925 gexpr();
5926 } else {
5927 gexpr();
5928 vpop();
5931 skip(';');
5936 /* This skips over a stream of tokens containing balanced {} and ()
5937 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
5938 with a '{'). If STR then allocates and stores the skipped tokens
5939 in *STR. This doesn't check if () and {} are nested correctly,
5940 i.e. "({)}" is accepted. */
5941 static void skip_or_save_block(TokenString **str)
5943 int braces = tok == '{';
5944 int level = 0;
5945 if (str)
5946 *str = tok_str_alloc();
5948 while ((level > 0 || (tok != '}' && tok != ',' && tok != ';' && tok != ')'))) {
5949 int t;
5950 if (tok == TOK_EOF) {
5951 if (str || level > 0)
5952 tcc_error("unexpected end of file");
5953 else
5954 break;
5956 if (str)
5957 tok_str_add_tok(*str);
5958 t = tok;
5959 next();
5960 if (t == '{' || t == '(') {
5961 level++;
5962 } else if (t == '}' || t == ')') {
5963 level--;
5964 if (level == 0 && braces && t == '}')
5965 break;
5968 if (str) {
5969 tok_str_add(*str, -1);
5970 tok_str_add(*str, 0);
5974 #define EXPR_CONST 1
5975 #define EXPR_ANY 2
5977 static void parse_init_elem(int expr_type)
5979 int saved_global_expr;
5980 switch(expr_type) {
5981 case EXPR_CONST:
5982 /* compound literals must be allocated globally in this case */
5983 saved_global_expr = global_expr;
5984 global_expr = 1;
5985 expr_const1();
5986 global_expr = saved_global_expr;
5987 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
5988 (compound literals). */
5989 if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
5990 && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
5991 || vtop->sym->v < SYM_FIRST_ANOM))
5992 #ifdef TCC_TARGET_PE
5993 || ((vtop->r & VT_SYM) && vtop->sym->a.dllimport)
5994 #endif
5996 tcc_error("initializer element is not constant");
5997 break;
5998 case EXPR_ANY:
5999 expr_eq();
6000 break;
6004 /* put zeros for variable based init */
6005 static void init_putz(Section *sec, unsigned long c, int size)
6007 if (sec) {
6008 /* nothing to do because globals are already set to zero */
6009 } else {
6010 vpush_global_sym(&func_old_type, TOK_memset);
6011 vseti(VT_LOCAL, c);
6012 #ifdef TCC_TARGET_ARM
6013 vpushs(size);
6014 vpushi(0);
6015 #else
6016 vpushi(0);
6017 vpushs(size);
6018 #endif
6019 gfunc_call(3);
6023 /* t is the array or struct type. c is the array or struct
6024 address. cur_field is the pointer to the current
6025 field, for arrays the 'c' member contains the current start
6026 index. 'size_only' is true if only size info is needed (only used
6027 in arrays). al contains the already initialized length of the
6028 current container (starting at c). This returns the new length of that. */
6029 static int decl_designator(CType *type, Section *sec, unsigned long c,
6030 Sym **cur_field, int size_only, int al)
6032 Sym *s, *f;
6033 int index, index_last, align, l, nb_elems, elem_size;
6034 unsigned long corig = c;
6036 elem_size = 0;
6037 nb_elems = 1;
6038 if (gnu_ext && (l = is_label()) != 0)
6039 goto struct_field;
6040 /* NOTE: we only support ranges for last designator */
6041 while (nb_elems == 1 && (tok == '[' || tok == '.')) {
6042 if (tok == '[') {
6043 if (!(type->t & VT_ARRAY))
6044 expect("array type");
6045 next();
6046 index = index_last = expr_const();
6047 if (tok == TOK_DOTS && gnu_ext) {
6048 next();
6049 index_last = expr_const();
6051 skip(']');
6052 s = type->ref;
6053 if (index < 0 || (s->c >= 0 && index_last >= s->c) ||
6054 index_last < index)
6055 tcc_error("invalid index");
6056 if (cur_field)
6057 (*cur_field)->c = index_last;
6058 type = pointed_type(type);
6059 elem_size = type_size(type, &align);
6060 c += index * elem_size;
6061 nb_elems = index_last - index + 1;
6062 } else {
6063 next();
6064 l = tok;
6065 struct_field:
6066 next();
6067 if ((type->t & VT_BTYPE) != VT_STRUCT)
6068 expect("struct/union type");
6069 f = find_field(type, l);
6070 if (!f)
6071 expect("field");
6072 if (cur_field)
6073 *cur_field = f;
6074 type = &f->type;
6075 c += f->c;
6077 cur_field = NULL;
6079 if (!cur_field) {
6080 if (tok == '=') {
6081 next();
6082 } else if (!gnu_ext) {
6083 expect("=");
6085 } else {
6086 if (type->t & VT_ARRAY) {
6087 index = (*cur_field)->c;
6088 if (type->ref->c >= 0 && index >= type->ref->c)
6089 tcc_error("index too large");
6090 type = pointed_type(type);
6091 c += index * type_size(type, &align);
6092 } else {
6093 f = *cur_field;
6094 while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD))
6095 *cur_field = f = f->next;
6096 if (!f)
6097 tcc_error("too many field init");
6098 type = &f->type;
6099 c += f->c;
6102 /* must put zero in holes (note that doing it that way
6103 ensures that it even works with designators) */
6104 if (!size_only && c - corig > al)
6105 init_putz(sec, corig + al, c - corig - al);
6106 decl_initializer(type, sec, c, 0, size_only);
6108 /* XXX: make it more general */
6109 if (!size_only && nb_elems > 1) {
6110 unsigned long c_end;
6111 uint8_t *src, *dst;
6112 int i;
6114 if (!sec) {
6115 vset(type, VT_LOCAL|VT_LVAL, c);
6116 for (i = 1; i < nb_elems; i++) {
6117 vset(type, VT_LOCAL|VT_LVAL, c + elem_size * i);
6118 vswap();
6119 vstore();
6121 vpop();
6122 } else {
6123 c_end = c + nb_elems * elem_size;
6124 if (c_end > sec->data_allocated)
6125 section_realloc(sec, c_end);
6126 src = sec->data + c;
6127 dst = src;
6128 for(i = 1; i < nb_elems; i++) {
6129 dst += elem_size;
6130 memcpy(dst, src, elem_size);
6134 c += nb_elems * type_size(type, &align);
6135 if (c - corig > al)
6136 al = c - corig;
6137 return al;
6140 /* store a value or an expression directly in global data or in local array */
6141 static void init_putv(CType *type, Section *sec, unsigned long c)
6143 int bt, bit_pos, bit_size;
6144 void *ptr;
6145 unsigned long long bit_mask;
6146 CType dtype;
6148 dtype = *type;
6149 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
6151 if (sec) {
6152 int size, align;
6153 /* XXX: not portable */
6154 /* XXX: generate error if incorrect relocation */
6155 gen_assign_cast(&dtype);
6156 bt = type->t & VT_BTYPE;
6157 size = type_size(type, &align);
6158 section_reserve(sec, c + size);
6159 ptr = sec->data + c;
6160 /* XXX: make code faster ? */
6161 if (!(type->t & VT_BITFIELD)) {
6162 bit_pos = 0;
6163 bit_size = PTR_SIZE * 8;
6164 bit_mask = -1LL;
6165 } else {
6166 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
6167 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6168 bit_mask = (1LL << bit_size) - 1;
6170 if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
6171 vtop->sym->v >= SYM_FIRST_ANOM &&
6172 /* XXX This rejects compound literals like
6173 '(void *){ptr}'. The problem is that '&sym' is
6174 represented the same way, which would be ruled out
6175 by the SYM_FIRST_ANOM check above, but also '"string"'
6176 in 'char *p = "string"' is represented the same
6177 with the type being VT_PTR and the symbol being an
6178 anonymous one. That is, there's no difference in vtop
6179 between '(void *){x}' and '&(void *){x}'. Ignore
6180 pointer typed entities here. Hopefully no real code
6181 will every use compound literals with scalar type. */
6182 (vtop->type.t & VT_BTYPE) != VT_PTR) {
6183 /* These come from compound literals, memcpy stuff over. */
6184 Section *ssec;
6185 ElfW(Sym) *esym;
6186 ElfW_Rel *rel;
6187 esym = &((ElfW(Sym) *)symtab_section->data)[vtop->sym->c];
6188 ssec = tcc_state->sections[esym->st_shndx];
6189 memmove (ptr, ssec->data + esym->st_value, size);
6190 if (ssec->reloc) {
6191 /* We need to copy over all memory contents, and that
6192 includes relocations. Use the fact that relocs are
6193 created it order, so look from the end of relocs
6194 until we hit one before the copied region. */
6195 int num_relocs = ssec->reloc->data_offset / sizeof(*rel);
6196 rel = (ElfW_Rel*)(ssec->reloc->data + ssec->reloc->data_offset);
6197 while (num_relocs--) {
6198 rel--;
6199 if (rel->r_offset >= esym->st_value + size)
6200 continue;
6201 if (rel->r_offset < esym->st_value)
6202 break;
6203 /* Note: if the same fields are initialized multiple
6204 times (possible with designators) then we possibly
6205 add multiple relocations for the same offset here.
6206 That would lead to wrong code, the last reloc needs
6207 to win. We clean this up later after the whole
6208 initializer is parsed. */
6209 put_elf_reloca(symtab_section, sec,
6210 c + rel->r_offset - esym->st_value,
6211 ELFW(R_TYPE)(rel->r_info),
6212 ELFW(R_SYM)(rel->r_info),
6213 #if PTR_SIZE == 8
6214 rel->r_addend
6215 #else
6217 #endif
6221 } else {
6222 if ((vtop->r & VT_SYM) &&
6223 (bt == VT_BYTE ||
6224 bt == VT_SHORT ||
6225 bt == VT_DOUBLE ||
6226 bt == VT_LDOUBLE ||
6227 #if PTR_SIZE == 8
6228 (bt == VT_LLONG && bit_size != 64) ||
6229 bt == VT_INT
6230 #else
6231 bt == VT_LLONG ||
6232 (bt == VT_INT && bit_size != 32)
6233 #endif
6235 tcc_error("initializer element is not computable at load time");
6236 switch(bt) {
6237 /* XXX: when cross-compiling we assume that each type has the
6238 same representation on host and target, which is likely to
6239 be wrong in the case of long double */
6240 case VT_BOOL:
6241 vtop->c.i = (vtop->c.i != 0);
6242 case VT_BYTE:
6243 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
6244 break;
6245 case VT_SHORT:
6246 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
6247 break;
6248 case VT_FLOAT:
6249 *(float*)ptr = vtop->c.f;
6250 break;
6251 case VT_DOUBLE:
6252 *(double *)ptr = vtop->c.d;
6253 break;
6254 case VT_LDOUBLE:
6255 if (sizeof(long double) == LDOUBLE_SIZE)
6256 *(long double *)ptr = vtop->c.ld;
6257 else if (sizeof(double) == LDOUBLE_SIZE)
6258 *(double *)ptr = (double)vtop->c.ld;
6259 #if (defined __i386__ || defined __x86_64__) && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
6260 else if (sizeof (long double) >= 10)
6261 memcpy(memset(ptr, 0, LDOUBLE_SIZE), &vtop->c.ld, 10);
6262 #ifdef __TINYC__
6263 else if (sizeof (long double) == sizeof (double))
6264 __asm__("fldl %1\nfstpt %0\n" : "=m"
6265 (memset(ptr, 0, LDOUBLE_SIZE), ptr) : "m" (vtop->c.ld));
6266 #endif
6267 #endif
6268 else
6269 tcc_error("can't cross compile long double constants");
6270 break;
6271 #if PTR_SIZE != 8
6272 case VT_LLONG:
6273 *(long long *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
6274 break;
6275 #else
6276 case VT_LLONG:
6277 #endif
6278 case VT_PTR:
6280 addr_t val = (vtop->c.i & bit_mask) << bit_pos;
6281 #if PTR_SIZE == 8
6282 if (vtop->r & VT_SYM)
6283 greloca(sec, vtop->sym, c, R_DATA_PTR, val);
6284 else
6285 *(addr_t *)ptr |= val;
6286 #else
6287 if (vtop->r & VT_SYM)
6288 greloc(sec, vtop->sym, c, R_DATA_PTR);
6289 *(addr_t *)ptr |= val;
6290 #endif
6291 break;
6293 default:
6295 int val = (vtop->c.i & bit_mask) << bit_pos;
6296 #if PTR_SIZE == 8
6297 if (vtop->r & VT_SYM)
6298 greloca(sec, vtop->sym, c, R_DATA_PTR, val);
6299 else
6300 *(int *)ptr |= val;
6301 #else
6302 if (vtop->r & VT_SYM)
6303 greloc(sec, vtop->sym, c, R_DATA_PTR);
6304 *(int *)ptr |= val;
6305 #endif
6306 break;
6310 vtop--;
6311 } else {
6312 vset(&dtype, VT_LOCAL|VT_LVAL, c);
6313 vswap();
6314 vstore();
6315 vpop();
6319 /* 't' contains the type and storage info. 'c' is the offset of the
6320 object in section 'sec'. If 'sec' is NULL, it means stack based
6321 allocation. 'first' is true if array '{' must be read (multi
6322 dimension implicit array init handling). 'size_only' is true if
6323 size only evaluation is wanted (only for arrays). */
6324 static void decl_initializer(CType *type, Section *sec, unsigned long c,
6325 int first, int size_only)
6327 int len, n, no_oblock, nb, i;
6328 int size1, align1;
6329 int have_elem;
6330 Sym *s, *f;
6331 Sym indexsym;
6332 CType *t1;
6334 /* If we currently are at an '}' or ',' we have read an initializer
6335 element in one of our callers, and not yet consumed it. */
6336 have_elem = tok == '}' || tok == ',';
6337 if (!have_elem && tok != '{' &&
6338 /* In case of strings we have special handling for arrays, so
6339 don't consume them as initializer value (which would commit them
6340 to some anonymous symbol). */
6341 tok != TOK_LSTR && tok != TOK_STR &&
6342 !size_only) {
6343 parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
6344 have_elem = 1;
6347 if (have_elem &&
6348 !(type->t & VT_ARRAY) &&
6349 /* Use i_c_parameter_t, to strip toplevel qualifiers.
6350 The source type might have VT_CONSTANT set, which is
6351 of course assignable to non-const elements. */
6352 is_compatible_unqualified_types(type, &vtop->type)) {
6353 init_putv(type, sec, c);
6354 } else if (type->t & VT_ARRAY) {
6355 s = type->ref;
6356 n = s->c;
6357 t1 = pointed_type(type);
6358 size1 = type_size(t1, &align1);
6360 no_oblock = 1;
6361 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
6362 tok == '{') {
6363 if (tok != '{')
6364 tcc_error("character array initializer must be a literal,"
6365 " optionally enclosed in braces");
6366 skip('{');
6367 no_oblock = 0;
6370 /* only parse strings here if correct type (otherwise: handle
6371 them as ((w)char *) expressions */
6372 if ((tok == TOK_LSTR &&
6373 #ifdef TCC_TARGET_PE
6374 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
6375 #else
6376 (t1->t & VT_BTYPE) == VT_INT
6377 #endif
6378 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
6379 len = 0;
6380 while (tok == TOK_STR || tok == TOK_LSTR) {
6381 int cstr_len, ch;
6383 /* compute maximum number of chars wanted */
6384 if (tok == TOK_STR)
6385 cstr_len = tokc.str.size;
6386 else
6387 cstr_len = tokc.str.size / sizeof(nwchar_t);
6388 cstr_len--;
6389 nb = cstr_len;
6390 if (n >= 0 && nb > (n - len))
6391 nb = n - len;
6392 if (!size_only) {
6393 if (cstr_len > nb)
6394 tcc_warning("initializer-string for array is too long");
6395 /* in order to go faster for common case (char
6396 string in global variable, we handle it
6397 specifically */
6398 if (sec && tok == TOK_STR && size1 == 1) {
6399 memcpy(sec->data + c + len, tokc.str.data, nb);
6400 } else {
6401 for(i=0;i<nb;i++) {
6402 if (tok == TOK_STR)
6403 ch = ((unsigned char *)tokc.str.data)[i];
6404 else
6405 ch = ((nwchar_t *)tokc.str.data)[i];
6406 vpushi(ch);
6407 init_putv(t1, sec, c + (len + i) * size1);
6411 len += nb;
6412 next();
6414 /* only add trailing zero if enough storage (no
6415 warning in this case since it is standard) */
6416 if (n < 0 || len < n) {
6417 if (!size_only) {
6418 vpushi(0);
6419 init_putv(t1, sec, c + (len * size1));
6421 len++;
6423 len *= size1;
6424 } else {
6425 indexsym.c = 0;
6426 f = &indexsym;
6428 do_init_list:
6429 len = 0;
6430 while (tok != '}' || have_elem) {
6431 len = decl_designator(type, sec, c, &f, size_only, len);
6432 have_elem = 0;
6433 if (type->t & VT_ARRAY) {
6434 ++indexsym.c;
6435 /* special test for multi dimensional arrays (may not
6436 be strictly correct if designators are used at the
6437 same time) */
6438 if (no_oblock && len >= n*size1)
6439 break;
6440 } else {
6441 if (s->type.t == VT_UNION)
6442 f = NULL;
6443 else
6444 f = f->next;
6445 if (no_oblock && f == NULL)
6446 break;
6449 if (tok == '}')
6450 break;
6451 skip(',');
6454 /* put zeros at the end */
6455 if (!size_only && len < n*size1)
6456 init_putz(sec, c + len, n*size1 - len);
6457 if (!no_oblock)
6458 skip('}');
6459 /* patch type size if needed, which happens only for array types */
6460 if (n < 0)
6461 s->c = size1 == 1 ? len : ((len + size1 - 1)/size1);
6462 } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
6463 size1 = 1;
6464 no_oblock = 1;
6465 if (first || tok == '{') {
6466 skip('{');
6467 no_oblock = 0;
6469 s = type->ref;
6470 f = s->next;
6471 n = s->c;
6472 goto do_init_list;
6473 } else if (tok == '{') {
6474 next();
6475 decl_initializer(type, sec, c, first, size_only);
6476 skip('}');
6477 } else if (size_only) {
6478 /* If we supported only ISO C we wouldn't have to accept calling
6479 this on anything than an array size_only==1 (and even then
6480 only on the outermost level, so no recursion would be needed),
6481 because initializing a flex array member isn't supported.
6482 But GNU C supports it, so we need to recurse even into
6483 subfields of structs and arrays when size_only is set. */
6484 /* just skip expression */
6485 skip_or_save_block(NULL);
6486 } else {
6487 if (!have_elem) {
6488 /* This should happen only when we haven't parsed
6489 the init element above for fear of committing a
6490 string constant to memory too early. */
6491 if (tok != TOK_STR && tok != TOK_LSTR)
6492 expect("string constant");
6493 parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
6495 init_putv(type, sec, c);
6499 /* parse an initializer for type 't' if 'has_init' is non zero, and
6500 allocate space in local or global data space ('r' is either
6501 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6502 variable 'v' of scope 'scope' is declared before initializers
6503 are parsed. If 'v' is zero, then a reference to the new object
6504 is put in the value stack. If 'has_init' is 2, a special parsing
6505 is done to handle string constants. */
6506 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
6507 int has_init, int v, int scope)
6509 int size, align, addr;
6510 ParseState saved_parse_state = {0};
6511 TokenString *init_str = NULL;
6512 Section *sec;
6513 Sym *flexible_array;
6514 Sym *sym = NULL;
6516 flexible_array = NULL;
6517 if ((type->t & VT_BTYPE) == VT_STRUCT) {
6518 Sym *field = type->ref->next;
6519 if (field) {
6520 while (field->next)
6521 field = field->next;
6522 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
6523 flexible_array = field;
6527 size = type_size(type, &align);
6528 /* If unknown size, we must evaluate it before
6529 evaluating initializers because
6530 initializers can generate global data too
6531 (e.g. string pointers or ISOC99 compound
6532 literals). It also simplifies local
6533 initializers handling */
6534 if (size < 0 || (flexible_array && has_init)) {
6535 if (!has_init)
6536 tcc_error("unknown type size");
6537 /* get all init string */
6538 if (has_init == 2) {
6539 init_str = tok_str_alloc();
6540 /* only get strings */
6541 while (tok == TOK_STR || tok == TOK_LSTR) {
6542 tok_str_add_tok(init_str);
6543 next();
6545 tok_str_add(init_str, -1);
6546 tok_str_add(init_str, 0);
6547 } else {
6548 skip_or_save_block(&init_str);
6551 /* compute size */
6552 save_parse_state(&saved_parse_state);
6554 begin_macro(init_str, 1);
6555 next();
6556 decl_initializer(type, NULL, 0, 1, 1);
6557 /* prepare second initializer parsing */
6558 macro_ptr = init_str->str;
6559 next();
6561 /* if still unknown size, error */
6562 size = type_size(type, &align);
6563 if (size < 0)
6564 tcc_error("unknown type size");
6566 /* If there's a flex member and it was used in the initializer
6567 adjust size. */
6568 if (flexible_array &&
6569 flexible_array->type.ref->c > 0)
6570 size += flexible_array->type.ref->c
6571 * pointed_size(&flexible_array->type);
6572 /* take into account specified alignment if bigger */
6573 if (ad->a.aligned) {
6574 int speca = 1 << (ad->a.aligned - 1);
6575 if (speca > align)
6576 align = speca;
6577 } else if (ad->a.packed) {
6578 align = 1;
6580 if ((r & VT_VALMASK) == VT_LOCAL) {
6581 sec = NULL;
6582 #ifdef CONFIG_TCC_BCHECK
6583 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
6584 loc--;
6586 #endif
6587 loc = (loc - size) & -align;
6588 addr = loc;
6589 #ifdef CONFIG_TCC_BCHECK
6590 /* handles bounds */
6591 /* XXX: currently, since we do only one pass, we cannot track
6592 '&' operators, so we add only arrays */
6593 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
6594 addr_t *bounds_ptr;
6595 /* add padding between regions */
6596 loc--;
6597 /* then add local bound info */
6598 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t));
6599 bounds_ptr[0] = addr;
6600 bounds_ptr[1] = size;
6602 #endif
6603 if (v) {
6604 /* local variable */
6605 #ifdef CONFIG_TCC_ASM
6606 if (ad->asm_label) {
6607 int reg = asm_parse_regvar(ad->asm_label);
6608 if (reg >= 0)
6609 r = (r & ~VT_VALMASK) | reg;
6611 #endif
6612 sym = sym_push(v, type, r, addr);
6613 sym->a = ad->a;
6614 } else {
6615 /* push local reference */
6616 vset(type, r, addr);
6618 } else {
6619 if (v && scope == VT_CONST) {
6620 /* see if the symbol was already defined */
6621 sym = sym_find(v);
6622 if (sym) {
6623 patch_storage(sym, ad, type);
6624 if (sym->type.t & VT_EXTERN) {
6625 /* if the variable is extern, it was not allocated */
6626 sym->type.t &= ~VT_EXTERN;
6627 /* set array size if it was omitted in extern
6628 declaration */
6629 if ((sym->type.t & VT_ARRAY) &&
6630 sym->type.ref->c < 0 &&
6631 type->ref->c >= 0)
6632 sym->type.ref->c = type->ref->c;
6633 } else if (!has_init) {
6634 /* we accept several definitions of the same
6635 global variable. this is tricky, because we
6636 must play with the SHN_COMMON type of the symbol */
6637 /* no init data, we won't add more to the symbol */
6638 goto no_alloc;
6639 } else if (sym->c) {
6640 ElfW(Sym) *esym;
6641 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
6642 if (esym->st_shndx == data_section->sh_num)
6643 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
6648 /* allocate symbol in corresponding section */
6649 sec = ad->section;
6650 if (!sec) {
6651 if (has_init)
6652 sec = data_section;
6653 else if (tcc_state->nocommon)
6654 sec = bss_section;
6657 if (sec) {
6658 addr = section_add(sec, size, align);
6659 #ifdef CONFIG_TCC_BCHECK
6660 /* add padding if bound check */
6661 if (tcc_state->do_bounds_check)
6662 section_add(sec, 1, 1);
6663 #endif
6664 } else {
6665 addr = align; /* SHN_COMMON is special, symbol value is align */
6666 sec = common_section;
6669 if (v) {
6670 if (!sym) {
6671 sym = sym_push(v, type, r | VT_SYM, 0);
6672 patch_storage(sym, ad, NULL);
6674 /* update symbol definition */
6675 put_extern_sym(sym, sec, addr, size);
6676 } else {
6677 /* push global reference */
6678 sym = get_sym_ref(type, sec, addr, size);
6679 vpushsym(type, sym);
6680 vtop->r |= r;
6683 #ifdef CONFIG_TCC_BCHECK
6684 /* handles bounds now because the symbol must be defined
6685 before for the relocation */
6686 if (tcc_state->do_bounds_check) {
6687 addr_t *bounds_ptr;
6689 greloca(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR, 0);
6690 /* then add global bound info */
6691 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t));
6692 bounds_ptr[0] = 0; /* relocated */
6693 bounds_ptr[1] = size;
6695 #endif
6698 if (type->t & VT_VLA) {
6699 int a;
6701 /* save current stack pointer */
6702 if (vlas_in_scope == 0) {
6703 if (vla_sp_root_loc == -1)
6704 vla_sp_root_loc = (loc -= PTR_SIZE);
6705 gen_vla_sp_save(vla_sp_root_loc);
6708 vla_runtime_type_size(type, &a);
6709 gen_vla_alloc(type, a);
6710 gen_vla_sp_save(addr);
6711 vla_sp_loc = addr;
6712 vlas_in_scope++;
6714 } else if (has_init) {
6715 size_t oldreloc_offset = 0;
6716 if (sec && sec->reloc)
6717 oldreloc_offset = sec->reloc->data_offset;
6718 decl_initializer(type, sec, addr, 1, 0);
6719 if (sec && sec->reloc)
6720 squeeze_multi_relocs(sec, oldreloc_offset);
6721 /* patch flexible array member size back to -1, */
6722 /* for possible subsequent similar declarations */
6723 if (flexible_array)
6724 flexible_array->type.ref->c = -1;
6727 no_alloc:
6728 /* restore parse state if needed */
6729 if (init_str) {
6730 end_macro();
6731 restore_parse_state(&saved_parse_state);
6735 /* parse a function defined by symbol 'sym' and generate its code in
6736 'cur_text_section' */
6737 static void gen_function(Sym *sym)
6739 nocode_wanted = 0;
6740 ind = cur_text_section->data_offset;
6741 /* NOTE: we patch the symbol size later */
6742 put_extern_sym(sym, cur_text_section, ind, 0);
6743 funcname = get_tok_str(sym->v, NULL);
6744 func_ind = ind;
6745 /* Initialize VLA state */
6746 vla_sp_loc = -1;
6747 vla_sp_root_loc = -1;
6748 /* put debug symbol */
6749 tcc_debug_funcstart(tcc_state, sym);
6750 /* push a dummy symbol to enable local sym storage */
6751 sym_push2(&local_stack, SYM_FIELD, 0, 0);
6752 local_scope = 1; /* for function parameters */
6753 gfunc_prolog(&sym->type);
6754 local_scope = 0;
6755 rsym = 0;
6756 block(NULL, NULL, 0);
6757 nocode_wanted = 0;
6758 gsym(rsym);
6759 gfunc_epilog();
6760 cur_text_section->data_offset = ind;
6761 label_pop(&global_label_stack, NULL);
6762 /* reset local stack */
6763 local_scope = 0;
6764 sym_pop(&local_stack, NULL, 0);
6765 /* end of function */
6766 /* patch symbol size */
6767 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
6768 ind - func_ind;
6769 tcc_debug_funcend(tcc_state, ind - func_ind);
6770 /* It's better to crash than to generate wrong code */
6771 cur_text_section = NULL;
6772 funcname = ""; /* for safety */
6773 func_vt.t = VT_VOID; /* for safety */
6774 func_var = 0; /* for safety */
6775 ind = 0; /* for safety */
6776 nocode_wanted = 1;
6777 check_vstack();
6780 static void gen_inline_functions(TCCState *s)
6782 Sym *sym;
6783 int inline_generated, i, ln;
6784 struct InlineFunc *fn;
6786 ln = file->line_num;
6787 /* iterate while inline function are referenced */
6788 for(;;) {
6789 inline_generated = 0;
6790 for (i = 0; i < s->nb_inline_fns; ++i) {
6791 fn = s->inline_fns[i];
6792 sym = fn->sym;
6793 if (sym && sym->c) {
6794 /* the function was used: generate its code and
6795 convert it to a normal function */
6796 fn->sym = NULL;
6797 if (file)
6798 pstrcpy(file->filename, sizeof file->filename, fn->filename);
6799 sym->type.t &= ~VT_INLINE;
6801 begin_macro(fn->func_str, 1);
6802 next();
6803 cur_text_section = text_section;
6804 gen_function(sym);
6805 end_macro();
6807 inline_generated = 1;
6810 if (!inline_generated)
6811 break;
6813 file->line_num = ln;
6816 ST_FUNC void free_inline_functions(TCCState *s)
6818 int i;
6819 /* free tokens of unused inline functions */
6820 for (i = 0; i < s->nb_inline_fns; ++i) {
6821 struct InlineFunc *fn = s->inline_fns[i];
6822 if (fn->sym)
6823 tok_str_free(fn->func_str);
6825 dynarray_reset(&s->inline_fns, &s->nb_inline_fns);
6828 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
6829 if parsing old style parameter decl list (and FUNC_SYM is set then) */
6830 static int decl0(int l, int is_for_loop_init, Sym *func_sym)
6832 int v, has_init, r;
6833 CType type, btype;
6834 Sym *sym;
6835 AttributeDef ad;
6837 while (1) {
6838 if (!parse_btype(&btype, &ad)) {
6839 if (is_for_loop_init)
6840 return 0;
6841 /* skip redundant ';' if not in old parameter decl scope */
6842 if (tok == ';' && l != VT_CMP) {
6843 next();
6844 continue;
6846 if (l == VT_CONST &&
6847 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6848 /* global asm block */
6849 asm_global_instr();
6850 continue;
6852 /* special test for old K&R protos without explicit int
6853 type. Only accepted when defining global data */
6854 if (l != VT_CONST || tok < TOK_UIDENT)
6855 break;
6856 btype.t = VT_INT;
6858 if (tok == ';') {
6859 if ((btype.t & VT_BTYPE) == VT_STRUCT) {
6860 int v = btype.ref->v;
6861 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)
6862 tcc_warning("unnamed struct/union that defines no instances");
6863 next();
6864 continue;
6866 if (IS_ENUM(btype.t)) {
6867 next();
6868 continue;
6871 while (1) { /* iterate thru each declaration */
6872 type = btype;
6873 /* If the base type itself was an array type of unspecified
6874 size (like in 'typedef int arr[]; arr x = {1};') then
6875 we will overwrite the unknown size by the real one for
6876 this decl. We need to unshare the ref symbol holding
6877 that size. */
6878 if ((type.t & VT_ARRAY) && type.ref->c < 0) {
6879 type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c);
6881 type_decl(&type, &ad, &v, TYPE_DIRECT);
6882 #if 0
6884 char buf[500];
6885 type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL));
6886 printf("type = '%s'\n", buf);
6888 #endif
6889 if ((type.t & VT_BTYPE) == VT_FUNC) {
6890 if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
6891 tcc_error("function without file scope cannot be static");
6893 /* if old style function prototype, we accept a
6894 declaration list */
6895 sym = type.ref;
6896 if (sym->f.func_type == FUNC_OLD && l == VT_CONST)
6897 decl0(VT_CMP, 0, sym);
6900 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6901 ad.asm_label = asm_label_instr();
6902 /* parse one last attribute list, after asm label */
6903 parse_attribute(&ad);
6904 if (tok == '{')
6905 expect(";");
6908 #ifdef TCC_TARGET_PE
6909 if (ad.a.dllimport || ad.a.dllexport) {
6910 if (type.t & (VT_STATIC|VT_TYPEDEF))
6911 tcc_error("cannot have dll linkage with static or typedef");
6912 if (ad.a.dllimport) {
6913 if ((type.t & VT_BTYPE) == VT_FUNC)
6914 ad.a.dllimport = 0;
6915 else
6916 type.t |= VT_EXTERN;
6919 #endif
6920 if (tok == '{') {
6921 if (l != VT_CONST)
6922 tcc_error("cannot use local functions");
6923 if ((type.t & VT_BTYPE) != VT_FUNC)
6924 expect("function definition");
6926 /* reject abstract declarators in function definition
6927 make old style params without decl have int type */
6928 sym = type.ref;
6929 while ((sym = sym->next) != NULL) {
6930 if (!(sym->v & ~SYM_FIELD))
6931 expect("identifier");
6932 if (sym->type.t == VT_VOID)
6933 sym->type = int_type;
6936 /* XXX: cannot do better now: convert extern line to static inline */
6937 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
6938 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6940 sym = sym_find(v);
6941 if (sym) {
6942 Sym *ref;
6943 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
6944 goto func_error1;
6946 ref = sym->type.ref;
6948 /* use func_call from prototype if not defined */
6949 if (ref->f.func_call != FUNC_CDECL
6950 && type.ref->f.func_call == FUNC_CDECL)
6951 type.ref->f.func_call = ref->f.func_call;
6953 /* use static from prototype */
6954 if (sym->type.t & VT_STATIC)
6955 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6957 /* If the definition has no visibility use the
6958 one from prototype. */
6959 if (!type.ref->a.visibility)
6960 type.ref->a.visibility = ref->a.visibility;
6961 /* apply other storage attributes from prototype */
6962 type.ref->a.dllexport |= ref->a.dllexport;
6963 type.ref->a.weak |= ref->a.weak;
6965 if (!is_compatible_types(&sym->type, &type)) {
6966 func_error1:
6967 tcc_error("incompatible types for redefinition of '%s'",
6968 get_tok_str(v, NULL));
6970 if (ref->f.func_body)
6971 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
6972 /* if symbol is already defined, then put complete type */
6973 sym->type = type;
6975 } else {
6976 /* put function symbol */
6977 sym = global_identifier_push(v, type.t, 0);
6978 sym->type.ref = type.ref;
6981 sym->type.ref->f.func_body = 1;
6982 sym->r = VT_SYM | VT_CONST;
6983 patch_storage(sym, &ad, NULL);
6985 /* static inline functions are just recorded as a kind
6986 of macro. Their code will be emitted at the end of
6987 the compilation unit only if they are used */
6988 if ((type.t & (VT_INLINE | VT_STATIC)) ==
6989 (VT_INLINE | VT_STATIC)) {
6990 struct InlineFunc *fn;
6991 const char *filename;
6993 filename = file ? file->filename : "";
6994 fn = tcc_malloc(sizeof *fn + strlen(filename));
6995 strcpy(fn->filename, filename);
6996 fn->sym = sym;
6997 skip_or_save_block(&fn->func_str);
6998 dynarray_add(&tcc_state->inline_fns,
6999 &tcc_state->nb_inline_fns, fn);
7000 } else {
7001 /* compute text section */
7002 cur_text_section = ad.section;
7003 if (!cur_text_section)
7004 cur_text_section = text_section;
7005 gen_function(sym);
7007 break;
7008 } else {
7009 if (l == VT_CMP) {
7010 /* find parameter in function parameter list */
7011 for (sym = func_sym->next; sym; sym = sym->next)
7012 if ((sym->v & ~SYM_FIELD) == v)
7013 goto found;
7014 tcc_error("declaration for parameter '%s' but no such parameter",
7015 get_tok_str(v, NULL));
7016 found:
7017 if (type.t & VT_STORAGE) /* 'register' is okay */
7018 tcc_error("storage class specified for '%s'",
7019 get_tok_str(v, NULL));
7020 if (sym->type.t != VT_VOID)
7021 tcc_error("redefinition of parameter '%s'",
7022 get_tok_str(v, NULL));
7023 convert_parameter_type(&type);
7024 sym->type = type;
7025 } else if (type.t & VT_TYPEDEF) {
7026 /* save typedefed type */
7027 /* XXX: test storage specifiers ? */
7028 sym = sym_find(v);
7029 if (sym && sym->sym_scope == local_scope) {
7030 if (!is_compatible_types(&sym->type, &type)
7031 || !(sym->type.t & VT_TYPEDEF))
7032 tcc_error("incompatible redefinition of '%s'",
7033 get_tok_str(v, NULL));
7034 sym->type = type;
7035 } else {
7036 sym = sym_push(v, &type, 0, 0);
7038 sym->a = ad.a;
7039 } else {
7040 r = 0;
7041 if ((type.t & VT_BTYPE) == VT_FUNC) {
7042 /* external function definition */
7043 /* specific case for func_call attribute */
7044 type.ref->a = ad.a;
7045 } else if (!(type.t & VT_ARRAY)) {
7046 /* not lvalue if array */
7047 r |= lvalue_type(type.t);
7049 has_init = (tok == '=');
7050 if (has_init && (type.t & VT_VLA))
7051 tcc_error("variable length array cannot be initialized");
7052 if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) ||
7053 ((type.t & VT_BTYPE) == VT_FUNC) ||
7054 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
7055 !has_init && l == VT_CONST && type.ref->c < 0)) {
7056 /* external variable or function */
7057 /* NOTE: as GCC, uninitialized global static
7058 arrays of null size are considered as
7059 extern */
7060 sym = external_sym(v, &type, r, &ad);
7061 if (ad.alias_target) {
7062 Section tsec;
7063 ElfW(Sym) *esym;
7064 Sym *alias_target;
7065 alias_target = sym_find(ad.alias_target);
7066 if (!alias_target || !alias_target->c)
7067 tcc_error("unsupported forward __alias__ attribute");
7068 esym = &((ElfW(Sym) *)symtab_section->data)[alias_target->c];
7069 tsec.sh_num = esym->st_shndx;
7070 put_extern_sym2(sym, &tsec, esym->st_value, esym->st_size, 0);
7072 } else {
7073 if (type.t & VT_STATIC)
7074 r |= VT_CONST;
7075 else
7076 r |= l;
7077 if (has_init)
7078 next();
7079 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
7082 if (tok != ',') {
7083 if (is_for_loop_init)
7084 return 1;
7085 skip(';');
7086 break;
7088 next();
7090 ad.a.aligned = 0;
7093 return 0;
7096 ST_FUNC void decl(int l)
7098 decl0(l, 0, NULL);
7101 /* ------------------------------------------------------------------------- */