Skip to content

Commit 84db6b8

Browse files
committed
cleanup
1 parent 9354896 commit 84db6b8

File tree

1 file changed

+25
-214
lines changed

1 file changed

+25
-214
lines changed

compiler/closureiters.nim

Lines changed: 25 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -846,56 +846,12 @@ proc lowerStmtListExprs(ctx: var Ctx, n: PNode, needsSplit: var bool): PNode =
846846

847847
proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
848848
# Generate the following code:
849-
# if :unrollFinally:
850-
# if :curExc.isNil:
851-
# WHEN nearestFinally == 0:
852-
# return :tmpResult
853-
# else:
854-
# :state = nearestFinally # bubble up
855-
# else:
856-
# raise
857-
858-
# let curExc = ctx.newCurExcAccess()
859-
# let nilnode = newNode(nkNilLit)
860-
# nilnode.typ() = curExc.typ
861-
# let cmp = newTree(nkCall, newSymNode(ctx.g.getSysMagic(info, "==", mEqRef), info), curExc, nilnode)
862-
# cmp.typ() = ctx.g.getSysType(info, tyBool)
863-
864-
# let retStmt =
865-
# if ctx.nearestFinally == 0:
866-
# # last finally, we can return
867-
# let retValue = if ctx.fn.typ.returnType.isNil:
868-
# ctx.g.emptyNode
869-
# else:
870-
# newTree(nkFastAsgn,
871-
# newSymNode(getClosureIterResult(ctx.g, ctx.fn, ctx.idgen), info),
872-
# ctx.newTmpResultAccess())
873-
# newTree(nkReturnStmt, retValue)
849+
# if :finallyPath[FINALLY_LEVEL] == 0:
850+
# if :curExcLevel == 0:
851+
# :state = -1
852+
# return result = :tmpResult
874853
# else:
875-
# # bubble up to next finally
876-
# newTree(nkGotoState, ctx.g.newIntLit(info, ctx.nearestFinally))
877-
878-
# let branch = newTree(nkElifBranch, cmp, retStmt)
879-
880-
# let nullifyExc = newTree(nkCall, newSymNode(ctx.g.getCompilerProc("closureIterSetupExc")), nilnode)
881-
# nullifyExc.info = info
882-
# let raiseStmt = newTree(nkRaiseStmt, curExc)
883-
# raiseStmt.info = info
884-
# let elseBranch = newTree(nkElse, newTree(nkStmtList, nullifyExc, raiseStmt))
885-
886-
# let ifBody = newTree(nkIfStmt, branch, elseBranch)
887-
# let elifBranch = newTree(nkElifBranch, ctx.newUnrollFinallyAccess(info), ifBody)
888-
# elifBranch.info = info
889-
# result = newTree(nkIfStmt, elifBranch)
890-
891-
# Generate the following code:
892-
# if :env.finallyPath[curFinallyLevel_const] == 0
893-
# if :curExc.isNil:
894-
# return :tmpResult
895-
# else:
896-
# raise
897-
# else:
898-
# :state = :env.finallyPath[curFinallyLevel_const]
854+
# raise
899855

900856
let nextState = ctx.newFinallyPathAccess(ctx.curFinallyLevel, info)
901857
let exitFinally = newTree(nkGotoState, nextState)
@@ -907,7 +863,6 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
907863
ctx.g.newIntLit(info, 0))
908864

909865
let curExc = ctx.newCurExcLevelAccess()
910-
# let nilnode = newNodeIT(nkNilLit, info, curExc.typ)
911866
let excNilCmp = newTreeIT(nkCall,
912867
info, ctx.g.getSysType(info, tyBool),
913868
newSymNode(ctx.g.getSysMagic(info, "==", mEqI), info),
@@ -925,7 +880,6 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
925880
newTree(nkReturnStmt, retValue)
926881
retStmt.flags.incl(nfNoRewrite)
927882

928-
929883
let ifBody = newTree(nkIfStmt,
930884
newTree(nkElifBranch, excNilCmp, retStmt),
931885
newTree(nkElse,
@@ -934,9 +888,7 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
934888
newTreeI(nkRaiseStmt, info, ctx.g.emptyNode))))
935889

