Separate the coro_susp_point from the coro stack

This commit is contained in:
Nigel Tao committed 2019-02-09 11:03:50 +11:00
1 parent 2928806ab6
commit 9518af5a95
6 files changed
+292 -317

No files matched your search

+2 -2
View File
@@ -593,7 +593,7 @@ func (g *gen) writeBuiltinQuestionCall(b *buffer, n *a.Expr, depth uint32) error
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.FuncName().Str(g.tm))
sPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("%s = ", scratchName)
if err := g.writeExpr(b, x, depth); err != nil {
@@ -667,7 +667,7 @@ func (g *gen) writeReadUxxAsUyy(b *buffer, n *a.Expr, preName string, xx uint8,
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.FuncName().Str(g.tm))
sPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("if (WUFFS_BASE__LIKELY(io1_a_src - iop_a_src >= %d)) {", xx/8)
b.printf("%s%d =", tPrefix, temp)
+15 -3
View File
@@ -52,10 +52,11 @@ var (
// "double" being a valid Wuffs variable name but not a valid C one.
const (
aPrefix = "a_" // Function argument.
cPrefix = "c_" // Coroutine state.
fPrefix = "f_" // Struct field.
iPrefix = "i_" // Iterate variable.
oPrefix = "o_" // Temporary io_bind variable.
pPrefix = "p_" // Coroutine suspension point (program counter).
sPrefix = "s_" // Coroutine stack (saved local variables).
tPrefix = "t_" // Temporary local variable.
uPrefix = "u_" // Derived from a local variable.
vPrefix = "v_" // Local variable.
@@ -760,11 +761,18 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error {
if k.coroSuspPoint == 0 && !k.usesScratch {
continue
}
// TODO: allow max depth > 1 for recursive coroutines.
const maxDepth = 1
if k.coroSuspPoint != 0 {
b.printf("uint32_t %s%s[%d];\n", pPrefix, o.FuncName().Str(g.tm), maxDepth)
}
oldLenB0 := len(*b)
b.writes("struct {\n")
oldLenB1 := len(*b)
if k.coroSuspPoint != 0 {
b.writes("uint32_t coro_susp_point;\n")
if err := g.writeVars(b, &k, true); err != nil {
return err
}
@@ -772,7 +780,11 @@ func (g *gen) writeStructPrivateImpl(b *buffer, n *a.Struct) error {
if k.usesScratch {
b.writes("uint64_t scratch;\n")
}
b.printf("} %s%s[%d];\n", cPrefix, o.FuncName().Str(g.tm), maxDepth)
if oldLenB1 != len(*b) {
b.printf("} %s%s[%d];\n", sPrefix, o.FuncName().Str(g.tm), maxDepth)
} else {
*b = (*b)[:oldLenB0]
}
}
}
}
+2 -2
View File
@@ -62,8 +62,8 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, depth uint32) error {
} else if ident == t.IDCoroutineResumed {
if g.currFunk.astFunc.Effect().Coroutine() {
// TODO: don't hard-code [0], and allow recursive coroutines.
b.printf("(self->private_impl.%s%s[0].coro_susp_point != 0)",
cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("(self->private_impl.%s%s[0] != 0)",
pPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
} else {
b.writes("false")
}
+6 -6
View File
@@ -303,8 +303,8 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error {
func (g *gen) writeFuncImplBodyResume(b *buffer) error {
if g.currFunk.astFunc.Effect().Coroutine() {
// TODO: don't hard-code [0], and allow recursive coroutines.
b.printf("uint32_t coro_susp_point = self->private_impl.%s%s[0].coro_susp_point;\n",
cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("uint32_t coro_susp_point = self->private_impl.%s%s[0];\n",
pPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("if (coro_susp_point) {\n")
if err := g.writeResumeSuspend(b, &g.currFunk, false); err != nil {
return err
@@ -334,14 +334,14 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
// suspension point so that the next call to this function starts at
// the top.
b.writes("\ngoto ok;ok:") // The goto avoids the "unused label" warning.
b.printf("self->private_impl.%s%s[0].coro_susp_point = 0;\n",
cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("self->private_impl.%s%s[0] = 0;\n",
pPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.writes("goto exit; }\n\n") // Close the coroutine switch.
b.writes("goto suspend;suspend:") // The goto avoids the "unused label" warning.
b.printf("self->private_impl.%s%s[0].coro_susp_point = coro_susp_point;\n",
cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("self->private_impl.%s%s[0] = coro_susp_point;\n",
pPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
if g.currFunk.astFunc.Public() {
b.printf("self->private_impl.active_coroutine = %d;\n", g.currFunk.coroID)
}
+1 -1
View File
@@ -229,7 +229,7 @@ func (g *gen) writeResumeSuspend1(b *buffer, f *funk, n *a.Var, suspend bool) er
local := fmt.Sprintf("%s%s", vPrefix, n.Name().Str(g.tm))
lhs := local
// TODO: don't hard-code [0], and allow recursive coroutines.
rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", cPrefix, g.currFunk.astFunc.FuncName().Str(g.tm), lhs)
rhs := fmt.Sprintf("self->private_impl.%s%s[0].%s", sPrefix, g.currFunk.astFunc.FuncName().Str(g.tm), lhs)
if suspend {
lhs, rhs = rhs, lhs
}