From e49ec3fd909596f060ffd27ec1565560364e46db Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 4 Jun 2017 11:48:11 +1000 Subject: [PATCH] Let a short_src be either an error or a status. Error-ness depends on whether there's possibly more source data. Also rename short_dst and short_src to short_write and short_read, as the new names don't depend on the function arguments being named exactly dst and src. --- cmd/puffs-gen-c/main.go | 43 ++++++++++++++++++++++------------------ gen/c/gif.c | 14 ++++++------- gen/h/gif.h | 6 +++--- std/gif/decode_lzw.puffs | 2 +- test/c/gif/basic.c | 10 +++++----- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 5de18622..88b1374f 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -56,20 +56,20 @@ func main() { } var builtInStatuses = [...]string{ - // For API/ABI compatibility, the very first two status values must be - // status_ok (with generated value 0) and error_bad_version (with generated + // For API/ABI compatibility, the very first two statuses must be "status + // ok" (with generated value 0) and "error bad version" (with generated // value -2 + 1). This lets caller code check the constructor return value - // for error_bad_version even if the caller and callee were built with + // for "error bad version" even if the caller and callee were built with // different versions. - "status_ok", - "error_bad_version", - // The order of the remaining status values is less important. - "error_bad_receiver", - "error_bad_argument", - "error_constructor_not_called", - "status_short_dst", - "status_short_src", - "status_short_buffer", + "status ok", + "error bad version", + // The order of the remaining statuses is less important. + "error bad receiver", + "error bad argument", + "error constructor not called", + "error unexpected EOF", // Used if read foo passed final == true. + "status short read", // Used if read foo passed final == false. + "status short write", } type replacementPolicy bool @@ -138,9 +138,11 @@ func (g *gen) generate() error { g.writes("typedef enum {\n") for i, s := range builtInStatuses { nudge := "" - if strings.HasPrefix(s, "error_") { + if strings.HasPrefix(s, "error ") { nudge = "+1" } + s = strings.Replace(s, " ", "_", -1) + s = strings.ToLower(s) g.printf("puffs_%s_%s = %d%s,\n", g.pkgName, s, -2*i, nudge) } g.printf("} puffs_%s_status;\n\n", g.pkgName) @@ -174,12 +176,12 @@ func (g *gen) generate() error { "return s & 1; }\n\n", g.pkgName, g.pkgName) g.printf("const char* puffs_%s_status_strings[%d] = {\n", g.pkgName, len(builtInStatuses)) for _, s := range builtInStatuses { - if strings.HasPrefix(s, "status_") { - s = s[len("status_"):] - } else if strings.HasPrefix(s, "error_") { - s = s[len("error_"):] + if strings.HasPrefix(s, "status ") { + s = s[len("status "):] + } else if strings.HasPrefix(s, "error ") { + s = s[len("error "):] } - s = g.pkgName + ": " + strings.Replace(s, "_", " ", -1) + s = g.pkgName + ": " + s g.printf("%q,", s) } g.writes("};\n\n") @@ -695,7 +697,10 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error { // TODO: delete this hack that only matches "in.src.read_u8?()". if isInSrcReadU8(g.idMap, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 { // TODO: suspend coroutine state. - g.printf("if (%ssrc->ri >= %ssrc->wi) { status = puffs_%s_status_short_src;", + // + // TODO: choose error_unexpected_eof versus status_short_read based on + // src_final. + g.printf("if (%ssrc->ri >= %ssrc->wi) { status = puffs_%s_status_short_read;", aPrefix, aPrefix, g.pkgName) if g.perFunc.cleanup0 { g.writes("goto cleanup0;") diff --git a/gen/c/gif.c b/gen/c/gif.c index 2ece0029..b7582877 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -60,9 +60,9 @@ typedef enum { puffs_gif_error_bad_receiver = -4 + 1, puffs_gif_error_bad_argument = -6 + 1, puffs_gif_error_constructor_not_called = -8 + 1, - puffs_gif_status_short_dst = -10, - puffs_gif_status_short_src = -12, - puffs_gif_status_short_buffer = -14, + puffs_gif_error_unexpected_eof = -10 + 1, + puffs_gif_status_short_read = -12, + puffs_gif_status_short_write = -14, } puffs_gif_status; bool puffs_gif_status_is_error(puffs_gif_status s); @@ -127,9 +127,9 @@ const char* puffs_gif_status_strings[8] = { "gif: bad receiver", "gif: bad argument", "gif: constructor not called", - "gif: short dst", - "gif: short src", - "gif: short buffer", + "gif: unexpected EOF", + "gif: short read", + "gif: short write", }; const char* puffs_gif_status_string(puffs_gif_status s) { @@ -217,7 +217,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, while (1) { while ((v_n_bits < v_width)) { if (a_src->ri >= a_src->wi) { - status = puffs_gif_status_short_src; + status = puffs_gif_status_short_read; goto cleanup0; } uint8_t t_0 = a_src->ptr[a_src->ri++]; diff --git a/gen/h/gif.h b/gen/h/gif.h index fde99da2..58607698 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -60,9 +60,9 @@ typedef enum { puffs_gif_error_bad_receiver = -4 + 1, puffs_gif_error_bad_argument = -6 + 1, puffs_gif_error_constructor_not_called = -8 + 1, - puffs_gif_status_short_dst = -10, - puffs_gif_status_short_src = -12, - puffs_gif_status_short_buffer = -14, + puffs_gif_error_unexpected_eof = -10 + 1, + puffs_gif_status_short_read = -12, + puffs_gif_status_short_write = -14, } puffs_gif_status; bool puffs_gif_status_is_error(puffs_gif_status s); diff --git a/std/gif/decode_lzw.puffs b/std/gif/decode_lzw.puffs index 4fc5a39c..365625ba 100644 --- a/std/gif/decode_lzw.puffs +++ b/std/gif/decode_lzw.puffs @@ -24,7 +24,7 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() { post n_bits >= width, { assert n_bits < 12 via "a < b: a < c; c <= b"(c:width) - bits |= in.src.read_u8?() as u32 // TODO + bits |= in.src.read_u8?() as u32 // TODO: read_u8 should take src_final. n_bits += 8 } // TODO: var code u32[..4095] = bits.low_bits(width) diff --git a/test/c/gif/basic.c b/test/c/gif/basic.c index 0429935c..17311af5 100644 --- a/test/c/gif/basic.c +++ b/test/c/gif/basic.c @@ -89,8 +89,8 @@ void test_status_is_error() { FAIL("test_status_is_error: is_error(bad_version) returned false"); return; } - if (puffs_gif_status_is_error(puffs_gif_status_short_dst)) { - FAIL("test_status_is_error: is_error(short_dst) returned true"); + if (puffs_gif_status_is_error(puffs_gif_status_short_write)) { + FAIL("test_status_is_error: is_error(short_write) returned true"); return; } } @@ -106,9 +106,9 @@ void test_status_strings() { FAIL("test_status_strings: got \"%s\", want \"gif: bad version\"", s1); return; } - const char* s2 = puffs_gif_status_string(puffs_gif_status_short_dst); - if (strcmp(s2, "gif: short dst")) { - FAIL("test_status_strings: got \"%s\", want \"gif: short dst\"", s2); + const char* s2 = puffs_gif_status_string(puffs_gif_status_short_write); + if (strcmp(s2, "gif: short write")) { + FAIL("test_status_strings: got \"%s\", want \"gif: short write\"", s2); return; } }