mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-03 03:32:32 +00:00
Simplify cgen forward proc (#9635)
* cgen: remove module resetting (unused) * cgen: keep single list of forwarded modules fewer counters to keep track of * cgen: fix module lookup
This commit is contained in:
@@ -45,8 +45,7 @@ when options.hasTinyCBackend:
|
||||
# implementation
|
||||
|
||||
proc addForwardedProc(m: BModule, prc: PSym) =
|
||||
m.forwardedProcs.add(prc)
|
||||
inc(m.g.forwardedProcsCounter)
|
||||
m.g.forwardedProcs.add(prc)
|
||||
|
||||
proc findPendingModule(m: BModule, s: PSym): BModule =
|
||||
var ms = getModule(s)
|
||||
@@ -1394,7 +1393,6 @@ proc rawNewModule(g: BModuleList; module: PSym, filename: AbsoluteFile): BModule
|
||||
result.preInitProc = newPreInitProc(result)
|
||||
initNodeTable(result.dataCache)
|
||||
result.typeStack = @[]
|
||||
result.forwardedProcs = @[]
|
||||
result.typeNodesName = getTempName(result)
|
||||
result.nimTypesName = getTempName(result)
|
||||
# no line tracing for the init sections of the system module so that we
|
||||
@@ -1410,49 +1408,6 @@ proc nullify[T](arr: var T) =
|
||||
for i in low(arr)..high(arr):
|
||||
arr[i] = Rope(nil)
|
||||
|
||||
proc resetModule*(m: BModule) =
|
||||
# between two compilations in CAAS mode, we can throw
|
||||
# away all the data that was written to disk
|
||||
m.headerFiles = @[]
|
||||
m.declaredProtos = initIntSet()
|
||||
m.forwTypeCache = initTable[SigHash, Rope]()
|
||||
m.initProc = newProc(nil, m)
|
||||
m.initProc.options = initProcOptions(m)
|
||||
m.preInitProc = newPreInitProc(m)
|
||||
initNodeTable(m.dataCache)
|
||||
m.typeStack = @[]
|
||||
m.forwardedProcs = @[]
|
||||
m.typeNodesName = getTempName(m)
|
||||
m.nimTypesName = getTempName(m)
|
||||
if sfSystemModule in m.module.flags:
|
||||
incl m.flags, preventStackTrace
|
||||
else:
|
||||
excl m.flags, preventStackTrace
|
||||
nullify m.s
|
||||
m.typeNodes = 0
|
||||
m.nimTypes = 0
|
||||
nullify m.extensionLoaders
|
||||
|
||||
# indicate that this is now cached module
|
||||
# the cache will be invalidated by nullifying gModules
|
||||
#m.fromCache = true
|
||||
m.g = nil
|
||||
|
||||
# we keep only the "merge info" information for the module
|
||||
# and the properties that can't change:
|
||||
# m.filename
|
||||
# m.cfilename
|
||||
# m.isHeaderFile
|
||||
# m.module ?
|
||||
# m.typeCache
|
||||
# m.declaredThings
|
||||
# m.typeInfoMarker
|
||||
# m.labels
|
||||
# m.FrameDeclared
|
||||
|
||||
proc resetCgenModules*(g: BModuleList) =
|
||||
for m in cgenModules(g): resetModule(m)
|
||||
|
||||
proc rawNewModule(g: BModuleList; module: PSym; conf: ConfigRef): BModule =
|
||||
result = rawNewModule(g, module, AbsoluteFile toFullPath(conf, module.position.FileIndex))
|
||||
|
||||
@@ -1531,20 +1486,6 @@ proc myProcess(b: PPassContext, n: PNode): PNode =
|
||||
let tranformed_n = transformStmt(m.g.graph, m.module, n)
|
||||
genStmts(m.initProc, tranformed_n)
|
||||
|
||||
proc finishModule(m: BModule) =
|
||||
var i = 0
|
||||
while i <= high(m.forwardedProcs):
|
||||
# Note: ``genProc`` may add to ``m.forwardedProcs``, so we cannot use
|
||||
# a ``for`` loop here
|
||||
var prc = m.forwardedProcs[i]
|
||||
if sfForward in prc.flags:
|
||||
internalError(m.config, prc.info, "still forwarded: " & prc.name.s)
|
||||
genProcNoForward(m, prc)
|
||||
inc(i)
|
||||
assert(m.g.forwardedProcsCounter >= i)
|
||||
dec(m.g.forwardedProcsCounter, i)
|
||||
setLen(m.forwardedProcs, 0)
|
||||
|
||||
proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool =
|
||||
result = true
|
||||
if optForceFullMake notin m.config.globalOptions:
|
||||
@@ -1640,12 +1581,26 @@ proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
|
||||
registerModuleToMain(m.g, m.module)
|
||||
|
||||
if sfMainModule in m.module.flags:
|
||||
if m.g.forwardedProcsCounter == 0:
|
||||
if m.g.forwardedProcs.len == 0:
|
||||
incl m.flags, objHasKidsValid
|
||||
let disp = generateMethodDispatchers(graph)
|
||||
for x in disp: genProcAux(m, x.sym)
|
||||
genMainProc(m)
|
||||
|
||||
proc genForwardedProcs(g: BModuleList) =
|
||||
# Forward declared proc:s lack bodies when first encountered, so they're given
|
||||
# a second pass here
|
||||
# Note: ``genProcNoForward`` may add to ``forwardedProcs``
|
||||
while g.forwardedProcs.len > 0:
|
||||
let
|
||||
prc = g.forwardedProcs.pop()
|
||||
ms = getModule(prc)
|
||||
m = g.modules[ms.position]
|
||||
if sfForward in prc.flags:
|
||||
internalError(m.config, prc.info, "still forwarded: " & prc.name.s)
|
||||
|
||||
genProcNoForward(m, prc)
|
||||
|
||||
proc cgenWriteModules*(backend: RootRef, config: ConfigRef) =
|
||||
let g = BModuleList(backend)
|
||||
# we need to process the transitive closure because recursive module
|
||||
@@ -1655,10 +1610,9 @@ proc cgenWriteModules*(backend: RootRef, config: ConfigRef) =
|
||||
let (outDir, _, _) = splitFile(config.outfile)
|
||||
if not outDir.isEmpty:
|
||||
createDir(outDir)
|
||||
if g.generatedHeader != nil: finishModule(g.generatedHeader)
|
||||
while g.forwardedProcsCounter > 0:
|
||||
for m in cgenModules(g):
|
||||
finishModule(m)
|
||||
|
||||
genForwardedProcs(g)
|
||||
|
||||
for m in cgenModules(g):
|
||||
m.writeModule(pending=true)
|
||||
writeMapping(config, g.mapping)
|
||||
|
||||
@@ -112,7 +112,7 @@ type
|
||||
mainModProcs*, mainModInit*, otherModsInit*, mainDatInit*: Rope
|
||||
mapping*: Rope # the generated mapping file (if requested)
|
||||
modules*: seq[BModule] # list of all compiled modules
|
||||
forwardedProcsCounter*: int
|
||||
forwardedProcs*: seq[PSym] # proc:s that did not yet have a body
|
||||
generatedHeader*: BModule
|
||||
breakPointId*: int
|
||||
breakpoints*: Rope # later the breakpoints are inserted into the main proc
|
||||
@@ -150,7 +150,6 @@ type
|
||||
preInitProc*: BProc # code executed before the init proc
|
||||
typeStack*: TTypeSeq # used for type generation
|
||||
dataCache*: TNodeTable
|
||||
forwardedProcs*: TSymSeq # keep forwarded procs here
|
||||
typeNodes*, nimTypes*: int # used for type info generation
|
||||
typeNodesName*, nimTypesName*: Rope # used for type info generation
|
||||
labels*: Natural # for generating unique module-scope names
|
||||
@@ -188,12 +187,12 @@ proc newProc*(prc: PSym, module: BModule): BProc =
|
||||
result.sigConflicts = initCountTable[string]()
|
||||
|
||||
proc newModuleList*(g: ModuleGraph): BModuleList =
|
||||
BModuleList(modules: @[], typeInfoMarker: initTable[SigHash, Rope](), config: g.config,
|
||||
graph: g, nimtvDeps: @[], nimtvDeclared: initIntSet())
|
||||
BModuleList(typeInfoMarker: initTable[SigHash, Rope](), config: g.config,
|
||||
graph: g, nimtvDeclared: initIntSet())
|
||||
|
||||
iterator cgenModules*(g: BModuleList): BModule =
|
||||
for i in 0..high(g.modules):
|
||||
for m in g.modules:
|
||||
# ultimately, we are iterating over the file ids here.
|
||||
# some "files" won't have an associated cgen module (like stdin)
|
||||
# and we must skip over them.
|
||||
if g.modules[i] != nil: yield g.modules[i]
|
||||
if m != nil: yield m
|
||||
|
||||
Reference in New Issue
Block a user