From 209178a2c6d2f7de523342716bdfe0b51f4e9ff3 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 31 Aug 2017 11:16:23 +1000 Subject: [PATCH] Add a std/flate decode_blocks method. --- cmd/puffs-c/internal/cgen/func.go | 8 +++++ gen/c/std/flate.c | 54 +++++++++++++++++++++++++------ gen/h/std/flate.h | 5 ++- lang/check/bounds.go | 1 + lang/check/type.go | 1 + std/flate/decode_flate.puffs | 5 +++ 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/func.go b/cmd/puffs-c/internal/cgen/func.go index 673c7996..5abbdb20 100644 --- a/cmd/puffs-c/internal/cgen/func.go +++ b/cmd/puffs-c/internal/cgen/func.go @@ -1089,6 +1089,14 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error { } b.writes("if (status) { goto suspend; }\n") + } else if isThisMethod(g.tm, n, "decode_blocks", 2) { + b.printf("status = %s%s_decode_blocks(self, %sdst, %ssrc);\n", + g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix) + if err := g.writeLoadExprDerivedVars(b, n); err != nil { + return err + } + b.writes("if (status) { goto suspend; }\n") + } else if isThisMethod(g.tm, n, "decode_uncompressed", 2) { b.printf("status = %s%s_decode_uncompressed(self, %sdst, %ssrc);\n", g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix, aPrefix) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 73ccb8c0..60f973a7 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -160,11 +160,14 @@ typedef struct { uint32_t f_n_huffs_bits[2]; uint8_t f_code_lengths[320]; + struct { + uint32_t coro_susp_point; + } c_decode[1]; struct { uint32_t coro_susp_point; uint32_t v_final; uint32_t v_type; - } c_decode[1]; + } c_decode_blocks[1]; struct { uint32_t coro_susp_point; uint32_t v_n; @@ -413,6 +416,10 @@ static const uint32_t puffs_flate_dcode_magic_numbers[32] = { // ---------------- Private Function Prototypes +puffs_flate_status puffs_flate_decoder_decode_blocks(puffs_flate_decoder* self, + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src); + puffs_flate_status puffs_flate_decoder_decode_uncompressed( puffs_flate_decoder* self, puffs_base_writer1 a_dst, @@ -514,6 +521,35 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, } puffs_flate_status status = PUFFS_FLATE_STATUS_OK; + uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; + if (coro_susp_point) { + } + switch (coro_susp_point) { + PUFFS_COROUTINE_SUSPENSION_POINT(0); + + PUFFS_COROUTINE_SUSPENSION_POINT(1); + status = puffs_flate_decoder_decode_blocks(self, a_dst, a_src); + if (status) { + goto suspend; + } + self->private_impl.c_decode[0].coro_susp_point = 0; + goto exit; + } + + goto suspend; +suspend: + self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; + +exit: + self->private_impl.status = status; + return status; +} + +puffs_flate_status puffs_flate_decoder_decode_blocks(puffs_flate_decoder* self, + puffs_base_writer1 a_dst, + puffs_base_reader1 a_src) { + puffs_flate_status status = PUFFS_FLATE_STATUS_OK; + uint32_t v_final; uint32_t v_type; @@ -531,10 +567,11 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, b_rend_src = b_rptr_src + len; } - uint32_t coro_susp_point = self->private_impl.c_decode[0].coro_susp_point; + uint32_t coro_susp_point = + self->private_impl.c_decode_blocks[0].coro_susp_point; if (coro_susp_point) { - v_final = self->private_impl.c_decode[0].v_final; - v_type = self->private_impl.c_decode[0].v_type; + v_final = self->private_impl.c_decode_blocks[0].v_final; + v_type = self->private_impl.c_decode_blocks[0].v_type; } switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); @@ -648,15 +685,15 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, goto suspend; } } - self->private_impl.c_decode[0].coro_susp_point = 0; + self->private_impl.c_decode_blocks[0].coro_susp_point = 0; goto exit; } goto suspend; suspend: - self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; - self->private_impl.c_decode[0].v_final = v_final; - self->private_impl.c_decode[0].v_type = v_type; + self->private_impl.c_decode_blocks[0].coro_susp_point = coro_susp_point; + self->private_impl.c_decode_blocks[0].v_final = v_final; + self->private_impl.c_decode_blocks[0].v_type = v_type; exit: if (a_src.buf) { @@ -670,7 +707,6 @@ exit: } } - self->private_impl.status = status; return status; short_read_src: diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index ca9452d9..669f911f 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -160,11 +160,14 @@ typedef struct { uint32_t f_n_huffs_bits[2]; uint8_t f_code_lengths[320]; + struct { + uint32_t coro_susp_point; + } c_decode[1]; struct { uint32_t coro_susp_point; uint32_t v_final; uint32_t v_type; - } c_decode[1]; + } c_decode_blocks[1]; struct { uint32_t coro_susp_point; uint32_t v_n; diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 140f4fbe..c38325ac 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -652,6 +652,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_huffman", 2) || + isThisMethod(q.tm, n, "decode_blocks", 2) || isThisMethod(q.tm, n, "init_fixed_huffman", 0) || isThisMethod(q.tm, n, "init_dynamic_huffman", 1) || isThisMethod(q.tm, n, "init_huff", 4) { diff --git a/lang/check/type.go b/lang/check/type.go index 3c2017e3..98a3d075 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -352,6 +352,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_huffman", 2) || + isThisMethod(q.tm, n, "decode_blocks", 2) || isThisMethod(q.tm, n, "init_fixed_huffman", 0) || isThisMethod(q.tm, n, "init_dynamic_huffman", 1) || isThisMethod(q.tm, n, "init_huff", 4) { diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index c567a951..9f9a5ef7 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -90,6 +90,11 @@ pub struct decoder?( ) pub func decoder.decode?(dst writer1, src reader1)() { + // TODO: copy from dst to history, if we aren't finished. + this.decode_blocks?(dst:in.dst, src:in.src) +} + +pri func decoder.decode_blocks?(dst writer1, src reader1)() { var final u32 while final == 0 { while this.n_bits < 3,