Skip to content

Commit 260b423

Browse files
authored
use out parameters for getTemp (#22399)
1 parent b5b4b48 commit 260b423

File tree

6 files changed

+39
-43
lines changed

6 files changed

+39
-43
lines changed

compiler/ccgcalls.nim

+5-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
9696
pl.add(");\n")
9797
line(p, cpsStmts, pl)
9898
else:
99-
var tmp: TLoc = default(TLoc)
99+
var tmp: TLoc
100100
getTemp(p, typ[0], tmp, needsInit=true)
101101
pl.add(addrLoc(p.config, tmp))
102102
pl.add(");\n")
@@ -133,7 +133,7 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
133133
genAssignment(p, d, list, {}) # no need for deep copying
134134
if canRaise: raiseExit(p)
135135
else:
136-
var tmp: TLoc = default(TLoc)
136+
var tmp: TLoc
137137
getTemp(p, typ[0], tmp, needsInit=true)
138138
var list: TLoc = default(TLoc)
139139
initLoc(list, locCall, d.lode, OnUnknown)
@@ -273,14 +273,12 @@ proc withTmpIfNeeded(p: BProc, a: TLoc, needsTmp: bool): TLoc =
273273
# Also don't regress for non ARC-builds, too risky.
274274
if needsTmp and a.lode.typ != nil and p.config.selectedGC in {gcArc, gcAtomicArc, gcOrc} and
275275
getSize(p.config, a.lode.typ) < 1024:
276-
result = default(TLoc)
277276
getTemp(p, a.lode.typ, result, needsInit=false)
278277
genAssignment(p, result, a, {})
279278
else:
280279
result = a
281280

282281
proc literalsNeedsTmp(p: BProc, a: TLoc): TLoc =
283-
result = default(TLoc)
284282
getTemp(p, a.lode.typ, result, needsInit=false)
285283
genAssignment(p, result, a, {})
286284

@@ -483,7 +481,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
483481
genCallPattern()
484482
if canRaise: raiseExit(p)
485483
else:
486-
var tmp: TLoc = default(TLoc)
484+
var tmp: TLoc
487485
getTemp(p, typ[0], tmp, needsInit=true)
488486
pl.add(addrLoc(p.config, tmp))
489487
genCallPattern()
@@ -501,7 +499,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
501499
genAssignment(p, d, list, {}) # no need for deep copying
502500
if canRaise: raiseExit(p)
503501
else:
504-
var tmp: TLoc = default(TLoc)
502+
var tmp: TLoc
505503
getTemp(p, typ[0], tmp)
506504
assert(d.t != nil) # generate an assignment to d:
507505
var list: TLoc = default(TLoc)
@@ -782,7 +780,7 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
782780
pl.add("];\n")
783781
line(p, cpsStmts, pl)
784782
else:
785-
var tmp: TLoc = default(TLoc)
783+
var tmp: TLoc
786784
getTemp(p, typ[0], tmp, needsInit=true)
787785
pl.add(addrLoc(p.config, tmp))
788786
pl.add("];\n")

compiler/ccgexprs.nim

+17-11
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
348348
linefmt(p, cpsStmts, "$1 = #copyString($2);$n", [dest.rdLoc, src.rdLoc])
349349
elif dest.storage == OnHeap:
350350
# we use a temporary to care for the dreaded self assignment:
351-
var tmp: TLoc = default(TLoc)
351+
var tmp: TLoc
352352
getTemp(p, ty, tmp)
353353
linefmt(p, cpsStmts, "$3 = $1; $1 = #copyStringRC1($2);$n",
354354
[dest.rdLoc, src.rdLoc, tmp.rdLoc])
@@ -431,7 +431,7 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
431431
proc genDeepCopy(p: BProc; dest, src: TLoc) =
432432
template addrLocOrTemp(a: TLoc): Rope =
433433
if a.k == locExpr:
434-
var tmp: TLoc = default(TLoc)
434+
var tmp: TLoc
435435
getTemp(p, a.t, tmp)
436436
genAssignment(p, tmp, a, {})
437437
addrLoc(p.config, tmp)
@@ -1197,7 +1197,7 @@ proc genAndOr(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
11971197
else:
11981198
var
11991199
L: TLabel
1200-
tmp: TLoc = default(TLoc)
1200+
tmp: TLoc
12011201
getTemp(p, e.typ, tmp) # force it into a temp!
12021202
inc p.splitDecls
12031203
expr(p, e[1], tmp)
@@ -1276,7 +1276,8 @@ proc genStrConcat(p: BProc, e: PNode, d: var TLoc) =
12761276
# appendChar(tmp0, 'z');
12771277
# asgn(s, tmp0);
12781278
# }
1279-
var a, tmp: TLoc = default(TLoc)
1279+
var a = default(TLoc)
1280+
var tmp: TLoc
12801281
getTemp(p, e.typ, tmp)
12811282
var L = 0
12821283
var appends: Rope = ""
@@ -1556,7 +1557,7 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
15561557
(d.k notin {locTemp,locLocalVar,locGlobalVar,locParam,locField}) or
15571558
(isPartOf(d.lode, e) != arNo)
15581559

1559-
var tmp: TLoc = default(TLoc)
1560+
var tmp: TLoc = TLoc()
15601561
var r: Rope
15611562
if useTemp:
15621563
getTemp(p, t, tmp)
@@ -1604,7 +1605,8 @@ proc lhsDoesAlias(a, b: PNode): bool =
16041605
if isPartOf(a, y) != arNo: return true
16051606

16061607
proc genSeqConstr(p: BProc, n: PNode, d: var TLoc) =
1607-
var arr, tmp: TLoc = default(TLoc)
1608+
var arr = default(TLoc)
1609+
var tmp: TLoc = default(TLoc)
16081610
# bug #668
16091611
let doesAlias = lhsDoesAlias(d.lode, n)
16101612
let dest = if doesAlias: addr(tmp) else: addr(d)
@@ -1669,7 +1671,7 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) =
16691671
arr.r = ropecg(p.module, "$1[$2]", [rdLoc(a), lit])
16701672
genAssignment(p, elem, arr, {needToCopy})
16711673
else:
1672-
var i: TLoc = default(TLoc)
1674+
var i: TLoc
16731675
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), i)
16741676
linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n", [i.r, L])
16751677
initLoc(elem, locExpr, lodeTyp elemType(skipTypes(n.typ, abstractInst)), OnHeap)
@@ -1986,7 +1988,8 @@ proc genSwap(p: BProc, e: PNode, d: var TLoc) =
19861988
# b = temp
19871989
cowBracket(p, e[1])
19881990
cowBracket(p, e[2])
1989-
var a, b, tmp: TLoc = default(TLoc)
1991+
var a, b = default(TLoc)
1992+
var tmp: TLoc
19901993
getTemp(p, skipTypes(e[1].typ, abstractVar), tmp)
19911994
initLocExpr(p, e[1], a) # eval a
19921995
initLocExpr(p, e[2], b) # eval b
@@ -2090,7 +2093,8 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
20902093
"&",
20912094
"|",
20922095
"& ~"]
2093-
var a, b, i: TLoc = default(TLoc)
2096+
var a, b = default(TLoc)
2097+
var i: TLoc
20942098
var setType = skipTypes(e[1].typ, abstractVar)
20952099
var size = int(getSize(p.config, setType))
20962100
case size
@@ -2645,7 +2649,8 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
26452649
# nimZeroMem(tmp, sizeof(tmp)); inclRange(tmp, a, b); incl(tmp, c);
26462650
# incl(tmp, d); incl(tmp, e); inclRange(tmp, f, g);
26472651
var
2648-
a, b, idx: TLoc = default(TLoc)
2652+
a, b = default(TLoc)
2653+
var idx: TLoc
26492654
if nfAllConst in e.flags:
26502655
var elem = newRopeAppender()
26512656
genSetNode(p, e, elem)
@@ -2744,7 +2749,8 @@ proc genClosure(p: BProc, n: PNode, d: var TLoc) =
27442749
p.module.s[cfsData].add data
27452750
putIntoDest(p, d, n, tmp, OnStatic)
27462751
else:
2747-
var tmp, a, b: TLoc = default(TLoc)
2752+
var tmp: TLoc
2753+
var a, b = default(TLoc)
27482754
initLocExpr(p, n[0], a)
27492755
initLocExpr(p, n[1], b)
27502756
if n[0].skipConv.kind == nkClosure:

