Let "x != lo" narrow x's bounds lo..hi.

This commit is contained in:
Nigel Tao committed 2017-06-10 14:43:16 +10:00
1 parent 9d0558df5a
commit 98a07921b1
4 files changed
+127 -111

No files matched your search

+7 -1
View File
@@ -269,7 +269,13 @@ label_0_continue:;
goto cleanup0; goto cleanup0;
} else if (v_code <= v_save_code) { } else if (v_code <= v_save_code) {
v_s = 4095; v_s = 4095;
v_s = (0 * v_s); while (v_s != 0) {
if (v_s == 0) {
status = puffs_gif_error_bad_gif_image;
goto cleanup0;
}
v_s -= 1;
}
} else { } else {
status = puffs_gif_error_bad_gif_image; status = puffs_gif_error_bad_gif_image;
goto cleanup0; goto cleanup0;
+104 -91
View File
@@ -12,17 +12,37 @@ import (
t "github.com/google/puffs/lang/token" t "github.com/google/puffs/lang/token"
) )
// eqEqOtherHandSide returns the other hand side when n is an expression like // otherHandSide returns the operator and other hand side when n is an
// "thisHS == thatHS" or "thatHS == thisHS". If not, it returns nil. // binary-op expression like "thisHS == thatHS" or "thatHS < thisHS" (which is
func eqEqOtherHandSide(n *a.Expr, thisHS *a.Expr) (thatHS *a.Expr) { // equivalent to "thisHS > thatHS"). If not, it returns (0, nil).
if n.ID0().Key() == t.KeyXBinaryEqEq { func otherHandSide(n *a.Expr, thisHS *a.Expr) (op t.ID, thatHS *a.Expr) {
op = n.ID0()
reverseOp := t.ID(0)
switch op.Key() {
case t.KeyXBinaryNotEq:
reverseOp = t.IDXBinaryNotEq
case t.KeyXBinaryLessThan:
reverseOp = t.IDXBinaryGreaterEq
case t.KeyXBinaryLessEq:
reverseOp = t.IDXBinaryGreaterThan
case t.KeyXBinaryEqEq:
reverseOp = t.IDXBinaryEqEq
case t.KeyXBinaryGreaterEq:
reverseOp = t.IDXBinaryLessThan
case t.KeyXBinaryGreaterThan:
reverseOp = t.IDXBinaryLessEq
}
if reverseOp != 0 {
if thisHS.Eq(n.LHS().Expr()) { if thisHS.Eq(n.LHS().Expr()) {
return n.RHS().Expr() return op, n.RHS().Expr()
} else if thisHS.Eq(n.RHS().Expr()) { }
return n.LHS().Expr() if thisHS.Eq(n.RHS().Expr()) {
return reverseOp, n.LHS().Expr()
} }
} }
return nil return 0, nil
} }
type facts []*a.Expr type facts []*a.Expr
@@ -38,7 +58,7 @@ func (z *facts) appendFact(fact *a.Expr) {
switch fact.ID0().Key() { switch fact.ID0().Key() {
case 0: case 0:
for _, x := range *z { for _, x := range *z {
if other := eqEqOtherHandSide(x, fact); other != nil { if op, other := otherHandSide(x, fact); op.Key() == t.KeyXBinaryEqEq {
z.appendFact(other) z.appendFact(other)
} }
} }
@@ -70,75 +90,63 @@ func (z *facts) update(f func(*a.Expr) *a.Expr) {
*z = (*z)[:i] *z = (*z)[:i]
} }
func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int) (*big.Int, *big.Int) { func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int, tm *t.Map) (*big.Int, *big.Int, error) {
if n.ID0() != 0 || !n.ID1().IsIdent() { if nMin == nil || nMax == nil {
// TODO. return nMin, nMax, nil
return nMin, nMax
} }
for _, x := range z { for _, x := range z {
xMin, xMax := refine(x, n.ID1()) op, other := otherHandSide(x, n)
if xMin != nil && nMin.Cmp(xMin) < 0 { if op == 0 {
nMin = xMin continue
} }
if xMax != nil && nMax.Cmp(xMax) > 0 { cv := other.ConstValue()
nMax = xMax if cv == nil {
continue
} }
}
return nMin, nMax
}
// refine returns fact's lower or upper bound for id. originalNMin, originalNMax, changed := nMin, nMax, false
func refine(fact *a.Expr, id t.ID) (*big.Int, *big.Int) { switch op.Key() {
op := fact.ID0() case t.KeyXBinaryNotEq:
if !op.IsBinaryOp() || op.Key() == t.KeyAs { if nMin.Cmp(cv) == 0 {
return nil, nil nMin = add1(nMin)
} changed = true
} else if nMax.Cmp(cv) == 0 {
nMax = sub1(nMax)
changed = true
}
case t.KeyXBinaryLessThan:
if nMax.Cmp(cv) >= 0 {
nMax = sub1(cv)
changed = true
}
case t.KeyXBinaryLessEq:
if nMax.Cmp(cv) > 0 {
nMax = cv
changed = true
}
case t.KeyXBinaryEqEq:
nMin, nMax = cv, cv
changed = true
case t.KeyXBinaryGreaterEq:
if nMin.Cmp(cv) < 0 {
nMin = cv
changed = true
}
case t.KeyXBinaryGreaterThan:
if nMin.Cmp(cv) <= 0 {
nMin = add1(cv)
changed = true
}
}
lhs := fact.LHS().Expr() if changed && nMin.Cmp(nMax) > 0 {
rhs := fact.RHS().Expr() return nil, nil, fmt.Errorf("check: expression %q bounds [%v..%v] inconsistent with fact %q",
cv := (*big.Int)(nil) n.String(tm), originalNMin, originalNMax, x.String(tm))
idLeft := lhs.ID0() == 0 && lhs.ID1() == id
if idLeft {
cv = rhs.ConstValue()
} else {
if rhs.ID0() != 0 || rhs.ID1() != id {
return nil, nil
}
cv = lhs.ConstValue()
}
if cv == nil {
return nil, nil
}
switch op.Key() {
case t.KeyXBinaryLessThan:
if idLeft {
return nil, sub1(cv)
} else {
return add1(cv), nil
}
case t.KeyXBinaryLessEq:
if idLeft {
return nil, cv
} else {
return cv, nil
}
case t.KeyXBinaryGreaterEq:
if idLeft {
return cv, nil
} else {
return nil, cv
}
case t.KeyXBinaryGreaterThan:
if idLeft {
return add1(cv), nil
} else {
return nil, sub1(cv)
} }
} }
return nil, nil return nMin, nMax, nil
} }
// simplify returns a simplified form of n. For example, (x - x) becomes 0. // simplify returns a simplified form of n. For example, (x - x) becomes 0.
@@ -205,27 +213,25 @@ func proveBinaryOpConstValues(op t.Key, lMin *big.Int, lMax *big.Int, rMin *big.
return false return false
} }
func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) (ok bool) { func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
lhsCV := lhs.ConstValue() lhsCV := lhs.ConstValue()
if lhsCV != nil { if lhsCV != nil {
rMin, rMax, err := q.bcheckExpr(rhs, 0) rMin, rMax, err := q.bcheckExpr(rhs, 0)
if err != nil { if err != nil {
// TODO: should this function return the error? return err
return false
} }
if proveBinaryOpConstValues(op, lhsCV, lhsCV, rMin, rMax) { if proveBinaryOpConstValues(op, lhsCV, lhsCV, rMin, rMax) {
return true return nil
} }
} }
rhsCV := rhs.ConstValue() rhsCV := rhs.ConstValue()
if rhsCV != nil { if rhsCV != nil {
lMin, lMax, err := q.bcheckExpr(lhs, 0) lMin, lMax, err := q.bcheckExpr(lhs, 0)
if err != nil { if err != nil {
// TODO: should this function return the error? return err
return false
} }
if proveBinaryOpConstValues(op, lMin, lMax, rhsCV, rhsCV) { if proveBinaryOpConstValues(op, lMin, lMax, rhsCV, rhsCV) {
return true return nil
} }
} }
@@ -235,29 +241,36 @@ func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) (ok bool) {
} }
factOp := x.ID0().Key() factOp := x.ID0().Key()
if factOp == op && x.RHS().Expr().Eq(rhs) { if factOp == op && x.RHS().Expr().Eq(rhs) {
return true return nil
} }
if factOp == t.KeyXBinaryEqEq && rhsCV != nil { if factOp == t.KeyXBinaryEqEq && rhsCV != nil {
if factCV := x.RHS().Expr().ConstValue(); factCV != nil { if factCV := x.RHS().Expr().ConstValue(); factCV != nil {
switch op { switch op {
case t.KeyXBinaryNotEq: case t.KeyXBinaryNotEq:
return factCV.Cmp(rhsCV) != 0 return errFailedOrNil(factCV.Cmp(rhsCV) != 0)
case t.KeyXBinaryLessThan: case t.KeyXBinaryLessThan:
return factCV.Cmp(rhsCV) < 0 return errFailedOrNil(factCV.Cmp(rhsCV) < 0)
case t.KeyXBinaryLessEq: case t.KeyXBinaryLessEq:
return factCV.Cmp(rhsCV) <= 0 return errFailedOrNil(factCV.Cmp(rhsCV) <= 0)
case t.KeyXBinaryEqEq: case t.KeyXBinaryEqEq:
return factCV.Cmp(rhsCV) == 0 return errFailedOrNil(factCV.Cmp(rhsCV) == 0)
case t.KeyXBinaryGreaterEq: case t.KeyXBinaryGreaterEq:
return factCV.Cmp(rhsCV) >= 0 return errFailedOrNil(factCV.Cmp(rhsCV) >= 0)
case t.KeyXBinaryGreaterThan: case t.KeyXBinaryGreaterThan:
return factCV.Cmp(rhsCV) > 0 return errFailedOrNil(factCV.Cmp(rhsCV) > 0)
} }
} }
} }
} }
return false return errFailed
}
func errFailedOrNil(ok bool) error {
if ok {
return nil
}
return errFailed
} }
var errFailed = errors.New("failed") var errFailed = errors.New("failed")
@@ -279,11 +292,11 @@ var reasons = [...]struct {
if op.Key() != t.KeyXBinaryPlus { if op.Key() != t.KeyXBinaryPlus {
return errFailed return errFailed
} }
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) { if err := q.proveBinaryOp(t.KeyXBinaryLessThan, a, c); err != nil {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.tm), c.String(q.tm)) return fmt.Errorf("cannot prove \"%s < %s\": %v", a.String(q.tm), c.String(q.tm), err)
} }
if !q.proveBinaryOp(t.KeyXBinaryLessEq, zeroExpr, b) { if err := q.proveBinaryOp(t.KeyXBinaryLessEq, zeroExpr, b); err != nil {
return fmt.Errorf("cannot prove \"%s <= %s\"", zeroExpr.String(q.tm), b.String(q.tm)) return fmt.Errorf("cannot prove \"%s <= %s\": %v", zeroExpr.String(q.tm), b.String(q.tm), err)
} }
return nil return nil
}}, }},
@@ -296,11 +309,11 @@ var reasons = [...]struct {
if op.Key() != t.KeyXBinaryLessThan { if op.Key() != t.KeyXBinaryLessThan {
return errFailed return errFailed
} }
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) { if err := q.proveBinaryOp(t.KeyXBinaryLessThan, a, c); err != nil {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.tm), c.String(q.tm)) return fmt.Errorf("cannot prove \"%s < %s\": %v", a.String(q.tm), c.String(q.tm), err)
} }
if !q.proveBinaryOp(t.KeyXBinaryLessEq, c, b) { if err := q.proveBinaryOp(t.KeyXBinaryLessEq, c, b); err != nil {
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.tm), b.String(q.tm)) return fmt.Errorf("cannot prove \"%s <= %s\": %v", c.String(q.tm), b.String(q.tm), err)
} }
return nil return nil
}}, }},
+6 -18
View File
@@ -234,9 +234,7 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
err = fmt.Errorf("no such reason %s", reasonID.String(q.tm)) err = fmt.Errorf("no such reason %s", reasonID.String(q.tm))
} }
} else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs { } else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs {
if q.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) { err = q.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr())
err = nil
}
} }
if err != nil { if err != nil {
if err == errFailed { if err == errFailed {
@@ -559,6 +557,10 @@ func (q *checker) bcheckExpr(n *a.Expr, depth uint32) (*big.Int, *big.Int, error
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
nMin, nMax, err = q.facts.refine(n, nMin, nMax, q.tm)
if err != nil {
return nil, nil, err
}
tMin, tMax, err := q.bcheckTypeExpr(n.MType()) tMin, tMax, err := q.bcheckTypeExpr(n.MType())
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@@ -574,14 +576,6 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (*big.Int, *big.Int, erro
if cv := n.ConstValue(); cv != nil { if cv := n.ConstValue(); cv != nil {
return cv, cv, nil return cv, cv, nil
} }
// Look in the known facts for "n == constValue".
for _, x := range q.facts {
if other := eqEqOtherHandSide(x, n); other != nil {
if cv := other.ConstValue(); cv != nil {
return cv, cv, nil
}
}
}
switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { switch n.ID0().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
case 0: case 0:
return q.bcheckExprOther(n, depth) return q.bcheckExprOther(n, depth)
@@ -673,13 +667,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
default: default:
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key()) return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
} }
return q.bcheckTypeExpr(n.MType())
nMin, nMax, err := q.bcheckTypeExpr(n.MType())
if err != nil {
return nil, nil, err
}
nMin, nMax = q.facts.refine(n, nMin, nMax)
return nMin, nMax, nil
} }
func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { func (q *checker) bcheckExprUnaryOp(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
+10 -1
View File
@@ -63,7 +63,16 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
} else if code <= save_code { } else if code <= save_code {
var s u32[..4095] = 4095 var s u32[..4095] = 4095
s = 0 * s
while s != 0,
inv n_bits < 8,
{
if s == 0 {
return error "bad GIF image"
}
s -= 1
}
// TODO: implement. // TODO: implement.
} else { } else {