159
159
fn: PSym
160
160
tmpResultSym: PSym # Used when we return, but finally has to interfere
161
161
finallyPathSym: PSym
162
- curExcSym : PSym # Current exception
162
+ curExcLevelSym : PSym # Current exception
163
163
164
164
# states: seq[tuple[label: PNode, body: PNode, inlinable: bool]] # The resulting states. Label is int literal.
165
165
states: seq [State] # The resulting states. Label is int literal.
@@ -246,10 +246,10 @@ proc newFinallyPathAssign(ctx: var Ctx, level: int, label: PNode, info: TLineInf
246
246
let fp = newFinallyPathAccess(ctx, level, info)
247
247
result = newTree(nkAsgn, fp, label)
248
248
249
- proc newCurExcAccess (ctx: var Ctx): PNode =
250
- if ctx.curExcSym .isNil:
251
- ctx.curExcSym = ctx.newEnvVar(" :curExc " , ctx.g.callCodegenProc( " getCurrentException " ).typ )
252
- ctx.newEnvVarAccess(ctx.curExcSym )
249
+ proc newCurExcLevelAccess (ctx: var Ctx): PNode =
250
+ if ctx.curExcLevelSym .isNil:
251
+ ctx.curExcLevelSym = ctx.newEnvVar(" :curExcLevel " , ctx.g.getSysType(ctx.fn.info, tyInt16) )
252
+ ctx.newEnvVarAccess(ctx.curExcLevelSym )
253
253
254
254
proc newStateLabel(ctx: Ctx): PNode =
255
255
ctx.g.newIntLit(TLineInfo(), 0 )
@@ -310,13 +310,24 @@ proc hasYields(n: PNode): bool =
310
310
result = true
311
311
break
312
312
313
- proc newNullifyCurExc (ctx: var Ctx, info: TLineInfo): PNode =
314
- # :curEcx = nil
315
- let curExc = ctx.newCurExcAccess ()
313
+ proc newNullifyCurExcLevel (ctx: var Ctx, info: TLineInfo, decrement = false ): PNode =
314
+ # :curEcx = 0
315
+ let curExc = ctx.newCurExcLevelAccess ()
316
316
curExc.info = info
317
- let nilnode = newNodeIT(nkNilLit, info, curExc.typ )
317
+ let nilnode = ctx.g.newIntLit( info, 0 )
318
318
result = newTree(nkAsgn, curExc, nilnode)
319
319
320
+ proc newChangeCurExcLevel(ctx: var Ctx, info: TLineInfo, by: int ): PNode =
321
+ # inc :curEcx
322
+ let curExc = ctx.newCurExcLevelAccess()
323
+ curExc.info = info
324
+ # let nilnode = newNodeIT(nkNilLit, info, curExc.typ)
325
+ # result = newTree(nkAsgn, curExc, nilnode)
326
+
327
+ result = newTreeIT(nkCall, info, ctx.g.getSysType(info, tyVoid),
328
+ newSymNode(ctx.g.getSysMagic(info, " inc" , mInc)), curExc,
329
+ ctx.g.newIntLit(info, by))
330
+
320
331
proc newOr(g: ModuleGraph, a, b: PNode): PNode {.inline.} =
321
332
result = newTreeIT(nkCall, a.info, g.getSysType(a.info, tyBool),
322
333
newSymNode(g.getSysMagic(a.info, " or" , mOr)), a, b)
@@ -389,7 +400,7 @@ proc addElseToExcept(ctx: var Ctx, n, gotoOut: PNode) =
389
400
390
401
n.add newTree(nkCall,
391
402
newSymNode(ctx.g.getCompilerProc(" popCurrentException" )))
392
- n.add(ctx.newNullifyCurExc (n.info))
403
+ n.add(ctx.newChangeCurExcLevel (n.info, - 1 ))
393
404
if gotoOut != nil :
394
405
n.add(ctx.newFinallyPathAssign(ctx.curFinallyLevel - 1 , gotoOut[0 ], n.info))
395
406
@@ -891,13 +902,13 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
891
902
nextState,
892
903
ctx.g.newIntLit(info, 0 ))
893
904
894
- let curExc = ctx.newCurExcAccess ()
895
- let nilnode = newNodeIT(nkNilLit, info, curExc.typ)
905
+ let curExc = ctx.newCurExcLevelAccess ()
906
+ # let nilnode = newNodeIT(nkNilLit, info, curExc.typ)
896
907
let excNilCmp = newTreeIT(nkCall,
897
908
info, ctx.g.getSysType(info, tyBool),
898
- newSymNode(ctx.g.getSysMagic(info, " ==" , mEqRef ), info),
909
+ newSymNode(ctx.g.getSysMagic(info, " ==" , mEqI ), info),
899
910
curExc,
900
- nilnode )
911
+ ctx.g.newIntLit(info, 0 ) )
901
912
902
913
let retStmt =
903
914
block :
@@ -917,7 +928,7 @@ proc newEndFinallyNode(ctx: var Ctx, info: TLineInfo): PNode =
917
928
newTree(nkElse,
918
929
newTree(nkStmtList,
919
930
# newTreeI(nkCall, info, newSymNode(ctx.g.getCompilerProc("closureIterSetupExc")), nilnode),
920
- newTreeI(nkRaiseStmt, info, curExc ))))
931
+ newTreeI(nkRaiseStmt, info, ctx.g.emptyNode ))))
921
932
922
933
result = newTree(nkIfStmt,
923
934
newTreeI(nkElifBranch, info, cmpStateToZero, ifBody),
@@ -1000,7 +1011,7 @@ proc transformReturnStmt(ctx: var Ctx, n: PNode): PNode =
1000
1011
# debug(n[ 0])
1001
1012
1002
1013
result = newNodeI(nkStmtList, n.info)
1003
- result .add(ctx.newNullifyCurExc (n.info))
1014
+ result .add(ctx.newNullifyCurExcLevel (n.info))
1004
1015
1005
1016
var finallyChain = newSeq[PNode]()
1006
1017
@@ -1428,10 +1439,10 @@ proc newCatchBody(ctx: var Ctx, info: TLineInfo): PNode {.inline.} =
1428
1439
# result.add(ifStmt)
1429
1440
1430
1441
# :curExc = getCurrentException()
1431
- block :
1432
- result .add(newTree(nkAsgn,
1433
- ctx.newCurExcAccess(),
1434
- ctx.g.callCodegenProc(" getCurrentException" )))
1442
+ # block:
1443
+ # result.add(newTree(nkAsgn,
1444
+ # ctx.newCurExcAccess(),
1445
+ # ctx.g.callCodegenProc("getCurrentException")))
1435
1446
1436
1447
proc wrapIntoTryExcept(ctx: var Ctx, n: PNode): PNode {.inline.} =
1437
1448
# let setupExc = newTree(nkCall,
@@ -1454,6 +1465,8 @@ proc wrapIntoTryExcept(ctx: var Ctx, n: PNode): PNode {.inline.} =
1454
1465
exceptBody.add ctx.newTempVarAsgn(tempExc, getCurExc)
1455
1466
1456
1467
result .add newTree(nkCall, newSymNode(ctx.g.getCompilerProc(" pushCurrentException" )), ctx.newTempVarAccess(tempExc))
1468
+ result .add ctx.newChangeCurExcLevel(n.info, 1 )
1469
+
1457
1470
# result = newTree(nkTryStmt, tryBody, exceptBranch)
1458
1471
1459
1472
proc wrapIntoStateLoop(ctx: var Ctx, n: PNode): PNode =
@@ -1648,7 +1661,7 @@ proc transformClosureIterator*(g: ModuleGraph; idgen: IdGenerator; fn: PSym, n:
1648
1661
result = wrapIntoStateLoop(ctx, caseDispatcher)
1649
1662
result = liftLocals(ctx, result )
1650
1663
1651
- when true :
1664
+ when false :
1652
1665
echo " TRANSFORM TO STATES: "
1653
1666
echo renderTree(result )
1654
1667
0 commit comments