compiler/ccgreset.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ proc specializeResetT(p: BProc, accessor: Rope, typ: PType) =
5757
specializeResetT(p, accessor, lastSon(typ))
5858
of tyArray:
5959
let arraySize = lengthOrd(p.config, typ[0])
60-
var i: TLoc = default(TLoc)
60+
var i: TLoc
6161
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), i)
6262
linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
6363
[i.r, arraySize])

compiler/ccgstmts.nim

+2-1
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,8 @@ when false:
16111611
expr(p, call, d)
16121612

16131613
proc asgnFieldDiscriminant(p: BProc, e: PNode) =
1614-
var a, tmp: TLoc = default(TLoc)
1614+
var a = default(TLoc)
1615+
var tmp: TLoc
16151616
var dotExpr = e[0]
16161617
if dotExpr.kind == nkCheckedFieldExpr: dotExpr = dotExpr[0]
16171618
initLocExpr(p, e[0], a)

compiler/ccgtrav.nim

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const
2121

2222
proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType)
2323
proc genCaseRange(p: BProc, branch: PNode)
24-
proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false)
24+
proc getTemp(p: BProc, t: PType, result: out TLoc; needsInit=false)
2525

2626
proc genTraverseProc(c: TTraversalClosure, accessor: Rope, n: PNode;
2727
typ: PType) =
@@ -74,7 +74,7 @@ proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType) =
7474
genTraverseProc(c, accessor, lastSon(typ))
7575
of tyArray:
7676
let arraySize = lengthOrd(c.p.config, typ[0])
77-
var i: TLoc = default(TLoc)
77+
var i: TLoc
7878
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt), i)
7979
var oldCode = p.s(cpsStmts)
8080
freeze oldCode
@@ -119,12 +119,11 @@ proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType) =
119119
proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) =
120120
var p = c.p
121121
assert typ.kind == tySequence
122-
var i: TLoc = default(TLoc)
122+
var i: TLoc
123123
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt), i)
124124
var oldCode = p.s(cpsStmts)
125125
freeze oldCode
126-
var a: TLoc = default(TLoc)
127-
a.r = accessor
126+
var a: TLoc = TLoc(r: accessor)
128127

