Generate C code to set coroutine state.

This commit is contained in:
Nigel Tao committed 2017-06-23 15:35:18 +10:00
1 parent bacd2e3b61
commit 8ad82f5e1f
5 files changed
+92 -32

No files matched your search

+5 -3
View File
@@ -9,9 +9,11 @@
// Use switch cases for coroutine state, similar to the technique in
// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
//
// We use a trivial macro instead of an explicit case statement so that
// clang-format doesn't get confused by the unusual "case"s.
#define PUFFS_COROUTINE_STATE(n) case n:
// 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; \
case n:
#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))
+1 -1
View File
@@ -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 case statement so that\n// clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_STATE(n) case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\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 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#endif // PUFFS_BASE_IMPL_H\n" +
""
+5 -5
View File
@@ -641,17 +641,16 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
g.writes("\n")
if g.perFunc.suspendible {
g.printf("if (self->private_impl.%s%s[0].coro_state) {\n", cPrefix, n.Name().String(g.tm))
// 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")
if err := g.writeResumeSuspend(n.Body(), false); err != nil {
return err
}
g.writes("}\n")
// Generate a coroutine switch similiar to the technique in
// https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
//
// TODO: don't hard-code [0], and allow recursive coroutines.
g.printf("switch (self->private_impl.%s%s[0].coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n",
cPrefix, n.Name().String(g.tm))
g.writes("switch (coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n")
}
// Generate the function body.
@@ -669,6 +668,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
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))
if err := g.writeResumeSuspend(n.Body(), true); err != nil {
return err
}