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.
| @@ -0,0 +1,17 @@ | ||
| 1 | +1 2 3 21 | |
| 2 | +1 2 8 9 10 10 | |
| 3 | +1 5 9 9 | |
| 4 | +1 4 9 | |
| 5 | +10 9 8 -6 | |
| 6 | +10 6 2 2 | |
| 7 | +5 | |
| 8 | +1 2 3 3 | |
| 9 | +6 255 | |
| 10 | +6 0 | |
| 11 | +11 255 | |
| 12 | +6 122 | |
| 13 | +6 127 | |
| 14 | +4 | |
| 15 | +0 2 4 6 6 | |
| 16 | +6 5 1 0 | |
| 17 | +5 5 |
| @@ -0,0 +1,42 @@ | ||
| 1 | +module ForEdges; | |
| 2 | + | |
| 3 | +(* for loops at their edges: the body moving the variable past the limit | |
| 4 | + and back, the value left after the loop, steps that would wrap the | |
| 5 | + type, a limit changed in the body, and enumerations. *) | |
| 6 | +import Out; | |
| 7 | + | |
| 8 | +type Day = (mon, tue, wed, thu, fri, sat, sun); | |
| 9 | + | |
| 10 | +proc show(n: I64); begin Out.int(n); Out.str(" "); end; | |
| 11 | + | |
| 12 | +proc main*; | |
| 13 | +var i, b, n: INT; u: U8; k: I8; d: Day; w: U64; | |
| 14 | +begin | |
| 15 | + for i := 1 to 10 do show(i); if i = 3 then i := 20; end; end; show(i); Out.ln; | |
| 16 | + for i := 1 to 10 do show(i); if i = 2 then i := 7; end; end; show(i); Out.ln; | |
| 17 | + for i := 1 to 10 by 4 do show(i); end; show(i); Out.ln; | |
| 18 | + for i := 1 to 10 by 3 do show(i); if i = 4 then i := 9; end; end; show(i); Out.ln; | |
| 19 | + for i := 10 to 1 by -1 do show(i); if i = 8 then i := -5; end; end; show(i); Out.ln; | |
| 20 | + for i := 10 to 1 by -4 do show(i); end; show(i); Out.ln; | |
| 21 | + for i := 5 to 1 do show(i); end; show(i); Out.ln; | |
| 22 | + b := 3; | |
| 23 | + for i := 1 to b do show(i); b := 10; end; show(i); Out.ln; | |
| 24 | + n := 0; | |
| 25 | + for u := 250 to 255 do INC(n); end; show(n); show(I64(u)); Out.ln; | |
| 26 | + n := 0; | |
| 27 | + for u := 5 to 0 by -1 do INC(n); end; show(n); show(I64(u)); Out.ln; | |
| 28 | + n := 0; | |
| 29 | + for u := 200 to 255 do INC(n); if u = 210 then u := 255; end; end; show(n); show(I64(u)); Out.ln; | |
| 30 | + n := 0; | |
| 31 | + for k := -128 to 127 by 50 do INC(n); end; show(n); show(I64(k)); Out.ln; | |
| 32 | + n := 0; | |
| 33 | + for k := 120 to 127 do INC(n); if k = 125 then k := 127; end; end; show(n); show(I64(k)); Out.ln; | |
| 34 | + n := 0; | |
| 35 | + for w := 0FFFFFFFFFFFFFFF0H to 0FFFFFFFFFFFFFFFFH by 5 do INC(n); end; show(n); Out.ln; | |
| 36 | + for d := mon to sun by 2 do show(I64(d)); end; show(I64(d)); Out.ln; | |
| 37 | + for d := sun to mon by -1 do if d = fri then d := tue; end; show(I64(d)); end; Out.ln; | |
| 38 | + n := 0; | |
| 39 | + for i := 0 to 9 do if i = 5 then break; end; INC(n); end; show(n); show(i); Out.ln; | |
| 40 | +end; | |
| 41 | + | |
| 42 | +end ForEdges. |
| @@ -14,8 +14,8 @@ | ||
| 14 | 14 | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | |
| 15 | 15 | | `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler | |
| 16 | 16 | | `src/gen.pas` | 1,665 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 17 | -| `src/item.pas` | 1,493 | 1,247 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | -| `src/parse.pas` | 2,957 | 2,609 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 17 | +| `src/item.pas` | 1,535 | 1,277 | Items: types, constants, variables, operators, conversions, calls | | |
| 18 | +| `src/parse.pas` | 2,966 | 2,615 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | 19 | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | |
| 20 | 20 | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 21 | 21 | | `src/raz.pas` | 197 | 147 | The command line | |
| @@ -482,7 +482,11 @@ | ||
| 482 | 482 | with gen's labels. A branch of an `if` that ends, in `return` or |
| 483 | 483 | `break` (`ends`), gets no jump past the others; and `if c then break; |
| 484 | 484 | end` is one jump out of the loop where `c` holds (`ThenPart`, |
| 485 | -`LoneBreak`, `CondJumpTrue`), not a jump past a jump. The blank is a symbol of its own, `sBlank`, so | |
| 485 | +`LoneBreak`, `CondJumpTrue`), not a jump past a jump. A `for` loop | |
| 486 | +steps its variable at the top and tests at the bottom, so that a round | |
| 487 | +by 1 is one compare and one jump; its rare case, a body that moves the | |
| 488 | +variable past the limit, has code of its own after (`ForNext`, whose | |
| 489 | +comment walks through the report's rule). The blank is a symbol of its own, `sBlank`, so | |
| 486 | 490 | no declaration or expression takes it: `_ := e` loads a single value |
| 487 | 491 | and drops it, and `MultiAssign` skips the results it stands for. A |
| 488 | 492 | `match` on an integer reads each arm's |
| @@ -105,7 +105,8 @@ | ||
| 105 | 105 | procedure SliceOf(var x: TItem; t: PType; len: int64); |
| 106 | 106 | procedure TempItem(var x: TItem; t: PType; off: int64); |
| 107 | 107 | function ScalarSize(t: PType): longint; |
| 108 | -procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 108 | +procedure ForStep(o: PObj; step: int64); | |
| 109 | +procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint); | |
| 109 | 110 | |
| 110 | 111 | procedure BeginProc(sig: PType; params: PObj); |
| 111 | 112 | procedure ReturnValue(var x: TItem); |
| @@ -1316,30 +1317,71 @@ | ||
| 1316 | 1317 | JumpCmp(sLeq, false, body); |
| 1317 | 1318 | end; |
| 1318 | 1319 | |
| 1319 | -{ The end of a round of a for loop: stop if the step would pass the | |
| 1320 | - limit, else step. Counting the distance left, rather than comparing | |
| 1321 | - after the step, can't be fooled by the variable wrapping around. } | |
| 1322 | -procedure ForNext(o: PObj; bslot, step: int64; var exitL: longint; top: longint); | |
| 1320 | +{ A for loop's variable o, stepped by step, wrapping as its type does } | |
| 1321 | +procedure ForStep(o: PObj; step: int64); | |
| 1323 | 1322 | var v, w: TItem; |
| 1324 | 1323 | begin |
| 1325 | - MakeItem(w, o); | |
| 1326 | - if step > 0 then begin | |
| 1327 | - PushVar(FrameAdr(bslot), 8, false, kWord); | |
| 1328 | - Value(w); | |
| 1329 | - end else begin | |
| 1330 | - Value(w); | |
| 1331 | - PushVar(FrameAdr(bslot), 8, false, kWord); | |
| 1332 | - end; | |
| 1333 | - gen.Op(sMinus, kWord, 8, false); | |
| 1334 | - PushConst(kWord, abs(step)); | |
| 1335 | - JumpCmp(sLss, false, exitL); | |
| 1336 | 1324 | MakeItem(v, o); |
| 1337 | 1325 | MakeItem(w, o); |
| 1338 | 1326 | Value(w); |
| 1339 | 1327 | PushConst(kWord, step); |
| 1340 | 1328 | gen.Op(sPlus, kWord, IntSize(o^.typ), Signed(o^.typ)); |
| 1341 | 1329 | Assign(v, w); |
| 1342 | - JumpBack(top); | |
| 1330 | +end; | |
| 1331 | + | |
| 1332 | +{ For ForNext: pushes the variable o and the limit lim, a constant or | |
| 1333 | + the slot it was put in, and jumps to L if the one is op the other, as | |
| 1334 | + the type of o compares } | |
| 1335 | +procedure ForCmp(o: PObj; const lim: TItem; op: TSym; var L: longint); | |
| 1336 | +var v, b: TItem; | |
| 1337 | +begin | |
| 1338 | + MakeItem(v, o); | |
| 1339 | + Value(v); | |
| 1340 | + b := lim; | |
| 1341 | + Value(b); | |
| 1342 | + JumpCmp(op, o^.typ^.signed, L); | |
| 1343 | +end; | |
| 1344 | + | |
| 1345 | +{ The end of a round of a for loop, as the report has it for a positive | |
| 1346 | + step s: if v <= b and b - v < s, the loop ends; otherwise v becomes | |
| 1347 | + v + s and the loop goes on while v <= b. Counting the distance left, | |
| 1348 | + rather than comparing after the step, can't be fooled by v wrapping | |
| 1349 | + around. So the loop goes on, to top, where v is stepped, if v < b for | |
| 1350 | + a step of 1, or if v <= b and b - v >= s for another: then v + s <= b | |
| 1351 | + needs no test. It ends if v = b, or b - v < s. Only where the body has | |
| 1352 | + put v past b is it stepped here and tested, past. A negative step is | |
| 1353 | + the same the other way round. } | |
| 1354 | +procedure ForNext(o: PObj; const lim: TItem; step: int64; var exitL: longint; top, body: longint); | |
| 1355 | +const | |
| 1356 | + Before: array[boolean] of TSym = (sGtr, sLss); { by a negative step, by a positive } | |
| 1357 | + Past: array[boolean] of TSym = (sLss, sGtr); | |
| 1358 | + Within: array[boolean] of TSym = (sGeq, sLeq); | |
| 1359 | +var v, b: TItem; L, pastL: longint; up: boolean; | |
| 1360 | +begin | |
| 1361 | + up := step > 0; | |
| 1362 | + L := NoLabel; | |
| 1363 | + pastL := NoLabel; | |
| 1364 | + if abs(step) = 1 then begin | |
| 1365 | + ForCmp(o, lim, Before[up], L); | |
| 1366 | + FixTo(L, top); | |
| 1367 | + ForCmp(o, lim, sEql, exitL); | |
| 1368 | + end else begin | |
| 1369 | + ForCmp(o, lim, Past[up], pastL); | |
| 1370 | + MakeItem(v, o); | |
| 1371 | + b := lim; | |
| 1372 | + if up then begin Value(b); Value(v) end | |
| 1373 | + else begin Value(v); Value(b) end; | |
| 1374 | + gen.Op(sMinus, kWord, 8, false); | |
| 1375 | + PushConst(kWord, abs(step)); | |
| 1376 | + JumpCmp(sGeq, false, L); | |
| 1377 | + FixTo(L, top); | |
| 1378 | + Jump(exitL); | |
| 1379 | + end; | |
| 1380 | + Fix(pastL); | |
| 1381 | + ForStep(o, step); | |
| 1382 | + L := NoLabel; | |
| 1383 | + ForCmp(o, lim, Within[up], L); | |
| 1384 | + FixTo(L, body); | |
| 1343 | 1385 | end; |
| 1344 | 1386 | |
| 1345 | 1387 | procedure CondJump(var x: TItem; var L: longint); |
| @@ -1322,15 +1322,17 @@ | ||
| 1322 | 1322 | end; |
| 1323 | 1323 | |
| 1324 | 1324 | { for v := a to b by s do ... end, laid out as |
| 1325 | - v := a; bslot := b | |
| 1326 | - top: if v is past bslot then goto exit | |
| 1327 | - the statements | |
| 1328 | - if the distance from v to bslot < |s| then goto exit (ForNext) | |
| 1329 | - v := v + s; goto top | |
| 1330 | - exit: } | |
| 1325 | + v := a; the limit b, in a slot unless it is a constant | |
| 1326 | + if v is past b then goto exit | |
| 1327 | + goto body | |
| 1328 | + top: v := v + s | |
| 1329 | + body: the statements | |
| 1330 | + the end of a round: goto top, or exit (ForNext) | |
| 1331 | + exit: | |
| 1332 | + so that a round of a loop by 1 takes one compare and one jump. } | |
| 1331 | 1333 | procedure ForStatement(const start: TPos; ind: longint); |
| 1332 | 1334 | var |
| 1333 | - o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL: longint; | |
| 1335 | + o: PObj; v, a, b, s, lim: TItem; step: int64; top, body, bodyL, exitL: longint; | |
| 1334 | 1336 | lp: TLoop; old: PLoop; |
| 1335 | 1337 | begin |
| 1336 | 1338 | Next; |
| @@ -1351,7 +1353,8 @@ | ||
| 1351 | 1353 | Expect(sTo); |
| 1352 | 1354 | Expression(b, o^.typ); |
| 1353 | 1355 | CoerceExact(b, o^.typ, ''); |
| 1354 | - bslot := ToSlot(b); | |
| 1356 | + { the limit, evaluated once: a constant as it is, else in a slot } | |
| 1357 | + if b.mode <> mConst then TempItem(b, b.typ, ToSlot(b)); | |
| 1355 | 1358 | step := 1; |
| 1356 | 1359 | if tok = sBy then begin |
| 1357 | 1360 | Next; |
| @@ -1361,17 +1364,23 @@ | ||
| 1361 | 1364 | end; |
| 1362 | 1365 | Expect(sDo); |
| 1363 | 1366 | exitL := NoLabel; |
| 1364 | - top := pc; | |
| 1365 | 1367 | MakeItem(v, o); |
| 1366 | 1368 | Value(v); |
| 1367 | - PushVar(FrameAdr(bslot), 8, false, kWord); | |
| 1369 | + lim := b; | |
| 1370 | + Value(lim); | |
| 1368 | 1371 | if step > 0 then JumpCmp(sGtr, o^.typ^.signed, exitL) |
| 1369 | 1372 | else JumpCmp(sLss, o^.typ^.signed, exitL); |
| 1373 | + bodyL := NoLabel; | |
| 1374 | + Jump(bodyL); | |
| 1375 | + top := pc; | |
| 1376 | + ForStep(o, step); | |
| 1377 | + Fix(bodyL); | |
| 1378 | + body := pc; | |
| 1370 | 1379 | Loop(lp, old); |
| 1371 | 1380 | Statements; |
| 1372 | 1381 | curLoop := old; |
| 1373 | 1382 | CloseEnd(start, ind); |
| 1374 | - ForNext(o, bslot, step, exitL, top); | |
| 1383 | + ForNext(o, b, step, exitL, top, body); | |
| 1375 | 1384 | Fix(exitL); |
| 1376 | 1385 | Fix(lp.brk); |
| 1377 | 1386 | end; |
| @@ -24,37 +24,40 @@ | ||
| 24 | 24 | 17 proc main*; |
| 25 | 25 | 0 push rbp |
| 26 | 26 | 1 mov rbp, rsp |
| 27 | - 4 sub rsp, 32 | |
| 27 | + 4 sub rsp, 16 | |
| 28 | 28 | 11 mov [rbp - 8], rbx |
| 29 | 29 | 15 xor ebx, ebx |
| 30 | 30 | 20 for i := 1 to 3 do |
| 31 | 31 | 17 mov rbx, 1 |
| 32 | - 22 mov qword [rbp - 16], 3 | |
| 33 | - 30 mov r9, [rbp - 16] | |
| 34 | - 34 cmp rbx, r9 | |
| 35 | - 37 jg 129 | |
| 32 | + 22 cmp rbx, 3 | |
| 33 | + 26 jg 141 | |
| 34 | + 32 jmp 47 | |
| 35 | + 37 mov r8, rbx | |
| 36 | + 40 add r8, 1 | |
| 37 | + 44 mov rbx, r8 | |
| 36 | 38 | 21 if i # 2 then total := add(total, twice(i)); end; |
| 37 | - 43 cmp rbx, 2 | |
| 38 | - 47 je 97 | |
| 39 | - 53 mov r8, [Listing.total] | |
| 40 | - 60 mov [rbp - 24], r8 | |
| 41 | - 64 mov rdi, rbx | |
| 42 | - 67 call Listing.twice | |
| 43 | - 72 mov r9, rax | |
| 44 | - 75 mov rdi, [rbp - 24] | |
| 45 | - 79 mov rsi, r9 | |
| 46 | - 82 call Listing.add | |
| 47 | - 87 mov r8, rax | |
| 48 | - 90 mov [Listing.total], r8 | |
| 49 | - 97 mov r8, [rbp - 16] | |
| 50 | - 101 sub r8, rbx | |
| 51 | - 104 cmp r8, 1 | |
| 52 | - 108 jb 129 | |
| 53 | - 114 mov r8, rbx | |
| 54 | - 117 add r8, 1 | |
| 55 | - 121 mov rbx, r8 | |
| 56 | - 124 jmp 30 | |
| 39 | + 47 cmp rbx, 2 | |
| 40 | + 51 je 101 | |
| 41 | + 57 mov r8, [Listing.total] | |
| 42 | + 64 mov [rbp - 16], r8 | |
| 43 | + 68 mov rdi, rbx | |
| 44 | + 71 call Listing.twice | |
| 45 | + 76 mov r9, rax | |
| 46 | + 79 mov rdi, [rbp - 16] | |
| 47 | + 83 mov rsi, r9 | |
| 48 | + 86 call Listing.add | |
| 49 | + 91 mov r8, rax | |
| 50 | + 94 mov [Listing.total], r8 | |
| 51 | + 101 cmp rbx, 3 | |
| 52 | + 105 jl 37 | |
| 53 | + 111 cmp rbx, 3 | |
| 54 | + 115 je 141 | |
| 55 | + 121 mov r8, rbx | |
| 56 | + 124 add r8, 1 | |
| 57 | + 128 mov rbx, r8 | |
| 58 | + 131 cmp rbx, 3 | |
| 59 | + 135 jle 47 | |
| 57 | 60 | 23 end main; |
| 58 | - 129 mov rbx, [rbp - 8] | |
| 59 | - 133 leave | |
| 60 | - 134 ret | |
| 61 | + 141 mov rbx, [rbp - 8] | |
| 62 | + 145 leave | |
| 63 | + 146 ret |