Introduce finer-grained effects

This commit is contained in:
Nigel Tao committed 2018-07-29 13:40:49 +10:00
1 parent 77e538aec8
commit 0a47966e72
12 files changed
+129 -115

No files matched your search

+6 -5
View File
@@ -647,7 +647,7 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
b.writes("//\n")
b.writes("// It is a struct, not a struct*, so that it can be stack allocated.\n")
b.writes("struct {\n")
if n.Suspendible() {
if n.Optional() {
b.printf("wuffs_base__status status;\n")
b.writes("uint32_t magic;\n")
b.writes("\n")
@@ -661,7 +661,7 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
b.writes(";\n")
}
if n.Suspendible() {
if n.Optional() {
b.writeb('\n')
for _, file := range g.files {
for _, tld := range file.TopLevelDecls() {
@@ -669,7 +669,8 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
continue
}
o := tld.AsFunc()
if o.Receiver() != n.QID() || !o.Suspendible() {
// XXX: s/Optional/Coroutine/
if o.Receiver() != n.QID() || !o.Effect().Optional() {
continue
}
k := g.funks[o.QQID()]
@@ -871,7 +872,7 @@ func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) err
}
func (g *gen) writeInitializerPrototype(b *buffer, n *a.Struct) error {
if !n.Suspendible() {
if !n.Optional() {
return nil
}
if err := g.writeInitializerSignature(b, n, n.Public()); err != nil {
@@ -882,7 +883,7 @@ func (g *gen) writeInitializerPrototype(b *buffer, n *a.Struct) error {
}
func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
if !n.Suspendible() {
if !n.Optional() {
return nil
}
if err := g.writeInitializerSignature(b, n, false); err != nil {
+2 -1
View File
@@ -30,7 +30,8 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32
}
depth++
if rp == replaceCallSuspendibles && n.CallSuspendible() {
// XXX: s/Optional/Coroutine/
if rp == replaceCallSuspendibles && n.Effect().RootCause() && n.Effect().Optional() {
if g.currFunk.tempR >= g.currFunk.tempW {
return fmt.Errorf("internal error: temporary variable count out of sync")
}
+4 -3
View File
@@ -38,7 +38,7 @@ type funk struct {
tempW uint32
tempR uint32
public bool
suspendible bool
suspendible bool // TODO: rename to coroutine.
usesScratch bool
hasGotoOK bool
shortReads []string
@@ -86,7 +86,8 @@ func (g *gen) writeFuncSignature(b *buffer, n *a.Func, cpp uint32) error {
}
// TODO: write n's return values.
if n.Suspendible() {
// XXX: s/Optional/Coroutine/
if n.Effect().Optional() {
b.writes("wuffs_base__status ")
} else if out := n.Out(); out == nil {
// TODO: wuffs_base__empty_struct.
@@ -174,7 +175,7 @@ func (g *gen) gatherFuncImpl(_ *buffer, n *a.Func) error {
astFunc: n,
cName: g.funcCName(n),
public: n.Public(),
suspendible: n.Suspendible(),
suspendible: n.Effect().Optional(), // XXX: s/Optional/Coroutine/
}
if err := g.writeFuncImplHeader(&g.currFunk.bHeader); err != nil {
+13 -6
View File
@@ -43,10 +43,12 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
switch n.Kind() {
case a.KAssign:
n := n.AsAssign()
mightIntroduceTemporaries = n.LHS().Suspendible() || n.RHS().Suspendible()
// XXX: s/Optional/Coroutine/
mightIntroduceTemporaries = n.LHS().Effect().Optional() || n.RHS().Effect().Optional()
case a.KVar:
v := n.AsVar().Value()
mightIntroduceTemporaries = v != nil && v.Suspendible()
// XXX: s/Optional/Coroutine/
mightIntroduceTemporaries = v != nil && v.Effect().Optional()
}
if mightIntroduceTemporaries {
// Put n's code into its own block, to restrict the scope of the
@@ -139,7 +141,8 @@ func (g *gen) writeStatementExpr(b *buffer, n *a.Expr, depth uint32) error {
if err := g.writeSuspendibles(b, n, depth); err != nil {
return err
}
if n.CallSuspendible() {
// XXX: s/Optional/Coroutine/
if n.Effect().RootCause() && n.Effect().Optional() {
return nil
}
if err := g.writeExpr(b, n, replaceCallSuspendibles, depth); err != nil {
@@ -218,7 +221,9 @@ func (g *gen) writeStatementIf(b *buffer, n *a.If, depth uint32) error {
// sub-expression evaluation correct.
nCloseCurly := 1
for first := true; ; first = false {
if n.Condition().Suspendible() {
// TODO: restrict the if condition to effect-free expressions?
// XXX: s/Optional/Coroutine/
if n.Condition().Effect().Optional() {
if !first {
b.writeb('{')
const maxCloseCurly = 1000
@@ -482,7 +487,8 @@ func (g *gen) writeCoroSuspPoint(b *buffer, maybeSuspend bool) error {
}
func (g *gen) writeSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
if !n.Suspendible() {
// XXX: s/Optional/Coroutine/
if !n.Effect().Optional() {
return nil
}
if n.Operator() != t.IDTry {
@@ -501,7 +507,8 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
// The evaluation order for suspendible calls (which can have side effects)
// is important here: LHS, MHS, RHS, Args and finally the node itself.
if !n.CallSuspendible() {
// XXX: s/Optional/Coroutine/
if e := n.Effect(); !e.RootCause() || !e.Optional() {
for _, o := range n.AsNode().AsRaw().SubNodes() {
if o != nil && o.Kind() == a.KExpr {
if err := g.writeCallSuspendibles(b, o.AsExpr(), depth); err != nil {
+2 -8
View File
@@ -285,17 +285,11 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error
if !n.Public() {
continue
}
effect := ""
if n.Suspendible() {
effect = "?"
} else if n.Impure() {
effect = "!"
}
if n.Receiver().IsZero() {
return fmt.Errorf("TODO: genWuffs for a free-standing function")
}
// TODO: look at n.Asserts().
fmt.Fprintf(out, "pub func %s.%s%s(", n.Receiver().Str(&h.tm), n.FuncName().Str(&h.tm), effect)
fmt.Fprintf(out, "pub func %s.%s%v(", n.Receiver().Str(&h.tm), n.FuncName().Str(&h.tm), n.Effect())
for i, field := range n.In().Fields() {
field := field.AsField()
if i > 0 {
@@ -325,7 +319,7 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error
continue
}
effect := ""
if n.Suspendible() {
if n.Optional() {
effect = "?"
}
fmt.Fprintf(out, "pub struct %s%s()\n", n.QID().Str(&h.tm), effect)