From f627f57090b0c5a81a16b8b48c1027fcdd7291f6 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:15:15 +0100 Subject: [PATCH] for loops stepped at the top, tested at the bottom v0.7.0 step 6. A for loop jumps into its body, steps its variable at the top and tests at the end of each round, so a round by 1 is one compare and one jump back where it was two of each and a jump. The report's precise rule is kept: the case where the body has moved the variable past the limit has code of its own after the loop's test (ForNext, ForStep, ForCmp). A constant limit is compared as it is, not stored in a slot and read back. rat: within noise (506 -> 512 ms), text +1.1% for the rare case's code; a matrix product 14% faster (186 -> 160 ms), a sum over a slice 21% (213 -> 168 ms). conform/run/ForEdges checks the rule at its edges and gives what the previous compiler gave. --- diff --git a/conform/run/ForEdges.expect b/conform/run/ForEdges.expect new file mode 100644 index 0000000..0fed993 --- /dev/null +++ b/conform/run/ForEdges.expect @@ -0,0 +1,17 @@ +1 2 3 21 +1 2 8 9 10 10 +1 5 9 9 +1 4 9 +10 9 8 -6 +10 6 2 2 +5 +1 2 3 3 +6 255 +6 0 +11 255 +6 122 +6 127 +4 +0 2 4 6 6 +6 5 1 0 +5 5 diff --git a/conform/run/ForEdges.raz b/conform/run/ForEdges.raz new file mode 100644 index 0000000..776ebc8 --- /dev/null +++ b/conform/run/ForEdges.raz @@ -0,0 +1,42 @@ +module ForEdges; + +(* for loops at their edges: the body moving the variable past the limit + and back, the value left after the loop, steps that would wrap the + type, a limit changed in the body, and enumerations. *) +import Out; + +type Day = (mon, tue, wed, thu, fri, sat, sun); + +proc show(n: I64); begin Out.int(n); Out.str(" "); end; + +proc main*; +var i, b, n: INT; u: U8; k: I8; d: Day; w: U64; +begin + for i := 1 to 10 do show(i); if i = 3 then i := 20; end; end; show(i); Out.ln; + for i := 1 to 10 do show(i); if i = 2 then i := 7; end; end; show(i); Out.ln; + for i := 1 to 10 by 4 do show(i); end; show(i); Out.ln; + for i := 1 to 10 by 3 do show(i); if i = 4 then i := 9; end; end; show(i); Out.ln; + for i := 10 to 1 by -1 do show(i); if i = 8 then i := -5; end; end; show(i); Out.ln; + for i := 10 to 1 by -4 do show(i); end; show(i); Out.ln; + for i := 5 to 1 do show(i); end; show(i); Out.ln; + b := 3; + for i := 1 to b do show(i); b := 10; end; show(i); Out.ln; + n := 0; + for u := 250 to 255 do INC(n); end; show(n); show(I64(u)); Out.ln; + n := 0; + for u := 5 to 0 by -1 do INC(n); end; show(n); show(I64(u)); Out.ln; + n := 0; + for u := 200 to 255 do INC(n); if u = 210 then u := 255; end; end; show(n); show(I64(u)); Out.ln; + n := 0; + for k := -128 to 127 by 50 do INC(n); end; show(n); show(I64(k)); Out.ln; + n := 0; + for k := 120 to 127 do INC(n); if k = 125 then k := 127; end; end; show(n); show(I64(k)); Out.ln; + n := 0; + for w := 0FFFFFFFFFFFFFFF0H to 0FFFFFFFFFFFFFFFFH by 5 do INC(n); end; show(n); Out.ln; + for d := mon to sun by 2 do show(I64(d)); end; show(I64(d)); Out.ln; + for d := sun to mon by -1 do if d = fri then d := tue; end; show(I64(d)); end; Out.ln; + n := 0; + for i := 0 to 9 do if i = 5 then break; end; INC(n); end; show(n); show(i); Out.ln; +end; + +end ForEdges. diff --git a/doc/compiler.md b/doc/compiler.md index 693223e..50e2ffd 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -14,8 +14,8 @@ | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | | `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler | | `src/gen.pas` | 1,665 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,493 | 1,247 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,957 | 2,609 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/item.pas` | 1,535 | 1,277 | Items: types, constants, variables, operators, conversions, calls | +| `src/parse.pas` | 2,966 | 2,615 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 197 | 147 | The command line | @@ -482,7 +482,11 @@ with gen's labels. A branch of an `if` that ends, in `return` or `break` (`ends`), gets no jump past the others; and `if c then break; end` is one jump out of the loop where `c` holds (`ThenPart`, -`LoneBreak`, `CondJumpTrue`), not a jump past a jump. The blank is a symbol of its own, `sBlank`, so +`LoneBreak`, `CondJumpTrue`), not a jump past a jump. A `for` loop +steps its variable at the top and tests at the bottom, so that a round +by 1 is one compare and one jump; its rare case, a body that moves the +variable past the limit, has code of its own after (`ForNext`, whose +comment walks through the report's rule). The blank is a symbol of its own, `sBlank`, so no declaration or expression takes it: `_ := e` loads a single value and drops it, and `MultiAssign` skips the results it stands for. A `match` on an integer reads each arm's diff --git a/src/item.pas b/src/item.pas index 8805216..59f9491 100644 --- a/src/item.pas +++ b/src/item.pas @@ -105,7 +105,8 @@ procedure SliceOf(var x: TItem; t: PType; len: int64); procedure TempItem(var x: TItem; t: PType; off: int64); function ScalarSize(t: PType): longint; -procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); +procedure ForStep(o: PObj; step: int64); +procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint); procedure BeginProc(sig: PType; params: PObj); procedure ReturnValue(var x: TItem); @@ -1316,30 +1317,71 @@ JumpCmp(sLeq, false, body); end; -{ The end of a round of a for loop: stop if the step would pass the - limit, else step. Counting the distance left, rather than comparing - after the step, can't be fooled by the variable wrapping around. } -procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); +{ A for loop's variable o, stepped by step, wrapping as its type does } +procedure ForStep(o: PObj; step: int64); var v, w: TItem; begin - MakeItem(w, o); - if step > 0 then begin - PushVar(FrameAdr(bslot), 8, false, kWord); - Value(w); - end else begin - Value(w); - PushVar(FrameAdr(bslot), 8, false, kWord); - end; - gen.Op(sMinus, kWord, 8, false); - PushConst(kWord, abs(step)); - JumpCmp(sLss, false, exitL); MakeItem(v, o); MakeItem(w, o); Value(w); PushConst(kWord, step); gen.Op(sPlus, kWord, IntSize(o^.typ), Signed(o^.typ)); Assign(v, w); - JumpBack(top); +end; + +{ For ForNext: pushes the variable o and the limit lim, a constant or + the slot it was put in, and jumps to L if the one is op the other, as + the type of o compares } +procedure ForCmp(o: PObj; const lim: TItem; op: TSym; var L: longint); +var v, b: TItem; +begin + MakeItem(v, o); + Value(v); + b := lim; + Value(b); + JumpCmp(op, o^.typ^.signed, L); +end; + +{ The end of a round of a for loop, as the report has it for a positive + step s: if v <= b and b - v < s, the loop ends; otherwise v becomes + v + s and the loop goes on while v <= b. Counting the distance left, + rather than comparing after the step, can't be fooled by v wrapping + around. So the loop goes on, to top, where v is stepped, if v < b for + a step of 1, or if v <= b and b - v >= s for another: then v + s <= b + needs no test. It ends if v = b, or b - v < s. Only where the body has + put v past b is it stepped here and tested, past. A negative step is + the same the other way round. } +procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint); +const + Before: array[boolean] of TSym = (sGtr, sLss); { by a negative step, by a positive } + Past: array[boolean] of TSym = (sLss, sGtr); + Within: array[boolean] of TSym = (sGeq, sLeq); +var v, b: TItem; L, pastL: longint; up: boolean; +begin + up := step > 0; + L := NoLabel; + pastL := NoLabel; + if abs(step) = 1 then begin + ForCmp(o, lim, Before[up], L); + FixTo(L, top); + ForCmp(o, lim, sEql, exitL); + end else begin + ForCmp(o, lim, Past[up], pastL); + MakeItem(v, o); + b := lim; + if up then begin Value(b); Value(v) end + else begin Value(v); Value(b) end; + gen.Op(sMinus, kWord, 8, false); + PushConst(kWord, abs(step)); + JumpCmp(sGeq, false, L); + FixTo(L, top); + Jump(exitL); + end; + Fix(pastL); + ForStep(o, step); + L := NoLabel; + ForCmp(o, lim, Within[up], L); + FixTo(L, body); end; procedure CondJump(var x: TItem; var L: longint); diff --git a/src/parse.pas b/src/parse.pas index 300af58..c8a4e39 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1322,15 +1322,17 @@ end; { for v := a to b by s do ... end, laid out as - v := a; bslot := b - top: if v is past bslot then goto exit - the statements - if the distance from v to bslot < |s| then goto exit (ForNext) - v := v + s; goto top - exit: } + v := a; the limit b, in a slot unless it is a constant + if v is past b then goto exit + goto body + top: v := v + s + body: the statements + the end of a round: goto top, or exit (ForNext) + exit: + so that a round of a loop by 1 takes one compare and one jump. } procedure ForStatement(const start: TPos; ind: longint); var - o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL: longint; + o: PObj; v, a, b, s, lim: TItem; step: int64; top, body, bodyL, exitL: longint; lp: TLoop; old: PLoop; begin Next; @@ -1351,7 +1353,8 @@ Expect(sTo); Expression(b, o^.typ); CoerceExact(b, o^.typ, ''); - bslot := ToSlot(b); + { the limit, evaluated once: a constant as it is, else in a slot } + if b.mode <> mConst then TempItem(b, b.typ, ToSlot(b)); step := 1; if tok = sBy then begin Next; @@ -1361,17 +1364,23 @@ end; Expect(sDo); exitL := NoLabel; - top := pc; MakeItem(v, o); Value(v); - PushVar(FrameAdr(bslot), 8, false, kWord); + lim := b; + Value(lim); if step > 0 then JumpCmp(sGtr, o^.typ^.signed, exitL) else JumpCmp(sLss, o^.typ^.signed, exitL); + bodyL := NoLabel; + Jump(bodyL); + top := pc; + ForStep(o, step); + Fix(bodyL); + body := pc; Loop(lp, old); Statements; curLoop := old; CloseEnd(start, ind); - ForNext(o, bslot, step, exitL, top); + ForNext(o, b, step, exitL, top, body); Fix(exitL); Fix(lp.brk); end; diff --git a/test/list/Listing.expect b/test/list/Listing.expect index 8139459..ae71ccc 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -24,37 +24,40 @@ 17 proc main*; 0 push rbp 1 mov rbp, rsp - 4 sub rsp, 32 + 4 sub rsp, 16 11 mov [rbp - 8], rbx 15 xor ebx, ebx 20 for i := 1 to 3 do 17 mov rbx, 1 - 22 mov qword [rbp - 16], 3 - 30 mov r9, [rbp - 16] - 34 cmp rbx, r9 - 37 jg 129 + 22 cmp rbx, 3 + 26 jg 141 + 32 jmp 47 + 37 mov r8, rbx + 40 add r8, 1 + 44 mov rbx, r8 21 if i # 2 then total := add(total, twice(i)); end; - 43 cmp rbx, 2 - 47 je 97 - 53 mov r8, [Listing.total] - 60 mov [rbp - 24], r8 - 64 mov rdi, rbx - 67 call Listing.twice - 72 mov r9, rax - 75 mov rdi, [rbp - 24] - 79 mov rsi, r9 - 82 call Listing.add - 87 mov r8, rax - 90 mov [Listing.total], r8 - 97 mov r8, [rbp - 16] - 101 sub r8, rbx - 104 cmp r8, 1 - 108 jb 129 - 114 mov r8, rbx - 117 add r8, 1 - 121 mov rbx, r8 - 124 jmp 30 + 47 cmp rbx, 2 + 51 je 101 + 57 mov r8, [Listing.total] + 64 mov [rbp - 16], r8 + 68 mov rdi, rbx + 71 call Listing.twice + 76 mov r9, rax + 79 mov rdi, [rbp - 16] + 83 mov rsi, r9 + 86 call Listing.add + 91 mov r8, rax + 94 mov [Listing.total], r8 + 101 cmp rbx, 3 + 105 jl 37 + 111 cmp rbx, 3 + 115 je 141 + 121 mov r8, rbx + 124 add r8, 1 + 128 mov rbx, r8 + 131 cmp rbx, 3 + 135 jle 47 23 end main; - 129 mov rbx, [rbp - 8] - 133 leave - 134 ret + 141 mov rbx, [rbp - 8] + 145 leave + 146 ret