936890
result = newTree(nkIfStmt,
937-
newTreeI(nkElifBranch, info, cmpStateToZero, ifBody),
938-
# newTreeI(nkElse, info, exitFinally)
939-
)
891+
newTreeI(nkElifBranch, info, cmpStateToZero, ifBody))
940892

941893

942894
proc newJumpAlongFinallyChain(ctx: var Ctx, finallyChain: seq[PNode], info: TLineInfo): PNode =
@@ -961,10 +913,7 @@ proc transformBreakStmt(ctx: var Ctx, n: PNode): PNode =
961913
# unnamed blocks is forbidden). Thus the break target is a named nkBlock with corresponding
962914
# name. Let's find it in finallyTarget stack, and remember all finallies on the way to it.
963915

964-
# echo "BREAAAAKKKK: "
965-
# debug(n[0])
966916
let targetName = if n[0].kind == nkSym: n[0].sym else: nil
967-
968917
var finallyChain = newSeq[PNode]()
969918
970919
for i in countdown(ctx.finallyPathStack.high, 0):
@@ -984,24 +933,6 @@ proc transformBreakStmt(ctx: var Ctx, n: PNode): PNode =
984933
else:
985934
result = n
986935

987-
proc transformContinueStmt(ctx: var Ctx, n: PNode): PNode =
988-
# "Continuing" involves finding the corresponding target state in finallyPathStack,
989-
# setting finallyPath so that it chains to the target state and jumping to nearest finally.
990-
# If there are no finallies on the way, then jump to the target block right away
991-
var finallyChain = newSeq[PNode]()
992-
993-
for i in countdown(ctx.finallyPathStack.high, 0):
994-
let b = ctx.finallyPathStack[i].n
995-
if b.kind == nkWhileStmt:
996-
finallyChain.add(ctx.finallyPathStack[i].enterLabel)
997-
break
998-
elif b.kind == nkFinally:
999-
finallyChain.add(ctx.finallyPathStack[i].enterLabel)
1000-
1001-
doAssert(false, "CONTINUE")
1002-
result = ctx.newJumpAlongFinallyChain(finallyChain, n.info)
1003-
echo "CONTINUE ", renderTree(result)
1004-
1005936
proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1006937
# "Breaking" involves finding the corresponding target state in finallyPathStack,
1007938
# setting finallyPath so that it chains to the target state and jumping to nearest finally.
@@ -1012,9 +943,6 @@ proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1012943
# unnamed blocks is forbidden). Thus the break target is a named nkBlock with corresponding
1013944
# name. Let's find it in finallyTarget stack, and remember all finallies on the way to it.
1014945

1015-
# echo "BREAAAAKKKK: "
1016-
# debug(n[0])
1017-
1018946
result = newNodeI(nkStmtList, n.info)
1019947
result.add(ctx.newNullifyCurExcLevel(n.info))
1020948

@@ -1027,8 +955,6 @@ proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1027955
finallyChain.add(ctx.finallyPathStack[i].enterLabel)
1028956
1029957
if finallyChain.len > 0:
1030-
# assert(finallyChain.len > 0)
1031-
1032958
# Add proc exit state
1033959
finallyChain.add(ctx.g.newIntLit(n.info, 0))
1034960
@@ -1043,14 +969,13 @@ proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1043969
else:
1044970
result.add(n)
1045971

1046-
# echo "RETUUUURNM: ", result
1047-
1048972
proc transformBreaksContinuesAndReturns(ctx: var Ctx, n: PNode): PNode =
1049973
result = n
1050974
case n.kind
1051975
of nkSkip: discard
1052976
of nkBreakStmt: result = ctx.transformBreakStmt(n)
1053-
# of nkContinueStmt: result = ctx.transformContinueStmt(n)
977+
of nkContinueStmt:
978+
internalError(ctx.g.config, n.info, "Continue not lowered")
1054979
of nkReturnStmt:
1055980
if ctx.curFinallyLevel > 0 and nfNoRewrite notin n.flags:
1056981
result = ctx.transformReturnStmt(n)
@@ -1086,9 +1011,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
10861011

