first batch of rope code converted to rfmt

The results so far are inconclusive:
The rope cache tries have been reduced by more than 125K (nearly 20%),
but the bootstrap time seems to be ever slightly increasing (0.01s).
This commit is contained in:
Zahary Karadjov 2012-11-21 19:21:00 +02:00
commit e817d54369
5 changed files with 91 additions and 95 deletions

View file

@ -20,12 +20,12 @@ proc hasNoInit(call: PNode): bool {.inline.} =
proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc, proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
callee, params: PRope) = callee, params: PRope) =
var pl = con(callee, "(".toRope, params) var pl = con(callee, ~"(", params)
# getUniqueType() is too expensive here: # getUniqueType() is too expensive here:
var typ = skipTypes(ri.sons[0].typ, abstractInst) var typ = skipTypes(ri.sons[0].typ, abstractInst)
if typ.sons[0] != nil: if typ.sons[0] != nil:
if isInvalidReturnType(typ.sons[0]): if isInvalidReturnType(typ.sons[0]):
if params != nil: pl.app(", ") if params != nil: pl.app(~", ")
# beware of 'result = p(result)'. We may need to allocate a temporary: # beware of 'result = p(result)'. We may need to allocate a temporary:
if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri): if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri):
# Great, we can use 'd': # Great, we can use 'd':
@ -34,17 +34,17 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
# reset before pass as 'result' var: # reset before pass as 'result' var:
resetLoc(p, d) resetLoc(p, d)
app(pl, addrLoc(d)) app(pl, addrLoc(d))
appf(pl, ");$n") app(pl, ~");$n")
line(p, cpsStmts, pl) line(p, cpsStmts, pl)
else: else:
var tmp: TLoc var tmp: TLoc
getTemp(p, typ.sons[0], tmp) getTemp(p, typ.sons[0], tmp)
app(pl, addrLoc(tmp)) app(pl, addrLoc(tmp))
appf(pl, ");$n") app(pl, ~");$n")
line(p, cpsStmts, pl) line(p, cpsStmts, pl)
genAssignment(p, d, tmp, {}) # no need for deep copying genAssignment(p, d, tmp, {}) # no need for deep copying
else: else:
app(pl, ")") app(pl, ~")")
if d.k == locNone: getTemp(p, typ.sons[0], d) if d.k == locNone: getTemp(p, typ.sons[0], d)
assert(d.t != nil) # generate an assignment to d: assert(d.t != nil) # generate an assignment to d:
var list: TLoc var list: TLoc
@ -52,7 +52,7 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,
list.r = pl list.r = pl
genAssignment(p, d, list, {}) # no need for deep copying genAssignment(p, d, list, {}) # no need for deep copying
else: else:
appf(pl, ");$n") app(pl, ~");$n")
line(p, cpsStmts, pl) line(p, cpsStmts, pl)
proc isInCurrentFrame(p: BProc, n: PNode): bool = proc isInCurrentFrame(p: BProc, n: PNode): bool =
@ -130,7 +130,7 @@ proc genPrefixCall(p: BProc, le, ri: PNode, d: var TLoc) =
var length = sonsLen(ri) var length = sonsLen(ri)
for i in countup(1, length - 1): for i in countup(1, length - 1):
if ri.sons[i].typ.isCompileTimeOnly: continue if ri.sons[i].typ.isCompileTimeOnly: continue
if params != nil: app(params, ", ") if params != nil: app(params, ~", ")
if i < sonsLen(typ): if i < sonsLen(typ):
assert(typ.n.sons[i].kind == nkSym) assert(typ.n.sons[i].kind == nkSym)
app(params, genArg(p, ri.sons[i], typ.n.sons[i].sym)) app(params, genArg(p, ri.sons[i], typ.n.sons[i].sym))
@ -144,7 +144,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
result = getClosureType(p.module, t, clHalf) result = getClosureType(p.module, t, clHalf)
proc addComma(r: PRope): PRope = proc addComma(r: PRope): PRope =
result = if r == nil: r else: con(r, ", ") result = if r == nil: r else: con(r, ~", ")
const CallPattern = "$1.ClEnv? $1.ClPrc($3$1.ClEnv) : (($4)($1.ClPrc))($2);$n" const CallPattern = "$1.ClEnv? $1.ClPrc($3$1.ClEnv) : (($4)($1.ClPrc))($2);$n"
var op: TLoc var op: TLoc
@ -161,7 +161,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
app(pl, genArg(p, ri.sons[i], typ.n.sons[i].sym)) app(pl, genArg(p, ri.sons[i], typ.n.sons[i].sym))
else: else:
app(pl, genArgNoParam(p, ri.sons[i])) app(pl, genArgNoParam(p, ri.sons[i]))
if i < length - 1: app(pl, ", ") if i < length - 1: app(pl, ~", ")
template genCallPattern = template genCallPattern =
lineF(p, cpsStmts, CallPattern, op.r, pl, pl.addComma, rawProc) lineF(p, cpsStmts, CallPattern, op.r, pl, pl.addComma, rawProc)
@ -169,7 +169,7 @@ proc genClosureCall(p: BProc, le, ri: PNode, d: var TLoc) =
let rawProc = getRawProcType(p, typ) let rawProc = getRawProcType(p, typ)
if typ.sons[0] != nil: if typ.sons[0] != nil:
if isInvalidReturnType(typ.sons[0]): if isInvalidReturnType(typ.sons[0]):
if sonsLen(ri) > 1: app(pl, ", ") if sonsLen(ri) > 1: app(pl, ~", ")
# beware of 'result = p(result)'. We may need to allocate a temporary: # beware of 'result = p(result)'. We may need to allocate a temporary:
if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri): if d.k in {locTemp, locNone} or not leftAppearsOnRightSide(le, ri):
# Great, we can use 'd': # Great, we can use 'd':
@ -208,12 +208,12 @@ proc genInfixCall(p: BProc, le, ri: PNode, d: var TLoc) =
var param = typ.n.sons[1].sym var param = typ.n.sons[1].sym
app(pl, genArg(p, ri.sons[1], param)) app(pl, genArg(p, ri.sons[1], param))
if skipTypes(param.typ, {tyGenericInst}).kind == tyPtr: app(pl, "->") if skipTypes(param.typ, {tyGenericInst}).kind == tyPtr: app(pl, ~"->")
else: app(pl, ".") else: app(pl, ~".")
app(pl, op.r) app(pl, op.r)
var params: PRope var params: PRope
for i in countup(2, length - 1): for i in countup(2, length - 1):
if params != nil: params.app(", ") if params != nil: params.app(~", ")
assert(sonsLen(typ) == sonsLen(typ.n)) assert(sonsLen(typ) == sonsLen(typ.n))
if i < sonsLen(typ): if i < sonsLen(typ):
assert(typ.n.sons[i].kind == nkSym) assert(typ.n.sons[i].kind == nkSym)
@ -226,7 +226,7 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
# generates a crappy ObjC call # generates a crappy ObjC call
var op, a: TLoc var op, a: TLoc
initLocExpr(p, ri.sons[0], op) initLocExpr(p, ri.sons[0], op)
var pl = toRope"[" var pl = ~"["
# getUniqueType() is too expensive here: # getUniqueType() is too expensive here:
var typ = skipTypes(ri.sons[0].typ, abstractInst) var typ = skipTypes(ri.sons[0].typ, abstractInst)
assert(typ.kind == tyProc) assert(typ.kind == tyProc)
@ -235,10 +235,10 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
if length > 1: if length > 1:
app(pl, genArg(p, ri.sons[1], typ.n.sons[1].sym)) app(pl, genArg(p, ri.sons[1], typ.n.sons[1].sym))
app(pl, " ") app(pl, ~" ")
app(pl, op.r) app(pl, op.r)
if length > 2: if length > 2:
app(pl, ": ") app(pl, ~": ")
app(pl, genArg(p, ri.sons[2], typ.n.sons[2].sym)) app(pl, genArg(p, ri.sons[2], typ.n.sons[2].sym))
for i in countup(3, length-1): for i in countup(3, length-1):
assert(sonsLen(typ) == sonsLen(typ.n)) assert(sonsLen(typ) == sonsLen(typ.n))
@ -246,30 +246,30 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
InternalError(ri.info, "varargs for objective C method?") InternalError(ri.info, "varargs for objective C method?")
assert(typ.n.sons[i].kind == nkSym) assert(typ.n.sons[i].kind == nkSym)
var param = typ.n.sons[i].sym var param = typ.n.sons[i].sym
app(pl, " ") app(pl, ~" ")
app(pl, param.name.s) app(pl, param.name.s)
app(pl, ": ") app(pl, ~": ")
app(pl, genArg(p, ri.sons[i], param)) app(pl, genArg(p, ri.sons[i], param))
if typ.sons[0] != nil: if typ.sons[0] != nil:
if isInvalidReturnType(typ.sons[0]): if isInvalidReturnType(typ.sons[0]):
if sonsLen(ri) > 1: app(pl, " ") if sonsLen(ri) > 1: app(pl, ~" ")
# beware of 'result = p(result)'. We always allocate a temporary: # beware of 'result = p(result)'. We always allocate a temporary:
if d.k in {locTemp, locNone}: if d.k in {locTemp, locNone}:
# We already got a temp. Great, special case it: # We already got a temp. Great, special case it:
if d.k == locNone: getTemp(p, typ.sons[0], d) if d.k == locNone: getTemp(p, typ.sons[0], d)
app(pl, "Result: ") app(pl, ~"Result: ")
app(pl, addrLoc(d)) app(pl, addrLoc(d))
appf(pl, "];$n") app(pl, ~"];$n")
line(p, cpsStmts, pl) line(p, cpsStmts, pl)
else: else:
var tmp: TLoc var tmp: TLoc
getTemp(p, typ.sons[0], tmp) getTemp(p, typ.sons[0], tmp)
app(pl, addrLoc(tmp)) app(pl, addrLoc(tmp))
appf(pl, "];$n") app(pl, ~"];$n")
line(p, cpsStmts, pl) line(p, cpsStmts, pl)
genAssignment(p, d, tmp, {}) # no need for deep copying genAssignment(p, d, tmp, {}) # no need for deep copying
else: else:
app(pl, "]") app(pl, ~"]")
if d.k == locNone: getTemp(p, typ.sons[0], d) if d.k == locNone: getTemp(p, typ.sons[0], d)
assert(d.t != nil) # generate an assignment to d: assert(d.t != nil) # generate an assignment to d:
var list: TLoc var list: TLoc
@ -277,7 +277,7 @@ proc genNamedParamCall(p: BProc, ri: PNode, d: var TLoc) =
list.r = pl list.r = pl
genAssignment(p, d, list, {}) # no need for deep copying genAssignment(p, d, list, {}) # no need for deep copying
else: else:
appf(pl, "];$n") app(pl, ~"];$n")
line(p, cpsStmts, pl) line(p, cpsStmts, pl)
proc genCall(p: BProc, e: PNode, d: var TLoc) = proc genCall(p: BProc, e: PNode, d: var TLoc) =

