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