Skip to content

Commit 506c8a5

Browse files
authored
cbuilder: abstract over int and float generation (#24360)
Different from `intLiteral`, we add procs that just try to generate integer values, assuming they're not an edge case like `<= low(int32)` or `> high(int32)`.
1 parent 40fc2d0 commit 506c8a5

File tree

5 files changed

+75
-49
lines changed

5 files changed

+75
-49
lines changed

compiler/cbuilderbase.nim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,29 @@ type
44

55
template newBuilder(s: string): Builder =
66
s
7+
8+
proc addIntValue(builder: var Builder, val: int) =
9+
builder.addInt(val)
10+
11+
proc addIntValue(builder: var Builder, val: int64) =
12+
builder.addInt(val)
13+
14+
proc addIntValue(builder: var Builder, val: uint64) =
15+
builder.addInt(val)
16+
17+
proc addIntValue(builder: var Builder, val: Int128) =
18+
builder.addInt128(val)
19+
20+
template cIntValue(val: int): Snippet = $val
21+
template cIntValue(val: int64): Snippet = $val
22+
template cIntValue(val: uint64): Snippet = $val
23+
template cIntValue(val: Int128): Snippet = $val
24+
25+
import std/formatfloat
26+
27+
proc addFloatValue(builder: var Builder, val: float) =
28+
builder.addFloat(val)
29+
30+
proc cFloatValue(val: float): Snippet =
31+
result = ""
32+
result.addFloat(val)

compiler/cbuilderdecls.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ proc addArrayVar(builder: var Builder, kind: VarKind = Local, name: string, elem
6161
builder.add(" ")
6262
builder.add(name)
6363
builder.add("[")
64-
builder.addInt(len)
64+
builder.addIntValue(len)
6565
builder.add("]")
6666
if initializer.len != 0:
6767
builder.add(" = ")
@@ -75,7 +75,7 @@ template addArrayVarWithInitializer(builder: var Builder, kind: VarKind = Local,
7575
builder.add(" ")
7676
builder.add(name)
7777
builder.add("[")
78-
builder.addInt(len)
78+
builder.addIntValue(len)
7979
builder.add("] = ")
8080
body
8181
builder.add(";\n")
@@ -97,7 +97,7 @@ template addArrayTypedef(builder: var Builder, name: string, len: BiggestInt, ty
9797
builder.add(" ")
9898
builder.add(name)
9999
builder.add("[")
100-
builder.addInt(len)
100+
builder.addIntValue(len)
101101
builder.add("];\n")
102102

103103
type
@@ -173,7 +173,7 @@ proc addArrayField(obj: var Builder; name, elementType: Snippet; len: int; initi
173173
obj.add(" ")
174174
obj.add(name)
175175
obj.add("[")
176-
obj.addInt(len)
176+
obj.addIntValue(len)
177177
obj.add("]")
178178
if initializer.len != 0:
179179
obj.add(initializer)
@@ -184,7 +184,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo
184184
obj.add('\t')
185185
if field.alignment > 0:
186186
obj.add("NIM_ALIGN(")
187-
obj.addInt(field.alignment)
187+
obj.addIntValue(field.alignment)
188188
obj.add(") ")
189189
obj.add(typ)
190190
if sfNoalias in field.flags:
@@ -195,7 +195,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo
195195
obj.add("[SEQ_DECL_SIZE]")
196196
if field.bitsize != 0:
197197
obj.add(":")
198-
obj.addInt(field.bitsize)
198+
obj.addIntValue(field.bitsize)
199199
if initializer.len != 0:
200200
obj.add(initializer)
201201
obj.add(";\n")

compiler/ccgexprs.nim

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ proc genInOp(p: BProc, e: PNode, d: var TLoc) =
20422042
b.snippet.add(")")
20432043
else:
20442044
# handle the case of an empty set
2045-
b.snippet = rope("0")
2045+
b.snippet = cIntValue(0)
20462046
putIntoDest(p, d, e, b.snippet)
20472047
else:
20482048
assert(e[1].typ != nil)
@@ -2696,7 +2696,7 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
26962696
# small set
26972697
var ts = "NU" & $(getSize(p.config, e.typ) * 8)
26982698
p.s(cpsStmts).addAssignment(rdLoc(d)):
2699-
p.s(cpsStmts).add("0")
2699+
p.s(cpsStmts).addIntValue(0)
27002700
for it in e.sons:
27012701
if it.kind == nkRange:
27022702
idx = getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt)) # our counter
@@ -3260,8 +3260,8 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
32603260
var t = skipTypes(typ, abstractRange+{tyOwned}-{tyTypeDesc})
32613261
case t.kind
32623262
of tyBool: result.add rope"NIM_FALSE"
3263-
of tyEnum, tyChar, tyInt..tyInt64, tyUInt..tyUInt64: result.add rope"0"
3264-
of tyFloat..tyFloat128: result.add rope"0.0"
3263+
of tyEnum, tyChar, tyInt..tyInt64, tyUInt..tyUInt64: result.addIntValue(0)
3264+
of tyFloat..tyFloat128: result.addFloatValue(0.0)
32653265
of tyCstring, tyVar, tyLent, tyPointer, tyPtr, tyUntyped,
32663266
tyTyped, tyTypeDesc, tyStatic, tyRef, tyNil:
32673267
result.add rope"NIM_NIL"
@@ -3270,7 +3270,7 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
32703270
var seqInit: StructInitializer
32713271
result.addStructInitializer(seqInit, kind = siOrderedStruct):
32723272
result.addField(seqInit, name = "len"):
3273-
result.add("0")
3273+
result.addIntValue(0)
32743274
result.addField(seqInit, name = "p"):
32753275
result.add("NIM_NIL")
32763276
else:
@@ -3294,7 +3294,7 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
32943294
result.addStructInitializer(tupleInit, kind = siOrderedStruct):
32953295
if p.vccAndC and t.isEmptyTupleType:
32963296
result.addField(tupleInit, name = "dummy"):
3297-
result.add "0"
3297+
result.addIntValue(0)
32983298
for i, a in t.ikids:
32993299
result.addField(tupleInit, name = "Field" & $i):
33003300
getDefaultValue(p, a, info, result)
@@ -3311,13 +3311,13 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder)
33113311
result.addField(openArrInit, name = "Field0"):
33123312
result.add("NIM_NIL")
33133313
result.addField(openArrInit, name = "Field1"):
3314-
result.add("0")
3314+
result.addIntValue(0)
33153315
of tySet:
33163316
if mapSetType(p.config, t) == ctArray:
33173317
var setInit: StructInitializer
33183318
result.addStructInitializer(setInit, kind = siArray):
33193319
discard
3320-
else: result.add "0"
3320+
else: result.addIntValue(0)
33213321
else:
33223322
globalError(p.config, info, "cannot create null element for: " & $t.kind)
33233323

@@ -3459,7 +3459,7 @@ proc genConstTuple(p: BProc, n: PNode; isConst: bool; tup: PType; result: var Bu
34593459
result.addStructInitializer(tupleInit, kind = siOrderedStruct):
34603460
if p.vccAndC and n.len == 0:
34613461
result.addField(tupleInit, name = "dummy"):
3462-
result.add("0")
3462+
result.addIntValue(0)
34633463
for i in 0..<n.len:
34643464
var it = n[i]
34653465
if it.kind == nkExprColonExpr:
@@ -3527,7 +3527,7 @@ proc genConstSeqV2(p: BProc, n: PNode, t: PType; isConst: bool; result: var Buil
35273527
var resultInit: StructInitializer
35283528
result.addStructInitializer(resultInit, kind = siOrderedStruct):
35293529
result.addField(resultInit, name = "len"):
3530-
result.add(rope(n.len))
3530+
result.addIntValue(n.len)
35313531
result.addField(resultInit, name = "p"):
35323532
result.add cCast(typ = ptrType(getSeqPayloadType(p.module, t)), value = cAddr(payload))
35333533

@@ -3601,7 +3601,7 @@ proc genBracedInit(p: BProc, n: PNode; isConst: bool; optionalType: PType; resul
36013601
result.addField(openArrInit, name = "Field0"):
36023602
result.add(cCast(typ = ptrType(ctype), value = cAddr(payload)))
36033603
result.addField(openArrInit, name = "Field1"):
3604-
result.add(rope arrLen)
3604+
result.addIntValue(arrLen)
36053605

36063606
of tyObject:
36073607
genConstObjConstr(p, n, isConst, result)

compiler/ccgliterals.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ proc genStringLiteralDataOnlyV1(m: BModule, s: string; result: var Rope) =
4848
var seqInit: StructInitializer
4949
res.addStructInitializer(seqInit, kind = siOrderedStruct):
5050
res.addField(seqInit, name = "len"):
51-
res.add(rope(s.len))
51+
res.addIntValue(s.len)
5252
res.addField(seqInit, name = "reserved"):
5353
res.add(cCast("NI", bitOr(cCast("NU", rope(s.len)), "NIM_STRLIT_FLAG")))
5454
res.addField(strInit, name = "data"):
@@ -109,7 +109,7 @@ proc genStringLiteralV2(m: BModule; n: PNode; isConst: bool; result: var Rope) =
109109
var strInit: StructInitializer
110110
res.addStructInitializer(strInit, kind = siOrderedStruct):
111111
res.addField(strInit, name = "len"):
112-
res.add(rope(n.strVal.len))
112+
res.addIntValue(n.strVal.len)
113113
res.addField(strInit, name = "p"):
114114
res.add(cCast(ptrType("NimStrPayload"), cAddr(litName)))
115115
m.s[cfsStrData].add(res)
@@ -128,7 +128,7 @@ proc genStringLiteralV2Const(m: BModule; n: PNode; isConst: bool; result: var Ro
128128
var strInit: StructInitializer
129129
result.addStructInitializer(strInit, kind = siOrderedStruct):
130130
result.addField(strInit, name = "len"):
131-
result.add(rope(n.strVal.len))
131+
result.addIntValue(n.strVal.len)
132132
result.addField(strInit, name = "p"):
133133
result.add(cCast(ptrType("NimStrPayload"), cAddr(pureLit)))
134134

compiler/ccgtypes.nim

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ proc genProcHeader(m: BModule; prc: PSym; result: var Rope; asPtr: bool = false)
12651265

12661266
proc genTypeInfoV1(m: BModule; t: PType; info: TLineInfo): Rope
12671267
proc getNimNode(m: BModule): Rope =
1268-
result = subscript(m.typeNodesName, rope(m.typeNodes))
1268+
result = subscript(m.typeNodesName, cIntValue(m.typeNodes))
12691269
inc(m.typeNodes)
12701270

12711271
proc tiNameForHcr(m: BModule; name: Rope): Rope =
@@ -1324,11 +1324,11 @@ proc genTypeInfoAux(m: BModule; typ, origType: PType, name: Rope;
13241324
var x = typ.last
13251325
if typ.kind == tyObject: x = x.skipTypes(skipPtrs)
13261326
if typ.kind == tyPtr and x.kind == tyObject and incompleteType(x):
1327-
base = rope("0")
1327+
base = cIntValue(0)
13281328
else:
13291329
base = genTypeInfoV1(m, x, info)
13301330
else:
1331-
base = rope("0")
1331+
base = cIntValue(0)
13321332
genTypeInfoAuxBase(m, typ, origType, name, base, info)
13331333

13341334
proc discriminatorTableName(m: BModule; objtype: PType, d: PSym): Rope =
@@ -1366,20 +1366,20 @@ proc genObjectFields(m: BModule; typ, origType: PType, n: PNode, expr: Rope;
13661366
genTNimNodeArray(m, tmp, rope(n.len))
13671367
for i in 0..<n.len:
13681368
var tmp2 = getNimNode(m)
1369-
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, rope(i)):
1369+
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(i)):
13701370
m.s[cfsTypeInit3].add(cAddr(tmp2))
13711371
genObjectFields(m, typ, origType, n[i], tmp2, info)
13721372
m.s[cfsTypeInit3].addFieldAssignment(expr, "len"):
1373-
m.s[cfsTypeInit3].add(rope(n.len))
1373+
m.s[cfsTypeInit3].addIntValue(n.len)
13741374
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind"):
1375-
m.s[cfsTypeInit3].add("2")
1375+
m.s[cfsTypeInit3].addIntValue(2)
13761376
m.s[cfsTypeInit3].addFieldAssignment(expr, "sons"):
1377-
m.s[cfsTypeInit3].add(cAddr(subscript(tmp, "0")))
1377+
m.s[cfsTypeInit3].add(cAddr(subscript(tmp, cIntValue(0))))
13781378
else:
13791379
m.s[cfsTypeInit3].addFieldAssignment(expr, "len"):
1380-
m.s[cfsTypeInit3].add(rope(n.len))
1380+
m.s[cfsTypeInit3].addIntValue(n.len)
13811381
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind"):
1382-
m.s[cfsTypeInit3].add("2")
1382+
m.s[cfsTypeInit3].addIntValue(2)
13831383
of nkRecCase:
13841384
assert(n[0].kind == nkSym)
13851385
var field = n[0].sym
@@ -1410,14 +1410,14 @@ proc genObjectFields(m: BModule; typ, origType: PType, n: PNode, expr: Rope;
14101410
var x = toInt(getOrdValue(b[j][0]))
14111411
var y = toInt(getOrdValue(b[j][1]))
14121412
while x <= y:
1413-
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, rope(x)):
1413+
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(x)):
14141414
m.s[cfsTypeInit3].add(cAddr(tmp2))
14151415
inc(x)
14161416
else:
1417-
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, rope(getOrdValue(b[j]))):
1417+
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(getOrdValue(b[j]))):
14181418
m.s[cfsTypeInit3].add(cAddr(tmp2))
14191419
of nkElse:
1420-
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, rope(L)):
1420+
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(L)):
14211421
m.s[cfsTypeInit3].add(cAddr(tmp2))
14221422
else: internalError(m.config, n.info, "genObjectFields(nkRecCase)")
14231423
of nkSym:
@@ -1452,31 +1452,31 @@ proc genObjectInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo
14521452
t = t.baseClass
14531453

14541454
proc genTupleInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo) =
1455-
genTypeInfoAuxBase(m, typ, typ, name, rope("0"), info)
1455+
genTypeInfoAuxBase(m, typ, typ, name, cIntValue(0), info)
14561456
var expr = getNimNode(m)
14571457
if not typ.isEmptyTupleType:
14581458
var tmp = getTempName(m) & "_" & $typ.kidsLen
14591459
genTNimNodeArray(m, tmp, rope(typ.kidsLen))
14601460
for i, a in typ.ikids:
14611461
var tmp2 = getNimNode(m)
1462-
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, rope(i)):
1462+
m.s[cfsTypeInit3].addSubscriptAssignment(tmp, cIntValue(i)):
14631463
m.s[cfsTypeInit3].add(cAddr(tmp2))
14641464
m.s[cfsTypeInit3].addf("$1.kind = 1;$n" &
14651465
"$1.offset = offsetof($2, Field$3);$n" &
14661466
"$1.typ = $4;$n" &
14671467
"$1.name = \"Field$3\";$n",
14681468
[tmp2, getTypeDesc(m, origType, dkVar), rope(i), genTypeInfoV1(m, a, info)])
14691469
m.s[cfsTypeInit3].addFieldAssignment(expr, "len"):
1470-
m.s[cfsTypeInit3].add(rope(typ.kidsLen))
1470+
m.s[cfsTypeInit3].addIntValue(typ.kidsLen)
14711471
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind"):
1472-
m.s[cfsTypeInit3].add("2")
1472+
m.s[cfsTypeInit3].addIntValue(2)
14731473
m.s[cfsTypeInit3].addFieldAssignment(expr, "sons"):
1474-
m.s[cfsTypeInit3].add(cAddr(subscript(tmp, "0")))
1474+
m.s[cfsTypeInit3].add(cAddr(subscript(tmp, cIntValue(0))))
14751475
else:
14761476
m.s[cfsTypeInit3].addFieldAssignment(expr, "len"):
1477-
m.s[cfsTypeInit3].add(rope(typ.kidsLen))
1477+
m.s[cfsTypeInit3].addIntValue(typ.kidsLen)
14781478
m.s[cfsTypeInit3].addFieldAssignment(expr, "kind"):
1479-
m.s[cfsTypeInit3].add("2")
1479+
m.s[cfsTypeInit3].addIntValue(2)
14801480
m.s[cfsTypeInit3].addFieldAssignment(tiNameForHcr(m, name), "node"):
14811481
m.s[cfsTypeInit3].add(cAddr(expr))
14821482

