@@ -846,56 +846,12 @@ proc lowerStmtListExprs(ctx: var Ctx, n: PNode, needsSplit: var bool): PNode =
846
846
847
847
proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
848
848
# 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
874
853
# 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
899
855
900
856
let nextState = ctx.newFinallyPathAccess(ctx.curFinallyLevel, info)
901
857
let exitFinally = newTree(nkGotoState, nextState)
@@ -907,7 +863,6 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
907
863
ctx.g.newIntLit(info, 0 ))
908
864
909
865
let curExc = ctx.newCurExcLevelAccess()
910
- # let nilnode = newNodeIT(nkNilLit, info, curExc.typ)
911
866
let excNilCmp = newTreeIT(nkCall,
912
867
info, ctx.g.getSysType(info, tyBool),
913
868
newSymNode(ctx.g.getSysMagic(info, " ==" , mEqI), info),
@@ -925,7 +880,6 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
925
880
newTree(nkReturnStmt, retValue)
926
881
retStmt.flags.incl(nfNoRewrite)
927
882
928
-
929
883
let ifBody = newTree(nkIfStmt,
930
884
newTree(nkElifBranch, excNilCmp, retStmt),
931
885
newTree(nkElse,
@@ -934,9 +888,7 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
934
888
newTreeI(nkRaiseStmt, info, ctx.g.emptyNode))))
935
889
936
890
result = newTree(nkIfStmt,
937
- newTreeI(nkElifBranch, info, cmpStateToZero, ifBody),
938
- # newTreeI(nkElse, info, exitFinally)
939
- )
891
+ newTreeI(nkElifBranch, info, cmpStateToZero, ifBody))
940
892
941
893
942
894
proc newJumpAlongFinallyChain(ctx: var Ctx, finallyChain: seq [PNode], info: TLineInfo) : PNode =
@@ -961,10 +913,7 @@ proc transformBreakStmt(ctx: var Ctx, n: PNode): PNode =
961
913
# unnamed blocks is forbidden). Thus the break target is a named nkBlock with corresponding
962
914
# name. Let's find it in finallyTarget stack, and remember all finallies on the way to it.
963
915
964
- # echo "BREAAAAKKKK: "
965
- # debug(n[0])
966
916
let targetName = if n[0 ].kind == nkSym: n[0 ].sym else : nil
967
-
968
917
var finallyChain = newSeq[PNode]()
969
918
970
919
for i in countdown(ctx.finallyPathStack.high, 0):
@@ -984,24 +933,6 @@ proc transformBreakStmt(ctx: var Ctx, n: PNode): PNode =
984
933
else :
985
934
result = n
986
935
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
-
1005
936
proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1006
937
# "Breaking" involves finding the corresponding target state in finallyPathStack,
1007
938
# 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 =
1012
943
# unnamed blocks is forbidden). Thus the break target is a named nkBlock with corresponding
1013
944
# name. Let's find it in finallyTarget stack, and remember all finallies on the way to it.
1014
945
1015
- # echo "BREAAAAKKKK: "
1016
- # debug(n[ 0])
1017
-
1018
946
result = newNodeI(nkStmtList, n.info)
1019
947
result .add(ctx.newNullifyCurExcLevel(n.info))
1020
948
@@ -1027,8 +955,6 @@ proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1027
955
finallyChain.add(ctx.finallyPathStack[i].enterLabel)
1028
956
1029
957
if finallyChain.len > 0:
1030
- # assert(finallyChain.len > 0)
1031
-
1032
958
# Add proc exit state
1033
959
finallyChain.add(ctx.g.newIntLit(n.info, 0))
1034
960
@@ -1043,14 +969,13 @@ proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1043
969
else :
1044
970
result .add(n)
1045
971
1046
- # echo "RETUUUURNM: ", result
1047
-
1048
972
proc transformBreaksContinuesAndReturns(ctx: var Ctx, n: PNode): PNode =
1049
973
result = n
1050
974
case n.kind
1051
975
of nkSkip: discard
1052
976
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" )
1054
979
of nkReturnStmt:
1055
980
if ctx.curFinallyLevel > 0 and nfNoRewrite notin n.flags:
1056
981
result = ctx.transformReturnStmt(n)
@@ -1086,9 +1011,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
1086
1011
1087
1012
of nkYieldStmt:
1088
1013
result = addGotoOut(result , gotoOut)
1089
- # result = newNodeI(nkStmtList, n.info)
1090
- # result.add(n)
1091
- # result.add(gotoOut)
1092
1014
1093
1015
of nkElse, nkElseExpr:
1094
1016
result [0 ] = addGotoOut(result [0 ], gotoOut)
@@ -1159,10 +1081,8 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
1159
1081
result .add(tryLabel)
1160
1082
var tryBody = toStmtList(n[0 ])
1161
1083
1162
- var exceptBody = ctx.collectExceptState(n)
1084
+ let exceptBody = ctx.collectExceptState(n)
1163
1085
var finallyBody = ctx.getFinallyNode(n)
1164
- # var finallyBody = newTree(nkStmtList, getFinallyNode(ctx, n))
1165
- # finallyBody = ctx.transformReturnsInTry(finallyBody)
1166
1086
var exceptLabel, finallyLabel = ctx.g.emptyNode
1167
1087
1168
1088
if exceptBody.kind != nkEmpty:
@@ -1187,17 +1107,14 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
1187
1107
1188
1108
tryOut = newNodeI(nkGotoState, finallyBody.info)
1189
1109
tryOut.add(finallyLabel)
1190
- # let outToFinally = newNodeI(nkGotoState, finallyBody.info)
1191
- # outToFinally.add(finallyLabel)
1192
1110
1193
1111
block : # Process the states
1194
1112
let oldExcLandingState = ctx.curExcLandingState
1195
1113
ctx.curExcLandingState = if exceptBody.kind != nkEmpty: exceptLabel
1196
1114
elif finallyBody.kind != nkEmpty: finallyLabel
1197
1115
else : oldExcLandingState
1198
- # ctx.curExcHandlingState = exceptIdx
1116
+
1199
1117
discard ctx.newState(tryBody, false , tryLabel)
1200
- # assert(realTryIdx.intVal == tryIdx)
1201
1118
1202
1119
if finallyBody.kind != nkEmpty:
1203
1120
inc ctx.curFinallyLevel
@@ -1210,7 +1127,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
1210
1127
ctx.curExcLandingState = if finallyBody.kind != nkEmpty: finallyLabel
1211
1128
else : oldExcLandingState
1212
1129
discard ctx.newState(exceptBody, false , exceptLabel)
1213
- # assert(realExceptIdx.intVal == -exceptIdx)
1214
1130
1215
1131
let normalOut = if finallyBody.kind != nkEmpty: gotoOut else : nil
1216
1132
ctx.addElseToExcept(exceptBody, normalOut)
@@ -1228,32 +1144,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
1228
1144
internalError(ctx.g.config, " transformClosureIteratorBody != finallyBody" )
1229
1145
dec ctx.curFinallyLevel
1230
1146
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
-
1257
1147
of nkGotoState, nkForStmt:
1258
1148
internalError(ctx.g.config, " closure iter " & $ n.kind)
1259
1149
@@ -1263,7 +1153,6 @@ proc transformClosureIteratorBody(ctx: var Ctx, n: PNode, gotoOut: PNode): PNode
1263
1153
1264
1154
proc stateFromGotoState(n: PNode): PNode =
1265
1155
assert(n.kind == nkGotoState)
1266
- # result = n[0].intVal.int
1267
1156
result = n[0 ]
1268
1157
1269
1158
proc transformStateAssignments(ctx: var Ctx, n: PNode): PNode =
@@ -1330,47 +1219,6 @@ proc transformStateAssignments(ctx: var Ctx, n: PNode): PNode =
1330
1219
for i in 0 ..< n.len:
1331
1220
n[i] = ctx.transformStateAssignments(n[i])
1332
1221
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
-
1374
1222
proc createExceptionTable(ctx: var Ctx): PNode {.inline.} =
1375
1223
let typ = ctx.g.newArrayType(ctx.g.newIntLit(ctx.fn.info, ctx.states.high),
1376
1224
ctx.g.getSysType(ctx.fn.info, tyInt16), ctx.idgen, ctx.fn)
@@ -1382,14 +1230,10 @@ proc createExceptionTable(ctx: var Ctx): PNode {.inline.} =
1382
1230
result .add(elem)
1383
1231
1384
1232
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
1393
1237
result = newNodeI(nkStmtList, info)
1394
1238
1395
1239
let intTyp = ctx.g.getSysType(info, tyInt)
@@ -1417,50 +1261,19 @@ proc newCatchBody(ctx: var Ctx, info: TLineInfo): PNode {.inline.} =
1417
1261
let ifStmt = newTree(nkIfStmt, ifBranch)
1418
1262
result .add(ifStmt)
1419
1263
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
-
1454
1264
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)
1462
1276
1463
- # let tryBody = newTree(nkStmtList, setupExc, n)
1464
1277
let tryBody = newTree(nkStmtList, n)
1465
1278
let exceptBody = ctx.newCatchBody(ctx.fn.info)
1466
1279
let exceptBranch = newTree(nkExceptBranch, exceptBody)
@@ -1474,8 +1287,6 @@ proc wrapIntoTryExcept(ctx: var Ctx, n: PNode): PNode {.inline.} =
1474
1287
result .add newTree(nkCall, newSymNode(ctx.g.getCompilerProc(" pushCurrentException" )), ctx.newTempVarAccess(tempExc))
1475
1288
result .add ctx.newChangeCurExcLevel(n.info, 1 )
1476
1289
1477
- # result = newTree(nkTryStmt, tryBody, exceptBranch)
1478
-
1479
1290
proc wrapIntoStateLoop(ctx: var Ctx, n: PNode): PNode =
1480
1291
# while true:
1481
1292
# block :stateLoop:
0 commit comments