From ec19f601d55e57d8ecad95621d4ad1437558e438 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 8 Jun 2017 16:40:01 +1000 Subject: [PATCH] Generate C return's. --- cmd/puffs-gen-c/main.go | 27 +++++++++++++++++++-------- gen/c/gif.c | 6 ++++++ std/gif/decode_lzw.puffs | 7 +++++-- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 5ef35f5d..c8bb6146 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -105,7 +105,8 @@ type perFunc struct { jumpTargets map[*a.While]uint32 tempW uint32 tempR uint32 - cleanup0 bool + public bool + suspendible bool } type gen struct { @@ -421,11 +422,13 @@ func (g *gen) writeFuncImpl(n *a.Func) error { // Check the previous status and the args. if n.Public() { + g.perFunc.public = true if n.Receiver() != 0 { g.printf("if (!self) { return puffs_%s_error_bad_receiver; }\n", g.pkgName) } } if n.Suspendible() { + g.perFunc.suspendible = true g.printf("puffs_%s_status status = ", g.pkgName) if n.Receiver() != 0 { g.printf("self->status;\n") @@ -438,7 +441,6 @@ func (g *gen) writeFuncImpl(n *a.Func) error { if n.Public() { g.printf("if (self->magic != PUFFS_MAGIC) {"+ "status = puffs_%s_error_constructor_not_called; goto cleanup0; }\n", g.pkgName) - g.perFunc.cleanup0 = true } } else if r := n.Receiver(); r != 0 { // TODO: fix this. @@ -487,10 +489,10 @@ func (g *gen) writeFuncImpl(n *a.Func) error { } g.writes("\n") - if g.perFunc.cleanup0 { - g.printf("cleanup0: self->status = status;\n") - } - if n.Suspendible() { + if g.perFunc.suspendible { + if g.perFunc.public { + g.printf("cleanup0: self->status = status;\n") + } g.printf("return status;\n") } @@ -650,7 +652,16 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error { return nil case a.KReturn: - // TODO, including considering suspendible calls. + if !g.perFunc.suspendible { + // TODO: consider the return values, especially if they involve + // suspendible function calls. + g.writes("return;\n") + } else if g.perFunc.public { + g.printf("status = puffs_%s_status_ok; goto cleanup0;\n", g.pkgName) + } else { + g.printf("return puffs_%s_status_ok;\n", g.pkgName) + } + return nil case a.KVar: n := n.Var() @@ -743,7 +754,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error { // src_final. g.printf("if (%ssrc->ri >= %ssrc->wi) { status = puffs_%s_status_short_read;", aPrefix, aPrefix, g.pkgName) - if g.perFunc.cleanup0 { + if g.perFunc.public && g.perFunc.suspendible { g.writes("goto cleanup0;") } else { g.writes("return status;") diff --git a/gen/c/gif.c b/gen/c/gif.c index d415dbd7..bf16b7d0 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -210,12 +210,14 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, uint32_t v_bits; uint32_t v_n_bits; uint32_t v_clear_code; + uint32_t v_end_code; uint32_t v_width; uint32_t v_code; v_bits = 0; v_n_bits = 0; v_clear_code = (((uint32_t)(1)) << self->f_literal_width); + v_end_code = (v_clear_code + 1); v_width = (self->f_literal_width + 1); label_0_continue:; while (1) { @@ -234,7 +236,11 @@ label_0_continue:; if (v_code < v_clear_code) { goto label_0_break; } else if (v_code == v_clear_code) { + v_width = (self->f_literal_width + 1); goto label_0_continue; + } else if (v_code == v_end_code) { + status = puffs_gif_status_ok; + goto cleanup0; } goto label_0_break; } diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 6a7e29b2..e0e5693c 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -8,8 +8,8 @@ pub struct lzw_decoder?( pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { var bits u32 var n_bits u32 - var clear_code u32 = (1 as u32) << this.literal_width - // TODO: set end_code. + var clear_code u32[4..256] = (1 as u32) << this.literal_width + var end_code u32[5..257] = clear_code + 1 var width u32[..12] = this.literal_width + 1 // TODO: implement. @@ -33,7 +33,10 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { if code < clear_code { break } else if code == clear_code { + width = this.literal_width + 1 continue + } else if code == end_code { + return } // TODO: implement.