From b5a96d28c7e848a9a8fd9d7766d8c8462a12e2b8 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 29 Jun 2018 11:34:39 +0200 Subject: [PATCH] codgen refactoring: prepare for alternative string/seq implementations --- compiler/ccgcalls.nim | 12 ++++++------ compiler/ccgexprs.nim | 17 +++++++++-------- compiler/ccgtrav.nim | 4 ++-- compiler/cgen.nim | 9 ++++++--- lib/system/sysstr.nim | 8 ++++++-- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/compiler/ccgcalls.nim b/compiler/ccgcalls.nim index 2f9cc822b0..9712d5dcec 100644 --- a/compiler/ccgcalls.nim +++ b/compiler/ccgcalls.nim @@ -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 diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 0e8af5af5d..fc3b520374 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -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("") 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) = diff --git a/compiler/ccgtrav.nim b/compiler/ccgtrav.nim index 4514ce7dc7..e74d5a953a 100644 --- a/compiler/ccgtrav.nim +++ b/compiler/ccgtrav.nim @@ -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 diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 928d1fd225..a57192f9cd 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -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 = diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index 7b81f54da9..90ae91cf53 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -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)