Skip to content
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

experimental cps detection via scope injection #316

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions cps.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# we only support panics because we don't want to run finally on defect
when not defined(nimPanics):
{.warning: "cps supports --panics:on only; " &

Check warning on line 22 in cps.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nimskull *)

cps supports --panics:on only; see https://github.com/nim-works/cps/issues/110 [User]
" see https://github.com/nim-works/cps/issues/110".}

when (NimMajor, NimMinor, NimPatch) < (1, 7, 3):
Expand Down Expand Up @@ -76,19 +76,29 @@
else:
result = getAst(cpsTransform(tipe, n))

macro cps*(tipe: typed, n: untyped): untyped =
macro cpsMarkExtract(n: untyped): untyped =
let cpsExtract = bindSym"cpsExtract"
result = nnkPragmaBlock.newTree(
nnkPragma.newTree(cpsExtract),
n
)

template cps*(tipe: typed, n: untyped): untyped =
## When applied to a procedure, rewrites the procedure into a
## continuation form. When applied to a procedure type definition,
## rewrites the type into a callback form.
result = n
bind cpsTyped
bind cpsScalpel
bind cpsMarkExtract

when not defined(nimdoc):
# add the application of the typed transformation pass
n.addPragma:
nnkExprColonExpr.newTree(bindSym"cpsTyped", tipe)
# let the untyped pass do what it will with this input
# XXX: currently disabled because it's a slipperly slope of regret
#result = performUntypedPass(T, n)
result = n
cpsScalpel:
const isInCps {.inject.} = true

cpsMarkExtract:
cpsTyped(tipe, n)
else:
n

proc adaptArguments(sym: NormNode; args: seq[NormNode]): seq[NormNode] =
## convert any arguments in the list as necessary to match those of
Expand Down Expand Up @@ -232,7 +242,7 @@
let tipe = getTypeInst c
if not tipe.isNilOrVoid:
# assign the continuation to a variable to prevent re-evaluation
let continuation {.inject.} = nskLet.genSym"continuation"

Check failure on line 245 in cps.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nimskull *)

inconsistent typing for reintroduced symbol 'continuation': previous type was: cps:filter() env; new type is: cps:filter() env
result.add:
# assign the input to a variable that can be repeated evaluated
nnkLetSection.newTree:
Expand Down
2 changes: 1 addition & 1 deletion cps/normalizedast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ defineToNimNodeConverter(
allowAutoDowngradeNormalizedNode(
Name, TypeExpr, Call, Conv, PragmaStmt, PragmaAtom, IdentDef, RoutineDef,
ProcDef, FormalParams, RoutineParam, VarSection, LetSection, VarLet,
VarLetIdentDef, VarLetTuple, DefVarLet, IdentDefLet, Sym
VarLetIdentDef, VarLetTuple, DefVarLet, IdentDefLet, Sym, PragmaBlock
)

# types that go from a specific type to a less specific type, "downgrade"
Expand Down
1 change: 1 addition & 0 deletions cps/spec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
true

import cps/[rewrites, help, normalizedast]
export errorAst, desym, isEmpty, genField

Check warning on line 25 in cps/spec.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nim devel)

pending https://github.com/nim-lang/Nim/issues/17851; genField is deprecated [Deprecated]

Check warning on line 25 in cps/spec.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nimskull *)

pending https://github.com/nim-lang/Nim/issues/17851; genField is deprecated [Deprecated]

template cpsLift*() {.pragma.} ## lift this proc|type
template cpsCall*() {.pragma.} ## a cps call
Expand Down Expand Up @@ -50,6 +50,7 @@
template cpsHasException*(cont, ex: typed) {.pragma.} ##
## the continuation has an exception stored in `ex`, with `cont` being the
## continuation symbol used.
template cpsExtract*() {.pragma.} ## extract this block from the scope

const
cpsStackFrames* {.booldefine, used.} = compileOption"stacktrace"
Expand Down Expand Up @@ -101,7 +102,7 @@
proc `=copy`(dest: var ContinuationObj; src: ContinuationObj) {.error.} =
discard

proc `=destroy`(dest: var ContinuationObj) =

Check warning on line 105 in cps/spec.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nim devel)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
for key, value in dest.fieldPairs:
reset value

Expand Down
7 changes: 7 additions & 0 deletions cps/transform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,13 @@
result = floater:
copyNimTree n

macro cpsScalpel*(n: typed): untyped =
## returns the first `{.cpsExtract.}` block from `n`
debugAnnotation cpsScalpel, n:
it = it.findChildRecursive() do (n: NormNode) -> bool:
n.kind == nnkPragmaBlock and n.asPragmaBlock().hasPragma("cpsExtract")
it = it.last

macro cpsManageException(name: static[string]; n: typed): untyped =
## rewrites all continuations in `n` containing an exception so that exception
## become the "current" exception of that continuation while preserving the
Expand Down Expand Up @@ -1080,7 +1087,7 @@
## rewrites all continuations in `n` so that any unhandled exception will
## be first copied into the `ex` variable, then raise

{.warning: "compiler workaround here, see: https://github.com/nim-lang/Nim/issues/18352".}

Check warning on line 1090 in cps/transform.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nim devel)

compiler workaround here, see: https://github.com/nim-lang/Nim/issues/18352 [User]

Check warning on line 1090 in cps/transform.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nimskull *)

compiler workaround here, see: https://github.com/nim-lang/Nim/issues/18352 [User]
let contType = contType

proc handle(n: NormNode): NormNode =
Expand Down Expand Up @@ -1205,7 +1212,7 @@
n.pragma.add bindName"cpsCont"

# run other stages
{.warning: "compiler bug workaround, see: https://github.com/nim-lang/Nim/issues/18349".}

Check warning on line 1215 in cps/transform.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nim devel)

compiler bug workaround, see: https://github.com/nim-lang/Nim/issues/18349 [User]

Check warning on line 1215 in cps/transform.nim

View workflow job for this annotation

GitHub Actions / ubuntu-latest (nimskull *)

compiler bug workaround, see: https://github.com/nim-lang/Nim/issues/18349 [User]
let processMainContinuation =
newCall(bindSym"cpsFloater"):
newCall(bindSym"cpsResolver", NimNode env.identity, NimNode env.root):
Expand Down
Loading