From cb172328bae0d3201061f23de233a4a521418e17 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 7 Dec 2023 13:56:33 +0000 Subject: [PATCH] simple cases compile --- compiler/ccgcalls.nim | 6 +++--- compiler/ccgexprs.nim | 18 +++++++++--------- compiler/cgen.nim | 24 +++++++++++++++++------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/compiler/ccgcalls.nim b/compiler/ccgcalls.nim index 39dce8f5fb..4755ed1a2e 100644 --- a/compiler/ccgcalls.nim +++ b/compiler/ccgcalls.nim @@ -241,11 +241,11 @@ proc openArrayLoc(p: BProc, formalType: PType, n: PNode; result: var Rope) = var t: TLoc t.r = "(*$1)" % [a.rdLoc] result.add "($4) ? ((*$1)$3) : NIM_NIL, $2" % - [a.rdLoc, lenExpr(p, t), dataField(p, typKind == tyString), + [a.rdLoc, lenExpr(p, t, typKind == tyString), dataField(p, typKind == tyString), dataFieldAccessor(p, "*" & a.rdLoc)] else: result.add "($4) ? ($1$3) : NIM_NIL, $2" % - [a.rdLoc, lenExpr(p, a), dataField(p, typKind == tyString), dataFieldAccessor(p, a.rdLoc)] + [a.rdLoc, lenExpr(p, a, typKind == tyString), dataField(p, typKind == tyString), dataFieldAccessor(p, a.rdLoc)] of tyArray: result.add "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, a.t))] of tyPtr, tyRef: @@ -254,7 +254,7 @@ proc openArrayLoc(p: BProc, formalType: PType, n: PNode; result: var Rope) = var t: TLoc t.r = "(*$1)" % [a.rdLoc] result.add "($4) ? ((*$1)$3) : NIM_NIL, $2" % - [a.rdLoc, lenExpr(p, t), dataField(p, typKind == tyString), + [a.rdLoc, lenExpr(p, t, typKind == tyString), dataField(p, typKind == tyString), dataFieldAccessor(p, "*" & a.rdLoc)] of tyArray: result.add "$1, $2" % [rdLoc(a), rope(lengthOrd(p.config, lastSon(a.t)))] diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index c0ba474afa..da965c315f 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -309,7 +309,7 @@ proc genOpenArrayConv(p: BProc; d: TLoc; a: TLoc) = linefmt(p, cpsStmts, "#nimPrepareStrMutationV2($1);$n", [byRefLoc(p, a)]) linefmt(p, cpsStmts, "$1.Field0 = ($5) ? ($2$3) : NIM_NIL; $1.Field1 = $4;$n", - [rdLoc(d), a.rdLoc, dataField(p, isString = true), lenExpr(p, a), dataFieldAccessor(p, a.rdLoc)]) + [rdLoc(d), a.rdLoc, dataField(p, isString = true), lenExpr(p, a, isString = true), dataFieldAccessor(p, a.rdLoc)]) else: internalError(p.config, a.lode.info, "cannot handle " & $a.t.kind) @@ -1046,7 +1046,7 @@ proc genBoundsCheck(p: BProc; arr, a, b: TLoc) = linefmt(p, cpsStmts, "if ($2-$1 != -1 && " & "($1 < 0 || $1 >= $3 || $2 < 0 || $2 >= $3)){ #raiseIndexError4($1, $2, $3); ", - [rdLoc(a), rdLoc(b), lenExpr(p, arr)]) + [rdLoc(a), rdLoc(b), lenExpr(p, arr, ty.kind == tyString)]) raiseInstr(p, p.s(cpsStmts)) linefmt p, cpsStmts, "}$n", [] @@ -1086,7 +1086,7 @@ proc genSeqElem(p: BProc, n, x, y: PNode, d: var TLoc) = if optBoundsCheck in p.options: linefmt(p, cpsStmts, "if ($1 < 0 || $1 >= $2){ #raiseIndexError2($1,$2-1); ", - [rdCharLoc(b), lenExpr(p, a)]) + [rdCharLoc(b), lenExpr(p, a, ty.kind == tyString)]) raiseInstr(p, p.s(cpsStmts)) linefmt p, cpsStmts, "}$n", [] @@ -1261,7 +1261,7 @@ proc genStrConcat(p: BProc, e: PNode, d: var TLoc) = if e[i + 1].kind in {nkStrLit..nkTripleStrLit}: inc(L, e[i + 1].strVal.len) else: - lens.add(lenExpr(p, a)) + lens.add(lenExpr(p, a, isString = true)) lens.add(" + ") appends.add(ropecg(p.module, "#appendString($1, $2);$n", [strLoc(p, tmp), rdLoc(a)])) linefmt(p, cpsStmts, "$1 = #rawNewString($2$3);$n", [tmp.r, lens, L]) @@ -1301,7 +1301,7 @@ proc genStrAppend(p: BProc, e: PNode, d: var TLoc) = if e[i + 2].kind in {nkStrLit..nkTripleStrLit}: inc(L, e[i + 2].strVal.len) else: - lens.add(lenExpr(p, a)) + lens.add(lenExpr(p, a, isString = true)) lens.add(" + ") appends.add(ropecg(p.module, "#appendString($1, $2);$n", [strLoc(p, dest), rdLoc(a)])) @@ -1762,7 +1762,7 @@ proc genRepr(p: BProc, e: PNode, d: var TLoc) = of tyString, tySequence: putIntoDest(p, b, e, "($4) ? ($1$3) : NIM_NIL, $2" % - [rdLoc(a), lenExpr(p, a), dataField(p, typKind == tyString), dataFieldAccessor(p, a.rdLoc)], + [rdLoc(a), lenExpr(p, a, typKind == tyString), dataField(p, typKind == tyString), dataFieldAccessor(p, a.rdLoc)], a.storage) of tyArray: putIntoDest(p, b, e, @@ -1875,7 +1875,7 @@ proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) = else: unaryExpr(p, e, d, "#nimCStrLen($1)") of tyString: var a: TLoc = initLocExpr(p, e[1]) - var x = lenExpr(p, a) + var x = lenExpr(p, a, isString = true) if op == mHigh: x = "($1-1)" % [x] putIntoDest(p, d, e, x) of tySequence: @@ -2256,11 +2256,11 @@ proc genStrEquals(p: BProc, e: PNode, d: var TLoc) = if a.kind in {nkStrLit..nkTripleStrLit} and a.strVal == "": x = initLocExpr(p, e[2]) putIntoDest(p, d, e, - ropecg(p.module, "($1 == 0)", [lenExpr(p, x)])) + ropecg(p.module, "($1 == 0)", [lenExpr(p, x, isString = true)])) elif b.kind in {nkStrLit..nkTripleStrLit} and b.strVal == "": x = initLocExpr(p, e[1]) putIntoDest(p, d, e, - ropecg(p.module, "($1 == 0)", [lenExpr(p, x)])) + ropecg(p.module, "($1 == 0)", [lenExpr(p, x, isString = true)])) else: binaryExpr(p, e, d, "#eqStrings($1, $2)") diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 58b3a03640..543823a8b6 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -348,9 +348,9 @@ proc addRdLoc(a: TLoc; result: var Rope) = proc lenField(p: BProc): Rope {.inline.} = result = rope(if p.module.compileToCpp: "len" else: "Sup.len") -proc lenExpr(p: BProc; a: TLoc): Rope = +proc lenExpr(p: BProc; a: TLoc; isString = false): Rope = if optSeqDestructors in p.config.globalOptions: - if p.config.isDefined("nimSeqsV3"): + if isString and p.config.isDefined("nimSeqsV3"): result = ropecg(p.module, "(#nimStrLenV3($1))", [rdLoc(a)]) else: result = rdLoc(a) & ".len" @@ -486,10 +486,16 @@ proc resetLoc(p: BProc, loc: var TLoc) = assert loc.r != "" let atyp = skipTypes(loc.t, abstractInst) - if atyp.kind in {tyVar, tyLent}: - linefmt(p, cpsStmts, "$1->len = 0; $1->p = NIM_NIL;$n", [rdLoc(loc)]) + if p.config.isDefined("nimSeqsV3") and typ.kind == tyString: + if atyp.kind in {tyVar, tyLent}: + linefmt(p, cpsStmts, "$1->rawlen = 0; $1->p = NIM_NIL;$n", [rdLoc(loc)]) + else: + linefmt(p, cpsStmts, "$1.rawlen = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)]) else: - linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)]) + if atyp.kind in {tyVar, tyLent}: + linefmt(p, cpsStmts, "$1->len = 0; $1->p = NIM_NIL;$n", [rdLoc(loc)]) + else: + linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)]) elif not isComplexValueType(typ): if containsGcRef: var nilLoc: TLoc = initLoc(locTemp, loc.lode, OnStack) @@ -526,8 +532,12 @@ proc resetLoc(p: BProc, loc: var TLoc) = proc constructLoc(p: BProc, loc: var TLoc, isTemp = false) = let typ = loc.t - if optSeqDestructors in p.config.globalOptions and skipTypes(typ, abstractInst + {tyStatic}).kind in {tyString, tySequence}: - linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)]) + let typKind = skipTypes(typ, abstractInst + {tyStatic}).kind + if optSeqDestructors in p.config.globalOptions and typKind in {tyString, tySequence}: + if typKind == tyString and p.config.isDefined("nimSeqsV3"): + linefmt(p, cpsStmts, "$1.rawlen = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)]) + else: + linefmt(p, cpsStmts, "$1.len = 0; $1.p = NIM_NIL;$n", [rdLoc(loc)]) elif not isComplexValueType(typ): if containsGarbageCollectedRef(loc.t): var nilLoc: TLoc = initLoc(locTemp, loc.lode, OnStack)