From 8ad82f5e1fcb9d8caa28007c4181b1d5b1aa0e60 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 23 Jun 2017 15:30:05 +1000 Subject: [PATCH] Generate C code to set coroutine state. --- cmd/puffs-gen-c/base-impl.h | 8 +++-- cmd/puffs-gen-c/data.go | 2 +- cmd/puffs-gen-c/main.go | 10 +++---- gen/c/std/gif.c | 44 +++++++++++++++++---------- test/c/std/gif.c | 60 ++++++++++++++++++++++++++++++++----- 5 files changed, 92 insertions(+), 32 deletions(-) diff --git a/cmd/puffs-gen-c/base-impl.h b/cmd/puffs-gen-c/base-impl.h index c5f8c3b3..a7a4ee0f 100644 --- a/cmd/puffs-gen-c/base-impl.h +++ b/cmd/puffs-gen-c/base-impl.h @@ -9,9 +9,11 @@ // Use switch cases for coroutine state, similar to the technique in // https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html // -// We use a trivial macro instead of an explicit case statement so that -// clang-format doesn't get confused by the unusual "case"s. -#define PUFFS_COROUTINE_STATE(n) case n: +// We use a trivial macro instead of an explicit assignment and case statement +// so that clang-format doesn't get confused by the unusual "case"s. +#define PUFFS_COROUTINE_STATE(n) \ + coro_state = n; \ + case n: #define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) diff --git a/cmd/puffs-gen-c/data.go b/cmd/puffs-gen-c/data.go index 85d07309..6e0ceaf3 100644 --- a/cmd/puffs-gen-c/data.go +++ b/cmd/puffs-gen-c/data.go @@ -12,5 +12,5 @@ const baseHeader = "" + "" const baseImpl = "" + - "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine state, similar to the technique in\n// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit case statement so that\n// clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_STATE(n) case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n#endif // PUFFS_BASE_IMPL_H\n" + + "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine state, similar to the technique in\n// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_STATE(n) \\\n coro_state = n; \\\n case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 3ebfa02a..0a8a9adb 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -641,17 +641,16 @@ func (g *gen) writeFuncImpl(n *a.Func) error { g.writes("\n") if g.perFunc.suspendible { - g.printf("if (self->private_impl.%s%s[0].coro_state) {\n", cPrefix, n.Name().String(g.tm)) + // TODO: don't hard-code [0], and allow recursive coroutines. + g.printf("uint32_t coro_state = self->private_impl.%s%s[0].coro_state;\n", cPrefix, n.Name().String(g.tm)) + g.printf("if (coro_state) {\n") if err := g.writeResumeSuspend(n.Body(), false); err != nil { return err } g.writes("}\n") // Generate a coroutine switch similiar to the technique in // https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html - // - // TODO: don't hard-code [0], and allow recursive coroutines. - g.printf("switch (self->private_impl.%s%s[0].coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n", - cPrefix, n.Name().String(g.tm)) + g.writes("switch (coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n") } // Generate the function body. @@ -669,6 +668,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error { g.writes("goto suspend;\n") // Avoid the "unused label" warning. g.writes("suspend:\n") + g.printf("self->private_impl.%s%s[0].coro_state = coro_state;\n", cPrefix, n.Name().String(g.tm)) if err := g.writeResumeSuspend(n.Body(), true); err != nil { return err } diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index fd7fb637..54078634 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -243,9 +243,11 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, // Use switch cases for coroutine state, similar to the technique in // https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html // -// We use a trivial macro instead of an explicit case statement so that -// clang-format doesn't get confused by the unusual "case"s. -#define PUFFS_COROUTINE_STATE(n) case n: +// We use a trivial macro instead of an explicit assignment and case statement +// so that clang-format doesn't get confused by the unusual "case"s. +#define PUFFS_COROUTINE_STATE(n) \ + coro_state = n; \ + case n: #define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) @@ -390,10 +392,11 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, uint8_t v_c; - if (self->private_impl.c_decode[0].coro_state) { + uint32_t coro_state = self->private_impl.c_decode[0].coro_state; + if (coro_state) { v_c = self->private_impl.c_decode[0].v_c; } - switch (self->private_impl.c_decode[0].coro_state) { + switch (coro_state) { PUFFS_COROUTINE_STATE(0); PUFFS_COROUTINE_STATE(1); @@ -445,6 +448,7 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, goto suspend; suspend: + self->private_impl.c_decode[0].coro_state = coro_state; self->private_impl.c_decode[0].v_c = v_c; goto exit; @@ -460,11 +464,12 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, uint8_t v_c[6]; uint32_t v_i; - if (self->private_impl.c_decode_header[0].coro_state) { + uint32_t coro_state = self->private_impl.c_decode_header[0].coro_state; + if (coro_state) { memcpy(v_c, self->private_impl.c_decode_header[0].v_c, 6); v_i = self->private_impl.c_decode_header[0].v_i; } - switch (self->private_impl.c_decode_header[0].coro_state) { + switch (coro_state) { PUFFS_COROUTINE_STATE(0); for (size_t i = 0; i < 6; i++) { @@ -496,6 +501,7 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, goto suspend; suspend: + self->private_impl.c_decode_header[0].coro_state = coro_state; memcpy(self->private_impl.c_decode_header[0].v_c, v_c, 6); self->private_impl.c_decode_header[0].v_i = v_i; @@ -512,12 +518,13 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, uint32_t v_i; uint32_t v_gct_size; - if (self->private_impl.c_decode_lsd[0].coro_state) { + uint32_t coro_state = self->private_impl.c_decode_lsd[0].coro_state; + if (coro_state) { memcpy(v_c, self->private_impl.c_decode_lsd[0].v_c, 7); v_i = self->private_impl.c_decode_lsd[0].v_i; v_gct_size = self->private_impl.c_decode_lsd[0].v_gct_size; } - switch (self->private_impl.c_decode_lsd[0].coro_state) { + switch (coro_state) { PUFFS_COROUTINE_STATE(0); for (size_t i = 0; i < 7; i++) { @@ -596,6 +603,7 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, goto suspend; suspend: + self->private_impl.c_decode_lsd[0].coro_state = coro_state; memcpy(self->private_impl.c_decode_lsd[0].v_c, v_c, 7); self->private_impl.c_decode_lsd[0].v_i = v_i; self->private_impl.c_decode_lsd[0].v_gct_size = v_gct_size; @@ -612,11 +620,12 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self, uint8_t v_label; uint8_t v_block_size; - if (self->private_impl.c_decode_extension[0].coro_state) { + uint32_t coro_state = self->private_impl.c_decode_extension[0].coro_state; + if (coro_state) { v_label = self->private_impl.c_decode_extension[0].v_label; v_block_size = self->private_impl.c_decode_extension[0].v_block_size; } - switch (self->private_impl.c_decode_extension[0].coro_state) { + switch (coro_state) { PUFFS_COROUTINE_STATE(0); PUFFS_COROUTINE_STATE(1); @@ -673,6 +682,7 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self, goto suspend; suspend: + self->private_impl.c_decode_extension[0].coro_state = coro_state; self->private_impl.c_decode_extension[0].v_label = v_label; self->private_impl.c_decode_extension[0].v_block_size = v_block_size; @@ -694,7 +704,8 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, uint64_t l_lzw_src; puffs_base_reader1 v_lzw_src; - if (self->private_impl.c_decode_id[0].coro_state) { + uint32_t coro_state = self->private_impl.c_decode_id[0].coro_state; + if (coro_state) { memcpy(v_c, self->private_impl.c_decode_id[0].v_c, 9); v_i = self->private_impl.c_decode_id[0].v_i; v_interlace = self->private_impl.c_decode_id[0].v_interlace; @@ -703,7 +714,7 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, l_lzw_src = self->private_impl.c_decode_id[0].l_lzw_src; v_lzw_src = self->private_impl.c_decode_id[0].v_lzw_src; } - switch (self->private_impl.c_decode_id[0].coro_state) { + switch (coro_state) { PUFFS_COROUTINE_STATE(0); for (size_t i = 0; i < 9; i++) { @@ -786,6 +797,7 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, goto suspend; suspend: + self->private_impl.c_decode_id[0].coro_state = coro_state; memcpy(self->private_impl.c_decode_id[0].v_c, v_c, 9); self->private_impl.c_decode_id[0].v_i = v_i; self->private_impl.c_decode_id[0].v_interlace = v_interlace; @@ -846,7 +858,8 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, uint32_t v_s; uint32_t v_c; - if (self->private_impl.c_decode[0].coro_state) { + uint32_t coro_state = self->private_impl.c_decode[0].coro_state; + if (coro_state) { v_clear_code = self->private_impl.c_decode[0].v_clear_code; v_end_code = self->private_impl.c_decode[0].v_end_code; v_use_save_code = self->private_impl.c_decode[0].v_use_save_code; @@ -859,7 +872,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, v_s = self->private_impl.c_decode[0].v_s; v_c = self->private_impl.c_decode[0].v_c; } - switch (self->private_impl.c_decode[0].coro_state) { + switch (coro_state) { PUFFS_COROUTINE_STATE(0); v_clear_code = (((uint32_t)(1)) << self->private_impl.f_literal_width); @@ -969,6 +982,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, goto suspend; suspend: + self->private_impl.c_decode[0].coro_state = coro_state; self->private_impl.c_decode[0].v_clear_code = v_clear_code; self->private_impl.c_decode[0].v_end_code = v_end_code; self->private_impl.c_decode[0].v_use_save_code = v_use_save_code; diff --git a/test/c/std/gif.c b/test/c/std/gif.c index 2780881a..915bf671 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -179,8 +179,7 @@ cleanup0: // ---------------- LZW Tests -void test_lzw_decode() { - test_funcname = __func__; +void test_lzw_decode_xxx(uint64_t limit) { puffs_base_buf1 got = {.ptr = global_got_buffer, .len = BUFFER_SIZE}; puffs_base_buf1 want = {.ptr = global_want_buffer, .len = BUFFER_SIZE}; puffs_base_buf1 src = {.ptr = global_src_buffer, .len = BUFFER_SIZE}; @@ -216,11 +215,45 @@ void test_lzw_decode() { puffs_gif_lzw_decoder_set_literal_width(&dec, literal_width); puffs_base_writer1 got_writer = {.buf = &got}; puffs_base_reader1 src_reader = {.buf = &src}; - puffs_gif_status status = - puffs_gif_lzw_decoder_decode(&dec, got_writer, src_reader); - if (status != puffs_gif_status_ok) { - FAIL("status: got %d, want %d", status, puffs_gif_status_ok); - goto cleanup1; + int num_iters = 0; + while (true) { + num_iters++; + uint64_t lim = limit; + src_reader.limit.ptr_to_len = &lim; + size_t old_ri = src.ri; + puffs_gif_status status = + puffs_gif_lzw_decoder_decode(&dec, got_writer, src_reader); + if (src.ri == src.wi) { + if (status != puffs_gif_status_ok) { + FAIL("status: got %d, want %d", status, puffs_gif_status_ok); + goto cleanup1; + } + break; + } + if (status != puffs_gif_status_short_read) { + FAIL("status: got %d, want %d", status, puffs_gif_status_short_read); + goto cleanup1; + } + if (src.ri < old_ri) { + FAIL("read index src.ri went backwards"); + goto cleanup1; + } + if (src.ri == old_ri) { + FAIL("no progress was made"); + goto cleanup1; + } + } + + if (limit < 1000000) { + if (num_iters <= 1) { + FAIL("num_iters: got %d, want > 1", num_iters); + goto cleanup1; + } + } else { + if (num_iters != 1) { + FAIL("num_iters: got %d, want 1", num_iters); + goto cleanup1; + } } if (!buf1s_equal("", &got, &want)) { @@ -238,6 +271,16 @@ cleanup1: cleanup0:; } +void test_lzw_decode_many_small_inputs() { + test_funcname = __func__; + test_lzw_decode_xxx(100); +} + +void test_lzw_decode_one_large_input() { + test_funcname = __func__; + test_lzw_decode_xxx(1 << 30); +} + // ---------------- GIF Tests void test_gif_decode_input_is_a_xxx(const char* filename, @@ -328,7 +371,8 @@ test tests[] = { test_basic_status_strings, // test_basic_sub_struct_constructor, // // LZW Tests - test_lzw_decode, // + test_lzw_decode_many_small_inputs, // + test_lzw_decode_one_large_input, // // GIF Tests test_gif_decode_input_is_a_gif, // test_gif_decode_input_is_a_png, //