@@ -43,7 +43,7 @@ object HandlerLowering:
43
43
e
44
44
case Some (v) => v
45
45
46
- private case class LinkState (res : Local , cls : Path , uid : StateId , doUnwind : Opt [ LazyVal [ Path ]] )
46
+ private case class LinkState (res : Local , cls : Path , uid : Path )
47
47
48
48
type FnOrCls = Either [BlockMemberSymbol , MemberSymbol [? <: ClassLikeDef ] & InnerSymbol ]
49
49
@@ -78,6 +78,9 @@ object HandlerLowering:
78
78
def topLevel (debugNme : Str ) = DebugInfo (debugNme, Set .empty, N )
79
79
80
80
type StateId = BigInt
81
+
82
+ // TODO: move somewhere else, not sure where
83
+ def simpleParam (sym : VarSymbol ) = Param (FldFlags .empty, sym, N , Modulefulness .none)
81
84
82
85
import HandlerLowering .*
83
86
@@ -98,18 +101,24 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
98
101
99
102
private def funcLikeHandlerCtx (ctorThis : Option [Path ], isHandlerMtd : Bool , contNme : Str , debugNme : Str )(using h : HandlerCtx ) =
100
103
HandlerCtx (false , false , contNme, ctorThis, h.debugInfo.copy(debugNme), state =>
101
- if state.doUnwind.isEmpty then
102
- lastWords(" doUnwind function does not exist for this function" )
103
- Return (Call (
104
- state.doUnwind.get.get,
105
- state.res.asPath.asArg :: Value .Lit (Tree .IntLit (state.uid)).asArg :: Nil
106
- )(true , false ), false )
107
- )
104
+ blockBuilder
105
+ .assignFieldN(state.res.asPath.contTrace.last, nextIdent, Instantiate (
106
+ state.cls.selN(Tree .Ident (" class" )),
107
+ state.uid :: Nil ))
108
+ .assignFieldN(state.res.asPath.contTrace, lastIdent, state.res.asPath.contTrace.last.next)
109
+ .ret(state.res.asPath))
108
110
private def functionHandlerCtx (nme : Str , debugNme : Str )(using HandlerCtx ) = funcLikeHandlerCtx(N , false , nme, debugNme)
109
- private def topLevelCtx (nme : Str , debugNme : Str ) = HandlerCtx (true , false , nme, N , DebugInfo .topLevel(debugNme), state => Assign (
110
- state.res,
111
- Call (paths.topLevelEffectPath, state.res.asPath.asArg :: Value .Lit (Tree .BoolLit (opt.debug)).asArg :: Nil )(true , false ),
112
- End ()))
111
+ private def topLevelCall (state : LinkState ) = Call (
112
+ paths.topLevelEffectPath,
113
+ state.res.asPath.asArg :: Value .Lit (Tree .BoolLit (opt.debug)).asArg :: Nil
114
+ )(true , false )
115
+ private def topLevelCtx (nme : Str , debugNme : Str ) = HandlerCtx (
116
+ true , false , nme, N , DebugInfo .topLevel(debugNme),
117
+ state => Assign (
118
+ state.res,
119
+ topLevelCall(state),
120
+ End ())
121
+ )
113
122
private def ctorCtx (ctorThis : Path , nme : Str , debugNme : Str )(using HandlerCtx ) = funcLikeHandlerCtx(S (ctorThis), false , nme, debugNme)
114
123
private def handlerMtdCtx (nme : Str , debugNme : Str )(using HandlerCtx ) = funcLikeHandlerCtx(N , true , nme, debugNme)
115
124
private def handlerCtx (using HandlerCtx ): HandlerCtx = summon
@@ -258,8 +267,6 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
258
267
val rewrittenEntry = rewriteState(entryState)
259
268
val rewrittenStates = states.map(rewriteState)
260
269
261
- println(finalDests)
262
-
263
270
(rewrittenEntry, rewrittenStates)
264
271
265
272
def partitionBlock (blk : Block , inclEntryPoint : Bool , labelIds : Map [Symbol , (StateId , StateId )] = Map .empty): Ls [BlockState ] =
@@ -507,17 +514,9 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
507
514
doUnwindMap += fnOrCls -> doUnwindSym.asPath
508
515
val pcSym = VarSymbol (Tree .Ident (" pc" ))
509
516
val resSym = VarSymbol (Tree .Ident (" res" ))
510
- val doUnwindBlk = blockBuilder
511
- .assignFieldN(
512
- resSym.asPath.contTrace.last, nextIdent,
513
- Instantiate (
514
- cls.sym.asPath.selN(Tree .Ident (" class" )),
515
- pcSym.asPath :: Nil
516
- )
517
- )
518
- .assignFieldN(resSym.asPath.contTrace, lastIdent, resSym.asPath.contTrace.last.next)
519
- .ret(resSym.asPath)
520
-
517
+ val doUnwindBlk = h.linkAndHandle(
518
+ LinkState (resSym, cls.sym.asPath, pcSym.asPath)
519
+ )
521
520
def simpleParam (sym : VarSymbol ) = Param (FldFlags .empty, sym, N , Modulefulness .none)
522
521
val doUnwindDef = FunDefn (
523
522
N , doUnwindSym,
@@ -576,11 +575,6 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
576
575
val lbl = freshTmp(" handlerBody" )
577
576
val lblLoop = freshTmp(" handlerLoop" )
578
577
579
- val handlerBody = translateBlock(h.body, Set .empty, L (sym), HandlerCtx (false , true ,
580
- s " Cont $$ handleBlock $$ ${symToStr(h.lhs)}$$ " , N , handlerCtx.debugInfo.copy(debugNme = s " ‹handler body of ${h.lhs.nme}› " ), state => blockBuilder
581
- .assignFieldN(state.res.asPath.contTrace.last, nextIdent, PureCall (state.cls, Value .Lit (Tree .IntLit (state.uid)) :: Nil ))
582
- .ret(PureCall (paths.handleBlockImplPath, state.res.asPath :: h.lhs.asPath :: Nil ))))
583
-
584
578
val handlerMtds = h.handlers.map: handler =>
585
579
val sym = BlockMemberSymbol (" hdlrFun" , Nil , true )
586
580
val mtdBdy = translateBlock(handler.body,
@@ -605,16 +599,30 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
605
599
// NOTE: the super call is inside the preCtor
606
600
// during resumption we need to resume both the this.x = x bindings done in JSBuilder and the ctor
607
601
608
- val body = blockBuilder
609
- .define(clsDefn)
610
- .assign(h.lhs, Instantiate (Value .Ref (clsDefn.sym), Nil ))
611
- .rest(handlerBody)
602
+ val handlerBody = translateBlock(
603
+ h.body, Set .empty, L (sym),
604
+ HandlerCtx (
605
+ false , true ,
606
+ s " Cont $$ handleBlock $$ ${symToStr(h.lhs)}$$ " , N ,
607
+ handlerCtx.debugInfo.copy(debugNme = s " ‹handler body of ${h.lhs.nme}› " ),
608
+ state => blockBuilder
609
+ .assignFieldN(state.res.asPath.contTrace.last, nextIdent, PureCall (state.cls, state.uid :: Nil ))
610
+ .ret(PureCall (paths.handleBlockImplPath, state.res.asPath :: h.lhs.asPath :: Nil ))
611
+ )
612
+ )
612
613
613
614
val defn = FunDefn (
614
615
N , // no owner
615
- sym, PlainParamList (Nil ) :: Nil , body )
616
+ sym, PlainParamList (Nil ) :: Nil , handlerBody )
616
617
617
- val result = Define (defn, ResultPlaceholder (h.res, freshId(), Call (sym.asPath, Nil )(true , true ), h.rest))
618
+ // moved all defns outside
619
+ val result = blockBuilder
620
+ .define(defn)
621
+ .define(clsDefn)
622
+ .assign(h.lhs, Instantiate (Value .Ref (clsDefn.sym), Nil ))
623
+ .rest(
624
+ ResultPlaceholder (h.res, freshId(), Call (sym.asPath, Nil )(true , true ), h.rest)
625
+ )
618
626
result
619
627
620
628
private def genContClass (b : Block )(using h : HandlerCtx ): Opt [ClsLikeDefn ] =
@@ -644,7 +652,6 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
644
652
.assignFieldN(resSym.asPath.contTrace.last, nextIdent, clsSym.asPath)
645
653
.assignFieldN(resSym.asPath.contTrace, lastIdent, clsSym.asPath)
646
654
.ret(resSym.asPath)
647
- def simpleParam (sym : VarSymbol ) = Param (FldFlags .empty, sym, N , Modulefulness .none)
648
655
val doUnwindDef = FunDefn (
649
656
S (clsSym), doUnwindSym,
650
657
PlainParamList (simpleParam(resSym) :: simpleParam(newPcSym) :: Nil ) :: Nil ,
@@ -823,13 +830,16 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
823
830
private def genNormalBody (b : Block , clsSym : BlockMemberSymbol , doUnwind : Opt [LazyVal [Path ]])(using HandlerCtx ): Block =
824
831
val transform = new BlockTransformerShallow (SymbolSubst ()):
825
832
override def applyBlock (b : Block ): Block = b match
826
- case ResultPlaceholder (res, uid, c, rest) =>
833
+ case ResultPlaceholder (res, uid, c, rest) =>
834
+ val (doUnwindCall, implct) = doUnwind match
835
+ case None => (topLevelCall(LinkState (res, clsSym.asPath, Value .Lit (Tree .IntLit (uid)))), true )
836
+ case Some (doUnwind) => (PureCall (doUnwind.get, res.asPath :: Value .Lit (Tree .IntLit (uid)) :: Nil ), false )
827
837
blockBuilder
828
838
.assign(res, c)
829
839
.ifthen(
830
840
res.asPath,
831
841
Case .Cls (paths.effectSigSym, paths.effectSigPath),
832
- handlerCtx.linkAndHandle( LinkState (res, clsSym.asPath, uid, doUnwind) )
842
+ Return (doUnwindCall, implct )
833
843
)
834
844
.rest(applyBlock(rest))
835
845
case _ => super .applyBlock(b)
0 commit comments