Cleanups 20180401 (#7458)

This commit is contained in:
Jacek Sieka 2018-04-12 23:35:10 +08:00 • committed by Andreas Rumpf
commit 85e21158db
6 changed files with 73 additions and 78 deletions

View file

@ -978,11 +978,11 @@ proc genEcho(p: BProc, n: PNode) =
# bypass libc and print directly to the Genode LOG session # bypass libc and print directly to the Genode LOG session
var args: Rope = nil var args: Rope = nil
var a: TLoc var a: TLoc
for i in countup(0, n.len-1): for it in n.sons:
if n.sons[i].skipConv.kind == nkNilLit: if it.skipConv.kind == nkNilLit:
add(args, ", \"nil\"") add(args, ", \"nil\"")
else: else:
initLocExpr(p, n.sons[i], a) initLocExpr(p, it, a)
addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)]) addf(args, ", $1? ($1)->data:\"nil\"", [rdLoc(a)])
p.module.includeHeader("<base/log.h>") p.module.includeHeader("<base/log.h>")
linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args) linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args)
@ -1579,13 +1579,14 @@ proc genInOp(p: BProc, e: PNode, d: var TLoc) =
b.r = rope("(") b.r = rope("(")
var length = sonsLen(e.sons[1]) var length = sonsLen(e.sons[1])
for i in countup(0, length - 1): for i in countup(0, length - 1):
if e.sons[1].sons[i].kind == nkRange: let it = e.sons[1].sons[i]
initLocExpr(p, e.sons[1].sons[i].sons[0], x) if it.kind == nkRange:
initLocExpr(p, e.sons[1].sons[i].sons[1], y) initLocExpr(p, it.sons[0], x)
initLocExpr(p, it.sons[1], y)
addf(b.r, "$1 >= $2 && $1 <= $3", addf(b.r, "$1 >= $2 && $1 <= $3",
[rdCharLoc(a), rdCharLoc(x), rdCharLoc(y)]) [rdCharLoc(a), rdCharLoc(x), rdCharLoc(y)])
else: else:
initLocExpr(p, e.sons[1].sons[i], x) initLocExpr(p, it, x)
addf(b.r, "$1 == $2", [rdCharLoc(a), rdCharLoc(x)]) addf(b.r, "$1 == $2", [rdCharLoc(a), rdCharLoc(x)])
if i < length - 1: add(b.r, " || ") if i < length - 1: add(b.r, " || ")
add(b.r, ")") add(b.r, ")")
@ -1919,33 +1920,33 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
useStringh(p.module) useStringh(p.module)
lineF(p, cpsStmts, "memset($1, 0, sizeof($2));$n", lineF(p, cpsStmts, "memset($1, 0, sizeof($2));$n",
[rdLoc(d), getTypeDesc(p.module, e.typ)]) [rdLoc(d), getTypeDesc(p.module, e.typ)])
for i in countup(0, sonsLen(e) - 1): for it in e.sons:
if e.sons[i].kind == nkRange: if it.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, it.sons[0], a)
initLocExpr(p, e.sons[i].sons[1], b) initLocExpr(p, it.sons[1], b)
lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" & lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" &
"$2[(NU)($1)>>3] |=(1U<<((NU)($1)&7U));$n", [rdLoc(idx), rdLoc(d), "$2[(NU)($1)>>3] |=(1U<<((NU)($1)&7U));$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, it, a)
lineF(p, cpsStmts, "$1[(NU)($2)>>3] |=(1U<<((NU)($2)&7U));$n", lineF(p, cpsStmts, "$1[(NU)($2)>>3] |=(1U<<((NU)($2)&7U));$n",
[rdLoc(d), rdSetElemLoc(a, e.typ)]) [rdLoc(d), rdSetElemLoc(a, e.typ)])
else: else:
# small set # small set
var ts = "NU" & $(getSize(e.typ) * 8) var ts = "NU" & $(getSize(e.typ) * 8)
lineF(p, cpsStmts, "$1 = 0;$n", [rdLoc(d)]) lineF(p, cpsStmts, "$1 = 0;$n", [rdLoc(d)])
for i in countup(0, sonsLen(e) - 1): for it in e.sons:
if e.sons[i].kind == nkRange: if it.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, it.sons[0], a)
initLocExpr(p, e.sons[i].sons[1], b) initLocExpr(p, it.sons[1], b)
lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" & lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" &
"$2 |=((" & ts & ")(1)<<(($1)%(sizeof(" & ts & ")*8)));$n", [ "$2 |=((" & ts & ")(1)<<(($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, it, a)
lineF(p, cpsStmts, lineF(p, cpsStmts,
"$1 |=((" & ts & ")(1)<<(($2)%(sizeof(" & ts & ")*8)));$n", "$1 |=((" & ts & ")(1)<<(($2)%(sizeof(" & ts & ")*8)));$n",
[rdLoc(d), rdSetElemLoc(a, e.typ)]) [rdLoc(d), rdSetElemLoc(a, e.typ)])
@ -2155,6 +2156,9 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
else: else:
genComplexConst(p, sym, d) genComplexConst(p, sym, d)
of skEnumField: of skEnumField:
# we never reach this case - as of the time of this comment,
# skEnumField is folded to an int in semfold.nim, but this code
# remains for robustness
putIntoDest(p, d, n, rope(sym.position)) putIntoDest(p, d, n, rope(sym.position))
of skVar, skForVar, skResult, skLet: of skVar, skForVar, skResult, skLet:
if {sfGlobal, sfThread} * sym.flags != {}: if {sfGlobal, sfThread} * sym.flags != {}:
@ -2363,8 +2367,8 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo): Rope =
proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode, result: var Rope; count: var int) = proc getNullValueAux(p: BProc; t: PType; obj, cons: PNode, result: var Rope; count: var int) =
case obj.kind case obj.kind
of nkRecList: of nkRecList:
for i in countup(0, sonsLen(obj) - 1): for it in obj.sons:
getNullValueAux(p, t, obj.sons[i], cons, result, count) getNullValueAux(p, t, it, cons, result, count)
of nkRecCase: of nkRecCase:
getNullValueAux(p, t, obj.sons[0], cons, result, count) getNullValueAux(p, t, obj.sons[0], cons, result, count)
for i in countup(1, sonsLen(obj) - 1): for i in countup(1, sonsLen(obj) - 1):

View file

@ -37,7 +37,7 @@ proc isAssignedImmediately(n: PNode): bool {.inline.} =
return false return false
result = true result = true
proc inExceptBlockLen(p: BProc): int = proc inExceptBlockLen(p: BProc): int =
for x in p.nestedTryStmts: for x in p.nestedTryStmts:
if x.inExcept: result.inc if x.inExcept: result.inc
@ -189,8 +189,6 @@ proc genBreakState(p: BProc, n: PNode) =
lineF(p, cpsStmts, "if ((((NI*) $1.ClE_0)[1]) < 0) break;$n", [rdLoc(a)]) lineF(p, cpsStmts, "if ((((NI*) $1.ClE_0)[1]) < 0) break;$n", [rdLoc(a)])
# lineF(p, cpsStmts, "if (($1) < 0) break;$n", [rdLoc(a)]) # lineF(p, cpsStmts, "if (($1) < 0) break;$n", [rdLoc(a)])
proc genVarPrototypeAux(m: BModule, n: PNode)
proc genGotoVar(p: BProc; value: PNode) = proc genGotoVar(p: BProc; value: PNode) =
if value.kind notin {nkCharLit..nkUInt64Lit}: if value.kind notin {nkCharLit..nkUInt64Lit}:
localError(value.info, "'goto' target must be a literal value") localError(value.info, "'goto' target must be a literal value")
@ -225,7 +223,7 @@ proc genSingleVar(p: BProc, a: PNode) =
# Alternative construction using default constructor (which may zeromem): # Alternative construction using default constructor (which may zeromem):
# if sfImportc notin v.flags: constructLoc(p.module.preInitProc, v.loc) # if sfImportc notin v.flags: constructLoc(p.module.preInitProc, v.loc)
if sfExportc in v.flags and p.module.g.generatedHeader != nil: if sfExportc in v.flags and p.module.g.generatedHeader != nil:
genVarPrototypeAux(p.module.g.generatedHeader, vn) genVarPrototype(p.module.g.generatedHeader, vn)
registerGcRoot(p, v) registerGcRoot(p, v)
else: else:
let value = a.sons[2] let value = a.sons[2]
@ -271,28 +269,30 @@ proc genClosureVar(p: BProc, a: PNode) =
loadInto(p, a.sons[0], a.sons[2], v) loadInto(p, a.sons[0], a.sons[2], v)
proc genVarStmt(p: BProc, n: PNode) = proc genVarStmt(p: BProc, n: PNode) =
for i in countup(0, sonsLen(n) - 1): for it in n.sons:
var a = n.sons[i]
if a.kind == nkCommentStmt: continue
if a.kind == nkIdentDefs:
# can be a lifted var nowadays ...
if a.sons[0].kind == nkSym:
genSingleVar(p, a)
else:
genClosureVar(p, a)
else:
genVarTuple(p, a)
proc genConstStmt(p: BProc, t: PNode) =
for i in countup(0, sonsLen(t) - 1):
var it = t.sons[i]
if it.kind == nkCommentStmt: continue if it.kind == nkCommentStmt: continue
if it.kind != nkConstDef: internalError(t.info, "genConstStmt") if it.kind == nkIdentDefs:
var c = it.sons[0].sym # can be a lifted var nowadays ...
if c.typ.containsCompileTimeOnly: continue if it.sons[0].kind == nkSym:
elif c.typ.kind in ConstantDataTypes and lfNoDecl notin c.loc.flags and genSingleVar(p, it)
c.ast.len != 0: else:
if not emitLazily(c): requestConstImpl(p, c) genClosureVar(p, it)
else:
genVarTuple(p, it)
proc genConstStmt(p: BProc, n: PNode) =
for it in n.sons:
if it.kind == nkCommentStmt: continue
if it.kind != nkConstDef: internalError(n.info, "genConstStmt")
let sym = it.sons[0].sym
if sym.typ.containsCompileTimeOnly or
sym.typ.kind notin ConstantDataTypes or
sym.ast.len == 0 or
emitLazily(sym):
continue
requestConstImpl(p, sym)
proc genIf(p: BProc, n: PNode, d: var TLoc) = proc genIf(p: BProc, n: PNode, d: var TLoc) =
# #
@ -313,10 +313,9 @@ proc genIf(p: BProc, n: PNode, d: var TLoc) =
getTemp(p, n.typ, d) getTemp(p, n.typ, d)
genLineDir(p, n) genLineDir(p, n)
let lend = getLabel(p) let lend = getLabel(p)
for i in countup(0, sonsLen(n) - 1): for it in n.sons:
# bug #4230: avoid false sharing between branches: # bug #4230: avoid false sharing between branches:
if d.k == locTemp and isEmptyType(n.typ): d.k = locNone if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
let it = n.sons[i]
if it.len == 2: if it.len == 2:
when newScopeForIf: startBlock(p) when newScopeForIf: startBlock(p)
initLocExprSingleUse(p, it.sons[0], a) initLocExprSingleUse(p, it.sons[0], a)
@ -804,12 +803,12 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
# general_handler_body # general_handler_body
# } # }
# finallyPart(); # finallyPart();
template genExceptBranchBody(body: PNode) {.dirty.} = template genExceptBranchBody(body: PNode) {.dirty.} =
if optStackTrace in p.options: if optStackTrace in p.options:
linefmt(p, cpsStmts, "#setFrame((TFrame*)&FR_);$n") linefmt(p, cpsStmts, "#setFrame((TFrame*)&FR_);$n")
expr(p, body, d) expr(p, body, d)
if not isEmptyType(t.typ) and d.k == locNone: if not isEmptyType(t.typ) and d.k == locNone:
getTemp(p, t.typ, d) getTemp(p, t.typ, d)
genLineDir(p, t) genLineDir(p, t)
@ -956,15 +955,15 @@ proc genTry(p: BProc, t: PNode, d: var TLoc) =
proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope = proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope =
var res = "" var res = ""
for i in countup(0, sonsLen(t) - 1): for it in t.sons:
case t.sons[i].kind case it.kind
of nkStrLit..nkTripleStrLit: of nkStrLit..nkTripleStrLit:
res.add(t.sons[i].strVal) res.add(it.strVal)
of nkSym: of nkSym:
var sym = t.sons[i].sym var sym = it.sym
if sym.kind in {skProc, skFunc, skIterator, skMethod}: if sym.kind in {skProc, skFunc, skIterator, skMethod}:
var a: TLoc var a: TLoc
initLocExpr(p, t.sons[i], a) initLocExpr(p, it, a)
res.add($rdLoc(a)) res.add($rdLoc(a))
elif sym.kind == skType: elif sym.kind == skType:
res.add($getTypeDesc(p.module, sym.typ)) res.add($getTypeDesc(p.module, sym.typ))
@ -978,11 +977,11 @@ proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope =
sym.loc.r = r # but be consequent! sym.loc.r = r # but be consequent!
res.add($r) res.add($r)
of nkTypeOfExpr: of nkTypeOfExpr:
res.add($getTypeDesc(p.module, t.sons[i].typ)) res.add($getTypeDesc(p.module, it.typ))
else: else:
discard getTypeDesc(p.module, skipTypes(t[i].typ, abstractPtrs)) discard getTypeDesc(p.module, skipTypes(it.typ, abstractPtrs))
var a: TLoc var a: TLoc
initLocExpr(p, t[i], a) initLocExpr(p, it, a)
res.add($a.rdLoc) res.add($a.rdLoc)
if isAsmStmt and hasGnuAsm in CC[cCompiler].props: if isAsmStmt and hasGnuAsm in CC[cCompiler].props:
@ -1059,8 +1058,7 @@ proc genWatchpoint(p: BProc, n: PNode) =
genTypeInfo(p.module, typ, n.info)]) genTypeInfo(p.module, typ, n.info)])
proc genPragma(p: BProc, n: PNode) = proc genPragma(p: BProc, n: PNode) =
for i in countup(0, sonsLen(n) - 1): for it in n.sons:
var it = n.sons[i]
case whichPragma(it) case whichPragma(it)
of wEmit: genEmit(p, it) of wEmit: genEmit(p, it)
of wBreakpoint: genBreakPoint(p, it) of wBreakpoint: genBreakPoint(p, it)

