C variables are created in their enclosing block instead of their enclosing function
This commit is contained in:
parent
238202ccbc
commit
d2b45dbe85
8 changed files with 180 additions and 151 deletions
|
|
@ -120,6 +120,7 @@ proc genIfStmt(p: BProc, n: PNode) =
|
|||
# elsePart
|
||||
# Lend:
|
||||
#
|
||||
## XXX: push blocks here
|
||||
var
|
||||
a: TLoc
|
||||
Lelse: TLabel
|
||||
|
|
@ -132,12 +133,12 @@ proc genIfStmt(p: BProc, n: PNode) =
|
|||
initLocExpr(p, it.sons[0], a)
|
||||
Lelse = getLabel(p)
|
||||
inc(p.labels)
|
||||
appff(p.s[cpsStmts], "if (!$1) goto $2;$n",
|
||||
appff(p.s(cpsStmts), "if (!$1) goto $2;$n",
|
||||
"br i1 $1, label %LOC$3, label %$2$n" & "LOC$3: $n",
|
||||
[rdLoc(a), Lelse, toRope(p.labels)])
|
||||
genStmts(p, it.sons[1])
|
||||
if sonsLen(n) > 1:
|
||||
appff(p.s[cpsStmts], "goto $1;$n", "br label %$1$n", [Lend])
|
||||
appff(p.s(cpsStmts), "goto $1;$n", "br label %$1$n", [Lend])
|
||||
fixLabel(p, Lelse)
|
||||
of nkElse:
|
||||
genStmts(p, it.sons[0])
|
||||
|
|
@ -169,52 +170,69 @@ proc genReturnStmt(p: BProc, t: PNode) =
|
|||
genLineDir(p, t)
|
||||
if (t.sons[0].kind != nkEmpty): genStmts(p, t.sons[0])
|
||||
blockLeaveActions(p, min(1, p.nestedTryStmts.len))
|
||||
appff(p.s[cpsStmts], "goto BeforeRet;$n", "br label %BeforeRet$n", [])
|
||||
appff(p.s(cpsStmts), "goto BeforeRet;$n", "br label %BeforeRet$n", [])
|
||||
|
||||
proc genWhileStmt(p: BProc, t: PNode) =
|
||||
proc startBlock(p: BProc, start = "{$n"): int =
|
||||
inc(p.labels)
|
||||
result = len(p.blocks)
|
||||
setlen(p.blocks, result + 1)
|
||||
p.blocks[result].id = p.labels
|
||||
p.blocks[result].nestedTryStmts = p.nestedTryStmts.len
|
||||
appf(p.s(cpsStmts), start)
|
||||
|
||||
proc assignLabel(b: var TBlock): PRope {.inline.} =
|
||||
b.label = con("LA", b.id.toRope)
|
||||
result = b.label
|
||||
|
||||
proc blockBody(b: var TBlock): PRope {.inline.} =
|
||||
result = b.sections[cpsLocals].con(b.sections[cpsInit]).con(b.sections[cpsStmts])
|
||||
|
||||
var gBlockEndBracket = ropef("}$n")
|
||||
|
||||
proc endBlock(p: BProc) =
|
||||
let topBlock = p.blocks.len - 1
|
||||
let blockEnd = if p.blocks[topBlock].label != nil:
|
||||
ropef("} $1: ;$n", [p.blocks[topBlock].label])
|
||||
else:
|
||||
gBlockEndBracket
|
||||
# the block is merged into the parent block
|
||||
app(p.blocks[topBlock - 1].sections[cpsStmts], p.blocks[topBlock].blockBody)
|
||||
setlen(p.blocks, topBlock)
|
||||
# this is done after the block is popped so $n is
|
||||
# properly indented when pretty printing is enabled
|
||||
app(p.s(cpsStmts), blockEnd)
|
||||
|
||||
proc genWhileStmt(p: BProc, t: PNode) =
|
||||
# we don't generate labels here as for example GCC would produce
|
||||
# significantly worse code
|
||||
var
|
||||
a: TLoc
|
||||
Labl: TLabel
|
||||
length: int
|
||||
assert(sonsLen(t) == 2)
|
||||
inc(p.withinLoop)
|
||||
genLineDir(p, t)
|
||||
assert(sonsLen(t) == 2)
|
||||
inc(p.labels)
|
||||
Labl = con("LA", toRope(p.labels))
|
||||
length = len(p.blocks)
|
||||
setlen(p.blocks, length + 1)
|
||||
p.blocks[length].id = - p.labels # negative because it isn't used yet
|
||||
p.blocks[length].nestedTryStmts = p.nestedTryStmts.len
|
||||
appf(p.s[cpsStmts], "while (1) {$n")
|
||||
|
||||
var blockIdx = startBlock(p, "while (1) {$n")
|
||||
initLocExpr(p, t.sons[0], a)
|
||||
if (t.sons[0].kind != nkIntLit) or (t.sons[0].intVal == 0):
|
||||
p.blocks[length].id = abs(p.blocks[length].id)
|
||||
appf(p.s[cpsStmts], "if (!$1) goto $2;$n", [rdLoc(a), Labl])
|
||||
let label = assignLabel(p.blocks[blockIdx])
|
||||
appf(p.s(cpsStmts), "if (!$1) goto $2;$n", [rdLoc(a), label])
|
||||
genStmts(p, t.sons[1])
|
||||
if p.blocks[length].id > 0: appf(p.s[cpsStmts], "} $1: ;$n", [Labl])
|
||||
else: appf(p.s[cpsStmts], "}$n")
|
||||
setlen(p.blocks, len(p.blocks) - 1)
|
||||
endBlock(p)
|
||||
|
||||
dec(p.withinLoop)
|
||||
|
||||
proc genBlock(p: BProc, t: PNode, d: var TLoc) =
|
||||
inc(p.labels)
|
||||
var idx = len(p.blocks)
|
||||
var idx = startBlock(p)
|
||||
if t.sons[0].kind != nkEmpty:
|
||||
# named block?
|
||||
assert(t.sons[0].kind == nkSym)
|
||||
var sym = t.sons[0].sym
|
||||
sym.loc.k = locOther
|
||||
sym.loc.a = idx
|
||||
setlen(p.blocks, idx + 1)
|
||||
p.blocks[idx].id = -p.labels # negative because it isn't used yet
|
||||
p.blocks[idx].nestedTryStmts = p.nestedTryStmts.len
|
||||
if t.kind == nkBlockExpr: genStmtListExpr(p, t.sons[1], d)
|
||||
else: genStmts(p, t.sons[1])
|
||||
if p.blocks[idx].id > 0:
|
||||
appf(p.s[cpsStmts], "LA$1: ;$n", [toRope(p.blocks[idx].id)])
|
||||
setlen(p.blocks, idx)
|
||||
endBlock(p)
|
||||
|
||||
proc genBreakStmt(p: BProc, t: PNode) =
|
||||
var idx = len(p.blocks) - 1
|
||||
|
|
@ -224,10 +242,10 @@ proc genBreakStmt(p: BProc, t: PNode) =
|
|||
var sym = t.sons[0].sym
|
||||
assert(sym.loc.k == locOther)
|
||||
idx = sym.loc.a
|
||||
p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
|
||||
let label = assignLabel(p.blocks[idx])
|
||||
blockLeaveActions(p, p.nestedTryStmts.len - p.blocks[idx].nestedTryStmts)
|
||||
genLineDir(p, t)
|
||||
appf(p.s[cpsStmts], "goto LA$1;$n", [toRope(p.blocks[idx].id)])
|
||||
appf(p.s(cpsStmts), "goto $1;$n", [label])
|
||||
|
||||
proc getRaiseFrmt(p: BProc): string =
|
||||
#if gCmd == cmdCompileToCpp:
|
||||
|
|
@ -269,11 +287,11 @@ proc genCaseGenericBranch(p: BProc, b: PNode, e: TLoc,
|
|||
proc genCaseSecondPass(p: BProc, t: PNode, labId, until: int): TLabel =
|
||||
var Lend = getLabel(p)
|
||||
for i in 1..until:
|
||||
appf(p.s[cpsStmts], "LA$1: ;$n", [toRope(labId + i)])
|
||||
appf(p.s(cpsStmts), "LA$1: ;$n", [toRope(labId + i)])
|
||||
if t.sons[i].kind == nkOfBranch:
|
||||
var length = sonsLen(t.sons[i])
|
||||
genStmts(p, t.sons[i].sons[length - 1])
|
||||
appf(p.s[cpsStmts], "goto $1;$n", [Lend])
|
||||
appf(p.s(cpsStmts), "goto $1;$n", [Lend])
|
||||
else:
|
||||
genStmts(p, t.sons[i].sons[0])
|
||||
result = Lend
|
||||
|
|
@ -288,13 +306,13 @@ proc genIfForCaseUntil(p: BProc, t: PNode, rangeFormat, eqFormat: TFormatStr,
|
|||
genCaseGenericBranch(p, t.sons[i], a, rangeFormat, eqFormat,
|
||||
con("LA", toRope(p.labels)))
|
||||
else:
|
||||
appf(p.s[cpsStmts], "goto LA$1;$n", [toRope(p.labels)])
|
||||
appf(p.s(cpsStmts), "goto LA$1;$n", [toRope(p.labels)])
|
||||
if until < t.len-1:
|
||||
inc(p.labels)
|
||||
var gotoTarget = p.labels
|
||||
appf(p.s[cpsStmts], "goto LA$1;$n", [toRope(gotoTarget)])
|
||||
appf(p.s(cpsStmts), "goto LA$1;$n", [toRope(gotoTarget)])
|
||||
result = genCaseSecondPass(p, t, labId, until)
|
||||
appf(p.s[cpsStmts], "LA$1: ;$n", [toRope(gotoTarget)])
|
||||
appf(p.s(cpsStmts), "LA$1: ;$n", [toRope(gotoTarget)])
|
||||
else:
|
||||
result = genCaseSecondPass(p, t, labId, until)
|
||||
|
||||
|
|
@ -346,11 +364,11 @@ proc genStringCase(p: BProc, t: PNode) =
|
|||
if interior != brn:
|
||||
echo "BUG! ", interior, "-", brn
|
||||
if branches[j] != nil:
|
||||
appf(p.s[cpsStmts], "case $1: $n$2break;$n",
|
||||
appf(p.s(cpsStmts), "case $1: $n$2break;$n",
|
||||
[intLiteral(j), branches[j]])
|
||||
appf(p.s[cpsStmts], "}$n") # else statement:
|
||||
appf(p.s(cpsStmts), "}$n") # else statement:
|
||||
if t.sons[sonsLen(t) - 1].kind != nkOfBranch:
|
||||
appf(p.s[cpsStmts], "goto LA$1;$n", [toRope(p.labels)])
|
||||
appf(p.s(cpsStmts), "goto LA$1;$n", [toRope(p.labels)])
|
||||
# third pass: generate statements
|
||||
var Lend = genCaseSecondPass(p, t, labId, sonsLen(t)-1)
|
||||
fixLabel(p, Lend)
|
||||
|
|
@ -379,16 +397,16 @@ proc genCaseRange(p: BProc, branch: PNode) =
|
|||
for j in 0 .. length-2:
|
||||
if branch[j].kind == nkRange:
|
||||
if hasSwitchRange in CC[ccompiler].props:
|
||||
appf(p.s[cpsStmts], "case $1 ... $2:$n", [
|
||||
appf(p.s(cpsStmts), "case $1 ... $2:$n", [
|
||||
genLiteral(p, branch[j][0]),
|
||||
genLiteral(p, branch[j][1])])
|
||||
else:
|
||||
var v = copyNode(branch[j][0])
|
||||
while v.intVal <= branch[j][1].intVal:
|
||||
appf(p.s[cpsStmts], "case $1:$n", [genLiteral(p, v)])
|
||||
appf(p.s(cpsStmts), "case $1:$n", [genLiteral(p, v)])
|
||||
Inc(v.intVal)
|
||||
else:
|
||||
appf(p.s[cpsStmts], "case $1:$n", [genLiteral(p, branch[j])])
|
||||
appf(p.s(cpsStmts), "case $1:$n", [genLiteral(p, branch[j])])
|
||||
|
||||
proc genOrdinalCase(p: BProc, n: PNode) =
|
||||
# analyse 'case' statement:
|
||||
|
|
@ -404,7 +422,7 @@ proc genOrdinalCase(p: BProc, n: PNode) =
|
|||
|
||||
# generate switch part (might be empty):
|
||||
if splitPoint+1 < n.len:
|
||||
appf(p.s[cpsStmts], "switch ($1) {$n", [rdCharLoc(a)])
|
||||
appf(p.s(cpsStmts), "switch ($1) {$n", [rdCharLoc(a)])
|
||||
var hasDefault = false
|
||||
for i in splitPoint+1 .. < n.len:
|
||||
var branch = n[i]
|
||||
|
|
@ -413,13 +431,13 @@ proc genOrdinalCase(p: BProc, n: PNode) =
|
|||
genStmts(p, branch.lastSon)
|
||||
else:
|
||||
# else part of case statement:
|
||||
appf(p.s[cpsStmts], "default:$n")
|
||||
appf(p.s(cpsStmts), "default:$n")
|
||||
genStmts(p, branch[0])
|
||||
hasDefault = true
|
||||
appf(p.s[cpsStmts], "break;$n")
|
||||
appf(p.s(cpsStmts), "break;$n")
|
||||
if (hasAssume in CC[ccompiler].props) and not hasDefault:
|
||||
appf(p.s[cpsStmts], "default: __assume(0);$n")
|
||||
appf(p.s[cpsStmts], "}$n")
|
||||
appf(p.s(cpsStmts), "default: __assume(0);$n")
|
||||
appf(p.s(cpsStmts), "}$n")
|
||||
if Lend != nil: fixLabel(p, Lend)
|
||||
|
||||
proc genCaseStmt(p: BProc, t: PNode) =
|
||||
|
|
@ -475,42 +493,42 @@ proc genTryStmtCpp(p: BProc, t: PNode) =
|
|||
exc = getTempName()
|
||||
if not hasGeneralExceptSection(t):
|
||||
rethrowFlag = getTempName()
|
||||
appf(p.s[cpsLocals], "volatile NIM_BOOL $1 = NIM_FALSE;$n", [rethrowFlag])
|
||||
appf(p.s(cpsLocals), "volatile NIM_BOOL $1 = NIM_FALSE;$n", [rethrowFlag])
|
||||
if optStackTrace in p.Options:
|
||||
appcg(p, cpsStmts, "#setFrame((TFrame*)&F);$n")
|
||||
appf(p.s[cpsStmts], "try {$n")
|
||||
appf(p.s(cpsStmts), "try {$n")
|
||||
add(p.nestedTryStmts, t)
|
||||
genStmts(p, t.sons[0])
|
||||
length = sonsLen(t)
|
||||
if t.sons[1].kind == nkExceptBranch:
|
||||
appf(p.s[cpsStmts], "} catch (NimException& $1) {$n", [exc])
|
||||
appf(p.s(cpsStmts), "} catch (NimException& $1) {$n", [exc])
|
||||
if rethrowFlag != nil:
|
||||
appf(p.s[cpsStmts], "$1 = NIM_TRUE;$n", [rethrowFlag])
|
||||
appf(p.s[cpsStmts], "if ($1.sp.exc) {$n", [exc])
|
||||
appf(p.s(cpsStmts), "$1 = NIM_TRUE;$n", [rethrowFlag])
|
||||
appf(p.s(cpsStmts), "if ($1.sp.exc) {$n", [exc])
|
||||
i = 1
|
||||
while (i < length) and (t.sons[i].kind == nkExceptBranch):
|
||||
blen = sonsLen(t.sons[i])
|
||||
if blen == 1:
|
||||
# general except section:
|
||||
appf(p.s[cpsStmts], "default:$n")
|
||||
appf(p.s(cpsStmts), "default:$n")
|
||||
genStmts(p, t.sons[i].sons[0])
|
||||
else:
|
||||
for j in countup(0, blen - 2):
|
||||
assert(t.sons[i].sons[j].kind == nkType)
|
||||
appf(p.s[cpsStmts], "case $1:$n", [toRope(t.sons[i].sons[j].typ.id)])
|
||||
appf(p.s(cpsStmts), "case $1:$n", [toRope(t.sons[i].sons[j].typ.id)])
|
||||
genStmts(p, t.sons[i].sons[blen - 1])
|
||||
if rethrowFlag != nil:
|
||||
appf(p.s[cpsStmts], "$1 = NIM_FALSE; ", [rethrowFlag])
|
||||
appf(p.s[cpsStmts], "break;$n")
|
||||
appf(p.s(cpsStmts), "$1 = NIM_FALSE; ", [rethrowFlag])
|
||||
appf(p.s(cpsStmts), "break;$n")
|
||||
inc(i)
|
||||
if t.sons[1].kind == nkExceptBranch:
|
||||
appf(p.s[cpsStmts], "}}$n") # end of catch-switch statement
|
||||
appf(p.s(cpsStmts), "}}$n") # end of catch-switch statement
|
||||
appcg(p, cpsStmts, "#popSafePoint();")
|
||||
discard pop(p.nestedTryStmts)
|
||||
if (i < length) and (t.sons[i].kind == nkFinally):
|
||||
genStmts(p, t.sons[i].sons[0])
|
||||
if rethrowFlag != nil:
|
||||
appf(p.s[cpsStmts], "if ($1) { throw; }$n", [rethrowFlag])
|
||||
appf(p.s(cpsStmts), "if ($1) { throw; }$n", [rethrowFlag])
|
||||
|
||||
proc genTryStmt(p: BProc, t: PNode) =
|
||||
# code to generate:
|
||||
|
|
@ -535,6 +553,8 @@ proc genTryStmt(p: BProc, t: PNode) =
|
|||
# printf('fin!\n');
|
||||
# if (exception not cleared)
|
||||
# propagateCurrentException();
|
||||
#
|
||||
# XXX: push blocks here
|
||||
genLineDir(p, t)
|
||||
var safePoint = getTempName()
|
||||
discard cgsym(p.module, "E_Base")
|
||||
|
|
@ -543,7 +563,7 @@ proc genTryStmt(p: BProc, t: PNode) =
|
|||
"$1.status = setjmp($1.context);$n", [safePoint])
|
||||
if optStackTrace in p.Options:
|
||||
appcg(p, cpsStmts, "#setFrame((TFrame*)&F);$n")
|
||||
appf(p.s[cpsStmts], "if ($1.status == 0) {$n", [safePoint])
|
||||
appf(p.s(cpsStmts), "if ($1.status == 0) {$n", [safePoint])
|
||||
var length = sonsLen(t)
|
||||
add(p.nestedTryStmts, t)
|
||||
genStmts(p, t.sons[0])
|
||||
|
|
@ -554,13 +574,13 @@ proc genTryStmt(p: BProc, t: PNode) =
|
|||
var blen = sonsLen(t.sons[i])
|
||||
if blen == 1:
|
||||
# general except section:
|
||||
if i > 1: appf(p.s[cpsStmts], "else {$n")
|
||||
if i > 1: appf(p.s(cpsStmts), "else {$n")
|
||||
appcg(p, cpsStmts, "$1.status = 0;$n", [safePoint])
|
||||
inc p.popCurrExc
|
||||
genStmts(p, t.sons[i].sons[0])
|
||||
dec p.popCurrExc
|
||||
appcg(p, cpsStmts, "#popCurrentException();$n", [])
|
||||
if i > 1: appf(p.s[cpsStmts], "}$n")
|
||||
if i > 1: appf(p.s(cpsStmts), "}$n")
|
||||
else:
|
||||
inc p.popCurrExc
|
||||
var orExpr: PRope = nil
|
||||
|
|
@ -570,15 +590,15 @@ proc genTryStmt(p: BProc, t: PNode) =
|
|||
appcg(p.module, orExpr,
|
||||
"#isObj(#getCurrentException()->Sup.m_type, $1)",
|
||||
[genTypeInfo(p.module, t.sons[i].sons[j].typ)])
|
||||
if i > 1: app(p.s[cpsStmts], "else ")
|
||||
appf(p.s[cpsStmts], "if ($1) {$n", [orExpr])
|
||||
if i > 1: app(p.s(cpsStmts), "else ")
|
||||
appf(p.s(cpsStmts), "if ($1) {$n", [orExpr])
|
||||
appcg(p, cpsStmts, "$1.status = 0;$n", [safePoint])
|
||||
genStmts(p, t.sons[i].sons[blen-1])
|
||||
dec p.popCurrExc
|
||||
# code to clear the exception:
|
||||
appcg(p, cpsStmts, "#popCurrentException();}$n", [])
|
||||
inc(i)
|
||||
appf(p.s[cpsStmts], "}$n") # end of if statement
|
||||
appf(p.s(cpsStmts), "}$n") # end of if statement
|
||||
if i < length and t.sons[i].kind == nkFinally:
|
||||
genStmts(p, t.sons[i].sons[0])
|
||||
appcg(p, cpsStmts, "if ($1.status != 0) #reraiseException();$n", [safePoint])
|
||||
|
|
@ -608,7 +628,7 @@ proc genAsmStmt(p: BProc, t: PNode) =
|
|||
assert(t.kind == nkAsmStmt)
|
||||
genLineDir(p, t)
|
||||
var s = genAsmOrEmitStmt(p, t)
|
||||
appf(p.s[cpsStmts], CC[ccompiler].asmStmtFrmt, [s])
|
||||
appf(p.s(cpsStmts), CC[ccompiler].asmStmtFrmt, [s])
|
||||
|
||||
proc genEmit(p: BProc, t: PNode) =
|
||||
genLineDir(p, t)
|
||||
|
|
@ -617,7 +637,7 @@ proc genEmit(p: BProc, t: PNode) =
|
|||
# top level emit pragma?
|
||||
app(p.module.s[cfsProcHeaders], s)
|
||||
else:
|
||||
app(p.s[cpsStmts], s)
|
||||
app(p.s(cpsStmts), s)
|
||||
|
||||
var
|
||||
breakPointId: int = 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue