From f1643be2216e3afa1f2d0dc0059297e01405a2a0 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 28 Sep 2017 17:27:34 +1000 Subject: [PATCH] Have copy_from return the number of bytes copied. --- cmd/puffs-c/internal/cgen/base-impl.h | 5 +++-- cmd/puffs-c/internal/cgen/data.go | 4 ++-- cmd/puffs-c/internal/cgen/expr.go | 14 ++++++++++++++ cmd/puffs-c/internal/cgen/statement.go | 19 ++++--------------- gen/c/std/flate.c | 11 ++++++++--- gen/c/std/gif.c | 5 +++-- gen/h/std/flate.h | 1 + lang/check/type.go | 5 ++--- std/flate/decode_flate.puffs | 2 +- 9 files changed, 38 insertions(+), 28 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 84575f41..3a6bd501 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -119,12 +119,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( // // Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty // slice) is valid and results in a no-op. -static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, - puffs_base_slice_u8 src) { +static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, + puffs_base_slice_u8 src) { size_t n = dst.len < src.len ? dst.len : src.len; if (n > 0) { memmove(dst.ptr, src.ptr, n); } + return n; } #endif // PUFFS_BASE_IMPL_H diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 8eaa6d16..84eb65b5 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -15,8 +15,8 @@ const baseImpl = "" + "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\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 a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#de" + "fine PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n// ---------------- Static Inline Functions\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 puffs .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\nstatic inline uint16_t puffs_base_load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1" + "]) << 0);\n}\n\nstatic inline uint16_t puffs_base_load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t puffs_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 puffs_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 puffs_base_slice_u8 puffs_base_make_slice_u8_from_ints(\n uint8_t* start,\n uint64_t i,\n uint64_t j,\n uint64_t k) {\n if ((i <= j) && (j <= k) && (k <= SIZE_MAX)) {\n return ((puffs_base_slice_u8){\n .ptr = start + i,\n .len = j - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_make_slice_u8_from_ptrs(\n uint8_t* start,\n uint8_t* mark0,\n uint8_t* mark1,\n uint8_t* end) {\n if ((start <= mark0) && (mark0 <= mark1) && " + - "(mark1 <= end)) {\n return ((puffs_base_slice_u8){\n .ptr = mark0,\n .len = mark1 - mark0,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is\n// the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n " + - " puffs_base_slice_u8 src) {\n size_t n = dst.len < src.len ? dst.len : src.len;\n if (n > 0) {\n memmove(dst.ptr, src.ptr, n);\n }\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + + "(mark1 <= end)) {\n return ((puffs_base_slice_u8){\n .ptr = mark0,\n .len = mark1 - mark0,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\n// puffs_base_slice_u8_prefix returns up to the first n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_prefix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_suffix returns up to the last n bytes of s.\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_suffix(\n puffs_base_slice_u8 s,\n uint64_t n) {\n if ((uint64_t)(s.len) > n) {\n s.ptr += (uint64_t)(s.len) - n;\n s.len = n;\n }\n return s;\n}\n\n// puffs_base_slice_u8_copy_from calls memmove(dst.ptr, src.ptr, n) where n is\n// the minimum of dst.len and src.len.\n//\n// Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,\n " + + " puffs_base_slice_u8 src) {\n size_t n = dst.len < src.len ? dst.len : src.len;\n if (n > 0) {\n memmove(dst.ptr, src.ptr, n);\n }\n return n;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" type template_args_short_read struct { diff --git a/cmd/puffs-c/internal/cgen/expr.go b/cmd/puffs-c/internal/cgen/expr.go index 58213456..1f15d409 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -169,6 +169,20 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare b.printf("%sdst.buf ? %swptr_dst : NULL)", aPrefix, bPrefix) return nil } + if isThatMethod(g.tm, n, t.KeyCopyFrom, 1) { + b.writes("puffs_base_slice_u8_copy_from(") + receiver := n.LHS().Expr().LHS().Expr() + if err := g.writeExpr(b, receiver, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } + b.writeb(',') + a := n.Args()[0].Arg().Value() + if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err + } + b.writes(")\n") + return nil + } if isThatMethod(g.tm, n, t.KeyLength, 0) { if pp == parenthesesMandatory { b.writeb('(') diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index 03fd8147..b1597f87 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -58,22 +58,11 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { b.writes(");\n") return nil } - // TODO: delete this hack that only matches "foo.copy_from(etc)". - if isThatMethod(g.tm, n, t.KeyCopyFrom, 1) { - b.writes("puffs_base_slice_u8_copy_from(") - receiver := n.LHS().Expr().LHS().Expr() - if err := g.writeExpr(b, receiver, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { - return err - } - b.writeb(',') - a := n.Args()[0].Arg().Value() - if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { - return err - } - b.writes(");\n") - return nil + if err := g.writeExpr(b, n, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { + return err } - return fmt.Errorf("TODO: generate code for foo() when foo is not a ? call-suspendible") + b.writes(";\n") + return nil case a.KIf: // TODO: for writeSuspendibles, make sure that we get order of diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 1f2f466e..80187d7f 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -178,6 +178,7 @@ typedef struct { struct { uint32_t coro_susp_point; puffs_flate_status v_z; + uint64_t v_n; } c_decode[1]; struct { uint32_t coro_susp_point; @@ -410,12 +411,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( // // Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty // slice) is valid and results in a no-op. -static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, - puffs_base_slice_u8 src) { +static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, + puffs_base_slice_u8 src) { size_t n = dst.len < src.len ? dst.len : src.len; if (n > 0) { memmove(dst.ptr, src.ptr, n); } + return n; } #endif // PUFFS_BASE_IMPL_H @@ -632,6 +634,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, puffs_flate_status v_z; puffs_base_buf1_mark v_m1; puffs_base_slice_u8 v_written; + uint64_t v_n; uint8_t* b_wptr_dst = NULL; uint8_t* b_wend_dst = NULL; @@ -653,6 +656,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, v_z = self->private_impl.c_decode[0].v_z; v_m1 = ((puffs_base_buf1_mark){}); v_written = ((puffs_base_slice_u8){}); + v_n = self->private_impl.c_decode[0].v_n; } switch (coro_susp_point) { PUFFS_COROUTINE_SUSPENSION_POINT(0); @@ -697,7 +701,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, v_written); self->private_impl.f_history_index = 32768; } else { - puffs_base_slice_u8_copy_from( + v_n = puffs_base_slice_u8_copy_from( puffs_base_make_slice_u8_from_ints( self->private_impl.f_history, self->private_impl.f_history_index & 32767, 32768, 32768), @@ -714,6 +718,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, suspend: self->private_impl.c_decode[0].coro_susp_point = coro_susp_point; self->private_impl.c_decode[0].v_z = v_z; + self->private_impl.c_decode[0].v_n = v_n; exit: if (a_dst.buf) { diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index c506729b..a1a70919 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -382,12 +382,13 @@ static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( // // Passing a puffs_base_slice_u8 with all fields NULL or zero (a valid, empty // slice) is valid and results in a no-op. -static inline void puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, - puffs_base_slice_u8 src) { +static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst, + puffs_base_slice_u8 src) { size_t n = dst.len < src.len ? dst.len : src.len; if (n > 0) { memmove(dst.ptr, src.ptr, n); } + return n; } #endif // PUFFS_BASE_IMPL_H diff --git a/gen/h/std/flate.h b/gen/h/std/flate.h index addb32c2..1b623550 100644 --- a/gen/h/std/flate.h +++ b/gen/h/std/flate.h @@ -178,6 +178,7 @@ typedef struct { struct { uint32_t coro_susp_point; puffs_flate_status v_z; + uint64_t v_n; } c_decode[1]; struct { uint32_t coro_susp_point; diff --git a/lang/check/type.go b/lang/check/type.go index f24b60f1..ee0a35bf 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -436,8 +436,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { } // TODO: delete this hack that only matches "foo.set_literal_width(etc)". if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) || - isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) || - isThatMethod(q.tm, n, t.KeyCopyFrom, 1) { + isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) { foo := n.LHS().Expr().LHS().Expr() if err := q.tcheckExpr(foo, depth); err != nil { return err @@ -453,7 +452,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return nil } // TODO: delete this hack that only matches "foo.length(etc)". - if isThatMethod(q.tm, n, t.KeyLength, 0) { + if isThatMethod(q.tm, n, t.KeyCopyFrom, 1) || isThatMethod(q.tm, n, t.KeyLength, 0) { foo := n.LHS().Expr().LHS().Expr() if err := q.tcheckExpr(foo, depth); err != nil { return err diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index f253d574..5b7474ab 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -118,7 +118,7 @@ pub func decoder.decode?(dst writer1, src reader1)() { this.history_index = 32 * 1024 } else { // TODO: fill part of the history. - this.history[this.history_index & 0x7FFF:].copy_from(s:written) + var n u64 = this.history[this.history_index & 0x7FFF:].copy_from(s:written) } } return z