Add a slice.suffix method.

This commit is contained in:
Nigel Tao committed 2017-09-22 14:12:54 +10:00
1 parent 51f2b183a2
commit 2fb9972bb9
10 files changed
+176 -2

No files matched your search

+39
View File
@@ -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
+3 -1
View File
@@ -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 {
+14
View File
@@ -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('(')
+8
View File
@@ -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 {