129128
lineF(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
130129
[i.r, lenExpr(c.p, a)])

compiler/cgen.nim

+10-18
Original file line numberDiff line numberDiff line change
@@ -541,17 +541,14 @@ proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) =
541541
if not immediateAsgn:
542542
constructLoc(p, v.loc)
543543

544-
proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false) =
544+
proc getTemp(p: BProc, t: PType, result: out TLoc; needsInit=false) =
545545
inc(p.labels)
546-
result.r = "T" & rope(p.labels) & "_"
546+
result = TLoc(r: "T" & rope(p.labels) & "_", k: locTemp, lode: lodeTyp t,
547+
storage: OnStack, flags: {})
547548
if p.module.compileToCpp and isOrHasImportedCppType(t):
548549
linefmt(p, cpsLocals, "$1 $2{};$n", [getTypeDesc(p.module, t, dkVar), result.r])
549550
else:
550551
linefmt(p, cpsLocals, "$1 $2;$n", [getTypeDesc(p.module, t, dkVar), result.r])
551-
result.k = locTemp
552-
result.lode = lodeTyp t
553-
result.storage = OnStack
554-
result.flags = {}
555552
constructLoc(p, result, not needsInit)
556553
when false:
557554
# XXX Introduce a compiler switch in order to detect these easily.
@@ -562,23 +559,18 @@ proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false) =
562559
echo "ENORMOUS TEMPORARY! ", p.config $ p.lastLineInfo
563560
writeStackTrace()
564561

565-
proc getTempCpp(p: BProc, t: PType, result: var TLoc; value: Rope) =
562+
proc getTempCpp(p: BProc, t: PType, result: out TLoc; value: Rope) =
566563
inc(p.labels)
567-
result.r = "T" & rope(p.labels) & "_"
564+
result = TLoc(r: "T" & rope(p.labels) & "_", k: locTemp, lode: lodeTyp t,
565+
storage: OnStack, flags: {})
568566
linefmt(p, cpsStmts, "$1 $2 = $3;$n", [getTypeDesc(p.module, t, dkVar), result.r, value])
569-
result.k = locTemp
570-
result.lode = lodeTyp t
571-
result.storage = OnStack
572-
result.flags = {}
573567

574-
proc getIntTemp(p: BProc, result: var TLoc) =
568+
proc getIntTemp(p: BProc, result: out TLoc) =
575569
inc(p.labels)
576-
result.r = "T" & rope(p.labels) & "_"
570+
result = TLoc(r: "T" & rope(p.labels) & "_", k: locTemp,
571+
storage: OnStack, lode: lodeTyp getSysType(p.module.g.graph, unknownLineInfo, tyInt),
572+
flags: {})
577573
linefmt(p, cpsLocals, "NI $1;$n", [result.r])
578-
result.k = locTemp
579-
result.storage = OnStack
580-
result.lode = lodeTyp getSysType(p.module.g.graph, unknownLineInfo, tyInt)
581-
result.flags = {}
582574

583575
proc localVarDecl(p: BProc; n: PNode): Rope =
584576
result = ""

0 commit comments

Comments
 (0)