Add USES file which list some software known to support tcc builds.
[tinycc.git] / tccgen.c
blob77a71eeb856b4b906a90a70f350c5495ecb8c1d7
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 #define USING_GLOBALS
22 #include "tcc.h"
24 /********************************************************/
25 /* global variables */
27 /* loc : local variable index
28 ind : output code index
29 rsym: return symbol
30 anon_sym: anonymous symbol index
32 ST_DATA int rsym, anon_sym, ind, loc;
34 ST_DATA Sym *global_stack;
35 ST_DATA Sym *local_stack;
36 ST_DATA Sym *define_stack;
37 ST_DATA Sym *global_label_stack;
38 ST_DATA Sym *local_label_stack;
40 static Sym *sym_free_first;
41 static void **sym_pools;
42 static int nb_sym_pools;
44 static Sym *all_cleanups, *pending_gotos;
45 static int local_scope;
46 static int in_sizeof;
47 static int in_generic;
48 static int section_sym;
50 ST_DATA SValue *vtop;
51 static SValue _vstack[1 + VSTACK_SIZE];
52 #define vstack (_vstack + 1)
54 ST_DATA int const_wanted; /* true if constant wanted */
55 ST_DATA int nocode_wanted; /* no code generation wanted */
56 #define unevalmask 0xffff /* unevaluated subexpression */
57 #define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
58 #define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
60 /* Automagical code suppression ----> */
61 #define CODE_OFF() (nocode_wanted |= 0x20000000)
62 #define CODE_ON() (nocode_wanted &= ~0x20000000)
64 /* Clear 'nocode_wanted' at label if it was used */
65 ST_FUNC void gsym(int t) { if (t) { gsym_addr(t, ind); CODE_ON(); }}
66 static int gind(void) { CODE_ON(); return ind; }
68 /* Set 'nocode_wanted' after unconditional jumps */
69 static void gjmp_addr_acs(int t) { gjmp_addr(t); CODE_OFF(); }
70 static int gjmp_acs(int t) { t = gjmp(t); CODE_OFF(); return t; }
72 /* These are #undef'd at the end of this file */
73 #define gjmp_addr gjmp_addr_acs
74 #define gjmp gjmp_acs
75 /* <---- */
77 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
78 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
79 ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
80 ST_DATA int func_vc;
81 static int last_line_num, new_file, func_ind; /* debug info control */
82 ST_DATA const char *funcname;
83 ST_DATA CType int_type, func_old_type, char_type, char_pointer_type;
84 static CString initstr;
86 #if PTR_SIZE == 4
87 #define VT_SIZE_T (VT_INT | VT_UNSIGNED)
88 #define VT_PTRDIFF_T VT_INT
89 #elif LONG_SIZE == 4
90 #define VT_SIZE_T (VT_LLONG | VT_UNSIGNED)
91 #define VT_PTRDIFF_T VT_LLONG
92 #else
93 #define VT_SIZE_T (VT_LONG | VT_LLONG | VT_UNSIGNED)
94 #define VT_PTRDIFF_T (VT_LONG | VT_LLONG)
95 #endif
97 ST_DATA struct switch_t {
98 struct case_t {
99 int64_t v1, v2;
100 int sym;
101 } **p; int n; /* list of case ranges */
102 int def_sym; /* default symbol */
103 int *bsym;
104 struct scope *scope;
105 struct switch_t *prev;
106 SValue sv;
107 } *cur_switch; /* current switch */
109 #define MAX_TEMP_LOCAL_VARIABLE_NUMBER 8
110 /*list of temporary local variables on the stack in current function. */
111 ST_DATA struct temp_local_variable {
112 int location; //offset on stack. Svalue.c.i
113 short size;
114 short align;
115 } arr_temp_local_vars[MAX_TEMP_LOCAL_VARIABLE_NUMBER];
116 short nb_temp_local_vars;
118 static struct scope {
119 struct scope *prev;
120 struct { int loc, num; } vla;
121 struct { Sym *s; int n; } cl;
122 int *bsym, *csym;
123 Sym *lstk, *llstk;
124 } *cur_scope, *loop_scope, *root_scope;
126 /********************************************************/
127 /* stab debug support */
129 static const struct {
130 int type;
131 const char *name;
132 } default_debug[] = {
133 { VT_INT, "int:t1=r1;-2147483648;2147483647;" },
134 { VT_BYTE, "char:t2=r2;0;127;" },
135 #if LONG_SIZE == 4
136 { VT_LONG | VT_INT, "long int:t3=r3;-2147483648;2147483647;" },
137 #else
138 { VT_LLONG | VT_LONG, "long int:t3=r3;-9223372036854775808;9223372036854775807;" },
139 #endif
140 { VT_INT | VT_UNSIGNED, "unsigned int:t4=r4;0;037777777777;" },
141 #if LONG_SIZE == 4
142 { VT_LONG | VT_INT | VT_UNSIGNED, "long unsigned int:t5=r5;0;037777777777;" },
143 #else
144 /* use octal instead of -1 so size_t works (-gstabs+ in gcc) */
145 { VT_LLONG | VT_LONG | VT_UNSIGNED, "long unsigned int:t5=r5;0;01777777777777777777777;" },
146 #endif
147 { VT_QLONG, "__int128:t6=r6;0;-1;" },
148 { VT_QLONG | VT_UNSIGNED, "__int128 unsigned:t7=r7;0;-1;" },
149 { VT_LLONG, "long long int:t8=r8;-9223372036854775808;9223372036854775807;" },
150 { VT_LLONG | VT_UNSIGNED, "long long unsigned int:t9=r9;0;01777777777777777777777;" },
151 { VT_SHORT, "short int:t10=r10;-32768;32767;" },
152 { VT_SHORT | VT_UNSIGNED, "short unsigned int:t11=r11;0;65535;" },
153 { VT_BYTE | VT_DEFSIGN, "signed char:t12=r12;-128;127;" },
154 { VT_BYTE | VT_DEFSIGN | VT_UNSIGNED, "unsigned char:t13=r13;0;255;" },
155 { VT_FLOAT, "float:t14=r1;4;0;" },
156 { VT_DOUBLE, "double:t15=r1;8;0;" },
157 { VT_LDOUBLE, "long double:t16=r1;16;0;" },
158 { -1, "_Float32:t17=r1;4;0;" },
159 { -1, "_Float64:t18=r1;8;0;" },
160 { -1, "_Float128:t19=r1;16;0;" },
161 { -1, "_Float32x:t20=r1;8;0;" },
162 { -1, "_Float64x:t21=r1;16;0;" },
163 { -1, "_Decimal32:t22=r1;4;0;" },
164 { -1, "_Decimal64:t23=r1;8;0;" },
165 { -1, "_Decimal128:t24=r1;16;0;" },
166 /* if default char is unsigned */
167 { VT_BYTE | VT_UNSIGNED, "unsigned char:t25=r25;0;255;" },
168 { VT_VOID, "void:t26=26" },
171 static int debug_next_type;
173 static struct debug_hash {
174 int debug_type;
175 Sym *type;
176 } *debug_hash;
178 static int n_debug_hash;
180 static struct debug_info {
181 int start;
182 int end;
183 int n_sym;
184 struct debug_sym {
185 int type;
186 unsigned long value;
187 char *str;
188 Section *sec;
189 int sym_index;
190 } *sym;
191 struct debug_info *child, *next, *last, *parent;
192 } *debug_info, *debug_info_root;
194 /********************************************************/
195 #if 1
196 #define precedence_parser
197 static void init_prec(void);
198 #endif
199 /********************************************************/
200 #ifndef CONFIG_TCC_ASM
201 ST_FUNC void asm_instr(void)
203 tcc_error("inline asm() not supported");
205 ST_FUNC void asm_global_instr(void)
207 tcc_error("inline asm() not supported");
209 #endif
211 /* ------------------------------------------------------------------------- */
212 static void gen_cast(CType *type);
213 static void gen_cast_s(int t);
214 static inline CType *pointed_type(CType *type);
215 static int is_compatible_types(CType *type1, CType *type2);
216 static int parse_btype(CType *type, AttributeDef *ad);
217 static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
218 static void parse_expr_type(CType *type);
219 static void init_putv(CType *type, Section *sec, unsigned long c);
220 static void decl_initializer(CType *type, Section *sec, unsigned long c, int flags);
221 static void block(int is_expr);
222 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
223 static void decl(int l);
224 static int decl0(int l, int is_for_loop_init, Sym *);
225 static void expr_eq(void);
226 static void vla_runtime_type_size(CType *type, int *a);
227 static int is_compatible_unqualified_types(CType *type1, CType *type2);
228 static inline int64_t expr_const64(void);
229 static void vpush64(int ty, unsigned long long v);
230 static void vpush(CType *type);
231 static int gvtst(int inv, int t);
232 static void gen_inline_functions(TCCState *s);
233 static void free_inline_functions(TCCState *s);
234 static void skip_or_save_block(TokenString **str);
235 static void gv_dup(void);
236 static int get_temp_local_var(int size,int align);
237 static void clear_temp_local_var_list();
238 static void cast_error(CType *st, CType *dt);
240 ST_INLN int is_float(int t)
242 int bt = t & VT_BTYPE;
243 return bt == VT_LDOUBLE
244 || bt == VT_DOUBLE
245 || bt == VT_FLOAT
246 || bt == VT_QFLOAT;
249 static inline int is_integer_btype(int bt)
251 return bt == VT_BYTE
252 || bt == VT_BOOL
253 || bt == VT_SHORT
254 || bt == VT_INT
255 || bt == VT_LLONG;
258 static int btype_size(int bt)
260 return bt == VT_BYTE || bt == VT_BOOL ? 1 :
261 bt == VT_SHORT ? 2 :
262 bt == VT_INT ? 4 :
263 bt == VT_LLONG ? 8 :
264 bt == VT_PTR ? PTR_SIZE : 0;
267 /* returns function return register from type */
268 static int R_RET(int t)
270 if (!is_float(t))
271 return REG_IRET;
272 #ifdef TCC_TARGET_X86_64
273 if ((t & VT_BTYPE) == VT_LDOUBLE)
274 return TREG_ST0;
275 #elif defined TCC_TARGET_RISCV64
276 if ((t & VT_BTYPE) == VT_LDOUBLE)
277 return REG_IRET;
278 #endif
279 return REG_FRET;
282 /* returns 2nd function return register, if any */
283 static int R2_RET(int t)
285 t &= VT_BTYPE;
286 #if PTR_SIZE == 4
287 if (t == VT_LLONG)
288 return REG_IRE2;
289 #elif defined TCC_TARGET_X86_64
290 if (t == VT_QLONG)
291 return REG_IRE2;
292 if (t == VT_QFLOAT)
293 return REG_FRE2;
294 #elif defined TCC_TARGET_RISCV64
295 if (t == VT_LDOUBLE)
296 return REG_IRE2;
297 #endif
298 return VT_CONST;
301 /* returns true for two-word types */
302 #define USING_TWO_WORDS(t) (R2_RET(t) != VT_CONST)
304 /* put function return registers to stack value */
305 static void PUT_R_RET(SValue *sv, int t)
307 sv->r = R_RET(t), sv->r2 = R2_RET(t);
310 /* returns function return register class for type t */
311 static int RC_RET(int t)
313 return reg_classes[R_RET(t)] & ~(RC_FLOAT | RC_INT);
316 /* returns generic register class for type t */
317 static int RC_TYPE(int t)
319 if (!is_float(t))
320 return RC_INT;
321 #ifdef TCC_TARGET_X86_64
322 if ((t & VT_BTYPE) == VT_LDOUBLE)
323 return RC_ST0;
324 if ((t & VT_BTYPE) == VT_QFLOAT)
325 return RC_FRET;
326 #elif defined TCC_TARGET_RISCV64
327 if ((t & VT_BTYPE) == VT_LDOUBLE)
328 return RC_INT;
329 #endif
330 return RC_FLOAT;
333 /* returns 2nd register class corresponding to t and rc */
334 static int RC2_TYPE(int t, int rc)
336 if (!USING_TWO_WORDS(t))
337 return 0;
338 #ifdef RC_IRE2
339 if (rc == RC_IRET)
340 return RC_IRE2;
341 #endif
342 #ifdef RC_FRE2
343 if (rc == RC_FRET)
344 return RC_FRE2;
345 #endif
346 if (rc & RC_FLOAT)
347 return RC_FLOAT;
348 return RC_INT;
351 /* we use our own 'finite' function to avoid potential problems with
352 non standard math libs */
353 /* XXX: endianness dependent */
354 ST_FUNC int ieee_finite(double d)
356 int p[4];
357 memcpy(p, &d, sizeof(double));
358 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
361 /* compiling intel long double natively */
362 #if (defined __i386__ || defined __x86_64__) \
363 && (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
364 # define TCC_IS_NATIVE_387
365 #endif
367 ST_FUNC void test_lvalue(void)
369 if (!(vtop->r & VT_LVAL))
370 expect("lvalue");
373 ST_FUNC void check_vstack(void)
375 if (vtop != vstack - 1)
376 tcc_error("internal compiler error: vstack leak (%d)",
377 (int)(vtop - vstack + 1));
380 /* ------------------------------------------------------------------------- */
381 /* vstack debugging aid */
383 #if 0
384 void pv (const char *lbl, int a, int b)
386 int i;
387 for (i = a; i < a + b; ++i) {
388 SValue *p = &vtop[-i];
389 printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
390 lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
393 #endif
395 /* ------------------------------------------------------------------------- */
396 /* start of translation unit info */
397 ST_FUNC void tcc_debug_start(TCCState *s1)
399 if (s1->do_debug) {
400 int i;
401 char buf[512];
403 /* file info: full path + filename */
404 section_sym = put_elf_sym(symtab_section, 0, 0,
405 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
406 text_section->sh_num, NULL);
407 getcwd(buf, sizeof(buf));
408 #ifdef _WIN32
409 normalize_slashes(buf);
410 #endif
411 pstrcat(buf, sizeof(buf), "/");
412 put_stabs_r(s1, buf, N_SO, 0, 0,
413 text_section->data_offset, text_section, section_sym);
414 put_stabs_r(s1, file->prev->filename, N_SO, 0, 0,
415 text_section->data_offset, text_section, section_sym);
416 for (i = 0; i < sizeof (default_debug) / sizeof (default_debug[0]); i++)
417 put_stabs(s1, default_debug[i].name, N_LSYM, 0, 0, 0);
419 new_file = last_line_num = 0;
420 func_ind = -1;
421 debug_next_type = sizeof(default_debug) / sizeof(default_debug[0]);
422 debug_hash = NULL;
423 n_debug_hash = 0;
425 /* we're currently 'including' the <command line> */
426 tcc_debug_bincl(s1);
429 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
430 symbols can be safely used */
431 put_elf_sym(symtab_section, 0, 0,
432 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
433 SHN_ABS, file->filename);
436 static void tcc_debug_stabs (TCCState *s1, const char *str, int type, unsigned long value,
437 Section *sec, int sym_index)
439 struct debug_sym *s;
441 if (debug_info) {
442 debug_info->sym =
443 (struct debug_sym *)tcc_realloc (debug_info->sym,
444 sizeof(struct debug_sym) *
445 (debug_info->n_sym + 1));
446 s = debug_info->sym + debug_info->n_sym++;
447 s->type = type;
448 s->value = value;
449 s->str = tcc_strdup(str);
450 s->sec = sec;
451 s->sym_index = sym_index;
453 else if (sec)
454 put_stabs_r (s1, str, type, 0, 0, value, sec, sym_index);
455 else
456 put_stabs (s1, str, type, 0, 0, value);
459 static void tcc_debug_stabn(int type, int value)
461 if (type == N_LBRAC) {
462 struct debug_info *info =
463 (struct debug_info *) tcc_mallocz(sizeof (*info));
465 info->start = value;
466 info->parent = debug_info;
467 if (debug_info) {
468 if (debug_info->child) {
469 if (debug_info->child->last)
470 debug_info->child->last->next = info;
471 else
472 debug_info->child->next = info;
473 debug_info->child->last = info;
475 else
476 debug_info->child = info;
478 else
479 debug_info_root = info;
480 debug_info = info;
482 else {
483 debug_info->end = value;
484 debug_info = debug_info->parent;
488 static void tcc_get_debug_info(TCCState *s1, Sym *s, CString *result)
490 int type;
491 int n = 0;
492 int debug_type = -1;
493 Sym *t = s;
494 CString str;
496 for (;;) {
497 type = t->type.t & ~(VT_EXTERN | VT_STATIC | VT_CONSTANT | VT_VOLATILE);
498 if ((type & VT_BTYPE) != VT_BYTE)
499 type &= ~VT_DEFSIGN;
500 if (type == VT_PTR || type == (VT_PTR | VT_ARRAY))
501 n++, t = t->type.ref;
502 else
503 break;
505 if ((type & VT_BTYPE) == VT_STRUCT) {
506 int i;
508 t = t->type.ref;
509 for (i = 0; i < n_debug_hash; i++) {
510 if (t == debug_hash[i].type) {
511 debug_type = debug_hash[i].debug_type;
512 break;
515 if (debug_type == -1) {
516 debug_type = ++debug_next_type;
517 debug_hash = (struct debug_hash *)
518 tcc_realloc (debug_hash,
519 (n_debug_hash + 1) * sizeof(*debug_hash));
520 debug_hash[n_debug_hash].debug_type = debug_type;
521 debug_hash[n_debug_hash++].type = t;
522 cstr_new (&str);
523 cstr_printf (&str, "%s:T%d=%c%d",
524 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
525 ? "" : get_tok_str(t->v & ~SYM_STRUCT, NULL),
526 debug_type,
527 IS_UNION (t->type.t) ? 'u' : 's',
528 t->c);
529 while (t->next) {
530 int pos, size, align;
532 t = t->next;
533 cstr_printf (&str, "%s:",
534 (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
535 ? "" : get_tok_str(t->v & ~SYM_FIELD, NULL));
536 tcc_get_debug_info (s1, t, &str);
537 if (t->type.t & VT_BITFIELD) {
538 pos = t->c * 8 + BIT_POS(t->type.t);
539 size = BIT_SIZE(t->type.t);
541 else {
542 pos = t->c * 8;
543 size = type_size(&t->type, &align) * 8;
545 cstr_printf (&str, ",%d,%d;", pos, size);
547 cstr_printf (&str, ";");
548 tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0);
549 cstr_free (&str);
552 else if (IS_ENUM(type)) {
553 Sym *e = t = t->type.ref;
555 debug_type = ++debug_next_type;
556 cstr_new (&str);
557 cstr_printf (&str, "%s:T%d=e",
558 (t->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM
559 ? "" : get_tok_str(t->v & ~SYM_STRUCT, NULL),
560 debug_type);
561 while (t->next) {
562 t = t->next;
563 cstr_printf (&str, "%s:",
564 (t->v & ~SYM_FIELD) >= SYM_FIRST_ANOM
565 ? "" : get_tok_str(t->v & ~SYM_FIELD, NULL));
566 cstr_printf (&str, e->type.t & VT_UNSIGNED ? "%u," : "%d,",
567 (int)t->enum_val);
569 cstr_printf (&str, ";");
570 tcc_debug_stabs(s1, str.data, N_LSYM, 0, NULL, 0);
571 cstr_free (&str);
573 else if ((type & VT_BTYPE) != VT_FUNC) {
574 type &= ~VT_STRUCT_MASK;
575 for (debug_type = 1;
576 debug_type <= sizeof(default_debug) / sizeof(default_debug[0]);
577 debug_type++)
578 if (default_debug[debug_type - 1].type == type)
579 break;
580 if (debug_type > sizeof(default_debug) / sizeof(default_debug[0]))
581 return;
583 if (n > 0)
584 cstr_printf (result, "%d=", ++debug_next_type);
585 t = s;
586 for (;;) {
587 type = t->type.t & ~(VT_EXTERN | VT_STATIC | VT_CONSTANT | VT_VOLATILE);
588 if ((type & VT_BTYPE) != VT_BYTE)
589 type &= ~VT_DEFSIGN;
590 if (type == VT_PTR)
591 cstr_printf (result, "%d=*", ++debug_next_type);
592 else if (type == (VT_PTR | VT_ARRAY))
593 cstr_printf (result, "%d=ar1;0;%d;",
594 ++debug_next_type, t->type.ref->c - 1);
595 else if (type == VT_FUNC) {
596 cstr_printf (result, "%d=f", ++debug_next_type);
597 tcc_get_debug_info (s1, t->type.ref, result);
598 return;
600 else
601 break;
602 t = t->type.ref;
604 cstr_printf (result, "%d", debug_type);
607 static void tcc_debug_finish (TCCState *s1, struct debug_info *cur)
609 while (cur) {
610 int i;
611 struct debug_info *next = cur->next;
613 for (i = 0; i < cur->n_sym; i++) {
614 struct debug_sym *s = &cur->sym[i];
616 if (s->sec)
617 put_stabs_r(s1, s->str, s->type, 0, 0, s->value,
618 s->sec, s->sym_index);
619 else
620 put_stabs(s1, s->str, s->type, 0, 0, s->value);
621 tcc_free (s->str);
623 tcc_free (cur->sym);
624 put_stabn(s1, N_LBRAC, 0, 0, cur->start);
625 tcc_debug_finish (s1, cur->child);
626 put_stabn(s1, N_RBRAC, 0, 0, cur->end);
627 tcc_free (cur);
628 cur = next;
632 static void tcc_add_debug_info(TCCState *s1, int param, Sym *s, Sym *e)
634 CString debug_str;
635 cstr_new (&debug_str);
636 for (; s != e; s = s->prev) {
637 if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
638 continue;
639 cstr_reset (&debug_str);
640 cstr_printf (&debug_str, "%s:%s", get_tok_str(s->v, NULL), param ? "p" : "");
641 tcc_get_debug_info(s1, s, &debug_str);
642 tcc_debug_stabs(s1, debug_str.data, param ? N_PSYM : N_LSYM, s->c, NULL, 0);
644 cstr_free (&debug_str);
647 static void tcc_debug_extern_sym(TCCState *s1, Sym *sym, int sh_num, int sym_bind)
649 Section *s = s1->sections[sh_num];
650 CString str;
652 cstr_new (&str);
653 cstr_printf (&str, "%s:%c",
654 get_tok_str(sym->v, NULL),
655 sym_bind == STB_GLOBAL ? 'G' : local_scope ? 'V' : 'S'
657 tcc_get_debug_info(s1, sym, &str);
658 if (sym_bind == STB_GLOBAL)
659 tcc_debug_stabs(s1, str.data, N_GSYM, 0, NULL, 0);
660 else
661 tcc_debug_stabs(s1, str.data,
662 (sym->type.t & VT_STATIC) && data_section == s
663 ? N_STSYM : N_LCSYM, 0, s, sym->c);
664 cstr_free (&str);
667 /* put end of translation unit info */
668 ST_FUNC void tcc_debug_end(TCCState *s1)
670 if (!s1->do_debug)
671 return;
672 put_stabs_r(s1, NULL, N_SO, 0, 0,
673 text_section->data_offset, text_section, section_sym);
674 tcc_free(debug_hash);
677 static BufferedFile* put_new_file(TCCState *s1)
679 BufferedFile *f = file;
680 /* use upper file if from inline ":asm:" */
681 if (f->filename[0] == ':')
682 f = f->prev;
683 if (f && new_file) {
684 put_stabs_r(s1, f->filename, N_SOL, 0, 0, ind, text_section, section_sym);
685 new_file = last_line_num = 0;
687 return f;
690 /* generate line number info */
691 ST_FUNC void tcc_debug_line(TCCState *s1)
693 BufferedFile *f;
694 if (!s1->do_debug
695 || cur_text_section != text_section
696 || !(f = put_new_file(s1))
697 || last_line_num == f->line_num)
698 return;
699 if (func_ind != -1) {
700 put_stabn(s1, N_SLINE, 0, f->line_num, ind - func_ind);
701 } else {
702 /* from tcc_assemble */
703 put_stabs_r(s1, NULL, N_SLINE, 0, f->line_num, ind, text_section, section_sym);
705 last_line_num = f->line_num;
708 /* put function symbol */
709 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
711 CString debug_str;
712 BufferedFile *f;
713 if (!s1->do_debug)
714 return;
715 debug_info_root = NULL;
716 debug_info = NULL;
717 tcc_debug_stabn(N_LBRAC, ind - func_ind);
718 if (!(f = put_new_file(s1)))
719 return;
720 cstr_new (&debug_str);
721 cstr_printf(&debug_str, "%s:%c", funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
722 tcc_get_debug_info(s1, sym->type.ref, &debug_str);
723 put_stabs_r(s1, debug_str.data, N_FUN, 0, f->line_num, 0, cur_text_section, sym->c);
724 cstr_free (&debug_str);
726 tcc_debug_line(s1);
729 /* put function size */
730 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
732 if (!s1->do_debug)
733 return;
734 tcc_debug_stabn(N_RBRAC, size);
735 tcc_debug_finish (s1, debug_info_root);
738 /* put alternative filename */
739 ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename)
741 if (0 == strcmp(file->filename, filename))
742 return;
743 pstrcpy(file->filename, sizeof(file->filename), filename);
744 new_file = 1;
747 /* begin of #include */
748 ST_FUNC void tcc_debug_bincl(TCCState *s1)
750 if (!s1->do_debug)
751 return;
752 put_stabs(s1, file->filename, N_BINCL, 0, 0, 0);
753 new_file = 1;
756 /* end of #include */
757 ST_FUNC void tcc_debug_eincl(TCCState *s1)
759 if (!s1->do_debug)
760 return;
761 put_stabn(s1, N_EINCL, 0, 0, 0);
762 new_file = 1;
765 /* ------------------------------------------------------------------------- */
766 /* initialize vstack and types. This must be done also for tcc -E */
767 ST_FUNC void tccgen_init(TCCState *s1)
769 vtop = vstack - 1;
770 memset(vtop, 0, sizeof *vtop);
772 /* define some often used types */
773 int_type.t = VT_INT;
775 char_type.t = VT_BYTE;
776 if (s1->char_is_unsigned)
777 char_type.t |= VT_UNSIGNED;
778 char_pointer_type = char_type;
779 mk_pointer(&char_pointer_type);
781 func_old_type.t = VT_FUNC;
782 func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
783 func_old_type.ref->f.func_call = FUNC_CDECL;
784 func_old_type.ref->f.func_type = FUNC_OLD;
785 #ifdef precedence_parser
786 init_prec();
787 #endif
788 cstr_new(&initstr);
791 ST_FUNC int tccgen_compile(TCCState *s1)
793 cur_text_section = NULL;
794 funcname = "";
795 anon_sym = SYM_FIRST_ANOM;
796 section_sym = 0;
797 const_wanted = 0;
798 nocode_wanted = 0x80000000;
799 local_scope = 0;
801 tcc_debug_start(s1);
802 #ifdef TCC_TARGET_ARM
803 arm_init(s1);
804 #endif
805 #ifdef INC_DEBUG
806 printf("%s: **** new file\n", file->filename);
807 #endif
808 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
809 next();
810 decl(VT_CONST);
811 gen_inline_functions(s1);
812 check_vstack();
813 /* end of translation unit info */
814 tcc_debug_end(s1);
815 return 0;
818 ST_FUNC void tccgen_finish(TCCState *s1)
820 cstr_free(&initstr);
821 free_inline_functions(s1);
822 sym_pop(&global_stack, NULL, 0);
823 sym_pop(&local_stack, NULL, 0);
824 /* free preprocessor macros */
825 free_defines(NULL);
826 /* free sym_pools */
827 dynarray_reset(&sym_pools, &nb_sym_pools);
828 sym_free_first = NULL;
831 /* ------------------------------------------------------------------------- */
832 ST_FUNC ElfSym *elfsym(Sym *s)
834 if (!s || !s->c)
835 return NULL;
836 return &((ElfSym *)symtab_section->data)[s->c];
839 /* apply storage attributes to Elf symbol */
840 ST_FUNC void update_storage(Sym *sym)
842 ElfSym *esym;
843 int sym_bind, old_sym_bind;
845 esym = elfsym(sym);
846 if (!esym)
847 return;
849 if (sym->a.visibility)
850 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
851 | sym->a.visibility;
853 if (sym->type.t & (VT_STATIC | VT_INLINE))
854 sym_bind = STB_LOCAL;
855 else if (sym->a.weak)
856 sym_bind = STB_WEAK;
857 else
858 sym_bind = STB_GLOBAL;
859 old_sym_bind = ELFW(ST_BIND)(esym->st_info);
860 if (sym_bind != old_sym_bind) {
861 esym->st_info = ELFW(ST_INFO)(sym_bind, ELFW(ST_TYPE)(esym->st_info));
864 #ifdef TCC_TARGET_PE
865 if (sym->a.dllimport)
866 esym->st_other |= ST_PE_IMPORT;
867 if (sym->a.dllexport)
868 esym->st_other |= ST_PE_EXPORT;
869 #endif
871 #if 0
872 printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
873 get_tok_str(sym->v, NULL),
874 sym_bind == STB_WEAK ? 'w' : sym_bind == STB_LOCAL ? 'l' : 'g',
875 sym->a.visibility,
876 sym->a.dllexport,
877 sym->a.dllimport
879 #endif
882 /* ------------------------------------------------------------------------- */
883 /* update sym->c so that it points to an external symbol in section
884 'section' with value 'value' */
886 ST_FUNC void put_extern_sym2(Sym *sym, int sh_num,
887 addr_t value, unsigned long size,
888 int can_add_underscore)
890 int sym_type, sym_bind, info, other, t;
891 ElfSym *esym;
892 const char *name;
893 char buf1[256];
895 if (!sym->c) {
896 name = get_tok_str(sym->v, NULL);
897 t = sym->type.t;
898 if ((t & VT_BTYPE) == VT_FUNC) {
899 sym_type = STT_FUNC;
900 } else if ((t & VT_BTYPE) == VT_VOID) {
901 sym_type = STT_NOTYPE;
902 } else {
903 sym_type = STT_OBJECT;
905 if (t & (VT_STATIC | VT_INLINE))
906 sym_bind = STB_LOCAL;
907 else
908 sym_bind = STB_GLOBAL;
909 other = 0;
911 #ifdef TCC_TARGET_PE
912 if (sym_type == STT_FUNC && sym->type.ref) {
913 Sym *ref = sym->type.ref;
914 if (ref->a.nodecorate) {
915 can_add_underscore = 0;
917 if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) {
918 sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE);
919 name = buf1;
920 other |= ST_PE_STDCALL;
921 can_add_underscore = 0;
924 #endif
926 if (sym->asm_label) {
927 name = get_tok_str(sym->asm_label & ~SYM_FIELD, NULL);
928 /* with SYM_FIELD it was __attribute__((alias("..."))) actually */
929 if (!(sym->asm_label & SYM_FIELD))
930 can_add_underscore = 0;
933 if (tcc_state->leading_underscore && can_add_underscore) {
934 buf1[0] = '_';
935 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
936 name = buf1;
939 info = ELFW(ST_INFO)(sym_bind, sym_type);
940 sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
942 if (tcc_state->do_debug
943 && sym_type != STT_FUNC
944 && sym->v < SYM_FIRST_ANOM)
945 tcc_debug_extern_sym(tcc_state, sym, sh_num, sym_bind);
947 } else {
948 esym = elfsym(sym);
949 esym->st_value = value;
950 esym->st_size = size;
951 esym->st_shndx = sh_num;
953 update_storage(sym);
956 ST_FUNC void put_extern_sym(Sym *sym, Section *section,
957 addr_t value, unsigned long size)
959 int sh_num = section ? section->sh_num : SHN_UNDEF;
960 put_extern_sym2(sym, sh_num, value, size, 1);
963 /* add a new relocation entry to symbol 'sym' in section 's' */
964 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
965 addr_t addend)
967 int c = 0;
969 if (nocode_wanted && s == cur_text_section)
970 return;
972 if (sym) {
973 if (0 == sym->c)
974 put_extern_sym(sym, NULL, 0, 0);
975 c = sym->c;
978 /* now we can add ELF relocation info */
979 put_elf_reloca(symtab_section, s, offset, type, c, addend);
982 #if PTR_SIZE == 4
983 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
985 greloca(s, sym, offset, type, 0);
987 #endif
989 /* ------------------------------------------------------------------------- */
990 /* symbol allocator */
991 static Sym *__sym_malloc(void)
993 Sym *sym_pool, *sym, *last_sym;
994 int i;
996 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
997 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
999 last_sym = sym_free_first;
1000 sym = sym_pool;
1001 for(i = 0; i < SYM_POOL_NB; i++) {
1002 sym->next = last_sym;
1003 last_sym = sym;
1004 sym++;
1006 sym_free_first = last_sym;
1007 return last_sym;
1010 static inline Sym *sym_malloc(void)
1012 Sym *sym;
1013 #ifndef SYM_DEBUG
1014 sym = sym_free_first;
1015 if (!sym)
1016 sym = __sym_malloc();
1017 sym_free_first = sym->next;
1018 return sym;
1019 #else
1020 sym = tcc_malloc(sizeof(Sym));
1021 return sym;
1022 #endif
1025 ST_INLN void sym_free(Sym *sym)
1027 #ifndef SYM_DEBUG
1028 sym->next = sym_free_first;
1029 sym_free_first = sym;
1030 #else
1031 tcc_free(sym);
1032 #endif
1035 /* push, without hashing */
1036 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c)
1038 Sym *s;
1040 s = sym_malloc();
1041 memset(s, 0, sizeof *s);
1042 s->v = v;
1043 s->type.t = t;
1044 s->c = c;
1045 /* add in stack */
1046 s->prev = *ps;
1047 *ps = s;
1048 return s;
1051 /* find a symbol and return its associated structure. 's' is the top
1052 of the symbol stack */
1053 ST_FUNC Sym *sym_find2(Sym *s, int v)
1055 while (s) {
1056 if (s->v == v)
1057 return s;
1058 else if (s->v == -1)
1059 return NULL;
1060 s = s->prev;
1062 return NULL;
1065 /* structure lookup */
1066 ST_INLN Sym *struct_find(int v)
1068 v -= TOK_IDENT;
1069 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1070 return NULL;
1071 return table_ident[v]->sym_struct;
1074 /* find an identifier */
1075 ST_INLN Sym *sym_find(int v)
1077 v -= TOK_IDENT;
1078 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1079 return NULL;
1080 return table_ident[v]->sym_identifier;
1083 static int sym_scope(Sym *s)
1085 if (IS_ENUM_VAL (s->type.t))
1086 return s->type.ref->sym_scope;
1087 else
1088 return s->sym_scope;
1091 /* push a given symbol on the symbol stack */
1092 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
1094 Sym *s, **ps;
1095 TokenSym *ts;
1097 if (local_stack)
1098 ps = &local_stack;
1099 else
1100 ps = &global_stack;
1101 s = sym_push2(ps, v, type->t, c);
1102 s->type.ref = type->ref;
1103 s->r = r;
1104 /* don't record fields or anonymous symbols */
1105 /* XXX: simplify */
1106 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1107 /* record symbol in token array */
1108 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1109 if (v & SYM_STRUCT)
1110 ps = &ts->sym_struct;
1111 else
1112 ps = &ts->sym_identifier;
1113 s->prev_tok = *ps;
1114 *ps = s;
1115 s->sym_scope = local_scope;
1116 if (s->prev_tok && sym_scope(s->prev_tok) == s->sym_scope)
1117 tcc_error("redeclaration of '%s'",
1118 get_tok_str(v & ~SYM_STRUCT, NULL));
1120 return s;
1123 /* push a global identifier */
1124 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
1126 Sym *s, **ps;
1127 s = sym_push2(&global_stack, v, t, c);
1128 s->r = VT_CONST | VT_SYM;
1129 /* don't record anonymous symbol */
1130 if (v < SYM_FIRST_ANOM) {
1131 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1132 /* modify the top most local identifier, so that sym_identifier will
1133 point to 's' when popped; happens when called from inline asm */
1134 while (*ps != NULL && (*ps)->sym_scope)
1135 ps = &(*ps)->prev_tok;
1136 s->prev_tok = *ps;
1137 *ps = s;
1139 return s;
1142 /* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
1143 pop them yet from the list, but do remove them from the token array. */
1144 ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep)
1146 Sym *s, *ss, **ps;
1147 TokenSym *ts;
1148 int v;
1150 s = *ptop;
1151 while(s != b) {
1152 ss = s->prev;
1153 v = s->v;
1154 /* remove symbol in token array */
1155 /* XXX: simplify */
1156 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1157 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1158 if (v & SYM_STRUCT)
1159 ps = &ts->sym_struct;
1160 else
1161 ps = &ts->sym_identifier;
1162 *ps = s->prev_tok;
1164 if (!keep)
1165 sym_free(s);
1166 s = ss;
1168 if (!keep)
1169 *ptop = b;
1172 /* ------------------------------------------------------------------------- */
1173 static void vcheck_cmp(void)
1175 /* cannot let cpu flags if other instruction are generated. Also
1176 avoid leaving VT_JMP anywhere except on the top of the stack
1177 because it would complicate the code generator.
1179 Don't do this when nocode_wanted. vtop might come from
1180 !nocode_wanted regions (see 88_codeopt.c) and transforming
1181 it to a register without actually generating code is wrong
1182 as their value might still be used for real. All values
1183 we push under nocode_wanted will eventually be popped
1184 again, so that the VT_CMP/VT_JMP value will be in vtop
1185 when code is unsuppressed again. */
1187 if (vtop->r == VT_CMP && !nocode_wanted)
1188 gv(RC_INT);
1191 static void vsetc(CType *type, int r, CValue *vc)
1193 if (vtop >= vstack + (VSTACK_SIZE - 1))
1194 tcc_error("memory full (vstack)");
1195 vcheck_cmp();
1196 vtop++;
1197 vtop->type = *type;
1198 vtop->r = r;
1199 vtop->r2 = VT_CONST;
1200 vtop->c = *vc;
1201 vtop->sym = NULL;
1204 ST_FUNC void vswap(void)
1206 SValue tmp;
1208 vcheck_cmp();
1209 tmp = vtop[0];
1210 vtop[0] = vtop[-1];
1211 vtop[-1] = tmp;
1214 /* pop stack value */
1215 ST_FUNC void vpop(void)
1217 int v;
1218 v = vtop->r & VT_VALMASK;
1219 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1220 /* for x86, we need to pop the FP stack */
1221 if (v == TREG_ST0) {
1222 o(0xd8dd); /* fstp %st(0) */
1223 } else
1224 #endif
1225 if (v == VT_CMP) {
1226 /* need to put correct jump if && or || without test */
1227 gsym(vtop->jtrue);
1228 gsym(vtop->jfalse);
1230 vtop--;
1233 /* push constant of type "type" with useless value */
1234 static void vpush(CType *type)
1236 vset(type, VT_CONST, 0);
1239 /* push arbitrary 64bit constant */
1240 static void vpush64(int ty, unsigned long long v)
1242 CValue cval;
1243 CType ctype;
1244 ctype.t = ty;
1245 ctype.ref = NULL;
1246 cval.i = v;
1247 vsetc(&ctype, VT_CONST, &cval);
1250 /* push integer constant */
1251 ST_FUNC void vpushi(int v)
1253 vpush64(VT_INT, v);
1256 /* push a pointer sized constant */
1257 static void vpushs(addr_t v)
1259 vpush64(VT_SIZE_T, v);
1262 /* push long long constant */
1263 static inline void vpushll(long long v)
1265 vpush64(VT_LLONG, v);
1268 ST_FUNC void vset(CType *type, int r, int v)
1270 CValue cval;
1271 cval.i = v;
1272 vsetc(type, r, &cval);
1275 static void vseti(int r, int v)
1277 CType type;
1278 type.t = VT_INT;
1279 type.ref = NULL;
1280 vset(&type, r, v);
1283 ST_FUNC void vpushv(SValue *v)
1285 if (vtop >= vstack + (VSTACK_SIZE - 1))
1286 tcc_error("memory full (vstack)");
1287 vtop++;
1288 *vtop = *v;
1291 static void vdup(void)
1293 vpushv(vtop);
1296 /* rotate n first stack elements to the bottom
1297 I1 ... In -> I2 ... In I1 [top is right]
1299 ST_FUNC void vrotb(int n)
1301 int i;
1302 SValue tmp;
1304 vcheck_cmp();
1305 tmp = vtop[-n + 1];
1306 for(i=-n+1;i!=0;i++)
1307 vtop[i] = vtop[i+1];
1308 vtop[0] = tmp;
1311 /* rotate the n elements before entry e towards the top
1312 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1314 ST_FUNC void vrote(SValue *e, int n)
1316 int i;
1317 SValue tmp;
1319 vcheck_cmp();
1320 tmp = *e;
1321 for(i = 0;i < n - 1; i++)
1322 e[-i] = e[-i - 1];
1323 e[-n + 1] = tmp;
1326 /* rotate n first stack elements to the top
1327 I1 ... In -> In I1 ... I(n-1) [top is right]
1329 ST_FUNC void vrott(int n)
1331 vrote(vtop, n);
1334 /* ------------------------------------------------------------------------- */
1335 /* vtop->r = VT_CMP means CPU-flags have been set from comparison or test. */
1337 /* called from generators to set the result from relational ops */
1338 ST_FUNC void vset_VT_CMP(int op)
1340 vtop->r = VT_CMP;
1341 vtop->cmp_op = op;
1342 vtop->jfalse = 0;
1343 vtop->jtrue = 0;
1346 /* called once before asking generators to load VT_CMP to a register */
1347 static void vset_VT_JMP(void)
1349 int op = vtop->cmp_op;
1351 if (vtop->jtrue || vtop->jfalse) {
1352 /* we need to jump to 'mov $0,%R' or 'mov $1,%R' */
1353 int inv = op & (op < 2); /* small optimization */
1354 vseti(VT_JMP+inv, gvtst(inv, 0));
1355 } else {
1356 /* otherwise convert flags (rsp. 0/1) to register */
1357 vtop->c.i = op;
1358 if (op < 2) /* doesn't seem to happen */
1359 vtop->r = VT_CONST;
1363 /* Set CPU Flags, doesn't yet jump */
1364 static void gvtst_set(int inv, int t)
1366 int *p;
1368 if (vtop->r != VT_CMP) {
1369 vpushi(0);
1370 gen_op(TOK_NE);
1371 if (vtop->r != VT_CMP) /* must be VT_CONST then */
1372 vset_VT_CMP(vtop->c.i != 0);
1375 p = inv ? &vtop->jfalse : &vtop->jtrue;
1376 *p = gjmp_append(*p, t);
1379 /* Generate value test
1381 * Generate a test for any value (jump, comparison and integers) */
1382 static int gvtst(int inv, int t)
1384 int op, x, u;
1386 gvtst_set(inv, t);
1387 t = vtop->jtrue, u = vtop->jfalse;
1388 if (inv)
1389 x = u, u = t, t = x;
1390 op = vtop->cmp_op;
1392 /* jump to the wanted target */
1393 if (op > 1)
1394 t = gjmp_cond(op ^ inv, t);
1395 else if (op != inv)
1396 t = gjmp(t);
1397 /* resolve complementary jumps to here */
1398 gsym(u);
1400 vtop--;
1401 return t;
1404 /* generate a zero or nozero test */
1405 static void gen_test_zero(int op)
1407 if (vtop->r == VT_CMP) {
1408 int j;
1409 if (op == TOK_EQ) {
1410 j = vtop->jfalse;
1411 vtop->jfalse = vtop->jtrue;
1412 vtop->jtrue = j;
1413 vtop->cmp_op ^= 1;
1415 } else {
1416 vpushi(0);
1417 gen_op(op);
1421 /* ------------------------------------------------------------------------- */
1422 /* push a symbol value of TYPE */
1423 static inline void vpushsym(CType *type, Sym *sym)
1425 CValue cval;
1426 cval.i = 0;
1427 vsetc(type, VT_CONST | VT_SYM, &cval);
1428 vtop->sym = sym;
1431 /* Return a static symbol pointing to a section */
1432 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
1434 int v;
1435 Sym *sym;
1437 v = anon_sym++;
1438 sym = sym_push(v, type, VT_CONST | VT_SYM, 0);
1439 sym->type.t |= VT_STATIC;
1440 put_extern_sym(sym, sec, offset, size);
1441 return sym;
1444 /* push a reference to a section offset by adding a dummy symbol */
1445 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
1447 vpushsym(type, get_sym_ref(type, sec, offset, size));
1450 /* define a new external reference to a symbol 'v' of type 'u' */
1451 ST_FUNC Sym *external_global_sym(int v, CType *type)
1453 Sym *s;
1455 s = sym_find(v);
1456 if (!s) {
1457 /* push forward reference */
1458 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
1459 s->type.ref = type->ref;
1460 } else if (IS_ASM_SYM(s)) {
1461 s->type.t = type->t | (s->type.t & VT_EXTERN);
1462 s->type.ref = type->ref;
1463 update_storage(s);
1465 return s;
1468 /* Merge symbol attributes. */
1469 static void merge_symattr(struct SymAttr *sa, struct SymAttr *sa1)
1471 if (sa1->aligned && !sa->aligned)
1472 sa->aligned = sa1->aligned;
1473 sa->packed |= sa1->packed;
1474 sa->weak |= sa1->weak;
1475 if (sa1->visibility != STV_DEFAULT) {
1476 int vis = sa->visibility;
1477 if (vis == STV_DEFAULT
1478 || vis > sa1->visibility)
1479 vis = sa1->visibility;
1480 sa->visibility = vis;
1482 sa->dllexport |= sa1->dllexport;
1483 sa->nodecorate |= sa1->nodecorate;
1484 sa->dllimport |= sa1->dllimport;
1487 /* Merge function attributes. */
1488 static void merge_funcattr(struct FuncAttr *fa, struct FuncAttr *fa1)
1490 if (fa1->func_call && !fa->func_call)
1491 fa->func_call = fa1->func_call;
1492 if (fa1->func_type && !fa->func_type)
1493 fa->func_type = fa1->func_type;
1494 if (fa1->func_args && !fa->func_args)
1495 fa->func_args = fa1->func_args;
1496 if (fa1->func_noreturn)
1497 fa->func_noreturn = 1;
1498 if (fa1->func_ctor)
1499 fa->func_ctor = 1;
1500 if (fa1->func_dtor)
1501 fa->func_dtor = 1;
1504 /* Merge attributes. */
1505 static void merge_attr(AttributeDef *ad, AttributeDef *ad1)
1507 merge_symattr(&ad->a, &ad1->a);
1508 merge_funcattr(&ad->f, &ad1->f);
1510 if (ad1->section)
1511 ad->section = ad1->section;
1512 if (ad1->asm_label)
1513 ad->asm_label = ad1->asm_label;
1514 if (ad1->attr_mode)
1515 ad->attr_mode = ad1->attr_mode;
1518 /* Merge some type attributes. */
1519 static void patch_type(Sym *sym, CType *type)
1521 if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) {
1522 if (!(sym->type.t & VT_EXTERN))
1523 tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
1524 sym->type.t &= ~VT_EXTERN;
1527 if (IS_ASM_SYM(sym)) {
1528 /* stay static if both are static */
1529 sym->type.t = type->t & (sym->type.t | ~VT_STATIC);
1530 sym->type.ref = type->ref;
1533 if (!is_compatible_types(&sym->type, type)) {
1534 tcc_error("incompatible types for redefinition of '%s'",
1535 get_tok_str(sym->v, NULL));
1537 } else if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1538 int static_proto = sym->type.t & VT_STATIC;
1539 /* warn if static follows non-static function declaration */
1540 if ((type->t & VT_STATIC) && !static_proto
1541 /* XXX this test for inline shouldn't be here. Until we
1542 implement gnu-inline mode again it silences a warning for
1543 mingw caused by our workarounds. */
1544 && !((type->t | sym->type.t) & VT_INLINE))
1545 tcc_warning("static storage ignored for redefinition of '%s'",
1546 get_tok_str(sym->v, NULL));
1548 /* set 'inline' if both agree or if one has static */
1549 if ((type->t | sym->type.t) & VT_INLINE) {
1550 if (!((type->t ^ sym->type.t) & VT_INLINE)
1551 || ((type->t | sym->type.t) & VT_STATIC))
1552 static_proto |= VT_INLINE;
1555 if (0 == (type->t & VT_EXTERN)) {
1556 struct FuncAttr f = sym->type.ref->f;
1557 /* put complete type, use static from prototype */
1558 sym->type.t = (type->t & ~(VT_STATIC|VT_INLINE)) | static_proto;
1559 sym->type.ref = type->ref;
1560 merge_funcattr(&sym->type.ref->f, &f);
1561 } else {
1562 sym->type.t &= ~VT_INLINE | static_proto;
1565 if (sym->type.ref->f.func_type == FUNC_OLD
1566 && type->ref->f.func_type != FUNC_OLD) {
1567 sym->type.ref = type->ref;
1570 } else {
1571 if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) {
1572 /* set array size if it was omitted in extern declaration */
1573 sym->type.ref->c = type->ref->c;
1575 if ((type->t ^ sym->type.t) & VT_STATIC)
1576 tcc_warning("storage mismatch for redefinition of '%s'",
1577 get_tok_str(sym->v, NULL));
1581 /* Merge some storage attributes. */
1582 static void patch_storage(Sym *sym, AttributeDef *ad, CType *type)
1584 if (type)
1585 patch_type(sym, type);
1587 #ifdef TCC_TARGET_PE
1588 if (sym->a.dllimport != ad->a.dllimport)
1589 tcc_error("incompatible dll linkage for redefinition of '%s'",
1590 get_tok_str(sym->v, NULL));
1591 #endif
1592 merge_symattr(&sym->a, &ad->a);
1593 if (ad->asm_label)
1594 sym->asm_label = ad->asm_label;
1595 update_storage(sym);
1598 /* copy sym to other stack */
1599 static Sym *sym_copy(Sym *s0, Sym **ps)
1601 Sym *s;
1602 s = sym_malloc(), *s = *s0;
1603 s->prev = *ps, *ps = s;
1604 if (s->v < SYM_FIRST_ANOM) {
1605 ps = &table_ident[s->v - TOK_IDENT]->sym_identifier;
1606 s->prev_tok = *ps, *ps = s;
1608 return s;
1611 /* copy s->type.ref to stack 'ps' for VT_FUNC and VT_PTR */
1612 static void sym_copy_ref(Sym *s, Sym **ps)
1614 int bt = s->type.t & VT_BTYPE;
1615 if (bt == VT_FUNC || bt == VT_PTR) {
1616 Sym **sp = &s->type.ref;
1617 for (s = *sp, *sp = NULL; s; s = s->next) {
1618 Sym *s2 = sym_copy(s, ps);
1619 sp = &(*sp = s2)->next;
1620 sym_copy_ref(s2, ps);
1625 /* define a new external reference to a symbol 'v' */
1626 static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad)
1628 Sym *s;
1630 /* look for global symbol */
1631 s = sym_find(v);
1632 while (s && s->sym_scope)
1633 s = s->prev_tok;
1635 if (!s) {
1636 /* push forward reference */
1637 s = global_identifier_push(v, type->t, 0);
1638 s->r |= r;
1639 s->a = ad->a;
1640 s->asm_label = ad->asm_label;
1641 s->type.ref = type->ref;
1642 /* copy type to the global stack */
1643 if (local_stack)
1644 sym_copy_ref(s, &global_stack);
1645 } else {
1646 patch_storage(s, ad, type);
1648 /* push variables on local_stack if any */
1649 if (local_stack && (s->type.t & VT_BTYPE) != VT_FUNC)
1650 s = sym_copy(s, &local_stack);
1651 return s;
1654 /* push a reference to global symbol v */
1655 ST_FUNC void vpush_global_sym(CType *type, int v)
1657 vpushsym(type, external_global_sym(v, type));
1660 /* save registers up to (vtop - n) stack entry */
1661 ST_FUNC void save_regs(int n)
1663 SValue *p, *p1;
1664 for(p = vstack, p1 = vtop - n; p <= p1; p++)
1665 save_reg(p->r);
1668 /* save r to the memory stack, and mark it as being free */
1669 ST_FUNC void save_reg(int r)
1671 save_reg_upstack(r, 0);
1674 /* save r to the memory stack, and mark it as being free,
1675 if seen up to (vtop - n) stack entry */
1676 ST_FUNC void save_reg_upstack(int r, int n)
1678 int l, size, align, bt;
1679 SValue *p, *p1, sv;
1681 if ((r &= VT_VALMASK) >= VT_CONST)
1682 return;
1683 if (nocode_wanted)
1684 return;
1685 l = 0;
1686 for(p = vstack, p1 = vtop - n; p <= p1; p++) {
1687 if ((p->r & VT_VALMASK) == r || p->r2 == r) {
1688 /* must save value on stack if not already done */
1689 if (!l) {
1690 bt = p->type.t & VT_BTYPE;
1691 if (bt == VT_VOID)
1692 continue;
1693 if ((p->r & VT_LVAL) || bt == VT_FUNC)
1694 bt = VT_PTR;
1695 sv.type.t = bt;
1696 size = type_size(&sv.type, &align);
1697 l = get_temp_local_var(size,align);
1698 sv.r = VT_LOCAL | VT_LVAL;
1699 sv.c.i = l;
1700 store(p->r & VT_VALMASK, &sv);
1701 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1702 /* x86 specific: need to pop fp register ST0 if saved */
1703 if (r == TREG_ST0) {
1704 o(0xd8dd); /* fstp %st(0) */
1706 #endif
1707 /* special long long case */
1708 if (p->r2 < VT_CONST && USING_TWO_WORDS(bt)) {
1709 sv.c.i += PTR_SIZE;
1710 store(p->r2, &sv);
1713 /* mark that stack entry as being saved on the stack */
1714 if (p->r & VT_LVAL) {
1715 /* also clear the bounded flag because the
1716 relocation address of the function was stored in
1717 p->c.i */
1718 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
1719 } else {
1720 p->r = VT_LVAL | VT_LOCAL;
1722 p->r2 = VT_CONST;
1723 p->c.i = l;
1728 #ifdef TCC_TARGET_ARM
1729 /* find a register of class 'rc2' with at most one reference on stack.
1730 * If none, call get_reg(rc) */
1731 ST_FUNC int get_reg_ex(int rc, int rc2)
1733 int r;
1734 SValue *p;
1736 for(r=0;r<NB_REGS;r++) {
1737 if (reg_classes[r] & rc2) {
1738 int n;
1739 n=0;
1740 for(p = vstack; p <= vtop; p++) {
1741 if ((p->r & VT_VALMASK) == r ||
1742 p->r2 == r)
1743 n++;
1745 if (n <= 1)
1746 return r;
1749 return get_reg(rc);
1751 #endif
1753 /* find a free register of class 'rc'. If none, save one register */
1754 ST_FUNC int get_reg(int rc)
1756 int r;
1757 SValue *p;
1759 /* find a free register */
1760 for(r=0;r<NB_REGS;r++) {
1761 if (reg_classes[r] & rc) {
1762 if (nocode_wanted)
1763 return r;
1764 for(p=vstack;p<=vtop;p++) {
1765 if ((p->r & VT_VALMASK) == r ||
1766 p->r2 == r)
1767 goto notfound;
1769 return r;
1771 notfound: ;
1774 /* no register left : free the first one on the stack (VERY
1775 IMPORTANT to start from the bottom to ensure that we don't
1776 spill registers used in gen_opi()) */
1777 for(p=vstack;p<=vtop;p++) {
1778 /* look at second register (if long long) */
1779 r = p->r2;
1780 if (r < VT_CONST && (reg_classes[r] & rc))
1781 goto save_found;
1782 r = p->r & VT_VALMASK;
1783 if (r < VT_CONST && (reg_classes[r] & rc)) {
1784 save_found:
1785 save_reg(r);
1786 return r;
1789 /* Should never comes here */
1790 return -1;
1793 /* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
1794 static int get_temp_local_var(int size,int align){
1795 int i;
1796 struct temp_local_variable *temp_var;
1797 int found_var;
1798 SValue *p;
1799 int r;
1800 char free;
1801 char found;
1802 found=0;
1803 for(i=0;i<nb_temp_local_vars;i++){
1804 temp_var=&arr_temp_local_vars[i];
1805 if(temp_var->size<size||align!=temp_var->align){
1806 continue;
1808 /*check if temp_var is free*/
1809 free=1;
1810 for(p=vstack;p<=vtop;p++) {
1811 r=p->r&VT_VALMASK;
1812 if(r==VT_LOCAL||r==VT_LLOCAL){
1813 if(p->c.i==temp_var->location){
1814 free=0;
1815 break;
1819 if(free){
1820 found_var=temp_var->location;
1821 found=1;
1822 break;
1825 if(!found){
1826 loc = (loc - size) & -align;
1827 if(nb_temp_local_vars<MAX_TEMP_LOCAL_VARIABLE_NUMBER){
1828 temp_var=&arr_temp_local_vars[i];
1829 temp_var->location=loc;
1830 temp_var->size=size;
1831 temp_var->align=align;
1832 nb_temp_local_vars++;
1834 found_var=loc;
1836 return found_var;
1839 static void clear_temp_local_var_list(){
1840 nb_temp_local_vars=0;
1843 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
1844 if needed */
1845 static void move_reg(int r, int s, int t)
1847 SValue sv;
1849 if (r != s) {
1850 save_reg(r);
1851 sv.type.t = t;
1852 sv.type.ref = NULL;
1853 sv.r = s;
1854 sv.c.i = 0;
1855 load(r, &sv);
1859 /* get address of vtop (vtop MUST BE an lvalue) */
1860 ST_FUNC void gaddrof(void)
1862 vtop->r &= ~VT_LVAL;
1863 /* tricky: if saved lvalue, then we can go back to lvalue */
1864 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
1865 vtop->r = (vtop->r & ~VT_VALMASK) | VT_LOCAL | VT_LVAL;
1868 #ifdef CONFIG_TCC_BCHECK
1869 /* generate lvalue bound code */
1870 static void gbound(void)
1872 CType type1;
1874 vtop->r &= ~VT_MUSTBOUND;
1875 /* if lvalue, then use checking code before dereferencing */
1876 if (vtop->r & VT_LVAL) {
1877 /* if not VT_BOUNDED value, then make one */
1878 if (!(vtop->r & VT_BOUNDED)) {
1879 /* must save type because we must set it to int to get pointer */
1880 type1 = vtop->type;
1881 vtop->type.t = VT_PTR;
1882 gaddrof();
1883 vpushi(0);
1884 gen_bounded_ptr_add();
1885 vtop->r |= VT_LVAL;
1886 vtop->type = type1;
1888 /* then check for dereferencing */
1889 gen_bounded_ptr_deref();
1893 /* we need to call __bound_ptr_add before we start to load function
1894 args into registers */
1895 ST_FUNC void gbound_args(int nb_args)
1897 int i, v;
1898 SValue *sv;
1900 for (i = 1; i <= nb_args; ++i)
1901 if (vtop[1 - i].r & VT_MUSTBOUND) {
1902 vrotb(i);
1903 gbound();
1904 vrott(i);
1907 sv = vtop - nb_args;
1908 if (sv->r & VT_SYM) {
1909 v = sv->sym->v;
1910 if (v == TOK_setjmp
1911 || v == TOK__setjmp
1912 #ifndef TCC_TARGET_PE
1913 || v == TOK_sigsetjmp
1914 || v == TOK___sigsetjmp
1915 #endif
1917 vpush_global_sym(&func_old_type, TOK___bound_setjmp);
1918 vpushv(sv + 1);
1919 gfunc_call(1);
1920 func_bound_add_epilog = 1;
1922 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1923 if (v == TOK_alloca)
1924 func_bound_add_epilog = 1;
1925 #endif
1929 /* Add bounds for local symbols from S to E (via ->prev) */
1930 static void add_local_bounds(Sym *s, Sym *e)
1932 for (; s != e; s = s->prev) {
1933 if (!s->v || (s->r & VT_VALMASK) != VT_LOCAL)
1934 continue;
1935 /* Add arrays/structs/unions because we always take address */
1936 if ((s->type.t & VT_ARRAY)
1937 || (s->type.t & VT_BTYPE) == VT_STRUCT
1938 || s->a.addrtaken) {
1939 /* add local bound info */
1940 int align, size = type_size(&s->type, &align);
1941 addr_t *bounds_ptr = section_ptr_add(lbounds_section,
1942 2 * sizeof(addr_t));
1943 bounds_ptr[0] = s->c;
1944 bounds_ptr[1] = size;
1948 #endif
1950 /* Wrapper around sym_pop, that potentially also registers local bounds. */
1951 static void pop_local_syms(Sym **ptop, Sym *b, int keep, int ellipsis)
1953 #ifdef CONFIG_TCC_BCHECK
1954 if (tcc_state->do_bounds_check && !ellipsis && !keep)
1955 add_local_bounds(*ptop, b);
1956 #endif
1957 if (tcc_state->do_debug)
1958 tcc_add_debug_info (tcc_state, !local_scope, *ptop, b);
1959 sym_pop(ptop, b, keep);
1962 static void incr_bf_adr(int o)
1964 vtop->type = char_pointer_type;
1965 gaddrof();
1966 vpushs(o);
1967 gen_op('+');
1968 vtop->type.t = VT_BYTE | VT_UNSIGNED;
1969 vtop->r |= VT_LVAL;
1972 /* single-byte load mode for packed or otherwise unaligned bitfields */
1973 static void load_packed_bf(CType *type, int bit_pos, int bit_size)
1975 int n, o, bits;
1976 save_reg_upstack(vtop->r, 1);
1977 vpush64(type->t & VT_BTYPE, 0); // B X
1978 bits = 0, o = bit_pos >> 3, bit_pos &= 7;
1979 do {
1980 vswap(); // X B
1981 incr_bf_adr(o);
1982 vdup(); // X B B
1983 n = 8 - bit_pos;
1984 if (n > bit_size)
1985 n = bit_size;
1986 if (bit_pos)
1987 vpushi(bit_pos), gen_op(TOK_SHR), bit_pos = 0; // X B Y
1988 if (n < 8)
1989 vpushi((1 << n) - 1), gen_op('&');
1990 gen_cast(type);
1991 if (bits)
1992 vpushi(bits), gen_op(TOK_SHL);
1993 vrotb(3); // B Y X
1994 gen_op('|'); // B X
1995 bits += n, bit_size -= n, o = 1;
1996 } while (bit_size);
1997 vswap(), vpop();
1998 if (!(type->t & VT_UNSIGNED)) {
1999 n = ((type->t & VT_BTYPE) == VT_LLONG ? 64 : 32) - bits;
2000 vpushi(n), gen_op(TOK_SHL);
2001 vpushi(n), gen_op(TOK_SAR);
2005 /* single-byte store mode for packed or otherwise unaligned bitfields */
2006 static void store_packed_bf(int bit_pos, int bit_size)
2008 int bits, n, o, m, c;
2010 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2011 vswap(); // X B
2012 save_reg_upstack(vtop->r, 1);
2013 bits = 0, o = bit_pos >> 3, bit_pos &= 7;
2014 do {
2015 incr_bf_adr(o); // X B
2016 vswap(); //B X
2017 c ? vdup() : gv_dup(); // B V X
2018 vrott(3); // X B V
2019 if (bits)
2020 vpushi(bits), gen_op(TOK_SHR);
2021 if (bit_pos)
2022 vpushi(bit_pos), gen_op(TOK_SHL);
2023 n = 8 - bit_pos;
2024 if (n > bit_size)
2025 n = bit_size;
2026 if (n < 8) {
2027 m = ((1 << n) - 1) << bit_pos;
2028 vpushi(m), gen_op('&'); // X B V1
2029 vpushv(vtop-1); // X B V1 B
2030 vpushi(m & 0x80 ? ~m & 0x7f : ~m);
2031 gen_op('&'); // X B V1 B1
2032 gen_op('|'); // X B V2
2034 vdup(), vtop[-1] = vtop[-2]; // X B B V2
2035 vstore(), vpop(); // X B
2036 bits += n, bit_size -= n, bit_pos = 0, o = 1;
2037 } while (bit_size);
2038 vpop(), vpop();
2041 static int adjust_bf(SValue *sv, int bit_pos, int bit_size)
2043 int t;
2044 if (0 == sv->type.ref)
2045 return 0;
2046 t = sv->type.ref->auxtype;
2047 if (t != -1 && t != VT_STRUCT) {
2048 sv->type.t = (sv->type.t & ~VT_BTYPE) | t;
2049 sv->r |= VT_LVAL;
2051 return t;
2054 /* store vtop a register belonging to class 'rc'. lvalues are
2055 converted to values. Cannot be used if cannot be converted to
2056 register value (such as structures). */
2057 ST_FUNC int gv(int rc)
2059 int r, r2, r_ok, r2_ok, rc2, bt;
2060 int bit_pos, bit_size, size, align;
2062 /* NOTE: get_reg can modify vstack[] */
2063 if (vtop->type.t & VT_BITFIELD) {
2064 CType type;
2066 bit_pos = BIT_POS(vtop->type.t);
2067 bit_size = BIT_SIZE(vtop->type.t);
2068 /* remove bit field info to avoid loops */
2069 vtop->type.t &= ~VT_STRUCT_MASK;
2071 type.ref = NULL;
2072 type.t = vtop->type.t & VT_UNSIGNED;
2073 if ((vtop->type.t & VT_BTYPE) == VT_BOOL)
2074 type.t |= VT_UNSIGNED;
2076 r = adjust_bf(vtop, bit_pos, bit_size);
2078 if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
2079 type.t |= VT_LLONG;
2080 else
2081 type.t |= VT_INT;
2083 if (r == VT_STRUCT) {
2084 load_packed_bf(&type, bit_pos, bit_size);
2085 } else {
2086 int bits = (type.t & VT_BTYPE) == VT_LLONG ? 64 : 32;
2087 /* cast to int to propagate signedness in following ops */
2088 gen_cast(&type);
2089 /* generate shifts */
2090 vpushi(bits - (bit_pos + bit_size));
2091 gen_op(TOK_SHL);
2092 vpushi(bits - bit_size);
2093 /* NOTE: transformed to SHR if unsigned */
2094 gen_op(TOK_SAR);
2096 r = gv(rc);
2097 } else {
2098 if (is_float(vtop->type.t) &&
2099 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
2100 unsigned long offset;
2101 /* CPUs usually cannot use float constants, so we store them
2102 generically in data segment */
2103 size = type_size(&vtop->type, &align);
2104 if (NODATA_WANTED)
2105 size = 0, align = 1;
2106 offset = section_add(data_section, size, align);
2107 vpush_ref(&vtop->type, data_section, offset, size);
2108 vswap();
2109 init_putv(&vtop->type, data_section, offset);
2110 vtop->r |= VT_LVAL;
2112 #ifdef CONFIG_TCC_BCHECK
2113 if (vtop->r & VT_MUSTBOUND)
2114 gbound();
2115 #endif
2117 bt = vtop->type.t & VT_BTYPE;
2119 #ifdef TCC_TARGET_RISCV64
2120 /* XXX mega hack */
2121 if (bt == VT_LDOUBLE && rc == RC_FLOAT)
2122 rc = RC_INT;
2123 #endif
2124 rc2 = RC2_TYPE(bt, rc);
2126 /* need to reload if:
2127 - constant
2128 - lvalue (need to dereference pointer)
2129 - already a register, but not in the right class */
2130 r = vtop->r & VT_VALMASK;
2131 r_ok = !(vtop->r & VT_LVAL) && (r < VT_CONST) && (reg_classes[r] & rc);
2132 r2_ok = !rc2 || ((vtop->r2 < VT_CONST) && (reg_classes[vtop->r2] & rc2));
2134 if (!r_ok || !r2_ok) {
2135 if (!r_ok)
2136 r = get_reg(rc);
2137 if (rc2) {
2138 int load_type = (bt == VT_QFLOAT) ? VT_DOUBLE : VT_PTRDIFF_T;
2139 int original_type = vtop->type.t;
2141 /* two register type load :
2142 expand to two words temporarily */
2143 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
2144 /* load constant */
2145 unsigned long long ll = vtop->c.i;
2146 vtop->c.i = ll; /* first word */
2147 load(r, vtop);
2148 vtop->r = r; /* save register value */
2149 vpushi(ll >> 32); /* second word */
2150 } else if (vtop->r & VT_LVAL) {
2151 /* We do not want to modifier the long long pointer here.
2152 So we save any other instances down the stack */
2153 save_reg_upstack(vtop->r, 1);
2154 /* load from memory */
2155 vtop->type.t = load_type;
2156 load(r, vtop);
2157 vdup();
2158 vtop[-1].r = r; /* save register value */
2159 /* increment pointer to get second word */
2160 vtop->type.t = VT_PTRDIFF_T;
2161 gaddrof();
2162 vpushs(PTR_SIZE);
2163 gen_op('+');
2164 vtop->r |= VT_LVAL;
2165 vtop->type.t = load_type;
2166 } else {
2167 /* move registers */
2168 if (!r_ok)
2169 load(r, vtop);
2170 if (r2_ok && vtop->r2 < VT_CONST)
2171 goto done;
2172 vdup();
2173 vtop[-1].r = r; /* save register value */
2174 vtop->r = vtop[-1].r2;
2176 /* Allocate second register. Here we rely on the fact that
2177 get_reg() tries first to free r2 of an SValue. */
2178 r2 = get_reg(rc2);
2179 load(r2, vtop);
2180 vpop();
2181 /* write second register */
2182 vtop->r2 = r2;
2183 done:
2184 vtop->type.t = original_type;
2185 } else {
2186 if (vtop->r == VT_CMP)
2187 vset_VT_JMP();
2188 /* one register type load */
2189 load(r, vtop);
2192 vtop->r = r;
2193 #ifdef TCC_TARGET_C67
2194 /* uses register pairs for doubles */
2195 if (bt == VT_DOUBLE)
2196 vtop->r2 = r+1;
2197 #endif
2199 return r;
2202 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
2203 ST_FUNC void gv2(int rc1, int rc2)
2205 /* generate more generic register first. But VT_JMP or VT_CMP
2206 values must be generated first in all cases to avoid possible
2207 reload errors */
2208 if (vtop->r != VT_CMP && rc1 <= rc2) {
2209 vswap();
2210 gv(rc1);
2211 vswap();
2212 gv(rc2);
2213 /* test if reload is needed for first register */
2214 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
2215 vswap();
2216 gv(rc1);
2217 vswap();
2219 } else {
2220 gv(rc2);
2221 vswap();
2222 gv(rc1);
2223 vswap();
2224 /* test if reload is needed for first register */
2225 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
2226 gv(rc2);
2231 #if PTR_SIZE == 4
2232 /* expand 64bit on stack in two ints */
2233 ST_FUNC void lexpand(void)
2235 int u, v;
2236 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
2237 v = vtop->r & (VT_VALMASK | VT_LVAL);
2238 if (v == VT_CONST) {
2239 vdup();
2240 vtop[0].c.i >>= 32;
2241 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
2242 vdup();
2243 vtop[0].c.i += 4;
2244 } else {
2245 gv(RC_INT);
2246 vdup();
2247 vtop[0].r = vtop[-1].r2;
2248 vtop[0].r2 = vtop[-1].r2 = VT_CONST;
2250 vtop[0].type.t = vtop[-1].type.t = VT_INT | u;
2252 #endif
2254 #if PTR_SIZE == 4
2255 /* build a long long from two ints */
2256 static void lbuild(int t)
2258 gv2(RC_INT, RC_INT);
2259 vtop[-1].r2 = vtop[0].r;
2260 vtop[-1].type.t = t;
2261 vpop();
2263 #endif
2265 /* convert stack entry to register and duplicate its value in another
2266 register */
2267 static void gv_dup(void)
2269 int t, rc, r;
2271 t = vtop->type.t;
2272 #if PTR_SIZE == 4
2273 if ((t & VT_BTYPE) == VT_LLONG) {
2274 if (t & VT_BITFIELD) {
2275 gv(RC_INT);
2276 t = vtop->type.t;
2278 lexpand();
2279 gv_dup();
2280 vswap();
2281 vrotb(3);
2282 gv_dup();
2283 vrotb(4);
2284 /* stack: H L L1 H1 */
2285 lbuild(t);
2286 vrotb(3);
2287 vrotb(3);
2288 vswap();
2289 lbuild(t);
2290 vswap();
2291 return;
2293 #endif
2294 /* duplicate value */
2295 rc = RC_TYPE(t);
2296 gv(rc);
2297 r = get_reg(rc);
2298 vdup();
2299 load(r, vtop);
2300 vtop->r = r;
2303 #if PTR_SIZE == 4
2304 /* generate CPU independent (unsigned) long long operations */
2305 static void gen_opl(int op)
2307 int t, a, b, op1, c, i;
2308 int func;
2309 unsigned short reg_iret = REG_IRET;
2310 unsigned short reg_lret = REG_IRE2;
2311 SValue tmp;
2313 switch(op) {
2314 case '/':
2315 case TOK_PDIV:
2316 func = TOK___divdi3;
2317 goto gen_func;
2318 case TOK_UDIV:
2319 func = TOK___udivdi3;
2320 goto gen_func;
2321 case '%':
2322 func = TOK___moddi3;
2323 goto gen_mod_func;
2324 case TOK_UMOD:
2325 func = TOK___umoddi3;
2326 gen_mod_func:
2327 #ifdef TCC_ARM_EABI
2328 reg_iret = TREG_R2;
2329 reg_lret = TREG_R3;
2330 #endif
2331 gen_func:
2332 /* call generic long long function */
2333 vpush_global_sym(&func_old_type, func);
2334 vrott(3);
2335 gfunc_call(2);
2336 vpushi(0);
2337 vtop->r = reg_iret;
2338 vtop->r2 = reg_lret;
2339 break;
2340 case '^':
2341 case '&':
2342 case '|':
2343 case '*':
2344 case '+':
2345 case '-':
2346 //pv("gen_opl A",0,2);
2347 t = vtop->type.t;
2348 vswap();
2349 lexpand();
2350 vrotb(3);
2351 lexpand();
2352 /* stack: L1 H1 L2 H2 */
2353 tmp = vtop[0];
2354 vtop[0] = vtop[-3];
2355 vtop[-3] = tmp;
2356 tmp = vtop[-2];
2357 vtop[-2] = vtop[-3];
2358 vtop[-3] = tmp;
2359 vswap();
2360 /* stack: H1 H2 L1 L2 */
2361 //pv("gen_opl B",0,4);
2362 if (op == '*') {
2363 vpushv(vtop - 1);
2364 vpushv(vtop - 1);
2365 gen_op(TOK_UMULL);
2366 lexpand();
2367 /* stack: H1 H2 L1 L2 ML MH */
2368 for(i=0;i<4;i++)
2369 vrotb(6);
2370 /* stack: ML MH H1 H2 L1 L2 */
2371 tmp = vtop[0];
2372 vtop[0] = vtop[-2];
2373 vtop[-2] = tmp;
2374 /* stack: ML MH H1 L2 H2 L1 */
2375 gen_op('*');
2376 vrotb(3);
2377 vrotb(3);
2378 gen_op('*');
2379 /* stack: ML MH M1 M2 */
2380 gen_op('+');
2381 gen_op('+');
2382 } else if (op == '+' || op == '-') {
2383 /* XXX: add non carry method too (for MIPS or alpha) */
2384 if (op == '+')
2385 op1 = TOK_ADDC1;
2386 else
2387 op1 = TOK_SUBC1;
2388 gen_op(op1);
2389 /* stack: H1 H2 (L1 op L2) */
2390 vrotb(3);
2391 vrotb(3);
2392 gen_op(op1 + 1); /* TOK_xxxC2 */
2393 } else {
2394 gen_op(op);
2395 /* stack: H1 H2 (L1 op L2) */
2396 vrotb(3);
2397 vrotb(3);
2398 /* stack: (L1 op L2) H1 H2 */
2399 gen_op(op);
2400 /* stack: (L1 op L2) (H1 op H2) */
2402 /* stack: L H */
2403 lbuild(t);
2404 break;
2405 case TOK_SAR:
2406 case TOK_SHR:
2407 case TOK_SHL:
2408 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
2409 t = vtop[-1].type.t;
2410 vswap();
2411 lexpand();
2412 vrotb(3);
2413 /* stack: L H shift */
2414 c = (int)vtop->c.i;
2415 /* constant: simpler */
2416 /* NOTE: all comments are for SHL. the other cases are
2417 done by swapping words */
2418 vpop();
2419 if (op != TOK_SHL)
2420 vswap();
2421 if (c >= 32) {
2422 /* stack: L H */
2423 vpop();
2424 if (c > 32) {
2425 vpushi(c - 32);
2426 gen_op(op);
2428 if (op != TOK_SAR) {
2429 vpushi(0);
2430 } else {
2431 gv_dup();
2432 vpushi(31);
2433 gen_op(TOK_SAR);
2435 vswap();
2436 } else {
2437 vswap();
2438 gv_dup();
2439 /* stack: H L L */
2440 vpushi(c);
2441 gen_op(op);
2442 vswap();
2443 vpushi(32 - c);
2444 if (op == TOK_SHL)
2445 gen_op(TOK_SHR);
2446 else
2447 gen_op(TOK_SHL);
2448 vrotb(3);
2449 /* stack: L L H */
2450 vpushi(c);
2451 if (op == TOK_SHL)
2452 gen_op(TOK_SHL);
2453 else
2454 gen_op(TOK_SHR);
2455 gen_op('|');
2457 if (op != TOK_SHL)
2458 vswap();
2459 lbuild(t);
2460 } else {
2461 /* XXX: should provide a faster fallback on x86 ? */
2462 switch(op) {
2463 case TOK_SAR:
2464 func = TOK___ashrdi3;
2465 goto gen_func;
2466 case TOK_SHR:
2467 func = TOK___lshrdi3;
2468 goto gen_func;
2469 case TOK_SHL:
2470 func = TOK___ashldi3;
2471 goto gen_func;
2474 break;
2475 default:
2476 /* compare operations */
2477 t = vtop->type.t;
2478 vswap();
2479 lexpand();
2480 vrotb(3);
2481 lexpand();
2482 /* stack: L1 H1 L2 H2 */
2483 tmp = vtop[-1];
2484 vtop[-1] = vtop[-2];
2485 vtop[-2] = tmp;
2486 /* stack: L1 L2 H1 H2 */
2487 save_regs(4);
2488 /* compare high */
2489 op1 = op;
2490 /* when values are equal, we need to compare low words. since
2491 the jump is inverted, we invert the test too. */
2492 if (op1 == TOK_LT)
2493 op1 = TOK_LE;
2494 else if (op1 == TOK_GT)
2495 op1 = TOK_GE;
2496 else if (op1 == TOK_ULT)
2497 op1 = TOK_ULE;
2498 else if (op1 == TOK_UGT)
2499 op1 = TOK_UGE;
2500 a = 0;
2501 b = 0;
2502 gen_op(op1);
2503 if (op == TOK_NE) {
2504 b = gvtst(0, 0);
2505 } else {
2506 a = gvtst(1, 0);
2507 if (op != TOK_EQ) {
2508 /* generate non equal test */
2509 vpushi(0);
2510 vset_VT_CMP(TOK_NE);
2511 b = gvtst(0, 0);
2514 /* compare low. Always unsigned */
2515 op1 = op;
2516 if (op1 == TOK_LT)
2517 op1 = TOK_ULT;
2518 else if (op1 == TOK_LE)
2519 op1 = TOK_ULE;
2520 else if (op1 == TOK_GT)
2521 op1 = TOK_UGT;
2522 else if (op1 == TOK_GE)
2523 op1 = TOK_UGE;
2524 gen_op(op1);
2525 #if 0//def TCC_TARGET_I386
2526 if (op == TOK_NE) { gsym(b); break; }
2527 if (op == TOK_EQ) { gsym(a); break; }
2528 #endif
2529 gvtst_set(1, a);
2530 gvtst_set(0, b);
2531 break;
2534 #endif
2536 static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b)
2538 uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b);
2539 return (a ^ b) >> 63 ? -x : x;
2542 static int gen_opic_lt(uint64_t a, uint64_t b)
2544 return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63);
2547 /* handle integer constant optimizations and various machine
2548 independent opt */
2549 static void gen_opic(int op)
2551 SValue *v1 = vtop - 1;
2552 SValue *v2 = vtop;
2553 int t1 = v1->type.t & VT_BTYPE;
2554 int t2 = v2->type.t & VT_BTYPE;
2555 int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2556 int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2557 uint64_t l1 = c1 ? v1->c.i : 0;
2558 uint64_t l2 = c2 ? v2->c.i : 0;
2559 int shm = (t1 == VT_LLONG) ? 63 : 31;
2561 if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
2562 l1 = ((uint32_t)l1 |
2563 (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
2564 if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR))
2565 l2 = ((uint32_t)l2 |
2566 (v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000)));
2568 if (c1 && c2) {
2569 switch(op) {
2570 case '+': l1 += l2; break;
2571 case '-': l1 -= l2; break;
2572 case '&': l1 &= l2; break;
2573 case '^': l1 ^= l2; break;
2574 case '|': l1 |= l2; break;
2575 case '*': l1 *= l2; break;
2577 case TOK_PDIV:
2578 case '/':
2579 case '%':
2580 case TOK_UDIV:
2581 case TOK_UMOD:
2582 /* if division by zero, generate explicit division */
2583 if (l2 == 0) {
2584 if (const_wanted && !(nocode_wanted & unevalmask))
2585 tcc_error("division by zero in constant");
2586 goto general_case;
2588 switch(op) {
2589 default: l1 = gen_opic_sdiv(l1, l2); break;
2590 case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break;
2591 case TOK_UDIV: l1 = l1 / l2; break;
2592 case TOK_UMOD: l1 = l1 % l2; break;
2594 break;
2595 case TOK_SHL: l1 <<= (l2 & shm); break;
2596 case TOK_SHR: l1 >>= (l2 & shm); break;
2597 case TOK_SAR:
2598 l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm);
2599 break;
2600 /* tests */
2601 case TOK_ULT: l1 = l1 < l2; break;
2602 case TOK_UGE: l1 = l1 >= l2; break;
2603 case TOK_EQ: l1 = l1 == l2; break;
2604 case TOK_NE: l1 = l1 != l2; break;
2605 case TOK_ULE: l1 = l1 <= l2; break;
2606 case TOK_UGT: l1 = l1 > l2; break;
2607 case TOK_LT: l1 = gen_opic_lt(l1, l2); break;
2608 case TOK_GE: l1 = !gen_opic_lt(l1, l2); break;
2609 case TOK_LE: l1 = !gen_opic_lt(l2, l1); break;
2610 case TOK_GT: l1 = gen_opic_lt(l2, l1); break;
2611 /* logical */
2612 case TOK_LAND: l1 = l1 && l2; break;
2613 case TOK_LOR: l1 = l1 || l2; break;
2614 default:
2615 goto general_case;
2617 if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
2618 l1 = ((uint32_t)l1 |
2619 (v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
2620 v1->c.i = l1;
2621 vtop--;
2622 } else {
2623 /* if commutative ops, put c2 as constant */
2624 if (c1 && (op == '+' || op == '&' || op == '^' ||
2625 op == '|' || op == '*' || op == TOK_EQ || op == TOK_NE)) {
2626 vswap();
2627 c2 = c1; //c = c1, c1 = c2, c2 = c;
2628 l2 = l1; //l = l1, l1 = l2, l2 = l;
2630 if (!const_wanted &&
2631 c1 && ((l1 == 0 &&
2632 (op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
2633 (l1 == -1 && op == TOK_SAR))) {
2634 /* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
2635 vtop--;
2636 } else if (!const_wanted &&
2637 c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
2638 (op == '|' &&
2639 (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))) ||
2640 (l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
2641 /* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
2642 if (l2 == 1)
2643 vtop->c.i = 0;
2644 vswap();
2645 vtop--;
2646 } else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
2647 op == TOK_PDIV) &&
2648 l2 == 1) ||
2649 ((op == '+' || op == '-' || op == '|' || op == '^' ||
2650 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
2651 l2 == 0) ||
2652 (op == '&' &&
2653 (l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))))) {
2654 /* filter out NOP operations like x*1, x-0, x&-1... */
2655 vtop--;
2656 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
2657 /* try to use shifts instead of muls or divs */
2658 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
2659 int n = -1;
2660 while (l2) {
2661 l2 >>= 1;
2662 n++;
2664 vtop->c.i = n;
2665 if (op == '*')
2666 op = TOK_SHL;
2667 else if (op == TOK_PDIV)
2668 op = TOK_SAR;
2669 else
2670 op = TOK_SHR;
2672 goto general_case;
2673 } else if (c2 && (op == '+' || op == '-') &&
2674 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
2675 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
2676 /* symbol + constant case */
2677 if (op == '-')
2678 l2 = -l2;
2679 l2 += vtop[-1].c.i;
2680 /* The backends can't always deal with addends to symbols
2681 larger than +-1<<31. Don't construct such. */
2682 if ((int)l2 != l2)
2683 goto general_case;
2684 vtop--;
2685 vtop->c.i = l2;
2686 } else {
2687 general_case:
2688 /* call low level op generator */
2689 if (t1 == VT_LLONG || t2 == VT_LLONG ||
2690 (PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
2691 gen_opl(op);
2692 else
2693 gen_opi(op);
2698 /* generate a floating point operation with constant propagation */
2699 static void gen_opif(int op)
2701 int c1, c2;
2702 SValue *v1, *v2;
2703 #if defined _MSC_VER && defined __x86_64__
2704 /* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
2705 volatile
2706 #endif
2707 long double f1, f2;
2709 v1 = vtop - 1;
2710 v2 = vtop;
2711 /* currently, we cannot do computations with forward symbols */
2712 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2713 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2714 if (c1 && c2) {
2715 if (v1->type.t == VT_FLOAT) {
2716 f1 = v1->c.f;
2717 f2 = v2->c.f;
2718 } else if (v1->type.t == VT_DOUBLE) {
2719 f1 = v1->c.d;
2720 f2 = v2->c.d;
2721 } else {
2722 f1 = v1->c.ld;
2723 f2 = v2->c.ld;
2726 /* NOTE: we only do constant propagation if finite number (not
2727 NaN or infinity) (ANSI spec) */
2728 if (!ieee_finite(f1) || !ieee_finite(f2))
2729 goto general_case;
2731 switch(op) {
2732 case '+': f1 += f2; break;
2733 case '-': f1 -= f2; break;
2734 case '*': f1 *= f2; break;
2735 case '/':
2736 if (f2 == 0.0) {
2737 /* If not in initializer we need to potentially generate
2738 FP exceptions at runtime, otherwise we want to fold. */
2739 if (!const_wanted)
2740 goto general_case;
2742 f1 /= f2;
2743 break;
2744 /* XXX: also handles tests ? */
2745 default:
2746 goto general_case;
2748 /* XXX: overflow test ? */
2749 if (v1->type.t == VT_FLOAT) {
2750 v1->c.f = f1;
2751 } else if (v1->type.t == VT_DOUBLE) {
2752 v1->c.d = f1;
2753 } else {
2754 v1->c.ld = f1;
2756 vtop--;
2757 } else {
2758 general_case:
2759 gen_opf(op);
2763 /* print a type. If 'varstr' is not NULL, then the variable is also
2764 printed in the type */
2765 /* XXX: union */
2766 /* XXX: add array and function pointers */
2767 static void type_to_str(char *buf, int buf_size,
2768 CType *type, const char *varstr)
2770 int bt, v, t;
2771 Sym *s, *sa;
2772 char buf1[256];
2773 const char *tstr;
2775 t = type->t;
2776 bt = t & VT_BTYPE;
2777 buf[0] = '\0';
2779 if (t & VT_EXTERN)
2780 pstrcat(buf, buf_size, "extern ");
2781 if (t & VT_STATIC)
2782 pstrcat(buf, buf_size, "static ");
2783 if (t & VT_TYPEDEF)
2784 pstrcat(buf, buf_size, "typedef ");
2785 if (t & VT_INLINE)
2786 pstrcat(buf, buf_size, "inline ");
2787 if (t & VT_VOLATILE)
2788 pstrcat(buf, buf_size, "volatile ");
2789 if (t & VT_CONSTANT)
2790 pstrcat(buf, buf_size, "const ");
2792 if (((t & VT_DEFSIGN) && bt == VT_BYTE)
2793 || ((t & VT_UNSIGNED)
2794 && (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG)
2795 && !IS_ENUM(t)
2797 pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed ");
2799 buf_size -= strlen(buf);
2800 buf += strlen(buf);
2802 switch(bt) {
2803 case VT_VOID:
2804 tstr = "void";
2805 goto add_tstr;
2806 case VT_BOOL:
2807 tstr = "_Bool";
2808 goto add_tstr;
2809 case VT_BYTE:
2810 tstr = "char";
2811 goto add_tstr;
2812 case VT_SHORT:
2813 tstr = "short";
2814 goto add_tstr;
2815 case VT_INT:
2816 tstr = "int";
2817 goto maybe_long;
2818 case VT_LLONG:
2819 tstr = "long long";
2820 maybe_long:
2821 if (t & VT_LONG)
2822 tstr = "long";
2823 if (!IS_ENUM(t))
2824 goto add_tstr;
2825 tstr = "enum ";
2826 goto tstruct;
2827 case VT_FLOAT:
2828 tstr = "float";
2829 goto add_tstr;
2830 case VT_DOUBLE:
2831 tstr = "double";
2832 if (!(t & VT_LONG))
2833 goto add_tstr;
2834 case VT_LDOUBLE:
2835 tstr = "long double";
2836 add_tstr:
2837 pstrcat(buf, buf_size, tstr);
2838 break;
2839 case VT_STRUCT:
2840 tstr = "struct ";
2841 if (IS_UNION(t))
2842 tstr = "union ";
2843 tstruct:
2844 pstrcat(buf, buf_size, tstr);
2845 v = type->ref->v & ~SYM_STRUCT;
2846 if (v >= SYM_FIRST_ANOM)
2847 pstrcat(buf, buf_size, "<anonymous>");
2848 else
2849 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2850 break;
2851 case VT_FUNC:
2852 s = type->ref;
2853 buf1[0]=0;
2854 if (varstr && '*' == *varstr) {
2855 pstrcat(buf1, sizeof(buf1), "(");
2856 pstrcat(buf1, sizeof(buf1), varstr);
2857 pstrcat(buf1, sizeof(buf1), ")");
2859 pstrcat(buf1, buf_size, "(");
2860 sa = s->next;
2861 while (sa != NULL) {
2862 char buf2[256];
2863 type_to_str(buf2, sizeof(buf2), &sa->type, NULL);
2864 pstrcat(buf1, sizeof(buf1), buf2);
2865 sa = sa->next;
2866 if (sa)
2867 pstrcat(buf1, sizeof(buf1), ", ");
2869 if (s->f.func_type == FUNC_ELLIPSIS)
2870 pstrcat(buf1, sizeof(buf1), ", ...");
2871 pstrcat(buf1, sizeof(buf1), ")");
2872 type_to_str(buf, buf_size, &s->type, buf1);
2873 goto no_var;
2874 case VT_PTR:
2875 s = type->ref;
2876 if (t & VT_ARRAY) {
2877 if (varstr && '*' == *varstr)
2878 snprintf(buf1, sizeof(buf1), "(%s)[%d]", varstr, s->c);
2879 else
2880 snprintf(buf1, sizeof(buf1), "%s[%d]", varstr ? varstr : "", s->c);
2881 type_to_str(buf, buf_size, &s->type, buf1);
2882 goto no_var;
2884 pstrcpy(buf1, sizeof(buf1), "*");
2885 if (t & VT_CONSTANT)
2886 pstrcat(buf1, buf_size, "const ");
2887 if (t & VT_VOLATILE)
2888 pstrcat(buf1, buf_size, "volatile ");
2889 if (varstr)
2890 pstrcat(buf1, sizeof(buf1), varstr);
2891 type_to_str(buf, buf_size, &s->type, buf1);
2892 goto no_var;
2894 if (varstr) {
2895 pstrcat(buf, buf_size, " ");
2896 pstrcat(buf, buf_size, varstr);
2898 no_var: ;
2901 static void type_incompatibility_error(CType* st, CType* dt, const char* fmt)
2903 char buf1[256], buf2[256];
2904 type_to_str(buf1, sizeof(buf1), st, NULL);
2905 type_to_str(buf2, sizeof(buf2), dt, NULL);
2906 tcc_error(fmt, buf1, buf2);
2909 static void type_incompatibility_warning(CType* st, CType* dt, const char* fmt)
2911 char buf1[256], buf2[256];
2912 type_to_str(buf1, sizeof(buf1), st, NULL);
2913 type_to_str(buf2, sizeof(buf2), dt, NULL);
2914 tcc_warning(fmt, buf1, buf2);
2917 static int pointed_size(CType *type)
2919 int align;
2920 return type_size(pointed_type(type), &align);
2923 static void vla_runtime_pointed_size(CType *type)
2925 int align;
2926 vla_runtime_type_size(pointed_type(type), &align);
2929 static inline int is_null_pointer(SValue *p)
2931 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
2932 return 0;
2933 return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) ||
2934 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) ||
2935 ((p->type.t & VT_BTYPE) == VT_PTR &&
2936 (PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0) &&
2937 ((pointed_type(&p->type)->t & VT_BTYPE) == VT_VOID) &&
2938 0 == (pointed_type(&p->type)->t & (VT_CONSTANT | VT_VOLATILE))
2942 /* compare function types. OLD functions match any new functions */
2943 static int is_compatible_func(CType *type1, CType *type2)
2945 Sym *s1, *s2;
2947 s1 = type1->ref;
2948 s2 = type2->ref;
2949 if (s1->f.func_call != s2->f.func_call)
2950 return 0;
2951 if (s1->f.func_type != s2->f.func_type
2952 && s1->f.func_type != FUNC_OLD
2953 && s2->f.func_type != FUNC_OLD)
2954 return 0;
2955 /* we should check the function return type for FUNC_OLD too
2956 but that causes problems with the internally used support
2957 functions such as TOK_memmove */
2958 if (s1->f.func_type == FUNC_OLD && !s1->next)
2959 return 1;
2960 if (s2->f.func_type == FUNC_OLD && !s2->next)
2961 return 1;
2962 for (;;) {
2963 if (!is_compatible_unqualified_types(&s1->type, &s2->type))
2964 return 0;
2965 s1 = s1->next;
2966 s2 = s2->next;
2967 if (!s1)
2968 return !s2;
2969 if (!s2)
2970 return 0;
2974 /* return true if type1 and type2 are the same. If unqualified is
2975 true, qualifiers on the types are ignored.
2977 static int compare_types(CType *type1, CType *type2, int unqualified)
2979 int bt1, t1, t2;
2981 t1 = type1->t & VT_TYPE;
2982 t2 = type2->t & VT_TYPE;
2983 if (unqualified) {
2984 /* strip qualifiers before comparing */
2985 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2986 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2989 /* Default Vs explicit signedness only matters for char */
2990 if ((t1 & VT_BTYPE) != VT_BYTE) {
2991 t1 &= ~VT_DEFSIGN;
2992 t2 &= ~VT_DEFSIGN;
2994 /* XXX: bitfields ? */
2995 if (t1 != t2)
2996 return 0;
2998 if ((t1 & VT_ARRAY)
2999 && !(type1->ref->c < 0
3000 || type2->ref->c < 0
3001 || type1->ref->c == type2->ref->c))
3002 return 0;
3004 /* test more complicated cases */
3005 bt1 = t1 & VT_BTYPE;
3006 if (bt1 == VT_PTR) {
3007 type1 = pointed_type(type1);
3008 type2 = pointed_type(type2);
3009 return is_compatible_types(type1, type2);
3010 } else if (bt1 == VT_STRUCT) {
3011 return (type1->ref == type2->ref);
3012 } else if (bt1 == VT_FUNC) {
3013 return is_compatible_func(type1, type2);
3014 } else if (IS_ENUM(type1->t) && IS_ENUM(type2->t)) {
3015 /* If both are enums then they must be the same, if only one is then
3016 t1 and t2 must be equal, which was checked above already. */
3017 return type1->ref == type2->ref;
3018 } else {
3019 return 1;
3023 /* Check if OP1 and OP2 can be "combined" with operation OP, the combined
3024 type is stored in DEST if non-null (except for pointer plus/minus) . */
3025 static int combine_types(CType *dest, SValue *op1, SValue *op2, int op)
3027 CType *type1 = &op1->type, *type2 = &op2->type, type;
3028 int t1 = type1->t, t2 = type2->t, bt1 = t1 & VT_BTYPE, bt2 = t2 & VT_BTYPE;
3029 int ret = 1;
3031 type.t = VT_VOID;
3032 type.ref = NULL;
3034 if (bt1 == VT_VOID || bt2 == VT_VOID) {
3035 ret = op == '?' ? 1 : 0;
3036 /* NOTE: as an extension, we accept void on only one side */
3037 type.t = VT_VOID;
3038 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3039 if (op == '+') ; /* Handled in caller */
3040 /* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
3041 /* If one is a null ptr constant the result type is the other. */
3042 else if (is_null_pointer (op2)) type = *type1;
3043 else if (is_null_pointer (op1)) type = *type2;
3044 else if (bt1 != bt2) {
3045 /* accept comparison or cond-expr between pointer and integer
3046 with a warning */
3047 if ((op == '?' || TOK_ISCOND(op))
3048 && (is_integer_btype(bt1) || is_integer_btype(bt2)))
3049 tcc_warning("pointer/integer mismatch in %s",
3050 op == '?' ? "conditional expression" : "comparison");
3051 else if (op != '-' || !is_integer_btype(bt2))
3052 ret = 0;
3053 type = *(bt1 == VT_PTR ? type1 : type2);
3054 } else {
3055 CType *pt1 = pointed_type(type1);
3056 CType *pt2 = pointed_type(type2);
3057 int pbt1 = pt1->t & VT_BTYPE;
3058 int pbt2 = pt2->t & VT_BTYPE;
3059 int newquals, copied = 0;
3060 if (pbt1 != VT_VOID && pbt2 != VT_VOID
3061 && !compare_types(pt1, pt2, 1/*unqualif*/)) {
3062 if (op != '?' && !TOK_ISCOND(op))
3063 ret = 0;
3064 else
3065 type_incompatibility_warning(type1, type2,
3066 op == '?'
3067 ? "pointer type mismatch in conditional expression ('%s' and '%s')"
3068 : "pointer type mismatch in comparison('%s' and '%s')");
3070 if (op == '?') {
3071 /* pointers to void get preferred, otherwise the
3072 pointed to types minus qualifs should be compatible */
3073 type = *((pbt1 == VT_VOID) ? type1 : type2);
3074 /* combine qualifs */
3075 newquals = ((pt1->t | pt2->t) & (VT_CONSTANT | VT_VOLATILE));
3076 if ((~pointed_type(&type)->t & (VT_CONSTANT | VT_VOLATILE))
3077 & newquals)
3079 /* copy the pointer target symbol */
3080 type.ref = sym_push(SYM_FIELD, &type.ref->type,
3081 0, type.ref->c);
3082 copied = 1;
3083 pointed_type(&type)->t |= newquals;
3085 /* pointers to incomplete arrays get converted to
3086 pointers to completed ones if possible */
3087 if (pt1->t & VT_ARRAY
3088 && pt2->t & VT_ARRAY
3089 && pointed_type(&type)->ref->c < 0
3090 && (pt1->ref->c > 0 || pt2->ref->c > 0))
3092 if (!copied)
3093 type.ref = sym_push(SYM_FIELD, &type.ref->type,
3094 0, type.ref->c);
3095 pointed_type(&type)->ref =
3096 sym_push(SYM_FIELD, &pointed_type(&type)->ref->type,
3097 0, pointed_type(&type)->ref->c);
3098 pointed_type(&type)->ref->c =
3099 0 < pt1->ref->c ? pt1->ref->c : pt2->ref->c;
3103 if (TOK_ISCOND(op))
3104 type.t = VT_SIZE_T;
3105 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3106 if (op != '?' || !compare_types(type1, type2, 1))
3107 ret = 0;
3108 type = *type1;
3109 } else if (is_float(bt1) || is_float(bt2)) {
3110 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3111 type.t = VT_LDOUBLE;
3112 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3113 type.t = VT_DOUBLE;
3114 } else {
3115 type.t = VT_FLOAT;
3117 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3118 /* cast to biggest op */
3119 type.t = VT_LLONG | VT_LONG;
3120 if (bt1 == VT_LLONG)
3121 type.t &= t1;
3122 if (bt2 == VT_LLONG)
3123 type.t &= t2;
3124 /* convert to unsigned if it does not fit in a long long */
3125 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
3126 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
3127 type.t |= VT_UNSIGNED;
3128 } else {
3129 /* integer operations */
3130 type.t = VT_INT | (VT_LONG & (t1 | t2));
3131 /* convert to unsigned if it does not fit in an integer */
3132 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
3133 (t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
3134 type.t |= VT_UNSIGNED;
3136 if (dest)
3137 *dest = type;
3138 return ret;
3141 /* generic gen_op: handles types problems */
3142 ST_FUNC void gen_op(int op)
3144 int u, t1, t2, bt1, bt2, t;
3145 CType type1, combtype;
3147 redo:
3148 t1 = vtop[-1].type.t;
3149 t2 = vtop[0].type.t;
3150 bt1 = t1 & VT_BTYPE;
3151 bt2 = t2 & VT_BTYPE;
3153 if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3154 if (bt2 == VT_FUNC) {
3155 mk_pointer(&vtop->type);
3156 gaddrof();
3158 if (bt1 == VT_FUNC) {
3159 vswap();
3160 mk_pointer(&vtop->type);
3161 gaddrof();
3162 vswap();
3164 goto redo;
3165 } else if (!combine_types(&combtype, vtop - 1, vtop, op)) {
3166 tcc_error_noabort("invalid operand types for binary operation");
3167 vpop();
3168 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3169 /* at least one operand is a pointer */
3170 /* relational op: must be both pointers */
3171 if (TOK_ISCOND(op))
3172 goto std_op;
3173 /* if both pointers, then it must be the '-' op */
3174 if (bt1 == VT_PTR && bt2 == VT_PTR) {
3175 if (op != '-')
3176 tcc_error("cannot use pointers here");
3177 if (vtop[-1].type.t & VT_VLA) {
3178 vla_runtime_pointed_size(&vtop[-1].type);
3179 } else {
3180 vpushi(pointed_size(&vtop[-1].type));
3182 vrott(3);
3183 gen_opic(op);
3184 vtop->type.t = VT_PTRDIFF_T;
3185 vswap();
3186 gen_op(TOK_PDIV);
3187 } else {
3188 /* exactly one pointer : must be '+' or '-'. */
3189 if (op != '-' && op != '+')
3190 tcc_error("cannot use pointers here");
3191 /* Put pointer as first operand */
3192 if (bt2 == VT_PTR) {
3193 vswap();
3194 t = t1, t1 = t2, t2 = t;
3196 #if PTR_SIZE == 4
3197 if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG)
3198 /* XXX: truncate here because gen_opl can't handle ptr + long long */
3199 gen_cast_s(VT_INT);
3200 #endif
3201 type1 = vtop[-1].type;
3202 if (vtop[-1].type.t & VT_VLA)
3203 vla_runtime_pointed_size(&vtop[-1].type);
3204 else {
3205 u = pointed_size(&vtop[-1].type);
3206 if (u < 0)
3207 tcc_error("unknown array element size");
3208 #if PTR_SIZE == 8
3209 vpushll(u);
3210 #else
3211 /* XXX: cast to int ? (long long case) */
3212 vpushi(u);
3213 #endif
3215 gen_op('*');
3216 #ifdef CONFIG_TCC_BCHECK
3217 if (tcc_state->do_bounds_check && !const_wanted) {
3218 /* if bounded pointers, we generate a special code to
3219 test bounds */
3220 if (op == '-') {
3221 vpushi(0);
3222 vswap();
3223 gen_op('-');
3225 gen_bounded_ptr_add();
3226 } else
3227 #endif
3229 gen_opic(op);
3231 type1.t &= ~VT_ARRAY;
3232 /* put again type if gen_opic() swaped operands */
3233 vtop->type = type1;
3235 } else {
3236 /* floats can only be used for a few operations */
3237 if (is_float(combtype.t)
3238 && op != '+' && op != '-' && op != '*' && op != '/'
3239 && !TOK_ISCOND(op))
3240 tcc_error("invalid operands for binary operation");
3241 else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
3242 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
3243 if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED))
3244 t |= VT_UNSIGNED;
3245 t |= (VT_LONG & t1);
3246 combtype.t = t;
3248 std_op:
3249 t = t2 = combtype.t;
3250 /* XXX: currently, some unsigned operations are explicit, so
3251 we modify them here */
3252 if (t & VT_UNSIGNED) {
3253 if (op == TOK_SAR)
3254 op = TOK_SHR;
3255 else if (op == '/')
3256 op = TOK_UDIV;
3257 else if (op == '%')
3258 op = TOK_UMOD;
3259 else if (op == TOK_LT)
3260 op = TOK_ULT;
3261 else if (op == TOK_GT)
3262 op = TOK_UGT;
3263 else if (op == TOK_LE)
3264 op = TOK_ULE;
3265 else if (op == TOK_GE)
3266 op = TOK_UGE;
3268 vswap();
3269 gen_cast_s(t);
3270 vswap();
3271 /* special case for shifts and long long: we keep the shift as
3272 an integer */
3273 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
3274 t2 = VT_INT;
3275 gen_cast_s(t2);
3276 if (is_float(t))
3277 gen_opif(op);
3278 else
3279 gen_opic(op);
3280 if (TOK_ISCOND(op)) {
3281 /* relational op: the result is an int */
3282 vtop->type.t = VT_INT;
3283 } else {
3284 vtop->type.t = t;
3287 // Make sure that we have converted to an rvalue:
3288 if (vtop->r & VT_LVAL)
3289 gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
3292 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64 || defined TCC_TARGET_ARM
3293 #define gen_cvt_itof1 gen_cvt_itof
3294 #else
3295 /* generic itof for unsigned long long case */
3296 static void gen_cvt_itof1(int t)
3298 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
3299 (VT_LLONG | VT_UNSIGNED)) {
3301 if (t == VT_FLOAT)
3302 vpush_global_sym(&func_old_type, TOK___floatundisf);
3303 #if LDOUBLE_SIZE != 8
3304 else if (t == VT_LDOUBLE)
3305 vpush_global_sym(&func_old_type, TOK___floatundixf);
3306 #endif
3307 else
3308 vpush_global_sym(&func_old_type, TOK___floatundidf);
3309 vrott(2);
3310 gfunc_call(1);
3311 vpushi(0);
3312 PUT_R_RET(vtop, t);
3313 } else {
3314 gen_cvt_itof(t);
3317 #endif
3319 #if defined TCC_TARGET_ARM64 || defined TCC_TARGET_RISCV64
3320 #define gen_cvt_ftoi1 gen_cvt_ftoi
3321 #else
3322 /* generic ftoi for unsigned long long case */
3323 static void gen_cvt_ftoi1(int t)
3325 int st;
3326 if (t == (VT_LLONG | VT_UNSIGNED)) {
3327 /* not handled natively */
3328 st = vtop->type.t & VT_BTYPE;
3329 if (st == VT_FLOAT)
3330 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
3331 #if LDOUBLE_SIZE != 8
3332 else if (st == VT_LDOUBLE)
3333 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
3334 #endif
3335 else
3336 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
3337 vrott(2);
3338 gfunc_call(1);
3339 vpushi(0);
3340 PUT_R_RET(vtop, t);
3341 } else {
3342 gen_cvt_ftoi(t);
3345 #endif
3347 /* special delayed cast for char/short */
3348 static void force_charshort_cast(void)
3350 int sbt = BFGET(vtop->r, VT_MUSTCAST) == 2 ? VT_LLONG : VT_INT;
3351 int dbt = vtop->type.t;
3352 vtop->r &= ~VT_MUSTCAST;
3353 vtop->type.t = sbt;
3354 gen_cast_s(dbt == VT_BOOL ? VT_BYTE|VT_UNSIGNED : dbt);
3355 vtop->type.t = dbt;
3358 static void gen_cast_s(int t)
3360 CType type;
3361 type.t = t;
3362 type.ref = NULL;
3363 gen_cast(&type);
3366 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
3367 static void gen_cast(CType *type)
3369 int sbt, dbt, sf, df, c;
3370 int dbt_bt, sbt_bt, ds, ss, bits, trunc;
3372 /* special delayed cast for char/short */
3373 if (vtop->r & VT_MUSTCAST)
3374 force_charshort_cast();
3376 /* bitfields first get cast to ints */
3377 if (vtop->type.t & VT_BITFIELD)
3378 gv(RC_INT);
3380 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
3381 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3382 if (sbt == VT_FUNC)
3383 sbt = VT_PTR;
3385 again:
3386 if (sbt != dbt) {
3387 sf = is_float(sbt);
3388 df = is_float(dbt);
3389 dbt_bt = dbt & VT_BTYPE;
3390 sbt_bt = sbt & VT_BTYPE;
3392 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3393 #if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
3394 c &= (dbt != VT_LDOUBLE) | !!nocode_wanted;
3395 #endif
3396 if (c) {
3397 /* constant case: we can do it now */
3398 /* XXX: in ISOC, cannot do it if error in convert */
3399 if (sbt == VT_FLOAT)
3400 vtop->c.ld = vtop->c.f;
3401 else if (sbt == VT_DOUBLE)
3402 vtop->c.ld = vtop->c.d;
3404 if (df) {
3405 if (sbt_bt == VT_LLONG) {
3406 if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 63))
3407 vtop->c.ld = vtop->c.i;
3408 else
3409 vtop->c.ld = -(long double)-vtop->c.i;
3410 } else if(!sf) {
3411 if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 31))
3412 vtop->c.ld = (uint32_t)vtop->c.i;
3413 else
3414 vtop->c.ld = -(long double)-(uint32_t)vtop->c.i;
3417 if (dbt == VT_FLOAT)
3418 vtop->c.f = (float)vtop->c.ld;
3419 else if (dbt == VT_DOUBLE)
3420 vtop->c.d = (double)vtop->c.ld;
3421 } else if (sf && dbt == VT_BOOL) {
3422 vtop->c.i = (vtop->c.ld != 0);
3423 } else {
3424 if(sf)
3425 vtop->c.i = vtop->c.ld;
3426 else if (sbt_bt == VT_LLONG || (PTR_SIZE == 8 && sbt == VT_PTR))
3428 else if (sbt & VT_UNSIGNED)
3429 vtop->c.i = (uint32_t)vtop->c.i;
3430 else
3431 vtop->c.i = ((uint32_t)vtop->c.i | -(vtop->c.i & 0x80000000));
3433 if (dbt_bt == VT_LLONG || (PTR_SIZE == 8 && dbt == VT_PTR))
3435 else if (dbt == VT_BOOL)
3436 vtop->c.i = (vtop->c.i != 0);
3437 else {
3438 uint32_t m = dbt_bt == VT_BYTE ? 0xff :
3439 dbt_bt == VT_SHORT ? 0xffff :
3440 0xffffffff;
3441 vtop->c.i &= m;
3442 if (!(dbt & VT_UNSIGNED))
3443 vtop->c.i |= -(vtop->c.i & ((m >> 1) + 1));
3446 goto done;
3448 } else if (dbt == VT_BOOL
3449 && (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM))
3450 == (VT_CONST | VT_SYM)) {
3451 /* addresses are considered non-zero (see tcctest.c:sinit23) */
3452 vtop->r = VT_CONST;
3453 vtop->c.i = 1;
3454 goto done;
3457 /* cannot generate code for global or static initializers */
3458 if (STATIC_DATA_WANTED)
3459 goto done;
3461 /* non constant case: generate code */
3462 if (dbt == VT_BOOL) {
3463 gen_test_zero(TOK_NE);
3464 goto done;
3467 if (sf || df) {
3468 if (sf && df) {
3469 /* convert from fp to fp */
3470 gen_cvt_ftof(dbt);
3471 } else if (df) {
3472 /* convert int to fp */
3473 gen_cvt_itof1(dbt);
3474 } else {
3475 /* convert fp to int */
3476 sbt = dbt;
3477 if (dbt_bt != VT_LLONG && dbt_bt != VT_INT)
3478 sbt = VT_INT;
3479 gen_cvt_ftoi1(sbt);
3480 goto again; /* may need char/short cast */
3482 goto done;
3485 ds = btype_size(dbt_bt);
3486 ss = btype_size(sbt_bt);
3487 if (ds == 0 || ss == 0) {
3488 if (dbt_bt == VT_VOID)
3489 goto done;
3490 cast_error(&vtop->type, type);
3492 if (IS_ENUM(type->t) && type->ref->c < 0)
3493 tcc_error("cast to incomplete type");
3495 /* same size and no sign conversion needed */
3496 if (ds == ss && ds >= 4)
3497 goto done;
3498 if (dbt_bt == VT_PTR || sbt_bt == VT_PTR) {
3499 tcc_warning("cast between pointer and integer of different size");
3500 if (sbt_bt == VT_PTR) {
3501 /* put integer type to allow logical operations below */
3502 vtop->type.t = (PTR_SIZE == 8 ? VT_LLONG : VT_INT);
3506 /* processor allows { int a = 0, b = *(char*)&a; }
3507 That means that if we cast to less width, we can just
3508 change the type and read it still later. */
3509 #define ALLOW_SUBTYPE_ACCESS 1
3511 if (ALLOW_SUBTYPE_ACCESS && (vtop->r & VT_LVAL)) {
3512 /* value still in memory */
3513 if (ds <= ss)
3514 goto done;
3515 /* ss <= 4 here */
3516 if (ds <= 4) {
3517 gv(RC_INT);
3518 goto done; /* no 64bit envolved */
3521 gv(RC_INT);
3523 trunc = 0;
3524 #if PTR_SIZE == 4
3525 if (ds == 8) {
3526 /* generate high word */
3527 if (sbt & VT_UNSIGNED) {
3528 vpushi(0);
3529 gv(RC_INT);
3530 } else {
3531 gv_dup();
3532 vpushi(31);
3533 gen_op(TOK_SAR);
3535 lbuild(dbt);
3536 } else if (ss == 8) {
3537 /* from long long: just take low order word */
3538 lexpand();
3539 vpop();
3541 ss = 4;
3543 #elif PTR_SIZE == 8
3544 if (ds == 8) {
3545 /* need to convert from 32bit to 64bit */
3546 if (sbt & VT_UNSIGNED) {
3547 #if defined(TCC_TARGET_RISCV64)
3548 /* RISC-V keeps 32bit vals in registers sign-extended.
3549 So here we need a zero-extension. */
3550 trunc = 32;
3551 #else
3552 goto done;
3553 #endif
3554 } else {
3555 gen_cvt_sxtw();
3556 goto done;
3558 ss = ds, ds = 4, dbt = sbt;
3559 } else if (ss == 8) {
3560 /* XXX some architectures (e.g. risc-v) would like it
3561 better for this merely being a 32-to-64 sign or zero-
3562 extension. */
3563 trunc = 32; /* zero upper 32 bits */
3564 } else {
3565 ss = 4;
3567 #endif
3569 if (ds >= ss)
3570 goto done;
3571 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM64
3572 if (ss == 4) {
3573 gen_cvt_csti(dbt);
3574 goto done;
3576 #endif
3577 bits = (ss - ds) * 8;
3578 /* for unsigned, gen_op will convert SAR to SHR */
3579 vtop->type.t = (ss == 8 ? VT_LLONG : VT_INT) | (dbt & VT_UNSIGNED);
3580 vpushi(bits);
3581 gen_op(TOK_SHL);
3582 vpushi(bits - trunc);
3583 gen_op(TOK_SAR);
3584 vpushi(trunc);
3585 gen_op(TOK_SHR);
3587 done:
3588 vtop->type = *type;
3589 vtop->type.t &= ~ ( VT_CONSTANT | VT_VOLATILE | VT_ARRAY );
3592 /* return type size as known at compile time. Put alignment at 'a' */
3593 ST_FUNC int type_size(CType *type, int *a)
3595 Sym *s;
3596 int bt;
3598 bt = type->t & VT_BTYPE;
3599 if (bt == VT_STRUCT) {
3600 /* struct/union */
3601 s = type->ref;
3602 *a = s->r;
3603 return s->c;
3604 } else if (bt == VT_PTR) {
3605 if (type->t & VT_ARRAY) {
3606 int ts;
3608 s = type->ref;
3609 ts = type_size(&s->type, a);
3611 if (ts < 0 && s->c < 0)
3612 ts = -ts;
3614 return ts * s->c;
3615 } else {
3616 *a = PTR_SIZE;
3617 return PTR_SIZE;
3619 } else if (IS_ENUM(type->t) && type->ref->c < 0) {
3620 return -1; /* incomplete enum */
3621 } else if (bt == VT_LDOUBLE) {
3622 *a = LDOUBLE_ALIGN;
3623 return LDOUBLE_SIZE;
3624 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
3625 #ifdef TCC_TARGET_I386
3626 #ifdef TCC_TARGET_PE
3627 *a = 8;
3628 #else
3629 *a = 4;
3630 #endif
3631 #elif defined(TCC_TARGET_ARM)
3632 #ifdef TCC_ARM_EABI
3633 *a = 8;
3634 #else
3635 *a = 4;
3636 #endif
3637 #else
3638 *a = 8;
3639 #endif
3640 return 8;
3641 } else if (bt == VT_INT || bt == VT_FLOAT) {
3642 *a = 4;
3643 return 4;
3644 } else if (bt == VT_SHORT) {
3645 *a = 2;
3646 return 2;
3647 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
3648 *a = 8;
3649 return 16;
3650 } else {
3651 /* char, void, function, _Bool */
3652 *a = 1;
3653 return 1;
3657 /* push type size as known at runtime time on top of value stack. Put
3658 alignment at 'a' */
3659 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
3661 if (type->t & VT_VLA) {
3662 type_size(&type->ref->type, a);
3663 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
3664 } else {
3665 vpushi(type_size(type, a));
3669 /* return the pointed type of t */
3670 static inline CType *pointed_type(CType *type)
3672 return &type->ref->type;
3675 /* modify type so that its it is a pointer to type. */
3676 ST_FUNC void mk_pointer(CType *type)
3678 Sym *s;
3679 s = sym_push(SYM_FIELD, type, 0, -1);
3680 type->t = VT_PTR | (type->t & VT_STORAGE);
3681 type->ref = s;
3684 /* return true if type1 and type2 are exactly the same (including
3685 qualifiers).
3687 static int is_compatible_types(CType *type1, CType *type2)
3689 return compare_types(type1,type2,0);
3692 /* return true if type1 and type2 are the same (ignoring qualifiers).
3694 static int is_compatible_unqualified_types(CType *type1, CType *type2)
3696 return compare_types(type1,type2,1);
3699 static void cast_error(CType *st, CType *dt)
3701 type_incompatibility_error(st, dt, "cannot convert '%s' to '%s'");
3704 /* verify type compatibility to store vtop in 'dt' type */
3705 static void verify_assign_cast(CType *dt)
3707 CType *st, *type1, *type2;
3708 int dbt, sbt, qualwarn, lvl;
3710 st = &vtop->type; /* source type */
3711 dbt = dt->t & VT_BTYPE;
3712 sbt = st->t & VT_BTYPE;
3713 if (dt->t & VT_CONSTANT)
3714 tcc_warning("assignment of read-only location");
3715 switch(dbt) {
3716 case VT_VOID:
3717 if (sbt != dbt)
3718 tcc_error("assignment to void expression");
3719 break;
3720 case VT_PTR:
3721 /* special cases for pointers */
3722 /* '0' can also be a pointer */
3723 if (is_null_pointer(vtop))
3724 break;
3725 /* accept implicit pointer to integer cast with warning */
3726 if (is_integer_btype(sbt)) {
3727 tcc_warning("assignment makes pointer from integer without a cast");
3728 break;
3730 type1 = pointed_type(dt);
3731 if (sbt == VT_PTR)
3732 type2 = pointed_type(st);
3733 else if (sbt == VT_FUNC)
3734 type2 = st; /* a function is implicitly a function pointer */
3735 else
3736 goto error;
3737 if (is_compatible_types(type1, type2))
3738 break;
3739 for (qualwarn = lvl = 0;; ++lvl) {
3740 if (((type2->t & VT_CONSTANT) && !(type1->t & VT_CONSTANT)) ||
3741 ((type2->t & VT_VOLATILE) && !(type1->t & VT_VOLATILE)))
3742 qualwarn = 1;
3743 dbt = type1->t & (VT_BTYPE|VT_LONG);
3744 sbt = type2->t & (VT_BTYPE|VT_LONG);
3745 if (dbt != VT_PTR || sbt != VT_PTR)
3746 break;
3747 type1 = pointed_type(type1);
3748 type2 = pointed_type(type2);
3750 if (!is_compatible_unqualified_types(type1, type2)) {
3751 if ((dbt == VT_VOID || sbt == VT_VOID) && lvl == 0) {
3752 /* void * can match anything */
3753 } else if (dbt == sbt
3754 && is_integer_btype(sbt & VT_BTYPE)
3755 && IS_ENUM(type1->t) + IS_ENUM(type2->t)
3756 + !!((type1->t ^ type2->t) & VT_UNSIGNED) < 2) {
3757 /* Like GCC don't warn by default for merely changes
3758 in pointer target signedness. Do warn for different
3759 base types, though, in particular for unsigned enums
3760 and signed int targets. */
3761 } else {
3762 tcc_warning("assignment from incompatible pointer type");
3763 break;
3766 if (qualwarn)
3767 tcc_warning("assignment discards qualifiers from pointer target type");
3768 break;
3769 case VT_BYTE:
3770 case VT_SHORT:
3771 case VT_INT:
3772 case VT_LLONG:
3773 if (sbt == VT_PTR || sbt == VT_FUNC) {
3774 tcc_warning("assignment makes integer from pointer without a cast");
3775 } else if (sbt == VT_STRUCT) {
3776 goto case_VT_STRUCT;
3778 /* XXX: more tests */
3779 break;
3780 case VT_STRUCT:
3781 case_VT_STRUCT:
3782 if (!is_compatible_unqualified_types(dt, st)) {
3783 error:
3784 cast_error(st, dt);
3786 break;
3790 static void gen_assign_cast(CType *dt)
3792 verify_assign_cast(dt);
3793 gen_cast(dt);
3796 /* store vtop in lvalue pushed on stack */
3797 ST_FUNC void vstore(void)
3799 int sbt, dbt, ft, r, size, align, bit_size, bit_pos, delayed_cast;
3801 ft = vtop[-1].type.t;
3802 sbt = vtop->type.t & VT_BTYPE;
3803 dbt = ft & VT_BTYPE;
3805 verify_assign_cast(&vtop[-1].type);
3807 if (sbt == VT_STRUCT) {
3808 /* if structure, only generate pointer */
3809 /* structure assignment : generate memcpy */
3810 /* XXX: optimize if small size */
3811 size = type_size(&vtop->type, &align);
3813 /* destination */
3814 vswap();
3815 #ifdef CONFIG_TCC_BCHECK
3816 if (vtop->r & VT_MUSTBOUND)
3817 gbound(); /* check would be wrong after gaddrof() */
3818 #endif
3819 vtop->type.t = VT_PTR;
3820 gaddrof();
3822 /* address of memcpy() */
3823 #ifdef TCC_ARM_EABI
3824 if(!(align & 7))
3825 vpush_global_sym(&func_old_type, TOK_memmove8);
3826 else if(!(align & 3))
3827 vpush_global_sym(&func_old_type, TOK_memmove4);
3828 else
3829 #endif
3830 /* Use memmove, rather than memcpy, as dest and src may be same: */
3831 vpush_global_sym(&func_old_type, TOK_memmove);
3833 vswap();
3834 /* source */
3835 vpushv(vtop - 2);
3836 #ifdef CONFIG_TCC_BCHECK
3837 if (vtop->r & VT_MUSTBOUND)
3838 gbound();
3839 #endif
3840 vtop->type.t = VT_PTR;
3841 gaddrof();
3842 /* type size */
3843 vpushi(size);
3844 gfunc_call(3);
3845 /* leave source on stack */
3847 } else if (ft & VT_BITFIELD) {
3848 /* bitfield store handling */
3850 /* save lvalue as expression result (example: s.b = s.a = n;) */
3851 vdup(), vtop[-1] = vtop[-2];
3853 bit_pos = BIT_POS(ft);
3854 bit_size = BIT_SIZE(ft);
3855 /* remove bit field info to avoid loops */
3856 vtop[-1].type.t = ft & ~VT_STRUCT_MASK;
3858 if (dbt == VT_BOOL) {
3859 gen_cast(&vtop[-1].type);
3860 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
3862 r = adjust_bf(vtop - 1, bit_pos, bit_size);
3863 if (dbt != VT_BOOL) {
3864 gen_cast(&vtop[-1].type);
3865 dbt = vtop[-1].type.t & VT_BTYPE;
3867 if (r == VT_STRUCT) {
3868 store_packed_bf(bit_pos, bit_size);
3869 } else {
3870 unsigned long long mask = (1ULL << bit_size) - 1;
3871 if (dbt != VT_BOOL) {
3872 /* mask source */
3873 if (dbt == VT_LLONG)
3874 vpushll(mask);
3875 else
3876 vpushi((unsigned)mask);
3877 gen_op('&');
3879 /* shift source */
3880 vpushi(bit_pos);
3881 gen_op(TOK_SHL);
3882 vswap();
3883 /* duplicate destination */
3884 vdup();
3885 vrott(3);
3886 /* load destination, mask and or with source */
3887 if (dbt == VT_LLONG)
3888 vpushll(~(mask << bit_pos));
3889 else
3890 vpushi(~((unsigned)mask << bit_pos));
3891 gen_op('&');
3892 gen_op('|');
3893 /* store result */
3894 vstore();
3895 /* ... and discard */
3896 vpop();
3898 } else if (dbt == VT_VOID) {
3899 --vtop;
3900 } else {
3901 /* optimize char/short casts */
3902 delayed_cast = 0;
3903 if ((dbt == VT_BYTE || dbt == VT_SHORT)
3904 && is_integer_btype(sbt)
3906 if ((vtop->r & VT_MUSTCAST)
3907 && btype_size(dbt) > btype_size(sbt)
3909 force_charshort_cast();
3910 delayed_cast = 1;
3911 } else {
3912 gen_cast(&vtop[-1].type);
3915 #ifdef CONFIG_TCC_BCHECK
3916 /* bound check case */
3917 if (vtop[-1].r & VT_MUSTBOUND) {
3918 vswap();
3919 gbound();
3920 vswap();
3922 #endif
3923 gv(RC_TYPE(dbt)); /* generate value */
3925 if (delayed_cast) {
3926 vtop->r |= BFVAL(VT_MUSTCAST, (sbt == VT_LLONG) + 1);
3927 //tcc_warning("deley cast %x -> %x", sbt, dbt);
3928 vtop->type.t = ft & VT_TYPE;
3931 /* if lvalue was saved on stack, must read it */
3932 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
3933 SValue sv;
3934 r = get_reg(RC_INT);
3935 sv.type.t = VT_PTRDIFF_T;
3936 sv.r = VT_LOCAL | VT_LVAL;
3937 sv.c.i = vtop[-1].c.i;
3938 load(r, &sv);
3939 vtop[-1].r = r | VT_LVAL;
3942 r = vtop->r & VT_VALMASK;
3943 /* two word case handling :
3944 store second register at word + 4 (or +8 for x86-64) */
3945 if (USING_TWO_WORDS(dbt)) {
3946 int load_type = (dbt == VT_QFLOAT) ? VT_DOUBLE : VT_PTRDIFF_T;
3947 vtop[-1].type.t = load_type;
3948 store(r, vtop - 1);
3949 vswap();
3950 /* convert to int to increment easily */
3951 vtop->type.t = VT_PTRDIFF_T;
3952 gaddrof();
3953 vpushs(PTR_SIZE);
3954 gen_op('+');
3955 vtop->r |= VT_LVAL;
3956 vswap();
3957 vtop[-1].type.t = load_type;
3958 /* XXX: it works because r2 is spilled last ! */
3959 store(vtop->r2, vtop - 1);
3960 } else {
3961 /* single word */
3962 store(r, vtop - 1);
3964 vswap();
3965 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3969 /* post defines POST/PRE add. c is the token ++ or -- */
3970 ST_FUNC void inc(int post, int c)
3972 test_lvalue();
3973 vdup(); /* save lvalue */
3974 if (post) {
3975 gv_dup(); /* duplicate value */
3976 vrotb(3);
3977 vrotb(3);
3979 /* add constant */
3980 vpushi(c - TOK_MID);
3981 gen_op('+');
3982 vstore(); /* store value */
3983 if (post)
3984 vpop(); /* if post op, return saved value */
3987 ST_FUNC void parse_mult_str (CString *astr, const char *msg)
3989 /* read the string */
3990 if (tok != TOK_STR)
3991 expect(msg);
3992 cstr_new(astr);
3993 while (tok == TOK_STR) {
3994 /* XXX: add \0 handling too ? */
3995 cstr_cat(astr, tokc.str.data, -1);
3996 next();
3998 cstr_ccat(astr, '\0');
4001 /* If I is >= 1 and a power of two, returns log2(i)+1.
4002 If I is 0 returns 0. */
4003 ST_FUNC int exact_log2p1(int i)
4005 int ret;
4006 if (!i)
4007 return 0;
4008 for (ret = 1; i >= 1 << 8; ret += 8)
4009 i >>= 8;
4010 if (i >= 1 << 4)
4011 ret += 4, i >>= 4;
4012 if (i >= 1 << 2)
4013 ret += 2, i >>= 2;
4014 if (i >= 1 << 1)
4015 ret++;
4016 return ret;
4019 /* Parse __attribute__((...)) GNUC extension. */
4020 static void parse_attribute(AttributeDef *ad)
4022 int t, n;
4023 CString astr;
4025 redo:
4026 if (tok != TOK_ATTRIBUTE1 && tok != TOK_ATTRIBUTE2)
4027 return;
4028 next();
4029 skip('(');
4030 skip('(');
4031 while (tok != ')') {
4032 if (tok < TOK_IDENT)
4033 expect("attribute name");
4034 t = tok;
4035 next();
4036 switch(t) {
4037 case TOK_CLEANUP1:
4038 case TOK_CLEANUP2:
4040 Sym *s;
4042 skip('(');
4043 s = sym_find(tok);
4044 if (!s) {
4045 tcc_warning("implicit declaration of function '%s'",
4046 get_tok_str(tok, &tokc));
4047 s = external_global_sym(tok, &func_old_type);
4048 } else if ((s->type.t & VT_BTYPE) != VT_FUNC)
4049 tcc_error("'%s' is not declared as function", get_tok_str(tok, &tokc));
4050 ad->cleanup_func = s;
4051 next();
4052 skip(')');
4053 break;
4055 case TOK_CONSTRUCTOR1:
4056 case TOK_CONSTRUCTOR2:
4057 ad->f.func_ctor = 1;
4058 break;
4059 case TOK_DESTRUCTOR1:
4060 case TOK_DESTRUCTOR2:
4061 ad->f.func_dtor = 1;
4062 break;
4063 case TOK_ALWAYS_INLINE1:
4064 case TOK_ALWAYS_INLINE2:
4065 ad->f.func_alwinl = 1;
4066 break;
4067 case TOK_SECTION1:
4068 case TOK_SECTION2:
4069 skip('(');
4070 parse_mult_str(&astr, "section name");
4071 ad->section = find_section(tcc_state, (char *)astr.data);
4072 skip(')');
4073 cstr_free(&astr);
4074 break;
4075 case TOK_ALIAS1:
4076 case TOK_ALIAS2:
4077 skip('(');
4078 parse_mult_str(&astr, "alias(\"target\")");
4079 ad->asm_label = /* save string as token, for later */
4080 tok_alloc((char*)astr.data, astr.size-1)->tok | SYM_FIELD;
4081 skip(')');
4082 cstr_free(&astr);
4083 break;
4084 case TOK_VISIBILITY1:
4085 case TOK_VISIBILITY2:
4086 skip('(');
4087 parse_mult_str(&astr,
4088 "visibility(\"default|hidden|internal|protected\")");
4089 if (!strcmp (astr.data, "default"))
4090 ad->a.visibility = STV_DEFAULT;
4091 else if (!strcmp (astr.data, "hidden"))
4092 ad->a.visibility = STV_HIDDEN;
4093 else if (!strcmp (astr.data, "internal"))
4094 ad->a.visibility = STV_INTERNAL;
4095 else if (!strcmp (astr.data, "protected"))
4096 ad->a.visibility = STV_PROTECTED;
4097 else
4098 expect("visibility(\"default|hidden|internal|protected\")");
4099 skip(')');
4100 cstr_free(&astr);
4101 break;
4102 case TOK_ALIGNED1:
4103 case TOK_ALIGNED2:
4104 if (tok == '(') {
4105 next();
4106 n = expr_const();
4107 if (n <= 0 || (n & (n - 1)) != 0)
4108 tcc_error("alignment must be a positive power of two");
4109 skip(')');
4110 } else {
4111 n = MAX_ALIGN;
4113 ad->a.aligned = exact_log2p1(n);
4114 if (n != 1 << (ad->a.aligned - 1))
4115 tcc_error("alignment of %d is larger than implemented", n);
4116 break;
4117 case TOK_PACKED1:
4118 case TOK_PACKED2:
4119 ad->a.packed = 1;
4120 break;
4121 case TOK_WEAK1:
4122 case TOK_WEAK2:
4123 ad->a.weak = 1;
4124 break;
4125 case TOK_UNUSED1:
4126 case TOK_UNUSED2:
4127 /* currently, no need to handle it because tcc does not
4128 track unused objects */
4129 break;
4130 case TOK_NORETURN1:
4131 case TOK_NORETURN2:
4132 ad->f.func_noreturn = 1;
4133 break;
4134 case TOK_CDECL1:
4135 case TOK_CDECL2:
4136 case TOK_CDECL3:
4137 ad->f.func_call = FUNC_CDECL;
4138 break;
4139 case TOK_STDCALL1:
4140 case TOK_STDCALL2:
4141 case TOK_STDCALL3:
4142 ad->f.func_call = FUNC_STDCALL;
4143 break;
4144 #ifdef TCC_TARGET_I386
4145 case TOK_REGPARM1:
4146 case TOK_REGPARM2:
4147 skip('(');
4148 n = expr_const();
4149 if (n > 3)
4150 n = 3;
4151 else if (n < 0)
4152 n = 0;
4153 if (n > 0)
4154 ad->f.func_call = FUNC_FASTCALL1 + n - 1;
4155 skip(')');
4156 break;
4157 case TOK_FASTCALL1:
4158 case TOK_FASTCALL2:
4159 case TOK_FASTCALL3:
4160 ad->f.func_call = FUNC_FASTCALLW;
4161 break;
4162 #endif
4163 case TOK_MODE:
4164 skip('(');
4165 switch(tok) {
4166 case TOK_MODE_DI:
4167 ad->attr_mode = VT_LLONG + 1;
4168 break;
4169 case TOK_MODE_QI:
4170 ad->attr_mode = VT_BYTE + 1;
4171 break;
4172 case TOK_MODE_HI:
4173 ad->attr_mode = VT_SHORT + 1;
4174 break;
4175 case TOK_MODE_SI:
4176 case TOK_MODE_word:
4177 ad->attr_mode = VT_INT + 1;
4178 break;
4179 default:
4180 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
4181 break;
4183 next();
4184 skip(')');
4185 break;
4186 case TOK_DLLEXPORT:
4187 ad->a.dllexport = 1;
4188 break;
4189 case TOK_NODECORATE:
4190 ad->a.nodecorate = 1;
4191 break;
4192 case TOK_DLLIMPORT:
4193 ad->a.dllimport = 1;
4194 break;
4195 default:
4196 if (tcc_state->warn_unsupported)
4197 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
4198 /* skip parameters */
4199 if (tok == '(') {
4200 int parenthesis = 0;
4201 do {
4202 if (tok == '(')
4203 parenthesis++;
4204 else if (tok == ')')
4205 parenthesis--;
4206 next();
4207 } while (parenthesis && tok != -1);
4209 break;
4211 if (tok != ',')
4212 break;
4213 next();
4215 skip(')');
4216 skip(')');
4217 goto redo;
4220 static Sym * find_field (CType *type, int v, int *cumofs)
4222 Sym *s = type->ref;
4223 v |= SYM_FIELD;
4224 while ((s = s->next) != NULL) {
4225 if ((s->v & SYM_FIELD) &&
4226 (s->type.t & VT_BTYPE) == VT_STRUCT &&
4227 (s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
4228 Sym *ret = find_field (&s->type, v, cumofs);
4229 if (ret) {
4230 *cumofs += s->c;
4231 return ret;
4234 if (s->v == v)
4235 break;
4237 return s;
4240 static void struct_layout(CType *type, AttributeDef *ad)
4242 int size, align, maxalign, offset, c, bit_pos, bit_size;
4243 int packed, a, bt, prevbt, prev_bit_size;
4244 int pcc = !tcc_state->ms_bitfields;
4245 int pragma_pack = *tcc_state->pack_stack_ptr;
4246 Sym *f;
4248 maxalign = 1;
4249 offset = 0;
4250 c = 0;
4251 bit_pos = 0;
4252 prevbt = VT_STRUCT; /* make it never match */
4253 prev_bit_size = 0;
4255 //#define BF_DEBUG
4257 for (f = type->ref->next; f; f = f->next) {
4258 if (f->type.t & VT_BITFIELD)
4259 bit_size = BIT_SIZE(f->type.t);
4260 else
4261 bit_size = -1;
4262 size = type_size(&f->type, &align);
4263 a = f->a.aligned ? 1 << (f->a.aligned - 1) : 0;
4264 packed = 0;
4266 if (pcc && bit_size == 0) {
4267 /* in pcc mode, packing does not affect zero-width bitfields */
4269 } else {
4270 /* in pcc mode, attribute packed overrides if set. */
4271 if (pcc && (f->a.packed || ad->a.packed))
4272 align = packed = 1;
4274 /* pragma pack overrides align if lesser and packs bitfields always */
4275 if (pragma_pack) {
4276 packed = 1;
4277 if (pragma_pack < align)
4278 align = pragma_pack;
4279 /* in pcc mode pragma pack also overrides individual align */
4280 if (pcc && pragma_pack < a)
4281 a = 0;
4284 /* some individual align was specified */
4285 if (a)
4286 align = a;
4288 if (type->ref->type.t == VT_UNION) {
4289 if (pcc && bit_size >= 0)
4290 size = (bit_size + 7) >> 3;
4291 offset = 0;
4292 if (size > c)
4293 c = size;
4295 } else if (bit_size < 0) {
4296 if (pcc)
4297 c += (bit_pos + 7) >> 3;
4298 c = (c + align - 1) & -align;
4299 offset = c;
4300 if (size > 0)
4301 c += size;
4302 bit_pos = 0;
4303 prevbt = VT_STRUCT;
4304 prev_bit_size = 0;
4306 } else {
4307 /* A bit-field. Layout is more complicated. There are two
4308 options: PCC (GCC) compatible and MS compatible */
4309 if (pcc) {
4310 /* In PCC layout a bit-field is placed adjacent to the
4311 preceding bit-fields, except if:
4312 - it has zero-width
4313 - an individual alignment was given
4314 - it would overflow its base type container and
4315 there is no packing */
4316 if (bit_size == 0) {
4317 new_field:
4318 c = (c + ((bit_pos + 7) >> 3) + align - 1) & -align;
4319 bit_pos = 0;
4320 } else if (f->a.aligned) {
4321 goto new_field;
4322 } else if (!packed) {
4323 int a8 = align * 8;
4324 int ofs = ((c * 8 + bit_pos) % a8 + bit_size + a8 - 1) / a8;
4325 if (ofs > size / align)
4326 goto new_field;
4329 /* in pcc mode, long long bitfields have type int if they fit */
4330 if (size == 8 && bit_size <= 32)
4331 f->type.t = (f->type.t & ~VT_BTYPE) | VT_INT, size = 4;
4333 while (bit_pos >= align * 8)
4334 c += align, bit_pos -= align * 8;
4335 offset = c;
4337 /* In PCC layout named bit-fields influence the alignment
4338 of the containing struct using the base types alignment,
4339 except for packed fields (which here have correct align). */
4340 if (f->v & SYM_FIRST_ANOM
4341 // && bit_size // ??? gcc on ARM/rpi does that
4343 align = 1;
4345 } else {
4346 bt = f->type.t & VT_BTYPE;
4347 if ((bit_pos + bit_size > size * 8)
4348 || (bit_size > 0) == (bt != prevbt)
4350 c = (c + align - 1) & -align;
4351 offset = c;
4352 bit_pos = 0;
4353 /* In MS bitfield mode a bit-field run always uses
4354 at least as many bits as the underlying type.
4355 To start a new run it's also required that this
4356 or the last bit-field had non-zero width. */
4357 if (bit_size || prev_bit_size)
4358 c += size;
4360 /* In MS layout the records alignment is normally
4361 influenced by the field, except for a zero-width
4362 field at the start of a run (but by further zero-width
4363 fields it is again). */
4364 if (bit_size == 0 && prevbt != bt)
4365 align = 1;
4366 prevbt = bt;
4367 prev_bit_size = bit_size;
4370 f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
4371 | (bit_pos << VT_STRUCT_SHIFT);
4372 bit_pos += bit_size;
4374 if (align > maxalign)
4375 maxalign = align;
4377 #ifdef BF_DEBUG
4378 printf("set field %s offset %-2d size %-2d align %-2d",
4379 get_tok_str(f->v & ~SYM_FIELD, NULL), offset, size, align);
4380 if (f->type.t & VT_BITFIELD) {
4381 printf(" pos %-2d bits %-2d",
4382 BIT_POS(f->type.t),
4383 BIT_SIZE(f->type.t)
4386 printf("\n");
4387 #endif
4389 f->c = offset;
4390 f->r = 0;
4393 if (pcc)
4394 c += (bit_pos + 7) >> 3;
4396 /* store size and alignment */
4397 a = bt = ad->a.aligned ? 1 << (ad->a.aligned - 1) : 1;
4398 if (a < maxalign)
4399 a = maxalign;
4400 type->ref->r = a;
4401 if (pragma_pack && pragma_pack < maxalign && 0 == pcc) {
4402 /* can happen if individual align for some member was given. In
4403 this case MSVC ignores maxalign when aligning the size */
4404 a = pragma_pack;
4405 if (a < bt)
4406 a = bt;
4408 c = (c + a - 1) & -a;
4409 type->ref->c = c;
4411 #ifdef BF_DEBUG
4412 printf("struct size %-2d align %-2d\n\n", c, a), fflush(stdout);
4413 #endif
4415 /* check whether we can access bitfields by their type */
4416 for (f = type->ref->next; f; f = f->next) {
4417 int s, px, cx, c0;
4418 CType t;
4420 if (0 == (f->type.t & VT_BITFIELD))
4421 continue;
4422 f->type.ref = f;
4423 f->auxtype = -1;
4424 bit_size = BIT_SIZE(f->type.t);
4425 if (bit_size == 0)
4426 continue;
4427 bit_pos = BIT_POS(f->type.t);
4428 size = type_size(&f->type, &align);
4429 if (bit_pos + bit_size <= size * 8 && f->c + size <= c)
4430 continue;
4432 /* try to access the field using a different type */
4433 c0 = -1, s = align = 1;
4434 t.t = VT_BYTE;
4435 for (;;) {
4436 px = f->c * 8 + bit_pos;
4437 cx = (px >> 3) & -align;
4438 px = px - (cx << 3);
4439 if (c0 == cx)
4440 break;
4441 s = (px + bit_size + 7) >> 3;
4442 if (s > 4) {
4443 t.t = VT_LLONG;
4444 } else if (s > 2) {
4445 t.t = VT_INT;
4446 } else if (s > 1) {
4447 t.t = VT_SHORT;
4448 } else {
4449 t.t = VT_BYTE;
4451 s = type_size(&t, &align);
4452 c0 = cx;
4455 if (px + bit_size <= s * 8 && cx + s <= c) {
4456 /* update offset and bit position */
4457 f->c = cx;
4458 bit_pos = px;
4459 f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
4460 | (bit_pos << VT_STRUCT_SHIFT);
4461 if (s != size)
4462 f->auxtype = t.t;
4463 #ifdef BF_DEBUG
4464 printf("FIX field %s offset %-2d size %-2d align %-2d "
4465 "pos %-2d bits %-2d\n",
4466 get_tok_str(f->v & ~SYM_FIELD, NULL),
4467 cx, s, align, px, bit_size);
4468 #endif
4469 } else {
4470 /* fall back to load/store single-byte wise */
4471 f->auxtype = VT_STRUCT;
4472 #ifdef BF_DEBUG
4473 printf("FIX field %s : load byte-wise\n",
4474 get_tok_str(f->v & ~SYM_FIELD, NULL));
4475 #endif
4480 /* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
4481 static void struct_decl(CType *type, int u)
4483 int v, c, size, align, flexible;
4484 int bit_size, bsize, bt;
4485 Sym *s, *ss, **ps;
4486 AttributeDef ad, ad1;
4487 CType type1, btype;
4489 memset(&ad, 0, sizeof ad);
4490 next();
4491 parse_attribute(&ad);
4492 if (tok != '{') {
4493 v = tok;
4494 next();
4495 /* struct already defined ? return it */
4496 if (v < TOK_IDENT)
4497 expect("struct/union/enum name");
4498 s = struct_find(v);
4499 if (s && (s->sym_scope == local_scope || tok != '{')) {
4500 if (u == s->type.t)
4501 goto do_decl;
4502 if (u == VT_ENUM && IS_ENUM(s->type.t))
4503 goto do_decl;
4504 tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
4506 } else {
4507 v = anon_sym++;
4509 /* Record the original enum/struct/union token. */
4510 type1.t = u == VT_ENUM ? u | VT_INT | VT_UNSIGNED : u;
4511 type1.ref = NULL;
4512 /* we put an undefined size for struct/union */
4513 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
4514 s->r = 0; /* default alignment is zero as gcc */
4515 do_decl:
4516 type->t = s->type.t;
4517 type->ref = s;
4519 if (tok == '{') {
4520 next();
4521 if (s->c != -1)
4522 tcc_error("struct/union/enum already defined");
4523 s->c = -2;
4524 /* cannot be empty */
4525 /* non empty enums are not allowed */
4526 ps = &s->next;
4527 if (u == VT_ENUM) {
4528 long long ll = 0, pl = 0, nl = 0;
4529 CType t;
4530 t.ref = s;
4531 /* enum symbols have static storage */
4532 t.t = VT_INT|VT_STATIC|VT_ENUM_VAL;
4533 for(;;) {
4534 v = tok;
4535 if (v < TOK_UIDENT)
4536 expect("identifier");
4537 ss = sym_find(v);
4538 if (ss && !local_stack)
4539 tcc_error("redefinition of enumerator '%s'",
4540 get_tok_str(v, NULL));
4541 next();
4542 if (tok == '=') {
4543 next();
4544 ll = expr_const64();
4546 ss = sym_push(v, &t, VT_CONST, 0);
4547 ss->enum_val = ll;
4548 *ps = ss, ps = &ss->next;
4549 if (ll < nl)
4550 nl = ll;
4551 if (ll > pl)
4552 pl = ll;
4553 if (tok != ',')
4554 break;
4555 next();
4556 ll++;
4557 /* NOTE: we accept a trailing comma */
4558 if (tok == '}')
4559 break;
4561 skip('}');
4562 /* set integral type of the enum */
4563 t.t = VT_INT;
4564 if (nl >= 0) {
4565 if (pl != (unsigned)pl)
4566 t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
4567 t.t |= VT_UNSIGNED;
4568 } else if (pl != (int)pl || nl != (int)nl)
4569 t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
4570 s->type.t = type->t = t.t | VT_ENUM;
4571 s->c = 0;
4572 /* set type for enum members */
4573 for (ss = s->next; ss; ss = ss->next) {
4574 ll = ss->enum_val;
4575 if (ll == (int)ll) /* default is int if it fits */
4576 continue;
4577 if (t.t & VT_UNSIGNED) {
4578 ss->type.t |= VT_UNSIGNED;
4579 if (ll == (unsigned)ll)
4580 continue;
4582 ss->type.t = (ss->type.t & ~VT_BTYPE)
4583 | (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
4585 } else {
4586 c = 0;
4587 flexible = 0;
4588 while (tok != '}') {
4589 if (!parse_btype(&btype, &ad1)) {
4590 skip(';');
4591 continue;
4593 while (1) {
4594 if (flexible)
4595 tcc_error("flexible array member '%s' not at the end of struct",
4596 get_tok_str(v, NULL));
4597 bit_size = -1;
4598 v = 0;
4599 type1 = btype;
4600 if (tok != ':') {
4601 if (tok != ';')
4602 type_decl(&type1, &ad1, &v, TYPE_DIRECT);
4603 if (v == 0) {
4604 if ((type1.t & VT_BTYPE) != VT_STRUCT)
4605 expect("identifier");
4606 else {
4607 int v = btype.ref->v;
4608 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
4609 if (tcc_state->ms_extensions == 0)
4610 expect("identifier");
4614 if (type_size(&type1, &align) < 0) {
4615 if ((u == VT_STRUCT) && (type1.t & VT_ARRAY) && c)
4616 flexible = 1;
4617 else
4618 tcc_error("field '%s' has incomplete type",
4619 get_tok_str(v, NULL));
4621 if ((type1.t & VT_BTYPE) == VT_FUNC ||
4622 (type1.t & VT_BTYPE) == VT_VOID ||
4623 (type1.t & VT_STORAGE))
4624 tcc_error("invalid type for '%s'",
4625 get_tok_str(v, NULL));
4627 if (tok == ':') {
4628 next();
4629 bit_size = expr_const();
4630 /* XXX: handle v = 0 case for messages */
4631 if (bit_size < 0)
4632 tcc_error("negative width in bit-field '%s'",
4633 get_tok_str(v, NULL));
4634 if (v && bit_size == 0)
4635 tcc_error("zero width for bit-field '%s'",
4636 get_tok_str(v, NULL));
4637 parse_attribute(&ad1);
4639 size = type_size(&type1, &align);
4640 if (bit_size >= 0) {
4641 bt = type1.t & VT_BTYPE;
4642 if (bt != VT_INT &&
4643 bt != VT_BYTE &&
4644 bt != VT_SHORT &&
4645 bt != VT_BOOL &&
4646 bt != VT_LLONG)
4647 tcc_error("bitfields must have scalar type");
4648 bsize = size * 8;
4649 if (bit_size > bsize) {
4650 tcc_error("width of '%s' exceeds its type",
4651 get_tok_str(v, NULL));
4652 } else if (bit_size == bsize
4653 && !ad.a.packed && !ad1.a.packed) {
4654 /* no need for bit fields */
4656 } else if (bit_size == 64) {
4657 tcc_error("field width 64 not implemented");
4658 } else {
4659 type1.t = (type1.t & ~VT_STRUCT_MASK)
4660 | VT_BITFIELD
4661 | (bit_size << (VT_STRUCT_SHIFT + 6));
4664 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
4665 /* Remember we've seen a real field to check
4666 for placement of flexible array member. */
4667 c = 1;
4669 /* If member is a struct or bit-field, enforce
4670 placing into the struct (as anonymous). */
4671 if (v == 0 &&
4672 ((type1.t & VT_BTYPE) == VT_STRUCT ||
4673 bit_size >= 0)) {
4674 v = anon_sym++;
4676 if (v) {
4677 ss = sym_push(v | SYM_FIELD, &type1, 0, 0);
4678 ss->a = ad1.a;
4679 *ps = ss;
4680 ps = &ss->next;
4682 if (tok == ';' || tok == TOK_EOF)
4683 break;
4684 skip(',');
4686 skip(';');
4688 skip('}');
4689 parse_attribute(&ad);
4690 if (ad.cleanup_func) {
4691 tcc_warning("attribute '__cleanup__' ignored on type");
4693 struct_layout(type, &ad);
4698 static void sym_to_attr(AttributeDef *ad, Sym *s)
4700 merge_symattr(&ad->a, &s->a);
4701 merge_funcattr(&ad->f, &s->f);
4704 /* Add type qualifiers to a type. If the type is an array then the qualifiers
4705 are added to the element type, copied because it could be a typedef. */
4706 static void parse_btype_qualify(CType *type, int qualifiers)
4708 while (type->t & VT_ARRAY) {
4709 type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
4710 type = &type->ref->type;
4712 type->t |= qualifiers;
4715 /* return 0 if no type declaration. otherwise, return the basic type
4716 and skip it.
4718 static int parse_btype(CType *type, AttributeDef *ad)
4720 int t, u, bt, st, type_found, typespec_found, g, n;
4721 Sym *s;
4722 CType type1;
4724 memset(ad, 0, sizeof(AttributeDef));
4725 type_found = 0;
4726 typespec_found = 0;
4727 t = VT_INT;
4728 bt = st = -1;
4729 type->ref = NULL;
4731 while(1) {
4732 switch(tok) {
4733 case TOK_EXTENSION:
4734 /* currently, we really ignore extension */
4735 next();
4736 continue;
4738 /* basic types */
4739 case TOK_CHAR:
4740 u = VT_BYTE;
4741 basic_type:
4742 next();
4743 basic_type1:
4744 if (u == VT_SHORT || u == VT_LONG) {
4745 if (st != -1 || (bt != -1 && bt != VT_INT))
4746 tmbt: tcc_error("too many basic types");
4747 st = u;
4748 } else {
4749 if (bt != -1 || (st != -1 && u != VT_INT))
4750 goto tmbt;
4751 bt = u;
4753 if (u != VT_INT)
4754 t = (t & ~(VT_BTYPE|VT_LONG)) | u;
4755 typespec_found = 1;
4756 break;
4757 case TOK_VOID:
4758 u = VT_VOID;
4759 goto basic_type;
4760 case TOK_SHORT:
4761 u = VT_SHORT;
4762 goto basic_type;
4763 case TOK_INT:
4764 u = VT_INT;
4765 goto basic_type;
4766 case TOK_ALIGNAS:
4767 { int n;
4768 AttributeDef ad1;
4769 next();
4770 skip('(');
4771 memset(&ad1, 0, sizeof(AttributeDef));
4772 if (parse_btype(&type1, &ad1)) {
4773 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4774 if (ad1.a.aligned)
4775 n = 1 << (ad1.a.aligned - 1);
4776 else
4777 type_size(&type1, &n);
4778 } else {
4779 n = expr_const();
4780 if (n <= 0 || (n & (n - 1)) != 0)
4781 tcc_error("alignment must be a positive power of two");
4783 skip(')');
4784 ad->a.aligned = exact_log2p1(n);
4786 continue;
4787 case TOK_LONG:
4788 if ((t & VT_BTYPE) == VT_DOUBLE) {
4789 t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
4790 } else if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
4791 t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LLONG;
4792 } else {
4793 u = VT_LONG;
4794 goto basic_type;
4796 next();
4797 break;
4798 #ifdef TCC_TARGET_ARM64
4799 case TOK_UINT128:
4800 /* GCC's __uint128_t appears in some Linux header files. Make it a
4801 synonym for long double to get the size and alignment right. */
4802 u = VT_LDOUBLE;
4803 goto basic_type;
4804 #endif
4805 case TOK_BOOL:
4806 u = VT_BOOL;
4807 goto basic_type;
4808 case TOK_FLOAT:
4809 u = VT_FLOAT;
4810 goto basic_type;
4811 case TOK_DOUBLE:
4812 if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
4813 t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
4814 } else {
4815 u = VT_DOUBLE;
4816 goto basic_type;
4818 next();
4819 break;
4820 case TOK_ENUM:
4821 struct_decl(&type1, VT_ENUM);
4822 basic_type2:
4823 u = type1.t;
4824 type->ref = type1.ref;
4825 goto basic_type1;
4826 case TOK_STRUCT:
4827 struct_decl(&type1, VT_STRUCT);
4828 goto basic_type2;
4829 case TOK_UNION:
4830 struct_decl(&type1, VT_UNION);
4831 goto basic_type2;
4833 /* type modifiers */
4834 case TOK_CONST1:
4835 case TOK_CONST2:
4836 case TOK_CONST3:
4837 type->t = t;
4838 parse_btype_qualify(type, VT_CONSTANT);
4839 t = type->t;
4840 next();
4841 break;
4842 case TOK_VOLATILE1:
4843 case TOK_VOLATILE2:
4844 case TOK_VOLATILE3:
4845 type->t = t;
4846 parse_btype_qualify(type, VT_VOLATILE);
4847 t = type->t;
4848 next();
4849 break;
4850 case TOK_SIGNED1:
4851 case TOK_SIGNED2:
4852 case TOK_SIGNED3:
4853 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
4854 tcc_error("signed and unsigned modifier");
4855 t |= VT_DEFSIGN;
4856 next();
4857 typespec_found = 1;
4858 break;
4859 case TOK_REGISTER:
4860 case TOK_AUTO:
4861 case TOK_RESTRICT1:
4862 case TOK_RESTRICT2:
4863 case TOK_RESTRICT3:
4864 next();
4865 break;
4866 case TOK_UNSIGNED:
4867 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
4868 tcc_error("signed and unsigned modifier");
4869 t |= VT_DEFSIGN | VT_UNSIGNED;
4870 next();
4871 typespec_found = 1;
4872 break;
4874 /* storage */
4875 case TOK_EXTERN:
4876 g = VT_EXTERN;
4877 goto storage;
4878 case TOK_STATIC:
4879 g = VT_STATIC;
4880 goto storage;
4881 case TOK_TYPEDEF:
4882 g = VT_TYPEDEF;
4883 goto storage;
4884 storage:
4885 if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g)
4886 tcc_error("multiple storage classes");
4887 t |= g;
4888 next();
4889 break;
4890 case TOK_INLINE1:
4891 case TOK_INLINE2:
4892 case TOK_INLINE3:
4893 t |= VT_INLINE;
4894 next();
4895 break;
4896 case TOK_NORETURN3:
4897 next();
4898 ad->f.func_noreturn = 1;
4899 break;
4900 /* GNUC attribute */
4901 case TOK_ATTRIBUTE1:
4902 case TOK_ATTRIBUTE2:
4903 parse_attribute(ad);
4904 if (ad->attr_mode) {
4905 u = ad->attr_mode -1;
4906 t = (t & ~(VT_BTYPE|VT_LONG)) | u;
4908 continue;
4909 /* GNUC typeof */
4910 case TOK_TYPEOF1:
4911 case TOK_TYPEOF2:
4912 case TOK_TYPEOF3:
4913 next();
4914 parse_expr_type(&type1);
4915 /* remove all storage modifiers except typedef */
4916 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
4917 if (type1.ref)
4918 sym_to_attr(ad, type1.ref);
4919 goto basic_type2;
4920 default:
4921 if (typespec_found)
4922 goto the_end;
4923 s = sym_find(tok);
4924 if (!s || !(s->type.t & VT_TYPEDEF))
4925 goto the_end;
4927 n = tok, next();
4928 if (tok == ':' && !in_generic) {
4929 /* ignore if it's a label */
4930 unget_tok(n);
4931 goto the_end;
4934 t &= ~(VT_BTYPE|VT_LONG);
4935 u = t & ~(VT_CONSTANT | VT_VOLATILE), t ^= u;
4936 type->t = (s->type.t & ~VT_TYPEDEF) | u;
4937 type->ref = s->type.ref;
4938 if (t)
4939 parse_btype_qualify(type, t);
4940 t = type->t;
4941 /* get attributes from typedef */
4942 sym_to_attr(ad, s);
4943 typespec_found = 1;
4944 st = bt = -2;
4945 break;
4947 type_found = 1;
4949 the_end:
4950 if (tcc_state->char_is_unsigned) {
4951 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
4952 t |= VT_UNSIGNED;
4954 /* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
4955 bt = t & (VT_BTYPE|VT_LONG);
4956 if (bt == VT_LONG)
4957 t |= LONG_SIZE == 8 ? VT_LLONG : VT_INT;
4958 #if defined TCC_TARGET_PE || (defined _WIN32 && defined _MSC_VER)
4959 if (bt == VT_LDOUBLE)
4960 t = (t & ~(VT_BTYPE|VT_LONG)) | (VT_DOUBLE|VT_LONG);
4961 #endif
4962 type->t = t;
4963 return type_found;
4966 /* convert a function parameter type (array to pointer and function to
4967 function pointer) */
4968 static inline void convert_parameter_type(CType *pt)
4970 /* remove const and volatile qualifiers (XXX: const could be used
4971 to indicate a const function parameter */
4972 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
4973 /* array must be transformed to pointer according to ANSI C */
4974 pt->t &= ~VT_ARRAY;
4975 if ((pt->t & VT_BTYPE) == VT_FUNC) {
4976 mk_pointer(pt);
4980 ST_FUNC void parse_asm_str(CString *astr)
4982 skip('(');
4983 parse_mult_str(astr, "string constant");
4986 /* Parse an asm label and return the token */
4987 static int asm_label_instr(void)
4989 int v;
4990 CString astr;
4992 next();
4993 parse_asm_str(&astr);
4994 skip(')');
4995 #ifdef ASM_DEBUG
4996 printf("asm_alias: \"%s\"\n", (char *)astr.data);
4997 #endif
4998 v = tok_alloc(astr.data, astr.size - 1)->tok;
4999 cstr_free(&astr);
5000 return v;
5003 static int post_type(CType *type, AttributeDef *ad, int storage, int td)
5005 int n, l, t1, arg_size, align, unused_align;
5006 Sym **plast, *s, *first;
5007 AttributeDef ad1;
5008 CType pt;
5010 if (tok == '(') {
5011 /* function type, or recursive declarator (return if so) */
5012 next();
5013 if (td && !(td & TYPE_ABSTRACT))
5014 return 0;
5015 if (tok == ')')
5016 l = 0;
5017 else if (parse_btype(&pt, &ad1))
5018 l = FUNC_NEW;
5019 else if (td) {
5020 merge_attr (ad, &ad1);
5021 return 0;
5022 } else
5023 l = FUNC_OLD;
5024 first = NULL;
5025 plast = &first;
5026 arg_size = 0;
5027 if (l) {
5028 for(;;) {
5029 /* read param name and compute offset */
5030 if (l != FUNC_OLD) {
5031 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
5032 break;
5033 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
5034 if ((pt.t & VT_BTYPE) == VT_VOID)
5035 tcc_error("parameter declared as void");
5036 } else {
5037 n = tok;
5038 if (n < TOK_UIDENT)
5039 expect("identifier");
5040 pt.t = VT_VOID; /* invalid type */
5041 pt.ref = NULL;
5042 next();
5044 convert_parameter_type(&pt);
5045 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
5046 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
5047 *plast = s;
5048 plast = &s->next;
5049 if (tok == ')')
5050 break;
5051 skip(',');
5052 if (l == FUNC_NEW && tok == TOK_DOTS) {
5053 l = FUNC_ELLIPSIS;
5054 next();
5055 break;
5057 if (l == FUNC_NEW && !parse_btype(&pt, &ad1))
5058 tcc_error("invalid type");
5060 } else
5061 /* if no parameters, then old type prototype */
5062 l = FUNC_OLD;
5063 skip(')');
5064 /* NOTE: const is ignored in returned type as it has a special
5065 meaning in gcc / C++ */
5066 type->t &= ~VT_CONSTANT;
5067 /* some ancient pre-K&R C allows a function to return an array
5068 and the array brackets to be put after the arguments, such
5069 that "int c()[]" means something like "int[] c()" */
5070 if (tok == '[') {
5071 next();
5072 skip(']'); /* only handle simple "[]" */
5073 mk_pointer(type);
5075 /* we push a anonymous symbol which will contain the function prototype */
5076 ad->f.func_args = arg_size;
5077 ad->f.func_type = l;
5078 s = sym_push(SYM_FIELD, type, 0, 0);
5079 s->a = ad->a;
5080 s->f = ad->f;
5081 s->next = first;
5082 type->t = VT_FUNC;
5083 type->ref = s;
5084 } else if (tok == '[') {
5085 int saved_nocode_wanted = nocode_wanted;
5086 /* array definition */
5087 next();
5088 while (1) {
5089 /* XXX The optional type-quals and static should only be accepted
5090 in parameter decls. The '*' as well, and then even only
5091 in prototypes (not function defs). */
5092 switch (tok) {
5093 case TOK_RESTRICT1: case TOK_RESTRICT2: case TOK_RESTRICT3:
5094 case TOK_CONST1:
5095 case TOK_VOLATILE1:
5096 case TOK_STATIC:
5097 case '*':
5098 next();
5099 continue;
5100 default:
5101 break;
5103 break;
5105 n = -1;
5106 t1 = 0;
5107 if (tok != ']') {
5108 if (!local_stack || (storage & VT_STATIC))
5109 vpushi(expr_const());
5110 else {
5111 /* VLAs (which can only happen with local_stack && !VT_STATIC)
5112 length must always be evaluated, even under nocode_wanted,
5113 so that its size slot is initialized (e.g. under sizeof
5114 or typeof). */
5115 nocode_wanted = 0;
5116 gexpr();
5118 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5119 n = vtop->c.i;
5120 if (n < 0)
5121 tcc_error("invalid array size");
5122 } else {
5123 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
5124 tcc_error("size of variable length array should be an integer");
5125 n = 0;
5126 t1 = VT_VLA;
5129 skip(']');
5130 /* parse next post type */
5131 post_type(type, ad, storage, 0);
5133 if ((type->t & VT_BTYPE) == VT_FUNC)
5134 tcc_error("declaration of an array of functions");
5135 if ((type->t & VT_BTYPE) == VT_VOID
5136 || type_size(type, &unused_align) < 0)
5137 tcc_error("declaration of an array of incomplete type elements");
5139 t1 |= type->t & VT_VLA;
5141 if (t1 & VT_VLA) {
5142 if (n < 0)
5143 tcc_error("need explicit inner array size in VLAs");
5144 loc -= type_size(&int_type, &align);
5145 loc &= -align;
5146 n = loc;
5148 vla_runtime_type_size(type, &align);
5149 gen_op('*');
5150 vset(&int_type, VT_LOCAL|VT_LVAL, n);
5151 vswap();
5152 vstore();
5154 if (n != -1)
5155 vpop();
5156 nocode_wanted = saved_nocode_wanted;
5158 /* we push an anonymous symbol which will contain the array
5159 element type */
5160 s = sym_push(SYM_FIELD, type, 0, n);
5161 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
5162 type->ref = s;
5164 return 1;
5167 /* Parse a type declarator (except basic type), and return the type
5168 in 'type'. 'td' is a bitmask indicating which kind of type decl is
5169 expected. 'type' should contain the basic type. 'ad' is the
5170 attribute definition of the basic type. It can be modified by
5171 type_decl(). If this (possibly abstract) declarator is a pointer chain
5172 it returns the innermost pointed to type (equals *type, but is a different
5173 pointer), otherwise returns type itself, that's used for recursive calls. */
5174 static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td)
5176 CType *post, *ret;
5177 int qualifiers, storage;
5179 /* recursive type, remove storage bits first, apply them later again */
5180 storage = type->t & VT_STORAGE;
5181 type->t &= ~VT_STORAGE;
5182 post = ret = type;
5184 while (tok == '*') {
5185 qualifiers = 0;
5186 redo:
5187 next();
5188 switch(tok) {
5189 case TOK_CONST1:
5190 case TOK_CONST2:
5191 case TOK_CONST3:
5192 qualifiers |= VT_CONSTANT;
5193 goto redo;
5194 case TOK_VOLATILE1:
5195 case TOK_VOLATILE2:
5196 case TOK_VOLATILE3:
5197 qualifiers |= VT_VOLATILE;
5198 goto redo;
5199 case TOK_RESTRICT1:
5200 case TOK_RESTRICT2:
5201 case TOK_RESTRICT3:
5202 goto redo;
5203 /* XXX: clarify attribute handling */
5204 case TOK_ATTRIBUTE1:
5205 case TOK_ATTRIBUTE2:
5206 parse_attribute(ad);
5207 break;
5209 mk_pointer(type);
5210 type->t |= qualifiers;
5211 if (ret == type)
5212 /* innermost pointed to type is the one for the first derivation */
5213 ret = pointed_type(type);
5216 if (tok == '(') {
5217 /* This is possibly a parameter type list for abstract declarators
5218 ('int ()'), use post_type for testing this. */
5219 if (!post_type(type, ad, 0, td)) {
5220 /* It's not, so it's a nested declarator, and the post operations
5221 apply to the innermost pointed to type (if any). */
5222 /* XXX: this is not correct to modify 'ad' at this point, but
5223 the syntax is not clear */
5224 parse_attribute(ad);
5225 post = type_decl(type, ad, v, td);
5226 skip(')');
5227 } else
5228 goto abstract;
5229 } else if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
5230 /* type identifier */
5231 *v = tok;
5232 next();
5233 } else {
5234 abstract:
5235 if (!(td & TYPE_ABSTRACT))
5236 expect("identifier");
5237 *v = 0;
5239 post_type(post, ad, storage, 0);
5240 parse_attribute(ad);
5241 type->t |= storage;
5242 return ret;
5245 /* indirection with full error checking and bound check */
5246 ST_FUNC void indir(void)
5248 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
5249 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
5250 return;
5251 expect("pointer");
5253 if (vtop->r & VT_LVAL)
5254 gv(RC_INT);
5255 vtop->type = *pointed_type(&vtop->type);
5256 /* Arrays and functions are never lvalues */
5257 if (!(vtop->type.t & (VT_ARRAY | VT_VLA))
5258 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
5259 vtop->r |= VT_LVAL;
5260 /* if bound checking, the referenced pointer must be checked */
5261 #ifdef CONFIG_TCC_BCHECK
5262 if (tcc_state->do_bounds_check)
5263 vtop->r |= VT_MUSTBOUND;
5264 #endif
5268 /* pass a parameter to a function and do type checking and casting */
5269 static void gfunc_param_typed(Sym *func, Sym *arg)
5271 int func_type;
5272 CType type;
5274 func_type = func->f.func_type;
5275 if (func_type == FUNC_OLD ||
5276 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
5277 /* default casting : only need to convert float to double */
5278 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
5279 gen_cast_s(VT_DOUBLE);
5280 } else if (vtop->type.t & VT_BITFIELD) {
5281 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5282 type.ref = vtop->type.ref;
5283 gen_cast(&type);
5284 } else if (vtop->r & VT_MUSTCAST) {
5285 force_charshort_cast();
5287 } else if (arg == NULL) {
5288 tcc_error("too many arguments to function");
5289 } else {
5290 type = arg->type;
5291 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5292 gen_assign_cast(&type);
5296 /* parse an expression and return its type without any side effect. */
5297 static void expr_type(CType *type, void (*expr_fn)(void))
5299 nocode_wanted++;
5300 expr_fn();
5301 *type = vtop->type;
5302 vpop();
5303 nocode_wanted--;
5306 /* parse an expression of the form '(type)' or '(expr)' and return its
5307 type */
5308 static void parse_expr_type(CType *type)
5310 int n;
5311 AttributeDef ad;
5313 skip('(');
5314 if (parse_btype(type, &ad)) {
5315 type_decl(type, &ad, &n, TYPE_ABSTRACT);
5316 } else {
5317 expr_type(type, gexpr);
5319 skip(')');
5322 static void parse_type(CType *type)
5324 AttributeDef ad;
5325 int n;
5327 if (!parse_btype(type, &ad)) {
5328 expect("type");
5330 type_decl(type, &ad, &n, TYPE_ABSTRACT);
5333 static void parse_builtin_params(int nc, const char *args)
5335 char c, sep = '(';
5336 CType type;
5337 if (nc)
5338 nocode_wanted++;
5339 next();
5340 if (*args == 0)
5341 skip(sep);
5342 while ((c = *args++)) {
5343 skip(sep);
5344 sep = ',';
5345 if (c == 't') {
5346 parse_type(&type);
5347 vpush(&type);
5348 continue;
5350 expr_eq();
5351 type.ref = NULL;
5352 type.t = 0;
5353 switch (c) {
5354 case 'e':
5355 continue;
5356 case 'V':
5357 type.t = VT_CONSTANT;
5358 case 'v':
5359 type.t |= VT_VOID;
5360 mk_pointer (&type);
5361 break;
5362 case 'S':
5363 type.t = VT_CONSTANT;
5364 case 's':
5365 type.t |= char_type.t;
5366 mk_pointer (&type);
5367 break;
5368 case 'i':
5369 type.t = VT_INT;
5370 break;
5371 case 'l':
5372 type.t = VT_SIZE_T;
5373 break;
5374 default:
5375 tcc_error("internal error");
5377 gen_assign_cast(&type);
5379 skip(')');
5380 if (nc)
5381 nocode_wanted--;
5384 ST_FUNC void unary(void)
5386 int n, t, align, size, r, sizeof_caller;
5387 CType type;
5388 Sym *s;
5389 AttributeDef ad;
5391 /* generate line number info */
5392 if (tcc_state->do_debug)
5393 tcc_debug_line(tcc_state);
5395 sizeof_caller = in_sizeof;
5396 in_sizeof = 0;
5397 type.ref = NULL;
5398 /* XXX: GCC 2.95.3 does not generate a table although it should be
5399 better here */
5400 tok_next:
5401 switch(tok) {
5402 case TOK_EXTENSION:
5403 next();
5404 goto tok_next;
5405 case TOK_LCHAR:
5406 #ifdef TCC_TARGET_PE
5407 t = VT_SHORT|VT_UNSIGNED;
5408 goto push_tokc;
5409 #endif
5410 case TOK_CINT:
5411 case TOK_CCHAR:
5412 t = VT_INT;
5413 push_tokc:
5414 type.t = t;
5415 vsetc(&type, VT_CONST, &tokc);
5416 next();
5417 break;
5418 case TOK_CUINT:
5419 t = VT_INT | VT_UNSIGNED;
5420 goto push_tokc;
5421 case TOK_CLLONG:
5422 t = VT_LLONG;
5423 goto push_tokc;
5424 case TOK_CULLONG:
5425 t = VT_LLONG | VT_UNSIGNED;
5426 goto push_tokc;
5427 case TOK_CFLOAT:
5428 t = VT_FLOAT;
5429 goto push_tokc;
5430 case TOK_CDOUBLE:
5431 t = VT_DOUBLE;
5432 goto push_tokc;
5433 case TOK_CLDOUBLE:
5434 t = VT_LDOUBLE;
5435 goto push_tokc;
5436 case TOK_CLONG:
5437 t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG;
5438 goto push_tokc;
5439 case TOK_CULONG:
5440 t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG | VT_UNSIGNED;
5441 goto push_tokc;
5442 case TOK___FUNCTION__:
5443 if (!gnu_ext)
5444 goto tok_identifier;
5445 /* fall thru */
5446 case TOK___FUNC__:
5448 void *ptr;
5449 int len;
5450 /* special function name identifier */
5451 len = strlen(funcname) + 1;
5452 /* generate char[len] type */
5453 type.t = VT_BYTE;
5454 mk_pointer(&type);
5455 type.t |= VT_ARRAY;
5456 type.ref->c = len;
5457 vpush_ref(&type, data_section, data_section->data_offset, len);
5458 if (!NODATA_WANTED) {
5459 ptr = section_ptr_add(data_section, len);
5460 memcpy(ptr, funcname, len);
5462 next();
5464 break;
5465 case TOK_LSTR:
5466 #ifdef TCC_TARGET_PE
5467 t = VT_SHORT | VT_UNSIGNED;
5468 #else
5469 t = VT_INT;
5470 #endif
5471 goto str_init;
5472 case TOK_STR:
5473 /* string parsing */
5474 t = VT_BYTE;
5475 if (tcc_state->char_is_unsigned)
5476 t = VT_BYTE | VT_UNSIGNED;
5477 str_init:
5478 if (tcc_state->warn_write_strings)
5479 t |= VT_CONSTANT;
5480 type.t = t;
5481 mk_pointer(&type);
5482 type.t |= VT_ARRAY;
5483 memset(&ad, 0, sizeof(AttributeDef));
5484 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
5485 break;
5486 case '(':
5487 next();
5488 /* cast ? */
5489 if (parse_btype(&type, &ad)) {
5490 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
5491 skip(')');
5492 /* check ISOC99 compound literal */
5493 if (tok == '{') {
5494 /* data is allocated locally by default */
5495 if (global_expr)
5496 r = VT_CONST;
5497 else
5498 r = VT_LOCAL;
5499 /* all except arrays are lvalues */
5500 if (!(type.t & VT_ARRAY))
5501 r |= VT_LVAL;
5502 memset(&ad, 0, sizeof(AttributeDef));
5503 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
5504 } else {
5505 if (sizeof_caller) {
5506 vpush(&type);
5507 return;
5509 unary();
5510 gen_cast(&type);
5512 } else if (tok == '{') {
5513 int saved_nocode_wanted = nocode_wanted;
5514 if (const_wanted && !(nocode_wanted & unevalmask))
5515 tcc_error("expected constant");
5516 /* save all registers */
5517 save_regs(0);
5518 /* statement expression : we do not accept break/continue
5519 inside as GCC does. We do retain the nocode_wanted state,
5520 as statement expressions can't ever be entered from the
5521 outside, so any reactivation of code emission (from labels
5522 or loop heads) can be disabled again after the end of it. */
5523 block(1);
5524 nocode_wanted = saved_nocode_wanted;
5525 skip(')');
5526 } else {
5527 gexpr();
5528 skip(')');
5530 break;
5531 case '*':
5532 next();
5533 unary();
5534 indir();
5535 break;
5536 case '&':
5537 next();
5538 unary();
5539 /* functions names must be treated as function pointers,
5540 except for unary '&' and sizeof. Since we consider that
5541 functions are not lvalues, we only have to handle it
5542 there and in function calls. */
5543 /* arrays can also be used although they are not lvalues */
5544 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
5545 !(vtop->type.t & VT_ARRAY))
5546 test_lvalue();
5547 if (vtop->sym)
5548 vtop->sym->a.addrtaken = 1;
5549 mk_pointer(&vtop->type);
5550 gaddrof();
5551 break;
5552 case '!':
5553 next();
5554 unary();
5555 gen_test_zero(TOK_EQ);
5556 break;
5557 case '~':
5558 next();
5559 unary();
5560 vpushi(-1);
5561 gen_op('^');
5562 break;
5563 case '+':
5564 next();
5565 unary();
5566 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
5567 tcc_error("pointer not accepted for unary plus");
5568 /* In order to force cast, we add zero, except for floating point
5569 where we really need an noop (otherwise -0.0 will be transformed
5570 into +0.0). */
5571 if (!is_float(vtop->type.t)) {
5572 vpushi(0);
5573 gen_op('+');
5575 break;
5576 case TOK_SIZEOF:
5577 case TOK_ALIGNOF1:
5578 case TOK_ALIGNOF2:
5579 case TOK_ALIGNOF3:
5580 t = tok;
5581 next();
5582 in_sizeof++;
5583 expr_type(&type, unary); /* Perform a in_sizeof = 0; */
5584 s = NULL;
5585 if (vtop[1].r & VT_SYM)
5586 s = vtop[1].sym; /* hack: accessing previous vtop */
5587 size = type_size(&type, &align);
5588 if (s && s->a.aligned)
5589 align = 1 << (s->a.aligned - 1);
5590 if (t == TOK_SIZEOF) {
5591 if (!(type.t & VT_VLA)) {
5592 if (size < 0)
5593 tcc_error("sizeof applied to an incomplete type");
5594 vpushs(size);
5595 } else {
5596 vla_runtime_type_size(&type, &align);
5598 } else {
5599 vpushs(align);
5601 vtop->type.t |= VT_UNSIGNED;
5602 break;
5604 case TOK_builtin_expect:
5605 /* __builtin_expect is a no-op for now */
5606 parse_builtin_params(0, "ee");
5607 vpop();
5608 break;
5609 case TOK_builtin_types_compatible_p:
5610 parse_builtin_params(0, "tt");
5611 vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
5612 vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
5613 n = is_compatible_types(&vtop[-1].type, &vtop[0].type);
5614 vtop -= 2;
5615 vpushi(n);
5616 break;
5617 case TOK_builtin_choose_expr:
5619 int64_t c;
5620 next();
5621 skip('(');
5622 c = expr_const64();
5623 skip(',');
5624 if (!c) {
5625 nocode_wanted++;
5627 expr_eq();
5628 if (!c) {
5629 vpop();
5630 nocode_wanted--;
5632 skip(',');
5633 if (c) {
5634 nocode_wanted++;
5636 expr_eq();
5637 if (c) {
5638 vpop();
5639 nocode_wanted--;
5641 skip(')');
5643 break;
5644 case TOK_builtin_constant_p:
5645 parse_builtin_params(1, "e");
5646 n = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5647 vtop--;
5648 vpushi(n);
5649 break;
5650 case TOK_builtin_frame_address:
5651 case TOK_builtin_return_address:
5653 int tok1 = tok;
5654 int level;
5655 next();
5656 skip('(');
5657 if (tok != TOK_CINT) {
5658 tcc_error("%s only takes positive integers",
5659 tok1 == TOK_builtin_return_address ?
5660 "__builtin_return_address" :
5661 "__builtin_frame_address");
5663 level = (uint32_t)tokc.i;
5664 next();
5665 skip(')');
5666 type.t = VT_VOID;
5667 mk_pointer(&type);
5668 vset(&type, VT_LOCAL, 0); /* local frame */
5669 while (level--) {
5670 #ifdef TCC_TARGET_RISCV64
5671 vpushi(2*PTR_SIZE);
5672 gen_op('-');
5673 #endif
5674 mk_pointer(&vtop->type);
5675 indir(); /* -> parent frame */
5677 if (tok1 == TOK_builtin_return_address) {
5678 // assume return address is just above frame pointer on stack
5679 #ifdef TCC_TARGET_ARM
5680 vpushi(2*PTR_SIZE);
5681 gen_op('+');
5682 #elif defined TCC_TARGET_RISCV64
5683 vpushi(PTR_SIZE);
5684 gen_op('-');
5685 #else
5686 vpushi(PTR_SIZE);
5687 gen_op('+');
5688 #endif
5689 mk_pointer(&vtop->type);
5690 indir();
5693 break;
5694 #ifdef TCC_TARGET_RISCV64
5695 case TOK_builtin_va_start:
5696 parse_builtin_params(0, "ee");
5697 r = vtop->r & VT_VALMASK;
5698 if (r == VT_LLOCAL)
5699 r = VT_LOCAL;
5700 if (r != VT_LOCAL)
5701 tcc_error("__builtin_va_start expects a local variable");
5702 gen_va_start();
5703 vstore();
5704 break;
5705 #endif
5706 #ifdef TCC_TARGET_X86_64
5707 #ifdef TCC_TARGET_PE
5708 case TOK_builtin_va_start:
5709 parse_builtin_params(0, "ee");
5710 r = vtop->r & VT_VALMASK;
5711 if (r == VT_LLOCAL)
5712 r = VT_LOCAL;
5713 if (r != VT_LOCAL)
5714 tcc_error("__builtin_va_start expects a local variable");
5715 vtop->r = r;
5716 vtop->type = char_pointer_type;
5717 vtop->c.i += 8;
5718 vstore();
5719 break;
5720 #else
5721 case TOK_builtin_va_arg_types:
5722 parse_builtin_params(0, "t");
5723 vpushi(classify_x86_64_va_arg(&vtop->type));
5724 vswap();
5725 vpop();
5726 break;
5727 #endif
5728 #endif
5730 #ifdef TCC_TARGET_ARM64
5731 case TOK_builtin_va_start: {
5732 parse_builtin_params(0, "ee");
5733 //xx check types
5734 gen_va_start();
5735 vpushi(0);
5736 vtop->type.t = VT_VOID;
5737 break;
5739 case TOK_builtin_va_arg: {
5740 parse_builtin_params(0, "et");
5741 type = vtop->type;
5742 vpop();
5743 //xx check types
5744 gen_va_arg(&type);
5745 vtop->type = type;
5746 break;
5748 case TOK___arm64_clear_cache: {
5749 parse_builtin_params(0, "ee");
5750 gen_clear_cache();
5751 vpushi(0);
5752 vtop->type.t = VT_VOID;
5753 break;
5755 #endif
5757 /* pre operations */
5758 case TOK_INC:
5759 case TOK_DEC:
5760 t = tok;
5761 next();
5762 unary();
5763 inc(0, t);
5764 break;
5765 case '-':
5766 next();
5767 unary();
5768 t = vtop->type.t & VT_BTYPE;
5769 if (is_float(t)) {
5770 /* In IEEE negate(x) isn't subtract(0,x), but rather
5771 subtract(-0, x). */
5772 vpush(&vtop->type);
5773 if (t == VT_FLOAT)
5774 vtop->c.f = -1.0 * 0.0;
5775 else if (t == VT_DOUBLE)
5776 vtop->c.d = -1.0 * 0.0;
5777 else
5778 vtop->c.ld = -1.0 * 0.0;
5779 } else
5780 vpushi(0);
5781 vswap();
5782 gen_op('-');
5783 break;
5784 case TOK_LAND:
5785 if (!gnu_ext)
5786 goto tok_identifier;
5787 next();
5788 /* allow to take the address of a label */
5789 if (tok < TOK_UIDENT)
5790 expect("label identifier");
5791 s = label_find(tok);
5792 if (!s) {
5793 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
5794 } else {
5795 if (s->r == LABEL_DECLARED)
5796 s->r = LABEL_FORWARD;
5798 if (!s->type.t) {
5799 s->type.t = VT_VOID;
5800 mk_pointer(&s->type);
5801 s->type.t |= VT_STATIC;
5803 vpushsym(&s->type, s);
5804 next();
5805 break;
5807 case TOK_GENERIC:
5809 CType controlling_type;
5810 int has_default = 0;
5811 int has_match = 0;
5812 int learn = 0;
5813 TokenString *str = NULL;
5814 int saved_const_wanted = const_wanted;
5816 next();
5817 skip('(');
5818 const_wanted = 0;
5819 expr_type(&controlling_type, expr_eq);
5820 controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
5821 if ((controlling_type.t & VT_BTYPE) == VT_FUNC)
5822 mk_pointer(&controlling_type);
5823 const_wanted = saved_const_wanted;
5824 for (;;) {
5825 learn = 0;
5826 skip(',');
5827 if (tok == TOK_DEFAULT) {
5828 if (has_default)
5829 tcc_error("too many 'default'");
5830 has_default = 1;
5831 if (!has_match)
5832 learn = 1;
5833 next();
5834 } else {
5835 AttributeDef ad_tmp;
5836 int itmp;
5837 CType cur_type;
5839 in_generic++;
5840 parse_btype(&cur_type, &ad_tmp);
5841 in_generic--;
5843 type_decl(&cur_type, &ad_tmp, &itmp, TYPE_ABSTRACT);
5844 if (compare_types(&controlling_type, &cur_type, 0)) {
5845 if (has_match) {
5846 tcc_error("type match twice");
5848 has_match = 1;
5849 learn = 1;
5852 skip(':');
5853 if (learn) {
5854 if (str)
5855 tok_str_free(str);
5856 skip_or_save_block(&str);
5857 } else {
5858 skip_or_save_block(NULL);
5860 if (tok == ')')
5861 break;
5863 if (!str) {
5864 char buf[60];
5865 type_to_str(buf, sizeof buf, &controlling_type, NULL);
5866 tcc_error("type '%s' does not match any association", buf);
5868 begin_macro(str, 1);
5869 next();
5870 expr_eq();
5871 if (tok != TOK_EOF)
5872 expect(",");
5873 end_macro();
5874 next();
5875 break;
5877 // special qnan , snan and infinity values
5878 case TOK___NAN__:
5879 n = 0x7fc00000;
5880 special_math_val:
5881 vpushi(n);
5882 vtop->type.t = VT_FLOAT;
5883 next();
5884 break;
5885 case TOK___SNAN__:
5886 n = 0x7f800001;
5887 goto special_math_val;
5888 case TOK___INF__:
5889 n = 0x7f800000;
5890 goto special_math_val;
5892 default:
5893 tok_identifier:
5894 t = tok;
5895 next();
5896 if (t < TOK_UIDENT)
5897 expect("identifier");
5898 s = sym_find(t);
5899 if (!s || IS_ASM_SYM(s)) {
5900 const char *name = get_tok_str(t, NULL);
5901 if (tok != '(')
5902 tcc_error("'%s' undeclared", name);
5903 /* for simple function calls, we tolerate undeclared
5904 external reference to int() function */
5905 if (tcc_state->warn_implicit_function_declaration
5906 #ifdef TCC_TARGET_PE
5907 /* people must be warned about using undeclared WINAPI functions
5908 (which usually start with uppercase letter) */
5909 || (name[0] >= 'A' && name[0] <= 'Z')
5910 #endif
5912 tcc_warning("implicit declaration of function '%s'", name);
5913 s = external_global_sym(t, &func_old_type);
5916 r = s->r;
5917 /* A symbol that has a register is a local register variable,
5918 which starts out as VT_LOCAL value. */
5919 if ((r & VT_VALMASK) < VT_CONST)
5920 r = (r & ~VT_VALMASK) | VT_LOCAL;
5922 vset(&s->type, r, s->c);
5923 /* Point to s as backpointer (even without r&VT_SYM).
5924 Will be used by at least the x86 inline asm parser for
5925 regvars. */
5926 vtop->sym = s;
5928 if (r & VT_SYM) {
5929 vtop->c.i = 0;
5930 } else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) {
5931 vtop->c.i = s->enum_val;
5933 break;
5936 /* post operations */
5937 while (1) {
5938 if (tok == TOK_INC || tok == TOK_DEC) {
5939 inc(1, tok);
5940 next();
5941 } else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) {
5942 int qualifiers, cumofs = 0;
5943 /* field */
5944 if (tok == TOK_ARROW)
5945 indir();
5946 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
5947 test_lvalue();
5948 gaddrof();
5949 /* expect pointer on structure */
5950 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
5951 expect("struct or union");
5952 if (tok == TOK_CDOUBLE)
5953 expect("field name");
5954 next();
5955 if (tok == TOK_CINT || tok == TOK_CUINT)
5956 expect("field name");
5957 s = find_field(&vtop->type, tok, &cumofs);
5958 if (!s)
5959 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc));
5960 /* add field offset to pointer */
5961 vtop->type = char_pointer_type; /* change type to 'char *' */
5962 vpushi(cumofs + s->c);
5963 gen_op('+');
5964 /* change type to field type, and set to lvalue */
5965 vtop->type = s->type;
5966 vtop->type.t |= qualifiers;
5967 /* an array is never an lvalue */
5968 if (!(vtop->type.t & VT_ARRAY)) {
5969 vtop->r |= VT_LVAL;
5970 #ifdef CONFIG_TCC_BCHECK
5971 /* if bound checking, the referenced pointer must be checked */
5972 if (tcc_state->do_bounds_check)
5973 vtop->r |= VT_MUSTBOUND;
5974 #endif
5976 next();
5977 } else if (tok == '[') {
5978 next();
5979 gexpr();
5980 gen_op('+');
5981 indir();
5982 skip(']');
5983 } else if (tok == '(') {
5984 SValue ret;
5985 Sym *sa;
5986 int nb_args, ret_nregs, ret_align, regsize, variadic;
5988 /* function call */
5989 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
5990 /* pointer test (no array accepted) */
5991 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
5992 vtop->type = *pointed_type(&vtop->type);
5993 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
5994 goto error_func;
5995 } else {
5996 error_func:
5997 expect("function pointer");
5999 } else {
6000 vtop->r &= ~VT_LVAL; /* no lvalue */
6002 /* get return type */
6003 s = vtop->type.ref;
6004 next();
6005 sa = s->next; /* first parameter */
6006 nb_args = regsize = 0;
6007 ret.r2 = VT_CONST;
6008 /* compute first implicit argument if a structure is returned */
6009 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6010 variadic = (s->f.func_type == FUNC_ELLIPSIS);
6011 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
6012 &ret_align, &regsize);
6013 if (ret_nregs <= 0) {
6014 /* get some space for the returned structure */
6015 size = type_size(&s->type, &align);
6016 #ifdef TCC_TARGET_ARM64
6017 /* On arm64, a small struct is return in registers.
6018 It is much easier to write it to memory if we know
6019 that we are allowed to write some extra bytes, so
6020 round the allocated space up to a power of 2: */
6021 if (size < 16)
6022 while (size & (size - 1))
6023 size = (size | (size - 1)) + 1;
6024 #endif
6025 loc = (loc - size) & -align;
6026 ret.type = s->type;
6027 ret.r = VT_LOCAL | VT_LVAL;
6028 /* pass it as 'int' to avoid structure arg passing
6029 problems */
6030 vseti(VT_LOCAL, loc);
6031 ret.c = vtop->c;
6032 if (ret_nregs < 0)
6033 vtop--;
6034 else
6035 nb_args++;
6037 } else {
6038 ret_nregs = 1;
6039 ret.type = s->type;
6042 if (ret_nregs > 0) {
6043 /* return in register */
6044 ret.c.i = 0;
6045 PUT_R_RET(&ret, ret.type.t);
6047 if (tok != ')') {
6048 for(;;) {
6049 expr_eq();
6050 gfunc_param_typed(s, sa);
6051 nb_args++;
6052 if (sa)
6053 sa = sa->next;
6054 if (tok == ')')
6055 break;
6056 skip(',');
6059 if (sa)
6060 tcc_error("too few arguments to function");
6061 skip(')');
6062 gfunc_call(nb_args);
6064 if (ret_nregs < 0) {
6065 vsetc(&ret.type, ret.r, &ret.c);
6066 #ifdef TCC_TARGET_RISCV64
6067 arch_transfer_ret_regs(1);
6068 #endif
6069 } else {
6070 /* return value */
6071 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
6072 vsetc(&ret.type, r, &ret.c);
6073 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
6076 /* handle packed struct return */
6077 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
6078 int addr, offset;
6080 size = type_size(&s->type, &align);
6081 /* We're writing whole regs often, make sure there's enough
6082 space. Assume register size is power of 2. */
6083 if (regsize > align)
6084 align = regsize;
6085 loc = (loc - size) & -align;
6086 addr = loc;
6087 offset = 0;
6088 for (;;) {
6089 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
6090 vswap();
6091 vstore();
6092 vtop--;
6093 if (--ret_nregs == 0)
6094 break;
6095 offset += regsize;
6097 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
6100 /* Promote char/short return values. This is matters only
6101 for calling function that were not compiled by TCC and
6102 only on some architectures. For those where it doesn't
6103 matter we expect things to be already promoted to int,
6104 but not larger. */
6105 t = s->type.t & VT_BTYPE;
6106 if (t == VT_BYTE || t == VT_SHORT || t == VT_BOOL) {
6107 #ifdef PROMOTE_RET
6108 vtop->r |= BFVAL(VT_MUSTCAST, 1);
6109 #else
6110 vtop->type.t = VT_INT;
6111 #endif
6114 if (s->f.func_noreturn)
6115 CODE_OFF();
6116 } else {
6117 break;
6122 #ifndef precedence_parser /* original top-down parser */
6124 static void expr_prod(void)
6126 int t;
6128 unary();
6129 while ((t = tok) == '*' || t == '/' || t == '%') {
6130 next();
6131 unary();
6132 gen_op(t);
6136 static void expr_sum(void)
6138 int t;
6140 expr_prod();
6141 while ((t = tok) == '+' || t == '-') {
6142 next();
6143 expr_prod();
6144 gen_op(t);
6148 static void expr_shift(void)
6150 int t;
6152 expr_sum();
6153 while ((t = tok) == TOK_SHL || t == TOK_SAR) {
6154 next();
6155 expr_sum();
6156 gen_op(t);
6160 static void expr_cmp(void)
6162 int t;
6164 expr_shift();
6165 while (((t = tok) >= TOK_ULE && t <= TOK_GT) ||
6166 t == TOK_ULT || t == TOK_UGE) {
6167 next();
6168 expr_shift();
6169 gen_op(t);
6173 static void expr_cmpeq(void)
6175 int t;
6177 expr_cmp();
6178 while ((t = tok) == TOK_EQ || t == TOK_NE) {
6179 next();
6180 expr_cmp();
6181 gen_op(t);
6185 static void expr_and(void)
6187 expr_cmpeq();
6188 while (tok == '&') {
6189 next();
6190 expr_cmpeq();
6191 gen_op('&');
6195 static void expr_xor(void)
6197 expr_and();
6198 while (tok == '^') {
6199 next();
6200 expr_and();
6201 gen_op('^');
6205 static void expr_or(void)
6207 expr_xor();
6208 while (tok == '|') {
6209 next();
6210 expr_xor();
6211 gen_op('|');
6215 static void expr_landor(int op);
6217 static void expr_land(void)
6219 expr_or();
6220 if (tok == TOK_LAND)
6221 expr_landor(tok);
6224 static void expr_lor(void)
6226 expr_land();
6227 if (tok == TOK_LOR)
6228 expr_landor(tok);
6231 # define expr_landor_next(op) op == TOK_LAND ? expr_or() : expr_land()
6232 #else /* defined precedence_parser */
6233 # define expr_landor_next(op) unary(), expr_infix(precedence(op) + 1)
6234 # define expr_lor() unary(), expr_infix(1)
6236 static int precedence(int tok)
6238 switch (tok) {
6239 case TOK_LOR: return 1;
6240 case TOK_LAND: return 2;
6241 case '|': return 3;
6242 case '^': return 4;
6243 case '&': return 5;
6244 case TOK_EQ: case TOK_NE: return 6;
6245 relat: case TOK_ULT: case TOK_UGE: return 7;
6246 case TOK_SHL: case TOK_SAR: return 8;
6247 case '+': case '-': return 9;
6248 case '*': case '/': case '%': return 10;
6249 default:
6250 if (tok >= TOK_ULE && tok <= TOK_GT)
6251 goto relat;
6252 return 0;
6255 static unsigned char prec[256];
6256 static void init_prec(void)
6258 int i;
6259 for (i = 0; i < 256; i++)
6260 prec[i] = precedence(i);
6262 #define precedence(i) ((unsigned)i < 256 ? prec[i] : 0)
6264 static void expr_landor(int op);
6266 static void expr_infix(int p)
6268 int t = tok, p2;
6269 while ((p2 = precedence(t)) >= p) {
6270 if (t == TOK_LOR || t == TOK_LAND) {
6271 expr_landor(t);
6272 } else {
6273 next();
6274 unary();
6275 if (precedence(tok) > p2)
6276 expr_infix(p2 + 1);
6277 gen_op(t);
6279 t = tok;
6282 #endif
6284 /* Assuming vtop is a value used in a conditional context
6285 (i.e. compared with zero) return 0 if it's false, 1 if
6286 true and -1 if it can't be statically determined. */
6287 static int condition_3way(void)
6289 int c = -1;
6290 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
6291 (!(vtop->r & VT_SYM) || !vtop->sym->a.weak)) {
6292 vdup();
6293 gen_cast_s(VT_BOOL);
6294 c = vtop->c.i;
6295 vpop();
6297 return c;
6300 static void expr_landor(int op)
6302 int t = 0, cc = 1, f = 0, i = op == TOK_LAND, c;
6303 for(;;) {
6304 c = f ? i : condition_3way();
6305 if (c < 0)
6306 save_regs(1), cc = 0;
6307 else if (c != i)
6308 nocode_wanted++, f = 1;
6309 if (tok != op)
6310 break;
6311 if (c < 0)
6312 t = gvtst(i, t);
6313 else
6314 vpop();
6315 next();
6316 expr_landor_next(op);
6318 if (cc || f) {
6319 vpop();
6320 vpushi(i ^ f);
6321 gsym(t);
6322 nocode_wanted -= f;
6323 } else {
6324 gvtst_set(i, t);
6328 static int is_cond_bool(SValue *sv)
6330 if ((sv->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST
6331 && (sv->type.t & VT_BTYPE) == VT_INT)
6332 return (unsigned)sv->c.i < 2;
6333 if (sv->r == VT_CMP)
6334 return 1;
6335 return 0;
6338 static void expr_cond(void)
6340 int tt, u, r1, r2, rc, t1, t2, islv, c, g;
6341 SValue sv;
6342 CType type;
6343 int ncw_prev;
6345 expr_lor();
6346 if (tok == '?') {
6347 next();
6348 c = condition_3way();
6349 g = (tok == ':' && gnu_ext);
6350 tt = 0;
6351 if (!g) {
6352 if (c < 0) {
6353 save_regs(1);
6354 tt = gvtst(1, 0);
6355 } else {
6356 vpop();
6358 } else if (c < 0) {
6359 /* needed to avoid having different registers saved in
6360 each branch */
6361 save_regs(1);
6362 gv_dup();
6363 tt = gvtst(0, 0);
6366 ncw_prev = nocode_wanted;
6367 if (c == 0)
6368 nocode_wanted++;
6369 if (!g)
6370 gexpr();
6372 if (c < 0 && vtop->r == VT_CMP) {
6373 t1 = gvtst(0, 0);
6374 vpushi(0);
6375 gvtst_set(0, t1);
6376 gv(RC_INT);
6379 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
6380 mk_pointer(&vtop->type);
6381 sv = *vtop; /* save value to handle it later */
6382 vtop--; /* no vpop so that FP stack is not flushed */
6384 if (g) {
6385 u = tt;
6386 } else if (c < 0) {
6387 u = gjmp(0);
6388 gsym(tt);
6389 } else
6390 u = 0;
6392 nocode_wanted = ncw_prev;
6393 if (c == 1)
6394 nocode_wanted++;
6395 skip(':');
6396 expr_cond();
6398 if (c < 0 && is_cond_bool(vtop) && is_cond_bool(&sv)) {
6399 if (sv.r == VT_CMP) {
6400 t1 = sv.jtrue;
6401 t2 = u;
6402 } else {
6403 t1 = gvtst(0, 0);
6404 t2 = gjmp(0);
6405 gsym(u);
6406 vpushv(&sv);
6408 gvtst_set(0, t1);
6409 gvtst_set(1, t2);
6410 nocode_wanted = ncw_prev;
6411 // tcc_warning("two conditions expr_cond");
6412 return;
6415 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
6416 mk_pointer(&vtop->type);
6418 /* cast operands to correct type according to ISOC rules */
6419 if (!combine_types(&type, &sv, vtop, '?'))
6420 type_incompatibility_error(&sv.type, &vtop->type,
6421 "type mismatch in conditional expression (have '%s' and '%s')");
6422 /* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
6423 that `(expr ? a : b).mem` does not error with "lvalue expected" */
6424 islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
6426 /* now we convert second operand */
6427 if (c != 1) {
6428 gen_cast(&type);
6429 if (islv) {
6430 mk_pointer(&vtop->type);
6431 gaddrof();
6432 } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
6433 gaddrof();
6436 rc = RC_TYPE(type.t);
6437 /* for long longs, we use fixed registers to avoid having
6438 to handle a complicated move */
6439 if (USING_TWO_WORDS(type.t))
6440 rc = RC_RET(type.t);
6442 tt = r2 = 0;
6443 if (c < 0) {
6444 r2 = gv(rc);
6445 tt = gjmp(0);
6447 gsym(u);
6448 nocode_wanted = ncw_prev;
6450 /* this is horrible, but we must also convert first
6451 operand */
6452 if (c != 0) {
6453 *vtop = sv;
6454 gen_cast(&type);
6455 if (islv) {
6456 mk_pointer(&vtop->type);
6457 gaddrof();
6458 } else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
6459 gaddrof();
6462 if (c < 0) {
6463 r1 = gv(rc);
6464 move_reg(r2, r1, islv ? VT_PTR : type.t);
6465 vtop->r = r2;
6466 gsym(tt);
6469 if (islv)
6470 indir();
6474 static void expr_eq(void)
6476 int t;
6478 expr_cond();
6479 if ((t = tok) == '=' || TOK_ASSIGN(t)) {
6480 test_lvalue();
6481 next();
6482 if (t == '=') {
6483 expr_eq();
6484 } else {
6485 vdup();
6486 expr_eq();
6487 gen_op(TOK_ASSIGN_OP(t));
6489 vstore();
6493 ST_FUNC void gexpr(void)
6495 while (1) {
6496 expr_eq();
6497 if (tok != ',')
6498 break;
6499 vpop();
6500 next();
6504 /* parse a constant expression and return value in vtop. */
6505 static void expr_const1(void)
6507 const_wanted++;
6508 nocode_wanted += unevalmask + 1;
6509 expr_cond();
6510 nocode_wanted -= unevalmask + 1;
6511 const_wanted--;
6514 /* parse an integer constant and return its value. */
6515 static inline int64_t expr_const64(void)
6517 int64_t c;
6518 expr_const1();
6519 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
6520 expect("constant expression");
6521 c = vtop->c.i;
6522 vpop();
6523 return c;
6526 /* parse an integer constant and return its value.
6527 Complain if it doesn't fit 32bit (signed or unsigned). */
6528 ST_FUNC int expr_const(void)
6530 int c;
6531 int64_t wc = expr_const64();
6532 c = wc;
6533 if (c != wc && (unsigned)c != wc)
6534 tcc_error("constant exceeds 32 bit");
6535 return c;
6538 /* ------------------------------------------------------------------------- */
6539 /* return from function */
6541 #ifndef TCC_TARGET_ARM64
6542 static void gfunc_return(CType *func_type)
6544 if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
6545 CType type, ret_type;
6546 int ret_align, ret_nregs, regsize;
6547 ret_nregs = gfunc_sret(func_type, func_var, &ret_type,
6548 &ret_align, &regsize);
6549 if (ret_nregs < 0) {
6550 #ifdef TCC_TARGET_RISCV64
6551 arch_transfer_ret_regs(0);
6552 #endif
6553 } else if (0 == ret_nregs) {
6554 /* if returning structure, must copy it to implicit
6555 first pointer arg location */
6556 type = *func_type;
6557 mk_pointer(&type);
6558 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
6559 indir();
6560 vswap();
6561 /* copy structure value to pointer */
6562 vstore();
6563 } else {
6564 /* returning structure packed into registers */
6565 int size, addr, align, rc;
6566 size = type_size(func_type,&align);
6567 if ((vtop->r != (VT_LOCAL | VT_LVAL) ||
6568 (vtop->c.i & (ret_align-1)))
6569 && (align & (ret_align-1))) {
6570 loc = (loc - size) & -ret_align;
6571 addr = loc;
6572 type = *func_type;
6573 vset(&type, VT_LOCAL | VT_LVAL, addr);
6574 vswap();
6575 vstore();
6576 vpop();
6577 vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
6579 vtop->type = ret_type;
6580 rc = RC_RET(ret_type.t);
6581 if (ret_nregs == 1)
6582 gv(rc);
6583 else {
6584 for (;;) {
6585 vdup();
6586 gv(rc);
6587 vpop();
6588 if (--ret_nregs == 0)
6589 break;
6590 /* We assume that when a structure is returned in multiple
6591 registers, their classes are consecutive values of the
6592 suite s(n) = 2^n */
6593 rc <<= 1;
6594 vtop->c.i += regsize;
6598 } else {
6599 gv(RC_RET(func_type->t));
6601 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6603 #endif
6605 static void check_func_return(void)
6607 if ((func_vt.t & VT_BTYPE) == VT_VOID)
6608 return;
6609 if (!strcmp (funcname, "main")
6610 && (func_vt.t & VT_BTYPE) == VT_INT) {
6611 /* main returns 0 by default */
6612 vpushi(0);
6613 gen_assign_cast(&func_vt);
6614 gfunc_return(&func_vt);
6615 } else {
6616 tcc_warning("function might return no value: '%s'", funcname);
6620 /* ------------------------------------------------------------------------- */
6621 /* switch/case */
6623 static int case_cmp(const void *pa, const void *pb)
6625 int64_t a = (*(struct case_t**) pa)->v1;
6626 int64_t b = (*(struct case_t**) pb)->v1;
6627 return a < b ? -1 : a > b;
6630 static void gtst_addr(int t, int a)
6632 gsym_addr(gvtst(0, t), a);
6635 static void gcase(struct case_t **base, int len, int *bsym)
6637 struct case_t *p;
6638 int e;
6639 int ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
6640 while (len > 8) {
6641 /* binary search */
6642 p = base[len/2];
6643 vdup();
6644 if (ll)
6645 vpushll(p->v2);
6646 else
6647 vpushi(p->v2);
6648 gen_op(TOK_LE);
6649 e = gvtst(1, 0);
6650 vdup();
6651 if (ll)
6652 vpushll(p->v1);
6653 else
6654 vpushi(p->v1);
6655 gen_op(TOK_GE);
6656 gtst_addr(0, p->sym); /* v1 <= x <= v2 */
6657 /* x < v1 */
6658 gcase(base, len/2, bsym);
6659 /* x > v2 */
6660 gsym(e);
6661 e = len/2 + 1;
6662 base += e; len -= e;
6664 /* linear scan */
6665 while (len--) {
6666 p = *base++;
6667 vdup();
6668 if (ll)
6669 vpushll(p->v2);
6670 else
6671 vpushi(p->v2);
6672 if (p->v1 == p->v2) {
6673 gen_op(TOK_EQ);
6674 gtst_addr(0, p->sym);
6675 } else {
6676 gen_op(TOK_LE);
6677 e = gvtst(1, 0);
6678 vdup();
6679 if (ll)
6680 vpushll(p->v1);
6681 else
6682 vpushi(p->v1);
6683 gen_op(TOK_GE);
6684 gtst_addr(0, p->sym);
6685 gsym(e);
6688 *bsym = gjmp(*bsym);
6691 /* ------------------------------------------------------------------------- */
6692 /* __attribute__((cleanup(fn))) */
6694 static void try_call_scope_cleanup(Sym *stop)
6696 Sym *cls = cur_scope->cl.s;
6698 for (; cls != stop; cls = cls->ncl) {
6699 Sym *fs = cls->next;
6700 Sym *vs = cls->prev_tok;
6702 vpushsym(&fs->type, fs);
6703 vset(&vs->type, vs->r, vs->c);
6704 vtop->sym = vs;
6705 mk_pointer(&vtop->type);
6706 gaddrof();
6707 gfunc_call(1);
6711 static void try_call_cleanup_goto(Sym *cleanupstate)
6713 Sym *oc, *cc;
6714 int ocd, ccd;
6716 if (!cur_scope->cl.s)
6717 return;
6719 /* search NCA of both cleanup chains given parents and initial depth */
6720 ocd = cleanupstate ? cleanupstate->v & ~SYM_FIELD : 0;
6721 for (ccd = cur_scope->cl.n, oc = cleanupstate; ocd > ccd; --ocd, oc = oc->ncl)
6723 for (cc = cur_scope->cl.s; ccd > ocd; --ccd, cc = cc->ncl)
6725 for (; cc != oc; cc = cc->ncl, oc = oc->ncl, --ccd)
6728 try_call_scope_cleanup(cc);
6731 /* call 'func' for each __attribute__((cleanup(func))) */
6732 static void block_cleanup(struct scope *o)
6734 int jmp = 0;
6735 Sym *g, **pg;
6736 for (pg = &pending_gotos; (g = *pg) && g->c > o->cl.n;) {
6737 if (g->prev_tok->r & LABEL_FORWARD) {
6738 Sym *pcl = g->next;
6739 if (!jmp)
6740 jmp = gjmp(0);
6741 gsym(pcl->jnext);
6742 try_call_scope_cleanup(o->cl.s);
6743 pcl->jnext = gjmp(0);
6744 if (!o->cl.n)
6745 goto remove_pending;
6746 g->c = o->cl.n;
6747 pg = &g->prev;
6748 } else {
6749 remove_pending:
6750 *pg = g->prev;
6751 sym_free(g);
6754 gsym(jmp);
6755 try_call_scope_cleanup(o->cl.s);
6758 /* ------------------------------------------------------------------------- */
6759 /* VLA */
6761 static void vla_restore(int loc)
6763 if (loc)
6764 gen_vla_sp_restore(loc);
6767 static void vla_leave(struct scope *o)
6769 if (o->vla.num < cur_scope->vla.num)
6770 vla_restore(o->vla.loc);
6773 /* ------------------------------------------------------------------------- */
6774 /* local scopes */
6776 void new_scope(struct scope *o)
6778 /* copy and link previous scope */
6779 *o = *cur_scope;
6780 o->prev = cur_scope;
6781 cur_scope = o;
6783 /* record local declaration stack position */
6784 o->lstk = local_stack;
6785 o->llstk = local_label_stack;
6787 ++local_scope;
6789 if (tcc_state->do_debug)
6790 tcc_debug_stabn(N_LBRAC, ind - func_ind);
6793 void prev_scope(struct scope *o, int is_expr)
6795 vla_leave(o->prev);
6797 if (o->cl.s != o->prev->cl.s)
6798 block_cleanup(o->prev);
6800 /* pop locally defined labels */
6801 label_pop(&local_label_stack, o->llstk, is_expr);
6803 /* In the is_expr case (a statement expression is finished here),
6804 vtop might refer to symbols on the local_stack. Either via the
6805 type or via vtop->sym. We can't pop those nor any that in turn
6806 might be referred to. To make it easier we don't roll back
6807 any symbols in that case; some upper level call to block() will
6808 do that. We do have to remove such symbols from the lookup
6809 tables, though. sym_pop will do that. */
6811 /* pop locally defined symbols */
6812 pop_local_syms(&local_stack, o->lstk, is_expr, 0);
6813 cur_scope = o->prev;
6814 --local_scope;
6816 if (tcc_state->do_debug)
6817 tcc_debug_stabn(N_RBRAC, ind - func_ind);
6820 /* leave a scope via break/continue(/goto) */
6821 void leave_scope(struct scope *o)
6823 if (!o)
6824 return;
6825 try_call_scope_cleanup(o->cl.s);
6826 vla_leave(o);
6829 /* ------------------------------------------------------------------------- */
6830 /* call block from 'for do while' loops */
6832 static void lblock(int *bsym, int *csym)
6834 struct scope *lo = loop_scope, *co = cur_scope;
6835 int *b = co->bsym, *c = co->csym;
6836 if (csym) {
6837 co->csym = csym;
6838 loop_scope = co;
6840 co->bsym = bsym;
6841 block(0);
6842 co->bsym = b;
6843 if (csym) {
6844 co->csym = c;
6845 loop_scope = lo;
6849 static void block(int is_expr)
6851 int a, b, c, d, e, t;
6852 struct scope o;
6853 Sym *s;
6855 if (is_expr) {
6856 /* default return value is (void) */
6857 vpushi(0);
6858 vtop->type.t = VT_VOID;
6861 again:
6862 t = tok, next();
6864 if (t == TOK_IF) {
6865 skip('(');
6866 gexpr();
6867 skip(')');
6868 a = gvtst(1, 0);
6869 block(0);
6870 if (tok == TOK_ELSE) {
6871 d = gjmp(0);
6872 gsym(a);
6873 next();
6874 block(0);
6875 gsym(d); /* patch else jmp */
6876 } else {
6877 gsym(a);
6880 } else if (t == TOK_WHILE) {
6881 d = gind();
6882 skip('(');
6883 gexpr();
6884 skip(')');
6885 a = gvtst(1, 0);
6886 b = 0;
6887 lblock(&a, &b);
6888 gjmp_addr(d);
6889 gsym_addr(b, d);
6890 gsym(a);
6892 } else if (t == '{') {
6893 new_scope(&o);
6895 /* handle local labels declarations */
6896 while (tok == TOK_LABEL) {
6897 do {
6898 next();
6899 if (tok < TOK_UIDENT)
6900 expect("label identifier");
6901 label_push(&local_label_stack, tok, LABEL_DECLARED);
6902 next();
6903 } while (tok == ',');
6904 skip(';');
6907 while (tok != '}') {
6908 decl(VT_LOCAL);
6909 if (tok != '}') {
6910 if (is_expr)
6911 vpop();
6912 block(is_expr);
6916 prev_scope(&o, is_expr);
6917 if (local_scope)
6918 next();
6919 else if (!nocode_wanted)
6920 check_func_return();
6922 } else if (t == TOK_RETURN) {
6923 b = (func_vt.t & VT_BTYPE) != VT_VOID;
6924 if (tok != ';') {
6925 gexpr();
6926 if (b) {
6927 gen_assign_cast(&func_vt);
6928 } else {
6929 if (vtop->type.t != VT_VOID)
6930 tcc_warning("void function returns a value");
6931 vtop--;
6933 } else if (b) {
6934 tcc_warning("'return' with no value");
6935 b = 0;
6937 leave_scope(root_scope);
6938 if (b)
6939 gfunc_return(&func_vt);
6940 skip(';');
6941 /* jump unless last stmt in top-level block */
6942 if (tok != '}' || local_scope != 1)
6943 rsym = gjmp(rsym);
6944 CODE_OFF();
6946 } else if (t == TOK_BREAK) {
6947 /* compute jump */
6948 if (!cur_scope->bsym)
6949 tcc_error("cannot break");
6950 if (cur_switch && cur_scope->bsym == cur_switch->bsym)
6951 leave_scope(cur_switch->scope);
6952 else
6953 leave_scope(loop_scope);
6954 *cur_scope->bsym = gjmp(*cur_scope->bsym);
6955 skip(';');
6957 } else if (t == TOK_CONTINUE) {
6958 /* compute jump */
6959 if (!cur_scope->csym)
6960 tcc_error("cannot continue");
6961 leave_scope(loop_scope);
6962 *cur_scope->csym = gjmp(*cur_scope->csym);
6963 skip(';');
6965 } else if (t == TOK_FOR) {
6966 new_scope(&o);
6968 skip('(');
6969 if (tok != ';') {
6970 /* c99 for-loop init decl? */
6971 if (!decl0(VT_LOCAL, 1, NULL)) {
6972 /* no, regular for-loop init expr */
6973 gexpr();
6974 vpop();
6977 skip(';');
6978 a = b = 0;
6979 c = d = gind();
6980 if (tok != ';') {
6981 gexpr();
6982 a = gvtst(1, 0);
6984 skip(';');
6985 if (tok != ')') {
6986 e = gjmp(0);
6987 d = gind();
6988 gexpr();
6989 vpop();
6990 gjmp_addr(c);
6991 gsym(e);
6993 skip(')');
6994 lblock(&a, &b);
6995 gjmp_addr(d);
6996 gsym_addr(b, d);
6997 gsym(a);
6998 prev_scope(&o, 0);
7000 } else if (t == TOK_DO) {
7001 a = b = 0;
7002 d = gind();
7003 lblock(&a, &b);
7004 gsym(b);
7005 skip(TOK_WHILE);
7006 skip('(');
7007 gexpr();
7008 skip(')');
7009 skip(';');
7010 c = gvtst(0, 0);
7011 gsym_addr(c, d);
7012 gsym(a);
7014 } else if (t == TOK_SWITCH) {
7015 struct switch_t *sw;
7017 sw = tcc_mallocz(sizeof *sw);
7018 sw->bsym = &a;
7019 sw->scope = cur_scope;
7020 sw->prev = cur_switch;
7021 cur_switch = sw;
7023 skip('(');
7024 gexpr();
7025 skip(')');
7026 sw->sv = *vtop--; /* save switch value */
7028 a = 0;
7029 b = gjmp(0); /* jump to first case */
7030 lblock(&a, NULL);
7031 a = gjmp(a); /* add implicit break */
7032 /* case lookup */
7033 gsym(b);
7035 qsort(sw->p, sw->n, sizeof(void*), case_cmp);
7036 for (b = 1; b < sw->n; b++)
7037 if (sw->p[b - 1]->v2 >= sw->p[b]->v1)
7038 tcc_error("duplicate case value");
7040 /* Our switch table sorting is signed, so the compared
7041 value needs to be as well when it's 64bit. */
7042 vpushv(&sw->sv);
7043 if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
7044 vtop->type.t &= ~VT_UNSIGNED;
7045 gv(RC_INT);
7046 d = 0, gcase(sw->p, sw->n, &d);
7047 vpop();
7048 if (sw->def_sym)
7049 gsym_addr(d, sw->def_sym);
7050 else
7051 gsym(d);
7052 /* break label */
7053 gsym(a);
7055 dynarray_reset(&sw->p, &sw->n);
7056 cur_switch = sw->prev;
7057 tcc_free(sw);
7059 } else if (t == TOK_CASE) {
7060 struct case_t *cr = tcc_malloc(sizeof(struct case_t));
7061 if (!cur_switch)
7062 expect("switch");
7063 cr->v1 = cr->v2 = expr_const64();
7064 if (gnu_ext && tok == TOK_DOTS) {
7065 next();
7066 cr->v2 = expr_const64();
7067 if (cr->v2 < cr->v1)
7068 tcc_warning("empty case range");
7070 cr->sym = gind();
7071 dynarray_add(&cur_switch->p, &cur_switch->n, cr);
7072 skip(':');
7073 is_expr = 0;
7074 goto block_after_label;
7076 } else if (t == TOK_DEFAULT) {
7077 if (!cur_switch)
7078 expect("switch");
7079 if (cur_switch->def_sym)
7080 tcc_error("too many 'default'");
7081 cur_switch->def_sym = gind();
7082 skip(':');
7083 is_expr = 0;
7084 goto block_after_label;
7086 } else if (t == TOK_GOTO) {
7087 vla_restore(root_scope->vla.loc);
7088 if (tok == '*' && gnu_ext) {
7089 /* computed goto */
7090 next();
7091 gexpr();
7092 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7093 expect("pointer");
7094 ggoto();
7096 } else if (tok >= TOK_UIDENT) {
7097 s = label_find(tok);
7098 /* put forward definition if needed */
7099 if (!s)
7100 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7101 else if (s->r == LABEL_DECLARED)
7102 s->r = LABEL_FORWARD;
7104 if (s->r & LABEL_FORWARD) {
7105 /* start new goto chain for cleanups, linked via label->next */
7106 if (cur_scope->cl.s && !nocode_wanted) {
7107 sym_push2(&pending_gotos, SYM_FIELD, 0, cur_scope->cl.n);
7108 pending_gotos->prev_tok = s;
7109 s = sym_push2(&s->next, SYM_FIELD, 0, 0);
7110 pending_gotos->next = s;
7112 s->jnext = gjmp(s->jnext);
7113 } else {
7114 try_call_cleanup_goto(s->cleanupstate);
7115 gjmp_addr(s->jnext);
7117 next();
7119 } else {
7120 expect("label identifier");
7122 skip(';');
7124 } else if (t == TOK_ASM1 || t == TOK_ASM2 || t == TOK_ASM3) {
7125 asm_instr();
7127 } else {
7128 if (tok == ':' && t >= TOK_UIDENT) {
7129 /* label case */
7130 next();
7131 s = label_find(t);
7132 if (s) {
7133 if (s->r == LABEL_DEFINED)
7134 tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
7135 s->r = LABEL_DEFINED;
7136 if (s->next) {
7137 Sym *pcl; /* pending cleanup goto */
7138 for (pcl = s->next; pcl; pcl = pcl->prev)
7139 gsym(pcl->jnext);
7140 sym_pop(&s->next, NULL, 0);
7141 } else
7142 gsym(s->jnext);
7143 } else {
7144 s = label_push(&global_label_stack, t, LABEL_DEFINED);
7146 s->jnext = gind();
7147 s->cleanupstate = cur_scope->cl.s;
7149 block_after_label:
7150 vla_restore(cur_scope->vla.loc);
7151 /* we accept this, but it is a mistake */
7152 if (tok == '}') {
7153 tcc_warning("deprecated use of label at end of compound statement");
7154 } else {
7155 goto again;
7158 } else {
7159 /* expression case */
7160 if (t != ';') {
7161 unget_tok(t);
7162 if (is_expr) {
7163 vpop();
7164 gexpr();
7165 } else {
7166 gexpr();
7167 vpop();
7169 skip(';');
7175 /* This skips over a stream of tokens containing balanced {} and ()
7176 pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
7177 with a '{'). If STR then allocates and stores the skipped tokens
7178 in *STR. This doesn't check if () and {} are nested correctly,
7179 i.e. "({)}" is accepted. */
7180 static void skip_or_save_block(TokenString **str)
7182 int braces = tok == '{';
7183 int level = 0;
7184 if (str)
7185 *str = tok_str_alloc();
7187 while ((level > 0 || (tok != '}' && tok != ',' && tok != ';' && tok != ')'))) {
7188 int t;
7189 if (tok == TOK_EOF) {
7190 if (str || level > 0)
7191 tcc_error("unexpected end of file");
7192 else
7193 break;
7195 if (str)
7196 tok_str_add_tok(*str);
7197 t = tok;
7198 next();
7199 if (t == '{' || t == '(') {
7200 level++;
7201 } else if (t == '}' || t == ')') {
7202 level--;
7203 if (level == 0 && braces && t == '}')
7204 break;
7207 if (str) {
7208 tok_str_add(*str, -1);
7209 tok_str_add(*str, 0);
7213 #define EXPR_CONST 1
7214 #define EXPR_ANY 2
7216 static void parse_init_elem(int expr_type)
7218 int saved_global_expr;
7219 switch(expr_type) {
7220 case EXPR_CONST:
7221 /* compound literals must be allocated globally in this case */
7222 saved_global_expr = global_expr;
7223 global_expr = 1;
7224 expr_const1();
7225 global_expr = saved_global_expr;
7226 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
7227 (compound literals). */
7228 if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
7229 && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
7230 || vtop->sym->v < SYM_FIRST_ANOM))
7231 #ifdef TCC_TARGET_PE
7232 || ((vtop->r & VT_SYM) && vtop->sym->a.dllimport)
7233 #endif
7235 tcc_error("initializer element is not constant");
7236 break;
7237 case EXPR_ANY:
7238 expr_eq();
7239 break;
7243 /* put zeros for variable based init */
7244 static void init_putz(Section *sec, unsigned long c, int size)
7246 if (sec) {
7247 /* nothing to do because globals are already set to zero */
7248 } else {
7249 vpush_global_sym(&func_old_type, TOK_memset);
7250 vseti(VT_LOCAL, c);
7251 #ifdef TCC_TARGET_ARM
7252 vpushs(size);
7253 vpushi(0);
7254 #else
7255 vpushi(0);
7256 vpushs(size);
7257 #endif
7258 gfunc_call(3);
7262 #define DIF_FIRST 1
7263 #define DIF_SIZE_ONLY 2
7264 #define DIF_HAVE_ELEM 4
7266 /* t is the array or struct type. c is the array or struct
7267 address. cur_field is the pointer to the current
7268 field, for arrays the 'c' member contains the current start
7269 index. 'flags' is as in decl_initializer.
7270 'al' contains the already initialized length of the
7271 current container (starting at c). This returns the new length of that. */
7272 static int decl_designator(CType *type, Section *sec, unsigned long c,
7273 Sym **cur_field, int flags, int al)
7275 Sym *s, *f;
7276 int index, index_last, align, l, nb_elems, elem_size;
7277 unsigned long corig = c;
7279 elem_size = 0;
7280 nb_elems = 1;
7282 if (flags & DIF_HAVE_ELEM)
7283 goto no_designator;
7285 if (gnu_ext && tok >= TOK_UIDENT) {
7286 l = tok, next();
7287 if (tok == ':')
7288 goto struct_field;
7289 unget_tok(l);
7292 /* NOTE: we only support ranges for last designator */
7293 while (nb_elems == 1 && (tok == '[' || tok == '.')) {
7294 if (tok == '[') {
7295 if (!(type->t & VT_ARRAY))
7296 expect("array type");
7297 next();
7298 index = index_last = expr_const();
7299 if (tok == TOK_DOTS && gnu_ext) {
7300 next();
7301 index_last = expr_const();
7303 skip(']');
7304 s = type->ref;
7305 if (index < 0 || (s->c >= 0 && index_last >= s->c) ||
7306 index_last < index)
7307 tcc_error("invalid index");
7308 if (cur_field)
7309 (*cur_field)->c = index_last;
7310 type = pointed_type(type);
7311 elem_size = type_size(type, &align);
7312 c += index * elem_size;
7313 nb_elems = index_last - index + 1;
7314 } else {
7315 int cumofs;
7316 next();
7317 l = tok;
7318 struct_field:
7319 next();
7320 if ((type->t & VT_BTYPE) != VT_STRUCT)
7321 expect("struct/union type");
7322 cumofs = 0;
7323 f = find_field(type, l, &cumofs);
7324 if (!f)
7325 expect("field");
7326 if (cur_field)
7327 *cur_field = f;
7328 type = &f->type;
7329 c += cumofs + f->c;
7331 cur_field = NULL;
7333 if (!cur_field) {
7334 if (tok == '=') {
7335 next();
7336 } else if (!gnu_ext) {
7337 expect("=");
7339 } else {
7340 no_designator:
7341 if (type->t & VT_ARRAY) {
7342 index = (*cur_field)->c;
7343 if (type->ref->c >= 0 && index >= type->ref->c)
7344 tcc_error("index too large");
7345 type = pointed_type(type);
7346 c += index * type_size(type, &align);
7347 } else {
7348 f = *cur_field;
7349 while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD))
7350 *cur_field = f = f->next;
7351 if (!f)
7352 tcc_error("too many field init");
7353 type = &f->type;
7354 c += f->c;
7357 /* must put zero in holes (note that doing it that way
7358 ensures that it even works with designators) */
7359 if (!(flags & DIF_SIZE_ONLY) && c - corig > al)
7360 init_putz(sec, corig + al, c - corig - al);
7361 decl_initializer(type, sec, c, flags & ~DIF_FIRST);
7363 /* XXX: make it more general */
7364 if (!(flags & DIF_SIZE_ONLY) && nb_elems > 1) {
7365 unsigned long c_end;
7366 uint8_t *src, *dst;
7367 int i;
7369 if (!sec) {
7370 vset(type, VT_LOCAL|VT_LVAL, c);
7371 for (i = 1; i < nb_elems; i++) {
7372 vset(type, VT_LOCAL|VT_LVAL, c + elem_size * i);
7373 vswap();
7374 vstore();
7376 vpop();
7377 } else if (!NODATA_WANTED) {
7378 c_end = c + nb_elems * elem_size;
7379 if (c_end > sec->data_allocated)
7380 section_realloc(sec, c_end);
7381 src = sec->data + c;
7382 dst = src;
7383 for(i = 1; i < nb_elems; i++) {
7384 dst += elem_size;
7385 memcpy(dst, src, elem_size);
7389 c += nb_elems * type_size(type, &align);
7390 if (c - corig > al)
7391 al = c - corig;
7392 return al;
7395 /* store a value or an expression directly in global data or in local array */
7396 static void init_putv(CType *type, Section *sec, unsigned long c)
7398 int bt;
7399 void *ptr;
7400 CType dtype;
7402 dtype = *type;
7403 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7405 if (sec) {
7406 int size, align;
7407 /* XXX: not portable */
7408 /* XXX: generate error if incorrect relocation */
7409 gen_assign_cast(&dtype);
7410 bt = type->t & VT_BTYPE;
7412 if ((vtop->r & VT_SYM)
7413 && bt != VT_PTR
7414 && bt != VT_FUNC
7415 && (bt != (PTR_SIZE == 8 ? VT_LLONG : VT_INT)
7416 || (type->t & VT_BITFIELD))
7417 && !((vtop->r & VT_CONST) && vtop->sym->v >= SYM_FIRST_ANOM)
7419 tcc_error("initializer element is not computable at load time");
7421 if (NODATA_WANTED) {
7422 vtop--;
7423 return;
7426 size = type_size(type, &align);
7427 section_reserve(sec, c + size);
7428 ptr = sec->data + c;
7430 /* XXX: make code faster ? */
7431 if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
7432 vtop->sym->v >= SYM_FIRST_ANOM &&
7433 /* XXX This rejects compound literals like
7434 '(void *){ptr}'. The problem is that '&sym' is
7435 represented the same way, which would be ruled out
7436 by the SYM_FIRST_ANOM check above, but also '"string"'
7437 in 'char *p = "string"' is represented the same
7438 with the type being VT_PTR and the symbol being an
7439 anonymous one. That is, there's no difference in vtop
7440 between '(void *){x}' and '&(void *){x}'. Ignore
7441 pointer typed entities here. Hopefully no real code
7442 will ever use compound literals with scalar type. */
7443 (vtop->type.t & VT_BTYPE) != VT_PTR) {
7444 /* These come from compound literals, memcpy stuff over. */
7445 Section *ssec;
7446 ElfSym *esym;
7447 ElfW_Rel *rel;
7448 esym = elfsym(vtop->sym);
7449 ssec = tcc_state->sections[esym->st_shndx];
7450 memmove (ptr, ssec->data + esym->st_value + (int)vtop->c.i, size);
7451 if (ssec->reloc) {
7452 /* We need to copy over all memory contents, and that
7453 includes relocations. Use the fact that relocs are
7454 created it order, so look from the end of relocs
7455 until we hit one before the copied region. */
7456 int num_relocs = ssec->reloc->data_offset / sizeof(*rel);
7457 rel = (ElfW_Rel*)(ssec->reloc->data + ssec->reloc->data_offset);
7458 while (num_relocs--) {
7459 rel--;
7460 if (rel->r_offset >= esym->st_value + size)
7461 continue;
7462 if (rel->r_offset < esym->st_value)
7463 break;
7464 /* Note: if the same fields are initialized multiple
7465 times (possible with designators) then we possibly
7466 add multiple relocations for the same offset here.
7467 That would lead to wrong code, the last reloc needs
7468 to win. We clean this up later after the whole
7469 initializer is parsed. */
7470 put_elf_reloca(symtab_section, sec,
7471 c + rel->r_offset - esym->st_value,
7472 ELFW(R_TYPE)(rel->r_info),
7473 ELFW(R_SYM)(rel->r_info),
7474 #if PTR_SIZE == 8
7475 rel->r_addend
7476 #else
7478 #endif
7482 } else {
7483 if (type->t & VT_BITFIELD) {
7484 int bit_pos, bit_size, bits, n;
7485 unsigned char *p, v, m;
7486 bit_pos = BIT_POS(vtop->type.t);
7487 bit_size = BIT_SIZE(vtop->type.t);
7488 p = (unsigned char*)ptr + (bit_pos >> 3);
7489 bit_pos &= 7, bits = 0;
7490 while (bit_size) {
7491 n = 8 - bit_pos;
7492 if (n > bit_size)
7493 n = bit_size;
7494 v = vtop->c.i >> bits << bit_pos;
7495 m = ((1 << n) - 1) << bit_pos;
7496 *p = (*p & ~m) | (v & m);
7497 bits += n, bit_size -= n, bit_pos = 0, ++p;
7499 } else
7500 switch(bt) {
7501 /* XXX: when cross-compiling we assume that each type has the
7502 same representation on host and target, which is likely to
7503 be wrong in the case of long double */
7504 case VT_BOOL:
7505 vtop->c.i = vtop->c.i != 0;
7506 case VT_BYTE:
7507 *(char *)ptr |= vtop->c.i;
7508 break;
7509 case VT_SHORT:
7510 *(short *)ptr |= vtop->c.i;
7511 break;
7512 case VT_FLOAT:
7513 *(float*)ptr = vtop->c.f;
7514 break;
7515 case VT_DOUBLE:
7516 *(double *)ptr = vtop->c.d;
7517 break;
7518 case VT_LDOUBLE:
7519 #if defined TCC_IS_NATIVE_387
7520 if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
7521 memcpy(ptr, &vtop->c.ld, 10);
7522 #ifdef __TINYC__
7523 else if (sizeof (long double) == sizeof (double))
7524 __asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr) : "m" (vtop->c.ld));
7525 #endif
7526 else if (vtop->c.ld == 0.0)
7528 else
7529 #endif
7530 if (sizeof(long double) == LDOUBLE_SIZE)
7531 *(long double*)ptr = vtop->c.ld;
7532 else if (sizeof(double) == LDOUBLE_SIZE)
7533 *(double *)ptr = (double)vtop->c.ld;
7534 else
7535 tcc_error("can't cross compile long double constants");
7536 break;
7537 #if PTR_SIZE != 8
7538 case VT_LLONG:
7539 *(long long *)ptr |= vtop->c.i;
7540 break;
7541 #else
7542 case VT_LLONG:
7543 #endif
7544 case VT_PTR:
7546 addr_t val = vtop->c.i;
7547 #if PTR_SIZE == 8
7548 if (vtop->r & VT_SYM)
7549 greloca(sec, vtop->sym, c, R_DATA_PTR, val);
7550 else
7551 *(addr_t *)ptr |= val;
7552 #else
7553 if (vtop->r & VT_SYM)
7554 greloc(sec, vtop->sym, c, R_DATA_PTR);
7555 *(addr_t *)ptr |= val;
7556 #endif
7557 break;
7559 default:
7561 int val = vtop->c.i;
7562 #if PTR_SIZE == 8
7563 if (vtop->r & VT_SYM)
7564 greloca(sec, vtop->sym, c, R_DATA_PTR, val);
7565 else
7566 *(int *)ptr |= val;
7567 #else
7568 if (vtop->r & VT_SYM)
7569 greloc(sec, vtop->sym, c, R_DATA_PTR);
7570 *(int *)ptr |= val;
7571 #endif
7572 break;
7576 vtop--;
7577 } else {
7578 vset(&dtype, VT_LOCAL|VT_LVAL, c);
7579 vswap();
7580 vstore();
7581 vpop();
7585 /* 't' contains the type and storage info. 'c' is the offset of the
7586 object in section 'sec'. If 'sec' is NULL, it means stack based
7587 allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
7588 dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
7589 size only evaluation is wanted (only for arrays). */
7590 static void decl_initializer(CType *type, Section *sec, unsigned long c,
7591 int flags)
7593 int len, n, no_oblock, i;
7594 int size1, align1;
7595 Sym *s, *f;
7596 Sym indexsym;
7597 CType *t1;
7599 if (!(flags & DIF_HAVE_ELEM) && tok != '{' &&
7600 /* In case of strings we have special handling for arrays, so
7601 don't consume them as initializer value (which would commit them
7602 to some anonymous symbol). */
7603 tok != TOK_LSTR && tok != TOK_STR &&
7604 !(flags & DIF_SIZE_ONLY)) {
7605 parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
7606 flags |= DIF_HAVE_ELEM;
7609 if ((flags & DIF_HAVE_ELEM) &&
7610 !(type->t & VT_ARRAY) &&
7611 /* Use i_c_parameter_t, to strip toplevel qualifiers.
7612 The source type might have VT_CONSTANT set, which is
7613 of course assignable to non-const elements. */
7614 is_compatible_unqualified_types(type, &vtop->type)) {
7615 init_putv(type, sec, c);
7616 } else if (type->t & VT_ARRAY) {
7617 s = type->ref;
7618 n = s->c;
7619 t1 = pointed_type(type);
7620 size1 = type_size(t1, &align1);
7622 no_oblock = 1;
7623 if (((flags & DIF_FIRST) && tok != TOK_LSTR && tok != TOK_STR) ||
7624 tok == '{') {
7625 if (tok != '{')
7626 tcc_error("character array initializer must be a literal,"
7627 " optionally enclosed in braces");
7628 skip('{');
7629 no_oblock = 0;
7632 /* only parse strings here if correct type (otherwise: handle
7633 them as ((w)char *) expressions */
7634 if ((tok == TOK_LSTR &&
7635 #ifdef TCC_TARGET_PE
7636 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
7637 #else
7638 (t1->t & VT_BTYPE) == VT_INT
7639 #endif
7640 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
7641 int nb;
7642 len = 0;
7643 cstr_reset(&initstr);
7644 if (size1 != (tok == TOK_STR ? 1 : sizeof(nwchar_t)))
7645 tcc_error("unhandled string literal merging");
7646 while (tok == TOK_STR || tok == TOK_LSTR) {
7647 if (initstr.size)
7648 initstr.size -= size1;
7649 if (tok == TOK_STR)
7650 len += tokc.str.size;
7651 else
7652 len += tokc.str.size / sizeof(nwchar_t);
7653 len--;
7654 cstr_cat(&initstr, tokc.str.data, tokc.str.size);
7655 next();
7657 if (tok != ')' && tok != '}' && tok != ',' && tok != ';'
7658 && tok != TOK_EOF) {
7659 /* Not a lone literal but part of a bigger expression. */
7660 unget_tok(size1 == 1 ? TOK_STR : TOK_LSTR);
7661 tokc.str.size = initstr.size;
7662 tokc.str.data = initstr.data;
7663 indexsym.c = 0;
7664 f = &indexsym;
7665 goto do_init_list;
7667 nb = len;
7668 if (n >= 0 && len > n)
7669 nb = n;
7670 if (!(flags & DIF_SIZE_ONLY)) {
7671 if (sec && !NODATA_WANTED &&
7672 (c + nb > sec->data_allocated))
7673 nb = sec->data_allocated - c;
7674 if (len > nb)
7675 tcc_warning("initializer-string for array is too long");
7676 /* in order to go faster for common case (char
7677 string in global variable, we handle it
7678 specifically */
7679 if (sec && size1 == 1) {
7680 if (!NODATA_WANTED)
7681 memcpy(sec->data + c, initstr.data, nb);
7682 } else {
7683 for(i=0;i<nb;i++) {
7684 if (size1 == 1)
7685 ch = ((unsigned char *)initstr.data)[i];
7686 else
7687 ch = ((nwchar_t *)initstr.data)[i];
7688 vpushi(ch);
7689 init_putv(t1, sec, c + i * size1);
7693 /* only add trailing zero if enough storage (no
7694 warning in this case since it is standard) */
7695 if (n < 0 || len < n) {
7696 if (!(flags & DIF_SIZE_ONLY)) {
7697 vpushi(0);
7698 init_putv(t1, sec, c + (len * size1));
7700 len++;
7702 len *= size1;
7703 } else {
7704 indexsym.c = 0;
7705 f = &indexsym;
7707 do_init_list:
7708 len = 0;
7709 while (tok != '}' || (flags & DIF_HAVE_ELEM)) {
7710 len = decl_designator(type, sec, c, &f, flags, len);
7711 flags &= ~DIF_HAVE_ELEM;
7712 if (type->t & VT_ARRAY) {
7713 ++indexsym.c;
7714 /* special test for multi dimensional arrays (may not
7715 be strictly correct if designators are used at the
7716 same time) */
7717 if (no_oblock && len >= n*size1)
7718 break;
7719 } else {
7720 if (s->type.t == VT_UNION)
7721 f = NULL;
7722 else
7723 f = f->next;
7724 if (no_oblock && f == NULL)
7725 break;
7728 if (tok == '}')
7729 break;
7730 skip(',');
7733 /* put zeros at the end */
7734 if (!(flags & DIF_SIZE_ONLY) && len < n*size1)
7735 init_putz(sec, c + len, n*size1 - len);
7736 if (!no_oblock)
7737 skip('}');
7738 /* patch type size if needed, which happens only for array types */
7739 if (n < 0)
7740 s->c = size1 == 1 ? len : ((len + size1 - 1)/size1);
7741 } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
7742 size1 = 1;
7743 no_oblock = 1;
7744 if ((flags & DIF_FIRST) || tok == '{') {
7745 skip('{');
7746 no_oblock = 0;
7748 s = type->ref;
7749 f = s->next;
7750 n = s->c;
7751 goto do_init_list;
7752 } else if (tok == '{') {
7753 if (flags & DIF_HAVE_ELEM)
7754 skip(';');
7755 next();
7756 decl_initializer(type, sec, c, flags & ~DIF_HAVE_ELEM);
7757 skip('}');
7758 } else if ((flags & DIF_SIZE_ONLY)) {
7759 /* If we supported only ISO C we wouldn't have to accept calling
7760 this on anything than an array if DIF_SIZE_ONLY (and even then
7761 only on the outermost level, so no recursion would be needed),
7762 because initializing a flex array member isn't supported.
7763 But GNU C supports it, so we need to recurse even into
7764 subfields of structs and arrays when DIF_SIZE_ONLY is set. */
7765 /* just skip expression */
7766 skip_or_save_block(NULL);
7767 } else {
7768 if (!(flags & DIF_HAVE_ELEM)) {
7769 /* This should happen only when we haven't parsed
7770 the init element above for fear of committing a
7771 string constant to memory too early. */
7772 if (tok != TOK_STR && tok != TOK_LSTR)
7773 expect("string constant");
7774 parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
7776 init_putv(type, sec, c);
7780 /* parse an initializer for type 't' if 'has_init' is non zero, and
7781 allocate space in local or global data space ('r' is either
7782 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7783 variable 'v' of scope 'scope' is declared before initializers
7784 are parsed. If 'v' is zero, then a reference to the new object
7785 is put in the value stack. If 'has_init' is 2, a special parsing
7786 is done to handle string constants. */
7787 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
7788 int has_init, int v, int scope)
7790 int size, align, addr;
7791 TokenString *init_str = NULL;
7793 Section *sec;
7794 Sym *flexible_array;
7795 Sym *sym = NULL;
7796 int saved_nocode_wanted = nocode_wanted;
7797 #ifdef CONFIG_TCC_BCHECK
7798 int bcheck = tcc_state->do_bounds_check && !NODATA_WANTED;
7799 #endif
7801 /* Always allocate static or global variables */
7802 if (v && (r & VT_VALMASK) == VT_CONST)
7803 nocode_wanted |= 0x80000000;
7805 flexible_array = NULL;
7806 if ((type->t & VT_BTYPE) == VT_STRUCT) {
7807 Sym *field = type->ref->next;
7808 if (field) {
7809 while (field->next)
7810 field = field->next;
7811 if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
7812 flexible_array = field;
7816 size = type_size(type, &align);
7817 /* If unknown size, we must evaluate it before
7818 evaluating initializers because
7819 initializers can generate global data too
7820 (e.g. string pointers or ISOC99 compound
7821 literals). It also simplifies local
7822 initializers handling */
7823 if (size < 0 || (flexible_array && has_init)) {
7824 if (!has_init)
7825 tcc_error("unknown type size");
7826 /* get all init string */
7827 if (has_init == 2) {
7828 init_str = tok_str_alloc();
7829 /* only get strings */
7830 while (tok == TOK_STR || tok == TOK_LSTR) {
7831 tok_str_add_tok(init_str);
7832 next();
7834 tok_str_add(init_str, -1);
7835 tok_str_add(init_str, 0);
7836 } else {
7837 skip_or_save_block(&init_str);
7839 unget_tok(0);
7841 /* compute size */
7842 begin_macro(init_str, 1);
7843 next();
7844 decl_initializer(type, NULL, 0, DIF_FIRST | DIF_SIZE_ONLY);
7845 /* prepare second initializer parsing */
7846 macro_ptr = init_str->str;
7847 next();
7849 /* if still unknown size, error */
7850 size = type_size(type, &align);
7851 if (size < 0)
7852 tcc_error("unknown type size");
7854 /* If there's a flex member and it was used in the initializer
7855 adjust size. */
7856 if (flexible_array &&
7857 flexible_array->type.ref->c > 0)
7858 size += flexible_array->type.ref->c
7859 * pointed_size(&flexible_array->type);
7860 /* take into account specified alignment if bigger */
7861 if (ad->a.aligned) {
7862 int speca = 1 << (ad->a.aligned - 1);
7863 if (speca > align)
7864 align = speca;
7865 } else if (ad->a.packed) {
7866 align = 1;
7869 if (!v && NODATA_WANTED)
7870 size = 0, align = 1;
7872 if ((r & VT_VALMASK) == VT_LOCAL) {
7873 sec = NULL;
7874 #ifdef CONFIG_TCC_BCHECK
7875 if (bcheck && v) {
7876 /* add padding between stack variables for bound checking */
7877 loc--;
7879 #endif
7880 loc = (loc - size) & -align;
7881 addr = loc;
7882 #ifdef CONFIG_TCC_BCHECK
7883 if (bcheck && v) {
7884 /* add padding between stack variables for bound checking */
7885 loc--;
7887 #endif
7888 if (v) {
7889 /* local variable */
7890 #ifdef CONFIG_TCC_ASM
7891 if (ad->asm_label) {
7892 int reg = asm_parse_regvar(ad->asm_label);
7893 if (reg >= 0)
7894 r = (r & ~VT_VALMASK) | reg;
7896 #endif
7897 sym = sym_push(v, type, r, addr);
7898 if (ad->cleanup_func) {
7899 Sym *cls = sym_push2(&all_cleanups,
7900 SYM_FIELD | ++cur_scope->cl.n, 0, 0);
7901 cls->prev_tok = sym;
7902 cls->next = ad->cleanup_func;
7903 cls->ncl = cur_scope->cl.s;
7904 cur_scope->cl.s = cls;
7907 sym->a = ad->a;
7908 } else {
7909 /* push local reference */
7910 vset(type, r, addr);
7912 } else {
7913 if (v && scope == VT_CONST) {
7914 /* see if the symbol was already defined */
7915 sym = sym_find(v);
7916 if (sym) {
7917 patch_storage(sym, ad, type);
7918 /* we accept several definitions of the same global variable. */
7919 if (!has_init && sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)
7920 goto no_alloc;
7924 /* allocate symbol in corresponding section */
7925 sec = ad->section;
7926 if (!sec) {
7927 if (has_init)
7928 sec = data_section;
7929 else if (tcc_state->nocommon)
7930 sec = bss_section;
7933 if (sec) {
7934 addr = section_add(sec, size, align);
7935 #ifdef CONFIG_TCC_BCHECK
7936 /* add padding if bound check */
7937 if (bcheck)
7938 section_add(sec, 1, 1);
7939 #endif
7940 } else {
7941 addr = align; /* SHN_COMMON is special, symbol value is align */
7942 sec = common_section;
7945 if (v) {
7946 if (!sym) {
7947 sym = sym_push(v, type, r | VT_SYM, 0);
7948 patch_storage(sym, ad, NULL);
7950 /* update symbol definition */
7951 put_extern_sym(sym, sec, addr, size);
7952 } else {
7953 /* push global reference */
7954 vpush_ref(type, sec, addr, size);
7955 sym = vtop->sym;
7956 vtop->r |= r;
7959 #ifdef CONFIG_TCC_BCHECK
7960 /* handles bounds now because the symbol must be defined
7961 before for the relocation */
7962 if (bcheck) {
7963 addr_t *bounds_ptr;
7965 greloca(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR, 0);
7966 /* then add global bound info */
7967 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t));
7968 bounds_ptr[0] = 0; /* relocated */
7969 bounds_ptr[1] = size;
7971 #endif
7974 if (type->t & VT_VLA) {
7975 int a;
7977 if (NODATA_WANTED)
7978 goto no_alloc;
7980 /* save current stack pointer */
7981 if (root_scope->vla.loc == 0) {
7982 struct scope *v = cur_scope;
7983 gen_vla_sp_save(loc -= PTR_SIZE);
7984 do v->vla.loc = loc; while ((v = v->prev));
7987 vla_runtime_type_size(type, &a);
7988 gen_vla_alloc(type, a);
7989 #if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
7990 /* on _WIN64, because of the function args scratch area, the
7991 result of alloca differs from RSP and is returned in RAX. */
7992 gen_vla_result(addr), addr = (loc -= PTR_SIZE);
7993 #endif
7994 gen_vla_sp_save(addr);
7995 cur_scope->vla.loc = addr;
7996 cur_scope->vla.num++;
7997 } else if (has_init) {
7998 size_t oldreloc_offset = 0;
7999 if (sec && sec->reloc)
8000 oldreloc_offset = sec->reloc->data_offset;
8001 decl_initializer(type, sec, addr, DIF_FIRST);
8002 if (sec && sec->reloc)
8003 squeeze_multi_relocs(sec, oldreloc_offset);
8004 /* patch flexible array member size back to -1, */
8005 /* for possible subsequent similar declarations */
8006 if (flexible_array)
8007 flexible_array->type.ref->c = -1;
8010 no_alloc:
8011 /* restore parse state if needed */
8012 if (init_str) {
8013 end_macro();
8014 next();
8017 nocode_wanted = saved_nocode_wanted;
8020 /* parse a function defined by symbol 'sym' and generate its code in
8021 'cur_text_section' */
8022 static void gen_function(Sym *sym)
8024 /* Initialize VLA state */
8025 struct scope f = { 0 };
8026 cur_scope = root_scope = &f;
8028 nocode_wanted = 0;
8029 ind = cur_text_section->data_offset;
8030 if (sym->a.aligned) {
8031 size_t newoff = section_add(cur_text_section, 0,
8032 1 << (sym->a.aligned - 1));
8033 gen_fill_nops(newoff - ind);
8035 /* NOTE: we patch the symbol size later */
8036 put_extern_sym(sym, cur_text_section, ind, 0);
8037 if (sym->type.ref->f.func_ctor)
8038 add_array (tcc_state, ".init_array", sym->c);
8039 if (sym->type.ref->f.func_dtor)
8040 add_array (tcc_state, ".fini_array", sym->c);
8042 funcname = get_tok_str(sym->v, NULL);
8043 func_ind = ind;
8044 func_vt = sym->type.ref->type;
8045 func_var = sym->type.ref->f.func_type == FUNC_ELLIPSIS;
8047 /* put debug symbol */
8048 tcc_debug_funcstart(tcc_state, sym);
8049 /* push a dummy symbol to enable local sym storage */
8050 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8051 local_scope = 1; /* for function parameters */
8052 gfunc_prolog(sym);
8053 local_scope = 0;
8054 rsym = 0;
8055 clear_temp_local_var_list();
8056 block(0);
8057 gsym(rsym);
8058 nocode_wanted = 0;
8059 /* reset local stack */
8060 pop_local_syms(&local_stack, NULL, 0, func_var);
8061 gfunc_epilog();
8062 cur_text_section->data_offset = ind;
8063 local_scope = 0;
8064 label_pop(&global_label_stack, NULL, 0);
8065 sym_pop(&all_cleanups, NULL, 0);
8066 /* patch symbol size */
8067 elfsym(sym)->st_size = ind - func_ind;
8068 /* end of function */
8069 tcc_debug_funcend(tcc_state, ind - func_ind);
8070 /* It's better to crash than to generate wrong code */
8071 cur_text_section = NULL;
8072 funcname = ""; /* for safety */
8073 func_vt.t = VT_VOID; /* for safety */
8074 func_var = 0; /* for safety */
8075 ind = 0; /* for safety */
8076 nocode_wanted = 0x80000000;
8077 check_vstack();
8078 /* do this after funcend debug info */
8079 next();
8082 static void gen_inline_functions(TCCState *s)
8084 Sym *sym;
8085 int inline_generated, i;
8086 struct InlineFunc *fn;
8088 tcc_open_bf(s, ":inline:", 0);
8089 /* iterate while inline function are referenced */
8090 do {
8091 inline_generated = 0;
8092 for (i = 0; i < s->nb_inline_fns; ++i) {
8093 fn = s->inline_fns[i];
8094 sym = fn->sym;
8095 if (sym && (sym->c || !(sym->type.t & VT_INLINE))) {
8096 /* the function was used or forced (and then not internal):
8097 generate its code and convert it to a normal function */
8098 fn->sym = NULL;
8099 tcc_debug_putfile(s, fn->filename);
8100 begin_macro(fn->func_str, 1);
8101 next();
8102 cur_text_section = text_section;
8103 gen_function(sym);
8104 end_macro();
8106 inline_generated = 1;
8109 } while (inline_generated);
8110 tcc_close();
8113 static void free_inline_functions(TCCState *s)
8115 int i;
8116 /* free tokens of unused inline functions */
8117 for (i = 0; i < s->nb_inline_fns; ++i) {
8118 struct InlineFunc *fn = s->inline_fns[i];
8119 if (fn->sym)
8120 tok_str_free(fn->func_str);
8122 dynarray_reset(&s->inline_fns, &s->nb_inline_fns);
8125 /* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
8126 if parsing old style parameter decl list (and FUNC_SYM is set then) */
8127 static int decl0(int l, int is_for_loop_init, Sym *func_sym)
8129 int v, has_init, r;
8130 CType type, btype;
8131 Sym *sym;
8132 AttributeDef ad, adbase;
8134 while (1) {
8135 if (tok == TOK_STATIC_ASSERT) {
8136 CString error_str;
8137 int c;
8139 next();
8140 skip('(');
8141 c = expr_const();
8143 if (tok == ')') {
8144 if (!c)
8145 tcc_error("_Static_assert fail");
8146 next();
8147 goto static_assert_out;
8150 skip(',');
8151 parse_mult_str(&error_str, "string constant");
8152 if (c == 0)
8153 tcc_error("%s", (char *)error_str.data);
8154 cstr_free(&error_str);
8155 skip(')');
8156 static_assert_out:
8157 skip(';');
8158 continue;
8160 if (!parse_btype(&btype, &adbase)) {
8161 if (is_for_loop_init)
8162 return 0;
8163 /* skip redundant ';' if not in old parameter decl scope */
8164 if (tok == ';' && l != VT_CMP) {
8165 next();
8166 continue;
8168 if (l != VT_CONST)
8169 break;
8170 if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8171 /* global asm block */
8172 asm_global_instr();
8173 continue;
8175 if (tok >= TOK_UIDENT) {
8176 /* special test for old K&R protos without explicit int
8177 type. Only accepted when defining global data */
8178 btype.t = VT_INT;
8179 } else {
8180 if (tok != TOK_EOF)
8181 expect("declaration");
8182 break;
8185 if (tok == ';') {
8186 if ((btype.t & VT_BTYPE) == VT_STRUCT) {
8187 int v = btype.ref->v;
8188 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)
8189 tcc_warning("unnamed struct/union that defines no instances");
8190 next();
8191 continue;
8193 if (IS_ENUM(btype.t)) {
8194 next();
8195 continue;
8198 while (1) { /* iterate thru each declaration */
8199 type = btype;
8200 /* If the base type itself was an array type of unspecified
8201 size (like in 'typedef int arr[]; arr x = {1};') then
8202 we will overwrite the unknown size by the real one for
8203 this decl. We need to unshare the ref symbol holding
8204 that size. */
8205 if ((type.t & VT_ARRAY) && type.ref->c < 0) {
8206 type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c);
8208 ad = adbase;
8209 type_decl(&type, &ad, &v, TYPE_DIRECT);
8210 #if 0
8212 char buf[500];
8213 type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL));
8214 printf("type = '%s'\n", buf);
8216 #endif
8217 if ((type.t & VT_BTYPE) == VT_FUNC) {
8218 if ((type.t & VT_STATIC) && (l == VT_LOCAL))
8219 tcc_error("function without file scope cannot be static");
8220 /* if old style function prototype, we accept a
8221 declaration list */
8222 sym = type.ref;
8223 if (sym->f.func_type == FUNC_OLD && l == VT_CONST)
8224 decl0(VT_CMP, 0, sym);
8225 #ifdef TCC_TARGET_MACHO
8226 if (sym->f.func_alwinl
8227 && ((type.t & (VT_EXTERN | VT_INLINE))
8228 == (VT_EXTERN | VT_INLINE))) {
8229 /* always_inline functions must be handled as if they
8230 don't generate multiple global defs, even if extern
8231 inline, i.e. GNU inline semantics for those. Rewrite
8232 them into static inline. */
8233 type.t &= ~VT_EXTERN;
8234 type.t |= VT_STATIC;
8236 #endif
8237 /* always compile 'extern inline' */
8238 if (type.t & VT_EXTERN)
8239 type.t &= ~VT_INLINE;
8242 if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
8243 ad.asm_label = asm_label_instr();
8244 /* parse one last attribute list, after asm label */
8245 parse_attribute(&ad);
8246 #if 0
8247 /* gcc does not allow __asm__("label") with function definition,
8248 but why not ... */
8249 if (tok == '{')
8250 expect(";");
8251 #endif
8254 #ifdef TCC_TARGET_PE
8255 if (ad.a.dllimport || ad.a.dllexport) {
8256 if (type.t & VT_STATIC)
8257 tcc_error("cannot have dll linkage with static");
8258 if (type.t & VT_TYPEDEF) {
8259 tcc_warning("'%s' attribute ignored for typedef",
8260 ad.a.dllimport ? (ad.a.dllimport = 0, "dllimport") :
8261 (ad.a.dllexport = 0, "dllexport"));
8262 } else if (ad.a.dllimport) {
8263 if ((type.t & VT_BTYPE) == VT_FUNC)
8264 ad.a.dllimport = 0;
8265 else
8266 type.t |= VT_EXTERN;
8269 #endif
8270 if (tok == '{') {
8271 if (l != VT_CONST)
8272 tcc_error("cannot use local functions");
8273 if ((type.t & VT_BTYPE) != VT_FUNC)
8274 expect("function definition");
8276 /* reject abstract declarators in function definition
8277 make old style params without decl have int type */
8278 sym = type.ref;
8279 while ((sym = sym->next) != NULL) {
8280 if (!(sym->v & ~SYM_FIELD))
8281 expect("identifier");
8282 if (sym->type.t == VT_VOID)
8283 sym->type = int_type;
8286 /* apply post-declaraton attributes */
8287 merge_funcattr(&type.ref->f, &ad.f);
8289 /* put function symbol */
8290 type.t &= ~VT_EXTERN;
8291 sym = external_sym(v, &type, 0, &ad);
8293 /* static inline functions are just recorded as a kind
8294 of macro. Their code will be emitted at the end of
8295 the compilation unit only if they are used */
8296 if (sym->type.t & VT_INLINE) {
8297 struct InlineFunc *fn;
8298 fn = tcc_malloc(sizeof *fn + strlen(file->filename));
8299 strcpy(fn->filename, file->filename);
8300 fn->sym = sym;
8301 skip_or_save_block(&fn->func_str);
8302 dynarray_add(&tcc_state->inline_fns,
8303 &tcc_state->nb_inline_fns, fn);
8304 } else {
8305 /* compute text section */
8306 cur_text_section = ad.section;
8307 if (!cur_text_section)
8308 cur_text_section = text_section;
8309 gen_function(sym);
8311 break;
8312 } else {
8313 if (l == VT_CMP) {
8314 /* find parameter in function parameter list */
8315 for (sym = func_sym->next; sym; sym = sym->next)
8316 if ((sym->v & ~SYM_FIELD) == v)
8317 goto found;
8318 tcc_error("declaration for parameter '%s' but no such parameter",
8319 get_tok_str(v, NULL));
8320 found:
8321 if (type.t & VT_STORAGE) /* 'register' is okay */
8322 tcc_error("storage class specified for '%s'",
8323 get_tok_str(v, NULL));
8324 if (sym->type.t != VT_VOID)
8325 tcc_error("redefinition of parameter '%s'",
8326 get_tok_str(v, NULL));
8327 convert_parameter_type(&type);
8328 sym->type = type;
8329 } else if (type.t & VT_TYPEDEF) {
8330 /* save typedefed type */
8331 /* XXX: test storage specifiers ? */
8332 sym = sym_find(v);
8333 if (sym && sym->sym_scope == local_scope) {
8334 if (!is_compatible_types(&sym->type, &type)
8335 || !(sym->type.t & VT_TYPEDEF))
8336 tcc_error("incompatible redefinition of '%s'",
8337 get_tok_str(v, NULL));
8338 sym->type = type;
8339 } else {
8340 sym = sym_push(v, &type, 0, 0);
8342 sym->a = ad.a;
8343 sym->f = ad.f;
8344 } else if ((type.t & VT_BTYPE) == VT_VOID
8345 && !(type.t & VT_EXTERN)) {
8346 tcc_error("declaration of void object");
8347 } else {
8348 r = 0;
8349 if ((type.t & VT_BTYPE) == VT_FUNC) {
8350 /* external function definition */
8351 /* specific case for func_call attribute */
8352 type.ref->f = ad.f;
8353 } else if (!(type.t & VT_ARRAY)) {
8354 /* not lvalue if array */
8355 r |= VT_LVAL;
8357 has_init = (tok == '=');
8358 if (has_init && (type.t & VT_VLA))
8359 tcc_error("variable length array cannot be initialized");
8360 if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST))
8361 || (type.t & VT_BTYPE) == VT_FUNC
8362 /* as with GCC, uninitialized global arrays with no size
8363 are considered extern: */
8364 || ((type.t & VT_ARRAY) && !has_init
8365 && l == VT_CONST && type.ref->c < 0)
8367 /* external variable or function */
8368 type.t |= VT_EXTERN;
8369 sym = external_sym(v, &type, r, &ad);
8370 } else {
8371 if (type.t & VT_STATIC)
8372 r |= VT_CONST;
8373 else
8374 r |= l;
8375 if (has_init)
8376 next();
8377 else if (l == VT_CONST)
8378 /* uninitialized global variables may be overridden */
8379 type.t |= VT_EXTERN;
8380 decl_initializer_alloc(&type, &ad, r, has_init, v, l);
8383 if (tok != ',') {
8384 if (is_for_loop_init)
8385 return 1;
8386 skip(';');
8387 break;
8389 next();
8393 return 0;
8396 static void decl(int l)
8398 decl0(l, 0, NULL);
8401 /* ------------------------------------------------------------------------- */
8402 #undef gjmp_addr
8403 #undef gjmp
8404 /* ------------------------------------------------------------------------- */