Unified · Split
| @@ -132,7 +132,7 @@ void gsym(int t) · 131 unmodified lines | ||
| 132 | 132 | #define psym oad |
| 133 | 133 | |
| 134 | 134 | /* instruction + 4 bytes data. Return the address of the data */ |
| 135 | −int oad(int c, int s) | |
| 135 | +static int oad(int c, int s) | |
| 136 | 136 | { |
| 137 | 137 | int ind1; |
| 138 | 138 | |
| @@ -147,26 +147,22 @@ int oad(int c, int s) · 8 unmodified lines | ||
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /* output constant with relocation if 'r & VT_SYM' is true */ |
| 150 | −void gen_addr32(int r, int c) | |
| 150 | +static void gen_addr32(int r, Sym *sym, int c) | |
| 151 | 151 | { |
| 152 | − if (!(r & VT_SYM)) { | |
| 153 | − gen_le32(c); | |
| 154 | − } else { | |
| 155 | − greloc(cur_text_section, | |
| 156 | − (Sym *)c, ind, R_386_32); | |
| 157 | − gen_le32(0); | |
| 158 | − } | |
| 152 | + if (r & VT_SYM) | |
| 153 | + greloc(cur_text_section, sym, ind, R_386_32); | |
| 154 | + gen_le32(c); | |
| 159 | 155 | } |
| 160 | 156 | |
| 161 | 157 | /* generate a modrm reference. 'op_reg' contains the addtionnal 3 |
| 162 | 158 | opcode bits */ |
| 163 | −void gen_modrm(int op_reg, int r, int c) | |
| 159 | +static void gen_modrm(int op_reg, int r, Sym *sym, int c) | |
| 164 | 160 | { |
| 165 | 161 | op_reg = op_reg << 3; |
| 166 | 162 | if ((r & VT_VALMASK) == VT_CONST) { |
| 167 | 163 | /* constant memory reference */ |
| 168 | 164 | o(0x05 | op_reg); |
| 169 | − gen_addr32(r, c); | |
| 165 | + gen_addr32(r, sym, c); | |
| 170 | 166 | } else if ((r & VT_VALMASK) == VT_LOCAL) { |
| 171 | 167 | /* currently, we use only ebp as base */ |
| 172 | 168 | if (c == (char)c) { |
| @@ -221,14 +217,14 @@ void load(int r, SValue *sv) · 48 unmodified lines | ||
| 221 | 217 | } else { |
| 222 | 218 | o(0x8b); /* movl */ |
| 223 | 219 | } |
| 224 | − gen_modrm(r, fr, fc); | |
| 220 | + gen_modrm(r, fr, sv->sym, fc); | |
| 225 | 221 | } else { |
| 226 | 222 | if (v == VT_CONST) { |
| 227 | 223 | o(0xb8 + r); /* mov $xx, r */ |
| 228 | − gen_addr32(fr, fc); | |
| 224 | + gen_addr32(fr, sv->sym, fc); | |
| 229 | 225 | } else if (v == VT_LOCAL) { |
| 230 | 226 | o(0x8d); /* lea xxx(%ebp), r */ |
| 231 | − gen_modrm(r, VT_LOCAL, fc); | |
| 227 | + gen_modrm(r, VT_LOCAL, sv->sym, fc); | |
| 232 | 228 | } else if (v == VT_CMP) { |
| 233 | 229 | oad(0xb8 + r, 0); /* mov $0, r */ |
| 234 | 230 | o(0x0f); /* setxx %br */ |
| @@ -278,7 +274,7 @@ void store(int r, SValue *v) · 43 unmodified lines | ||
| 278 | 274 | if (fr == VT_CONST || |
| 279 | 275 | fr == VT_LOCAL || |
| 280 | 276 | (v->r & VT_LVAL)) { |
| 281 | − gen_modrm(r, v->r, fc); | |
| 277 | + gen_modrm(r, v->r, v->sym, fc); | |
| 282 | 278 | } else if (fr != r) { |
| 283 | 279 | o(0xc0 + fr + r * 8); /* mov r, fr */ |
| 284 | 280 | } |
| @@ -362,9 +358,9 @@ void gfunc_call(GFuncContext *c) · 77 unmodified lines | ||
| 362 | 358 | /* constant case */ |
| 363 | 359 | if (vtop->r & VT_SYM) { |
| 364 | 360 | /* relocation case */ |
| 365 | − greloc(cur_text_section, vtop->c.sym, | |
| 361 | + greloc(cur_text_section, vtop->sym, | |
| 366 | 362 | ind + 1, R_386_PC32); |
| 367 | − oad(0xe8, -4); | |
| 363 | + oad(0xe8, vtop->c.ul - 4); | |
| 368 | 364 | } else { |
| 369 | 365 | oad(0xe8, vtop->c.ul - ind - 5); |
| 370 | 366 | } |
| @@ -443,7 +439,7 @@ void gfunc_epilog(void) · 72 unmodified lines | ||
| 443 | 439 | greloc(cur_text_section, sym_data, |
| 444 | 440 | ind + 1, R_386_32); |
| 445 | 441 | oad(0xb8, 0); /* mov %eax, xxx */ |
| 446 | − sym = external_sym(TOK___bound_local_new, func_old_type, 0); | |
| 442 | + sym = external_global_sym(TOK___bound_local_new, func_old_type, 0); | |
| 447 | 443 | greloc(cur_text_section, sym, |
| 448 | 444 | ind + 1, R_386_PC32); |
| 449 | 445 | oad(0xe8, -4); |
| @@ -453,7 +449,7 @@ void gfunc_epilog(void) · 3 unmodified lines | ||
| 453 | 449 | greloc(cur_text_section, sym_data, |
| 454 | 450 | ind + 1, R_386_32); |
| 455 | 451 | oad(0xb8, 0); /* mov %eax, xxx */ |
| 456 | − sym = external_sym(TOK___bound_local_delete, func_old_type, 0); | |
| 452 | + sym = external_global_sym(TOK___bound_local_delete, func_old_type, 0); | |
| 457 | 453 | greloc(cur_text_section, sym, |
| 458 | 454 | ind + 1, R_386_PC32); |
| 459 | 455 | oad(0xe8, -4); |
| @@ -766,7 +762,7 @@ void gen_opf(int op) · 306 unmodified lines | ||
| 766 | 762 | o(0xdc); |
| 767 | 763 | else |
| 768 | 764 | o(0xd8); |
| 769 | − gen_modrm(a, r, fc); | |
| 765 | + gen_modrm(a, r, vtop->sym, fc); | |
| 770 | 766 | } |
| 771 | 767 | vtop--; |
| 772 | 768 | } |
| @@ -816,8 +812,8 @@ void gen_cvt_ftoi(int t) · 43 unmodified lines | ||
| 816 | 812 | size = 4; |
| 817 | 813 | |
| 818 | 814 | o(0x2dd9); /* ldcw xxx */ |
| 819 | − sym = external_sym(TOK___tcc_int_fpu_control, | |
| 820 | − VT_SHORT | VT_UNSIGNED, VT_LVAL); | |
| 815 | + sym = external_global_sym(TOK___tcc_int_fpu_control, | |
| 816 | + VT_SHORT | VT_UNSIGNED, VT_LVAL); | |
| 821 | 817 | greloc(cur_text_section, sym, |
| 822 | 818 | ind, R_386_32); |
| 823 | 819 | gen_le32(0); |
| @@ -829,8 +825,8 @@ void gen_cvt_ftoi(int t) · 5 unmodified lines | ||
| 829 | 825 | o(0x3cdf); /* fistpll */ |
| 830 | 826 | o(0x24); |
| 831 | 827 | o(0x2dd9); /* ldcw xxx */ |
| 832 | − sym = external_sym(TOK___tcc_fpu_control, | |
| 833 | − VT_SHORT | VT_UNSIGNED, VT_LVAL); | |
| 828 | + sym = external_global_sym(TOK___tcc_fpu_control, | |
| 829 | + VT_SHORT | VT_UNSIGNED, VT_LVAL); | |
| 834 | 830 | greloc(cur_text_section, sym, |
| 835 | 831 | ind, R_386_32); |
| 836 | 832 | gen_le32(0); |
| @@ -871,7 +867,7 @@ void gen_bounded_ptr_add(void) · 34 unmodified lines | ||
| 871 | 867 | vtop -= 2; |
| 872 | 868 | save_regs(0); |
| 873 | 869 | /* do a fast function call */ |
| 874 | − sym = external_sym(TOK___bound_ptr_add, func_old_type, 0); | |
| 870 | + sym = external_global_sym(TOK___bound_ptr_add, func_old_type, 0); | |
| 875 | 871 | greloc(cur_text_section, sym, |
| 876 | 872 | ind + 1, R_386_PC32); |
| 877 | 873 | oad(0xe8, -4); |
| @@ -917,7 +913,7 @@ void gen_bounded_ptr_deref(void) · 39 unmodified lines | ||
| 917 | 913 | /* patch relocation */ |
| 918 | 914 | /* XXX: find a better solution ? */ |
| 919 | 915 | rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul); |
| 920 | − sym = external_sym(func, func_old_type, 0); | |
| 916 | + sym = external_global_sym(func, func_old_type, 0); | |
| 921 | 917 | if (!sym->c) |
| 922 | 918 | put_extern_sym(sym, NULL, 0, 0); |
| 923 | 919 | rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info)); |
| @@ -100,7 +100,6 @@ typedef union CValue { · 99 unmodified lines | ||
| 100 | 100 | long long ll; |
| 101 | 101 | unsigned long long ull; |
| 102 | 102 | struct CString *cstr; |
| 103 | − struct Sym *sym; | |
| 104 | 103 | void *ptr; |
| 105 | 104 | int tab[1]; |
| 106 | 105 | } CValue; |
| @@ -112,6 +111,7 @@ typedef struct SValue { · 5 unmodified lines | ||
| 112 | 111 | unsigned short r2; /* second register, used for 'long long' |
| 113 | 112 | type. If not used, set to VT_CONST */ |
| 114 | 113 | CValue c; /* constant, if VT_CONST */ |
| 114 | + struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */ | |
| 115 | 115 | } SValue; |
| 116 | 116 | |
| 117 | 117 | /* symbol management */ |
| @@ -548,7 +548,7 @@ static char *pstrcat(char *buf, int buf_size, const char *s); · 430 unmodified lines | ||
| 548 | 548 | void sum(int l); |
| 549 | 549 | void next(void); |
| 550 | 550 | void next_nomacro(void); |
| 551 | −int expr_const(void); | |
| 551 | +static int expr_const(void); | |
| 552 | 552 | void expr_eq(void); |
| 553 | 553 | void gexpr(void); |
| 554 | 554 | void decl(int l); |
| @@ -2895,8 +2895,9 @@ static void vpush_ref(int t, Section *sec, unsigned long offset, unsigned long s · 2340 unmodified lines | ||
| 2895 | 2895 | { |
| 2896 | 2896 | CValue cval; |
| 2897 | 2897 | |
| 2898 | − cval.sym = get_sym_ref(t, sec, offset, size); | |
| 2898 | + cval.ul = 0; | |
| 2899 | 2899 | vsetc(t, VT_CONST | VT_SYM, &cval); |
| 2900 | + vtop->sym = get_sym_ref(t, sec, offset, size); | |
| 2900 | 2901 | } |
| 2901 | 2902 | |
| 2902 | 2903 | /* define a new external reference to a symbol 'v' of type 'u' */ |
| @@ -2934,8 +2935,9 @@ static void vpush_global_sym(int t, int v) · 31 unmodified lines | ||
| 2934 | 2935 | CValue cval; |
| 2935 | 2936 | |
| 2936 | 2937 | sym = external_global_sym(v, t, 0); |
| 2937 | − cval.sym = sym; | |
| 2938 | + cval.ul = 0; | |
| 2938 | 2939 | vsetc(t, VT_CONST | VT_SYM, &cval); |
| 2940 | + vtop->sym = sym; | |
| 2939 | 2941 | } |
| 2940 | 2942 | |
| 2941 | 2943 | void vset(int t, int r, int v) |
| @@ -3160,7 +3162,8 @@ int gv(int rc) · 218 unmodified lines | ||
| 3160 | 3162 | ptr[i] = vtop->c.tab[i]; |
| 3161 | 3163 | sym = get_sym_ref(vtop->t, data_section, offset, size << 2); |
| 3162 | 3164 | vtop->r |= VT_LVAL | VT_SYM; |
| 3163 | − vtop->c.sym = sym; | |
| 3165 | + vtop->sym = sym; | |
| 3166 | + vtop->c.ul = 0; | |
| 3164 | 3167 | } |
| 3165 | 3168 | #ifdef CONFIG_TCC_BCHECK |
| 3166 | 3169 | if (vtop->r & VT_MUSTBOUND) |
| @@ -3675,6 +3678,14 @@ void gen_opic(int op) · 508 unmodified lines | ||
| 3675 | 3678 | op = TOK_SHR; |
| 3676 | 3679 | } |
| 3677 | 3680 | goto general_case; |
| 3681 | + } else if (c2 && (op == '+' || op == '-') && | |
| 3682 | + (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == | |
| 3683 | + (VT_CONST | VT_SYM)) { | |
| 3684 | + /* symbol + constant case */ | |
| 3685 | + if (op == '-') | |
| 3686 | + fc = -fc; | |
| 3687 | + vtop--; | |
| 3688 | + vtop->c.i += fc; | |
| 3678 | 3689 | } else { |
| 3679 | 3690 | general_case: |
| 3680 | 3691 | /* call low level op generator */ |
| @@ -5189,8 +5200,10 @@ void unary(void) · 1508 unmodified lines | ||
| 5189 | 5200 | } |
| 5190 | 5201 | vset(s->t, s->r, s->c); |
| 5191 | 5202 | /* if forward reference, we must point to s */ |
| 5192 | − if (vtop->r & VT_SYM) | |
| 5193 | − vtop->c.sym = s; | |
| 5203 | + if (vtop->r & VT_SYM) { | |
| 5204 | + vtop->sym = s; | |
| 5205 | + vtop->c.ul = 0; | |
| 5206 | + } | |
| 5194 | 5207 | } |
| 5195 | 5208 | } |
| 5196 | 5209 | |
| @@ -5273,7 +5286,7 @@ void unary(void) · 76 unmodified lines | ||
| 5273 | 5286 | tok_str_new(&str); |
| 5274 | 5287 | parlevel = 0; |
| 5275 | 5288 | while ((parlevel > 0 || (tok != ')' && tok != ',')) && |
| 5276 | − tok != -1) { | |
| 5289 | + tok != TOK_EOF) { | |
| 5277 | 5290 | if (tok == '(') |
| 5278 | 5291 | parlevel++; |
| 5279 | 5292 | else if (tok == ')') |
| @@ -5509,23 +5522,23 @@ void gexpr(void) · 229 unmodified lines | ||
| 5509 | 5522 | } |
| 5510 | 5523 | } |
| 5511 | 5524 | |
| 5512 | −/* parse a constant expression and return value in vtop */ | |
| 5513 | −void expr_const1(void) | |
| 5525 | +/* parse a constant expression and return value in vtop. */ | |
| 5526 | +static void expr_const1(void) | |
| 5514 | 5527 | { |
| 5515 | 5528 | int a; |
| 5516 | 5529 | a = const_wanted; |
| 5517 | 5530 | const_wanted = 1; |
| 5518 | 5531 | expr_eq(); |
| 5519 | − if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST) | |
| 5520 | − expect("constant"); | |
| 5521 | 5532 | const_wanted = a; |
| 5522 | 5533 | } |
| 5523 | 5534 | |
| 5524 | −/* parse an integer constant and return its value */ | |
| 5525 | −int expr_const(void) | |
| 5535 | +/* parse an integer constant and return its value. */ | |
| 5536 | +static int expr_const(void) | |
| 5526 | 5537 | { |
| 5527 | 5538 | int c; |
| 5528 | 5539 | expr_const1(); |
| 5540 | + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST) | |
| 5541 | + expect("constant expression"); | |
| 5529 | 5542 | c = vtop->c.i; |
| 5530 | 5543 | vpop(); |
| 5531 | 5544 | return c; |
| @@ -5899,6 +5912,9 @@ static void init_putv(int t, Section *sec, unsigned long c, · 367 unmodified lines | ||
| 5899 | 5912 | global_expr = 1; |
| 5900 | 5913 | expr_const1(); |
| 5901 | 5914 | global_expr = saved_global_expr; |
| 5915 | + /* NOTE: symbols are accepted */ | |
| 5916 | + if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST) | |
| 5917 | + error("initializer element is not constant"); | |
| 5902 | 5918 | break; |
| 5903 | 5919 | case EXPR_ANY: |
| 5904 | 5920 | expr_eq(); |
| @@ -5936,11 +5952,9 @@ static void init_putv(int t, Section *sec, unsigned long c, · 31 unmodified lines | ||
| 5936 | 5952 | break; |
| 5937 | 5953 | default: |
| 5938 | 5954 | if (vtop->r & VT_SYM) { |
| 5939 | − greloc(sec, vtop->c.sym, c, R_DATA_32); | |
| 5940 | − *(int *)ptr = 0; | |
| 5941 | − } else { | |
| 5942 | − *(int *)ptr = vtop->c.i; | |
| 5955 | + greloc(sec, vtop->sym, c, R_DATA_32); | |
| 5943 | 5956 | } |
| 5957 | + *(int *)ptr = vtop->c.i; | |
| 5944 | 5958 | break; |
| 5945 | 5959 | } |
| 5946 | 5960 | vtop--; |
| @@ -6284,8 +6298,9 @@ static void decl_initializer_alloc(int t, AttributeDef *ad, int r, · 337 unmodified lines | ||
| 6284 | 6298 | |
| 6285 | 6299 | /* push global reference */ |
| 6286 | 6300 | sym = get_sym_ref(t, sec, addr, size); |
| 6287 | − cval.sym = sym; | |
| 6301 | + cval.ul = 0; | |
| 6288 | 6302 | vsetc(t, VT_CONST | VT_SYM, &cval); |
| 6303 | + vtop->sym = sym; | |
| 6289 | 6304 | } |
| 6290 | 6305 | |
| 6291 | 6306 | /* handles bounds now because the symbol must be defined |