From d0aa106b94c649b4828825295e1cd1eeafbd82da Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 11:44:41 +0100 Subject: [PATCH] An element of an array as the machine has it x[i], of an array of the module with i in a register, was a copy of i, a shift, a lea and an add before its load. Now an element of 1, 2, 4 or 8 bytes of an array in a chunk, its index a variable's register, is noted as the operand [base + i * 8], the array's address made in the value's own register; one in the frame is [rbp + i * 8 + off], whatever its index (IndexArray). CheckIndex compares a variable's register as it is. NBody 225 -> 147 ms (FPC -O2 85); rat 464 -> 463, within noise; the other kernels unchanged. 9,036 lines of code. --- diff --git a/PLAN.md b/PLAN.md index 65162fd..c30d2cb 100644 --- a/PLAN.md +++ b/PLAN.md @@ -29,7 +29,13 @@ `movsd`, a constant read from read-only data, and an operation takes its right operand from memory as it is. Jacobi 251 ms to 129, NBody 375 to 225; rat within noise. -3. **Real variables in xmm registers**, where that pays: measured, and +3. Done: **An element of an array as the machine has it.** NBody's + time went mostly to its addresses, not its reals: `x[i]`, of an + array of the module, was a copy, a shift, a `lea` and an add before + 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 kept only if it does. ## Next, when the owner says diff --git a/doc/compiler.md b/doc/compiler.md index 708f9fd..85cf3f6 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -15,8 +15,8 @@ | `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,093 | 1,522 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,579 | 1,302 | Items: types, constants, variables, operators, conversions, calls | +| `src/gen.pas` | 2,121 | 1,542 | 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 | | `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers | @@ -489,6 +489,36 @@ under it as it does the integers. Jacobi 251 ms to 129, NBody 375 to 225; rat within noise. +2. **An element of an array as the machine has it.** That left + NBody's reals waiting on their addresses: `x[i]`, of an array of + the module, was worked out as an address, and the index copied + from its register to be scaled: + + ``` + mov r8, rbx ; i + shl r8, 3 + lea r9, [NBody.x] + add r8, r9 + movsd xmm8, [r8] + ``` + + Now an element of 1, 2, 4 or 8 bytes of an array in a chunk, with + its index in a variable's register, is noted as the operand that + reads it, its base the array's address, made in the value's own + register; one in the frame has `rbp` as its base, whatever its + index (`IndexArray`, as `IndexSlice` does for a slice in + registers): + + ``` + lea r8, [NBody.x] + movsd xmm8, [r8 + rbx * 8] + ``` + + A computed index in a chunk's array is added up as before, as it + 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. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is @@ -656,7 +686,9 @@ `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for `and` and `or`, `Fix` where the label is, and `JoinJumps`; - checks: `CheckIndex`, `CheckEnum`, and `IndexSlice`, `Slice` and - `SlicePtr`, which check as they make their address or slice; + `SlicePtr`, which check as they make their address or slice; and + `IndexArray`, an element's address in an array in the frame or a + chunk, the index checked before; - calls (`Call`), frames (`BeginProc`, `EnterBody`, `ReturnJump`, `EndProc`, the temporaries of `AllocTemp`), deferred statements (`DeferBegin`, `DeferEnd`, `RunDefer`), the built-ins that are diff --git a/src/gen.pas b/src/gen.pas index 5e773c6..3b887c5 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -169,6 +169,7 @@ procedure CheckIndex(n: int64; const p: TPos); procedure CheckEnum(n: int64; name: longint; const p: TPos); procedure CheckNil(reach: int64; const p: TPos); +procedure IndexArray(const a: TAdr; size: int64); procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); procedure Slice(const a: TAdr; isArray: boolean; n, size, tmp: int64; checked: boolean; const p: TPos); procedure SlicePtr(size, tmp: int64; checked: boolean; const p: TPos); @@ -1352,7 +1353,7 @@ procedure CheckIndex(n: int64; const p: TPos); var r: longint; begin - r := Top; + r := Peek(depth - 1); CmpI(r, n); Check(ccB, 0, p, r, NoMem, n, -1); end; @@ -1424,6 +1425,32 @@ if inRax then MovRR(Target, RAX) else Claim(depth, kWord); { the index's register again, now for the address } Computed(kWord); +end; + +{ Pops an index, checked, and makes the address of the element it + picks of the array at a. An element of 1, 2, + 4 or 8 bytes is only noted, as the operand [base + index * size] + (lIdx): an array in the frame has rbp as its base, and one in a + 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; +begin + if (size in [1, 2, 4, 8]) and (a.base = bFrame) then begin + r := Peek(depth - 1); + Drop; + NewMem(lIdx, MemIdx(RBP, r, size, a.off), 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; + Drop; + b := Target; + Lea(b, MemRip(a.chunk, a.off)); + NewMem(lIdx, MemIdx(b, r, size, 0), kWord, 8, false); + end else begin + MulI(Top, size); + PushAdr(a); + Op(sPlus, kWord, 8, false); + end; end; { Pops lo and hi, and the address of the array or slice at a if it is diff --git a/src/item.pas b/src/item.pas index d69bf11..0cafaee 100644 --- a/src/item.pas +++ b/src/item.pas @@ -640,9 +640,8 @@ end else if t^.form = fArray then begin Value(y); if checks then CheckIndex(t^.len, y.pos); - ScaleBy(s); - if x.base <> bStack then begin PushAdr(AdrOf(x)); x.off := 0 end; - gen.Op(sPlus, kWord, 8, false); + if x.base = bStack then begin ScaleBy(s); gen.Op(sPlus, kWord, 8, false) end + else begin IndexArray(AdrOf(x), s); x.off := 0 end; x.base := bStack; end else begin Value(y);