Add skip32_fast method

This commit is contained in:
Nigel Tao committed 2018-07-01 11:14:25 +10:00
1 parent bb4477d91f
commit 2599476d2c
6 files changed
+75 -24

No files matched your search

+31 -9
View File
@@ -969,16 +969,38 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (*big.Int,
}
} else if recvTyp.IsIOType() {
if method >= t.IDPeekU8 {
advance, update := (*big.Int)(nil), false
if method == t.IDSkip32Fast {
args := n.Args()
if len(args) != 2 {
return nil, nil, fmt.Errorf("check: internal error: bad skip32_fast arguments")
}
actual := args[0].Arg().Value()
worstCase := args[1].Arg().Value()
if err := q.proveBinaryOp(t.IDXBinaryLessEq, actual, worstCase); err == errFailed {
return nil, nil, fmt.Errorf("check: could not prove skip32_fast precondition: %s <= %s",
actual.Str(q.tm), worstCase.Str(q.tm))
} else if err != nil {
return nil, nil, err
}
advance, update = worstCase.ConstValue(), true
if advance == nil {
return nil, nil, fmt.Errorf("check: skip32_fast worst_case is not a constant value")
}
} else if method >= t.IDPeekU8 {
if m := method - t.IDPeekU8; m < t.ID(len(ioMethodAdvances)) {
if advance := ioMethodAdvances[m]; advance != nil {
if ok, err := q.optimizeIOMethodAdvance(recv, advance, false); err != nil {
return nil, nil, err
} else if !ok {
return nil, nil, fmt.Errorf("check: could not prove %s precondition: %s.available() >= %v",
method.Str(q.tm), recv.Str(q.tm), advance)
}
}
advance, update = ioMethodAdvances[m], false
}
}
if advance != nil {
if ok, err := q.optimizeIOMethodAdvance(recv, advance, update); err != nil {
return nil, nil, err
} else if !ok {
return nil, nil, fmt.Errorf("check: could not prove %s precondition: %s.available() >= %v",
method.Str(q.tm), recv.Str(q.tm), advance)
}
}
}
+9
View File
@@ -224,6 +224,10 @@ func (q *checker) optimizeSuspendible(n *a.Expr, depth uint32) error {
}
func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, update bool) (retOK bool, retErr error) {
// TODO: do two passes? The first one to non-destructively check retOK. The
// second one to drop facts if indeed retOK? Otherwise, an advance
// precondition failure will lose some of the facts in its error message.
retErr = q.facts.update(func(x *a.Expr) (*a.Expr, error) {
// TODO: update (discard?) any facts that merely mention
// receiver.available(), even if they aren't an exact match.
@@ -271,6 +275,11 @@ func (q *checker) optimizeIOMethodAdvance(receiver *a.Expr, advance *big.Int, up
return x, nil
}
if rcv.Cmp(advance) == 0 {
// TODO: delete the (adjusted) fact, as newRCV will be zero, and
// "foo.advance() >= 0" is redundant.
}
// Create a new a.Expr to hold the adjusted RHS constant value, newRCV.
newRCV := big.NewInt(0).Sub(rcv, advance)
id, err := q.tm.Insert(newRCV.String())