| | @@ -213,10 +213,11 @@ |
| 213 | 213 | nothing in an expression can change } |
| 214 | 214 | lAdr, { not loaded: the address of chunk, or of the frame |
| 215 | 215 | if chunk is -1, plus val } |
| 216 | | - lIdx, { not loaded: the address m, an element of a slice |
| 217 | | - in registers: its pointer's register as the base, |
| 218 | | - and the index in a register variable's or the |
| 219 | | - value's own register (IndexSlice) } |
| 216 | + lIdx, { not loaded: the address m, an element of an array |
| 217 | + or of a slice in registers: the array's or the |
| 218 | + pointer's register, or rbp, as the base, and the |
| 219 | + index in a register variable's or the value's |
| 220 | + own register (IndexSlice, IndexArray) } |
| 220 | 221 | lMem, { not loaded: the value of size bytes at m, in the |
| 221 | 222 | frame, a chunk, or at an address lAdr or lIdx |
| 222 | 223 | noted, read where it is used (PushVar) } |
| | @@ -235,6 +236,10 @@ |
| 235 | 236 | size: longint; { lMem } |
| 236 | 237 | signed: boolean; { lMem } |
| 237 | 238 | k: TKind; |
| 239 | + off: int64; { lHeld, lReg: a word, its register plus off, the |
| 240 | + constant of x + 1 not yet added (Op), so that an |
| 241 | + operand that uses it can take it as its |
| 242 | + displacement: [r13 + r9 * 8 - 8] } |
| 238 | 243 | end; |
| 239 | 244 | |
| 240 | 245 | const |
| | @@ -306,6 +311,11 @@ |
| 306 | 311 | from xmm8 up carry no arguments, so that Call can move a real into |
| 307 | 312 | xmm0 to xmm7 without overwriting another still to go. } |
| 308 | 313 | |
| 314 | +function Fits32(v: int64): boolean; |
| 315 | +begin |
| 316 | + Fits32 := (v >= low(longint)) and (v <= high(longint)); |
| 317 | +end; |
| 318 | + |
| 309 | 319 | { The register of kind k for the value at depth i } |
| 310 | 320 | function RegIn(k: TKind; i: longint): longint; |
| 311 | 321 | begin |
| | @@ -353,7 +363,7 @@ |
| 353 | 363 | procedure Spill(i: longint); |
| 354 | 364 | var s: int64; |
| 355 | 365 | begin |
| 356 | | - if stack[i].loc <> lReg then Loaded(i); |
| 366 | + if (stack[i].loc <> lReg) or (stack[i].off <> 0) then Loaded(i); |
| 357 | 367 | s := AllocTemp(8, 8); |
| 358 | 368 | if IsReal(i) then MovsMX(Mem(RBP, s), RegOf(i), false) |
| 359 | 369 | else Store(Mem(RBP, s), RegOf(i), 8); |
| | @@ -419,6 +429,10 @@ |
| 419 | 429 | end; |
| 420 | 430 | stack[i].loc := lReg; |
| 421 | 431 | end; |
| 432 | + if stack[i].off <> 0 then begin |
| 433 | + Lea(RegOf(i), Mem(RegOf(i), stack[i].off)); |
| 434 | + stack[i].off := 0; |
| 435 | + end; |
| 422 | 436 | Loaded := RegOf(i); |
| 423 | 437 | end; |
| 424 | 438 | |
| | @@ -426,9 +440,28 @@ |
| 426 | 440 | its register } |
| 427 | 441 | function Peek(i: longint): longint; |
| 428 | 442 | begin |
| 429 | | - if stack[i].loc = lHeld then Peek := stack[i].val else Peek := Loaded(i); |
| 443 | + if (stack[i].loc = lHeld) and (stack[i].off = 0) then Peek := stack[i].val else Peek := Loaded(i); |
| 430 | 444 | end; |
| 431 | 445 | |
| 446 | +{ The offset of value i, taken from it, for the caller to add or to |
| 447 | + make a displacement of } |
| 448 | +function TakeOff(i: longint): int64; |
| 449 | +begin |
| 450 | + TakeOff := stack[i].off; |
| 451 | + stack[i].off := 0; |
| 452 | +end; |
| 453 | + |
| 454 | +{ The word on top, in a register, has o added: noted, if o can be a |
| 455 | + displacement, else added now } |
| 456 | +procedure PutOff(o: int64); |
| 457 | +begin |
| 458 | + if Fits32(stack[depth - 1].off + o) then stack[depth - 1].off := stack[depth - 1].off + o |
| 459 | + else begin |
| 460 | + MovRI(RCX, o); |
| 461 | + Alu(aluAdd, Loaded(depth - 1), RCX); |
| 462 | + end; |
| 463 | +end; |
| 464 | + |
| 432 | 465 | function Top: longint; |
| 433 | 466 | begin |
| 434 | 467 | if depth = 0 then Fatal('internal error: the stack is empty'); |
| | @@ -442,6 +475,7 @@ |
| 442 | 475 | stack[depth].loc := loc; |
| 443 | 476 | stack[depth].val := v; |
| 444 | 477 | stack[depth].chunk := -1; |
| 478 | + stack[depth].off := 0; |
| 445 | 479 | inc(depth); |
| 446 | 480 | end; |
| 447 | 481 | |
| | @@ -505,6 +539,7 @@ |
| 505 | 539 | else if RegOf(i) <> r then MovRR(r, RegOf(i)); |
| 506 | 540 | else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 507 | 541 | end; |
| 542 | + if stack[i].off <> 0 then Lea(r, Mem(r, stack[i].off)); |
| 508 | 543 | end; |
| 509 | 544 | |
| 510 | 545 | { The value on top, popped into register r } |
| | @@ -534,11 +569,6 @@ |
| 534 | 569 | NewEntry(k, lConst, v); |
| 535 | 570 | end; |
| 536 | 571 | |
| 537 | | -function Fits32(v: int64): boolean; |
| 538 | | -begin |
| 539 | | - Fits32 := (v >= low(longint)) and (v <= high(longint)); |
| 540 | | -end; |
| 541 | | - |
| 542 | 572 | { The register of the word at a, a variable in registers: the variable's |
| 543 | 573 | own, or for a slice its pointer's (off 0) or its length's (off 8) } |
| 544 | 574 | function RegAt(const a: TAdr): longint; |
| | @@ -549,7 +579,7 @@ |
| 549 | 579 | { The memory a names. An address on the stack is popped, and is in its |
| 550 | 580 | register still; one in a slot is loaded into r. } |
| 551 | 581 | function Place(const a: TAdr; r: longint): TMem; |
| 552 | | -var ra: longint; |
| 582 | +var ra: longint; o: int64; |
| 553 | 583 | begin |
| 554 | 584 | if a.base = bReg then Fatal('internal error: a register has no address'); |
| 555 | 585 | case a.base of |
| | @@ -564,9 +594,12 @@ |
| 564 | 594 | Place.disp := Place.disp + a.off; |
| 565 | 595 | Drop; |
| 566 | 596 | end else begin |
| 597 | + { an offset noted on the address is the operand's, as |
| 598 | + p^.f + 8: [r8 + 8] } |
| 599 | + if Fits32(a.off + stack[depth - 1].off) then o := TakeOff(depth - 1) else o := 0; |
| 567 | 600 | ra := Peek(depth - 1); |
| 568 | 601 | Drop; |
| 569 | | - Place := Mem(ra, a.off) |
| 602 | + Place := Mem(ra, a.off + o) |
| 570 | 603 | end; |
| 571 | 604 | else { bSlot: the address is in the frame slot a.chunk } |
| 572 | 605 | Load(r, Mem(RBP, a.chunk), 8, false); |
| | @@ -876,7 +909,7 @@ |
| 876 | 909 | { The two values on top, the left op the right, an integer of size |
| 877 | 910 | bytes, signed or not, brought back to its width. } |
| 878 | 911 | procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); |
| 879 | | -var v: int64; r, y, sh, bits: longint; c: boolean; m: TMem; |
| 912 | +var v, o: int64; r, y, sh, bits: longint; c: boolean; m: TMem; |
| 880 | 913 | begin |
| 881 | 914 | if k in [kF32, kF64] then begin RealOp(op, k); exit end; |
| 882 | 915 | bits := size * 8; |
| | @@ -885,6 +918,26 @@ |
| 885 | 918 | if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst) |
| 886 | 919 | and (stack[depth - 1].loc <> lConst) then Swap; |
| 887 | 920 | c := TopConst(v); |
| 921 | + { A word's offset (see TEntry) is carried through + and -, and |
| 922 | + through * by a constant, as (x + a) * k is x * k + a * k: the |
| 923 | + operation is done on the registers, the offsets worked out here, |
| 924 | + and put on the result. x + 1 is only noted. A value changed in |
| 925 | + place, lInto, has no offset. } |
| 926 | + o := 0; |
| 927 | + if (bits = 64) and (stack[depth - 2].loc <> lInto) |
| 928 | + and ((op in [sPlus, sMinus]) or (op = sTimes) and c and Fits32(v)) then begin |
| 929 | + o := TakeOff(depth - 2); |
| 930 | + if op = sTimes then o := o * v |
| 931 | + else if c and Fits32(v) then begin |
| 932 | + if op = sPlus then o := o + v else o := o - v; |
| 933 | + Drop; |
| 934 | + if not (stack[depth - 1].loc in [lReg, lHeld]) then Top; |
| 935 | + PutOff(o); |
| 936 | + exit; |
| 937 | + end |
| 938 | + else if op = sPlus then o := o + TakeOff(depth - 1) |
| 939 | + else o := o - TakeOff(depth - 1); |
| 940 | + end; |
| 888 | 941 | sh := -1; |
| 889 | 942 | if c and (op in [sTimes, sDiv, sMod]) then sh := ExactLog2(v); |
| 890 | 943 | if c and (sh >= 1) then begin |
| | @@ -932,6 +985,7 @@ |
| 932 | 985 | end; |
| 933 | 986 | if bits < 64 then Extend(r, size, signed); |
| 934 | 987 | stack[depth - 1].k := k; |
| 988 | + if o <> 0 then PutOff(o); |
| 935 | 989 | end; |
| 936 | 990 | |
| 937 | 991 | { The left op the right of the two values on top, as a BOOL } |
| | @@ -1379,6 +1433,15 @@ |
| 1379 | 1433 | Check(ccNE, 4, p, r, NoMem, 0, -1); |
| 1380 | 1434 | end; |
| 1381 | 1435 | |
| 1436 | +{ The displacement disp of an element of size bytes, with the offset of |
| 1437 | + the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8 |
| 1438 | + - 8] } |
| 1439 | +function IndexOff(size, disp: int64): int64; |
| 1440 | +begin |
| 1441 | + IndexOff := disp; |
| 1442 | + if Fits32(disp + stack[depth - 1].off * size) then IndexOff := disp + TakeOff(depth - 1) * size; |
| 1443 | +end; |
| 1444 | + |
| 1382 | 1445 | { Pops an index, and the address of the slice at a if it is on the |
| 1383 | 1446 | stack, and pushes the address of its element of size bytes: the |
| 1384 | 1447 | index, checked against the length, times size, plus the pointer. |
| | @@ -1390,16 +1453,18 @@ |
| 1390 | 1453 | bytes, its address is only noted, as the operand [pointer + index * |
| 1391 | 1454 | size], to be read or written where it is used (lIdx). } |
| 1392 | 1455 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 1393 | | -var m, ml: TMem; r: longint; inRax: boolean; |
| 1456 | +var m, ml: TMem; r: longint; inRax: boolean; o: int64; |
| 1394 | 1457 | begin |
| 1395 | 1458 | if (a.base = bReg) and (size in [1, 2, 4, 8]) then begin |
| 1459 | + o := 0; |
| 1460 | + if not checked then o := IndexOff(size, 0); |
| 1396 | 1461 | r := Peek(depth - 1); |
| 1397 | 1462 | if checked then begin |
| 1398 | 1463 | Alu(aluCmp, r, a.hi); |
| 1399 | 1464 | Check(ccB, 0, p, r, InRegister(a.hi), 0, -1); |
| 1400 | 1465 | end; |
| 1401 | 1466 | Drop; |
| 1402 | | - NewMem(lIdx, MemIdx(a.chunk, r, size, 0), kWord, 8, false); |
| 1467 | + NewMem(lIdx, MemIdx(a.chunk, r, size, o), kWord, 8, false); |
| 1403 | 1468 | exit; |
| 1404 | 1469 | end; |
| 1405 | 1470 | inRax := a.base = bStack; |
| | @@ -1434,18 +1499,20 @@ |
| 1434 | 1499 | chunk, if the index is a variable's register, its address made in |
| 1435 | 1500 | the value's own register. Any other is added up. } |
| 1436 | 1501 | procedure IndexArray(const a: TAdr; size: int64); |
| 1437 | | -var r, b: longint; |
| 1502 | +var r, b: longint; o: int64; |
| 1438 | 1503 | begin |
| 1439 | 1504 | if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin |
| 1505 | + o := IndexOff(size, a.off); |
| 1440 | 1506 | r := Peek(depth - 1); |
| 1441 | 1507 | Drop; |
| 1442 | | - NewMem(lIdx, MemIdx(RBP, r, size, a.off), kWord, 8, false); |
| 1508 | + NewMem(lIdx, MemIdx(RBP, r, size, o), kWord, 8, false); |
| 1443 | 1509 | end else if (size in [1, 2, 4, 8]) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then begin |
| 1444 | | - r := stack[depth - 1].val; |
| 1510 | + o := IndexOff(size, 0); |
| 1511 | + r := Peek(depth - 1); |
| 1445 | 1512 | Drop; |
| 1446 | 1513 | b := Target; |
| 1447 | 1514 | Lea(b, MemRip(a.chunk, a.off)); |
| 1448 | | - NewMem(lIdx, MemIdx(b, r, size, 0), kWord, 8, false); |
| 1515 | + NewMem(lIdx, MemIdx(b, r, size, o), kWord, 8, false); |
| 1449 | 1516 | end else begin |
| 1450 | 1517 | MulI(Top, size); |
| 1451 | 1518 | PushAdr(a); |