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.
| @@ -16,12 +16,12 @@ | ||
| 16 | 16 | | `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | |
| 17 | 17 | | `src/Scan.raz` | 1,062 | 886 | Symbols, literals, comments, positions | |
| 18 | 18 | | `src/Sym.raz` | 687 | 556 | Types, objects, scopes, layout | |
| 19 | -| `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes | | |
| 20 | -| `src/X64.raz` | 1,493 | 1,238 | The instruction encoder, jumps within a procedure, the assembler | | |
| 21 | -| `src/Gen.raz` | 2,509 | 1,797 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 19 | +| `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | |
| 20 | +| `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | |
| 21 | +| `src/Gen.raz` | 2,506 | 1,794 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 22 | 22 | | `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls | |
| 23 | 23 | | `src/Parse.raz` | 3,853 | 3,361 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 24 | -| `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | | |
| 24 | +| `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| 27 | 27 | |
| @@ -779,6 +779,34 @@ | ||
| 779 | 779 | ``` |
| 780 | 780 | |
| 781 | 781 | NBody 8.6% fewer instructions, 2.8% faster; the rest unchanged. |
| 782 | + | |
| 783 | +2. **An element of a module's array by its address.** x86 has no | |
| 784 | + `rip + index`, so `x[i]`, of an array in a chunk, made the array's | |
| 785 | + address in a register first, for each element, twice in | |
| 786 | + `x[i] - x[j]`: | |
| 787 | + | |
| 788 | + ``` | |
| 789 | + lea r8, [NBody.x] | |
| 790 | + lea r9, [NBody.x] | |
| 791 | + movsd xmm8, [r8 + rbx * 8] | |
| 792 | + subsd xmm8, [r9 + r12 * 8] | |
| 793 | + ``` | |
| 794 | + | |
| 795 | + But the executable is loaded where the linker placed it, below 2 GB, | |
| 796 | + so the array's address fits a 32-bit displacement with no base | |
| 797 | + register at all (`X.memAbs`, a `RIP` base with an index; its fixup | |
| 798 | + holds the address itself, `Chunk.addAbsFixup`, not the distance from | |
| 799 | + the field). `indexArray` notes such an element as it does one in the | |
| 800 | + frame, whatever register its index is in: | |
| 801 | + | |
| 802 | + ``` | |
| 803 | + movsd xmm8, [NBody.x + rbx * 8] | |
| 804 | + subsd xmm8, [NBody.x + r12 * 8] | |
| 805 | + ``` | |
| 806 | + | |
| 807 | + NBody 25% fewer instructions, 16% faster. Grep is 5% slower with | |
| 808 | + the same code in its loops, only moved 96 bytes: where a loop falls | |
| 809 | + changes its speed, as v0.8.0 found. | |
| 782 | 810 | |
| 783 | 811 | ## Diag.raz: messages |
| 784 | 812 |
| @@ -56,6 +56,7 @@ | ||
| 56 | 56 | dataFixups, the 64-bit one, from its chunk's start *) |
| 57 | 57 | chunk*: INT; (* the chunk it refers to *) |
| 58 | 58 | addend*: INT; (* added to the chunk's address *) |
| 59 | + abs*: BOOL; (* text: the address itself, not from the field's end *) | |
| 59 | 60 | end; |
| 60 | 61 | |
| 61 | 62 | var |
| @@ -246,7 +247,15 @@ | ||
| 246 | 247 | fixups[numFixups].at := at; |
| 247 | 248 | fixups[numFixups].chunk := chunk; |
| 248 | 249 | fixups[numFixups].addend := addend; |
| 250 | + fixups[numFixups].abs := false; | |
| 249 | 251 | INC(numFixups); |
| 252 | +end; | |
| 253 | + | |
| 254 | +(* The same, for X64.memAbs: the field holds the address itself *) | |
| 255 | +proc addAbsFixup*(at, chunk, addend: INT); | |
| 256 | +begin | |
| 257 | + addFixup(at, chunk, addend); | |
| 258 | + fixups[numFixups - 1].abs := true; | |
| 250 | 259 | end; |
| 251 | 260 | |
| 252 | 261 | (* The address of chunk, plus addend, at offset at of rodata chunk c, |
| @@ -1640,23 +1640,20 @@ | ||
| 1640 | 1640 | picks of the array at a. An element of 1, 2, |
| 1641 | 1641 | 4 or 8 bytes is only noted, as the operand [base + index * size] |
| 1642 | 1642 | (lIdx): an array in the frame has rbp as its base, and one in a |
| 1643 | - chunk, if the index is a variable's register, its address made in | |
| 1644 | - the value's own register. Any other is added up. *) | |
| 1643 | + chunk its address as the displacement (X.memAbs), with no lea. Any | |
| 1644 | + other is added up. *) | |
| 1645 | 1645 | proc indexArray*(a: Adr; size: I64); |
| 1646 | -var r, b: INT; o: I64; | |
| 1646 | +var r: INT; o: I64; | |
| 1647 | 1647 | begin |
| 1648 | - if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base = bFrame) then | |
| 1648 | + if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and ((a.base = bFrame) or (a.base = bGlobal)) then | |
| 1649 | 1649 | o := indexOff(size, a.off); |
| 1650 | 1650 | r := peek(depth - 1); |
| 1651 | 1651 | drop(); |
| 1652 | - newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); | |
| 1653 | - elsif (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base = bGlobal) and (stack[depth - 1].loc = lHeld) then | |
| 1654 | - o := indexOff(size, 0); | |
| 1655 | - r := peek(depth - 1); | |
| 1656 | - drop(); | |
| 1657 | - b := target(); | |
| 1658 | - X.lea(b, X.memRip(a.chunk, INT(a.off))); | |
| 1659 | - newMem(lIdx, X.memIdx(b, r, INT(size), o), kWord, 8, false); | |
| 1652 | + if a.base = bFrame then | |
| 1653 | + newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); | |
| 1654 | + else | |
| 1655 | + newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false); | |
| 1656 | + end; | |
| 1660 | 1657 | else |
| 1661 | 1658 | mulI(top(), size); |
| 1662 | 1659 | pushAdr(a); |
| @@ -358,9 +358,10 @@ | ||
| 358 | 358 | COPY(textImg[p..p + Chunk.chunks[c].size], Chunk.text[Chunk.chunks[c].start..Chunk.chunks[c].start + Chunk.chunks[c].size]); |
| 359 | 359 | for f := Chunk.chunks[c].firstFix to Chunk.chunks[c].firstFix + Chunk.chunks[c].numFix - 1 do |
| 360 | 360 | at := Chunk.fixups[f].at - Chunk.chunks[c].start; |
| 361 | - (* rel32: from the end of the field to the target *) | |
| 362 | - v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend | |
| 363 | - - (Chunk.chunks[c].adr + at + 4); | |
| 361 | + (* rel32: from the end of the field to the target; | |
| 362 | + or, X64.memAbs's, the target's address *) | |
| 363 | + v := Chunk.chunks[Chunk.fixups[f].chunk].adr + Chunk.fixups[f].addend; | |
| 364 | + if not Chunk.fixups[f].abs then v := v - (Chunk.chunks[c].adr + at + 4); end; | |
| 364 | 365 | patch4(textImg, p + at, v); |
| 365 | 366 | end; |
| 366 | 367 | elsif Chunk.chunks[c].sec = Chunk.secRodata then |
| @@ -35,7 +35,8 @@ | ||
| 35 | 35 | |
| 36 | 36 | type |
| 37 | 37 | Mem* = record |
| 38 | - base*: INT; (* a register, or RIP *) | |
| 38 | + base*: INT; (* a register, or RIP; RIP with an index is the | |
| 39 | + chunk's address itself, as below (memAbs) *) | |
| 39 | 40 | index*: INT; (* a register, or NoIndex *) |
| 40 | 41 | scale*: INT; (* 1, 2, 4 or 8 *) |
| 41 | 42 | disp*: INT; |
| @@ -80,6 +81,19 @@ | ||
| 80 | 81 | begin |
| 81 | 82 | m.base := RIP; m.index := NoIndex; m.scale := 1; |
| 82 | 83 | m.disp := disp; m.chunk := chunk; |
| 84 | + return m; | |
| 85 | +end; | |
| 86 | + | |
| 87 | +(* An element of the array in chunk, [chunk + index * scale + disp]. x86 | |
| 88 | + has no rip + index, but the executable is loaded where the linker put | |
| 89 | + it, below 2 GB, so the chunk's address fits the 32 bits of a | |
| 90 | + displacement with no base: one instruction, where rip-relative needs a | |
| 91 | + lea of the chunk first. *) | |
| 92 | +proc memAbs*(chunk, index, scale, disp: INT): Mem; | |
| 93 | +var m: Mem; | |
| 94 | +begin | |
| 95 | + m := memRip(chunk, disp); | |
| 96 | + m.index := index; m.scale := scale; | |
| 83 | 97 | return m; |
| 84 | 98 | end; |
| 85 | 99 | |
| @@ -130,7 +144,7 @@ | ||
| 130 | 144 | var md, b, sc: INT; |
| 131 | 145 | begin |
| 132 | 146 | byteReg := byteReg and (reg >= 4) and (reg <= 7); |
| 133 | - if m.base = RIP then | |
| 147 | + if (m.base = RIP) and (m.index = NoIndex) then | |
| 134 | 148 | prefix(pfx, w, reg, NoIndex, 0, byteReg); |
| 135 | 149 | else |
| 136 | 150 | prefix(pfx, w, reg, m.index, m.base, byteReg); |
| @@ -138,6 +152,16 @@ | ||
| 138 | 152 | opcode(op); |
| 139 | 153 | reg := reg & 7; |
| 140 | 154 | if m.base = RIP then |
| 155 | + if m.index # NoIndex then | |
| 156 | + 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; | |
| 157 | + (* memAbs: mod 00, r/m 100 for a SIB byte, whose base 101 | |
| 158 | + under mod 00 means none, but a disp32: the chunk's address *) | |
| 159 | + Chunk.put1((reg shl 3) | 4); | |
| 160 | + Chunk.put1((sc shl 6) | ((m.index & 7) shl 3) | 5); | |
| 161 | + Chunk.addAbsFixup(Chunk.pc, m.chunk, m.disp); | |
| 162 | + Chunk.put4(0); | |
| 163 | + return; | |
| 164 | + end; | |
| 141 | 165 | Chunk.put1((reg shl 3) | 5); |
| 142 | 166 | (* mod 00 with r/m 101 is rip + disp32. The displacement counts |
| 143 | 167 | from the end of the instruction, and the fixup takes it to end |
| @@ -345,7 +369,7 @@ | ||
| 345 | 369 | else |
| 346 | 370 | n := 4; |
| 347 | 371 | end; |
| 348 | - if m.base = RIP then m.disp := m.disp - n; end; | |
| 372 | + if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end; | |
| 349 | 373 | if size = 1 then |
| 350 | 374 | opM(0, false, 80H, op, m, false); |
| 351 | 375 | elsif n = 1 then |
| @@ -363,7 +387,7 @@ | ||
| 363 | 387 | if listing then say(text2("mov", mnSized(m, size), num(v))); end; |
| 364 | 388 | n := size; |
| 365 | 389 | if n > 4 then n := 4; end; |
| 366 | - if m.base = RIP then m.disp := m.disp - n; end; | |
| 390 | + if (m.base = RIP) and (m.index = NoIndex) then m.disp := m.disp - n; end; | |
| 367 | 391 | if size = 1 then |
| 368 | 392 | opM(0, false, 0C6H, 0, m, false); |
| 369 | 393 | else |