summary refs log tree commit diff stats
path: root/compiler/cgen.nim
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/cgen.nim')
-rwxr-xr-xcompiler/cgen.nim27
1 files changed, 27 insertions, 0 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index e437cbb66..2c2230be9 100755
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -418,9 +418,20 @@ proc assignLocalVar(p: BProc, s: PSym) =
 
 include ccgthreadvars
 
+proc VarInDynamicLib(m: BModule, sym: PSym)
+proc mangleDynLibProc(sym: PSym): PRope
+
 proc assignGlobalVar(p: BProc, s: PSym) = 
   if s.loc.k == locNone: 
     fillLoc(s.loc, locGlobalVar, s.typ, mangleName(s), OnHeap)
+  
+  if lfDynamicLib in s.loc.flags:
+    var q = findPendingModule(p.module, s)
+    if q != nil and not ContainsOrIncl(q.declaredThings, s.id): 
+      VarInDynamicLib(q, s)
+    else:
+      s.loc.r = mangleDynLibProc(s)
+    return
   useHeader(p.module, s)
   if lfNoDecl in s.loc.flags: return
   if sfThread in s.flags: 
@@ -537,6 +548,21 @@ proc SymInDynamicLib(m: BModule, sym: PSym) =
       "$1 = linkonce global $2 zeroinitializer$n", 
       [sym.loc.r, getTypeDesc(m, sym.loc.t)])
 
+proc VarInDynamicLib(m: BModule, sym: PSym) = 
+  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], 
+      "$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",
+      [sym.loc.r, getTypeDesc(m, sym.loc.t)])
+
 proc SymInDynamicLibPartial(m: BModule, sym: PSym) =
   sym.loc.r = mangleDynLibProc(sym)
   sym.typ.sym = nil           # generate a new name
@@ -751,6 +777,7 @@ proc genVarPrototype(m: BModule, sym: PSym) =
     else:
       app(m.s[cfsVars], "extern ")
       app(m.s[cfsVars], getTypeDesc(m, sym.loc.t))
+      if lfDynamicLib in sym.loc.flags: app(m.s[cfsVars], "*")
       if sfRegister in sym.flags: app(m.s[cfsVars], " register")
       if sfVolatile in sym.flags: app(m.s[cfsVars], " volatile")
       appf(m.s[cfsVars], " $1;$n", [sym.loc.r])