From 2fb9972bb90683731eb6ec304aa409dba8e58bd2 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 22 Sep 2017 14:12:54 +1000 Subject: [PATCH] Add a slice.suffix method. --- cmd/puffs-c/internal/cgen/base-impl.h | 39 +++++++++++++++++++++++++ cmd/puffs-c/internal/cgen/data.go | 4 ++- cmd/puffs-c/internal/cgen/expr.go | 14 +++++++++ cmd/puffs-c/internal/cgen/statement.go | 8 ++++++ gen/c/std/flate.c | 40 ++++++++++++++++++++++++++ gen/c/std/gif.c | 39 +++++++++++++++++++++++++ lang/check/bounds.go | 2 +- lang/check/type.go | 25 ++++++++++++++++ lang/token/list.go | 6 ++++ std/flate/decode_flate.puffs | 1 + 10 files changed, 176 insertions(+), 2 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index f676d43a..8cc7f4d0 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -40,4 +40,43 @@ extern int printf(const char* __restrict __format, ...); #define PUFFS_UNLIKELY(expr) (expr) #endif +// ---------------- Static Inline Functions +// +// The helpers below are functions, instead of macros, because their arguments +// can be an expression that we shouldn't evaluate more than once. +// +// They are in base-impl.h and hence copy/pasted into every generated C file, +// instead of being in some "base.c" file, since a design goal is that users of +// the generated C code can often just #include a single .c file, such as +// "gif.c", without having to additionally include or otherwise build and link +// a "base.c" file. +// +// They are static, so that linking multiple puffs .o files won't complain about +// duplicate function definitions. +// +// They are explicitly marked inline, even if modern compilers don't use the +// inline attribute to guide optimizations such as inlining, to avoid the +// -Wunused-function warning, and we like to compile with -Wall -Werror. + +// puffs_base_slice_u8_prefix returns up to the first n bytes of s. +static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( + puffs_base_slice_u8 s, + uint64_t n) { + if ((uint64_t)(s.len) > n) { + s.len = n; + } + return s; +} + +// puffs_base_slice_u8_suffix returns up to the last n bytes of s. +static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( + puffs_base_slice_u8 s, + uint64_t n) { + if ((uint64_t)(s.len) > n) { + s.ptr += (uint64_t)(s.len) - n; + s.len = n; + } + return s; +} + #endif // PUFFS_BASE_IMPL_H diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 7422bd43..dda60400 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -13,7 +13,9 @@ const baseHeader = "" + 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// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\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 in" + - "stead 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#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" + + "stead 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#define 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\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 retu" + + "rn s;\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 d3551df4..417f1990 100644 --- a/cmd/puffs-c/internal/cgen/expr.go +++ b/cmd/puffs-c/internal/cgen/expr.go @@ -134,6 +134,20 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare } return nil } + if isIsPrefixSuffix(g.tm, n, t.KeySuffix) { + // TODO: don't assume that the slice is a slice of u8. + b.writes("puffs_base_slice_u8_suffix(") + x := n.LHS().Expr().LHS().Expr() + if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil { + return err + } + b.writes(", ") + if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil { + return err + } + b.writes(")") + return nil + } if isInDst(g.tm, n, t.KeySlice, 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 aeb9b996..4dd3cfb8 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -760,6 +760,14 @@ func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool { return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName } +func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool { + if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { + return false + } + n = n.LHS().Expr() + return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName +} + func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool { // TODO: check that n.Args() is "(lw:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 35c692cd..a9a727a6 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -324,6 +324,45 @@ extern int printf(const char* __restrict __format, ...); #define PUFFS_UNLIKELY(expr) (expr) #endif +// ---------------- Static Inline Functions +// +// The helpers below are functions, instead of macros, because their arguments +// can be an expression that we shouldn't evaluate more than once. +// +// They are in base-impl.h and hence copy/pasted into every generated C file, +// instead of being in some "base.c" file, since a design goal is that users of +// the generated C code can often just #include a single .c file, such as +// "gif.c", without having to additionally include or otherwise build and link +// a "base.c" file. +// +// They are static, so that linking multiple puffs .o files won't complain about +// duplicate function definitions. +// +// They are explicitly marked inline, even if modern compilers don't use the +// inline attribute to guide optimizations such as inlining, to avoid the +// -Wunused-function warning, and we like to compile with -Wall -Werror. + +// puffs_base_slice_u8_prefix returns up to the first n bytes of s. +static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( + puffs_base_slice_u8 s, + uint64_t n) { + if ((uint64_t)(s.len) > n) { + s.len = n; + } + return s; +} + +// puffs_base_slice_u8_suffix returns up to the last n bytes of s. +static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( + puffs_base_slice_u8 s, + uint64_t n) { + if ((uint64_t)(s.len) > n) { + s.ptr += (uint64_t)(s.len) - n; + s.len = n; + } + return s; +} + #endif // PUFFS_BASE_IMPL_H // ---------------- Status Codes Implementations @@ -589,6 +628,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, v_written = ((puffs_base_slice_u8){.ptr = a_dst.buf->ptr, .len = b_wptr_dst - a_dst.buf->ptr}); if (((uint64_t)(v_written.len)) >= 32768) { + v_written = puffs_base_slice_u8_suffix(v_written, 32768); } } status = v_z; diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index 35fbbc35..64d6d0a0 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -297,6 +297,45 @@ extern int printf(const char* __restrict __format, ...); #define PUFFS_UNLIKELY(expr) (expr) #endif +// ---------------- Static Inline Functions +// +// The helpers below are functions, instead of macros, because their arguments +// can be an expression that we shouldn't evaluate more than once. +// +// They are in base-impl.h and hence copy/pasted into every generated C file, +// instead of being in some "base.c" file, since a design goal is that users of +// the generated C code can often just #include a single .c file, such as +// "gif.c", without having to additionally include or otherwise build and link +// a "base.c" file. +// +// They are static, so that linking multiple puffs .o files won't complain about +// duplicate function definitions. +// +// They are explicitly marked inline, even if modern compilers don't use the +// inline attribute to guide optimizations such as inlining, to avoid the +// -Wunused-function warning, and we like to compile with -Wall -Werror. + +// puffs_base_slice_u8_prefix returns up to the first n bytes of s. +static inline puffs_base_slice_u8 puffs_base_slice_u8_prefix( + puffs_base_slice_u8 s, + uint64_t n) { + if ((uint64_t)(s.len) > n) { + s.len = n; + } + return s; +} + +// puffs_base_slice_u8_suffix returns up to the last n bytes of s. +static inline puffs_base_slice_u8 puffs_base_slice_u8_suffix( + puffs_base_slice_u8 s, + uint64_t n) { + if ((uint64_t)(s.len) > n) { + s.ptr += (uint64_t)(s.len) - n; + s.len = n; + } + return s; +} + #endif // PUFFS_BASE_IMPL_H // ---------------- Status Codes Implementations diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 4f052088..d1346d78 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -689,7 +689,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, return zero, bitMask(int(aMax.Int64())), nil } // TODO: delete this hack that only matches "foo.is_suspension(etc)". - if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) { + if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) || isIsPrefixSuffix(q.tm, n, t.KeySuffix) { return nil, nil, nil } // TODO: delete this hack that only matches "foo.set_literal_width(etc)". diff --git a/lang/check/type.go b/lang/check/type.go index 578008ec..f2ed3358 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -410,6 +410,23 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.SetMType(typeExprBool) return nil } + // TODO: delete this hack that only matches "foo.suffix(etc)". + if isIsPrefixSuffix(q.tm, n, t.KeySuffix) { + foo := n.LHS().Expr().LHS().Expr() + if err := q.tcheckExpr(foo, depth); err != nil { + return err + } + n.LHS().SetTypeChecked() + n.LHS().Expr().SetMType(typeExprPlaceholder) // HACK. + for _, o := range n.Args() { + if err := q.tcheckArg(o.Arg(), depth); err != nil { + return err + } + } + // TODO: don't assume that the slice is a slice of u8. + n.SetMType(typeExprSliceU8) + return nil + } // TODO: delete this hack that only matches "foo.set_literal_width(etc)". if isSetLiteralWidth(q.tm, n) { foo := n.LHS().Expr().LHS().Expr() @@ -597,6 +614,14 @@ func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool { return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName } +func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool { + if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { + return false + } + n = n.LHS().Expr() + return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName +} + func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool { // TODO: check that n.Args() is "(lw:bar)". if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 { diff --git a/lang/token/list.go b/lang/token/list.go index a6b2fa50..309400fb 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -271,6 +271,8 @@ const ( KeyIsSuspension = Key(IDIsSuspension >> KeyShift) KeySlice = Key(IDSlice >> KeyShift) KeyLength = Key(IDLength >> KeyShift) + KeyPrefix = Key(IDPrefix >> KeyShift) + KeySuffix = Key(IDSuffix >> KeyShift) KeyXUnaryPlus = Key(IDXUnaryPlus >> KeyShift) KeyXUnaryMinus = Key(IDXUnaryMinus >> KeyShift) @@ -446,6 +448,8 @@ const ( IDIsSuspension = ID(0xC7<= (32 * 1024) { + written = written.suffix(up_to:32 * 1024) } } return z