| @@ -28,7 +28,11 @@ · 27 unmodified lines |
| 28 | 28 | #include <unistd.h> |
| 29 | 29 | #include <fcntl.h> |
| 30 | 30 | #include <setjmp.h> |
| 31 | +#ifdef WIN32 |
| 32 | +#include <sys/timeb.h> |
| 33 | +#endif |
| 31 | 34 | #ifndef WIN32 |
| 35 | +#include <sys/time.h> |
| 32 | 36 | #include <sys/ucontext.h> |
| 33 | 37 | #endif |
| 34 | 38 | #include "elf.h" |
| @@ -80,6 +84,7 @@ · 45 unmodified lines |
| 80 | 84 | /* token symbol management */ |
| 81 | 85 | typedef struct TokenSym { |
| 82 | 86 | struct TokenSym *hash_next; |
| 87 | + struct Sym *sym_define; /* direct pointer to define */ |
| 83 | 88 | int tok; /* token number */ |
| 84 | 89 | int len; |
| 85 | 90 | char str[1]; |
| @@ -92,6 +97,12 @@ typedef struct CString { · 6 unmodified lines |
| 92 | 97 | void *data_allocated; /* if non NULL, data has been malloced */ |
| 93 | 98 | } CString; |
| 94 | 99 | |
| 100 | +/* type definition */ |
| 101 | +typedef struct CType { |
| 102 | + int t; |
| 103 | + struct Sym *ref; |
| 104 | +} CType; |
| 105 | + |
| 95 | 106 | /* constant value */ |
| 96 | 107 | typedef union CValue { |
| 97 | 108 | long double ld; |
| @@ -109,7 +120,7 @@ typedef union CValue { · 11 unmodified lines |
| 109 | 120 | |
| 110 | 121 | /* value on stack */ |
| 111 | 122 | typedef struct SValue { |
| 112 | | − int t; /* type */ |
| 123 | + CType type; /* type */ |
| 113 | 124 | unsigned short r; /* register + flags */ |
| 114 | 125 | unsigned short r2; /* second register, used for 'long long' |
| 115 | 126 | type. If not used, set to VT_CONST */ |
| @@ -120,9 +131,9 @@ typedef struct SValue { · 4 unmodified lines |
| 120 | 131 | /* symbol management */ |
| 121 | 132 | typedef struct Sym { |
| 122 | 133 | int v; /* symbol token */ |
| 123 | | − int t; /* associated type */ |
| 124 | 134 | int r; /* associated register */ |
| 125 | 135 | int c; /* associated number */ |
| 136 | + CType type; /* associated type */ |
| 126 | 137 | struct Sym *next; /* next related symbol */ |
| 127 | 138 | struct Sym *prev; /* prev symbol in stack */ |
| 128 | 139 | struct Sym *hash_next; /* next symbol in hash table */ |
| @@ -273,20 +284,22 @@ int const_wanted; /* true if constant wanted */ · 144 unmodified lines |
| 273 | 284 | int nocode_wanted; /* true if no code generation wanted for an expression */ |
| 274 | 285 | int global_expr; /* true if compound literals must be allocated |
| 275 | 286 | 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; |
| 278 | 290 | int last_line_num, last_ind, func_ind; /* debug last line number and pc */ |
| 279 | 291 | int tok_ident; |
| 280 | 292 | TokenSym **table_ident; |
| 281 | 293 | TokenSym *hash_ident[TOK_HASH_SIZE]; |
| 282 | 294 | char token_buf[STRING_MAX_SIZE + 1]; |
| 283 | 295 | char *funcname; |
| 284 | | −SymStack define_stack, global_stack, local_stack, label_stack; |
| 296 | +SymStack global_stack, local_stack, label_stack; |
| 297 | +Sym *define_stack; |
| 285 | 298 | |
| 286 | 299 | SValue vstack[VSTACK_SIZE], *vtop; |
| 287 | 300 | 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; |
| 290 | 303 | |
| 291 | 304 | /* compile with debug symbol (and use them if error during execution) */ |
| 292 | 305 | int do_debug = 0; |
| @@ -511,6 +524,7 @@ static const char *tcc_keywords = · 218 unmodified lines |
| 511 | 524 | |
| 512 | 525 | #ifdef WIN32 |
| 513 | 526 | #define snprintf _snprintf |
| 527 | +#define vsnprintf _vsnprintf |
| 514 | 528 | #endif |
| 515 | 529 | |
| 516 | 530 | #if defined(WIN32) || defined(TCC_UCLIBC) |
| @@ -535,16 +549,16 @@ static char *pstrcat(char *buf, int buf_size, const char *s); · 18 unmodified lines |
| 535 | 549 | static void sum(int l); |
| 536 | 550 | static void next(void); |
| 537 | 551 | 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); |
| 541 | 555 | static int expr_const(void); |
| 542 | 556 | static void expr_eq(void); |
| 543 | 557 | static void gexpr(void); |
| 544 | 558 | 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, |
| 546 | 560 | 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, |
| 548 | 562 | int has_init, int v, int scope); |
| 549 | 563 | int gv(int rc); |
| 550 | 564 | void gv2(int rc1, int rc2); |
| @@ -561,30 +575,29 @@ static void macro_subst(TokenString *tok_str, · 10 unmodified lines |
| 561 | 575 | int save_reg_forced(int r); |
| 562 | 576 | void gen_op(int op); |
| 563 | 577 | void force_charshort_cast(int t); |
| 564 | | −void gen_cast(int t); |
| 578 | +static void gen_cast(CType *type); |
| 565 | 579 | void vstore(void); |
| 566 | 580 | 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); |
| 568 | 582 | |
| 569 | 583 | /* 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); |
| 573 | 587 | 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); |
| 577 | 591 | |
| 578 | 592 | void error(const char *fmt, ...); |
| 579 | | −void rt_error(struct ucontext *uc, const char *fmt, ...); |
| 580 | 593 | void vpushi(int v); |
| 581 | | −void vset(int t, int r, int v); |
| 594 | +void vset(CType *type, int r, int v); |
| 582 | 595 | void type_to_str(char *buf, int buf_size, |
| 583 | | − int t, const char *varstr); |
| 596 | + CType *type, const char *varstr); |
| 584 | 597 | 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, |
| 586 | 599 | 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); |
| 588 | 601 | |
| 589 | 602 | /* section generation */ |
| 590 | 603 | static void section_realloc(Section *sec, unsigned long new_size); |
| @@ -632,6 +645,7 @@ static inline int is_float(int t) · 41 unmodified lines |
| 632 | 645 | #define RTLD_LAZY 0x001 |
| 633 | 646 | #define RTLD_NOW 0x002 |
| 634 | 647 | #define RTLD_GLOBAL 0x100 |
| 648 | +#define RTLD_DEFAULT NULL |
| 635 | 649 | |
| 636 | 650 | /* dummy function for profiling */ |
| 637 | 651 | void *dlopen(const char *filename, int flag) |
| @@ -895,18 +909,17 @@ static void put_extern_sym(Sym *sym, Section *section, · 257 unmodified lines |
| 895 | 909 | int sym_type, sym_bind, sh_num, info; |
| 896 | 910 | Elf32_Sym *esym; |
| 897 | 911 | const char *name; |
| 898 | | − char buf[32]; |
| 899 | 912 | |
| 900 | 913 | if (section) |
| 901 | 914 | sh_num = section->sh_num; |
| 902 | 915 | else |
| 903 | 916 | sh_num = SHN_UNDEF; |
| 904 | 917 | if (!sym->c) { |
| 905 | | − if ((sym->t & VT_BTYPE) == VT_FUNC) |
| 918 | + if ((sym->type.t & VT_BTYPE) == VT_FUNC) |
| 906 | 919 | sym_type = STT_FUNC; |
| 907 | 920 | else |
| 908 | 921 | sym_type = STT_OBJECT; |
| 909 | | − if (sym->t & VT_STATIC) |
| 922 | + if (sym->type.t & VT_STATIC) |
| 910 | 923 | sym_bind = STB_LOCAL; |
| 911 | 924 | else |
| 912 | 925 | sym_bind = STB_GLOBAL; |
| @@ -914,6 +927,8 @@ static void put_extern_sym(Sym *sym, Section *section, · 1 unmodified line |
| 914 | 927 | name = get_tok_str(sym->v, NULL); |
| 915 | 928 | #ifdef CONFIG_TCC_BCHECK |
| 916 | 929 | if (do_bounds_check) { |
| 930 | + char buf[32]; |
| 931 | + |
| 917 | 932 | /* XXX: avoid doing that for statics ? */ |
| 918 | 933 | /* if bound checking is activated, we change some function |
| 919 | 934 | names by adding the "__bound" prefix */ |
| @@ -1131,6 +1146,7 @@ TokenSym *tok_alloc(const char *str, int len) · 211 unmodified lines |
| 1131 | 1146 | ts = tcc_malloc(sizeof(TokenSym) + len); |
| 1132 | 1147 | table_ident[i] = ts; |
| 1133 | 1148 | ts->tok = tok_ident++; |
| 1149 | + ts->sym_define = NULL; |
| 1134 | 1150 | ts->len = len; |
| 1135 | 1151 | ts->hash_next = NULL; |
| 1136 | 1152 | memcpy(ts->str, str, len + 1); |
| @@ -1331,7 +1347,7 @@ Sym *sym_push2(Sym **ps, int v, int t, int c) · 194 unmodified lines |
| 1331 | 1347 | Sym *s; |
| 1332 | 1348 | s = tcc_malloc(sizeof(Sym)); |
| 1333 | 1349 | s->v = v; |
| 1334 | | − s->t = t; |
| 1350 | + s->type.t = t; |
| 1335 | 1351 | s->c = c; |
| 1336 | 1352 | s->next = NULL; |
| 1337 | 1353 | /* add in stack */ |
| @@ -1393,13 +1409,14 @@ Sym *sym_find(int v) · 55 unmodified lines |
| 1393 | 1409 | } |
| 1394 | 1410 | |
| 1395 | 1411 | /* 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) |
| 1397 | 1413 | { |
| 1398 | 1414 | Sym *s; |
| 1399 | 1415 | if (local_stack.top) |
| 1400 | | − s = sym_push1(&local_stack, v, t, c); |
| 1416 | + s = sym_push1(&local_stack, v, type->t, c); |
| 1401 | 1417 | 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; |
| 1403 | 1420 | s->r = r; |
| 1404 | 1421 | return s; |
| 1405 | 1422 | } |
| @@ -1422,21 +1439,6 @@ void sym_pop(SymStack *st, Sym *b) · 16 unmodified lines |
| 1422 | 1439 | st->top = b; |
| 1423 | 1440 | } |
| 1424 | 1441 | |
| 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 | | − |
| 1440 | 1442 | /* I/O layer */ |
| 1441 | 1443 | |
| 1442 | 1444 | BufferedFile *tcc_open(TCCState *s1, const char *filename) |
| @@ -1794,6 +1796,55 @@ static int tok_get(int **tok_str, CValue *cv) · 351 unmodified lines |
| 1794 | 1796 | return t; |
| 1795 | 1797 | } |
| 1796 | 1798 | |
| 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 | + |
| 1797 | 1848 | /* eval an expression for #if/#elif */ |
| 1798 | 1849 | int expr_preprocess(void) |
| 1799 | 1850 | { |
| @@ -1808,7 +1859,7 @@ int expr_preprocess(void) · 8 unmodified lines |
| 1808 | 1859 | t = tok; |
| 1809 | 1860 | if (t == '(') |
| 1810 | 1861 | next_nomacro(); |
| 1811 | | − c = sym_find1(&define_stack, tok) != 0; |
| 1862 | + c = define_find(tok) != 0; |
| 1812 | 1863 | if (t == '(') |
| 1813 | 1864 | next_nomacro(); |
| 1814 | 1865 | tok = TOK_CINT; |
| @@ -1855,6 +1906,8 @@ void parse_define(void) · 40 unmodified lines |
| 1855 | 1906 | TokenString str; |
| 1856 | 1907 | |
| 1857 | 1908 | v = tok; |
| 1909 | + if (v < TOK_IDENT) |
| 1910 | + error("invalid macro name '%s'", get_tok_str(tok, &tokc)); |
| 1858 | 1911 | /* XXX: should check if same macro (ANSI) */ |
| 1859 | 1912 | first = NULL; |
| 1860 | 1913 | t = MACRO_OBJ; |
| @@ -1876,7 +1929,7 @@ void parse_define(void) · 15 unmodified lines |
| 1876 | 1929 | } |
| 1877 | 1930 | if (varg < TOK_IDENT) |
| 1878 | 1931 | 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); |
| 1880 | 1933 | *ps = s; |
| 1881 | 1934 | ps = &s->next; |
| 1882 | 1935 | if (tok != ',') |
| @@ -1897,8 +1950,7 @@ void parse_define(void) · 14 unmodified lines |
| 1897 | 1950 | printf("define %s %d: ", get_tok_str(v, NULL), t); |
| 1898 | 1951 | tok_print(str.str); |
| 1899 | 1952 | #endif |
| 1900 | | − s = sym_push1(&define_stack, v, t, (int)str.str); |
| 1901 | | − s->next = first; |
| 1953 | + define_push(v, t, str.str, first); |
| 1902 | 1954 | } |
| 1903 | 1955 | |
| 1904 | 1956 | /* XXX: use a token or a hash table to accelerate matching ? */ |
| @@ -1965,10 +2017,10 @@ void preprocess(void) · 60 unmodified lines |
| 1965 | 2017 | parse_define(); |
| 1966 | 2018 | } else if (tok == TOK_UNDEF) { |
| 1967 | 2019 | next_nomacro(); |
| 1968 | | − s = sym_find1(&define_stack, tok); |
| 2020 | + s = define_find(tok); |
| 1969 | 2021 | /* undefine symbol by putting an invalid name */ |
| 1970 | 2022 | if (s) |
| 1971 | | − sym_undef(&define_stack, s); |
| 2023 | + define_undef(s); |
| 1972 | 2024 | } else if (tok == TOK_INCLUDE) { |
| 1973 | 2025 | skip_spaces(); |
| 1974 | 2026 | if (ch == '<') { |
| @@ -2022,7 +2074,7 @@ void preprocess(void) · 47 unmodified lines |
| 2022 | 2074 | |
| 2023 | 2075 | ch = '\n'; |
| 2024 | 2076 | 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)) { |
| 2026 | 2078 | /* no need to parse the include because the 'ifndef macro' |
| 2027 | 2079 | is defined */ |
| 2028 | 2080 | #ifdef INC_DEBUG |
| @@ -2105,7 +2157,7 @@ void preprocess(void) · 76 unmodified lines |
| 2105 | 2157 | } |
| 2106 | 2158 | state = INCLUDE_STATE_NONE; |
| 2107 | 2159 | } |
| 2108 | | − c = (sym_find1(&define_stack, tok) != 0) ^ c; |
| 2160 | + c = (define_find(tok) != 0) ^ c; |
| 2109 | 2161 | do_if: |
| 2110 | 2162 | if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE) |
| 2111 | 2163 | error("memory full"); |
| @@ -2772,7 +2824,7 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 660 unmodified lines |
| 2772 | 2824 | /* XXX: test of the ',' is not 100% |
| 2773 | 2825 | reliable. should fix it to avoid security |
| 2774 | 2826 | problems */ |
| 2775 | | − if (gnu_ext && s->t && |
| 2827 | + if (gnu_ext && s->type.t && |
| 2776 | 2828 | last_tok == TOK_TWOSHARPS && |
| 2777 | 2829 | str.len >= 2 && str.str[str.len - 2] == ',') { |
| 2778 | 2830 | if (*st == 0) { |
| @@ -2959,7 +3011,7 @@ static int macro_subst_tok(TokenString *tok_str, · 180 unmodified lines |
| 2959 | 3011 | } else { |
| 2960 | 3012 | mstr = (int *)s->c; |
| 2961 | 3013 | mstr_allocated = 0; |
| 2962 | | − if (s->t == MACRO_FUNC) { |
| 3014 | + if (s->type.t == MACRO_FUNC) { |
| 2963 | 3015 | /* NOTE: we do not use next_nomacro to avoid eating the |
| 2964 | 3016 | next token. XXX: find better solution */ |
| 2965 | 3017 | if (macro_ptr) { |
| @@ -2990,7 +3042,7 @@ static int macro_subst_tok(TokenString *tok_str, · 24 unmodified lines |
| 2990 | 3042 | /* NOTE: non zero sa->t indicates VA_ARGS */ |
| 2991 | 3043 | while ((parlevel > 0 || |
| 2992 | 3044 | (tok != ')' && |
| 2993 | | − (tok != ',' || sa->t))) && |
| 3045 | + (tok != ',' || sa->type.t))) && |
| 2994 | 3046 | tok != -1) { |
| 2995 | 3047 | if (tok == '(') |
| 2996 | 3048 | parlevel++; |
| @@ -3000,12 +3052,12 @@ static int macro_subst_tok(TokenString *tok_str, · 3 unmodified lines |
| 3000 | 3052 | next_nomacro(); |
| 3001 | 3053 | } |
| 3002 | 3054 | 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); |
| 3004 | 3056 | sa = sa->next; |
| 3005 | 3057 | if (tok == ')') { |
| 3006 | 3058 | /* special case for gcc var args: add an empty |
| 3007 | 3059 | var arg argument if it is omitted */ |
| 3008 | | − if (sa && sa->t && gnu_ext) |
| 3060 | + if (sa && sa->type.t && gnu_ext) |
| 3009 | 3061 | continue; |
| 3010 | 3062 | else |
| 3011 | 3063 | break; |
| @@ -3063,7 +3115,7 @@ static void macro_subst(TokenString *tok_str, · 51 unmodified lines |
| 3063 | 3115 | next_nomacro(); |
| 3064 | 3116 | if (tok == 0) |
| 3065 | 3117 | break; |
| 3066 | | − s = sym_find1(&define_stack, tok); |
| 3118 | + s = define_find(tok); |
| 3067 | 3119 | if (s != NULL) { |
| 3068 | 3120 | if (macro_subst_tok(tok_str, nested_list, s) != 0) |
| 3069 | 3121 | goto no_subst; |
| @@ -3094,7 +3146,7 @@ static void next(void) · 24 unmodified lines |
| 3094 | 3146 | /* if not reading from macro substituted string, then try |
| 3095 | 3147 | to substitute macros */ |
| 3096 | 3148 | if (tok >= TOK_IDENT) { |
| 3097 | | − s = sym_find1(&define_stack, tok); |
| 3149 | + s = define_find(tok); |
| 3098 | 3150 | if (s) { |
| 3099 | 3151 | /* we have a macro: we try to substitute */ |
| 3100 | 3152 | tok_str_new(&str); |
| @@ -3135,7 +3187,7 @@ void swap(int *p, int *q) · 34 unmodified lines |
| 3135 | 3187 | *q = t; |
| 3136 | 3188 | } |
| 3137 | 3189 | |
| 3138 | | −void vsetc(int t, int r, CValue *vc) |
| 3190 | +void vsetc(CType *type, int r, CValue *vc) |
| 3139 | 3191 | { |
| 3140 | 3192 | int v; |
| 3141 | 3193 | |
| @@ -3150,7 +3202,7 @@ void vsetc(int t, int r, CValue *vc) · 8 unmodified lines |
| 3150 | 3202 | gv(RC_INT); |
| 3151 | 3203 | } |
| 3152 | 3204 | vtop++; |
| 3153 | | − vtop->t = t; |
| 3205 | + vtop->type = *type; |
| 3154 | 3206 | vtop->r = r; |
| 3155 | 3207 | vtop->r2 = VT_CONST; |
| 3156 | 3208 | vtop->c = *vc; |
| @@ -3161,35 +3213,36 @@ void vpushi(int v) · 4 unmodified lines |
| 3161 | 3213 | { |
| 3162 | 3214 | CValue cval; |
| 3163 | 3215 | cval.i = v; |
| 3164 | | − vsetc(VT_INT, VT_CONST, &cval); |
| 3216 | + vsetc(&int_type, VT_CONST, &cval); |
| 3165 | 3217 | } |
| 3166 | 3218 | |
| 3167 | 3219 | /* 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, |
| 3169 | 3221 | unsigned long offset, unsigned long size) |
| 3170 | 3222 | { |
| 3171 | 3223 | int v; |
| 3172 | 3224 | Sym *sym; |
| 3173 | 3225 | |
| 3174 | 3226 | 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; |
| 3176 | 3229 | sym->r = VT_CONST | VT_SYM; |
| 3177 | 3230 | put_extern_sym(sym, sec, offset, size); |
| 3178 | 3231 | return sym; |
| 3179 | 3232 | } |
| 3180 | 3233 | |
| 3181 | 3234 | /* 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) |
| 3183 | 3236 | { |
| 3184 | 3237 | CValue cval; |
| 3185 | 3238 | |
| 3186 | 3239 | 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); |
| 3189 | 3242 | } |
| 3190 | 3243 | |
| 3191 | 3244 | /* 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) |
| 3193 | 3246 | { |
| 3194 | 3247 | Sym *s; |
| 3195 | 3248 | |
| @@ -3197,43 +3250,52 @@ static Sym *external_global_sym(int v, int u, int r) · 1 unmodified line |
| 3197 | 3250 | if (!s) { |
| 3198 | 3251 | /* push forward reference */ |
| 3199 | 3252 | s = sym_push1(&global_stack, |
| 3200 | | − v, u | VT_EXTERN, 0); |
| 3253 | + v, type->t | VT_EXTERN, 0); |
| 3254 | + s->type.ref = type->ref; |
| 3201 | 3255 | s->r = r | VT_CONST | VT_SYM; |
| 3202 | 3256 | } |
| 3203 | 3257 | return s; |
| 3204 | 3258 | } |
| 3205 | 3259 | |
| 3206 | 3260 | /* 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) |
| 3208 | 3262 | { |
| 3209 | 3263 | Sym *s; |
| 3210 | 3264 | |
| 3211 | 3265 | s = sym_find(v); |
| 3212 | 3266 | if (!s) { |
| 3213 | 3267 | /* 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; |
| 3215 | 3270 | } |
| 3216 | 3271 | return s; |
| 3217 | 3272 | } |
| 3218 | 3273 | |
| 3219 | 3274 | /* 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) |
| 3221 | 3276 | { |
| 3222 | 3277 | Sym *sym; |
| 3223 | 3278 | CValue cval; |
| 3224 | 3279 | |
| 3225 | | − sym = external_global_sym(v, t, 0); |
| 3280 | + sym = external_global_sym(v, type, 0); |
| 3226 | 3281 | cval.ul = 0; |
| 3227 | | − vsetc(t, VT_CONST | VT_SYM, &cval); |
| 3282 | + vsetc(type, VT_CONST | VT_SYM, &cval); |
| 3228 | 3283 | vtop->sym = sym; |
| 3229 | 3284 | } |
| 3230 | 3285 | |
| 3231 | | −void vset(int t, int r, int v) |
| 3286 | +void vset(CType *type, int r, int v) |
| 3232 | 3287 | { |
| 3233 | 3288 | CValue cval; |
| 3234 | 3289 | |
| 3235 | 3290 | 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); |
| 3237 | 3299 | } |
| 3238 | 3300 | |
| 3239 | 3301 | void vswap(void) |
| @@ -3261,8 +3323,9 @@ void vdup(void) · 21 unmodified lines |
| 3261 | 3323 | /* save r to the memory stack, and mark it as being free */ |
| 3262 | 3324 | void save_reg(int r) |
| 3263 | 3325 | { |
| 3264 | | − int l, saved, t, size, align; |
| 3326 | + int l, saved, size, align; |
| 3265 | 3327 | SValue *p, sv; |
| 3328 | + CType *type; |
| 3266 | 3329 | |
| 3267 | 3330 | /* modify all stack values */ |
| 3268 | 3331 | saved = 0; |
| @@ -3275,13 +3338,13 @@ void save_reg(int r) · 6 unmodified lines |
| 3275 | 3338 | /* NOTE: must reload 'r' because r might be equal to r2 */ |
| 3276 | 3339 | r = p->r & VT_VALMASK; |
| 3277 | 3340 | /* store register in the stack */ |
| 3278 | | − t = p->t; |
| 3341 | + type = &p->type; |
| 3279 | 3342 | 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); |
| 3283 | 3346 | loc = (loc - size) & -align; |
| 3284 | | − sv.t = t; |
| 3347 | + sv.type.t = type->t; |
| 3285 | 3348 | sv.r = VT_LOCAL | VT_LVAL; |
| 3286 | 3349 | sv.c.ul = loc; |
| 3287 | 3350 | store(r, &sv); |
| @@ -3292,7 +3355,7 @@ void save_reg(int r) · 4 unmodified lines |
| 3292 | 3355 | } |
| 3293 | 3356 | #endif |
| 3294 | 3357 | /* special long long case */ |
| 3295 | | − if ((t & VT_BTYPE) == VT_LLONG) { |
| 3358 | + if ((type->t & VT_BTYPE) == VT_LLONG) { |
| 3296 | 3359 | sv.c.ul += 4; |
| 3297 | 3360 | store(p->r2, &sv); |
| 3298 | 3361 | } |
| @@ -3306,7 +3369,7 @@ void save_reg(int r) · 7 unmodified lines |
| 3306 | 3369 | p->c.ul */ |
| 3307 | 3370 | p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL; |
| 3308 | 3371 | } else { |
| 3309 | | − p->r = lvalue_type(p->t) | VT_LOCAL; |
| 3372 | + p->r = lvalue_type(p->type.t) | VT_LOCAL; |
| 3310 | 3373 | } |
| 3311 | 3374 | p->r2 = VT_CONST; |
| 3312 | 3375 | p->c.ul = l; |
| @@ -3374,7 +3437,7 @@ void move_reg(int r, int s) · 61 unmodified lines |
| 3374 | 3437 | |
| 3375 | 3438 | if (r != s) { |
| 3376 | 3439 | save_reg(r); |
| 3377 | | − sv.t = VT_INT; |
| 3440 | + sv.type.t = VT_INT; |
| 3378 | 3441 | sv.r = s; |
| 3379 | 3442 | sv.c.ul = 0; |
| 3380 | 3443 | load(r, &sv); |
| @@ -3394,7 +3457,8 @@ void gaddrof(void) · 13 unmodified lines |
| 3394 | 3457 | /* generate lvalue bound code */ |
| 3395 | 3458 | void gbound(void) |
| 3396 | 3459 | { |
| 3397 | | − int lval_type, t1; |
| 3460 | + int lval_type; |
| 3461 | + CType type1; |
| 3398 | 3462 | |
| 3399 | 3463 | vtop->r &= ~VT_MUSTBOUND; |
| 3400 | 3464 | /* if lvalue, then use checking code before dereferencing */ |
| @@ -3403,13 +3467,13 @@ void gbound(void) · 2 unmodified lines |
| 3403 | 3467 | if (!(vtop->r & VT_BOUNDED)) { |
| 3404 | 3468 | lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL); |
| 3405 | 3469 | /* 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; |
| 3408 | 3472 | gaddrof(); |
| 3409 | 3473 | vpushi(0); |
| 3410 | 3474 | gen_bounded_ptr_add(); |
| 3411 | 3475 | vtop->r |= lval_type; |
| 3412 | | − vtop->t = t1; |
| 3476 | + vtop->type = type1; |
| 3413 | 3477 | } |
| 3414 | 3478 | /* then check for dereferencing */ |
| 3415 | 3479 | gen_bounded_ptr_deref(); |
| @@ -3426,11 +3490,11 @@ int gv(int rc) · 10 unmodified lines |
| 3426 | 3490 | unsigned long long ll; |
| 3427 | 3491 | |
| 3428 | 3492 | /* 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; |
| 3432 | 3496 | /* 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)); |
| 3434 | 3498 | /* generate shifts */ |
| 3435 | 3499 | vpushi(32 - (bit_pos + bit_size)); |
| 3436 | 3500 | gen_op(TOK_SHL); |
| @@ -3439,7 +3503,7 @@ int gv(int rc) · 2 unmodified lines |
| 3439 | 3503 | gen_op(TOK_SAR); |
| 3440 | 3504 | r = gv(rc); |
| 3441 | 3505 | } else { |
| 3442 | | − if (is_float(vtop->t) && |
| 3506 | + if (is_float(vtop->type.t) && |
| 3443 | 3507 | (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { |
| 3444 | 3508 | Sym *sym; |
| 3445 | 3509 | int *ptr; |
| @@ -3448,7 +3512,7 @@ int gv(int rc) · 2 unmodified lines |
| 3448 | 3512 | /* XXX: unify with initializers handling ? */ |
| 3449 | 3513 | /* CPUs usually cannot use float constants, so we store them |
| 3450 | 3514 | generically in data segment */ |
| 3451 | | − size = type_size(vtop->t, &align); |
| 3515 | + size = type_size(&vtop->type, &align); |
| 3452 | 3516 | offset = (data_section->data_offset + align - 1) & -align; |
| 3453 | 3517 | data_section->data_offset = offset; |
| 3454 | 3518 | /* XXX: not portable yet */ |
| @@ -3456,7 +3520,7 @@ int gv(int rc) · 1 unmodified line |
| 3456 | 3520 | size = size >> 2; |
| 3457 | 3521 | for(i=0;i<size;i++) |
| 3458 | 3522 | 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); |
| 3460 | 3524 | vtop->r |= VT_LVAL | VT_SYM; |
| 3461 | 3525 | vtop->sym = sym; |
| 3462 | 3526 | vtop->c.ul = 0; |
| @@ -3474,10 +3538,10 @@ int gv(int rc) · 11 unmodified lines |
| 3474 | 3538 | if (r >= VT_CONST || |
| 3475 | 3539 | (vtop->r & VT_LVAL) || |
| 3476 | 3540 | !(reg_classes[r] & rc) || |
| 3477 | | − ((vtop->t & VT_BTYPE) == VT_LLONG && |
| 3541 | + ((vtop->type.t & VT_BTYPE) == VT_LLONG && |
| 3478 | 3542 | !(reg_classes[vtop->r2] & rc))) { |
| 3479 | 3543 | r = get_reg(rc); |
| 3480 | | − if ((vtop->t & VT_BTYPE) == VT_LLONG) { |
| 3544 | + if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { |
| 3481 | 3545 | /* two register type load : expand to two words |
| 3482 | 3546 | temporarily */ |
| 3483 | 3547 | if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { |
| @@ -3494,7 +3558,7 @@ int gv(int rc) · 10 unmodified lines |
| 3494 | 3558 | vdup(); |
| 3495 | 3559 | vtop[-1].r = r; /* save register value */ |
| 3496 | 3560 | /* increment pointer to get second word */ |
| 3497 | | − vtop->t = VT_INT; |
| 3561 | + vtop->type.t = VT_INT; |
| 3498 | 3562 | gaddrof(); |
| 3499 | 3563 | vpushi(4); |
| 3500 | 3564 | gen_op('+'); |
| @@ -3515,11 +3579,11 @@ int gv(int rc) · 14 unmodified lines |
| 3515 | 3579 | vpop(); |
| 3516 | 3580 | /* write second register */ |
| 3517 | 3581 | 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)) { |
| 3519 | 3583 | int t1, t; |
| 3520 | 3584 | /* lvalue of scalar type : need to use lvalue type |
| 3521 | 3585 | because of possible cast */ |
| 3522 | | − t = vtop->t; |
| 3586 | + t = vtop->type.t; |
| 3523 | 3587 | t1 = t; |
| 3524 | 3588 | /* compute memory access type */ |
| 3525 | 3589 | if (vtop->r & VT_LVAL_BYTE) |
| @@ -3528,10 +3592,10 @@ int gv(int rc) · 2 unmodified lines |
| 3528 | 3592 | t = VT_SHORT; |
| 3529 | 3593 | if (vtop->r & VT_LVAL_UNSIGNED) |
| 3530 | 3594 | t |= VT_UNSIGNED; |
| 3531 | | − vtop->t = t; |
| 3595 | + vtop->type.t = t; |
| 3532 | 3596 | load(r, vtop); |
| 3533 | 3597 | /* restore wanted type */ |
| 3534 | | − vtop->t = t1; |
| 3598 | + vtop->type.t = t1; |
| 3535 | 3599 | } else { |
| 3536 | 3600 | /* one register type load */ |
| 3537 | 3601 | load(r, vtop); |
| @@ -3579,14 +3643,14 @@ void lexpand(void) · 41 unmodified lines |
| 3579 | 3643 | { |
| 3580 | 3644 | int u; |
| 3581 | 3645 | |
| 3582 | | − u = vtop->t & VT_UNSIGNED; |
| 3646 | + u = vtop->type.t & VT_UNSIGNED; |
| 3583 | 3647 | gv(RC_INT); |
| 3584 | 3648 | vdup(); |
| 3585 | 3649 | vtop[0].r = vtop[-1].r2; |
| 3586 | 3650 | vtop[0].r2 = VT_CONST; |
| 3587 | 3651 | 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; |
| 3590 | 3654 | } |
| 3591 | 3655 | |
| 3592 | 3656 | /* build a long long from two ints */ |
| @@ -3594,7 +3658,7 @@ void lbuild(int t) · 1 unmodified line |
| 3594 | 3658 | { |
| 3595 | 3659 | gv2(RC_INT, RC_INT); |
| 3596 | 3660 | vtop[-1].r2 = vtop[0].r; |
| 3597 | | − vtop[-1].t = t; |
| 3661 | + vtop[-1].type.t = t; |
| 3598 | 3662 | vpop(); |
| 3599 | 3663 | } |
| 3600 | 3664 | |
| @@ -3635,7 +3699,7 @@ void gv_dup(void) · 34 unmodified lines |
| 3635 | 3699 | int rc, t, r, r1; |
| 3636 | 3700 | SValue sv; |
| 3637 | 3701 | |
| 3638 | | − t = vtop->t; |
| 3702 | + t = vtop->type.t; |
| 3639 | 3703 | if ((t & VT_BTYPE) == VT_LLONG) { |
| 3640 | 3704 | lexpand(); |
| 3641 | 3705 | gv_dup(); |
| @@ -3653,10 +3717,10 @@ void gv_dup(void) · 11 unmodified lines |
| 3653 | 3717 | } else { |
| 3654 | 3718 | /* duplicate value */ |
| 3655 | 3719 | rc = RC_INT; |
| 3656 | | − sv.t = VT_INT; |
| 3720 | + sv.type.t = VT_INT; |
| 3657 | 3721 | if (is_float(t)) { |
| 3658 | 3722 | rc = RC_FLOAT; |
| 3659 | | − sv.t = t; |
| 3723 | + sv.type.t = t; |
| 3660 | 3724 | } |
| 3661 | 3725 | r = gv(rc); |
| 3662 | 3726 | r1 = get_reg(rc); |
| @@ -3695,7 +3759,7 @@ void gen_opl(int op) · 32 unmodified lines |
| 3695 | 3759 | gfunc_start(&gf, FUNC_CDECL); |
| 3696 | 3760 | gfunc_param(&gf); |
| 3697 | 3761 | gfunc_param(&gf); |
| 3698 | | − vpush_global_sym(func_old_type, func); |
| 3762 | + vpush_global_sym(&func_old_type, func); |
| 3699 | 3763 | gfunc_call(&gf); |
| 3700 | 3764 | vpushi(0); |
| 3701 | 3765 | vtop->r = REG_IRET; |
| @@ -3707,7 +3771,7 @@ void gen_opl(int op) · 5 unmodified lines |
| 3707 | 3771 | case '*': |
| 3708 | 3772 | case '+': |
| 3709 | 3773 | case '-': |
| 3710 | | − t = vtop->t; |
| 3774 | + t = vtop->type.t; |
| 3711 | 3775 | vswap(); |
| 3712 | 3776 | lexpand(); |
| 3713 | 3777 | vrotb(3); |
| @@ -3768,7 +3832,7 @@ void gen_opl(int op) · 54 unmodified lines |
| 3768 | 3832 | case TOK_SHR: |
| 3769 | 3833 | case TOK_SHL: |
| 3770 | 3834 | if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { |
| 3771 | | − t = vtop[-1].t; |
| 3835 | + t = vtop[-1].type.t; |
| 3772 | 3836 | vswap(); |
| 3773 | 3837 | lexpand(); |
| 3774 | 3838 | vrotb(3); |
| @@ -3836,7 +3900,7 @@ void gen_opl(int op) · 61 unmodified lines |
| 3836 | 3900 | break; |
| 3837 | 3901 | default: |
| 3838 | 3902 | /* compare operations */ |
| 3839 | | − t = vtop->t; |
| 3903 | + t = vtop->type.t; |
| 3840 | 3904 | vswap(); |
| 3841 | 3905 | lexpand(); |
| 3842 | 3906 | vrotb(3); |
| @@ -3881,7 +3945,7 @@ void gen_opl(int op) · 38 unmodified lines |
| 3881 | 3945 | gen_op(op); |
| 3882 | 3946 | a = gtst(1, a); |
| 3883 | 3947 | gsym(b); |
| 3884 | | − vset(VT_INT, VT_JMPI, a); |
| 3948 | + vseti(VT_JMPI, a); |
| 3885 | 3949 | break; |
| 3886 | 3950 | } |
| 3887 | 3951 | } |
| @@ -4015,10 +4079,10 @@ void gen_opif(int op) · 127 unmodified lines |
| 4015 | 4079 | c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; |
| 4016 | 4080 | c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST; |
| 4017 | 4081 | if (c1 && c2) { |
| 4018 | | − if (v1->t == VT_FLOAT) { |
| 4082 | + if (v1->type.t == VT_FLOAT) { |
| 4019 | 4083 | f1 = v1->c.f; |
| 4020 | 4084 | f2 = v2->c.f; |
| 4021 | | − } else if (v1->t == VT_DOUBLE) { |
| 4085 | + } else if (v1->type.t == VT_DOUBLE) { |
| 4022 | 4086 | f1 = v1->c.d; |
| 4023 | 4087 | f2 = v2->c.d; |
| 4024 | 4088 | } else { |
| @@ -4048,9 +4112,9 @@ void gen_opif(int op) · 23 unmodified lines |
| 4048 | 4112 | goto general_case; |
| 4049 | 4113 | } |
| 4050 | 4114 | /* XXX: overflow test ? */ |
| 4051 | | − if (v1->t == VT_FLOAT) { |
| 4115 | + if (v1->type.t == VT_FLOAT) { |
| 4052 | 4116 | v1->c.f = f1; |
| 4053 | | − } else if (v1->t == VT_DOUBLE) { |
| 4117 | + } else if (v1->type.t == VT_DOUBLE) { |
| 4054 | 4118 | v1->c.d = f1; |
| 4055 | 4119 | } else { |
| 4056 | 4120 | v1->c.ld = f1; |
| @@ -4066,9 +4130,10 @@ void gen_opif(int op) · 9 unmodified lines |
| 4066 | 4130 | } |
| 4067 | 4131 | } |
| 4068 | 4132 | |
| 4069 | | −int pointed_size(int t) |
| 4133 | +static int pointed_size(CType *type) |
| 4070 | 4134 | { |
| 4071 | | − return type_size(pointed_type(t), &t); |
| 4135 | + int align; |
| 4136 | + return type_size(pointed_type(type), &align); |
| 4072 | 4137 | } |
| 4073 | 4138 | |
| 4074 | 4139 | #if 0 |
| @@ -4090,9 +4155,10 @@ void check_pointer_types(SValue *p1, SValue *p2) · 15 unmodified lines |
| 4090 | 4155 | void gen_op(int op) |
| 4091 | 4156 | { |
| 4092 | 4157 | int u, t1, t2, bt1, bt2, t; |
| 4158 | + CType type1; |
| 4093 | 4159 | |
| 4094 | | − t1 = vtop[-1].t; |
| 4095 | | − t2 = vtop[0].t; |
| 4160 | + t1 = vtop[-1].type.t; |
| 4161 | + t2 = vtop[0].type.t; |
| 4096 | 4162 | bt1 = t1 & VT_BTYPE; |
| 4097 | 4163 | bt2 = t2 & VT_BTYPE; |
| 4098 | 4164 | |
| @@ -4106,16 +4172,15 @@ void gen_op(int op) · 7 unmodified lines |
| 4106 | 4172 | goto std_op; |
| 4107 | 4173 | } |
| 4108 | 4174 | /* 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) { |
| 4111 | 4176 | if (op != '-') |
| 4112 | 4177 | error("cannot use pointers here"); |
| 4113 | 4178 | // check_pointer_types(vtop - 1, vtop); |
| 4114 | 4179 | /* XXX: check that types are compatible */ |
| 4115 | | − u = pointed_size(t1); |
| 4180 | + u = pointed_size(&vtop[-1].type); |
| 4116 | 4181 | gen_opic(op); |
| 4117 | 4182 | /* set to integer type */ |
| 4118 | | − vtop->t = VT_INT; |
| 4183 | + vtop->type.t = VT_INT; |
| 4119 | 4184 | vpushi(u); |
| 4120 | 4185 | gen_op(TOK_PDIV); |
| 4121 | 4186 | } else { |
| @@ -4123,12 +4188,13 @@ void gen_op(int op) · 1 unmodified line |
| 4123 | 4188 | if (op != '-' && op != '+') |
| 4124 | 4189 | error("cannot use pointers here"); |
| 4125 | 4190 | /* Put pointer as first operand */ |
| 4126 | | − if ((t2 & VT_BTYPE) == VT_PTR) { |
| 4191 | + if (bt2 == VT_PTR) { |
| 4127 | 4192 | vswap(); |
| 4128 | 4193 | swap(&t1, &t2); |
| 4129 | 4194 | } |
| 4195 | + type1 = vtop[-1].type; |
| 4130 | 4196 | /* XXX: cast to int ? (long long case) */ |
| 4131 | | − vpushi(pointed_size(vtop[-1].t)); |
| 4197 | + vpushi(pointed_size(&vtop[-1].type)); |
| 4132 | 4198 | gen_op('*'); |
| 4133 | 4199 | #ifdef CONFIG_TCC_BCHECK |
| 4134 | 4200 | /* if evaluating constant expression, no code should be |
| @@ -4148,7 +4214,7 @@ void gen_op(int op) · 13 unmodified lines |
| 4148 | 4214 | gen_opic(op); |
| 4149 | 4215 | } |
| 4150 | 4216 | /* put again type if gen_opic() swaped operands */ |
| 4151 | | − vtop->t = t1; |
| 4217 | + vtop->type = type1; |
| 4152 | 4218 | } |
| 4153 | 4219 | } else if (is_float(bt1) || is_float(bt2)) { |
| 4154 | 4220 | /* compute bigger type and do implicit casts */ |
| @@ -4199,14 +4265,14 @@ void gen_op(int op) · 44 unmodified lines |
| 4199 | 4265 | op = TOK_UGE; |
| 4200 | 4266 | } |
| 4201 | 4267 | vswap(); |
| 4202 | | − gen_cast(t); |
| 4268 | + type1.t = t; |
| 4269 | + gen_cast(&type1); |
| 4203 | 4270 | vswap(); |
| 4204 | 4271 | /* special case for shifts and long long: we keep the shift as |
| 4205 | 4272 | an integer */ |
| 4206 | 4273 | 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); |
| 4210 | 4276 | if (is_float(t)) |
| 4211 | 4277 | gen_opif(op); |
| 4212 | 4278 | else if ((t & VT_BTYPE) == VT_LLONG) |
| @@ -4215,9 +4281,9 @@ void gen_op(int op) · 2 unmodified lines |
| 4215 | 4281 | gen_opic(op); |
| 4216 | 4282 | if (op >= TOK_ULT && op <= TOK_GT) { |
| 4217 | 4283 | /* relationnal op: the result is an int */ |
| 4218 | | − vtop->t = VT_INT; |
| 4284 | + vtop->type.t = VT_INT; |
| 4219 | 4285 | } else { |
| 4220 | | − vtop->t = t; |
| 4286 | + vtop->type.t = t; |
| 4221 | 4287 | } |
| 4222 | 4288 | } |
| 4223 | 4289 | } |
| @@ -4227,17 +4293,17 @@ void gen_cvt_itof1(int t) · 3 unmodified lines |
| 4227 | 4293 | { |
| 4228 | 4294 | GFuncContext gf; |
| 4229 | 4295 | |
| 4230 | | − if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == |
| 4296 | + if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) == |
| 4231 | 4297 | (VT_LLONG | VT_UNSIGNED)) { |
| 4232 | 4298 | |
| 4233 | 4299 | gfunc_start(&gf, FUNC_CDECL); |
| 4234 | 4300 | gfunc_param(&gf); |
| 4235 | 4301 | if (t == VT_FLOAT) |
| 4236 | | − vpush_global_sym(func_old_type, TOK___ulltof); |
| 4302 | + vpush_global_sym(&func_old_type, TOK___ulltof); |
| 4237 | 4303 | else if (t == VT_DOUBLE) |
| 4238 | | − vpush_global_sym(func_old_type, TOK___ulltod); |
| 4304 | + vpush_global_sym(&func_old_type, TOK___ulltod); |
| 4239 | 4305 | else |
| 4240 | | − vpush_global_sym(func_old_type, TOK___ulltold); |
| 4306 | + vpush_global_sym(&func_old_type, TOK___ulltold); |
| 4241 | 4307 | gfunc_call(&gf); |
| 4242 | 4308 | vpushi(0); |
| 4243 | 4309 | vtop->r = REG_FRET; |
| @@ -4255,14 +4321,14 @@ void gen_cvt_ftoi1(int t) · 11 unmodified lines |
| 4255 | 4321 | if (t == (VT_LLONG | VT_UNSIGNED)) { |
| 4256 | 4322 | /* not handled natively */ |
| 4257 | 4323 | gfunc_start(&gf, FUNC_CDECL); |
| 4258 | | − st = vtop->t & VT_BTYPE; |
| 4324 | + st = vtop->type.t & VT_BTYPE; |
| 4259 | 4325 | gfunc_param(&gf); |
| 4260 | 4326 | if (st == VT_FLOAT) |
| 4261 | | − vpush_global_sym(func_old_type, TOK___fixunssfdi); |
| 4327 | + vpush_global_sym(&func_old_type, TOK___fixunssfdi); |
| 4262 | 4328 | else if (st == VT_DOUBLE) |
| 4263 | | − vpush_global_sym(func_old_type, TOK___fixunsdfdi); |
| 4329 | + vpush_global_sym(&func_old_type, TOK___fixunsdfdi); |
| 4264 | 4330 | else |
| 4265 | | − vpush_global_sym(func_old_type, TOK___fixunsxfdi); |
| 4331 | + vpush_global_sym(&func_old_type, TOK___fixunsxfdi); |
| 4266 | 4332 | gfunc_call(&gf); |
| 4267 | 4333 | vpushi(0); |
| 4268 | 4334 | vtop->r = REG_IRET; |
| @@ -4294,8 +4360,8 @@ void force_charshort_cast(int t) · 25 unmodified lines |
| 4294 | 4360 | } |
| 4295 | 4361 | } |
| 4296 | 4362 | |
| 4297 | | −/* cast 'vtop' to 't' type */ |
| 4298 | | −void gen_cast(int t) |
| 4363 | +/* cast 'vtop' to 'type' */ |
| 4364 | +static void gen_cast(CType *type) |
| 4299 | 4365 | { |
| 4300 | 4366 | int sbt, dbt, sf, df, c; |
| 4301 | 4367 | |
| @@ -4304,11 +4370,11 @@ void gen_cast(int t) · 2 unmodified lines |
| 4304 | 4370 | be incorrect */ |
| 4305 | 4371 | if (vtop->r & VT_MUSTCAST) { |
| 4306 | 4372 | vtop->r &= ~VT_MUSTCAST; |
| 4307 | | − force_charshort_cast(vtop->t); |
| 4373 | + force_charshort_cast(vtop->type.t); |
| 4308 | 4374 | } |
| 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); |
| 4312 | 4378 | |
| 4313 | 4379 | if (sbt != dbt && !nocode_wanted) { |
| 4314 | 4380 | sf = is_float(sbt); |
| @@ -4395,10 +4461,10 @@ void gen_cast(int t) · 80 unmodified lines |
| 4395 | 4461 | do_ftoi: |
| 4396 | 4462 | gen_cvt_ftoi1(dbt); |
| 4397 | 4463 | } |
| 4398 | | − if (dbt == VT_INT && (t & (VT_BTYPE | VT_UNSIGNED)) != dbt) { |
| 4464 | + if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) { |
| 4399 | 4465 | /* additionnal cast for char/short/bool... */ |
| 4400 | | − vtop->t = dbt; |
| 4401 | | − gen_cast(t); |
| 4466 | + vtop->type.t = dbt; |
| 4467 | + gen_cast(type); |
| 4402 | 4468 | } |
| 4403 | 4469 | } else if ((dbt & VT_BTYPE) == VT_LLONG) { |
| 4404 | 4470 | if ((sbt & VT_BTYPE) != VT_LLONG) { |
| @@ -4431,7 +4497,7 @@ void gen_cast(int t) · 26 unmodified lines |
| 4431 | 4497 | gen_op(TOK_NE); |
| 4432 | 4498 | } else if ((dbt & VT_BTYPE) == VT_BYTE || |
| 4433 | 4499 | (dbt & VT_BTYPE) == VT_SHORT) { |
| 4434 | | − force_charshort_cast(t); |
| 4500 | + force_charshort_cast(dbt); |
| 4435 | 4501 | } else if ((dbt & VT_BTYPE) == VT_INT) { |
| 4436 | 4502 | /* scalar to int */ |
| 4437 | 4503 | if (sbt == VT_LLONG) { |
| @@ -4444,25 +4510,25 @@ void gen_cast(int t) · 6 unmodified lines |
| 4444 | 4510 | VT_LVAL_xxx constants) */ |
| 4445 | 4511 | } |
| 4446 | 4512 | } |
| 4447 | | − vtop->t = t; |
| 4513 | + vtop->type = *type; |
| 4448 | 4514 | } |
| 4449 | 4515 | |
| 4450 | 4516 | /* return type size. Put alignment at 'a' */ |
| 4451 | | −int type_size(int t, int *a) |
| 4517 | +int type_size(CType *type, int *a) |
| 4452 | 4518 | { |
| 4453 | 4519 | Sym *s; |
| 4454 | 4520 | int bt; |
| 4455 | 4521 | |
| 4456 | | − bt = t & VT_BTYPE; |
| 4522 | + bt = type->t & VT_BTYPE; |
| 4457 | 4523 | if (bt == VT_STRUCT) { |
| 4458 | 4524 | /* struct/union */ |
| 4459 | | − s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 4525 | + s = type->ref; |
| 4460 | 4526 | *a = 4; /* XXX: cannot store it yet. Doing that is safe */ |
| 4461 | 4527 | return s->c; |
| 4462 | 4528 | } 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; |
| 4466 | 4532 | } else { |
| 4467 | 4533 | *a = PTR_SIZE; |
| 4468 | 4534 | return PTR_SIZE; |
| @@ -4487,52 +4553,50 @@ int type_size(int t, int *a) · 18 unmodified lines |
| 4487 | 4553 | } |
| 4488 | 4554 | |
| 4489 | 4555 | /* return the pointed type of t */ |
| 4490 | | −int pointed_type(int t) |
| 4556 | +static inline CType *pointed_type(CType *type) |
| 4491 | 4557 | { |
| 4492 | | − Sym *s; |
| 4493 | | − s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); |
| 4494 | | − return s->t | (t & ~VT_TYPE); |
| 4558 | + return &type->ref->type; |
| 4495 | 4559 | } |
| 4496 | 4560 | |
| 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) |
| 4498 | 4563 | { |
| 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; |
| 4503 | 4568 | } |
| 4504 | 4569 | |
| 4505 | | −int is_compatible_types(int t1, int t2) |
| 4570 | +static int is_compatible_types(CType *type1, CType *type2) |
| 4506 | 4571 | { |
| 4507 | 4572 | Sym *s1, *s2; |
| 4508 | | − int bt1, bt2; |
| 4573 | + int bt1, bt2, t1, t2; |
| 4509 | 4574 | |
| 4510 | | − t1 &= VT_TYPE; |
| 4511 | | − t2 &= VT_TYPE; |
| 4575 | + t1 = type1->t & VT_TYPE; |
| 4576 | + t2 = type2->t & VT_TYPE; |
| 4512 | 4577 | bt1 = t1 & VT_BTYPE; |
| 4513 | 4578 | bt2 = t2 & VT_BTYPE; |
| 4514 | 4579 | if (bt1 == VT_PTR) { |
| 4515 | | − t1 = pointed_type(t1); |
| 4580 | + type1 = pointed_type(type1); |
| 4516 | 4581 | /* if function, then convert implicitely to function pointer */ |
| 4517 | 4582 | if (bt2 != VT_FUNC) { |
| 4518 | 4583 | if (bt2 != VT_PTR) |
| 4519 | 4584 | return 0; |
| 4520 | | − t2 = pointed_type(t2); |
| 4585 | + type2 = pointed_type(type2); |
| 4521 | 4586 | } |
| 4522 | 4587 | /* 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) |
| 4526 | 4590 | return 1; |
| 4527 | | − return is_compatible_types(t1, t2); |
| 4591 | + return is_compatible_types(type1, type2); |
| 4528 | 4592 | } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) { |
| 4529 | 4593 | return (t2 == t1); |
| 4530 | 4594 | } else if (bt1 == VT_FUNC) { |
| 4531 | 4595 | if (bt2 != VT_FUNC) |
| 4532 | 4596 | 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)) |
| 4536 | 4600 | return 0; |
| 4537 | 4601 | /* XXX: not complete */ |
| 4538 | 4602 | if (s1->c == FUNC_OLD || s2->c == FUNC_OLD) |
| @@ -4542,7 +4606,7 @@ int is_compatible_types(int t1, int t2) · 3 unmodified lines |
| 4542 | 4606 | while (s1 != NULL) { |
| 4543 | 4607 | if (s2 == NULL) |
| 4544 | 4608 | return 0; |
| 4545 | | − if (!is_compatible_types(s1->t, s2->t)) |
| 4609 | + if (!is_compatible_types(&s1->type, &s2->type)) |
| 4546 | 4610 | return 0; |
| 4547 | 4611 | s1 = s1->next; |
| 4548 | 4612 | s2 = s2->next; |
| @@ -4561,14 +4625,14 @@ int is_compatible_types(int t1, int t2) · 12 unmodified lines |
| 4561 | 4625 | /* XXX: union */ |
| 4562 | 4626 | /* XXX: add array and function pointers */ |
| 4563 | 4627 | void type_to_str(char *buf, int buf_size, |
| 4564 | | − int t, const char *varstr) |
| 4628 | + CType *type, const char *varstr) |
| 4565 | 4629 | { |
| 4566 | | − int bt, v; |
| 4630 | + int bt, v, t; |
| 4567 | 4631 | Sym *s, *sa; |
| 4568 | 4632 | char buf1[256]; |
| 4569 | 4633 | const char *tstr; |
| 4570 | 4634 | |
| 4571 | | − t = t & VT_TYPE; |
| 4635 | + t = type->t & VT_TYPE; |
| 4572 | 4636 | bt = t & VT_BTYPE; |
| 4573 | 4637 | buf[0] = '\0'; |
| 4574 | 4638 | if (t & VT_UNSIGNED) |
| @@ -4613,19 +4677,19 @@ void type_to_str(char *buf, int buf_size, · 38 unmodified lines |
| 4613 | 4677 | else |
| 4614 | 4678 | tstr = "enum "; |
| 4615 | 4679 | pstrcat(buf, buf_size, tstr); |
| 4616 | | − v = (unsigned)t >> VT_STRUCT_SHIFT; |
| 4680 | + v = type->ref->v; |
| 4617 | 4681 | if (v >= SYM_FIRST_ANOM) |
| 4618 | 4682 | pstrcat(buf, buf_size, "<anonymous>"); |
| 4619 | 4683 | else |
| 4620 | 4684 | pstrcat(buf, buf_size, get_tok_str(v, NULL)); |
| 4621 | 4685 | break; |
| 4622 | 4686 | 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); |
| 4625 | 4689 | pstrcat(buf, buf_size, "("); |
| 4626 | 4690 | sa = s->next; |
| 4627 | 4691 | while (sa != NULL) { |
| 4628 | | − type_to_str(buf1, sizeof(buf1), sa->t, NULL); |
| 4692 | + type_to_str(buf1, sizeof(buf1), &sa->type, NULL); |
| 4629 | 4693 | pstrcat(buf, buf_size, buf1); |
| 4630 | 4694 | sa = sa->next; |
| 4631 | 4695 | if (sa) |
| @@ -4634,11 +4698,11 @@ void type_to_str(char *buf, int buf_size, · 2 unmodified lines |
| 4634 | 4698 | pstrcat(buf, buf_size, ")"); |
| 4635 | 4699 | goto no_var; |
| 4636 | 4700 | case VT_PTR: |
| 4637 | | − s = sym_find((unsigned)t >> VT_STRUCT_SHIFT); |
| 4701 | + s = type->ref; |
| 4638 | 4702 | pstrcpy(buf1, sizeof(buf1), "*"); |
| 4639 | 4703 | if (varstr) |
| 4640 | 4704 | 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); |
| 4642 | 4706 | goto no_var; |
| 4643 | 4707 | } |
| 4644 | 4708 | if (varstr) { |
| @@ -4650,23 +4714,23 @@ void type_to_str(char *buf, int buf_size, · 5 unmodified lines |
| 4650 | 4714 | |
| 4651 | 4715 | /* verify type compatibility to store vtop in 'dt' type, and generate |
| 4652 | 4716 | casts if needed. */ |
| 4653 | | −void gen_assign_cast(int dt) |
| 4717 | +void gen_assign_cast(CType *dt) |
| 4654 | 4718 | { |
| 4655 | | − int st; |
| 4719 | + CType *st; |
| 4656 | 4720 | char buf1[256], buf2[256]; |
| 4657 | 4721 | |
| 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) { |
| 4660 | 4724 | /* special cases for pointers */ |
| 4661 | 4725 | /* a function is implicitely a function pointer */ |
| 4662 | | − if ((st & VT_BTYPE) == VT_FUNC) { |
| 4726 | + if ((st->t & VT_BTYPE) == VT_FUNC) { |
| 4663 | 4727 | if (!is_compatible_types(pointed_type(dt), st)) |
| 4664 | 4728 | goto error; |
| 4665 | 4729 | else |
| 4666 | 4730 | goto type_ok; |
| 4667 | 4731 | } |
| 4668 | 4732 | /* '0' can also be a pointer */ |
| 4669 | | − if ((st & VT_BTYPE) == VT_INT && |
| 4733 | + if ((st->t & VT_BTYPE) == VT_INT && |
| 4670 | 4734 | ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) && |
| 4671 | 4735 | vtop->c.i == 0) |
| 4672 | 4736 | goto type_ok; |
| @@ -4687,17 +4751,17 @@ void vstore(void) · 14 unmodified lines |
| 4687 | 4751 | int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast; |
| 4688 | 4752 | GFuncContext gf; |
| 4689 | 4753 | |
| 4690 | | − ft = vtop[-1].t; |
| 4691 | | − sbt = vtop->t & VT_BTYPE; |
| 4754 | + ft = vtop[-1].type.t; |
| 4755 | + sbt = vtop->type.t & VT_BTYPE; |
| 4692 | 4756 | dbt = ft & VT_BTYPE; |
| 4693 | 4757 | if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) || |
| 4694 | 4758 | (sbt == VT_INT && dbt == VT_SHORT)) { |
| 4695 | 4759 | /* optimize char/short casts */ |
| 4696 | 4760 | delayed_cast = VT_MUSTCAST; |
| 4697 | | − vtop->t = ft & VT_TYPE; |
| 4761 | + vtop->type.t = ft & VT_TYPE; |
| 4698 | 4762 | } else { |
| 4699 | 4763 | delayed_cast = 0; |
| 4700 | | − gen_assign_cast(ft & VT_TYPE); |
| 4764 | + gen_assign_cast(&vtop[-1].type); |
| 4701 | 4765 | } |
| 4702 | 4766 | |
| 4703 | 4767 | if (sbt == VT_STRUCT) { |
| @@ -4708,21 +4772,21 @@ void vstore(void) · 4 unmodified lines |
| 4708 | 4772 | vdup(); |
| 4709 | 4773 | gfunc_start(&gf, FUNC_CDECL); |
| 4710 | 4774 | /* type size */ |
| 4711 | | − size = type_size(vtop->t, &align); |
| 4775 | + size = type_size(&vtop->type, &align); |
| 4712 | 4776 | vpushi(size); |
| 4713 | 4777 | gfunc_param(&gf); |
| 4714 | 4778 | /* source */ |
| 4715 | | − vtop->t = VT_INT; |
| 4779 | + vtop->type.t = VT_INT; |
| 4716 | 4780 | gaddrof(); |
| 4717 | 4781 | gfunc_param(&gf); |
| 4718 | 4782 | /* destination */ |
| 4719 | 4783 | vswap(); |
| 4720 | | − vtop->t = VT_INT; |
| 4784 | + vtop->type.t = VT_INT; |
| 4721 | 4785 | gaddrof(); |
| 4722 | 4786 | gfunc_param(&gf); |
| 4723 | 4787 | |
| 4724 | 4788 | save_regs(0); |
| 4725 | | − vpush_global_sym(func_old_type, TOK_memcpy); |
| 4789 | + vpush_global_sym(&func_old_type, TOK_memcpy); |
| 4726 | 4790 | gfunc_call(&gf); |
| 4727 | 4791 | } else { |
| 4728 | 4792 | vswap(); |
| @@ -4734,7 +4798,7 @@ void vstore(void) · 5 unmodified lines |
| 4734 | 4798 | bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f; |
| 4735 | 4799 | bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f; |
| 4736 | 4800 | /* 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)); |
| 4738 | 4802 | |
| 4739 | 4803 | /* duplicate destination */ |
| 4740 | 4804 | vdup(); |
| @@ -4770,7 +4834,7 @@ void vstore(void) · 29 unmodified lines |
| 4770 | 4834 | if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) { |
| 4771 | 4835 | SValue sv; |
| 4772 | 4836 | t = get_reg(RC_INT); |
| 4773 | | − sv.t = VT_INT; |
| 4837 | + sv.type.t = VT_INT; |
| 4774 | 4838 | sv.r = VT_LOCAL | VT_LVAL; |
| 4775 | 4839 | sv.c.ul = vtop[-1].c.ul; |
| 4776 | 4840 | load(t, &sv); |
| @@ -4781,7 +4845,7 @@ void vstore(void) · 4 unmodified lines |
| 4781 | 4845 | if ((ft & VT_BTYPE) == VT_LLONG) { |
| 4782 | 4846 | vswap(); |
| 4783 | 4847 | /* convert to int to increment easily */ |
| 4784 | | − vtop->t = VT_INT; |
| 4848 | + vtop->type.t = VT_INT; |
| 4785 | 4849 | gaddrof(); |
| 4786 | 4850 | vpushi(4); |
| 4787 | 4851 | gen_op('+'); |
| @@ -4892,13 +4956,14 @@ void parse_attribute(AttributeDef *ad) · 104 unmodified lines |
| 4892 | 4956 | skip(')'); |
| 4893 | 4957 | } |
| 4894 | 4958 | |
| 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) |
| 4897 | 4961 | { |
| 4898 | | − int a, t, b, v, size, align, maxalign, c, offset; |
| 4962 | + int a, v, size, align, maxalign, c, offset; |
| 4899 | 4963 | int bit_size, bit_pos, bsize, bt, lbit_pos; |
| 4900 | 4964 | Sym *s, *ss, **ps; |
| 4901 | 4965 | AttributeDef ad; |
| 4966 | + CType type1, btype; |
| 4902 | 4967 | |
| 4903 | 4968 | a = tok; /* save decl type */ |
| 4904 | 4969 | next(); |
| @@ -4909,17 +4974,19 @@ int struct_decl(int u) · 4 unmodified lines |
| 4909 | 4974 | /* XXX: check consistency */ |
| 4910 | 4975 | s = sym_find(v | SYM_STRUCT); |
| 4911 | 4976 | if (s) { |
| 4912 | | − if (s->t != a) |
| 4977 | + if (s->type.t != a) |
| 4913 | 4978 | error("invalid type"); |
| 4914 | 4979 | goto do_decl; |
| 4915 | 4980 | } |
| 4916 | 4981 | } else { |
| 4917 | 4982 | v = anon_sym++; |
| 4918 | 4983 | } |
| 4919 | | − s = sym_push(v | SYM_STRUCT, a, 0, 0); |
| 4984 | + type1.t = a; |
| 4985 | + s = sym_push(v | SYM_STRUCT, &type1, 0, 0); |
| 4920 | 4986 | /* put struct/union/enum name in type */ |
| 4921 | 4987 | do_decl: |
| 4922 | | − u = u | (v << VT_STRUCT_SHIFT); |
| 4988 | + type->t = u; |
| 4989 | + type->ref = s; |
| 4923 | 4990 | |
| 4924 | 4991 | if (tok == '{') { |
| 4925 | 4992 | next(); |
| @@ -4940,23 +5007,23 @@ int struct_decl(int u) · 14 unmodified lines |
| 4940 | 5007 | c = expr_const(); |
| 4941 | 5008 | } |
| 4942 | 5009 | /* 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; |
| 4944 | 5012 | if (tok == ',') |
| 4945 | 5013 | next(); |
| 4946 | 5014 | c++; |
| 4947 | 5015 | } else { |
| 4948 | | − parse_btype(&b, &ad); |
| 5016 | + parse_btype(&btype, &ad); |
| 4949 | 5017 | while (1) { |
| 4950 | 5018 | bit_size = -1; |
| 4951 | 5019 | v = 0; |
| 5020 | + type1 = btype; |
| 4952 | 5021 | 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))) |
| 4956 | 5025 | error("invalid type for '%s'", |
| 4957 | 5026 | get_tok_str(v, NULL)); |
| 4958 | | − } else { |
| 4959 | | − t = b; |
| 4960 | 5027 | } |
| 4961 | 5028 | if (tok == ':') { |
| 4962 | 5029 | next(); |
| @@ -4969,10 +5036,10 @@ int struct_decl(int u) · 6 unmodified lines |
| 4969 | 5036 | error("zero width for bit-field '%s'", |
| 4970 | 5037 | get_tok_str(v, NULL)); |
| 4971 | 5038 | } |
| 4972 | | − size = type_size(t, &align); |
| 5039 | + size = type_size(&type1, &align); |
| 4973 | 5040 | lbit_pos = 0; |
| 4974 | 5041 | if (bit_size >= 0) { |
| 4975 | | − bt = t & VT_BTYPE; |
| 5042 | + bt = type1.t & VT_BTYPE; |
| 4976 | 5043 | if (bt != VT_INT && |
| 4977 | 5044 | bt != VT_BYTE && |
| 4978 | 5045 | bt != VT_SHORT) |
| @@ -4996,7 +5063,7 @@ int struct_decl(int u) · 17 unmodified lines |
| 4996 | 5063 | bit_pos = 0; |
| 4997 | 5064 | lbit_pos = bit_pos; |
| 4998 | 5065 | /* XXX: handle LSB first */ |
| 4999 | | − t |= VT_BITFIELD | |
| 5066 | + type1.t |= VT_BITFIELD | |
| 5000 | 5067 | (bit_pos << VT_STRUCT_SHIFT) | |
| 5001 | 5068 | (bit_size << (VT_STRUCT_SHIFT + 6)); |
| 5002 | 5069 | bit_pos += bit_size; |
| @@ -5023,14 +5090,14 @@ int struct_decl(int u) · 20 unmodified lines |
| 5023 | 5090 | #if 0 |
| 5024 | 5091 | printf("add field %s offset=%d", |
| 5025 | 5092 | get_tok_str(v, NULL), offset); |
| 5026 | | − if (t & VT_BITFIELD) { |
| 5093 | + if (type1.t & VT_BITFIELD) { |
| 5027 | 5094 | 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); |
| 5030 | 5097 | } |
| 5031 | 5098 | printf("\n"); |
| 5032 | 5099 | #endif |
| 5033 | | − ss = sym_push(v | SYM_FIELD, t, 0, offset); |
| 5100 | + ss = sym_push(v | SYM_FIELD, &type1, 0, offset); |
| 5034 | 5101 | *ps = ss; |
| 5035 | 5102 | ps = &ss->next; |
| 5036 | 5103 | } |
| @@ -5047,16 +5114,16 @@ int struct_decl(int u) · 10 unmodified lines |
| 5047 | 5114 | /* size for struct/union, dummy for enum */ |
| 5048 | 5115 | s->c = (c + maxalign - 1) & -maxalign; |
| 5049 | 5116 | } |
| 5050 | | − return u; |
| 5051 | 5117 | } |
| 5052 | 5118 | |
| 5053 | 5119 | /* return 0 if no type declaration. otherwise, return the basic type |
| 5054 | 5120 | and skip it. |
| 5055 | 5121 | */ |
| 5056 | | −int parse_btype(int *type_ptr, AttributeDef *ad) |
| 5122 | +static int parse_btype(CType *type, AttributeDef *ad) |
| 5057 | 5123 | { |
| 5058 | 5124 | int t, u, type_found; |
| 5059 | 5125 | Sym *s; |
| 5126 | + CType type1; |
| 5060 | 5127 | |
| 5061 | 5128 | memset(ad, 0, sizeof(AttributeDef)); |
| 5062 | 5129 | type_found = 0; |
| @@ -5109,12 +5176,15 @@ int parse_btype(int *type_ptr, AttributeDef *ad) · 46 unmodified lines |
| 5109 | 5176 | } |
| 5110 | 5177 | break; |
| 5111 | 5178 | 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; |
| 5113 | 5183 | goto basic_type1; |
| 5114 | 5184 | case TOK_STRUCT: |
| 5115 | 5185 | case TOK_UNION: |
| 5116 | | − u = struct_decl(VT_STRUCT); |
| 5117 | | − goto basic_type1; |
| 5186 | + struct_decl(&type1, VT_STRUCT); |
| 5187 | + goto basic_type2; |
| 5118 | 5188 | |
| 5119 | 5189 | /* type modifiers */ |
| 5120 | 5190 | case TOK_CONST: |
| @@ -5153,13 +5223,14 @@ int parse_btype(int *type_ptr, AttributeDef *ad) · 32 unmodified lines |
| 5153 | 5223 | /* GNUC typeof */ |
| 5154 | 5224 | case TOK_TYPEOF: |
| 5155 | 5225 | next(); |
| 5156 | | − u = parse_expr_type(); |
| 5157 | | − goto basic_type1; |
| 5226 | + parse_expr_type(&type1); |
| 5227 | + goto basic_type2; |
| 5158 | 5228 | default: |
| 5159 | 5229 | s = sym_find(tok); |
| 5160 | | − if (!s || !(s->t & VT_TYPEDEF)) |
| 5230 | + if (!s || !(s->type.t & VT_TYPEDEF)) |
| 5161 | 5231 | goto the_end; |
| 5162 | | − t |= (s->t & ~VT_TYPEDEF); |
| 5232 | + t |= (s->type.t & ~VT_TYPEDEF); |
| 5233 | + type->ref = s->type.ref; |
| 5163 | 5234 | next(); |
| 5164 | 5235 | break; |
| 5165 | 5236 | } |
| @@ -5169,15 +5240,16 @@ the_end: · 3 unmodified lines |
| 5169 | 5240 | /* long is never used as type */ |
| 5170 | 5241 | if ((t & VT_BTYPE) == VT_LONG) |
| 5171 | 5242 | t = (t & ~VT_BTYPE) | VT_INT; |
| 5172 | | − *type_ptr = t; |
| 5243 | + type->t = t; |
| 5173 | 5244 | return type_found; |
| 5174 | 5245 | } |
| 5175 | 5246 | |
| 5176 | | −int post_type(int t, AttributeDef *ad) |
| 5247 | +static void post_type(CType *type, AttributeDef *ad) |
| 5177 | 5248 | { |
| 5178 | | − int p, n, pt, l, t1; |
| 5249 | + int n, l, t1; |
| 5179 | 5250 | Sym **plast, *s, *first; |
| 5180 | 5251 | AttributeDef ad1; |
| 5252 | + CType pt; |
| 5181 | 5253 | |
| 5182 | 5254 | if (tok == '(') { |
| 5183 | 5255 | /* function declaration */ |
| @@ -5197,20 +5269,20 @@ int post_type(int t, AttributeDef *ad) · 13 unmodified lines |
| 5197 | 5269 | } |
| 5198 | 5270 | } |
| 5199 | 5271 | l = FUNC_NEW; |
| 5200 | | − if ((pt & VT_BTYPE) == VT_VOID && tok == ')') |
| 5272 | + if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')') |
| 5201 | 5273 | 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) |
| 5204 | 5276 | error("parameter declared as void"); |
| 5205 | 5277 | } else { |
| 5206 | 5278 | old_proto: |
| 5207 | 5279 | n = tok; |
| 5208 | | − pt = VT_INT; |
| 5280 | + pt.t = VT_INT; |
| 5209 | 5281 | next(); |
| 5210 | 5282 | } |
| 5211 | 5283 | /* 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); |
| 5214 | 5286 | *plast = s; |
| 5215 | 5287 | plast = &s->next; |
| 5216 | 5288 | if (tok == ',') { |
| @@ -5226,13 +5298,14 @@ int post_type(int t, AttributeDef *ad) · 9 unmodified lines |
| 5226 | 5298 | if (l == 0) |
| 5227 | 5299 | l = FUNC_OLD; |
| 5228 | 5300 | 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); |
| 5231 | 5304 | /* 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); |
| 5234 | 5306 | s->next = first; |
| 5235 | | − t = t1 | VT_FUNC | (p << VT_STRUCT_SHIFT); |
| 5307 | + type->t = t1 | VT_FUNC; |
| 5308 | + type->ref = s; |
| 5236 | 5309 | } else if (tok == '[') { |
| 5237 | 5310 | /* array definition */ |
| 5238 | 5311 | next(); |
| @@ -5244,46 +5317,48 @@ int post_type(int t, AttributeDef *ad) · 5 unmodified lines |
| 5244 | 5317 | } |
| 5245 | 5318 | skip(']'); |
| 5246 | 5319 | /* 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); |
| 5249 | 5323 | |
| 5250 | 5324 | /* we push a anonymous symbol which will contain the array |
| 5251 | 5325 | 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; |
| 5255 | 5329 | } |
| 5256 | | − return t; |
| 5257 | 5330 | } |
| 5258 | 5331 | |
| 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) |
| 5264 | 5339 | { |
| 5265 | | − int u, p; |
| 5266 | 5340 | Sym *s; |
| 5341 | + CType type1, *type2; |
| 5267 | 5342 | |
| 5268 | 5343 | while (tok == '*') { |
| 5269 | 5344 | next(); |
| 5270 | 5345 | while (tok == TOK_CONST || tok == TOK_VOLATILE || tok == TOK_RESTRICT) |
| 5271 | 5346 | next(); |
| 5272 | | − t = mk_pointer(t); |
| 5347 | + mk_pointer(type); |
| 5273 | 5348 | } |
| 5274 | 5349 | |
| 5275 | 5350 | /* recursive type */ |
| 5276 | 5351 | /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */ |
| 5352 | + type1.t = 0; /* XXX: same as int */ |
| 5277 | 5353 | if (tok == '(') { |
| 5278 | 5354 | next(); |
| 5279 | 5355 | /* XXX: this is not correct to modify 'ad' at this point, but |
| 5280 | 5356 | the syntax is not clear */ |
| 5281 | 5357 | if (tok == TOK___ATTRIBUTE__) |
| 5282 | 5358 | parse_attribute(ad); |
| 5283 | | − u = type_decl(ad, v, 0, td); |
| 5359 | + type_decl(&type1, ad, v, td); |
| 5284 | 5360 | skip(')'); |
| 5285 | 5361 | } else { |
| 5286 | | − u = 0; |
| 5287 | 5362 | /* type identifier */ |
| 5288 | 5363 | if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) { |
| 5289 | 5364 | *v = tok; |
| @@ -5294,22 +5369,22 @@ int type_decl(AttributeDef *ad, int *v, int t, int td) · 4 unmodified lines |
| 5294 | 5369 | *v = 0; |
| 5295 | 5370 | } |
| 5296 | 5371 | } |
| 5297 | | − /* append t at the end of u */ |
| 5298 | | − t = post_type(t, ad); |
| 5372 | + post_type(type, ad); |
| 5299 | 5373 | if (tok == TOK___ATTRIBUTE__) |
| 5300 | 5374 | 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; |
| 5309 | 5384 | break; |
| 5310 | 5385 | } |
| 5311 | 5386 | } |
| 5312 | | − return u; |
| 5387 | + *type = type1; |
| 5313 | 5388 | } |
| 5314 | 5389 | |
| 5315 | 5390 | /* compute the lvalue VT_LVAL_xxx needed to match type t. */ |
| @@ -5332,14 +5407,14 @@ static int lvalue_type(int t) · 16 unmodified lines |
| 5332 | 5407 | /* indirection with full error checking and bound check */ |
| 5333 | 5408 | static void indir(void) |
| 5334 | 5409 | { |
| 5335 | | − if ((vtop->t & VT_BTYPE) != VT_PTR) |
| 5410 | + if ((vtop->type.t & VT_BTYPE) != VT_PTR) |
| 5336 | 5411 | expect("pointer"); |
| 5337 | 5412 | if ((vtop->r & VT_LVAL) && !nocode_wanted) |
| 5338 | 5413 | gv(RC_INT); |
| 5339 | | − vtop->t = pointed_type(vtop->t); |
| 5414 | + vtop->type = *pointed_type(&vtop->type); |
| 5340 | 5415 | /* 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); |
| 5343 | 5418 | /* if bound checking, the referenced pointer must be checked */ |
| 5344 | 5419 | if (do_bounds_check) |
| 5345 | 5420 | vtop->r |= VT_MUSTBOUND; |
| @@ -5350,16 +5425,20 @@ static void indir(void) · 4 unmodified lines |
| 5350 | 5425 | void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) |
| 5351 | 5426 | { |
| 5352 | 5427 | int func_type; |
| 5428 | + CType type; |
| 5429 | + |
| 5353 | 5430 | func_type = func->c; |
| 5354 | 5431 | if (func_type == FUNC_OLD || |
| 5355 | 5432 | (func_type == FUNC_ELLIPSIS && arg == NULL)) { |
| 5356 | 5433 | /* 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 | + } |
| 5359 | 5438 | } else if (arg == NULL) { |
| 5360 | 5439 | error("too many arguments to function"); |
| 5361 | 5440 | } else { |
| 5362 | | − gen_assign_cast(arg->t); |
| 5441 | + gen_assign_cast(&arg->type); |
| 5363 | 5442 | } |
| 5364 | 5443 | if (!nocode_wanted) { |
| 5365 | 5444 | gfunc_param(gf); |
| @@ -5370,24 +5449,31 @@ void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg) · 4 unmodified lines |
| 5370 | 5449 | |
| 5371 | 5450 | /* parse an expression of the form '(type)' or '(expr)' and return its |
| 5372 | 5451 | type */ |
| 5373 | | −static int parse_expr_type(void) |
| 5452 | +static void parse_expr_type(CType *type) |
| 5374 | 5453 | { |
| 5375 | | − int ft, n; |
| 5454 | + int n; |
| 5376 | 5455 | AttributeDef ad; |
| 5377 | 5456 | |
| 5378 | 5457 | 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); |
| 5381 | 5460 | } else { |
| 5382 | | − ft = expr_type(); |
| 5461 | + expr_type(type); |
| 5383 | 5462 | } |
| 5384 | 5463 | 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); |
| 5386 | 5471 | } |
| 5387 | 5472 | |
| 5388 | 5473 | static void unary(void) |
| 5389 | 5474 | { |
| 5390 | | − int n, t, ft, align, size, r; |
| 5475 | + int n, t, align, size, r; |
| 5476 | + CType type; |
| 5391 | 5477 | Sym *s; |
| 5392 | 5478 | GFuncContext gf; |
| 5393 | 5479 | AttributeDef ad; |
| @@ -5396,22 +5482,22 @@ static void unary(void) · 2 unmodified lines |
| 5396 | 5482 | vpushi(tokc.i); |
| 5397 | 5483 | next(); |
| 5398 | 5484 | } else if (tok == TOK_CUINT) { |
| 5399 | | − vsetc(VT_INT | VT_UNSIGNED, VT_CONST, &tokc); |
| 5485 | + vpush_tokc(VT_INT | VT_UNSIGNED); |
| 5400 | 5486 | next(); |
| 5401 | 5487 | } else if (tok == TOK_CLLONG) { |
| 5402 | | − vsetc(VT_LLONG, VT_CONST, &tokc); |
| 5488 | + vpush_tokc(VT_LLONG); |
| 5403 | 5489 | next(); |
| 5404 | 5490 | } else if (tok == TOK_CULLONG) { |
| 5405 | | − vsetc(VT_LLONG | VT_UNSIGNED, VT_CONST, &tokc); |
| 5491 | + vpush_tokc(VT_LLONG | VT_UNSIGNED); |
| 5406 | 5492 | next(); |
| 5407 | 5493 | } else if (tok == TOK_CFLOAT) { |
| 5408 | | − vsetc(VT_FLOAT, VT_CONST, &tokc); |
| 5494 | + vpush_tokc(VT_FLOAT); |
| 5409 | 5495 | next(); |
| 5410 | 5496 | } else if (tok == TOK_CDOUBLE) { |
| 5411 | | − vsetc(VT_DOUBLE, VT_CONST, &tokc); |
| 5497 | + vpush_tokc(VT_DOUBLE); |
| 5412 | 5498 | next(); |
| 5413 | 5499 | } else if (tok == TOK_CLDOUBLE) { |
| 5414 | | − vsetc(VT_LDOUBLE, VT_CONST, &tokc); |
| 5500 | + vpush_tokc(VT_LDOUBLE); |
| 5415 | 5501 | next(); |
| 5416 | 5502 | } else if (tok == TOK___FUNC__ || (tok == TOK___FUNCTION__ && gnu_ext)) { |
| 5417 | 5503 | void *ptr; |
| @@ -5420,10 +5506,11 @@ static void unary(void) · 2 unmodified lines |
| 5420 | 5506 | |
| 5421 | 5507 | len = strlen(funcname) + 1; |
| 5422 | 5508 | /* 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); |
| 5427 | 5514 | ptr = section_ptr_add(data_section, len); |
| 5428 | 5515 | memcpy(ptr, funcname, len); |
| 5429 | 5516 | next(); |
| @@ -5434,16 +5521,18 @@ static void unary(void) · 4 unmodified lines |
| 5434 | 5521 | /* string parsing */ |
| 5435 | 5522 | t = VT_BYTE; |
| 5436 | 5523 | str_init: |
| 5437 | | − t = VT_ARRAY | mk_pointer(t); |
| 5524 | + type.t = t; |
| 5525 | + mk_pointer(&type); |
| 5526 | + type.t |= VT_ARRAY; |
| 5438 | 5527 | 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); |
| 5440 | 5529 | } else { |
| 5441 | 5530 | t = tok; |
| 5442 | 5531 | next(); |
| 5443 | 5532 | if (t == '(') { |
| 5444 | 5533 | /* 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); |
| 5447 | 5536 | skip(')'); |
| 5448 | 5537 | /* check ISOC99 compound literal */ |
| 5449 | 5538 | if (tok == '{') { |
| @@ -5453,13 +5542,13 @@ static void unary(void) · 3 unmodified lines |
| 5453 | 5542 | else |
| 5454 | 5543 | r = VT_LOCAL; |
| 5455 | 5544 | /* 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); |
| 5458 | 5547 | 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); |
| 5460 | 5549 | } else { |
| 5461 | 5550 | unary(); |
| 5462 | | − gen_cast(ft); |
| 5551 | + gen_cast(&type); |
| 5463 | 5552 | } |
| 5464 | 5553 | } else { |
| 5465 | 5554 | gexpr(); |
| @@ -5475,10 +5564,10 @@ static void unary(void) · 9 unmodified lines |
| 5475 | 5564 | functions are not lvalues, we only have to handle it |
| 5476 | 5565 | there and in function calls. */ |
| 5477 | 5566 | /* 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)) |
| 5480 | 5569 | test_lvalue(); |
| 5481 | | − vtop->t = mk_pointer(vtop->t); |
| 5570 | + mk_pointer(&vtop->type); |
| 5482 | 5571 | gaddrof(); |
| 5483 | 5572 | } else if (t == '!') { |
| 5484 | 5573 | unary(); |
| @@ -5487,7 +5576,7 @@ static void unary(void) · 2 unmodified lines |
| 5487 | 5576 | else if ((vtop->r & VT_VALMASK) == VT_CMP) |
| 5488 | 5577 | vtop->c.i = vtop->c.i ^ 1; |
| 5489 | 5578 | else |
| 5490 | | − vset(VT_INT, VT_JMP, gtst(1, 0)); |
| 5579 | + vseti(VT_JMP, gtst(1, 0)); |
| 5491 | 5580 | } else if (t == '~') { |
| 5492 | 5581 | unary(); |
| 5493 | 5582 | vpushi(-1); |
| @@ -5495,17 +5584,17 @@ static void unary(void) · 1 unmodified line |
| 5495 | 5584 | } else if (t == '+') { |
| 5496 | 5585 | /* in order to force cast, we add zero */ |
| 5497 | 5586 | unary(); |
| 5498 | | − if ((vtop->t & VT_BTYPE) == VT_PTR) |
| 5587 | + if ((vtop->type.t & VT_BTYPE) == VT_PTR) |
| 5499 | 5588 | error("pointer not accepted for unary plus"); |
| 5500 | 5589 | vpushi(0); |
| 5501 | 5590 | gen_op('+'); |
| 5502 | 5591 | } else if (t == TOK_SIZEOF || t == TOK_ALIGNOF) { |
| 5503 | 5592 | if (tok == '(') { |
| 5504 | | − ft = parse_expr_type(); |
| 5593 | + parse_expr_type(&type); |
| 5505 | 5594 | } else { |
| 5506 | | − ft = unary_type(); |
| 5595 | + unary_type(&type); |
| 5507 | 5596 | } |
| 5508 | | − size = type_size(ft, &align); |
| 5597 | + size = type_size(&type, &align); |
| 5509 | 5598 | if (t == TOK_SIZEOF) |
| 5510 | 5599 | vpushi(size); |
| 5511 | 5600 | else |
| @@ -5526,9 +5615,12 @@ static void unary(void) · 14 unmodified lines |
| 5526 | 5615 | s = sym_push1(&label_stack, tok, 0, 0); |
| 5527 | 5616 | s->r = LABEL_FORWARD; |
| 5528 | 5617 | } |
| 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); |
| 5532 | 5624 | vtop->sym = s; |
| 5533 | 5625 | next(); |
| 5534 | 5626 | } else { |
| @@ -5540,9 +5632,9 @@ static void unary(void) · 5 unmodified lines |
| 5540 | 5632 | error("'%s' undeclared", get_tok_str(t, NULL)); |
| 5541 | 5633 | /* for simple function calls, we tolerate undeclared |
| 5542 | 5634 | 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); |
| 5544 | 5636 | } |
| 5545 | | − vset(s->t, s->r, s->c); |
| 5637 | + vset(&s->type, s->r, s->c); |
| 5546 | 5638 | /* if forward reference, we must point to s */ |
| 5547 | 5639 | if (vtop->r & VT_SYM) { |
| 5548 | 5640 | vtop->sym = s; |
| @@ -5564,9 +5656,9 @@ static void unary(void) · 15 unmodified lines |
| 5564 | 5656 | gaddrof(); |
| 5565 | 5657 | next(); |
| 5566 | 5658 | /* expect pointer on structure */ |
| 5567 | | − if ((vtop->t & VT_BTYPE) != VT_STRUCT) |
| 5659 | + if ((vtop->type.t & VT_BTYPE) != VT_STRUCT) |
| 5568 | 5660 | expect("struct or union"); |
| 5569 | | − s = sym_find(((unsigned)vtop->t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 5661 | + s = vtop->type.ref; |
| 5570 | 5662 | /* find field */ |
| 5571 | 5663 | tok |= SYM_FIELD; |
| 5572 | 5664 | while ((s = s->next) != NULL) { |
| @@ -5576,14 +5668,14 @@ static void unary(void) · 3 unmodified lines |
| 5576 | 5668 | if (!s) |
| 5577 | 5669 | error("field not found"); |
| 5578 | 5670 | /* 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 *' */ |
| 5580 | 5672 | vpushi(s->c); |
| 5581 | 5673 | gen_op('+'); |
| 5582 | 5674 | /* change type to field type, and set to lvalue */ |
| 5583 | | − vtop->t = s->t; |
| 5675 | + vtop->type = s->type; |
| 5584 | 5676 | /* 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); |
| 5587 | 5679 | /* if bound checking, the referenced pointer must be checked */ |
| 5588 | 5680 | if (do_bounds_check) |
| 5589 | 5681 | vtop->r |= VT_MUSTBOUND; |
| @@ -5600,11 +5692,11 @@ static void unary(void) · 10 unmodified lines |
| 5600 | 5692 | Sym *sa; |
| 5601 | 5693 | |
| 5602 | 5694 | /* function call */ |
| 5603 | | − if ((vtop->t & VT_BTYPE) != VT_FUNC) { |
| 5695 | + if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { |
| 5604 | 5696 | /* 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) |
| 5608 | 5700 | goto error_func; |
| 5609 | 5701 | } else { |
| 5610 | 5702 | error_func: |
| @@ -5614,7 +5706,7 @@ static void unary(void) · 3 unmodified lines |
| 5614 | 5706 | vtop->r &= ~VT_LVAL; /* no lvalue */ |
| 5615 | 5707 | } |
| 5616 | 5708 | /* get return type */ |
| 5617 | | − s = sym_find((unsigned)vtop->t >> VT_STRUCT_SHIFT); |
| 5709 | + s = vtop->type.ref; |
| 5618 | 5710 | if (!nocode_wanted) { |
| 5619 | 5711 | save_regs(0); /* save used temporary registers */ |
| 5620 | 5712 | gfunc_start(&gf, s->r); |
| @@ -5673,28 +5765,28 @@ static void unary(void) · 52 unmodified lines |
| 5673 | 5765 | } |
| 5674 | 5766 | #endif |
| 5675 | 5767 | /* 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) { |
| 5677 | 5769 | /* get some space for the returned structure */ |
| 5678 | | − size = type_size(s->t, &align); |
| 5770 | + size = type_size(&s->type, &align); |
| 5679 | 5771 | loc = (loc - size) & -align; |
| 5680 | | − ret.t = s->t; |
| 5772 | + ret.type = s->type; |
| 5681 | 5773 | ret.r = VT_LOCAL | VT_LVAL; |
| 5682 | 5774 | /* pass it as 'int' to avoid structure arg passing |
| 5683 | 5775 | problems */ |
| 5684 | | − vset(VT_INT, VT_LOCAL, loc); |
| 5776 | + vseti(VT_LOCAL, loc); |
| 5685 | 5777 | ret.c = vtop->c; |
| 5686 | 5778 | if (!nocode_wanted) |
| 5687 | 5779 | gfunc_param(&gf); |
| 5688 | 5780 | else |
| 5689 | 5781 | vtop--; |
| 5690 | 5782 | } else { |
| 5691 | | − ret.t = s->t; |
| 5783 | + ret.type = s->type; |
| 5692 | 5784 | ret.r2 = VT_CONST; |
| 5693 | 5785 | /* return in register */ |
| 5694 | | − if (is_float(ret.t)) { |
| 5786 | + if (is_float(ret.type.t)) { |
| 5695 | 5787 | ret.r = REG_FRET; |
| 5696 | 5788 | } else { |
| 5697 | | − if ((ret.t & VT_BTYPE) == VT_LLONG) |
| 5789 | + if ((ret.type.t & VT_BTYPE) == VT_LLONG) |
| 5698 | 5790 | ret.r2 = REG_LRET; |
| 5699 | 5791 | ret.r = REG_IRET; |
| 5700 | 5792 | } |
| @@ -5721,7 +5813,7 @@ static void unary(void) · 20 unmodified lines |
| 5721 | 5813 | else |
| 5722 | 5814 | vtop--; |
| 5723 | 5815 | /* return value */ |
| 5724 | | − vsetc(ret.t, ret.r, &ret.c); |
| 5816 | + vsetc(&ret.type, ret.r, &ret.c); |
| 5725 | 5817 | vtop->r2 = ret.r2; |
| 5726 | 5818 | } else { |
| 5727 | 5819 | break; |
| @@ -5790,7 +5882,7 @@ static void eand(void) · 62 unmodified lines |
| 5790 | 5882 | if (tok != TOK_LAND) { |
| 5791 | 5883 | if (t) { |
| 5792 | 5884 | t = gtst(1, t); |
| 5793 | | − vset(VT_INT, VT_JMPI, t); |
| 5885 | + vseti(VT_JMPI, t); |
| 5794 | 5886 | } |
| 5795 | 5887 | break; |
| 5796 | 5888 | } |
| @@ -5810,7 +5902,7 @@ static void eor(void) · 13 unmodified lines |
| 5810 | 5902 | if (tok != TOK_LOR) { |
| 5811 | 5903 | if (t) { |
| 5812 | 5904 | t = gtst(0, t); |
| 5813 | | − vset(VT_INT, VT_JMP, t); |
| 5905 | + vseti(VT_JMP, t); |
| 5814 | 5906 | } |
| 5815 | 5907 | break; |
| 5816 | 5908 | } |
| @@ -5823,8 +5915,9 @@ static void eor(void) · 6 unmodified lines |
| 5823 | 5915 | /* XXX: better constant handling */ |
| 5824 | 5916 | static void expr_eq(void) |
| 5825 | 5917 | { |
| 5826 | | − int tt, u, r1, r2, rc, t1, t2, t, bt1, bt2; |
| 5918 | + int tt, u, r1, r2, rc, t1, t2, bt1, bt2; |
| 5827 | 5919 | SValue sv; |
| 5920 | + CType type, type1, type2; |
| 5828 | 5921 | |
| 5829 | 5922 | if (const_wanted) { |
| 5830 | 5923 | int c1, c; |
| @@ -5849,8 +5942,7 @@ static void expr_eq(void) · 18 unmodified lines |
| 5849 | 5942 | at the top because it is a branch point */ |
| 5850 | 5943 | tt = gtst(1, 0); |
| 5851 | 5944 | gexpr(); |
| 5852 | | − t1 = vtop->t; |
| 5853 | | − bt1 = t1 & VT_BTYPE; |
| 5945 | + type1 = vtop->type; |
| 5854 | 5946 | sv = *vtop; /* save value to handle it later */ |
| 5855 | 5947 | vtop--; /* no vpop so that FP stack is not flushed */ |
| 5856 | 5948 | skip(':'); |
| @@ -5858,49 +5950,52 @@ static void expr_eq(void) · 1 unmodified line |
| 5858 | 5950 | |
| 5859 | 5951 | gsym(tt); |
| 5860 | 5952 | expr_eq(); |
| 5861 | | − t2 = vtop->t; |
| 5953 | + type2 = vtop->type; |
| 5862 | 5954 | |
| 5955 | + t1 = type1.t; |
| 5956 | + bt1 = t1 & VT_BTYPE; |
| 5957 | + t2 = type2.t; |
| 5863 | 5958 | bt2 = t2 & VT_BTYPE; |
| 5864 | 5959 | /* cast operands to correct type according to ISOC rules */ |
| 5865 | 5960 | if (is_float(bt1) || is_float(bt2)) { |
| 5866 | 5961 | if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) { |
| 5867 | | − t = VT_LDOUBLE; |
| 5962 | + type.t = VT_LDOUBLE; |
| 5868 | 5963 | } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) { |
| 5869 | | − t = VT_DOUBLE; |
| 5964 | + type.t = VT_DOUBLE; |
| 5870 | 5965 | } else { |
| 5871 | | − t = VT_FLOAT; |
| 5966 | + type.t = VT_FLOAT; |
| 5872 | 5967 | } |
| 5873 | 5968 | } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) { |
| 5874 | 5969 | /* cast to biggest op */ |
| 5875 | | − t = VT_LLONG; |
| 5970 | + type.t = VT_LLONG; |
| 5876 | 5971 | /* convert to unsigned if it does not fit in a long long */ |
| 5877 | 5972 | if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) || |
| 5878 | 5973 | (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED)) |
| 5879 | | − t |= VT_UNSIGNED; |
| 5974 | + type.t |= VT_UNSIGNED; |
| 5880 | 5975 | } else if (bt1 == VT_PTR || bt2 == VT_PTR) { |
| 5881 | 5976 | /* XXX: test pointer compatibility */ |
| 5882 | | − t = t1; |
| 5977 | + type = type1; |
| 5883 | 5978 | } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) { |
| 5884 | 5979 | /* XXX: test structure compatibility */ |
| 5885 | | − t = t1; |
| 5980 | + type = type1; |
| 5886 | 5981 | } else if (bt1 == VT_VOID || bt2 == VT_VOID) { |
| 5887 | 5982 | /* NOTE: as an extension, we accept void on only one side */ |
| 5888 | | − t = VT_VOID; |
| 5983 | + type.t = VT_VOID; |
| 5889 | 5984 | } else { |
| 5890 | 5985 | /* integer operations */ |
| 5891 | | − t = VT_INT; |
| 5986 | + type.t = VT_INT; |
| 5892 | 5987 | /* convert to unsigned if it does not fit in an integer */ |
| 5893 | 5988 | if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) || |
| 5894 | 5989 | (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) |
| 5895 | | − t |= VT_UNSIGNED; |
| 5990 | + type.t |= VT_UNSIGNED; |
| 5896 | 5991 | } |
| 5897 | 5992 | |
| 5898 | 5993 | /* now we convert second operand */ |
| 5899 | | − gen_cast(t); |
| 5994 | + gen_cast(&type); |
| 5900 | 5995 | rc = RC_INT; |
| 5901 | | − if (is_float(t)) { |
| 5996 | + if (is_float(type.t)) { |
| 5902 | 5997 | rc = RC_FLOAT; |
| 5903 | | − } else if ((t & VT_BTYPE) == VT_LLONG) { |
| 5998 | + } else if ((type.t & VT_BTYPE) == VT_LLONG) { |
| 5904 | 5999 | /* for long longs, we use fixed registers to avoid having |
| 5905 | 6000 | to handle a complicated move */ |
| 5906 | 6001 | rc = RC_IRET; |
| @@ -5912,7 +6007,7 @@ static void expr_eq(void) · 5 unmodified lines |
| 5912 | 6007 | gsym(u); |
| 5913 | 6008 | /* put again first value and cast it */ |
| 5914 | 6009 | *vtop = sv; |
| 5915 | | − gen_cast(t); |
| 6010 | + gen_cast(&type); |
| 5916 | 6011 | r1 = gv(rc); |
| 5917 | 6012 | move_reg(r2, r1); |
| 5918 | 6013 | vtop->r = r2; |
| @@ -5933,32 +6028,30 @@ static void gexpr(void) · 14 unmodified lines |
| 5933 | 6028 | } |
| 5934 | 6029 | |
| 5935 | 6030 | /* parse an expression and return its type without any side effect. */ |
| 5936 | | −static int expr_type(void) |
| 6031 | +static void expr_type(CType *type) |
| 5937 | 6032 | { |
| 5938 | | − int a, t; |
| 6033 | + int a; |
| 5939 | 6034 | |
| 5940 | 6035 | a = nocode_wanted; |
| 5941 | 6036 | nocode_wanted = 1; |
| 5942 | 6037 | gexpr(); |
| 5943 | | − t = vtop->t; |
| 6038 | + *type = vtop->type; |
| 5944 | 6039 | vpop(); |
| 5945 | 6040 | nocode_wanted = a; |
| 5946 | | − return t; |
| 5947 | 6041 | } |
| 5948 | 6042 | |
| 5949 | 6043 | /* parse a unary expression and return its type without any side |
| 5950 | 6044 | effect. */ |
| 5951 | | −static int unary_type(void) |
| 6045 | +static void unary_type(CType *type) |
| 5952 | 6046 | { |
| 5953 | | − int a, t; |
| 6047 | + int a; |
| 5954 | 6048 | |
| 5955 | 6049 | a = nocode_wanted; |
| 5956 | 6050 | nocode_wanted = 1; |
| 5957 | 6051 | unary(); |
| 5958 | | − t = vtop->t; |
| 6052 | + *type = vtop->type; |
| 5959 | 6053 | vpop(); |
| 5960 | 6054 | nocode_wanted = a; |
| 5961 | | − return t; |
| 5962 | 6055 | } |
| 5963 | 6056 | |
| 5964 | 6057 | /* 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 |
| 6068 | 6161 | next(); |
| 6069 | 6162 | if (tok != ';') { |
| 6070 | 6163 | 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; |
| 6073 | 6167 | /* if returning structure, must copy it to implicit |
| 6074 | 6168 | 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); |
| 6076 | 6172 | indir(); |
| 6077 | 6173 | vswap(); |
| 6078 | 6174 | /* copy structure value to pointer */ |
| 6079 | 6175 | vstore(); |
| 6080 | | − } else if (is_float(func_vt)) { |
| 6176 | + } else if (is_float(func_vt.t)) { |
| 6081 | 6177 | gv(RC_FRET); |
| 6082 | 6178 | } else { |
| 6083 | 6179 | 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 |
| 6184 | 6280 | /* since a case is like a label, we must skip it with a jmp */ |
| 6185 | 6281 | b = gjmp(0); |
| 6186 | 6282 | gsym(*case_sym); |
| 6187 | | − vset(VT_INT, case_reg, 0); |
| 6283 | + vseti(case_reg, 0); |
| 6188 | 6284 | vpushi(v1); |
| 6189 | 6285 | if (v1 == v2) { |
| 6190 | 6286 | 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 |
| 6192 | 6288 | } else { |
| 6193 | 6289 | gen_op(TOK_GE); |
| 6194 | 6290 | *case_sym = gtst(1, 0); |
| 6195 | | − vset(VT_INT, case_reg, 0); |
| 6291 | + vseti(case_reg, 0); |
| 6196 | 6292 | vpushi(v2); |
| 6197 | 6293 | gen_op(TOK_LE); |
| 6198 | 6294 | *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 |
| 6217 | 6313 | /* computed goto */ |
| 6218 | 6314 | next(); |
| 6219 | 6315 | gexpr(); |
| 6220 | | − if ((vtop->t & VT_BTYPE) != VT_PTR) |
| 6316 | + if ((vtop->type.t & VT_BTYPE) != VT_PTR) |
| 6221 | 6317 | expect("pointer"); |
| 6222 | 6318 | ggoto(); |
| 6223 | 6319 | } 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 |
| 6271 | 6367 | address. cur_index/cur_field is the pointer to the current |
| 6272 | 6368 | value. 'size_only' is true if only size info is needed (only used |
| 6273 | 6369 | 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, |
| 6275 | 6371 | int *cur_index, Sym **cur_field, |
| 6276 | 6372 | int size_only) |
| 6277 | 6373 | { |
| 6278 | 6374 | Sym *s, *f; |
| 6279 | 6375 | int notfirst, index, align, l; |
| 6376 | + CType type1; |
| 6280 | 6377 | |
| 6281 | 6378 | notfirst = 0; |
| 6282 | 6379 | 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 |
| 6284 | 6381 | |
| 6285 | 6382 | while (tok == '[' || tok == '.') { |
| 6286 | 6383 | if (tok == '[') { |
| 6287 | | − if (!(t & VT_ARRAY)) |
| 6384 | + if (!(type->t & VT_ARRAY)) |
| 6288 | 6385 | expect("array type"); |
| 6289 | | − s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); |
| 6386 | + s = type->ref; |
| 6290 | 6387 | next(); |
| 6291 | 6388 | index = expr_const(); |
| 6292 | 6389 | 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 |
| 6294 | 6391 | skip(']'); |
| 6295 | 6392 | if (!notfirst) |
| 6296 | 6393 | *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); |
| 6299 | 6396 | } else { |
| 6300 | 6397 | next(); |
| 6301 | 6398 | l = tok; |
| 6302 | 6399 | next(); |
| 6303 | 6400 | struct_field: |
| 6304 | | − if ((t & VT_BTYPE) != VT_STRUCT) |
| 6401 | + if ((type->t & VT_BTYPE) != VT_STRUCT) |
| 6305 | 6402 | expect("struct/union type"); |
| 6306 | | − s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 6403 | + s = type->ref; |
| 6307 | 6404 | l |= SYM_FIELD; |
| 6308 | 6405 | f = s->next; |
| 6309 | 6406 | while (f) { |
| @@ -6315,7 +6412,10 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 5 unmodified lines |
| 6315 | 6412 | expect("field"); |
| 6316 | 6413 | if (!notfirst) |
| 6317 | 6414 | *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; |
| 6319 | 6419 | c += f->c; |
| 6320 | 6420 | } |
| 6321 | 6421 | notfirst = 1; |
| @@ -6328,19 +6428,22 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 6 unmodified lines |
| 6328 | 6428 | expect("="); |
| 6329 | 6429 | } |
| 6330 | 6430 | } else { |
| 6331 | | − if (t & VT_ARRAY) { |
| 6431 | + if (type->t & VT_ARRAY) { |
| 6332 | 6432 | 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); |
| 6335 | 6435 | } else { |
| 6336 | 6436 | f = *cur_field; |
| 6337 | 6437 | if (!f) |
| 6338 | 6438 | 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; |
| 6340 | 6443 | c += f->c; |
| 6341 | 6444 | } |
| 6342 | 6445 | } |
| 6343 | | − decl_initializer(t, sec, c, 0, size_only); |
| 6446 | + decl_initializer(type, sec, c, 0, size_only); |
| 6344 | 6447 | } |
| 6345 | 6448 | |
| 6346 | 6449 | #define EXPR_VAL 0 |
| @@ -6348,7 +6451,7 @@ static void decl_designator(int t, Section *sec, unsigned long c, · 1 unmodified line |
| 6348 | 6451 | #define EXPR_ANY 2 |
| 6349 | 6452 | |
| 6350 | 6453 | /* 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, |
| 6352 | 6455 | int v, int expr_type) |
| 6353 | 6456 | { |
| 6354 | 6457 | int saved_global_expr, bt; |
| @@ -6376,8 +6479,8 @@ static void init_putv(int t, Section *sec, unsigned long c, · 21 unmodified lines |
| 6376 | 6479 | if (sec) { |
| 6377 | 6480 | /* XXX: not portable */ |
| 6378 | 6481 | /* 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; |
| 6381 | 6484 | ptr = sec->data + c; |
| 6382 | 6485 | if ((vtop->r & VT_SYM) && |
| 6383 | 6486 | (bt == VT_BYTE || |
| @@ -6411,7 +6514,7 @@ static void init_putv(int t, Section *sec, unsigned long c, · 27 unmodified lines |
| 6411 | 6514 | } |
| 6412 | 6515 | vtop--; |
| 6413 | 6516 | } else { |
| 6414 | | − vset(t, VT_LOCAL, c); |
| 6517 | + vset(type, VT_LOCAL, c); |
| 6415 | 6518 | vswap(); |
| 6416 | 6519 | vstore(); |
| 6417 | 6520 | vpop(); |
| @@ -6419,7 +6522,7 @@ static void init_putv(int t, Section *sec, unsigned long c, · 1 unmodified line |
| 6419 | 6522 | } |
| 6420 | 6523 | |
| 6421 | 6524 | /* 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) |
| 6423 | 6526 | { |
| 6424 | 6527 | GFuncContext gf; |
| 6425 | 6528 | |
| @@ -6431,9 +6534,9 @@ static void init_putz(int t, Section *sec, unsigned long c, int size) · 5 unmodified lines |
| 6431 | 6534 | gfunc_param(&gf); |
| 6432 | 6535 | vpushi(0); |
| 6433 | 6536 | gfunc_param(&gf); |
| 6434 | | − vset(VT_INT, VT_LOCAL, c); |
| 6537 | + vseti(VT_LOCAL, c); |
| 6435 | 6538 | gfunc_param(&gf); |
| 6436 | | − vpush_global_sym(func_old_type, TOK_memset); |
| 6539 | + vpush_global_sym(&func_old_type, TOK_memset); |
| 6437 | 6540 | gfunc_call(&gf); |
| 6438 | 6541 | } |
| 6439 | 6542 | } |
| @@ -6443,18 +6546,19 @@ static void init_putz(int t, Section *sec, unsigned long c, int size) · 3 unmodified lines |
| 6443 | 6546 | allocation. 'first' is true if array '{' must be read (multi |
| 6444 | 6547 | dimension implicit array init handling). 'size_only' is true if |
| 6445 | 6548 | 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, |
| 6447 | 6550 | int first, int size_only) |
| 6448 | 6551 | { |
| 6449 | 6552 | int index, array_length, n, no_oblock, nb, parlevel, i; |
| 6450 | | − int t1, size1, align1, expr_type; |
| 6553 | + int size1, align1, expr_type; |
| 6451 | 6554 | Sym *s, *f; |
| 6555 | + CType *t1; |
| 6452 | 6556 | |
| 6453 | | − if (t & VT_ARRAY) { |
| 6454 | | − s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); |
| 6557 | + if (type->t & VT_ARRAY) { |
| 6558 | + s = type->ref; |
| 6455 | 6559 | n = s->c; |
| 6456 | 6560 | array_length = 0; |
| 6457 | | − t1 = pointed_type(t); |
| 6561 | + t1 = pointed_type(type); |
| 6458 | 6562 | size1 = type_size(t1, &align1); |
| 6459 | 6563 | |
| 6460 | 6564 | no_oblock = 1; |
| @@ -6467,9 +6571,9 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 6 unmodified lines |
| 6467 | 6571 | /* only parse strings here if correct type (otherwise: handle |
| 6468 | 6572 | them as ((w)char *) expressions */ |
| 6469 | 6573 | if ((tok == TOK_LSTR && |
| 6470 | | − (t1 & VT_BTYPE) == VT_INT) || |
| 6574 | + (t1->t & VT_BTYPE) == VT_INT) || |
| 6471 | 6575 | (tok == TOK_STR && |
| 6472 | | − (t1 & VT_BTYPE) == VT_BYTE)) { |
| 6576 | + (t1->t & VT_BTYPE) == VT_BYTE)) { |
| 6473 | 6577 | while (tok == TOK_STR || tok == TOK_LSTR) { |
| 6474 | 6578 | int cstr_len, ch; |
| 6475 | 6579 | CString *cstr; |
| @@ -6517,7 +6621,7 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 41 unmodified lines |
| 6517 | 6621 | } else { |
| 6518 | 6622 | index = 0; |
| 6519 | 6623 | while (tok != '}') { |
| 6520 | | − decl_designator(t, sec, c, &index, NULL, size_only); |
| 6624 | + decl_designator(type, sec, c, &index, NULL, size_only); |
| 6521 | 6625 | if (n >= 0 && index >= n) |
| 6522 | 6626 | error("index too large"); |
| 6523 | 6627 | /* 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 |
| 6549 | 6653 | /* patch type size if needed */ |
| 6550 | 6654 | if (n < 0) |
| 6551 | 6655 | s->c = array_length; |
| 6552 | | − } else if ((t & VT_BTYPE) == VT_STRUCT && tok == '{') { |
| 6656 | + } else if ((type->t & VT_BTYPE) == VT_STRUCT && tok == '{') { |
| 6553 | 6657 | /* XXX: union needs only one init */ |
| 6554 | 6658 | next(); |
| 6555 | | − s = sym_find(((unsigned)t >> VT_STRUCT_SHIFT) | SYM_STRUCT); |
| 6659 | + s = type->ref; |
| 6556 | 6660 | f = s->next; |
| 6557 | 6661 | array_length = 0; |
| 6558 | 6662 | index = 0; |
| 6559 | 6663 | n = s->c; |
| 6560 | 6664 | while (tok != '}') { |
| 6561 | | − decl_designator(t, sec, c, NULL, &f, size_only); |
| 6665 | + decl_designator(type, sec, c, NULL, &f, size_only); |
| 6562 | 6666 | /* fill with zero between fields */ |
| 6563 | 6667 | index = f->c; |
| 6564 | 6668 | if (!size_only && array_length < index) { |
| 6565 | | − init_putz(t, sec, c + array_length, |
| 6669 | + init_putz(type, sec, c + array_length, |
| 6566 | 6670 | index - array_length); |
| 6567 | 6671 | } |
| 6568 | | − index = index + type_size(f->t, &align1); |
| 6672 | + index = index + type_size(&f->type, &align1); |
| 6569 | 6673 | if (index > array_length) |
| 6570 | 6674 | array_length = index; |
| 6571 | 6675 | if (tok == '}') |
| @@ -6575,13 +6679,13 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 3 unmodified lines |
| 6575 | 6679 | } |
| 6576 | 6680 | /* put zeros at the end */ |
| 6577 | 6681 | if (!size_only && array_length < n) { |
| 6578 | | − init_putz(t, sec, c + array_length, |
| 6682 | + init_putz(type, sec, c + array_length, |
| 6579 | 6683 | n - array_length); |
| 6580 | 6684 | } |
| 6581 | 6685 | skip('}'); |
| 6582 | 6686 | } else if (tok == '{') { |
| 6583 | 6687 | next(); |
| 6584 | | − decl_initializer(t, sec, c, first, size_only); |
| 6688 | + decl_initializer(type, sec, c, first, size_only); |
| 6585 | 6689 | skip('}'); |
| 6586 | 6690 | } else if (size_only) { |
| 6587 | 6691 | /* just skip expression */ |
| @@ -6600,7 +6704,7 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 12 unmodified lines |
| 6600 | 6704 | expr_type = EXPR_CONST; |
| 6601 | 6705 | if (!sec) |
| 6602 | 6706 | expr_type = EXPR_ANY; |
| 6603 | | − init_putv(t, sec, c, 0, expr_type); |
| 6707 | + init_putv(type, sec, c, 0, expr_type); |
| 6604 | 6708 | } |
| 6605 | 6709 | } |
| 6606 | 6710 | |
| @@ -6611,7 +6715,7 @@ static void decl_initializer(int t, Section *sec, unsigned long c, · 4 unmodified lines |
| 6611 | 6715 | parsed. If 'v' is zero, then a reference to the new object is put |
| 6612 | 6716 | in the value stack. If 'has_init' is 2, a special parsing is done |
| 6613 | 6717 | 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, |
| 6615 | 6719 | int has_init, int v, int scope) |
| 6616 | 6720 | { |
| 6617 | 6721 | int size, align, addr, data_offset; |
| @@ -6620,7 +6724,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 2 unmodified lines |
| 6620 | 6724 | TokenString init_str; |
| 6621 | 6725 | Section *sec; |
| 6622 | 6726 | |
| 6623 | | − size = type_size(t, &align); |
| 6727 | + size = type_size(type, &align); |
| 6624 | 6728 | /* If unknown size, we must evaluate it before |
| 6625 | 6729 | evaluating initializers because |
| 6626 | 6730 | initializers can generate global data too |
| @@ -6662,13 +6766,13 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 35 unmodified lines |
| 6662 | 6766 | |
| 6663 | 6767 | macro_ptr = init_str.str; |
| 6664 | 6768 | next(); |
| 6665 | | − decl_initializer(t, NULL, 0, 1, 1); |
| 6769 | + decl_initializer(type, NULL, 0, 1, 1); |
| 6666 | 6770 | /* prepare second initializer parsing */ |
| 6667 | 6771 | macro_ptr = init_str.str; |
| 6668 | 6772 | next(); |
| 6669 | 6773 | |
| 6670 | 6774 | /* if still unknown size, error */ |
| 6671 | | − size = type_size(t, &align); |
| 6775 | + size = type_size(type, &align); |
| 6672 | 6776 | if (size < 0) |
| 6673 | 6777 | error("unknown type size"); |
| 6674 | 6778 | } |
| @@ -6677,7 +6781,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 2 unmodified lines |
| 6677 | 6781 | align = ad->aligned; |
| 6678 | 6782 | if ((r & VT_VALMASK) == VT_LOCAL) { |
| 6679 | 6783 | sec = NULL; |
| 6680 | | − if (do_bounds_check && (t & VT_ARRAY)) |
| 6784 | + if (do_bounds_check && (type->t & VT_ARRAY)) |
| 6681 | 6785 | loc--; |
| 6682 | 6786 | #ifdef TCC_TARGET_IL |
| 6683 | 6787 | /* 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 |
| 6691 | 6795 | /* handles bounds */ |
| 6692 | 6796 | /* XXX: currently, since we do only one pass, we cannot track |
| 6693 | 6797 | '&' operators, so we add only arrays */ |
| 6694 | | − if (do_bounds_check && (t & VT_ARRAY)) { |
| 6798 | + if (do_bounds_check && (type->t & VT_ARRAY)) { |
| 6695 | 6799 | unsigned long *bounds_ptr; |
| 6696 | 6800 | /* add padding between regions */ |
| 6697 | 6801 | loc--; |
| @@ -6727,10 +6831,10 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 29 unmodified lines |
| 6727 | 6831 | if (!sec) { |
| 6728 | 6832 | if (v) { |
| 6729 | 6833 | /* local variable */ |
| 6730 | | − sym_push(v, t, r, addr); |
| 6834 | + sym_push(v, type, r, addr); |
| 6731 | 6835 | } else { |
| 6732 | 6836 | /* push local reference */ |
| 6733 | | − vset(t, r, addr); |
| 6837 | + vset(type, r, addr); |
| 6734 | 6838 | } |
| 6735 | 6839 | } else { |
| 6736 | 6840 | Sym *sym; |
| @@ -6741,24 +6845,24 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 4 unmodified lines |
| 6741 | 6845 | sym = sym_find(v); |
| 6742 | 6846 | if (!sym) |
| 6743 | 6847 | goto do_def; |
| 6744 | | − if (!is_compatible_types(sym->t, t)) |
| 6848 | + if (!is_compatible_types(&sym->type, type)) |
| 6745 | 6849 | error("incompatible types for redefinition of '%s'", |
| 6746 | 6850 | get_tok_str(v, NULL)); |
| 6747 | | − if (!(sym->t & VT_EXTERN)) |
| 6851 | + if (!(sym->type.t & VT_EXTERN)) |
| 6748 | 6852 | error("redefinition of '%s'", get_tok_str(v, NULL)); |
| 6749 | | − sym->t &= ~VT_EXTERN; |
| 6853 | + sym->type.t &= ~VT_EXTERN; |
| 6750 | 6854 | } else { |
| 6751 | 6855 | do_def: |
| 6752 | | − sym = sym_push(v, t, r | VT_SYM, 0); |
| 6856 | + sym = sym_push(v, type, r | VT_SYM, 0); |
| 6753 | 6857 | } |
| 6754 | 6858 | put_extern_sym(sym, sec, addr, size); |
| 6755 | 6859 | } else { |
| 6756 | 6860 | CValue cval; |
| 6757 | 6861 | |
| 6758 | 6862 | /* push global reference */ |
| 6759 | | − sym = get_sym_ref(t, sec, addr, size); |
| 6863 | + sym = get_sym_ref(type, sec, addr, size); |
| 6760 | 6864 | cval.ul = 0; |
| 6761 | | − vsetc(t, VT_CONST | VT_SYM, &cval); |
| 6865 | + vsetc(type, VT_CONST | VT_SYM, &cval); |
| 6762 | 6866 | vtop->sym = sym; |
| 6763 | 6867 | } |
| 6764 | 6868 | |
| @@ -6775,7 +6879,7 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 10 unmodified lines |
| 6775 | 6879 | } |
| 6776 | 6880 | } |
| 6777 | 6881 | if (has_init) { |
| 6778 | | − decl_initializer(t, sec, addr, 1, 0); |
| 6882 | + decl_initializer(type, sec, addr, 1, 0); |
| 6779 | 6883 | /* restore parse state if needed */ |
| 6780 | 6884 | if (init_str.str) { |
| 6781 | 6885 | tok_str_free(init_str.str); |
| @@ -6791,7 +6895,7 @@ void put_func_debug(Sym *sym) · 9 unmodified lines |
| 6791 | 6895 | /* stabs info */ |
| 6792 | 6896 | /* XXX: we put here a dummy type */ |
| 6793 | 6897 | snprintf(buf, sizeof(buf), "%s:%c1", |
| 6794 | | − funcname, sym->t & VT_STATIC ? 'f' : 'F'); |
| 6898 | + funcname, sym->type.t & VT_STATIC ? 'f' : 'F'); |
| 6795 | 6899 | put_stabs_r(buf, N_FUN, 0, file->line_num, 0, |
| 6796 | 6900 | cur_text_section, sym->c); |
| 6797 | 6901 | last_ind = 0; |
| @@ -6862,19 +6966,22 @@ void analyse_function(void) · 64 unmodified lines |
| 6862 | 6966 | static void func_decl_list(Sym *func_sym) |
| 6863 | 6967 | { |
| 6864 | 6968 | AttributeDef ad; |
| 6865 | | − int b, v, t; |
| 6969 | + int v; |
| 6866 | 6970 | Sym *s; |
| 6971 | + CType btype, type; |
| 6972 | + |
| 6867 | 6973 | /* parse each declaration */ |
| 6868 | 6974 | while (tok != '{' && tok != ';' && tok != TOK_EOF) { |
| 6869 | | − if (!parse_btype(&b, &ad)) |
| 6975 | + if (!parse_btype(&btype, &ad)) |
| 6870 | 6976 | 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) && |
| 6873 | 6979 | tok == ';') { |
| 6874 | 6980 | /* we accept no variable after */ |
| 6875 | 6981 | } else { |
| 6876 | 6982 | for(;;) { |
| 6877 | | − t = type_decl(&ad, &v, b, TYPE_DIRECT); |
| 6983 | + type = btype; |
| 6984 | + type_decl(&type, &ad, &v, TYPE_DIRECT); |
| 6878 | 6985 | /* find parameter in function parameter list */ |
| 6879 | 6986 | s = func_sym->next; |
| 6880 | 6987 | while (s != NULL) { |
| @@ -6886,10 +6993,10 @@ static void func_decl_list(Sym *func_sym) · 5 unmodified lines |
| 6886 | 6993 | get_tok_str(v, NULL)); |
| 6887 | 6994 | found: |
| 6888 | 6995 | /* 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)) |
| 6890 | 6997 | error("storage class specified for '%s'", get_tok_str(v, NULL)); |
| 6891 | 6998 | /* we can add the type (NOTE: it could be local to the function) */ |
| 6892 | | − s->t = t; |
| 6999 | + s->type = type; |
| 6893 | 7000 | /* accept other parameters */ |
| 6894 | 7001 | if (tok == ',') |
| 6895 | 7002 | next(); |
| @@ -6904,12 +7011,13 @@ static void func_decl_list(Sym *func_sym) · 8 unmodified lines |
| 6904 | 7011 | /* 'l' is VT_LOCAL or VT_CONST to define default storage type */ |
| 6905 | 7012 | static void decl(int l) |
| 6906 | 7013 | { |
| 6907 | | − int t, b, v, has_init, r; |
| 7014 | + int v, has_init, r; |
| 7015 | + CType type, btype; |
| 6908 | 7016 | Sym *sym; |
| 6909 | 7017 | AttributeDef ad; |
| 6910 | 7018 | |
| 6911 | 7019 | while (1) { |
| 6912 | | − if (!parse_btype(&b, &ad)) { |
| 7020 | + if (!parse_btype(&btype, &ad)) { |
| 6913 | 7021 | /* skip redundant ';' */ |
| 6914 | 7022 | /* XXX: find more elegant solution */ |
| 6915 | 7023 | if (tok == ';') { |
| @@ -6920,17 +7028,18 @@ static void decl(int l) · 4 unmodified lines |
| 6920 | 7028 | type. Only accepted when defining global data */ |
| 6921 | 7029 | if (l == VT_LOCAL || tok < TOK_DEFINE) |
| 6922 | 7030 | break; |
| 6923 | | − b = VT_INT; |
| 7031 | + btype.t = VT_INT; |
| 6924 | 7032 | } |
| 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) && |
| 6927 | 7035 | tok == ';') { |
| 6928 | 7036 | /* we accept no variable after */ |
| 6929 | 7037 | next(); |
| 6930 | 7038 | continue; |
| 6931 | 7039 | } |
| 6932 | 7040 | 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); |
| 6934 | 7043 | #if 0 |
| 6935 | 7044 | { |
| 6936 | 7045 | char buf[500]; |
| @@ -6938,10 +7047,10 @@ static void decl(int l) · 1 unmodified line |
| 6938 | 7047 | printf("type = '%s'\n", buf); |
| 6939 | 7048 | } |
| 6940 | 7049 | #endif |
| 6941 | | − if ((t & VT_BTYPE) == VT_FUNC) { |
| 7050 | + if ((type.t & VT_BTYPE) == VT_FUNC) { |
| 6942 | 7051 | /* if old style function prototype, we accept a |
| 6943 | 7052 | declaration list */ |
| 6944 | | − sym = sym_find(((unsigned)t >> VT_STRUCT_SHIFT)); |
| 7053 | + sym = type.ref; |
| 6945 | 7054 | if (sym->c == FUNC_OLD) |
| 6946 | 7055 | func_decl_list(sym); |
| 6947 | 7056 | } |
| @@ -6955,7 +7064,7 @@ static void decl(int l) · 7 unmodified lines |
| 6955 | 7064 | |
| 6956 | 7065 | if (l == VT_LOCAL) |
| 6957 | 7066 | error("cannot use local functions"); |
| 6958 | | − if (!(t & VT_FUNC)) |
| 7067 | + if (!(type.t & VT_FUNC)) |
| 6959 | 7068 | expect("function definition"); |
| 6960 | 7069 | |
| 6961 | 7070 | #ifdef CONFIG_REG_VARS |
| @@ -6998,10 +7107,11 @@ static void decl(int l) · 36 unmodified lines |
| 6998 | 7107 | sym = sym_find(v); |
| 6999 | 7108 | if (sym) { |
| 7000 | 7109 | /* if symbol is already defined, then put complete type */ |
| 7001 | | − sym->t = t; |
| 7110 | + sym->type = type; |
| 7002 | 7111 | } else { |
| 7003 | 7112 | /* 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; |
| 7005 | 7115 | } |
| 7006 | 7116 | /* NOTE: we patch the symbol size later */ |
| 7007 | 7117 | put_extern_sym(sym, cur_text_section, ind, 0); |
| @@ -7012,7 +7122,7 @@ static void decl(int l) · 4 unmodified lines |
| 7012 | 7122 | put_func_debug(sym); |
| 7013 | 7123 | /* push a dummy symbol to enable local sym storage */ |
| 7014 | 7124 | sym_push1(&local_stack, 0, 0, 0); |
| 7015 | | − gfunc_prolog(t); |
| 7125 | + gfunc_prolog(&type); |
| 7016 | 7126 | loc = 0; |
| 7017 | 7127 | rsym = 0; |
| 7018 | 7128 | #ifdef CONFIG_REG_VARS |
| @@ -7049,7 +7159,7 @@ static void decl(int l) · 30 unmodified lines |
| 7049 | 7159 | put_stabn(N_FUN, 0, 0, ind - func_ind); |
| 7050 | 7160 | } |
| 7051 | 7161 | funcname = ""; /* for safety */ |
| 7052 | | − func_vt = VT_VOID; /* for safety */ |
| 7162 | + func_vt.t = VT_VOID; /* for safety */ |
| 7053 | 7163 | ind = 0; /* for safety */ |
| 7054 | 7164 | |
| 7055 | 7165 | #ifdef CONFIG_REG_VARS |
| @@ -7058,30 +7168,31 @@ static void decl(int l) · 2 unmodified lines |
| 7058 | 7168 | #endif |
| 7059 | 7169 | break; |
| 7060 | 7170 | } else { |
| 7061 | | − if (b & VT_TYPEDEF) { |
| 7171 | + if (btype.t & VT_TYPEDEF) { |
| 7062 | 7172 | /* save typedefed type */ |
| 7063 | 7173 | /* 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) { |
| 7066 | 7177 | /* external function definition */ |
| 7067 | | − external_sym(v, t, 0); |
| 7178 | + external_sym(v, &type, 0); |
| 7068 | 7179 | } else { |
| 7069 | 7180 | /* not lvalue if array */ |
| 7070 | 7181 | 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) { |
| 7074 | 7185 | /* external variable */ |
| 7075 | | − external_sym(v, t, r); |
| 7186 | + external_sym(v, &type, r); |
| 7076 | 7187 | } else { |
| 7077 | | − if (t & VT_STATIC) |
| 7188 | + if (type.t & VT_STATIC) |
| 7078 | 7189 | r |= VT_CONST; |
| 7079 | 7190 | else |
| 7080 | 7191 | r |= l; |
| 7081 | 7192 | has_init = (tok == '='); |
| 7082 | 7193 | if (has_init) |
| 7083 | 7194 | next(); |
| 7084 | | − decl_initializer_alloc(t, &ad, r, |
| 7195 | + decl_initializer_alloc(&type, &ad, r, |
| 7085 | 7196 | has_init, v, l); |
| 7086 | 7197 | } |
| 7087 | 7198 | } |
| @@ -7095,29 +7206,12 @@ static void decl(int l) · 7 unmodified lines |
| 7095 | 7206 | } |
| 7096 | 7207 | } |
| 7097 | 7208 | |
| 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 | | − |
| 7114 | 7209 | /* compile the C file opened in 'file'. Return non zero if errors. */ |
| 7115 | 7210 | static int tcc_compile(TCCState *s1) |
| 7116 | 7211 | { |
| 7117 | | − Sym *define_start, *sym; |
| 7212 | + Sym *define_start; |
| 7118 | 7213 | char buf[512]; |
| 7119 | 7214 | volatile int section_sym; |
| 7120 | | − int p; |
| 7121 | 7215 | |
| 7122 | 7216 | #ifdef INC_DEBUG |
| 7123 | 7217 | printf("%s: **** new file\n", file->filename); |
| @@ -7152,13 +7246,15 @@ static int tcc_compile(TCCState *s1) · 28 unmodified lines |
| 7152 | 7246 | ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0, |
| 7153 | 7247 | SHN_ABS, file->filename); |
| 7154 | 7248 | |
| 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 | + |
| 7162 | 7258 | #if 0 |
| 7163 | 7259 | /* define 'void *alloca(unsigned int)' builtin function */ |
| 7164 | 7260 | { |
| @@ -7173,7 +7269,7 @@ static int tcc_compile(TCCState *s1) · 8 unmodified lines |
| 7173 | 7269 | } |
| 7174 | 7270 | #endif |
| 7175 | 7271 | |
| 7176 | | − define_start = define_stack.top; |
| 7272 | + define_start = define_stack; |
| 7177 | 7273 | |
| 7178 | 7274 | if (setjmp(s1->error_jmp_buf) == 0) { |
| 7179 | 7275 | s1->nb_errors = 0; |
| @@ -7222,7 +7318,7 @@ int tcc_compile_string(TCCState *s, const char *str) · 42 unmodified lines |
| 7222 | 7318 | return ret; |
| 7223 | 7319 | } |
| 7224 | 7320 | |
| 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 */ |
| 7226 | 7322 | void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) |
| 7227 | 7323 | { |
| 7228 | 7324 | BufferedFile bf1, *bf = &bf1; |
| @@ -7252,15 +7348,16 @@ void tcc_define_symbol(TCCState *s1, const char *sym, const char *value) · 23 unmodified lines |
| 7252 | 7348 | file = NULL; |
| 7253 | 7349 | } |
| 7254 | 7350 | |
| 7351 | +/* undefine a preprocessor symbol */ |
| 7255 | 7352 | void tcc_undefine_symbol(TCCState *s1, const char *sym) |
| 7256 | 7353 | { |
| 7257 | 7354 | TokenSym *ts; |
| 7258 | 7355 | Sym *s; |
| 7259 | 7356 | ts = tok_alloc(sym, strlen(sym)); |
| 7260 | | − s = sym_find1(&define_stack, tok); |
| 7357 | + s = define_find(ts->tok); |
| 7261 | 7358 | /* undefine symbol by putting an invalid name */ |
| 7262 | 7359 | if (s) |
| 7263 | | − sym_undef(&define_stack, s); |
| 7360 | + define_undef(s); |
| 7264 | 7361 | } |
| 7265 | 7362 | |
| 7266 | 7363 | #include "tccelf.c" |
| @@ -7387,6 +7484,8 @@ static void rt_printline(unsigned long wanted_pc) · 120 unmodified lines |
| 7387 | 7484 | fprintf(stderr, "\n"); |
| 7388 | 7485 | } |
| 7389 | 7486 | |
| 7487 | +#ifndef WIN32 |
| 7488 | + |
| 7390 | 7489 | #ifdef __i386__ |
| 7391 | 7490 | |
| 7392 | 7491 | #ifndef EIP |
| @@ -7417,7 +7516,7 @@ static int rt_get_caller_pc(unsigned long *paddr, · 24 unmodified lines |
| 7417 | 7516 | } |
| 7418 | 7517 | } |
| 7419 | 7518 | #else |
| 7420 | | −#error add corresponding function |
| 7519 | +#error add arch specific rt_get_caller_pc() |
| 7421 | 7520 | #endif |
| 7422 | 7521 | |
| 7423 | 7522 | /* emit a run time error at position 'pc' */ |
| @@ -7444,7 +7543,6 @@ void rt_error(struct ucontext *uc, const char *fmt, ...) · 20 unmodified lines |
| 7444 | 7543 | va_end(ap); |
| 7445 | 7544 | } |
| 7446 | 7545 | |
| 7447 | | −#ifndef WIN32 |
| 7448 | 7546 | /* signal handler for fatal errors */ |
| 7449 | 7547 | static void sig_error(int signum, siginfo_t *siginf, void *puc) |
| 7450 | 7548 | { |
| @@ -7594,10 +7692,10 @@ TCCState *tcc_new(void) · 143 unmodified lines |
| 7594 | 7692 | |
| 7595 | 7693 | /* we add dummy defines for some special macros to speed up tests |
| 7596 | 7694 | 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); |
| 7601 | 7699 | |
| 7602 | 7700 | /* standard defines */ |
| 7603 | 7701 | tcc_define_symbol(s, "__STDC__", NULL); |
| @@ -7889,6 +7987,19 @@ int tcc_set_output_type(TCCState *s, int output_type) · 285 unmodified lines |
| 7889 | 7987 | |
| 7890 | 7988 | #if !defined(LIBTCC) |
| 7891 | 7989 | |
| 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 | + |
| 7892 | 8003 | void help(void) |
| 7893 | 8004 | { |
| 7894 | 8005 | 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 |
| 7930 | 8041 | char **files; |
| 7931 | 8042 | int nb_files, nb_libraries, nb_objfiles, dminus, ret; |
| 7932 | 8043 | char objfilename[1024]; |
| 8044 | + int64_t start_time = 0; |
| 7933 | 8045 | |
| 7934 | 8046 | s = tcc_new(); |
| 7935 | 8047 | output_type = TCC_OUTPUT_MEMORY; |
| @@ -8061,6 +8173,10 @@ int main(int argc, char **argv) · 125 unmodified lines |
| 8061 | 8173 | outfile = objfilename; |
| 8062 | 8174 | } |
| 8063 | 8175 | |
| 8176 | + if (do_bench) { |
| 8177 | + start_time = getclock_us(); |
| 8178 | + } |
| 8179 | + |
| 8064 | 8180 | tcc_set_output_type(s, output_type); |
| 8065 | 8181 | |
| 8066 | 8182 | /* compile or add each files or library */ |
| @@ -8083,8 +8199,16 @@ int main(int argc, char **argv) · 16 unmodified lines |
| 8083 | 8199 | tcc_free(files); |
| 8084 | 8200 | |
| 8085 | 8201 | 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); |
| 8088 | 8212 | } |
| 8089 | 8213 | |
| 8090 | 8214 | if (s->output_type != TCC_OUTPUT_MEMORY) { |