From 4ba21f513ff71afb143a55f4b8cd033490e0efea Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 21 Apr 2021 22:07:39 +0200 Subject: [PATCH 1/9] preparations for IC+ORC --- compiler/ic/ic.nim | 19 +++++++++---------- compiler/ic/replayer.nim | 7 +++++++ compiler/modulegraphs.nim | 14 ++++++++------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/compiler/ic/ic.nim b/compiler/ic/ic.nim index d9a8756f174bf..dfb2d6a7360c8 100644 --- a/compiler/ic/ic.nim +++ b/compiler/ic/ic.nim @@ -40,11 +40,10 @@ type reexports*: seq[(LitId, PackedItemId)] compilerProcs*: seq[(LitId, int32)] converters*, methods*, trmacros*, pureEnums*: seq[int32] - macroUsages*: seq[(PackedItemId, PackedLineInfo)] typeInstCache*: seq[(PackedItemId, PackedItemId)] procInstCache*: seq[PackedInstantiation] - attachedOps*: seq[(TTypeAttachedOp, PackedItemId, PackedItemId)] + attachedOps*: seq[(PackedItemId, TTypeAttachedOp, PackedItemId)] methodsPerType*: seq[(PackedItemId, int, PackedItemId)] enumToStringProcs*: seq[(PackedItemId, PackedItemId)] @@ -523,6 +522,14 @@ proc toPackedGeneratedProcDef*(s: PSym, encoder: var PackedEncoder; m: var Packe toPackedProcDef(s.ast, m.topLevel, encoder, m) #flush encoder, m +proc storeAttachedProcDef*(t: PType; op: TTypeAttachedOp; s: PSym, + encoder: var PackedEncoder; m: var PackedModule) = + assert s.kind in routineKinds + #toPackedProcDef(s.ast, m.topLevel, encoder, m) + let tid = storeTypeLater(t, encoder, m) + let sid = storeSymLater(s, encoder, m) + m.attachedOps.add (tid, op, sid) + proc storeInstantiation*(c: var PackedEncoder; m: var PackedModule; s: PSym; i: PInstantiation) = var t = newSeq[PackedItemId](i.concreteTypes.len) for j in 0..high(i.concreteTypes): @@ -587,7 +594,6 @@ proc loadRodFile*(filename: AbsoluteFile; m: var PackedModule; config: ConfigRef loadSeqSection convertersSection, m.converters loadSeqSection methodsSection, m.methods loadSeqSection pureEnumsSection, m.pureEnums - loadSeqSection macroUsagesSection, m.macroUsages loadSeqSection toReplaySection, m.toReplay.nodes loadSeqSection topLevelSection, m.topLevel.nodes @@ -651,7 +657,6 @@ proc saveRodFile*(filename: AbsoluteFile; encoder: var PackedEncoder; m: var Pac storeSeqSection convertersSection, m.converters storeSeqSection methodsSection, m.methods storeSeqSection pureEnumsSection, m.pureEnums - storeSeqSection macroUsagesSection, m.macroUsages storeSeqSection toReplaySection, m.toReplay.nodes storeSeqSection topLevelSection, m.topLevel.nodes @@ -892,16 +897,10 @@ proc typeBodyFromPacked(c: var PackedDecoder; g: var PackedModuleGraph; t: PackedType; si, item: int32; result: PType) = result.sym = loadSym(c, g, si, t.sym) result.owner = loadSym(c, g, si, t.owner) - when false: - for op, item in pairs t.attachedOps: - result.attachedOps[op] = loadSym(c, g, si, item) result.typeInst = loadType(c, g, si, t.typeInst) for son in items t.types: result.sons.add loadType(c, g, si, son) loadAstBody(t, n) - when false: - for gen, id in items t.methods: - result.methods.add((gen, loadSym(c, g, si, id))) proc loadType(c: var PackedDecoder; g: var PackedModuleGraph; thisModule: int; t: PackedItemId): PType = if t == nilItemId: diff --git a/compiler/ic/replayer.nim b/compiler/ic/replayer.nim index 61aa0e697f6bc..d76dee6e205f6 100644 --- a/compiler/ic/replayer.nim +++ b/compiler/ic/replayer.nim @@ -117,6 +117,13 @@ proc replayGenericCacheInformation*(g: ModuleGraph; module: int) = let symId = FullId(module: tmp.module, packed: it[2]) g.methodsPerType.mgetOrPut(key, @[]).add (col, LazySym(id: symId, sym: nil)) + for it in mitems(g.packed[module].fromDisk.attachedOps): + let key = translateId(it[0], g.packed, module, g.config) + let op = it[1] + let tmp = translateId(it[2], g.packed, module, g.config) + let symId = FullId(module: tmp.module, packed: it[2]) + g.attachedOps[op][key] = LazySym(id: symId, sym: nil) + for it in mitems(g.packed[module].fromDisk.enumToStringProcs): let key = translateId(it[0], g.packed, module, g.config) let tmp = translateId(it[1], g.packed, module, g.config) diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 9df66269b8175..23d5f0a2f0b70 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -57,7 +57,7 @@ type typeInstCache*: Table[ItemId, seq[LazyType]] # A symbol's ItemId. procInstCache*: Table[ItemId, seq[LazyInstantiation]] # A symbol's ItemId. - attachedOps*: array[TTypeAttachedOp, Table[ItemId, PSym]] # Type ID, destructors, etc. + attachedOps*: array[TTypeAttachedOp, Table[ItemId, LazySym]] # Type ID, destructors, etc. methodsPerType*: Table[ItemId, seq[(int, LazySym)]] # Type ID, attached methods enumToStringProcs*: Table[ItemId, LazySym] emittedTypeInfo*: Table[string, FileIndex] @@ -292,22 +292,24 @@ iterator procInstCacheItems*(g: ModuleGraph; s: PSym): PInstantiation = proc getAttachedOp*(g: ModuleGraph; t: PType; op: TTypeAttachedOp): PSym = ## returns the requested attached operation for type `t`. Can return nil ## if no such operation exists. - result = g.attachedOps[op].getOrDefault(t.itemId) + if g.attachedOps[op].contains(t.itemId): + result = resolveSym(g, g.attachedOps[op][t.itemId]) + else: + result = nil proc setAttachedOp*(g: ModuleGraph; module: int; t: PType; op: TTypeAttachedOp; value: PSym) = ## we also need to record this to the packed module. - g.attachedOps[op][t.itemId] = value + g.attachedOps[op][t.itemId] = LazySym(sym: value) proc setAttachedOpPartial*(g: ModuleGraph; module: int; t: PType; op: TTypeAttachedOp; value: PSym) = ## we also need to record this to the packed module. - g.attachedOps[op][t.itemId] = value - # XXX Also add to the packed module! + g.attachedOps[op][t.itemId] = LazySym(sym: value) proc completePartialOp*(g: ModuleGraph; module: int; t: PType; op: TTypeAttachedOp; value: PSym) = if g.config.symbolFiles != disabledSf: assert module < g.encoders.len assert isActive(g.encoders[module]) - toPackedGeneratedProcDef(value, g.encoders[module], g.packed[module].fromDisk) + storeAttachedProcDef(t, op, value, g.encoders[module], g.packed[module].fromDisk) proc getToStringProc*(g: ModuleGraph; t: PType): PSym = result = resolveSym(g, g.enumToStringProcs[t.itemId]) From cc299e144af31dca6c32c9d361b6fbee73a8559f Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 23 Apr 2021 09:42:15 +0200 Subject: [PATCH 2/9] progress --- compiler/ccgexprs.nim | 4 ++++ compiler/ic/cbackend.nim | 5 ++++- compiler/ic/ic.nim | 4 ++-- compiler/ic/replayer.nim | 27 +++++++++++++++------------ compiler/ic/rodfiles.nim | 1 - compiler/modulegraphs.nim | 9 ++++++++- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index a42c30aae3dcb..f12beaa4ca515 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -2891,6 +2891,10 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = if useAliveDataFromDce in p.module.flags: if p.module.alive.contains(prc.itemId.item) and prc.magic in {mNone, mIsolate}: genProc(p.module, prc) + elif prc.name.s[0] == '=': + genProc(p.module, prc) + echo "came here for ", prc.name.s + elif prc.skipGenericOwner.kind == skModule and sfCompileTime notin prc.flags: if ({sfExportc, sfCompilerProc} * prc.flags == {sfExportc}) or (sfExportc in prc.flags and lfExportLib in prc.loc.flags) or diff --git a/compiler/ic/cbackend.nim b/compiler/ic/cbackend.nim index f5811cb3b4079..5e80ac1f16c16 100644 --- a/compiler/ic/cbackend.nim +++ b/compiler/ic/cbackend.nim @@ -23,7 +23,7 @@ import std/packedsets, algorithm, tables import ".."/[ast, options, lineinfos, modulegraphs, cgendata, cgen, pathutils, extccomp, msgs] -import packed_ast, ic, dce, rodfiles +import packed_ast, ic, dce, rodfiles, replayer proc unpackTree(g: ModuleGraph; thisModule: int; tree: PackedTree; n: NodePos): PNode = @@ -153,8 +153,11 @@ proc generateCode*(g: ModuleGraph) = of loading, stored: assert false of storing, outdated: + replayBackendProcs(g, i) setupBackendModule(g, g.packed[i]) of loaded: + replayBackendProcs(g, i) + # Even though this module didn't change, DCE might trigger a change. # Consider this case: Module A uses symbol S from B and B does not use # S itself. A is then edited not to use S either. Thus we have to diff --git a/compiler/ic/ic.nim b/compiler/ic/ic.nim index fb8b8c0241a37..4896c35c1d1f8 100644 --- a/compiler/ic/ic.nim +++ b/compiler/ic/ic.nim @@ -34,7 +34,6 @@ type toReplay*: PackedTree # pragmas and VM specific state to replay. topLevel*: PackedTree # top level statements bodies*: PackedTree # other trees. Referenced from typ.n and sym.ast by their position. - #producedGenerics*: Table[GenericKey, SymId] exports*: seq[(LitId, int32)] hidden*: seq[(LitId, int32)] reexports*: seq[(LitId, PackedItemId)] @@ -525,10 +524,11 @@ proc toPackedGeneratedProcDef*(s: PSym, encoder: var PackedEncoder; m: var Packe proc storeAttachedProcDef*(t: PType; op: TTypeAttachedOp; s: PSym, encoder: var PackedEncoder; m: var PackedModule) = assert s.kind in routineKinds - #toPackedProcDef(s.ast, m.topLevel, encoder, m) + assert isActive(encoder) let tid = storeTypeLater(t, encoder, m) let sid = storeSymLater(s, encoder, m) m.attachedOps.add (tid, op, sid) + toPackedGeneratedProcDef(s, encoder, m) proc storeInstantiation*(c: var PackedEncoder; m: var PackedModule; s: PSym; i: PInstantiation) = var t = newSeq[PackedItemId](i.concreteTypes.len) diff --git a/compiler/ic/replayer.nim b/compiler/ic/replayer.nim index d76dee6e205f6..845a94b6d7d88 100644 --- a/compiler/ic/replayer.nim +++ b/compiler/ic/replayer.nim @@ -86,6 +86,20 @@ proc replayStateChanges*(module: PSym; g: ModuleGraph) = else: internalAssert g.config, false +proc replayBackendProcs*(g: ModuleGraph; module: int) = + for it in mitems(g.packed[module].fromDisk.attachedOps): + let key = translateId(it[0], g.packed, module, g.config) + let op = it[1] + let tmp = translateId(it[2], g.packed, module, g.config) + let symId = FullId(module: tmp.module, packed: it[2]) + g.attachedOps[op][key] = LazySym(id: symId, sym: nil) + + for it in mitems(g.packed[module].fromDisk.enumToStringProcs): + let key = translateId(it[0], g.packed, module, g.config) + let tmp = translateId(it[1], g.packed, module, g.config) + let symId = FullId(module: tmp.module, packed: it[1]) + g.enumToStringProcs[key] = LazySym(id: symId, sym: nil) + proc replayGenericCacheInformation*(g: ModuleGraph; module: int) = ## We remember the generic instantiations a module performed ## in order to to avoid the code bloat that generic code tends @@ -117,18 +131,7 @@ proc replayGenericCacheInformation*(g: ModuleGraph; module: int) = let symId = FullId(module: tmp.module, packed: it[2]) g.methodsPerType.mgetOrPut(key, @[]).add (col, LazySym(id: symId, sym: nil)) - for it in mitems(g.packed[module].fromDisk.attachedOps): - let key = translateId(it[0], g.packed, module, g.config) - let op = it[1] - let tmp = translateId(it[2], g.packed, module, g.config) - let symId = FullId(module: tmp.module, packed: it[2]) - g.attachedOps[op][key] = LazySym(id: symId, sym: nil) - - for it in mitems(g.packed[module].fromDisk.enumToStringProcs): - let key = translateId(it[0], g.packed, module, g.config) - let tmp = translateId(it[1], g.packed, module, g.config) - let symId = FullId(module: tmp.module, packed: it[1]) - g.enumToStringProcs[key] = LazySym(id: symId, sym: nil) + replayBackendProcs(g, module) for it in mitems(g.packed[module].fromDisk.methods): let sym = loadSymFromId(g.config, g.cache, g.packed, module, diff --git a/compiler/ic/rodfiles.nim b/compiler/ic/rodfiles.nim index a52bd18b33a10..4276c8373893e 100644 --- a/compiler/ic/rodfiles.nim +++ b/compiler/ic/rodfiles.nim @@ -82,7 +82,6 @@ type convertersSection methodsSection pureEnumsSection - macroUsagesSection toReplaySection topLevelSection bodiesSection diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index c34345540e911..e52f17c28cc5f 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -266,6 +266,13 @@ proc resolveSym(g: ModuleGraph; t: var LazySym): PSym = t.sym = result assert result != nil +proc resolveAttachedOp(g: ModuleGraph; t: var LazySym): PSym = + result = t.sym + if result == nil: + result = loadSymFromId(g.config, g.cache, g.packed, t.id.module, t.id.packed) + t.sym = result + assert result != nil + proc resolveInst(g: ModuleGraph; t: var LazyInstantiation): PInstantiation = result = t.inst if result == nil and isCachedModule(g, t.module): @@ -293,7 +300,7 @@ proc getAttachedOp*(g: ModuleGraph; t: PType; op: TTypeAttachedOp): PSym = ## returns the requested attached operation for type `t`. Can return nil ## if no such operation exists. if g.attachedOps[op].contains(t.itemId): - result = resolveSym(g, g.attachedOps[op][t.itemId]) + result = resolveAttachedOp(g, g.attachedOps[op][t.itemId]) else: result = nil From 2bc817532b6e41edcaee32558c54e66615eb7834 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 23 Apr 2021 14:11:00 +0200 Subject: [PATCH 3/9] wip --- compiler/finalast.nim | 20 ++++++++++++++++++++ compiler/ic/cbackend.nim | 5 +---- compiler/int128.nim | 12 ------------ 3 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 compiler/finalast.nim diff --git a/compiler/finalast.nim b/compiler/finalast.nim new file mode 100644 index 0000000000000..052a0bf41ad18 --- /dev/null +++ b/compiler/finalast.nim @@ -0,0 +1,20 @@ +# +# +# The Nim Compiler +# (c) Copyright 2021 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## This module implements a tiny intermediate layer. + +import + ast, transf, + +proc finalProcBody*(prc: PSym; n: PNode): PNode = + ## Transformations after sem'checking that we need to do. + result = transformBody(m.g.graph, m.idgen, prc, cache = false) + if sfInjectDestructors in prc.flags: + result = injectDestructorCalls(m.g.graph, m.idgen, prc, result) + diff --git a/compiler/ic/cbackend.nim b/compiler/ic/cbackend.nim index 5e80ac1f16c16..f5811cb3b4079 100644 --- a/compiler/ic/cbackend.nim +++ b/compiler/ic/cbackend.nim @@ -23,7 +23,7 @@ import std/packedsets, algorithm, tables import ".."/[ast, options, lineinfos, modulegraphs, cgendata, cgen, pathutils, extccomp, msgs] -import packed_ast, ic, dce, rodfiles, replayer +import packed_ast, ic, dce, rodfiles proc unpackTree(g: ModuleGraph; thisModule: int; tree: PackedTree; n: NodePos): PNode = @@ -153,11 +153,8 @@ proc generateCode*(g: ModuleGraph) = of loading, stored: assert false of storing, outdated: - replayBackendProcs(g, i) setupBackendModule(g, g.packed[i]) of loaded: - replayBackendProcs(g, i) - # Even though this module didn't change, DCE might trigger a change. # Consider this case: Module A uses symbol S from B and B does not use # S itself. A is then edited not to use S either. Thus we have to diff --git a/compiler/int128.nim b/compiler/int128.nim index 6ba7c19611008..b3c8925b98a56 100644 --- a/compiler/int128.nim +++ b/compiler/int128.nim @@ -354,18 +354,6 @@ proc low64(a: Int128): uint64 = bitconcat(a.udata[1], a.udata[0]) proc `*`*(lhs, rhs: Int128): Int128 = - let - a = cast[uint64](lhs.udata[0]) - b = cast[uint64](lhs.udata[1]) - c = cast[uint64](lhs.udata[2]) - d = cast[uint64](lhs.udata[3]) - - e = cast[uint64](rhs.udata[0]) - f = cast[uint64](rhs.udata[1]) - g = cast[uint64](rhs.udata[2]) - h = cast[uint64](rhs.udata[3]) - - let a32 = cast[uint64](lhs.udata[1]) let a00 = cast[uint64](lhs.udata[0]) let b32 = cast[uint64](rhs.udata[1]) From ac87cb1b6da85deb6ff79adefe110c6b36d44eab Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 23 Apr 2021 16:10:37 +0200 Subject: [PATCH 4/9] WIP --- compiler/ast.nim | 1 + compiler/cgen.nim | 8 +++-- compiler/cgmeth.nim | 42 +------------------------ compiler/finalast.nim | 10 +++--- compiler/jsgen.nim | 9 ++++-- compiler/lambdalifting.nim | 6 ++-- compiler/main.nim | 2 +- compiler/passaux.nim | 2 +- compiler/sem.nim | 4 ++- compiler/sempass2.nim | 7 ++++- compiler/transf.nim | 64 +++++++++++++++++++++++++++++++++----- compiler/vm.nim | 13 +++++--- compiler/vmgen.nim | 4 +-- 13 files changed, 99 insertions(+), 73 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 02a2ae7a438ef..a85efcdf7f123 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -855,6 +855,7 @@ type #procInstCache*: seq[PInstantiation] gcUnsafetyReason*: PSym # for better error messages wrt gcsafe transformedBody*: PNode # cached body after transf pass + semcheckedBody*: PNode # proc body after semantic checking of skLet, skVar, skField, skForVar: guard*: PSym bitsize*: int diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 5d6de2a03f1be..43373475145c1 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1014,9 +1014,11 @@ proc genProcAux(m: BModule, prc: PSym) = var returnStmt: Rope = nil assert(prc.ast != nil) - var procBody = transformBody(m.g.graph, m.idgen, prc, cache = false) - if sfInjectDestructors in prc.flags: - procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody) + when false: + var procBody = transformBody(m.g.graph, m.idgen, prc, cache = false) + if sfInjectDestructors in prc.flags: + procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody) + let procBody = getBody(m.g.graph, prc) if sfPure notin prc.flags and prc.typ[0] != nil: if resultPos >= prc.ast.len: diff --git a/compiler/cgmeth.nim b/compiler/cgmeth.nim index 484bc9d976006..b3589d42aa0b1 100644 --- a/compiler/cgmeth.nim +++ b/compiler/cgmeth.nim @@ -11,47 +11,7 @@ import intsets, options, ast, msgs, idents, renderer, types, magicsys, - sempass2, strutils, modulegraphs, lineinfos - -proc genConv(n: PNode, d: PType, downcast: bool; conf: ConfigRef): PNode = - var dest = skipTypes(d, abstractPtrs) - var source = skipTypes(n.typ, abstractPtrs) - if (source.kind == tyObject) and (dest.kind == tyObject): - var diff = inheritanceDiff(dest, source) - if diff == high(int): - # no subtype relation, nothing to do - result = n - elif diff < 0: - result = newNodeIT(nkObjUpConv, n.info, d) - result.add n - if downcast: internalError(conf, n.info, "cgmeth.genConv: no upcast allowed") - elif diff > 0: - result = newNodeIT(nkObjDownConv, n.info, d) - result.add n - if not downcast: - internalError(conf, n.info, "cgmeth.genConv: no downcast allowed") - else: - result = n - else: - result = n - -proc getDispatcher*(s: PSym): PSym = - ## can return nil if is has no dispatcher. - if dispatcherPos < s.ast.len: - result = s.ast[dispatcherPos].sym - doAssert sfDispatcher in result.flags - -proc methodCall*(n: PNode; conf: ConfigRef): PNode = - result = n - # replace ordinary method by dispatcher method: - let disp = getDispatcher(result[0].sym) - if disp != nil: - result[0].sym = disp - # change the arguments to up/downcasts to fit the dispatcher's parameters: - for i in 1.. 0: + result = newNodeIT(nkObjDownConv, n.info, d) + result.add n + if not downcast: + internalError(conf, n.info, "cgmeth.genConv: no downcast allowed") + else: + result = n + else: + result = n + +proc getDispatcher*(s: PSym): PSym = + ## can return nil if is has no dispatcher. + if dispatcherPos < s.ast.len: + result = s.ast[dispatcherPos].sym + doAssert sfDispatcher in result.flags + +proc methodCall*(n: PNode; conf: ConfigRef): PNode = + result = n + # replace ordinary method by dispatcher method: + let disp = getDispatcher(result[0].sym) + if disp != nil: + result[0].sym = disp + # change the arguments to up/downcasts to fit the dispatcher's parameters: + for i in 1.. Date: Sat, 24 Apr 2021 06:42:04 +0200 Subject: [PATCH 5/9] removed debug leftover --- compiler/ccgexprs.nim | 4 ---- 1 file changed, 4 deletions(-) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index f12beaa4ca515..a42c30aae3dcb 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -2891,10 +2891,6 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = if useAliveDataFromDce in p.module.flags: if p.module.alive.contains(prc.itemId.item) and prc.magic in {mNone, mIsolate}: genProc(p.module, prc) - elif prc.name.s[0] == '=': - genProc(p.module, prc) - echo "came here for ", prc.name.s - elif prc.skipGenericOwner.kind == skModule and sfCompileTime notin prc.flags: if ({sfExportc, sfCompilerProc} * prc.flags == {sfExportc}) or (sfExportc in prc.flags and lfExportLib in prc.loc.flags) or From 524d3ccd57e6ee7a2b8aa4e5b812dd41f42156f9 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 26 Apr 2021 15:10:20 +0200 Subject: [PATCH 6/9] progress --- compiler/ast.nim | 4 ++-- compiler/lambdalifting.nim | 9 ++++----- compiler/modulegraphs.nim | 4 ++++ compiler/transf.nim | 21 ++++++++++----------- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index a85efcdf7f123..c6ad7b9d102f5 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -854,7 +854,6 @@ type of routineKinds: #procInstCache*: seq[PInstantiation] gcUnsafetyReason*: PSym # for better error messages wrt gcsafe - transformedBody*: PNode # cached body after transf pass semcheckedBody*: PNode # proc body after semantic checking of skLet, skVar, skField, skForVar: guard*: PSym @@ -1637,7 +1636,8 @@ proc transitionGenericParamToType*(s: PSym) = proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) = transitionSymKindCommon(kind) s.gcUnsafetyReason = obj.gcUnsafetyReason - s.transformedBody = obj.transformedBody + s.semcheckedBody = obj.semcheckedBody + #s.transformedBody = obj.transformedBody proc transitionToLet*(s: PSym) = transitionSymKindCommon(skLet) diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim index 5e1258f108f9c..c7b62959d4624 100644 --- a/compiler/lambdalifting.nim +++ b/compiler/lambdalifting.nim @@ -734,11 +734,10 @@ proc liftCapturedVars(n: PNode; owner: PSym; d: var DetectionPass; c.inContainer = 0 var body = getBody(d.graph, s) #transformBody(d.graph, d.idgen, s, dontUseCache) body = liftCapturedVars(body, s, d, c) - if c.envVars.getOrDefault(s.id).isNil: - s.transformedBody = body - else: - s.transformedBody = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body) - finishClosureCreation(s, d, c, n.info, s.transformedBody) + if not c.envVars.getOrDefault(s.id).isNil: + body = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body) + finishClosureCreation(s, d, c, n.info, body) + setRoutineBody(d.graph, s, body) c.inContainer = oldInContainer if s.typ.callConv == ccClosure: diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index e52f17c28cc5f..2540f646c77a5 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -591,3 +591,7 @@ proc moduleFromRodFile*(g: ModuleGraph; fileIdx: FileIndex; proc configComplete*(g: ModuleGraph) = rememberStartupConfig(g.startupPackedConfig, g.config) + +proc setRoutineBody*(g: ModuleGraph; s: PSym; body: PNode) = + # XXX Check sanity of IC implementation for this here. + s.ast[bodyPos] = body diff --git a/compiler/transf.nim b/compiler/transf.nim index 2e89544f0f286..c6d5ef1e0043a 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -1135,12 +1135,10 @@ template liftDefer(c, root) = proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flag: TransformBodyFlag): PNode = assert prc.kind in routineKinds - if prc.transformedBody != nil: - result = prc.transformedBody - elif nfTransf in getBody(g, prc).flags or prc.kind in {skTemplate}: + if nfTransf in getBody(g, prc).flags or prc.kind in {skTemplate}: result = getBody(g, prc) else: - prc.transformedBody = newNode(nkEmpty) # protects from recursion + #prc.transformedBody = newNode(nkEmpty) # protects from recursion var c = openTransf(g, prc.getModule, "", idgen) result = liftLambdas(g, prc, getBody(g, prc), c.tooEarly, c.idgen) result = processTransf(c, result, prc) @@ -1152,13 +1150,14 @@ proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flag: Transfo incl(result.flags, nfTransf) - if flag == useCache or prc.typ.callConv == ccInline: - # genProc for inline procs will be called multiple times from different modules, - # it is important to transform exactly once to get sym ids and locations right - prc.transformedBody = result - else: - prc.transformedBody = nil - # XXX Rodfile support for transformedBody! + when false: + if flag == useCache or prc.typ.callConv == ccInline: + # genProc for inline procs will be called multiple times from different modules, + # it is important to transform exactly once to get sym ids and locations right + prc.transformedBody = result + else: + prc.transformedBody = nil + # XXX Rodfile support for transformedBody! #if prc.name.s == "main": # echo "transformed into ", renderTree(result, {renderIds}) From 096d5066f27c6489100cace264597687bde66973 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 26 Apr 2021 16:01:05 +0200 Subject: [PATCH 7/9] bootstrapping works again --- compiler/main.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/main.nim b/compiler/main.nim index 265e5385d036f..ca1ab79d9b77f 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -19,7 +19,8 @@ import cgen, json, nversion, nimconf, passaux, depends, vm, modules, - modulegraphs, tables, lineinfos, pathutils, vmprofiler + modulegraphs, tables, lineinfos, pathutils, vmprofiler, + platform import ic / [cbackend, integrity, navigator] from ic / ic import rodViewer From 0059ab08fbeed484c53c00bc9ba6386581f35cb3 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 30 Apr 2021 10:25:27 +0200 Subject: [PATCH 8/9] progress --- compiler/enumtostr.nim | 2 +- compiler/injectdestructors.nim | 2 +- compiler/modulegraphs.nim | 4 ++++ compiler/semtypes.nim | 2 +- compiler/transf.nim | 9 +++++++++ compiler/vmgen.nim | 6 ++++-- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/compiler/enumtostr.nim b/compiler/enumtostr.nim index 9bfa7001a9d88..246654332d217 100644 --- a/compiler/enumtostr.nim +++ b/compiler/enumtostr.nim @@ -26,7 +26,7 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGener assert(t.n[i].kind == nkSym) var field = t.n[i].sym let val = if field.ast == nil: field.name.s else: field.ast.strVal - caseStmt.add newTree(nkOfBranch, newSymNode(field), + caseStmt.add newTree(nkOfBranch, newIntTypeNode(field.position, t), newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res), newStrNode(val, info)))) #newIntTypeNode(nkIntLit, field.position, t) diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 152efd8a1dd7f..bbd80f977ef6e 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -964,7 +964,7 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode): PNode = result[0] = p(n[0], c, s, mode) for i in 1.. Date: Mon, 28 Mar 2022 23:30:57 +0200 Subject: [PATCH 9/9] undo the refactorings; doesn't help though --- compiler/ast.nim | 5 ++++- compiler/cgen.nim | 7 ++++--- compiler/jsgen.nim | 8 ++++---- compiler/lambdalifting.nim | 29 ++++++++++++++++++++++------- compiler/semtypes.nim | 2 +- compiler/transf.nim | 30 ++++++++++++++++++++---------- compiler/vm.nim | 9 +++++---- compiler/vmgen.nim | 5 ++++- 8 files changed, 64 insertions(+), 31 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 446b2cd5616eb..0b10d38c7e02b 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -867,6 +867,8 @@ type #procInstCache*: seq[PInstantiation] gcUnsafetyReason*: PSym # for better error messages regarding gcsafe semcheckedBody*: PNode # proc body after semantic checking + when not defined(nimOrcic): + transformedBody*: PNode # cached body after transf pass of skLet, skVar, skField, skForVar: guard*: PSym bitsize*: int @@ -1718,7 +1720,8 @@ proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) = transitionSymKindCommon(kind) s.gcUnsafetyReason = obj.gcUnsafetyReason s.semcheckedBody = obj.semcheckedBody - #s.transformedBody = obj.transformedBody + when not defined(nimOrcic): + s.transformedBody = obj.transformedBody proc transitionToLet*(s: PSym) = transitionSymKindCommon(skLet) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index b0899dce88b7f..c7cac8258acc0 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1034,11 +1034,12 @@ proc genProcAux(m: BModule, prc: PSym) = var returnStmt: Rope = nil assert(prc.ast != nil) - when false: - var procBody = transformBody(m.g.graph, m.idgen, prc, cache = false) + when not defined(nimOrcic): + var procBody = transformBody(m.g.graph, m.idgen, prc, dontUseCache) if sfInjectDestructors in prc.flags: procBody = injectDestructorCalls(m.g.graph, m.idgen, prc, procBody) - let procBody = getBody(m.g.graph, prc) + else: + let procBody = getBody(m.g.graph, prc) if sfPure notin prc.flags and prc.typ[0] != nil: if resultPos >= prc.ast.len: diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 22da73122a152..ebb7f94218764 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -2421,12 +2421,12 @@ proc genProc(oldProc: PProc, prc: PSym): Rope = else: returnStmt = "return $#;$n" % [a.res] - when false: - var transformedBody = transformBody(p.module.graph, p.module.idgen, prc, cache = false) + when not defined(nimOrcic): + var transformedBody = transformBody(p.module.graph, p.module.idgen, prc, dontUseCache) if sfInjectDestructors in prc.flags: transformedBody = injectDestructorCalls(p.module.graph, p.module.idgen, prc, transformedBody) - - let transformedBody = getBody(p.module.graph, prc) + else: + let transformedBody = getBody(p.module.graph, prc) p.nested: genStmt(p, transformedBody) diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim index 9dad2c4fe9875..c89aa1ecbd9a9 100644 --- a/compiler/lambdalifting.nim +++ b/compiler/lambdalifting.nim @@ -14,6 +14,9 @@ import idents, renderer, types, magicsys, lowerings, tables, modulegraphs, lineinfos, liftdestructors, typeallowed +when not defined(nimOrcic): + import transf + when defined(nimPreviewSlimSystem): import std/assertions @@ -437,7 +440,10 @@ proc detectCapturedVars(n: PNode; owner: PSym; c: var DetectionPass) = if innerProc: if s.isIterator: c.somethingToDo = true if not c.processed.containsOrIncl(s.id): - let body = getBody(c.graph, s) # transformBody(c.graph, c.idgen, s, useCache) + when not defined(nimOrcic): + let body = transformBody(c.graph, c.idgen, s, useCache) + else: + let body = getBody(c.graph, s) detectCapturedVars(body, s, c) let ow = s.skipGenericOwner if ow == owner: @@ -733,12 +739,21 @@ proc liftCapturedVars(n: PNode; owner: PSym; d: var DetectionPass; # echo renderTree(s.getBody, {renderIds}) let oldInContainer = c.inContainer c.inContainer = 0 - var body = getBody(d.graph, s) #transformBody(d.graph, d.idgen, s, dontUseCache) - body = liftCapturedVars(body, s, d, c) - if not c.envVars.getOrDefault(s.id).isNil: - body = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body) - finishClosureCreation(s, d, c, n.info, body) - setRoutineBody(d.graph, s, body) + when not defined(nimOrcic): + var body = transformBody(d.graph, d.idgen, s, dontUseCache) + body = liftCapturedVars(body, s, d, c) + if c.envVars.getOrDefault(s.id).isNil: + s.transformedBody = body + else: + s.transformedBody = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body) + finishClosureCreation(s, d, c, n.info, s.transformedBody) + else: + var body = getBody(d.graph, s) #transformBody(d.graph, d.idgen, s, dontUseCache) + body = liftCapturedVars(body, s, d, c) + if not c.envVars.getOrDefault(s.id).isNil: + body = newTree(nkStmtList, rawClosureCreation(s, d, c, n.info), body) + finishClosureCreation(s, d, c, n.info, body) + setRoutineBody(d.graph, s, body) c.inContainer = oldInContainer if s.typ.callConv == ccClosure: diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 4c447b57ac0be..b4f385fe615fa 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -2091,7 +2091,7 @@ proc processMagicType(c: PContext, m: PSym) = rawAddSon(m.typ, newTypeS(tyNone, c)) of mPNimrodNode: incl m.typ.flags, tfTriggersCompileTime - #incl m.typ.flags, tfCheckedForDestructor + incl m.typ.flags, tfCheckedForDestructor of mException: discard of mBuiltinType: case m.name.s diff --git a/compiler/transf.nim b/compiler/transf.nim index dc711a07613d7..99d798fbdc055 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -28,7 +28,9 @@ type TransformBodyFlag* = enum dontUseCache, useCache -#proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flag: TransformBodyFlag): PNode +when not defined(nimOrcic): + proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flag: TransformBodyFlag): PNode + when defined(nimPreviewSlimSystem): import std/assertions @@ -115,8 +117,9 @@ proc newAsgnStmt(c: PTransf, kind: TNodeKind, le: PNode, ri: PNode): PNode = proc transformSymAux(c: PTransf, n: PNode): PNode = let s = n.sym if s.typ != nil and s.typ.callConv == ccClosure: - #if s.kind in routineKinds: - # discard transformBody(c.graph, c.idgen, s, useCache) + when not defined(nimOrcic): + if s.kind in routineKinds: + discard transformBody(c.graph, c.idgen, s, useCache) if s.kind == skIterator: if c.tooEarly: return n else: return liftIterSym(c.graph, n, c.idgen, getCurrOwner(c)) @@ -712,9 +715,10 @@ proc transformFor(c: PTransf, n: PNode): PNode = newC.forLoopBody = loopBody # this can fail for 'nimsuggest' and 'check': if iter.kind != skIterator: return result - if c.graph.config.symbolFiles != disabledSf: - storeExpansion(c.graph.encoders[c.module.position], - c.graph.packed[c.module.position].fromDisk, call[0].info, iter) + when not defined(nimOrcic): + if c.graph.config.symbolFiles != disabledSf: + storeExpansion(c.graph.encoders[c.module.position], + c.graph.packed[c.module.position].fromDisk, call[0].info, iter) # generate access statements for the parameters (unless they are constant) pushTransCon(c, newC) @@ -752,7 +756,10 @@ proc transformFor(c: PTransf, n: PNode): PNode = stmtList.add(newAsgnStmt(c, nkFastAsgn, temp, arg)) idNodeTablePut(newC.mapping, formal, temp) - let body = getBody(c.graph, iter) # transformBody(c.graph, c.idgen, iter, useCache) + when not defined(nimOrcic): + let body = getBody(c.graph, iter) + else: + let body = transformBody(c.graph, c.idgen, iter, useCache) pushInfoContext(c.graph.config, n.info) inc(c.inlining) stmtList.add(transform(c, body)) @@ -1182,10 +1189,13 @@ template liftDefer(c, root) = proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flag: TransformBodyFlag): PNode = assert prc.kind in routineKinds - if nfTransf in getBody(g, prc).flags or prc.kind in {skTemplate}: + if prc.transformedBody != nil: + result = prc.transformedBody + elif nfTransf in getBody(g, prc).flags or prc.kind in {skTemplate}: result = getBody(g, prc) else: - #prc.transformedBody = newNode(nkEmpty) # protects from recursion + when not defined(nimOrcic): + prc.transformedBody = newNode(nkEmpty) # protects from recursion var c = openTransf(g, prc.getModule, "", idgen) result = liftLambdas(g, prc, getBody(g, prc), c.tooEarly, c.idgen) result = processTransf(c, result, prc) @@ -1197,7 +1207,7 @@ proc transformBody*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; flag: Transfo incl(result.flags, nfTransf) - when false: + when not defined(nimOrcic): if flag == useCache or prc.typ.callConv == ccInline: # genProc for inline procs will be called multiple times from different modules, # it is important to transform exactly once to get sym ids and locations right diff --git a/compiler/vm.nim b/compiler/vm.nim index 7b2f796226223..bd07c77369e2f 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -1135,7 +1135,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = if a.kind == nkVarTy: a = a[0] if a.kind == nkSym: regs[ra].node = if a.sym.ast.isNil: newNode(nkNilLit) - elif a.sym.kind in routineKinds: copyTree(a.sym.semcheckedBody) + #elif a.sym.kind in routineKinds: copyTree(a.sym.semcheckedBody) else: copyTree(a.sym.ast) regs[ra].node.flags.incl nfIsRef else: @@ -1148,13 +1148,14 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = if a.sym.ast.isNil: newNode(nkNilLit) else: - when false: + when not defined(nimOrcic): let ast = a.sym.ast.shallowCopy for i in 0..