Skip to content

Commit

Permalink
produce better code for object constructions and 'result' [backport] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Sep 11, 2023
1 parent 7e86cd6 commit 8f5b90f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
8 changes: 6 additions & 2 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =

var tmp: TLoc = default(TLoc)
var r: Rope
let needsZeroMem = p.config.selectedGC notin {gcArc, gcAtomicArc, gcOrc} or nfAllFieldsSet notin e.flags
if useTemp:
tmp = getTemp(p, t)
r = rdLoc(tmp)
Expand All @@ -1545,10 +1546,13 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
t = t.lastSon.skipTypes(abstractInstOwned)
r = "(*$1)" % [r]
gcUsage(p.config, e)
else:
elif needsZeroMem:
constructLoc(p, tmp)
else:
genObjectInit(p, cpsStmts, t, tmp, constructObj)
else:
resetLoc(p, d)
if needsZeroMem: resetLoc(p, d)
else: genObjectInit(p, cpsStmts, d.t, d, if isRef: constructRefObj else: constructObj)
r = rdLoc(d)
discard getTypeDesc(p.module, t)
let ty = getUniqueType(t)
Expand Down
22 changes: 16 additions & 6 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -984,22 +984,26 @@ proc closureSetup(p: BProc, prc: PSym) =
linefmt(p, cpsStmts, "$1 = ($2) ClE_0;$n",
[rdLoc(env.loc), getTypeDesc(p.module, env.typ)])

const harmless = {nkConstSection, nkTypeSection, nkEmpty, nkCommentStmt, nkTemplateDef,
nkMacroDef, nkMixinStmt, nkBindStmt, nkFormalParams} +
declarativeDefs

proc containsResult(n: PNode): bool =
result = false
case n.kind
of nkEmpty..pred(nkSym), succ(nkSym)..nkNilLit, nkFormalParams:
of succ(nkEmpty)..pred(nkSym), succ(nkSym)..nkNilLit, harmless:
discard
of nkReturnStmt:
for i in 0..<n.len:
if containsResult(n[i]): return true
result = n.len > 0 and n[0].kind == nkEmpty
of nkSym:
if n.sym.kind == skResult:
result = true
else:
for i in 0..<n.len:
if containsResult(n[i]): return true

const harmless = {nkConstSection, nkTypeSection, nkEmpty, nkCommentStmt, nkTemplateDef,
nkMacroDef, nkMixinStmt, nkBindStmt, nkFormalParams} +
declarativeDefs

proc easyResultAsgn(n: PNode): PNode =
result = nil
case n.kind
Expand Down Expand Up @@ -1174,7 +1178,13 @@ proc genProcAux*(m: BModule, prc: PSym) =
# declare the result symbol:
assignLocalVar(p, resNode)
assert(res.loc.r != "")
initLocalVar(p, res, immediateAsgn=false)
if p.config.selectedGC in {gcArc, gcAtomicArc, gcOrc} and

This comment has been minimized.

Copy link
@ringabout

ringabout Dec 1, 2023

Member

for the record #22909

allPathsAsgnResult(procBody) == InitSkippable:
# In an ideal world the codegen could rely on injectdestructors doing its job properly
# and then the analysis step would not be required.
discard "result init optimized out"
else:
initLocalVar(p, res, immediateAsgn=false)
returnStmt = ropecg(p.module, "\treturn $1;$n", [rdLoc(res.loc)])
elif sfConstructor in prc.flags:
fillLoc(resNode.sym.loc, locParam, resNode, "this", OnHeap)
Expand Down
11 changes: 7 additions & 4 deletions tests/misc/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,13 @@ sub/mmain.idx""", context
check execCmdEx(cmd) == ("12\n", 0)

block: # bug #15316
let file = testsDir / "misc/m15316.nim"
let cmd = fmt"{nim} check --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("m15316.nim(1, 15) Error: expression expected, but found \')\'\nm15316.nim(2, 1) Error: expected: \':\', but got: \'[EOF]\'\nm15316.nim(2, 1) Error: expression expected, but found \'[EOF]\'\nm15316.nim(2, 1) " &
"Error: expected: \')\', but got: \'[EOF]\'\nError: illformed AST: \n", 1)
when not defined(windows):
# This never worked reliably on Windows. Needs further investigation but it is hard to reproduce.
# Looks like a mild stack corruption when bailing out of nested exception handling.
let file = testsDir / "misc/m15316.nim"
let cmd = fmt"{nim} check --hints:off --nimcache:{nimcache} {file}"
check execCmdEx(cmd) == ("m15316.nim(1, 15) Error: expression expected, but found \')\'\nm15316.nim(2, 1) Error: expected: \':\', but got: \'[EOF]\'\nm15316.nim(2, 1) Error: expression expected, but found \'[EOF]\'\nm15316.nim(2, 1) " &
"Error: expected: \')\', but got: \'[EOF]\'\nError: illformed AST: \n", 1)


block: # config.nims, nim.cfg, hintConf, bug #16557
Expand Down

0 comments on commit 8f5b90f

Please sign in to comment.