diff --git a/cmd/wuffs-c/internal/cgen/base-impl.h b/cmd/wuffs-c/internal/cgen/base-impl.h index 37c1ecd6..5fab14b3 100644 --- a/cmd/wuffs-c/internal/cgen/base-impl.h +++ b/cmd/wuffs-c/internal/cgen/base-impl.h @@ -14,6 +14,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called. diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index a035b74e..6e37cc94 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -354,14 +354,6 @@ func (g *gen) genImpl(b *buffer) error { } } - // TODO: don't hard-code wuffs_gif__lzw_decoder__reset. - if g.pkgName == "gif" { - b.writes("static inline void wuffs_gif__lzw_decoder__reset(wuffs_gif__lzw_decoder *self) {") - b.writes("memset(self, 0, sizeof *self);") - b.writes("wuffs_gif__lzw_decoder__check_wuffs_version(self, sizeof *self, WUFFS_VERSION);") - b.writes("}\n\n") - } - b.writes("// ---------------- Private Function Prototypes\n\n") if err := g.forEachFunc(b, priOnly, (*gen).writeFuncPrototype); err != nil { return err diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 5b994f81..3a55df53 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -85,10 +85,10 @@ const baseHeader = "" + "" const baseImpl = "" + - "// Copyright 2017 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// Denote intentional fallthroughs for -Wimplic" + - "it-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status < 0) { \\\n goto exit; " + - " \\\n } else if (status == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// Uncomment this #include for printf-debugging.\n// #include \n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They are in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a" + - " design goal is that users of\n// the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple wuffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\n" + + "// Copyright 2017 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nstatic inline wuffs_base__empty_struct wuffs_base__return_empty_struct() {\n return ((wuffs_base__empty_struct){});\n}\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero memory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, base" + + "d on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_" + + "SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status < 0) { \\\n goto exit; \\\n } else if (status == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// Uncomment this #include for printf-debugging.\n// #include \n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluate more than once.\n//\n// They a" + + "re in base-impl.h and hence copy/pasted into every generated C file,\n// instead of being in some \"base.c\" file, since a design goal is that users of\n// the generated C code can often just #include a single .c file, such as\n// \"gif.c\", without having to additionally include or otherwise build and link\n// a \"base.c\" file.\n//\n// They are static, so that linking multiple wuffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\n" + "" + "// ---------------- Numeric Types\n\nstatic inline uint16_t wuffs_base__load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t wuffs_base__load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t wuffs_base__load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t wuffs_base__load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);\n}\n\nstatic inline uint64_t wuffs_base__load_u64be(uint8_t* p) {\n return ((uint64_t)(p[0]) << 56) | ((uint64_t)(p[1]) << 48) |\n ((uint64_t)(p[2]) << 40) | ((uint64_t)(p[3]) << 32) |\n ((uint64_t)(p[4]) << 24) | ((uint64_t)(p[5]) << 16) |\n ((uint64_t)(p[6]) << 8) | ((uint64_t)(p[7]) << 0);\n}\n\nstatic inline uint64_t wuffs_base__load_u64le(uint8_t* p) {" + "\n return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) |\n ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |\n ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) |\n ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56);\n}\n\n" + diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index a762a5b5..4fa82938 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -88,6 +88,43 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth u if err := g.writeBuiltinCall(b, n, rp, depth); err != errNoSuchBuiltin { return err } + + if n.LHS().Expr().Ident() == t.IDReset { + method := n.LHS().Expr() + recv := method.LHS().Expr() + recvTyp, addr := recv.MType(), "&" + if recvTyp.Decorator() == t.IDPtr { + recvTyp, addr = recvTyp.Inner(), "" + } + if recvTyp.Decorator() != 0 { + return fmt.Errorf("cannot generate reset method call %q for receiver type %q", + n.Str(g.tm), recv.MType().Str(g.tm)) + } + qid := recvTyp.QID() + + // Generate a three part expression using the comma operator: + // "(memset call, check_wuffs_version call, return_empty_struct + // call)". The final part is a function call (to a static inline + // function) instead of a struct literal, to avoid a "expression + // result unused" compiler error. + + b.printf("(memset(%s", addr) + // TODO: ensure that the recv expression is idempotent. + if err := g.writeExpr(b, recv, rp, depth); err != nil { + return err + } + b.printf(", 0, sizeof ((%s%s){})),", g.packagePrefix(qid), qid[1].Str(g.tm)) + + b.printf("%s%s__check_wuffs_version(%s", g.packagePrefix(qid), qid[1].Str(g.tm), addr) + if err := g.writeExpr(b, recv, rp, depth); err != nil { + return err + } + b.printf(", sizeof ((%s%s){}), WUFFS_VERSION)", g.packagePrefix(qid), qid[1].Str(g.tm)) + + b.writes(", wuffs_base__return_empty_struct())") + return nil + } + return g.writeExprUserDefinedCall(b, n, rp, depth) case t.IDOpenBracket: diff --git a/gen/c/std/adler32.c b/gen/c/std/adler32.c index 52ba9eca..d3f967f8 100644 --- a/gen/c/std/adler32.c +++ b/gen/c/std/adler32.c @@ -1254,6 +1254,10 @@ uint32_t wuffs_adler32__hasher__update(wuffs_adler32__hasher* self, // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called. diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index cde854a0..9eaa470d 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -1253,6 +1253,10 @@ uint32_t wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called. diff --git a/gen/c/std/deflate.c b/gen/c/std/deflate.c index 217c7331..91d3f113 100644 --- a/gen/c/std/deflate.c +++ b/gen/c/std/deflate.c @@ -1344,6 +1344,10 @@ wuffs_deflate__status wuffs_deflate__decoder__decode( // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called. diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 58dd998f..883d549b 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -1397,6 +1397,10 @@ wuffs_gif__status wuffs_gif__decoder__decode_up_to_id_part1( // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called. @@ -1958,12 +1962,6 @@ void wuffs_gif__lzw_decoder__check_wuffs_version(wuffs_gif__lzw_decoder* self, size_t sizeof_star_self, uint32_t wuffs_version); -static inline void wuffs_gif__lzw_decoder__reset(wuffs_gif__lzw_decoder* self) { - memset(self, 0, sizeof *self); - wuffs_gif__lzw_decoder__check_wuffs_version(self, sizeof *self, - WUFFS_VERSION); -} - // ---------------- Private Function Prototypes static wuffs_gif__status wuffs_gif__decoder__decode_header( @@ -3401,7 +3399,11 @@ static wuffs_gif__status wuffs_gif__decoder__decode_id_part1( } } if (self->private_impl.f_previous_lzw_decode_ended_abruptly) { - wuffs_gif__lzw_decoder__reset(&self->private_impl.f_lzw); + (memset(&self->private_impl.f_lzw, 0, sizeof((wuffs_gif__lzw_decoder){})), + wuffs_gif__lzw_decoder__check_wuffs_version( + &self->private_impl.f_lzw, sizeof((wuffs_gif__lzw_decoder){}), + WUFFS_VERSION), + wuffs_base__return_empty_struct()); } { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); diff --git a/gen/c/std/gzip.c b/gen/c/std/gzip.c index 0f3d471d..52ba7619 100644 --- a/gen/c/std/gzip.c +++ b/gen/c/std/gzip.c @@ -1556,6 +1556,10 @@ wuffs_gzip__status wuffs_gzip__decoder__decode(wuffs_gzip__decoder* self, // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called. diff --git a/gen/c/std/zlib.c b/gen/c/std/zlib.c index 3e5403fe..838ce050 100644 --- a/gen/c/std/zlib.c +++ b/gen/c/std/zlib.c @@ -1556,6 +1556,10 @@ wuffs_zlib__status wuffs_zlib__decoder__decode(wuffs_zlib__decoder* self, // See the License for the specific language governing permissions and // limitations under the License. +static inline wuffs_base__empty_struct wuffs_base__return_empty_struct() { + return ((wuffs_base__empty_struct){}); +} + #define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) // WUFFS_BASE__MAGIC is a magic number to check that initializers are called.