-
Notifications
You must be signed in to change notification settings - Fork 39
add delimited continuations #1530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
zerbina
wants to merge
8
commits into
nim-works:devel
Choose a base branch
from
zerbina:delimited-continuations
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2e032aa
manual: first version of a specification
zerbina 25747c7
system: add the `suspend` magic
zerbina 960df89
sem: implement the first version of `suspend` analysis
zerbina 07548f0
tailcall_analysis: allow tail calls in `suspend`
zerbina 3d87dda
mir: add `mnkFork` and `mnkLand`
zerbina 09cf602
mirgen: implement a first version of the `suspend` lowering
zerbina 648effc
document the planned lowering approach
zerbina f60b704
add a small demo
zerbina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## Implements the lowering of `fork`/`land` pairs. For every static `fork`, the | ||
## continuation of the fork (i.e., the code following the land) is reified | ||
## into a standalone procedure -- the context (i.e., all active locals) are | ||
## saved into a separate context object, which must be passed to the reified | ||
## procedure. | ||
## | ||
## A rough summary of the reification process is that the procedure body is | ||
## duplicated and all basic-blocks not reachable from the resumption point | ||
## are removed. | ||
## | ||
## Forks in loops make this a bit trickier in practice. Consider: | ||
## | ||
## loop: | ||
## def _1 = ... | ||
## if cond: | ||
## goto L2 | ||
## fork a, b, L1 | ||
## ... | ||
## land L1: | ||
## ... | ||
## L2: | ||
## ... | ||
## | ||
## Here, the loop must also be part of the reified procedure, but a naive | ||
## removal of all unused basic blocks would leave it at the start, which is | ||
## wrong. | ||
## | ||
## The solution is to split the continuation into multiple subroutines, one | ||
## for each such problematic join point. | ||
## | ||
## There are two possible strategies to implement subroutines: | ||
## 1. via separate procedure that tail call each other, passing along extra | ||
## locals crossing the border as parameter | ||
## 2. or, use a case statement dispatcher in a loop, with each target | ||
## corresponding to a subroutine. Invoking a subroutine means changing the | ||
## selector value and jumping back to the loop start -- local variables | ||
## living across subroutine boundaries are lifted to the top-level scope | ||
## | ||
## Number 2 is chosen because it's slightly simpler to implement and doesn't | ||
## put pressure on the available tailcall argument sizes (and thus the size | ||
## of the context object). | ||
## | ||
## A reified continuation is created for each fork/land pair. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -410,6 +410,100 @@ proc semPrivateAccess(c: PContext, n: PNode): PNode = | |||||
c.currentScope.allowPrivateAccess.add t.sym | ||||||
result = newNodeIT(nkEmpty, n.info, getSysType(c.graph, n.info, tyVoid)) | ||||||
|
||||||
proc semSuspend(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode = | ||||||
## Analyzes a 'suspend' magic call, producing a typed AST or an error. If | ||||||
## the call doesn't have the right shape, analysis fall back to overload | ||||||
## resolution. | ||||||
addInNimDebugUtils(c.config, "semSuspend", n, result) | ||||||
if n.len != 4: | ||||||
# could be some other call | ||||||
return semDirectOp(c, n, flags) | ||||||
|
||||||
result = shallowCopy(n) | ||||||
result[0] = newSymNode(s, n[0].info) | ||||||
result[1] = semExprWithType(c, n[1]) | ||||||
|
||||||
var paramType = result[1].typ | ||||||
if paramType.kind != tyError: | ||||||
if paramType.kind == tyTypeDesc: | ||||||
paramType = paramType.lastSon | ||||||
else: | ||||||
result[1] = c.config.newError(result[1], PAstDiag(kind: adSemTypeExpected)) | ||||||
paramType = result[1].typ | ||||||
|
||||||
let hasResult = paramType.skipTypes({tyAlias}).kind != tyVoid | ||||||
|
||||||
# create an new object for the context. It's populated at a (much) later stage | ||||||
let objSym = newSym(skType, c.cache.getIdent("Ctx"), nextSymId(c.idgen), | ||||||
getCurrOwner(c), n.info) | ||||||
# enable special name mangling: | ||||||
objSym.flags.incl sfFromGeneric | ||||||
|
||||||
let obj = newTypeS(tyObject, c) | ||||||
obj.rawAddSon(nil) # the base type | ||||||
obj.size = szUnknownSize | ||||||
obj.align = szUnknownSize | ||||||
obj.n = newTree(nkRecList) | ||||||
obj.flags.incl tfHasAsgn # the object has custom copy logic | ||||||
objSym.linkTo(obj) | ||||||
|
||||||
proc addParam(prc: PType, name: string, typ: PType, info: TLineInfo, | ||||||
c: PContext) = | ||||||
let p = newSym(skParam, c.cache.getIdent(name), nextSymId(c.idgen), | ||||||
getCurrOwner(c), info) | ||||||
p.typ = typ | ||||||
prc.rawAddSon(typ, propagateHasAsgn=false) | ||||||
prc.n.add newSymNode(p) | ||||||
|
||||||
# create the type of the continuation procedure: | ||||||
let prc = newProcType(n.info, nextTypeId(c.idgen), getCurrOwner(c)) | ||||||
prc.callConv = ccNimCall # TODO: use tailcall | ||||||
# TODO: handle the "unresolved auto return type" case. The easiest solution | ||||||
# is just reporting an error | ||||||
prc[0] = c.p.owner.typ[0] # use the enclosing routine's return type | ||||||
if hasResult: | ||||||
prc.addParam("arg", newTypeWithSons(c, tySink, @[paramType]), n.info, c) | ||||||
prc.addParam("c", newTypeWithSons(c, tySink, @[obj]), n.info, c) | ||||||
|
||||||
# set up the type to use for the local: | ||||||
let tup = newTypeS(tyTuple, c) | ||||||
tup.rawAddSon(obj) | ||||||
tup.rawAddSon(prc) | ||||||
|
||||||
c.openScope() | ||||||
# create a let section and type that. This makes sure the symbol is properly | ||||||
# registered everywhere, and retyping is also taken care. The initializer | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Minor typo. |
||||||
# needs to be some well-formed, non empty expression for the analysis to | ||||||
# succeed -- we use a correctly typed but gramatically incorrect node as | ||||||
# the expression | ||||||
let cons = newNodeIT(nkType, n.info, tup) | ||||||
cons.flags.incl nfSem # prevent the expression from being analyzed | ||||||
|
||||||
let | ||||||
ls = nkLetSection.newTree( | ||||||
nkIdentDefs.newTree(n[2], newNodeIT(nkType, n.info, tup), cons)) | ||||||
tmp = semNormalizedLetOrVar(c, ls, skLet) | ||||||
if tmp.kind == nkError: | ||||||
# place the erroneous identifier node back into the call | ||||||
result[2] = tmp.diag.wrongNode[0][0] | ||||||
else: | ||||||
result[2] = tmp[0][0] | ||||||
|
||||||
var call = semExprWithType(c, n[3]) | ||||||
# TODO: noreturn handling... | ||||||
call = fitNode(c, c.p.owner.typ[0], call, n[3].info) | ||||||
c.closeScope() | ||||||
|
||||||
result[3] = call | ||||||
if hasResult: | ||||||
result.typ = paramType | ||||||
|
||||||
if nkError in {result[1].kind, result[2].kind, result[3].kind}: | ||||||
result = c.config.wrapError(result) | ||||||
elif ecfStatic in c.executionCons[^1].flags: | ||||||
# TODO: report an error | ||||||
discard | ||||||
|
||||||
proc magicsAfterOverloadResolution(c: PContext, n: PNode, | ||||||
flags: TExprFlags): PNode = | ||||||
## This is the preferred code point to implement magics. | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible typo.