enhancements for cgen.libCandidates
This commit is contained in:
parent
27d7e3a0ae
commit
93a140b904
2 changed files with 34 additions and 45 deletions
53
rod/cgen.nim
53
rod/cgen.nim
|
|
@ -326,18 +326,13 @@ include "ccgexprs.nim", "ccgstmts.nim"
|
||||||
# We don't finalize dynamic libs as this does the OS for us.
|
# We don't finalize dynamic libs as this does the OS for us.
|
||||||
|
|
||||||
proc libCandidates(s: string, dest: var TStringSeq) =
|
proc libCandidates(s: string, dest: var TStringSeq) =
|
||||||
var
|
var le = strutils.find(s, '(')
|
||||||
prefix, suffix: string
|
var ri = strutils.find(s, ')', le+1)
|
||||||
le, ri, L: int
|
if le >= 0 and ri > le:
|
||||||
temp: TStringSeq
|
var prefix = copy(s, 0, le - 1)
|
||||||
le = strutils.find(s, '(')
|
var suffix = copy(s, ri + 1)
|
||||||
ri = strutils.find(s, ')')
|
for middle in split(copy(s, le + 1, ri - 1), {'|'}):
|
||||||
if (le >= 0) and (ri > le):
|
libCandidates(prefix & middle & suffix, dest)
|
||||||
prefix = copy(s, 0, le - 1)
|
|
||||||
suffix = copy(s, ri + 1)
|
|
||||||
temp = split(copy(s, le + 1, ri - 1), {'|'})
|
|
||||||
for i in countup(0, high(temp)):
|
|
||||||
libCandidates(prefix & temp[i] & suffix, dest)
|
|
||||||
else:
|
else:
|
||||||
add(dest, s)
|
add(dest, s)
|
||||||
|
|
||||||
|
|
@ -359,7 +354,8 @@ proc loadDynamicLib(m: BModule, lib: PLib) =
|
||||||
for i in countup(0, high(s)):
|
for i in countup(0, high(s)):
|
||||||
inc(m.labels)
|
inc(m.labels)
|
||||||
if i > 0: app(loadlib, "||")
|
if i > 0: app(loadlib, "||")
|
||||||
appff(loadlib, "($1 = nimLoadLibrary((NimStringDesc*) &$2))$n", "%MOC$4 = call i8* @nimLoadLibrary($3 $2)$n" &
|
appff(loadlib, "($1 = nimLoadLibrary((NimStringDesc*) &$2))$n",
|
||||||
|
"%MOC$4 = call i8* @nimLoadLibrary($3 $2)$n" &
|
||||||
"store i8* %MOC$4, i8** $1$n", [tmp, getStrLit(m, s[i]),
|
"store i8* %MOC$4, i8** $1$n", [tmp, getStrLit(m, s[i]),
|
||||||
getTypeDesc(m, getSysType(tyString)), toRope(m.labels)])
|
getTypeDesc(m, getSysType(tyString)), toRope(m.labels)])
|
||||||
appff(m.s[cfsDynLibInit],
|
appff(m.s[cfsDynLibInit],
|
||||||
|
|
@ -374,15 +370,12 @@ proc loadDynamicLib(m: BModule, lib: PLib) =
|
||||||
if lib.name == nil: InternalError("loadDynamicLib")
|
if lib.name == nil: InternalError("loadDynamicLib")
|
||||||
|
|
||||||
proc SymInDynamicLib(m: BModule, sym: PSym) =
|
proc SymInDynamicLib(m: BModule, sym: PSym) =
|
||||||
var
|
var lib = sym.annex
|
||||||
lib: PLib
|
var extname = sym.loc.r
|
||||||
extname, tmp: PRope
|
|
||||||
lib = sym.annex
|
|
||||||
extname = sym.loc.r
|
|
||||||
loadDynamicLib(m, lib)
|
loadDynamicLib(m, lib)
|
||||||
useMagic(m, "nimGetProcAddr")
|
useMagic(m, "nimGetProcAddr")
|
||||||
if gCmd == cmdCompileToLLVM: incl(sym.loc.flags, lfIndirect)
|
if gCmd == cmdCompileToLLVM: incl(sym.loc.flags, lfIndirect)
|
||||||
tmp = ropeff("Dl_$1", "@Dl_$1", [toRope(sym.id)])
|
var tmp = ropeff("Dl_$1", "@Dl_$1", [toRope(sym.id)])
|
||||||
sym.loc.r = tmp # from now on we only need the internal name
|
sym.loc.r = tmp # from now on we only need the internal name
|
||||||
sym.typ.sym = nil # generate a new name
|
sym.typ.sym = nil # generate a new name
|
||||||
inc(m.labels, 2)
|
inc(m.labels, 2)
|
||||||
|
|
@ -392,12 +385,12 @@ proc SymInDynamicLib(m: BModule, sym: PSym) =
|
||||||
"store $2 %MOC$6, $2* $1$n", [tmp, getTypeDesc(m, sym.typ),
|
"store $2 %MOC$6, $2* $1$n", [tmp, getTypeDesc(m, sym.typ),
|
||||||
lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname)),
|
lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname)),
|
||||||
toRope(m.labels), toRope(m.labels - 1)])
|
toRope(m.labels), toRope(m.labels - 1)])
|
||||||
appff(m.s[cfsVars], "$2 $1;$n", "$1 = linkonce global $2 zeroinitializer$n",
|
appff(m.s[cfsVars], "$2 $1;$n",
|
||||||
|
"$1 = linkonce global $2 zeroinitializer$n",
|
||||||
[sym.loc.r, getTypeDesc(m, sym.loc.t)])
|
[sym.loc.r, getTypeDesc(m, sym.loc.t)])
|
||||||
|
|
||||||
proc UseMagic(m: BModule, name: string) =
|
proc UseMagic(m: BModule, name: string) =
|
||||||
var sym: PSym
|
var sym = magicsys.getCompilerProc(name)
|
||||||
sym = magicsys.getCompilerProc(name)
|
|
||||||
if sym != nil:
|
if sym != nil:
|
||||||
case sym.kind
|
case sym.kind
|
||||||
of skProc, skMethod, skConverter: genProc(m, sym)
|
of skProc, skMethod, skConverter: genProc(m, sym)
|
||||||
|
|
@ -426,11 +419,13 @@ proc getFrameDecl(p: BProc) =
|
||||||
else:
|
else:
|
||||||
slots = nil
|
slots = nil
|
||||||
appff(p.s[cpsLocals], "volatile struct {TFrame* prev;" &
|
appff(p.s[cpsLocals], "volatile struct {TFrame* prev;" &
|
||||||
"NCSTRING procname;NI line;NCSTRING filename;" & "NI len;$n$1} F;$n",
|
"NCSTRING procname;NI line;NCSTRING filename;" &
|
||||||
"%TF = type {%TFrame*, i8*, %NI, %NI$1}$n" & "%F = alloca %TF$n",
|
"NI len;$n$1} F;$n",
|
||||||
[slots])
|
"%TF = type {%TFrame*, i8*, %NI, %NI$1}$n" &
|
||||||
|
"%F = alloca %TF$n", [slots])
|
||||||
inc(p.labels)
|
inc(p.labels)
|
||||||
prepend(p.s[cpsInit], ropeff("F.len = $1;$n", "%LOC$2 = getelementptr %TF %F, %NI 4$n" &
|
prepend(p.s[cpsInit], ropeff("F.len = $1;$n",
|
||||||
|
"%LOC$2 = getelementptr %TF %F, %NI 4$n" &
|
||||||
"store %NI $1, %NI* %LOC$2$n", [toRope(p.frameLen), toRope(p.labels)]))
|
"store %NI $1, %NI* %LOC$2$n", [toRope(p.frameLen), toRope(p.labels)]))
|
||||||
|
|
||||||
proc retIsNotVoid(s: PSym): bool =
|
proc retIsNotVoid(s: PSym): bool =
|
||||||
|
|
@ -439,7 +434,8 @@ proc retIsNotVoid(s: PSym): bool =
|
||||||
proc initFrame(p: BProc, procname, filename: PRope): PRope =
|
proc initFrame(p: BProc, procname, filename: PRope): PRope =
|
||||||
inc(p.labels, 5)
|
inc(p.labels, 5)
|
||||||
result = ropeff("F.procname = $1;$n" & "F.prev = framePtr;$n" &
|
result = ropeff("F.procname = $1;$n" & "F.prev = framePtr;$n" &
|
||||||
"F.filename = $2;$n" & "F.line = 0;$n" & "framePtr = (TFrame*)&F;$n", "%LOC$3 = getelementptr %TF %F, %NI 1$n" &
|
"F.filename = $2;$n" & "F.line = 0;$n" & "framePtr = (TFrame*)&F;$n",
|
||||||
|
"%LOC$3 = getelementptr %TF %F, %NI 1$n" &
|
||||||
"%LOC$4 = getelementptr %TF %F, %NI 0$n" &
|
"%LOC$4 = getelementptr %TF %F, %NI 0$n" &
|
||||||
"%LOC$5 = getelementptr %TF %F, %NI 3$n" &
|
"%LOC$5 = getelementptr %TF %F, %NI 3$n" &
|
||||||
"%LOC$6 = getelementptr %TF %F, %NI 2$n" & "store i8* $1, i8** %LOC$3$n" &
|
"%LOC$6 = getelementptr %TF %F, %NI 2$n" & "store i8* $1, i8** %LOC$3$n" &
|
||||||
|
|
@ -452,7 +448,8 @@ proc initFrame(p: BProc, procname, filename: PRope): PRope =
|
||||||
|
|
||||||
proc deinitFrame(p: BProc): PRope =
|
proc deinitFrame(p: BProc): PRope =
|
||||||
inc(p.labels, 3)
|
inc(p.labels, 3)
|
||||||
result = ropeff("framePtr = framePtr->prev;$n", "%LOC$1 = load %TFrame* @framePtr$n" &
|
result = ropeff("framePtr = framePtr->prev;$n",
|
||||||
|
"%LOC$1 = load %TFrame* @framePtr$n" &
|
||||||
"%LOC$2 = getelementptr %TFrame* %LOC$1, %NI 0$n" &
|
"%LOC$2 = getelementptr %TFrame* %LOC$1, %NI 0$n" &
|
||||||
"%LOC$3 = load %TFrame** %LOC$2$n" &
|
"%LOC$3 = load %TFrame** %LOC$2$n" &
|
||||||
"store %TFrame* $LOC$3, %TFrame** @framePtr", [toRope(p.labels),
|
"store %TFrame* $LOC$3, %TFrame** @framePtr", [toRope(p.labels),
|
||||||
|
|
|
||||||
|
|
@ -320,17 +320,12 @@ proc noVal(n: PNode) =
|
||||||
if n.kind == nkExprColonExpr: invalidPragma(n)
|
if n.kind == nkExprColonExpr: invalidPragma(n)
|
||||||
|
|
||||||
proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
|
proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
|
||||||
var
|
|
||||||
key, it: PNode
|
|
||||||
k: TSpecialWord
|
|
||||||
lib: PLib
|
|
||||||
if n == nil: return
|
if n == nil: return
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
it = n.sons[i]
|
var it = n.sons[i]
|
||||||
if it.kind == nkExprColonExpr: key = it.sons[0]
|
var key = if it.kind == nkExprColonExpr: it.sons[0] else: it
|
||||||
else: key = it
|
|
||||||
if key.kind == nkIdent:
|
if key.kind == nkIdent:
|
||||||
k = whichKeyword(key.ident)
|
var k = whichKeyword(key.ident)
|
||||||
if k in validPragmas:
|
if k in validPragmas:
|
||||||
case k
|
case k
|
||||||
of wExportc:
|
of wExportc:
|
||||||
|
|
@ -367,7 +362,7 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
|
||||||
noval(it)
|
noval(it)
|
||||||
incl(sym.flags, sfMerge)
|
incl(sym.flags, sfMerge)
|
||||||
of wHeader:
|
of wHeader:
|
||||||
lib = getLib(c, libHeader, expectStrLit(c, it))
|
var lib = getLib(c, libHeader, expectStrLit(c, it))
|
||||||
addToLib(lib, sym)
|
addToLib(lib, sym)
|
||||||
incl(sym.flags, sfImportc)
|
incl(sym.flags, sfImportc)
|
||||||
incl(sym.loc.flags, lfHeader)
|
incl(sym.loc.flags, lfHeader)
|
||||||
|
|
@ -417,12 +412,9 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
|
||||||
of wTypeCheck:
|
of wTypeCheck:
|
||||||
noVal(it)
|
noVal(it)
|
||||||
incl(sym.flags, sfTypeCheck)
|
incl(sym.flags, sfTypeCheck)
|
||||||
of wHint:
|
of wHint: liMessage(it.info, hintUser, expectStrLit(c, it))
|
||||||
liMessage(it.info, hintUser, expectStrLit(c, it))
|
of wWarning: liMessage(it.info, warnUser, expectStrLit(c, it))
|
||||||
of wWarning:
|
of wError: liMessage(it.info, errUser, expectStrLit(c, it))
|
||||||
liMessage(it.info, warnUser, expectStrLit(c, it))
|
|
||||||
of wError:
|
|
||||||
liMessage(it.info, errUser, expectStrLit(c, it))
|
|
||||||
of wFatal:
|
of wFatal:
|
||||||
liMessage(it.info, errUser, expectStrLit(c, it))
|
liMessage(it.info, errUser, expectStrLit(c, it))
|
||||||
quit(1)
|
quit(1)
|
||||||
|
|
@ -454,7 +446,7 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) =
|
||||||
if (sym != nil) and (sym.kind != skModule):
|
if (sym != nil) and (sym.kind != skModule):
|
||||||
if (lfExportLib in sym.loc.flags) and not (sfExportc in sym.flags):
|
if (lfExportLib in sym.loc.flags) and not (sfExportc in sym.flags):
|
||||||
liMessage(n.info, errDynlibRequiresExportc)
|
liMessage(n.info, errDynlibRequiresExportc)
|
||||||
lib = POptionEntry(c.optionstack.tail).dynlib
|
var lib = POptionEntry(c.optionstack.tail).dynlib
|
||||||
if ({lfDynamicLib, lfHeader} * sym.loc.flags == {}) and
|
if ({lfDynamicLib, lfHeader} * sym.loc.flags == {}) and
|
||||||
(sfImportc in sym.flags) and (lib != nil):
|
(sfImportc in sym.flags) and (lib != nil):
|
||||||
incl(sym.loc.flags, lfDynamicLib)
|
incl(sym.loc.flags, lfDynamicLib)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue