Cosmetic compiler cleanup (#12718)
* Cleanup compiler code base
* Unify add calls
* Unify len invocations
* Unify range operators
* Fix oversight
* Remove {.procvar.} pragma
* initCandidate -> newCandidate where reasonable
* Unify safeLen calls
This commit is contained in:
parent
b662842bd0
commit
7e747d11c6
109 changed files with 6115 additions and 6254 deletions
|
|
@ -92,10 +92,9 @@ proc lookupSlot(c: AnalysisCtx; s: PSym): int =
|
|||
proc getSlot(c: var AnalysisCtx; v: PSym): ptr MonotonicVar =
|
||||
let s = lookupSlot(c, v)
|
||||
if s >= 0: return addr(c.locals[s])
|
||||
let L = c.locals.len
|
||||
c.locals.setLen(L+1)
|
||||
c.locals[L].v = v
|
||||
return addr(c.locals[L])
|
||||
c.locals.setLen(c.locals.len+1)
|
||||
c.locals[^1].v = v
|
||||
return addr(c.locals[^1])
|
||||
|
||||
proc gatherArgs(c: var AnalysisCtx; n: PNode) =
|
||||
for i in 0..<n.safeLen:
|
||||
|
|
@ -123,7 +122,7 @@ proc checkLocal(c: AnalysisCtx; n: PNode) =
|
|||
if s >= 0 and c.locals[s].stride != nil:
|
||||
localError(c.graph.config, n.info, "invalid usage of counter after increment")
|
||||
else:
|
||||
for i in 0 ..< n.safeLen: checkLocal(c, n.sons[i])
|
||||
for i in 0..<n.safeLen: checkLocal(c, n[i])
|
||||
|
||||
template `?`(x): untyped = x.renderTree
|
||||
|
||||
|
|
@ -184,7 +183,7 @@ proc stride(c: AnalysisCtx; n: PNode): BiggestInt =
|
|||
if s >= 0 and c.locals[s].stride != nil:
|
||||
result = c.locals[s].stride.intVal
|
||||
else:
|
||||
for i in 0 ..< n.safeLen: result += stride(c, n.sons[i])
|
||||
for i in 0..<n.safeLen: result += stride(c, n[i])
|
||||
|
||||
proc subStride(c: AnalysisCtx; n: PNode): PNode =
|
||||
# substitute with stride:
|
||||
|
|
@ -196,7 +195,7 @@ proc subStride(c: AnalysisCtx; n: PNode): PNode =
|
|||
result = n
|
||||
elif n.safeLen > 0:
|
||||
result = shallowCopy(n)
|
||||
for i in 0 ..< n.len: result.sons[i] = subStride(c, n.sons[i])
|
||||
for i in 0..<n.len: result[i] = subStride(c, n[i])
|
||||
else:
|
||||
result = n
|
||||
|
||||
|
|
@ -216,8 +215,8 @@ proc checkSlicesAreDisjoint(c: var AnalysisCtx) =
|
|||
#
|
||||
# Or even worse:
|
||||
# while true:
|
||||
# spawn f(a[i+1 .. i+3])
|
||||
# spawn f(a[i+4 .. i+5])
|
||||
# spawn f(a[i+1..i+3])
|
||||
# spawn f(a[i+4..i+5])
|
||||
# inc i, 4
|
||||
# Prove that i*k*stride + 3 != i*k'*stride + 5
|
||||
# For the correct example this amounts to
|
||||
|
|
@ -226,15 +225,15 @@ proc checkSlicesAreDisjoint(c: var AnalysisCtx) =
|
|||
# For now, we don't try to prove things like that at all, even though it'd
|
||||
# be feasible for many useful examples. Instead we attach the slice to
|
||||
# a spawn and if the attached spawns differ, we bail out:
|
||||
for i in 0 .. high(c.slices):
|
||||
for j in i+1 .. high(c.slices):
|
||||
for i in 0..high(c.slices):
|
||||
for j in i+1..high(c.slices):
|
||||
let x = c.slices[i]
|
||||
let y = c.slices[j]
|
||||
if x.spawnId != y.spawnId and guards.sameTree(x.x, y.x):
|
||||
if not x.inLoop or not y.inLoop:
|
||||
# XXX strictly speaking, 'or' is not correct here and it needs to
|
||||
# be 'and'. However this prevents too many obviously correct programs
|
||||
# like f(a[0..x]); for i in x+1 .. a.high: f(a[i])
|
||||
# like f(a[0..x]); for i in x+1..a.high: f(a[i])
|
||||
overlap(c.guards, c.graph.config, x.a, x.b, y.a, y.b)
|
||||
elif (let k = simpleSlice(x.a, x.b); let m = simpleSlice(y.a, y.b);
|
||||
k >= 0 and m >= 0):
|
||||
|
|
@ -255,7 +254,7 @@ proc checkSlicesAreDisjoint(c: var AnalysisCtx) =
|
|||
proc analyse(c: var AnalysisCtx; n: PNode)
|
||||
|
||||
proc analyseSons(c: var AnalysisCtx; n: PNode) =
|
||||
for i in 0 ..< safeLen(n): analyse(c, n[i])
|
||||
for i in 0..<n.safeLen: analyse(c, n[i])
|
||||
|
||||
proc min(a, b: PNode): PNode =
|
||||
if a.isNil: result = b
|
||||
|
|
@ -295,31 +294,31 @@ proc analyseCall(c: var AnalysisCtx; n: PNode; op: PSym) =
|
|||
analyseSons(c, n)
|
||||
|
||||
proc analyseCase(c: var AnalysisCtx; n: PNode) =
|
||||
analyse(c, n.sons[0])
|
||||
analyse(c, n[0])
|
||||
let oldFacts = c.guards.s.len
|
||||
for i in 1..<n.len:
|
||||
let branch = n.sons[i]
|
||||
let branch = n[i]
|
||||
setLen(c.guards.s, oldFacts)
|
||||
addCaseBranchFacts(c.guards, n, i)
|
||||
for i in 0 ..< branch.len:
|
||||
analyse(c, branch.sons[i])
|
||||
for i in 0..<branch.len:
|
||||
analyse(c, branch[i])
|
||||
setLen(c.guards.s, oldFacts)
|
||||
|
||||
proc analyseIf(c: var AnalysisCtx; n: PNode) =
|
||||
analyse(c, n.sons[0].sons[0])
|
||||
analyse(c, n[0][0])
|
||||
let oldFacts = c.guards.s.len
|
||||
addFact(c.guards, canon(n.sons[0].sons[0], c.guards.o))
|
||||
addFact(c.guards, canon(n[0][0], c.guards.o))
|
||||
|
||||
analyse(c, n.sons[0].sons[1])
|
||||
analyse(c, n[0][1])
|
||||
for i in 1..<n.len:
|
||||
let branch = n.sons[i]
|
||||
let branch = n[i]
|
||||
setLen(c.guards.s, oldFacts)
|
||||
for j in 0..i-1:
|
||||
addFactNeg(c.guards, canon(n.sons[j].sons[0], c.guards.o))
|
||||
addFactNeg(c.guards, canon(n[j][0], c.guards.o))
|
||||
if branch.len > 1:
|
||||
addFact(c.guards, canon(branch.sons[0], c.guards.o))
|
||||
for i in 0 ..< branch.len:
|
||||
analyse(c, branch.sons[i])
|
||||
addFact(c.guards, canon(branch[0], c.guards.o))
|
||||
for i in 0..<branch.len:
|
||||
analyse(c, branch[i])
|
||||
setLen(c.guards.s, oldFacts)
|
||||
|
||||
proc analyse(c: var AnalysisCtx; n: PNode) =
|
||||
|
|
@ -365,7 +364,7 @@ proc analyse(c: var AnalysisCtx; n: PNode) =
|
|||
gatherArgs(c, value[1])
|
||||
analyseSons(c, value[1])
|
||||
if value.kind != nkEmpty:
|
||||
for j in 0 .. it.len-3:
|
||||
for j in 0..<it.len-2:
|
||||
if it[j].isLocal:
|
||||
let slot = c.getSlot(it[j].sym)
|
||||
if slot.lower.isNil: slot.lower = value
|
||||
|
|
@ -374,22 +373,22 @@ proc analyse(c: var AnalysisCtx; n: PNode) =
|
|||
of nkCaseStmt: analyseCase(c, n)
|
||||
of nkWhen, nkIfStmt, nkIfExpr: analyseIf(c, n)
|
||||
of nkWhileStmt:
|
||||
analyse(c, n.sons[0])
|
||||
analyse(c, n[0])
|
||||
# 'while true' loop?
|
||||
inc c.inLoop
|
||||
if isTrue(n.sons[0]):
|
||||
analyseSons(c, n.sons[1])
|
||||
if isTrue(n[0]):
|
||||
analyseSons(c, n[1])
|
||||
else:
|
||||
# loop may never execute:
|
||||
let oldState = c.locals.len
|
||||
let oldFacts = c.guards.s.len
|
||||
addFact(c.guards, canon(n.sons[0], c.guards.o))
|
||||
analyse(c, n.sons[1])
|
||||
addFact(c.guards, canon(n[0], c.guards.o))
|
||||
analyse(c, n[1])
|
||||
setLen(c.locals, oldState)
|
||||
setLen(c.guards.s, oldFacts)
|
||||
# we know after the loop the negation holds:
|
||||
if not hasSubnodeWith(n.sons[1], nkBreakStmt):
|
||||
addFactNeg(c.guards, canon(n.sons[0], c.guards.o))
|
||||
if not hasSubnodeWith(n[1], nkBreakStmt):
|
||||
addFactNeg(c.guards, canon(n[0], c.guards.o))
|
||||
dec c.inLoop
|
||||
of nkTypeSection, nkProcDef, nkConverterDef, nkMethodDef, nkIteratorDef,
|
||||
nkMacroDef, nkTemplateDef, nkConstSection, nkPragma, nkFuncDef:
|
||||
|
|
@ -412,16 +411,16 @@ proc transformSlices(g: ModuleGraph; n: PNode): PNode =
|
|||
return result
|
||||
if n.safeLen > 0:
|
||||
result = shallowCopy(n)
|
||||
for i in 0 ..< n.len:
|
||||
result.sons[i] = transformSlices(g, n.sons[i])
|
||||
for i in 0..<n.len:
|
||||
result[i] = transformSlices(g, n[i])
|
||||
else:
|
||||
result = n
|
||||
|
||||
proc transformSpawn(g: ModuleGraph; owner: PSym; n, barrier: PNode): PNode
|
||||
proc transformSpawnSons(g: ModuleGraph; owner: PSym; n, barrier: PNode): PNode =
|
||||
result = shallowCopy(n)
|
||||
for i in 0 ..< n.len:
|
||||
result.sons[i] = transformSpawn(g, owner, n.sons[i], barrier)
|
||||
for i in 0..<n.len:
|
||||
result[i] = transformSpawn(g, owner, n[i], barrier)
|
||||
|
||||
proc transformSpawn(g: ModuleGraph; owner: PSym; n, barrier: PNode): PNode =
|
||||
case n.kind
|
||||
|
|
@ -435,16 +434,16 @@ proc transformSpawn(g: ModuleGraph; owner: PSym; n, barrier: PNode): PNode =
|
|||
if result.isNil:
|
||||
result = newNodeI(nkStmtList, n.info)
|
||||
result.add n
|
||||
let t = b[1][0].typ.sons[0]
|
||||
let t = b[1][0].typ[0]
|
||||
if spawnResult(t, true) == srByVar:
|
||||
result.add wrapProcForSpawn(g, owner, m, b.typ, barrier, it[0])
|
||||
it.sons[it.len-1] = newNodeI(nkEmpty, it.info)
|
||||
it[^1] = newNodeI(nkEmpty, it.info)
|
||||
else:
|
||||
it.sons[it.len-1] = wrapProcForSpawn(g, owner, m, b.typ, barrier, nil)
|
||||
it[^1] = wrapProcForSpawn(g, owner, m, b.typ, barrier, nil)
|
||||
if result.isNil: result = n
|
||||
of nkAsgn, nkFastAsgn:
|
||||
let b = n[1]
|
||||
if getMagic(b) == mSpawn and (let t = b[1][0].typ.sons[0];
|
||||
if getMagic(b) == mSpawn and (let t = b[1][0].typ[0];
|
||||
spawnResult(t, true) == srByVar):
|
||||
let m = transformSlices(g, b)
|
||||
return wrapProcForSpawn(g, owner, m, b.typ, barrier, n[0])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue