Skip to content

Commit e8bfbb1

Browse files
committed
ok
1 parent fe08317 commit e8bfbb1

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

compiler/closureiters.nim

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,28 +1316,19 @@ proc replaceInlinedStates(ctx: var Ctx, n: PNode): PNode =
13161316
else:
13171317
n[i] = ctx.replaceInlinedStates(c)
13181318

1319-
proc deleteEmptyStates(ctx: var Ctx) =
1320-
# 1. Assign temporary indexes to state labels to facilitate
1321-
# countStateOccurences. Label number will correspond to index in
1322-
# ctx.states
1319+
proc optimizeStates(ctx: var Ctx) =
1320+
# Optimize empty states away and inline inlinable states
1321+
# This step requires that unique indexes are already assigned to state labels
13231322

1324-
# for i in 0 .. ctx.states.high:
1325-
# ctx.states[i].label.intVal = i
1326-
1327-
# 1. Delete empty states: go through ctx.states,
1328-
# if a state consists only of gotoState node, delete it from states
1329-
# and replace its label index to its target state
1330-
1331-
# var labelsToReplace = newSeq[(PNode, PNode, bool)]()
1332-
1333-
# Collect empty states
1323+
# Find empty states (those consisting only of gotoState node) and mark
1324+
# them deletable.
13341325
for i in 0 .. ctx.states.high:
13351326
let s = ctx.states[i]
13361327
let body = skipStmtList(s.body)
13371328
if body.kind == nkGotoState and body[0].kind == nkIntLit and body[0].intVal >= 0:
13381329
ctx.states[i].deletable = true
13391330

1340-
# Delete empty states
1331+
# Replace deletable state labels to labels of respective non-empty states
13411332
for i in 0 .. ctx.states.high:
13421333
ctx.states[i].body = ctx.replaceDeletedStates(ctx.states[i].body)
13431334

@@ -1353,18 +1344,15 @@ proc deleteEmptyStates(ctx: var Ctx) =
13531344
for i in 0 .. ctx.states.high:
13541345
ctx.states[i].label.intVal = i
13551346

1356-
# 2. Count state occurences
1347+
# Count state occurences
13571348
var stateOccurences = newSeq[int](ctx.states.len)
13581349
for s in ctx.states:
13591350
ctx.countStateOccurences(s.body, stateOccurences)
13601351

1361-
var statesToInline = newSeq[int]()
1352+
# If there are inlinable states refered not exactly once, prevent them from inlining
13621353
for i, o in stateOccurences:
1363-
if o == 1 and ctx.states[i].inlinable:
1364-
discard
1365-
else:
1354+
if o != 1:
13661355
ctx.states[i].inlinable = false
1367-
# statesToInline.add(i)
13681356

13691357
# echo "States to optimize:"
13701358
# for i, s in ctx.states:
@@ -1383,7 +1371,7 @@ proc deleteEmptyStates(ctx: var Ctx) =
13831371
else:
13841372
inc i
13851373

1386-
# Reassign state label indexes
1374+
# Reassign state label indexes one last time
13871375
for i in 0 .. ctx.states.high:
13881376
ctx.states[i].label.intVal = i
13891377

@@ -1484,8 +1472,7 @@ proc transformClosureIterator*(g: ModuleGraph; idgen: IdGenerator; fn: PSym, n:
14841472
for i in 0 .. ctx.states.high:
14851473
ctx.states[i].label.intVal = i
14861474

1487-
# Optimize empty states away
1488-
ctx.deleteEmptyStates()
1475+
ctx.optimizeStates()
14891476

14901477
let caseDispatcher = newTreeI(nkCaseStmt, n.info,
14911478
ctx.newStateAccess())

0 commit comments

Comments
 (0)