Skip to content

Commit

Permalink
brute force varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 26, 2024
1 parent acb0117 commit eb7bfc3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
52 changes: 35 additions & 17 deletions compiler/cbuilderdecls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ template addTypedef(builder: var Builder, name: string, typeBody: typed) =
builder.add(name)
builder.addLineEnd(";")

proc addProcTypedef(builder: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet) =
proc addProcTypedef(builder: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet, isVarargs = false) =
when buildNifc:
builder.add("(type :")
builder.add(name)
Expand All @@ -177,8 +177,9 @@ proc addProcTypedef(builder: var Builder, callConv: TCallingConvention, name: st
builder.add(" ")
builder.add(rettype)
builder.add(" (pragmas ")
# XXX varargs goes here
builder.add(CallingConvToStr[callConv])
if isVarargs:
builder.add(" (varargs)")
builder.addLineEnd(")))")
else:
builder.add("typedef ")
Expand Down Expand Up @@ -382,7 +383,7 @@ proc addField(obj: var Builder; field: PSym; name, typ: Snippet; isFlexArray: bo
obj.add(cppInitializer)
obj.add(";\n")

proc addProcField(obj: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet) =
proc addProcField(obj: var Builder, callConv: TCallingConvention, name: string, rettype, params: Snippet, isVarargs = false) =
when buildNifc:
obj.add("(fld :")
obj.add(name)
Expand All @@ -391,8 +392,9 @@ proc addProcField(obj: var Builder, callConv: TCallingConvention, name: string,
obj.add(" ")
obj.add(rettype)
obj.add(" (pragmas ")
# XXX varargs goes here
obj.add(CallingConvToStr[callConv])
if isVarargs:
obj.add(" (varargs)")
obj.addLineEnd(")))")
else:
obj.add(CallingConvToStr[callConv])
Expand Down Expand Up @@ -779,7 +781,7 @@ proc addUnnamedParam(builder: var Builder, params: var ProcParamBuilder, typ: Sn
else:
builder.add(typ)

proc addProcTypedParam(builder: var Builder, paramBuilder: var ProcParamBuilder, callConv: TCallingConvention, name: string, rettype, params: Snippet) =
proc addProcTypedParam(builder: var Builder, paramBuilder: var ProcParamBuilder, callConv: TCallingConvention, name: string, rettype, params: Snippet, isVarargs = false) =
if paramBuilder.needsComma:
when buildNifc:
builder.add(" ")
Expand All @@ -794,9 +796,10 @@ proc addProcTypedParam(builder: var Builder, paramBuilder: var ProcParamBuilder,
builder.add(params)
builder.add(" ")
builder.add(rettype)
# XXX varargs goes here
builder.add(" (pragmas ")
builder.add(CallingConvToStr[callConv])
if isVarargs:
builder.add(" (varargs)")
builder.add(")))")
else:
builder.add(CallingConvToStr[callConv])
Expand All @@ -808,9 +811,8 @@ proc addProcTypedParam(builder: var Builder, paramBuilder: var ProcParamBuilder,
builder.add(params)

proc addVarargsParam(builder: var Builder, params: var ProcParamBuilder) =
# does not exist in NIFC, needs to be proc pragma
when buildNifc:
# XXX not implemented
# information needs to be provided to proc type generators
discard
else:
if params.needsComma:
Expand Down Expand Up @@ -853,7 +855,7 @@ proc cProcParams(params: varargs[SimpleProcParam]): Snippet =
result.add(")")

template addProcHeaderWithParams(builder: var Builder, callConv: TCallingConvention,
name: string, rettype: Snippet, paramBuilder: typed) =
name: string, rettype: Snippet, isVarargs: bool, paramBuilder: typed) =
when buildNifc:
builder.add("(proc :")
builder.add(name)
Expand All @@ -870,13 +872,19 @@ template addProcHeaderWithParams(builder: var Builder, callConv: TCallingConvent
builder.add(" ")
builder.add(rettype)
builder.add(" (pragmas ")
# XXX varargs goes here
builder.add(CallingConvToStr[callConv])
if isVarargs:
builder.add(" (varargs)")
builder.add(") ")

template addProcHeaderWithParams(builder: var Builder, callConv: TCallingConvention,
name: string, rettype: Snippet, paramBuilder: typed) =
# overload without isVarargs
addProcHeaderWithParams(builder, callConv, name, rettype, isVarargs = false, paramBuilder)

proc addProcHeader(builder: var Builder, callConv: TCallingConvention,
name: string, rettype, params: Snippet) =
addProcHeaderWithParams(builder, callConv, name, rettype):
name: string, rettype, params: Snippet, isVarargs = false) =
addProcHeaderWithParams(builder, callConv, name, rettype, isVarargs):
builder.add(params)

proc addProcHeader(builder: var Builder, name: string, rettype, params: Snippet, isConstructor = false) =
Expand All @@ -900,7 +908,7 @@ proc addProcHeader(builder: var Builder, name: string, rettype, params: Snippet,
builder.add(name)
builder.add(params)

proc addProcHeader(builder: var Builder, m: BModule, prc: PSym, name: string, params, rettype: Snippet, addAttributes: bool) =
proc addProcHeader(builder: var Builder, m: BModule, prc: PSym, name: string, params, rettype: Snippet, addAttributes: bool, isVarargs = false) =
let noreturn = isNoReturn(m, prc)
when buildNifc:
# XXX no declspec on nifc
Expand All @@ -912,6 +920,8 @@ proc addProcHeader(builder: var Builder, m: BModule, prc: PSym, name: string, pa
builder.add(rettype)
builder.add(" (pragmas ")
builder.add(CallingConvToStr[prc.typ.callConv])
if isVarargs:
builder.add(" (varargs)")
if addAttributes:
if sfPure in prc.flags and hasAttribute in extccomp.CC[m.config.cCompiler].props:
builder.add(" (attr \"naked\")")
Expand Down Expand Up @@ -954,7 +964,7 @@ template finishProcHeaderWithBody(builder: var Builder, body: typed) =
builder.addNewline()

proc addProcVar(builder: var Builder, m: BModule, prc: PSym, name: string, params, rettype: Snippet,
isStatic = false, ignoreAttributes = false) =
isStatic = false, ignoreAttributes = false, isVarargs = false) =
let noreturn = isNoReturn(m, prc)
when buildNifc:
# XXX declspec not supported in NIFC
Expand All @@ -968,6 +978,8 @@ proc addProcVar(builder: var Builder, m: BModule, prc: PSym, name: string, param
builder.add(rettype)
builder.add(" (pragmas ")
builder.add(CallingConvToStr[prc.typ.callConv])
if isVarargs:
builder.add(" (varargs)")
if not ignoreAttributes:
if sfPure in prc.flags and hasAttribute in extccomp.CC[m.config.cCompiler].props:
builder.add(" (attr \"naked\")")
Expand Down Expand Up @@ -1000,7 +1012,7 @@ proc addProcVar(builder: var Builder, m: BModule, prc: PSym, name: string, param

proc addProcVar(builder: var Builder, callConv: TCallingConvention,
name: string, params, rettype: Snippet,
isStatic = false, isVolatile = false) =
isStatic = false, isVolatile = false, isVarargs = false) =
when buildNifc:
# XXX volatile not supported in nifc
builder.add("(")
Expand All @@ -1013,6 +1025,8 @@ proc addProcVar(builder: var Builder, callConv: TCallingConvention,
builder.add(rettype)
builder.add(" (pragmas ")
builder.add(CallingConvToStr[callConv])
if isVarargs:
builder.add(" (varargs)")
# ensure we are just adding a variable:
builder.addLineEnd(")) .)")
else:
Expand All @@ -1032,7 +1046,7 @@ proc addProcVar(builder: var Builder, callConv: TCallingConvention,

proc addProcVar(builder: var Builder,
name: string, params, rettype: Snippet,
isStatic = false, isVolatile = false) =
isStatic = false, isVolatile = false, isVarargs = false) =
# no callconv
when buildNifc:
# XXX volatile not supported in nifc
Expand All @@ -1044,7 +1058,11 @@ proc addProcVar(builder: var Builder,
builder.add(params)
builder.add(" ")
builder.add(rettype)
builder.add(" .) .)")
if isVarargs:
builder.add(" (pragmas (varargs))")
else:
builder.add(" .")
builder.add(") .)")
else:
if isStatic:
builder.add("static ")
Expand Down
5 changes: 3 additions & 2 deletions compiler/cbuilderexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ else:
"N_NOCONV" #ccMember is N_NOCONV
]

proc procPtrTypeUnnamed(rettype, params: Snippet): Snippet =
proc procPtrTypeUnnamed(rettype, params: Snippet, isVarargs = false): Snippet =
when buildNifc:
"(proctype . " & params & " " & rettype & " .)"
"(proctype . " & params & " " & rettype & " " &
(if isVarargs: "(pragmas (varargs))" else: ".") & ")"
else:
rettype & "(*)" & params

Expand Down
12 changes: 6 additions & 6 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -970,11 +970,11 @@ proc getTypeDescAux(m: BModule; origTyp: PType, check: var IntSet; kind: TypeDes
let params = extract(desc)
if not isImportedType(t):
if t.callConv != ccClosure: # procedure vars may need a closure!
m.s[cfsTypes].addProcTypedef(callConv = t.callConv, name = result, rettype = rettype, params = params)
m.s[cfsTypes].addProcTypedef(callConv = t.callConv, name = result, rettype = rettype, params = params, isVarargs = tfVarargs in t.flags)
else:
m.s[cfsTypes].addTypedef(name = result):
m.s[cfsTypes].addSimpleStruct(m, name = "", baseType = ""):
m.s[cfsTypes].addProcField(name = "ClP_0", callConv = ccNimCall, rettype = rettype, params = params)
m.s[cfsTypes].addProcField(name = "ClP_0", callConv = ccNimCall, rettype = rettype, params = params, isVarargs = tfVarargs in t.flags)
m.s[cfsTypes].addField(name = "ClE_0", typ = CPointer)
of tySequence:
if optSeqDestructors in m.config.globalOptions:
Expand Down Expand Up @@ -1128,11 +1128,11 @@ proc getClosureType(m: BModule; t: PType, kind: TClosureTypeKind): Rope =
let params = extract(desc)
if not isImportedType(t):
if t.callConv != ccClosure or kind != clFull:
m.s[cfsTypes].addProcTypedef(callConv = t.callConv, name = result, rettype = rettype, params = params)
m.s[cfsTypes].addProcTypedef(callConv = t.callConv, name = result, rettype = rettype, params = params, isVarargs = tfVarargs in t.flags)
else:
m.s[cfsTypes].addTypedef(name = result):
m.s[cfsTypes].addSimpleStruct(m, name = "", baseType = ""):
m.s[cfsTypes].addProcField(name = "ClP_0", callConv = ccNimCall, rettype = rettype, params = params)
m.s[cfsTypes].addProcField(name = "ClP_0", callConv = ccNimCall, rettype = rettype, params = params, isVarargs = tfVarargs in t.flags)
m.s[cfsTypes].addField(name = "ClE_0", typ = CPointer)

proc finishTypeDescriptions(m: BModule) =
Expand Down Expand Up @@ -1254,9 +1254,9 @@ proc genProcHeader(m: BModule; prc: PSym; result: var Builder; visibility: var D
elif sfImportc notin prc.flags:
visibility = Private
if asPtr:
result.addProcVar(m, prc, name, params, rettype, isStatic = isStaticVar, ignoreAttributes = true)
result.addProcVar(m, prc, name, params, rettype, isStatic = isStaticVar, ignoreAttributes = true, isVarargs = tfVarargs in prc.typ.flags)
else:
result.addProcHeader(m, prc, name, params, rettype, addAttributes)
result.addProcHeader(m, prc, name, params, rettype, addAttributes, isVarargs = tfVarargs in prc.typ.flags)
else:
let asPtrStr = if asPtr: (rope("(*") & name & ")") else: name
result.add runtimeFormat(prc.cgDeclFrmt, [rettype, asPtrStr, params])
Expand Down
2 changes: 1 addition & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ proc getProcTypeCast(m: BModule, prc: PSym): Rope =
var check = initIntSet()
genProcParams(m, prc.typ, rettype, desc, check)
let params = extract(desc)
result = procPtrTypeUnnamed(rettype = rettype, params = params)
result = procPtrTypeUnnamed(rettype = rettype, params = params, isVarargs = tfVarargs in prc.typ.flags)

proc genProcBody(p: BProc; procBody: PNode) =
genStmts(p, procBody) # modifies p.locals, p.init, etc.
Expand Down

0 comments on commit eb7bfc3

Please sign in to comment.