From 008bce65f2f3fe437ab6044572d5fe654021ee7b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 25 Mar 2018 17:04:14 +1100 Subject: [PATCH] Have crc32's ieee.update return u32 --- cmd/wuffs-c/internal/cgen/func.go | 16 ++++++++++++++-- gen/c/std/crc32.c | 16 ++++++++++------ gen/h/std/crc32.h | 6 ++++-- std/crc32/decode_crc32.wuffs | 8 +++++++- test/c/std/crc32.c | 10 +++++----- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/func.go b/cmd/wuffs-c/internal/cgen/func.go index d87e53c3..f0f76c08 100644 --- a/cmd/wuffs-c/internal/cgen/func.go +++ b/cmd/wuffs-c/internal/cgen/func.go @@ -171,11 +171,18 @@ func (g *gen) gatherFuncImpl(_ *buffer, n *a.Func) error { func (g *gen) writeFuncImplHeader(b *buffer) error { // Check the previous status and the "self" arg. if g.currFunk.public && !g.currFunk.astFunc.Receiver().IsZero() { + outFields := g.currFunk.astFunc.Out().Fields() + b.writes("if (!self) {") if g.currFunk.suspendible { b.printf("return %sERROR_BAD_RECEIVER;", g.PKGPREFIX) - } else { + } else if len(outFields) == 0 { b.printf("return;") + } else if len(outFields) == 1 { + // TODO: don't assume that the return type is an integer. + b.printf("return 0;") + } else { + return fmt.Errorf("TODO: handle structured return types") } b.writes("}") @@ -185,8 +192,13 @@ func (g *gen) writeFuncImplHeader(b *buffer) error { b.writes("if (self->private_impl.status < 0) {") if g.currFunk.suspendible { b.writes("return self->private_impl.status;") - } else { + } else if len(outFields) == 0 { b.writes("return;") + } else if len(outFields) == 1 { + // TODO: don't assume that the return type is an integer. + b.writes("return 0;") + } else { + return fmt.Errorf("TODO: handle structured return types") } b.writes("}\n") } diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index 8115c109..07e6a98c 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -231,6 +231,8 @@ typedef struct { wuffs_crc32__status status; uint32_t magic; + uint32_t f_state; + } private_impl; } wuffs_crc32__ieee; @@ -247,8 +249,8 @@ void wuffs_crc32__ieee__initialize(wuffs_crc32__ieee* self, // ---------------- Public Function Prototypes -void wuffs_crc32__ieee__update(wuffs_crc32__ieee* self, - wuffs_base__slice_u8 a_x); +uint32_t wuffs_crc32__ieee__update(wuffs_crc32__ieee* self, + wuffs_base__slice_u8 a_x); #ifdef __cplusplus } // extern "C" @@ -682,15 +684,17 @@ void wuffs_crc32__ieee__initialize(wuffs_crc32__ieee* self, // ---------------- Function Implementations -void wuffs_crc32__ieee__update(wuffs_crc32__ieee* self, - wuffs_base__slice_u8 a_x) { +uint32_t wuffs_crc32__ieee__update(wuffs_crc32__ieee* self, + wuffs_base__slice_u8 a_x) { if (!self) { - return; + return 0; } if (self->private_impl.magic != WUFFS_BASE__MAGIC) { self->private_impl.status = WUFFS_CRC32__ERROR_INITIALIZER_NOT_CALLED; } if (self->private_impl.status < 0) { - return; + return 0; } + + return self->private_impl.f_state; } diff --git a/gen/h/std/crc32.h b/gen/h/std/crc32.h index 6fefa7ad..7805e9f3 100644 --- a/gen/h/std/crc32.h +++ b/gen/h/std/crc32.h @@ -231,6 +231,8 @@ typedef struct { wuffs_crc32__status status; uint32_t magic; + uint32_t f_state; + } private_impl; } wuffs_crc32__ieee; @@ -247,8 +249,8 @@ void wuffs_crc32__ieee__initialize(wuffs_crc32__ieee* self, // ---------------- Public Function Prototypes -void wuffs_crc32__ieee__update(wuffs_crc32__ieee* self, - wuffs_base__slice_u8 a_x); +uint32_t wuffs_crc32__ieee__update(wuffs_crc32__ieee* self, + wuffs_base__slice_u8 a_x); #ifdef __cplusplus } // extern "C" diff --git a/std/crc32/decode_crc32.wuffs b/std/crc32/decode_crc32.wuffs index e3ee2e4a..20229bb8 100644 --- a/std/crc32/decode_crc32.wuffs +++ b/std/crc32/decode_crc32.wuffs @@ -16,9 +16,15 @@ packageid "crc3" // TODO: drop the '?' but still generate wuffs_crc32__ieee__initialize? pub struct ieee?( + state u32, ) // TODO: add a ! as this function is impure. -pub func ieee.update(x[] u8)() { +// +// TODO: should it be ? instead of ! because it's public, and might need a way +// to signal "initializer not called"? Should the return type, in the generated +// C code, be "struct{ uint32_t checksum; wuffs_crc32__status status }"? +pub func ieee.update(x[] u8)(checksum u32) { // TODO: implement. + return this.state } diff --git a/test/c/std/crc32.c b/test/c/std/crc32.c index 2f90b3e9..b826b815 100644 --- a/test/c/std/crc32.c +++ b/test/c/std/crc32.c @@ -74,11 +74,11 @@ const char* wuffs_bench_crc32(wuffs_base__buf1* dst, // TODO: don't ignore wlimit and rlimit. wuffs_crc32__ieee checksum; wuffs_crc32__ieee__initialize(&checksum, WUFFS_VERSION, 0); - // TODO: global_wuffs_crc32_unused_u32 = - wuffs_crc32__ieee__update(&checksum, ((wuffs_base__slice_u8){ - .ptr = src->ptr + src->ri, - .len = src->wi - src->ri, - })); + global_wuffs_crc32_unused_u32 = + wuffs_crc32__ieee__update(&checksum, ((wuffs_base__slice_u8){ + .ptr = src->ptr + src->ri, + .len = src->wi - src->ri, + })); src->ri = src->wi; return NULL; }