| | @@ -205,6 +205,13 @@ |
| 205 | 205 | nothing in an expression can change } |
| 206 | 206 | lAdr, { not loaded: the address of chunk, or of the frame |
| 207 | 207 | if chunk is -1, plus val } |
| 208 | + lIdx, { not loaded: the address m, an element of a slice |
| 209 | + in registers: its pointer's register as the base, |
| 210 | + and the index in a register variable's or the |
| 211 | + value's own register (IndexSlice) } |
| 212 | + lMem, { not loaded: the value of size bytes at m, in the |
| 213 | + frame, a chunk, or at an address lAdr or lIdx |
| 214 | + noted, read where it is used (PushVar) } |
| 208 | 215 | lReg, { in its register } |
| 209 | 216 | lFrame); { spilled: in the frame slot val } |
| 210 | 217 | |
| | @@ -213,6 +220,9 @@ |
| 213 | 220 | val: int64; { lConst: the value; lHeld: the register; lAdr: the |
| 214 | 221 | offset; lFrame: the frame slot } |
| 215 | 222 | chunk: longint; { lAdr: the chunk, or -1 for the frame } |
| 223 | + m: TMem; { lIdx, lMem } |
| 224 | + size: longint; { lMem } |
| 225 | + signed: boolean; { lMem } |
| 216 | 226 | k: TKind; |
| 217 | 227 | end; |
| 218 | 228 | |
| | @@ -283,10 +293,14 @@ |
| 283 | 293 | RegOf := Regs[i mod NumRegs]; |
| 284 | 294 | end; |
| 285 | 295 | |
| 286 | | -{ The value i, in a register, goes to a frame slot } |
| 296 | +function Loaded(i: longint): longint; forward; |
| 297 | + |
| 298 | +{ The value i, in a register, goes to a frame slot; one noted that |
| 299 | + holds its register, as an index, is worked out in it first } |
| 287 | 300 | procedure Spill(i: longint); |
| 288 | 301 | var s: int64; |
| 289 | 302 | begin |
| 303 | + if stack[i].loc <> lReg then Loaded(i); |
| 290 | 304 | s := AllocTemp(8, 8); |
| 291 | 305 | Store(Mem(RBP, s), RegOf(i), 8); |
| 292 | 306 | stack[i].loc := lFrame; |
| | @@ -294,10 +308,21 @@ |
| 294 | 308 | owner[i mod NumRegs] := -1; |
| 295 | 309 | end; |
| 296 | 310 | |
| 297 | | -{ Every value below depth d in a register, spilled } |
| 311 | +{ Each value below depth d read from memory now, as what comes next |
| 312 | + may change it } |
| 313 | +procedure ReadMems(d: longint); |
| 314 | +var i: longint; |
| 315 | +begin |
| 316 | + for i := 0 to d - 1 do |
| 317 | + if stack[i].loc = lMem then Loaded(i); |
| 318 | +end; |
| 319 | + |
| 320 | +{ Every value below depth d in a register, spilled; one noted in memory |
| 321 | + is read first, as a call may change it } |
| 298 | 322 | procedure SpillBelow(d: longint); |
| 299 | 323 | var r: longint; |
| 300 | 324 | begin |
| 325 | + ReadMems(d); |
| 301 | 326 | for r := 0 to NumRegs - 1 do |
| 302 | 327 | if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]); |
| 303 | 328 | end; |
| | @@ -326,10 +351,14 @@ |
| 326 | 351 | begin |
| 327 | 352 | if stack[i].loc <> lReg then begin |
| 328 | 353 | r := Claim(i); |
| 329 | | - if stack[i].loc = lConst then MovRI(r, stack[i].val) |
| 330 | | - else if stack[i].loc = lHeld then MovRR(r, stack[i].val) |
| 331 | | - else if stack[i].loc = lAdr then Lea(r, AdrMem(i, 0)) |
| 354 | + case stack[i].loc of |
| 355 | + lConst: MovRI(r, stack[i].val); |
| 356 | + lHeld: MovRR(r, stack[i].val); |
| 357 | + lAdr: Lea(r, AdrMem(i, 0)); |
| 358 | + lIdx: Lea(r, stack[i].m); |
| 359 | + lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); |
| 332 | 360 | else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 361 | + end; |
| 333 | 362 | stack[i].loc := lReg; |
| 334 | 363 | end; |
| 335 | 364 | Loaded := RegOf(i); |
| | @@ -358,6 +387,18 @@ |
| 358 | 387 | inc(depth); |
| 359 | 388 | end; |
| 360 | 389 | |
| 390 | +{ An address, lIdx, or a value at it, lMem, noted as the operand m. Its |
| 391 | + index may be in the register of the value just dropped, at this |
| 392 | + depth: it is the new value's own, then. } |
| 393 | +procedure NewMem(loc: TLoc; const m: TMem; k: TKind; size: longint; signed: boolean); |
| 394 | +begin |
| 395 | + NewEntry(k, loc, 0); |
| 396 | + stack[depth - 1].m := m; |
| 397 | + stack[depth - 1].size := size; |
| 398 | + stack[depth - 1].signed := signed; |
| 399 | + if m.index = RegOf(depth - 1) then owner[(depth - 1) mod NumRegs] := depth - 1; |
| 400 | +end; |
| 401 | + |
| 361 | 402 | { The register a new value on top is to be computed in } |
| 362 | 403 | function Target: longint; |
| 363 | 404 | begin |
| | @@ -373,7 +414,7 @@ |
| 373 | 414 | procedure Drop; |
| 374 | 415 | begin |
| 375 | 416 | dec(depth); |
| 376 | | - if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1; |
| 417 | + if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1; |
| 377 | 418 | end; |
| 378 | 419 | |
| 379 | 420 | { rsi and rdi are about to be used for other work, as in copying: a |
| | @@ -393,6 +434,8 @@ |
| 393 | 434 | lConst: MovRI(r, stack[i].val); |
| 394 | 435 | lHeld: MovRR(r, stack[i].val); |
| 395 | 436 | lAdr: Lea(r, AdrMem(i, 0)); |
| 437 | + lIdx: Lea(r, stack[i].m); |
| 438 | + lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); |
| 396 | 439 | lReg: if RegOf(i) <> r then MovRR(r, RegOf(i)); |
| 397 | 440 | else Load(r, Mem(RBP, stack[i].val), 8, false); |
| 398 | 441 | end; |
| | @@ -450,6 +493,10 @@ |
| 450 | 493 | if stack[depth - 1].loc = lAdr then begin |
| 451 | 494 | Place := AdrMem(depth - 1, a.off); |
| 452 | 495 | Drop; |
| 496 | + end else if stack[depth - 1].loc = lIdx then begin |
| 497 | + Place := stack[depth - 1].m; |
| 498 | + Place.disp := Place.disp + a.off; |
| 499 | + Drop; |
| 453 | 500 | end else begin |
| 454 | 501 | ra := Peek(depth - 1); |
| 455 | 502 | Drop; |
| | @@ -487,6 +534,13 @@ |
| 487 | 534 | NewEntry(k, lHeld, RegAt(a)); |
| 488 | 535 | exit; |
| 489 | 536 | end; |
| 537 | + { a variable in the frame or a chunk, or at an address only noted, is |
| 538 | + only noted too: read where it is used, as an operand if it can be } |
| 539 | + if (a.base in [bFrame, bGlobal]) or (a.base = bStack) and (stack[depth - 1].loc in [lAdr, lIdx]) then begin |
| 540 | + m := Place(a, -1); |
| 541 | + NewMem(lMem, m, k, size, signed); |
| 542 | + exit; |
| 543 | + end; |
| 490 | 544 | { in the order PushAdr explains } |
| 491 | 545 | if a.base = bStack then begin m := Place(a, -1); r := Target end |
| 492 | 546 | else begin r := Target; m := Place(a, r) end; |
| | @@ -529,6 +583,10 @@ |
| 529 | 583 | var e: TEntry; x, y: longint; rx, ry: boolean; |
| 530 | 584 | begin |
| 531 | 585 | x := depth - 2; y := depth - 1; |
| 586 | + { a value noted at an address that uses its register is worked out, |
| 587 | + so that its register moves with it } |
| 588 | + if stack[x].loc in [lIdx, lMem] then Loaded(x); |
| 589 | + if stack[y].loc in [lIdx, lMem] then Loaded(y); |
| 532 | 590 | rx := stack[x].loc = lReg; |
| 533 | 591 | ry := stack[y].loc = lReg; |
| 534 | 592 | if rx and ry then begin |
| | @@ -672,6 +730,13 @@ |
| 672 | 730 | FixLink(L); |
| 673 | 731 | end; |
| 674 | 732 | |
| 733 | +{ Whether the value on top is a word in memory that can be the right |
| 734 | + operand of an instruction as it is: add r, [m] } |
| 735 | +function TopInMem: boolean; |
| 736 | +begin |
| 737 | + TopInMem := (stack[depth - 1].loc = lMem) and (stack[depth - 1].size = 8); |
| 738 | +end; |
| 739 | + |
| 675 | 740 | { The right operand of the two values on top, popped, in register y: |
| 676 | 741 | its own, or rcx for a constant; the left one stays, in its register, |
| 677 | 742 | which is returned. } |
| | @@ -714,7 +779,7 @@ |
| 714 | 779 | { The two values on top, the left op the right, an integer of size |
| 715 | 780 | bytes, signed or not, brought back to its width. } |
| 716 | 781 | procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); |
| 717 | | -var v: int64; r, y, sh, bits: longint; c: boolean; |
| 782 | +var v: int64; r, y, sh, bits: longint; c: boolean; m: TMem; |
| 718 | 783 | begin |
| 719 | 784 | if k in [kF32, kF64] then begin RealOp(op, k); exit end; |
| 720 | 785 | bits := size * 8; |
| | @@ -738,6 +803,11 @@ |
| 738 | 803 | sShr: if signed then ShiftI(shSar, r, v and (bits - 1)) else ShiftI(shShr, r, v and (bits - 1)); |
| 739 | 804 | else AluI(AluOf(op), r, v); |
| 740 | 805 | end; |
| 806 | + end else if TopInMem and (op in [sPlus, sMinus, sAmp, sBar, sXor]) then begin |
| 807 | + m := stack[depth - 1].m; |
| 808 | + Drop; |
| 809 | + r := Top; |
| 810 | + AluM(AluOf(op), r, m); |
| 741 | 811 | end else begin |
| 742 | 812 | r := Operands(y); |
| 743 | 813 | case op of |
| | @@ -772,7 +842,7 @@ |
| 772 | 842 | const |
| 773 | 843 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 774 | 844 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 775 | | -var f: boolean; r, y: longint; v: int64; |
| 845 | +var f: boolean; r, y: longint; v: int64; m: TMem; |
| 776 | 846 | begin |
| 777 | 847 | if k in [kF32, kF64] then begin |
| 778 | 848 | f := k = kF32; |
| | @@ -795,6 +865,7 @@ |
| 795 | 865 | end; |
| 796 | 866 | end else begin |
| 797 | 867 | if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end |
| 868 | + else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Top; AluM(aluCmp, r, m) end |
| 798 | 869 | else begin r := Operands(y); Alu(aluCmp, r, y) end; |
| 799 | 870 | if signed then SetCC(CcSigned[op], r) else SetCC(CcUnsigned[op], r); |
| 800 | 871 | end; |
| | @@ -979,6 +1050,7 @@ |
| 979 | 1050 | procedure Atomic(which: longint); |
| 980 | 1051 | var r, n: longint; |
| 981 | 1052 | begin |
| 1053 | + ReadMems(depth - 2 - ord(which = bCas)); { what it writes may be one } |
| 982 | 1054 | if which = bCas then begin n := Top; Drop end; |
| 983 | 1055 | PopTo(RAX); { cmpxchg compares with rax, so CAS's old value goes there } |
| 984 | 1056 | r := Top; |
| | @@ -1001,12 +1073,7 @@ |
| 1001 | 1073 | begin |
| 1002 | 1074 | ClobberArgs; |
| 1003 | 1075 | SpillBelow(depth); |
| 1004 | | - for i := 0 to n - 1 do |
| 1005 | | - with stack[depth - n + i] do |
| 1006 | | - if loc = lConst then MovRI(SysRegs[i], val) |
| 1007 | | - else if loc = lHeld then MovRR(SysRegs[i], val) |
| 1008 | | - else if loc = lAdr then Lea(SysRegs[i], AdrMem(depth - n + i, 0)) |
| 1009 | | - else Load(SysRegs[i], Mem(RBP, val), 8, false); |
| 1076 | + for i := 0 to n - 1 do ValueTo(depth - n + i, SysRegs[i]); |
| 1010 | 1077 | for i := 1 to n do Drop; |
| 1011 | 1078 | x64.Syscall; |
| 1012 | 1079 | MovRR(Target, RAX); |
| | @@ -1040,10 +1107,11 @@ |
| 1040 | 1107 | const |
| 1041 | 1108 | CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE); |
| 1042 | 1109 | CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE); |
| 1043 | | -var r, y: longint; v: int64; |
| 1110 | +var r, y: longint; v: int64; m: TMem; |
| 1044 | 1111 | begin |
| 1045 | 1112 | { a comparison changes neither: a variable's register will do } |
| 1046 | 1113 | if TopConst(v) then begin Drop; r := Peek(depth - 1); CmpI(r, v) end |
| 1114 | + else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Peek(depth - 1); AluM(aluCmp, r, m) end |
| 1047 | 1115 | else begin y := Peek(depth - 1); Drop; r := Peek(depth - 1); Alu(aluCmp, r, y) end; |
| 1048 | 1116 | Drop; |
| 1049 | 1117 | if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L); |
| | @@ -1212,10 +1280,22 @@ |
| 1212 | 1280 | address; but if the slice's address is on the stack, under the |
| 1213 | 1281 | index, the element's register is the one that holds that, still |
| 1214 | 1282 | needed, so the work is done in rax. A slice in registers is |
| 1215 | | - compared with and added to them. } |
| 1283 | + compared with and added to them; for an element of 1, 2, 4 or 8 |
| 1284 | + bytes, its address is only noted, as the operand [pointer + index * |
| 1285 | + size], to be read or written where it is used (lIdx). } |
| 1216 | 1286 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 1217 | 1287 | var m, ml: TMem; r: longint; inRax: boolean; |
| 1218 | 1288 | begin |
| 1289 | + if (a.base = bReg) and (size in [1, 2, 4, 8]) then begin |
| 1290 | + r := Peek(depth - 1); |
| 1291 | + if checked then begin |
| 1292 | + Alu(aluCmp, r, a.hi); |
| 1293 | + Check(ccB, 0, p, r, InRegister(a.hi), 0, -1); |
| 1294 | + end; |
| 1295 | + Drop; |
| 1296 | + NewMem(lIdx, MemIdx(a.chunk, r, size, 0), kWord, 8, false); |
| 1297 | + exit; |
| 1298 | + end; |
| 1219 | 1299 | inRax := a.base = bStack; |
| 1220 | 1300 | if inRax then begin r := RAX; PopTo(RAX) end |
| 1221 | 1301 | else begin r := Top; Drop end; |