raz / commit b8d1e313

commit
b8d1e31395e8f532269bfda7c16fbdefc902f65a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
11681e72
Shifts for sizes, constants to the right

v0.7.0 step 5. An index into a slice, and a slice's bounds, are
multiplied by the element's size with a shift where it is a power of
two (MulI), one cycle where imul takes three, on the chain of every
load through an index. A constant on the left of + * & | xor goes
right, where it is an immediate or a shift: 2 * root + 1 is shl, add.
compiler.md's worked example is what the compiler makes now.

rat: within noise (504 -> 506 ms), text -0.2%; a heap sort and a walk
down a list of records 11% faster (116 -> 103 ms).

patch · log

2 files changed, +24 -12

doc/compiler.md (history) modified +8 -8

@@ -13,7 +13,7 @@
1313 | `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
1515 | `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,653 | 1,284 | The stack machine, for x86-64: values, operations, checks, calls, frames |
16+| `src/gen.pas` | 1,665 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames |
1717 | `src/item.pas` | 1,493 | 1,247 | Items: types, constants, variables, operators, conversions, calls |
1818 | `src/parse.pas` | 2,957 | 2,609 | Declarations, statements, expressions, built-ins, recovery, warnings |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
@@ -109,11 +109,8 @@
109109 mov r9,QWORD PTR [rbp-0x10] ; b, at depth 1; the 2 is only noted
110110 shl r9,0x1 ; times 2 is a shift
111111 add r8,r9
112-mov rax,r8 ; the result
113-jmp <leave> ; return
114-xor eax,eax ; running off the end returns zero
115-movq xmm0,rax
116-leave
112+mov rax,r8 ; return: the result
113+leave ; no jump to here, as it is next
117114 ret
118115 ```
119116
@@ -317,8 +314,11 @@
317314 (`JumpOut`), which both ways to its label must find in the same place. `pushed` counts what
318315 is on the machine's stack, which a call must align: the return
319316 address, in a deferred statement. `Op` takes a
320-constant on the right as it is, and a power of two on the right of
321-`*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow.
317+constant on the right as it is, after moving one on the left of `+`,
318+`*`, `&`, `|` or `xor` there, and a power of two on the right of `*`,
319+`div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow; an
320+element's size multiplies an index by a shift too (`MulI`), as a shift
321+takes one cycle and `imul` three.
322322 Narrow integers are brought back to their width after each operation,
323323 which is how `U8` arithmetic wraps; since they are computed in 64 bits,
324324 a shift's count is cut to their width first, and `DivTrap` makes the

src/gen.pas modified +16 -4

@@ -548,6 +548,14 @@
548548 Log2 := n;
549549 end;
550550
551+{ r times v, a size: by a shift where v is a power of two, as it
552+ mostly is, since a shift takes one cycle and imul three }
553+procedure MulI(r: longint; v: int64);
554+begin
555+ if Log2(v) > 0 then ShiftI(shShl, r, Log2(v))
556+ else if v <> 1 then ImulI(r, v);
557+end;
558+
551559 { r compared with v, through rdx if v needs more than 32 bits; with
552560 zero by test, which sets the flags as cmp would and is shorter }
553561 procedure CmpI(r: longint; v: int64);
@@ -637,6 +645,10 @@
637645 begin
638646 if k in [kF32, kF64] then begin RealOp(op, k); exit end;
639647 bits := size * 8;
648+ { a constant on the left of an operation whose order doesn't matter
649+ goes right, where it can be an immediate or a shift }
650+ if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst)
651+ and (stack[depth - 1].loc <> lConst) then Swap;
640652 c := TopConst(v);
641653 sh := -1;
642654 if c and (op in [sTimes, sDiv, sMod]) then sh := Log2(v);
@@ -1096,7 +1108,7 @@
10961108 AluM(aluCmp, r, ml);
10971109 Check(ccB, 0, p, r, ml, 0, -1);
10981110 end;
1099- if size <> 1 then ImulI(r, size);
1111+ MulI(r, size);
11001112 AluM(aluAdd, r, m);
11011113 if inRax then MovRR(Target, RAX)
11021114 else Claim(depth); { the index's register again, now for the address }
@@ -1123,7 +1135,7 @@
11231135 MovRR(RAX, RDX);
11241136 Alu(aluSub, RAX, RCX);
11251137 Store(Mem(RBP, tmp + 8), RAX, 8);
1126- if size <> 1 then ImulI(RCX, size);
1138+ MulI(RCX, size);
11271139 if isArray then Lea(RAX, m) else Load(RAX, m, 8, false);
11281140 Alu(aluAdd, RAX, RCX);
11291141 Store(Mem(RBP, tmp), RAX, 8);
@@ -1144,7 +1156,7 @@
11441156 Check(ccBE, 1, p, RAX, ml, 0, -1);
11451157 end;
11461158 MovRR(RCX, RDX);
1147- if size <> 1 then ImulI(RCX, size);
1159+ MulI(RCX, size);
11481160 Load(RSI, Mem(RBP, sOff), 8, false);
11491161 Load(RDI, Mem(RBP, dOff), 8, false);
11501162 Alu(aluCmp, RDI, RSI);
@@ -1176,7 +1188,7 @@
11761188 MovRR(RAX, RDX);
11771189 Alu(aluSub, RAX, RCX);
11781190 Store(Mem(RBP, tmp + 8), RAX, 8);
1179- if size <> 1 then ImulI(RCX, size);
1191+ MulI(RCX, size);
11801192 Alu(aluAdd, RCX, RDI);
11811193 Store(Mem(RBP, tmp), RCX, 8);
11821194 end;