Skip to content

Commit

Permalink
mangling changes
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 29, 2024
1 parent 58407e5 commit fafa3cb
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 88 deletions.
6 changes: 6 additions & 0 deletions compiler/cbuilderbase.nim
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ when buildNifc:
raiseAssert "invalid char in escaped c symbol " & s[i]
inc i

proc rawFieldName*(s: string): Snippet =
result = s
if '.' notin s:
result.add ".c"
else:
template cSymbol*(x: string): Snippet = x
template unescapeCSymbol*(x: Snippet): string = x

template rawFieldName*(s: string): Snippet = s
25 changes: 15 additions & 10 deletions compiler/cbuilderdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ when buildNifc:
result = m.arrayTypes[key]
else:
let tmp = getTempName(m)
result = tmp & "_Arr_" & $len
result = tmp & (when buildNifc: ".arr." else: "_Arr_") & $len
m.s[cfsTypes].add("(type :")
m.s[cfsTypes].add(result)
m.s[cfsTypes].add(" . (array ")
Expand Down Expand Up @@ -226,15 +226,17 @@ type
## if true, fields will not be named, instead values are placed in order
needsComma: bool

proc initStructInitializer(builder: var Builder, kind: StructInitializerKind): StructInitializer =
proc initStructInitializer(builder: var Builder, kind: StructInitializerKind#[, typ: Snippet]#): StructInitializer =
## starts building a struct initializer, i.e. braced initializer list
result = StructInitializer(kind: kind, needsComma: false)
when buildNifc:
case kind
of siOrderedStruct, siNamedStruct:
builder.add("(oconstr ")
#builder.add(typ)
of siArray:
builder.add("(aconstr ")
#builder.add(typ)
of siWrapper: discard
else:
if kind != siWrapper:
Expand All @@ -251,22 +253,25 @@ template addField(builder: var Builder, constr: var StructInitializer, name: str
case constr.kind
of siArray, siWrapper:
# no name, can just add value
when buildNifc:
if constr.kind == siArray:
builder.add(" ")
valueBody
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(" (kv ")
builder.add(rawFieldName(name))
builder.add(" ")
valueBody
when buildNifc:
builder.add(")")
of siNamedStruct:
assert name.len != 0, "name has to be given for struct initializer field"
when buildNifc:
builder.add("(kv ")
builder.add(name)
builder.add(" (kv ")
builder.add(rawFieldName(name))
builder.add(" ")
else:
builder.add(".")
Expand Down Expand Up @@ -296,7 +301,7 @@ proc addField(obj: var Builder; name, typ: Snippet; isFlexArray: bool = false) =
## adds a field inside a struct/union type
when buildNifc:
obj.add("\n\t(fld :")
obj.add(name)
obj.add(rawFieldName(name))
obj.add(" . ") # pragmas
if isFlexArray:
obj.add("(flexarray ")
Expand All @@ -318,7 +323,7 @@ proc addArrayField(obj: var Builder; m: BModule, name, elementType: Snippet; len
## adds an array field inside a struct/union type
when buildNifc:
obj.add("\n\t(fld :")
obj.add(name)
obj.add(rawFieldName(name))
obj.add(" . ") # pragmas
obj.add(getArrayType(m, elementType, len))
obj.add(")")
Expand All @@ -335,7 +340,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo
## adds an field inside a struct/union type, based on an `skField` symbol
when buildNifc:
obj.add("\n\t(fld :")
obj.add(name)
obj.add(rawFieldName(name))
var pragmasInner = ""
if field.alignment > 0:
pragmasInner.add("(align ")
Expand Down Expand Up @@ -387,7 +392,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo
proc addProcField(obj: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet, isVarargs = false) =
when buildNifc:
obj.add("(fld :")
obj.add(name)
obj.add(rawFieldName(name))
obj.add(" . (proctype . ")
obj.add(params)
obj.add(" ")
Expand Down
4 changes: 2 additions & 2 deletions compiler/cbuilderexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ proc subscript(a, b: Snippet): Snippet =

proc dotField(a, b: Snippet): Snippet =
when buildNifc:
"(dot " & a & " " & b & " +0)" # XXX inheritance field
"(dot " & a & " " & rawFieldName(b) & " +0)" # XXX inheritance field
else:
a & "." & b

proc derefField(a, b: Snippet): Snippet =
when buildNifc:
"(dot " & cDeref(a) & " " & b & " +0)" # XXX inheritance field
"(dot " & cDeref(a) & " " & rawFieldName(b) & " +0)" # XXX inheritance field
else:
a & "->" & b

Expand Down
6 changes: 3 additions & 3 deletions compiler/cbuilderstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ template addScope(builder: var Builder, body: typed) =

proc addLabel(builder: var Builder, name: TLabel) =
when buildNifc:
builder.add("(lab ")
builder.add(name)
builder.add("(lab :")
builder.add(cSymbol(name))
builder.addLineEnd(")")
else:
builder.add(name)
Expand All @@ -506,7 +506,7 @@ proc addReturn(builder: var Builder, value: Snippet) =
proc addGoto(builder: var Builder, label: TLabel) =
when buildNifc:
builder.add("(jmp ")
builder.add(label)
builder.add(cSymbol(label))
builder.addLineEnd(")")
else:
builder.add("goto ")
Expand Down
10 changes: 5 additions & 5 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Builder) =
let t = getTypeDesc(p.module, ty)
p.module.s[cfsStrData].addVarWithInitializer(kind = Const, name = tmpName, typ = t):
var closureInit: StructInitializer
p.module.s[cfsStrData].addStructInitializer(closureInit, kind = siOrderedStruct):
p.module.s[cfsStrData].addStructInitializer(closureInit, kind = siOrderedStruct#[, typ = t]#):
p.module.s[cfsStrData].addField(closureInit, name = "ClP_0"):
p.module.s[cfsStrData].add(NimNil)
p.module.s[cfsStrData].addField(closureInit, name = "ClE_0"):
Expand Down Expand Up @@ -92,10 +92,10 @@ proc genLiteral(p: BProc, n: PNode, ty: PType; result: var Builder) =
proc genLiteral(p: BProc, n: PNode; result: var Builder) =
genLiteral(p, n, n.typ, result)

proc genRawSetData(cs: TBitSet, size: int; result: var Builder) =
proc genRawSetData(cs: TBitSet, size: int;#[ t: Snippet;]# result: var Builder) =
if size > 8:
var setInit: StructInitializer
result.addStructInitializer(setInit, kind = siArray):
result.addStructInitializer(setInit, kind = siArray#[, typ = t]#):
for i in 0..<size:
if i mod 8 == 0:
result.addNewline()
Expand All @@ -117,10 +117,10 @@ proc genSetNode(p: BProc, n: PNode; result: var Builder) =
inc(p.module.labels)
let td = getTypeDesc(p.module, n.typ)
p.module.s[cfsStrData].addVarWithInitializer(kind = Const, name = tmpName, typ = td):
genRawSetData(cs, size, p.module.s[cfsStrData])
genRawSetData(cs, size, #[td, ]# p.module.s[cfsStrData])
result.add tmpName
else:
genRawSetData(cs, size, result)
genRawSetData(cs, size, #[getTypeDesc(p.module, n.typ), ]# result)

proc getStorageLoc(n: PNode): TStorageLoc =
## deadcode
Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgliterals.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ proc genStringLiteralDataOnlyV1(m: BModule, s: string; result: var Rope) =
cgsym(m, "TGenericSeq")
let tmp = getTempName(m)
result.add tmp
let typ = tmp & "_StrLit"
let typ = tmp & (when buildNifc: ".strlit" else: "_StrLit")
var typBuilder = newBuilder("")
typBuilder.addTypedef(name = typ):
typBuilder.addSimpleStruct(m, name = "", baseType = ""):
Expand Down Expand Up @@ -74,7 +74,7 @@ proc genStringLiteralV1(m: BModule; n: PNode; result: var Builder) =
# ------ Version 2: destructor based strings and seqs -----------------------

proc genStringLiteralDataOnlyV2(m: BModule, s: string; result: Rope; isConst: bool) =
let typ = getTempName(m) & "_StrLit"
let typ = getTempName(m) & (when buildNifc: ".strlit" else: "_StrLit")
var typBuilder = newBuilder("")
typBuilder.addTypedef(name = typ):
typBuilder.addSimpleStruct(m, name = "", baseType = ""):
Expand Down
15 changes: 8 additions & 7 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,11 @@ proc genRestoreFrameAfterException(p: BProc) =
if hasCurFramePointer notin p.flags:
p.flags.incl hasCurFramePointer
p.procSec(cpsLocals).add('\t')
p.procSec(cpsLocals).addVar(kind = Local, name = "_nimCurFrame", typ = ptrType("TFrame"))
p.procSec(cpsLocals).addVar(kind = Local, name = cSymbol("_nimCurFrame"), typ = ptrType(cSymbol("TFrame")))
p.procSec(cpsInit).add('\t')
p.procSec(cpsInit).addAssignmentWithValue("_nimCurFrame"):
p.procSec(cpsInit).addAssignmentWithValue(cSymbol("_nimCurFrame")):
p.procSec(cpsInit).addCall(cgsymValue(p.module, "getFrame"))
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "setFrame"), "_nimCurFrame")
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "setFrame"), cSymbol("_nimCurFrame"))

proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
#[ code to generate:
Expand Down Expand Up @@ -1556,17 +1556,18 @@ proc genTryGoto(p: BProc; t: PNode; d: var TLoc) =
genStmts(p, t[i][0])
else:
# pretend we did handle the error for the safe execution of the 'finally' section:
p.procSec(cpsLocals).addVar(kind = Local, name = "oldNimErrFin" & $lab & "_", typ = NimBool)
p.s(cpsStmts).addAssignment("oldNimErrFin" & $lab & "_", cDeref("nimErr_"))
p.s(cpsStmts).addAssignment(cDeref("nimErr_"), NimFalse)
let name = cSymbol("oldNimErrFin" & $lab & "_")
p.procSec(cpsLocals).addVar(kind = Local, name = name, typ = NimBool)
p.s(cpsStmts).addAssignment(name, cDeref(cSymbol("nimErr_")))
p.s(cpsStmts).addAssignment(cDeref(cSymbol("nimErr_")), NimFalse)
genStmts(p, t[i][0])
# this is correct for all these cases:
# 1. finally is run during ordinary control flow
# 2. finally is run after 'except' block handling: these however set the
# error back to nil.
# 3. finally is run for exception handling code without any 'except'
# handler present or only handlers that did not match.
p.s(cpsStmts).addAssignment(cDeref("nimErr_"), "oldNimErrFin" & $lab & "_")
p.s(cpsStmts).addAssignment(cDeref(cSymbol("nimErr_")), name)
endSimpleBlock(p, finallyScope)
raiseExit(p)
if hasExcept: inc p.withinTryWithExcept
Expand Down
26 changes: 19 additions & 7 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ proc mangleField(m: BModule; name: PIdent): string =
result.add "_0"

proc mangleProc(m: BModule; s: PSym; makeUnique: bool): string =
result = "_Z" # Common prefix in Itanium ABI
when buildNifc:
result = ".Z"
else:
result = "_Z" # Common prefix in Itanium ABI
result.add encodeSym(m, s, makeUnique)
if s.typ.len > 1: #we dont care about the return param
for i in 1..<s.typ.len:
Expand Down Expand Up @@ -112,11 +115,15 @@ proc fillLocalName(p: BProc; s: PSym) =
var key = s.name.s.mangle
let counter = p.sigConflicts.getOrDefault(key)
var result = key.rope
if s.kind == skTemp:
# speed up conflict search for temps (these are quite common):
if counter != 0: result.add "_" & rope(counter+1)
elif counter != 0 or isKeyword(s.name) or p.module.g.config.cppDefines.contains(key):
result.add "_" & rope(counter+1)
when buildNifc:
result.add "."
result.addInt counter+1
else:
if s.kind == skTemp:
# speed up conflict search for temps (these are quite common):
if counter != 0: result.add "_" & rope(counter+1)
elif counter != 0 or isKeyword(s.name) or p.module.g.config.cppDefines.contains(key):
result.add "_" & rope(counter+1)
p.sigConflicts.inc(key)
s.loc.snippet = result

Expand All @@ -137,7 +144,10 @@ proc typeName(typ: PType; result: var Rope) =
let typ = typ.skipTypes(irrelevantForBackend)
result.add $typ.kind
if typ.sym != nil and typ.kind in {tyObject, tyEnum}:
result.add "_"
when buildNifc:
result.add "."
else:
result.add "_"
result.add typ.sym.name.s.mangle

proc getTypeName(m: BModule; typ: PType; sig: SigHash): Rope =
Expand All @@ -153,6 +163,8 @@ proc getTypeName(m: BModule; typ: PType; sig: SigHash): Rope =
let typ = if typ.kind in {tyAlias, tySink, tyOwned}: typ.elementType else: typ
if typ.loc.snippet == "":
typ.typeName(typ.loc.snippet)
when buildNifc:
typ.loc.snippet.add "."
typ.loc.snippet.add $sig
else:
when defined(debugSigHashes):
Expand Down
Loading

0 comments on commit fafa3cb

Please sign in to comment.