From cee2693c965181d315d7613f25bf6dc8ace4d073 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:24:28 +0100 Subject: [PATCH] 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%. --- diff --git a/PLAN.md b/PLAN.md index 45cbbe6..9be170a 100644 --- a/PLAN.md +++ b/PLAN.md @@ -37,7 +37,7 @@ plan, as the sum stayed at 1.75 without them: `t := t + e` and a `for` loop's step are worked out in the variable's own register, and a `for` loop's top is aligned to 32 bytes. -4. **`while` tested at the bottom**, its test moved after the body and +4. Done: **`while` tested at the bottom**, its test moved after the body and jumped to first, as `for` is now. ulam found nothing in it, and lost 3% where the test was long: measured, and kept only if it pays. 5. **One table of lines, not two.** Rt finds a place in `.debug_line`, diff --git a/doc/compiler.md b/doc/compiler.md index 0e3310a..1a42159 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -387,6 +387,36 @@ of 32 bytes (`AlignLoop`, `Align` in x64), the no-ops before it never run. The sum 41% faster, the search 6%; rat within noise. +4. **`while` tested at the bottom.** A `while` loop tested its + condition at the top and jumped back to it, two jumps a round, one + taken each way: + + ``` + top: cmp rbx, r13 ; while child < hi + jge out + ... ; the body + jmp top + out: + ``` + + Now the loop jumps to its test first, which comes after the body + and jumps back to the top while the condition holds: + + ``` + jmp test + top: ... ; the body, at a multiple of 32 bytes + test: cmp rbx, r13 + jl top + ``` + + One pass reads the condition before the body, so `WhileStatement` + compiles it where it is written, for its messages, takes its code + back (`Rewind`), and reads it again after the body (`SaveScan`, + `RestoreScan`). A constant condition, as in `while true`, is no + test. The heap sort 3% faster, rat 2%; the search lost 7%, all of + it to where its loops fell: without the top aligned it lost + nothing, and rat gained nothing either. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is diff --git a/src/gen.pas b/src/gen.pas index 27acef7..5f9579a 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -76,7 +76,7 @@ end; { The code made so far, to go back to for a second try at a body } - TCodeMark = record pc, fixups, lines, said: longint; end; + TCodeMark = record pc, fixups, lines, said, fails: longint; end; { For EntryPoint: the address of chunk, put in the variable whose chunk is into before the program starts } @@ -188,6 +188,7 @@ procedure UseRegs(mask: longint); procedure MarkCode(var m: TCodeMark); procedure Redo(const m: TCodeMark); +procedure Rewind(const m: TCodeMark); procedure EnterBody(c, line: longint); procedure ResultTo(off: int64; size: longint); procedure CopyResult(off, size: int64); @@ -1707,6 +1708,15 @@ procedure MarkCode(var m: TCodeMark); begin m.pc := pc; m.fixups := numFixups; m.lines := numLines; m.said := SaidCount; + m.fails := numFails; +end; + +{ The code made since m taken back, as if it had never been made; the + frame is left as it is } +procedure Rewind(const m: TCodeMark); +begin + pc := m.pc; numFixups := m.fixups; numLines := m.lines; Unsay(m.said); + numFails := m.fails; end; { For a second try at the body: the code as it was at m, and the frame diff --git a/src/parse.pas b/src/parse.pas index 27ef574..faa493d 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1287,24 +1287,50 @@ ends := all; end; +{ while c do s end: the test comes after the body, and the loop jumps to + it first, so that a round takes one jump, back to the top if c holds: + + jmp test rather than top: c, jump to out if false + top: s s + test: c, jump to top if true jmp top + out: + + One pass reads c before s: so c is compiled where it is written, for + its messages, and its code taken back (Rewind); then read again, and + compiled, after s (SaveScan, RestoreScan). } procedure WhileStatement(const start: TPos; ind: longint); -var x: TItem; top, L: longint; lp: TLoop; old: PLoop; forever: boolean; +var x: TItem; top, L, test: longint; lp: TLoop; old: PLoop; cond, after: TScanState; m: TCodeMark; begin Next; - top := pc; + SaveScan(cond); + MarkCode(m); Expression(x, nil); - forever := (x.mode = mConst) and (x.val <> 0); + Discard(x); + Rewind(m); Expect(sDo); - L := NoLabel; - CondJump(x, L); + test := NoLabel; + Jump(test); + AlignLoop; + top := pc; Loop(lp, old); Statements; - JumpBack(top); + curLoop := old; + SaveScan(after); + RestoreScan(cond); + Fix(test); + NoteLine(CurLine, CurCol); + Expression(x, nil); + ends := (x.mode = mConst) and (x.val <> 0) and (lp.brk = NoLabel); { while true } + if (x.mode = mConst) and (x.typ^.form = fBool) then begin + if x.val <> 0 then JumpBack(top); + end else begin + L := NoLabel; + CondJumpTrue(x, L); + FixTo(L, top); + end; + RestoreScan(after); CloseEnd(start, ind); - ends := forever and (lp.brk = NoLabel); - Fix(L); Fix(lp.brk); - curLoop := old; end; procedure RepeatStatement; diff --git a/test/run/WhileBottom.expect b/test/run/WhileBottom.expect new file mode 100644 index 0000000..610e13e --- /dev/null +++ b/test/run/WhileBottom.expect @@ -0,0 +1,4 @@ +5 +1 +3 4 +4 diff --git a/test/run/WhileBottom.raz b/test/run/WhileBottom.raz new file mode 100644 index 0000000..0530030 --- /dev/null +++ b/test/run/WhileBottom.raz @@ -0,0 +1,37 @@ +(* while, tested after its body: rounds none, one and many; a condition + with and, or and a call; break; while true; and a check that fails in + the condition, told at its place in the source. *) +module WhileBottom; +import Out; + +var calls: INT; + +proc small(n: INT): BOOL; +begin + INC(calls); + return n < 3; +end; + +proc main*; +var i, n: INT; a: [4]INT; +begin + i := 5; + while i < 3 do INC(i); end; + Out.int(i); Out.ln; + i := 0; + while i < 1 do INC(i); end; + Out.int(i); Out.ln; + i := 0; + while (i < 10) and small(i) or (i = 7) do INC(i); end; + Out.int(i); Out.char(' '); Out.int(calls); Out.ln; + n := 0; + while true do + INC(n); + if n = 4 then break; end; + end; + Out.int(n); Out.ln; + i := 0; + while a[i] = 0 do INC(i); end; +end; + +end WhileBottom. diff --git a/test/run/WhileBottom.status b/test/run/WhileBottom.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/WhileBottom.status @@ -0,0 +1 @@ +2 diff --git a/test/run/WhileBottom.stderr b/test/run/WhileBottom.stderr new file mode 100644 index 0000000..ae23bbd --- /dev/null +++ b/test/run/WhileBottom.stderr @@ -0,0 +1,2 @@ +test/run/WhileBottom.raz:34:13: index 4 is out of range for a length of 4 + in WhileBottom.main at test/run/WhileBottom.raz:34:13: i = 4, n = 4