Factor out a evalConstValueBinaryOp function.

This commit is contained in:
Nigel Tao committed 2017-07-22 13:55:39 +10:00
1 parent dec234779e
commit 9933c971a2
2 files changed
+54 -45

No files matched your search

+25 -18
View File
@@ -159,19 +159,26 @@ func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int, tm *t.Map) (*big.
// simplify returns a simplified form of n. For example, (x - x) becomes 0.
func simplify(tm *t.Map, n *a.Expr) (*a.Expr, error) {
// TODO: be rigorous about this, not ad hoc.
switch op, lhs, rhs := parseBinaryOp(n); op.Key() {
case t.KeyXBinaryPlus:
op, lhs, rhs := parseBinaryOp(n)
if lhs != nil && rhs != nil {
if lcv, rcv := lhs.ConstValue(), rhs.ConstValue(); lcv != nil && rcv != nil {
ocv := big.NewInt(0).Add(lcv, rcv)
id, err := tm.Insert(ocv.String())
ncv, err := evalConstValueBinaryOp(tm, n, lcv, rcv)
if err != nil {
return nil, err
}
id, err := tm.Insert(ncv.String())
if err != nil {
return nil, err
}
o := a.NewExpr(a.FlagsTypeChecked, 0, id, nil, nil, nil, nil)
o.SetConstValue(ocv)
o.SetConstValue(ncv)
o.SetMType(typeExprIdeal)
return o, nil
}
}
switch op.Key() {
case t.KeyXBinaryPlus:
// TODO: more constant folding, so ((x + 1) + 1) becomes (x + 2).
case t.KeyXBinaryMinus:
@@ -250,23 +257,23 @@ 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) error {
lhsCV := lhs.ConstValue()
if lhsCV != nil {
lcv := lhs.ConstValue()
if lcv != nil {
rMin, rMax, err := q.bcheckExpr(rhs, 0)
if err != nil {
return err
}
if proveBinaryOpConstValues(op, lhsCV, lhsCV, rMin, rMax) {
if proveBinaryOpConstValues(op, lcv, lcv, rMin, rMax) {
return nil
}
}
rhsCV := rhs.ConstValue()
if rhsCV != nil {
rcv := rhs.ConstValue()
if rcv != nil {
lMin, lMax, err := q.bcheckExpr(lhs, 0)
if err != nil {
return err
}
if proveBinaryOpConstValues(op, lMin, lMax, rhsCV, rhsCV) {
if proveBinaryOpConstValues(op, lMin, lMax, rcv, rcv) {
return nil
}
}
@@ -280,21 +287,21 @@ func (q *checker) proveBinaryOp(op t.Key, lhs *a.Expr, rhs *a.Expr) error {
return nil
}
if factOp == t.KeyXBinaryEqEq && rhsCV != nil {
if factOp == t.KeyXBinaryEqEq && rcv != nil {
if factCV := x.RHS().Expr().ConstValue(); factCV != nil {
switch op {
case t.KeyXBinaryNotEq:
return errFailedOrNil(factCV.Cmp(rhsCV) != 0)
return errFailedOrNil(factCV.Cmp(rcv) != 0)
case t.KeyXBinaryLessThan:
return errFailedOrNil(factCV.Cmp(rhsCV) < 0)
return errFailedOrNil(factCV.Cmp(rcv) < 0)
case t.KeyXBinaryLessEq:
return errFailedOrNil(factCV.Cmp(rhsCV) <= 0)
return errFailedOrNil(factCV.Cmp(rcv) <= 0)
case t.KeyXBinaryEqEq:
return errFailedOrNil(factCV.Cmp(rhsCV) == 0)
return errFailedOrNil(factCV.Cmp(rcv) == 0)
case t.KeyXBinaryGreaterEq:
return errFailedOrNil(factCV.Cmp(rhsCV) >= 0)
return errFailedOrNil(factCV.Cmp(rcv) >= 0)
case t.KeyXBinaryGreaterThan:
return errFailedOrNil(factCV.Cmp(rhsCV) > 0)
return errFailedOrNil(factCV.Cmp(rcv) > 0)
}
}
}
+29 -27
View File
@@ -719,10 +719,12 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
}
}
if l, r := lhs.ConstValue(), rhs.ConstValue(); l != nil && r != nil {
if err := q.setConstValueBinaryOp(n, l, r); err != nil {
if lcv, rcv := lhs.ConstValue(), rhs.ConstValue(); lcv != nil && rcv != nil {
ncv, err := evalConstValueBinaryOp(q.tm, n, lcv, rcv)
if err != nil {
return err
}
n.SetConstValue(ncv)
}
if comparisonOps[0xFF&op.Key()] {
@@ -736,59 +738,59 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return nil
}
func (q *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error {
func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.Int, error) {
switch n.ID0().Key() {
case t.KeyXBinaryPlus:
n.SetConstValue(big.NewInt(0).Add(l, r))
return big.NewInt(0).Add(l, r), nil
case t.KeyXBinaryMinus:
n.SetConstValue(big.NewInt(0).Sub(l, r))
return big.NewInt(0).Sub(l, r), nil
case t.KeyXBinaryStar:
n.SetConstValue(big.NewInt(0).Mul(l, r))
return big.NewInt(0).Mul(l, r), nil
case t.KeyXBinarySlash:
if r.Cmp(zero) == 0 {
return fmt.Errorf("check: division by zero in const expression %q", n.String(q.tm))
return nil, fmt.Errorf("check: division by zero in const expression %q", n.String(tm))
}
// TODO: decide on Euclidean division vs other definitions. See "go doc
// math/big int.divmod" for details.
n.SetConstValue(big.NewInt(0).Div(l, r))
return big.NewInt(0).Div(l, r), nil
case t.KeyXBinaryShiftL:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(q.tm), n.String(q.tm))
return nil, fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(tm), n.String(tm))
}
n.SetConstValue(big.NewInt(0).Lsh(l, uint(r.Uint64())))
return big.NewInt(0).Lsh(l, uint(r.Uint64())), nil
case t.KeyXBinaryShiftR:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(q.tm), n.String(q.tm))
return nil, fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(tm), n.String(tm))
}
n.SetConstValue(big.NewInt(0).Rsh(l, uint(r.Uint64())))
return big.NewInt(0).Rsh(l, uint(r.Uint64())), nil
case t.KeyXBinaryAmp:
n.SetConstValue(big.NewInt(0).And(l, r))
return big.NewInt(0).And(l, r), nil
case t.KeyXBinaryAmpHat:
n.SetConstValue(big.NewInt(0).AndNot(l, r))
return big.NewInt(0).AndNot(l, r), nil
case t.KeyXBinaryPipe:
n.SetConstValue(big.NewInt(0).Or(l, r))
return big.NewInt(0).Or(l, r), nil
case t.KeyXBinaryHat:
n.SetConstValue(big.NewInt(0).Xor(l, r))
return big.NewInt(0).Xor(l, r), nil
case t.KeyXBinaryNotEq:
n.SetConstValue(btoi(l.Cmp(r) != 0))
return btoi(l.Cmp(r) != 0), nil
case t.KeyXBinaryLessThan:
n.SetConstValue(btoi(l.Cmp(r) < 0))
return btoi(l.Cmp(r) < 0), nil
case t.KeyXBinaryLessEq:
n.SetConstValue(btoi(l.Cmp(r) <= 0))
return btoi(l.Cmp(r) <= 0), nil
case t.KeyXBinaryEqEq:
n.SetConstValue(btoi(l.Cmp(r) == 0))
return btoi(l.Cmp(r) == 0), nil
case t.KeyXBinaryGreaterEq:
n.SetConstValue(btoi(l.Cmp(r) >= 0))
return btoi(l.Cmp(r) >= 0), nil
case t.KeyXBinaryGreaterThan:
n.SetConstValue(btoi(l.Cmp(r) > 0))
return btoi(l.Cmp(r) > 0), nil
case t.KeyXBinaryAnd:
n.SetConstValue(btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)))
return btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)), nil
case t.KeyXBinaryOr:
n.SetConstValue(btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)))
return btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)), nil
}
return nil
return nil, fmt.Errorf("check: unrecognized token.Key (0x%02X) for evalConstValueBinaryOp", n.ID0().Key())
}
func (q *checker) tcheckExprAssociativeOp(n *a.Expr, depth uint32) error {