From b97a7dbf3d1874b7dbf6b541ac57b27f8fbcd367 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 9 Oct 2018 19:24:03 +0200 Subject: [PATCH] Make the registered passes local to the ModuleGraph (#9259) Closes #9068 --- compiler/cgen.nim | 2 +- compiler/cgendata.nim | 4 +- compiler/depends.nim | 4 +- compiler/docgen2.nim | 4 +- compiler/jsgen.nim | 4 +- compiler/modulegraphs.nim | 13 +++++++ compiler/passaux.nim | 4 +- compiler/passes.nim | 81 ++++++++++++++++----------------------- compiler/sem.nim | 2 +- compiler/transf.nim | 2 +- compiler/vm.nim | 2 +- compiler/vmdef.nim | 2 +- 12 files changed, 60 insertions(+), 64 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 517e53947e..2500446011 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -18,7 +18,7 @@ import import strutils except `%` # collides with ropes.`%` -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext from lineinfos import warnGcMem, errXMustBeCompileTime, hintDependency, errGenerated, errCannotOpenFile import dynlib diff --git a/compiler/cgendata.nim b/compiler/cgendata.nim index 406ee050f4..0c6097fbe1 100644 --- a/compiler/cgendata.nim +++ b/compiler/cgendata.nim @@ -13,7 +13,7 @@ import ast, astalgo, ropes, passes, options, intsets, platform, sighashes, tables, ndi, lineinfos, pathutils -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext type TLabel* = Rope # for the C generator a label is just a rope @@ -132,7 +132,7 @@ type # 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 PPassContext # represents a C source file s*: TCFileSections # sections of the C file flags*: set[Codegenflag] module*: PSym diff --git a/compiler/depends.nim b/compiler/depends.nim index c26593ea5a..d380d637a4 100644 --- a/compiler/depends.nim +++ b/compiler/depends.nim @@ -13,10 +13,10 @@ import os, options, ast, astalgo, msgs, ropes, idents, passes, modulepaths, pathutils -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext type - TGen = object of TPassContext + TGen = object of PPassContext module: PSym config: ConfigRef graph: ModuleGraph diff --git a/compiler/docgen2.nim b/compiler/docgen2.nim index 01acdd4ca4..2171a7194e 100644 --- a/compiler/docgen2.nim +++ b/compiler/docgen2.nim @@ -14,10 +14,10 @@ import os, options, ast, astalgo, msgs, ropes, idents, passes, docgen, lineinfos, pathutils -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext type - TGen = object of TPassContext + TGen = object of PPassContext doc: PDoc module: PSym PGen = ref TGen diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index aa23865260..277fe00f5f 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -34,10 +34,10 @@ import times, ropes, math, passes, ccgutils, wordrecg, renderer, intsets, cgmeth, lowerings, sighashes, lineinfos, rodutils, pathutils -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext type - TJSGen = object of TPassContext + TJSGen = object of PPassContext module: PSym graph: ModuleGraph config: ConfigRef diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 1eecc4176c..16704fab6f 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -62,6 +62,19 @@ type cacheSeqs*: Table[string, PNode] # state that is shared to suppor the 'macrocache' API cacheCounters*: Table[string, BiggestInt] cacheTables*: Table[string, BTree[string, PNode]] + passes*: seq[TPass] + + TPassContext* = object of RootObj # the pass's context + PPassContext* = ref TPassContext + + TPassOpen* = proc (graph: ModuleGraph; module: PSym): PPassContext {.nimcall.} + TPassClose* = proc (graph: ModuleGraph; p: PPassContext, n: PNode): PNode {.nimcall.} + TPassProcess* = proc (p: PPassContext, topLevelStmt: PNode): PNode {.nimcall.} + + TPass* = tuple[open: TPassOpen, + process: TPassProcess, + close: TPassClose, + isFrontend: bool] proc hash*(x: FileIndex): Hash {.borrow.} diff --git a/compiler/passaux.nim b/compiler/passaux.nim index eabce88222..09f656d58f 100644 --- a/compiler/passaux.nim +++ b/compiler/passaux.nim @@ -12,10 +12,10 @@ import strutils, ast, astalgo, passes, idents, msgs, options, idgen, lineinfos -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext type - VerboseRef = ref object of TPassContext + VerboseRef = ref object of PPassContext config: ConfigRef proc verboseOpen(graph: ModuleGraph; s: PSym): PPassContext = diff --git a/compiler/passes.nim b/compiler/passes.nim index 718b42c2ab..9ccd2240a3 100644 --- a/compiler/passes.nim +++ b/compiler/passes.nim @@ -16,21 +16,8 @@ import nimsets, syntaxes, times, idgen, modulegraphs, reorder, rod, lineinfos, pathutils - type - TPassContext* = object of RootObj # the pass's context - - PPassContext* = ref TPassContext - - TPassOpen* = proc (graph: ModuleGraph; module: PSym): PPassContext {.nimcall.} - TPassClose* = proc (graph: ModuleGraph; p: PPassContext, n: PNode): PNode {.nimcall.} - TPassProcess* = proc (p: PPassContext, topLevelStmt: PNode): PNode {.nimcall.} - - TPass* = tuple[open: TPassOpen, process: TPassProcess, close: TPassClose, - isFrontend: bool] - TPassData* = tuple[input: PNode, closeOutput: PNode] - TPasses* = openArray[TPass] # a pass is a tuple of procedure vars ``TPass.close`` may produce additional # nodes. These are passed to the other close procedures. @@ -57,16 +44,12 @@ const type TPassContextArray = array[0..maxPasses - 1, PPassContext] -var - gPasses: array[0..maxPasses - 1, TPass] - gPassesLen*: int - proc clearPasses*(g: ModuleGraph) = - gPassesLen = 0 + g.passes.setLen(0) proc registerPass*(g: ModuleGraph; p: TPass) = - gPasses[gPassesLen] = p - inc(gPassesLen) + internalAssert g.config, g.passes.len < maxPasses + g.passes.add(p) proc carryPass*(g: ModuleGraph; p: TPass, module: PSym; m: TPassData): TPassData = @@ -76,7 +59,7 @@ proc carryPass*(g: ModuleGraph; p: TPass, module: PSym; else: m.closeOutput proc carryPasses*(g: ModuleGraph; nodes: PNode, module: PSym; - passes: TPasses) = + passes: openArray[TPass]) = var passdata: TPassData passdata.input = nodes for pass in passes: @@ -84,23 +67,23 @@ proc carryPasses*(g: ModuleGraph; nodes: PNode, module: PSym; proc openPasses(g: ModuleGraph; a: var TPassContextArray; module: PSym) = - for i in countup(0, gPassesLen - 1): - if not isNil(gPasses[i].open): - a[i] = gPasses[i].open(g, module) + for i in countup(0, g.passes.len - 1): + if not isNil(g.passes[i].open): + a[i] = g.passes[i].open(g, module) else: a[i] = nil proc closePasses(graph: ModuleGraph; a: var TPassContextArray) = var m: PNode = nil - for i in countup(0, gPassesLen - 1): - if not isNil(gPasses[i].close): m = gPasses[i].close(graph, a[i], m) + for i in countup(0, graph.passes.len - 1): + if not isNil(graph.passes[i].close): m = graph.passes[i].close(graph, a[i], m) a[i] = nil # free the memory here -proc processTopLevelStmt(n: PNode, a: var TPassContextArray): bool = +proc processTopLevelStmt(graph: ModuleGraph, n: PNode, a: var TPassContextArray): bool = # this implements the code transformation pipeline var m = n - for i in countup(0, gPassesLen - 1): - if not isNil(gPasses[i].process): - m = gPasses[i].process(a[i], m) + for i in countup(0, graph.passes.len - 1): + if not isNil(graph.passes[i].process): + m = graph.passes[i].process(a[i], m) if isNil(m): return false result = true @@ -111,19 +94,19 @@ proc resolveMod(conf: ConfigRef; module, relativeTo: string): FileIndex = else: result = fileInfoIdx(conf, fullPath) -proc processImplicits(conf: ConfigRef; implicits: seq[string], nodeKind: TNodeKind, +proc processImplicits(graph: ModuleGraph; implicits: seq[string], nodeKind: TNodeKind, a: var TPassContextArray; m: PSym) = # XXX fixme this should actually be relative to the config file! let gCmdLineInfo = newLineInfo(FileIndex(0), 1, 1) - let relativeTo = toFullPath(conf, m.info) + let relativeTo = toFullPath(graph.config, m.info) for module in items(implicits): # implicit imports should not lead to a module importing itself - if m.position != resolveMod(conf, module, relativeTo).int32: + if m.position != resolveMod(graph.config, module, relativeTo).int32: var importStmt = newNodeI(nodeKind, gCmdLineInfo) var str = newStrNode(nkStrLit, module) str.info = gCmdLineInfo importStmt.addSon str - if not processTopLevelStmt(importStmt, a): break + if not processTopLevelStmt(graph, importStmt, a): break const imperativeCode = {low(TNodeKind)..high(TNodeKind)} - {nkTemplateDef, nkProcDef, nkMethodDef, @@ -139,25 +122,25 @@ proc processModule*(graph: ModuleGraph; module: PSym, stream: PLLStream): bool { fileIdx = module.fileIdx if module.id < 0: # new module caching mechanism: - for i in 0.. 0 if we are in inlining context (copy vars) diff --git a/compiler/vm.nim b/compiler/vm.nim index faff81697f..4255ba3fc4 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -24,7 +24,7 @@ import from semfold import leValueConv, ordinalValToString from evaltempl import evalTemplate -from modulegraphs import ModuleGraph +from modulegraphs import ModuleGraph, PPassContext when hasFFI: import evalffi diff --git a/compiler/vmdef.nim b/compiler/vmdef.nim index d642043dcf..17f48da07b 100644 --- a/compiler/vmdef.nim +++ b/compiler/vmdef.nim @@ -191,7 +191,7 @@ type VmCallback* = proc (args: VmArgs) {.closure.} PCtx* = ref TCtx - TCtx* = object of passes.TPassContext # code gen context + TCtx* = object of TPassContext # code gen context code*: seq[TInstr] debug*: seq[TLineInfo] # line info for every instruction; kept separate # to not slow down interpretation