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