@@ -1506,7 +1506,7 @@ proc genEnumInfo(m: BModule; typ: PType, name: Rope; info: TLineInfo) =
15061506
enumNames.add(makeCString(field.ast.strVal))
15071507
if field.position != i or tfEnumHasHoles in typ.flags:
15081508
specialCases.addFieldAssignment(elemNode, "offset"):
1509-
specialCases.add(rope(field.position))
1509+
specialCases.addIntValue(field.position)
15101510
hasHoles = true
15111511
var enumArray = getTempName(m)
15121512
var counter = getTempName(m)
@@ -1524,11 +1524,11 @@ proc genEnumInfo(m: BModule; typ: PType, name: Rope; info: TLineInfo) =
15241524
m.s[cfsTypeInit3].add(specialCases)
15251525
let n = getNimNode(m)
15261526
m.s[cfsTypeInit3].addFieldAssignment(n, "len"):
1527-
m.s[cfsTypeInit3].add(rope(typ.n.len))
1527+
m.s[cfsTypeInit3].addIntValue(typ.n.len)
15281528
m.s[cfsTypeInit3].addFieldAssignment(n, "kind"):
1529-
m.s[cfsTypeInit3].add("2")
1529+
m.s[cfsTypeInit3].addIntValue(0)
15301530
m.s[cfsTypeInit3].addFieldAssignment(n, "sons"):
1531-
m.s[cfsTypeInit3].add(cAddr(subscript(nodePtrs, "0")))
1531+
m.s[cfsTypeInit3].add(cAddr(subscript(nodePtrs, cIntValue(0))))
15321532
m.s[cfsTypeInit3].addFieldAssignment(tiNameForHcr(m, name), "node"):
15331533
m.s[cfsTypeInit3].add(cAddr(n))
15341534
if hasHoles:
@@ -1540,9 +1540,9 @@ proc genSetInfo(m: BModule; typ: PType, name: Rope; info: TLineInfo) =
15401540
genTypeInfoAux(m, typ, typ, name, info)
15411541
var tmp = getNimNode(m)
15421542
m.s[cfsTypeInit3].addFieldAssignment(tmp, "len"):
1543-
m.s[cfsTypeInit3].add(rope(firstOrd(m.config, typ)))
1543+
m.s[cfsTypeInit3].addIntValue(firstOrd(m.config, typ))
15441544
m.s[cfsTypeInit3].addFieldAssignment(tmp, "kind"):
1545-
m.s[cfsTypeInit3].add("0")
1545+
m.s[cfsTypeInit3].addIntValue(0)
15461546
m.s[cfsTypeInit3].addFieldAssignment(tiNameForHcr(m, name), "node"):
15471547
m.s[cfsTypeInit3].add(cAddr(tmp))
15481548

@@ -1746,7 +1746,7 @@ proc genTypeInfoV2OldImpl(m: BModule; t, origType: PType, name: Rope; info: TLin
17461746
len = objDepth + 1,
17471747
initializer = objDisplay)
17481748
typeEntry.addFieldAssignment(name, "display"):
1749-
typeEntry.add(rope(objDisplayStore))
1749+
typeEntry.add(objDisplayStore)
17501750

17511751
let dispatchMethods = toSeq(getMethodsPerType(m.g.graph, t))
17521752
if dispatchMethods.len > 0:
@@ -1940,9 +1940,9 @@ proc genTypeInfoV1(m: BModule; t: PType; info: TLineInfo): Rope =
19401940
rememberEmittedTypeInfo(m.g.graph, FileIndex(owner), $result)
19411941

19421942
case t.kind
1943-
of tyEmpty, tyVoid: result = rope"0"
1943+
of tyEmpty, tyVoid: result = cIntValue(0)
19441944
of tyPointer, tyBool, tyChar, tyCstring, tyString, tyInt..tyUInt64, tyVar, tyLent:
1945-
genTypeInfoAuxBase(m, t, t, result, rope"0", info)
1945+
genTypeInfoAuxBase(m, t, t, result, cIntValue(0), info)
19461946
of tyStatic:
19471947
if t.n != nil: result = genTypeInfoV1(m, skipModifier t, info)
19481948
else: internalError(m.config, "genTypeInfoV1(" & $t.kind & ')')
@@ -1951,7 +1951,7 @@ proc genTypeInfoV1(m: BModule; t: PType; info: TLineInfo): Rope =
19511951
return genTypeInfoV1(m, t.skipModifier, info)
19521952
of tyProc:
19531953
if t.callConv != ccClosure:
1954-
genTypeInfoAuxBase(m, t, t, result, rope"0", info)
1954+
genTypeInfoAuxBase(m, t, t, result, cIntValue(0), info)
19551955
else:
19561956
let x = fakeClosureType(m, t.owner)
19571957
genTupleInfo(m, x, x, result, info)

0 commit comments

Comments
 (0)