when nimvm stmt proof of concept

This commit is contained in:
Yuriy Glukhov
2015-09-03 19:20:23 +03:00
parent 178275f494
commit 49869a2b10
6 changed files with 43 additions and 13 deletions

View File

@@ -2069,6 +2069,9 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
of nkStmtList:
for i in countup(0, sonsLen(n) - 1): genStmts(p, n.sons[i])
of nkIfExpr, nkIfStmt: genIf(p, n, d)
of nkWhen:
# This should be a "when nimvm" node.
expr(p, n.sons[1].sons[0], d)
of nkObjDownConv: downConv(p, n, d)
of nkObjUpConv: upConv(p, n, d)
of nkChckRangeF: genRangeChck(p, n, d, "chckRangeF")

View File

@@ -1687,6 +1687,9 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
gen(p, lastSon(n), r)
of nkBlockStmt, nkBlockExpr: genBlock(p, n, r)
of nkIfStmt, nkIfExpr: genIf(p, n, r)
of nkWhen:
# This is "when nimvm" node
gen(p, n.sons[1].sons[0], r)
of nkWhileStmt: genWhileStmt(p, n)
of nkVarSection, nkLetSection: genVarStmt(p, n)
of nkConstSection: discard

View File

@@ -1764,22 +1764,39 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
if semCheck: result = semStmt(c, e) # do not open a new scope!
else: result = e
# Check if the node is "when nimvm"
# when nimvm:
# ...
# else:
# ...
let whenNimvm = n.sons.len == 2 and n.sons[0].kind == nkElifBranch and
n.sons[1].kind == nkElse and n.sons[0].sons[0].kind == nkIdent and
lookUp(c, n.sons[0].sons[0]).magic == mNimvm
for i in countup(0, sonsLen(n) - 1):
var it = n.sons[i]
case it.kind
of nkElifBranch, nkElifExpr:
checkSonsLen(it, 2)
var e = semConstExpr(c, it.sons[0])
if e.kind != nkIntLit:
# can happen for cascading errors, assume false
# InternalError(n.info, "semWhen")
discard
elif e.intVal != 0 and result == nil:
setResult(it.sons[1])
if whenNimvm:
if semCheck:
it.sons[1] = semStmt(c, it.sons[1])
result = n # when nimvm is not elimited until codegen
else:
var e = semConstExpr(c, it.sons[0])
if e.kind != nkIntLit:
# can happen for cascading errors, assume false
# InternalError(n.info, "semWhen")
discard
elif e.intVal != 0 and result == nil:
setResult(it.sons[1])
of nkElse, nkElseExpr:
checkSonsLen(it, 1)
if result == nil:
setResult(it.sons[0])
if result == nil or whenNimvm:
if semCheck:
it.sons[0] = semStmt(c, it.sons[0])
if result == nil:
result = it.sons[0]
else: illFormedAst(n)
if result == nil:
result = newNodeI(nkEmpty, n.info)
@@ -2162,7 +2179,11 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
result = semWhen(c, n, true)
else:
result = semWhen(c, n, false)
result = semExpr(c, result, flags)
if result == n:
# This is a "when nimvm" stmt.
result = semWhen(c, n, true)
else:
result = semExpr(c, result, flags)
of nkBracketExpr:
checkMinSonsLen(n, 1)
var s = qualifiedLookUp(c, n.sons[0], {checkUndeclared})

View File

@@ -363,7 +363,7 @@ proc analyse(c: var AnalysisCtx; n: PNode) =
else: internalError(it.info, "slot already has a lower bound")
if not isSpawned: analyse(c, value)
of nkCaseStmt: analyseCase(c, n)
of nkIfStmt, nkIfExpr: analyseIf(c, n)
of nkWhen, nkIfStmt, nkIfExpr: analyseIf(c, n)
of nkWhileStmt:
analyse(c, n.sons[0])
# 'while true' loop?

View File

@@ -735,7 +735,7 @@ proc track(tracked: PEffects, n: PNode) =
# since 'var (a, b): T = ()' is not even allowed, there is always type
# inference for (a, b) and thus no nil checking is necessary.
of nkCaseStmt: trackCase(tracked, n)
of nkIfStmt, nkIfExpr: trackIf(tracked, n)
of nkWhen, nkIfStmt, nkIfExpr: trackIf(tracked, n)
of nkBlockStmt, nkBlockExpr: trackBlock(tracked, n.sons[1])
of nkWhileStmt:
track(tracked, n.sons[0])

View File

@@ -1664,7 +1664,10 @@ proc gen(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}) =
of nkBracketExpr: genArrAccess(c, n, dest, flags)
of nkDerefExpr, nkHiddenDeref: genAddrDeref(c, n, dest, opcLdDeref, flags)
of nkAddr, nkHiddenAddr: genAddrDeref(c, n, dest, opcAddrNode, flags)
of nkWhenStmt, nkIfStmt, nkIfExpr: genIf(c, n, dest)
of nkIfStmt, nkIfExpr: genIf(c, n, dest)
of nkWhenStmt:
# This is "when nimvm" node. Chose the first branch.
gen(c, n.sons[0].sons[1], dest)
of nkCaseStmt: genCase(c, n, dest)
of nkWhileStmt:
unused(n, dest)