From 3f5bb7a02b4800ee37ec156aeb60d80b346a083c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 1 Jun 2017 17:11:01 +1000 Subject: [PATCH] Check a while loop's pre and inv conditions. --- gen/c/gif.c | 1 + lang/ast/eq.go | 18 +++++++++++ lang/check/assert.go | 51 ++++++++++++++++++++++++++++--- lang/check/bounds.go | 66 ++++++++++++++++++++++++++-------------- std/gif/decode_lzw.puffs | 8 ++++- 5 files changed, 115 insertions(+), 29 deletions(-) diff --git a/gen/c/gif.c b/gen/c/gif.c index 38545611..521ac01a 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -168,6 +168,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) { v_n_bits += 8; } goto label_0_break; + v_n_bits = 0; } label_0_break:; diff --git a/lang/ast/eq.go b/lang/ast/eq.go index 0f0e4cab..62c2eb2b 100644 --- a/lang/ast/eq.go +++ b/lang/ast/eq.go @@ -53,6 +53,24 @@ func (n *Expr) Eq(o *Expr) bool { return true } +func (n *Expr) Mentions(o *Expr) bool { + if n == nil { + return false + } + if n.Eq(o) || + n.lhs.Expr().Mentions(o) || + n.mhs.Expr().Mentions(o) || + (n.id0.Key() != t.KeyXBinaryAs && n.rhs.Expr().Mentions(o)) { + return true + } + for _, x := range n.list0 { + if x.Expr().Mentions(o) { + return true + } + } + return false +} + // Eq returns whether n and o are equal. func (n *TypeExpr) Eq(o *TypeExpr) bool { return n.eq(o, false) diff --git a/lang/check/assert.go b/lang/check/assert.go index 2f06b1e3..daaede24 100644 --- a/lang/check/assert.go +++ b/lang/check/assert.go @@ -24,6 +24,22 @@ func (z *facts) appendFact(x *a.Expr) { *z = append(*z, x) } +// drop drops any facts that mention x. They are replaced with a nil *a.Expr, +// and then the slice is compacted to remove all nils. +func (z *facts) drop(x *a.Expr) { + i := 0 + for _, f := range *z { + if !f.Mentions(x) { + (*z)[i] = f + i++ + } + } + for j := i; j < len(*z); j++ { + (*z)[j] = nil + } + *z = (*z)[:i] +} + func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int) (*big.Int, *big.Int) { if n.ID0() != 0 || !n.ID1().IsIdent() { // TODO. @@ -137,31 +153,56 @@ func proveBinaryOpConstValues(op t.Key, lMin *big.Int, lMax *big.Int, rMin *big. } func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) (ok bool) { - if cv := lhs.ConstValue(); cv != nil { + lhsCV := lhs.ConstValue() + if lhsCV != nil { rMin, rMax, err := q.bcheckExpr(rhs, 0) if err != nil { // TODO: should this function return the error? return false } - if proveBinaryOpConstValues(op, cv, cv, rMin, rMax) { + if proveBinaryOpConstValues(op, lhsCV, lhsCV, rMin, rMax) { return true } } - if cv := rhs.ConstValue(); cv != nil { + rhsCV := rhs.ConstValue() + if rhsCV != nil { lMin, lMax, err := q.bcheckExpr(lhs, 0) if err != nil { // TODO: should this function return the error? return false } - if proveBinaryOpConstValues(op, lMin, lMax, cv, cv) { + if proveBinaryOpConstValues(op, lMin, lMax, rhsCV, rhsCV) { return true } } for _, f := range q.facts { - if f.ID0().Key() == op && f.LHS().Expr().Eq(lhs) && f.RHS().Expr().Eq(rhs) { + if !f.LHS().Expr().Eq(lhs) { + continue + } + factOp := f.ID0().Key() + if factOp == op && f.RHS().Expr().Eq(rhs) { return true } + + if factOp == t.KeyXBinaryEqEq && rhsCV != nil { + if factCV := f.RHS().Expr().ConstValue(); factCV != nil { + switch op { + case t.KeyXBinaryNotEq: + return factCV.Cmp(rhsCV) != 0 + case t.KeyXBinaryLessThan: + return factCV.Cmp(rhsCV) < 0 + case t.KeyXBinaryLessEq: + return factCV.Cmp(rhsCV) <= 0 + case t.KeyXBinaryEqEq: + return factCV.Cmp(rhsCV) == 0 + case t.KeyXBinaryGreaterEq: + return factCV.Cmp(rhsCV) >= 0 + case t.KeyXBinaryGreaterThan: + return factCV.Cmp(rhsCV) > 0 + } + } + } } return false } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 1ad93b77..5e96d9c5 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -144,7 +144,7 @@ func (q *checker) bcheckStatement(n *a.Node) error { case a.KAssign: n := n.Assign() - return q.bcheckAssignment(n.LHS(), n.LHS().MType(), n.Operator(), n.RHS()) + return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS()) case a.KIf: // TODO. @@ -157,7 +157,17 @@ func (q *checker) bcheckStatement(n *a.Node) error { case a.KVar: n := n.Var() - return q.bcheckAssignment(nil, n.XType(), t.IDEq, n.Value()) + lhs := a.NewExpr(a.FlagsTypeChecked, 0, n.Name(), nil, nil, nil, nil) + lhs.SetMType(n.XType()) + rhs := n.Value() + // "var x T" has an implicit "= _". + if rhs == nil { + rhs = a.NewExpr(a.FlagsTypeChecked, 0, t.IDUnderscore, nil, nil, nil, nil) + // TODO: check that T is an integer type. + rhs.SetConstValue(zero) + rhs.SetMType(TypeExprIdealNumber) + } + return q.bcheckAssignment(lhs, t.IDEq, rhs) case a.KWhile: return q.bcheckWhile(n.While()) @@ -197,16 +207,26 @@ func (q *checker) bcheckAssert(n *a.Assert) error { return nil } -func (q *checker) bcheckAssignment(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error { - if err := q.bcheckAssignment1(lhs, lTyp, op, rhs); err != nil { +func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { + if err := q.bcheckAssignment1(lhs, op, rhs); err != nil { return err } - // TODO: drop any facts involving lhs. + // TODO: check lhs and rhs are pure expressions. + if op == t.IDEq { + // Drop any facts involving lhs. + q.facts.drop(lhs) + + o := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryEqEq, 0, lhs.Node(), nil, rhs.Node(), nil) + o.SetMType(lhs.MType()) + q.facts.appendFact(o) + } else { + // TODO: update any facts involving lhs. + } return nil } -func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error { - switch lTyp.PackageOrDecorator().Key() { +func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error { + switch lhs.MType().PackageOrDecorator().Key() { case t.KeyPtr: // TODO: handle. return nil @@ -214,23 +234,18 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs // TODO: handle. return nil } - if lTyp.Name().Key() == t.KeyBool { + if lhs.MType().Name().Key() == t.KeyBool { return nil } - lMin, lMax, err := q.bcheckTypeExpr(lTyp) + lMin, lMax, err := q.bcheckTypeExpr(lhs.MType()) if err != nil { return err } rMin, rMax := (*big.Int)(nil), (*big.Int)(nil) if op == t.IDEq { - cv := zero - // rhs might be nil because "var x T" has an implicit "= 0". - if rhs != nil { - cv = rhs.ConstValue() - } - if cv != nil { + if cv := rhs.ConstValue(); cv != nil { if cv.Cmp(lMin) < 0 || cv.Cmp(lMax) > 0 { return fmt.Errorf("check: constant %v is not within bounds [%v..%v]", cv, lMin, lMax) } @@ -257,18 +272,21 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs } func (q *checker) bcheckWhile(n *a.While) error { - // TODO: check that n.Condition() has no side effects. - - if _, _, err := q.bcheckExpr(n.Condition(), 0); err != nil { - return err - } - // Check the pre and inv conditions on entry. for _, o := range n.Asserts() { if o.Assert().Keyword().Key() == t.KeyPost { continue } - // TODO + if err := q.bcheckAssert(o.Assert()); err != nil { + return err + } + } + + // Check the while condition. + // + // TODO: check that n.Condition() has no side effects. + if _, _, err := q.bcheckExpr(n.Condition(), 0); err != nil { + return err } // Check the post conditions on exit, assuming only the pre and inv @@ -329,7 +347,9 @@ func (q *checker) bcheckWhile(n *a.While) error { if o.Assert().Keyword().Key() == t.KeyPost { continue } - // TODO + if err := q.bcheckAssert(o.Assert()); err != nil { + return err + } } // TODO: check the pre and inv conditions for each continue. diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 3c470ea7..793b1e33 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -17,7 +17,9 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() // TODO: implement. - while true { + while true, + pre n_bits < 8, + { while n_bits < width, post n_bits >= width, { @@ -28,5 +30,9 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)() // TODO: implement. break + + // TODO: process n_bits further so that (n_bits < 8) and remove this + // temporary hack. + n_bits = 0 } }