diff options
Diffstat (limited to 'compiler/ccgstmts.nim')
-rw-r--r-- | compiler/ccgstmts.nim | 83 |
1 files changed, 34 insertions, 49 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 1751321f3..b79eaf346 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -72,7 +72,6 @@ template startBlock(p: BProc, start: FormatStr = "{$n", proc endBlock(p: BProc) proc genVarTuple(p: BProc, n: PNode) = - var tup, field: TLoc = default(TLoc) if n.kind != nkVarTuple: internalError(p.config, n.info, "genVarTuple") # if we have a something that's been captured, use the lowering instead: @@ -96,7 +95,7 @@ proc genVarTuple(p: BProc, n: PNode) = startBlock(p) genLineDir(p, n) - initLocExpr(p, n[^1], tup) + var tup = initLocExpr(p, n[^1]) var t = tup.t.skipTypes(abstractInst) for i in 0..<n.len-2: let vn = n[i] @@ -109,7 +108,7 @@ proc genVarTuple(p: BProc, n: PNode) = else: assignLocalVar(p, vn) initLocalVar(p, v, immediateAsgn=isAssignedImmediately(p.config, n[^1])) - initLoc(field, locExpr, vn, tup.storage) + var field = initLoc(locExpr, vn, tup.storage) if t.kind == tyTuple: field.r = "$1.Field$2" % [rdLoc(tup), rope(i)] else: @@ -245,8 +244,7 @@ proc genGotoState(p: BProc, n: PNode) = # switch (x.state) { # case 0: goto STATE0; # ... - var a: TLoc = default(TLoc) - initLocExpr(p, n[0], a) + var a: TLoc = initLocExpr(p, n[0]) lineF(p, cpsStmts, "switch ($1) {$n", [rdLoc(a)]) p.flags.incl beforeRetNeeded lineF(p, cpsStmts, "case -1:$n", []) @@ -264,14 +262,14 @@ proc genGotoState(p: BProc, n: PNode) = lineF(p, cpsStmts, "}$n", []) proc genBreakState(p: BProc, n: PNode, d: var TLoc) = - var a: TLoc = default(TLoc) - initLoc(d, locExpr, n, OnUnknown) + var a: TLoc + d = initLoc(locExpr, n, OnUnknown) if n[0].kind == nkClosure: - initLocExpr(p, n[0][1], a) + a = initLocExpr(p, n[0][1]) d.r = "(((NI*) $1)[1] < 0)" % [rdLoc(a)] else: - initLocExpr(p, n[0], a) + a = initLocExpr(p, n[0]) # the environment is guaranteed to contain the 'state' field at offset 1: d.r = "((((NI*) $1.ClE_0)[1]) < 0)" % [rdLoc(a)] @@ -342,7 +340,7 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) = # Only do this for complex types that may need a call to `objectInit` if sfThread in v.flags and emulatedThreadVars(p.config) and isComplexValueType(v.typ): - initLocExprSingleUse(p.module.preInitProc, vn, loc) + loc = initLocExprSingleUse(p.module.preInitProc, vn) genObjectInit(p.module.preInitProc, cpsInit, v.typ, loc, constructObj) # Alternative construction using default constructor (which may zeromem): # if sfImportc notin v.flags: constructLoc(p.module.preInitProc, v.loc) @@ -358,12 +356,12 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) = # generate better code here: 'Foo f = x;' genLineDir(p, vn) var decl = localVarDecl(p, vn) - var tmp: TLoc = default(TLoc) + var tmp: TLoc if isCppCtorCall: genCppVarForCtor(p, v, vn, value, decl) line(p, cpsStmts, decl) else: - initLocExprSingleUse(p, value, tmp) + tmp = initLocExprSingleUse(p, value) lineF(p, cpsStmts, "$# = $#;\n", [decl, tmp.rdLoc]) return assignLocalVar(p, vn) @@ -409,8 +407,7 @@ proc genSingleVar(p: BProc, a: PNode) = proc genClosureVar(p: BProc, a: PNode) = var immediateAsgn = a[2].kind != nkEmpty - var v: TLoc - initLocExpr(p, a[0], v) + var v: TLoc = initLocExpr(p, a[0]) genLineDir(p, a) if immediateAsgn: loadInto(p, a[0], a[2], v) @@ -442,7 +439,7 @@ proc genIf(p: BProc, n: PNode, d: var TLoc) = # { elsePart } # Lend: var - a: TLoc = default(TLoc) + a: TLoc lelse: TLabel if not isEmptyType(n.typ) and d.k == locNone: getTemp(p, n.typ, d) @@ -453,7 +450,7 @@ proc genIf(p: BProc, n: PNode, d: var TLoc) = if d.k == locTemp and isEmptyType(n.typ): d.k = locNone if it.len == 2: startBlock(p) - initLocExprSingleUse(p, it[0], a) + a = initLocExprSingleUse(p, it[0]) lelse = getLabel(p) inc(p.labels) lineF(p, cpsStmts, "if (!$1) goto $2;$n", @@ -555,8 +552,7 @@ proc genComputedGoto(p: BProc; n: PNode) = genStmts(p, n[j]) let caseStmt = n[casePos] - var a: TLoc = default(TLoc) - initLocExpr(p, caseStmt[0], a) + var a: TLoc = initLocExpr(p, caseStmt[0]) # first goto: lineF(p, cpsStmts, "goto *$#[$#];$n", [tmp, a.rdLoc]) @@ -594,8 +590,7 @@ proc genComputedGoto(p: BProc; n: PNode) = else: genStmts(p, it) - var a: TLoc = default(TLoc) - initLocExpr(p, caseStmt[0], a) + var a: TLoc = initLocExpr(p, caseStmt[0]) lineF(p, cpsStmts, "goto *$#[$#];$n", [tmp, a.rdLoc]) endBlock(p) @@ -607,7 +602,7 @@ proc genWhileStmt(p: BProc, t: PNode) = # we don't generate labels here as for example GCC would produce # significantly worse code var - a: TLoc = default(TLoc) + a: TLoc assert(t.len == 2) inc(p.withinLoop) genLineDir(p, t) @@ -623,7 +618,7 @@ proc genWhileStmt(p: BProc, t: PNode) = else: p.breakIdx = startBlock(p, "while (1) {$n") p.blocks[p.breakIdx].isLoop = true - initLocExpr(p, t[0], a) + a = initLocExpr(p, t[0]) if (t[0].kind != nkIntLit) or (t[0].intVal == 0): lineF(p, cpsStmts, "if (!$1) goto ", [rdLoc(a)]) assignLabel(p.blocks[p.breakIdx], p.s(cpsStmts)) @@ -662,14 +657,13 @@ proc genParForStmt(p: BProc, t: PNode) = preserveBreakIdx: let forLoopVar = t[0].sym - var rangeA, rangeB: TLoc = default(TLoc) assignLocalVar(p, t[0]) #initLoc(forLoopVar.loc, locLocalVar, forLoopVar.typ, onStack) #discard mangleName(forLoopVar) let call = t[1] assert(call.len == 4 or call.len == 5) - initLocExpr(p, call[1], rangeA) - initLocExpr(p, call[2], rangeB) + var rangeA = initLocExpr(p, call[1]) + var rangeB = initLocExpr(p, call[2]) # $n at the beginning because of #9710 if call.len == 4: # procName(a, b, annotation) @@ -686,8 +680,7 @@ proc genParForStmt(p: BProc, t: PNode) = rangeA.rdLoc, rangeB.rdLoc, call[3].getStr.rope]) else: # `||`(a, b, step, annotation) - var step: TLoc = default(TLoc) - initLocExpr(p, call[3], step) + var step: TLoc = initLocExpr(p, call[3]) lineF(p, cpsStmts, "$n#pragma omp $5$n" & "for ($1 = $2; $1 <= $3; $1 += $4)", [forLoopVar.loc.rdLoc, @@ -757,8 +750,7 @@ proc raiseInstr(p: BProc; result: var Rope) = proc genRaiseStmt(p: BProc, t: PNode) = if t[0].kind != nkEmpty: - var a: TLoc = default(TLoc) - initLocExprSingleUse(p, t[0], a) + var a: TLoc = initLocExprSingleUse(p, t[0]) finallyActions(p) var e = rdLoc(a) discard getTypeDesc(p.module, t[0].typ) @@ -785,15 +777,15 @@ proc genRaiseStmt(p: BProc, t: PNode) = template genCaseGenericBranch(p: BProc, b: PNode, e: TLoc, rangeFormat, eqFormat: FormatStr, labl: TLabel) = - var x, y: TLoc = default(TLoc) + var x, y: TLoc for i in 0..<b.len - 1: if b[i].kind == nkRange: - initLocExpr(p, b[i][0], x) - initLocExpr(p, b[i][1], y) + x = initLocExpr(p, b[i][0]) + y = initLocExpr(p, b[i][1]) lineCg(p, cpsStmts, rangeFormat, [rdCharLoc(e), rdCharLoc(x), rdCharLoc(y), labl]) else: - initLocExpr(p, b[i], x) + x = initLocExpr(p, b[i]) lineCg(p, cpsStmts, eqFormat, [rdCharLoc(e), rdCharLoc(x), labl]) proc genCaseSecondPass(p: BProc, t: PNode, d: var TLoc, @@ -835,18 +827,17 @@ template genIfForCaseUntil(p: BProc, t: PNode, d: var TLoc, template genCaseGeneric(p: BProc, t: PNode, d: var TLoc, rangeFormat, eqFormat: FormatStr) = - var a: TLoc = default(TLoc) - initLocExpr(p, t[0], a) + var a: TLoc = initLocExpr(p, t[0]) var lend = genIfForCaseUntil(p, t, d, rangeFormat, eqFormat, t.len-1, a) fixLabel(p, lend) proc genCaseStringBranch(p: BProc, b: PNode, e: TLoc, labl: TLabel, stringKind: TTypeKind, branches: var openArray[Rope]) = - var x: TLoc = default(TLoc) + var x: TLoc for i in 0..<b.len - 1: assert(b[i].kind != nkRange) - initLocExpr(p, b[i], x) + x = initLocExpr(p, b[i]) var j: int = 0 case b[i].kind of nkStrLit..nkTripleStrLit: @@ -870,8 +861,7 @@ proc genStringCase(p: BProc, t: PNode, stringKind: TTypeKind, d: var TLoc) = var bitMask = math.nextPowerOfTwo(strings) - 1 var branches: seq[Rope] newSeq(branches, bitMask + 1) - var a: TLoc = default(TLoc) - initLocExpr(p, t[0], a) # fist pass: generate ifs+goto: + var a: TLoc = initLocExpr(p, t[0]) # first pass: generate ifs+goto: var labId = p.labels for i in 1..<t.len: inc(p.labels) @@ -951,8 +941,7 @@ proc genOrdinalCase(p: BProc, n: PNode, d: var TLoc) = var splitPoint = ifSwitchSplitPoint(p, n) # generate if part (might be empty): - var a: TLoc = default(TLoc) - initLocExpr(p, n[0], a) + var a: TLoc = initLocExpr(p, n[0]) var lend = if splitPoint > 0: genIfForCaseUntil(p, n, d, rangeFormat = "if ($1 >= $2 && $1 <= $3) goto $4;$n", eqFormat = "if ($1 == $2) goto $3;$n", @@ -1495,8 +1484,7 @@ proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false; result: var Rope) = of nkSym: var sym = it.sym if sym.kind in {skProc, skFunc, skIterator, skMethod}: - var a: TLoc = default(TLoc) - initLocExpr(p, it, a) + var a: TLoc = initLocExpr(p, it) res.add($rdLoc(a)) elif sym.kind == skType: res.add($getTypeDesc(p.module, sym.typ)) @@ -1508,8 +1496,7 @@ proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false; result: var Rope) = res.add($getTypeDesc(p.module, it.typ)) else: discard getTypeDesc(p.module, skipTypes(it.typ, abstractPtrs)) - var a: TLoc = default(TLoc) - initLocExpr(p, it, a) + var a: TLoc = initLocExpr(p, it) res.add($a.rdLoc) if isAsmStmt and hasGnuAsm in CC[p.config.cCompiler].props: @@ -1611,11 +1598,10 @@ when false: expr(p, call, d) proc asgnFieldDiscriminant(p: BProc, e: PNode) = - var a = default(TLoc) var tmp: TLoc var dotExpr = e[0] if dotExpr.kind == nkCheckedFieldExpr: dotExpr = dotExpr[0] - initLocExpr(p, e[0], a) + var a = initLocExpr(p, e[0]) getTemp(p, a.t, tmp) expr(p, e[1], tmp) if p.inUncheckedAssignSection == 0: @@ -1634,9 +1620,8 @@ proc genAsgn(p: BProc, e: PNode, fastAsgn: bool) = else: let le = e[0] let ri = e[1] - var a: TLoc = default(TLoc) + var a: TLoc = initLoc(locNone, le, OnUnknown) discard getTypeDesc(p.module, le.typ.skipTypes(skipPtrs), dkVar) - initLoc(a, locNone, le, OnUnknown) a.flags.incl(lfEnforceDeref) a.flags.incl(lfPrepareForMutation) genLineDir(p, le) # it can be a nkBracketExpr, which may raise |