Skip to content

Commit 2c56872

Browse files
authored
use cbuilder for int, set, const literals (#24336)
Most of this code was already structured in the `var Builder` manner. `intLiteral` etc could be used for all int literals in the future.
1 parent ca5df9a commit 2c56872

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

compiler/cbuilderexprs.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ proc procPtrType(conv: TCallingConvention, rettype: Snippet, name: string): Snip
1919
proc cCast(typ, value: Snippet): Snippet =
2020
"((" & typ & ") " & value & ")"
2121

22+
template addCast(builder: var Builder, typ: Snippet, valueBody: typed) =
23+
## adds a cast to `typ` with value built by `valueBody`
24+
builder.add "(("
25+
builder.add typ
26+
builder.add ") "
27+
valueBody
28+
builder.add ")"
29+
2230
proc cAddr(value: Snippet): Snippet =
2331
"&" & value
2432

compiler/ccgexprs.nim

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ proc getNullValueAuxT(p: BProc; orig, t: PType; obj, constOrNil: PNode,
2020

2121
proc rdSetElemLoc(conf: ConfigRef; a: TLoc, typ: PType; result: var Rope)
2222

23-
proc int64Literal(i: BiggestInt; result: var Rope) =
23+
proc int64Literal(i: BiggestInt; result: var Builder) =
2424
if i > low(int64):
2525
result.add "IL64($1)" % [rope(i)]
2626
else:
2727
result.add "(IL64(-9223372036854775807) - IL64(1))"
2828

29-
proc uint64Literal(i: uint64; result: var Rope) = result.add rope($i & "ULL")
29+
proc uint64Literal(i: uint64; result: var Builder) =
30+
result.add rope($i & "ULL")
3031

31-
proc intLiteral(i: BiggestInt; result: var Rope) =
32+
proc intLiteral(i: BiggestInt; result: var Builder) =
3233
if i > low(int32) and i <= high(int32):
3334
result.add rope(i)
3435
elif i == low(int32):
@@ -39,10 +40,10 @@ proc intLiteral(i: BiggestInt; result: var Rope) =
3940
else:
4041
result.add "(IL64(-9223372036854775807) - IL64(1))"
4142

42-
proc intLiteral(i: Int128; result: var Rope) =
43+
proc intLiteral(i: Int128; result: var Builder) =
4344
intLiteral(toInt64(i), result)
4445

45-
proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Rope) =
46+
proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Builder) =
4647
case n.kind
4748
of nkCharLit..nkUInt64Lit:
4849
var k: TTypeKind
@@ -63,11 +64,8 @@ proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Rope) =
6364
of tyInt64: int64Literal(n.intVal, result)
6465
of tyUInt64: uint64Literal(uint64(n.intVal), result)
6566
else:
66-
result.add "(("
67-
result.add getTypeDesc(p.module, ty)
68-
result.add ")"
69-
intLiteral(n.intVal, result)
70-
result.add ")"
67+
result.addCast(getTypeDesc(p.module, ty)):
68+
intLiteral(n.intVal, result)
7169
of nkNilLit:
7270
let k = if ty == nil: tyPointer else: skipTypes(ty, abstractVarRange).kind
7371
if k == tyProc and skipTypes(ty, abstractVarRange).callConv == ccClosure:
@@ -76,14 +74,22 @@ proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Rope) =
7674
if id == p.module.labels:
7775
# not found in cache:
7876
inc(p.module.labels)
79-
p.module.s[cfsStrData].addf(
80-
"static NIM_CONST $1 $2 = {NIM_NIL,NIM_NIL};$n",
81-
[getTypeDesc(p.module, ty), tmpName])
77+
var data = newBuilder("")
78+
data.addVarWithTypeAndInitializer(kind = Const, name = tmpName):
79+
data.add(getTypeDesc(p.module, ty))
80+
do:
81+
var closureInit: StructInitializer
82+
data.addStructInitializer(closureInit, kind = siOrderedStruct):
83+
data.addField(closureInit, name = "ClP_0"):
84+
data.add("NIM_NIL")
85+
data.addField(closureInit, name = "ClE_0"):
86+
data.add("NIM_NIL")
87+
p.module.s[cfsStrData].add(data)
8288
result.add tmpName
8389
elif k in {tyPointer, tyNil, tyProc}:
8490
result.add rope("NIM_NIL")
8591
else:
86-
result.add "(($1) NIM_NIL)" % [getTypeDesc(p.module, ty)]
92+
result.add cCast(getTypeDesc(p.module, ty), "NIM_NIL")
8793
of nkStrLit..nkTripleStrLit:
8894
let k = if ty == nil: tyString
8995
else: skipTypes(ty, abstractVarRange + {tyStatic, tyUserTypeClass, tyUserTypeClassInst}).kind
@@ -109,30 +115,24 @@ proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Rope) =
109115
else:
110116
internalError(p.config, n.info, "genLiteral(" & $n.kind & ')')
111117

112-
proc genLiteral(p: BProc, n: PNode; result: var Rope) =
118+
proc genLiteral(p: BProc, n: PNode; result: var Builder) =
113119
genLiteral(p, n, n.typ, result)
114120

115-
proc genRawSetData(cs: TBitSet, size: int; result: var Rope) =
121+
proc genRawSetData(cs: TBitSet, size: int; result: var Builder) =
116122
if size > 8:
117-
var res = "{\n"
118-
for i in 0..<size:
119-
res.add "0x"
120-
res.add "0123456789abcdef"[cs[i] div 16]
121-
res.add "0123456789abcdef"[cs[i] mod 16]
122-
if i < size - 1:
123-
# not last iteration
124-
if i mod 8 == 7:
125-
res.add ",\n"
126-
else:
127-
res.add ", "
128-
else:
129-
res.add "}\n"
130-
131-
result.add rope(res)
123+
var setInit: StructInitializer
124+
result.addStructInitializer(setInit, kind = siArray):
125+
for i in 0..<size:
126+
if i mod 8 == 0:
127+
result.add("\n")
128+
result.addField(setInit, name = ""):
129+
result.add "0x"
130+
result.add "0123456789abcdef"[cs[i] div 16]
131+
result.add "0123456789abcdef"[cs[i] mod 16]
132132
else:
133133
intLiteral(cast[BiggestInt](bitSetToWord(cs, size)), result)
134134

135-
proc genSetNode(p: BProc, n: PNode; result: var Rope) =
135+
proc genSetNode(p: BProc, n: PNode; result: var Builder) =
136136
var size = int(getSize(p.config, n.typ))
137137
let cs = toBitSet(p.config, n)
138138
if size > 8:
@@ -141,10 +141,12 @@ proc genSetNode(p: BProc, n: PNode; result: var Rope) =
141141
if id == p.module.labels:
142142
# not found in cache:
143143
inc(p.module.labels)
144-
p.module.s[cfsStrData].addf("static NIM_CONST $1 $2 = ",
145-
[getTypeDesc(p.module, n.typ), tmpName])
146-
genRawSetData(cs, size, p.module.s[cfsStrData])
147-
p.module.s[cfsStrData].addf(";$n", [])
144+
var data = newBuilder("")
145+
data.addVarWithTypeAndInitializer(kind = Const, name = tmpName):
146+
data.add(getTypeDesc(p.module, n.typ))
147+
do:
148+
genRawSetData(cs, size, data)
149+
p.module.s[cfsStrData].add(data)
148150
result.add tmpName
149151
else:
150152
genRawSetData(cs, size, result)
@@ -1490,12 +1492,14 @@ proc rawConstExpr(p: BProc, n: PNode; d: var TLoc) =
14901492
if id == p.module.labels:
14911493
# expression not found in the cache:
14921494
inc(p.module.labels)
1493-
var data = "static NIM_CONST $1 $2 = " % [getTypeDesc(p.module, t), d.snippet]
1494-
# bug #23627; when generating const object fields, it's likely that
1495-
# we need to generate type infos for the object, which may be an object with
1496-
# custom hooks. We need to generate potential consts in the hooks first.
1497-
genBracedInit(p, n, isConst = true, t, data)
1498-
data.addf(";$n", [])
1495+
var data = newBuilder("")
1496+
data.addVarWithTypeAndInitializer(kind = Const, name = d.snippet):
1497+
data.add(getTypeDesc(p.module, t))
1498+
do:
1499+
# bug #23627; when generating const object fields, it's likely that
1500+
# we need to generate type infos for the object, which may be an object with
1501+
# custom hooks. We need to generate potential consts in the hooks first.
1502+
genBracedInit(p, n, isConst = true, t, data)
14991503
p.module.s[cfsData].add data
15001504

15011505
proc handleConstExpr(p: BProc, n: PNode, d: var TLoc): bool =
@@ -2721,9 +2725,11 @@ proc genClosure(p: BProc, n: PNode, d: var TLoc) =
27212725
if isConstClosure(n):
27222726
inc(p.module.labels)
27232727
var tmp = "CNSTCLOSURE" & rope(p.module.labels)
2724-
var data = "static NIM_CONST $1 $2 = " % [getTypeDesc(p.module, n.typ), tmp]
2725-
genBracedInit(p, n, isConst = true, n.typ, data)
2726-
data.addf(";$n", [])
2728+
var data = newBuilder("")
2729+
data.addVarWithTypeAndInitializer(kind = Const, name = tmp):
2730+
data.add(getTypeDesc(p.module, n.typ))
2731+
do:
2732+
genBracedInit(p, n, isConst = true, n.typ, data)
27272733
p.module.s[cfsData].add data
27282734
putIntoDest(p, d, n, tmp, OnStatic)
27292735
else:

0 commit comments

Comments
 (0)