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
|
|
@ -6,6 +6,8 @@
|
||||||
# See the file "copying.txt", included in this
|
# See the file "copying.txt", included in this
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
#
|
#
|
||||||
|
#
|
||||||
|
# included from cgen.nim
|
||||||
|
|
||||||
proc leftAppearsOnRightSide(le, ri: PNode): bool =
|
proc leftAppearsOnRightSide(le, ri: PNode): bool =
|
||||||
if le != nil:
|
if le != nil:
|
||||||
|
|
@ -34,15 +36,15 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc, pl: PRope) =
|
||||||
resetLoc(p, d)
|
resetLoc(p, d)
|
||||||
app(pl, addrLoc(d))
|
app(pl, addrLoc(d))
|
||||||
app(pl, ")")
|
app(pl, ")")
|
||||||
app(p.s[cpsStmts], pl)
|
app(p.s(cpsStmts), pl)
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
else:
|
else:
|
||||||
var tmp: TLoc
|
var tmp: TLoc
|
||||||
getTemp(p, typ.sons[0], tmp)
|
getTemp(p, typ.sons[0], tmp)
|
||||||
app(pl, addrLoc(tmp))
|
app(pl, addrLoc(tmp))
|
||||||
app(pl, ")")
|
app(pl, ")")
|
||||||
app(p.s[cpsStmts], pl)
|
app(p.s(cpsStmts), pl)
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
genAssignment(p, d, tmp, {}) # no need for deep copying
|
genAssignment(p, d, tmp, {}) # no need for deep copying
|
||||||
else:
|
else:
|
||||||
app(pl, ")")
|
app(pl, ")")
|
||||||
|
|
@ -54,8 +56,8 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc, pl: PRope) =
|
||||||
genAssignment(p, d, list, {}) # no need for deep copying
|
genAssignment(p, d, list, {}) # no need for deep copying
|
||||||
else:
|
else:
|
||||||
app(pl, ")")
|
app(pl, ")")
|
||||||
app(p.s[cpsStmts], pl)
|
app(p.s(cpsStmts), pl)
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
|
|
||||||
proc isInCurrentFrame(p: BProc, n: PNode): bool =
|
proc isInCurrentFrame(p: BProc, n: PNode): bool =
|
||||||
# checks if `n` is an expression that refers to the current frame;
|
# checks if `n` is an expression that refers to the current frame;
|
||||||
|
|
@ -164,7 +166,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
||||||
if i < length - 1: app(pl, ", ")
|
if i < length - 1: app(pl, ", ")
|
||||||
|
|
||||||
template genCallPattern =
|
template genCallPattern =
|
||||||
appf(p.s[cpsStmts], CallPattern, op.r, pl, pl.addComma, rawProc)
|
appf(p.s(cpsStmts), CallPattern, op.r, pl, pl.addComma, rawProc)
|
||||||
|
|
||||||
let rawProc = getRawProcType(p, typ)
|
let rawProc = getRawProcType(p, typ)
|
||||||
if typ.sons[0] != nil:
|
if typ.sons[0] != nil:
|
||||||
|
|
@ -179,13 +181,13 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
||||||
resetLoc(p, d)
|
resetLoc(p, d)
|
||||||
app(pl, addrLoc(d))
|
app(pl, addrLoc(d))
|
||||||
genCallPattern()
|
genCallPattern()
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
else:
|
else:
|
||||||
var tmp: TLoc
|
var tmp: TLoc
|
||||||
getTemp(p, typ.sons[0], tmp)
|
getTemp(p, typ.sons[0], tmp)
|
||||||
app(pl, addrLoc(tmp))
|
app(pl, addrLoc(tmp))
|
||||||
genCallPattern()
|
genCallPattern()
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
genAssignment(p, d, tmp, {}) # no need for deep copying
|
genAssignment(p, d, tmp, {}) # no need for deep copying
|
||||||
else:
|
else:
|
||||||
if d.k == locNone: getTemp(p, typ.sons[0], d)
|
if d.k == locNone: getTemp(p, typ.sons[0], d)
|
||||||
|
|
@ -196,7 +198,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
||||||
genAssignment(p, d, list, {}) # no need for deep copying
|
genAssignment(p, d, list, {}) # no need for deep copying
|
||||||
else:
|
else:
|
||||||
genCallPattern()
|
genCallPattern()
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
|
|
||||||
proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
||||||
var op, a: TLoc
|
var op, a: TLoc
|
||||||
|
|
@ -261,15 +263,15 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
|
||||||
app(pl, "Result: ")
|
app(pl, "Result: ")
|
||||||
app(pl, addrLoc(d))
|
app(pl, addrLoc(d))
|
||||||
app(pl, "]")
|
app(pl, "]")
|
||||||
app(p.s[cpsStmts], pl)
|
app(p.s(cpsStmts), pl)
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
else:
|
else:
|
||||||
var tmp: TLoc
|
var tmp: TLoc
|
||||||
getTemp(p, typ.sons[0], tmp)
|
getTemp(p, typ.sons[0], tmp)
|
||||||
app(pl, addrLoc(tmp))
|
app(pl, addrLoc(tmp))
|
||||||
app(pl, "]")
|
app(pl, "]")
|
||||||
app(p.s[cpsStmts], pl)
|
app(p.s(cpsStmts), pl)
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
genAssignment(p, d, tmp, {}) # no need for deep copying
|
genAssignment(p, d, tmp, {}) # no need for deep copying
|
||||||
else:
|
else:
|
||||||
app(pl, "]")
|
app(pl, "]")
|
||||||
|
|
@ -281,8 +283,8 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
|
||||||
genAssignment(p, d, list, {}) # no need for deep copying
|
genAssignment(p, d, list, {}) # no need for deep copying
|
||||||
else:
|
else:
|
||||||
app(pl, "]")
|
app(pl, "]")
|
||||||
app(p.s[cpsStmts], pl)
|
app(p.s(cpsStmts), pl)
|
||||||
appf(p.s[cpsStmts], ";$n")
|
appf(p.s(cpsStmts), ";$n")
|
||||||
|
|
||||||
proc genCall(p: BProc, e: PNode, d: var TLoc) =
|
proc genCall(p: BProc, e: PNode, d: var TLoc) =
|
||||||
if e.sons[0].typ.callConv == ccClosure:
|
if e.sons[0].typ.callConv == ccClosure:
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,7 @@ proc getStorageLoc(n: PNode): TStorageLoc =
|
||||||
|
|
||||||
proc genRefAssign(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
proc genRefAssign(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
||||||
if dest.s == OnStack or optRefcGC notin gGlobalOptions:
|
if dest.s == OnStack or optRefcGC notin gGlobalOptions:
|
||||||
appf(p.s[cpsStmts], "$1 = $2;$n", [rdLoc(dest), rdLoc(src)])
|
appf(p.s(cpsStmts), "$1 = $2;$n", [rdLoc(dest), rdLoc(src)])
|
||||||
if needToKeepAlive in flags: keepAlive(p, dest)
|
if needToKeepAlive in flags: keepAlive(p, dest)
|
||||||
elif dest.s == OnHeap:
|
elif dest.s == OnHeap:
|
||||||
# location is on heap
|
# location is on heap
|
||||||
|
|
@ -180,13 +180,13 @@ proc genRefAssign(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
|
||||||
# appf(p.s[cpsStmts], 'if ($1) nimGCunref($1);$n', [rdLoc(dest)])
|
# appf(p.s[cpsStmts], 'if ($1) nimGCunref($1);$n', [rdLoc(dest)])
|
||||||
# appf(p.s[cpsStmts], '$1 = $2;$n', [rdLoc(dest), rdLoc(src)])
|
# appf(p.s[cpsStmts], '$1 = $2;$n', [rdLoc(dest), rdLoc(src)])
|
||||||
if canFormAcycle(dest.t):
|
if canFormAcycle(dest.t):
|
||||||
appcg(p.module, p.s[cpsStmts], "#asgnRef((void**) $1, $2);$n",
|
appcg(p.module, p.s(cpsStmts), "#asgnRef((void**) $1, $2);$n",
|
||||||
[addrLoc(dest), rdLoc(src)])
|
[addrLoc(dest), rdLoc(src)])
|
||||||
else:
|
else:
|
||||||
appcg(p.module, p.s[cpsStmts], "#asgnRefNoCycle((void**) $1, $2);$n",
|
appcg(p.module, p.s(cpsStmts), "#asgnRefNoCycle((void**) $1, $2);$n",
|
||||||
[addrLoc(dest), rdLoc(src)])
|
[addrLoc(dest), rdLoc(src)])
|
||||||
else:
|
else:
|
||||||
appcg(p.module, p.s[cpsStmts], "#unsureAsgnRef((void**) $1, $2);$n",
|
appcg(p.module, p.s(cpsStmts), "#unsureAsgnRef((void**) $1, $2);$n",
|
||||||
[addrLoc(dest), rdLoc(src)])
|
[addrLoc(dest), rdLoc(src)])
|
||||||
if needToKeepAlive in flags: keepAlive(p, dest)
|
if needToKeepAlive in flags: keepAlive(p, dest)
|
||||||
|
|
||||||
|
|
@ -736,9 +736,9 @@ proc genAndOr(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
|
||||||
expr(p, e.sons[1], tmp)
|
expr(p, e.sons[1], tmp)
|
||||||
L = getLabel(p)
|
L = getLabel(p)
|
||||||
if m == mOr:
|
if m == mOr:
|
||||||
appf(p.s[cpsStmts], "if ($1) goto $2;$n", [rdLoc(tmp), L])
|
appf(p.s(cpsStmts), "if ($1) goto $2;$n", [rdLoc(tmp), L])
|
||||||
else:
|
else:
|
||||||
appf(p.s[cpsStmts], "if (!($1)) goto $2;$n", [rdLoc(tmp), L])
|
appf(p.s(cpsStmts), "if (!($1)) goto $2;$n", [rdLoc(tmp), L])
|
||||||
expr(p, e.sons[2], tmp)
|
expr(p, e.sons[2], tmp)
|
||||||
fixLabel(p, L)
|
fixLabel(p, L)
|
||||||
if d.k == locNone:
|
if d.k == locNone:
|
||||||
|
|
@ -771,9 +771,9 @@ proc genIfExpr(p: BProc, n: PNode, d: var TLoc) =
|
||||||
of nkElifExpr:
|
of nkElifExpr:
|
||||||
initLocExpr(p, it.sons[0], a)
|
initLocExpr(p, it.sons[0], a)
|
||||||
Lelse = getLabel(p)
|
Lelse = getLabel(p)
|
||||||
appf(p.s[cpsStmts], "if (!$1) goto $2;$n", [rdLoc(a), Lelse])
|
appf(p.s(cpsStmts), "if (!$1) goto $2;$n", [rdLoc(a), Lelse])
|
||||||
expr(p, it.sons[1], tmp)
|
expr(p, it.sons[1], tmp)
|
||||||
appf(p.s[cpsStmts], "goto $1;$n", [Lend])
|
appf(p.s(cpsStmts), "goto $1;$n", [Lend])
|
||||||
fixLabel(p, Lelse)
|
fixLabel(p, Lelse)
|
||||||
of nkElseExpr:
|
of nkElseExpr:
|
||||||
expr(p, it.sons[0], tmp)
|
expr(p, it.sons[0], tmp)
|
||||||
|
|
@ -832,7 +832,7 @@ proc genStrConcat(p: BProc, e: PNode, d: var TLoc) =
|
||||||
appf(lens, "$1->$2 + ", [rdLoc(a), lenField()])
|
appf(lens, "$1->$2 + ", [rdLoc(a), lenField()])
|
||||||
appcg(p.module, appends, "#appendString($1, $2);$n", [tmp.r, rdLoc(a)])
|
appcg(p.module, appends, "#appendString($1, $2);$n", [tmp.r, rdLoc(a)])
|
||||||
appcg(p, cpsStmts, "$1 = #rawNewString($2$3);$n", [tmp.r, lens, toRope(L)])
|
appcg(p, cpsStmts, "$1 = #rawNewString($2$3);$n", [tmp.r, lens, toRope(L)])
|
||||||
app(p.s[cpsStmts], appends)
|
app(p.s(cpsStmts), appends)
|
||||||
if d.k == locNone:
|
if d.k == locNone:
|
||||||
d = tmp
|
d = tmp
|
||||||
keepAlive(p, tmp)
|
keepAlive(p, tmp)
|
||||||
|
|
@ -874,7 +874,7 @@ proc genStrAppend(p: BProc, e: PNode, d: var TLoc) =
|
||||||
appcg(p, cpsStmts, "$1 = #resizeString($1, $2$3);$n",
|
appcg(p, cpsStmts, "$1 = #resizeString($1, $2$3);$n",
|
||||||
[rdLoc(dest), lens, toRope(L)])
|
[rdLoc(dest), lens, toRope(L)])
|
||||||
keepAlive(p, dest)
|
keepAlive(p, dest)
|
||||||
app(p.s[cpsStmts], appends)
|
app(p.s(cpsStmts), appends)
|
||||||
|
|
||||||
proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
|
proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
|
||||||
# seq &= x -->
|
# seq &= x -->
|
||||||
|
|
@ -1171,7 +1171,7 @@ proc binaryStmtInExcl(p: BProc, e: PNode, d: var TLoc, frmt: string) =
|
||||||
assert(d.k == locNone)
|
assert(d.k == locNone)
|
||||||
InitLocExpr(p, e.sons[1], a)
|
InitLocExpr(p, e.sons[1], a)
|
||||||
InitLocExpr(p, e.sons[2], b)
|
InitLocExpr(p, e.sons[2], b)
|
||||||
appf(p.s[cpsStmts], frmt, [rdLoc(a), rdSetElemLoc(b, a.t)])
|
appf(p.s(cpsStmts), frmt, [rdLoc(a), rdSetElemLoc(b, a.t)])
|
||||||
|
|
||||||
proc genInOp(p: BProc, e: PNode, d: var TLoc) =
|
proc genInOp(p: BProc, e: PNode, d: var TLoc) =
|
||||||
var a, b, x, y: TLoc
|
var a, b, x, y: TLoc
|
||||||
|
|
@ -1247,7 +1247,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
|
||||||
initLocExpr(p, e.sons[1], a)
|
initLocExpr(p, e.sons[1], a)
|
||||||
initLocExpr(p, e.sons[2], b)
|
initLocExpr(p, e.sons[2], b)
|
||||||
if d.k == locNone: getTemp(p, a.t, d)
|
if d.k == locNone: getTemp(p, a.t, d)
|
||||||
appf(p.s[cpsStmts], lookupOpr[op],
|
appf(p.s(cpsStmts), lookupOpr[op],
|
||||||
[rdLoc(i), toRope(size), rdLoc(d), rdLoc(a), rdLoc(b)])
|
[rdLoc(i), toRope(size), rdLoc(d), rdLoc(a), rdLoc(b)])
|
||||||
of mEqSet:
|
of mEqSet:
|
||||||
binaryExprChar(p, e, d, "(memcmp($1, $2, " & $(size) & ")==0)")
|
binaryExprChar(p, e, d, "(memcmp($1, $2, " & $(size) & ")==0)")
|
||||||
|
|
@ -1257,7 +1257,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
|
||||||
initLocExpr(p, e.sons[1], a)
|
initLocExpr(p, e.sons[1], a)
|
||||||
initLocExpr(p, e.sons[2], b)
|
initLocExpr(p, e.sons[2], b)
|
||||||
if d.k == locNone: getTemp(p, a.t, d)
|
if d.k == locNone: getTemp(p, a.t, d)
|
||||||
appf(p.s[cpsStmts],
|
appf(p.s(cpsStmts),
|
||||||
"for ($1 = 0; $1 < $2; $1++) $n" &
|
"for ($1 = 0; $1 < $2; $1++) $n" &
|
||||||
" $3[$1] = $4[$1] $6 $5[$1];$n", [
|
" $3[$1] = $4[$1] $6 $5[$1];$n", [
|
||||||
rdLoc(i), toRope(size), rdLoc(d), rdLoc(a), rdLoc(b),
|
rdLoc(i), toRope(size), rdLoc(d), rdLoc(a), rdLoc(b),
|
||||||
|
|
@ -1467,35 +1467,35 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
|
||||||
if d.k == locNone: getTemp(p, e.typ, d)
|
if d.k == locNone: getTemp(p, e.typ, d)
|
||||||
if getSize(e.typ) > 8:
|
if getSize(e.typ) > 8:
|
||||||
# big set:
|
# big set:
|
||||||
appf(p.s[cpsStmts], "memset($1, 0, sizeof($1));$n", [rdLoc(d)])
|
appf(p.s(cpsStmts), "memset($1, 0, sizeof($1));$n", [rdLoc(d)])
|
||||||
for i in countup(0, sonsLen(e) - 1):
|
for i in countup(0, sonsLen(e) - 1):
|
||||||
if e.sons[i].kind == nkRange:
|
if e.sons[i].kind == nkRange:
|
||||||
getTemp(p, getSysType(tyInt), idx) # our counter
|
getTemp(p, getSysType(tyInt), idx) # our counter
|
||||||
initLocExpr(p, e.sons[i].sons[0], a)
|
initLocExpr(p, e.sons[i].sons[0], a)
|
||||||
initLocExpr(p, e.sons[i].sons[1], b)
|
initLocExpr(p, e.sons[i].sons[1], b)
|
||||||
appf(p.s[cpsStmts], "for ($1 = $3; $1 <= $4; $1++) $n" &
|
appf(p.s(cpsStmts), "for ($1 = $3; $1 <= $4; $1++) $n" &
|
||||||
"$2[$1/8] |=(1<<($1%8));$n", [rdLoc(idx), rdLoc(d),
|
"$2[$1/8] |=(1<<($1%8));$n", [rdLoc(idx), rdLoc(d),
|
||||||
rdSetElemLoc(a, e.typ), rdSetElemLoc(b, e.typ)])
|
rdSetElemLoc(a, e.typ), rdSetElemLoc(b, e.typ)])
|
||||||
else:
|
else:
|
||||||
initLocExpr(p, e.sons[i], a)
|
initLocExpr(p, e.sons[i], a)
|
||||||
appf(p.s[cpsStmts], "$1[$2/8] |=(1<<($2%8));$n",
|
appf(p.s(cpsStmts), "$1[$2/8] |=(1<<($2%8));$n",
|
||||||
[rdLoc(d), rdSetElemLoc(a, e.typ)])
|
[rdLoc(d), rdSetElemLoc(a, e.typ)])
|
||||||
else:
|
else:
|
||||||
# small set
|
# small set
|
||||||
var ts = "NI" & $(getSize(e.typ) * 8)
|
var ts = "NI" & $(getSize(e.typ) * 8)
|
||||||
appf(p.s[cpsStmts], "$1 = 0;$n", [rdLoc(d)])
|
appf(p.s(cpsStmts), "$1 = 0;$n", [rdLoc(d)])
|
||||||
for i in countup(0, sonsLen(e) - 1):
|
for i in countup(0, sonsLen(e) - 1):
|
||||||
if e.sons[i].kind == nkRange:
|
if e.sons[i].kind == nkRange:
|
||||||
getTemp(p, getSysType(tyInt), idx) # our counter
|
getTemp(p, getSysType(tyInt), idx) # our counter
|
||||||
initLocExpr(p, e.sons[i].sons[0], a)
|
initLocExpr(p, e.sons[i].sons[0], a)
|
||||||
initLocExpr(p, e.sons[i].sons[1], b)
|
initLocExpr(p, e.sons[i].sons[1], b)
|
||||||
appf(p.s[cpsStmts], "for ($1 = $3; $1 <= $4; $1++) $n" &
|
appf(p.s(cpsStmts), "for ($1 = $3; $1 <= $4; $1++) $n" &
|
||||||
"$2 |=(1<<((" & ts & ")($1)%(sizeof(" & ts & ")*8)));$n", [
|
"$2 |=(1<<((" & ts & ")($1)%(sizeof(" & ts & ")*8)));$n", [
|
||||||
rdLoc(idx), rdLoc(d), rdSetElemLoc(a, e.typ),
|
rdLoc(idx), rdLoc(d), rdSetElemLoc(a, e.typ),
|
||||||
rdSetElemLoc(b, e.typ)])
|
rdSetElemLoc(b, e.typ)])
|
||||||
else:
|
else:
|
||||||
initLocExpr(p, e.sons[i], a)
|
initLocExpr(p, e.sons[i], a)
|
||||||
appf(p.s[cpsStmts],
|
appf(p.s(cpsStmts),
|
||||||
"$1 |=(1<<((" & ts & ")($2)%(sizeof(" & ts & ")*8)));$n",
|
"$1 |=(1<<((" & ts & ")($2)%(sizeof(" & ts & ")*8)));$n",
|
||||||
[rdLoc(d), rdSetElemLoc(a, e.typ)])
|
[rdLoc(d), rdSetElemLoc(a, e.typ)])
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -280,7 +280,7 @@ proc mergeRequired*(m: BModule): bool =
|
||||||
#echo "not empty: ", i, " ", ropeToStr(m.s[i])
|
#echo "not empty: ", i, " ", ropeToStr(m.s[i])
|
||||||
return true
|
return true
|
||||||
for i in low(TCProcSection)..high(TCProcSection):
|
for i in low(TCProcSection)..high(TCProcSection):
|
||||||
if m.initProc.s[i] != nil:
|
if m.initProc.s(i) != nil:
|
||||||
#echo "not empty: ", i, " ", ropeToStr(m.initProc.s[i])
|
#echo "not empty: ", i, " ", ropeToStr(m.initProc.s[i])
|
||||||
return true
|
return true
|
||||||
|
|
||||||
|
|
@ -292,5 +292,5 @@ proc mergeFiles*(cfilename: string, m: BModule) =
|
||||||
for i in low(TCFileSection)..high(TCFileSection):
|
for i in low(TCFileSection)..high(TCFileSection):
|
||||||
m.s[i] = con(old.f[i], m.s[i])
|
m.s[i] = con(old.f[i], m.s[i])
|
||||||
for i in low(TCProcSection)..high(TCProcSection):
|
for i in low(TCProcSection)..high(TCProcSection):
|
||||||
m.initProc.s[i] = con(old.p[i], m.initProc.s[i])
|
m.initProc.s(i) = con(old.p[i], m.initProc.s(i))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,7 @@ proc genIfStmt(p: BProc, n: PNode) =
|
||||||
# elsePart
|
# elsePart
|
||||||
# Lend:
|
# Lend:
|
||||||
#
|
#
|
||||||
|
## XXX: push blocks here
|
||||||
var
|
var
|
||||||
a: TLoc
|
a: TLoc
|
||||||
Lelse: TLabel
|
Lelse: TLabel
|
||||||
|
|
@ -132,12 +133,12 @@ proc genIfStmt(p: BProc, n: PNode) =
|
||||||
initLocExpr(p, it.sons[0], a)
|
initLocExpr(p, it.sons[0], a)
|
||||||
Lelse = getLabel(p)
|
Lelse = getLabel(p)
|
||||||
inc(p.labels)
|
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",
|
"br i1 $1, label %LOC$3, label %$2$n" & "LOC$3: $n",
|
||||||
[rdLoc(a), Lelse, toRope(p.labels)])
|
[rdLoc(a), Lelse, toRope(p.labels)])
|
||||||
genStmts(p, it.sons[1])
|
genStmts(p, it.sons[1])
|
||||||
if sonsLen(n) > 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)
|
fixLabel(p, Lelse)
|
||||||
of nkElse:
|
of nkElse:
|
||||||
genStmts(p, it.sons[0])
|
genStmts(p, it.sons[0])
|
||||||
|
|
@ -169,52 +170,69 @@ proc genReturnStmt(p: BProc, t: PNode) =
|
||||||
genLineDir(p, t)
|
genLineDir(p, t)
|
||||||
if (t.sons[0].kind != nkEmpty): genStmts(p, t.sons[0])
|
if (t.sons[0].kind != nkEmpty): genStmts(p, t.sons[0])
|
||||||
blockLeaveActions(p, min(1, p.nestedTryStmts.len))
|
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
|
# we don't generate labels here as for example GCC would produce
|
||||||
# significantly worse code
|
# significantly worse code
|
||||||
var
|
var
|
||||||
a: TLoc
|
a: TLoc
|
||||||
Labl: TLabel
|
Labl: TLabel
|
||||||
length: int
|
assert(sonsLen(t) == 2)
|
||||||
inc(p.withinLoop)
|
inc(p.withinLoop)
|
||||||
genLineDir(p, t)
|
genLineDir(p, t)
|
||||||
assert(sonsLen(t) == 2)
|
|
||||||
inc(p.labels)
|
var blockIdx = startBlock(p, "while (1) {$n")
|
||||||
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")
|
|
||||||
initLocExpr(p, t.sons[0], a)
|
initLocExpr(p, t.sons[0], a)
|
||||||
if (t.sons[0].kind != nkIntLit) or (t.sons[0].intVal == 0):
|
if (t.sons[0].kind != nkIntLit) or (t.sons[0].intVal == 0):
|
||||||
p.blocks[length].id = abs(p.blocks[length].id)
|
let label = assignLabel(p.blocks[blockIdx])
|
||||||
appf(p.s[cpsStmts], "if (!$1) goto $2;$n", [rdLoc(a), Labl])
|
appf(p.s(cpsStmts), "if (!$1) goto $2;$n", [rdLoc(a), label])
|
||||||
genStmts(p, t.sons[1])
|
genStmts(p, t.sons[1])
|
||||||
if p.blocks[length].id > 0: appf(p.s[cpsStmts], "} $1: ;$n", [Labl])
|
endBlock(p)
|
||||||
else: appf(p.s[cpsStmts], "}$n")
|
|
||||||
setlen(p.blocks, len(p.blocks) - 1)
|
|
||||||
dec(p.withinLoop)
|
dec(p.withinLoop)
|
||||||
|
|
||||||
proc genBlock(p: BProc, t: PNode, d: var TLoc) =
|
proc genBlock(p: BProc, t: PNode, d: var TLoc) =
|
||||||
inc(p.labels)
|
var idx = startBlock(p)
|
||||||
var idx = len(p.blocks)
|
|
||||||
if t.sons[0].kind != nkEmpty:
|
if t.sons[0].kind != nkEmpty:
|
||||||
# named block?
|
# named block?
|
||||||
assert(t.sons[0].kind == nkSym)
|
assert(t.sons[0].kind == nkSym)
|
||||||
var sym = t.sons[0].sym
|
var sym = t.sons[0].sym
|
||||||
sym.loc.k = locOther
|
sym.loc.k = locOther
|
||||||
sym.loc.a = idx
|
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)
|
if t.kind == nkBlockExpr: genStmtListExpr(p, t.sons[1], d)
|
||||||
else: genStmts(p, t.sons[1])
|
else: genStmts(p, t.sons[1])
|
||||||
if p.blocks[idx].id > 0:
|
endBlock(p)
|
||||||
appf(p.s[cpsStmts], "LA$1: ;$n", [toRope(p.blocks[idx].id)])
|
|
||||||
setlen(p.blocks, idx)
|
|
||||||
|
|
||||||
proc genBreakStmt(p: BProc, t: PNode) =
|
proc genBreakStmt(p: BProc, t: PNode) =
|
||||||
var idx = len(p.blocks) - 1
|
var idx = len(p.blocks) - 1
|
||||||
|
|
@ -224,10 +242,10 @@ proc genBreakStmt(p: BProc, t: PNode) =
|
||||||
var sym = t.sons[0].sym
|
var sym = t.sons[0].sym
|
||||||
assert(sym.loc.k == locOther)
|
assert(sym.loc.k == locOther)
|
||||||
idx = sym.loc.a
|
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)
|
blockLeaveActions(p, p.nestedTryStmts.len - p.blocks[idx].nestedTryStmts)
|
||||||
genLineDir(p, t)
|
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 =
|
proc getRaiseFrmt(p: BProc): string =
|
||||||
#if gCmd == cmdCompileToCpp:
|
#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 =
|
proc genCaseSecondPass(p: BProc, t: PNode, labId, until: int): TLabel =
|
||||||
var Lend = getLabel(p)
|
var Lend = getLabel(p)
|
||||||
for i in 1..until:
|
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:
|
if t.sons[i].kind == nkOfBranch:
|
||||||
var length = sonsLen(t.sons[i])
|
var length = sonsLen(t.sons[i])
|
||||||
genStmts(p, t.sons[i].sons[length - 1])
|
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:
|
else:
|
||||||
genStmts(p, t.sons[i].sons[0])
|
genStmts(p, t.sons[i].sons[0])
|
||||||
result = Lend
|
result = Lend
|
||||||
|
|
@ -288,13 +306,13 @@ proc genIfForCaseUntil(p: BProc, t: PNode, rangeFormat, eqFormat: TFormatStr,
|
||||||
genCaseGenericBranch(p, t.sons[i], a, rangeFormat, eqFormat,
|
genCaseGenericBranch(p, t.sons[i], a, rangeFormat, eqFormat,
|
||||||
con("LA", toRope(p.labels)))
|
con("LA", toRope(p.labels)))
|
||||||
else:
|
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:
|
if until < t.len-1:
|
||||||
inc(p.labels)
|
inc(p.labels)
|
||||||
var gotoTarget = 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)
|
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:
|
else:
|
||||||
result = genCaseSecondPass(p, t, labId, until)
|
result = genCaseSecondPass(p, t, labId, until)
|
||||||
|
|
||||||
|
|
@ -346,11 +364,11 @@ proc genStringCase(p: BProc, t: PNode) =
|
||||||
if interior != brn:
|
if interior != brn:
|
||||||
echo "BUG! ", interior, "-", brn
|
echo "BUG! ", interior, "-", brn
|
||||||
if branches[j] != nil:
|
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]])
|
[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:
|
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
|
# third pass: generate statements
|
||||||
var Lend = genCaseSecondPass(p, t, labId, sonsLen(t)-1)
|
var Lend = genCaseSecondPass(p, t, labId, sonsLen(t)-1)
|
||||||
fixLabel(p, Lend)
|
fixLabel(p, Lend)
|
||||||
|
|
@ -379,16 +397,16 @@ proc genCaseRange(p: BProc, branch: PNode) =
|
||||||
for j in 0 .. length-2:
|
for j in 0 .. length-2:
|
||||||
if branch[j].kind == nkRange:
|
if branch[j].kind == nkRange:
|
||||||
if hasSwitchRange in CC[ccompiler].props:
|
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][0]),
|
||||||
genLiteral(p, branch[j][1])])
|
genLiteral(p, branch[j][1])])
|
||||||
else:
|
else:
|
||||||
var v = copyNode(branch[j][0])
|
var v = copyNode(branch[j][0])
|
||||||
while v.intVal <= branch[j][1].intVal:
|
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)
|
Inc(v.intVal)
|
||||||
else:
|
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) =
|
proc genOrdinalCase(p: BProc, n: PNode) =
|
||||||
# analyse 'case' statement:
|
# analyse 'case' statement:
|
||||||
|
|
@ -404,7 +422,7 @@ proc genOrdinalCase(p: BProc, n: PNode) =
|
||||||
|
|
||||||
# generate switch part (might be empty):
|
# generate switch part (might be empty):
|
||||||
if splitPoint+1 < n.len:
|
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
|
var hasDefault = false
|
||||||
for i in splitPoint+1 .. < n.len:
|
for i in splitPoint+1 .. < n.len:
|
||||||
var branch = n[i]
|
var branch = n[i]
|
||||||
|
|
@ -413,13 +431,13 @@ proc genOrdinalCase(p: BProc, n: PNode) =
|
||||||
genStmts(p, branch.lastSon)
|
genStmts(p, branch.lastSon)
|
||||||
else:
|
else:
|
||||||
# else part of case statement:
|
# else part of case statement:
|
||||||
appf(p.s[cpsStmts], "default:$n")
|
appf(p.s(cpsStmts), "default:$n")
|
||||||
genStmts(p, branch[0])
|
genStmts(p, branch[0])
|
||||||
hasDefault = true
|
hasDefault = true
|
||||||
appf(p.s[cpsStmts], "break;$n")
|
appf(p.s(cpsStmts), "break;$n")
|
||||||
if (hasAssume in CC[ccompiler].props) and not hasDefault:
|
if (hasAssume in CC[ccompiler].props) and not hasDefault:
|
||||||
appf(p.s[cpsStmts], "default: __assume(0);$n")
|
appf(p.s(cpsStmts), "default: __assume(0);$n")
|
||||||
appf(p.s[cpsStmts], "}$n")
|
appf(p.s(cpsStmts), "}$n")
|
||||||
if Lend != nil: fixLabel(p, Lend)
|
if Lend != nil: fixLabel(p, Lend)
|
||||||
|
|
||||||
proc genCaseStmt(p: BProc, t: PNode) =
|
proc genCaseStmt(p: BProc, t: PNode) =
|
||||||
|
|
@ -475,42 +493,42 @@ proc genTryStmtCpp(p: BProc, t: PNode) =
|
||||||
exc = getTempName()
|
exc = getTempName()
|
||||||
if not hasGeneralExceptSection(t):
|
if not hasGeneralExceptSection(t):
|
||||||
rethrowFlag = getTempName()
|
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:
|
if optStackTrace in p.Options:
|
||||||
appcg(p, cpsStmts, "#setFrame((TFrame*)&F);$n")
|
appcg(p, cpsStmts, "#setFrame((TFrame*)&F);$n")
|
||||||
appf(p.s[cpsStmts], "try {$n")
|
appf(p.s(cpsStmts), "try {$n")
|
||||||
add(p.nestedTryStmts, t)
|
add(p.nestedTryStmts, t)
|
||||||
genStmts(p, t.sons[0])
|
genStmts(p, t.sons[0])
|
||||||
length = sonsLen(t)
|
length = sonsLen(t)
|
||||||
if t.sons[1].kind == nkExceptBranch:
|
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:
|
if rethrowFlag != nil:
|
||||||
appf(p.s[cpsStmts], "$1 = NIM_TRUE;$n", [rethrowFlag])
|
appf(p.s(cpsStmts), "$1 = NIM_TRUE;$n", [rethrowFlag])
|
||||||
appf(p.s[cpsStmts], "if ($1.sp.exc) {$n", [exc])
|
appf(p.s(cpsStmts), "if ($1.sp.exc) {$n", [exc])
|
||||||
i = 1
|
i = 1
|
||||||
while (i < length) and (t.sons[i].kind == nkExceptBranch):
|
while (i < length) and (t.sons[i].kind == nkExceptBranch):
|
||||||
blen = sonsLen(t.sons[i])
|
blen = sonsLen(t.sons[i])
|
||||||
if blen == 1:
|
if blen == 1:
|
||||||
# general except section:
|
# general except section:
|
||||||
appf(p.s[cpsStmts], "default:$n")
|
appf(p.s(cpsStmts), "default:$n")
|
||||||
genStmts(p, t.sons[i].sons[0])
|
genStmts(p, t.sons[i].sons[0])
|
||||||
else:
|
else:
|
||||||
for j in countup(0, blen - 2):
|
for j in countup(0, blen - 2):
|
||||||
assert(t.sons[i].sons[j].kind == nkType)
|
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])
|
genStmts(p, t.sons[i].sons[blen - 1])
|
||||||
if rethrowFlag != nil:
|
if rethrowFlag != nil:
|
||||||
appf(p.s[cpsStmts], "$1 = NIM_FALSE; ", [rethrowFlag])
|
appf(p.s(cpsStmts), "$1 = NIM_FALSE; ", [rethrowFlag])
|
||||||
appf(p.s[cpsStmts], "break;$n")
|
appf(p.s(cpsStmts), "break;$n")
|
||||||
inc(i)
|
inc(i)
|
||||||
if t.sons[1].kind == nkExceptBranch:
|
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();")
|
appcg(p, cpsStmts, "#popSafePoint();")
|
||||||
discard pop(p.nestedTryStmts)
|
discard pop(p.nestedTryStmts)
|
||||||
if (i < length) and (t.sons[i].kind == nkFinally):
|
if (i < length) and (t.sons[i].kind == nkFinally):
|
||||||
genStmts(p, t.sons[i].sons[0])
|
genStmts(p, t.sons[i].sons[0])
|
||||||
if rethrowFlag != nil:
|
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) =
|
proc genTryStmt(p: BProc, t: PNode) =
|
||||||
# code to generate:
|
# code to generate:
|
||||||
|
|
@ -535,6 +553,8 @@ proc genTryStmt(p: BProc, t: PNode) =
|
||||||
# printf('fin!\n');
|
# printf('fin!\n');
|
||||||
# if (exception not cleared)
|
# if (exception not cleared)
|
||||||
# propagateCurrentException();
|
# propagateCurrentException();
|
||||||
|
#
|
||||||
|
# XXX: push blocks here
|
||||||
genLineDir(p, t)
|
genLineDir(p, t)
|
||||||
var safePoint = getTempName()
|
var safePoint = getTempName()
|
||||||
discard cgsym(p.module, "E_Base")
|
discard cgsym(p.module, "E_Base")
|
||||||
|
|
@ -543,7 +563,7 @@ proc genTryStmt(p: BProc, t: PNode) =
|
||||||
"$1.status = setjmp($1.context);$n", [safePoint])
|
"$1.status = setjmp($1.context);$n", [safePoint])
|
||||||
if optStackTrace in p.Options:
|
if optStackTrace in p.Options:
|
||||||
appcg(p, cpsStmts, "#setFrame((TFrame*)&F);$n")
|
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)
|
var length = sonsLen(t)
|
||||||
add(p.nestedTryStmts, t)
|
add(p.nestedTryStmts, t)
|
||||||
genStmts(p, t.sons[0])
|
genStmts(p, t.sons[0])
|
||||||
|
|
@ -554,13 +574,13 @@ proc genTryStmt(p: BProc, t: PNode) =
|
||||||
var blen = sonsLen(t.sons[i])
|
var blen = sonsLen(t.sons[i])
|
||||||
if blen == 1:
|
if blen == 1:
|
||||||
# general except section:
|
# 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])
|
appcg(p, cpsStmts, "$1.status = 0;$n", [safePoint])
|
||||||
inc p.popCurrExc
|
inc p.popCurrExc
|
||||||
genStmts(p, t.sons[i].sons[0])
|
genStmts(p, t.sons[i].sons[0])
|
||||||
dec p.popCurrExc
|
dec p.popCurrExc
|
||||||
appcg(p, cpsStmts, "#popCurrentException();$n", [])
|
appcg(p, cpsStmts, "#popCurrentException();$n", [])
|
||||||
if i > 1: appf(p.s[cpsStmts], "}$n")
|
if i > 1: appf(p.s(cpsStmts), "}$n")
|
||||||
else:
|
else:
|
||||||
inc p.popCurrExc
|
inc p.popCurrExc
|
||||||
var orExpr: PRope = nil
|
var orExpr: PRope = nil
|
||||||
|
|
@ -570,15 +590,15 @@ proc genTryStmt(p: BProc, t: PNode) =
|
||||||
appcg(p.module, orExpr,
|
appcg(p.module, orExpr,
|
||||||
"#isObj(#getCurrentException()->Sup.m_type, $1)",
|
"#isObj(#getCurrentException()->Sup.m_type, $1)",
|
||||||
[genTypeInfo(p.module, t.sons[i].sons[j].typ)])
|
[genTypeInfo(p.module, t.sons[i].sons[j].typ)])
|
||||||
if i > 1: app(p.s[cpsStmts], "else ")
|
if i > 1: app(p.s(cpsStmts), "else ")
|
||||||
appf(p.s[cpsStmts], "if ($1) {$n", [orExpr])
|
appf(p.s(cpsStmts), "if ($1) {$n", [orExpr])
|
||||||
appcg(p, cpsStmts, "$1.status = 0;$n", [safePoint])
|
appcg(p, cpsStmts, "$1.status = 0;$n", [safePoint])
|
||||||
genStmts(p, t.sons[i].sons[blen-1])
|
genStmts(p, t.sons[i].sons[blen-1])
|
||||||
dec p.popCurrExc
|
dec p.popCurrExc
|
||||||
# code to clear the exception:
|
# code to clear the exception:
|
||||||
appcg(p, cpsStmts, "#popCurrentException();}$n", [])
|
appcg(p, cpsStmts, "#popCurrentException();}$n", [])
|
||||||
inc(i)
|
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:
|
if i < length and t.sons[i].kind == nkFinally:
|
||||||
genStmts(p, t.sons[i].sons[0])
|
genStmts(p, t.sons[i].sons[0])
|
||||||
appcg(p, cpsStmts, "if ($1.status != 0) #reraiseException();$n", [safePoint])
|
appcg(p, cpsStmts, "if ($1.status != 0) #reraiseException();$n", [safePoint])
|
||||||
|
|
@ -608,7 +628,7 @@ proc genAsmStmt(p: BProc, t: PNode) =
|
||||||
assert(t.kind == nkAsmStmt)
|
assert(t.kind == nkAsmStmt)
|
||||||
genLineDir(p, t)
|
genLineDir(p, t)
|
||||||
var s = genAsmOrEmitStmt(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) =
|
proc genEmit(p: BProc, t: PNode) =
|
||||||
genLineDir(p, t)
|
genLineDir(p, t)
|
||||||
|
|
@ -617,7 +637,7 @@ proc genEmit(p: BProc, t: PNode) =
|
||||||
# top level emit pragma?
|
# top level emit pragma?
|
||||||
app(p.module.s[cfsProcHeaders], s)
|
app(p.module.s[cfsProcHeaders], s)
|
||||||
else:
|
else:
|
||||||
app(p.s[cpsStmts], s)
|
app(p.s(cpsStmts), s)
|
||||||
|
|
||||||
var
|
var
|
||||||
breakPointId: int = 0
|
breakPointId: int = 0
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@
|
||||||
## Thread var support for crappy architectures that lack native support for
|
## Thread var support for crappy architectures that lack native support for
|
||||||
## thread local storage. (**Thank you Mac OS X!**)
|
## thread local storage. (**Thank you Mac OS X!**)
|
||||||
|
|
||||||
|
# included from cgen.nim
|
||||||
|
|
||||||
proc emulatedThreadVars(): bool {.inline.} =
|
proc emulatedThreadVars(): bool {.inline.} =
|
||||||
result = {optThreads, optTlsEmulation} <= gGlobalOptions
|
result = {optThreads, optTlsEmulation} <= gGlobalOptions
|
||||||
|
|
||||||
|
|
@ -17,7 +19,7 @@ proc AccessThreadLocalVar(p: BProc, s: PSym) =
|
||||||
if emulatedThreadVars() and not p.ThreadVarAccessed:
|
if emulatedThreadVars() and not p.ThreadVarAccessed:
|
||||||
p.ThreadVarAccessed = true
|
p.ThreadVarAccessed = true
|
||||||
p.module.usesThreadVars = true
|
p.module.usesThreadVars = true
|
||||||
appf(p.s[cpsLocals], "NimThreadVars* NimTV;$n")
|
appf(p.s(cpsLocals), "NimThreadVars* NimTV;$n")
|
||||||
appcg(p, cpsInit, "NimTV=(NimThreadVars*)#GetThreadLocalVars();$n")
|
appcg(p, cpsInit, "NimTV=(NimThreadVars*)#GetThreadLocalVars();$n")
|
||||||
|
|
||||||
var
|
var
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@
|
||||||
## Generates traversal procs for the C backend. Traversal procs are only an
|
## Generates traversal procs for the C backend. Traversal procs are only an
|
||||||
## optimization; the GC works without them too.
|
## optimization; the GC works without them too.
|
||||||
|
|
||||||
|
# included from cgen.nim
|
||||||
|
|
||||||
type
|
type
|
||||||
TTraversalClosure {.pure, final.} = object
|
TTraversalClosure {.pure, final.} = object
|
||||||
p: BProc
|
p: BProc
|
||||||
|
|
@ -29,17 +31,17 @@ proc genTraverseProc(c: var TTraversalClosure, accessor: PRope, n: PNode) =
|
||||||
if (n.sons[0].kind != nkSym): InternalError(n.info, "genTraverseProc")
|
if (n.sons[0].kind != nkSym): InternalError(n.info, "genTraverseProc")
|
||||||
var p = c.p
|
var p = c.p
|
||||||
let disc = n.sons[0].sym
|
let disc = n.sons[0].sym
|
||||||
p.s[cpsStmts].appf("switch ($1.$2) {$n", accessor, disc.loc.r)
|
p.s(cpsStmts).appf("switch ($1.$2) {$n", accessor, disc.loc.r)
|
||||||
for i in countup(1, sonsLen(n) - 1):
|
for i in countup(1, sonsLen(n) - 1):
|
||||||
let branch = n.sons[i]
|
let branch = n.sons[i]
|
||||||
assert branch.kind in {nkOfBranch, nkElse}
|
assert branch.kind in {nkOfBranch, nkElse}
|
||||||
if branch.kind == nkOfBranch:
|
if branch.kind == nkOfBranch:
|
||||||
genCaseRange(c.p, branch)
|
genCaseRange(c.p, branch)
|
||||||
else:
|
else:
|
||||||
p.s[cpsStmts].appf("default:$n")
|
p.s(cpsStmts).appf("default:$n")
|
||||||
genTraverseProc(c, accessor, lastSon(branch))
|
genTraverseProc(c, accessor, lastSon(branch))
|
||||||
p.s[cpsStmts].appf("break;$n")
|
p.s(cpsStmts).appf("break;$n")
|
||||||
p.s[cpsStmts].appf("} $n")
|
p.s(cpsStmts).appf("} $n")
|
||||||
of nkSym:
|
of nkSym:
|
||||||
let field = n.sym
|
let field = n.sym
|
||||||
genTraverseProc(c, ropef("$1.$2", accessor, field.loc.r), field.loc.t)
|
genTraverseProc(c, ropef("$1.$2", accessor, field.loc.r), field.loc.t)
|
||||||
|
|
@ -61,10 +63,10 @@ proc genTraverseProc(c: var TTraversalClosure, accessor: PRope, typ: PType) =
|
||||||
let arraySize = lengthOrd(typ.sons[0])
|
let arraySize = lengthOrd(typ.sons[0])
|
||||||
var i: TLoc
|
var i: TLoc
|
||||||
getTemp(p, getSysType(tyInt), i)
|
getTemp(p, getSysType(tyInt), i)
|
||||||
appf(p.s[cpsStmts], "for ($1 = 0; $1 < $2; $1++) {$n",
|
appf(p.s(cpsStmts), "for ($1 = 0; $1 < $2; $1++) {$n",
|
||||||
i.r, arraySize.toRope)
|
i.r, arraySize.toRope)
|
||||||
genTraverseProc(c, ropef("$1[$2]", accessor, i.r), typ.sons[1])
|
genTraverseProc(c, ropef("$1[$2]", accessor, i.r), typ.sons[1])
|
||||||
appf(p.s[cpsStmts], "}$n")
|
appf(p.s(cpsStmts), "}$n")
|
||||||
of tyObject:
|
of tyObject:
|
||||||
for i in countup(0, sonsLen(typ) - 1):
|
for i in countup(0, sonsLen(typ) - 1):
|
||||||
genTraverseProc(c, accessor.parentObj, typ.sons[i])
|
genTraverseProc(c, accessor.parentObj, typ.sons[i])
|
||||||
|
|
@ -87,10 +89,10 @@ proc genTraverseProcSeq(c: var TTraversalClosure, accessor: PRope, typ: PType) =
|
||||||
assert typ.kind == tySequence
|
assert typ.kind == tySequence
|
||||||
var i: TLoc
|
var i: TLoc
|
||||||
getTemp(p, getSysType(tyInt), i)
|
getTemp(p, getSysType(tyInt), i)
|
||||||
appf(p.s[cpsStmts], "for ($1 = 0; $1 < $2->$3; $1++) {$n",
|
appf(p.s(cpsStmts), "for ($1 = 0; $1 < $2->$3; $1++) {$n",
|
||||||
i.r, accessor, toRope(if gCmd != cmdCompileToCpp: "Sup.len" else: "len"))
|
i.r, accessor, toRope(if gCmd != cmdCompileToCpp: "Sup.len" else: "len"))
|
||||||
genTraverseProc(c, ropef("$1->data[$2]", accessor, i.r), typ.sons[0])
|
genTraverseProc(c, ropef("$1->data[$2]", accessor, i.r), typ.sons[0])
|
||||||
appf(p.s[cpsStmts], "}$n")
|
appf(p.s(cpsStmts), "}$n")
|
||||||
|
|
||||||
proc genTraverseProc(m: BModule, typ: PType, reason: TTypeInfoReason): PRope =
|
proc genTraverseProc(m: BModule, typ: PType, reason: TTypeInfoReason): PRope =
|
||||||
var c: TTraversalClosure
|
var c: TTraversalClosure
|
||||||
|
|
@ -104,8 +106,8 @@ proc genTraverseProc(m: BModule, typ: PType, reason: TTypeInfoReason): PRope =
|
||||||
let header = ropef("N_NIMCALL(void, $1)(void* p, NI op)", result)
|
let header = ropef("N_NIMCALL(void, $1)(void* p, NI op)", result)
|
||||||
|
|
||||||
let t = getTypeDesc(m, typ)
|
let t = getTypeDesc(m, typ)
|
||||||
p.s[cpsLocals].appf("$1 a;$n", t)
|
p.s(cpsLocals).appf("$1 a;$n", t)
|
||||||
p.s[cpsInit].appf("a = ($1)p;$n", t)
|
p.s(cpsInit).appf("a = ($1)p;$n", t)
|
||||||
|
|
||||||
c.p = p
|
c.p = p
|
||||||
if typ.kind == tySequence:
|
if typ.kind == tySequence:
|
||||||
|
|
@ -118,7 +120,7 @@ proc genTraverseProc(m: BModule, typ: PType, reason: TTypeInfoReason): PRope =
|
||||||
genTraverseProc(c, "(*a)".toRope, typ.sons[0])
|
genTraverseProc(c, "(*a)".toRope, typ.sons[0])
|
||||||
|
|
||||||
let generatedProc = ropef("$1 {$n$2$3$4}$n",
|
let generatedProc = ropef("$1 {$n$2$3$4}$n",
|
||||||
[header, p.s[cpsLocals], p.s[cpsInit], p.s[cpsStmts]])
|
[header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)])
|
||||||
|
|
||||||
m.s[cfsProcHeaders].appf("$1;$n", header)
|
m.s[cfsProcHeaders].appf("$1;$n", header)
|
||||||
m.s[cfsProcs].app(generatedProc)
|
m.s[cfsProcs].app(generatedProc)
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ proc appcg(m: BModule, s: TCFileSection, frmt: TFormatStr,
|
||||||
|
|
||||||
proc appcg(p: BProc, s: TCProcSection, frmt: TFormatStr,
|
proc appcg(p: BProc, s: TCProcSection, frmt: TFormatStr,
|
||||||
args: openarray[PRope]) =
|
args: openarray[PRope]) =
|
||||||
app(p.s[s], ropecg(p.module, frmt, args))
|
app(p.s(s), ropecg(p.module, frmt, args))
|
||||||
|
|
||||||
proc safeLineNm(info: TLineInfo): int =
|
proc safeLineNm(info: TLineInfo): int =
|
||||||
result = toLinenumber(info)
|
result = toLinenumber(info)
|
||||||
|
|
@ -168,14 +168,14 @@ proc genCLineDir(r: var PRope, info: TLineInfo) =
|
||||||
|
|
||||||
proc genLineDir(p: BProc, t: PNode) =
|
proc genLineDir(p: BProc, t: PNode) =
|
||||||
var line = t.info.safeLineNm
|
var line = t.info.safeLineNm
|
||||||
genCLineDir(p.s[cpsStmts], t.info.toFullPath, line)
|
genCLineDir(p.s(cpsStmts), t.info.toFullPath, line)
|
||||||
if ({optStackTrace, optEndb} * p.Options == {optStackTrace, optEndb}) and
|
if ({optStackTrace, optEndb} * p.Options == {optStackTrace, optEndb}) and
|
||||||
(p.prc == nil or sfPure notin p.prc.flags):
|
(p.prc == nil or sfPure notin p.prc.flags):
|
||||||
appcg(p, cpsStmts, "#endb($1);$n", [toRope(line)])
|
appcg(p, cpsStmts, "#endb($1);$n", [toRope(line)])
|
||||||
elif ({optLineTrace, optStackTrace} * p.Options ==
|
elif ({optLineTrace, optStackTrace} * p.Options ==
|
||||||
{optLineTrace, optStackTrace}) and
|
{optLineTrace, optStackTrace}) and
|
||||||
(p.prc == nil or sfPure notin p.prc.flags):
|
(p.prc == nil or sfPure notin p.prc.flags):
|
||||||
appf(p.s[cpsStmts], "F.line = $1;F.filename = $2;$n",
|
appf(p.s(cpsStmts), "F.line = $1;F.filename = $2;$n",
|
||||||
[toRope(line), makeCString(toFilename(t.info).extractFilename)])
|
[toRope(line), makeCString(toFilename(t.info).extractFilename)])
|
||||||
|
|
||||||
include "ccgtypes.nim"
|
include "ccgtypes.nim"
|
||||||
|
|
@ -232,29 +232,29 @@ proc isComplexValueType(t: PType): bool {.inline.} =
|
||||||
proc zeroVar(p: BProc, loc: TLoc, containsGCref: bool) =
|
proc zeroVar(p: BProc, loc: TLoc, containsGCref: bool) =
|
||||||
if not isComplexValueType(skipTypes(loc.t, abstractVarRange)):
|
if not isComplexValueType(skipTypes(loc.t, abstractVarRange)):
|
||||||
if containsGcref and p.WithInLoop > 0:
|
if containsGcref and p.WithInLoop > 0:
|
||||||
appf(p.s[cpsInit], "$1 = 0;$n", [rdLoc(loc)])
|
appf(p.s(cpsInit), "$1 = 0;$n", [rdLoc(loc)])
|
||||||
var nilLoc: TLoc
|
var nilLoc: TLoc
|
||||||
initLoc(nilLoc, locTemp, loc.t, onStack)
|
initLoc(nilLoc, locTemp, loc.t, onStack)
|
||||||
nilLoc.r = toRope("NIM_NIL")
|
nilLoc.r = toRope("NIM_NIL")
|
||||||
# puts ``unsureAsgnRef`` etc to ``p.s[cpsStmts]``:
|
# puts ``unsureAsgnRef`` etc to ``p.s[cpsStmts]``:
|
||||||
genRefAssign(p, loc, nilLoc, {afSrcIsNil})
|
genRefAssign(p, loc, nilLoc, {afSrcIsNil})
|
||||||
else:
|
else:
|
||||||
appf(p.s[cpsStmts], "$1 = 0;$n", [rdLoc(loc)])
|
appf(p.s(cpsStmts), "$1 = 0;$n", [rdLoc(loc)])
|
||||||
else:
|
else:
|
||||||
if containsGcref and p.WithInLoop > 0:
|
if containsGcref and p.WithInLoop > 0:
|
||||||
appf(p.s[cpsInit], "memset((void*)$1, 0, sizeof($2));$n",
|
appf(p.s(cpsInit), "memset((void*)$1, 0, sizeof($2));$n",
|
||||||
[addrLoc(loc), rdLoc(loc)])
|
[addrLoc(loc), rdLoc(loc)])
|
||||||
genObjectInit(p, cpsInit, loc.t, loc, true)
|
genObjectInit(p, cpsInit, loc.t, loc, true)
|
||||||
appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
|
appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
|
||||||
[addrLoc(loc), genTypeInfo(p.module, loc.t)])
|
[addrLoc(loc), genTypeInfo(p.module, loc.t)])
|
||||||
else:
|
else:
|
||||||
appf(p.s[cpsStmts], "memset((void*)$1, 0, sizeof($2));$n",
|
appf(p.s(cpsStmts), "memset((void*)$1, 0, sizeof($2));$n",
|
||||||
[addrLoc(loc), rdLoc(loc)])
|
[addrLoc(loc), rdLoc(loc)])
|
||||||
genObjectInit(p, cpsStmts, loc.t, loc, true)
|
genObjectInit(p, cpsStmts, loc.t, loc, true)
|
||||||
|
|
||||||
proc zeroTemp(p: BProc, loc: TLoc) =
|
proc zeroTemp(p: BProc, loc: TLoc) =
|
||||||
if not isComplexValueType(skipTypes(loc.t, abstractVarRange)):
|
if not isComplexValueType(skipTypes(loc.t, abstractVarRange)):
|
||||||
appf(p.s[cpsStmts], "$1 = 0;$n", [rdLoc(loc)])
|
appf(p.s(cpsStmts), "$1 = 0;$n", [rdLoc(loc)])
|
||||||
when false:
|
when false:
|
||||||
var nilLoc: TLoc
|
var nilLoc: TLoc
|
||||||
initLoc(nilLoc, locTemp, loc.t, onStack)
|
initLoc(nilLoc, locTemp, loc.t, onStack)
|
||||||
|
|
@ -262,7 +262,7 @@ proc zeroTemp(p: BProc, loc: TLoc) =
|
||||||
# puts ``unsureAsgnRef`` etc to ``p.s[cpsStmts]``:
|
# puts ``unsureAsgnRef`` etc to ``p.s[cpsStmts]``:
|
||||||
genRefAssign(p, loc, nilLoc, {afSrcIsNil})
|
genRefAssign(p, loc, nilLoc, {afSrcIsNil})
|
||||||
else:
|
else:
|
||||||
appf(p.s[cpsStmts], "memset((void*)$1, 0, sizeof($2));$n",
|
appf(p.s(cpsStmts), "memset((void*)$1, 0, sizeof($2));$n",
|
||||||
[addrLoc(loc), rdLoc(loc)])
|
[addrLoc(loc), rdLoc(loc)])
|
||||||
# XXX no object init necessary for temporaries?
|
# XXX no object init necessary for temporaries?
|
||||||
when false:
|
when false:
|
||||||
|
|
@ -291,7 +291,7 @@ proc getTemp(p: BProc, t: PType, result: var TLoc) =
|
||||||
result.r = con("%LOC", toRope(p.labels))
|
result.r = con("%LOC", toRope(p.labels))
|
||||||
else:
|
else:
|
||||||
result.r = con("LOC", toRope(p.labels))
|
result.r = con("LOC", toRope(p.labels))
|
||||||
appf(p.s[cpsLocals], "$1 $2;$n", [getTypeDesc(p.module, t), result.r])
|
appf(p.s(cpsLocals), "$1 $2;$n", [getTypeDesc(p.module, t), result.r])
|
||||||
result.k = locTemp
|
result.k = locTemp
|
||||||
result.a = - 1
|
result.a = - 1
|
||||||
result.t = getUniqueType(t)
|
result.t = getUniqueType(t)
|
||||||
|
|
@ -361,7 +361,7 @@ proc allocParam(p: BProc, s: PSym) =
|
||||||
var tmp = con("%LOC", toRope(p.labels))
|
var tmp = con("%LOC", toRope(p.labels))
|
||||||
incl(s.loc.flags, lfParamCopy)
|
incl(s.loc.flags, lfParamCopy)
|
||||||
incl(s.loc.flags, lfIndirect)
|
incl(s.loc.flags, lfIndirect)
|
||||||
appf(p.s[cpsInit], "$1 = alloca $3$n" & "store $3 $2, $3* $1$n",
|
appf(p.s(cpsInit), "$1 = alloca $3$n" & "store $3 $2, $3* $1$n",
|
||||||
[tmp, s.loc.r, getTypeDesc(p.module, s.loc.t)])
|
[tmp, s.loc.r, getTypeDesc(p.module, s.loc.t)])
|
||||||
s.loc.r = tmp
|
s.loc.r = tmp
|
||||||
|
|
||||||
|
|
@ -371,7 +371,7 @@ proc localDebugInfo(p: BProc, s: PSym) =
|
||||||
if skipTypes(s.typ, abstractVar).kind == tyOpenArray: return
|
if skipTypes(s.typ, abstractVar).kind == tyOpenArray: return
|
||||||
var a = con("&", s.loc.r)
|
var a = con("&", s.loc.r)
|
||||||
if (s.kind == skParam) and ccgIntroducedPtr(s): a = s.loc.r
|
if (s.kind == skParam) and ccgIntroducedPtr(s): a = s.loc.r
|
||||||
appf(p.s[cpsInit],
|
appf(p.s(cpsInit),
|
||||||
"F.s[$1].address = (void*)$3; F.s[$1].typ = $4; F.s[$1].name = $2;$n",
|
"F.s[$1].address = (void*)$3; F.s[$1].typ = $4; F.s[$1].name = $2;$n",
|
||||||
[toRope(p.frameLen), makeCString(normalize(s.name.s)), a,
|
[toRope(p.frameLen), makeCString(normalize(s.name.s)), a,
|
||||||
genTypeInfo(p.module, s.loc.t)])
|
genTypeInfo(p.module, s.loc.t)])
|
||||||
|
|
@ -384,13 +384,13 @@ proc assignLocalVar(p: BProc, s: PSym) =
|
||||||
if s.loc.k == locNone:
|
if s.loc.k == locNone:
|
||||||
fillLoc(s.loc, locLocalVar, s.typ, mangleName(s), OnStack)
|
fillLoc(s.loc, locLocalVar, s.typ, mangleName(s), OnStack)
|
||||||
if s.kind == skLet: incl(s.loc.flags, lfNoDeepCopy)
|
if s.kind == skLet: incl(s.loc.flags, lfNoDeepCopy)
|
||||||
app(p.s[cpsLocals], getTypeDesc(p.module, s.loc.t))
|
app(p.s(cpsLocals), getTypeDesc(p.module, s.loc.t))
|
||||||
if sfRegister in s.flags: app(p.s[cpsLocals], " register")
|
if sfRegister in s.flags: app(p.s(cpsLocals), " register")
|
||||||
#elif skipTypes(s.typ, abstractInst).kind in GcTypeKinds:
|
#elif skipTypes(s.typ, abstractInst).kind in GcTypeKinds:
|
||||||
# app(p.s[cpsLocals], " GC_GUARD")
|
# app(p.s[cpsLocals], " GC_GUARD")
|
||||||
if (sfVolatile in s.flags) or (p.nestedTryStmts.len > 0):
|
if (sfVolatile in s.flags) or (p.nestedTryStmts.len > 0):
|
||||||
app(p.s[cpsLocals], " volatile")
|
app(p.s(cpsLocals), " volatile")
|
||||||
appf(p.s[cpsLocals], " $1;$n", [s.loc.r])
|
appf(p.s(cpsLocals), " $1;$n", [s.loc.r])
|
||||||
localDebugInfo(p, s)
|
localDebugInfo(p, s)
|
||||||
|
|
||||||
include ccgthreadvars
|
include ccgthreadvars
|
||||||
|
|
@ -433,7 +433,7 @@ proc getLabel(p: BProc): TLabel =
|
||||||
result = con("LA", toRope(p.labels))
|
result = con("LA", toRope(p.labels))
|
||||||
|
|
||||||
proc fixLabel(p: BProc, labl: TLabel) =
|
proc fixLabel(p: BProc, labl: TLabel) =
|
||||||
appf(p.s[cpsStmts], "$1: ;$n", [labl])
|
appf(p.s(cpsStmts), "$1: ;$n", [labl])
|
||||||
|
|
||||||
proc genVarPrototype(m: BModule, sym: PSym)
|
proc genVarPrototype(m: BModule, sym: PSym)
|
||||||
proc requestConstImpl(p: BProc, sym: PSym)
|
proc requestConstImpl(p: BProc, sym: PSym)
|
||||||
|
|
@ -481,9 +481,9 @@ proc loadDynamicLib(m: BModule, lib: PLib) =
|
||||||
var p = newProc(nil, m)
|
var p = newProc(nil, m)
|
||||||
var dest: TLoc
|
var dest: TLoc
|
||||||
initLocExpr(p, lib.path, dest)
|
initLocExpr(p, lib.path, dest)
|
||||||
app(m.s[cfsVars], p.s[cpsLocals])
|
app(m.s[cfsVars], p.s(cpsLocals))
|
||||||
app(m.s[cfsDynLibInit], p.s[cpsInit])
|
app(m.s[cfsDynLibInit], p.s(cpsInit))
|
||||||
app(m.s[cfsDynLibInit], p.s[cpsStmts])
|
app(m.s[cfsDynLibInit], p.s(cpsStmts))
|
||||||
appcg(m, m.s[cfsDynLibInit],
|
appcg(m, m.s[cfsDynLibInit],
|
||||||
"if (!($1 = #nimLoadLibrary($2))) #nimLoadLibraryError($2);$n",
|
"if (!($1 = #nimLoadLibrary($2))) #nimLoadLibraryError($2);$n",
|
||||||
[tmp, rdLoc(dest)])
|
[tmp, rdLoc(dest)])
|
||||||
|
|
@ -551,13 +551,13 @@ proc getFrameDecl(p: BProc) =
|
||||||
[toRope(p.frameLen)])
|
[toRope(p.frameLen)])
|
||||||
else:
|
else:
|
||||||
slots = nil
|
slots = nil
|
||||||
appff(p.s[cpsLocals], "volatile struct {TFrame* prev;" &
|
appff(p.s(cpsLocals), "volatile struct {TFrame* prev;" &
|
||||||
"NCSTRING procname;NI line;NCSTRING filename;" &
|
"NCSTRING procname;NI line;NCSTRING filename;" &
|
||||||
"NI len;$n$1} F;$n",
|
"NI len;$n$1} F;$n",
|
||||||
"%TF = type {%TFrame*, i8*, %NI, %NI$1}$n" &
|
"%TF = type {%TFrame*, i8*, %NI, %NI$1}$n" &
|
||||||
"%F = alloca %TF$n", [slots])
|
"%F = alloca %TF$n", [slots])
|
||||||
inc(p.labels)
|
inc(p.labels)
|
||||||
prepend(p.s[cpsInit], ropeff("F.len = $1;$n",
|
prepend(p.s(cpsInit), ropeff("F.len = $1;$n",
|
||||||
"%LOC$2 = getelementptr %TF %F, %NI 4$n" &
|
"%LOC$2 = getelementptr %TF %F, %NI 4$n" &
|
||||||
"store %NI $1, %NI* %LOC$2$n", [toRope(p.frameLen), toRope(p.labels)]))
|
"store %NI $1, %NI* %LOC$2$n", [toRope(p.frameLen), toRope(p.labels)]))
|
||||||
|
|
||||||
|
|
@ -613,32 +613,32 @@ proc genProcAux(m: BModule, prc: PSym) =
|
||||||
var generatedProc: PRope
|
var generatedProc: PRope
|
||||||
if sfPure in prc.flags:
|
if sfPure in prc.flags:
|
||||||
generatedProc = ropeff("$1 {$n$2$3$4}$n", "define $1 {$n$2$3$4}$n",
|
generatedProc = ropeff("$1 {$n$2$3$4}$n", "define $1 {$n$2$3$4}$n",
|
||||||
[header, p.s[cpsLocals], p.s[cpsInit], p.s[cpsStmts]])
|
[header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)])
|
||||||
else:
|
else:
|
||||||
generatedProc = ropeff("$1 {$n", "define $1 {$n", [header])
|
generatedProc = ropeff("$1 {$n", "define $1 {$n", [header])
|
||||||
app(generatedProc, initGCFrame(p))
|
app(generatedProc, initGCFrame(p))
|
||||||
if optStackTrace in prc.options:
|
if optStackTrace in prc.options:
|
||||||
getFrameDecl(p)
|
getFrameDecl(p)
|
||||||
app(generatedProc, p.s[cpsLocals])
|
app(generatedProc, p.s(cpsLocals))
|
||||||
var procname = CStringLit(p, generatedProc, prc.name.s)
|
var procname = CStringLit(p, generatedProc, prc.name.s)
|
||||||
var filename = CStringLit(p, generatedProc, toFilename(prc.info))
|
var filename = CStringLit(p, generatedProc, toFilename(prc.info))
|
||||||
app(generatedProc, initFrame(p, procname, filename))
|
app(generatedProc, initFrame(p, procname, filename))
|
||||||
else:
|
else:
|
||||||
app(generatedProc, p.s[cpsLocals])
|
app(generatedProc, p.s(cpsLocals))
|
||||||
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
|
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
|
||||||
if gProcProfile >= 64 * 1024:
|
if gProcProfile >= 64 * 1024:
|
||||||
InternalError(prc.info, "too many procedures for profiling")
|
InternalError(prc.info, "too many procedures for profiling")
|
||||||
discard cgsym(m, "profileData")
|
discard cgsym(m, "profileData")
|
||||||
appf(p.s[cpsLocals], "ticks NIM_profilingStart;$n")
|
appf(p.s(cpsLocals), "ticks NIM_profilingStart;$n")
|
||||||
if prc.loc.a < 0:
|
if prc.loc.a < 0:
|
||||||
appf(m.s[cfsDebugInit], "profileData[$1].procname = $2;$n", [
|
appf(m.s[cfsDebugInit], "profileData[$1].procname = $2;$n", [
|
||||||
toRope(gProcProfile),
|
toRope(gProcProfile),
|
||||||
makeCString(prc.name.s)])
|
makeCString(prc.name.s)])
|
||||||
prc.loc.a = gProcProfile
|
prc.loc.a = gProcProfile
|
||||||
inc(gProcProfile)
|
inc(gProcProfile)
|
||||||
prepend(p.s[cpsInit], ropef("NIM_profilingStart = getticks();$n"))
|
prepend(p.s(cpsInit), ropef("NIM_profilingStart = getticks();$n"))
|
||||||
app(generatedProc, p.s[cpsInit])
|
app(generatedProc, p.s(cpsInit))
|
||||||
app(generatedProc, p.s[cpsStmts])
|
app(generatedProc, p.s(cpsStmts))
|
||||||
if p.beforeRetNeeded: appf(generatedProc, "BeforeRet: $n;")
|
if p.beforeRetNeeded: appf(generatedProc, "BeforeRet: $n;")
|
||||||
app(generatedProc, deinitGCFrame(p))
|
app(generatedProc, deinitGCFrame(p))
|
||||||
if optStackTrace in prc.options: app(generatedProc, deinitFrame(p))
|
if optStackTrace in prc.options: app(generatedProc, deinitFrame(p))
|
||||||
|
|
@ -858,8 +858,8 @@ proc genInitCode(m: BModule) =
|
||||||
app(prc, initGCFrame(m.initProc))
|
app(prc, initGCFrame(m.initProc))
|
||||||
|
|
||||||
app(prc, genSectionStart(cpsLocals))
|
app(prc, genSectionStart(cpsLocals))
|
||||||
app(prc, m.initProc.s[cpsLocals])
|
app(prc, m.initProc.s(cpsLocals))
|
||||||
app(prc, m.preInitProc.s[cpsLocals])
|
app(prc, m.preInitProc.s(cpsLocals))
|
||||||
app(prc, genSectionEnd(cpsLocals))
|
app(prc, genSectionEnd(cpsLocals))
|
||||||
|
|
||||||
app(prc, genSectionStart(cfsTypeInit1))
|
app(prc, genSectionStart(cfsTypeInit1))
|
||||||
|
|
@ -876,13 +876,13 @@ proc genInitCode(m: BModule) =
|
||||||
app(prc, genSectionEnd(i))
|
app(prc, genSectionEnd(i))
|
||||||
|
|
||||||
app(prc, genSectionStart(cpsInit))
|
app(prc, genSectionStart(cpsInit))
|
||||||
app(prc, m.preInitProc.s[cpsInit])
|
app(prc, m.preInitProc.s(cpsInit))
|
||||||
app(prc, m.initProc.s[cpsInit])
|
app(prc, m.initProc.s(cpsInit))
|
||||||
app(prc, genSectionEnd(cpsInit))
|
app(prc, genSectionEnd(cpsInit))
|
||||||
|
|
||||||
app(prc, genSectionStart(cpsStmts))
|
app(prc, genSectionStart(cpsStmts))
|
||||||
app(prc, m.preInitProc.s[cpsStmts])
|
app(prc, m.preInitProc.s(cpsStmts))
|
||||||
app(prc, m.initProc.s[cpsStmts])
|
app(prc, m.initProc.s(cpsStmts))
|
||||||
if optStackTrace in m.initProc.options and not m.PreventStackTrace:
|
if optStackTrace in m.initProc.options and not m.PreventStackTrace:
|
||||||
app(prc, deinitFrame(m.initProc))
|
app(prc, deinitFrame(m.initProc))
|
||||||
app(prc, genSectionEnd(cpsStmts))
|
app(prc, genSectionEnd(cpsStmts))
|
||||||
|
|
|
||||||
|
|
@ -48,13 +48,14 @@ type
|
||||||
TCProcSections* = array[TCProcSection, PRope] # represents a generated C proc
|
TCProcSections* = array[TCProcSection, PRope] # represents a generated C proc
|
||||||
BModule* = ref TCGen
|
BModule* = ref TCGen
|
||||||
BProc* = ref TCProc
|
BProc* = ref TCProc
|
||||||
TBlock{.final.} = object
|
TBlock*{.final.} = object
|
||||||
id*: int # the ID of the label; positive means that it
|
id*: int # the ID of the label; positive means that it
|
||||||
# has been used (i.e. the label should be emitted)
|
label*: PRope # generated text for the label
|
||||||
|
# nil if label is not used
|
||||||
nestedTryStmts*: int # how many try statements is it nested into
|
nestedTryStmts*: int # how many try statements is it nested into
|
||||||
|
sections*: TCProcSections # the code beloging
|
||||||
|
|
||||||
TCProc{.final.} = object # represents C proc that is currently generated
|
TCProc{.final.} = object # represents C proc that is currently generated
|
||||||
s*: TCProcSections # the procs sections; short name for readability
|
|
||||||
prc*: PSym # the Nimrod proc that this C proc belongs to
|
prc*: PSym # the Nimrod proc that this C proc belongs to
|
||||||
BeforeRetNeeded*: bool # true iff 'BeforeRet' label for proc is needed
|
BeforeRetNeeded*: bool # true iff 'BeforeRet' label for proc is needed
|
||||||
ThreadVarAccessed*: bool # true if the proc already accessed some threadvar
|
ThreadVarAccessed*: bool # true if the proc already accessed some threadvar
|
||||||
|
|
@ -113,6 +114,8 @@ var
|
||||||
gForwardedProcsCounter*: int = 0
|
gForwardedProcsCounter*: int = 0
|
||||||
gNimDat*: BModule # generated global data
|
gNimDat*: BModule # generated global data
|
||||||
|
|
||||||
|
proc s*(prc: BProc, s: TCProcSection): var PRope {.inline.} =
|
||||||
|
result = prc.blocks[prc.blocks.len - 1].sections[s]
|
||||||
|
|
||||||
proc newProc*(prc: PSym, module: BModule): BProc =
|
proc newProc*(prc: PSym, module: BModule): BProc =
|
||||||
new(result)
|
new(result)
|
||||||
|
|
@ -120,6 +123,6 @@ proc newProc*(prc: PSym, module: BModule): BProc =
|
||||||
result.module = module
|
result.module = module
|
||||||
if prc != nil: result.options = prc.options
|
if prc != nil: result.options = prc.options
|
||||||
else: result.options = gOptions
|
else: result.options = gOptions
|
||||||
result.blocks = @[]
|
newSeq(result.blocks, 1)
|
||||||
result.nestedTryStmts = @[]
|
result.nestedTryStmts = @[]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue