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%.
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | plan, as the sum stayed at 1.75 without them: `t := t + e` and a |
| 38 | 38 | `for` loop's step are worked out in the variable's own register, |
| 39 | 39 | 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 | |
| 41 | 41 | jumped to first, as `for` is now. ulam found nothing in it, and lost |
| 42 | 42 | 3% where the test was long: measured, and kept only if it pays. |
| 43 | 43 | 5. **One table of lines, not two.** Rt finds a place in `.debug_line`, |
| @@ -387,6 +387,36 @@ | ||
| 387 | 387 | of 32 bytes (`AlignLoop`, `Align` in x64), the no-ops before it |
| 388 | 388 | never run. The sum 41% faster, the search 6%; rat within noise. |
| 389 | 389 | |
| 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 | + | |
| 390 | 420 | ## diag.pas: messages |
| 391 | 421 | |
| 392 | 422 | Every message the compiler prints is made here. A position, `TPos`, is |
| @@ -76,7 +76,7 @@ | ||
| 76 | 76 | end; |
| 77 | 77 | |
| 78 | 78 | { 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; | |
| 80 | 80 | |
| 81 | 81 | { For EntryPoint: the address of chunk, put in the variable whose |
| 82 | 82 | chunk is into before the program starts } |
| @@ -188,6 +188,7 @@ | ||
| 188 | 188 | procedure UseRegs(mask: longint); |
| 189 | 189 | procedure MarkCode(var m: TCodeMark); |
| 190 | 190 | procedure Redo(const m: TCodeMark); |
| 191 | +procedure Rewind(const m: TCodeMark); | |
| 191 | 192 | procedure EnterBody(c, line: longint); |
| 192 | 193 | procedure ResultTo(off: int64; size: longint); |
| 193 | 194 | procedure CopyResult(off, size: int64); |
| @@ -1707,6 +1708,15 @@ | ||
| 1707 | 1708 | procedure MarkCode(var m: TCodeMark); |
| 1708 | 1709 | begin |
| 1709 | 1710 | 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; | |
| 1710 | 1720 | end; |
| 1711 | 1721 | |
| 1712 | 1722 | { For a second try at the body: the code as it was at m, and the frame |
| @@ -1287,24 +1287,50 @@ | ||
| 1287 | 1287 | ends := all; |
| 1288 | 1288 | end; |
| 1289 | 1289 | |
| 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). } | |
| 1290 | 1301 | 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; | |
| 1292 | 1303 | begin |
| 1293 | 1304 | Next; |
| 1294 | - top := pc; | |
| 1305 | + SaveScan(cond); | |
| 1306 | + MarkCode(m); | |
| 1295 | 1307 | Expression(x, nil); |
| 1296 | - forever := (x.mode = mConst) and (x.val <> 0); | |
| 1308 | + Discard(x); | |
| 1309 | + Rewind(m); | |
| 1297 | 1310 | Expect(sDo); |
| 1298 | - L := NoLabel; | |
| 1299 | - CondJump(x, L); | |
| 1311 | + test := NoLabel; | |
| 1312 | + Jump(test); | |
| 1313 | + AlignLoop; | |
| 1314 | + top := pc; | |
| 1300 | 1315 | Loop(lp, old); |
| 1301 | 1316 | 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); | |
| 1303 | 1332 | CloseEnd(start, ind); |
| 1304 | - ends := forever and (lp.brk = NoLabel); | |
| 1305 | - Fix(L); | |
| 1306 | 1333 | Fix(lp.brk); |
| 1307 | - curLoop := old; | |
| 1308 | 1334 | end; |
| 1309 | 1335 | |
| 1310 | 1336 | procedure RepeatStatement; |
| @@ -0,0 +1,4 @@ | ||
| 1 | +5 | |
| 2 | +1 | |
| 3 | +3 4 | |
| 4 | +4 |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -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 |