Skip to content

Commit

Permalink
make all sizeof calls in codegen use types
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 14, 2024
1 parent 726195d commit 8a3bf29
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
13 changes: 7 additions & 6 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ proc genGenericAsgn(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
if (dest.storage == OnStack and p.config.selectedGC != gcGo) or not usesWriteBarrier(p.config):
let rad = addrLoc(p.config, dest)
let ras = addrLoc(p.config, src)
let rd = rdLoc(dest)
let td = getTypeDesc(p.module, dest.t)
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimCopyMem"),
cCast("void*", rad),
cCast(ptrConstType("void"), ras),
cSizeof(rd))
cSizeof(td))
else:
let rad = addrLoc(p.config, dest)
let ras = addrLoc(p.config, src)
Expand Down Expand Up @@ -2549,7 +2549,7 @@ proc genCast(p: BProc, e: PNode, d: var TLoc) =
p.s(cpsLocals).addField(name = "source", typ = srcTyp)
p.s(cpsLocals).addCallStmt(cgsymValue(p.module, "nimZeroMem"),
cAddr("LOC" & lbl),
cSizeof("LOC" & lbl))
cIntValue(destsize))
else:
p.s(cpsLocals).addVarWithType(kind = Local, name = "LOC" & lbl):
p.s(cpsLocals).addUnionType():
Expand Down Expand Up @@ -3051,7 +3051,8 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
putIntoDest(p, d, e, extract(elem))
else:
if d.k == locNone: d = getTemp(p, e.typ)
if getSize(p.config, e.typ) > 8:
let size = getSize(p.config, e.typ)
if size > 8:
# big set:
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimZeroMem"),
rdLoc(d),
Expand Down Expand Up @@ -3099,15 +3100,15 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
p.s(cpsStmts).addForRangeInclusive(ri, aa, bb):
p.s(cpsStmts).addInPlaceOp(BitOr, ts, rd,
cOp(Shl, ts, cCast(ts, cIntValue(1)),
cOp(Mod, ts, ri, cOp(Mul, ts, cSizeof(ts), cIntValue(8)))))
cOp(Mod, ts, ri, cOp(Mul, ts, cIntValue(size), cIntValue(8)))))
else:
a = initLocExpr(p, it)
var aa: Snippet = ""
rdSetElemLoc(p.config, a, e.typ, aa)
let rd = rdLoc(d)
p.s(cpsStmts).addInPlaceOp(BitOr, ts, rd,
cOp(Shl, ts, cCast(ts, cIntValue(1)),
cOp(Mod, ts, aa, cOp(Mul, ts, cSizeof(ts), cIntValue(8)))))
cOp(Mod, ts, aa, cOp(Mul, ts, cIntValue(size), cIntValue(8)))))

proc genTupleConstr(p: BProc, n: PNode, d: var TLoc) =
var rec: TLoc
Expand Down
11 changes: 6 additions & 5 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ proc genVarTuple(p: BProc, n: PNode) =
typ = "NIM_BOOL",
initializer = "NIM_FALSE")
for curr in hcrGlobals:
let rc = rdLoc(curr.loc)
let tc = getTypeDesc(p.module, curr.loc.t)
p.s(cpsLocals).addInPlaceOp(BitOr, "NIM_BOOL",
hcrCond,
cCall("hcrRegisterGlobal",
getModuleDllPath(p.module, n[0].sym),
'"' & curr.loc.snippet & '"',
cSizeof(rc),
cSizeof(tc),
curr.tp,
cCast("void**", cAddr(curr.loc.snippet))))

Expand Down Expand Up @@ -434,11 +434,11 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
if forHcr and targetProc.blocks.len > 3 and v.owner.kind == skModule:
# put it in the locals section - mainly because of loops which
# use the var in a call to resetLoc() in the statements section
let rv = rdLoc(v.loc)
let tv = getTypeDesc(p.module, v.loc.t)
p.s(cpsLocals).addCallStmt("hcrRegisterGlobal",
getModuleDllPath(p.module, v),
'"' & v.loc.snippet & '"',
cSizeof(rv),
cSizeof(tv),
traverseProc,
cCast("void**", cAddr(v.loc.snippet)))
# nothing special left to do later on - let's avoid closing and reopening blocks
Expand All @@ -449,12 +449,13 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
# be able to re-run it but without the top level code - just the init of globals
var hcrInit = default(IfBuilder)
if forHcr:
let tv = getTypeDesc(p.module, v.loc.t)
startBlockWith(targetProc):
hcrInit = initIfStmt(p.s(cpsStmts))
initElifBranch(p.s(cpsStmts), hcrInit, cCall("hcrRegisterGlobal",
getModuleDllPath(p.module, v),
'"' & v.loc.snippet & '"',
cSizeof(rdLoc(v.loc)),
cSizeof(tv),
traverseProc,
cCast("void**", cAddr(v.loc.snippet))))
if value.kind != nkEmpty and valueAsRope.len == 0:
Expand Down
10 changes: 6 additions & 4 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1984,18 +1984,20 @@ proc registerModuleToMain(g: BModuleList; m: BModule) =
# bug #16265.
let osModulePath = ($systemModulePath).replace("stdlib_system", "stdlib_os").rope
g.mainDatInit.addCallStmt("hcrAddModule", osModulePath)
g.mainDatInit.addVar(name = "cmd_count", typ = ptrType("int"))
g.mainDatInit.addVar(name = "cmd_line", typ = ptrType(ptrType(ptrType("char"))))
let cmdCountTyp = ptrType("int")
let cmdLineTyp = ptrType(ptrType(ptrType("char")))
g.mainDatInit.addVar(name = "cmd_count", typ = cmdCountTyp)
g.mainDatInit.addVar(name = "cmd_line", typ = cmdLineTyp)
g.mainDatInit.addCallStmt("hcrRegisterGlobal",
osModulePath,
"\"cmdCount\"",
cSizeof("cmd_count"),
cSizeof(cmdCountTyp),
"NULL",
cCast("void**", cAddr("cmd_count")))
g.mainDatInit.addCallStmt("hcrRegisterGlobal",
osModulePath,
"\"cmdLine\"",
cSizeof("cmd_line"),
cSizeof(cmdLineTyp),
"NULL",
cCast("void**", cAddr("cmd_line")))
g.mainDatInit.addAssignment(cDeref("cmd_count"), "cmdCount")
Expand Down

0 comments on commit 8a3bf29

Please sign in to comment.