10871012
of nkYieldStmt:
10881013
result = addGotoOut(result, gotoOut)
1089-
# result = newNodeI(nkStmtList, n.info)
1090-
# result.add(n)
1091-
# result.add(gotoOut)
10921014

10931015
of nkElse, nkElseExpr:
10941016
result[0] = addGotoOut(result[0], gotoOut)
@@ -1159,10 +1081,8 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
11591081
result.add(tryLabel)
11601082
var tryBody = toStmtList(n[0])
11611083

1162-
var exceptBody = ctx.collectExceptState(n)
1084+
let exceptBody = ctx.collectExceptState(n)
11631085
var finallyBody = ctx.getFinallyNode(n)
1164-
# var finallyBody = newTree(nkStmtList, getFinallyNode(ctx, n))
1165-
# finallyBody = ctx.transformReturnsInTry(finallyBody)
11661086
var exceptLabel, finallyLabel = ctx.g.emptyNode
11671087

11681088
if exceptBody.kind != nkEmpty:
@@ -1187,17 +1107,14 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
11871107

11881108
tryOut = newNodeI(nkGotoState, finallyBody.info)
11891109
tryOut.add(finallyLabel)
1190-
# let outToFinally = newNodeI(nkGotoState, finallyBody.info)
1191-
# outToFinally.add(finallyLabel)
11921110

11931111
block: # Process the states
11941112
let oldExcLandingState = ctx.curExcLandingState
11951113
ctx.curExcLandingState = if exceptBody.kind != nkEmpty: exceptLabel
11961114
elif finallyBody.kind != nkEmpty: finallyLabel
11971115
else: oldExcLandingState
1198-
# ctx.curExcHandlingState = exceptIdx
1116+
11991117
discard ctx.newState(tryBody, false, tryLabel)
1200-
# assert(realTryIdx.intVal == tryIdx)
12011118

12021119
if finallyBody.kind != nkEmpty:
12031120
inc ctx.curFinallyLevel
@@ -1210,7 +1127,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
12101127
ctx.curExcLandingState = if finallyBody.kind != nkEmpty: finallyLabel
12111128
else: oldExcLandingState
12121129
discard ctx.newState(exceptBody, false, exceptLabel)
1213-
# assert(realExceptIdx.intVal == -exceptIdx)
12141130

12151131
let normalOut = if finallyBody.kind != nkEmpty: gotoOut else: nil
12161132
ctx.addElseToExcept(exceptBody, normalOut)
@@ -1228,32 +1144,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
12281144
internalError(ctx.g.config, "transformClosureIteratorBody != finallyBody")
12291145
dec ctx.curFinallyLevel
12301146

1231-
# assert(finallyLabel.intVal == finallyIdx)
1232-
1233-
# block: # Subdivide the states
1234-
# let oldNearestFinally = ctx.nearestFinally
1235-
# ctx.nearestFinally = finallyIdx
1236-
1237-
# let oldExcLandingState = ctx.curExcHandlingState
1238-
1239-
# ctx.curExcHandlingState = exceptIdx
1240-
1241-
# if ctx.transformReturnsInTry(tryBody) != tryBody:
1242-
# internalError(ctx.g.config, "transformReturnsInTry != tryBody")
1243-
# if ctx.transformClosureIteratorBody(tryBody, tryOut) != tryBody:
1244-
# internalError(ctx.g.config, "transformClosureIteratorBody != tryBody")
1245-
1246-
# ctx.curExcHandlingState = finallyIdx
1247-
# ctx.addElseToExcept(exceptBody)
1248-
# if ctx.transformReturnsInTry(exceptBody) != exceptBody:
1249-
# internalError(ctx.g.config, "transformReturnsInTry != exceptBody")
1250-
# if ctx.transformClosureIteratorBody(exceptBody, outToFinally) != exceptBody:
1251-
# internalError(ctx.g.config, "transformClosureIteratorBody != exceptBody")
1252-
1253-
# ctx.nearestFinally = oldNearestFinally
1254-
# if ctx.transformClosureIteratorBody(finallyBody, gotoOut) != finallyBody:
1255-
# internalError(ctx.g.config, "transformClosureIteratorBody != finallyBody")
1256-
12571147
of nkGotoState, nkForStmt:
12581148
internalError(ctx.g.config, "closure iter " & $n.kind)
12591149

@@ -1263,7 +1153,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
12631153

12641154
proc stateFromGotoState(n: PNode): PNode =
12651155
assert(n.kind == nkGotoState)
1266-
# result = n[0].intVal.int
12671156
result = n[0]
12681157

12691158
proc transformStateAssignments(ctx: var Ctx, n: PNode): PNode =
@@ -1330,47 +1219,6 @@ proc transformStateAssignments(ctx: var Ctx, n: PNode): PNode =
13301219
for i in 0..<n.len:
13311220
n[i] = ctx.transformStateAssignments(n[i])
13321221

1333-
# proc skipStmtList(ctx: Ctx; n: PNode): PNode =
1334-
# result = n
1335-
# while result.kind in {nkStmtList}:
1336-
# if result.len == 0: return ctx.g.emptyNode
1337-
# result = result[0]
1338-
1339-
# proc skipEmptyStates(ctx: Ctx, stateIdx: int): int =
1340-
# # Returns first non-empty state idx for `stateIdx`. Returns `stateIdx` if
1341-
# # it is not empty
1342-
# var maxJumps = ctx.states.len # maxJumps used only for debugging purposes.
1343-
# var stateIdx = stateIdx
1344-
# while true:
1345-
# let label = stateIdx
1346-
# if label == ctx.exitStateIdx: break
1347-
# var newLabel = label
1348-
# if label == emptyStateLabel:
1349-
# newLabel = ctx.exitStateIdx
1350-
# else:
1351-
# let fs = skipStmtList(ctx, ctx.states[label].body)
1352-
# if fs.kind == nkGotoState:
1353-
# newLabel = fs[0].intVal.int
1354-
# if label == newLabel: break
1355-
# stateIdx = newLabel
1356-
# dec maxJumps
1357-
# if maxJumps == 0:
1358-
# assert(false, "Internal error")
1359-
1360-
# result = ctx.states[stateIdx].label
1361-
1362-
# proc skipThroughEmptyStates(ctx: var Ctx, n: PNode): PNode=
1363-
# result = n
1364-
# case n.kind
1365-
# of nkSkip:
1366-
# discard
1367-
# of nkGotoState:
1368-
# result = copyTree(n)
1369-
# result[0].intVal = ctx.skipEmptyStates(result[0].intVal.int)
1370-
# else:
1371-
# for i in 0..<n.len:
1372-
# n[i] = ctx.skipThroughEmptyStates(n[i])
1373-
13741222
proc createExceptionTable(ctx: var Ctx): PNode {.inline.} =
13751223
let typ = ctx.g.newArrayType(ctx.g.newIntLit(ctx.fn.info, ctx.states.high),
13761224
ctx.g.getSysType(ctx.fn.info, tyInt16), ctx.idgen, ctx.fn)
@@ -1382,14 +1230,10 @@ proc createExceptionTable(ctx: var Ctx): PNode {.inline.} =
13821230
result.add(elem)
13831231

13841232
proc newCatchBody(ctx: var Ctx, info: TLineInfo): PNode {.inline.} =
1385-
# Generates the code:
1386-
# :state = exceptionTable[:state]
1387-
# if :state == 0: raise
1388-
# :unrollFinally = :state > 0
1389-
# if :state < 0:
1390-
# :state = -:state
1391-
# :curExc = getCurrentException()
1392-
1233+
# Generates code:
1234+
# :state = exceptionTable[:state]
1235+
# if :state == 0:
1236+
# raise
13931237
result = newNodeI(nkStmtList, info)
13941238

