mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 17:34:43 +00:00
C variables are created in their enclosing block instead of their enclosing function
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
#
|
||||
# included from cgen.nim
|
||||
|
||||
proc leftAppearsOnRightSide(le, ri: PNode): bool =
|
||||
if le != nil:
|
||||
@@ -34,15 +36,15 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc, pl: PRope) =
|
||||
resetLoc(p, d)
|
||||
app(pl, addrLoc(d))
|
||||
app(pl, ")")
|
||||
app(p.s[cpsStmts], pl)
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
app(p.s(cpsStmts), pl)
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
else:
|
||||
var tmp: TLoc
|
||||
getTemp(p, typ.sons[0], tmp)
|
||||
app(pl, addrLoc(tmp))
|
||||
app(pl, ")")
|
||||
app(p.s[cpsStmts], pl)
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
app(p.s(cpsStmts), pl)
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
genAssignment(p, d, tmp, {}) # no need for deep copying
|
||||
else:
|
||||
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
|
||||
else:
|
||||
app(pl, ")")
|
||||
app(p.s[cpsStmts], pl)
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
app(p.s(cpsStmts), pl)
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
|
||||
proc isInCurrentFrame(p: BProc, n: PNode): bool =
|
||||
# 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, ", ")
|
||||
|
||||
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)
|
||||
if typ.sons[0] != nil:
|
||||
@@ -179,13 +181,13 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
||||
resetLoc(p, d)
|
||||
app(pl, addrLoc(d))
|
||||
genCallPattern()
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
else:
|
||||
var tmp: TLoc
|
||||
getTemp(p, typ.sons[0], tmp)
|
||||
app(pl, addrLoc(tmp))
|
||||
genCallPattern()
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
genAssignment(p, d, tmp, {}) # no need for deep copying
|
||||
else:
|
||||
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
|
||||
else:
|
||||
genCallPattern()
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
|
||||
proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) =
|
||||
var op, a: TLoc
|
||||
@@ -261,15 +263,15 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
|
||||
app(pl, "Result: ")
|
||||
app(pl, addrLoc(d))
|
||||
app(pl, "]")
|
||||
app(p.s[cpsStmts], pl)
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
app(p.s(cpsStmts), pl)
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
else:
|
||||
var tmp: TLoc
|
||||
getTemp(p, typ.sons[0], tmp)
|
||||
app(pl, addrLoc(tmp))
|
||||
app(pl, "]")
|
||||
app(p.s[cpsStmts], pl)
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
app(p.s(cpsStmts), pl)
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
genAssignment(p, d, tmp, {}) # no need for deep copying
|
||||
else:
|
||||
app(pl, "]")
|
||||
@@ -281,8 +283,8 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
|
||||
genAssignment(p, d, list, {}) # no need for deep copying
|
||||
else:
|
||||
app(pl, "]")
|
||||
app(p.s[cpsStmts], pl)
|
||||
appf(p.s[cpsStmts], ";$n")
|
||||
app(p.s(cpsStmts), pl)
|
||||
appf(p.s(cpsStmts), ";$n")
|
||||
|
||||
proc genCall(p: BProc, e: PNode, d: var TLoc) =
|
||||
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) =
|
||||
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)
|
||||
elif dest.s == OnHeap:
|
||||
# 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], '$1 = $2;$n', [rdLoc(dest), rdLoc(src)])
|
||||
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)])
|
||||
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)])
|
||||
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)])
|
||||
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)
|
||||
L = getLabel(p)
|
||||
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:
|
||||
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)
|
||||
fixLabel(p, L)
|
||||
if d.k == locNone:
|
||||
@@ -771,9 +771,9 @@ proc genIfExpr(p: BProc, n: PNode, d: var TLoc) =
|
||||
of nkElifExpr:
|
||||
initLocExpr(p, it.sons[0], a)
|
||||
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)
|
||||
appf(p.s[cpsStmts], "goto $1;$n", [Lend])
|
||||
appf(p.s(cpsStmts), "goto $1;$n", [Lend])
|
||||
fixLabel(p, Lelse)
|
||||
of nkElseExpr:
|
||||
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()])
|
||||
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)])
|
||||
app(p.s[cpsStmts], appends)
|
||||
app(p.s(cpsStmts), appends)
|
||||
if d.k == locNone:
|
||||
d = 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",
|
||||
[rdLoc(dest), lens, toRope(L)])
|
||||
keepAlive(p, dest)
|
||||
app(p.s[cpsStmts], appends)
|
||||
app(p.s(cpsStmts), appends)
|
||||
|
||||
proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
|
||||
# seq &= x -->
|
||||
@@ -1171,7 +1171,7 @@ proc binaryStmtInExcl(p: BProc, e: PNode, d: var TLoc, frmt: string) =
|
||||
assert(d.k == locNone)
|
||||
InitLocExpr(p, e.sons[1], a)
|
||||
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) =
|
||||
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[2], b)
|
||||
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)])
|
||||
of mEqSet:
|
||||
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[2], b)
|
||||
if d.k == locNone: getTemp(p, a.t, d)
|
||||
appf(p.s[cpsStmts],
|
||||
appf(p.s(cpsStmts),
|
||||
"for ($1 = 0; $1 < $2; $1++) $n" &
|
||||
" $3[$1] = $4[$1] $6 $5[$1];$n", [
|
||||
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 getSize(e.typ) > 8:
|
||||
# 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):
|
||||
if e.sons[i].kind == nkRange:
|
||||
getTemp(p, getSysType(tyInt), idx) # our counter
|
||||
initLocExpr(p, e.sons[i].sons[0], a)
|
||||
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),
|
||||
rdSetElemLoc(a, e.typ), rdSetElemLoc(b, e.typ)])
|
||||
else:
|
||||
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)])
|
||||
else:
|
||||
# small set
|
||||
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):
|
||||
if e.sons[i].kind == nkRange:
|
||||
getTemp(p, getSysType(tyInt), idx) # our counter
|
||||
initLocExpr(p, e.sons[i].sons[0], a)
|
||||
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", [
|
||||
rdLoc(idx), rdLoc(d), rdSetElemLoc(a, e.typ),
|
||||
rdSetElemLoc(b, e.typ)])
|
||||
else:
|
||||
initLocExpr(p, e.sons[i], a)
|
||||
appf(p.s[cpsStmts],
|
||||
appf(p.s(cpsStmts),
|
||||
"$1 |=(1<<((" & ts & ")($2)%(sizeof(" & ts & ")*8)));$n",
|
||||
[rdLoc(d), rdSetElemLoc(a, e.typ)])
|
||||
|
||||
|
||||
@@ -280,7 +280,7 @@ proc mergeRequired*(m: BModule): bool =
|
||||
#echo "not empty: ", i, " ", ropeToStr(m.s[i])
|
||||
return true
|
||||
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])
|
||||
return true
|
||||
|
||||
@@ -292,5 +292,5 @@ proc mergeFiles*(cfilename: string, m: BModule) =
|
||||
for i in low(TCFileSection)..high(TCFileSection):
|
||||
m.s[i] = con(old.f[i], m.s[i])
|
||||
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
|
||||
# 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
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
## Thread var support for crappy architectures that lack native support for
|
||||
## thread local storage. (**Thank you Mac OS X!**)
|
||||
|
||||
# included from cgen.nim
|
||||
|
||||
proc emulatedThreadVars(): bool {.inline.} =
|
||||
result = {optThreads, optTlsEmulation} <= gGlobalOptions
|
||||
|
||||
@@ -17,7 +19,7 @@ proc AccessThreadLocalVar(p: BProc, s: PSym) =
|
||||
if emulatedThreadVars() and not p.ThreadVarAccessed:
|
||||
p.ThreadVarAccessed = 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")
|
||||
|
||||
var
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
## Generates traversal procs for the C backend. Traversal procs are only an
|
||||
## optimization; the GC works without them too.
|
||||
|
||||
# included from cgen.nim
|
||||
|
||||
type
|
||||
TTraversalClosure {.pure, final.} = object
|
||||
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")
|
||||
var p = c.p
|
||||
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):
|
||||
let branch = n.sons[i]
|
||||
assert branch.kind in {nkOfBranch, nkElse}
|
||||
if branch.kind == nkOfBranch:
|
||||
genCaseRange(c.p, branch)
|
||||
else:
|
||||
p.s[cpsStmts].appf("default:$n")
|
||||
p.s(cpsStmts).appf("default:$n")
|
||||
genTraverseProc(c, accessor, lastSon(branch))
|
||||
p.s[cpsStmts].appf("break;$n")
|
||||
p.s[cpsStmts].appf("} $n")
|
||||
p.s(cpsStmts).appf("break;$n")
|
||||
p.s(cpsStmts).appf("} $n")
|
||||
of nkSym:
|
||||
let field = n.sym
|
||||
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])
|
||||
var i: TLoc
|
||||
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)
|
||||
genTraverseProc(c, ropef("$1[$2]", accessor, i.r), typ.sons[1])
|
||||
appf(p.s[cpsStmts], "}$n")
|
||||
appf(p.s(cpsStmts), "}$n")
|
||||
of tyObject:
|
||||
for i in countup(0, sonsLen(typ) - 1):
|
||||
genTraverseProc(c, accessor.parentObj, typ.sons[i])
|
||||
@@ -87,10 +89,10 @@ proc genTraverseProcSeq(c: var TTraversalClosure, accessor: PRope, typ: PType) =
|
||||
assert typ.kind == tySequence
|
||||
var i: TLoc
|
||||
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"))
|
||||
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 =
|
||||
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 t = getTypeDesc(m, typ)
|
||||
p.s[cpsLocals].appf("$1 a;$n", t)
|
||||
p.s[cpsInit].appf("a = ($1)p;$n", t)
|
||||
p.s(cpsLocals).appf("$1 a;$n", t)
|
||||
p.s(cpsInit).appf("a = ($1)p;$n", t)
|
||||
|
||||
c.p = p
|
||||
if typ.kind == tySequence:
|
||||
@@ -118,7 +120,7 @@ proc genTraverseProc(m: BModule, typ: PType, reason: TTypeInfoReason): PRope =
|
||||
genTraverseProc(c, "(*a)".toRope, typ.sons[0])
|
||||
|
||||
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[cfsProcs].app(generatedProc)
|
||||
|
||||
@@ -151,7 +151,7 @@ proc appcg(m: BModule, s: TCFileSection, frmt: TFormatStr,
|
||||
|
||||
proc appcg(p: BProc, s: TCProcSection, frmt: TFormatStr,
|
||||
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 =
|
||||
result = toLinenumber(info)
|
||||
@@ -168,14 +168,14 @@ proc genCLineDir(r: var PRope, info: TLineInfo) =
|
||||
|
||||
proc genLineDir(p: BProc, t: PNode) =
|
||||
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
|
||||
(p.prc == nil or sfPure notin p.prc.flags):
|
||||
appcg(p, cpsStmts, "#endb($1);$n", [toRope(line)])
|
||||
elif ({optLineTrace, optStackTrace} * p.Options ==
|
||||
{optLineTrace, optStackTrace}) and
|
||||
(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)])
|
||||
|
||||
include "ccgtypes.nim"
|
||||
@@ -232,29 +232,29 @@ proc isComplexValueType(t: PType): bool {.inline.} =
|
||||
proc zeroVar(p: BProc, loc: TLoc, containsGCref: bool) =
|
||||
if not isComplexValueType(skipTypes(loc.t, abstractVarRange)):
|
||||
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
|
||||
initLoc(nilLoc, locTemp, loc.t, onStack)
|
||||
nilLoc.r = toRope("NIM_NIL")
|
||||
# puts ``unsureAsgnRef`` etc to ``p.s[cpsStmts]``:
|
||||
genRefAssign(p, loc, nilLoc, {afSrcIsNil})
|
||||
else:
|
||||
appf(p.s[cpsStmts], "$1 = 0;$n", [rdLoc(loc)])
|
||||
appf(p.s(cpsStmts), "$1 = 0;$n", [rdLoc(loc)])
|
||||
else:
|
||||
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)])
|
||||
genObjectInit(p, cpsInit, loc.t, loc, true)
|
||||
appcg(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
|
||||
[addrLoc(loc), genTypeInfo(p.module, loc.t)])
|
||||
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)])
|
||||
genObjectInit(p, cpsStmts, loc.t, loc, true)
|
||||
|
||||
proc zeroTemp(p: BProc, loc: TLoc) =
|
||||
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:
|
||||
var nilLoc: TLoc
|
||||
initLoc(nilLoc, locTemp, loc.t, onStack)
|
||||
@@ -262,7 +262,7 @@ proc zeroTemp(p: BProc, loc: TLoc) =
|
||||
# puts ``unsureAsgnRef`` etc to ``p.s[cpsStmts]``:
|
||||
genRefAssign(p, loc, nilLoc, {afSrcIsNil})
|
||||
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)])
|
||||
# XXX no object init necessary for temporaries?
|
||||
when false:
|
||||
@@ -291,7 +291,7 @@ proc getTemp(p: BProc, t: PType, result: var TLoc) =
|
||||
result.r = con("%LOC", toRope(p.labels))
|
||||
else:
|
||||
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.a = - 1
|
||||
result.t = getUniqueType(t)
|
||||
@@ -361,7 +361,7 @@ proc allocParam(p: BProc, s: PSym) =
|
||||
var tmp = con("%LOC", toRope(p.labels))
|
||||
incl(s.loc.flags, lfParamCopy)
|
||||
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)])
|
||||
s.loc.r = tmp
|
||||
|
||||
@@ -371,7 +371,7 @@ proc localDebugInfo(p: BProc, s: PSym) =
|
||||
if skipTypes(s.typ, abstractVar).kind == tyOpenArray: return
|
||||
var a = con("&", 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",
|
||||
[toRope(p.frameLen), makeCString(normalize(s.name.s)), a,
|
||||
genTypeInfo(p.module, s.loc.t)])
|
||||
@@ -384,13 +384,13 @@ proc assignLocalVar(p: BProc, s: PSym) =
|
||||
if s.loc.k == locNone:
|
||||
fillLoc(s.loc, locLocalVar, s.typ, mangleName(s), OnStack)
|
||||
if s.kind == skLet: incl(s.loc.flags, lfNoDeepCopy)
|
||||
app(p.s[cpsLocals], getTypeDesc(p.module, s.loc.t))
|
||||
if sfRegister in s.flags: app(p.s[cpsLocals], " register")
|
||||
app(p.s(cpsLocals), getTypeDesc(p.module, s.loc.t))
|
||||
if sfRegister in s.flags: app(p.s(cpsLocals), " register")
|
||||
#elif skipTypes(s.typ, abstractInst).kind in GcTypeKinds:
|
||||
# app(p.s[cpsLocals], " GC_GUARD")
|
||||
if (sfVolatile in s.flags) or (p.nestedTryStmts.len > 0):
|
||||
app(p.s[cpsLocals], " volatile")
|
||||
appf(p.s[cpsLocals], " $1;$n", [s.loc.r])
|
||||
app(p.s(cpsLocals), " volatile")
|
||||
appf(p.s(cpsLocals), " $1;$n", [s.loc.r])
|
||||
localDebugInfo(p, s)
|
||||
|
||||
include ccgthreadvars
|
||||
@@ -433,7 +433,7 @@ proc getLabel(p: BProc): TLabel =
|
||||
result = con("LA", toRope(p.labels))
|
||||
|
||||
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 requestConstImpl(p: BProc, sym: PSym)
|
||||
@@ -481,9 +481,9 @@ proc loadDynamicLib(m: BModule, lib: PLib) =
|
||||
var p = newProc(nil, m)
|
||||
var dest: TLoc
|
||||
initLocExpr(p, lib.path, dest)
|
||||
app(m.s[cfsVars], p.s[cpsLocals])
|
||||
app(m.s[cfsDynLibInit], p.s[cpsInit])
|
||||
app(m.s[cfsDynLibInit], p.s[cpsStmts])
|
||||
app(m.s[cfsVars], p.s(cpsLocals))
|
||||
app(m.s[cfsDynLibInit], p.s(cpsInit))
|
||||
app(m.s[cfsDynLibInit], p.s(cpsStmts))
|
||||
appcg(m, m.s[cfsDynLibInit],
|
||||
"if (!($1 = #nimLoadLibrary($2))) #nimLoadLibraryError($2);$n",
|
||||
[tmp, rdLoc(dest)])
|
||||
@@ -551,13 +551,13 @@ proc getFrameDecl(p: BProc) =
|
||||
[toRope(p.frameLen)])
|
||||
else:
|
||||
slots = nil
|
||||
appff(p.s[cpsLocals], "volatile struct {TFrame* prev;" &
|
||||
appff(p.s(cpsLocals), "volatile struct {TFrame* prev;" &
|
||||
"NCSTRING procname;NI line;NCSTRING filename;" &
|
||||
"NI len;$n$1} F;$n",
|
||||
"%TF = type {%TFrame*, i8*, %NI, %NI$1}$n" &
|
||||
"%F = alloca %TF$n", [slots])
|
||||
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" &
|
||||
"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
|
||||
if sfPure in prc.flags:
|
||||
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:
|
||||
generatedProc = ropeff("$1 {$n", "define $1 {$n", [header])
|
||||
app(generatedProc, initGCFrame(p))
|
||||
if optStackTrace in prc.options:
|
||||
getFrameDecl(p)
|
||||
app(generatedProc, p.s[cpsLocals])
|
||||
app(generatedProc, p.s(cpsLocals))
|
||||
var procname = CStringLit(p, generatedProc, prc.name.s)
|
||||
var filename = CStringLit(p, generatedProc, toFilename(prc.info))
|
||||
app(generatedProc, initFrame(p, procname, filename))
|
||||
else:
|
||||
app(generatedProc, p.s[cpsLocals])
|
||||
app(generatedProc, p.s(cpsLocals))
|
||||
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
|
||||
if gProcProfile >= 64 * 1024:
|
||||
InternalError(prc.info, "too many procedures for profiling")
|
||||
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:
|
||||
appf(m.s[cfsDebugInit], "profileData[$1].procname = $2;$n", [
|
||||
toRope(gProcProfile),
|
||||
makeCString(prc.name.s)])
|
||||
prc.loc.a = gProcProfile
|
||||
inc(gProcProfile)
|
||||
prepend(p.s[cpsInit], ropef("NIM_profilingStart = getticks();$n"))
|
||||
app(generatedProc, p.s[cpsInit])
|
||||
app(generatedProc, p.s[cpsStmts])
|
||||
prepend(p.s(cpsInit), ropef("NIM_profilingStart = getticks();$n"))
|
||||
app(generatedProc, p.s(cpsInit))
|
||||
app(generatedProc, p.s(cpsStmts))
|
||||
if p.beforeRetNeeded: appf(generatedProc, "BeforeRet: $n;")
|
||||
app(generatedProc, deinitGCFrame(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, genSectionStart(cpsLocals))
|
||||
app(prc, m.initProc.s[cpsLocals])
|
||||
app(prc, m.preInitProc.s[cpsLocals])
|
||||
app(prc, m.initProc.s(cpsLocals))
|
||||
app(prc, m.preInitProc.s(cpsLocals))
|
||||
app(prc, genSectionEnd(cpsLocals))
|
||||
|
||||
app(prc, genSectionStart(cfsTypeInit1))
|
||||
@@ -876,13 +876,13 @@ proc genInitCode(m: BModule) =
|
||||
app(prc, genSectionEnd(i))
|
||||
|
||||
app(prc, genSectionStart(cpsInit))
|
||||
app(prc, m.preInitProc.s[cpsInit])
|
||||
app(prc, m.initProc.s[cpsInit])
|
||||
app(prc, m.preInitProc.s(cpsInit))
|
||||
app(prc, m.initProc.s(cpsInit))
|
||||
app(prc, genSectionEnd(cpsInit))
|
||||
|
||||
app(prc, genSectionStart(cpsStmts))
|
||||
app(prc, m.preInitProc.s[cpsStmts])
|
||||
app(prc, m.initProc.s[cpsStmts])
|
||||
app(prc, m.preInitProc.s(cpsStmts))
|
||||
app(prc, m.initProc.s(cpsStmts))
|
||||
if optStackTrace in m.initProc.options and not m.PreventStackTrace:
|
||||
app(prc, deinitFrame(m.initProc))
|
||||
app(prc, genSectionEnd(cpsStmts))
|
||||
|
||||
@@ -48,13 +48,14 @@ type
|
||||
TCProcSections* = array[TCProcSection, PRope] # represents a generated C proc
|
||||
BModule* = ref TCGen
|
||||
BProc* = ref TCProc
|
||||
TBlock{.final.} = object
|
||||
TBlock*{.final.} = object
|
||||
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
|
||||
sections*: TCProcSections # the code beloging
|
||||
|
||||
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
|
||||
BeforeRetNeeded*: bool # true iff 'BeforeRet' label for proc is needed
|
||||
ThreadVarAccessed*: bool # true if the proc already accessed some threadvar
|
||||
@@ -113,6 +114,8 @@ var
|
||||
gForwardedProcsCounter*: int = 0
|
||||
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 =
|
||||
new(result)
|
||||
@@ -120,6 +123,6 @@ proc newProc*(prc: PSym, module: BModule): BProc =
|
||||
result.module = module
|
||||
if prc != nil: result.options = prc.options
|
||||
else: result.options = gOptions
|
||||
result.blocks = @[]
|
||||
newSeq(result.blocks, 1)
|
||||
result.nestedTryStmts = @[]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user