Skip to content

Commit

Permalink
handle cases i can find of array to pointer decay
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Dec 5, 2024
1 parent ac1780f commit e595c24
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
6 changes: 6 additions & 0 deletions compiler/cbuilderexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,9 @@ template cUnlikely(val: Snippet): Snippet =
val # not implemented
else:
"NIM_UNLIKELY(" & val & ")"

template arrayAddr(val: Snippet): Snippet =
when buildNifc:
cAddr(subscript(val, cIntValue(0)))
else:
val
2 changes: 2 additions & 0 deletions compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ proc genArg(p: BProc, n: PNode, param: PSym; call: PNode; result: var Builder; n
{lfHeader, lfNoDecl} * callee.sym.loc.flags != {} and
needsIndirect:
addAddrLoc(p.config, a, result)
elif not needsIndirect and buildNifc:
addAddrLoc(p.config, a, result)
else:
addRdLoc(a, result)
else:
Expand Down
13 changes: 7 additions & 6 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ proc genAssignment(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
let rd = rdLoc(dest)
let rs = rdLoc(src)
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimCopyMem"),
cCast(CPointer, rd),
cCast(CConstPointer, rs),
cCast(CPointer, arrayAddr(rd)),
cCast(CConstPointer, arrayAddr(rs)),
cIntValue(getSize(p.config, dest.t)))
else:
simpleAsgn(p.s(cpsStmts), dest, src)
Expand Down Expand Up @@ -530,8 +530,8 @@ proc genDeepCopy(p: BProc; dest, src: TLoc) =
let rd = rdLoc(dest)
let rs = rdLoc(src)
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimCopyMem"),
cCast(CPointer, rd),
cCast(CConstPointer, rs),
cCast(CPointer, arrayAddr(rd)),
cCast(CConstPointer, arrayAddr(rs)),
cIntValue(getSize(p.config, dest.t)))
else:
simpleAsgn(p.s(cpsStmts), dest, src)
Expand Down Expand Up @@ -1006,7 +1006,8 @@ proc genAddr(p: BProc, e: PNode, d: var TLoc) =
var a: TLoc = initLocExpr(p, e[0])
putIntoDest(p, d, e, cAddr(a.snippet), a.storage)
#Message(e.info, warnUser, "HERE NEW &")
elif mapType(p.config, e[0].typ, mapTypeChooser(e[0]) == skParam) == ctArray or isCppRef(p, e.typ):
elif (mapType(p.config, e[0].typ, mapTypeChooser(e[0]) == skParam) == ctArray and
not buildNifc) or isCppRef(p, e.typ):
expr(p, e[0], d)
# bug #19497
d.lode = e
Expand Down Expand Up @@ -1462,7 +1463,7 @@ proc genEcho(p: BProc, n: PNode) =
var a: TLoc = initLocExpr(p, n)
let ra = a.rdLoc
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "echoBinSafe"),
ra,
arrayAddr(ra),
cIntValue(n.len))
when false:
p.module.includeHeader("<stdio.h>")
Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1818,7 +1818,7 @@ proc genTypeInfoV2OldImpl(m: BModule; t, origType: PType, name: Rope; info: TLin
else:
let arrTyp = ""
genDisplay(m.s[cfsVars], m, t, objDepth, arrTyp)
typeEntry.addFieldAssignment(name, "display", objDisplayStore)
typeEntry.addFieldAssignment(name, "display", arrayAddr(objDisplayStore))

let dispatchMethods = toSeq(getMethodsPerType(m.g.graph, t))
if dispatchMethods.len > 0:
Expand Down Expand Up @@ -1883,7 +1883,7 @@ proc genTypeInfoV2Impl(m: BModule; t, origType: PType, name: Rope; info: TLineIn
len = objDepth + 1):
genDisplay(m.s[cfsVars], m, t, objDepth)
typeEntry.addField(typeInit, name = "display"):
typeEntry.add(objDisplayStore)
typeEntry.add(arrayAddr(objDisplayStore))
if isDefined(m.config, "nimTypeNames"):
var typeName: Rope
if t.kind in {tyObject, tyDistinct}:
Expand Down
25 changes: 15 additions & 10 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,23 +409,28 @@ template mapTypeChooser(n: PNode): TSymKind =
template mapTypeChooser(a: TLoc): TSymKind = mapTypeChooser(a.lode)

proc addAddrLoc(conf: ConfigRef; a: TLoc; result: var Builder) =
if lfIndirect notin a.flags and mapType(conf, a.t, mapTypeChooser(a) == skParam) != ctArray:
result.add wrapPar(cAddr(a.snippet))
else:
if lfIndirect in a.flags:
result.add a.snippet
elif mapType(conf, a.t, mapTypeChooser(a) == skParam) == ctArray:
result.add arrayAddr(a.snippet)
else:
result.add wrapPar(cAddr(a.snippet))

proc addrLoc(conf: ConfigRef; a: TLoc): Rope =
if lfIndirect notin a.flags and mapType(conf, a.t, mapTypeChooser(a) == skParam) != ctArray:
result = wrapPar(cAddr(a.snippet))
else:
if lfIndirect in a.flags:
result = a.snippet
elif mapType(conf, a.t, mapTypeChooser(a) == skParam) == ctArray:
result = arrayAddr(a.snippet)
else:
result = wrapPar(cAddr(a.snippet))

proc byRefLoc(p: BProc; a: TLoc): Rope =
if lfIndirect notin a.flags and mapType(p.config, a.t, mapTypeChooser(a) == skParam) != ctArray and not
p.module.compileToCpp:
result = wrapPar(cAddr(a.snippet))
else:
if lfIndirect in a.flags or p.module.compileToCpp:
result = a.snippet
elif mapType(p.config, a.t, mapTypeChooser(a) == skParam) == ctArray:
result = arrayAddr(a.snippet)
else:
result = wrapPar(cAddr(a.snippet))

proc rdCharLoc(a: TLoc): Rope =
# read a location that may need a char-cast:
Expand Down

0 comments on commit e595c24

Please sign in to comment.