diff --git a/compiler/cbuilderbase.nim b/compiler/cbuilderbase.nim index a93d2b5f41..dcefdf42df 100644 --- a/compiler/cbuilderbase.nim +++ b/compiler/cbuilderbase.nim @@ -4,3 +4,29 @@ type template newBuilder(s: string): Builder = s + +proc addIntValue(builder: var Builder, val: int) = + builder.addInt(val) + +proc addIntValue(builder: var Builder, val: int64) = + builder.addInt(val) + +proc addIntValue(builder: var Builder, val: uint64) = + builder.addInt(val) + +proc addIntValue(builder: var Builder, val: Int128) = + builder.addInt128(val) + +template cIntValue(val: int): Snippet = $val +template cIntValue(val: int64): Snippet = $val +template cIntValue(val: uint64): Snippet = $val +template cIntValue(val: Int128): Snippet = $val + +import std/formatfloat + +proc addFloatValue(builder: var Builder, val: float) = + builder.addFloat(val) + +proc cFloatValue(val: float): Snippet = + result = "" + result.addFloat(val) diff --git a/compiler/cbuilderdecls.nim b/compiler/cbuilderdecls.nim index 62f753a7ef..959beae51d 100644 --- a/compiler/cbuilderdecls.nim +++ b/compiler/cbuilderdecls.nim @@ -61,7 +61,7 @@ proc addArrayVar(builder: var Builder, kind: VarKind = Local, name: string, elem builder.add(" ") builder.add(name) builder.add("[") - builder.addInt(len) + builder.addIntValue(len) builder.add("]") if initializer.len != 0: builder.add(" = ") @@ -75,7 +75,7 @@ template addArrayVarWithInitializer(builder: var Builder, kind: VarKind = Local, builder.add(" ") builder.add(name) builder.add("[") - builder.addInt(len) + builder.addIntValue(len) builder.add("] = ") body builder.add(";\n") @@ -97,7 +97,7 @@ template addArrayTypedef(builder: var Builder, name: string, len: BiggestInt, ty builder.add(" ") builder.add(name) builder.add("[") - builder.addInt(len) + builder.addIntValue(len) builder.add("];\n") type @@ -173,7 +173,7 @@ proc addArrayField(obj: var Builder; name, elementType: Snippet; len: int; initi obj.add(" ") obj.add(name) obj.add("[") - obj.addInt(len) + obj.addIntValue(len) obj.add("]") if initializer.len != 0: obj.add(initializer) @@ -184,7 +184,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo obj.add('\t') if field.alignment > 0: obj.add("NIM_ALIGN(") - obj.addInt(field.alignment) + obj.addIntValue(field.alignment) obj.add(") ") obj.add(typ) if sfNoalias in field.flags: @@ -195,7 +195,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo obj.add("[SEQ_DECL_SIZE]") if field.bitsize != 0: obj.add(":") - obj.addInt(field.bitsize) + obj.addIntValue(field.bitsize) if initializer.len != 0: obj.add(initializer) obj.add(";\n") diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 36fb8ae195..7ffb799a61 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -2042,7 +2042,7 @@ proc genInOp(p: BProc, e: PNode, d: var TLoc) = b.snippet.add(")") else: # handle the case of an empty set - b.snippet = rope("0") + b.snippet = cIntValue(0) putIntoDest(p, d, e, b.snippet) else: assert(e[1].typ != nil) @@ -2696,7 +2696,7 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) = # small set var ts = "NU" & $(getSize(p.config, e.typ) * 8) p.s(cpsStmts).addAssignment(rdLoc(d)): - p.s(cpsStmts).add("0") + p.s(cpsStmts).addIntValue(0) for it in e.sons: if it.kind == nkRange: 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) var t = skipTypes(typ, abstractRange+{tyOwned}-{tyTypeDesc}) case t.kind of tyBool: result.add rope"NIM_FALSE" - of tyEnum, tyChar, tyInt..tyInt64, tyUInt..tyUInt64: result.add rope"0" - of tyFloat..tyFloat128: result.add rope"0.0" + of tyEnum, tyChar, tyInt..tyInt64, tyUInt..tyUInt64: result.addIntValue(0) + of tyFloat..tyFloat128: result.addFloatValue(0.0) of tyCstring, tyVar, tyLent, tyPointer, tyPtr, tyUntyped, tyTyped, tyTypeDesc, tyStatic, tyRef, tyNil: result.add rope"NIM_NIL" @@ -3270,7 +3270,7 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder) var seqInit: StructInitializer result.addStructInitializer(seqInit, kind = siOrderedStruct): result.addField(seqInit, name = "len"): - result.add("0") + result.addIntValue(0) result.addField(seqInit, name = "p"): result.add("NIM_NIL") else: @@ -3294,7 +3294,7 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder) result.addStructInitializer(tupleInit, kind = siOrderedStruct): if p.vccAndC and t.isEmptyTupleType: result.addField(tupleInit, name = "dummy"): - result.add "0" + result.addIntValue(0) for i, a in t.ikids: result.addField(tupleInit, name = "Field" & $i): getDefaultValue(p, a, info, result) @@ -3311,13 +3311,13 @@ proc getDefaultValue(p: BProc; typ: PType; info: TLineInfo; result: var Builder) result.addField(openArrInit, name = "Field0"): result.add("NIM_NIL") result.addField(openArrInit, name = "Field1"): - result.add("0") + result.addIntValue(0) of tySet: if mapSetType(p.config, t) == ctArray: var setInit: StructInitializer result.addStructInitializer(setInit, kind = siArray): discard - else: result.add "0" + else: result.addIntValue(0) else: globalError(p.config, info, "cannot create null element for: " & $t.kind) @@ -3459,7 +3459,7 @@ proc genConstTuple(p: BProc, n: PNode; isConst: bool; tup: PType; result: var Bu result.addStructInitializer(tupleInit, kind = siOrderedStruct): if p.vccAndC and n.len == 0: result.addField(tupleInit, name = "dummy"): - result.add("0") + result.addIntValue(0) for i in 0.. 0: @@ -1940,9 +1940,9 @@ proc genTypeInfoV1(m: BModule; t: PType; info: TLineInfo): Rope = rememberEmittedTypeInfo(m.g.graph, FileIndex(owner), $result) case t.kind - of tyEmpty, tyVoid: result = rope"0" + of tyEmpty, tyVoid: result = cIntValue(0) of tyPointer, tyBool, tyChar, tyCstring, tyString, tyInt..tyUInt64, tyVar, tyLent: - genTypeInfoAuxBase(m, t, t, result, rope"0", info) + genTypeInfoAuxBase(m, t, t, result, cIntValue(0), info) of tyStatic: if t.n != nil: result = genTypeInfoV1(m, skipModifier t, info) else: internalError(m.config, "genTypeInfoV1(" & $t.kind & ')') @@ -1951,7 +1951,7 @@ proc genTypeInfoV1(m: BModule; t: PType; info: TLineInfo): Rope = return genTypeInfoV1(m, t.skipModifier, info) of tyProc: if t.callConv != ccClosure: - genTypeInfoAuxBase(m, t, t, result, rope"0", info) + genTypeInfoAuxBase(m, t, t, result, cIntValue(0), info) else: let x = fakeClosureType(m, t.owner) genTupleInfo(m, x, x, result, info)