raz / commit 11681e72

commit
11681e72f338c3b044a354ff995d62599011d717
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
dd5e5a98
Conditions as jumps

v0.7.0 step 4. "if c then break; end" is one jump out of the loop
where c holds (ThenPart, LoneBreak, CondJumpTrue), not a jump over a
jump, when no deferred statement waits; a branch of an if that ends
gets no jump past the others; a return that ends a procedure no longer
jumps to the next instruction (DropJumpTo, TakeBack moving the line
notes with it), and the zeros for running off the end are made only
where it can be run off. A compare with zero is test; a BOOL is tested
where it is. labelAt, set and never read, goes.

rat: within noise (505 -> 504 ms), text -0.7%; a search loop that
leaves by break 28% faster (74 -> 53 ms). conform/run/Breaks.

patch · log

9 files changed, +140 -34

conform/run/Breaks.expect (history) added +4 -0

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

conform/run/Breaks.raz (history) added +22 -0

@@ -0,0 +1,22 @@
1+module Breaks;
2+
3+(* Leaving loops by break: on conditions of and and or, before elsif
4+ and else, and with a deferred statement to run on the way out. *)
5+import Out;
6+var log: INT;
7+proc note; begin INC(log); end;
8+proc main*;
9+var i, s: INT; b: BOOL;
10+begin
11+ for i := 0 to 100 do if i > 3 and i # 4 then break; end; s := s + i; end;
12+ Out.int(s); Out.ln; (* 0+1+2+3+4 = 10 *)
13+ s := 0;
14+ for i := 0 to 100 do if i = 7 or i * i > 30 then break; elsif i = 2 then s := s + 100; else s := s + i; end; end;
15+ Out.int(s); Out.ln; (* 0+1+100+3+4+5 = 113 *)
16+ s := 0; i := 0;
17+ while true do b := i = 5; if b then break; end; INC(i); end;
18+ Out.int(i); Out.ln; (* 5 *)
19+ for i := 0 to 3 do defer note; if i = 2 then break; end; end;
20+ Out.int(log); Out.ln; (* 3 *)
21+end;
22+end Breaks.

doc/compiler.md (history) modified +13 -6

@@ -11,11 +11,11 @@
1111 | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout |
14-| `src/chunk.pas` | 302 | 234 | The compiled program: chunks, fixups, line notes |
15-| `src/x64.pas` | 1,113 | 930 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,650 | 1,282 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17-| `src/item.pas` | 1,478 | 1,234 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,927 | 2,588 | Declarations, statements, expressions, built-ins, recovery, warnings |
14+| `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes |
15+| `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler |
16+| `src/gen.pas` | 1,653 | 1,284 | 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 |
1919 | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places |
2020 | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
2121 | `src/raz.pas` | 197 | 147 | The command line |
@@ -263,6 +263,10 @@
263263 the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the
264264 chain and patches each once the label's position is known. This is the
265265 trick from Wirth's compilers, and it means a label is one integer.
266+`DropJumpTo` takes back a `jmp` just made to a label about to be
267+placed after it, so a `return` that ends a procedure doesn't jump to
268+the next instruction; the line notes made after it move back with it
269+(`TakeBack`, in `chunk.pas`).
266270
267271 ## gen.pas: the stack machine
268272
@@ -475,7 +479,10 @@
475479 Then statements: `Statement` makes each one a recovery point (below)
476480 and `OneStatement` dispatches; `IfStatement`,
477481 `WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps
478-with gen's labels. The blank is a symbol of its own, `sBlank`, so
482+with gen's labels. A branch of an `if` that ends, in `return` or
483+`break` (`ends`), gets no jump past the others; and `if c then break;
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
479486 no declaration or expression takes it: `_ := e` loads a single value
480487 and drops it, and `MultiAssign` skips the results it stands for. A
481488 `match` on an integer reads each arm's

src/chunk.pas modified +16 -0

@@ -106,6 +106,7 @@
106106 procedure Put8(v: int64);
107107 procedure Put4At(at, v: longint);
108108 function Get4At(at: longint): longint;
109+procedure TakeBack(n: longint);
109110
110111 implementation
111112
@@ -142,6 +143,21 @@
142143 function Get4At(at: longint): longint;
143144 begin
144145 Get4At := PLongint(@code^[at])^;
146+end;
147+
148+{ The last n bytes of code taken back. A line noted past the new end
149+ now starts there, in place of one noted there before, as NoteLine
150+ would have it. }
151+procedure TakeBack(n: longint);
152+begin
153+ dec(pc, n);
154+ if (numLines > 0) and (lines^[numLines - 1].at > pc) then begin
155+ lines^[numLines - 1].at := pc;
156+ if (numLines > 1) and (lines^[numLines - 2].at = pc) then begin
157+ lines^[numLines - 2] := lines^[numLines - 1];
158+ dec(numLines);
159+ end;
160+ end;
145161 end;
146162
147163 procedure Put8(v: int64);

