From 389d9bc8bfa0d1bff04522e09c62147895ff47a9 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 22 Jul 2017 10:14:50 +1000 Subject: [PATCH] Remove the placeholder code limit count. --- gen/c/std/flate.c | 2 +- lang/check/assert.go | 86 +++++++++++++++++++++++++++++++----- lang/check/bounds.go | 45 ++++++++++++------- lang/token/token.go | 8 ++-- std/flate/decode_flate.puffs | 34 +++++++++++--- 5 files changed, 134 insertions(+), 41 deletions(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 0b02f425..99b2445b 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -804,7 +804,7 @@ puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self, goto suspend; } v_i = 0; - while (v_i < 31) { + while (v_i < (v_n_lit + v_n_dist)) { v_symbol = 0; v_code = 0; v_count = 0; diff --git a/lang/check/assert.go b/lang/check/assert.go index 62cf2236..61e1cc00 100644 --- a/lang/check/assert.go +++ b/lang/check/assert.go @@ -78,10 +78,13 @@ func (z *facts) appendFact(fact *a.Expr) { // update applies f to each fact, replacing the slice element with the result // of the function call. The slice is then compacted to remove all nils. -func (z *facts) update(f func(*a.Expr) *a.Expr) { +func (z *facts) update(f func(*a.Expr) (*a.Expr, error)) error { i := 0 for _, x := range *z { - x = f(x) + x, err := f(x) + if err != nil { + return err + } if x != nil { (*z)[i] = x i++ @@ -91,6 +94,7 @@ func (z *facts) update(f func(*a.Expr) *a.Expr) { (*z)[j] = nil } *z = (*z)[:i] + return nil } func (z facts) refine(n *a.Expr, nMin *big.Int, nMax *big.Int, tm *t.Map) (*big.Int, *big.Int, error) { @@ -153,26 +157,55 @@ 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(n *a.Expr) *a.Expr { +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: - // TODO: constant folding, so ((x + 1) + 1) becomes (x + 2). + if lcv, rcv := lhs.ConstValue(), rhs.ConstValue(); lcv != nil && rcv != nil { + ocv := big.NewInt(0).Add(lcv, rcv) + id, err := tm.Insert(ocv.String()) + if err != nil { + return nil, err + } + o := a.NewExpr(a.FlagsTypeChecked, 0, id, nil, nil, nil, nil) + o.SetConstValue(ocv) + o.SetMType(typeExprIdeal) + return o, nil + } + // TODO: more constant folding, so ((x + 1) + 1) becomes (x + 2). case t.KeyXBinaryMinus: if lhs.Eq(rhs) { - return zeroExpr + return zeroExpr, nil } if lOp, lLHS, lRHS := parseBinaryOp(lhs); lOp.Key() == t.KeyXBinaryPlus { if lLHS.Eq(rhs) { - return lRHS + return lRHS, nil } if lRHS.Eq(rhs) { - return lLHS + return lLHS, nil } } + + case t.KeyXBinaryNotEq, t.KeyXBinaryLessThan, t.KeyXBinaryLessEq, + t.KeyXBinaryEqEq, t.KeyXBinaryGreaterEq, t.KeyXBinaryGreaterThan: + + l, err := simplify(tm, lhs) + if err != nil { + return nil, err + } + r, err := simplify(tm, rhs) + if err != nil { + return nil, err + } + if l != lhs || r != rhs { + o := a.NewExpr(a.FlagsTypeChecked, op, 0, l.Node(), nil, r.Node(), nil) + o.SetConstValue(n.ConstValue()) + o.SetMType(n.MType()) + return o, nil + } } - return n + return n, nil } func argValue(tm *t.Map, args []*a.Node, name string) *a.Expr { @@ -292,11 +325,11 @@ var reasons = [...]struct { r reason }{ {`"a < (b + c): a < c; 0 <= b"`, func(q *checker, n *a.Assert) error { - op, xa, bc := parseBinaryOp(n.Condition()) + op, xa, xbc := parseBinaryOp(n.Condition()) if op.Key() != t.KeyXBinaryLessThan { return errFailed } - op, xb, xc := parseBinaryOp(bc) + op, xb, xc := parseBinaryOp(xbc) if op.Key() != t.KeyXBinaryPlus { return errFailed } @@ -309,11 +342,11 @@ var reasons = [...]struct { return nil }}, {`"(a + b) <= c: a <= (c - b)"`, func(q *checker, n *a.Assert) error { - op, ab, xc := parseBinaryOp(n.Condition()) + op, xab, xc := parseBinaryOp(n.Condition()) if op.Key() != t.KeyXBinaryLessEq { return errFailed } - op, xa, xb := parseBinaryOp(ab) + op, xa, xb := parseBinaryOp(xab) if op.Key() != t.KeyXBinaryPlus { return errFailed } @@ -340,4 +373,33 @@ var reasons = [...]struct { } return nil }}, + {`"a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"`, func(q *checker, n *a.Assert) error { + xb0 := argValue(q.tm, n.Args(), "b0") + if xb0 == nil { + return errFailed + } + xc0 := argValue(q.tm, n.Args(), "c0") + if xc0 == nil { + return errFailed + } + op, xa, xbc := parseBinaryOp(n.Condition()) + if op.Key() != t.KeyXBinaryLessThan { + return errFailed + } + op, xb, xc := parseBinaryOp(xbc) + if op.Key() != t.KeyXBinaryPlus { + return errFailed + } + plus := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryPlus, 0, xb0.Node(), nil, xc0.Node(), nil) + if err := q.proveBinaryOp(t.KeyXBinaryLessThan, xa, plus); err != nil { + return binOpReasonError(q.tm, t.IDXBinaryLessThan, xa, plus, err) + } + if err := q.proveBinaryOp(t.KeyXBinaryLessEq, xb0, xb); err != nil { + return binOpReasonError(q.tm, t.IDXBinaryLessEq, xb0, xb, err) + } + if err := q.proveBinaryOp(t.KeyXBinaryLessEq, xc0, xc); err != nil { + return binOpReasonError(q.tm, t.IDXBinaryLessEq, xc0, xc, err) + } + return nil + }}, } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 541ba7e1..dc94141f 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -269,7 +269,11 @@ func (q *checker) bcheckAssert(n *a.Assert) error { } return fmt.Errorf("check: cannot prove %q: %v", condition.String(q.tm), err) } - q.facts.appendFact(condition) + o, err := simplify(q.tm, condition) + if err != nil { + return err + } + q.facts.appendFact(o) return nil } @@ -280,12 +284,14 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { // TODO: check lhs and rhs are pure expressions. if op == t.IDEq { // Drop any facts involving lhs. - q.facts.update(func(x *a.Expr) *a.Expr { + if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) { if x.Mentions(lhs) { - return nil + return nil, nil } - return x - }) + return x, nil + }); err != nil { + return err + } if lhs.Pure() && rhs.Pure() { o := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryEqEq, 0, lhs.Node(), nil, rhs.Node(), nil) @@ -294,27 +300,33 @@ func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error { } } else { // Update any facts involving lhs. - q.facts.update(func(x *a.Expr) *a.Expr { + if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) { xOp, xLHS, xRHS := parseBinaryOp(x) if xOp == 0 || !xLHS.Eq(lhs) { if x.Mentions(lhs) { - return nil + return nil, nil } - return x + return x, nil } if xRHS.Mentions(lhs) { - return nil + return nil, nil } switch op.Key() { case t.KeyPlusEq, t.KeyMinusEq: oRHS := a.NewExpr(a.FlagsTypeChecked, op.BinaryForm(), 0, xRHS.Node(), nil, rhs.Node(), nil) oRHS.SetMType(xRHS.MType()) - o := a.NewExpr(a.FlagsTypeChecked, xOp, 0, xLHS.Node(), nil, simplify(oRHS).Node(), nil) + oRHS, err := simplify(q.tm, oRHS) + if err != nil { + return nil, err + } + o := a.NewExpr(a.FlagsTypeChecked, xOp, 0, xLHS.Node(), nil, oRHS.Node(), nil) o.SetMType(x.MType()) - return o + return o, nil } - return nil - }) + return nil, nil + }); err != nil { + return err + } } return nil } @@ -426,13 +438,12 @@ func (q *checker) unify(branches [][]*a.Expr) error { } } - q.facts.update(func(n *a.Expr) *a.Expr { + return q.facts.update(func(n *a.Expr) (*a.Expr, error) { if m[n.String(q.tm)] == len(branches) { - return n + return n, nil } - return nil + return nil, nil }) - return nil } func (q *checker) bcheckIf(n *a.If) error { diff --git a/lang/token/token.go b/lang/token/token.go index 84245745..b06c66dc 100644 --- a/lang/token/token.go +++ b/lang/token/token.go @@ -25,7 +25,7 @@ type Map struct { byKey []string } -func (m *Map) insert(name string) (ID, error) { +func (m *Map) Insert(name string) (ID, error) { if name == "" { return 0, nil } @@ -158,7 +158,7 @@ loop: return nil, nil, fmt.Errorf("token: string too long at %s:%d", filename, line) } } - id, err := m.insert(string(src[i:j])) + id, err := m.Insert(string(src[i:j])) if err != nil { return nil, nil, err } @@ -174,7 +174,7 @@ loop: return nil, nil, fmt.Errorf("token: identifier too long at %s:%d", filename, line) } } - id, err := m.insert(string(src[i:j])) + id, err := m.Insert(string(src[i:j])) if err != nil { return nil, nil, err } @@ -200,7 +200,7 @@ loop: return nil, nil, fmt.Errorf("token: constant too long at %s:%d", filename, line) } } - id, err := m.insert(string(src[i:j])) + id, err := m.Insert(string(src[i:j])) if err != nil { return nil, nil, err } diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 84804b50..4cb1d81c 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -104,11 +104,16 @@ pri func decoder.init_huffs?(src reader1)() { bits >>= 4 n_bits -= 14 - // Read the clcode Huffman table. + // Read the clcode Huffman table: H-CL. var i u32 = 0 - while i < n_clen { + while i < n_clen, + inv n_dist <= 30, + inv n_lit <= 286, + { while n_bits < 3, inv i < n_clen, + inv n_dist <= 30, + inv n_lit <= 286, post n_bits >= 3, { bits |= (in.src.read_u8?() as u32) << n_bits @@ -120,14 +125,23 @@ pri func decoder.init_huffs?(src reader1)() { n_bits -= 3 i += 1 } - while i < 19 { + while i < 19, + inv n_dist <= 30, + inv n_lit <= 286, + { this.code_lengths[code_order[i]] = 0 i += 1 } this.init_huff?(which:0, n_codes:19) + // Decode the code lengths for the next two Huffman tables. i = 0 - while i < 31 { // TODO: replace 31 with n_lit + n_dist. + while i < (n_lit + n_dist), + inv n_dist <= 30, + inv n_lit <= 286, + { + assert i < (286 + 30) via "a < (b + c): a < (b0 + c0); b0 <= b; c0 <= c"(b0:n_lit, c0:n_dist) + // Decode a clcode symbol from H-CL. var symbol u32[..289] var code u32 @@ -138,7 +152,9 @@ pri func decoder.init_huffs?(src reader1)() { while true, pre first <= 0xFFFF, pre index <= 0xFFFF, - inv i < 31, + inv i < 316, + inv n_dist <= 30, + inv n_lit <= 286, { if length > 15 { return error "bad Huffman code" @@ -147,9 +163,11 @@ pri func decoder.init_huffs?(src reader1)() { // Read the next bit. while n_bits <= 0, inv first <= 0xFFFF, + inv i < 316, inv index <= 0xFFFF, - inv i < 31, inv length <= 15, + inv n_dist <= 30, + inv n_lit <= 286, post n_bits > 0, { bits |= (in.src.read_u8?() as u32) << n_bits @@ -234,7 +252,9 @@ pri func decoder.init_huffs?(src reader1)() { n_extra_bits = 7 } while n_bits < n_extra_bits, - inv i < 31, + inv i < 316, + inv n_dist <= 30, + inv n_lit <= 286, post n_bits >= n_extra_bits, { assert n_bits < 7 via "a < b: a < c; c <= b"(c:n_extra_bits)