codgen refactoring: prepare for alternative string/seq implementations

This commit is contained in:
Andreas Rumpf
2018-06-29 11:34:39 +02:00
parent a9e96888d0
commit b5a96d28c7
5 changed files with 29 additions and 21 deletions

View File

@@ -111,9 +111,9 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
of tyString, tySequence:
if skipTypes(n.typ, abstractInst).kind == tyVar and
not compileToCpp(p.module):
result = "(*$1)->data+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
result = "(*$1)$4+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dataField(p)]
else:
result = "$1->data+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c)]
result = "$1$4+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dataField(p)]
else:
internalError(p.config, "openArrayLoc: " & typeToString(a.t))
else:
@@ -124,15 +124,15 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
of tyString, tySequence:
if skipTypes(n.typ, abstractInst).kind == tyVar and
not compileToCpp(p.module):
result = "(*$1)->data, (*$1 ? (*$1)->$2 : 0)" % [a.rdLoc, lenField(p)]
result = "(*$1)$3, (*$1 ? (*$1)->$2 : 0)" % [a.rdLoc, lenField(p), dataField(p)]
else:
result = "$1->data, ($1 ? $1->$2 : 0)" % [a.rdLoc, lenField(p)]
result = "$1$3, ($1 ? $1->$2 : 0)" % [a.rdLoc, lenField(p), dataField(p)]
of tyArray:
result = "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, a.t))]
of tyPtr, tyRef:
case lastSon(a.t).kind
of tyString, tySequence:
result = "(*$1)->data, (*$1 ? (*$1)->$2 : 0)" % [a.rdLoc, lenField(p)]
result = "(*$1)$3, (*$1 ? (*$1)->$2 : 0)" % [a.rdLoc, lenField(p), dataField(p)]
of tyArray:
result = "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, lastSon(a.t)))]
else:
@@ -142,7 +142,7 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
proc genArgStringToCString(p: BProc, n: PNode): Rope {.inline.} =
var a: TLoc
initLocExpr(p, n.sons[0], a)
result = "($1 ? $1->data : (NCSTRING)\"\")" % [a.rdLoc]
result = ropecg(p.module, "#nimToCStringConv($1)", [a.rdLoc])
proc genArg(p: BProc, n: PNode, param: PSym; call: PNode): Rope =
var a: TLoc

View File

