Skip to content

Commit

Permalink
cbuilder: use constants for type names, some cleanups (#24438)
Browse files Browse the repository at this point in the history
As described in #24432
  • Loading branch information
metagn authored Nov 18, 2024
1 parent f053767 commit 712f5be
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 348 deletions.
37 changes: 37 additions & 0 deletions compiler/cbuilderbase.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,43 @@ proc cIntLiteral*(i: BiggestInt): Snippet =
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*"

proc cIntType*(bits: int): Snippet =
"NI" & $bits

proc cUintType*(bits: int): Snippet =
"NU" & $bits

type
IfBuilderState* = enum
WaitingIf, WaitingElseIf, InBlock
Expand Down
6 changes: 3 additions & 3 deletions compiler/cbuilderdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ proc startSimpleStruct(obj: var Builder; m: BModule; name: string; baseType: Sni
proc finishSimpleStruct(obj: var Builder; m: BModule; info: StructBuilderInfo) =
if info.baseKind == bcNone and info.preFieldsLen == obj.buf.len:
# no fields were added, add dummy field
obj.addField(name = "dummy", typ = "char")
obj.addField(name = "dummy", typ = CChar)
if info.named:
obj.add("};\n")
else:
Expand Down Expand Up @@ -330,7 +330,7 @@ proc startStruct(obj: var Builder; m: BModule; t: PType; name: string; baseType:
t.n != nil and t.n.len == 1 and t.n[0].kind == nkSym and
t.n[0].sym.typ.skipTypes(abstractInst).kind == tyUncheckedArray:
# only consists of flexible array field, add *initial* dummy field
obj.addField(name = "dummy", typ = "char")
obj.addField(name = "dummy", typ = CChar)
of bcCppInherit: discard
of bcNoneRtti:
obj.addField(name = "m_type", typ = ptrType(cgsymValue(m, "TNimType")))
Expand All @@ -343,7 +343,7 @@ proc finishStruct(obj: var Builder; m: BModule; t: PType; info: StructBuilderInf
if info.baseKind == bcNone and info.preFieldsLen == obj.buf.len and
t.itemId notin m.g.graph.memberProcsPerType:
# no fields were added, add dummy field
obj.addField(name = "dummy", typ = "char")
obj.addField(name = "dummy", typ = CChar)
if info.named:
obj.add("};\n")
else:
Expand Down
6 changes: 0 additions & 6 deletions compiler/cbuilderexprs.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# XXX make complex ones like bitOr use builder instead
# XXX add stuff like NI, NIM_NIL as constants

proc constType(t: Snippet): Snippet =
# needs manipulation of `t` in nifc
"NIM_CONST " & t
Expand Down Expand Up @@ -87,9 +84,6 @@ proc dotField(a, b: Snippet): Snippet =
proc derefField(a, b: Snippet): Snippet =
a & "->" & b

proc bitOr(a, b: Snippet): Snippet =
"(" & a & " | " & b & ")"

type CallBuilder = object
needsComma: bool

Expand Down
5 changes: 5 additions & 0 deletions compiler/cbuilderstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,8 @@ template addCPragma(builder: var Builder, val: Snippet) =
builder.add("#pragma ")
builder.add(val)
builder.addNewline()

proc addDiscard(builder: var Builder, val: Snippet) =
builder.add("(void)")
builder.add(val)
builder.addLineEnd(";")
22 changes: 11 additions & 11 deletions compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,20 @@ proc genOpenArraySlice(p: BProc; q: PNode; formalType, destType: PType; prepareF
let ra = rdLoc(a)
let rb = rdLoc(b)
let rc = rdLoc(c)
let lengthExpr = cOp(Add, "NI", cOp(Sub, "NI", rc, rb), cIntValue(1))
let lengthExpr = cOp(Add, NimInt, cOp(Sub, NimInt, rc, rb), cIntValue(1))
case ty.kind
of tyArray:
let first = toInt64(firstOrd(p.config, ty))
if first == 0:
result = (cCast(ptrType(dest), cOp(Add, "NI", ra, rb)), lengthExpr)
result = (cCast(ptrType(dest), cOp(Add, NimInt, ra, rb)), lengthExpr)
else:
let lit = cIntLiteral(first)
result = (cCast(ptrType(dest), cOp(Add, "NI", ra, cOp(Sub, "NI", rb, lit))), lengthExpr)
result = (cCast(ptrType(dest), cOp(Add, NimInt, ra, cOp(Sub, NimInt, rb, lit))), lengthExpr)
of tyOpenArray, tyVarargs:
let data = if reifiedOpenArray(q[1]): dotField(ra, "Field0") else: ra
result = (cCast(ptrType(dest), cOp(Add, "NI", data, rb)), lengthExpr)
result = (cCast(ptrType(dest), cOp(Add, NimInt, data, rb)), lengthExpr)
of tyUncheckedArray, tyCstring:
result = (cCast(ptrType(dest), cOp(Add, "NI", ra, rb)), lengthExpr)
result = (cCast(ptrType(dest), cOp(Add, NimInt, ra, rb)), lengthExpr)
of tyString, tySequence:
let atyp = skipTypes(a.t, abstractInst)
if formalType.skipTypes(abstractInst).kind in {tyVar} and atyp.kind == tyString and
Expand All @@ -241,8 +241,8 @@ proc genOpenArraySlice(p: BProc; q: PNode; formalType, destType: PType; prepareF
val = ra
result = (
cIfExpr(dataFieldAccessor(p, val),
cCast(ptrType(dest), cOp(Add, "NI", dataField(p, val), rb)),
"NIM_NIL"),
cCast(ptrType(dest), cOp(Add, NimInt, dataField(p, val), rb)),
NimNil),
lengthExpr)
else:
result = ("", "")
Expand Down Expand Up @@ -295,13 +295,13 @@ proc openArrayLoc(p: BProc, formalType: PType, n: PNode; result: var Builder) =
let ra = a.rdLoc
var t = TLoc(snippet: cDeref(ra))
let lt = lenExpr(p, t)
result.add(cIfExpr(dataFieldAccessor(p, t.snippet), dataField(p, t.snippet), "NIM_NIL"))
result.add(cIfExpr(dataFieldAccessor(p, t.snippet), dataField(p, t.snippet), NimNil))
result.addArgumentSeparator()
result.add(lt)
else:
let ra = a.rdLoc
let la = lenExpr(p, a)
result.add(cIfExpr(dataFieldAccessor(p, ra), dataField(p, ra), "NIM_NIL"))
result.add(cIfExpr(dataFieldAccessor(p, ra), dataField(p, ra), NimNil))
result.addArgumentSeparator()
result.add(la)
of tyArray:
Expand All @@ -315,7 +315,7 @@ proc openArrayLoc(p: BProc, formalType: PType, n: PNode; result: var Builder) =
let ra = a.rdLoc
var t = TLoc(snippet: cDeref(ra))
let lt = lenExpr(p, t)
result.add(cIfExpr(dataFieldAccessor(p, t.snippet), dataField(p, t.snippet), "NIM_NIL"))
result.add(cIfExpr(dataFieldAccessor(p, t.snippet), dataField(p, t.snippet), NimNil))
result.addArgumentSeparator()
result.add(lt)
of tyArray:
Expand Down Expand Up @@ -767,7 +767,7 @@ proc genPatternCall(p: BProc; ri: PNode; pat: string; typ: PType; result: var Bu
var idx, stars: int = 0
if scanCppGenericSlot(pat, i, idx, stars):
var t = resolveStarsInCppType(typ, idx, stars)
if t == nil: result.add("void")
if t == nil: result.add(CVoid)
else: result.add(getTypeDesc(p.module, t))
else:
let start = i
Expand Down
Loading

0 comments on commit 712f5be

Please sign in to comment.