13951239
let intTyp = ctx.g.getSysType(info, tyInt)
@@ -1417,50 +1261,19 @@ proc newCatchBody(ctx: var Ctx, info: TLineInfo): PNode {.inline.} =
14171261
let ifStmt = newTree(nkIfStmt, ifBranch)
14181262
result.add(ifStmt)
14191263

1420-
# :unrollFinally = :state > 0
1421-
# block:
1422-
# let cond = newTree(nkCall,
1423-
# ctx.g.getSysMagic(info, "<", mLtI).newSymNode,
1424-
# newIntTypeNode(0, intTyp),
1425-
# ctx.newStateAccess())
1426-
# cond.typ() = boolTyp
1427-
1428-
# let asgn = newTree(nkAsgn, ctx.newUnrollFinallyAccess(info), cond)
1429-
# result.add(asgn)
1430-
1431-
# if :state < 0: :state = -:state
1432-
# block:
1433-
# let cond = newTree(nkCall,
1434-
# ctx.g.getSysMagic(info, "<", mLtI).newSymNode,
1435-
# ctx.newStateAccess(),
1436-
# newIntTypeNode(0, intTyp))
1437-
# cond.typ() = boolTyp
1438-
1439-
# let negateState = newTree(nkCall,
1440-
# ctx.g.getSysMagic(info, "-", mUnaryMinusI).newSymNode,
1441-
# ctx.newStateAccess())
1442-
# negateState.typ() = intTyp
1443-
1444-
# let ifBranch = newTree(nkElifBranch, cond, ctx.newStateAssgn(negateState))
1445-
# let ifStmt = newTree(nkIfStmt, ifBranch)
1446-
# result.add(ifStmt)
1447-
1448-
# :curExc = getCurrentException()
1449-
# block:
1450-
# result.add(newTree(nkAsgn,
1451-
# ctx.newCurExcAccess(),
1452-
# ctx.g.callCodegenProc("getCurrentException")))
1453-
14541264
proc wrapIntoTryExcept(ctx: var Ctx, n: PNode): PNode {.inline.} =
1455-
# let setupExc = newTree(nkCall,
1456-
# newSymNode(ctx.g.getCompilerProc("closureIterSetupExc")),
1457-
# ctx.newCurExcAccess())
1458-
1459-
# let printDebug = newTree(nkCall,
1460-
# newSymNode(ctx.g.getCompilerProc("closureIterDebug")),
1461-
# ctx.newStateAccess())
1265+
# Generates code:
1266+
# var :tmp = nil
1267+
# try:
1268+
# body
1269+
# except:
1270+
# :state = exceptionTable[:state]
1271+
# if :state == 0:
1272+
# raise
1273+
# :tmp = getCurrentException()
1274+
#
1275+
# pushCurrentException(:tmp)
14621276

1463-
# let tryBody = newTree(nkStmtList, setupExc, n)
14641277
let tryBody = newTree(nkStmtList, n)
14651278
let exceptBody = ctx.newCatchBody(ctx.fn.info)
14661279
let exceptBranch = newTree(nkExceptBranch, exceptBody)
@@ -1474,8 +1287,6 @@ proc wrapIntoTryExcept(ctx: var Ctx, n: PNode): PNode {.inline.} =
14741287
result.add newTree(nkCall, newSymNode(ctx.g.getCompilerProc("pushCurrentException")), ctx.newTempVarAccess(tempExc))
14751288
result.add ctx.newChangeCurExcLevel(n.info, 1)
14761289

1477-
# result = newTree(nkTryStmt, tryBody, exceptBranch)
1478-
14791290
proc wrapIntoStateLoop(ctx: var Ctx, n: PNode): PNode =
14801291
# while true:
14811292
# block :stateLoop:

0 commit comments

Comments
 (0)