History

added CType structure for type storage - supressed define hash table - added time dump in bench

bellard committed 24 years ago

Commit 9620fd11 parent 6cd2d5d2 files · +664 −536Browse files

Unified · Split

2 files
  1. i386-gen.c +40 −36
  2. tcc.c +624 −500
i386-gen.c+40−36
@@ -185,13 +185,13 @@ void load(int r, SValue *sv) · 184 unmodified lines
185185 SValue v1;
186186
187187 fr = sv->r;
188− ft = sv->t;
188+ ft = sv->type.t;
189189 fc = sv->c.ul;
190190
191191 v = fr & VT_VALMASK;
192192 if (fr & VT_LVAL) {
193193 if (v == VT_LLOCAL) {
194− v1.t = VT_INT;
194+ v1.type.t = VT_INT;
195195 v1.r = VT_LOCAL | VT_LVAL;
196196 v1.c.ul = fc;
197197 load(r, &v1);
@@ -248,7 +248,7 @@ void store(int r, SValue *v) · 50 unmodified lines
248248 {
249249 int fr, bt, ft, fc;
250250
251− ft = v->t;
251+ ft = v->type.t;
252252 fc = v->c.ul;
253253 fr = v->r & VT_VALMASK;
254254 bt = ft & VT_BTYPE;
@@ -293,8 +293,8 @@ void gfunc_param(GFuncContext *c) · 38 unmodified lines
293293 {
294294 int size, align, r;
295295
296− if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
297− size = type_size(vtop->t, &align);
296+ if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
297+ size = type_size(&vtop->type, &align);
298298 /* align to stack align size */
299299 size = (size + 3) & ~3;
300300 /* allocate the necessary size on stack */
@@ -303,15 +303,15 @@ void gfunc_param(GFuncContext *c) · 2 unmodified lines
303303 r = get_reg(RC_INT);
304304 o(0x89); /* mov %esp, r */
305305 o(0xe0 + r);
306− vset(vtop->t, r | VT_LVAL, 0);
306+ vset(&vtop->type, r | VT_LVAL, 0);
307307 vswap();
308308 vstore();
309309 c->args_size += size;
310− } else if (is_float(vtop->t)) {
310+ } else if (is_float(vtop->type.t)) {
311311 gv(RC_FLOAT); /* only one float register */
312− if ((vtop->t & VT_BTYPE) == VT_FLOAT)
312+ if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
313313 size = 4;
314− else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
314+ else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
315315 size = 8;
316316 else
317317 size = 12;
@@ -327,7 +327,7 @@ void gfunc_param(GFuncContext *c) · 9 unmodified lines
327327 /* simple type (currently always same size) */
328328 /* XXX: implicit cast ? */
329329 r = gv(RC_INT);
330− if ((vtop->t & VT_BTYPE) == VT_LLONG) {
330+ if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
331331 size = 8;
332332 o(0x50 + vtop->r2); /* push r */
333333 } else {
@@ -384,31 +384,32 @@ void gfunc_call(GFuncContext *c) · 50 unmodified lines
384384 }
385385
386386 /* generate function prolog of type 't' */
387−void gfunc_prolog(int t)
387+void gfunc_prolog(CType *func_type)
388388 {
389− int addr, align, size, u, func_call;
389+ int addr, align, size, func_call;
390390 Sym *sym;
391+ CType *type;
391392
392− sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
393+ sym = func_type->ref;
393394 func_call = sym->r;
394395 addr = 8;
395396 /* if the function returns a structure, then add an
396397 implicit pointer parameter */
397− func_vt = sym->t;
398− if ((func_vt & VT_BTYPE) == VT_STRUCT) {
398+ func_vt = sym->type;
399+ if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
399400 func_vc = addr;
400401 addr += 4;
401402 }
402403 /* define parameters */
403404 while ((sym = sym->next) != NULL) {
404− u = sym->t;
405− sym_push(sym->v & ~SYM_FIELD, u,
405+ type = &sym->type;
406+ sym_push(sym->v & ~SYM_FIELD, type,
406407 VT_LOCAL | VT_LVAL, addr);
407− size = type_size(u, &align);
408+ size = type_size(type, &align);
408409 size = (size + 3) & ~3;
409410 #ifdef FUNC_STRUCT_PARAM_AS_PTR
410411 /* structs are passed as pointer */
411− if ((u & VT_BTYPE) == VT_STRUCT) {
412+ if ((type->t & VT_BTYPE) == VT_STRUCT) {
412413 size = 4;
413414 }
414415 #endif
@@ -442,12 +443,12 @@ void gfunc_epilog(void) · 27 unmodified lines
442443 /* generate bound local allocation */
443444 saved_ind = ind;
444445 ind = func_sub_sp_offset + 4;
445− sym_data = get_sym_ref(char_pointer_type, lbounds_section,
446+ sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
446447 func_bound_offset, lbounds_section->data_offset);
447448 greloc(cur_text_section, sym_data,
448449 ind + 1, R_386_32);
449450 oad(0xb8, 0); /* mov %eax, xxx */
450− sym = external_global_sym(TOK___bound_local_new, func_old_type, 0);
451+ sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
451452 greloc(cur_text_section, sym,
452453 ind + 1, R_386_PC32);
453454 oad(0xe8, -4);
@@ -457,7 +458,7 @@ void gfunc_epilog(void) · 3 unmodified lines
457458 greloc(cur_text_section, sym_data,
458459 ind + 1, R_386_32);
459460 oad(0xb8, 0); /* mov %eax, xxx */
460− sym = external_global_sym(TOK___bound_local_delete, func_old_type, 0);
461+ sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
461462 greloc(cur_text_section, sym,
462463 ind + 1, R_386_PC32);
463464 oad(0xe8, -4);
@@ -518,7 +519,7 @@ int gtst(int inv, int t) · 54 unmodified lines
518519 gsym(vtop->c.i);
519520 }
520521 } else {
521− if (is_float(vtop->t)) {
522+ if (is_float(vtop->type.t)) {
522523 vpushi(0);
523524 gen_op(TOK_NE);
524525 }
@@ -572,8 +573,8 @@ void gen_opi(int op) · 47 unmodified lines
572573 }
573574 vtop--;
574575 if (op >= TOK_ULT && op <= TOK_GT) {
575− vtop--;
576− vset(VT_INT, VT_CMP, op);
576+ vtop->r = VT_CMP;
577+ vtop->c.i = op;
577578 }
578579 break;
579580 case '-':
@@ -731,7 +732,7 @@ void gen_opf(int op) · 151 unmodified lines
731732 vtop->c.i = op;
732733 } else {
733734 /* no memory reference possible for long double operations */
734− if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) {
735+ if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
735736 load(REG_ST0, vtop);
736737 swapped = !swapped;
737738 }
@@ -755,7 +756,7 @@ void gen_opf(int op) · 17 unmodified lines
755756 a++;
756757 break;
757758 }
758− ft = vtop->t;
759+ ft = vtop->type.t;
759760 fc = vtop->c.ul;
760761 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
761762 o(0xde); /* fxxxp %st, %st(1) */
@@ -766,7 +767,7 @@ void gen_opf(int op) · 4 unmodified lines
766767 if ((r & VT_VALMASK) == VT_LLOCAL) {
767768 SValue v1;
768769 r = get_reg(RC_INT);
769− v1.t = VT_INT;
770+ v1.type.t = VT_INT;
770771 v1.r = VT_LOCAL | VT_LVAL;
771772 v1.c.ul = fc;
772773 load(r, &v1);
@@ -789,14 +790,14 @@ void gen_cvt_itof(int t) · 16 unmodified lines
789790 {
790791 save_reg(REG_ST0);
791792 gv(RC_INT);
792− if ((vtop->t & VT_BTYPE) == VT_LLONG) {
793+ if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
793794 /* signed long long to float/double/long double (unsigned case
794795 is handled generically) */
795796 o(0x50 + vtop->r2); /* push r2 */
796797 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
797798 o(0x242cdf); /* fildll (%esp) */
798799 o(0x08c483); /* add $8, %esp */
799− } else if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
800+ } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
800801 (VT_INT | VT_UNSIGNED)) {
801802 /* unsigned int to float/double/long double */
802803 o(0x6a); /* push $0 */
@@ -819,6 +820,9 @@ void gen_cvt_ftoi(int t) · 16 unmodified lines
819820 {
820821 int r, r2, size;
821822 Sym *sym;
823+ CType ushort_type;
824+
825+ ushort_type.t = VT_SHORT | VT_UNSIGNED;
822826
823827 gv(RC_FLOAT);
824828 if (t != VT_INT)
@@ -828,7 +832,7 @@ void gen_cvt_ftoi(int t) · 3 unmodified lines
828832
829833 o(0x2dd9); /* ldcw xxx */
830834 sym = external_global_sym(TOK___tcc_int_fpu_control,
831− VT_SHORT | VT_UNSIGNED, VT_LVAL);
835+ &ushort_type, VT_LVAL);
832836 greloc(cur_text_section, sym,
833837 ind, R_386_32);
834838 gen_le32(0);
@@ -841,7 +845,7 @@ void gen_cvt_ftoi(int t) · 6 unmodified lines
841845 o(0x24);
842846 o(0x2dd9); /* ldcw xxx */
843847 sym = external_global_sym(TOK___tcc_fpu_control,
844− VT_SHORT | VT_UNSIGNED, VT_LVAL);
848+ &ushort_type, VT_LVAL);
845849 greloc(cur_text_section, sym,
846850 ind, R_386_32);
847851 gen_le32(0);
@@ -889,7 +893,7 @@ void gen_bounded_ptr_add(void) · 41 unmodified lines
889893 vtop -= 2;
890894 save_regs(0);
891895 /* do a fast function call */
892− sym = external_global_sym(TOK___bound_ptr_add, func_old_type, 0);
896+ sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
893897 greloc(cur_text_section, sym,
894898 ind + 1, R_386_PC32);
895899 oad(0xe8, -4);
@@ -911,14 +915,14 @@ void gen_bounded_ptr_deref(void) · 15 unmodified lines
911915
912916 size = 0;
913917 /* XXX: put that code in generic part of tcc */
914− if (!is_float(vtop->t)) {
918+ if (!is_float(vtop->type.t)) {
915919 if (vtop->r & VT_LVAL_BYTE)
916920 size = 1;
917921 else if (vtop->r & VT_LVAL_SHORT)
918922 size = 2;
919923 }
920924 if (!size)
921− size = type_size(vtop->t, &align);
925+ size = type_size(&vtop->type, &align);
922926 switch(size) {
923927 case 1: func = TOK___bound_ptr_indir1; break;
924928 case 2: func = TOK___bound_ptr_indir2; break;
@@ -935,7 +939,7 @@ void gen_bounded_ptr_deref(void) · 10 unmodified lines
935939 /* patch relocation */
936940 /* XXX: find a better solution ? */
937941 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
938− sym = external_global_sym(func, func_old_type, 0);
942+ sym = external_global_sym(func, &func_old_type, 0);
939943 if (!sym->c)
940944 put_extern_sym(sym, NULL, 0, 0);
941945 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
tcc.c+624−500
@@ -28,7 +28,11 @@ · 27 unmodified lines
2828 #include <unistd.h>
2929 #include <fcntl.h>
3030 #include <setjmp.h>
31+#ifdef WIN32
32+#include <sys/timeb.h>
33+#endif
3134 #ifndef WIN32
35+#include <sys/time.h>
3236 #include <sys/ucontext.h>
3337 #endif
3438 #include "elf.h"
@@ -80,6 +84,7 @@ · 45 unmodified lines
8084 /* token symbol management */
8185 typedef struct TokenSym {
8286 struct TokenSym *hash_next;
87+ struct Sym *sym_define; /* direct pointer to define */
8388 int tok; /* token number */
8489 int len;
8590 char str[1];
@@ -92,6 +97,12 @@ typedef struct CString { · 6 unmodified lines
9297 void *data_allocated; /* if non NULL, data has been malloced */
9398 } CString;
9499
100+/* type definition */
101+typedef struct CType {
102+ int t;
103+ struct Sym *ref;
104+} CType;
105+
95106 /* constant value */
96107 typedef union CValue {
97108 long double ld;
@@ -109,7 +120,7 @@ typedef union CValue { · 11 unmodified lines
109120
110121 /* value on stack */
111122 typedef struct SValue {
112− int t; /* type */
123+ CType type; /* type */
113124 unsigned short r; /* register + flags */
114125 unsigned short r2; /* second register, used for 'long long'
115126 type. If not used, set to VT_CONST */
@@ -120,9 +131,9 @@ typedef struct SValue { · 4 unmodified lines
120131 /* symbol management */
121132 typedef struct Sym {
122133 int v; /* symbol token */
123− int t; /* associated type */
124134 int r; /* associated register */
125135 int c; /* associated number */
136+ CType type; /* associated type */
126137 struct Sym *next; /* next related symbol */
127138 struct Sym *prev; /* prev symbol in stack */
128139 struct Sym *hash_next; /* next symbol in hash table */
@@ -273,20 +284,22 @@ int const_wanted; /* true if constant wanted */ · 144 unmodified lines
273284 int nocode_wanted; /* true if no code generation wanted for an expression */
274285 int global_expr; /* true if compound literals must be allocated
275286 globally (used during initializers parsing */
276−int func_vt, func_vc; /* current function return type (used by
277− return instruction) */
287+CType func_vt; /* current function return type (used by return
288+ instruction) */
289+int func_vc;
278290 int last_line_num, last_ind, func_ind; /* debug last line number and pc */
279291 int tok_ident;
280292 TokenSym **table_ident;
281293 TokenSym *hash_ident[TOK_HASH_SIZE];
282294 char token_buf[STRING_MAX_SIZE + 1];
283295 char *funcname;
284−SymStack define_stack, global_stack, local_stack, label_stack;
296+SymStack global_stack, local_stack, label_stack;
297+Sym *define_stack;
285298
286299 SValue vstack[VSTACK_SIZE], *vtop;
287300 int *macro_ptr, *macro_ptr_allocated;
288−int char_pointer_type;
289−int func_old_type;
301+/* some predefined types */
302+CType char_pointer_type, func_old_type, int_type;
290303
291304 /* compile with debug symbol (and use them if error during execution) */
292305 int do_debug = 0;
@@ -511,6 +524,7 @@ static const char *tcc_keywords = · 218 unmodified lines
511524
512525 #ifdef WIN32
513526 #define snprintf _snprintf
527+#define vsnprintf _vsnprintf
514528 #endif
515529
516530 #if defined(WIN32) || defined(TCC_UCLIBC)
@@ -535,16 +549,16 @@ static char *pstrcat(char *buf, int buf_size, const char *s); · 18 unmodified lines
535549 static void sum(int l);
536550 static void next(void);
537551 static void next_nomacro(void);
538−static int parse_expr_type(void);
539−static int expr_type(void);
540−static int unary_type(void);
552+static void parse_expr_type(CType *type);
553+static void expr_type(CType *type);
554+static void unary_type(CType *type);
541555 static int expr_const(void);
542556 static void expr_eq(void);
543557 static void gexpr(void);
544558 static void decl(int l);
545−static void decl_initializer(int t, Section *sec, unsigned long c,
559+static void decl_initializer(CType *type, Section *sec, unsigned long c,
546560 int first, int size_only);
547−static void decl_initializer_alloc(int t, AttributeDef *ad, int r,
561+static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
548562 int has_init, int v, int scope);
549563 int gv(int rc);
550564 void gv2(int rc1, int rc2);
@@ -561,30 +575,29 @@ static void macro_subst(TokenString *tok_str, · 10 unmodified lines
561575 int save_reg_forced(int r);
562576 void gen_op(int op);
563577 void force_charshort_cast(int t);
564−void gen_cast(int t);
578+static void gen_cast(CType *type);
565579 void vstore(void);
566580 Sym *sym_find(int v);
567−Sym *sym_push(int v, int t, int r, int c);
581+Sym *sym_push(int v, CType *type, int r, int c);
568582
569583 /* type handling */
570−int type_size(int t, int *a);
571−int pointed_type(int t);
572−int pointed_size(int t);
584+int type_size(CType *type, int *a);
585+static inline CType *pointed_type(CType *type);
586+static int pointed_size(CType *type);
573587 static int lvalue_type(int t);
574−int is_compatible_types(int t1, int t2);
575−int parse_btype(int *type_ptr, AttributeDef *ad);
576−int type_decl(AttributeDef *ad, int *v, int t, int td);
588+static int is_compatible_types(CType *type1, CType *type2);
589+static int parse_btype(CType *type, AttributeDef *ad);
590+static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
577591
578592 void error(const char *fmt, ...);
579−void rt_error(struct ucontext *uc, const char *fmt, ...);
580593 void vpushi(int v);
581−void vset(int t, int r, int v);
594+void vset(CType *type, int r, int v);
582595 void type_to_str(char *buf, int buf_size,
583− int t, const char *varstr);
596+ CType *type, const char *varstr);
584597 char *get_tok_str(int v, CValue *cv);
585−static Sym *get_sym_ref(int t, Section *sec,
598+static Sym *get_sym_ref(CType *type, Section *sec,
586599 unsigned long offset, unsigned long size);
587−static Sym *external_global_sym(int v, int u, int r);
600+static Sym *external_global_sym(int v, CType *type, int r);
588601
589602 /* section generation */
590603 static void section_realloc(Section *sec, unsigned long new_size);
@@ -632,6 +645,7 @@ static inline int is_float(int t) · 41 unmodified lines
632645 #define RTLD_LAZY 0x001
633646 #define RTLD_NOW 0x002
634647 #define RTLD_GLOBAL 0x100
648+#define RTLD_DEFAULT NULL
635649
636650 /* dummy function for profiling */
637651 void *dlopen(const char *filename, int flag)
@@ -895,18 +909,17 @@ static void put_extern_sym(Sym *sym, Section *section, · 257 unmodified lines
895909 int sym_type, sym_bind, sh_num, info;
896910 Elf32_Sym *esym;
897911 const char *name;
898− char buf[32];
899912
900913 if (section)
901914 sh_num = section->sh_num;
902915 else
903916 sh_num = SHN_UNDEF;
904917 if (!sym->c) {
905− if ((sym->t & VT_BTYPE) == VT_FUNC)
918+ if ((sym->type.t & VT_BTYPE) == VT_FUNC)
906919 sym_type = STT_FUNC;
907920 else
908921 sym_type = STT_OBJECT;
909− if (sym->t & VT_STATIC)
922+ if (sym->type.t & VT_STATIC)
910923 sym_bind = STB_LOCAL;
911924 else
912925 sym_bind = STB_GLOBAL;
@@ -914,6 +927,8 @@ static void put_extern_sym(Sym *sym, Section *section, · 1 unmodified line
914927 name = get_tok_str(sym->v, NULL);
915928 #ifdef CONFIG_TCC_BCHECK
916929 if (do_bounds_check) {
930+ char buf[32];
931+
917932 /* XXX: avoid doing that for statics ? */
918933 /* if bound checking is activated, we change some function
919934 names by adding the "__bound" prefix */
@@ -1131,6 +1146,7 @@ TokenSym *tok_alloc(const char *str, int len) · 211 unmodified lines
11311146 ts = tcc_malloc(sizeof(TokenSym) + len);
11321147 table_ident[i] = ts;
11331148 ts->tok = tok_ident++;
1149+ ts->sym_define = NULL;
11341150 ts->len = len;
11351151 ts->hash_next = NULL;
11361152 memcpy(ts->str, str, len + 1);
@@ -1331,7 +1347,7 @@ Sym *sym_push2(Sym **ps, int v, int t, int c) · 194 unmodified lines
13311347 Sym *s;
13321348 s = tcc_malloc(sizeof(Sym));
13331349 s->v = v;
1334− s->t = t;
1350+ s->type.t = t;
13351351 s->c = c;
13361352 s->next = NULL;
13371353 /* add in stack */
@@ -1393,13 +1409,14 @@ Sym *sym_find(int v) · 55 unmodified lines
13931409 }
13941410
13951411 /* push a given symbol on the symbol stack */
1396−Sym *sym_push(int v, int t, int r, int c)
1412+Sym *sym_push(int v, CType *type, int r, int c)
13971413 {
13981414 Sym *s;
13991415 if (local_stack.top)
1400− s = sym_push1(&local_stack, v, t, c);
1416+ s = sym_push1(&local_stack, v, type->t, c);
14011417 else
1402− s = sym_push1(&global_stack, v, t, c);
1418+ s = sym_push1(&global_stack, v, type->t, c);
1419+ s->type.ref = type->ref;
14031420 s->r = r;
14041421 return s;
14051422 }
@@ -1422,21 +1439,6 @@ void sym_pop(SymStack *st, Sym *b) · 16 unmodified lines
14221439 st->top = b;
14231440 }
14241441
1425−/* undefined a hashed symbol (used for #undef). Its name is set to
1426− zero */
1427−void sym_undef(SymStack *st, Sym *s)
1428−{
1429− Sym **ss;
1430− ss = &st->hash[HASH_SYM(s->v)];
1431− while (*ss != NULL) {
1432− if (*ss == s)
1433− break;
1434− ss = &(*ss)->hash_next;
1435− }
1436− *ss = s->hash_next;
1437− s->v = 0;
1438−}
1439−
14401442 /* I/O layer */
14411443
14421444 BufferedFile *tcc_open(TCCState *s1, const char *filename)
@@ -1794,6 +1796,55 @@ static int tok_get(int **tok_str, CValue *cv) · 351 unmodified lines
17941796 return t;
17951797 }
17961798
1799+/* defines handling */
1800+static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
1801+{
1802+ Sym *s;
1803+
1804+ s = sym_push2(&define_stack, v, macro_type, (int)str);
1805+ s->next = first_arg;
1806+ table_ident[v - TOK_IDENT]->sym_define = s;
1807+}
1808+
1809+/* undefined a define symbol. Its name is just set to zero */
1810+static void define_undef(Sym *s)
1811+{
1812+ int v;
1813+ v = s->v;
1814+ if (v >= TOK_IDENT && v < tok_ident)
1815+ table_ident[v - TOK_IDENT]->sym_define = NULL;
1816+ s->v = 0;
1817+}
1818+
1819+static inline Sym *define_find(int v)
1820+{
1821+ v -= TOK_IDENT;
1822+ if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1823+ return NULL;
1824+ return table_ident[v]->sym_define;
1825+}
1826+
1827+/* free define stack until top reaches 'b' */
1828+static void free_defines(Sym *b)
1829+{
1830+ Sym *top, *top1;
1831+ int v;
1832+
1833+ top = define_stack;
1834+ while (top != b) {
1835+ top1 = top->prev;
1836+ /* do not free args or predefined defines */
1837+ if (top->c)
1838+ tok_str_free((int *)top->c);
1839+ v = top->v;
1840+ if (v >= TOK_IDENT && v < tok_ident)
1841+ table_ident[v - TOK_IDENT]->sym_define = NULL;
1842+ tcc_free(top);
1843+ top = top1;
1844+ }
1845+ define_stack = b;
1846+}
1847+
17971848 /* eval an expression for #if/#elif */
17981849 int expr_preprocess(void)
17991850 {
@@ -1808,7 +1859,7 @@ int expr_preprocess(void) · 8 unmodified lines
18081859 t = tok;
18091860 if (t == '(')
18101861 next_nomacro();
1811− c = sym_find1(&define_stack, tok) != 0;
1862+ c = define_find(tok) != 0;
18121863 if (t == '(')
18131864 next_nomacro();
18141865 tok = TOK_CINT;
@@ -1855,6 +1906,8 @@ void parse_define(void) · 40 unmodified lines
18551906 TokenString str;
18561907
18571908 v = tok;
1909+ if (v < TOK_IDENT)
1910+ error("invalid macro name '%s'", get_tok_str(tok, &tokc));
18581911 /* XXX: should check if same macro (ANSI) */
18591912 first = NULL;
18601913 t = MACRO_OBJ;
@@ -1876,7 +1929,7 @@ void parse_define(void) · 15 unmodified lines
18761929 }
18771930 if (varg < TOK_IDENT)
18781931 error("badly punctuated parameter list");
1879− s = sym_push1(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
1932+ s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
18801933 *ps = s;
18811934 ps = &s->next;
18821935 if (tok != ',')
@@ -1897,8 +1950,7 @@ void parse_define(void) · 14 unmodified lines
18971950 printf("define %s %d: ", get_tok_str(v, NULL), t);
18981951 tok_print(str.str);
18991952 #endif
1900− s = sym_push1(&define_stack, v, t, (int)str.str);
1901− s->next = first;
1953+ define_push(v, t, str.str, first);
19021954 }
19031955
19041956 /* XXX: use a token or a hash table to accelerate matching ? */
@@ -1965,10 +2017,10 @@ void preprocess(void) · 60 unmodified lines
19652017 parse_define();
19662018 } else if (tok == TOK_UNDEF) {
19672019 next_nomacro();
1968− s = sym_find1(&define_stack, tok);
2020+ s = define_find(tok);
19692021 /* undefine symbol by putting an invalid name */
19702022 if (s)
1971− sym_undef(&define_stack, s);
2023+ define_undef(s);
19722024 } else if (tok == TOK_INCLUDE) {
19732025 skip_spaces();
19742026 if (ch == '<') {
@@ -2022,7 +2074,7 @@ void preprocess(void) · 47 unmodified lines
20222074
20232075 ch = '\n';
20242076 e = search_cached_include(s1, c, buf);
2025− if (e && sym_find1(&define_stack, e->ifndef_macro)) {
2077+ if (e && define_find(e->ifndef_macro)) {
20262078 /* no need to parse the include because the 'ifndef macro'
20272079 is defined */
20282080 #ifdef INC_DEBUG
@@ -2105,7 +2157,7 @@ void preprocess(void) · 76 unmodified lines
21052157 }
21062158 state = INCLUDE_STATE_NONE;
21072159 }
2108− c = (sym_find1(&define_stack, tok) != 0) ^ c;
2160+ c = (define_find(tok) != 0) ^ c;
21092161 do_if:
21102162 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
21112163 error("memory full");
@@ -2772,7 +2824,7 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 660 unmodified lines
27722824 /* XXX: test of the ',' is not 100%
27732825 reliable. should fix it to avoid security
27742826 problems */
2775− if (gnu_ext && s->t &&
2827+ if (gnu_ext && s->type.t &&
27762828 last_tok == TOK_TWOSHARPS &&
27772829 str.len >= 2 && str.str[str.len - 2] == ',') {
27782830 if (*st == 0) {
@@ -2959,7 +3011,7 @@ static int macro_subst_tok(TokenString *tok_str, · 180 unmodified lines
29593011 } else {
29603012 mstr = (int *)s->c;
29613013 mstr_allocated = 0;
2962− if (s->t == MACRO_FUNC) {
3014+ if (s->type.t == MACRO_FUNC) {
29633015 /* NOTE: we do not use next_nomacro to avoid eating the
29643016 next token. XXX: find better solution */
29653017 if (macro_ptr) {
@@ -2990,7 +3042,7 @@ static int macro_subst_tok(TokenString *tok_str, · 24 unmodified lines
29903042 /* NOTE: non zero sa->t indicates VA_ARGS */
29913043 while ((parlevel > 0 ||
29923044 (tok != ')' &&
2993− (tok != ',' || sa->t))) &&
3045+ (tok != ',' || sa->type.t))) &&
29943046 tok != -1) {
29953047 if (tok == '(')
29963048 parlevel++;
@@ -3000,12 +3052,12 @@ static int macro_subst_tok(TokenString *tok_str, · 3 unmodified lines
30003052 next_nomacro();
30013053 }
30023054 tok_str_add(&str, 0);
3003− sym_push2(&args, sa->v & ~SYM_FIELD, sa->t, (int)str.str);
3055+ sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
30043056 sa = sa->next;
30053057 if (tok == ')') {
30063058 /* special case for gcc var args: add an empty
30073059 var arg argument if it is omitted */
3008− if (sa && sa->t && gnu_ext)
3060+ if (sa && sa->type.t && gnu_ext)
30093061 continue;
30103062 else
30113063 break;
@@ -3063,7 +3115,7 @@ static void macro_subst(TokenString *tok_str, · 51 unmodified lines
30633115 next_nomacro();
30643116 if (tok == 0)
30653117 break;
3066− s = sym_find1(&define_stack, tok);
3118+ s = define_find(tok);
30673119 if (s != NULL) {
30683120 if (macro_subst_tok(tok_str, nested_list, s) != 0)
30693121 goto no_subst;
@@ -3094,7 +3146,7 @@ static void next(void) · 24 unmodified lines
30943146 /* if not reading from macro substituted string, then try
30953147 to substitute macros */
30963148 if (tok >= TOK_IDENT) {
3097− s = sym_find1(&define_stack, tok);
3149+ s = define_find(tok);
30983150 if (s) {
30993151 /* we have a macro: we try to substitute */
31003152 tok_str_new(&str);
@@ -3135,7 +3187,7 @@ void swap(int *p, int *q) · 34 unmodified lines
31353187 *q = t;
31363188 }
31373189
3138−void vsetc(int t, int r, CValue *vc)
3190+void vsetc(CType *type, int r, CValue *vc)
31393191 {
31403192 int v;
31413193
@@ -3150,7 +3202,7 @@ void vsetc(int t, int r, CValue *vc) · 8 unmodified lines
31503202 gv(RC_INT);
31513203 }
31523204 vtop++;
3153− vtop->t = t;
3205+ vtop->type = *type;
31543206 vtop->r = r;
31553207 vtop->r2 = VT_CONST;
31563208 vtop->c = *vc;
@@ -3161,35 +3213,36 @@ void vpushi(int v) · 4 unmodified lines
31613213 {
31623214 CValue cval;
31633215 cval.i = v;
3164− vsetc(VT_INT, VT_CONST, &cval);
3216+ vsetc(&int_type, VT_CONST, &cval);
31653217 }
31663218
31673219 /* Return a static symbol pointing to a section */
3168−static Sym *get_sym_ref(int t, Section *sec,
3220+static Sym *get_sym_ref(CType *type, Section *sec,
31693221 unsigned long offset, unsigned long size)
31703222 {
31713223 int v;
31723224 Sym *sym;
31733225
31743226 v = anon_sym++;
3175− sym = sym_push1(&global_stack, v, t | VT_STATIC, 0);
3227+ sym = sym_push1(&global_stack, v, type->t | VT_STATIC, 0);
3228+ sym->type.ref = type->ref;
31763229 sym->r = VT_CONST | VT_SYM;
31773230 put_extern_sym(sym, sec, offset, size);
31783231 return sym;
31793232 }
31803233
31813234 /* push a reference to a section offset by adding a dummy symbol */
3182−static void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long size)
3235+static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
31833236 {
31843237 CValue cval;
31853238
31863239 cval.ul = 0;
3187− vsetc(t, VT_CONST | VT_SYM, &cval);
3188− vtop->sym = get_sym_ref(t, sec, offset, size);
3240+ vsetc(type, VT_CONST | VT_SYM, &cval);
3241+ vtop->sym = get_sym_ref(type, sec, offset, size);
31893242 }
31903243
31913244 /* define a new external reference to a symbol 'v' of type 'u' */
3192−static Sym *external_global_sym(int v, int u, int r)
3245+static Sym *external_global_sym(int v, CType *type, int r)
31933246 {
31943247 Sym *s;
31953248
@@ -3197,43 +3250,52 @@ static Sym *external_global_sym(int v, int u, int r) · 1 unmodified line
31973250 if (!s) {
31983251 /* push forward reference */
31993252 s = sym_push1(&global_stack,
3200− v, u | VT_EXTERN, 0);
3253+ v, type->t | VT_EXTERN, 0);
3254+ s->type.ref = type->ref;
32013255 s->r = r | VT_CONST | VT_SYM;
32023256 }
32033257 return s;
32043258 }
32053259
32063260 /* define a new external reference to a symbol 'v' of type 'u' */
3207−static Sym *external_sym(int v, int u, int r)
3261+static Sym *external_sym(int v, CType *type, int r)
32083262 {
32093263 Sym *s;
32103264
32113265 s = sym_find(v);
32123266 if (!s) {
32133267 /* push forward reference */
3214− s = sym_push(v, u | VT_EXTERN, r | VT_CONST | VT_SYM, 0);
3268+ s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
3269+ s->type.t |= VT_EXTERN;
32153270 }
32163271 return s;
32173272 }
32183273
32193274 /* push a reference to global symbol v */
3220−static void vpush_global_sym(int t, int v)
3275+static void vpush_global_sym(CType *type, int v)
32213276 {
32223277 Sym *sym;
32233278 CValue cval;
32243279
3225− sym = external_global_sym(v, t, 0);
3280+ sym = external_global_sym(v, type, 0);
32263281 cval.ul = 0;
3227− vsetc(t, VT_CONST | VT_SYM, &cval);
3282+ vsetc(type, VT_CONST | VT_SYM, &cval);
32283283 vtop->sym = sym;
32293284 }
32303285
3231−void vset(int t, int r, int v)
3286+void vset(CType *type, int r, int v)
32323287 {
32333288 CValue cval;
32343289
32353290 cval.i = v;
3236− vsetc(t, r, &cval);
3291+ vsetc(type, r, &cval);
3292+}
3293+
3294+void vseti(int r, int v)
3295+{
3296+ CType type;
3297+ type.t = VT_INT;
3298+ vset(&type, r, v);
32373299 }
32383300
32393301 void vswap(void)
@@ -3261,8 +3323,9 @@ void vdup(void) · 21 unmodified lines
32613323 /* save r to the memory stack, and mark it as being free */
32623324 void save_reg(int r)
32633325 {
3264− int l, saved, t, size, align;
3326+ int l, saved, size, align;
32653327 SValue *p, sv;
3328+ CType *type;
32663329
32673330 /* modify all stack values */
32683331 saved = 0;
@@ -3275,13 +3338,13 @@ void save_reg(int r) · 6 unmodified lines
32753338 /* NOTE: must reload 'r' because r might be equal to r2 */
32763339 r = p->r & VT_VALMASK;
32773340 /* store register in the stack */
3278− t = p->t;
3341+ type = &p->type;
32793342 if ((p->r & VT_LVAL) ||
3280− (!is_float(t) && (t & VT_BTYPE) != VT_LLONG))
3281− t = VT_INT;
3282− size = type_size(t, &align);
3343+ (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
3344+ type = &int_type;
3345+ size = type_size(type, &align);
32833346 loc = (loc - size) & -align;
3284− sv.t = t;
3347+ sv.type.t = type->t;
32853348 sv.r = VT_LOCAL | VT_LVAL;
32863349 sv.c.ul = loc;
32873350 store(r, &sv);
@@ -3292,7 +3355,7 @@ void save_reg(int r) · 4 unmodified lines
32923355 }
32933356 #endif
32943357 /* special long long case */
3295− if ((t & VT_BTYPE) == VT_LLONG) {
3358+ if ((type->t & VT_BTYPE) == VT_LLONG) {
32963359 sv.c.ul += 4;
32973360 store(p->r2, &sv);
32983361 }
@@ -3306,7 +3369,7 @@ void save_reg(int r) · 7 unmodified lines
33063369 p->c.ul */
33073370 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
33083371 } else {
3309− p->r = lvalue_type(p->t) | VT_LOCAL;
3372+ p->r = lvalue_type(p->type.t) | VT_LOCAL;
33103373 }
33113374 p->r2 = VT_CONST;
33123375 p->c.ul = l;
@@ -3374,7 +3437,7 @@ void move_reg(int r, int s) · 61 unmodified lines
33743437
33753438 if (r != s) {
33763439 save_reg(r);
3377− sv.t = VT_INT;
3440+ sv.type.t = VT_INT;
33783441 sv.r = s;
33793442 sv.c.ul = 0;
33803443 load(r, &sv);
@@ -3394,7 +3457,8 @@ void gaddrof(void) · 13 unmodified lines
33943457 /* generate lvalue bound code */
33953458 void gbound(void)
33963459 {
3397− int lval_type, t1;
3460+ int lval_type;
3461+ CType type1;
33983462
33993463 vtop->r &= ~VT_MUSTBOUND;
34003464 /* if lvalue, then use checking code before dereferencing */
@@ -3403,13 +3467,13 @@ void gbound(void) · 2 unmodified lines
34033467 if (!(vtop->r & VT_BOUNDED)) {
34043468 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
34053469 /* must save type because we must set it to int to get pointer */
3406− t1 = vtop->t;
3407− vtop->t = VT_INT;
3470+ type1 = vtop->type;
3471+ vtop->type.t = VT_INT;
34083472 gaddrof();
34093473 vpushi(0);
34103474 gen_bounded_ptr_add();
34113475 vtop->r |= lval_type;
3412− vtop->t = t1;
3476+ vtop->type = type1;
34133477 }
34143478 /* then check for dereferencing */
34153479 gen_bounded_ptr_deref();
@@ -3426,11 +3490,11 @@ int gv(int rc) · 10 unmodified lines
34263490 unsigned long long ll;
34273491
34283492 /* NOTE: get_reg can modify vstack[] */
3429− if (vtop->t & VT_BITFIELD) {
3430− bit_pos = (vtop->t >> VT_STRUCT_SHIFT) & 0x3f;
3431− bit_size = (vtop->t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
3493+ if (vtop->type.t & VT_BITFIELD) {
3494+ bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
3495+ bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
34323496 /* remove bit field info to avoid loops */
3433− vtop->t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
3497+ vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
34343498 /* generate shifts */
34353499 vpushi(32 - (bit_pos + bit_size));
34363500 gen_op(TOK_SHL);
@@ -3439,7 +3503,7 @@ int gv(int rc) · 2 unmodified lines
34393503 gen_op(TOK_SAR);
34403504 r = gv(rc);
34413505 } else {
3442− if (is_float(vtop->t) &&
3506+ if (is_float(vtop->type.t) &&
34433507 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
34443508 Sym *sym;
34453509 int *ptr;
@@ -3448,7 +3512,7 @@ int gv(int rc) · 2 unmodified lines
34483512 /* XXX: unify with initializers handling ? */
34493513 /* CPUs usually cannot use float constants, so we store them
34503514 generically in data segment */
3451− size = type_size(vtop->t, &align);
3515+ size = type_size(&vtop->type, &align);
34523516 offset = (data_section->data_offset + align - 1) & -align;
34533517 data_section->data_offset = offset;
34543518 /* XXX: not portable yet */
@@ -3456,7 +3520,7 @@ int gv(int rc) · 1 unmodified line
34563520 size = size >> 2;
34573521 for(i=0;i<size;i++)
34583522 ptr[i] = vtop->c.tab[i];
3459− sym = get_sym_ref(vtop->t, data_section, offset, size << 2);
3523+ sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
34603524 vtop->r |= VT_LVAL | VT_SYM;
34613525 vtop->sym = sym;
34623526 vtop->c.ul = 0;
@@ -3474,10 +3538,10 @@ int gv(int rc) · 11 unmodified lines
34743538 if (r >= VT_CONST ||
34753539 (vtop->r & VT_LVAL) ||
34763540 !(reg_classes[r] & rc) ||
3477− ((vtop->t & VT_BTYPE) == VT_LLONG &&
3541+ ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
34783542 !(reg_classes[vtop->r2] & rc))) {
34793543 r = get_reg(rc);
3480− if ((vtop->t & VT_BTYPE) == VT_LLONG) {
3544+ if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
34813545 /* two register type load : expand to two words
34823546 temporarily */
34833547 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
@@ -3494,7 +3558,7 @@ int gv(int rc) · 10 unmodified lines
34943558 vdup();
34953559 vtop[-1].r = r; /* save register value */
34963560 /* increment pointer to get second word */
3497− vtop->t = VT_INT;
3561+ vtop->type.t = VT_INT;
34983562 gaddrof();
34993563 vpushi(4);
35003564 gen_op('+');
@@ -3515,11 +3579,11 @@ int gv(int rc) · 14 unmodified lines
35153579 vpop();
35163580 /* write second register */
35173581 vtop->r2 = r2;
3518− } else if ((vtop->r & VT_LVAL) && !is_float(vtop->t)) {
3582+ } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
35193583 int t1, t;
35203584 /* lvalue of scalar type : need to use lvalue type
35213585 because of possible cast */
3522− t = vtop->t;
3586+ t = vtop->type.t;
35233587 t1 = t;
35243588 /* compute memory access type */
35253589 if (vtop->r & VT_LVAL_BYTE)
@@ -3528,10 +3592,10 @@ int gv(int rc) · 2 unmodified lines
35283592 t = VT_SHORT;
35293593 if (vtop->r & VT_LVAL_UNSIGNED)
35303594 t |= VT_UNSIGNED;
3531− vtop->t = t;
3595+ vtop->type.t = t;
35323596 load(r, vtop);
35333597 /* restore wanted type */
3534− vtop->t = t1;
3598+ vtop->type.t = t1;
35353599 } else {
35363600 /* one register type load */
35373601 load(r, vtop);
@@ -3579,14 +3643,14 @@ void lexpand(void) · 41 unmodified lines
35793643 {
35803644 int u;
35813645
3582− u = vtop->t & VT_UNSIGNED;
3646+ u = vtop->type.t & VT_UNSIGNED;
35833647 gv(RC_INT);
35843648 vdup();
35853649 vtop[0].r = vtop[-1].r2;
35863650 vtop[0].r2 = VT_CONST;
35873651 vtop[-1].r2 = VT_CONST;
3588− vtop[0].t = VT_INT | u;
3589− vtop[-1].t = VT_INT | u;
3652+ vtop[0].type.t = VT_INT | u;
3653+ vtop[-1].type.t = VT_INT | u;
35903654 }
35913655
35923656 /* build a long long from two ints */
@@ -3594,7 +3658,7 @@ void lbuild(int t) · 1 unmodified line
35943658 {
35953659 gv2(RC_INT, RC_INT);
35963660 vtop[-1].r2 = vtop[0].r;
3597− vtop[-1].t = t;
3661+ vtop[-1].type.t = t;
35983662 vpop();
35993663 }
36003664
@@ -3635,7 +3699,7 @@ void gv_dup(void) · 34 unmodified lines
36353699 int rc, t, r, r1;
36363700 SValue sv;
36373701
3638− t = vtop->t;
3702+ t = vtop->type.t;
36393703 if ((t & VT_BTYPE) == VT_LLONG) {
36403704 lexpand();
36413705 gv_dup();
@@ -3653,10 +3717,10 @@ void gv_dup(void) · 11 unmodified lines
36533717 } else {
36543718 /* duplicate value */
36553719 rc = RC_INT;
3656− sv.t = VT_INT;
3720+ sv.type.t = VT_INT;
36573721 if (is_float(t)) {
36583722 rc = RC_FLOAT;
3659− sv.t = t;
3723+ sv.type.t = t;
36603724 }
36613725 r = gv(rc);
36623726 r1 = get_reg(rc);
@@ -3695,7 +3759,7 @@ void gen_opl(int op) · 32 unmodified lines
36953759 gfunc_start(&gf, FUNC_CDECL);
36963760 gfunc_param(&gf);
36973761 gfunc_param(&gf);
3698− vpush_global_sym(func_old_type, func);
3762+ vpush_global_sym(&func_old_type, func);
36993763 gfunc_call(&gf);
37003764 vpushi(0);
37013765 vtop->r = REG_IRET;
@@ -3707,7 +3771,7 @@ void gen_opl(int op) · 5 unmodified lines
37073771 case '*':
37083772 case '+':
37093773 case '-':
3710− t = vtop->t;
3774+ t = vtop->type.t;
37113775 vswap();
37123776 lexpand();
37133777 vrotb(3);
@@ -3768,7 +3832,7 @@ void gen_opl(int op) · 54 unmodified lines
37683832 case TOK_SHR:
37693833 case TOK_SHL:
37703834 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3771− t = vtop[-1].t;
3835+ t = vtop[-1].type.t;
37723836 vswap();
37733837 lexpand();
37743838 vrotb(3);
@@ -3836,7 +3900,7 @@ void gen_opl(int op) · 61 unmodified lines
38363900 break;
38373901 default:
38383902 /* compare operations */
3839− t = vtop->t;
3903+ t = vtop->type.t;
38403904 vswap();
38413905 lexpand();
38423906 vrotb(3);
@@ -3881,7 +3945,7 @@ void gen_opl(int op) · 38 unmodified lines
38813945 gen_op(op);
38823946 a = gtst(1, a);
38833947 gsym(b);
3884− vset(VT_INT, VT_JMPI, a);
3948+ vseti(VT_JMPI, a);
38853949 break;
38863950 }
38873951 }
@@ -4015,10 +4079,10 @@ void gen_opif(int op) · 127 unmodified lines
40154079 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
40164080 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
40174081 if (c1 && c2) {
4018− if (v1->t == VT_FLOAT) {
4082+ if (v1->type.t == VT_FLOAT) {
40194083 f1 = v1->c.f;
40204084 f2 = v2->c.f;
4021− } else if (v1->t == VT_DOUBLE) {
4085+ } else if (v1->type.t == VT_DOUBLE) {
40224086 f1 = v1->c.d;
40234087 f2 = v2->c.d;
40244088 } else {
@@ -4048,9 +4112,9 @@ void gen_opif(int op) · 23 unmodified lines
40484112 goto general_case;
40494113 }
40504114 /* XXX: overflow test ? */
4051− if (v1->t == VT_FLOAT) {
4115+ if (v1->type.t == VT_FLOAT) {
40524116 v1->c.f = f1;
4053− } else if (v1->t == VT_DOUBLE) {
4117+ } else if (v1->type.t == VT_DOUBLE) {
40544118 v1->c.d = f1;
40554119 } else {
40564120 v1->c.ld = f1;
@@ -4066,9 +4130,10 @@ void gen_opif(int op) · 9 unmodified lines
40664130 }
40674131 }
40684132
4069−int pointed_size(int t)
4133+static int pointed_size(CType *type)
40704134 {
4071− return type_size(pointed_type(t), &t);
4135+ int align;
4136+ return type_size(pointed_type(type), &align);
40724137 }
40734138
40744139 #if 0
@@ -4090,9 +4155,10 @@ void check_pointer_types(SValue *p1, SValue *p2) · 15 unmodified lines
40904155 void gen_op(int op)
40914156 {
40924157 int u, t1, t2, bt1, bt2, t;
4158+ CType type1;
40934159
4094− t1 = vtop[-1].t;
4095− t2 = vtop[0].t;
4160+ t1 = vtop[-1].type.t;
4161+ t2 = vtop[0].type.t;
40964162 bt1 = t1 & VT_BTYPE;
40974163 bt2 = t2 & VT_BTYPE;
40984164
@@ -4106,16 +4172,15 @@ void gen_op(int op) · 7 unmodified lines
41064172 goto std_op;
41074173 }
41084174 /* if both pointers, then it must be the '-' op */
4109− if ((t1 & VT_BTYPE) == VT_PTR &&
4110− (t2 & VT_BTYPE) == VT_PTR) {
4175+ if (bt1 == VT_PTR && bt2 == VT_PTR) {
41114176 if (op != '-')
41124177 error("cannot use pointers here");
41134178 // check_pointer_types(vtop - 1, vtop);
41144179 /* XXX: check that types are compatible */
4115− u = pointed_size(t1);
4180+ u = pointed_size(&vtop[-1].type);
41164181 gen_opic(op);
41174182 /* set to integer type */
4118− vtop->t = VT_INT;
4183+ vtop->type.t = VT_INT;
41194184 vpushi(u);
41204185 gen_op(TOK_PDIV);
41214186 } else {
@@ -4123,12 +4188,13 @@ void gen_op(int op) · 1 unmodified line
41234188 if (op != '-' && op != '+')
41244189 error("cannot use pointers here");
41254190 /* Put pointer as first operand */
4126− if ((t2 & VT_BTYPE) == VT_PTR) {
4191+ if (bt2 == VT_PTR) {
41274192 vswap();
41284193 swap(&t1, &t2);
41294194 }
4195+ type1 = vtop[-1].type;
41304196 /* XXX: cast to int ? (long long case) */
4131− vpushi(pointed_size(vtop[-1].t));
4197+ vpushi(pointed_size(&vtop[-1].type));
41324198 gen_op('*');
41334199 #ifdef CONFIG_TCC_BCHECK
41344200 /* if evaluating constant expression, no code should be
@@ -4148,7 +4214,7 @@ void gen_op(int op) · 13 unmodified lines
41484214 gen_opic(op);
41494215 }
41504216 /* put again type if gen_opic() swaped operands */
4151− vtop->t = t1;
4217+ vtop->type = type1;
41524218 }
41534219 } else if (is_float(bt1) || is_float(bt2)) {
41544220 /* compute bigger type and do implicit casts */
@@ -4199,14 +4265,14 @@ void gen_op(int op) · 44 unmodified lines
41994265 op = TOK_UGE;
42004266 }
42014267 vswap();
4202− gen_cast(t);
4268+ type1.t = t;
4269+ gen_cast(&type1);
42034270 vswap();
42044271 /* special case for shifts and long long: we keep the shift as
42054272 an integer */
42064273 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
4207− gen_cast(VT_INT);
4208− else
4209− gen_cast(t);
4274+ type1.t = VT_INT;
4275+ gen_cast(&type1);
42104276 if (is_float(t))
42114277 gen_opif(op);
42124278 else if ((t & VT_BTYPE) == VT_LLONG)
@@ -4215,9 +4281,9 @@ void gen_op(int op) · 2 unmodified lines
42154281 gen_opic(op);
42164282 if (op >= TOK_ULT && op <= TOK_GT) {
42174283 /* relationnal op: the result is an int */
4218− vtop->t = VT_INT;
4284+ vtop->type.t = VT_INT;
42194285 } else {
4220− vtop->t = t;
4286+ vtop->type.t = t;
42214287 }
42224288 }
42234289 }
@@ -4227,17 +4293,17 @@ void gen_cvt_itof1(int t) · 3 unmodified lines
42274293 {
42284294 GFuncContext gf;
42294295
4230− if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
4296+ if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
42314297 (VT_LLONG | VT_UNSIGNED)) {
42324298
42334299 gfunc_start(&gf, FUNC_CDECL);
42344300 gfunc_param(&gf);
42354301 if (t == VT_FLOAT)
4236− vpush_global_sym(func_old_type, TOK___ulltof);
4302+ vpush_global_sym(&func_old_type, TOK___ulltof);
42374303 else if (t == VT_DOUBLE)
4238− vpush_global_sym(func_old_type, TOK___ulltod);
4304+ vpush_global_sym(&func_old_type, TOK___ulltod);
42394305 else
4240− vpush_global_sym(func_old_type, TOK___ulltold);
4306+ vpush_global_sym(&func_old_type, TOK___ulltold);
42414307 gfunc_call(&gf);
42424308 vpushi(0);
42434309 vtop->r = REG_FRET;
@@ -4255,14 +4321,14 @@ void gen_cvt_ftoi1(int t) · 11 unmodified lines
42554321 if (t == (VT_LLONG | VT_UNSIGNED)) {
42564322 /* not handled natively */
42574323 gfunc_start(&gf, FUNC_CDECL);
4258− st = vtop->t & VT_BTYPE;
4324+ st = vtop->type.t & VT_BTYPE;
42594325 gfunc_param(&gf);
42604326 if (st == VT_FLOAT)
4261− vpush_global_sym(func_old_type, TOK___fixunssfdi);
4327+ vpush_global_sym(&func_old_type, TOK___fixunssfdi);
42624328 else if (st == VT_DOUBLE)
4263− vpush_global_sym(func_old_type, TOK___fixunsdfdi);
4329+ vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
42644330 else
4265− vpush_global_sym(func_old_type, TOK___fixunsxfdi);
4331+ vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
42664332 gfunc_call(&gf);
42674333 vpushi(0);
42684334 vtop->r = REG_IRET;
@@ -4294,8 +4360,8 @@ void force_charshort_cast(int t) · 25 unmodified lines
42944360 }
42954361 }
42964362
4297−/* cast 'vtop' to 't' type */
4298−void gen_cast(int t)
4363+/* cast 'vtop' to 'type' */
4364+static void gen_cast(CType *type)
42994365 {
43004366 int sbt, dbt, sf, df, c;
43014367
@@ -4304,11 +4370,11 @@ void gen_cast(int t) · 2 unmodified lines
43044370 be incorrect */
43054371 if (vtop->r & VT_MUSTCAST) {
43064372 vtop->r &= ~VT_MUSTCAST;
4307− force_charshort_cast(vtop->t);
4373+ force_charshort_cast(vtop->type.t);
43084374 }
4309−
4310− dbt = t & (VT_BTYPE | VT_UNSIGNED);
4311− sbt = vtop->t & (VT_BTYPE | VT_UNSIGNED);
4375+
4376+ dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
4377+ sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
43124378
43134379 if (sbt != dbt && !nocode_wanted) {
43144380 sf = is_float(sbt);
@@ -4395,10 +4461,10 @@ void gen_cast(int t) · 80 unmodified lines
43954461 do_ftoi:
43964462 gen_cvt_ftoi1(dbt);
43974463 }
4398− if (dbt == VT_INT && (t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
4464+ if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
43994465 /* additionnal cast for char/short/bool... */
4400− vtop->t = dbt;
4401− gen_cast(t);
4466+ vtop->type.t = dbt;
4467+ gen_cast(type);
44024468 }
44034469 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
44044470 if ((sbt & VT_BTYPE) != VT_LLONG) {
@@ -4431,7 +4497,7 @@ void gen_cast(int t) · 26 unmodified lines
44314497 gen_op(TOK_NE);
44324498 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
44334499 (dbt & VT_BTYPE) == VT_SHORT) {
4434− force_charshort_cast(t);
4500+ force_charshort_cast(dbt);
44354501 } else if ((dbt & VT_BTYPE) == VT_INT) {
44364502 /* scalar to int */
44374503 if (sbt == VT_LLONG) {
@@ -4444,25 +4510,25 @@ void gen_cast(int t) · 6 unmodified lines
44444510 VT_LVAL_xxx constants) */
44454511 }
44464512 }
4447− vtop->t = t;
4513+ vtop->type = *type;
44484514 }
44494515
44504516 /* return type size. Put alignment at 'a' */
4451−int type_size(int t, int *a)
4517+int type_size(CType *type, int *a)
44524518 {
44534519 Sym *s;
44544520 int bt;
44554521
4456− bt = t & VT_BTYPE;
4522+ bt = type->t & VT_BTYPE;
44574523 if (bt == VT_STRUCT) {
44584524 /* struct/union */
4459− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
4525+ s = type->ref;
44604526 *a = 4; /* XXX: cannot store it yet. Doing that is safe */
44614527 return s->c;
44624528 } else if (bt == VT_PTR) {
4463− if (t & VT_ARRAY) {
4464− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
4465− return type_size(s->t, a) * s->c;
4529+ if (type->t & VT_ARRAY) {
4530+ s = type->ref;
4531+ return type_size(&s->type, a) * s->c;
44664532 } else {
44674533 *a = PTR_SIZE;
44684534 return PTR_SIZE;
@@ -4487,52 +4553,50 @@ int type_size(int t, int *a) · 18 unmodified lines
44874553 }
44884554
44894555 /* return the pointed type of t */
4490−int pointed_type(int t)
4556+static inline CType *pointed_type(CType *type)
44914557 {
4492− Sym *s;
4493− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
4494− return s->t | (t & ~VT_TYPE);
4558+ return &type->ref->type;
44954559 }
44964560
4497−int mk_pointer(int t)
4561+/* modify type so that its it is a pointer to type. */
4562+static void mk_pointer(CType *type)
44984563 {
4499− int p;
4500− p = anon_sym++;
4501− sym_push(p, t, 0, -1);
4502− return VT_PTR | (p << VT_STRUCT_SHIFT) | (t & ~VT_TYPE);
4564+ Sym *s;
4565+ s = sym_push(0, type, 0, -1);
4566+ type->t = VT_PTR | (type->t & ~VT_TYPE);
4567+ type->ref = s;
45034568 }
45044569
4505−int is_compatible_types(int t1, int t2)
4570+static int is_compatible_types(CType *type1, CType *type2)
45064571 {
45074572 Sym *s1, *s2;
4508− int bt1, bt2;
4573+ int bt1, bt2, t1, t2;
45094574
4510− t1 &= VT_TYPE;
4511− t2 &= VT_TYPE;
4575+ t1 = type1->t & VT_TYPE;
4576+ t2 = type2->t & VT_TYPE;
45124577 bt1 = t1 & VT_BTYPE;
45134578 bt2 = t2 & VT_BTYPE;
45144579 if (bt1 == VT_PTR) {
4515− t1 = pointed_type(t1);
4580+ type1 = pointed_type(type1);
45164581 /* if function, then convert implicitely to function pointer */
45174582 if (bt2 != VT_FUNC) {
45184583 if (bt2 != VT_PTR)
45194584 return 0;
4520− t2 = pointed_type(t2);
4585+ type2 = pointed_type(type2);
45214586 }
45224587 /* void matches everything */
4523− t1 &= VT_TYPE;
4524− t2 &= VT_TYPE;
4525− if (t1 == VT_VOID || t2 == VT_VOID)
4588+ /* XXX: not fully compliant */
4589+ if ((type1->t & VT_TYPE) == VT_VOID || (type2->t & VT_TYPE) == VT_VOID)
45264590 return 1;
4527− return is_compatible_types(t1, t2);
4591+ return is_compatible_types(type1, type2);
45284592 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
45294593 return (t2 == t1);
45304594 } else if (bt1 == VT_FUNC) {
45314595 if (bt2 != VT_FUNC)
45324596 return 0;
4533− s1 = sym_find(((unsigned)t1 >> VT_STRUCT_SHIFT));
4534− s2 = sym_find(((unsigned)t2 >> VT_STRUCT_SHIFT));
4535− if (!is_compatible_types(s1->t, s2->t))
4597+ s1 = type1->ref;
4598+ s2 = type2->ref;
4599+ if (!is_compatible_types(&s1->type, &s2->type))
45364600 return 0;
45374601 /* XXX: not complete */
45384602 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
@@ -4542,7 +4606,7 @@ int is_compatible_types(int t1, int t2) · 3 unmodified lines
45424606 while (s1 != NULL) {
45434607 if (s2 == NULL)
45444608 return 0;
4545− if (!is_compatible_types(s1->t, s2->t))
4609+ if (!is_compatible_types(&s1->type, &s2->type))
45464610 return 0;
45474611 s1 = s1->next;
45484612 s2 = s2->next;
@@ -4561,14 +4625,14 @@ int is_compatible_types(int t1, int t2) · 12 unmodified lines
45614625 /* XXX: union */
45624626 /* XXX: add array and function pointers */
45634627 void type_to_str(char *buf, int buf_size,
4564− int t, const char *varstr)
4628+ CType *type, const char *varstr)
45654629 {
4566− int bt, v;
4630+ int bt, v, t;
45674631 Sym *s, *sa;
45684632 char buf1[256];
45694633 const char *tstr;
45704634
4571− t = t & VT_TYPE;
4635+ t = type->t & VT_TYPE;
45724636 bt = t & VT_BTYPE;
45734637 buf[0] = '\0';
45744638 if (t & VT_UNSIGNED)
@@ -4613,19 +4677,19 @@ void type_to_str(char *buf, int buf_size, · 38 unmodified lines
46134677 else
46144678 tstr = "enum ";
46154679 pstrcat(buf, buf_size, tstr);
4616− v = (unsigned)t >> VT_STRUCT_SHIFT;
4680+ v = type->ref->v;
46174681 if (v >= SYM_FIRST_ANOM)
46184682 pstrcat(buf, buf_size, "<anonymous>");
46194683 else
46204684 pstrcat(buf, buf_size, get_tok_str(v, NULL));
46214685 break;
46224686 case VT_FUNC:
4623− s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
4624− type_to_str(buf, buf_size, s->t, varstr);
4687+ s = type->ref;
4688+ type_to_str(buf, buf_size, &s->type, varstr);
46254689 pstrcat(buf, buf_size, "(");
46264690 sa = s->next;
46274691 while (sa != NULL) {
4628− type_to_str(buf1, sizeof(buf1), sa->t, NULL);
4692+ type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
46294693 pstrcat(buf, buf_size, buf1);
46304694 sa = sa->next;
46314695 if (sa)
@@ -4634,11 +4698,11 @@ void type_to_str(char *buf, int buf_size, · 2 unmodified lines
46344698 pstrcat(buf, buf_size, ")");
46354699 goto no_var;
46364700 case VT_PTR:
4637− s = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
4701+ s = type->ref;
46384702 pstrcpy(buf1, sizeof(buf1), "*");
46394703 if (varstr)
46404704 pstrcat(buf1, sizeof(buf1), varstr);
4641− type_to_str(buf, buf_size, s->t, buf1);
4705+ type_to_str(buf, buf_size, &s->type, buf1);
46424706 goto no_var;
46434707 }
46444708 if (varstr) {
@@ -4650,23 +4714,23 @@ void type_to_str(char *buf, int buf_size, · 5 unmodified lines
46504714
46514715 /* verify type compatibility to store vtop in 'dt' type, and generate
46524716 casts if needed. */
4653−void gen_assign_cast(int dt)
4717+void gen_assign_cast(CType *dt)
46544718 {
4655− int st;
4719+ CType *st;
46564720 char buf1[256], buf2[256];
46574721
4658− st = vtop->t; /* source type */
4659− if ((dt & VT_BTYPE) == VT_PTR) {
4722+ st = &vtop->type; /* source type */
4723+ if ((dt->t & VT_BTYPE) == VT_PTR) {
46604724 /* special cases for pointers */
46614725 /* a function is implicitely a function pointer */
4662− if ((st & VT_BTYPE) == VT_FUNC) {
4726+ if ((st->t & VT_BTYPE) == VT_FUNC) {
46634727 if (!is_compatible_types(pointed_type(dt), st))
46644728 goto error;
46654729 else
46664730 goto type_ok;
46674731 }
46684732 /* '0' can also be a pointer */
4669− if ((st & VT_BTYPE) == VT_INT &&
4733+ if ((st->t & VT_BTYPE) == VT_INT &&
46704734 ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) &&
46714735 vtop->c.i == 0)
46724736 goto type_ok;
@@ -4687,17 +4751,17 @@ void vstore(void) · 14 unmodified lines
46874751 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
46884752 GFuncContext gf;
46894753
4690− ft = vtop[-1].t;
4691− sbt = vtop->t & VT_BTYPE;
4754+ ft = vtop[-1].type.t;
4755+ sbt = vtop->type.t & VT_BTYPE;
46924756 dbt = ft & VT_BTYPE;
46934757 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
46944758 (sbt == VT_INT && dbt == VT_SHORT)) {
46954759 /* optimize char/short casts */
46964760 delayed_cast = VT_MUSTCAST;
4697− vtop->t = ft & VT_TYPE;
4761+ vtop->type.t = ft & VT_TYPE;
46984762 } else {
46994763 delayed_cast = 0;
4700− gen_assign_cast(ft & VT_TYPE);
4764+ gen_assign_cast(&vtop[-1].type);
47014765 }
47024766
47034767 if (sbt == VT_STRUCT) {
@@ -4708,21 +4772,21 @@ void vstore(void) · 4 unmodified lines
47084772 vdup();
47094773 gfunc_start(&gf, FUNC_CDECL);
47104774 /* type size */
4711− size = type_size(vtop->t, &align);
4775+ size = type_size(&vtop->type, &align);
47124776 vpushi(size);
47134777 gfunc_param(&gf);
47144778 /* source */
4715− vtop->t = VT_INT;
4779+ vtop->type.t = VT_INT;
47164780 gaddrof();
47174781 gfunc_param(&gf);
47184782 /* destination */
47194783 vswap();
4720− vtop->t = VT_INT;
4784+ vtop->type.t = VT_INT;
47214785 gaddrof();
47224786 gfunc_param(&gf);
47234787
47244788 save_regs(0);
4725− vpush_global_sym(func_old_type, TOK_memcpy);
4789+ vpush_global_sym(&func_old_type, TOK_memcpy);
47264790 gfunc_call(&gf);
47274791 } else {
47284792 vswap();
@@ -4734,7 +4798,7 @@ void vstore(void) · 5 unmodified lines
47344798 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
47354799 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
47364800 /* remove bit field info to avoid loops */
4737− vtop[-1].t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4801+ vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
47384802
47394803 /* duplicate destination */
47404804 vdup();
@@ -4770,7 +4834,7 @@ void vstore(void) · 29 unmodified lines
47704834 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
47714835 SValue sv;
47724836 t = get_reg(RC_INT);
4773− sv.t = VT_INT;
4837+ sv.type.t = VT_INT;
47744838 sv.r = VT_LOCAL | VT_LVAL;
47754839 sv.c.ul = vtop[-1].c.ul;
47764840 load(t, &sv);
@@ -4781,7 +4845,7 @@ void vstore(void) · 4 unmodified lines
47814845 if ((ft & VT_BTYPE) == VT_LLONG) {
47824846 vswap();
47834847 /* convert to int to increment easily */
4784− vtop->t = VT_INT;
4848+ vtop->type.t = VT_INT;
47854849 gaddrof();
47864850 vpushi(4);
47874851 gen_op('+');
@@ -4892,13 +4956,14 @@ void parse_attribute(AttributeDef *ad) · 104 unmodified lines
48924956 skip(')');
48934957 }
48944958
4895−/* enum/struct/union declaration */
4896−int struct_decl(int u)
4959+/* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
4960+static void struct_decl(CType *type, int u)
48974961 {
4898− int a, t, b, v, size, align, maxalign, c, offset;
4962+ int a, v, size, align, maxalign, c, offset;
48994963 int bit_size, bit_pos, bsize, bt, lbit_pos;
49004964 Sym *s, *ss, **ps;
49014965 AttributeDef ad;
4966+ CType type1, btype;
49024967
49034968 a = tok; /* save decl type */
49044969 next();
@@ -4909,17 +4974,19 @@ int struct_decl(int u) · 4 unmodified lines
49094974 /* XXX: check consistency */
49104975 s = sym_find(v | SYM_STRUCT);
49114976 if (s) {
4912− if (s->t != a)
4977+ if (s->type.t != a)
49134978 error("invalid type");
49144979 goto do_decl;
49154980 }
49164981 } else {
49174982 v = anon_sym++;
49184983 }
4919− s = sym_push(v | SYM_STRUCT, a, 0, 0);
4984+ type1.t = a;
4985+ s = sym_push(v | SYM_STRUCT, &type1, 0, 0);
49204986 /* put struct/union/enum name in type */
49214987 do_decl:
4922− u = u | (v << VT_STRUCT_SHIFT);
4988+ type->t = u;
4989+ type->ref = s;
49234990
49244991 if (tok == '{') {
49254992 next();
@@ -4940,23 +5007,23 @@ int struct_decl(int u) · 14 unmodified lines
49405007 c = expr_const();
49415008 }
49425009 /* enum symbols have static storage */
4943− sym_push(v, VT_STATIC | VT_INT, VT_CONST, c);
5010+ ss = sym_push(v, &int_type, VT_CONST, c);
5011+ ss->type.t |= VT_STATIC;
49445012 if (tok == ',')
49455013 next();
49465014 c++;
49475015 } else {
4948− parse_btype(&b, &ad);
5016+ parse_btype(&btype, &ad);
49495017 while (1) {
49505018 bit_size = -1;
49515019 v = 0;
5020+ type1 = btype;
49525021 if (tok != ':') {
4953− t = type_decl(&ad, &v, b, TYPE_DIRECT);
4954− if ((t & VT_BTYPE) == VT_FUNC ||
4955− (t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))
5022+ type_decl(&type1, &ad, &v, TYPE_DIRECT);
5023+ if ((type1.t & VT_BTYPE) == VT_FUNC ||
5024+ (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN)))
49565025 error("invalid type for '%s'",
49575026 get_tok_str(v, NULL));
4958− } else {
4959− t = b;
49605027 }
49615028 if (tok == ':') {
49625029 next();
@@ -4969,10 +5036,10 @@ int struct_decl(int u) · 6 unmodified lines
49695036 error("zero width for bit-field '%s'",
49705037 get_tok_str(v, NULL));
49715038 }
4972− size = type_size(t, &align);
5039+ size = type_size(&type1, &align);
49735040 lbit_pos = 0;
49745041 if (bit_size >= 0) {
4975− bt = t & VT_BTYPE;
5042+ bt = type1.t & VT_BTYPE;
49765043 if (bt != VT_INT &&
49775044 bt != VT_BYTE &&
49785045 bt != VT_SHORT)
@@ -4996,7 +5063,7 @@ int struct_decl(int u) · 17 unmodified lines
49965063 bit_pos = 0;
49975064 lbit_pos = bit_pos;
49985065 /* XXX: handle LSB first */
4999− t |= VT_BITFIELD |
5066+ type1.t |= VT_BITFIELD |
50005067 (bit_pos << VT_STRUCT_SHIFT) |
50015068 (bit_size << (VT_STRUCT_SHIFT + 6));
50025069 bit_pos += bit_size;
@@ -5023,14 +5090,14 @@ int struct_decl(int u) · 20 unmodified lines
50235090 #if 0
50245091 printf("add field %s offset=%d",
50255092 get_tok_str(v, NULL), offset);
5026− if (t & VT_BITFIELD) {
5093+ if (type1.t & VT_BITFIELD) {
50275094 printf(" pos=%d size=%d",
5028− (t >> VT_STRUCT_SHIFT) & 0x3f,
5029− (t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
5095+ (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
5096+ (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
50305097 }
50315098 printf("\n");
50325099 #endif
5033− ss = sym_push(v | SYM_FIELD, t, 0, offset);
5100+ ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
50345101 *ps = ss;
50355102 ps = &ss->next;
50365103 }
@@ -5047,16 +5114,16 @@ int struct_decl(int u) · 10 unmodified lines
50475114 /* size for struct/union, dummy for enum */
50485115 s->c = (c + maxalign - 1) & -maxalign;
50495116 }
5050− return u;
50515117 }
50525118
50535119 /* return 0 if no type declaration. otherwise, return the basic type
50545120 and skip it.
50555121 */
5056−int parse_btype(int *type_ptr, AttributeDef *ad)
5122+static int parse_btype(CType *type, AttributeDef *ad)
50575123 {
50585124 int t, u, type_found;
50595125 Sym *s;
5126+ CType type1;
50605127
50615128 memset(ad, 0, sizeof(AttributeDef));
50625129 type_found = 0;
@@ -5109,12 +5176,15 @@ int parse_btype(int *type_ptr, AttributeDef *ad) · 46 unmodified lines
51095176 }
51105177 break;
51115178 case TOK_ENUM:
5112− u = struct_decl(VT_ENUM);
5179+ struct_decl(&type1, VT_ENUM);
5180+ basic_type2:
5181+ u = type1.t;
5182+ type->ref = type1.ref;
51135183 goto basic_type1;
51145184 case TOK_STRUCT:
51155185 case TOK_UNION:
5116− u = struct_decl(VT_STRUCT);
5117− goto basic_type1;
5186+ struct_decl(&type1, VT_STRUCT);
5187+ goto basic_type2;
51185188
51195189 /* type modifiers */
51205190 case TOK_CONST:
@@ -5153,13 +5223,14 @@ int parse_btype(int *type_ptr, AttributeDef *ad) · 32 unmodified lines
51535223 /* GNUC typeof */
51545224 case TOK_TYPEOF:
51555225 next();
5156− u = parse_expr_type();
5157− goto basic_type1;
5226+ parse_expr_type(&type1);
5227+ goto basic_type2;
51585228 default:
51595229 s = sym_find(tok);
5160− if (!s || !(s->t & VT_TYPEDEF))
5230+ if (!s || !(s->type.t & VT_TYPEDEF))
51615231 goto the_end;
5162− t |= (s->t & ~VT_TYPEDEF);
5232+ t |= (s->type.t & ~VT_TYPEDEF);
5233+ type->ref = s->type.ref;
51635234 next();
51645235 break;
51655236 }
@@ -5169,15 +5240,16 @@ the_end: · 3 unmodified lines
51695240 /* long is never used as type */
51705241 if ((t & VT_BTYPE) == VT_LONG)
51715242 t = (t & ~VT_BTYPE) | VT_INT;
5172− *type_ptr = t;
5243+ type->t = t;
51735244 return type_found;
51745245 }
51755246
5176−int post_type(int t, AttributeDef *ad)
5247+static void post_type(CType *type, AttributeDef *ad)
51775248 {
5178− int p, n, pt, l, t1;
5249+ int n, l, t1;
51795250 Sym **plast, *s, *first;
51805251 AttributeDef ad1;
5252+ CType pt;
51815253
51825254 if (tok == '(') {
51835255 /* function declaration */
@@ -5197,20 +5269,20 @@ int post_type(int t, AttributeDef *ad) · 13 unmodified lines
51975269 }
51985270 }
51995271 l = FUNC_NEW;
5200− if ((pt & VT_BTYPE) == VT_VOID && tok == ')')
5272+ if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
52015273 break;
5202− pt = type_decl(&ad1, &n, pt, TYPE_DIRECT | TYPE_ABSTRACT);
5203− if ((pt & VT_BTYPE) == VT_VOID)
5274+ type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
5275+ if ((pt.t & VT_BTYPE) == VT_VOID)
52045276 error("parameter declared as void");
52055277 } else {
52065278 old_proto:
52075279 n = tok;
5208− pt = VT_INT;
5280+ pt.t = VT_INT;
52095281 next();
52105282 }
52115283 /* array must be transformed to pointer according to ANSI C */
5212− pt &= ~VT_ARRAY;
5213− s = sym_push(n | SYM_FIELD, pt, 0, 0);
5284+ pt.t &= ~VT_ARRAY;
5285+ s = sym_push(n | SYM_FIELD, &pt, 0, 0);
52145286 *plast = s;
52155287 plast = &s->next;
52165288 if (tok == ',') {
@@ -5226,13 +5298,14 @@ int post_type(int t, AttributeDef *ad) · 9 unmodified lines
52265298 if (l == 0)
52275299 l = FUNC_OLD;
52285300 skip(')');
5229− t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5230− t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN), ad);
5301+ t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5302+ type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5303+ post_type(type, ad);
52315304 /* we push a anonymous symbol which will contain the function prototype */
5232− p = anon_sym++;
5233− s = sym_push(p, t, ad->func_call, l);
5305+ s = sym_push(0, type, ad->func_call, l);
52345306 s->next = first;
5235− t = t1 | VT_FUNC | (p << VT_STRUCT_SHIFT);
5307+ type->t = t1 | VT_FUNC;
5308+ type->ref = s;
52365309 } else if (tok == '[') {
52375310 /* array definition */
52385311 next();
@@ -5244,46 +5317,48 @@ int post_type(int t, AttributeDef *ad) · 5 unmodified lines
52445317 }
52455318 skip(']');
52465319 /* parse next post type */
5247− t1 = t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5248− t = post_type(t & ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN), ad);
5320+ t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5321+ type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5322+ post_type(type, ad);
52495323
52505324 /* we push a anonymous symbol which will contain the array
52515325 element type */
5252− p = anon_sym++;
5253− sym_push(p, t, 0, n);
5254− t = t1 | VT_ARRAY | VT_PTR | (p << VT_STRUCT_SHIFT);
5326+ s = sym_push(0, type, 0, n);
5327+ type->t = t1 | VT_ARRAY | VT_PTR;
5328+ type->ref = s;
52555329 }
5256− return t;
52575330 }
52585331
5259−/* Read a type declaration (except basic type), and return the
5260− type. 'td' is a bitmask indicating which kind of type decl is
5261− expected. 't' should contain the basic type. 'ad' is the attribute
5262− definition of the basic type. It can be modified by type_decl(). */
5263−int type_decl(AttributeDef *ad, int *v, int t, int td)
5332+/* Parse a type declaration (except basic type), and return the type
5333+ in 'type'. 'td' is a bitmask indicating which kind of type decl is
5334+ expected. 'type' should contain the basic type. 'ad' is the
5335+ attribute definition of the basic type. It can be modified by
5336+ type_decl().
5337+ */
5338+static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
52645339 {
5265− int u, p;
52665340 Sym *s;
5341+ CType type1, *type2;
52675342
52685343 while (tok == '*') {
52695344 next();
52705345 while (tok == TOK_CONST || tok == TOK_VOLATILE || tok == TOK_RESTRICT)
52715346 next();
5272− t = mk_pointer(t);
5347+ mk_pointer(type);
52735348 }
52745349
52755350 /* recursive type */
52765351 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
5352+ type1.t = 0; /* XXX: same as int */
52775353 if (tok == '(') {
52785354 next();
52795355 /* XXX: this is not correct to modify 'ad' at this point, but
52805356 the syntax is not clear */
52815357 if (tok == TOK___ATTRIBUTE__)
52825358 parse_attribute(ad);
5283− u = type_decl(ad, v, 0, td);
5359+ type_decl(&type1, ad, v, td);
52845360 skip(')');
52855361 } else {
5286− u = 0;
52875362 /* type identifier */
52885363 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
52895364 *v = tok;
@@ -5294,22 +5369,22 @@ int type_decl(AttributeDef *ad, int *v, int t, int td) · 4 unmodified lines
52945369 *v = 0;
52955370 }
52965371 }
5297− /* append t at the end of u */
5298− t = post_type(t, ad);
5372+ post_type(type, ad);
52995373 if (tok == TOK___ATTRIBUTE__)
53005374 parse_attribute(ad);
5301− if (!u)
5302− return t;
5303− p = u;
5304− while(1) {
5305− s = sym_find((unsigned)p >> VT_STRUCT_SHIFT);
5306− p = s->t;
5307− if (!p) {
5308− s->t = t;
5375+ if (!type1.t)
5376+ return;
5377+ /* append type at the end of type1 */
5378+ type2 = &type1;
5379+ for(;;) {
5380+ s = type2->ref;
5381+ type2 = &s->type;
5382+ if (!type2->t) {
5383+ *type2 = *type;
53095384 break;
53105385 }
53115386 }
5312− return u;
5387+ *type = type1;
53135388 }
53145389
53155390 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
@@ -5332,14 +5407,14 @@ static int lvalue_type(int t) · 16 unmodified lines
53325407 /* indirection with full error checking and bound check */
53335408 static void indir(void)
53345409 {
5335− if ((vtop->t & VT_BTYPE) != VT_PTR)
5410+ if ((vtop->type.t & VT_BTYPE) != VT_PTR)
53365411 expect("pointer");
53375412 if ((vtop->r & VT_LVAL) && !nocode_wanted)
53385413 gv(RC_INT);
5339− vtop->t = pointed_type(vtop->t);
5414+ vtop->type = *pointed_type(&vtop->type);
53405415 /* an array is never an lvalue */
5341− if (!(vtop->t & VT_ARRAY)) {
5342− vtop->r |= lvalue_type(vtop->t);
5416+ if (!(vtop->type.t & VT_ARRAY)) {
5417+ vtop->r |= lvalue_type(vtop->type.t);
53435418 /* if bound checking, the referenced pointer must be checked */
53445419 if (do_bounds_check)
53455420 vtop->r |= VT_MUSTBOUND;
@@ -5350,16 +5425,20 @@ static void indir(void) · 4 unmodified lines
53505425 void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
53515426 {
53525427 int func_type;
5428+ CType type;
5429+
53535430 func_type = func->c;
53545431 if (func_type == FUNC_OLD ||
53555432 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
53565433 /* default casting : only need to convert float to double */
5357− if ((vtop->t & VT_BTYPE) == VT_FLOAT)
5358− gen_cast(VT_DOUBLE);
5434+ if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
5435+ type.t = VT_DOUBLE;
5436+ gen_cast(&type);
5437+ }
53595438 } else if (arg == NULL) {
53605439 error("too many arguments to function");
53615440 } else {
5362− gen_assign_cast(arg->t);
5441+ gen_assign_cast(&arg->type);
53635442 }
53645443 if (!nocode_wanted) {
53655444 gfunc_param(gf);
@@ -5370,24 +5449,31 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 4 unmodified lines
53705449
53715450 /* parse an expression of the form '(type)' or '(expr)' and return its
53725451 type */
5373−static int parse_expr_type(void)
5452+static void parse_expr_type(CType *type)
53745453 {
5375− int ft, n;
5454+ int n;
53765455 AttributeDef ad;
53775456
53785457 skip('(');
5379− if (parse_btype(&ft, &ad)) {
5380− ft = type_decl(&ad, &n, ft, TYPE_ABSTRACT);
5458+ if (parse_btype(type, &ad)) {
5459+ type_decl(type, &ad, &n, TYPE_ABSTRACT);
53815460 } else {
5382− ft = expr_type();
5461+ expr_type(type);
53835462 }
53845463 skip(')');
5385− return ft;
5464+}
5465+
5466+static void vpush_tokc(int t)
5467+{
5468+ CType type;
5469+ type.t = t;
5470+ vsetc(&type, VT_CONST, &tokc);
53865471 }
53875472
53885473 static void unary(void)
53895474 {
5390− int n, t, ft, align, size, r;
5475+ int n, t, align, size, r;
5476+ CType type;
53915477 Sym *s;
53925478 GFuncContext gf;
53935479 AttributeDef ad;
@@ -5396,22 +5482,22 @@ static void unary(void) · 2 unmodified lines
53965482 vpushi(tokc.i);
53975483 next();
53985484 } else if (tok == TOK_CUINT) {
5399− vsetc(VT_INT | VT_UNSIGNED, VT_CONST, &tokc);
5485+ vpush_tokc(VT_INT | VT_UNSIGNED);
54005486 next();
54015487 } else if (tok == TOK_CLLONG) {
5402− vsetc(VT_LLONG, VT_CONST, &tokc);
5488+ vpush_tokc(VT_LLONG);
54035489 next();
54045490 } else if (tok == TOK_CULLONG) {
5405− vsetc(VT_LLONG | VT_UNSIGNED, VT_CONST, &tokc);
5491+ vpush_tokc(VT_LLONG | VT_UNSIGNED);
54065492 next();
54075493 } else if (tok == TOK_CFLOAT) {
5408− vsetc(VT_FLOAT, VT_CONST, &tokc);
5494+ vpush_tokc(VT_FLOAT);
54095495 next();
54105496 } else if (tok == TOK_CDOUBLE) {
5411− vsetc(VT_DOUBLE, VT_CONST, &tokc);
5497+ vpush_tokc(VT_DOUBLE);
54125498 next();
54135499 } else if (tok == TOK_CLDOUBLE) {
5414− vsetc(VT_LDOUBLE, VT_CONST, &tokc);
5500+ vpush_tokc(VT_LDOUBLE);
54155501 next();
54165502 } else if (tok == TOK___FUNC__ || (tok == TOK___FUNCTION__ && gnu_ext)) {
54175503 void *ptr;
@@ -5420,10 +5506,11 @@ static void unary(void) · 2 unmodified lines
54205506
54215507 len = strlen(funcname) + 1;
54225508 /* generate char[len] type */
5423− t = VT_ARRAY | mk_pointer(VT_BYTE);
5424− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
5425− s->c = len;
5426− vpush_ref(t, data_section, data_section->data_offset, len);
5509+ type.t = VT_BYTE;
5510+ mk_pointer(&type);
5511+ type.t |= VT_ARRAY;
5512+ type.ref->c = len;
5513+ vpush_ref(&type, data_section, data_section->data_offset, len);
54275514 ptr = section_ptr_add(data_section, len);
54285515 memcpy(ptr, funcname, len);
54295516 next();
@@ -5434,16 +5521,18 @@ static void unary(void) · 4 unmodified lines
54345521 /* string parsing */
54355522 t = VT_BYTE;
54365523 str_init:
5437− t = VT_ARRAY | mk_pointer(t);
5524+ type.t = t;
5525+ mk_pointer(&type);
5526+ type.t |= VT_ARRAY;
54385527 memset(&ad, 0, sizeof(AttributeDef));
5439− decl_initializer_alloc(t, &ad, VT_CONST, 2, 0, 0);
5528+ decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
54405529 } else {
54415530 t = tok;
54425531 next();
54435532 if (t == '(') {
54445533 /* cast ? */
5445− if (parse_btype(&t, &ad)) {
5446− ft = type_decl(&ad, &n, t, TYPE_ABSTRACT);
5534+ if (parse_btype(&type, &ad)) {
5535+ type_decl(&type, &ad, &n, TYPE_ABSTRACT);
54475536 skip(')');
54485537 /* check ISOC99 compound literal */
54495538 if (tok == '{') {
@@ -5453,13 +5542,13 @@ static void unary(void) · 3 unmodified lines
54535542 else
54545543 r = VT_LOCAL;
54555544 /* all except arrays are lvalues */
5456− if (!(ft & VT_ARRAY))
5457− r |= lvalue_type(ft);
5545+ if (!(type.t & VT_ARRAY))
5546+ r |= lvalue_type(type.t);
54585547 memset(&ad, 0, sizeof(AttributeDef));
5459− decl_initializer_alloc(ft, &ad, r, 1, 0, 0);
5548+ decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
54605549 } else {
54615550 unary();
5462− gen_cast(ft);
5551+ gen_cast(&type);
54635552 }
54645553 } else {
54655554 gexpr();
@@ -5475,10 +5564,10 @@ static void unary(void) · 9 unmodified lines
54755564 functions are not lvalues, we only have to handle it
54765565 there and in function calls. */
54775566 /* arrays can also be used although they are not lvalues */
5478− if ((vtop->t & VT_BTYPE) != VT_FUNC &&
5479− !(vtop->t & VT_ARRAY))
5567+ if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
5568+ !(vtop->type.t & VT_ARRAY))
54805569 test_lvalue();
5481− vtop->t = mk_pointer(vtop->t);
5570+ mk_pointer(&vtop->type);
54825571 gaddrof();
54835572 } else if (t == '!') {
54845573 unary();
@@ -5487,7 +5576,7 @@ static void unary(void) · 2 unmodified lines
54875576 else if ((vtop->r & VT_VALMASK) == VT_CMP)
54885577 vtop->c.i = vtop->c.i ^ 1;
54895578 else
5490− vset(VT_INT, VT_JMP, gtst(1, 0));
5579+ vseti(VT_JMP, gtst(1, 0));
54915580 } else if (t == '~') {
54925581 unary();
54935582 vpushi(-1);
@@ -5495,17 +5584,17 @@ static void unary(void) · 1 unmodified line
54955584 } else if (t == '+') {
54965585 /* in order to force cast, we add zero */
54975586 unary();
5498− if ((vtop->t & VT_BTYPE) == VT_PTR)
5587+ if ((vtop->type.t & VT_BTYPE) == VT_PTR)
54995588 error("pointer not accepted for unary plus");
55005589 vpushi(0);
55015590 gen_op('+');
55025591 } else if (t == TOK_SIZEOF || t == TOK_ALIGNOF) {
55035592 if (tok == '(') {
5504− ft = parse_expr_type();
5593+ parse_expr_type(&type);
55055594 } else {
5506− ft = unary_type();
5595+ unary_type(&type);
55075596 }
5508− size = type_size(ft, &align);
5597+ size = type_size(&type, &align);
55095598 if (t == TOK_SIZEOF)
55105599 vpushi(size);
55115600 else
@@ -5526,9 +5615,12 @@ static void unary(void) · 14 unmodified lines
55265615 s = sym_push1(&label_stack, tok, 0, 0);
55275616 s->r = LABEL_FORWARD;
55285617 }
5529− if (!s->t)
5530− s->t = mk_pointer(VT_VOID) | VT_STATIC;
5531− vset(s->t, VT_CONST | VT_SYM, 0);
5618+ if (!s->type.t) {
5619+ s->type.t = VT_VOID;
5620+ mk_pointer(&s->type);
5621+ s->type.t |= VT_STATIC;
5622+ }
5623+ vset(&s->type, VT_CONST | VT_SYM, 0);
55325624 vtop->sym = s;
55335625 next();
55345626 } else {
@@ -5540,9 +5632,9 @@ static void unary(void) · 5 unmodified lines
55405632 error("'%s' undeclared", get_tok_str(t, NULL));
55415633 /* for simple function calls, we tolerate undeclared
55425634 external reference to int() function */
5543− s = external_global_sym(t, func_old_type, 0);
5635+ s = external_global_sym(t, &func_old_type, 0);
55445636 }
5545− vset(s->t, s->r, s->c);
5637+ vset(&s->type, s->r, s->c);
55465638 /* if forward reference, we must point to s */
55475639 if (vtop->r & VT_SYM) {
55485640 vtop->sym = s;
@@ -5564,9 +5656,9 @@ static void unary(void) · 15 unmodified lines
55645656 gaddrof();
55655657 next();
55665658 /* expect pointer on structure */
5567− if ((vtop->t & VT_BTYPE) != VT_STRUCT)
5659+ if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
55685660 expect("struct or union");
5569− s = sym_find(((unsigned)vtop->t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
5661+ s = vtop->type.ref;
55705662 /* find field */
55715663 tok |= SYM_FIELD;
55725664 while ((s = s->next) != NULL) {
@@ -5576,14 +5668,14 @@ static void unary(void) · 3 unmodified lines
55765668 if (!s)
55775669 error("field not found");
55785670 /* add field offset to pointer */
5579− vtop->t = char_pointer_type; /* change type to 'char *' */
5671+ vtop->type = char_pointer_type; /* change type to 'char *' */
55805672 vpushi(s->c);
55815673 gen_op('+');
55825674 /* change type to field type, and set to lvalue */
5583− vtop->t = s->t;
5675+ vtop->type = s->type;
55845676 /* an array is never an lvalue */
5585− if (!(vtop->t & VT_ARRAY)) {
5586− vtop->r |= lvalue_type(vtop->t);
5677+ if (!(vtop->type.t & VT_ARRAY)) {
5678+ vtop->r |= lvalue_type(vtop->type.t);
55875679 /* if bound checking, the referenced pointer must be checked */
55885680 if (do_bounds_check)
55895681 vtop->r |= VT_MUSTBOUND;
@@ -5600,11 +5692,11 @@ static void unary(void) · 10 unmodified lines
56005692 Sym *sa;
56015693
56025694 /* function call */
5603− if ((vtop->t & VT_BTYPE) != VT_FUNC) {
5695+ if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
56045696 /* pointer test (no array accepted) */
5605− if ((vtop->t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
5606− vtop->t = pointed_type(vtop->t);
5607− if ((vtop->t & VT_BTYPE) != VT_FUNC)
5697+ if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
5698+ vtop->type = *pointed_type(&vtop->type);
5699+ if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
56085700 goto error_func;
56095701 } else {
56105702 error_func:
@@ -5614,7 +5706,7 @@ static void unary(void) · 3 unmodified lines
56145706 vtop->r &= ~VT_LVAL; /* no lvalue */
56155707 }
56165708 /* get return type */
5617− s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT);
5709+ s = vtop->type.ref;
56185710 if (!nocode_wanted) {
56195711 save_regs(0); /* save used temporary registers */
56205712 gfunc_start(&gf, s->r);
@@ -5673,28 +5765,28 @@ static void unary(void) · 52 unmodified lines
56735765 }
56745766 #endif
56755767 /* compute first implicit argument if a structure is returned */
5676− if ((s->t & VT_BTYPE) == VT_STRUCT) {
5768+ if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
56775769 /* get some space for the returned structure */
5678− size = type_size(s->t, &align);
5770+ size = type_size(&s->type, &align);
56795771 loc = (loc - size) & -align;
5680− ret.t = s->t;
5772+ ret.type = s->type;
56815773 ret.r = VT_LOCAL | VT_LVAL;
56825774 /* pass it as 'int' to avoid structure arg passing
56835775 problems */
5684− vset(VT_INT, VT_LOCAL, loc);
5776+ vseti(VT_LOCAL, loc);
56855777 ret.c = vtop->c;
56865778 if (!nocode_wanted)
56875779 gfunc_param(&gf);
56885780 else
56895781 vtop--;
56905782 } else {
5691− ret.t = s->t;
5783+ ret.type = s->type;
56925784 ret.r2 = VT_CONST;
56935785 /* return in register */
5694− if (is_float(ret.t)) {
5786+ if (is_float(ret.type.t)) {
56955787 ret.r = REG_FRET;
56965788 } else {
5697− if ((ret.t & VT_BTYPE) == VT_LLONG)
5789+ if ((ret.type.t & VT_BTYPE) == VT_LLONG)
56985790 ret.r2 = REG_LRET;
56995791 ret.r = REG_IRET;
57005792 }
@@ -5721,7 +5813,7 @@ static void unary(void) · 20 unmodified lines
57215813 else
57225814 vtop--;
57235815 /* return value */
5724− vsetc(ret.t, ret.r, &ret.c);
5816+ vsetc(&ret.type, ret.r, &ret.c);
57255817 vtop->r2 = ret.r2;
57265818 } else {
57275819 break;
@@ -5790,7 +5882,7 @@ static void eand(void) · 62 unmodified lines
57905882 if (tok != TOK_LAND) {
57915883 if (t) {
57925884 t = gtst(1, t);
5793− vset(VT_INT, VT_JMPI, t);
5885+ vseti(VT_JMPI, t);
57945886 }
57955887 break;
57965888 }
@@ -5810,7 +5902,7 @@ static void eor(void) · 13 unmodified lines
58105902 if (tok != TOK_LOR) {
58115903 if (t) {
58125904 t = gtst(0, t);
5813− vset(VT_INT, VT_JMP, t);
5905+ vseti(VT_JMP, t);
58145906 }
58155907 break;
58165908 }
@@ -5823,8 +5915,9 @@ static void eor(void) · 6 unmodified lines
58235915 /* XXX: better constant handling */
58245916 static void expr_eq(void)
58255917 {
5826− int tt, u, r1, r2, rc, t1, t2, t, bt1, bt2;
5918+ int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
58275919 SValue sv;
5920+ CType type, type1, type2;
58285921
58295922 if (const_wanted) {
58305923 int c1, c;
@@ -5849,8 +5942,7 @@ static void expr_eq(void) · 18 unmodified lines
58495942 at the top because it is a branch point */
58505943 tt = gtst(1, 0);
58515944 gexpr();
5852− t1 = vtop->t;
5853− bt1 = t1 & VT_BTYPE;
5945+ type1 = vtop->type;
58545946 sv = *vtop; /* save value to handle it later */
58555947 vtop--; /* no vpop so that FP stack is not flushed */
58565948 skip(':');
@@ -5858,49 +5950,52 @@ static void expr_eq(void) · 1 unmodified line
58585950
58595951 gsym(tt);
58605952 expr_eq();
5861− t2 = vtop->t;
5953+ type2 = vtop->type;
58625954
5955+ t1 = type1.t;
5956+ bt1 = t1 & VT_BTYPE;
5957+ t2 = type2.t;
58635958 bt2 = t2 & VT_BTYPE;
58645959 /* cast operands to correct type according to ISOC rules */
58655960 if (is_float(bt1) || is_float(bt2)) {
58665961 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5867− t = VT_LDOUBLE;
5962+ type.t = VT_LDOUBLE;
58685963 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5869− t = VT_DOUBLE;
5964+ type.t = VT_DOUBLE;
58705965 } else {
5871− t = VT_FLOAT;
5966+ type.t = VT_FLOAT;
58725967 }
58735968 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
58745969 /* cast to biggest op */
5875− t = VT_LLONG;
5970+ type.t = VT_LLONG;
58765971 /* convert to unsigned if it does not fit in a long long */
58775972 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
58785973 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5879− t |= VT_UNSIGNED;
5974+ type.t |= VT_UNSIGNED;
58805975 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
58815976 /* XXX: test pointer compatibility */
5882− t = t1;
5977+ type = type1;
58835978 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
58845979 /* XXX: test structure compatibility */
5885− t = t1;
5980+ type = type1;
58865981 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
58875982 /* NOTE: as an extension, we accept void on only one side */
5888− t = VT_VOID;
5983+ type.t = VT_VOID;
58895984 } else {
58905985 /* integer operations */
5891− t = VT_INT;
5986+ type.t = VT_INT;
58925987 /* convert to unsigned if it does not fit in an integer */
58935988 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
58945989 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5895− t |= VT_UNSIGNED;
5990+ type.t |= VT_UNSIGNED;
58965991 }
58975992
58985993 /* now we convert second operand */
5899− gen_cast(t);
5994+ gen_cast(&type);
59005995 rc = RC_INT;
5901− if (is_float(t)) {
5996+ if (is_float(type.t)) {
59025997 rc = RC_FLOAT;
5903− } else if ((t & VT_BTYPE) == VT_LLONG) {
5998+ } else if ((type.t & VT_BTYPE) == VT_LLONG) {
59045999 /* for long longs, we use fixed registers to avoid having
59056000 to handle a complicated move */
59066001 rc = RC_IRET;
@@ -5912,7 +6007,7 @@ static void expr_eq(void) · 5 unmodified lines
59126007 gsym(u);
59136008 /* put again first value and cast it */
59146009 *vtop = sv;
5915− gen_cast(t);
6010+ gen_cast(&type);
59166011 r1 = gv(rc);
59176012 move_reg(r2, r1);
59186013 vtop->r = r2;
@@ -5933,32 +6028,30 @@ static void gexpr(void) · 14 unmodified lines
59336028 }
59346029
59356030 /* parse an expression and return its type without any side effect. */
5936−static int expr_type(void)
6031+static void expr_type(CType *type)
59376032 {
5938− int a, t;
6033+ int a;
59396034
59406035 a = nocode_wanted;
59416036 nocode_wanted = 1;
59426037 gexpr();
5943− t = vtop->t;
6038+ *type = vtop->type;
59446039 vpop();
59456040 nocode_wanted = a;
5946− return t;
59476041 }
59486042
59496043 /* parse a unary expression and return its type without any side
59506044 effect. */
5951−static int unary_type(void)
6045+static void unary_type(CType *type)
59526046 {
5953− int a, t;
6047+ int a;
59546048
59556049 a = nocode_wanted;
59566050 nocode_wanted = 1;
59576051 unary();
5958− t = vtop->t;
6052+ *type = vtop->type;
59596053 vpop();
59606054 nocode_wanted = a;
5961− return t;
59626055 }
59636056
59646057 /* parse a constant expression and return value in vtop. */
@@ -6068,16 +6161,19 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 103 unmodified lines
60686161 next();
60696162 if (tok != ';') {
60706163 gexpr();
6071− gen_assign_cast(func_vt);
6072− if ((func_vt & VT_BTYPE) == VT_STRUCT) {
6164+ gen_assign_cast(&func_vt);
6165+ if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
6166+ CType type;
60736167 /* if returning structure, must copy it to implicit
60746168 first pointer arg location */
6075− vset(mk_pointer(func_vt), VT_LOCAL | VT_LVAL, func_vc);
6169+ type = func_vt;
6170+ mk_pointer(&type);
6171+ vset(&type, VT_LOCAL | VT_LVAL, func_vc);
60766172 indir();
60776173 vswap();
60786174 /* copy structure value to pointer */
60796175 vstore();
6080− } else if (is_float(func_vt)) {
6176+ } else if (is_float(func_vt.t)) {
60816177 gv(RC_FRET);
60826178 } else {
60836179 gv(RC_IRET);
@@ -6184,7 +6280,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 100 unmodified lines
61846280 /* since a case is like a label, we must skip it with a jmp */
61856281 b = gjmp(0);
61866282 gsym(*case_sym);
6187− vset(VT_INT, case_reg, 0);
6283+ vseti(case_reg, 0);
61886284 vpushi(v1);
61896285 if (v1 == v2) {
61906286 gen_op(TOK_EQ);
@@ -6192,7 +6288,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 1 unmodified line
61926288 } else {
61936289 gen_op(TOK_GE);
61946290 *case_sym = gtst(1, 0);
6195− vset(VT_INT, case_reg, 0);
6291+ vseti(case_reg, 0);
61966292 vpushi(v2);
61976293 gen_op(TOK_LE);
61986294 *case_sym = gtst(1, *case_sym);
@@ -6217,7 +6313,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 18 unmodified lines
62176313 /* computed goto */
62186314 next();
62196315 gexpr();
6220− if ((vtop->t & VT_BTYPE) != VT_PTR)
6316+ if ((vtop->type.t & VT_BTYPE) != VT_PTR)
62216317 expect("pointer");
62226318 ggoto();
62236319 } else if (tok >= TOK_UIDENT) {
@@ -6271,12 +6367,13 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 47 unmodified lines
62716367 address. cur_index/cur_field is the pointer to the current
62726368 value. 'size_only' is true if only size info is needed (only used
62736369 in arrays) */
6274−static void decl_designator(int t, Section *sec, unsigned long c,
6370+static void decl_designator(CType *type, Section *sec, unsigned long c,
62756371 int *cur_index, Sym **cur_field,
62766372 int size_only)
62776373 {
62786374 Sym *s, *f;
62796375 int notfirst, index, align, l;
6376+ CType type1;
62806377
62816378 notfirst = 0;
62826379 if (gnu_ext && (l = is_label()) != 0)
@@ -6284,9 +6381,9 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 1 unmodified line
62846381
62856382 while (tok == '[' || tok == '.') {
62866383 if (tok == '[') {
6287− if (!(t & VT_ARRAY))
6384+ if (!(type->t & VT_ARRAY))
62886385 expect("array type");
6289− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
6386+ s = type->ref;
62906387 next();
62916388 index = expr_const();
62926389 if (index < 0 || (s->c >= 0 && index >= s->c))
@@ -6294,16 +6391,16 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 1 unmodified line
62946391 skip(']');
62956392 if (!notfirst)
62966393 *cur_index = index;
6297− t = pointed_type(t);
6298− c += index * type_size(t, &align);
6394+ type = pointed_type(type);
6395+ c += index * type_size(type, &align);
62996396 } else {
63006397 next();
63016398 l = tok;
63026399 next();
63036400 struct_field:
6304− if ((t & VT_BTYPE) != VT_STRUCT)
6401+ if ((type->t & VT_BTYPE) != VT_STRUCT)
63056402 expect("struct/union type");
6306− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
6403+ s = type->ref;
63076404 l |= SYM_FIELD;
63086405 f = s->next;
63096406 while (f) {
@@ -6315,7 +6412,10 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 5 unmodified lines
63156412 expect("field");
63166413 if (!notfirst)
63176414 *cur_field = f;
6318− t = f->t | (t & ~VT_TYPE);
6415+ /* XXX: suppress this mess by using explicit storage field */
6416+ type1 = f->type;
6417+ type1.t |= (type->t & ~VT_TYPE);
6418+ type = &type1;
63196419 c += f->c;
63206420 }
63216421 notfirst = 1;
@@ -6328,19 +6428,22 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 6 unmodified lines
63286428 expect("=");
63296429 }
63306430 } else {
6331− if (t & VT_ARRAY) {
6431+ if (type->t & VT_ARRAY) {
63326432 index = *cur_index;
6333− t = pointed_type(t);
6334− c += index * type_size(t, &align);
6433+ type = pointed_type(type);
6434+ c += index * type_size(type, &align);
63356435 } else {
63366436 f = *cur_field;
63376437 if (!f)
63386438 error("too many field init");
6339− t = f->t | (t & ~VT_TYPE);
6439+ /* XXX: suppress this mess by using explicit storage field */
6440+ type1 = f->type;
6441+ type1.t |= (type->t & ~VT_TYPE);
6442+ type = &type1;
63406443 c += f->c;
63416444 }
63426445 }
6343− decl_initializer(t, sec, c, 0, size_only);
6446+ decl_initializer(type, sec, c, 0, size_only);
63446447 }
63456448
63466449 #define EXPR_VAL 0
@@ -6348,7 +6451,7 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 1 unmodified line
63486451 #define EXPR_ANY 2
63496452
63506453 /* store a value or an expression directly in global data or in local array */
6351−static void init_putv(int t, Section *sec, unsigned long c,
6454+static void init_putv(CType *type, Section *sec, unsigned long c,
63526455 int v, int expr_type)
63536456 {
63546457 int saved_global_expr, bt;
@@ -6376,8 +6479,8 @@ static void init_putv(int t, Section *sec, unsigned long c, · 21 unmodified lines
63766479 if (sec) {
63776480 /* XXX: not portable */
63786481 /* XXX: generate error if incorrect relocation */
6379− gen_assign_cast(t);
6380− bt = t & VT_BTYPE;
6482+ gen_assign_cast(type);
6483+ bt = type->t & VT_BTYPE;
63816484 ptr = sec->data + c;
63826485 if ((vtop->r & VT_SYM) &&
63836486 (bt == VT_BYTE ||
@@ -6411,7 +6514,7 @@ static void init_putv(int t, Section *sec, unsigned long c, · 27 unmodified lines
64116514 }
64126515 vtop--;
64136516 } else {
6414− vset(t, VT_LOCAL, c);
6517+ vset(type, VT_LOCAL, c);
64156518 vswap();
64166519 vstore();
64176520 vpop();
@@ -6419,7 +6522,7 @@ static void init_putv(int t, Section *sec, unsigned long c, · 1 unmodified line
64196522 }
64206523
64216524 /* put zeros for variable based init */
6422−static void init_putz(int t, Section *sec, unsigned long c, int size)
6525+static void init_putz(CType *t, Section *sec, unsigned long c, int size)
64236526 {
64246527 GFuncContext gf;
64256528
@@ -6431,9 +6534,9 @@ static void init_putz(int t, Section *sec, unsigned long c, int size) · 5 unmodified lines
64316534 gfunc_param(&gf);
64326535 vpushi(0);
64336536 gfunc_param(&gf);
6434− vset(VT_INT, VT_LOCAL, c);
6537+ vseti(VT_LOCAL, c);
64356538 gfunc_param(&gf);
6436− vpush_global_sym(func_old_type, TOK_memset);
6539+ vpush_global_sym(&func_old_type, TOK_memset);
64376540 gfunc_call(&gf);
64386541 }
64396542 }
@@ -6443,18 +6546,19 @@ static void init_putz(int t, Section *sec, unsigned long c, int size) · 3 unmodified lines
64436546 allocation. 'first' is true if array '{' must be read (multi
64446547 dimension implicit array init handling). 'size_only' is true if
64456548 size only evaluation is wanted (only for arrays). */
6446−static void decl_initializer(int t, Section *sec, unsigned long c,
6549+static void decl_initializer(CType *type, Section *sec, unsigned long c,
64476550 int first, int size_only)
64486551 {
64496552 int index, array_length, n, no_oblock, nb, parlevel, i;
6450− int t1, size1, align1, expr_type;
6553+ int size1, align1, expr_type;
64516554 Sym *s, *f;
6555+ CType *t1;
64526556
6453− if (t & VT_ARRAY) {
6454− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
6557+ if (type->t & VT_ARRAY) {
6558+ s = type->ref;
64556559 n = s->c;
64566560 array_length = 0;
6457− t1 = pointed_type(t);
6561+ t1 = pointed_type(type);
64586562 size1 = type_size(t1, &align1);
64596563
64606564 no_oblock = 1;
@@ -6467,9 +6571,9 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 6 unmodified lines
64676571 /* only parse strings here if correct type (otherwise: handle
64686572 them as ((w)char *) expressions */
64696573 if ((tok == TOK_LSTR &&
6470− (t1 & VT_BTYPE) == VT_INT) ||
6574+ (t1->t & VT_BTYPE) == VT_INT) ||
64716575 (tok == TOK_STR &&
6472− (t1 & VT_BTYPE) == VT_BYTE)) {
6576+ (t1->t & VT_BTYPE) == VT_BYTE)) {
64736577 while (tok == TOK_STR || tok == TOK_LSTR) {
64746578 int cstr_len, ch;
64756579 CString *cstr;
@@ -6517,7 +6621,7 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 41 unmodified lines
65176621 } else {
65186622 index = 0;
65196623 while (tok != '}') {
6520− decl_designator(t, sec, c, &index, NULL, size_only);
6624+ decl_designator(type, sec, c, &index, NULL, size_only);
65216625 if (n >= 0 && index >= n)
65226626 error("index too large");
65236627 /* must put zero in holes (note that doing it that way
@@ -6549,23 +6653,23 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 25 unmodified lines
65496653 /* patch type size if needed */
65506654 if (n < 0)
65516655 s->c = array_length;
6552− } else if ((t & VT_BTYPE) == VT_STRUCT && tok == '{') {
6656+ } else if ((type->t & VT_BTYPE) == VT_STRUCT && tok == '{') {
65536657 /* XXX: union needs only one init */
65546658 next();
6555− s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT);
6659+ s = type->ref;
65566660 f = s->next;
65576661 array_length = 0;
65586662 index = 0;
65596663 n = s->c;
65606664 while (tok != '}') {
6561− decl_designator(t, sec, c, NULL, &f, size_only);
6665+ decl_designator(type, sec, c, NULL, &f, size_only);
65626666 /* fill with zero between fields */
65636667 index = f->c;
65646668 if (!size_only && array_length < index) {
6565− init_putz(t, sec, c + array_length,
6669+ init_putz(type, sec, c + array_length,
65666670 index - array_length);
65676671 }
6568− index = index + type_size(f->t, &align1);
6672+ index = index + type_size(&f->type, &align1);
65696673 if (index > array_length)
65706674 array_length = index;
65716675 if (tok == '}')
@@ -6575,13 +6679,13 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 3 unmodified lines
65756679 }
65766680 /* put zeros at the end */
65776681 if (!size_only && array_length < n) {
6578− init_putz(t, sec, c + array_length,
6682+ init_putz(type, sec, c + array_length,
65796683 n - array_length);
65806684 }
65816685 skip('}');
65826686 } else if (tok == '{') {
65836687 next();
6584− decl_initializer(t, sec, c, first, size_only);
6688+ decl_initializer(type, sec, c, first, size_only);
65856689 skip('}');
65866690 } else if (size_only) {
65876691 /* just skip expression */
@@ -6600,7 +6704,7 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 12 unmodified lines
66006704 expr_type = EXPR_CONST;
66016705 if (!sec)
66026706 expr_type = EXPR_ANY;
6603− init_putv(t, sec, c, 0, expr_type);
6707+ init_putv(type, sec, c, 0, expr_type);
66046708 }
66056709 }
66066710
@@ -6611,7 +6715,7 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 4 unmodified lines
66116715 parsed. If 'v' is zero, then a reference to the new object is put
66126716 in the value stack. If 'has_init' is 2, a special parsing is done
66136717 to handle string constants. */
6614−static void decl_initializer_alloc(int t, AttributeDef *ad, int r,
6718+static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
66156719 int has_init, int v, int scope)
66166720 {
66176721 int size, align, addr, data_offset;
@@ -6620,7 +6724,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 2 unmodified lines
66206724 TokenString init_str;
66216725 Section *sec;
66226726
6623− size = type_size(t, &align);
6727+ size = type_size(type, &align);
66246728 /* If unknown size, we must evaluate it before
66256729 evaluating initializers because
66266730 initializers can generate global data too
@@ -6662,13 +6766,13 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 35 unmodified lines
66626766
66636767 macro_ptr = init_str.str;
66646768 next();
6665− decl_initializer(t, NULL, 0, 1, 1);
6769+ decl_initializer(type, NULL, 0, 1, 1);
66666770 /* prepare second initializer parsing */
66676771 macro_ptr = init_str.str;
66686772 next();
66696773
66706774 /* if still unknown size, error */
6671− size = type_size(t, &align);
6775+ size = type_size(type, &align);
66726776 if (size < 0)
66736777 error("unknown type size");
66746778 }
@@ -6677,7 +6781,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 2 unmodified lines
66776781 align = ad->aligned;
66786782 if ((r & VT_VALMASK) == VT_LOCAL) {
66796783 sec = NULL;
6680− if (do_bounds_check && (t & VT_ARRAY))
6784+ if (do_bounds_check && (type->t & VT_ARRAY))
66816785 loc--;
66826786 #ifdef TCC_TARGET_IL
66836787 /* XXX: ugly patch to allocate local variables for IL, just
@@ -6691,7 +6795,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 7 unmodified lines
66916795 /* handles bounds */
66926796 /* XXX: currently, since we do only one pass, we cannot track
66936797 '&' operators, so we add only arrays */
6694− if (do_bounds_check && (t & VT_ARRAY)) {
6798+ if (do_bounds_check && (type->t & VT_ARRAY)) {
66956799 unsigned long *bounds_ptr;
66966800 /* add padding between regions */
66976801 loc--;
@@ -6727,10 +6831,10 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 29 unmodified lines
67276831 if (!sec) {
67286832 if (v) {
67296833 /* local variable */
6730− sym_push(v, t, r, addr);
6834+ sym_push(v, type, r, addr);
67316835 } else {
67326836 /* push local reference */
6733− vset(t, r, addr);
6837+ vset(type, r, addr);
67346838 }
67356839 } else {
67366840 Sym *sym;
@@ -6741,24 +6845,24 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 4 unmodified lines
67416845 sym = sym_find(v);
67426846 if (!sym)
67436847 goto do_def;
6744− if (!is_compatible_types(sym->t, t))
6848+ if (!is_compatible_types(&sym->type, type))
67456849 error("incompatible types for redefinition of '%s'",
67466850 get_tok_str(v, NULL));
6747− if (!(sym->t & VT_EXTERN))
6851+ if (!(sym->type.t & VT_EXTERN))
67486852 error("redefinition of '%s'", get_tok_str(v, NULL));
6749− sym->t &= ~VT_EXTERN;
6853+ sym->type.t &= ~VT_EXTERN;
67506854 } else {
67516855 do_def:
6752− sym = sym_push(v, t, r | VT_SYM, 0);
6856+ sym = sym_push(v, type, r | VT_SYM, 0);
67536857 }
67546858 put_extern_sym(sym, sec, addr, size);
67556859 } else {
67566860 CValue cval;
67576861
67586862 /* push global reference */
6759− sym = get_sym_ref(t, sec, addr, size);
6863+ sym = get_sym_ref(type, sec, addr, size);
67606864 cval.ul = 0;
6761− vsetc(t, VT_CONST | VT_SYM, &cval);
6865+ vsetc(type, VT_CONST | VT_SYM, &cval);
67626866 vtop->sym = sym;
67636867 }
67646868
@@ -6775,7 +6879,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 10 unmodified lines
67756879 }
67766880 }
67776881 if (has_init) {
6778− decl_initializer(t, sec, addr, 1, 0);
6882+ decl_initializer(type, sec, addr, 1, 0);
67796883 /* restore parse state if needed */
67806884 if (init_str.str) {
67816885 tok_str_free(init_str.str);
@@ -6791,7 +6895,7 @@ void put_func_debug(Sym *sym) · 9 unmodified lines
67916895 /* stabs info */
67926896 /* XXX: we put here a dummy type */
67936897 snprintf(buf, sizeof(buf), "%s:%c1",
6794− funcname, sym->t & VT_STATIC ? 'f' : 'F');
6898+ funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
67956899 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
67966900 cur_text_section, sym->c);
67976901 last_ind = 0;
@@ -6862,19 +6966,22 @@ void analyse_function(void) · 64 unmodified lines
68626966 static void func_decl_list(Sym *func_sym)
68636967 {
68646968 AttributeDef ad;
6865− int b, v, t;
6969+ int v;
68666970 Sym *s;
6971+ CType btype, type;
6972+
68676973 /* parse each declaration */
68686974 while (tok != '{' && tok != ';' && tok != TOK_EOF) {
6869− if (!parse_btype(&b, &ad))
6975+ if (!parse_btype(&btype, &ad))
68706976 expect("declaration list");
6871− if (((b & VT_BTYPE) == VT_ENUM ||
6872− (b & VT_BTYPE) == VT_STRUCT) &&
6977+ if (((btype.t & VT_BTYPE) == VT_ENUM ||
6978+ (btype.t & VT_BTYPE) == VT_STRUCT) &&
68736979 tok == ';') {
68746980 /* we accept no variable after */
68756981 } else {
68766982 for(;;) {
6877− t = type_decl(&ad, &v, b, TYPE_DIRECT);
6983+ type = btype;
6984+ type_decl(&type, &ad, &v, TYPE_DIRECT);
68786985 /* find parameter in function parameter list */
68796986 s = func_sym->next;
68806987 while (s != NULL) {
@@ -6886,10 +6993,10 @@ static void func_decl_list(Sym *func_sym) · 5 unmodified lines
68866993 get_tok_str(v, NULL));
68876994 found:
68886995 /* check that no storage specifier except 'register' was given */
6889− if (t & (VT_EXTERN | VT_STATIC | VT_TYPEDEF))
6996+ if (type.t & (VT_EXTERN | VT_STATIC | VT_TYPEDEF))
68906997 error("storage class specified for '%s'", get_tok_str(v, NULL));
68916998 /* we can add the type (NOTE: it could be local to the function) */
6892− s->t = t;
6999+ s->type = type;
68937000 /* accept other parameters */
68947001 if (tok == ',')
68957002 next();
@@ -6904,12 +7011,13 @@ static void func_decl_list(Sym *func_sym) · 8 unmodified lines
69047011 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
69057012 static void decl(int l)
69067013 {
6907− int t, b, v, has_init, r;
7014+ int v, has_init, r;
7015+ CType type, btype;
69087016 Sym *sym;
69097017 AttributeDef ad;
69107018
69117019 while (1) {
6912− if (!parse_btype(&b, &ad)) {
7020+ if (!parse_btype(&btype, &ad)) {
69137021 /* skip redundant ';' */
69147022 /* XXX: find more elegant solution */
69157023 if (tok == ';') {
@@ -6920,17 +7028,18 @@ static void decl(int l) · 4 unmodified lines
69207028 type. Only accepted when defining global data */
69217029 if (l == VT_LOCAL || tok < TOK_DEFINE)
69227030 break;
6923− b = VT_INT;
7031+ btype.t = VT_INT;
69247032 }
6925− if (((b & VT_BTYPE) == VT_ENUM ||
6926− (b & VT_BTYPE) == VT_STRUCT) &&
7033+ if (((btype.t & VT_BTYPE) == VT_ENUM ||
7034+ (btype.t & VT_BTYPE) == VT_STRUCT) &&
69277035 tok == ';') {
69287036 /* we accept no variable after */
69297037 next();
69307038 continue;
69317039 }
69327040 while (1) { /* iterate thru each declaration */
6933− t = type_decl(&ad, &v, b, TYPE_DIRECT);
7041+ type = btype;
7042+ type_decl(&type, &ad, &v, TYPE_DIRECT);
69347043 #if 0
69357044 {
69367045 char buf[500];
@@ -6938,10 +7047,10 @@ static void decl(int l) · 1 unmodified line
69387047 printf("type = '%s'\n", buf);
69397048 }
69407049 #endif
6941− if ((t & VT_BTYPE) == VT_FUNC) {
7050+ if ((type.t & VT_BTYPE) == VT_FUNC) {
69427051 /* if old style function prototype, we accept a
69437052 declaration list */
6944− sym = sym_find(((unsigned)t >> VT_STRUCT_SHIFT));
7053+ sym = type.ref;
69457054 if (sym->c == FUNC_OLD)
69467055 func_decl_list(sym);
69477056 }
@@ -6955,7 +7064,7 @@ static void decl(int l) · 7 unmodified lines
69557064
69567065 if (l == VT_LOCAL)
69577066 error("cannot use local functions");
6958− if (!(t & VT_FUNC))
7067+ if (!(type.t & VT_FUNC))
69597068 expect("function definition");
69607069
69617070 #ifdef CONFIG_REG_VARS
@@ -6998,10 +7107,11 @@ static void decl(int l) · 36 unmodified lines
69987107 sym = sym_find(v);
69997108 if (sym) {
70007109 /* if symbol is already defined, then put complete type */
7001− sym->t = t;
7110+ sym->type = type;
70027111 } else {
70037112 /* put function symbol */
7004− sym = sym_push1(&global_stack, v, t, 0);
7113+ sym = sym_push1(&global_stack, v, type.t, 0);
7114+ sym->type.ref = type.ref;
70057115 }
70067116 /* NOTE: we patch the symbol size later */
70077117 put_extern_sym(sym, cur_text_section, ind, 0);
@@ -7012,7 +7122,7 @@ static void decl(int l) · 4 unmodified lines
70127122 put_func_debug(sym);
70137123 /* push a dummy symbol to enable local sym storage */
70147124 sym_push1(&local_stack, 0, 0, 0);
7015− gfunc_prolog(t);
7125+ gfunc_prolog(&type);
70167126 loc = 0;
70177127 rsym = 0;
70187128 #ifdef CONFIG_REG_VARS
@@ -7049,7 +7159,7 @@ static void decl(int l) · 30 unmodified lines
70497159 put_stabn(N_FUN, 0, 0, ind - func_ind);
70507160 }
70517161 funcname = ""; /* for safety */
7052− func_vt = VT_VOID; /* for safety */
7162+ func_vt.t = VT_VOID; /* for safety */
70537163 ind = 0; /* for safety */
70547164
70557165 #ifdef CONFIG_REG_VARS
@@ -7058,30 +7168,31 @@ static void decl(int l) · 2 unmodified lines
70587168 #endif
70597169 break;
70607170 } else {
7061− if (b & VT_TYPEDEF) {
7171+ if (btype.t & VT_TYPEDEF) {
70627172 /* save typedefed type */
70637173 /* XXX: test storage specifiers ? */
7064− sym_push(v, t | VT_TYPEDEF, 0, 0);
7065− } else if ((t & VT_BTYPE) == VT_FUNC) {
7174+ sym = sym_push(v, &type, 0, 0);
7175+ sym->type.t |= VT_TYPEDEF;
7176+ } else if ((type.t & VT_BTYPE) == VT_FUNC) {
70667177 /* external function definition */
7067− external_sym(v, t, 0);
7178+ external_sym(v, &type, 0);
70687179 } else {
70697180 /* not lvalue if array */
70707181 r = 0;
7071− if (!(t & VT_ARRAY))
7072− r |= lvalue_type(t);
7073− if (b & VT_EXTERN) {
7182+ if (!(type.t & VT_ARRAY))
7183+ r |= lvalue_type(type.t);
7184+ if (btype.t & VT_EXTERN) {
70747185 /* external variable */
7075− external_sym(v, t, r);
7186+ external_sym(v, &type, r);
70767187 } else {
7077− if (t & VT_STATIC)
7188+ if (type.t & VT_STATIC)
70787189 r |= VT_CONST;
70797190 else
70807191 r |= l;
70817192 has_init = (tok == '=');
70827193 if (has_init)
70837194 next();
7084− decl_initializer_alloc(t, &ad, r,
7195+ decl_initializer_alloc(&type, &ad, r,
70857196 has_init, v, l);
70867197 }
70877198 }
@@ -7095,29 +7206,12 @@ static void decl(int l) · 7 unmodified lines
70957206 }
70967207 }
70977208
7098−/* free define stack until top reaches 'b' */
7099−static void free_defines(Sym *b)
7100−{
7101− Sym *top, *top1;
7102−
7103− top = define_stack.top;
7104− while (top != b) {
7105− top1 = top->prev;
7106− /* do not free args or predefined defines */
7107− if (top->c)
7108− tok_str_free((int *)top->c);
7109− sym_pop(&define_stack, top1);
7110− top = top1;
7111− }
7112−}
7113−
71147209 /* compile the C file opened in 'file'. Return non zero if errors. */
71157210 static int tcc_compile(TCCState *s1)
71167211 {
7117− Sym *define_start, *sym;
7212+ Sym *define_start;
71187213 char buf[512];
71197214 volatile int section_sym;
7120− int p;
71217215
71227216 #ifdef INC_DEBUG
71237217 printf("%s: **** new file\n", file->filename);
@@ -7152,13 +7246,15 @@ static int tcc_compile(TCCState *s1) · 28 unmodified lines
71527246 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
71537247 SHN_ABS, file->filename);
71547248
7155− /* define common 'char *' type because it is often used internally
7156− for arrays and struct dereference */
7157− char_pointer_type = mk_pointer(VT_BYTE);
7158− /* define an old type function 'int func()' */
7159− p = anon_sym++;
7160− sym = sym_push(p, 0, FUNC_CDECL, FUNC_OLD);
7161− func_old_type = VT_FUNC | (p << VT_STRUCT_SHIFT);
7249+ /* define some often used types */
7250+ int_type.t = VT_INT;
7251+
7252+ char_pointer_type.t = VT_BYTE;
7253+ mk_pointer(&char_pointer_type);
7254+
7255+ func_old_type.t = VT_FUNC;
7256+ func_old_type.ref = sym_push(0, &int_type, FUNC_CDECL, FUNC_OLD);
7257+
71627258 #if 0
71637259 /* define 'void *alloca(unsigned int)' builtin function */
71647260 {
@@ -7173,7 +7269,7 @@ static int tcc_compile(TCCState *s1) · 8 unmodified lines
71737269 }
71747270 #endif
71757271
7176− define_start = define_stack.top;
7272+ define_start = define_stack;
71777273
71787274 if (setjmp(s1->error_jmp_buf) == 0) {
71797275 s1->nb_errors = 0;
@@ -7222,7 +7318,7 @@ int tcc_compile_string(TCCState *s, const char *str) · 42 unmodified lines
72227318 return ret;
72237319 }
72247320
7225−/* define a symbol. A value can also be provided with the '=' operator */
7321+/* define a preprocessor symbol. A value can also be provided with the '=' operator */
72267322 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
72277323 {
72287324 BufferedFile bf1, *bf = &bf1;
@@ -7252,15 +7348,16 @@ void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) · 23 unmodified lines
72527348 file = NULL;
72537349 }
72547350
7351+/* undefine a preprocessor symbol */
72557352 void tcc_undefine_symbol(TCCState *s1, const char *sym)
72567353 {
72577354 TokenSym *ts;
72587355 Sym *s;
72597356 ts = tok_alloc(sym, strlen(sym));
7260− s = sym_find1(&define_stack, tok);
7357+ s = define_find(ts->tok);
72617358 /* undefine symbol by putting an invalid name */
72627359 if (s)
7263− sym_undef(&define_stack, s);
7360+ define_undef(s);
72647361 }
72657362
72667363 #include "tccelf.c"
@@ -7387,6 +7484,8 @@ static void rt_printline(unsigned long wanted_pc) · 120 unmodified lines
73877484 fprintf(stderr, "\n");
73887485 }
73897486
7487+#ifndef WIN32
7488+
73907489 #ifdef __i386__
73917490
73927491 #ifndef EIP
@@ -7417,7 +7516,7 @@ static int rt_get_caller_pc(unsigned long *paddr, · 24 unmodified lines
74177516 }
74187517 }
74197518 #else
7420−#error add corresponding function
7519+#error add arch specific rt_get_caller_pc()
74217520 #endif
74227521
74237522 /* emit a run time error at position 'pc' */
@@ -7444,7 +7543,6 @@ void rt_error(struct ucontext *uc, const char *fmt, ...) · 20 unmodified lines
74447543 va_end(ap);
74457544 }
74467545
7447−#ifndef WIN32
74487546 /* signal handler for fatal errors */
74497547 static void sig_error(int signum, siginfo_t *siginf, void *puc)
74507548 {
@@ -7594,10 +7692,10 @@ TCCState *tcc_new(void) · 143 unmodified lines
75947692
75957693 /* we add dummy defines for some special macros to speed up tests
75967694 and to have working defined() */
7597− sym_push1(&define_stack, TOK___LINE__, MACRO_OBJ, 0);
7598− sym_push1(&define_stack, TOK___FILE__, MACRO_OBJ, 0);
7599− sym_push1(&define_stack, TOK___DATE__, MACRO_OBJ, 0);
7600− sym_push1(&define_stack, TOK___TIME__, MACRO_OBJ, 0);
7695+ define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
7696+ define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
7697+ define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
7698+ define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
76017699
76027700 /* standard defines */
76037701 tcc_define_symbol(s, "__STDC__", NULL);
@@ -7889,6 +7987,19 @@ int tcc_set_output_type(TCCState *s, int output_type) · 285 unmodified lines
78897987
78907988 #if !defined(LIBTCC)
78917989
7990+static int64_t getclock_us(void)
7991+{
7992+#ifdef WIN32
7993+ struct _timeb tb;
7994+ _ftime(&tb);
7995+ return (tb.time * 1000LL + tb.millitm) * 1000LL;
7996+#else
7997+ struct timeval tv;
7998+ gettimeofday(&tv, NULL);
7999+ return tv.tv_sec * 1000000LL + tv.tv_usec;
8000+#endif
8001+}
8002+
78928003 void help(void)
78938004 {
78948005 printf("tcc version 0.9.13 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
@@ -7930,6 +8041,7 @@ int main(int argc, char **argv) · 35 unmodified lines
79308041 char **files;
79318042 int nb_files, nb_libraries, nb_objfiles, dminus, ret;
79328043 char objfilename[1024];
8044+ int64_t start_time = 0;
79338045
79348046 s = tcc_new();
79358047 output_type = TCC_OUTPUT_MEMORY;
@@ -8061,6 +8173,10 @@ int main(int argc, char **argv) · 125 unmodified lines
80618173 outfile = objfilename;
80628174 }
80638175
8176+ if (do_bench) {
8177+ start_time = getclock_us();
8178+ }
8179+
80648180 tcc_set_output_type(s, output_type);
80658181
80668182 /* compile or add each files or library */
@@ -8083,8 +8199,16 @@ int main(int argc, char **argv) · 16 unmodified lines
80838199 tcc_free(files);
80848200
80858201 if (do_bench) {
8086− printf("total: %d idents, %d lines, %d bytes\n",
8087− tok_ident - TOK_IDENT, total_lines, total_bytes);
8202+ double total_time;
8203+ total_time = (double)(getclock_us() - start_time) / 1000000.0;
8204+ if (total_time < 0.001)
8205+ total_time = 0.001;
8206+ if (total_bytes < 1)
8207+ total_bytes = 1;
8208+ printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
8209+ tok_ident - TOK_IDENT, total_lines, total_bytes,
8210+ total_time, (int)(total_lines / total_time),
8211+ total_bytes / total_time / 1000000.0);
80888212 }
80898213
80908214 if (s->output_type != TCC_OUTPUT_MEMORY) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t