summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/cgen.nim34
-rw-r--r--tests/dll/nimhcr_0_3.nim1
-rw-r--r--tests/dll/nimhcr_2_1.nim2
-rw-r--r--tests/dll/nimhcr_integration.nim1
4 files changed, 32 insertions, 6 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 7fe33fc59..274f01ad7 100644
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -1166,21 +1166,43 @@ proc requestConstImpl(p: BProc, sym: PSym) =
   useHeader(m, sym)
   if sym.loc.k == locNone:
     fillLoc(sym.loc, locData, sym.ast, mangleName(p.module, sym), OnStatic)
+  if m.hcrOn: incl(sym.loc.flags, lfIndirect)
+
   if lfNoDecl in sym.loc.flags: return
   # declare implementation:
   var q = findPendingModule(m, sym)
   if q != nil and not containsOrIncl(q.declaredThings, sym.id):
     assert q.initProc.module == q
+    # add a suffix for hcr - will later init the global pointer with this data
+    let actualConstName = if m.hcrOn: sym.loc.r & "_const" else: sym.loc.r
     q.s[cfsData].addf("N_LIB_PRIVATE NIM_CONST $1 $2 = $3;$n",
-        [getTypeDesc(q, sym.typ), sym.loc.r, genBracedInit(q.initProc, sym.ast, isConst = true)])
+        [getTypeDesc(q, sym.typ), actualConstName, genBracedInit(q.initProc, sym.ast, isConst = true)])
+    if m.hcrOn:
+      # generate the global pointer with the real name
+      q.s[cfsVars].addf("static $1* $2;$n", [getTypeDesc(m, sym.loc.t), sym.loc.r])
+      # register it (but ignore the boolean result of hcrRegisterGlobal)
+      q.initProc.procSec(cpsLocals).addf(
+        "\thcrRegisterGlobal($1, \"$2\", sizeof($3), NULL, (void**)&$2);$n",
+        [getModuleDllPath(q, sym), sym.loc.r, rdLoc(sym.loc)])
+      # always copy over the contents of the actual constant with the _const
+      # suffix ==> this means that the constant is reloadable & updatable!
+      q.initProc.procSec(cpsLocals).add(ropecg(q,
+        "\t#nimCopyMem((void*)$1, (NIM_CONST void*)&$2, sizeof($3));$n",
+        [sym.loc.r, actualConstName, rdLoc(sym.loc)]))
   # declare header:
   if q != m and not containsOrIncl(m.declaredThings, sym.id):
     assert(sym.loc.r != nil)
-    let headerDecl = "extern NIM_CONST $1 $2;$n" %
-        [getTypeDesc(m, sym.loc.t), sym.loc.r]
-    m.s[cfsData].add(headerDecl)
-    if sfExportc in sym.flags and p.module.g.generatedHeader != nil:
-      p.module.g.generatedHeader.s[cfsData].add(headerDecl)
+    if m.hcrOn:
+      m.s[cfsVars].addf("static $1* $2;$n", [getTypeDesc(m, sym.loc.t), sym.loc.r]);
+      m.initProc.procSec(cpsLocals).addf(
+        "\t$1 = ($2*)hcrGetGlobal($3, \"$1\");$n", [sym.loc.r,
+        getTypeDesc(m, sym.loc.t), getModuleDllPath(q, sym)])
+    else:
+      let headerDecl = "extern NIM_CONST $1 $2;$n" %
+          [getTypeDesc(m, sym.loc.t), sym.loc.r]
+      m.s[cfsData].add(headerDecl)
+      if sfExportc in sym.flags and p.module.g.generatedHeader != nil:
+        p.module.g.generatedHeader.s[cfsData].add(headerDecl)
 
 proc isActivated(prc: PSym): bool = prc.typ != nil
 
diff --git a/tests/dll/nimhcr_0_3.nim b/tests/dll/nimhcr_0_3.nim
index 56f66e08c..183424e11 100644
--- a/tests/dll/nimhcr_0_3.nim
+++ b/tests/dll/nimhcr_0_3.nim
@@ -14,5 +14,6 @@ let c = makeCounter()
 afterCodeReload:
   echo "   0: after - closure iterator: ", c()
   echo "   0: after - closure iterator: ", c()
+  echo "   0: after - c_2 = ", c_2
 
 proc getInt*(): int = return g_1 + g_2.len
diff --git a/tests/dll/nimhcr_2_1.nim b/tests/dll/nimhcr_2_1.nim
index faafb1f76..705ed6d5a 100644
--- a/tests/dll/nimhcr_2_1.nim
+++ b/tests/dll/nimhcr_2_1.nim
@@ -7,6 +7,8 @@ type
 
 let g_2* = @[Type2(data: 2), Type2(data: 3)][1..^1] # should have a length of 1
 
+const c_2* = [1, 2, 3] # testing that a complext const object is properly exported
+
 var a: tuple[str: string, i: int]
 a.str = "   2: random string"
 echo a.str
diff --git a/tests/dll/nimhcr_integration.nim b/tests/dll/nimhcr_integration.nim
index 3f73341be..f6c6d21bc 100644
--- a/tests/dll/nimhcr_integration.nim
+++ b/tests/dll/nimhcr_integration.nim
@@ -36,6 +36,7 @@ max mutual recursion reached!
 bar
    0: after - closure iterator: 0
    0: after - closure iterator: 1
+   0: after - c_2 = [1, 2, 3]
 main: after
               The answer is: 9
 main: hasAnyModuleChanged? true