From 4d68ba95f3479811ca7bf70d076cd2c5b06eb6af Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 20:01:05 +0000 Subject: [PATCH] An element of a module's array by its address, with no lea x86 has no rip + index, so x[i] of an array in a chunk made the array's address in a register first, for every element. The executable is loaded below 2 GB where the linker placed it, so the address fits a 32-bit displacement with no base: X64.memAbs, whose fixup holds the address itself (Chunk.addAbsFixup), and indexArray notes the element as it does one in the frame, whatever register its index is in. NBody 25% fewer instructions, 16% faster. Grep's loops are unchanged but moved 96 bytes, and it runs 5% slower for it. --- diff --git a/doc/compiler.md b/doc/compiler.md index 2c4840e..fe87e22 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -16,12 +16,12 @@ | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions | | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | -| `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes | -| `src/X64.raz` | 1,493 | 1,238 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,509 | 1,797 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | +| `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | +| `src/Gen.raz` | 2,506 | 1,794 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 3,853 | 3,361 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | +| `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -779,6 +779,34 @@ ``` NBody 8.6% fewer instructions, 2.8% faster; the rest unchanged. + +2. **An element of a module's array by its address.** x86 has no + `rip + index`, so `x[i]`, of an array in a chunk, made the array's + address in a register first, for each element, twice in + `x[i] - x[j]`: + + ``` + lea r8, [NBody.x] + lea r9, [NBody.x] + movsd xmm8, [r8 + rbx * 8] + subsd xmm8, [r9 + r12 * 8] + ``` + + But the executable is loaded where the linker placed it, below 2 GB, + so the array's address fits a 32-bit displacement with no base + register at all (`X.memAbs`, a `RIP` base with an index; its fixup + holds the address itself, `Chunk.addAbsFixup`, not the distance from + the field). `indexArray` notes such an element as it does one in the + frame, whatever register its index is in: + + ``` + movsd xmm8, [NBody.x + rbx * 8] + subsd xmm8, [NBody.x + r12 * 8] + ``` + + NBody 25% fewer instructions, 16% faster. Grep is 5% slower with + the same code in its loops, only moved 96 bytes: where a loop falls + changes its speed, as v0.8.0 found. ## Diag.raz: messages diff --git a/src/Chunk.raz b/src/Chunk.raz index 3078ac5..4dd9464 100644 --- a/src/Chunk.raz +++ b/src/Chunk.raz @@ -56,6 +56,7 @@ dataFixups, the 64-bit one, from its chunk's start *) chunk*: INT; (* the chunk it refers to *) addend*: INT; (* added to the chunk's address *) + abs*: BOOL; (* text: the address itself, not from the field's end *) end; var @@ -246,7 +247,15 @@ fixups[numFixups].at := at; fixups[numFixups].chunk := chunk; fixups[numFixups].addend := addend; + fixups[numFixups].abs := false; INC(numFixups); +end; + +(* The same, for X64.memAbs: the field holds the address itself *) +proc addAbsFixup*(at, chunk, addend: INT); +begin + addFixup(at, chunk, addend); + fixups[numFixups - 1].abs := true; end; (* The address of chunk, plus addend, at offset at of rodata chunk c, diff --git a/src/Gen.raz b/src/Gen.raz index 09ff7d3..0e8733e 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -1640,23 +1640,20 @@ 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. *) + chunk its address as the displacement (X.memAbs), with no lea. Any + other is added up. *) proc indexArray*(a: Adr; size: I64); -var r, b: INT; o: I64; +var r: INT; o: I64; begin - if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base = bFrame) then + if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and ((a.base = bFrame) or (a.base = bGlobal)) then o := indexOff(size, a.off); r := peek(depth - 1); drop(); - newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); - elsif (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then - o := indexOff(size, 0); - r := peek(depth - 1); - drop(); - b := target(); - X.lea(b, X.memRip(a.chunk, INT(a.off))); - newMem(lIdx, X.memIdx(b, r, INT(size), o), kWord, 8, false); + if a.base = bFrame then + newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); + else + newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false); + end; else mulI(top(), size); pushAdr(a); diff --git a/src/Link.raz b/src/Link.raz index 852dce6..dee903f 100644 --- a/src/Link.raz +++ b/src/Link.raz @@ -358,9 +358,10 @@ COPY(textImg[p..p + Chunk.chunks[c].size], Chunk.text[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]); for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do at := Chunk.fixups[f].at - Chunk.chunks[c].start; - (* rel32: from the end of the field to the target *) - v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend - - (Chunk.chunks[c].adr + at + 4); + (* rel32: from the end of the field to the target; + or, X64.memAbs's, the target's address *) + v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend; + if not Chunk.fixups[f].abs then v := v - (Chunk.chunks[c].adr + at + 4); end; patch4(textImg, p + at, v); end; elsif Chunk.chunks[c].sec = Chunk.secRodata then diff --git a/src/X64.raz b/src/X64.raz index 2e0bd85..e3a744b 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -35,7 +35,8 @@ type Mem* = record - base*: INT; (* a register, or RIP *) + base*: INT; (* a register, or RIP; RIP with an index is the + chunk's address itself, as below (memAbs) *) index*: INT; (* a register, or NoIndex *) scale*: INT; (* 1, 2, 4 or 8 *) disp*: INT; @@ -80,6 +81,19 @@ begin m.base := RIP; m.index := NoIndex; m.scale := 1; m.disp := disp; m.chunk := chunk; + return m; +end; + +(* An element of the array in chunk, [chunk + index * scale + disp]. x86 + has no rip + index, but the executable is loaded where the linker put + it, below 2 GB, so the chunk's address fits the 32 bits of a + displacement with no base: one instruction, where rip-relative needs a + lea of the chunk first. *) +proc memAbs*(chunk, index, scale, disp: INT): Mem; +var m: Mem; +begin + m := memRip(chunk, disp); + m.index := index; m.scale := scale; return m; end; @@ -130,7 +144,7 @@ var md, b, sc: INT; begin byteReg := byteReg and (reg >= 4) and (reg <= 7); - if m.base = RIP then + if (m.base = RIP) and (m.index = NoIndex) then prefix(pfx, w, reg, NoIndex, 0, byteReg); else prefix(pfx, w, reg, m.index, m.base, byteReg); @@ -138,6 +152,16 @@ opcode(op); reg := reg & 7; if m.base = RIP then + if m.index # NoIndex then + if m.scale = 1 then sc := 0; elsif m.scale = 2 then sc := 1; elsif m.scale = 4 then sc := 2; else sc := 3; end; + (* memAbs: mod 00, r/m 100 for a SIB byte, whose base 101 + under mod 00 means none, but a disp32: the chunk's address *) + Chunk.put1((reg shl 3) | 4); + Chunk.put1((sc shl 6) | ((m.index & 7) shl 3) | 5); + Chunk.addAbsFixup(Chunk.pc, m.chunk, m.disp); + Chunk.put4(0); + return; + end; Chunk.put1((reg shl 3) | 5); (* mod 00 with r/m 101 is rip + disp32. The displacement counts from the end of the instruction, and the fixup takes it to end @@ -345,7 +369,7 @@ else n := 4; end; - if m.base = RIP then m.disp := m.disp - n; end; + if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end; if size = 1 then opM(0, false, 80H, op, m, false); elsif n = 1 then @@ -363,7 +387,7 @@ if listing then say(text2("mov", mnSized(m, size), num(v))); end; n := size; if n > 4 then n := 4; end; - if m.base = RIP then m.disp := m.disp - n; end; + if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end; if size = 1 then opM(0, false, 0C6H, 0, m, false); else