From e2ce9f1b1d863aeb132b43d54ad43268ea33c919 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 9 Nov 2017 10:37:25 +1100 Subject: [PATCH] Add a writer1.is_marked method. --- cmd/puffs-c/internal/cgen/expr.go | 11 +++++++++++ cmd/puffs-c/internal/cgen/statement.go | 1 + gen/c/std/flate.c | 4 ++++ lang/check/bounds.go | 2 +- lang/check/type.go | 5 ++++- lang/token/list.go | 3 +++ std/flate/decode_huffman_fast.puffs | 17 ++++++++++++++--- 7 files changed, 38 insertions(+), 5 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index c1251e62..3310df35 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -185,6 +185,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare aPrefix, aPrefix, bPrefix, aPrefix) return nil } + // TODO: reader1.is_marked, not just writer1.is_marked? if isInDst(g.tm, n, t.KeyLimit, 1) { return fmt.Errorf(`TODO: cgen an "in.dst.limit" expression`) } @@ -203,6 +204,16 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare aPrefix, aPrefix, bPrefix, aPrefix) return nil } + if isInDst(g.tm, n, t.KeyIsMarked, 0) { + if pp == parenthesesMandatory { + b.writeb('(') + } + b.printf("%sdst.private_impl.mark != NULL", aPrefix) + if pp == parenthesesMandatory { + b.writeb(')') + } + return nil + } if isInDst(g.tm, n, t.KeyCopyFromReader32, 2) { b.printf("puffs_base__writer1__copy_from_reader32(&%swptr_dst, %swend_dst", bPrefix, bPrefix) // TODO: don't assume that the first argument is "in.src". diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 81b4f459..225bcb74 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -785,6 +785,7 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { methodName != t.KeyCopyFromSlice32 && methodName != t.KeyCopyFromSlice && methodName != t.KeySinceMark && + methodName != t.KeyIsMarked && methodName != t.KeyMark && methodName != t.KeyLimit // TODO: check that n.Args() is "(x:bar)". diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 98931012..944a0c4b 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -2017,6 +2017,10 @@ static puffs_flate__status puffs_flate__flate_decoder__decode_huffman_fast( b_rend_src = b_rptr_src + len; } + if ((a_dst.private_impl.mark != NULL) != true) { + status = PUFFS_FLATE__ERROR_BAD_ARGUMENT; + goto exit; + } if ((self->private_impl.f_n_bits >= 8) || ((self->private_impl.f_bits >> self->private_impl.f_n_bits) != 0)) { status = PUFFS_FLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_N_BITS; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 5437ed70..f3d6fb3e 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -750,7 +750,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) || isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) || isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySinceMark, 0) || - isInDst(q.tm, n, t.KeyMark, 0) || + isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeyIsMarked, 0) || isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) || isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) || isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_blocks", 2) || diff --git a/lang/check/type.go b/lang/check/type.go index 8293fef4..e7726446 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -415,7 +415,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { isInDst(q.tm, n, t.KeyCopyFromSlice, 1) || isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) || isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) || isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySinceMark, 0) || - isInDst(q.tm, n, t.KeyMark, 0) || + isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeyIsMarked, 0) || isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) || isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) || isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_blocks", 2) || @@ -446,6 +446,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { } else if isInDst(q.tm, n, t.KeyCopyFromSlice32, 2) || isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) { n.SetMType(typeExprU32) // HACK. + } else if isInDst(q.tm, n, t.KeyIsMarked, 0) { + n.SetMType(typeExprBool) // HACK. } else { n.SetMType(typeExprPlaceholder) // HACK. } @@ -684,6 +686,7 @@ func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool { methodName != t.KeyCopyFromSlice32 && methodName != t.KeyCopyFromSlice && methodName != t.KeySinceMark && + methodName != t.KeyIsMarked && methodName != t.KeyMark && methodName != t.KeyLimit // TODO: check that n.Args() is "(x:bar)". diff --git a/lang/token/list.go b/lang/token/list.go index fe23dafd..7d5685a7 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -303,6 +303,7 @@ const ( KeyLowBits = Key(IDLowBits >> KeyShift) KeyHighBits = Key(IDHighBits >> KeyShift) KeyUnreadU8 = Key(IDUnreadU8 >> KeyShift) + KeyIsMarked = Key(IDIsMarked >> KeyShift) KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift) KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift) @@ -490,6 +491,7 @@ const ( IDLowBits = ID(0xAE<= 8) or ((this.bits >> this.n_bits) != 0) { return error "internal error: inconsistent n_bits" } @@ -59,7 +64,9 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { // for each. There are also up to 13 extra bits, or 2 bytes. Thus, each // code requires up to 2 + 2 + 2 == 6 bytes, and there are two codes (one // from H-L and one from H-D). Therefore, we check for at least 12 bytes. - while:loop(in.dst.available() >= 258) and (in.src.available() >= 12) { + while:loop(in.dst.available() >= 258) and (in.src.available() >= 12), + inv in.dst.is_marked() == true, // TODO: remove the redundant "== true". + { // Ensure that we have at least 15 bits of input. if n_bits < 15 { assert n_bits >= 0 // TODO: this shouldn't be necessary. @@ -260,7 +267,9 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { // The "while true { etc; break }" is a redundant version of "etc", but // its presence minimizes the diff between decode_huffman_fast and // decode_huffman_slow. - while true { + while true, + inv in.dst.is_marked() == true, + { // Copy from this.history. if (distance as u64) > in.dst.since_mark().length() { // Set (hlen, hdist) to be the length-distance pair to copy @@ -288,7 +297,9 @@ pri func flate_decoder.decode_huffman_fast?(dst writer1, src reader1)() { // The "while true { etc; break }" is a redundant version of // "etc", but its presence minimizes the diff between // decode_huffman_fast and decode_huffman_slow. - while true { + while true, + inv in.dst.is_marked() == true, + { n_copied = in.dst.copy_from_slice32(s:this.history[hdist:], length:hlen) if hlen <= n_copied { break