raz / commit cee2693c

commit
cee2693c965181d315d7613f25bf6dc8ace4d073
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4acc715a
while tested at the bottom

A while loop jumps to its test, after its body, and the test jumps
back to the top, aligned as a for loop's is, while the condition
holds: one jump a round. The condition is compiled where it is
written, for its messages, its code taken back (Rewind), and read
again after the body. A constant condition is no test.

User-space cycles, fewest of nine: rat -2.2% over four runs, heap
sort -2.8%, search +7%, sum within noise; text +0.5%. Without the
alignment: rat +0.6%, heap sort -3.1%, search -0.7%. Tested at the
top as well as the bottom, the condition twice: rat +0.6%, search
+9.5%, text +2.2%.

patch · log

8 files changed, +121 -11

PLAN.md (history) modified +1 -1

@@ -37,7 +37,7 @@
3737 plan, as the sum stayed at 1.75 without them: `t := t + e` and a
3838 `for` loop's step are worked out in the variable's own register,
3939 and a `for` loop's top is aligned to 32 bytes.
40-4. **`while` tested at the bottom**, its test moved after the body and
40+4. Done: **`while` tested at the bottom**, its test moved after the body and
4141 jumped to first, as `for` is now. ulam found nothing in it, and lost
4242 3% where the test was long: measured, and kept only if it pays.
4343 5. **One table of lines, not two.** Rt finds a place in `.debug_line`,

doc/compiler.md (history) modified +30 -0

@@ -387,6 +387,36 @@
387387 of 32 bytes (`AlignLoop`, `Align` in x64), the no-ops before it
388388 never run. The sum 41% faster, the search 6%; rat within noise.
389389
390+4. **`while` tested at the bottom.** A `while` loop tested its
391+ condition at the top and jumped back to it, two jumps a round, one
392+ taken each way:
393+
394+ ```
395+ top: cmp rbx, r13 ; while child < hi
396+ jge out
397+ ... ; the body
398+ jmp top
399+ out:
400+ ```
401+
402+ Now the loop jumps to its test first, which comes after the body
403+ and jumps back to the top while the condition holds:
404+
405+ ```
406+ jmp test
407+ top: ... ; the body, at a multiple of 32 bytes
408+ test: cmp rbx, r13
409+ jl top
410+ ```
411+
412+ One pass reads the condition before the body, so `WhileStatement`
413+ compiles it where it is written, for its messages, takes its code
414+ back (`Rewind`), and reads it again after the body (`SaveScan`,
415+ `RestoreScan`). A constant condition, as in `while true`, is no
416+ test. The heap sort 3% faster, rat 2%; the search lost 7%, all of
417+ it to where its loops fell: without the top aligned it lost
418+ nothing, and rat gained nothing either.
419+
390420 ## diag.pas: messages
391421
392422 Every message the compiler prints is made here. A position, `TPos`, is

src/gen.pas modified +11 -1

