mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 16:47:28 +01:00
Rename some liveness.go types
This commit is contained in:
1 parent
1fc8910952
commit
3fb68ac477
1 file changed
+50
-50
+50
-50
@@ -44,12 +44,12 @@ package cgen
|
|||||||
// Algorithmically, we walk through a function's statements in two passes. The
|
// Algorithmically, we walk through a function's statements in two passes. The
|
||||||
// first pass finds all of the local variables, and assigns an integer index to
|
// first pass finds all of the local variables, and assigns an integer index to
|
||||||
// each one. The second tracks (in a slice indexed by that integer index) each
|
// each one. The second tracks (in a slice indexed by that integer index) each
|
||||||
// local variable's resumability, one of three possible states: none, weak and
|
// local variable's liveness, one of three possible states: none, weak and
|
||||||
// strong. Strong means that the local variable definitely needs to be kept.
|
// strong. Strong means that the local variable definitely needs to be kept.
|
||||||
// Weak means that we have (in the worst case, for branches' and loops'
|
// Weak means that we have (in the worst case, for branches' and loops'
|
||||||
// multiple paths) seen a CSP without a write afterwards, so that seeing a read
|
// multiple paths) seen a CSP without a write afterwards, so that seeing a read
|
||||||
// would change the resumability to strong. None means that we have not seen a
|
// would change the liveness to strong. None means that we have not seen a CSP
|
||||||
// CSP since the last write.
|
// since the last write.
|
||||||
//
|
//
|
||||||
// Essentially, the state transitions are: strong is sticky, weak to strong
|
// Essentially, the state transitions are: strong is sticky, weak to strong
|
||||||
// happens on a read, weak to none happens on a write, and none to weak happens
|
// happens on a read, weak to none happens on a write, and none to weak happens
|
||||||
@@ -70,26 +70,26 @@ import (
|
|||||||
t "github.com/google/wuffs/lang/token"
|
t "github.com/google/wuffs/lang/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
// resumability tracks whether a local variable's value will need to be kept
|
// liveness tracks whether a local variable's value will need to be kept across
|
||||||
// across CSPs (coroutine suspension points).
|
// CSPs (coroutine suspension points).
|
||||||
type resumability uint32
|
type liveness uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
resumabilityNone = resumability(0)
|
livenessNone = liveness(0)
|
||||||
resumabilityWeak = resumability(1)
|
livenessWeak = liveness(1)
|
||||||
resumabilityStrong = resumability(2)
|
livenessStrong = liveness(2)
|
||||||
)
|
)
|
||||||
|
|
||||||
// resumabilities is a slice of resumability values, one per local variable.
|
// livenesses is a slice of liveness values, one per local variable.
|
||||||
type resumabilities []resumability
|
type livenesses []liveness
|
||||||
|
|
||||||
func (r resumabilities) clone() resumabilities {
|
func (r livenesses) clone() livenesses {
|
||||||
return append(resumabilities(nil), r...)
|
return append(livenesses(nil), r...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r resumabilities) reconcile(s resumabilities) (changed bool) {
|
func (r livenesses) reconcile(s livenesses) (changed bool) {
|
||||||
if len(r) != len(s) {
|
if len(r) != len(s) {
|
||||||
panic("resumabilities have different length")
|
panic("livenesses have different length")
|
||||||
}
|
}
|
||||||
for i := range r {
|
for i := range r {
|
||||||
if r[i] < s[i] {
|
if r[i] < s[i] {
|
||||||
@@ -100,43 +100,43 @@ func (r resumabilities) reconcile(s resumabilities) (changed bool) {
|
|||||||
return changed
|
return changed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r resumabilities) lowerWeakToNone(i int) {
|
func (r livenesses) lowerWeakToNone(i int) {
|
||||||
if r[i] == resumabilityWeak {
|
if r[i] == livenessWeak {
|
||||||
r[i] = resumabilityNone
|
r[i] = livenessNone
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r resumabilities) raiseWeakToStrong(i int) {
|
func (r livenesses) raiseWeakToStrong(i int) {
|
||||||
if r[i] == resumabilityWeak {
|
if r[i] == livenessWeak {
|
||||||
r[i] = resumabilityStrong
|
r[i] = livenessStrong
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r resumabilities) raiseNoneToWeak() {
|
func (r livenesses) raiseNoneToWeak() {
|
||||||
for i, x := range r {
|
for i, x := range r {
|
||||||
if x == resumabilityNone {
|
if x == livenessNone {
|
||||||
r[i] = resumabilityWeak
|
r[i] = livenessWeak
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type loopResumable struct {
|
type loopLivenesses struct {
|
||||||
before resumabilities
|
before livenesses
|
||||||
after resumabilities
|
after livenesses
|
||||||
changed bool
|
changed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type resumabilityHelper struct {
|
type livenessHelper struct {
|
||||||
tm *t.Map
|
tm *t.Map
|
||||||
vars map[t.ID]int // Maps from local variable name to resumabilities index.
|
vars map[t.ID]int // Maps from local variable name to livenesses index.
|
||||||
loops map[a.Loop]*loopResumable
|
loops map[a.Loop]*loopLivenesses
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) findVars() error {
|
func (g *gen) findVars() error {
|
||||||
h := resumabilityHelper{
|
h := livenessHelper{
|
||||||
tm: g.tm,
|
tm: g.tm,
|
||||||
vars: map[t.ID]int{},
|
vars: map[t.ID]int{},
|
||||||
loops: map[a.Loop]*loopResumable{},
|
loops: map[a.Loop]*loopLivenesses{},
|
||||||
}
|
}
|
||||||
|
|
||||||
f := g.currFunk.astFunc
|
f := g.currFunk.astFunc
|
||||||
@@ -149,21 +149,21 @@ func (g *gen) findVars() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if f.Effect().Coroutine() {
|
if f.Effect().Coroutine() {
|
||||||
r := make(resumabilities, len(h.vars))
|
r := make(livenesses, len(h.vars))
|
||||||
if err := h.doBlock(r, f.Body(), 0); err != nil {
|
if err := h.doBlock(r, f.Body(), 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
g.currFunk.varResumables = map[t.ID]bool{}
|
g.currFunk.varResumables = map[t.ID]bool{}
|
||||||
for i, v := range g.currFunk.varList {
|
for i, v := range g.currFunk.varList {
|
||||||
g.currFunk.varResumables[v.Name()] = r[i] == resumabilityStrong
|
g.currFunk.varResumables[v.Name()] = r[i] == livenessStrong
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doBlock(r resumabilities, block []*a.Node, depth uint32) error {
|
func (h *livenessHelper) doBlock(r livenesses, block []*a.Node, depth uint32) error {
|
||||||
if depth > a.MaxBodyDepth {
|
if depth > a.MaxBodyDepth {
|
||||||
return fmt.Errorf("body recursion depth too large")
|
return fmt.Errorf("body recursion depth too large")
|
||||||
}
|
}
|
||||||
@@ -225,7 +225,7 @@ loop:
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doAssign(r resumabilities, n *a.Assign, depth uint32) error {
|
func (h *livenessHelper) doAssign(r livenesses, n *a.Assign, depth uint32) error {
|
||||||
if n.Operator() == t.IDEqQuestion {
|
if n.Operator() == t.IDEqQuestion {
|
||||||
if err := h.doExpr1(r, n.RHS(), 0); err != nil {
|
if err := h.doExpr1(r, n.RHS(), 0); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -260,7 +260,7 @@ func (h *resumabilityHelper) doAssign(r resumabilities, n *a.Assign, depth uint3
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doExpr(r resumabilities, n *a.Expr) error {
|
func (h *livenessHelper) doExpr(r livenesses, n *a.Expr) error {
|
||||||
if err := h.doExpr1(r, n, 0); err != nil {
|
if err := h.doExpr1(r, n, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -270,7 +270,7 @@ func (h *resumabilityHelper) doExpr(r resumabilities, n *a.Expr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doExpr1(r resumabilities, n *a.Expr, depth uint32) error {
|
func (h *livenessHelper) doExpr1(r livenesses, n *a.Expr, depth uint32) error {
|
||||||
if depth > a.MaxBodyDepth {
|
if depth > a.MaxBodyDepth {
|
||||||
return fmt.Errorf("body recursion depth too large")
|
return fmt.Errorf("body recursion depth too large")
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,7 @@ func (h *resumabilityHelper) doExpr1(r resumabilities, n *a.Expr, depth uint32)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doIOBind(r resumabilities, n *a.IOBind, depth uint32) error {
|
func (h *livenessHelper) doIOBind(r livenesses, n *a.IOBind, depth uint32) error {
|
||||||
if err := h.doExpr(r, n.IO()); err != nil {
|
if err := h.doExpr(r, n.IO()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -317,9 +317,9 @@ func (h *resumabilityHelper) doIOBind(r resumabilities, n *a.IOBind, depth uint3
|
|||||||
return h.doBlock(r, n.Body(), depth)
|
return h.doBlock(r, n.Body(), depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doIf(r resumabilities, n *a.If, depth uint32) error {
|
func (h *livenessHelper) doIf(r livenesses, n *a.If, depth uint32) error {
|
||||||
scratch := make(resumabilities, len(r))
|
scratch := make(livenesses, len(r))
|
||||||
result := make(resumabilities, len(r))
|
result := make(livenesses, len(r))
|
||||||
for ; n != nil; n = n.ElseIf() {
|
for ; n != nil; n = n.ElseIf() {
|
||||||
if err := h.doExpr(r, n.Condition()); err != nil {
|
if err := h.doExpr(r, n.Condition()); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -341,7 +341,7 @@ func (h *resumabilityHelper) doIf(r resumabilities, n *a.If, depth uint32) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doIterate(r resumabilities, n *a.Iterate, depth uint32) error {
|
func (h *livenessHelper) doIterate(r livenesses, n *a.Iterate, depth uint32) error {
|
||||||
// TODO: ban jumps, rets and coroutine calls inside an iterate. Also ensure
|
// TODO: ban jumps, rets and coroutine calls inside an iterate. Also ensure
|
||||||
// that the iterate variable values are all pure expressions.
|
// that the iterate variable values are all pure expressions.
|
||||||
for _, o := range n.Assigns() {
|
for _, o := range n.Assigns() {
|
||||||
@@ -359,7 +359,7 @@ func (h *resumabilityHelper) doIterate(r resumabilities, n *a.Iterate, depth uin
|
|||||||
return h.doBlock(r, n.Body(), depth)
|
return h.doBlock(r, n.Body(), depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doJump(r resumabilities, n *a.Jump, depth uint32) error {
|
func (h *livenessHelper) doJump(r livenesses, n *a.Jump, depth uint32) error {
|
||||||
l := h.loops[n.JumpTarget()]
|
l := h.loops[n.JumpTarget()]
|
||||||
switch n.Keyword() {
|
switch n.Keyword() {
|
||||||
case t.IDBreak:
|
case t.IDBreak:
|
||||||
@@ -372,7 +372,7 @@ func (h *resumabilityHelper) doJump(r resumabilities, n *a.Jump, depth uint32) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doRet(r resumabilities, n *a.Ret, depth uint32) error {
|
func (h *livenessHelper) doRet(r livenesses, n *a.Ret, depth uint32) error {
|
||||||
if err := h.doExpr(r, n.Value()); err != nil {
|
if err := h.doExpr(r, n.Value()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -388,7 +388,7 @@ func (h *resumabilityHelper) doRet(r resumabilities, n *a.Ret, depth uint32) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doVar(r resumabilities, n *a.Var, depth uint32) error {
|
func (h *livenessHelper) doVar(r livenesses, n *a.Var, depth uint32) error {
|
||||||
name := n.Name()
|
name := n.Name()
|
||||||
if i, ok := h.vars[name]; !ok {
|
if i, ok := h.vars[name]; !ok {
|
||||||
return fmt.Errorf("unrecognized variable %q", name.Str(h.tm))
|
return fmt.Errorf("unrecognized variable %q", name.Str(h.tm))
|
||||||
@@ -398,10 +398,10 @@ func (h *resumabilityHelper) doVar(r resumabilities, n *a.Var, depth uint32) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *resumabilityHelper) doWhile(r resumabilities, n *a.While, depth uint32) error {
|
func (h *livenessHelper) doWhile(r livenesses, n *a.While, depth uint32) error {
|
||||||
l := &loopResumable{
|
l := &loopLivenesses{
|
||||||
before: make(resumabilities, len(r)),
|
before: make(livenesses, len(r)),
|
||||||
after: make(resumabilities, len(r)),
|
after: make(livenesses, len(r)),
|
||||||
changed: false,
|
changed: false,
|
||||||
}
|
}
|
||||||
h.loops[n] = l
|
h.loops[n] = l
|
||||||
|
|||||||
Reference in new issue
Block a user