From 312cdc8a0a6098ba9844809d6877986bcc0aba09 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 24 Nov 2018 16:41:45 +1100 Subject: [PATCH] Mark deflate's init_huff as merely !? not !?? --- cmd/wuffs-c/internal/cgen/func.go | 4 +- cmd/wuffs-c/internal/cgen/statement.go | 15 ++- release/c/wuffs-unsupported-snapshot.h | 179 ++++++++----------------- std/deflate/decode_deflate.wuffs | 15 +-- 4 files changed, 74 insertions(+), 139 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/func.go b/cmd/wuffs-c/internal/cgen/func.go index c2d5305f..4b8bac42 100644 --- a/cmd/wuffs-c/internal/cgen/func.go +++ b/cmd/wuffs-c/internal/cgen/func.go @@ -346,9 +346,9 @@ func (g *gen) writeFuncImplEpilogue(b *buffer) error { b.writes("if (wuffs_base__status__is_error(status)) { " + "self->private_impl.magic = WUFFS_BASE__DISABLED; }\n") } - b.writes("return status;\n\n") + b.writes("return status;\n") } else if effect.Optional() { - // TODO. + b.writes("return NULL;\n") } else if g.currFunk.astFunc.Out() == nil { b.writes("return ((wuffs_base__empty_struct){});\n") } diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index e3476a2c..2b9524b1 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -158,13 +158,22 @@ func (g *gen) writeStatementExpr(b *buffer, n *a.Expr, depth uint32) error { if err := g.writeSuspendibles(b, n, depth); err != nil { return err } - if n.Effect().RootCause() && n.Effect().Coroutine() { - return nil + checkStatus := false + if n.Effect().RootCause() { + if n.Effect().Coroutine() { + return nil + } else if n.Effect().Optional() { + b.writes("status = ") + checkStatus = true + } } if err := g.writeExpr(b, n, replaceCallSuspendibles, depth); err != nil { return err } b.writes(";\n") + if checkStatus { + b.writes("if (status) { goto exit; }\n") + } return nil } @@ -364,7 +373,7 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error { } b.writes("return ") - if g.currFunk.astFunc.Out() == nil { // TODO: Out() can be nil if the func has ? effect. + if g.currFunk.astFunc.Out() == nil && !g.currFunk.astFunc.Effect().Optional() { return fmt.Errorf("TODO: allow empty return type (when not suspendible)") } else if err := g.writeExpr(b, retExpr, replaceCallSuspendibles, depth); err != nil { return err diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index cab9f6cc..63c649c6 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2374,9 +2374,6 @@ struct wuffs_deflate__decoder__struct { uint32_t v_length; uint64_t scratch; } c_decode_uncompressed[1]; - struct { - uint32_t coro_susp_point; - } c_init_fixed_huffman[1]; struct { uint32_t coro_susp_point; uint32_t v_bits; @@ -5427,57 +5424,35 @@ wuffs_deflate__decoder__init_fixed_huffman(wuffs_deflate__decoder* self) { uint32_t v_i; - uint32_t coro_susp_point = - self->private_impl.c_init_fixed_huffman[0].coro_susp_point; - if (coro_susp_point) { - v_i = 0; - } else { + v_i = 0; + while (v_i < 144) { + self->private_impl.f_code_lengths[v_i] = 8; + v_i += 1; } - switch (coro_susp_point) { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - - v_i = 0; - while (v_i < 144) { - self->private_impl.f_code_lengths[v_i] = 8; - v_i += 1; - } - while (v_i < 256) { - self->private_impl.f_code_lengths[v_i] = 9; - v_i += 1; - } - while (v_i < 280) { - self->private_impl.f_code_lengths[v_i] = 7; - v_i += 1; - } - while (v_i < 288) { - self->private_impl.f_code_lengths[v_i] = 8; - v_i += 1; - } - while (v_i < 320) { - self->private_impl.f_code_lengths[v_i] = 5; - v_i += 1; - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); - status = wuffs_deflate__decoder__init_huff(self, 0, 0, 288, 257); - if (status) { - goto suspend; - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); - status = wuffs_deflate__decoder__init_huff(self, 1, 288, 320, 0); - if (status) { - goto suspend; - } - - goto ok; - ok: - self->private_impl.c_init_fixed_huffman[0].coro_susp_point = 0; + while (v_i < 256) { + self->private_impl.f_code_lengths[v_i] = 9; + v_i += 1; + } + while (v_i < 280) { + self->private_impl.f_code_lengths[v_i] = 7; + v_i += 1; + } + while (v_i < 288) { + self->private_impl.f_code_lengths[v_i] = 8; + v_i += 1; + } + while (v_i < 320) { + self->private_impl.f_code_lengths[v_i] = 5; + v_i += 1; + } + status = wuffs_deflate__decoder__init_huff(self, 0, 0, 288, 257); + if (status) { + goto exit; + } + status = wuffs_deflate__decoder__init_huff(self, 1, 288, 320, 0); + if (status) { goto exit; } - - goto suspend; -suspend: - self->private_impl.c_init_fixed_huffman[0].coro_susp_point = coro_susp_point; - goto exit; exit: return status; @@ -5594,10 +5569,9 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, self->private_impl.f_code_lengths[wuffs_deflate__code_order[v_i]] = 0; v_i += 1; } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); status = wuffs_deflate__decoder__init_huff(self, 0, 0, 19, 4095); if (status) { - goto suspend; + goto exit; } v_mask = ((((uint32_t)(1)) << self->private_impl.f_n_huffs_bits[0]) - 1); v_i = 0; @@ -5613,7 +5587,7 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, goto label_1_break; } { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) { status = wuffs_base__suspension__short_read; goto suspend; @@ -5661,7 +5635,7 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, } while (v_n_bits < v_n_extra_bits) { { - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) { status = wuffs_base__suspension__short_read; goto suspend; @@ -5692,16 +5666,14 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, status = wuffs_deflate__error__missing_end_of_block_code; goto exit; } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); status = wuffs_deflate__decoder__init_huff(self, 0, 0, v_n_lit, 257); if (status) { - goto suspend; + goto exit; } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); status = wuffs_deflate__decoder__init_huff(self, 1, v_n_lit, (v_n_lit + v_n_dist), 0); if (status) { - goto suspend; + goto exit; } self->private_impl.f_bits = v_bits; self->private_impl.f_n_bits = v_n_bits; @@ -5746,8 +5718,6 @@ wuffs_deflate__decoder__init_huff(wuffs_deflate__decoder* self, uint32_t a_n_codes0, uint32_t a_n_codes1, uint32_t a_base_symbol) { - wuffs_base__status status = NULL; - uint16_t v_counts[16]; uint32_t v_i; uint32_t v_remaining; @@ -5778,36 +5748,29 @@ wuffs_deflate__decoder__init_huff(wuffs_deflate__decoder* self, v_i = a_n_codes0; while (v_i < a_n_codes1) { if (v_counts[self->private_impl.f_code_lengths[v_i]] >= 320) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_counts[self->private_impl.f_code_lengths[v_i]] += 1; v_i += 1; } if ((((uint32_t)(v_counts[0])) + a_n_codes0) == a_n_codes1) { - status = wuffs_deflate__error__no_huffman_codes; - goto exit; + return wuffs_deflate__error__no_huffman_codes; } v_remaining = 1; v_i = 1; while (v_i <= 15) { if (v_remaining > 1073741824) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_remaining <<= 1; if (v_remaining < ((uint32_t)(v_counts[v_i]))) { - status = wuffs_deflate__error__bad_huffman_code_over_subscribed; - goto exit; + return wuffs_deflate__error__bad_huffman_code_over_subscribed; } v_remaining -= ((uint32_t)(v_counts[v_i])); v_i += 1; } if (v_remaining != 0) { - status = wuffs_deflate__error__bad_huffman_code_under_subscribed; - goto exit; + return wuffs_deflate__error__bad_huffman_code_under_subscribed; } memset(v_offsets, 0, sizeof(v_offsets)); v_n_symbols = 0; @@ -5816,31 +5779,23 @@ wuffs_deflate__decoder__init_huff(wuffs_deflate__decoder* self, v_offsets[v_i] = ((uint16_t)(v_n_symbols)); v_count = ((uint32_t)(v_counts[v_i])); if (v_n_symbols > (320 - v_count)) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_n_symbols = (v_n_symbols + v_count); v_i += 1; } if (v_n_symbols > 288) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } memset(v_symbols, 0, sizeof(v_symbols)); v_i = a_n_codes0; while (v_i < a_n_codes1) { if (v_i < a_n_codes0) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } if (self->private_impl.f_code_lengths[v_i] != 0) { if (v_offsets[self->private_impl.f_code_lengths[v_i]] >= 320) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_symbols[v_offsets[self->private_impl.f_code_lengths[v_i]]] = ((uint16_t)((v_i - a_n_codes0))); @@ -5854,8 +5809,7 @@ wuffs_deflate__decoder__init_huff(wuffs_deflate__decoder* self, goto label_0_break; } if (v_min_cl >= 9) { - status = wuffs_deflate__error__bad_huffman_minimum_code_length; - goto exit; + return wuffs_deflate__error__bad_huffman_minimum_code_length; } v_min_cl += 1; } @@ -5866,8 +5820,7 @@ label_0_break:; goto label_1_break; } if (v_max_cl <= 1) { - status = wuffs_deflate__error__no_huffman_codes; - goto exit; + return wuffs_deflate__error__no_huffman_codes; } v_max_cl -= 1; } @@ -5880,14 +5833,10 @@ label_1_break:; v_i = 0; if ((v_n_symbols != ((uint32_t)(v_offsets[v_max_cl]))) || (v_n_symbols != ((uint32_t)(v_offsets[15])))) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } if ((a_n_codes0 + ((uint32_t)(v_symbols[0]))) >= 320) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_initial_high_bits = 512; if (v_max_cl < 9) { @@ -5903,18 +5852,14 @@ label_1_break:; v_value = 0; while (true) { if ((a_n_codes0 + ((uint32_t)(v_symbols[v_i]))) >= 320) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_cl = ((uint32_t)(self->private_impl.f_code_lengths[( a_n_codes0 + ((uint32_t)(v_symbols[v_i])))])); if (v_cl > v_prev_cl) { v_code <<= (v_cl - v_prev_cl); if (v_code >= 32768) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } } v_prev_cl = v_cl; @@ -5934,26 +5879,20 @@ label_1_break:; } v_remaining -= ((uint32_t)(v_counts[v_j])); if (v_remaining > 1073741824) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_remaining <<= 1; v_j += 1; } label_2_break:; if ((v_j <= 9) || (15 < v_j)) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_tmp = (v_j - 9); v_initial_high_bits = (((uint32_t)(1)) << v_tmp); v_top = v_next_top; if ((v_top + (((uint32_t)(1)) << v_tmp)) > 1234) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_next_top = (v_top + (((uint32_t)(1)) << v_tmp)); v_redirect_key = @@ -5964,9 +5903,7 @@ label_1_break:; } } if ((v_key >= 512) || (v_counts[v_prev_cl] <= 0)) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_counts[v_prev_cl] -= 1; v_reversed_key = (((uint32_t)(wuffs_deflate__reverse8[(v_key >> 1)])) | @@ -5985,18 +5922,14 @@ label_1_break:; v_value = (wuffs_deflate__dcode_magic_numbers[(v_symbol & 31)] | v_cl); } } else { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } v_high_bits = v_initial_high_bits; v_delta = (((uint32_t)(1)) << v_cl); while (v_high_bits >= v_delta) { v_high_bits -= v_delta; if ((v_top + ((v_high_bits | v_reversed_key) & 511)) >= 1234) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } self->private_impl .f_huffs[a_which][(v_top + ((v_high_bits | v_reversed_key) & 511))] = @@ -6008,15 +5941,11 @@ label_1_break:; } v_code += 1; if (v_code >= 32768) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; + return wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; } } label_3_break:; - goto exit; -exit: - return status; + return NULL; } // -------- func deflate.decoder.decode_huffman_fast diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index 0574c5ae..91fcdd90 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -265,8 +265,8 @@ pri func decoder.init_fixed_huffman!??() { this.code_lengths[i] = 5 i += 1 } - this.init_huff!??(which:0, n_codes0:0, n_codes1:288, base_symbol:257) - this.init_huff!??(which:1, n_codes0:288, n_codes1:320, base_symbol:0) + this.init_huff!?(which:0, n_codes0:0, n_codes1:288, base_symbol:257) + this.init_huff!?(which:1, n_codes0:288, n_codes1:320, base_symbol:0) } // init_dynamic_huffman initializes this.huffs as per the RFC section 3.2.7. @@ -313,7 +313,7 @@ pri func decoder.init_dynamic_huffman!??(src base.io_reader) { this.code_lengths[code_order[i]] = 0 i += 1 } - this.init_huff!??(which:0, n_codes0:0, n_codes1:19, base_symbol:0xFFF) + this.init_huff!?(which:0, n_codes0:0, n_codes1:19, base_symbol:0xFFF) // Decode the code lengths for the next two Huffman tables. var mask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1 @@ -408,8 +408,8 @@ pri func decoder.init_dynamic_huffman!??(src base.io_reader) { return "?missing end-of-block code" } - this.init_huff!??(which:0, n_codes0:0, n_codes1:n_lit, base_symbol:257) - this.init_huff!??(which:1, n_codes0:n_lit, n_codes1:n_lit + n_dist, base_symbol:0) + this.init_huff!?(which:0, n_codes0:0, n_codes1:n_lit, base_symbol:257) + this.init_huff!?(which:1, n_codes0:n_lit, n_codes1:n_lit + n_dist, base_symbol:0) this.bits = bits this.n_bits = n_bits @@ -417,10 +417,7 @@ pri func decoder.init_dynamic_huffman!??(src base.io_reader) { // TODO: make named constants for 15, 19, 319, etc. -// TODO: skip generating WUFFS_COROUTINE_SUSPENSION_POINTs for *callers* of -// this function, as it can only return errors, not suspensions. - -pri func decoder.init_huff!??(which base.u32[..1], n_codes0 base.u32[..320], n_codes1 base.u32[..320], base_symbol base.u32) { +pri func decoder.init_huff!?(which base.u32[..1], n_codes0 base.u32[..320], n_codes1 base.u32[..320], base_symbol base.u32) { // For the clcode example in this package's README.md: // - n_codes0 = 0 // - n_codes1 = 19