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.
| @@ -0,0 +1 @@ | ||
| 1 | +1gh |
| @@ -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. |
| @@ -1570,7 +1570,11 @@ | ||
| 1570 | 1570 | operand, unless it is a constant; for `and` and `or` it makes the left |
| 1571 | 1571 | one a condition (`makeCond`) and jumps past the right one, which `op2` |
| 1572 | 1572 | 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 | |
| 1574 | 1578 | types, folds constants (`foldInt`, `foldReal`), and otherwise pushes the |
| 1575 | 1579 | right operand and has Gen do the operation; a constant on the left goes |
| 1576 | 1580 | on after the right, and the two `swap`. A set's operations are |
| @@ -1188,7 +1188,12 @@ | ||
| 1188 | 1188 | begin |
| 1189 | 1189 | L := Gen.NoLabel; |
| 1190 | 1190 | 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; | |
| 1192 | 1197 | try makeCond(x); |
| 1193 | 1198 | if op = Scan.sAnd then |
| 1194 | 1199 | Gen.jumpOut(negated(x.rel), x.relSigned, x.fj); |
| @@ -1392,7 +1397,7 @@ | ||
| 1392 | 1397 | return Scan.markAt(Diag.join(x.pos, y.pos), say2("{} doesn't apply to {}", Scan.symText(op), Sym.typeName(t))); |
| 1393 | 1398 | end; |
| 1394 | 1399 | |
| 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; | |
| 1396 | 1401 | var xt, yt, t, e: ^Sym.Type; r: Item; scale: INT; isPtr: BOOL; a, b, v: I64; |
| 1397 | 1402 | begin |
| 1398 | 1403 | xt := x.typ; yt := y.typ; |
| @@ -1404,10 +1409,12 @@ | ||
| 1404 | 1409 | if yt^.form # Sym.fBool then return wrongLogic(op, y); end; |
| 1405 | 1410 | if (x.mode = mConst) and (y.mode = mConst) then |
| 1406 | 1411 | 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 *) | |
| 1407 | 1413 | 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 *) | |
| 1409 | 1416 | 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; | |
| 1411 | 1418 | else |
| 1412 | 1419 | try makeCond(y); |
| 1413 | 1420 | if op = Scan.sAnd then x.fj := Gen.joinJumps(x.fj, y.fj); x.tj := y.tj; |