mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Use "coroutine suspension point" consistently.
This commit is contained in:
1 parent
d83cf7f3d5
commit
e7873b5ca4
8 files changed
+136
-130
No files matched your search
@@ -6,13 +6,13 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be found
|
||||
// in the LICENSE file.
|
||||
|
||||
// Use switch cases for coroutine state, similar to the technique in
|
||||
// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
||||
// Use switch cases for coroutine suspension points, similar to the technique
|
||||
// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
||||
//
|
||||
// We use a trivial macro instead of an explicit assignment and case statement
|
||||
// so that clang-format doesn't get confused by the unusual "case"s.
|
||||
#define PUFFS_COROUTINE_STATE(n) \
|
||||
coro_state = n; \
|
||||
#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \
|
||||
coro_susp_point = n; \
|
||||
case n:
|
||||
|
||||
#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))
|
||||
|
||||
@@ -12,5 +12,5 @@ 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// Use switch cases for coroutine state, similar to the technique in\n// 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_STATE(n) \\\n coro_state = n; \\\n case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\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#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\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// 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_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\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#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" +
|
||||
""
|
||||
+27
-24
@@ -13,16 +13,16 @@ import (
|
||||
)
|
||||
|
||||
type perFunc struct {
|
||||
funk *a.Func
|
||||
derivedVars map[t.ID]struct{}
|
||||
jumpTargets map[*a.While]uint32
|
||||
coroState uint32
|
||||
tempW uint32
|
||||
tempR uint32
|
||||
public bool
|
||||
suspendible bool
|
||||
limitVarName string
|
||||
shortReads []string
|
||||
funk *a.Func
|
||||
derivedVars map[t.ID]struct{}
|
||||
jumpTargets map[*a.While]uint32
|
||||
coroSuspPoint uint32
|
||||
tempW uint32
|
||||
tempR uint32
|
||||
public bool
|
||||
suspendible bool
|
||||
limitVarName string
|
||||
shortReads []string
|
||||
}
|
||||
|
||||
func (g *gen) writeFuncSignature(n *a.Func) error {
|
||||
@@ -129,8 +129,9 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
|
||||
g.writes("\n")
|
||||
|
||||
// TODO: don't hard-code [0], and allow recursive coroutines.
|
||||
g.printf("uint32_t coro_state = self->private_impl.%s%s[0].coro_state;\n", cPrefix, n.Name().String(g.tm))
|
||||
g.printf("if (coro_state) {\n")
|
||||
g.printf("uint32_t coro_susp_point = self->private_impl.%s%s[0].coro_susp_point;\n",
|
||||
cPrefix, n.Name().String(g.tm))
|
||||
g.printf("if (coro_susp_point) {\n")
|
||||
if err := g.writeResumeSuspend(n.Body(), false); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -139,7 +140,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
|
||||
// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
||||
//
|
||||
// The matching } is written below. See "Close the coroutine switch".
|
||||
g.writes("switch (coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n")
|
||||
g.writes("switch (coro_susp_point) {\nPUFFS_COROUTINE_SUSPENSION_POINT(0);\n\n")
|
||||
}
|
||||
|
||||
// Generate the function body.
|
||||
@@ -151,13 +152,15 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
|
||||
|
||||
if g.perFunc.suspendible {
|
||||
// We've reached the end of the function body. Reset the coroutine
|
||||
// state so that the next call to this function starts at the top.
|
||||
g.writes("coro_state = 0;\n")
|
||||
// suspension point so that the next call to this function starts at
|
||||
// the top.
|
||||
g.writes("coro_susp_point = 0;\n")
|
||||
g.writes("}\n\n") // Close the coroutine switch.
|
||||
|
||||
g.writes("goto suspend;\n") // Avoid the "unused label" warning.
|
||||
g.writes("suspend:\n")
|
||||
g.printf("self->private_impl.%s%s[0].coro_state = coro_state;\n", cPrefix, n.Name().String(g.tm))
|
||||
g.printf("self->private_impl.%s%s[0].coro_susp_point = coro_susp_point;\n",
|
||||
cPrefix, n.Name().String(g.tm))
|
||||
if err := g.writeResumeSuspend(n.Body(), true); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -726,14 +729,14 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
||||
return fmt.Errorf("unrecognized ast.Kind (%s) for writeStatement", n.Kind())
|
||||
}
|
||||
|
||||
func (g *gen) writeSuspend() error {
|
||||
const maxCoroState = 0xFFFFFFFF
|
||||
g.perFunc.coroState++
|
||||
if g.perFunc.coroState == maxCoroState {
|
||||
return fmt.Errorf("too many coroutine states required")
|
||||
func (g *gen) writeCoroSuspPoint() error {
|
||||
const maxCoroSuspPoint = 0xFFFFFFFF
|
||||
g.perFunc.coroSuspPoint++
|
||||
if g.perFunc.coroSuspPoint == maxCoroSuspPoint {
|
||||
return fmt.Errorf("too many coroutine suspension points required")
|
||||
}
|
||||
|
||||
g.printf("PUFFS_COROUTINE_STATE(%d);\n", g.perFunc.coroState)
|
||||
g.printf("PUFFS_COROUTINE_SUSPENSION_POINT(%d);\n", g.perFunc.coroSuspPoint)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -741,7 +744,7 @@ func (g *gen) writeSuspendibles(n *a.Expr, depth uint32) error {
|
||||
if !n.Suspendible() {
|
||||
return nil
|
||||
}
|
||||
if err := g.writeSuspend(); err != nil {
|
||||
if err := g.writeCoroSuspPoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.writeCallSuspendibles(n, depth)
|
||||
@@ -884,7 +887,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
g.writes(";\n")
|
||||
|
||||
if err := g.writeSuspend(); err != nil {
|
||||
if err := g.writeCoroSuspPoint(); err != nil {
|
||||
return err
|
||||
}
|
||||
g.printf("size_t %s%d = self->private_impl.scratch;\n", tPrefix, temp)
|
||||
|
||||
@@ -468,7 +468,7 @@ func (g *gen) writeStruct(n *a.Struct) error {
|
||||
// TODO: allow max depth > 1 for recursive coroutines.
|
||||
const maxDepth = 1
|
||||
g.writes("struct {\n")
|
||||
g.writes("uint32_t coro_state;\n")
|
||||
g.writes("uint32_t coro_susp_point;\n")
|
||||
if err := g.writeVars(o.Body()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user