diff --git a/compiler/cbuilderbase.nim b/compiler/cbuilderbase.nim index d0ffbd791eb16..3cb42bbd95c39 100644 --- a/compiler/cbuilderbase.nim +++ b/compiler/cbuilderbase.nim @@ -1,4 +1,6 @@ -import ropes, int128 +import ropes, int128, std/formatfloat + +const buildNifc* = defined(nimCompileToNifc) type Snippet* = string @@ -43,119 +45,237 @@ proc addLineEndDedent*(builder: var Builder, s: string) = builder.addNewline() proc addLineComment*(builder: var Builder, comment: string) = - # probably no-op on nifc - builder.add("// ") - builder.add(comment) - builder.addNewline() + when not buildNifc: + builder.add("// ") + builder.add(comment) + builder.addNewline() proc addIntValue*(builder: var Builder, val: int) = + when buildNifc: + if val >= 0: + builder.buf.add('+') builder.buf.addInt(val) proc addIntValue*(builder: var Builder, val: int64) = + when buildNifc: + if val >= 0: + builder.buf.add('+') builder.buf.addInt(val) proc addIntValue*(builder: var Builder, val: uint64) = + when buildNifc: + builder.buf.add('+') builder.buf.addInt(val) proc addIntValue*(builder: var Builder, val: Int128) = + when buildNifc: + if val >= 0: + builder.buf.add('+') builder.buf.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 - -template cUintValue*(val: uint): Snippet = $val & "U" - -import std/formatfloat - -proc addFloatValue*(builder: var Builder, val: float) = - builder.buf.addFloat(val) - -template cFloatValue*(val: float): Snippet = $val +when not buildNifc: + template cIntValue*(val: int): Snippet = $val + template cIntValue*(val: int64): Snippet = $val + template cIntValue*(val: uint64): Snippet = $val + template cIntValue*(val: Int128): Snippet = $val + + template cUintValue*(val: uint): Snippet = $val & "U" + + proc addFloatValue*(builder: var Builder, val: float) = + builder.buf.addFloat(val) + + template cFloatValue*(val: float): Snippet = $val +else: + proc cIntValue*(val: int): Snippet = + result = if val >= 0: "+" else: "" + result.addInt(val) + proc cIntValue*(val: int64): Snippet = + result = if val >= 0: "+" else: "" + result.addInt(val) + proc cIntValue*(val: uint64): Snippet = + result = "+" + result.addInt(val) + proc cIntValue*(val: Int128): Snippet = + result = if val >= 0: "+" else: "" + result.addInt128(val) + proc cUintValue*(val: uint): Snippet = + result = "+" + result.addInt(val) + result.add('u') + + import std/math + + proc addFloatValue*(builder: var Builder, val: float) = + let kind = classify(val) + case kind + of fcNan: + builder.buf.add("(nan)") + of fcInf: + builder.buf.add("(inf)") + of fcNegInf: + builder.buf.add("(neginf)") + else: + if val >= 0: + builder.buf.add("+") + builder.buf.addFloat(val) + + proc cFloatValue*(val: float): Snippet = + let kind = classify(val) + case kind + of fcNan: + result = "(nan)" + of fcInf: + result = "(inf)" + of fcNegInf: + result = "(neginf)" + else: + result = if val >= 0: "+" else: "-" + result.addFloat(val) proc addInt64Literal*(result: var Builder; i: BiggestInt) = - if i > low(int64): - result.add "IL64($1)" % [rope(i)] + when buildNifc: + result.add("(suf ") + result.addIntValue(i) + result.add(" \"i64\")") else: - result.add "(IL64(-9223372036854775807) - IL64(1))" + if i > low(int64): + result.add "IL64($1)" % [rope(i)] + else: + result.add "(IL64(-9223372036854775807) - IL64(1))" proc addUint64Literal*(result: var Builder; i: uint64) = - result.add rope($i & "ULL") + when buildNifc: + result.add("(suf ") + result.addIntValue(i) + result.add('u') + result.add(" \"u64\")") + else: + result.add rope($i & "ULL") proc addIntLiteral*(result: var Builder; i: BiggestInt) = - if i > low(int32) and i <= high(int32): + when buildNifc: + # nifc handles this result.addIntValue(i) - elif i == low(int32): - # Nim has the same bug for the same reasons :-) - result.add "(-2147483647 -1)" - elif i > low(int64): - result.add "IL64($1)" % [rope(i)] else: - result.add "(IL64(-9223372036854775807) - IL64(1))" + if i > low(int32) and i <= high(int32): + result.addIntValue(i) + elif i == low(int32): + # Nim has the same bug for the same reasons :-) + result.add "(-2147483647 -1)" + elif i > low(int64): + result.add "IL64($1)" % [rope(i)] + else: + result.add "(IL64(-9223372036854775807) - IL64(1))" proc addIntLiteral*(result: var Builder; i: Int128) = addIntLiteral(result, toInt64(i)) proc cInt64Literal*(i: BiggestInt): Snippet = - if i > low(int64): - result = "IL64($1)" % [rope(i)] + when buildNifc: + result = "(suf " & cIntValue(i) & " \"i64\")" else: - result = "(IL64(-9223372036854775807) - IL64(1))" + if i > low(int64): + result = "IL64($1)" % [rope(i)] + else: + result = "(IL64(-9223372036854775807) - IL64(1))" proc cUint64Literal*(i: uint64): Snippet = - result = $i & "ULL" + when buildNifc: + result = "(suf " & cUintValue(i) & " \"u64\")" + else: + result = $i & "ULL" proc cIntLiteral*(i: BiggestInt): Snippet = - if i > low(int32) and i <= high(int32): - result = rope(i) - elif i == low(int32): - # Nim has the same bug for the same reasons :-) - result = "(-2147483647 -1)" - elif i > low(int64): - result = "IL64($1)" % [rope(i)] + when buildNifc: + result = cIntValue(i) else: - result = "(IL64(-9223372036854775807) - IL64(1))" + if i > low(int32) and i <= high(int32): + result = rope(i) + elif i == low(int32): + # Nim has the same bug for the same reasons :-) + result = "(-2147483647 -1)" + elif i > low(int64): + result = "IL64($1)" % [rope(i)] + else: + result = "(IL64(-9223372036854775807) - IL64(1))" proc cIntLiteral*(i: Int128): Snippet = result = cIntLiteral(toInt64(i)) -const - NimInt* = "NI" - NimInt8* = "NI8" - NimInt16* = "NI16" - NimInt32* = "NI32" - NimInt64* = "NI64" - CInt* = "int" - NimUint* = "NU" - NimUint8* = "NU8" - NimUint16* = "NU16" - NimUint32* = "NU32" - NimUint64* = "NU64" - NimFloat* = "NF" - NimFloat32* = "NF32" - NimFloat64* = "NF64" - NimFloat128* = "NF128" # not actually defined - NimNan* = "NAN" - NimInf* = "INF" - NimBool* = "NIM_BOOL" - NimTrue* = "NIM_TRUE" - NimFalse* = "NIM_FALSE" - NimChar* = "NIM_CHAR" - CChar* = "char" - NimCstring* = "NCSTRING" - NimNil* = "NIM_NIL" - CNil* = "NULL" - NimStrlitFlag* = "NIM_STRLIT_FLAG" - CVoid* = "void" - CPointer* = "void*" - CConstPointer* = "NIM_CONST void*" +when buildNifc: + const + NimInt* = "(i -1)" + NimInt8* = "(i +8)" + NimInt16* = "(i +16)" + NimInt32* = "(i +32)" + NimInt64* = "(i +64)" + CInt* = "(i +32)" # int.c + NimUint* = "(u -1)" + NimUint8* = "(u +8)" + NimUint16* = "(u +16)" + NimUint32* = "(u +32)" + NimUint64* = "(u +64)" + NimFloat* = "(f +64)" + NimFloat32* = "(f +32)" + NimFloat64* = "(f +64)" + NimFloat128* = "(f +128)" + NimNan* = "(nan)" + NimInf* = "(inf)" + NimBool* = "(bool)" + NimTrue* = "(true)" + NimFalse* = "(false)" + NimChar* = "(c +8)" + CChar* = "(c +8)" # char.c + NimCstring* = "(ptr (c +8))" + NimNil* = "(nil)" + CNil* = "(nil)" # NULL.c + NimStrlitFlag* = "NIM_STRLIT_FLAG" # XXX + CVoid* = "(void)" + CPointer* = "(ptr (void))" + CConstPointer* = "(ptr (void))" # (void (ro)) illegal +else: + const + NimInt* = "NI" + NimInt8* = "NI8" + NimInt16* = "NI16" + NimInt32* = "NI32" + NimInt64* = "NI64" + CInt* = "int" + NimUint* = "NU" + NimUint8* = "NU8" + NimUint16* = "NU16" + NimUint32* = "NU32" + NimUint64* = "NU64" + NimFloat* = "NF" + NimFloat32* = "NF32" + NimFloat64* = "NF64" + NimFloat128* = "NF128" # not actually defined + NimNan* = "NAN" + NimInf* = "INF" + NimBool* = "NIM_BOOL" + NimTrue* = "NIM_TRUE" + NimFalse* = "NIM_FALSE" + NimChar* = "NIM_CHAR" + CChar* = "char" + NimCstring* = "NCSTRING" + NimNil* = "NIM_NIL" + CNil* = "NULL" + NimStrlitFlag* = "NIM_STRLIT_FLAG" + CVoid* = "void" + CPointer* = "void*" + CConstPointer* = "NIM_CONST void*" proc cIntType*(bits: BiggestInt): Snippet = - "NI" & $bits + when buildNifc: + "(i +" & $bits & ")" + else: + "NI" & $bits proc cUintType*(bits: BiggestInt): Snippet = - "NU" & $bits + when buildNifc: + "(u +" & $bits & ")" + else: + "NU" & $bits type IfBuilderState* = enum diff --git a/compiler/cbuilderdecls.nim b/compiler/cbuilderdecls.nim index 5919ae8a910a9..8e1ffcd57f784 100644 --- a/compiler/cbuilderdecls.nim +++ b/compiler/cbuilderdecls.nim @@ -13,115 +13,240 @@ proc addVarHeader(builder: var Builder, kind: VarKind) = ## AlwaysConst has `static const` modifier (NIM_CONST is no-op on C++) ## Threadvar is unimplemented case kind - of Local: discard + of Local: + when buildNifc: + builder.add("var ") of Global: - builder.add("static ") + when buildNifc: + builder.add("gvar ") + else: + builder.add("static ") of Const: - builder.add("static NIM_CONST ") + when buildNifc: + builder.add("const ") + else: + builder.add("static NIM_CONST ") of AlwaysConst: - builder.add("static const ") + when buildNifc: + builder.add("const ") + else: + builder.add("static const ") of Threadvar: doAssert false, "unimplemented" proc addVar(builder: var Builder, kind: VarKind = Local, name: string, typ: Snippet, initializer: Snippet = "") = ## adds a variable declaration to the builder - builder.addVarHeader(kind) - builder.add(typ) - builder.add(" ") - builder.add(name) - if initializer.len != 0: - builder.add(" = ") - builder.add(initializer) - builder.addLineEnd(";") + when buildNifc: + builder.add("(") + builder.addVarHeader(kind) + builder.add(":") + builder.add(name) + builder.add(" . ") # pragmas + builder.add(typ) + builder.add(" ") + if initializer.len != 0: + builder.add(initializer) + else: + builder.add(".") + builder.addLineEnd(")") + else: + builder.addVarHeader(kind) + builder.add(typ) + builder.add(" ") + builder.add(name) + if initializer.len != 0: + builder.add(" = ") + builder.add(initializer) + builder.addLineEnd(";") template addVarWithType(builder: var Builder, kind: VarKind = Local, name: string, body: typed) = ## adds a variable declaration to the builder, with the `body` building the type - builder.addVarHeader(kind) - body - builder.add(" ") - builder.add(name) - builder.addLineEnd(";") + when buildNifc: + builder.add("(") + builder.addVarHeader(kind) + builder.add(":") + builder.add(name) + builder.add(" . ") # pragmas + body + builder.add(" .") # initializer + builder.addLineEnd(")") + else: + builder.addVarHeader(kind) + body + builder.add(" ") + builder.add(name) + builder.addLineEnd(";") template addVarWithInitializer(builder: var Builder, kind: VarKind = Local, name: string, typ: Snippet, initializerBody: typed) = ## adds a variable declaration to the builder, with ## `initializerBody` building the initializer. initializer must be provided - builder.addVarHeader(kind) - builder.add(typ) - builder.add(" ") - builder.add(name) - builder.add(" = ") - initializerBody - builder.addLineEnd(";") + when buildNifc: + builder.add("(") + builder.addVarHeader(kind) + builder.add(":") + builder.add(name) + builder.add(" . ") # pragmas + builder.add(typ) + builder.add(" ") + initializerBody + builder.addLineEnd(")") + else: + builder.addVarHeader(kind) + builder.add(typ) + builder.add(" ") + builder.add(name) + builder.add(" = ") + initializerBody + builder.addLineEnd(";") template addVarWithTypeAndInitializer(builder: var Builder, kind: VarKind = Local, name: string, typeBody, initializerBody: typed) = ## adds a variable declaration to the builder, with `typeBody` building the type, and ## `initializerBody` building the initializer. initializer must be provided - builder.addVarHeader(kind) - typeBody - builder.add(" ") - builder.add(name) - builder.add(" = ") - initializerBody - builder.addLineEnd(";") + when buildNifc: + builder.add("(") + builder.addVarHeader(kind) + builder.add(":") + builder.add(name) + builder.add(" . ") # pragmas + typeBody + builder.add(" ") + initializerBody + builder.addLineEnd(")") + else: + builder.addVarHeader(kind) + typeBody + builder.add(" ") + builder.add(name) + builder.add(" = ") + initializerBody + builder.addLineEnd(";") -proc addArrayVar(builder: var Builder, kind: VarKind = Local, name: string, elementType: Snippet, len: int, initializer: Snippet = "") = +when buildNifc: + proc getArrayType(m: BModule, elementType: Snippet, len: int): Snippet = + let key = (elementType, len) + if key in m.arrayTypes: + result = m.arrayTypes[key] + else: + result = elementType & "_Arr_" & $len + m.s[cfsTypes].add("(type :") + m.s[cfsTypes].add(result) + m.s[cfsTypes].add(" (array ") + m.s[cfsTypes].add(elementType) + m.s[cfsTypes].add(" ") + m.s[cfsTypes].add(cIntValue(len)) + m.s[cfsTypes].addLineEnd("))") + m.arrayTypes[key] = result + +proc addArrayVar(builder: var Builder, m: BModule, kind: VarKind = Local, name: string, elementType: Snippet, len: int, initializer: Snippet = "") = ## adds an array variable declaration to the builder - builder.addVarHeader(kind) - builder.add(elementType) - builder.add(" ") - builder.add(name) - builder.add("[") - builder.addIntValue(len) - builder.add("]") - if initializer.len != 0: - builder.add(" = ") - builder.add(initializer) - builder.addLineEnd(";") + when buildNifc: + builder.add("(") + builder.addVarHeader(kind) + builder.add(":") + builder.add(name) + builder.add(" . ") # pragmas + builder.add(getArrayType(m, elementType, len)) + builder.add(" ") + if initializer.len != 0: + builder.add(initializer) + else: + builder.add(".") + builder.addLineEnd(")") + else: + builder.addVarHeader(kind) + builder.add(elementType) + builder.add(" ") + builder.add(name) + builder.add("[") + builder.addIntValue(len) + builder.add("]") + if initializer.len != 0: + builder.add(" = ") + builder.add(initializer) + builder.addLineEnd(";") -template addArrayVarWithInitializer(builder: var Builder, kind: VarKind = Local, name: string, elementType: Snippet, len: int, body: typed) = +template addArrayVarWithInitializer(builder: var Builder, m: BModule, kind: VarKind = Local, name: string, elementType: Snippet, len: int, body: typed) = ## adds an array variable declaration to the builder with the initializer built according to `body` - builder.addVarHeader(kind) - builder.add(elementType) - builder.add(" ") - builder.add(name) - builder.add("[") - builder.addIntValue(len) - builder.add("] = ") - body - builder.addLineEnd(";") + when buildNifc: + builder.add("(") + builder.addVarHeader(kind) + builder.add(":") + builder.add(name) + builder.add(" . ") # pragmas + builder.add(getArrayType(m, elementType, len)) + builder.add(" ") + body + builder.addLineEnd(")") + else: + builder.addVarHeader(kind) + builder.add(elementType) + builder.add(" ") + builder.add(name) + builder.add("[") + builder.addIntValue(len) + builder.add("] = ") + body + builder.addLineEnd(";") template addTypedef(builder: var Builder, name: string, typeBody: typed) = ## adds a typedef declaration to the builder with name `name` and type as ## built in `typeBody` - builder.add("typedef ") - typeBody - builder.add(" ") - builder.add(name) - builder.addLineEnd(";") + when buildNifc: + builder.add("(type :") + builder.add(name) + builder.add(" ") + typeBody + builder.addLineEnd(")") + else: + builder.add("typedef ") + typeBody + builder.add(" ") + builder.add(name) + builder.addLineEnd(";") proc addProcTypedef(builder: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet) = - builder.add("typedef ") - builder.add(CallingConvToStr[callConv]) - builder.add("_PTR(") - builder.add(rettype) - builder.add(", ") - builder.add(name) - builder.add(")") - builder.add(params) - builder.addLineEnd(";") + when buildNifc: + builder.add("(type :") + builder.add(name) + builder.add(" (proctype . ") + builder.add(params) + builder.add(" ") + builder.add(rettype) + builder.add(" (pragmas ") + builder.add(CallingConvToStr[callConv]) + builder.addLineEnd(")))") + else: + builder.add("typedef ") + builder.add(CallingConvToStr[callConv]) + builder.add("_PTR(") + builder.add(rettype) + builder.add(", ") + builder.add(name) + builder.add(")") + builder.add(params) + builder.addLineEnd(";") template addArrayTypedef(builder: var Builder, name: string, len: BiggestInt, typeBody: typed) = ## adds an array typedef declaration to the builder with name `name`, ## length `len`, and element type as built in `typeBody` - builder.add("typedef ") - typeBody - builder.add(" ") - builder.add(name) - builder.add("[") - builder.addIntValue(len) - builder.addLineEnd("];") + when buildNifc: + builder.add("(type :") + builder.add(name) + builder.add(" (array ") + typeBody + builder.add(" ") + builder.addIntValue(len) + builder.addLineEnd("))") + else: + builder.add("typedef ") + typeBody + builder.add(" ") + builder.add(name) + builder.add("[") + builder.addIntValue(len) + builder.addLineEnd("];") type StructInitializerKind = enum @@ -139,14 +264,23 @@ type proc initStructInitializer(builder: var Builder, kind: StructInitializerKind): StructInitializer = ## starts building a struct initializer, i.e. braced initializer list result = StructInitializer(kind: kind, needsComma: false) - if kind != siWrapper: - builder.add("{") + when buildNifc: + case kind + of siOrderedStruct, siNamedStruct: + builder.add("(oconstr ") + of siArray: + builder.add("(aconstr ") + of siWrapper: discard + else: + if kind != siWrapper: + builder.add("{") template addField(builder: var Builder, constr: var StructInitializer, name: string, valueBody: typed) = ## adds a field to a struct initializer, with the value built in `valueBody` if constr.needsComma: assert constr.kind != siWrapper, "wrapper constructor cannot have multiple fields" - builder.add(", ") + when not buildNifc: + builder.add(", ") else: constr.needsComma = true case constr.kind @@ -156,18 +290,34 @@ template addField(builder: var Builder, constr: var StructInitializer, name: str of siOrderedStruct: # no name, can just add value on C assert name.len != 0, "name has to be given for struct initializer field" + when buildNifc: + builder.add("(kv ") + builder.add(name) + builder.add(" ") valueBody + when buildNifc: + builder.add(")") of siNamedStruct: assert name.len != 0, "name has to be given for struct initializer field" - builder.add(".") - builder.add(name) - builder.add(" = ") + when buildNifc: + builder.add("(kv ") + builder.add(name) + builder.add(" ") + else: + builder.add(".") + builder.add(name) + builder.add(" = ") valueBody + when buildNifc: + builder.add(")") proc finishStructInitializer(builder: var Builder, constr: StructInitializer) = ## finishes building a struct initializer if constr.kind != siWrapper: - builder.add("}") + when buildNifc: + builder.add(")") + else: + builder.add("}") template addStructInitializer(builder: var Builder, constr: out StructInitializer, kind: StructInitializerKind, body: typed) = ## builds a struct initializer, i.e. `{ field1, field2 }` @@ -177,32 +327,46 @@ template addStructInitializer(builder: var Builder, constr: out StructInitialize body builder.finishStructInitializer(constr) -proc addField(obj: var Builder; name, typ: Snippet; isFlexArray: bool = false; initializer: Snippet = "") = +proc addField(obj: var Builder; name, typ: Snippet; isFlexArray: bool = false) = ## adds a field inside a struct/union type - obj.add('\t') - obj.add(typ) - obj.add(" ") - obj.add(name) - if isFlexArray: - obj.add("[SEQ_DECL_SIZE]") - if initializer.len != 0: - obj.add(initializer) - obj.add(";\n") + when buildNifc: + obj.add("\n\t(fld :") + obj.add(name) + obj.add(" . ") # pragmas + if isFlexArray: + obj.add("(flexarray ") + obj.add(typ) + obj.add(")") + else: + obj.add(typ) + obj.add(")") + else: + obj.add('\t') + obj.add(typ) + obj.add(" ") + obj.add(name) + if isFlexArray: + obj.add("[SEQ_DECL_SIZE]") + obj.add(";\n") -proc addArrayField(obj: var Builder; name, elementType: Snippet; len: int; initializer: Snippet = "") = +proc addArrayField(obj: var Builder; m: BModule, name, elementType: Snippet; len: int) = ## adds an array field inside a struct/union type - obj.add('\t') - obj.add(elementType) - obj.add(" ") - obj.add(name) - obj.add("[") - obj.addIntValue(len) - obj.add("]") - if initializer.len != 0: - obj.add(initializer) - obj.add(";\n") + when buildNifc: + obj.add("\n\t(fld :") + obj.add(name) + obj.add(" . ") # pragmas + obj.add(getArrayType(elementType, len)) + obj.add(")") + else: + obj.add('\t') + obj.add(elementType) + obj.add(" ") + obj.add(name) + obj.add("[") + obj.addIntValue(len) + obj.add("];\n") -proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bool = false; initializer: Snippet = "") = +proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bool = false; cppInitializer: Snippet = "") = ## adds an field inside a struct/union type, based on an `skField` symbol obj.add('\t') if field.alignment > 0: @@ -219,8 +383,8 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo if field.bitsize != 0: obj.add(":") obj.addIntValue(field.bitsize) - if initializer.len != 0: - obj.add(initializer) + if cppInitializer.len != 0: + obj.add(cppInitializer) obj.add(";\n") proc addProcField(obj: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet) = diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 5600f6842dd5d..85462e4c47bf1 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1982,7 +1982,8 @@ proc genOfHelper(p: BProc; dest: PType; a: Rope; info: TLineInfo; result: var Bu cgsym(p.module, "TNimType") inc p.module.labels let cache = "Nim_OfCheck_CACHE" & p.module.labels.rope - p.module.s[cfsVars].addArrayVar(kind = Global, + p.module.s[cfsVars].addArrayVar(p.module, + kind = Global, name = cache, elementType = ptrType("TNimType"), len = 2) @@ -3892,7 +3893,7 @@ proc genConstSeq(p: BProc, n: PNode, t: PType; isConst: bool; result: var Builde name = tmpName): def.addSimpleStruct(p.module, name = "", baseType = ""): def.addField(name = "sup", typ = cgsymValue(p.module, "TGenericSeq")) - def.addArrayField(name = "data", elementType = getTypeDesc(p.module, base), len = n.len) + def.addArrayField(p.module, name = "data", elementType = getTypeDesc(p.module, base), len = n.len) do: var structInit: StructInitializer def.addStructInitializer(structInit, kind = siOrderedStruct): @@ -3925,7 +3926,7 @@ proc genConstSeqV2(p: BProc, n: PNode, t: PType; isConst: bool; result: var Buil name = payload): def.addSimpleStruct(p.module, name = "", baseType = ""): def.addField(name = "cap", typ = NimInt) - def.addArrayField(name = "data", elementType = getTypeDesc(p.module, base), len = n.len) + def.addArrayField(p.module, name = "data", elementType = getTypeDesc(p.module, base), len = n.len) do: var structInit: StructInitializer def.addStructInitializer(structInit, kind = siOrderedStruct): @@ -4008,7 +4009,7 @@ proc genBracedInit(p: BProc, n: PNode; isConst: bool; optionalType: PType; resul let arrLen = n.len # genConstSimpleList can modify cfsStrData, we need an intermediate builder: var data = newBuilder("") - data.addArrayVarWithInitializer( + data.addArrayVarWithInitializer(p.module, kind = if isConst: AlwaysConst else: Global, name = payload, elementType = ctype, len = arrLen): genConstSimpleList(p, n, isConst, data) diff --git a/compiler/ccgliterals.nim b/compiler/ccgliterals.nim index 84f72017cf7e9..3ea327679e2a1 100644 --- a/compiler/ccgliterals.nim +++ b/compiler/ccgliterals.nim @@ -40,7 +40,7 @@ proc genStringLiteralDataOnlyV1(m: BModule, s: string; result: var Rope) = res.addVarWithTypeAndInitializer(AlwaysConst, name = tmp): res.addSimpleStruct(m, name = "", baseType = ""): res.addField(name = "Sup", typ = "TGenericSeq") - res.addArrayField(name = "data", elementType = NimChar, len = s.len + 1) + res.addArrayField(m, name = "data", elementType = NimChar, len = s.len + 1) do: var strInit: StructInitializer res.addStructInitializer(strInit, kind = siOrderedStruct): @@ -77,7 +77,7 @@ proc genStringLiteralDataOnlyV2(m: BModule, s: string; result: Rope; isConst: bo name = result): res.addSimpleStruct(m, name = "", baseType = ""): res.addField(name = "cap", typ = NimInt) - res.addArrayField(name = "data", elementType = NimChar, len = s.len + 1) + res.addArrayField(m, name = "data", elementType = NimChar, len = s.len + 1) do: var structInit: StructInitializer res.addStructInitializer(structInit, kind = siOrderedStruct): diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index ed840a97a8faa..5a4de33b164e1 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -621,7 +621,8 @@ proc genComputedGoto(p: BProc; n: PNode) = var id = p.labels+1 inc p.labels, arraySize+1 let tmp = "TMP$1_" % [id.rope] - p.s(cpsStmts).addArrayVarWithInitializer(kind = Global, + p.s(cpsStmts).addArrayVarWithInitializer(p.module, + kind = Global, name = tmp, elementType = CPointer, len = arraySize): diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index b3e03f57490f5..1e750cb7e9af8 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -1332,7 +1332,8 @@ proc rope(arg: Int128): Rope = rope($arg) proc discriminatorTableDecl(m: BModule; objtype: PType, d: PSym, result: var Builder) = cgsym(m, "TNimNode") var tmp = discriminatorTableName(m, objtype, d) - result.addArrayVar(kind = Local, + result.addArrayVar(m, + kind = Local, name = tmp, elementType = ptrType("TNimNode"), len = toInt(lengthOrd(m.config, d.typ)) + 1) @@ -1355,7 +1356,8 @@ proc genTNimNodeArray(m: BModule; name: Rope, size: int) = m.hcrCreateTypeInfosProc.addCast(typ = ptrType(CPointer)): m.hcrCreateTypeInfosProc.add(cAddr(name)) else: - m.s[cfsTypeInit1].addArrayVar(kind = Global, name = name, + m.s[cfsTypeInit1].addArrayVar(m, + kind = Global, name = name, elementType = ptrType("TNimNode"), len = size) proc genObjectFields(m: BModule; typ, origType: PType, n: PNode, expr: Rope; @@ -1395,7 +1397,8 @@ proc genObjectFields(m: BModule; typ, origType: PType, n: PNode, expr: Rope; m.s[cfsTypeInit3].addFieldAssignment(expr, "name", makeCString(field.name.s)) m.s[cfsTypeInit3].addFieldAssignment(expr, "sons", cAddr(subscript(tmp, cIntValue(0)))) m.s[cfsTypeInit3].addFieldAssignment(expr, "len", L) - m.s[cfsData].addArrayVar(kind = Local, name = tmp, + m.s[cfsData].addArrayVar(m, + kind = Local, name = tmp, elementType = ptrType("TNimNode"), len = toInt(L)+1) for i in 1..= 0: let objDisplayStore = getTempName(m) - m.s[cfsVars].addArrayVarWithInitializer(kind = Global, + m.s[cfsVars].addArrayVarWithInitializer(m, + kind = Global, name = objDisplayStore, elementType = getTypeDesc(m, getSysType(m.g.graph, unknownLineInfo, tyUInt32), dkVar), len = objDepth + 1): @@ -1765,7 +1769,8 @@ proc genTypeInfoV2OldImpl(m: BModule; t, origType: PType, name: Rope; info: TLin let dispatchMethods = toSeq(getMethodsPerType(m.g.graph, t)) if dispatchMethods.len > 0: let vTablePointerName = getTempName(m) - m.s[cfsVars].addArrayVarWithInitializer(kind = Global, + m.s[cfsVars].addArrayVarWithInitializer(m, + kind = Global, name = vTablePointerName, elementType = CPointer, len = dispatchMethods.len): @@ -1813,7 +1818,8 @@ proc genTypeInfoV2Impl(m: BModule; t, origType: PType, name: Rope; info: TLineIn if objDepth >= 0: let objDisplayStore = getTempName(m) - m.s[cfsVars].addArrayVarWithInitializer(kind = Const, + m.s[cfsVars].addArrayVarWithInitializer(m, + kind = Const, name = objDisplayStore, elementType = getTypeDesc(m, getSysType(m.g.graph, unknownLineInfo, tyUInt32), dkVar), len = objDepth + 1): diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 2d4e70420b9e2..30dcff953b620 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1918,7 +1918,8 @@ proc registerModuleToMain(g: BModuleList; m: BModule) = let systemModulePath = getModuleDllPath(m, g.modules[g.graph.config.m.systemFileIdx.int].module) let mainModulePath = getModuleDllPath(m, m.module) hcrModuleMeta.addDeclWithVisibility(Private): - hcrModuleMeta.addArrayVarWithInitializer(kind = Local, + hcrModuleMeta.addArrayVarWithInitializer(m, + kind = Local, name = "hcr_module_list", elementType = ptrConstType(CChar), len = g.graph.importDeps.getOrDefault(FileIndex(m.module.position)).len + @@ -2097,10 +2098,12 @@ proc genInitCode(m: BModule) = CNil, cCast(ptrType(CPointer), cAddr(m.typeNodesName))) else: - m.s[cfsTypeInit1].addArrayVar(Global, name = m.typeNodesName, + m.s[cfsTypeInit1].addArrayVar(m, + Global, name = m.typeNodesName, elementType = cgsymValue(m, "TNimNode"), len = m.typeNodes) if m.nimTypes > 0: - m.s[cfsTypeInit1].addArrayVar(Global, name = m.nimTypesName, + m.s[cfsTypeInit1].addArrayVar(m, + Global, name = m.nimTypesName, elementType = cgsymValue(m, "TNimType"), len = m.nimTypes) if m.hcrOn: diff --git a/compiler/cgendata.nim b/compiler/cgendata.nim index f9ed9c6fdae98..67523e7d2df7e 100644 --- a/compiler/cgendata.nim +++ b/compiler/cgendata.nim @@ -173,6 +173,8 @@ type # OpenGL wrapper sigConflicts*: CountTable[SigHash] g*: BModuleList + when buildNifc: + arrayTypes*: Table[(Snippet, int), Snippet] template config*(m: BModule): ConfigRef = m.g.config template config*(p: BProc): ConfigRef = p.module.g.config