diff options
author | Viktor Kirilov <vik.kirilov@gmail.com> | 2020-04-25 21:17:33 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-25 20:17:33 +0200 |
commit | 018e297d66f817ef11c409df1ddf7241b451bf09 (patch) | |
tree | f3f7c2a593543175604e5f17e98477eb3c03db74 /compiler | |
parent | 362c8964bfbc675efbb054ed2c81396697706309 (diff) | |
download | Nim-018e297d66f817ef11c409df1ddf7241b451bf09.tar.gz |
HCR: properly handling complex const objects in the codegen - fixes #13915 (#14115)
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/cgen.nim | 34 |
1 files changed, 28 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 |