From 84cb543cbd4a6281a6a37dd0f77a88ab678cd175 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:50:22 +0100 Subject: [PATCH] An offset as the machine has it s[i * N + j - 1] worked out its index in full, the constant added too, and (i - 1) * N subtracted before it shifted. Now a word in a register may carry an offset not yet added (off, in TEntry): Op notes x + c and x - c there, carries it through + and -, and through * by a constant, as (x + a) * k is x * k + a * k; an element's operand takes it as its displacement (IndexOff, for IndexSlice and IndexArray), as does a field's through a pointer (Place); anything else adds it with a lea where the value is read (Loaded, Peek, ValueTo). A checked index has it added first. Jacobi 129 -> 106 ms (FPC -O2 81); rat 493 -> 482 ms, within noise; the integer kernels unchanged. test/enctest takes the encoder's new real forms (and the renamed movsd). 9,082 lines of code. --- diff --git a/PLAN.md b/PLAN.md index c30d2cb..cad10ed 100644 --- a/PLAN.md +++ b/PLAN.md @@ -35,7 +35,13 @@ its load. Now an element of an array in the frame or a chunk is the operand `[base + i * 8]`, as one of a slice in registers is. NBody 225 ms to 147; rat within noise. -4. **Real variables in xmm registers**, where that pays: measured, and +4. Done: **An offset as the machine has it.** Jacobi's index + arithmetic, `s[i * N + j - 1]`, added its constant before the load; + now a word's constant offset waits on the stack, carried through + `+`, `-` and `*` by a constant, and an element's operand takes it as + its displacement: `[r13 + r9 * 8 - 8]`. Jacobi 129 ms to 106; rat + within noise. +5. **Real variables in xmm registers**, where that pays: measured, and kept only if it does. ## Next, when the owner says diff --git a/doc/compiler.md b/doc/compiler.md index 85cf3f6..536a292 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -15,7 +15,7 @@ | `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,201 | 992 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 2,121 | 1,542 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/gen.pas` | 2,188 | 1,588 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,578 | 1,301 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 3,103 | 2,707 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places | @@ -518,7 +518,47 @@ would need a third register. `CheckIndex` compares a variable's register as it is (`Peek`), not a copy. NBody 225 ms to 147; rat within noise. + +3. **An offset as the machine has it.** The sweep's four neighbours, + `s[i * N + j - 1]` and the others, each worked out its index in + full, the constant added too: + ``` + mov r9, r12 ; i * N + j - 1 + shl r9, 9 + add r9, rbx + sub r9, 1 + ... + mov r10, r12 ; (i - 1) * N + j + sub r10, 1 + shl r10, 9 + add r10, rbx + addsd xmm9, [r13 + r10 * 8] + ``` + + Now a word in a register may carry an offset not yet added (`off`, + in `TEntry`): `Op` notes `x + c` and `x - c` there, carries it + through `+` and `-`, and through `*` by a constant, as `(x + a) * k` + is `x * k + a * k`. An element's operand takes it as its + displacement (`IndexOff`, for `IndexSlice` and `IndexArray`), and so + does a field's through a pointer (`Place`); anything else adds it + where the value is read (`Loaded`, `Peek`, `ValueTo`, with a `lea`, + which leaves the flags alone). A value changed in place, `lInto`, + carries none, and an index that is checked has it added first: + + ``` + mov r9, r12 + shl r9, 9 + add r9, rbx + ... + movsd xmm9, [r13 + r9 * 8 - 8] + ... + addsd xmm9, [r13 + r10 * 8 - 4096] + ``` + + Jacobi 129 ms to 106; rat 2% faster, within noise; the integer + kernels unchanged. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is @@ -709,7 +749,8 @@ variable's own register, read in place (`lHeld`), the value's own register (`lReg`) or a frame slot (`lFrame`); `owner` says which value each register holds, `lIdx` and `lMem` holding theirs when it -is their index. The value at depth `d` belongs in +is their index. A word in a register may have an offset not yet +added (`off`), which an operand takes as its displacement. The value at depth `d` belongs in `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division and shifts need, and `rsi` and `rdi`, which copying needs, aren't among them. A real belongs in `XRegs[d mod 4]`, `xmm8` to `xmm11`, which diff --git a/src/gen.pas b/src/gen.pas index 3b887c5..2fc05f8 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -213,10 +213,11 @@ nothing in an expression can change } lAdr, { not loaded: the address of chunk, or of the frame if chunk is -1, plus val } - lIdx, { not loaded: the address m, an element of a slice - in registers: its pointer's register as the base, - and the index in a register variable's or the - value's own register (IndexSlice) } + lIdx, { not loaded: the address m, an element of an array + or of a slice in registers: the array's or the + pointer's register, or rbp, as the base, and the + index in a register variable's or the value's + own register (IndexSlice, IndexArray) } lMem, { not loaded: the value of size bytes at m, in the frame, a chunk, or at an address lAdr or lIdx noted, read where it is used (PushVar) } @@ -235,6 +236,10 @@ size: longint; { lMem } signed: boolean; { lMem } k: TKind; + off: int64; { lHeld, lReg: a word, its register plus off, the + constant of x + 1 not yet added (Op), so that an + operand that uses it can take it as its + displacement: [r13 + r9 * 8 - 8] } end; const @@ -306,6 +311,11 @@ from xmm8 up carry no arguments, so that Call can move a real into xmm0 to xmm7 without overwriting another still to go. } +function Fits32(v: int64): boolean; +begin + Fits32 := (v >= low(longint)) and (v <= high(longint)); +end; + { The register of kind k for the value at depth i } function RegIn(k: TKind; i: longint): longint; begin @@ -353,7 +363,7 @@ procedure Spill(i: longint); var s: int64; begin - if stack[i].loc <> lReg then Loaded(i); + if (stack[i].loc <> lReg) or (stack[i].off <> 0) then Loaded(i); s := AllocTemp(8, 8); if IsReal(i) then MovsMX(Mem(RBP, s), RegOf(i), false) else Store(Mem(RBP, s), RegOf(i), 8); @@ -419,6 +429,10 @@ end; stack[i].loc := lReg; end; + if stack[i].off <> 0 then begin + Lea(RegOf(i), Mem(RegOf(i), stack[i].off)); + stack[i].off := 0; + end; Loaded := RegOf(i); end; @@ -426,9 +440,28 @@ its register } function Peek(i: longint): longint; begin - if stack[i].loc = lHeld then Peek := stack[i].val else Peek := Loaded(i); + if (stack[i].loc = lHeld) and (stack[i].off = 0) then Peek := stack[i].val else Peek := Loaded(i); end; +{ The offset of value i, taken from it, for the caller to add or to + make a displacement of } +function TakeOff(i: longint): int64; +begin + TakeOff := stack[i].off; + stack[i].off := 0; +end; + +{ The word on top, in a register, has o added: noted, if o can be a + displacement, else added now } +procedure PutOff(o: int64); +begin + if Fits32(stack[depth - 1].off + o) then stack[depth - 1].off := stack[depth - 1].off + o + else begin + MovRI(RCX, o); + Alu(aluAdd, Loaded(depth - 1), RCX); + end; +end; + function Top: longint; begin if depth = 0 then Fatal('internal error: the stack is empty'); @@ -442,6 +475,7 @@ stack[depth].loc := loc; stack[depth].val := v; stack[depth].chunk := -1; + stack[depth].off := 0; inc(depth); end; @@ -505,6 +539,7 @@ else if RegOf(i) <> r then MovRR(r, RegOf(i)); else Load(r, Mem(RBP, stack[i].val), 8, false); end; + if stack[i].off <> 0 then Lea(r, Mem(r, stack[i].off)); end; { The value on top, popped into register r } @@ -534,11 +569,6 @@ NewEntry(k, lConst, v); end; -function Fits32(v: int64): boolean; -begin - Fits32 := (v >= low(longint)) and (v <= high(longint)); -end; - { The register of the word at a, a variable in registers: the variable's own, or for a slice its pointer's (off 0) or its length's (off 8) } function RegAt(const a: TAdr): longint; @@ -549,7 +579,7 @@ { The memory a names. An address on the stack is popped, and is in its register still; one in a slot is loaded into r. } function Place(const a: TAdr; r: longint): TMem; -var ra: longint; +var ra: longint; o: int64; begin if a.base = bReg then Fatal('internal error: a register has no address'); case a.base of @@ -564,9 +594,12 @@ Place.disp := Place.disp + a.off; Drop; end else begin + { an offset noted on the address is the operand's, as + p^.f + 8: [r8 + 8] } + if Fits32(a.off + stack[depth - 1].off) then o := TakeOff(depth - 1) else o := 0; ra := Peek(depth - 1); Drop; - Place := Mem(ra, a.off) + Place := Mem(ra, a.off + o) end; else { bSlot: the address is in the frame slot a.chunk } Load(r, Mem(RBP, a.chunk), 8, false); @@ -876,7 +909,7 @@ { The two values on top, the left op the right, an integer of size bytes, signed or not, brought back to its width. } procedure Op(op: TSym; k: TKind; size: longint; signed: boolean); -var v: int64; r, y, sh, bits: longint; c: boolean; m: TMem; +var v, o: int64; r, y, sh, bits: longint; c: boolean; m: TMem; begin if k in [kF32, kF64] then begin RealOp(op, k); exit end; bits := size * 8; @@ -885,6 +918,26 @@ if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst) and (stack[depth - 1].loc <> lConst) then Swap; c := TopConst(v); + { A word's offset (see TEntry) is carried through + and -, and + through * by a constant, as (x + a) * k is x * k + a * k: the + operation is done on the registers, the offsets worked out here, + and put on the result. x + 1 is only noted. A value changed in + place, lInto, has no offset. } + o := 0; + if (bits = 64) and (stack[depth - 2].loc <> lInto) + and ((op in [sPlus, sMinus]) or (op = sTimes) and c and Fits32(v)) then begin + o := TakeOff(depth - 2); + if op = sTimes then o := o * v + else if c and Fits32(v) then begin + if op = sPlus then o := o + v else o := o - v; + Drop; + if not (stack[depth - 1].loc in [lReg, lHeld]) then Top; + PutOff(o); + exit; + end + else if op = sPlus then o := o + TakeOff(depth - 1) + else o := o - TakeOff(depth - 1); + end; sh := -1; if c and (op in [sTimes, sDiv, sMod]) then sh := ExactLog2(v); if c and (sh >= 1) then begin @@ -932,6 +985,7 @@ end; if bits < 64 then Extend(r, size, signed); stack[depth - 1].k := k; + if o <> 0 then PutOff(o); end; { The left op the right of the two values on top, as a BOOL } @@ -1379,6 +1433,15 @@ Check(ccNE, 4, p, r, NoMem, 0, -1); end; +{ The displacement disp of an element of size bytes, with the offset of + the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8 + - 8] } +function IndexOff(size, disp: int64): int64; +begin + IndexOff := disp; + if Fits32(disp + stack[depth - 1].off * size) then IndexOff := disp + TakeOff(depth - 1) * size; +end; + { Pops an index, and the address of the slice at a if it is on the stack, and pushes the address of its element of size bytes: the index, checked against the length, times size, plus the pointer. @@ -1390,16 +1453,18 @@ bytes, its address is only noted, as the operand [pointer + index * size], to be read or written where it is used (lIdx). } procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); -var m, ml: TMem; r: longint; inRax: boolean; +var m, ml: TMem; r: longint; inRax: boolean; o: int64; begin if (a.base = bReg) and (size in [1, 2, 4, 8]) then begin + o := 0; + if not checked then o := IndexOff(size, 0); r := Peek(depth - 1); if checked then begin Alu(aluCmp, r, a.hi); Check(ccB, 0, p, r, InRegister(a.hi), 0, -1); end; Drop; - NewMem(lIdx, MemIdx(a.chunk, r, size, 0), kWord, 8, false); + NewMem(lIdx, MemIdx(a.chunk, r, size, o), kWord, 8, false); exit; end; inRax := a.base = bStack; @@ -1434,18 +1499,20 @@ chunk, if the index is a variable's register, its address made in the value's own register. Any other is added up. } procedure IndexArray(const a: TAdr; size: int64); -var r, b: longint; +var r, b: longint; o: int64; begin if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin + o := IndexOff(size, a.off); r := Peek(depth - 1); Drop; - NewMem(lIdx, MemIdx(RBP, r, size, a.off), kWord, 8, false); + NewMem(lIdx, MemIdx(RBP, r, size, o), kWord, 8, false); end else if (size in [1, 2, 4, 8]) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then begin - r := stack[depth - 1].val; + o := IndexOff(size, 0); + r := Peek(depth - 1); Drop; b := Target; Lea(b, MemRip(a.chunk, a.off)); - NewMem(lIdx, MemIdx(b, r, size, 0), kWord, 8, false); + NewMem(lIdx, MemIdx(b, r, size, o), kWord, 8, false); end else begin MulI(Top, size); PushAdr(a); diff --git a/test/enctest.pas b/test/enctest.pas index b732391..b1edf8d 100644 --- a/test/enctest.pas +++ b/test/enctest.pas @@ -23,7 +23,9 @@ SetCC(ccL, RAX); SetCC(ccA, RCX); SetCC(ccE, RDI); TestRR(RAX, RAX); Push(RAX); Push(12); Pop(RCX); Pop(R11); CallR(R11); CallM(Mem(RCX, 8)); Ret; Leave; Syscall; Mfence; Ud2; RepMovsb; RepStosb; LockCmpxchg(Mem(RCX, 0), RDX); Xchg(Mem(RCX, 0), RAX); LockXadd(Mem(RCX, 0), RAX); - MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsdXM(2, Mem(RBP, -8)); MovsdMX(Mem(RBP, -8), 7); + MovqXR(0, RAX); MovqXR(1, RCX); MovqRX(RAX, 0); MovsXM(2, Mem(RBP, -8), false); MovsMX(Mem(RBP, -8), 7, false); + MovX(9, 12); MovX(0, 8); MovsXM(8, MemIdx(R13, R9, 8, -8), true); MovsMX(Mem(RBP, -8), 11, true); + SseM($58, 9, MemIdx(R13, R10, 8, 4096), false); SseM($59, 0, Mem(RBP, -16), true); Sse($5C, 8, 11, false); Sse($58, 0, 1, false); Sse($5E, 0, 1, true); Sse($5A, 0, 0, false); Sse($5A, 0, 0, true); Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true); Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63);