src/gen.pas modified +12 -9

@@ -161,7 +161,7 @@
161161 procedure ResultTo(off: int64; size: longint);
162162 procedure CopyResult(off, size: int64);
163163 procedure ReturnJump;
164-procedure EndProc(c: longint);
164+procedure EndProc(c: longint; open: boolean);
165165 function DeferBegin(var L, saved: longint): longint;
166166 procedure DeferEnd(L, saved: longint);
167167 procedure RunDefer(at: longint);
@@ -548,10 +548,12 @@
548548 Log2 := n;
549549 end;
550550
551-{ r compared with v, through rdx if v needs more than 32 bits }
551+{ r compared with v, through rdx if v needs more than 32 bits; with
552+ zero by test, which sets the flags as cmp would and is shorter }
552553 procedure CmpI(r: longint; v: int64);
553554 begin
554- if Fits32(v) then AluI(aluCmp, r, v)
555+ if v = 0 then TestRR(r, r)
556+ else if Fits32(v) then AluI(aluCmp, r, v)
555557 else begin MovRI(RDX, v); Alu(aluCmp, r, RDX) end;
556558 end;
557559
@@ -924,7 +926,7 @@
924926 procedure JumpIf(var L: longint; when: boolean);
925927 var r: longint;
926928 begin
927- r := Top;
929+ r := Peek(depth - 1);
928930 Drop;
929931 TestRR(r, r);
930932 if when then Jcc(ccNE, L) else Jcc(ccE, L);
@@ -1499,14 +1501,14 @@
14991501 end;
15001502
15011503 { The end of the procedure in chunk c: the code that returns, and the
1502- frame's size, known only now, patched into its prologue. }
1503-procedure EndProc(c: longint);
1504+ frame's size, known only now, patched into its prologue. If it is
1505+ open, running off its end, it returns zeros. }
1506+procedure EndProc(c: longint; open: boolean);
15041507 var n: int64; i: longint;
15051508 begin
15061509 codeChunk := -1;
15071510 if (depth <> 0) or (pushed <> 0) then Fatal('internal error: stack depth');
1508- { running off the end returns zeros }
1509- if resType <> nil then
1511+ if open then
15101512 if BehindPointer(resType) then begin
15111513 Load(RDI, Mem(RBP, retSlot), 8, false);
15121514 MovRI(RCX, resType^.size);
@@ -1517,7 +1519,8 @@
15171519 MovRI(RAX, 0);
15181520 if InTwoRegs(resType) then MovRI(RDX, 0);
15191521 MovqXR(0, RAX);
1520- end;
1522+ end
1523+ else DropJumpTo(exitL);
15211524 FixLink(exitL);
15221525 n := 0;
15231526 for i := 0 to 4 do

src/item.pas modified +15 -0

@@ -100,6 +100,7 @@
100100 procedure MatchTag(p: int64; tag: int64; var L: longint; isEnum: boolean);
101101 procedure MatchRange(p, lo, hi: int64; var body: longint);
102102 procedure CondJump(var x: TItem; var L: longint);
103+procedure CondJumpTrue(var x: TItem; var L: longint);
103104
104105 procedure SliceOf(var x: TItem; t: PType; len: int64);
105106 procedure TempItem(var x: TItem; t: PType; off: int64);
@@ -1352,6 +1353,20 @@
13521353 end;
13531354 Value(x);
13541355 JumpIf(L, false);
1356+end;
1357+
1358+{ As CondJump, but the jump is taken where the condition holds }
1359+procedure CondJumpTrue(var x: TItem; var L: longint);
1360+begin
1361+ if x.typ^.form <> fBool then MarkAt(x.pos, 'a condition is a BOOL, found ' + What(x));
1362+ if x.mode = mCond then begin
1363+ JumpCmp(x.rel, x.relSigned, L);
1364+ L := JoinJumps(L, x.tj);
1365+ Fix(x.fj);
1366+ exit;
1367+ end;
1368+ Value(x);
1369+ JumpIf(L, true);
13551370 end;
13561371
13571372 { Procedures }

src/parse.pas modified +42 -12

