From bf51551eda4544fedf7baa0c557a26f47902f5b1 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 27 Jul 2017 14:42:26 +1000 Subject: [PATCH] Have init_huff take a slice of the code_lengths. --- cmd/puffs-gen-c/func.go | 15 ++++++++------- gen/c/std/flate.c | 19 +++++++++++-------- lang/check/bounds.go | 2 +- lang/check/type.go | 2 +- std/flate/decode_flate.puffs | 21 +++++++++++---------- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index bee0f9e7..f0ce735b 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -977,15 +977,16 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error { } g.writes("if (status) { goto suspend; }\n") - } else if isThisMethod(g.tm, n, "init_huff", 2) { + } else if isThisMethod(g.tm, n, "init_huff", 3) { g.printf("status = %s%s_init_huff(self,", g.pkgPrefix, g.perFunc.funk.Receiver().String(g.tm)) - if err := g.writeExpr(n.Args()[0].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil { - return err - } - g.writes(",") - if err := g.writeExpr(n.Args()[1].Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil { - return err + for i, o := range n.Args() { + if i != 0 { + g.writes(",") + } + if err := g.writeExpr(o.Arg().Value(), replaceNothing, parenthesesMandatory, depth); err != nil { + return err + } } g.writes(");\n") if err := g.writeLoadExprDerivedVars(n); err != nil { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index fefacc04..5d535d04 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -353,7 +353,8 @@ puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self, puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, uint32_t a_which, - uint32_t a_n_codes); + uint32_t a_n_codes0, + uint32_t a_n_codes1); // ---------------- Constructor and Destructor Implementations @@ -823,7 +824,7 @@ puffs_flate_status puffs_flate_decoder_init_huffs(puffs_flate_decoder* self, v_i += 1; } PUFFS_COROUTINE_SUSPENSION_POINT(3); - status = puffs_flate_decoder_init_huff(self, 0, 19); + status = puffs_flate_decoder_init_huff(self, 0, 0, 19); if (status) { goto suspend; } @@ -965,7 +966,8 @@ short_read_src: puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, uint32_t a_which, - uint32_t a_n_codes) { + uint32_t a_n_codes0, + uint32_t a_n_codes1) { puffs_flate_status status = PUFFS_FLATE_STATUS_OK; uint32_t v_i; @@ -1006,8 +1008,8 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, self->private_impl.f_counts[v_i] = 0; v_i += 1; } - v_i = 0; - while (v_i < a_n_codes) { + v_i = a_n_codes0; + while (v_i < a_n_codes1) { if (self->private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] >= 289) { status = @@ -1017,7 +1019,8 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, self->private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] += 1; v_i += 1; } - if (((uint32_t)(self->private_impl.f_counts[0])) == a_n_codes) { + if ((((uint32_t)(self->private_impl.f_counts[0])) + a_n_codes0) == + a_n_codes1) { status = PUFFS_FLATE_ERROR_NO_HUFFMAN_CODES; goto exit; } @@ -1040,8 +1043,8 @@ puffs_flate_status puffs_flate_decoder_init_huff(puffs_flate_decoder* self, v_total = (v_total + v_count); v_i += 1; } - v_i = 0; - while (v_i < a_n_codes) { + v_i = a_n_codes0; + while (v_i < a_n_codes1) { if (self->private_impl.f_code_lengths[v_i] != 0) { self->private_impl .f_symbols[v_offsets[self->private_impl.f_code_lengths[v_i]]] = diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 44ff3438..c4f366c7 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -650,7 +650,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, 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_dynamic", 2) || - isThisMethod(q.tm, n, "init_huffs", 1) || isThisMethod(q.tm, n, "init_huff", 2) { + isThisMethod(q.tm, n, "init_huffs", 1) || isThisMethod(q.tm, n, "init_huff", 3) { for _, o := range n.Args() { // TODO: check that the arg range at the caller and the diff --git a/lang/check/type.go b/lang/check/type.go index c7bc4187..7c86daa9 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -350,7 +350,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { 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_dynamic", 2) || - isThisMethod(q.tm, n, "init_huffs", 1) || isThisMethod(q.tm, n, "init_huff", 2) { + isThisMethod(q.tm, n, "init_huffs", 1) || isThisMethod(q.tm, n, "init_huff", 3) { if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil { return err diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index 5fdf14e9..b077de49 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -162,7 +162,7 @@ pri func decoder.init_huffs?(src reader1)() { this.code_lengths[code_order[i]] = 0 i += 1 } - this.init_huff?(which:0, n_codes:19) + this.init_huff?(which:0, n_codes0:0, n_codes1:19) // Decode the code lengths for the next two Huffman tables. var mask u32[..511] = ((1 as u32) << this.n_huffs_bits[0]) - 1 @@ -275,9 +275,10 @@ pri func decoder.init_huffs?(src reader1)() { // TODO: skip generating PUFFS_COROUTINE_SUSPENSION_POINTs for this function, // and its callers, as it can only return errors, not suspensions. -pri func decoder.init_huff?(which u32[..1], n_codes u32[..289])() { +pri func decoder.init_huff?(which u32[..1], n_codes0 u32[..289], n_codes1 u32[..289])() { // For the clcode example in this package's README.md: - // - n_codes = 19 + // - n_codes0 = 0 + // - n_codes1 = 19 // - code_lengths[ 0] = 3 // - code_lengths[ 1] = 0 // - code_lengths[ 2] = 0 @@ -303,9 +304,9 @@ pri func decoder.init_huff?(which u32[..1], n_codes u32[..289])() { this.counts[i] = 0 i += 1 } - i = 0 - while i < in.n_codes { - assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes) + i = in.n_codes0 + while i < in.n_codes1 { + assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes1) // TODO: this if should be unnecessary. Have some way to assert that, // for all j, counts[j] <= i, and thus counts[j]++ will not overflow. if this.counts[this.code_lengths[i]] >= 289 { @@ -314,7 +315,7 @@ pri func decoder.init_huff?(which u32[..1], n_codes u32[..289])() { this.counts[this.code_lengths[i]] += 1 i += 1 } - if (this.counts[0] as u32) == in.n_codes { + if ((this.counts[0] as u32) + in.n_codes0) == in.n_codes1 { return error "no Huffman codes" } @@ -355,9 +356,9 @@ pri func decoder.init_huff?(which u32[..1], n_codes u32[..289])() { // - offsets[6] = 10 // - all other offsets elements are 10. - i = 0 - while i < in.n_codes { - assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes) + i = in.n_codes0 + while i < in.n_codes1 { + assert i < 289 via "a < b: a < c; c <= b"(c:in.n_codes1) if this.code_lengths[i] != 0 { this.symbols[offsets[this.code_lengths[i]]] = i as u16 if offsets[this.code_lengths[i]] >= 289 {