Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make some trivial sizeof calls in codegen use types/literals #24445

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3055,7 +3055,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 @@ -3087,7 +3088,7 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
cOp(BitAnd, "NU", cCast("NU", aa), cUintValue(7))))
else:
# small set
var ts = "NU" & $(getSize(p.config, e.typ) * 8)
var ts = "NU" & $(size * 8)
p.s(cpsStmts).addAssignment(rdLoc(d), cIntValue(0))
for it in e.sons:
if it.kind == nkRange:
Expand All @@ -3103,15 +3104,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
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