@@ -1216,30 +1216,60 @@
12161216 for i := numDefers - 1 downto base do RunDefer(defers[i]);
12171217 end;
12181218
1219+{ Whether what follows "then" is a lone "break;", with no deferred
1220+ statement to run on the way out of the loop }
1221+function LoneBreak: boolean;
1222+var st: TScanState;
1223+begin
1224+ LoneBreak := false;
1225+ if (tok <> sBreak) or (curLoop = nil) or (numDefers <> curLoop^.base) then exit;
1226+ SaveScan(st);
1227+ Next;
1228+ if tok = sSemicolon then begin
1229+ Next;
1230+ LoneBreak := tok in [sEnd, sElsif, sElse];
1231+ end;
1232+ RestoreScan(st);
1233+end;
1234+
1235+{ The condition x of if or elsif, and the statements it guards, which
1236+ L skips when it is false. "if c then break; end" is one jump out of
1237+ the loop when c holds, not one past a jump out. }
1238+procedure ThenPart(var x: TItem; var L: longint);
1239+begin
1240+ Expect(sThen);
1241+ L := NoLabel;
1242+ if LoneBreak then begin
1243+ NoteLine(CurLine, CurCol);
1244+ CondJumpTrue(x, curLoop^.brk);
1245+ Next; Next; { break; }
1246+ ends := true;
1247+ end else begin
1248+ CondJump(x, L);
1249+ Statements;
1250+ end;
1251+end;
1252+
1253+{ A branch that ends, in return or break, needs no jump past the
1254+ others. }
12191255 procedure IfStatement(const start: TPos; ind: longint);
12201256 var x: TItem; L, endL: longint; all: boolean;
12211257 begin
12221258 Next;
12231259 Expression(x, nil);
1224- Expect(sThen);
1225- L := NoLabel;
1226- CondJump(x, L);
1227- Statements;
1260+ ThenPart(x, L);
12281261 all := ends;
12291262 endL := NoLabel;
12301263 while tok = sElsif do begin
1231- Jump(endL);
1264+ if not ends then Jump(endL);
12321265 Fix(L);
12331266 Next;
12341267 Expression(x, nil);
1235- Expect(sThen);
1236- L := NoLabel;
1237- CondJump(x, L);
1238- Statements;
1268+ ThenPart(x, L);
12391269 all := all and ends;
12401270 end;
12411271 if tok = sElse then begin
1242- Jump(endL);
1272+ if not ends then Jump(endL);
12431273 Fix(L);
12441274 Next;
12451275 if tok = sIf then elseIf := true;
@@ -1950,7 +1980,7 @@
19501980 PlaceLocals(params, before);
19511981 RestoreScan(st);
19521982 until false;
1953- EndProc(p^.chunk);
1983+ EndProc(p^.chunk, p^.openEnd);
19541984 { its parameters and variables, for the trap report }
19551985 if before = nil then o := curModule^.locals else o := before^.link;
19561986 q := params;
@@ -2655,7 +2685,7 @@
26552685 Statements;
26562686 NoteLine(CurLine, CurCol);
26572687 Expect(sEnd);
2658- EndProc(body);
2688+ EndProc(body, false);
26592689 end;
26602690 end else Expect(sEnd);
26612691 if (tok <> sIdent) or (id <> n) then EndMissing(MissingPos, 'expected ' + NameOf(n) + ', found ' + Found);

src/x64.pas modified +14 -2

@@ -44,7 +44,6 @@
4444 end;
4545
4646 var
47- labelAt: longint; { the last place a jump was made to land }
4847 listing: boolean; { -S: each instruction's text is kept, for List }
4948
5049 function Here: TMem;
@@ -106,6 +105,7 @@
106105 procedure JmpBack(target: longint);
107106 procedure JccBack(cc, target: longint);
108107 procedure FixLink(L: longint);
108+procedure DropJumpTo(var L: longint);
109109 procedure FixLinkTo(L, target: longint);
110110 function JoinLinks(L1, L2: longint): longint;
111111
@@ -695,7 +695,6 @@
695695 procedure FixLinkTo(L, target: longint);
696696 var next: longint;
697697 begin
698- if L <> NoLabel then labelAt := target;
699698 while L <> NoLabel do begin
700699 next := Get4At(L);
701700 Put4At(L, target - (L + 4));
@@ -706,6 +705,19 @@
706705 procedure FixLink(L: longint);
707706 begin
708707 FixLinkTo(L, pc);
708+end;
709+
710+{ If the last instruction made is a jmp to L, it is taken back, since L
711+ is to be placed just after it: a return at the end of a procedure
712+ needn't jump to the code that returns. The caller must place L next,
713+ before anything can have noted where the jmp ended. A jcc's field
714+ follows the bytes 0F 8x, never E9. }
715+procedure DropJumpTo(var L: longint);
716+begin
717+ if (L = NoLabel) or (L <> pc - 4) or (code^[L - 1] <> $E9) then exit;
718+ L := Get4At(L);
719+ TakeBack(5);
720+ if listing and (numSaid > 0) and (said[numSaid - 1].at = pc) then dec(numSaid);
709721 end;
710722
711723 { The jumps of chains L1 and L2, as one chain: each jump's field holds

test/list/Listing.expect (history) modified +2 -5

@@ -16,12 +16,9 @@
1616 27 shl r9, 1
1717 31 add r8, r9
1818 34 mov rax, r8
19- 37 jmp 49
2019 15 end add;
21- 42 xor eax, eax
22- 44 movq xmm0, rax
23- 49 leave
24- 50 ret
20+ 37 leave
21+ 38 ret
2522
2623 Listing.main:
2724 17 proc main*;