@@ -928,7 +928,7 @@ proc genSeqElem(p: BProc, n, x, y: PNode, d: var TLoc) =
if skipTypes(a.t, abstractVar).kind in {tyRef, tyPtr}:
a.r = ropecg(p.module, "(*$1)", a.r)
putIntoDest(p, d, n,
ropecg(p.module, "$1->data[$2]", rdLoc(a), rdCharLoc(b)), a.storage)
ropecg(p.module, "$1$3[$2]", rdLoc(a), rdCharLoc(b), dataField(p)), a.storage)
proc genBracketExpr(p: BProc; n: PNode; d: var TLoc) =
var ty = skipTypes(n.sons[0].typ, abstractVarRange + tyUserTypeClasses)
@@ -994,7 +994,7 @@ proc genEcho(p: BProc, n: PNode) =
add(args, ", \"\"")
else:
initLocExpr(p, it, a)
addf(args, ", $1? ($1)->data:\"\"", [rdLoc(a)])
add(args, ropecg(p.module, ", #nimToCStringConv($1)", [rdLoc(a)]))
p.module.includeHeader("<base/log.h>")
linefmt(p, cpsStmts, """Genode::log(""$1);$n""", args)
else:
@@ -1113,7 +1113,7 @@ proc genSeqElemAppend(p: BProc, e: PNode, d: var TLoc) =
initLoc(dest, locExpr, e.sons[2], OnHeap)
getIntTemp(p, tmpL)
lineCg(p, cpsStmts, "$1 = $2->$3++;$n", tmpL.r, rdLoc(a), lenField(p))
dest.r = ropecg(p.module, "$1->data[$2]", rdLoc(a), tmpL.r)
dest.r = ropecg(p.module, "$1$3[$2]", rdLoc(a), tmpL.r, dataField(p))
genAssignment(p, dest, b, {needToCopy, afDestIsNil})
gcUsage(p.config, e)
@@ -1287,7 +1287,7 @@ proc genSeqConstr(p: BProc, n: PNode, d: var TLoc) =
genNewSeqAux(p, dest[], intLiteral(sonsLen(n)))
for i in countup(0, sonsLen(n) - 1):
initLoc(arr, locExpr, n[i], OnHeap)
arr.r = ropecg(p.module, "$1->data[$2]", rdLoc(dest[]), intLiteral(i))
arr.r = ropecg(p.module, "$1$3[$2]", rdLoc(dest[]), intLiteral(i), dataField(p))
arr.storage = OnHeap # we know that sequences are on the heap
expr(p, n[i], arr)
gcUsage(p.config, n)
@@ -1313,7 +1313,7 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) =
if L < 10:
for i in countup(0, L - 1):
initLoc(elem, locExpr, lodeTyp elemType(skipTypes(n.typ, abstractInst)), OnHeap)
elem.r = ropecg(p.module, "$1->data[$2]", rdLoc(d), intLiteral(i))
elem.r = ropecg(p.module, "$1$3[$2]", rdLoc(d), intLiteral(i), dataField(p))
elem.storage = OnHeap # we know that sequences are on the heap
initLoc(arr, locExpr, lodeTyp elemType(skipTypes(n.sons[1].typ, abstractInst)), a.storage)
arr.r = ropecg(p.module, "$1[$2]", rdLoc(a), intLiteral(i))
@@ -1324,7 +1324,7 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) =
let oldCode = p.s(cpsStmts)
linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n", i.r, L.rope)
initLoc(elem, locExpr, lodeTyp elemType(skipTypes(n.typ, abstractInst)), OnHeap)
elem.r = ropecg(p.module, "$1->data[$2]", rdLoc(d), rdLoc(i))
elem.r = ropecg(p.module, "$1$3[$2]", rdLoc(d), rdLoc(i), dataField(p))
elem.storage = OnHeap # we know that sequences are on the heap
initLoc(arr, locExpr, lodeTyp elemType(skipTypes(n.sons[1].typ, abstractInst)), a.storage)
arr.r = ropecg(p.module, "$1[$2]", rdLoc(a), rdLoc(i))
@@ -1428,7 +1428,7 @@ proc genRepr(p: BProc, e: PNode, d: var TLoc) =
putIntoDest(p, b, e, "$1, $1Len_0" % [rdLoc(a)], a.storage)
of tyString, tySequence:
putIntoDest(p, b, e,
"$1->data, ($1 ? $1->$2 : 0)" % [rdLoc(a), lenField(p)], a.storage)
"$1$3, ($1 ? $1->$2 : 0)" % [rdLoc(a), lenField(p), dataField(p)], a.storage)
of tyArray:
putIntoDest(p, b, e,
"$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, a.t))], a.storage)
@@ -1751,7 +1751,8 @@ proc genConv(p: BProc, e: PNode, d: var TLoc) =
proc convStrToCStr(p: BProc, n: PNode, d: var TLoc) =
var a: TLoc
initLocExpr(p, n.sons[0], a)
putIntoDest(p, d, n, "($1 ? $1->data : (NCSTRING)\"\")" % [rdLoc(a)],
putIntoDest(p, d, n,
ropecg(p.module, "#nimToCStringConv($1)", [rdLoc(a)]),
a.storage)
proc convCStrToStr(p: BProc, n: PNode, d: var TLoc) =

View File

@@ -108,9 +108,9 @@ proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) =
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo(), tyInt), i)
let oldCode = p.s(cpsStmts)
lineF(p, cpsStmts, "for ($1 = 0; $1 < $2->$3; $1++) {$n",
[i.r, accessor, rope(if c.p.module.compileToCpp: "len" else: "Sup.len")])
[i.r, accessor, lenField(c.p)])
let oldLen = p.s(cpsStmts).len
genTraverseProc(c, "$1->data[$2]" % [accessor, i.r], typ.sons[0])
genTraverseProc(c, "$1$3[$2]" % [accessor, i.r, dataField(c.p)], typ.sons[0])
if p.s(cpsStmts).len == oldLen:
# do not emit dummy long loops for faster debug builds:
p.s(cpsStmts) = oldCode

View File

@@ -235,6 +235,12 @@ proc getTempName(m: BModule): Rope =
result = m.tmpBase & rope(m.labels)
inc m.labels
proc lenField(p: BProc): Rope =
result = rope(if p.module.compileToCpp: "len" else: "Sup.len")
proc dataField(p: BProc): Rope =
result = rope"->data"
include ccgliterals
include ccgtypes
@@ -495,9 +501,6 @@ proc initLocExprSingleUse(p: BProc, e: PNode, result: var TLoc) =
result.flags.incl lfSingleUse
expr(p, e, result)
proc lenField(p: BProc): Rope =
result = rope(if p.module.compileToCpp: "len" else: "Sup.len")
include ccgcalls, "ccgstmts.nim"
proc initFrame(p: BProc, procname, filename: Rope): Rope =

View File

@@ -94,8 +94,8 @@ proc mnewString(len: int): NimString {.compilerProc.} =
result.len = len
proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} =
var start = max(start, 0)
var len = min(last, s.len-1) - start + 1
let start = max(start, 0)
let len = min(last, s.len-1) - start + 1
if len > 0:
result = rawNewStringNoInit(len)
result.len = len
@@ -104,6 +104,10 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} =
else:
result = rawNewString(len)
proc nimToCStringConv(s: NimString): cstring {.compilerProc, inline.} =
if s == nil or s.len == 0: result = cstring""
else: result = cstring(addr s.data)
proc copyStr(s: NimString, start: int): NimString {.compilerProc.} =
result = copyStrLast(s, start, s.len-1)