mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Check a while loop's pre and inv conditions.
This commit is contained in:
1 parent
bcf6a7ee1d
commit
3f5bb7a02b
5 files changed
+115
-29
No files matched your search
@@ -168,6 +168,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self) {
|
|||||||
v_n_bits += 8;
|
v_n_bits += 8;
|
||||||
}
|
}
|
||||||
goto label_0_break;
|
goto label_0_break;
|
||||||
|
v_n_bits = 0;
|
||||||
}
|
}
|
||||||
label_0_break:;
|
label_0_break:;
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,24 @@ func (n *Expr) Eq(o *Expr) bool {
|
|||||||
return true
|
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.
|
// Eq returns whether n and o are equal.
|
||||||
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
||||||
return n.eq(o, false)
|
return n.eq(o, false)
|
||||||
|
|||||||
+46
-5
@@ -24,6 +24,22 @@ func (z *facts) appendFact(x *a.Expr) {
|
|||||||
*z = append(*z, x)
|
*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) {
|
func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int) (*big.Int, *big.Int) {
|
||||||
if n.ID0() != 0 || !n.ID1().IsIdent() {
|
if n.ID0() != 0 || !n.ID1().IsIdent() {
|
||||||
// TODO.
|
// 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) {
|
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)
|
rMin, rMax, err := q.bcheckExpr(rhs, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// TODO: should this function return the error?
|
// TODO: should this function return the error?
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if proveBinaryOpConstValues(op, cv, cv, rMin, rMax) {
|
if proveBinaryOpConstValues(op, lhsCV, lhsCV, rMin, rMax) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cv := rhs.ConstValue(); cv != nil {
|
rhsCV := rhs.ConstValue()
|
||||||
|
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?
|
// TODO: should this function return the error?
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if proveBinaryOpConstValues(op, lMin, lMax, cv, cv) {
|
if proveBinaryOpConstValues(op, lMin, lMax, rhsCV, rhsCV) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range q.facts {
|
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
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-23
@@ -144,7 +144,7 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
|
|
||||||
case a.KAssign:
|
case a.KAssign:
|
||||||
n := n.Assign()
|
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:
|
case a.KIf:
|
||||||
// TODO.
|
// TODO.
|
||||||
@@ -157,7 +157,17 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
|
|
||||||
case a.KVar:
|
case a.KVar:
|
||||||
n := n.Var()
|
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:
|
case a.KWhile:
|
||||||
return q.bcheckWhile(n.While())
|
return q.bcheckWhile(n.While())
|
||||||
@@ -197,16 +207,26 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *checker) bcheckAssignment(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error {
|
func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||||
if err := q.bcheckAssignment1(lhs, lTyp, op, rhs); err != nil {
|
if err := q.bcheckAssignment1(lhs, op, rhs); err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs *a.Expr) error {
|
func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||||
switch lTyp.PackageOrDecorator().Key() {
|
switch lhs.MType().PackageOrDecorator().Key() {
|
||||||
case t.KeyPtr:
|
case t.KeyPtr:
|
||||||
// TODO: handle.
|
// TODO: handle.
|
||||||
return nil
|
return nil
|
||||||
@@ -214,23 +234,18 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
|
|||||||
// TODO: handle.
|
// TODO: handle.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if lTyp.Name().Key() == t.KeyBool {
|
if lhs.MType().Name().Key() == t.KeyBool {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
lMin, lMax, err := q.bcheckTypeExpr(lTyp)
|
lMin, lMax, err := q.bcheckTypeExpr(lhs.MType())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rMin, rMax := (*big.Int)(nil), (*big.Int)(nil)
|
rMin, rMax := (*big.Int)(nil), (*big.Int)(nil)
|
||||||
if op == t.IDEq {
|
if op == t.IDEq {
|
||||||
cv := zero
|
if cv := rhs.ConstValue(); cv != nil {
|
||||||
// rhs might be nil because "var x T" has an implicit "= 0".
|
|
||||||
if rhs != nil {
|
|
||||||
cv = rhs.ConstValue()
|
|
||||||
}
|
|
||||||
if cv != nil {
|
|
||||||
if cv.Cmp(lMin) < 0 || cv.Cmp(lMax) > 0 {
|
if cv.Cmp(lMin) < 0 || cv.Cmp(lMax) > 0 {
|
||||||
return fmt.Errorf("check: constant %v is not within bounds [%v..%v]", cv, lMin, lMax)
|
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 {
|
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.
|
// Check the pre and inv conditions on entry.
|
||||||
for _, o := range n.Asserts() {
|
for _, o := range n.Asserts() {
|
||||||
if o.Assert().Keyword().Key() == t.KeyPost {
|
if o.Assert().Keyword().Key() == t.KeyPost {
|
||||||
continue
|
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
|
// 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 {
|
if o.Assert().Keyword().Key() == t.KeyPost {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// TODO
|
if err := q.bcheckAssert(o.Assert()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check the pre and inv conditions for each continue.
|
// TODO: check the pre and inv conditions for each continue.
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ pub func lzw_decoder.decode?(dst ptr io.buf1, src ptr io.buf1, src_final bool)()
|
|||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
|
|
||||||
while true {
|
while true,
|
||||||
|
pre n_bits < 8,
|
||||||
|
{
|
||||||
while n_bits < width,
|
while n_bits < width,
|
||||||
post 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.
|
// TODO: implement.
|
||||||
break
|
break
|
||||||
|
|
||||||
|
// TODO: process n_bits further so that (n_bits < 8) and remove this
|
||||||
|
// temporary hack.
|
||||||
|
n_bits = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user