Add a PUFFS_COROUTINE_STATE macro.

This commit is contained in:
Nigel Tao committed 2017-06-23 14:47:34 +10:00
1 parent 24c8509242
commit bacd2e3b61
4 files changed
+363 -345

No files matched your search

+10
View File
@@ -3,6 +3,16 @@
#ifndef PUFFS_BASE_IMPL_H
#define PUFFS_BASE_IMPL_H
// 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
//
// 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:
#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))
#endif // PUFFS_BASE_IMPL_H
+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#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 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" +
""
+2 -2
View File
@@ -650,7 +650,7 @@ func (g *gen) writeFuncImpl(n *a.Func) error {
// 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) {\ncase 0:\n\n",
g.printf("switch (self->private_impl.%s%s[0].coro_state) {\nPUFFS_COROUTINE_STATE(0);\n\n",
cPrefix, n.Name().String(g.tm))
}
@@ -1048,7 +1048,7 @@ func (g *gen) writeSuspend() error {
return fmt.Errorf("too many coroutine states required")
}
g.printf("case %d:;\n", g.perFunc.coroState)
g.printf("PUFFS_COROUTINE_STATE(%d);\n", g.perFunc.coroState)
return nil
}