raz / commit 201d9da2

commit
201d9da20c9e7d268e20a19b39ae6626750d2b3b
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
a861809d
and and or skip their right side after a constant that decides them

false and f() and true or f() called f, as a value and under not,
though 6.2 says the right side is evaluated only when needed: op2
computed y and dropped it. op1 now jumps over y's code to a label
op2 places after it. conform/run/ShortConst holds it.

patch · log

4 files changed, +46 -5

conform/run/ShortConst.expect (history) added +1 -0

@@ -0,0 +1 @@
1+1gh

conform/run/ShortConst.raz (history) added +29 -0

@@ -0,0 +1,29 @@
1+(* and and or evaluate their right side only when needed, also when
2+ the left side is a constant: false and f(), true or f() call no f,
3+ as a value and as a condition, under not too. *)
4+module ShortConst;
5+import Out;
6+
7+(* Says c, so the output shows each call *)
8+proc f(c: U8): BOOL;
9+begin
10+ Out.char(c);
11+ return true;
12+end;
13+
14+proc main*;
15+var b: BOOL;
16+begin
17+ b := false and f('a');
18+ b := true or f('b');
19+ b := not (false and f('c'));
20+ if b then Out.char('1'); end;
21+ if not (true or f('d')) then Out.char('x'); end;
22+ b := false and f('e') and f('f');
23+ b := true and f('g');
24+ b := false or f('h');
25+ if false and true then Out.char('x'); end;
26+ Out.char(10);
27+end;
28+
29+end ShortConst.

doc/compiler.md (history) modified +5 -1

@@ -1570,7 +1570,11 @@
15701570 operand, unless it is a constant; for `and` and `or` it makes the left
15711571 one a condition (`makeCond`) and jumps past the right one, which `op2`
15721572 joins to it: `(a < b) and (c < d)` in an `if` is two compares and two
1573-jumps, as `not` of a condition is the other comparison. `op2` checks the
1573+jumps, as `not` of a condition is the other comparison. A constant
1574+left that decides the result, `false and y` or `true or y`, is the
1575+constant itself: `op1` jumps over the right one's code, about to be
1576+made, to a label `op2` places after it, so that `y` is never run, as
1577+the report says. `op2` checks the
15741578 types, folds constants (`foldInt`, `foldReal`), and otherwise pushes the
15751579 right operand and has Gen do the operation; a constant on the left goes
15761580 on after the right, and the two `swap`. A set's operations are

src/Item.raz (history) modified +11 -4

@@ -1188,7 +1188,12 @@
11881188 begin
11891189 L := Gen.NoLabel;
11901190 if ((op = Scan.sAnd) or (op = Scan.sOr)) and (x.typ^.form = Sym.fBool) then
1191- if x.mode = mConst then return nil; end; (* folded by op2 *)
1191+ if x.mode = mConst then
1192+ (* false and y, true or y: y isn't evaluated, so its code,
1193+ about to be made, is jumped over to L, which op2 places *)
1194+ if (x.val = 1) # (op = Scan.sAnd) then Gen.jump(L); end;
1195+ return nil; (* folded by op2 *)
1196+ end;
11921197 try makeCond(x);
11931198 if op = Scan.sAnd then
11941199 Gen.jumpOut(negated(x.rel), x.relSigned, x.fj);
@@ -1392,7 +1397,7 @@
13921397 return Scan.markAt(Diag.join(x.pos, y.pos), say2("{} doesn't apply to {}", Scan.symText(op), Sym.typeName(t)));
13931398 end;
13941399
1395-proc op2*(op: Scan.Sym; var x, y: Item; _L: INT): ERROR;
1400+proc op2*(op: Scan.Sym; var x, y: Item; L: INT): ERROR;
13961401 var xt, yt, t, e: ^Sym.Type; r: Item; scale: INT; isPtr: BOOL; a, b, v: I64;
13971402 begin
13981403 xt := x.typ; yt := y.typ;
@@ -1404,10 +1409,12 @@
14041409 if yt^.form # Sym.fBool then return wrongLogic(op, y); end;
14051410 if (x.mode = mConst) and (y.mode = mConst) then
14061411 if op = Scan.sAnd then x.val := x.val & y.val; else x.val := x.val | y.val; end;
1412+ Gen.fix(L); (* op1's jump, if it made one, over no code *)
14071413 elsif x.mode = mConst then
1408- (* true and y, false or y: y; else x, y computed and dropped *)
1414+ (* true and y, false or y: y; else x, and y's code, which op1's
1415+ jump to L passes over, is never run *)
14091416 if (x.val = 1) = (op = Scan.sAnd) then x := y;
1410- else try discard(y); end;
1417+ else try discard(y); Gen.fix(L); end;
14111418 else
14121419 try makeCond(y);
14131420 if op = Scan.sAnd then x.fj := Gen.joinJumps(x.fj, y.fj); x.tj := y.tj;