remove the last global variables in the C code generator
This commit is contained in:
parent
e5281f9356
commit
5ad1acc60c
2 changed files with 19 additions and 21 deletions
|
|
@ -23,27 +23,14 @@ proc accessThreadLocalVar(p: BProc, s: PSym) =
|
||||||
add(p.procSec(cpsInit),
|
add(p.procSec(cpsInit),
|
||||||
ropecg(p.module, "\tNimTV_ = (NimThreadVars*) #GetThreadLocalVars();$n"))
|
ropecg(p.module, "\tNimTV_ = (NimThreadVars*) #GetThreadLocalVars();$n"))
|
||||||
|
|
||||||
var
|
|
||||||
nimtv: Rope # Nim thread vars; the struct body
|
|
||||||
nimtvDeps: seq[PType] = @[] # type deps: every module needs whole struct
|
|
||||||
nimtvDeclared = initIntSet() # so that every var/field exists only once
|
|
||||||
# in the struct
|
|
||||||
|
|
||||||
# 'nimtv' is incredibly hard to modularize! Best effort is to store all thread
|
|
||||||
# vars in a ROD section and with their type deps and load them
|
|
||||||
# unconditionally...
|
|
||||||
|
|
||||||
# nimtvDeps is VERY hard to cache because it's not a list of IDs nor can it be
|
|
||||||
# made to be one.
|
|
||||||
|
|
||||||
proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
|
proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
|
||||||
if emulatedThreadVars(m.config):
|
if emulatedThreadVars(m.config):
|
||||||
# we gather all thread locals var into a struct; we need to allocate
|
# we gather all thread locals var into a struct; we need to allocate
|
||||||
# storage for that somehow, can't use the thread local storage
|
# storage for that somehow, can't use the thread local storage
|
||||||
# allocator for it :-(
|
# allocator for it :-(
|
||||||
if not containsOrIncl(nimtvDeclared, s.id):
|
if not containsOrIncl(m.g.nimtvDeclared, s.id):
|
||||||
nimtvDeps.add(s.loc.t)
|
m.g.nimtvDeps.add(s.loc.t)
|
||||||
addf(nimtv, "$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r])
|
addf(m.g.nimtv, "$1 $2;$n", [getTypeDesc(m, s.loc.t), s.loc.r])
|
||||||
else:
|
else:
|
||||||
if isExtern: add(m.s[cfsVars], "extern ")
|
if isExtern: add(m.s[cfsVars], "extern ")
|
||||||
if optThreads in m.config.globalOptions: add(m.s[cfsVars], "NIM_THREADVAR ")
|
if optThreads in m.config.globalOptions: add(m.s[cfsVars], "NIM_THREADVAR ")
|
||||||
|
|
@ -51,12 +38,12 @@ proc declareThreadVar(m: BModule, s: PSym, isExtern: bool) =
|
||||||
addf(m.s[cfsVars], " $1;$n", [s.loc.r])
|
addf(m.s[cfsVars], " $1;$n", [s.loc.r])
|
||||||
|
|
||||||
proc generateThreadLocalStorage(m: BModule) =
|
proc generateThreadLocalStorage(m: BModule) =
|
||||||
if nimtv != nil and (usesThreadVars in m.flags or sfMainModule in m.module.flags):
|
if m.g.nimtv != nil and (usesThreadVars in m.flags or sfMainModule in m.module.flags):
|
||||||
for t in items(nimtvDeps): discard getTypeDesc(m, t)
|
for t in items(m.g.nimtvDeps): discard getTypeDesc(m, t)
|
||||||
addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [nimtv])
|
addf(m.s[cfsSeqTypes], "typedef struct {$1} NimThreadVars;$n", [m.g.nimtv])
|
||||||
|
|
||||||
proc generateThreadVarsSize(m: BModule) =
|
proc generateThreadVarsSize(m: BModule) =
|
||||||
if nimtv != nil:
|
if m.g.nimtv != nil:
|
||||||
let externc = if m.config.cmd == cmdCompileToCpp or
|
let externc = if m.config.cmd == cmdCompileToCpp or
|
||||||
sfCompileToCpp in m.module.flags: "extern \"C\" "
|
sfCompileToCpp in m.module.flags: "extern \"C\" "
|
||||||
else: ""
|
else: ""
|
||||||
|
|
|
||||||
|
|
@ -119,6 +119,17 @@ type
|
||||||
graph*: ModuleGraph
|
graph*: ModuleGraph
|
||||||
strVersion*, seqVersion*: int # version of the string/seq implementation to use
|
strVersion*, seqVersion*: int # version of the string/seq implementation to use
|
||||||
|
|
||||||
|
nimtv*: Rope # Nim thread vars; the struct body
|
||||||
|
nimtvDeps*: seq[PType] # type deps: every module needs whole struct
|
||||||
|
nimtvDeclared*: IntSet # so that every var/field exists only once
|
||||||
|
# in the struct
|
||||||
|
# 'nimtv' is incredibly hard to modularize! Best
|
||||||
|
# effort is to store all thread vars in a ROD
|
||||||
|
# section and with their type deps and load them
|
||||||
|
# unconditionally...
|
||||||
|
# nimtvDeps is VERY hard to cache because it's
|
||||||
|
# not a list of IDs nor can it be made to be one.
|
||||||
|
|
||||||
TCGen = object of TPassContext # represents a C source file
|
TCGen = object of TPassContext # represents a C source file
|
||||||
s*: TCFileSections # sections of the C file
|
s*: TCFileSections # sections of the C file
|
||||||
flags*: set[Codegenflag]
|
flags*: set[Codegenflag]
|
||||||
|
|
@ -177,7 +188,7 @@ proc newProc*(prc: PSym, module: BModule): BProc =
|
||||||
|
|
||||||
proc newModuleList*(g: ModuleGraph): BModuleList =
|
proc newModuleList*(g: ModuleGraph): BModuleList =
|
||||||
BModuleList(modules: @[], typeInfoMarker: initTable[SigHash, Rope](), config: g.config,
|
BModuleList(modules: @[], typeInfoMarker: initTable[SigHash, Rope](), config: g.config,
|
||||||
graph: g)
|
graph: g, nimtvDeps: @[], nimtvDeclared: initIntSet())
|
||||||
|
|
||||||
iterator cgenModules*(g: BModuleList): BModule =
|
iterator cgenModules*(g: BModuleList): BModule =
|
||||||
for i in 0..high(g.modules):
|
for i in 0..high(g.modules):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue