From d974988eab53b8e7abfef8596676e1967b3356d6 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 2 Jul 2017 13:59:42 +1000 Subject: [PATCH] Reset coro_state to zero at function end. This allows us to call that coroutine function multiple times. --- cmd/puffs-gen-c/func.go | 8 +++++--- gen/c/std/gif.c | 12 ++++++------ test/c/std/gif.c | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index f2a85c80..c480f186 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -141,6 +141,8 @@ func (g *gen) writeFuncImpl(n *a.Func) error { g.writes("}\n") // Generate a coroutine switch similiar to the technique in // https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html + // + // The matching } is written below. See "Close the coroutine switch". g.writes("switch (coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n") } @@ -152,9 +154,9 @@ func (g *gen) writeFuncImpl(n *a.Func) error { } if g.perFunc.suspendible { - if err := g.writeSuspend(); err != nil { - return err - } + // We've reached the end of the function body. Reset the coroutine + // state so that the next call to this function starts at the top. + g.writes("coro_state = 0;\n") g.writes("}\n\n") // Close the coroutine switch. g.writes("goto suspend;\n") // Avoid the "unused label" warning. diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 6776850b..5476db00 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -536,7 +536,7 @@ puffs_gif_status puffs_gif_decoder_decode(puffs_gif_decoder* self, goto suspend; } } - PUFFS_COROUTINE_STATE(6); + coro_state = 0; } goto suspend; @@ -613,7 +613,7 @@ puffs_gif_status puffs_gif_decoder_decode_header(puffs_gif_decoder* self, ((v_c[4] != 55) && (v_c[4] != 57)) || (v_c[5] != 97)) { return puffs_gif_error_bad_gif_header; } - PUFFS_COROUTINE_STATE(2); + coro_state = 0; } goto suspend; @@ -727,7 +727,7 @@ puffs_gif_status puffs_gif_decoder_decode_lsd(puffs_gif_decoder* self, v_i += 1; } } - PUFFS_COROUTINE_STATE(5); + coro_state = 0; } goto suspend; @@ -823,7 +823,7 @@ puffs_gif_status puffs_gif_decoder_decode_extension(puffs_gif_decoder* self, b_rptr_src += t_2; } label_0_break:; - PUFFS_COROUTINE_STATE(4); + coro_state = 0; } goto suspend; @@ -975,7 +975,7 @@ puffs_gif_status puffs_gif_decoder_decode_id(puffs_gif_decoder* self, } } label_0_break:; - PUFFS_COROUTINE_STATE(5); + coro_state = 0; } goto suspend; @@ -1188,7 +1188,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, } v_prev_code = v_code; } - PUFFS_COROUTINE_STATE(4); + coro_state = 0; } goto suspend; diff --git a/test/c/std/gif.c b/test/c/std/gif.c index e2318cd3..71f9ba93 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -618,7 +618,7 @@ proc tests[] = { // Mimic Tests test_gif_mimic_decode_bricks_dither, // - // test_gif_mimic_decode_bricks_gray, // TODO + test_gif_mimic_decode_bricks_gray, // test_gif_mimic_decode_bricks_nodither, // test_gif_mimic_decode_harvesters, // test_gif_mimic_decode_hat, //