diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index 7f3293fe..9dee5981 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -969,12 +969,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_huffman", 1) { + } else if isThisMethod(g.tm, n, "init_huffman", 2) { g.printf("status = %s%s_init_huffman(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 + } g.writes(");\n") if err := g.writeLoadExprDerivedVars(n); err != nil { return err @@ -1120,7 +1124,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro switch n.ID0().Key() { case 0: if id1 := n.ID1(); id1.Key() == t.KeyThis { - g.writes("self->private_impl") + g.writes("self") } else { if n.GlobalIdent() { g.writes(g.pkgPrefix) @@ -1187,20 +1191,22 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro // TODO. case t.KeyDot: - if n.LHS().Expr().ID1().Key() == t.KeyIn { + lhs := n.LHS().Expr() + if lhs.ID1().Key() == t.KeyIn { g.writes(aPrefix) g.writes(n.ID1().String(g.tm)) return nil } - if err := g.writeExpr(n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil { + if err := g.writeExpr(lhs, rp, parenthesesMandatory, depth); err != nil { return err } - // TODO: choose between . vs -> operators. - // - // TODO: don't assume that the fPrefix is necessary. - g.writes(".") - g.writes(fPrefix) + if k := lhs.MType().Decorator().Key(); k == t.KeyPtr || k == t.KeyNptr { + g.writes("->") + } else { + g.writes(".") + } + g.writes("private_impl." + fPrefix) g.writes(n.ID1().String(g.tm)) return nil diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 2dff08fe..7ac9f93b 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -131,6 +131,7 @@ typedef struct { // // It is a struct, not a struct*, so that it can be stack allocated. struct { + uint16_t f_counts[16]; } private_impl; } puffs_flate_huffman_decoder; @@ -174,7 +175,6 @@ typedef struct { } c_decode_dynamic[1]; struct { uint32_t coro_susp_point; - uint32_t v_counts[16]; uint32_t v_i; } c_init_huffman[1]; } private_impl; @@ -307,6 +307,7 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic(puffs_flate_decoder* self, puffs_base_reader1 a_src); puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self, + uint32_t a_which, uint32_t a_n_codes); // ---------------- Constructor and Destructor Implementations @@ -739,7 +740,7 @@ puffs_flate_status puffs_flate_decoder_decode_dynamic( self->private_impl.f_bits = v_bits; self->private_impl.f_n_bits = v_n_bits; PUFFS_COROUTINE_SUSPENSION_POINT(3); - status = puffs_flate_decoder_init_huffman(self, 19); + status = puffs_flate_decoder_init_huffman(self, 0, 19); if (status) { goto suspend; } @@ -786,37 +787,40 @@ short_read_src: } puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self, + uint32_t a_which, uint32_t a_n_codes) { puffs_flate_status status = PUFFS_FLATE_STATUS_OK; - uint32_t v_counts[16]; uint32_t v_i; uint32_t coro_susp_point = self->private_impl.c_init_huffman[0].coro_susp_point; if (coro_susp_point) { - memcpy(v_counts, self->private_impl.c_init_huffman[0].v_counts, 4 * 16); v_i = self->private_impl.c_init_huffman[0].v_i; } switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); - { - size_t i; - for (i = 0; i < 16; i++) { - v_counts[i] = 0; - } - }; + v_i = 0; + while (v_i < 16) { + self->private_impl.f_huff[a_which].private_impl.f_counts[v_i] = 0; + v_i += 1; + } v_i = 0; while (v_i < a_n_codes) { - if (v_counts[self->private_impl.f_code_lengths[v_i]] >= 289) { + if (self->private_impl.f_huff[a_which] + .private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] >= + 289) { status = PUFFS_FLATE_ERROR_INCONSISTENT_HUFFMAN_TABLE; goto exit; } - v_counts[self->private_impl.f_code_lengths[v_i]] += 1; + self->private_impl.f_huff[a_which] + .private_impl.f_counts[self->private_impl.f_code_lengths[v_i]] += 1; v_i += 1; } - if (v_counts[0] == a_n_codes) { + if (((uint32_t)( + self->private_impl.f_huff[a_which].private_impl.f_counts[0])) == + a_n_codes) { status = PUFFS_FLATE_ERROR_INCONSISTENT_HUFFMAN_TABLE; goto exit; } @@ -827,7 +831,6 @@ puffs_flate_status puffs_flate_decoder_init_huffman(puffs_flate_decoder* self, goto suspend; suspend: self->private_impl.c_init_huffman[0].coro_susp_point = coro_susp_point; - memcpy(self->private_impl.c_init_huffman[0].v_counts, v_counts, 4 * 16); self->private_impl.c_init_huffman[0].v_i = v_i; exit: diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index 216d801b..b56c3bf5 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -131,6 +131,7 @@ typedef struct { // // It is a struct, not a struct*, so that it can be stack allocated. struct { + uint16_t f_counts[16]; } private_impl; } puffs_flate_huffman_decoder; @@ -174,7 +175,6 @@ typedef struct { } c_decode_dynamic[1]; struct { uint32_t coro_susp_point; - uint32_t v_counts[16]; uint32_t v_i; } c_init_huffman[1]; } private_impl; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 0bbdd02c..36226187 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -625,7 +625,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_huffman", 1) { + isThisMethod(q.tm, n, "init_huffman", 2) { for _, o := range n.Args() { // TODO: check that the arg range at the caller and the @@ -913,7 +913,7 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) { b = numTypeBounds[key] } if b[0] == nil || b[1] == nil { - return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.tm)) + return nil, nil, nil } if n.IsRefined() { if x := n.Min(); x != nil { diff --git a/lang/check/type.go b/lang/check/type.go index 918ed68d..aa1ff5b5 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_huffman", 1) { + isThisMethod(q.tm, n, "init_huffman", 2) { 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 171c5843..5c1f4090 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -117,7 +117,7 @@ pri func decoder.decode_dynamic?(dst writer1, src reader1)() { this.bits = bits this.n_bits = n_bits - this.init_huffman?(n_codes:19) + this.init_huffman?(which:0, n_codes:19) // TODO: implement. this.wip0 = hlit @@ -132,20 +132,24 @@ pri func decoder.decode_dynamic?(dst writer1, 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_huffman?(n_codes u32[..289])() { - var counts[16] u32[..289] +pri func decoder.init_huffman?(which u32[..1], n_codes u32[..289])() { var i u32 + while i < 16 { + this.huff[in.which].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) // 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 counts[this.code_lengths[i]] >= 289 { + if this.huff[in.which].counts[this.code_lengths[i]] >= 289 { return error "inconsistent Huffman table" } - counts[this.code_lengths[i]] += 1 + this.huff[in.which].counts[this.code_lengths[i]] += 1 i += 1 } - if counts[0] == in.n_codes { + if (this.huff[in.which].counts[0] as u32) == in.n_codes { // There are no codes. return error "inconsistent Huffman table" } @@ -160,4 +164,5 @@ pri func decoder.init_huffman?(n_codes u32[..289])() { } pri struct huffman_decoder( + counts[16] u16[..289], )