View file

@ -17,11 +17,11 @@ type
p: BProc p: BProc
visitorFrmt: string visitorFrmt: string
proc genTraverseProc(c: var TTraversalClosure, accessor: Rope, typ: PType) proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType)
proc genCaseRange(p: BProc, branch: PNode) proc genCaseRange(p: BProc, branch: PNode)
proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false) proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false)
proc genTraverseProc(c: var TTraversalClosure, accessor: Rope, n: PNode; proc genTraverseProc(c: TTraversalClosure, accessor: Rope, n: PNode;
typ: PType) = typ: PType) =
if n == nil: return if n == nil: return
case n.kind case n.kind
@ -61,7 +61,7 @@ proc parentObj(accessor: Rope; m: BModule): Rope {.inline.} =
else: else:
result = accessor result = accessor
proc genTraverseProc(c: var TTraversalClosure, accessor: Rope, typ: PType) = proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType) =
if typ == nil: return if typ == nil: return
var p = c.p var p = c.p
@ -101,7 +101,7 @@ proc genTraverseProc(c: var TTraversalClosure, accessor: Rope, typ: PType) =
else: else:
discard discard
proc genTraverseProcSeq(c: var TTraversalClosure, accessor: Rope, typ: PType) = proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) =
var p = c.p var p = c.p
assert typ.kind == tySequence assert typ.kind == tySequence
var i: TLoc var i: TLoc
@ -117,18 +117,13 @@ proc genTraverseProcSeq(c: var TTraversalClosure, accessor: Rope, typ: PType) =
else: else:
lineF(p, cpsStmts, "}$n", []) lineF(p, cpsStmts, "}$n", [])
proc genTraverseProc(m: BModule, origTyp: PType; sig: SigHash; proc genTraverseProc(m: BModule, origTyp: PType; sig: SigHash): Rope =
reason: TTypeInfoReason): Rope =
var c: TTraversalClosure var c: TTraversalClosure
var p = newProc(nil, m) var p = newProc(nil, m)
result = "Marker_" & getTypeName(m, origTyp, sig) result = "Marker_" & getTypeName(m, origTyp, sig)
var typ = origTyp.skipTypes(abstractInst) var typ = origTyp.skipTypes(abstractInst)
if typ.kind == tyOpt: typ = optLowering(typ) if typ.kind == tyOpt: typ = optLowering(typ)
case reason
of tiNew: c.visitorFrmt = "#nimGCvisit((void*)$1, op);$n"
else: assert false
let header = "static N_NIMCALL(void, $1)(void* p, NI op)" % [result] let header = "static N_NIMCALL(void, $1)(void* p, NI op)" % [result]
let t = getTypeDesc(m, typ) let t = getTypeDesc(m, typ)
@ -136,6 +131,8 @@ proc genTraverseProc(m: BModule, origTyp: PType; sig: SigHash;
lineF(p, cpsInit, "a = ($1)p;$n", [t]) lineF(p, cpsInit, "a = ($1)p;$n", [t])
c.p = p c.p = p
c.visitorFrmt = "#nimGCvisit((void*)$1, op);$n"
assert typ.kind != tyTypeDesc assert typ.kind != tyTypeDesc
if typ.kind == tySequence: if typ.kind == tySequence:
genTraverseProcSeq(c, "a".rope, typ) genTraverseProcSeq(c, "a".rope, typ)

View file

@ -1207,10 +1207,6 @@ proc fakeClosureType(owner: PSym): PType =
r.rawAddSon(obj) r.rawAddSon(obj)
result.rawAddSon(r) result.rawAddSon(r)
type
TTypeInfoReason = enum ## for what do we need the type info?
tiNew, ## for 'new'
include ccgtrav include ccgtrav
proc genDeepCopyProc(m: BModule; s: PSym; result: Rope) = proc genDeepCopyProc(m: BModule; s: PSym; result: Rope) =
@ -1271,7 +1267,7 @@ proc genTypeInfo(m: BModule, t: PType; info: TLineInfo): Rope =
of tySequence, tyRef, tyOptAsRef: of tySequence, tyRef, tyOptAsRef:
genTypeInfoAux(m, t, t, result, info) genTypeInfoAux(m, t, t, result, info)
if gSelectedGC >= gcMarkAndSweep: if gSelectedGC >= gcMarkAndSweep:
let markerProc = genTraverseProc(m, origType, sig, tiNew) let markerProc = genTraverseProc(m, origType, sig)
addf(m.s[cfsTypeInit3], "$1.marker = $2;$n", [result, markerProc]) addf(m.s[cfsTypeInit3], "$1.marker = $2;$n", [result, markerProc])
of tyPtr, tyRange: genTypeInfoAux(m, t, t, result, info) of tyPtr, tyRange: genTypeInfoAux(m, t, t, result, info)
of tyArray: genArrayInfo(m, t, result, info) of tyArray: genArrayInfo(m, t, result, info)

View file

@ -894,7 +894,7 @@ proc genProc(m: BModule, prc: PSym) =
if not containsOrIncl(m.g.generatedHeader.declaredThings, prc.id): if not containsOrIncl(m.g.generatedHeader.declaredThings, prc.id):
genProcAux(m.g.generatedHeader, prc) genProcAux(m.g.generatedHeader, prc)
proc genVarPrototypeAux(m: BModule, n: PNode) = proc genVarPrototype(m: BModule, n: PNode) =
#assert(sfGlobal in sym.flags) #assert(sfGlobal in sym.flags)
let sym = n.sym let sym = n.sym
useHeader(m, sym) useHeader(m, sym)
@ -914,9 +914,6 @@ proc genVarPrototypeAux(m: BModule, n: PNode) =
if sfVolatile in sym.flags: add(m.s[cfsVars], " volatile") if sfVolatile in sym.flags: add(m.s[cfsVars], " volatile")
addf(m.s[cfsVars], " $1;$n", [sym.loc.r]) addf(m.s[cfsVars], " $1;$n", [sym.loc.r])
proc genVarPrototype(m: BModule, n: PNode) =
genVarPrototypeAux(m, n)
proc addIntTypes(result: var Rope) {.inline.} = proc addIntTypes(result: var Rope) {.inline.} =
addf(result, "#define NIM_NEW_MANGLING_RULES" & tnl & addf(result, "#define NIM_NEW_MANGLING_RULES" & tnl &
"#define NIM_INTBITS $1" & tnl, [ "#define NIM_INTBITS $1" & tnl, [

View file

@ -1768,6 +1768,9 @@ proc gen(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}) =
let constVal = if s.ast != nil: s.ast else: s.typ.n let constVal = if s.ast != nil: s.ast else: s.typ.n
gen(c, constVal, dest) gen(c, constVal, dest)
of skEnumField: of skEnumField:
# we never reach this case - as of the time of this comment,
# skEnumField is folded to an int in semfold.nim, but this code
# remains for robustness
if dest < 0: dest = c.getTemp(n.typ) if dest < 0: dest = c.getTemp(n.typ)
if s.position >= low(int16) and s.position <= high(int16): if s.position >= low(int16) and s.position <= high(int16):
c.gABx(n, opcLdImmInt, dest, s.position) c.gABx(n, opcLdImmInt, dest, s.position)