mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 16:32:11 +01:00
Drop the "?" from "copy_from32?".
Also add a PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND macro. Also add a loop to decoder.decode, as the code generator was previously (incorrectly) not generating a coro_susp_point at the end of the function.
This commit is contained in:
1 parent
0e09aa8c10
commit
585ea2024d
10 files changed
+218
-134
No files matched your search
@@ -15,7 +15,15 @@
|
||||
// so that clang-format doesn't get confused by the unusual "case"s.
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \
|
||||
coro_susp_point = n; \
|
||||
case n:
|
||||
case n:;
|
||||
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
|
||||
if (status <= 0) { \
|
||||
goto exit; \
|
||||
} \
|
||||
coro_susp_point = n; \
|
||||
goto suspend; \
|
||||
case n:;
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
@@ -132,6 +140,27 @@ static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_from32(uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
uint8_t** ptr_rptr,
|
||||
uint8_t* rend,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
if (length > (wend - wptr)) {
|
||||
length = wend - wptr;
|
||||
}
|
||||
uint8_t* rptr = *ptr_rptr;
|
||||
if (length > (rend - rptr)) {
|
||||
length = rend - rptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, rptr, length);
|
||||
*ptr_wptr += length;
|
||||
*ptr_rptr += length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
|
||||
@@ -126,7 +126,7 @@ const maxTemp = 10000
|
||||
type status struct {
|
||||
name string
|
||||
msg string
|
||||
isError bool
|
||||
keyword t.ID
|
||||
}
|
||||
|
||||
type buffer []byte
|
||||
@@ -230,7 +230,7 @@ func (g *gen) genHeader(b *buffer) error {
|
||||
}
|
||||
for i, s := range g.statusList {
|
||||
code := pkgID << statusCodeNamespaceShift
|
||||
if s.isError {
|
||||
if s.keyword.Key() == t.KeyError {
|
||||
code |= 1 << 31
|
||||
}
|
||||
code |= uint32(i)
|
||||
@@ -445,14 +445,13 @@ func (g *gen) gatherStatuses(b *buffer, n *a.Status) error {
|
||||
return fmt.Errorf("bad status message %q", raw)
|
||||
}
|
||||
prefix := "SUSPENSION_"
|
||||
isError := n.Keyword().Key() == t.KeyError
|
||||
if isError {
|
||||
if n.Keyword().Key() == t.KeyError {
|
||||
prefix = "ERROR_"
|
||||
}
|
||||
s := status{
|
||||
name: strings.ToUpper(g.cName(prefix + msg)),
|
||||
msg: msg,
|
||||
isError: isError,
|
||||
keyword: n.Keyword(),
|
||||
}
|
||||
g.statusList = append(g.statusList, s)
|
||||
g.statusMap[n.Message()] = s
|
||||
|
||||
@@ -12,12 +12,13 @@ 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// 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_slice_u8_subslice_i(\n puffs_base_slice_u8 s,\n uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(\n puffs_base_slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((pu" +
|
||||
"ffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8\npuffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\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 zer" +
|
||||
"o (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\nstatic inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t l = end - ptr;\n if ((size_t)(length) > l) {\n length = l;\n } else {\n l = length;\n }\n // TODO: is manual unrolling actually helpful?\n for (; l >= 8; l -" +
|
||||
"= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; l; l--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
"#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#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status <= 0) { \\\n goto exit; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\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\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_slice_u8_subslice_i(\n puffs_base_slice_u8 s,\n uint64_t i) {\n if ((i <= SIZE_MAX) && (i <= s.le" +
|
||||
"n)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = s.len - i,\n });\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8 puffs_base_slice_u8_subslice_j(\n puffs_base_slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((puffs_base_slice_u8){});\n}\n\nstatic inline puffs_base_slice_u8\npuffs_base_slice_u8_subslice_ij(puffs_base_slice_u8 s, uint64_t i, uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((puffs_base_slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\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 pu" +
|
||||
"ffs_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\nstatic inline uint32_t puffs_base_writer1_copy_from32(uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n " +
|
||||
" uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n if (length > (wend - wptr)) {\n length = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (length > (rend - rptr)) {\n length = rend - rptr;\n }\n if (length > 0) {\n memmove(wptr, rptr, length);\n *ptr_wptr += length;\n *ptr_rptr += length;\n }\n return length;\n}\n\nstatic inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n size_t d = ptr - start;\n if ((d == 0) || (d < (size_t)(distance))) {\n return 0;\n }\n start = ptr - distance;\n size_t l = end - ptr;\n if ((size_t)(length) > l) {\n length = l;\n } else {\n l = length;\n }\n // TODO: is manual unrolling actually helpful?\n f" +
|
||||
"or (; l >= 8; l -= 8) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; l; l--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
|
||||
type template_args_short_read struct {
|
||||
|
||||
@@ -148,6 +148,17 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
b.writes(")")
|
||||
return nil
|
||||
}
|
||||
if isInDst(g.tm, n, t.KeyCopyFrom32, 2) {
|
||||
b.printf("puffs_base_writer1_copy_from32(&%swptr_dst, %swend_dst", bPrefix, bPrefix)
|
||||
// TODO: don't assume that the first argument is "in.src".
|
||||
b.printf(", &%srptr_src, %srend_src,", bPrefix, bPrefix)
|
||||
a := n.Args()[1].Arg().Value()
|
||||
if err := g.writeExpr(b, a, rp, parenthesesOptional, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writeb(')')
|
||||
return nil
|
||||
}
|
||||
if isInDst(g.tm, n, t.KeyCopyHistory32, 2) {
|
||||
b.printf("puffs_base_writer1_copy_history32(&%swptr_dst, %swstart_dst, %swend_dst",
|
||||
bPrefix, bPrefix, bPrefix)
|
||||
@@ -304,8 +315,15 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
}
|
||||
|
||||
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
// TODO.
|
||||
return nil
|
||||
switch n.ID0().Key() {
|
||||
case t.KeyXUnaryPlus:
|
||||
b.writeb('+')
|
||||
case t.KeyXUnaryMinus:
|
||||
b.writeb('-')
|
||||
case t.KeyXUnaryNot:
|
||||
b.writeb('!')
|
||||
}
|
||||
return g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth)
|
||||
}
|
||||
|
||||
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
|
||||
@@ -132,10 +132,12 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
return fmt.Errorf("no status code for %q", msg)
|
||||
}
|
||||
ret.name = strings.ToUpper(g.cName(z.String()))
|
||||
ret.keyword = z.Keyword
|
||||
}
|
||||
} else if retExpr = n.Value(); retExpr == nil {
|
||||
ret.name = g.PKGPREFIX + "STATUS_OK"
|
||||
}
|
||||
|
||||
if g.currFunk.suspendible {
|
||||
b.writes("status = ")
|
||||
if retExpr == nil {
|
||||
@@ -146,17 +148,23 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
return err
|
||||
}
|
||||
b.writes(";")
|
||||
if ret.isError {
|
||||
b.writes("goto exit;")
|
||||
} else {
|
||||
b.writes("goto suspend;")
|
||||
if retExpr == nil {
|
||||
switch ret.keyword.Key() {
|
||||
case 0:
|
||||
// TODO: set csp=0, then goto ok.
|
||||
// Also update the PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND macro.
|
||||
b.writes("goto suspend;")
|
||||
return nil
|
||||
case t.KeyError:
|
||||
b.writes("goto exit;")
|
||||
return nil
|
||||
case t.KeySuspension:
|
||||
// No-op.
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// TODO: consider the return values, especially if they involve
|
||||
// suspendible function calls.
|
||||
b.writes("return;\n")
|
||||
return g.writeCoroSuspPoint(b, true)
|
||||
}
|
||||
return nil
|
||||
return fmt.Errorf("TODO: return statements in non-suspendible functions")
|
||||
|
||||
case a.KVar:
|
||||
n := n.Var()
|
||||
@@ -239,14 +247,18 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
||||
}
|
||||
|
||||
func (g *gen) writeCoroSuspPoint(b *buffer) error {
|
||||
func (g *gen) writeCoroSuspPoint(b *buffer, maybeSuspend bool) error {
|
||||
const maxCoroSuspPoint = 0xFFFFFFFF
|
||||
g.currFunk.coroSuspPoint++
|
||||
if g.currFunk.coroSuspPoint == maxCoroSuspPoint {
|
||||
return fmt.Errorf("too many coroutine suspension points required")
|
||||
}
|
||||
|
||||
b.printf("PUFFS_COROUTINE_SUSPENSION_POINT(%d);\n", g.currFunk.coroSuspPoint)
|
||||
macro := ""
|
||||
if maybeSuspend {
|
||||
macro = "_MAYBE_SUSPEND"
|
||||
}
|
||||
b.printf("PUFFS_COROUTINE_SUSPENSION_POINT%s(%d);\n", macro, g.currFunk.coroSuspPoint)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -254,7 +266,7 @@ func (g *gen) writeSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
if !n.Suspendible() {
|
||||
return nil
|
||||
}
|
||||
if err := g.writeCoroSuspPoint(b); err != nil {
|
||||
if err := g.writeCoroSuspPoint(b, false); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.writeCallSuspendibles(b, n, depth)
|
||||
@@ -335,7 +347,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
b.writes(";\n")
|
||||
|
||||
// TODO: the CSP prior to this is probably unnecessary.
|
||||
if err := g.writeCoroSuspPoint(b); err != nil {
|
||||
if err := g.writeCoroSuspPoint(b, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -381,53 +393,6 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
}
|
||||
b.writes(";\n")
|
||||
|
||||
} else if isInDst(g.tm, n, t.KeyCopyFrom32, 2) {
|
||||
if g.currFunk.tempW > maxTemp {
|
||||
return fmt.Errorf("too many temporary variables required")
|
||||
}
|
||||
temp := g.currFunk.tempW
|
||||
g.currFunk.tempW++
|
||||
g.currFunk.tempR++
|
||||
|
||||
b.writes("{\n")
|
||||
|
||||
g.currFunk.usesScratch = true
|
||||
// TODO: don't hard-code [0], and allow recursive coroutines.
|
||||
scratchName := fmt.Sprintf("self->private_impl.%s%s[0].scratch",
|
||||
cPrefix, g.currFunk.astFunc.Name().String(g.tm))
|
||||
|
||||
b.printf("%s = ", scratchName)
|
||||
x := n.Args()[1].Arg().Value()
|
||||
if err := g.writeExpr(b, x, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(";\n")
|
||||
|
||||
if err := g.writeCoroSuspPoint(b); err != nil {
|
||||
return err
|
||||
}
|
||||
b.printf("size_t %s%d = %s;\n", tPrefix, temp, scratchName)
|
||||
|
||||
const wName = "dst"
|
||||
b.printf("if (%s%d > %swend_%s - %swptr_%s) {\n", tPrefix, temp, bPrefix, wName, bPrefix, wName)
|
||||
b.printf("%s%d = %swend_%s - %swptr_%s;\n", tPrefix, temp, bPrefix, wName, bPrefix, wName)
|
||||
b.printf("status = %sSUSPENSION_SHORT_WRITE;\n", g.PKGPREFIX)
|
||||
b.writes("}\n")
|
||||
|
||||
// TODO: don't assume that the first argument is "in.src".
|
||||
const rName = "src"
|
||||
b.printf("if (%s%d > %srend_%s - %srptr_%s) {\n", tPrefix, temp, bPrefix, rName, bPrefix, rName)
|
||||
b.printf("%s%d = %srend_%s - %srptr_%s;\n", tPrefix, temp, bPrefix, rName, bPrefix, rName)
|
||||
b.printf("status = %sSUSPENSION_SHORT_READ;\n", g.PKGPREFIX)
|
||||
b.writes("}\n")
|
||||
|
||||
b.printf("memmove(%swptr_%s, %srptr_%s, %s%d);\n", bPrefix, wName, bPrefix, rName, tPrefix, temp)
|
||||
b.printf("%swptr_%s += %s%d;\n", bPrefix, wName, tPrefix, temp)
|
||||
b.printf("%srptr_%s += %s%d;\n", bPrefix, rName, tPrefix, temp)
|
||||
b.printf("if (status) { %s -= %s%d; goto suspend; }\n", scratchName, tPrefix, temp)
|
||||
|
||||
b.writes("}\n")
|
||||
|
||||
} else if isThisMethod(g.tm, n, "decode_header", 1) {
|
||||
b.printf("status = %s%s_decode_header(self, %ssrc);\n",
|
||||
g.pkgPrefix, g.currFunk.astFunc.Receiver().String(g.tm), aPrefix)
|
||||
@@ -586,7 +551,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, name string, size uint32, endia
|
||||
b.printf("%srptr_src += %d;\n", bPrefix, size/8)
|
||||
b.printf("} else {")
|
||||
b.printf("%s = 0;\n", scratchName)
|
||||
if err := g.writeCoroSuspPoint(b); err != nil {
|
||||
if err := g.writeCoroSuspPoint(b, false); err != nil {
|
||||
return err
|
||||
}
|
||||
b.printf("while (true) {")
|
||||
@@ -650,7 +615,9 @@ func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
}
|
||||
|
||||
func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
callSuspendible := methodName != t.KeyCopyHistory32 && methodName != t.KeySlice
|
||||
callSuspendible := methodName != t.KeyCopyFrom32 &&
|
||||
methodName != t.KeyCopyHistory32 &&
|
||||
methodName != t.KeySlice
|
||||
// TODO: check that n.Args() is "(x:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
|
||||
return false
|
||||
|
||||
+75
-48
@@ -183,6 +183,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint32_t v_length;
|
||||
uint32_t v_n;
|
||||
uint64_t scratch;
|
||||
} c_decode_uncompressed[1];
|
||||
struct {
|
||||
@@ -298,7 +299,15 @@ puffs_flate_status puffs_flate_zlib_decoder_decode(
|
||||
// so that clang-format doesn't get confused by the unusual "case"s.
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \
|
||||
coro_susp_point = n; \
|
||||
case n:
|
||||
case n:;
|
||||
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
|
||||
if (status <= 0) { \
|
||||
goto exit; \
|
||||
} \
|
||||
coro_susp_point = n; \
|
||||
goto suspend; \
|
||||
case n:;
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
@@ -415,6 +424,27 @@ static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_from32(uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
uint8_t** ptr_rptr,
|
||||
uint8_t* rend,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
if (length > (wend - wptr)) {
|
||||
length = wend - wptr;
|
||||
}
|
||||
uint8_t* rptr = *ptr_rptr;
|
||||
if (length > (rend - rptr)) {
|
||||
length = rend - rptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, rptr, length);
|
||||
*ptr_wptr += length;
|
||||
*ptr_rptr += length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
@@ -680,32 +710,36 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
switch (coro_susp_point) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
||||
if (a_dst.buf) {
|
||||
size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi);
|
||||
a_dst.buf->wi += n;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_dst.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
while (true) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(1);
|
||||
if (a_dst.buf) {
|
||||
size_t n = b_wptr_dst - (a_dst.buf->ptr + a_dst.buf->wi);
|
||||
a_dst.buf->wi += n;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_dst.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len) {
|
||||
*lim->ptr_to_len -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
puffs_flate_status t_0 =
|
||||
puffs_flate_decoder_decode_blocks(self, a_dst, a_src);
|
||||
if (a_dst.buf) {
|
||||
b_wptr_dst = a_dst.buf->ptr + a_dst.buf->wi;
|
||||
size_t len = a_dst.buf->len - a_dst.buf->wi;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_dst.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
puffs_flate_status t_0 =
|
||||
puffs_flate_decoder_decode_blocks(self, a_dst, a_src);
|
||||
if (a_dst.buf) {
|
||||
b_wptr_dst = a_dst.buf->ptr + a_dst.buf->wi;
|
||||
size_t len = a_dst.buf->len - a_dst.buf->wi;
|
||||
puffs_base_limit1* lim;
|
||||
for (lim = &a_dst.limit; lim; lim = lim->next) {
|
||||
if (lim->ptr_to_len && (len > *lim->ptr_to_len)) {
|
||||
len = *lim->ptr_to_len;
|
||||
}
|
||||
}
|
||||
b_wend_dst = b_wptr_dst + len;
|
||||
}
|
||||
v_z = t_0;
|
||||
if (!(v_z > 0)) {
|
||||
status = v_z;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2);
|
||||
}
|
||||
b_wend_dst = b_wptr_dst + len;
|
||||
}
|
||||
v_z = t_0;
|
||||
if (v_z > 0) {
|
||||
v_written = ((puffs_base_slice_u8){
|
||||
.ptr = b_wstart_dst,
|
||||
.len = b_wptr_dst - b_wstart_dst,
|
||||
@@ -742,9 +776,9 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self,
|
||||
((uint32_t)((v_n & 32767))) + v_already_full);
|
||||
}
|
||||
}
|
||||
status = v_z;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
|
||||
}
|
||||
status = v_z;
|
||||
goto suspend;
|
||||
self->private_impl.c_decode[0].coro_susp_point = 0;
|
||||
goto exit;
|
||||
}
|
||||
@@ -961,6 +995,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
puffs_flate_status status = PUFFS_FLATE_STATUS_OK;
|
||||
|
||||
uint32_t v_length;
|
||||
uint32_t v_n;
|
||||
|
||||
uint8_t* b_wptr_dst = NULL;
|
||||
uint8_t* b_wstart_dst = NULL;
|
||||
@@ -997,6 +1032,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
self->private_impl.c_decode_uncompressed[0].coro_susp_point;
|
||||
if (coro_susp_point) {
|
||||
v_length = self->private_impl.c_decode_uncompressed[0].v_length;
|
||||
v_n = self->private_impl.c_decode_uncompressed[0].v_n;
|
||||
}
|
||||
switch (coro_susp_point) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(0);
|
||||
@@ -1040,29 +1076,19 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
goto exit;
|
||||
}
|
||||
v_length = ((v_length) & ((1 << (16)) - 1));
|
||||
while (v_length > 0) {
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(3);
|
||||
{
|
||||
self->private_impl.c_decode_uncompressed[0].scratch = v_length;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT(4);
|
||||
size_t t_2 = self->private_impl.c_decode_uncompressed[0].scratch;
|
||||
if (t_2 > b_wend_dst - b_wptr_dst) {
|
||||
t_2 = b_wend_dst - b_wptr_dst;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
}
|
||||
if (t_2 > b_rend_src - b_rptr_src) {
|
||||
t_2 = b_rend_src - b_rptr_src;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_READ;
|
||||
}
|
||||
memmove(b_wptr_dst, b_rptr_src, t_2);
|
||||
b_wptr_dst += t_2;
|
||||
b_rptr_src += t_2;
|
||||
if (status) {
|
||||
self->private_impl.c_decode_uncompressed[0].scratch -= t_2;
|
||||
goto suspend;
|
||||
}
|
||||
while (true) {
|
||||
v_n = puffs_base_writer1_copy_from32(&b_wptr_dst, b_wend_dst, &b_rptr_src,
|
||||
b_rend_src, v_length);
|
||||
if (v_length == v_n) {
|
||||
goto label_0_break;
|
||||
} else if (v_length < v_n) {
|
||||
status =
|
||||
PUFFS_FLATE_ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE;
|
||||
goto exit;
|
||||
}
|
||||
goto label_0_break;
|
||||
v_length -= v_n;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_READ;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(3);
|
||||
}
|
||||
label_0_break:;
|
||||
self->private_impl.c_decode_uncompressed[0].coro_susp_point = 0;
|
||||
@@ -1073,6 +1099,7 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed(
|
||||
suspend:
|
||||
self->private_impl.c_decode_uncompressed[0].coro_susp_point = coro_susp_point;
|
||||
self->private_impl.c_decode_uncompressed[0].v_length = v_length;
|
||||
self->private_impl.c_decode_uncompressed[0].v_n = v_n;
|
||||
|
||||
exit:
|
||||
if (a_dst.buf) {
|
||||
@@ -1371,7 +1398,7 @@ puffs_flate_status puffs_flate_decoder_decode_huffman(
|
||||
}
|
||||
v_length -= v_n;
|
||||
status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE;
|
||||
goto suspend;
|
||||
PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(8);
|
||||
}
|
||||
label_5_break:;
|
||||
}
|
||||
|
||||
+30
-1
@@ -268,7 +268,15 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
||||
// so that clang-format doesn't get confused by the unusual "case"s.
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \
|
||||
coro_susp_point = n; \
|
||||
case n:
|
||||
case n:;
|
||||
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
|
||||
if (status <= 0) { \
|
||||
goto exit; \
|
||||
} \
|
||||
coro_susp_point = n; \
|
||||
goto suspend; \
|
||||
case n:;
|
||||
|
||||
// Clang also defines "__GNUC__".
|
||||
#if defined(__GNUC__)
|
||||
@@ -385,6 +393,27 @@ static inline uint64_t puffs_base_slice_u8_copy_from(puffs_base_slice_u8 dst,
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_from32(uint8_t** ptr_wptr,
|
||||
uint8_t* wend,
|
||||
uint8_t** ptr_rptr,
|
||||
uint8_t* rend,
|
||||
uint32_t length) {
|
||||
uint8_t* wptr = *ptr_wptr;
|
||||
if (length > (wend - wptr)) {
|
||||
length = wend - wptr;
|
||||
}
|
||||
uint8_t* rptr = *ptr_rptr;
|
||||
if (length > (rend - rptr)) {
|
||||
length = rend - rptr;
|
||||
}
|
||||
if (length > 0) {
|
||||
memmove(wptr, rptr, length);
|
||||
*ptr_wptr += length;
|
||||
*ptr_rptr += length;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
static inline uint32_t puffs_base_writer1_copy_history32(uint8_t** ptr_ptr,
|
||||
uint8_t* start,
|
||||
uint8_t* end,
|
||||
|
||||
@@ -183,6 +183,7 @@ typedef struct {
|
||||
struct {
|
||||
uint32_t coro_susp_point;
|
||||
uint32_t v_length;
|
||||
uint32_t v_n;
|
||||
uint64_t scratch;
|
||||
} c_decode_uncompressed[1];
|
||||
struct {
|
||||
|
||||
+4
-2
@@ -388,7 +388,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(typeExprPlaceholder16) // HACK.
|
||||
} else if isInDst(q.tm, n, t.KeySlice, 0) {
|
||||
n.SetMType(typeExprSliceU8) // HACK.
|
||||
} else if isInDst(q.tm, n, t.KeyCopyHistory32, 2) {
|
||||
} else if isInDst(q.tm, n, t.KeyCopyFrom32, 2) || isInDst(q.tm, n, t.KeyCopyHistory32, 2) {
|
||||
n.SetMType(typeExprU32) // HACK.
|
||||
} else {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
@@ -603,7 +603,9 @@ func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
}
|
||||
|
||||
func isInDst(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
callSuspendible := methodName != t.KeyCopyHistory32 && methodName != t.KeySlice
|
||||
callSuspendible := methodName != t.KeyCopyFrom32 &&
|
||||
methodName != t.KeyCopyHistory32 &&
|
||||
methodName != t.KeySlice
|
||||
// TODO: check that n.Args() is "(x:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() != callSuspendible || len(n.Args()) != nArgs {
|
||||
return false
|
||||
|
||||
@@ -102,8 +102,12 @@ pub struct decoder?(
|
||||
)
|
||||
|
||||
pub func decoder.decode?(dst writer1, src reader1)() {
|
||||
var z status = try this.decode_blocks?(dst:in.dst, src:in.src)
|
||||
if z.is_suspension() {
|
||||
while true {
|
||||
var z status = try this.decode_blocks?(dst:in.dst, src:in.src)
|
||||
if not z.is_suspension() {
|
||||
// TODO: rename "return" to "raise"?
|
||||
return z
|
||||
}
|
||||
// TODO: should "slice" be "slice!", as the return value lets you
|
||||
// modify the state of in.dst, so future mutations (via the slice) can
|
||||
// change the veracity of any in.dst assertions?
|
||||
@@ -147,8 +151,8 @@ pub func decoder.decode?(dst writer1, src reader1)() {
|
||||
this.history_index = (this.history_index & 0x7FFF) + ((n & 0x7FFF) as u32) + already_full
|
||||
}
|
||||
}
|
||||
return z
|
||||
}
|
||||
return z
|
||||
}
|
||||
|
||||
pri func decoder.decode_blocks?(dst writer1, src reader1)() {
|
||||
@@ -192,10 +196,17 @@ pri func decoder.decode_uncompressed?(dst writer1, src reader1)() {
|
||||
return error "inconsistent stored block length"
|
||||
}
|
||||
length = length.low_bits(n:16)
|
||||
while length > 0 {
|
||||
// TODO: drop the "?" from "copy_from32?"?
|
||||
in.dst.copy_from32?(r:in.src, n:length)
|
||||
break
|
||||
while true {
|
||||
var n u32 = in.dst.copy_from32(r:in.src, length:length)
|
||||
if length == n {
|
||||
break
|
||||
} else if length < n {
|
||||
return error "internal error: inconsistent Huffman decoder state"
|
||||
}
|
||||
length -= n
|
||||
// TODO: distinguish between short and limited writes?
|
||||
// TODO: distinguish between reads and writes.
|
||||
return suspension "short read"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user