diff options
author | Araq <rumpf_a@web.de> | 2012-07-19 00:20:22 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2012-07-19 00:20:22 +0200 |
commit | 150e733a408160340a44d71c950049fd3f49ed0d (patch) | |
tree | 658f439584ad7083352ed1a3413c4c610c8dcbf8 /compiler | |
parent | e68830a952473682fdb41808e2791e8ab8d1f2f6 (diff) | |
parent | 32e29946361bd4c8563881abbf5139509bfdc19d (diff) | |
download | Nim-150e733a408160340a44d71c950049fd3f49ed0d.tar.gz |
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'compiler')
-rwxr-xr-x | compiler/cgen.nim | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim index c7bfced73..3b89e60c8 100755 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -491,34 +491,35 @@ proc libCandidates(s: string, dest: var TStringSeq) = else: add(dest, s) -proc loadDynamicLib(m: BModule, lib: PLib) = +proc loadDynamicLib(m: BModule, lib: PLib) = + let g = gNimDat assert(lib != nil) if not lib.generated: lib.generated = true var tmp = getGlobalTempName() assert(lib.name == nil) lib.name = tmp # BUGFIX: cgsym has awful side-effects - appf(m.s[cfsVars], "static void* $1;$n", [tmp]) + appf(g.s[cfsVars], "static void* $1;$n", [tmp]) if lib.path.kind in {nkStrLit..nkTripleStrLit}: var s: TStringSeq = @[] libCandidates(lib.path.strVal, s) var loadlib: PRope = nil for i in countup(0, high(s)): - inc(m.labels) + inc(g.labels) if i > 0: app(loadlib, "||") - appcg(m, loadlib, "($1 = #nimLoadLibrary((#NimStringDesc*) &$2))$n", - [tmp, getStrLit(m, s[i])]) - appcg(m, m.s[cfsDynLibInit], + appcg(g, loadlib, "($1 = #nimLoadLibrary((#NimStringDesc*) &$2))$n", + [tmp, getStrLit(g, s[i])]) + appcg(g, g.s[cfsDynLibInit], "if (!($1)) #nimLoadLibraryError((#NimStringDesc*) &$2);$n", - [loadlib, getStrLit(m, lib.path.strVal)]) + [loadlib, getStrLit(g, lib.path.strVal)]) else: - var p = newProc(nil, m) + var p = newProc(nil, g) var dest: TLoc initLocExpr(p, lib.path, dest) - app(m.s[cfsVars], p.s(cpsLocals)) - app(m.s[cfsDynLibInit], p.s(cpsInit)) - app(m.s[cfsDynLibInit], p.s(cpsStmts)) - appcg(m, m.s[cfsDynLibInit], + app(g.s[cfsVars], p.s(cpsLocals)) + app(g.s[cfsDynLibInit], p.s(cpsInit)) + app(g.s[cfsDynLibInit], p.s(cpsStmts)) + appcg(g, g.s[cfsDynLibInit], "if (!($1 = #nimLoadLibrary($2))) #nimLoadLibraryError($2);$n", [tmp, rdLoc(dest)]) @@ -531,7 +532,8 @@ proc mangleDynLibProc(sym: PSym): PRope = else: result = ropef("Dl_$1", [toRope(sym.id)]) -proc SymInDynamicLib(m: BModule, sym: PSym) = +proc SymInDynamicLib(m: BModule, sym: PSym) = + let g = gNimDat var lib = sym.annex var extname = sym.loc.r loadDynamicLib(m, lib) @@ -539,28 +541,33 @@ proc SymInDynamicLib(m: BModule, sym: PSym) = var tmp = mangleDynLibProc(sym) sym.loc.r = tmp # from now on we only need the internal name sym.typ.sym = nil # generate a new name - inc(m.labels, 2) - appcg(m, m.s[cfsDynLibInit], + inc(g.labels, 2) + appcg(g, g.s[cfsDynLibInit], "$1 = ($2) #nimGetProcAddr($3, $4);$n", - [tmp, getTypeDesc(m, sym.typ), - lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname))]) - appff(m.s[cfsVars], "$2 $1;$n", - "$1 = linkonce global $2 zeroinitializer$n", + [tmp, getTypeDesc(g, sym.typ), + lib.name, cstringLit(g, g.s[cfsDynLibInit], ropeToStr(extname))]) + appff(g.s[cfsVars], "$2 $1;$n", + "$1 = linkonce global $2 zeroinitializer$n", + [sym.loc.r, getTypeDesc(g, sym.loc.t)]) + appf(m.s[cfsVars], "extern $2 $1;$n", [sym.loc.r, getTypeDesc(m, sym.loc.t)]) - -proc VarInDynamicLib(m: BModule, sym: PSym) = + +proc VarInDynamicLib(m: BModule, sym: PSym) = + let g = gNimDat var lib = sym.annex var extname = sym.loc.r loadDynamicLib(m, lib) incl(sym.loc.flags, lfIndirect) var tmp = mangleDynLibProc(sym) sym.loc.r = tmp # from now on we only need the internal name - inc(m.labels, 2) - appcg(m, m.s[cfsDynLibInit], + inc(g.labels, 2) + appcg(g, g.s[cfsDynLibInit], "$1 = ($2*) #nimGetProcAddr($3, $4);$n", - [tmp, getTypeDesc(m, sym.typ), - lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname))]) - appf(m.s[cfsVars], "$2* $1;$n", + [tmp, getTypeDesc(g, sym.typ), + lib.name, cstringLit(g, g.s[cfsDynLibInit], ropeToStr(extname))]) + appf(g.s[cfsVars], "$2* $1;$n", + [sym.loc.r, getTypeDesc(g, sym.loc.t)]) + appf(m.s[cfsVars], "extern $2* $1;$n", [sym.loc.r, getTypeDesc(m, sym.loc.t)]) proc SymInDynamicLibPartial(m: BModule, sym: PSym) = @@ -820,8 +827,8 @@ proc genMainProc(m: BModule) = const CommonMainBody = "\tnim__datInit();$n" & - "\tsystemInit();$n" & "$1" & + "\tsystemInit();$n" & "$2" & "$3" PosixNimMain = |