mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Remove the placeholder code limit count.
This commit is contained in:
1 parent
fd07fa1f27
commit
389d9bc8bf
5 files changed
+134
-41
No files matched your search
+1
-1
@@ -804,7 +804,7 @@ puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self,
|
|||||||
goto suspend;
|
goto suspend;
|
||||||
}
|
}
|
||||||
v_i = 0;
|
v_i = 0;
|
||||||
while (v_i < 31) {
|
while (v_i < (v_n_lit + v_n_dist)) {
|
||||||
v_symbol = 0;
|
v_symbol = 0;
|
||||||
v_code = 0;
|
v_code = 0;
|
||||||
v_count = 0;
|
v_count = 0;
|
||||||
|
|||||||
+74
-12
@@ -78,10 +78,13 @@ func (z *facts) appendFact(fact *a.Expr) {
|
|||||||
|
|
||||||
// update applies f to each fact, replacing the slice element with the result
|
// 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.
|
// 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
|
i := 0
|
||||||
for _, x := range *z {
|
for _, x := range *z {
|
||||||
x = f(x)
|
x, err := f(x)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if x != nil {
|
if x != nil {
|
||||||
(*z)[i] = x
|
(*z)[i] = x
|
||||||
i++
|
i++
|
||||||
@@ -91,6 +94,7 @@ func (z *facts) update(f func(*a.Expr) *a.Expr) {
|
|||||||
(*z)[j] = nil
|
(*z)[j] = nil
|
||||||
}
|
}
|
||||||
*z = (*z)[:i]
|
*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) {
|
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.
|
// 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.
|
// TODO: be rigorous about this, not ad hoc.
|
||||||
switch op, lhs, rhs := parseBinaryOp(n); op.Key() {
|
switch op, lhs, rhs := parseBinaryOp(n); op.Key() {
|
||||||
case t.KeyXBinaryPlus:
|
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:
|
case t.KeyXBinaryMinus:
|
||||||
if lhs.Eq(rhs) {
|
if lhs.Eq(rhs) {
|
||||||
return zeroExpr
|
return zeroExpr, nil
|
||||||
}
|
}
|
||||||
if lOp, lLHS, lRHS := parseBinaryOp(lhs); lOp.Key() == t.KeyXBinaryPlus {
|
if lOp, lLHS, lRHS := parseBinaryOp(lhs); lOp.Key() == t.KeyXBinaryPlus {
|
||||||
if lLHS.Eq(rhs) {
|
if lLHS.Eq(rhs) {
|
||||||
return lRHS
|
return lRHS, nil
|
||||||
}
|
}
|
||||||
if lRHS.Eq(rhs) {
|
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 {
|
func argValue(tm *t.Map, args []*a.Node, name string) *a.Expr {
|
||||||
@@ -292,11 +325,11 @@ var reasons = [...]struct {
|
|||||||
r reason
|
r reason
|
||||||
}{
|
}{
|
||||||
{`"a < (b + c): a < c; 0 <= b"`, func(q *checker, n *a.Assert) error {
|
{`"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 {
|
if op.Key() != t.KeyXBinaryLessThan {
|
||||||
return errFailed
|
return errFailed
|
||||||
}
|
}
|
||||||
op, xb, xc := parseBinaryOp(bc)
|
op, xb, xc := parseBinaryOp(xbc)
|
||||||
if op.Key() != t.KeyXBinaryPlus {
|
if op.Key() != t.KeyXBinaryPlus {
|
||||||
return errFailed
|
return errFailed
|
||||||
}
|
}
|
||||||
@@ -309,11 +342,11 @@ var reasons = [...]struct {
|
|||||||
return nil
|
return nil
|
||||||
}},
|
}},
|
||||||
{`"(a + b) <= c: a <= (c - b)"`, func(q *checker, n *a.Assert) error {
|
{`"(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 {
|
if op.Key() != t.KeyXBinaryLessEq {
|
||||||
return errFailed
|
return errFailed
|
||||||
}
|
}
|
||||||
op, xa, xb := parseBinaryOp(ab)
|
op, xa, xb := parseBinaryOp(xab)
|
||||||
if op.Key() != t.KeyXBinaryPlus {
|
if op.Key() != t.KeyXBinaryPlus {
|
||||||
return errFailed
|
return errFailed
|
||||||
}
|
}
|
||||||
@@ -340,4 +373,33 @@ var reasons = [...]struct {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}},
|
||||||
}
|
}
|
||||||
+28
-17
@@ -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)
|
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
|
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.
|
// TODO: check lhs and rhs are pure expressions.
|
||||||
if op == t.IDEq {
|
if op == t.IDEq {
|
||||||
// Drop any facts involving lhs.
|
// 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) {
|
if x.Mentions(lhs) {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return x
|
return x, nil
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if lhs.Pure() && rhs.Pure() {
|
if lhs.Pure() && rhs.Pure() {
|
||||||
o := a.NewExpr(a.FlagsTypeChecked, t.IDXBinaryEqEq, 0, lhs.Node(), nil, rhs.Node(), nil)
|
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 {
|
} else {
|
||||||
// Update any facts involving lhs.
|
// 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)
|
xOp, xLHS, xRHS := parseBinaryOp(x)
|
||||||
if xOp == 0 || !xLHS.Eq(lhs) {
|
if xOp == 0 || !xLHS.Eq(lhs) {
|
||||||
if x.Mentions(lhs) {
|
if x.Mentions(lhs) {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return x
|
return x, nil
|
||||||
}
|
}
|
||||||
if xRHS.Mentions(lhs) {
|
if xRHS.Mentions(lhs) {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
switch op.Key() {
|
switch op.Key() {
|
||||||
case t.KeyPlusEq, t.KeyMinusEq:
|
case t.KeyPlusEq, t.KeyMinusEq:
|
||||||
oRHS := a.NewExpr(a.FlagsTypeChecked, op.BinaryForm(), 0, xRHS.Node(), nil, rhs.Node(), nil)
|
oRHS := a.NewExpr(a.FlagsTypeChecked, op.BinaryForm(), 0, xRHS.Node(), nil, rhs.Node(), nil)
|
||||||
oRHS.SetMType(xRHS.MType())
|
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())
|
o.SetMType(x.MType())
|
||||||
return o
|
return o, nil
|
||||||
}
|
}
|
||||||
return nil
|
return nil, nil
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
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) {
|
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 {
|
func (q *checker) bcheckIf(n *a.If) error {
|
||||||
|
|||||||
+4
-4
@@ -25,7 +25,7 @@ type Map struct {
|
|||||||
byKey []string
|
byKey []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Map) insert(name string) (ID, error) {
|
func (m *Map) Insert(name string) (ID, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
@@ -158,7 +158,7 @@ loop:
|
|||||||
return nil, nil, fmt.Errorf("token: string too long at %s:%d", filename, line)
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -174,7 +174,7 @@ loop:
|
|||||||
return nil, nil, fmt.Errorf("token: identifier too long at %s:%d", filename, line)
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -200,7 +200,7 @@ loop:
|
|||||||
return nil, nil, fmt.Errorf("token: constant too long at %s:%d", filename, line)
|
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 {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,11 +104,16 @@ pri func decoder.init_huffs?(src reader1)() {
|
|||||||
bits >>= 4
|
bits >>= 4
|
||||||
n_bits -= 14
|
n_bits -= 14
|
||||||
|
|
||||||
// Read the clcode Huffman table.
|
// Read the clcode Huffman table: H-CL.
|
||||||
var i u32 = 0
|
var i u32 = 0
|
||||||
while i < n_clen {
|
while i < n_clen,
|
||||||
|
inv n_dist <= 30,
|
||||||
|
inv n_lit <= 286,
|
||||||
|
{
|
||||||
while n_bits < 3,
|
while n_bits < 3,
|
||||||
inv i < n_clen,
|
inv i < n_clen,
|
||||||
|
inv n_dist <= 30,
|
||||||
|
inv n_lit <= 286,
|
||||||
post n_bits >= 3,
|
post n_bits >= 3,
|
||||||
{
|
{
|
||||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||||
@@ -120,14 +125,23 @@ pri func decoder.init_huffs?(src reader1)() {
|
|||||||
n_bits -= 3
|
n_bits -= 3
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
while i < 19 {
|
while i < 19,
|
||||||
|
inv n_dist <= 30,
|
||||||
|
inv n_lit <= 286,
|
||||||
|
{
|
||||||
this.code_lengths[code_order[i]] = 0
|
this.code_lengths[code_order[i]] = 0
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
this.init_huff?(which:0, n_codes:19)
|
this.init_huff?(which:0, n_codes:19)
|
||||||
|
|
||||||
|
// Decode the code lengths for the next two Huffman tables.
|
||||||
i = 0
|
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.
|
// Decode a clcode symbol from H-CL.
|
||||||
var symbol u32[..289]
|
var symbol u32[..289]
|
||||||
var code u32
|
var code u32
|
||||||
@@ -138,7 +152,9 @@ pri func decoder.init_huffs?(src reader1)() {
|
|||||||
while true,
|
while true,
|
||||||
pre first <= 0xFFFF,
|
pre first <= 0xFFFF,
|
||||||
pre index <= 0xFFFF,
|
pre index <= 0xFFFF,
|
||||||
inv i < 31,
|
inv i < 316,
|
||||||
|
inv n_dist <= 30,
|
||||||
|
inv n_lit <= 286,
|
||||||
{
|
{
|
||||||
if length > 15 {
|
if length > 15 {
|
||||||
return error "bad Huffman code"
|
return error "bad Huffman code"
|
||||||
@@ -147,9 +163,11 @@ pri func decoder.init_huffs?(src reader1)() {
|
|||||||
// Read the next bit.
|
// Read the next bit.
|
||||||
while n_bits <= 0,
|
while n_bits <= 0,
|
||||||
inv first <= 0xFFFF,
|
inv first <= 0xFFFF,
|
||||||
|
inv i < 316,
|
||||||
inv index <= 0xFFFF,
|
inv index <= 0xFFFF,
|
||||||
inv i < 31,
|
|
||||||
inv length <= 15,
|
inv length <= 15,
|
||||||
|
inv n_dist <= 30,
|
||||||
|
inv n_lit <= 286,
|
||||||
post n_bits > 0,
|
post n_bits > 0,
|
||||||
{
|
{
|
||||||
bits |= (in.src.read_u8?() as u32) << n_bits
|
bits |= (in.src.read_u8?() as u32) << n_bits
|
||||||
@@ -234,7 +252,9 @@ pri func decoder.init_huffs?(src reader1)() {
|
|||||||
n_extra_bits = 7
|
n_extra_bits = 7
|
||||||
}
|
}
|
||||||
while n_bits < n_extra_bits,
|
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,
|
post n_bits >= n_extra_bits,
|
||||||
{
|
{
|
||||||
assert n_bits < 7 via "a < b: a < c; c <= b"(c:n_extra_bits)
|
assert n_bits < 7 via "a < b: a < c; c <= b"(c:n_extra_bits)
|
||||||
|
|||||||
Reference in new issue
Block a user