| @@ -21,11 +21,11 @@ · 20 unmodified lines |
| 21 | /* number of available registers */ | 21 | /* number of available registers */ |
| 22 | #define NB_REGS 4 | 22 | #define NB_REGS 4 |
| 23 | | 23 | |
| 24 | #define NB_REG_CLASSES 2 | | |
| 25 | | | |
| 26 | /* a register can belong to several classes */ | 24 | /* a register can belong to several classes */ |
| 27 | #define REG_CLASS_INT 0x0001 | 25 | #define RC_INT 0x0001 /* generic integer register */ |
| 28 | #define REG_CLASS_FLOAT 0x0002 | 26 | #define RC_FLOAT 0x0002 /* generic float register */ |
| | 27 | #define RC_IRET 0x0004 /* function returned integer register */ |
| | 28 | #define RC_FRET 0x0008 /* function returned float register */ |
| 29 | | 29 | |
| 30 | /* pretty names for the registers */ | 30 | /* pretty names for the registers */ |
| 31 | enum { | 31 | enum { |
| @@ -36,16 +36,15 @@ enum { · 4 unmodified lines |
| 36 | }; | 36 | }; |
| 37 | | 37 | |
| 38 | int reg_classes[NB_REGS] = { | 38 | int reg_classes[NB_REGS] = { |
| 39 | REG_CLASS_INT, /* eax */ | 39 | /* eax */ RC_INT | RC_IRET, |
| 40 | REG_CLASS_INT, /* ecx */ | 40 | /* ecx */ RC_INT, |
| 41 | REG_CLASS_INT, /* edx */ | 41 | /* edx */ RC_INT, |
| 42 | REG_CLASS_FLOAT, /* st0 */ | 42 | /* st0 */ RC_FLOAT | RC_FRET, |
| 43 | }; | 43 | }; |
| 44 | | 44 | |
| 45 | /* integer return register for functions */ | 45 | /* return registers for function */ |
| 46 | #define FUNC_RET_REG 0 | 46 | #define REG_IRET REG_EAX |
| 47 | /* float return register for functions */ | 47 | #define REG_FRET REG_ST0 |
| 48 | #define FUNC_RET_FREG 3 | | |
| 49 | | 48 | |
| 50 | /* defined if function parameters must be evaluated in reverse order */ | 49 | /* defined if function parameters must be evaluated in reverse order */ |
| 51 | #define INVERT_FUNC_PARAMS | 50 | #define INVERT_FUNC_PARAMS |
| @@ -89,40 +88,17 @@ void gen_le32(int c) · 37 unmodified lines |
| 89 | g(c >> 24); | 88 | g(c >> 24); |
| 90 | } | 89 | } |
| 91 | | 90 | |
| 92 | /* add a new relocation entry to symbol 's' */ | 91 | /* patch relocation entry with value 'val' */ |
| 93 | void greloc(Sym *s, int addr, int type) | 92 | void greloc_patch1(Reloc *p, int val) |
| 94 | { | | |
| 95 | Reloc *p; | | |
| 96 | p = malloc(sizeof(Reloc)); | | |
| 97 | if (!p) | | |
| 98 | error("memory full"); | | |
| 99 | p->type = type; | | |
| 100 | p->addr = addr; | | |
| 101 | p->next = (Reloc *)s->c; | | |
| 102 | s->c = (int)p; | | |
| 103 | } | | |
| 104 | | | |
| 105 | /* patch each relocation entry with value 'val' */ | | |
| 106 | void greloc_patch(Sym *s, int val) | | |
| 107 | { | 93 | { |
| 108 | Reloc *p, *p1; | 94 | switch(p->type) { |
| 109 | | 95 | case RELOC_ADDR32: |
| 110 | p = (Reloc *)s->c; | 96 | *(int *)p->addr = val; |
| 111 | while (p != NULL) { | 97 | break; |
| 112 | p1 = p->next; | 98 | case RELOC_REL32: |
| 113 | switch(p->type) { | 99 | *(int *)p->addr = val - p->addr - 4; |
| 114 | case RELOC_ADDR32: | 100 | break; |
| 115 | *(int *)p->addr = val; | | |
| 116 | break; | | |
| 117 | case RELOC_REL32: | | |
| 118 | *(int *)p->addr = val - p->addr - 4; | | |
| 119 | break; | | |
| 120 | } | | |
| 121 | free(p); | | |
| 122 | p = p1; | | |
| 123 | } | 101 | } |
| 124 | s->c = val; | | |
| 125 | s->r &= ~VT_FORWARD; | | |
| 126 | } | 102 | } |
| 127 | | 103 | |
| 128 | /* output a symbol and patch all calls to it */ | 104 | /* output a symbol and patch all calls to it */ |
| @@ -166,6 +142,30 @@ void gen_addr32(int r, int c) · 37 unmodified lines |
| 166 | } | 142 | } |
| 167 | } | 143 | } |
| 168 | | 144 | |
| | 145 | /* generate a modrm reference. 'op_reg' contains the addtionnal 3 |
| | 146 | opcode bits */ |
| | 147 | void gen_modrm(int op_reg, int r, int c) |
| | 148 | { |
| | 149 | op_reg = op_reg << 3; |
| | 150 | if ((r & VT_VALMASK) == VT_CONST) { |
| | 151 | /* constant memory reference */ |
| | 152 | o(0x05 | op_reg); |
| | 153 | gen_addr32(r, c); |
| | 154 | } else if ((r & VT_VALMASK) == VT_LOCAL) { |
| | 155 | /* currently, we use only ebp as base */ |
| | 156 | if (c == (char)c) { |
| | 157 | /* short reference */ |
| | 158 | o(0x45 | op_reg); |
| | 159 | g(c); |
| | 160 | } else { |
| | 161 | oad(0x85 | op_reg, c); |
| | 162 | } |
| | 163 | } else { |
| | 164 | g(0x00 | op_reg | (r & VT_VALMASK)); |
| | 165 | } |
| | 166 | } |
| | 167 | |
| | 168 | |
| 169 | /* load 'r' from value 'sv' */ | 169 | /* load 'r' from value 'sv' */ |
| 170 | void load(int r, SValue *sv) | 170 | void load(int r, SValue *sv) |
| 171 | { | 171 | { |
| @@ -183,7 +183,7 @@ void load(int r, SValue *sv) · 11 unmodified lines |
| 183 | v1.r = VT_LOCAL | VT_LVAL; | 183 | v1.r = VT_LOCAL | VT_LVAL; |
| 184 | v1.c.ul = fc; | 184 | v1.c.ul = fc; |
| 185 | load(r, &v1); | 185 | load(r, &v1); |
| 186 | v = r; | 186 | fr = r; |
| 187 | } | 187 | } |
| 188 | if ((ft & VT_BTYPE) == VT_FLOAT) { | 188 | if ((ft & VT_BTYPE) == VT_FLOAT) { |
| 189 | o(0xd9); /* flds */ | 189 | o(0xd9); /* flds */ |
| @@ -204,22 +204,14 @@ void load(int r, SValue *sv) · 14 unmodified lines |
| 204 | o(0xb70f); /* movzwl */ | 204 | o(0xb70f); /* movzwl */ |
| 205 | else | 205 | else |
| 206 | o(0x8b); /* movl */ | 206 | o(0x8b); /* movl */ |
| 207 | | 207 | gen_modrm(r, fr, fc); |
| 208 | if (v == VT_CONST) { | | |
| 209 | o(0x05 + r * 8); /* 0xXX, r */ | | |
| 210 | gen_addr32(fr, fc); | | |
| 211 | } else if (v == VT_LOCAL) { | | |
| 212 | oad(0x85 + r * 8, fc); /* xx(%ebp), r */ | | |
| 213 | } else { | | |
| 214 | g(0x00 + r * 8 + v); /* (v), r */ | | |
| 215 | } | | |
| 216 | } else { | 208 | } else { |
| 217 | if (v == VT_CONST) { | 209 | if (v == VT_CONST) { |
| 218 | o(0xb8 + r); /* mov $xx, r */ | 210 | o(0xb8 + r); /* mov $xx, r */ |
| 219 | gen_addr32(fr, fc); | 211 | gen_addr32(fr, fc); |
| 220 | } else if (v == VT_LOCAL) { | 212 | } else if (v == VT_LOCAL) { |
| 221 | o(0x8d); | 213 | o(0x8d); /* lea xxx(%ebp), r */ |
| 222 | oad(0x85 + r * 8, fc); /* lea xxx(%ebp), r */ | 214 | gen_modrm(r, VT_LOCAL, fc); |
| 223 | } else if (v == VT_CMP) { | 215 | } else if (v == VT_CMP) { |
| 224 | oad(0xb8 + r, 0); /* mov $0, r */ | 216 | oad(0xb8 + r, 0); /* mov $0, r */ |
| 225 | o(0x0f); /* setxx %br */ | 217 | o(0x0f); /* setxx %br */ |
| @@ -247,8 +239,7 @@ void store(int r, SValue *v) · 21 unmodified lines |
| 247 | fc = v->c.ul; | 239 | fc = v->c.ul; |
| 248 | fr = v->r & VT_VALMASK; | 240 | fr = v->r & VT_VALMASK; |
| 249 | bt = ft & VT_BTYPE; | 241 | bt = ft & VT_BTYPE; |
| 250 | /* XXX: incorrect if reg to reg */ | 242 | /* XXX: incorrect if float reg to reg */ |
| 251 | /* XXX: should not flush float stack */ | | |
| 252 | if (bt == VT_FLOAT) { | 243 | if (bt == VT_FLOAT) { |
| 253 | o(0xd9); /* fsts */ | 244 | o(0xd9); /* fsts */ |
| 254 | r = 2; | 245 | r = 2; |
| @@ -267,13 +258,10 @@ void store(int r, SValue *v) · 12 unmodified lines |
| 267 | else | 258 | else |
| 268 | o(0x89); | 259 | o(0x89); |
| 269 | } | 260 | } |
| 270 | if (fr == VT_CONST) { | 261 | if (fr == VT_CONST || |
| 271 | o(0x05 + r * 8); /* mov r,xxx */ | 262 | fr == VT_LOCAL || |
| 272 | gen_addr32(v->r, fc); | 263 | (v->r & VT_LVAL)) { |
| 273 | } else if (fr == VT_LOCAL) { | 264 | gen_modrm(r, v->r, fc); |
| 274 | oad(0x85 + r * 8, fc); /* mov r,xxx(%ebp) */ | | |
| 275 | } else if (v->r & VT_LVAL) { | | |
| 276 | g(fr + r * 8); /* mov r, (fr) */ | | |
| 277 | } else if (fr != r) { | 265 | } else if (fr != r) { |
| 278 | o(0xc0 + fr + r * 8); /* mov r, fr */ | 266 | o(0xc0 + fr + r * 8); /* mov r, fr */ |
| 279 | } | 267 | } |
| @@ -298,7 +286,7 @@ void gfunc_param(GFuncContext *c) · 18 unmodified lines |
| 298 | /* allocate the necessary size on stack */ | 286 | /* allocate the necessary size on stack */ |
| 299 | oad(0xec81, size); /* sub $xxx, %esp */ | 287 | oad(0xec81, size); /* sub $xxx, %esp */ |
| 300 | /* generate structure store */ | 288 | /* generate structure store */ |
| 301 | r = get_reg(REG_CLASS_INT); | 289 | r = get_reg(RC_INT); |
| 302 | o(0x89); /* mov %esp, r */ | 290 | o(0x89); /* mov %esp, r */ |
| 303 | o(0xe0 + r); | 291 | o(0xe0 + r); |
| 304 | vset(VT_INT, r, 0); | 292 | vset(VT_INT, r, 0); |
| @@ -306,7 +294,7 @@ void gfunc_param(GFuncContext *c) · 1 unmodified line |
| 306 | vstore(); | 294 | vstore(); |
| 307 | c->args_size += size; | 295 | c->args_size += size; |
| 308 | } else if (is_float(vtop->t)) { | 296 | } else if (is_float(vtop->t)) { |
| 309 | gv(); /* only one float register */ | 297 | gv(RC_FLOAT); /* only one float register */ |
| 310 | if ((vtop->t & VT_BTYPE) == VT_FLOAT) | 298 | if ((vtop->t & VT_BTYPE) == VT_FLOAT) |
| 311 | size = 4; | 299 | size = 4; |
| 312 | else if ((vtop->t & VT_BTYPE) == VT_DOUBLE) | 300 | else if ((vtop->t & VT_BTYPE) == VT_DOUBLE) |
| @@ -324,7 +312,7 @@ void gfunc_param(GFuncContext *c) · 11 unmodified lines |
| 324 | } else { | 312 | } else { |
| 325 | /* simple type (currently always same size) */ | 313 | /* simple type (currently always same size) */ |
| 326 | /* XXX: implicit cast ? */ | 314 | /* XXX: implicit cast ? */ |
| 327 | r = gv(); | 315 | r = gv(RC_INT); |
| 328 | o(0x50 + r); /* push r */ | 316 | o(0x50 + r); /* push r */ |
| 329 | c->args_size += 4; | 317 | c->args_size += 4; |
| 330 | } | 318 | } |
| @@ -347,7 +335,7 @@ void gfunc_call(GFuncContext *c) · 16 unmodified lines |
| 347 | } | 335 | } |
| 348 | } else { | 336 | } else { |
| 349 | /* otherwise, indirect call */ | 337 | /* otherwise, indirect call */ |
| 350 | r = gv(); | 338 | r = gv(RC_INT); |
| 351 | o(0xff); /* call *r */ | 339 | o(0xff); /* call *r */ |
| 352 | o(0xd0 + r); | 340 | o(0xd0 + r); |
| 353 | } | 341 | } |
| @@ -383,17 +371,22 @@ int gtst(int inv, int t) · 29 unmodified lines |
| 383 | t = gjmp(t); | 371 | t = gjmp(t); |
| 384 | gsym(vtop->c.i); | 372 | gsym(vtop->c.i); |
| 385 | } | 373 | } |
| 386 | } else if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) { | | |
| 387 | /* constant jmp optimization */ | | |
| 388 | if ((vtop->c.i != 0) != inv) | | |
| 389 | t = gjmp(t); | | |
| 390 | } else { | 374 | } else { |
| 391 | /* XXX: floats */ | 375 | if (is_float(vtop->t)) { |
| 392 | v = gv(); | 376 | vpushi(0); |
| 393 | o(0x85); | 377 | gen_op(TOK_NE); |
| 394 | o(0xc0 + v * 9); | 378 | } |
| 395 | g(0x0f); | 379 | if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) { |
| 396 | t = psym(0x85 ^ inv, t); | 380 | /* constant jmp optimization */ |
| | 381 | if ((vtop->c.i != 0) != inv) |
| | 382 | t = gjmp(t); |
| | 383 | } else { |
| | 384 | v = gv(RC_INT); |
| | 385 | o(0x85); |
| | 386 | o(0xc0 + v * 9); |
| | 387 | g(0x0f); |
| | 388 | t = psym(0x85 ^ inv, t); |
| | 389 | } |
| 397 | } | 390 | } |
| 398 | vtop--; | 391 | vtop--; |
| 399 | return t; | 392 | return t; |
| @@ -405,9 +398,9 @@ void gen_opi(int op) · 5 unmodified lines |
| 405 | int t, r, fr; | 398 | int t, r, fr; |
| 406 | | 399 | |
| 407 | vswap(); | 400 | vswap(); |
| 408 | r = gv(); | 401 | r = gv(RC_INT); |
| 409 | vswap(); | 402 | vswap(); |
| 410 | fr = gv(); | 403 | fr = gv(RC_INT); |
| 411 | vtop--; | 404 | vtop--; |
| 412 | | 405 | |
| 413 | if (op == '+') { | 406 | if (op == '+') { |
| @@ -460,9 +453,9 @@ void gen_opi(int op) · 46 unmodified lines |
| 460 | oad(0xbd, t); | 453 | oad(0xbd, t); |
| 461 | } | 454 | } |
| 462 | if (op == '%' | op == TOK_UMOD) | 455 | if (op == '%' | op == TOK_UMOD) |
| 463 | r = 2; | 456 | r = REG_EDX; |
| 464 | else | 457 | else |
| 465 | r = 0; | 458 | r = REG_EAX; |
| 466 | vtop->r = r; | 459 | vtop->r = r; |
| 467 | } else { | 460 | } else { |
| 468 | vtop--; | 461 | vtop--; |
| @@ -477,22 +470,22 @@ void gen_opi(int op) · 8 unmodified lines |
| 477 | /* NOTE: currently floats can only be lvalues */ | 470 | /* NOTE: currently floats can only be lvalues */ |
| 478 | void gen_opf(int op) | 471 | void gen_opf(int op) |
| 479 | { | 472 | { |
| 480 | int a, ft, fc, swapped, r; | 473 | int a, ft, fc, swapped; |
| 481 | | 474 | |
| 482 | /* convert constants to memory references */ | 475 | /* convert constants to memory references */ |
| 483 | if ((vtop[-1].r & (VT_CONST | VT_LVAL)) == VT_CONST) { | 476 | if ((vtop[-1].r & (VT_CONST | VT_LVAL)) == VT_CONST) { |
| 484 | vswap(); | 477 | vswap(); |
| 485 | gv(); | 478 | gv(RC_FLOAT); |
| 486 | vswap(); | 479 | vswap(); |
| 487 | } | 480 | } |
| 488 | if ((vtop[0].r & (VT_CONST | VT_LVAL)) == VT_CONST) | 481 | if ((vtop[0].r & (VT_CONST | VT_LVAL)) == VT_CONST) |
| 489 | gv(); | 482 | gv(RC_FLOAT); |
| 490 | | 483 | |
| 491 | /* must put at least one value in the floating point register */ | 484 | /* must put at least one value in the floating point register */ |
| 492 | if ((vtop[-1].r & VT_LVAL) && | 485 | if ((vtop[-1].r & VT_LVAL) && |
| 493 | (vtop[0].r & VT_LVAL)) { | 486 | (vtop[0].r & VT_LVAL)) { |
| 494 | vswap(); | 487 | vswap(); |
| 495 | gv(); | 488 | gv(RC_FLOAT); |
| 496 | vswap(); | 489 | vswap(); |
| 497 | } | 490 | } |
| 498 | if (op >= TOK_EQ && op <= TOK_GT) { | 491 | if (op >= TOK_EQ && op <= TOK_GT) { |
| @@ -520,13 +513,18 @@ void gen_opf(int op) · 21 unmodified lines |
| 520 | vtop->r = VT_CMP; | 513 | vtop->r = VT_CMP; |
| 521 | vtop->c.i = op; | 514 | vtop->c.i = op; |
| 522 | } else { | 515 | } else { |
| | 516 | swapped = 0; |
| 523 | /* swap the stack if needed so that t1 is the register and t2 is | 517 | /* swap the stack if needed so that t1 is the register and t2 is |
| 524 | the memory reference */ | 518 | the memory reference */ |
| 525 | swapped = 0; | | |
| 526 | if (vtop[-1].r & VT_LVAL) { | 519 | if (vtop[-1].r & VT_LVAL) { |
| 527 | vswap(); | 520 | vswap(); |
| 528 | swapped = 1; | 521 | swapped = 1; |
| 529 | } | 522 | } |
| | 523 | /* no memory reference possible for long double operations */ |
| | 524 | if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) { |
| | 525 | load(REG_ST0, vtop); |
| | 526 | swapped = !swapped; |
| | 527 | } |
| 530 | | 528 | |
| 531 | switch(op) { | 529 | switch(op) { |
| 532 | default: | 530 | default: |
| @@ -534,34 +532,30 @@ void gen_opf(int op) · 1 unmodified line |
| 534 | a = 0; | 532 | a = 0; |
| 535 | break; | 533 | break; |
| 536 | case '-': | 534 | case '-': |
| 537 | a = 0x20; | 535 | a = 4; |
| 538 | if (swapped) | 536 | if (swapped) |
| 539 | a += 8; | 537 | a++; |
| 540 | break; | 538 | break; |
| 541 | case '*': | 539 | case '*': |
| 542 | a = 0x08; | 540 | a = 1; |
| 543 | break; | 541 | break; |
| 544 | case '/': | 542 | case '/': |
| 545 | a = 0x30; | 543 | a = 6; |
| 546 | if (swapped) | 544 | if (swapped) |
| 547 | a += 8; | 545 | a++; |
| 548 | break; | 546 | break; |
| 549 | } | 547 | } |
| 550 | ft = vtop->t; | 548 | ft = vtop->t; |
| 551 | fc = vtop->c.ul; | 549 | fc = vtop->c.ul; |
| 552 | if ((ft & VT_BTYPE) == VT_DOUBLE) | 550 | if ((ft & VT_BTYPE) == VT_LDOUBLE) { |
| 553 | o(0xdc); | 551 | o(0xde); /* fxxxp %st, %st(1) */ |
| 554 | else | 552 | o(0xc1 + (a << 3)); |
| 555 | o(0xd8); | | |
| 556 | | | |
| 557 | r = vtop->r & VT_VALMASK; | | |
| 558 | if (r == VT_CONST) { | | |
| 559 | o(0x05 + a); | | |
| 560 | gen_addr32(vtop->r, fc); | | |
| 561 | } else if (r == VT_LOCAL) { | | |
| 562 | oad(0x85 + a, fc); | | |
| 563 | } else { | 553 | } else { |
| 564 | g(0x00 + a + r); | 554 | if ((ft & VT_BTYPE) == VT_DOUBLE) |
| | 555 | o(0xdc); |
| | 556 | else |
| | 557 | o(0xd8); |
| | 558 | gen_modrm(a, vtop->r, fc); |
| 565 | } | 559 | } |
| 566 | vtop--; | 560 | vtop--; |
| 567 | } | 561 | } |
| @@ -570,7 +564,7 @@ void gen_opf(int op) · 2 unmodified lines |
| 570 | /* convert integers to fp 't' type */ | 564 | /* convert integers to fp 't' type */ |
| 571 | void gen_cvt_itof(int t) | 565 | void gen_cvt_itof(int t) |
| 572 | { | 566 | { |
| 573 | gv(); | 567 | gv(RC_INT); |
| 574 | if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) { | 568 | if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) { |
| 575 | /* unsigned int to float/double/long double */ | 569 | /* unsigned int to float/double/long double */ |
| 576 | o(0x6a); /* push $0 */ | 570 | o(0x6a); /* push $0 */ |
| @@ -599,7 +593,7 @@ void gen_cvt_ftoi(int t) · 22 unmodified lines |
| 599 | { | 593 | { |
| 600 | int r, size; | 594 | int r, size; |
| 601 | | 595 | |
| 602 | gv(); | 596 | gv(RC_FLOAT); |
| 603 | if (t == VT_INT | VT_UNSIGNED && | 597 | if (t == VT_INT | VT_UNSIGNED && |
| 604 | t == VT_LLONG | VT_UNSIGNED && | 598 | t == VT_LLONG | VT_UNSIGNED && |
| 605 | t == VT_LLONG) | 599 | t == VT_LLONG) |
| @@ -607,7 +601,7 @@ void gen_cvt_ftoi(int t) · 1 unmodified line |
| 607 | else | 601 | else |
| 608 | size = 4; | 602 | size = 4; |
| 609 | | 603 | |
| 610 | r = get_reg(REG_CLASS_INT); | 604 | r = get_reg(RC_INT); |
| 611 | oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */ | 605 | oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */ |
| 612 | oad(0xec81, size); /* sub $xxx, %esp */ | 606 | oad(0xec81, size); /* sub $xxx, %esp */ |
| 613 | if (size == 4) | 607 | if (size == 4) |
| @@ -626,7 +620,7 @@ void gen_cvt_ftoi(int t) · 12 unmodified lines |
| 626 | void gen_cvt_ftof(int t) | 620 | void gen_cvt_ftof(int t) |
| 627 | { | 621 | { |
| 628 | /* all we have to do on i386 is to put the float in a register */ | 622 | /* all we have to do on i386 is to put the float in a register */ |
| 629 | gv(); | 623 | gv(RC_FLOAT); |
| 630 | } | 624 | } |
| 631 | | 625 | |
| 632 | /* pop stack value */ | 626 | /* pop stack value */ |