@@ -76,7 +76,7 @@
7676 end;
7777
7878 { The code made so far, to go back to for a second try at a body }
79- TCodeMark = record pc, fixups, lines, said: longint; end;
79+ TCodeMark = record pc, fixups, lines, said, fails: longint; end;
8080
8181 { For EntryPoint: the address of chunk, put in the variable whose
8282 chunk is into before the program starts }
@@ -188,6 +188,7 @@
188188 procedure UseRegs(mask: longint);
189189 procedure MarkCode(var m: TCodeMark);
190190 procedure Redo(const m: TCodeMark);
191+procedure Rewind(const m: TCodeMark);
191192 procedure EnterBody(c, line: longint);
192193 procedure ResultTo(off: int64; size: longint);
193194 procedure CopyResult(off, size: int64);
@@ -1707,6 +1708,15 @@
17071708 procedure MarkCode(var m: TCodeMark);
17081709 begin
17091710 m.pc := pc; m.fixups := numFixups; m.lines := numLines; m.said := SaidCount;
1711+ m.fails := numFails;
1712+end;
1713+
1714+{ The code made since m taken back, as if it had never been made; the
1715+ frame is left as it is }
1716+procedure Rewind(const m: TCodeMark);
1717+begin
1718+ pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said);
1719+ numFails := m.fails;
17101720 end;
17111721
17121722 { For a second try at the body: the code as it was at m, and the frame

src/parse.pas modified +35 -9

@@ -1287,24 +1287,50 @@
12871287 ends := all;
12881288 end;
12891289
1290+{ while c do s end: the test comes after the body, and the loop jumps to
1291+ it first, so that a round takes one jump, back to the top if c holds:
1292+
1293+ jmp test rather than top: c, jump to out if false
1294+ top: s s
1295+ test: c, jump to top if true jmp top
1296+ out:
1297+
1298+ One pass reads c before s: so c is compiled where it is written, for
1299+ its messages, and its code taken back (Rewind); then read again, and
1300+ compiled, after s (SaveScan, RestoreScan). }
12901301 procedure WhileStatement(const start: TPos; ind: longint);
1291-var x: TItem; top, L: longint; lp: TLoop; old: PLoop; forever: boolean;
1302+var x: TItem; top, L, test: longint; lp: TLoop; old: PLoop; cond, after: TScanState; m: TCodeMark;
12921303 begin
12931304 Next;
1294- top := pc;
1305+ SaveScan(cond);
1306+ MarkCode(m);
12951307 Expression(x, nil);
1296- forever := (x.mode = mConst) and (x.val <> 0);
1308+ Discard(x);
1309+ Rewind(m);
12971310 Expect(sDo);
1298- L := NoLabel;
1299- CondJump(x, L);
1311+ test := NoLabel;
1312+ Jump(test);
1313+ AlignLoop;
1314+ top := pc;
13001315 Loop(lp, old);
13011316 Statements;
1302- JumpBack(top);
1317+ curLoop := old;
1318+ SaveScan(after);
1319+ RestoreScan(cond);
1320+ Fix(test);
1321+ NoteLine(CurLine, CurCol);
1322+ Expression(x, nil);
1323+ ends := (x.mode = mConst) and (x.val <> 0) and (lp.brk = NoLabel); { while true }
1324+ if (x.mode = mConst) and (x.typ^.form = fBool) then begin
1325+ if x.val <> 0 then JumpBack(top);
1326+ end else begin
1327+ L := NoLabel;
1328+ CondJumpTrue(x, L);
1329+ FixTo(L, top);
1330+ end;
1331+ RestoreScan(after);
13031332 CloseEnd(start, ind);
1304- ends := forever and (lp.brk = NoLabel);
1305- Fix(L);
13061333 Fix(lp.brk);
1307- curLoop := old;
13081334 end;
13091335
13101336 procedure RepeatStatement;

test/run/WhileBottom.expect (history) added +4 -0

@@ -0,0 +1,4 @@
1+5
2+1
3+3 4
4+4

test/run/WhileBottom.raz (history) added +37 -0

@@ -0,0 +1,37 @@
1+(* while, tested after its body: rounds none, one and many; a condition
2+ with and, or and a call; break; while true; and a check that fails in
3+ the condition, told at its place in the source. *)
4+module WhileBottom;
5+import Out;
6+
7+var calls: INT;
8+
9+proc small(n: INT): BOOL;
10+begin
11+ INC(calls);
12+ return n < 3;
13+end;
14+
15+proc main*;
16+var i, n: INT; a: [4]INT;
17+begin
18+ i := 5;
19+ while i < 3 do INC(i); end;
20+ Out.int(i); Out.ln;
21+ i := 0;
22+ while i < 1 do INC(i); end;
23+ Out.int(i); Out.ln;
24+ i := 0;
25+ while (i < 10) and small(i) or (i = 7) do INC(i); end;
26+ Out.int(i); Out.char(' '); Out.int(calls); Out.ln;
27+ n := 0;
28+ while true do
29+ INC(n);
30+ if n = 4 then break; end;
31+ end;
32+ Out.int(n); Out.ln;
33+ i := 0;
34+ while a[i] = 0 do INC(i); end;
35+end;
36+
37+end WhileBottom.

test/run/WhileBottom.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/WhileBottom.stderr (history) added +2 -0

@@ -0,0 +1,2 @@
1+test/run/WhileBottom.raz:34:13: index 4 is out of range for a length of 4
2+ in WhileBottom.main at test/run/WhileBottom.raz:34:13: i = 4, n = 4