View file

@ -19,15 +19,15 @@ proc intLiteral(i: biggestInt): PRope =
result = toRope(i) result = toRope(i)
elif i == low(int32): elif i == low(int32):
# Nimrod has the same bug for the same reasons :-) # Nimrod has the same bug for the same reasons :-)
result = toRope("(-2147483647 -1)") result = ~"(-2147483647 -1)"
elif i > low(int64): elif i > low(int64):
result = ropef("IL64($1)", [toRope(i)]) result = rfmt(nil, "IL64($1)", toRope(i))
else: else:
result = toRope("(IL64(-9223372036854775807) - IL64(1))") result = ~"(IL64(-9223372036854775807) - IL64(1))"
proc int32Literal(i: Int): PRope = proc int32Literal(i: Int): PRope =
if i == int(low(int32)): if i == int(low(int32)):
result = toRope("(-2147483647 -1)") result = ~"(-2147483647 -1)"
else: else:
result = toRope(i) result = toRope(i)
@ -57,8 +57,8 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): PRope =
else: else:
result = intLiteral(n.intVal) result = intLiteral(n.intVal)
of tyBool: of tyBool:
if n.intVal != 0: result = toRope("NIM_TRUE") if n.intVal != 0: result = ~"NIM_TRUE"
else: result = toRope("NIM_FALSE") else: result = ~"NIM_FALSE"
else: else:
result = ropef("(($1) $2)", [getTypeDesc(p.module, result = ropef("(($1) $2)", [getTypeDesc(p.module,
skipTypes(ty, abstractVarRange)), intLiteral(n.intVal)]) skipTypes(ty, abstractVarRange)), intLiteral(n.intVal)])
@ -185,14 +185,14 @@ proc genRefAssign(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
# lineF(p, cpsStmts, 'if ($1) nimGCunref($1);$n', [rdLoc(dest)]) # lineF(p, cpsStmts, 'if ($1) nimGCunref($1);$n', [rdLoc(dest)])
# lineF(p, cpsStmts, '$1 = $2;$n', [rdLoc(dest), rdLoc(src)]) # lineF(p, cpsStmts, '$1 = $2;$n', [rdLoc(dest), rdLoc(src)])
if canFormAcycle(dest.t): if canFormAcycle(dest.t):
lineCg(p, cpsStmts, "#asgnRef((void**) $1, $2);$n", lineCg2(p, cpsStmts, "#asgnRef((void**) $1, $2);$n",
[addrLoc(dest), rdLoc(src)]) addrLoc(dest), rdLoc(src))
else: else:
lineCg(p, cpsStmts, "#asgnRefNoCycle((void**) $1, $2);$n", lineCg2(p, cpsStmts, "#asgnRefNoCycle((void**) $1, $2);$n",
[addrLoc(dest), rdLoc(src)]) addrLoc(dest), rdLoc(src))
else: else:
lineCg(p, cpsStmts, "#unsureAsgnRef((void**) $1, $2);$n", lineCg2(p, cpsStmts, "#unsureAsgnRef((void**) $1, $2);$n",
[addrLoc(dest), rdLoc(src)]) addrLoc(dest), rdLoc(src))
if needToKeepAlive in flags: keepAlive(p, dest) if needToKeepAlive in flags: keepAlive(p, dest)
proc genGenericAsgn(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) = proc genGenericAsgn(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
@ -205,23 +205,23 @@ proc genGenericAsgn(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
if needToCopy notin flags or if needToCopy notin flags or
tfShallow in skipTypes(dest.t, abstractVarRange).flags: tfShallow in skipTypes(dest.t, abstractVarRange).flags:
if dest.s == OnStack or optRefcGC notin gGlobalOptions: if dest.s == OnStack or optRefcGC notin gGlobalOptions:
lineCg(p, cpsStmts, lineCg2(p, cpsStmts,
"memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($3));$n", "memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($3));$n",
[addrLoc(dest), addrLoc(src), rdLoc(dest)]) addrLoc(dest), addrLoc(src), rdLoc(dest))
if needToKeepAlive in flags: keepAlive(p, dest) if needToKeepAlive in flags: keepAlive(p, dest)
else: else:
lineCg(p, cpsStmts, "#genericShallowAssign((void*)$1, (void*)$2, $3);$n", lineCg2(p, cpsStmts, "#genericShallowAssign((void*)$1, (void*)$2, $3);$n",
[addrLoc(dest), addrLoc(src), genTypeInfo(p.module, dest.t)]) addrLoc(dest), addrLoc(src), genTypeInfo(p.module, dest.t))
else: else:
lineCg(p, cpsStmts, "#genericAssign((void*)$1, (void*)$2, $3);$n", lineCg2(p, cpsStmts, "#genericAssign((void*)$1, (void*)$2, $3);$n",
[addrLoc(dest), addrLoc(src), genTypeInfo(p.module, dest.t)]) addrLoc(dest), addrLoc(src), genTypeInfo(p.module, dest.t))
proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) = proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
# This function replaces all other methods for generating # This function replaces all other methods for generating
# the assignment operation in C. # the assignment operation in C.
if src.t != nil and src.t.kind == tyPtr: if src.t != nil and src.t.kind == tyPtr:
# little HACK to support the new 'var T' as return type: # little HACK to support the new 'var T' as return type:
lineCg(p, cpsStmts, "$1 = $2;$n", [rdLoc(dest), rdLoc(src)]) lineCg2(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
return return
var ty = skipTypes(dest.t, abstractRange) var ty = skipTypes(dest.t, abstractRange)
case ty.kind case ty.kind
@ -231,8 +231,8 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
if needToCopy notin flags: if needToCopy notin flags:
genRefAssign(p, dest, src, flags) genRefAssign(p, dest, src, flags)
else: else:
lineCg(p, cpsStmts, "#genericSeqAssign($1, $2, $3);$n", lineCg2(p, cpsStmts, "#genericSeqAssign($1, $2, $3);$n",
[addrLoc(dest), rdLoc(src), genTypeInfo(p.module, dest.t)]) addrLoc(dest), rdLoc(src), genTypeInfo(p.module, dest.t))
of tyString: of tyString:
if needToCopy notin flags: if needToCopy notin flags:
genRefAssign(p, dest, src, flags) genRefAssign(p, dest, src, flags)
@ -244,46 +244,46 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
# we use a temporary to care for the dreaded self assignment: # we use a temporary to care for the dreaded self assignment:
var tmp: TLoc var tmp: TLoc
getTemp(p, ty, tmp) getTemp(p, ty, tmp)
lineCg(p, cpsStmts, "$3 = $1; $1 = #copyStringRC1($2);$n", lineCg2(p, cpsStmts, "$3 = $1; $1 = #copyStringRC1($2);$n",
[dest.rdLoc, src.rdLoc, tmp.rdLoc]) dest.rdLoc, src.rdLoc, tmp.rdLoc)
lineCg(p, cpsStmts, "if ($1) #nimGCunrefNoCycle($1);$n", tmp.rdLoc) lineCg(p, cpsStmts, "if ($1) #nimGCunrefNoCycle($1);$n", tmp.rdLoc)
else: else:
lineCg(p, cpsStmts, "#unsureAsgnRef((void**) $1, #copyString($2));$n", lineCg2(p, cpsStmts, "#unsureAsgnRef((void**) $1, #copyString($2));$n",
[addrLoc(dest), rdLoc(src)]) addrLoc(dest), rdLoc(src))
if needToKeepAlive in flags: keepAlive(p, dest) if needToKeepAlive in flags: keepAlive(p, dest)
of tyTuple, tyObject, tyProc: of tyTuple, tyObject, tyProc:
# XXX: check for subtyping? # XXX: check for subtyping?
if needsComplexAssignment(dest.t): if needsComplexAssignment(dest.t):
genGenericAsgn(p, dest, src, flags) genGenericAsgn(p, dest, src, flags)
else: else:
lineCg(p, cpsStmts, "$1 = $2;$n", [rdLoc(dest), rdLoc(src)]) lineCg2(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
of tyArray, tyArrayConstr: of tyArray, tyArrayConstr:
if needsComplexAssignment(dest.t): if needsComplexAssignment(dest.t):
genGenericAsgn(p, dest, src, flags) genGenericAsgn(p, dest, src, flags)
else: else:
lineCg(p, cpsStmts, lineCg2(p, cpsStmts,
"memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($1));$n", "memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($1));$n",
[rdLoc(dest), rdLoc(src)]) rdLoc(dest), rdLoc(src))
of tyOpenArray, tyVarargs: of tyOpenArray, tyVarargs:
# open arrays are always on the stack - really? What if a sequence is # open arrays are always on the stack - really? What if a sequence is
# passed to an open array? # passed to an open array?
if needsComplexAssignment(dest.t): if needsComplexAssignment(dest.t):
lineCg(p, cpsStmts, # XXX: is this correct for arrays? lineCg2(p, cpsStmts, # XXX: is this correct for arrays?
"#genericAssignOpenArray((void*)$1, (void*)$2, $1Len0, $3);$n", "#genericAssignOpenArray((void*)$1, (void*)$2, $1Len0, $3);$n",
[addrLoc(dest), addrLoc(src), genTypeInfo(p.module, dest.t)]) addrLoc(dest), addrLoc(src), genTypeInfo(p.module, dest.t))
else: else:
lineCg(p, cpsStmts, lineCg2(p, cpsStmts,
"memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($1[0])*$1Len0);$n", "memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($1[0])*$1Len0);$n",
[rdLoc(dest), rdLoc(src)]) rdLoc(dest), rdLoc(src))
of tySet: of tySet:
if mapType(ty) == ctArray: if mapType(ty) == ctArray:
lineCg(p, cpsStmts, "memcpy((void*)$1, (NIM_CONST void*)$2, $3);$n", lineCg2(p, cpsStmts, "memcpy((void*)$1, (NIM_CONST void*)$2, $3);$n",
[rdLoc(dest), rdLoc(src), toRope(getSize(dest.t))]) rdLoc(dest), rdLoc(src), toRope(getSize(dest.t)))
else: else:
lineCg(p, cpsStmts, "$1 = $2;$n", [rdLoc(dest), rdLoc(src)]) lineCg2(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
of tyPtr, tyPointer, tyChar, tyBool, tyEnum, tyCString, of tyPtr, tyPointer, tyChar, tyBool, tyEnum, tyCString,
tyInt..tyUInt64, tyRange, tyVar: tyInt..tyUInt64, tyRange, tyVar:
lineCg(p, cpsStmts, "$1 = $2;$n", [rdLoc(dest), rdLoc(src)]) lineCg2(p, cpsStmts, "$1 = $2;$n", rdLoc(dest), rdLoc(src))
else: InternalError("genAssignment(" & $ty.kind & ')') else: InternalError("genAssignment(" & $ty.kind & ')')
proc expr(p: BProc, e: PNode, d: var TLoc) proc expr(p: BProc, e: PNode, d: var TLoc)
@ -322,7 +322,7 @@ proc binaryStmt(p: BProc, e: PNode, d: var TLoc, frmt: string) =
if d.k != locNone: InternalError(e.info, "binaryStmt") if d.k != locNone: InternalError(e.info, "binaryStmt")
InitLocExpr(p, e.sons[1], d) InitLocExpr(p, e.sons[1], d)
InitLocExpr(p, e.sons[2], b) InitLocExpr(p, e.sons[2], b)
lineCg(p, cpsStmts, frmt, [rdLoc(d), rdLoc(b)]) lineCg(p, cpsStmts, frmt, rdLoc(d), rdLoc(b))
proc unaryStmt(p: BProc, e: PNode, d: var TLoc, frmt: string) = proc unaryStmt(p: BProc, e: PNode, d: var TLoc, frmt: string) =
var a: TLoc var a: TLoc

View file

@ -72,14 +72,12 @@ proc endBlock(p: BProc, blockEnd: PRope) =
# properly indented when pretty printing is enabled # properly indented when pretty printing is enabled
line(p, cpsStmts, blockEnd) line(p, cpsStmts, blockEnd)
var gBlockEndBracket = ropef("}$n")
proc endBlock(p: BProc) = proc endBlock(p: BProc) =
let topBlock = p.blocks.len - 1 let topBlock = p.blocks.len - 1
let blockEnd = if p.blocks[topBlock].label != nil: let blockEnd = if p.blocks[topBlock].label != nil:
ropef("} $1: ;$n", [p.blocks[topBlock].label]) ropef("} $1: ;$n", [p.blocks[topBlock].label])
else: else:
gBlockEndBracket ~"}$n"
endBlock(p, blockEnd) endBlock(p, blockEnd)
proc genSimpleBlock(p: BProc, stmts: PNode) {.inline.} = proc genSimpleBlock(p: BProc, stmts: PNode) {.inline.} =

View file

@ -70,12 +70,12 @@ proc mangleName(s: PSym): PRope =
if gCmd == cmdCompileToLLVM: if gCmd == cmdCompileToLLVM:
case s.kind case s.kind
of skProc, skMethod, skConverter, skConst, skIterator: of skProc, skMethod, skConverter, skConst, skIterator:
result = toRope("@") result = ~"@"
of skVar, skForVar, skResult, skLet: of skVar, skForVar, skResult, skLet:
if sfGlobal in s.flags: result = toRope("@") if sfGlobal in s.flags: result = ~"@"
else: result = toRope("%") else: result = ~"%"
of skTemp, skParam, skType, skEnumField, skModule: of skTemp, skParam, skType, skEnumField, skModule:
result = toRope("%") result = ~"%"
else: InternalError(s.info, "mangleName") else: InternalError(s.info, "mangleName")
when oKeepVariableNames: when oKeepVariableNames:
let keepOrigName = s.kind in skLocalVars - {skForVar} and let keepOrigName = s.kind in skLocalVars - {skForVar} and
@ -129,11 +129,11 @@ proc mangleName(s: PSym): PRope =
result = s.name.s.mangle.toRope result = s.name.s.mangle.toRope
else: else:
app(result, toRope(mangle(s.name.s))) app(result, toRope(mangle(s.name.s)))
app(result, "_") app(result, ~"_")
app(result, toRope(s.id)) app(result, toRope(s.id))
else: else:
app(result, toRope(mangle(s.name.s))) app(result, toRope(mangle(s.name.s)))
app(result, "_") app(result, ~"_")
app(result, toRope(s.id)) app(result, toRope(s.id))
s.loc.r = result s.loc.r = result
@ -148,11 +148,9 @@ proc containsCompileTimeOnly(t: PType): bool =
return true return true
return false return false
var anonTypeName = toRope"TY"
proc typeName(typ: PType): PRope = proc typeName(typ: PType): PRope =
result = if typ.sym != nil: typ.sym.name.s.mangle.toRope result = if typ.sym != nil: typ.sym.name.s.mangle.toRope
else: anonTypeName else: ~"TY"
proc getTypeName(typ: PType): PRope = proc getTypeName(typ: PType): PRope =
if (typ.sym != nil) and ({sfImportc, sfExportc} * typ.sym.flags != {}) and if (typ.sym != nil) and ({sfImportc, sfExportc} * typ.sym.flags != {}) and
@ -161,7 +159,7 @@ proc getTypeName(typ: PType): PRope =
else: else:
if typ.loc.r == nil: if typ.loc.r == nil:
typ.loc.r = if gCmd != cmdCompileToLLVM: con(typ.typeName, typ.id.toRope) typ.loc.r = if gCmd != cmdCompileToLLVM: con(typ.typeName, typ.id.toRope)
else: con(["%".toRope, typ.typeName, typ.id.toRope]) else: con([~"%", typ.typeName, typ.id.toRope])
result = typ.loc.r result = typ.loc.r
if result == nil: InternalError("getTypeName: " & $typ.kind) if result == nil: InternalError("getTypeName: " & $typ.kind)
@ -249,10 +247,10 @@ proc CacheGetType(tab: TIdTable, key: PType): PRope =
result = PRope(IdTableGet(tab, key)) result = PRope(IdTableGet(tab, key))
proc getTempName(): PRope = proc getTempName(): PRope =
result = ropeff("TMP$1", "%TMP$1", [toRope(backendId())]) result = rfmt(nil, "TMP$1", toRope(backendId()))
proc getGlobalTempName(): PRope = proc getGlobalTempName(): PRope =
result = ropeff("TMP$1", "@TMP$1", [toRope(backendId())]) result = rfmt(nil, "TMP$1", toRope(backendId()))
proc ccgIntroducedPtr(s: PSym): bool = proc ccgIntroducedPtr(s: PSym): bool =
var pt = skipTypes(s.typ, abstractInst) var pt = skipTypes(s.typ, abstractInst)
@ -273,7 +271,7 @@ proc ccgIntroducedPtr(s: PSym): bool =
else: result = false else: result = false
proc fillResult(param: PSym) = proc fillResult(param: PSym) =
fillLoc(param.loc, locParam, param.typ, ropeff("Result", "%Result", []), fillLoc(param.loc, locParam, param.typ, ~"Result",
OnStack) OnStack)
if (mapReturnType(param.typ) != ctArray) and IsInvalidReturnType(param.typ): if (mapReturnType(param.typ) != ctArray) and IsInvalidReturnType(param.typ):
incl(param.loc.flags, lfIndirect) incl(param.loc.flags, lfIndirect)
@ -291,21 +289,21 @@ proc genProcParams(m: BModule, t: PType, rettype, params: var PRope,
check: var TIntSet, declareEnvironment=true) = check: var TIntSet, declareEnvironment=true) =
params = nil params = nil
if (t.sons[0] == nil) or isInvalidReturnType(t.sons[0]): if (t.sons[0] == nil) or isInvalidReturnType(t.sons[0]):
rettype = toRope("void") rettype = ~"void"
else: else:
rettype = getTypeDescAux(m, t.sons[0], check) rettype = getTypeDescAux(m, t.sons[0], check)
for i in countup(1, sonsLen(t.n) - 1): for i in countup(1, sonsLen(t.n) - 1):
if t.n.sons[i].kind != nkSym: InternalError(t.n.info, "genProcParams") if t.n.sons[i].kind != nkSym: InternalError(t.n.info, "genProcParams")
var param = t.n.sons[i].sym var param = t.n.sons[i].sym
if isCompileTimeOnly(param.typ): continue if isCompileTimeOnly(param.typ): continue
if params != nil: app(params, ", ") if params != nil: app(params, ~", ")
fillLoc(param.loc, locParam, param.typ, mangleName(param), OnStack) fillLoc(param.loc, locParam, param.typ, mangleName(param), OnStack)
app(params, getParamTypeDesc(m, param.typ, check)) app(params, getParamTypeDesc(m, param.typ, check))
if ccgIntroducedPtr(param): if ccgIntroducedPtr(param):
app(params, "*") app(params, ~"*")
incl(param.loc.flags, lfIndirect) incl(param.loc.flags, lfIndirect)
param.loc.s = OnUnknown param.loc.s = OnUnknown
app(params, " ") app(params, ~" ")
app(params, param.loc.r) app(params, param.loc.r)
# declare the len field for open arrays: # declare the len field for open arrays:
var arr = param.typ var arr = param.typ

View file

@ -303,11 +303,11 @@ proc genObjectInit(p: BProc, section: TCProcSection, t: PType, a: TLoc,
while (s.kind == tyObject) and (s.sons[0] != nil): while (s.kind == tyObject) and (s.sons[0] != nil):
app(r, ".Sup") app(r, ".Sup")
s = skipTypes(s.sons[0], abstractInst) s = skipTypes(s.sons[0], abstractInst)
lineCg(p, section, "$1.m_type = $2;$n", [r, genTypeInfo(p.module, t)]) lineCg2(p, section, "$1.m_type = $2;$n", r, genTypeInfo(p.module, t))
of frEmbedded: of frEmbedded:
# worst case for performance: # worst case for performance:
var r = if takeAddr: addrLoc(a) else: rdLoc(a) var r = if takeAddr: addrLoc(a) else: rdLoc(a)
lineCg(p, section, "#objectInit($1, $2);$n", [r, genTypeInfo(p.module, t)]) lineCg2(p, section, "#objectInit($1, $2);$n", r, genTypeInfo(p.module, t))
type type
TAssignmentFlag = enum TAssignmentFlag = enum
@ -330,27 +330,27 @@ proc resetLoc(p: BProc, loc: var TLoc) =
nilLoc.r = toRope("NIM_NIL") nilLoc.r = toRope("NIM_NIL")
genRefAssign(p, loc, nilLoc, {afSrcIsNil}) genRefAssign(p, loc, nilLoc, {afSrcIsNil})
else: else:
lineF(p, cpsStmts, "$1 = 0;$n", [rdLoc(loc)]) lineCg2(p, cpsStmts, "$1 = 0;$n", rdLoc(loc))
else: else:
if loc.s != OnStack: if loc.s != OnStack:
lineCg(p, cpsStmts, "#genericReset((void*)$1, $2);$n", lineCg2(p, cpsStmts, "#genericReset((void*)$1, $2);$n",
[addrLoc(loc), genTypeInfo(p.module, loc.t)]) addrLoc(loc), genTypeInfo(p.module, loc.t))
# XXX: generated reset procs should not touch the m_type # XXX: generated reset procs should not touch the m_type
# field, so disabling this should be safe: # field, so disabling this should be safe:
genObjectInit(p, cpsStmts, loc.t, loc, true) genObjectInit(p, cpsStmts, loc.t, loc, true)
else: else:
lineF(p, cpsStmts, "memset((void*)$1, 0, sizeof($2));$n", lineCg2(p, cpsStmts, "memset((void*)$1, 0, sizeof($2));$n",
[addrLoc(loc), rdLoc(loc)]) addrLoc(loc), rdLoc(loc))
# XXX: We can be extra clever here and call memset only # XXX: We can be extra clever here and call memset only
# on the bytes following the m_type field? # on the bytes following the m_type field?
genObjectInit(p, cpsStmts, loc.t, loc, true) genObjectInit(p, cpsStmts, loc.t, loc, true)
proc constructLoc(p: BProc, loc: TLoc, section = cpsStmts) = proc constructLoc(p: BProc, loc: TLoc, section = cpsStmts) =
if not isComplexValueType(skipTypes(loc.t, abstractRange)): if not isComplexValueType(skipTypes(loc.t, abstractRange)):
lineF(p, section, "$1 = 0;$n", [rdLoc(loc)]) lineCg2(p, section, "$1 = 0;$n", rdLoc(loc))
else: else:
lineF(p, section, "memset((void*)$1, 0, sizeof($2));$n", lineCg2(p, section, "memset((void*)$1, 0, sizeof($2));$n",
[addrLoc(loc), rdLoc(loc)]) addrLoc(loc), rdLoc(loc))
genObjectInit(p, section, loc.t, loc, true) genObjectInit(p, section, loc.t, loc, true)
proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) = proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) =
@ -377,7 +377,7 @@ proc getTemp(p: BProc, t: PType, result: var TLoc) =
result.r = con("%LOC", toRope(p.labels)) result.r = con("%LOC", toRope(p.labels))
else: else:
result.r = con("LOC", toRope(p.labels)) result.r = con("LOC", toRope(p.labels))
lineF(p, cpsLocals, "$1 $2;$n", [getTypeDesc(p.module, t), result.r]) lineCg2(p, cpsLocals, "$1 $2;$n", getTypeDesc(p.module, t), result.r)
result.k = locTemp result.k = locTemp
result.a = - 1 result.a = - 1
result.t = getUniqueType(t) result.t = getUniqueType(t)
@ -403,11 +403,11 @@ proc keepAlive(p: BProc, toKeepAlive: TLoc) =
result.flags = {} result.flags = {}
if not isComplexValueType(skipTypes(toKeepAlive.t, abstractVarRange)): if not isComplexValueType(skipTypes(toKeepAlive.t, abstractVarRange)):
lineF(p, cpsStmts, "$1 = $2;$n", [rdLoc(result), rdLoc(toKeepAlive)]) lineCg2(p, cpsStmts, "$1 = $2;$n", rdLoc(result), rdLoc(toKeepAlive))
else: else:
lineCg(p, cpsStmts, lineCg2(p, cpsStmts,
"memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($3));$n", "memcpy((void*)$1, (NIM_CONST void*)$2, sizeof($3));$n",
[addrLoc(result), addrLoc(toKeepAlive), rdLoc(result)]) addrLoc(result), addrLoc(toKeepAlive), rdLoc(result))
proc initGCFrame(p: BProc): PRope = proc initGCFrame(p: BProc): PRope =
if p.gcFrameId > 0: result = ropef("struct {$1} GCFRAME;$n", p.gcFrameType) if p.gcFrameId > 0: result = ropef("struct {$1} GCFRAME;$n", p.gcFrameType)